blob: 9a6fae3c3a7ea09cd70d64acc29996d4242f24ff [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)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000218 // "Tiramisu" needs to be in the next line for compatibility with soong code,
219 // not because of these tests specifically (it's not used by the tests)
220 variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900221 variables.Platform_vndk_version = proptools.StringPtr("29")
Paul Duffin37aad602021-03-08 09:47:16 +0000222 }),
223)
224
Paul Duffin52bfaa42021-03-23 23:40:12 +0000225var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
226 "system/sepolicy/apex/myapex-file_contexts": nil,
227})
228
Jooyung Han643adc42020-02-27 13:50:06 +0900229// ensure that 'result' equals 'expected'
230func ensureEquals(t *testing.T, result string, expected string) {
231 t.Helper()
232 if result != expected {
233 t.Errorf("%q != %q", expected, result)
234 }
235}
236
Jiyong Park25fc6a92018-11-18 18:02:45 +0900237// ensure that 'result' contains 'expected'
238func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900239 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900240 if !strings.Contains(result, expected) {
241 t.Errorf("%q is not found in %q", expected, result)
242 }
243}
244
Liz Kammer5bd365f2020-05-27 15:15:11 -0700245// ensure that 'result' contains 'expected' exactly one time
246func ensureContainsOnce(t *testing.T, result string, expected string) {
247 t.Helper()
248 count := strings.Count(result, expected)
249 if count != 1 {
250 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
251 }
252}
253
Jiyong Park25fc6a92018-11-18 18:02:45 +0900254// ensures that 'result' does not contain 'notExpected'
255func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900256 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900257 if strings.Contains(result, notExpected) {
258 t.Errorf("%q is found in %q", notExpected, result)
259 }
260}
261
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700262func ensureMatches(t *testing.T, result string, expectedRex string) {
263 ok, err := regexp.MatchString(expectedRex, result)
264 if err != nil {
265 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
266 return
267 }
268 if !ok {
269 t.Errorf("%s does not match regular expession %s", result, expectedRex)
270 }
271}
272
Jiyong Park25fc6a92018-11-18 18:02:45 +0900273func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900274 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900275 if !android.InList(expected, result) {
276 t.Errorf("%q is not found in %v", expected, result)
277 }
278}
279
280func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900281 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900282 if android.InList(notExpected, result) {
283 t.Errorf("%q is found in %v", notExpected, result)
284 }
285}
286
Jooyung Hane1633032019-08-01 17:41:43 +0900287func ensureListEmpty(t *testing.T, result []string) {
288 t.Helper()
289 if len(result) > 0 {
290 t.Errorf("%q is expected to be empty", result)
291 }
292}
293
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000294func ensureListNotEmpty(t *testing.T, result []string) {
295 t.Helper()
296 if len(result) == 0 {
297 t.Errorf("%q is expected to be not empty", result)
298 }
299}
300
Jiyong Park25fc6a92018-11-18 18:02:45 +0900301// Minimal test
302func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800303 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900304 apex_defaults {
305 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900306 manifest: ":myapex.manifest",
307 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900308 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900309 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900310 native_shared_libs: [
311 "mylib",
312 "libfoo.ffi",
313 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900314 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800315 multilib: {
316 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900317 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800318 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900319 },
Jiyong Park77acec62020-06-01 21:39:15 +0900320 java_libs: [
321 "myjar",
322 "myjar_dex",
323 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000324 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900325 }
326
Jiyong Park30ca9372019-02-07 16:27:23 +0900327 apex {
328 name: "myapex",
329 defaults: ["myapex-defaults"],
330 }
331
Jiyong Park25fc6a92018-11-18 18:02:45 +0900332 apex_key {
333 name: "myapex.key",
334 public_key: "testkey.avbpubkey",
335 private_key: "testkey.pem",
336 }
337
Jiyong Park809bb722019-02-13 21:33:49 +0900338 filegroup {
339 name: "myapex.manifest",
340 srcs: ["apex_manifest.json"],
341 }
342
343 filegroup {
344 name: "myapex.androidmanifest",
345 srcs: ["AndroidManifest.xml"],
346 }
347
Jiyong Park25fc6a92018-11-18 18:02:45 +0900348 cc_library {
349 name: "mylib",
350 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900351 shared_libs: [
352 "mylib2",
353 "libbar.ffi",
354 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900355 system_shared_libs: [],
356 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000357 // TODO: remove //apex_available:platform
358 apex_available: [
359 "//apex_available:platform",
360 "myapex",
361 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900362 }
363
Alex Light3d673592019-01-18 14:37:31 -0800364 cc_binary {
365 name: "foo",
366 srcs: ["mylib.cpp"],
367 compile_multilib: "both",
368 multilib: {
369 lib32: {
370 suffix: "32",
371 },
372 lib64: {
373 suffix: "64",
374 },
375 },
376 symlinks: ["foo_link_"],
377 symlink_preferred_arch: true,
378 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800379 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700380 apex_available: [ "myapex", "com.android.gki.*" ],
381 }
382
Jiyong Park99644e92020-11-17 22:21:02 +0900383 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000384 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900385 srcs: ["foo.rs"],
386 rlibs: ["libfoo.rlib.rust"],
387 dylibs: ["libfoo.dylib.rust"],
388 apex_available: ["myapex"],
389 }
390
391 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000392 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900393 srcs: ["foo.rs"],
394 crate_name: "foo",
395 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900396 shared_libs: ["libfoo.shared_from_rust"],
397 }
398
399 cc_library_shared {
400 name: "libfoo.shared_from_rust",
401 srcs: ["mylib.cpp"],
402 system_shared_libs: [],
403 stl: "none",
404 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900405 }
406
407 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000408 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900409 srcs: ["foo.rs"],
410 crate_name: "foo",
411 apex_available: ["myapex"],
412 }
413
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900414 rust_ffi_shared {
415 name: "libfoo.ffi",
416 srcs: ["foo.rs"],
417 crate_name: "foo",
418 apex_available: ["myapex"],
419 }
420
421 rust_ffi_shared {
422 name: "libbar.ffi",
423 srcs: ["foo.rs"],
424 crate_name: "bar",
425 apex_available: ["myapex"],
426 }
427
Yifan Hongd22a84a2020-07-28 17:37:46 -0700428 apex {
429 name: "com.android.gki.fake",
430 binaries: ["foo"],
431 key: "myapex.key",
432 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000433 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800434 }
435
Paul Duffindddd5462020-04-07 15:25:44 +0100436 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900437 name: "mylib2",
438 srcs: ["mylib.cpp"],
439 system_shared_libs: [],
440 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900441 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900442 static_libs: ["libstatic"],
443 // TODO: remove //apex_available:platform
444 apex_available: [
445 "//apex_available:platform",
446 "myapex",
447 ],
448 }
449
Paul Duffindddd5462020-04-07 15:25:44 +0100450 cc_prebuilt_library_shared {
451 name: "mylib2",
452 srcs: ["prebuilt.so"],
453 // TODO: remove //apex_available:platform
454 apex_available: [
455 "//apex_available:platform",
456 "myapex",
457 ],
458 }
459
Jiyong Park9918e1a2020-03-17 19:16:40 +0900460 cc_library_static {
461 name: "libstatic",
462 srcs: ["mylib.cpp"],
463 system_shared_libs: [],
464 stl: "none",
465 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000466 // TODO: remove //apex_available:platform
467 apex_available: [
468 "//apex_available:platform",
469 "myapex",
470 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900471 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900472
473 java_library {
474 name: "myjar",
475 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900476 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900477 sdk_version: "none",
478 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900479 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900480 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000481 // TODO: remove //apex_available:platform
482 apex_available: [
483 "//apex_available:platform",
484 "myapex",
485 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900486 }
487
Jiyong Park77acec62020-06-01 21:39:15 +0900488 dex_import {
489 name: "myjar_dex",
490 jars: ["prebuilt.jar"],
491 apex_available: [
492 "//apex_available:platform",
493 "myapex",
494 ],
495 }
496
Jiyong Park7f7766d2019-07-25 22:02:35 +0900497 java_library {
498 name: "myotherjar",
499 srcs: ["foo/bar/MyClass.java"],
500 sdk_version: "none",
501 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900502 // TODO: remove //apex_available:platform
503 apex_available: [
504 "//apex_available:platform",
505 "myapex",
506 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900507 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900508
509 java_library {
510 name: "mysharedjar",
511 srcs: ["foo/bar/MyClass.java"],
512 sdk_version: "none",
513 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900514 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900515 `)
516
Paul Duffina71a67a2021-03-29 00:42:57 +0100517 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900518
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900519 // Make sure that Android.mk is created
520 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700521 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900522 var builder strings.Builder
523 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
524
525 androidMk := builder.String()
526 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
527 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
528
Jiyong Park42cca6c2019-04-01 11:15:50 +0900529 optFlags := apexRule.Args["opt_flags"]
530 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700531 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100532 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900533
Jiyong Park25fc6a92018-11-18 18:02:45 +0900534 copyCmds := apexRule.Args["copy_commands"]
535
536 // Ensure that main rule creates an output
537 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
538
539 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700540 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
541 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
542 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900543 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900544 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900545
546 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700547 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
548 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900549 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
550 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900551 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900552 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900553
554 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800555 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
556 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900557 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900558 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900559 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900560 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
561 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900562 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900563 // .. but not for java libs
564 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900565 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800566
Colin Cross7113d202019-11-20 16:39:12 -0800567 // Ensure that the platform variant ends with _shared or _common
568 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
569 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900570 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
571 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900572 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
573
574 // Ensure that dynamic dependency to java libs are not included
575 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800576
577 // Ensure that all symlinks are present.
578 found_foo_link_64 := false
579 found_foo := false
580 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900581 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800582 if strings.HasSuffix(cmd, "bin/foo") {
583 found_foo = true
584 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
585 found_foo_link_64 = true
586 }
587 }
588 }
589 good := found_foo && found_foo_link_64
590 if !good {
591 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
592 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900593
Sundong Ahnabb64432019-10-22 13:58:29 +0900594 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700595 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900596 if len(noticeInputs) != 3 {
597 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900598 }
599 ensureListContains(t, noticeInputs, "NOTICE")
600 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900601 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900602
Artur Satayeva8bd1132020-04-27 18:07:06 +0100603 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100604 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100605 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
606 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
607 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100608
609 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100610 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100611 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
612 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
613 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800614}
615
Jooyung Hanf21c7972019-12-16 22:32:06 +0900616func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800617 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900618 apex_defaults {
619 name: "myapex-defaults",
620 key: "myapex.key",
621 prebuilts: ["myetc"],
622 native_shared_libs: ["mylib"],
623 java_libs: ["myjar"],
624 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900625 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800626 bpfs: ["bpf"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000627 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900628 }
629
630 prebuilt_etc {
631 name: "myetc",
632 src: "myprebuilt",
633 }
634
635 apex {
636 name: "myapex",
637 defaults: ["myapex-defaults"],
638 }
639
640 apex_key {
641 name: "myapex.key",
642 public_key: "testkey.avbpubkey",
643 private_key: "testkey.pem",
644 }
645
646 cc_library {
647 name: "mylib",
648 system_shared_libs: [],
649 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000650 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900651 }
652
653 java_library {
654 name: "myjar",
655 srcs: ["foo/bar/MyClass.java"],
656 sdk_version: "none",
657 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000658 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900659 }
660
661 android_app {
662 name: "AppFoo",
663 srcs: ["foo/bar/MyClass.java"],
664 sdk_version: "none",
665 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000666 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900667 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900668
669 runtime_resource_overlay {
670 name: "rro",
671 theme: "blue",
672 }
673
markchien2f59ec92020-09-02 16:23:38 +0800674 bpf {
675 name: "bpf",
676 srcs: ["bpf.c", "bpf2.c"],
677 }
678
Jooyung Hanf21c7972019-12-16 22:32:06 +0900679 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000680 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900681 "etc/myetc",
682 "javalib/myjar.jar",
683 "lib64/mylib.so",
684 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900685 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800686 "etc/bpf/bpf.o",
687 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900688 })
689}
690
Jooyung Han01a3ee22019-11-02 02:52:25 +0900691func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800692 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900693 apex {
694 name: "myapex",
695 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000696 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900697 }
698
699 apex_key {
700 name: "myapex.key",
701 public_key: "testkey.avbpubkey",
702 private_key: "testkey.pem",
703 }
704 `)
705
706 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900707 args := module.Rule("apexRule").Args
708 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
709 t.Error("manifest should be apex_manifest.pb, but " + manifest)
710 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900711}
712
Liz Kammer4854a7d2021-05-27 14:28:27 -0400713func TestApexManifestMinSdkVersion(t *testing.T) {
714 ctx := testApex(t, `
715 apex_defaults {
716 name: "my_defaults",
717 key: "myapex.key",
718 product_specific: true,
719 file_contexts: ":my-file-contexts",
720 updatable: false,
721 }
722 apex {
723 name: "myapex_30",
724 min_sdk_version: "30",
725 defaults: ["my_defaults"],
726 }
727
728 apex {
729 name: "myapex_current",
730 min_sdk_version: "current",
731 defaults: ["my_defaults"],
732 }
733
734 apex {
735 name: "myapex_none",
736 defaults: ["my_defaults"],
737 }
738
739 apex_key {
740 name: "myapex.key",
741 public_key: "testkey.avbpubkey",
742 private_key: "testkey.pem",
743 }
744
745 filegroup {
746 name: "my-file-contexts",
747 srcs: ["product_specific_file_contexts"],
748 }
749 `, withFiles(map[string][]byte{
750 "product_specific_file_contexts": nil,
751 }), android.FixtureModifyProductVariables(
752 func(variables android.FixtureProductVariables) {
753 variables.Unbundled_build = proptools.BoolPtr(true)
754 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
755 }), android.FixtureMergeEnv(map[string]string{
756 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
757 }))
758
759 testCases := []struct {
760 module string
761 minSdkVersion string
762 }{
763 {
764 module: "myapex_30",
765 minSdkVersion: "30",
766 },
767 {
768 module: "myapex_current",
769 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
770 },
771 {
772 module: "myapex_none",
773 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
774 },
775 }
776 for _, tc := range testCases {
777 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module+"_image")
778 args := module.Rule("apexRule").Args
779 optFlags := args["opt_flags"]
780 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
781 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
782 }
783 }
784}
785
Alex Light5098a612018-11-29 17:12:15 -0800786func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800787 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800788 apex {
789 name: "myapex",
790 key: "myapex.key",
791 payload_type: "zip",
792 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000793 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800794 }
795
796 apex_key {
797 name: "myapex.key",
798 public_key: "testkey.avbpubkey",
799 private_key: "testkey.pem",
800 }
801
802 cc_library {
803 name: "mylib",
804 srcs: ["mylib.cpp"],
805 shared_libs: ["mylib2"],
806 system_shared_libs: [],
807 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000808 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800809 }
810
811 cc_library {
812 name: "mylib2",
813 srcs: ["mylib.cpp"],
814 system_shared_libs: [],
815 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000816 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800817 }
818 `)
819
Sundong Ahnabb64432019-10-22 13:58:29 +0900820 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800821 copyCmds := zipApexRule.Args["copy_commands"]
822
823 // Ensure that main rule creates an output
824 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
825
826 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700827 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800828
829 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700830 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800831
832 // Ensure that both direct and indirect deps are copied into apex
833 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
834 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900835}
836
837func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800838 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900839 apex {
840 name: "myapex",
841 key: "myapex.key",
842 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900843 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000844 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900845 }
846
847 apex_key {
848 name: "myapex.key",
849 public_key: "testkey.avbpubkey",
850 private_key: "testkey.pem",
851 }
852
853 cc_library {
854 name: "mylib",
855 srcs: ["mylib.cpp"],
856 shared_libs: ["mylib2", "mylib3"],
857 system_shared_libs: [],
858 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000859 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900860 }
861
862 cc_library {
863 name: "mylib2",
864 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900865 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900866 system_shared_libs: [],
867 stl: "none",
868 stubs: {
869 versions: ["1", "2", "3"],
870 },
871 }
872
873 cc_library {
874 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900875 srcs: ["mylib.cpp"],
876 shared_libs: ["mylib4"],
877 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900878 stl: "none",
879 stubs: {
880 versions: ["10", "11", "12"],
881 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000882 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900883 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900884
885 cc_library {
886 name: "mylib4",
887 srcs: ["mylib.cpp"],
888 system_shared_libs: [],
889 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000890 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900891 }
Jiyong Park105dc322021-06-11 17:22:09 +0900892
893 rust_binary {
894 name: "foo.rust",
895 srcs: ["foo.rs"],
896 shared_libs: ["libfoo.shared_from_rust"],
897 prefer_rlib: true,
898 apex_available: ["myapex"],
899 }
900
901 cc_library_shared {
902 name: "libfoo.shared_from_rust",
903 srcs: ["mylib.cpp"],
904 system_shared_libs: [],
905 stl: "none",
906 stubs: {
907 versions: ["10", "11", "12"],
908 },
909 }
910
Jiyong Park25fc6a92018-11-18 18:02:45 +0900911 `)
912
Sundong Ahnabb64432019-10-22 13:58:29 +0900913 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900914 copyCmds := apexRule.Args["copy_commands"]
915
916 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800917 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900918
919 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800920 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900921
922 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800923 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900924
Colin Crossaede88c2020-08-11 12:17:01 -0700925 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900926
927 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900928 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900929 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900930 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900931
932 // 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 -0700933 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900934 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700935 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900936
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700937 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
938 // is replaced by sharing of "cFlags" in cc/builder.go.
939 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
940 // module variable representing "cflags". So it was not detected by ensureNotContains.
941 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
942 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
943 // including the original cflags's "-include mylib.h".
944 //
Jiyong Park64379952018-12-13 18:37:29 +0900945 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700946 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
947 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900948
949 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700950 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900951
Jooyung Hana57af4a2020-01-23 05:36:59 +0000952 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900953 "lib64/mylib.so",
954 "lib64/mylib3.so",
955 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +0900956 "bin/foo.rust",
957 "lib64/libc++.so", // by the implicit dependency from foo.rust
958 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +0900959 })
Jiyong Park105dc322021-06-11 17:22:09 +0900960
961 // Ensure that stub dependency from a rust module is not included
962 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
963 // The rust module is linked to the stub cc library
964 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
965 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
966 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900967}
968
Jiyong Park1bc84122021-06-22 20:23:05 +0900969func TestApexCanUsePrivateApis(t *testing.T) {
970 ctx := testApex(t, `
971 apex {
972 name: "myapex",
973 key: "myapex.key",
974 native_shared_libs: ["mylib"],
975 binaries: ["foo.rust"],
976 updatable: false,
977 platform_apis: true,
978 }
979
980 apex_key {
981 name: "myapex.key",
982 public_key: "testkey.avbpubkey",
983 private_key: "testkey.pem",
984 }
985
986 cc_library {
987 name: "mylib",
988 srcs: ["mylib.cpp"],
989 shared_libs: ["mylib2"],
990 system_shared_libs: [],
991 stl: "none",
992 apex_available: [ "myapex" ],
993 }
994
995 cc_library {
996 name: "mylib2",
997 srcs: ["mylib.cpp"],
998 cflags: ["-include mylib.h"],
999 system_shared_libs: [],
1000 stl: "none",
1001 stubs: {
1002 versions: ["1", "2", "3"],
1003 },
1004 }
1005
1006 rust_binary {
1007 name: "foo.rust",
1008 srcs: ["foo.rs"],
1009 shared_libs: ["libfoo.shared_from_rust"],
1010 prefer_rlib: true,
1011 apex_available: ["myapex"],
1012 }
1013
1014 cc_library_shared {
1015 name: "libfoo.shared_from_rust",
1016 srcs: ["mylib.cpp"],
1017 system_shared_libs: [],
1018 stl: "none",
1019 stubs: {
1020 versions: ["10", "11", "12"],
1021 },
1022 }
1023 `)
1024
1025 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1026 copyCmds := apexRule.Args["copy_commands"]
1027
1028 // Ensure that indirect stubs dep is not included
1029 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1030 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1031
1032 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1033 // of the platform_apis: true)
1034 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000_private").Rule("ld").Args["libFlags"]
1035 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1036 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1037 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000_private").Rule("rustc").Args["linkFlags"]
1038 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1039 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1040}
1041
Colin Cross7812fd32020-09-25 12:35:10 -07001042func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1043 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001044 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001045 apex {
1046 name: "myapex",
1047 key: "myapex.key",
1048 native_shared_libs: ["mylib", "mylib3"],
1049 min_sdk_version: "29",
1050 }
1051
1052 apex_key {
1053 name: "myapex.key",
1054 public_key: "testkey.avbpubkey",
1055 private_key: "testkey.pem",
1056 }
1057
1058 cc_library {
1059 name: "mylib",
1060 srcs: ["mylib.cpp"],
1061 shared_libs: ["mylib2", "mylib3"],
1062 system_shared_libs: [],
1063 stl: "none",
1064 apex_available: [ "myapex" ],
1065 min_sdk_version: "28",
1066 }
1067
1068 cc_library {
1069 name: "mylib2",
1070 srcs: ["mylib.cpp"],
1071 cflags: ["-include mylib.h"],
1072 system_shared_libs: [],
1073 stl: "none",
1074 stubs: {
1075 versions: ["28", "29", "30", "current"],
1076 },
1077 min_sdk_version: "28",
1078 }
1079
1080 cc_library {
1081 name: "mylib3",
1082 srcs: ["mylib.cpp"],
1083 shared_libs: ["mylib4"],
1084 system_shared_libs: [],
1085 stl: "none",
1086 stubs: {
1087 versions: ["28", "29", "30", "current"],
1088 },
1089 apex_available: [ "myapex" ],
1090 min_sdk_version: "28",
1091 }
1092
1093 cc_library {
1094 name: "mylib4",
1095 srcs: ["mylib.cpp"],
1096 system_shared_libs: [],
1097 stl: "none",
1098 apex_available: [ "myapex" ],
1099 min_sdk_version: "28",
1100 }
1101 `)
1102
1103 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1104 copyCmds := apexRule.Args["copy_commands"]
1105
1106 // Ensure that direct non-stubs dep is always included
1107 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1108
1109 // Ensure that indirect stubs dep is not included
1110 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1111
1112 // Ensure that direct stubs dep is included
1113 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1114
1115 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1116
Jiyong Park55549df2021-02-26 23:57:23 +09001117 // Ensure that mylib is linking with the latest version of stub for mylib2
1118 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001119 // ... and not linking to the non-stub (impl) variant of mylib2
1120 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1121
1122 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1123 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1124 // .. and not linking to the stubs variant of mylib3
1125 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1126
1127 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001128 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001129 ensureNotContains(t, mylib2Cflags, "-include ")
1130
1131 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -07001132 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -07001133
1134 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1135 "lib64/mylib.so",
1136 "lib64/mylib3.so",
1137 "lib64/mylib4.so",
1138 })
1139}
1140
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001141func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1142 t.Parallel()
1143 // myapex (Z)
1144 // mylib -----------------.
1145 // |
1146 // otherapex (29) |
1147 // libstub's versions: 29 Z current
1148 // |
1149 // <platform> |
1150 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001151 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001152 apex {
1153 name: "myapex",
1154 key: "myapex.key",
1155 native_shared_libs: ["mylib"],
1156 min_sdk_version: "Z", // non-final
1157 }
1158
1159 cc_library {
1160 name: "mylib",
1161 srcs: ["mylib.cpp"],
1162 shared_libs: ["libstub"],
1163 apex_available: ["myapex"],
1164 min_sdk_version: "Z",
1165 }
1166
1167 apex_key {
1168 name: "myapex.key",
1169 public_key: "testkey.avbpubkey",
1170 private_key: "testkey.pem",
1171 }
1172
1173 apex {
1174 name: "otherapex",
1175 key: "myapex.key",
1176 native_shared_libs: ["libstub"],
1177 min_sdk_version: "29",
1178 }
1179
1180 cc_library {
1181 name: "libstub",
1182 srcs: ["mylib.cpp"],
1183 stubs: {
1184 versions: ["29", "Z", "current"],
1185 },
1186 apex_available: ["otherapex"],
1187 min_sdk_version: "29",
1188 }
1189
1190 // platform module depending on libstub from otherapex should use the latest stub("current")
1191 cc_library {
1192 name: "libplatform",
1193 srcs: ["mylib.cpp"],
1194 shared_libs: ["libstub"],
1195 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001196 `,
1197 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1198 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1199 variables.Platform_sdk_final = proptools.BoolPtr(false)
1200 variables.Platform_version_active_codenames = []string{"Z"}
1201 }),
1202 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001203
Jiyong Park55549df2021-02-26 23:57:23 +09001204 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001205 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001206 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001207 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001208 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001209
1210 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1211 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1212 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1213 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1214 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1215}
1216
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001217func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001218 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001219 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001220 name: "myapex2",
1221 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001222 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001223 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001224 }
1225
1226 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001227 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001228 public_key: "testkey.avbpubkey",
1229 private_key: "testkey.pem",
1230 }
1231
1232 cc_library {
1233 name: "mylib",
1234 srcs: ["mylib.cpp"],
1235 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001236 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001237 system_shared_libs: [],
1238 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001239 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001240 }
1241
1242 cc_library {
1243 name: "libfoo",
1244 srcs: ["mylib.cpp"],
1245 shared_libs: ["libbar"],
1246 system_shared_libs: [],
1247 stl: "none",
1248 stubs: {
1249 versions: ["10", "20", "30"],
1250 },
1251 }
1252
1253 cc_library {
1254 name: "libbar",
1255 srcs: ["mylib.cpp"],
1256 system_shared_libs: [],
1257 stl: "none",
1258 }
1259
Jiyong Park678c8812020-02-07 17:25:49 +09001260 cc_library_static {
1261 name: "libbaz",
1262 srcs: ["mylib.cpp"],
1263 system_shared_libs: [],
1264 stl: "none",
1265 apex_available: [ "myapex2" ],
1266 }
1267
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001268 `)
1269
Jiyong Park83dc74b2020-01-14 18:38:44 +09001270 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001271 copyCmds := apexRule.Args["copy_commands"]
1272
1273 // Ensure that direct non-stubs dep is always included
1274 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1275
1276 // Ensure that indirect stubs dep is not included
1277 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1278
1279 // Ensure that dependency of stubs is not included
1280 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1281
Colin Crossaede88c2020-08-11 12:17:01 -07001282 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001283
1284 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001285 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001286 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001287 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001288
Jiyong Park3ff16992019-12-27 14:11:47 +09001289 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001290
1291 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1292 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001293
Artur Satayeva8bd1132020-04-27 18:07:06 +01001294 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001295 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001296
Artur Satayeva8bd1132020-04-27 18:07:06 +01001297 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001298 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001299}
1300
Jooyung Hand3639552019-08-09 12:57:43 +09001301func TestApexWithRuntimeLibsDependency(t *testing.T) {
1302 /*
1303 myapex
1304 |
1305 v (runtime_libs)
1306 mylib ------+------> libfoo [provides stub]
1307 |
1308 `------> libbar
1309 */
Colin Cross1c460562021-02-16 17:55:47 -08001310 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001311 apex {
1312 name: "myapex",
1313 key: "myapex.key",
1314 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001315 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001316 }
1317
1318 apex_key {
1319 name: "myapex.key",
1320 public_key: "testkey.avbpubkey",
1321 private_key: "testkey.pem",
1322 }
1323
1324 cc_library {
1325 name: "mylib",
1326 srcs: ["mylib.cpp"],
1327 runtime_libs: ["libfoo", "libbar"],
1328 system_shared_libs: [],
1329 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001330 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001331 }
1332
1333 cc_library {
1334 name: "libfoo",
1335 srcs: ["mylib.cpp"],
1336 system_shared_libs: [],
1337 stl: "none",
1338 stubs: {
1339 versions: ["10", "20", "30"],
1340 },
1341 }
1342
1343 cc_library {
1344 name: "libbar",
1345 srcs: ["mylib.cpp"],
1346 system_shared_libs: [],
1347 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001348 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001349 }
1350
1351 `)
1352
Sundong Ahnabb64432019-10-22 13:58:29 +09001353 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001354 copyCmds := apexRule.Args["copy_commands"]
1355
1356 // Ensure that direct non-stubs dep is always included
1357 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1358
1359 // Ensure that indirect stubs dep is not included
1360 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1361
1362 // Ensure that runtime_libs dep in included
1363 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1364
Sundong Ahnabb64432019-10-22 13:58:29 +09001365 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001366 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1367 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001368
1369}
1370
Paul Duffina02cae32021-03-09 01:44:06 +00001371var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1372 cc.PrepareForTestWithCcBuildComponents,
1373 PrepareForTestWithApexBuildComponents,
1374 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001375 apex {
1376 name: "com.android.runtime",
1377 key: "com.android.runtime.key",
1378 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001379 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001380 }
1381
1382 apex_key {
1383 name: "com.android.runtime.key",
1384 public_key: "testkey.avbpubkey",
1385 private_key: "testkey.pem",
1386 }
Paul Duffina02cae32021-03-09 01:44:06 +00001387 `),
1388 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1389)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001390
Paul Duffina02cae32021-03-09 01:44:06 +00001391func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001392 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001393 cc_library {
1394 name: "libc",
1395 no_libcrt: true,
1396 nocrt: true,
1397 stl: "none",
1398 system_shared_libs: [],
1399 stubs: { versions: ["1"] },
1400 apex_available: ["com.android.runtime"],
1401
1402 sanitize: {
1403 hwaddress: true,
1404 }
1405 }
1406
1407 cc_prebuilt_library_shared {
1408 name: "libclang_rt.hwasan-aarch64-android",
1409 no_libcrt: true,
1410 nocrt: true,
1411 stl: "none",
1412 system_shared_libs: [],
1413 srcs: [""],
1414 stubs: { versions: ["1"] },
1415
1416 sanitize: {
1417 never: true,
1418 },
Paul Duffina02cae32021-03-09 01:44:06 +00001419 } `)
1420 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001421
1422 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1423 "lib64/bionic/libc.so",
1424 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1425 })
1426
1427 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1428
1429 installed := hwasan.Description("install libclang_rt.hwasan")
1430 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1431
1432 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1433 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1434 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1435}
1436
1437func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001438 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001439 prepareForTestOfRuntimeApexWithHwasan,
1440 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1441 variables.SanitizeDevice = []string{"hwaddress"}
1442 }),
1443 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001444 cc_library {
1445 name: "libc",
1446 no_libcrt: true,
1447 nocrt: true,
1448 stl: "none",
1449 system_shared_libs: [],
1450 stubs: { versions: ["1"] },
1451 apex_available: ["com.android.runtime"],
1452 }
1453
1454 cc_prebuilt_library_shared {
1455 name: "libclang_rt.hwasan-aarch64-android",
1456 no_libcrt: true,
1457 nocrt: true,
1458 stl: "none",
1459 system_shared_libs: [],
1460 srcs: [""],
1461 stubs: { versions: ["1"] },
1462
1463 sanitize: {
1464 never: true,
1465 },
1466 }
Paul Duffina02cae32021-03-09 01:44:06 +00001467 `)
1468 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001469
1470 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1471 "lib64/bionic/libc.so",
1472 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1473 })
1474
1475 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1476
1477 installed := hwasan.Description("install libclang_rt.hwasan")
1478 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1479
1480 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1481 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1482 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1483}
1484
Jooyung Han61b66e92020-03-21 14:21:46 +00001485func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1486 testcases := []struct {
1487 name string
1488 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001489 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001490 shouldLink string
1491 shouldNotLink []string
1492 }{
1493 {
Jiyong Park55549df2021-02-26 23:57:23 +09001494 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001495 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001496 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001497 shouldLink: "current",
1498 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001499 },
1500 {
Jiyong Park55549df2021-02-26 23:57:23 +09001501 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001502 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001503 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001504 shouldLink: "current",
1505 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001506 },
1507 }
1508 for _, tc := range testcases {
1509 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001510 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001511 apex {
1512 name: "myapex",
1513 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001514 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001515 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001516 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001517 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001518
Jooyung Han61b66e92020-03-21 14:21:46 +00001519 apex_key {
1520 name: "myapex.key",
1521 public_key: "testkey.avbpubkey",
1522 private_key: "testkey.pem",
1523 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001524
Jooyung Han61b66e92020-03-21 14:21:46 +00001525 cc_library {
1526 name: "mylib",
1527 srcs: ["mylib.cpp"],
1528 vendor_available: true,
1529 shared_libs: ["libbar"],
1530 system_shared_libs: [],
1531 stl: "none",
1532 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001533 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001534 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001535
Jooyung Han61b66e92020-03-21 14:21:46 +00001536 cc_library {
1537 name: "libbar",
1538 srcs: ["mylib.cpp"],
1539 system_shared_libs: [],
1540 stl: "none",
1541 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001542 llndk: {
1543 symbol_file: "libbar.map.txt",
1544 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001545 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001546 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001547 withUnbundledBuild,
1548 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001549
Jooyung Han61b66e92020-03-21 14:21:46 +00001550 // Ensure that LLNDK dep is not included
1551 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1552 "lib64/mylib.so",
1553 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001554
Jooyung Han61b66e92020-03-21 14:21:46 +00001555 // Ensure that LLNDK dep is required
1556 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1557 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1558 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001559
Steven Moreland2c4000c2021-04-27 02:08:49 +00001560 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1561 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001562 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001563 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001564 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001565
Steven Moreland2c4000c2021-04-27 02:08:49 +00001566 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001567 ver := tc.shouldLink
1568 if tc.shouldLink == "current" {
1569 ver = strconv.Itoa(android.FutureApiLevelInt)
1570 }
1571 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001572 })
1573 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001574}
1575
Jiyong Park25fc6a92018-11-18 18:02:45 +09001576func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001577 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001578 apex {
1579 name: "myapex",
1580 key: "myapex.key",
1581 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001582 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001583 }
1584
1585 apex_key {
1586 name: "myapex.key",
1587 public_key: "testkey.avbpubkey",
1588 private_key: "testkey.pem",
1589 }
1590
1591 cc_library {
1592 name: "mylib",
1593 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001594 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001595 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_shared {
1601 name: "mylib_shared",
1602 srcs: ["mylib.cpp"],
1603 shared_libs: ["libdl#27"],
1604 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001605 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001606 }
1607
1608 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001609 name: "libBootstrap",
1610 srcs: ["mylib.cpp"],
1611 stl: "none",
1612 bootstrap: true,
1613 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001614 `)
1615
Sundong Ahnabb64432019-10-22 13:58:29 +09001616 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001617 copyCmds := apexRule.Args["copy_commands"]
1618
1619 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001620 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001621 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1622 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001623
1624 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001625 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001626
Colin Crossaede88c2020-08-11 12:17:01 -07001627 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1628 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1629 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001630
1631 // For dependency to libc
1632 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001633 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001634 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001635 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001636 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001637 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1638 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001639
1640 // For dependency to libm
1641 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001642 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001643 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001644 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001645 // ... and is not compiling with the stub
1646 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1647 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1648
1649 // For dependency to libdl
1650 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001651 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001652 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001653 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1654 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001655 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001656 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001657 // ... Cflags from stub is correctly exported to mylib
1658 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1659 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001660
1661 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001662 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1663 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1664 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1665 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001666}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001667
Jooyung Han749dc692020-04-15 11:03:39 +09001668func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001669 // there are three links between liba --> libz.
1670 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001671 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001672 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001673 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001674 apex {
1675 name: "myapex",
1676 key: "myapex.key",
1677 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001678 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001679 }
1680
1681 apex {
1682 name: "otherapex",
1683 key: "myapex.key",
1684 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001685 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001686 }
1687
1688 apex_key {
1689 name: "myapex.key",
1690 public_key: "testkey.avbpubkey",
1691 private_key: "testkey.pem",
1692 }
1693
1694 cc_library {
1695 name: "libx",
1696 shared_libs: ["liba"],
1697 system_shared_libs: [],
1698 stl: "none",
1699 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001700 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001701 }
1702
1703 cc_library {
1704 name: "liby",
1705 shared_libs: ["liba"],
1706 system_shared_libs: [],
1707 stl: "none",
1708 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001709 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001710 }
1711
1712 cc_library {
1713 name: "liba",
1714 shared_libs: ["libz"],
1715 system_shared_libs: [],
1716 stl: "none",
1717 apex_available: [
1718 "//apex_available:anyapex",
1719 "//apex_available:platform",
1720 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001721 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001722 }
1723
1724 cc_library {
1725 name: "libz",
1726 system_shared_libs: [],
1727 stl: "none",
1728 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001729 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001730 },
1731 }
Jooyung Han749dc692020-04-15 11:03:39 +09001732 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001733
1734 expectLink := func(from, from_variant, to, to_variant string) {
1735 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1736 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1737 }
1738 expectNoLink := func(from, from_variant, to, to_variant string) {
1739 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1740 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1741 }
1742 // platform liba is linked to non-stub version
1743 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001744 // liba in myapex is linked to current
1745 expectLink("liba", "shared_apex29", "libz", "shared_current")
1746 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001747 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001748 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001749 // liba in otherapex is linked to current
1750 expectLink("liba", "shared_apex30", "libz", "shared_current")
1751 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001752 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1753 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001754}
1755
Jooyung Hanaed150d2020-04-02 01:41:41 +09001756func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001757 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001758 apex {
1759 name: "myapex",
1760 key: "myapex.key",
1761 native_shared_libs: ["libx"],
1762 min_sdk_version: "R",
1763 }
1764
1765 apex_key {
1766 name: "myapex.key",
1767 public_key: "testkey.avbpubkey",
1768 private_key: "testkey.pem",
1769 }
1770
1771 cc_library {
1772 name: "libx",
1773 shared_libs: ["libz"],
1774 system_shared_libs: [],
1775 stl: "none",
1776 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001777 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001778 }
1779
1780 cc_library {
1781 name: "libz",
1782 system_shared_libs: [],
1783 stl: "none",
1784 stubs: {
1785 versions: ["29", "R"],
1786 },
1787 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001788 `,
1789 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1790 variables.Platform_version_active_codenames = []string{"R"}
1791 }),
1792 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001793
1794 expectLink := func(from, from_variant, to, to_variant string) {
1795 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1796 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1797 }
1798 expectNoLink := func(from, from_variant, to, to_variant string) {
1799 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1800 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1801 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001802 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1803 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001804 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1805 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001806}
1807
Jooyung Han4c4da062021-06-23 10:23:16 +09001808func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1809 testApex(t, `
1810 apex {
1811 name: "myapex",
1812 key: "myapex.key",
1813 java_libs: ["libx"],
1814 min_sdk_version: "S",
1815 }
1816
1817 apex_key {
1818 name: "myapex.key",
1819 public_key: "testkey.avbpubkey",
1820 private_key: "testkey.pem",
1821 }
1822
1823 java_library {
1824 name: "libx",
1825 srcs: ["a.java"],
1826 apex_available: [ "myapex" ],
1827 sdk_version: "current",
1828 min_sdk_version: "S", // should be okay
1829 }
1830 `,
1831 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1832 variables.Platform_version_active_codenames = []string{"S"}
1833 variables.Platform_sdk_codename = proptools.StringPtr("S")
1834 }),
1835 )
1836}
1837
Jooyung Han749dc692020-04-15 11:03:39 +09001838func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001839 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001840 apex {
1841 name: "myapex",
1842 key: "myapex.key",
1843 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001844 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001845 }
1846
1847 apex_key {
1848 name: "myapex.key",
1849 public_key: "testkey.avbpubkey",
1850 private_key: "testkey.pem",
1851 }
1852
1853 cc_library {
1854 name: "libx",
1855 shared_libs: ["libz"],
1856 system_shared_libs: [],
1857 stl: "none",
1858 apex_available: [ "myapex" ],
1859 }
1860
1861 cc_library {
1862 name: "libz",
1863 system_shared_libs: [],
1864 stl: "none",
1865 stubs: {
1866 versions: ["1", "2"],
1867 },
1868 }
1869 `)
1870
1871 expectLink := func(from, from_variant, to, to_variant string) {
1872 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1873 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1874 }
1875 expectNoLink := func(from, from_variant, to, to_variant string) {
1876 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1877 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1878 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001879 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001880 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001881 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001882 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001883}
1884
Jiyong Park5df7bd32021-08-25 16:18:46 +09001885func TestApexMinSdkVersion_crtobjectInVendorApex(t *testing.T) {
1886 ctx := testApex(t, `
1887 apex {
1888 name: "myapex",
1889 key: "myapex.key",
1890 native_shared_libs: ["mylib"],
1891 updatable: false,
1892 vendor: true,
1893 min_sdk_version: "29",
1894 }
1895
1896 apex_key {
1897 name: "myapex.key",
1898 public_key: "testkey.avbpubkey",
1899 private_key: "testkey.pem",
1900 }
1901
1902 cc_library {
1903 name: "mylib",
1904 vendor_available: true,
1905 system_shared_libs: [],
1906 stl: "none",
1907 apex_available: [ "myapex" ],
1908 min_sdk_version: "29",
1909 }
1910 `)
1911
1912 vendorVariant := "android_vendor.29_arm64_armv8-a"
1913
1914 // First check that the correct variant of crtbegin_so is used.
1915 ldRule := ctx.ModuleForTests("mylib", vendorVariant+"_shared_apex29").Rule("ld")
1916 crtBegin := names(ldRule.Args["crtBegin"])
1917 ensureListContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
1918
1919 // Ensure that the crtbegin_so used by the APEX is targeting 29
1920 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
1921 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
1922}
1923
Jooyung Han03b51852020-02-26 22:45:42 +09001924func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001925 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001926 apex {
1927 name: "myapex",
1928 key: "myapex.key",
1929 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001930 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001931 }
1932
1933 apex_key {
1934 name: "myapex.key",
1935 public_key: "testkey.avbpubkey",
1936 private_key: "testkey.pem",
1937 }
1938
1939 cc_library {
1940 name: "libx",
1941 system_shared_libs: [],
1942 stl: "none",
1943 apex_available: [ "myapex" ],
1944 stubs: {
1945 versions: ["1", "2"],
1946 },
1947 }
1948
1949 cc_library {
1950 name: "libz",
1951 shared_libs: ["libx"],
1952 system_shared_libs: [],
1953 stl: "none",
1954 }
1955 `)
1956
1957 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001958 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001959 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1960 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1961 }
1962 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001963 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001964 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1965 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1966 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001967 expectLink("libz", "shared", "libx", "shared_current")
1968 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09001969 expectNoLink("libz", "shared", "libz", "shared_1")
1970 expectNoLink("libz", "shared", "libz", "shared")
1971}
1972
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001973var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1974 func(variables android.FixtureProductVariables) {
1975 variables.SanitizeDevice = []string{"hwaddress"}
1976 },
1977)
1978
Jooyung Han75568392020-03-20 04:29:24 +09001979func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001980 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001981 apex {
1982 name: "myapex",
1983 key: "myapex.key",
1984 native_shared_libs: ["libx"],
1985 min_sdk_version: "29",
1986 }
1987
1988 apex_key {
1989 name: "myapex.key",
1990 public_key: "testkey.avbpubkey",
1991 private_key: "testkey.pem",
1992 }
1993
1994 cc_library {
1995 name: "libx",
1996 shared_libs: ["libbar"],
1997 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001998 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001999 }
2000
2001 cc_library {
2002 name: "libbar",
2003 stubs: {
2004 versions: ["29", "30"],
2005 },
2006 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002007 `,
2008 prepareForTestWithSantitizeHwaddress,
2009 )
Jooyung Han03b51852020-02-26 22:45:42 +09002010 expectLink := func(from, from_variant, to, to_variant string) {
2011 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2012 libFlags := ld.Args["libFlags"]
2013 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2014 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002015 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002016}
2017
Jooyung Han75568392020-03-20 04:29:24 +09002018func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002019 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002020 apex {
2021 name: "myapex",
2022 key: "myapex.key",
2023 native_shared_libs: ["libx"],
2024 min_sdk_version: "29",
2025 }
2026
2027 apex_key {
2028 name: "myapex.key",
2029 public_key: "testkey.avbpubkey",
2030 private_key: "testkey.pem",
2031 }
2032
2033 cc_library {
2034 name: "libx",
2035 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002036 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002037 }
Jooyung Han75568392020-03-20 04:29:24 +09002038 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002039
2040 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002041 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002042 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002043 // note that platform variant is not.
2044 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002045 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002046}
2047
Jooyung Han749dc692020-04-15 11:03:39 +09002048func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2049 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002050 apex {
2051 name: "myapex",
2052 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002053 native_shared_libs: ["mylib"],
2054 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002055 }
2056
2057 apex_key {
2058 name: "myapex.key",
2059 public_key: "testkey.avbpubkey",
2060 private_key: "testkey.pem",
2061 }
Jooyung Han749dc692020-04-15 11:03:39 +09002062
2063 cc_library {
2064 name: "mylib",
2065 srcs: ["mylib.cpp"],
2066 system_shared_libs: [],
2067 stl: "none",
2068 apex_available: [
2069 "myapex",
2070 ],
2071 min_sdk_version: "30",
2072 }
2073 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002074
2075 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2076 apex {
2077 name: "myapex",
2078 key: "myapex.key",
2079 native_shared_libs: ["libfoo.ffi"],
2080 min_sdk_version: "29",
2081 }
2082
2083 apex_key {
2084 name: "myapex.key",
2085 public_key: "testkey.avbpubkey",
2086 private_key: "testkey.pem",
2087 }
2088
2089 rust_ffi_shared {
2090 name: "libfoo.ffi",
2091 srcs: ["foo.rs"],
2092 crate_name: "foo",
2093 apex_available: [
2094 "myapex",
2095 ],
2096 min_sdk_version: "30",
2097 }
2098 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002099
2100 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2101 apex {
2102 name: "myapex",
2103 key: "myapex.key",
2104 java_libs: ["libfoo"],
2105 min_sdk_version: "29",
2106 }
2107
2108 apex_key {
2109 name: "myapex.key",
2110 public_key: "testkey.avbpubkey",
2111 private_key: "testkey.pem",
2112 }
2113
2114 java_import {
2115 name: "libfoo",
2116 jars: ["libfoo.jar"],
2117 apex_available: [
2118 "myapex",
2119 ],
2120 min_sdk_version: "30",
2121 }
2122 `)
Jooyung Han749dc692020-04-15 11:03:39 +09002123}
2124
2125func TestApexMinSdkVersion_Okay(t *testing.T) {
2126 testApex(t, `
2127 apex {
2128 name: "myapex",
2129 key: "myapex.key",
2130 native_shared_libs: ["libfoo"],
2131 java_libs: ["libbar"],
2132 min_sdk_version: "29",
2133 }
2134
2135 apex_key {
2136 name: "myapex.key",
2137 public_key: "testkey.avbpubkey",
2138 private_key: "testkey.pem",
2139 }
2140
2141 cc_library {
2142 name: "libfoo",
2143 srcs: ["mylib.cpp"],
2144 shared_libs: ["libfoo_dep"],
2145 apex_available: ["myapex"],
2146 min_sdk_version: "29",
2147 }
2148
2149 cc_library {
2150 name: "libfoo_dep",
2151 srcs: ["mylib.cpp"],
2152 apex_available: ["myapex"],
2153 min_sdk_version: "29",
2154 }
2155
2156 java_library {
2157 name: "libbar",
2158 sdk_version: "current",
2159 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002160 static_libs: [
2161 "libbar_dep",
2162 "libbar_import_dep",
2163 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002164 apex_available: ["myapex"],
2165 min_sdk_version: "29",
2166 }
2167
2168 java_library {
2169 name: "libbar_dep",
2170 sdk_version: "current",
2171 srcs: ["a.java"],
2172 apex_available: ["myapex"],
2173 min_sdk_version: "29",
2174 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002175
2176 java_import {
2177 name: "libbar_import_dep",
2178 jars: ["libbar.jar"],
2179 apex_available: ["myapex"],
2180 min_sdk_version: "29",
2181 }
Jooyung Han03b51852020-02-26 22:45:42 +09002182 `)
2183}
2184
Artur Satayev8cf899a2020-04-15 17:29:42 +01002185func TestJavaStableSdkVersion(t *testing.T) {
2186 testCases := []struct {
2187 name string
2188 expectedError string
2189 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002190 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002191 }{
2192 {
2193 name: "Non-updatable apex with non-stable dep",
2194 bp: `
2195 apex {
2196 name: "myapex",
2197 java_libs: ["myjar"],
2198 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002199 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002200 }
2201 apex_key {
2202 name: "myapex.key",
2203 public_key: "testkey.avbpubkey",
2204 private_key: "testkey.pem",
2205 }
2206 java_library {
2207 name: "myjar",
2208 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002209 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002210 apex_available: ["myapex"],
2211 }
2212 `,
2213 },
2214 {
2215 name: "Updatable apex with stable dep",
2216 bp: `
2217 apex {
2218 name: "myapex",
2219 java_libs: ["myjar"],
2220 key: "myapex.key",
2221 updatable: true,
2222 min_sdk_version: "29",
2223 }
2224 apex_key {
2225 name: "myapex.key",
2226 public_key: "testkey.avbpubkey",
2227 private_key: "testkey.pem",
2228 }
2229 java_library {
2230 name: "myjar",
2231 srcs: ["foo/bar/MyClass.java"],
2232 sdk_version: "current",
2233 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002234 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002235 }
2236 `,
2237 },
2238 {
2239 name: "Updatable apex with non-stable dep",
2240 expectedError: "cannot depend on \"myjar\"",
2241 bp: `
2242 apex {
2243 name: "myapex",
2244 java_libs: ["myjar"],
2245 key: "myapex.key",
2246 updatable: true,
2247 }
2248 apex_key {
2249 name: "myapex.key",
2250 public_key: "testkey.avbpubkey",
2251 private_key: "testkey.pem",
2252 }
2253 java_library {
2254 name: "myjar",
2255 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002256 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002257 apex_available: ["myapex"],
2258 }
2259 `,
2260 },
2261 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002262 name: "Updatable apex with non-stable legacy core platform dep",
2263 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2264 bp: `
2265 apex {
2266 name: "myapex",
2267 java_libs: ["myjar-uses-legacy"],
2268 key: "myapex.key",
2269 updatable: true,
2270 }
2271 apex_key {
2272 name: "myapex.key",
2273 public_key: "testkey.avbpubkey",
2274 private_key: "testkey.pem",
2275 }
2276 java_library {
2277 name: "myjar-uses-legacy",
2278 srcs: ["foo/bar/MyClass.java"],
2279 sdk_version: "core_platform",
2280 apex_available: ["myapex"],
2281 }
2282 `,
2283 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2284 },
2285 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002286 name: "Updatable apex with non-stable transitive dep",
2287 // This is not actually detecting that the transitive dependency is unstable, rather it is
2288 // detecting that the transitive dependency is building against a wider API surface than the
2289 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002290 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002291 bp: `
2292 apex {
2293 name: "myapex",
2294 java_libs: ["myjar"],
2295 key: "myapex.key",
2296 updatable: true,
2297 }
2298 apex_key {
2299 name: "myapex.key",
2300 public_key: "testkey.avbpubkey",
2301 private_key: "testkey.pem",
2302 }
2303 java_library {
2304 name: "myjar",
2305 srcs: ["foo/bar/MyClass.java"],
2306 sdk_version: "current",
2307 apex_available: ["myapex"],
2308 static_libs: ["transitive-jar"],
2309 }
2310 java_library {
2311 name: "transitive-jar",
2312 srcs: ["foo/bar/MyClass.java"],
2313 sdk_version: "core_platform",
2314 apex_available: ["myapex"],
2315 }
2316 `,
2317 },
2318 }
2319
2320 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002321 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2322 continue
2323 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002324 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002325 errorHandler := android.FixtureExpectsNoErrors
2326 if test.expectedError != "" {
2327 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002328 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002329 android.GroupFixturePreparers(
2330 java.PrepareForTestWithJavaDefaultModules,
2331 PrepareForTestWithApexBuildComponents,
2332 prepareForTestWithMyapex,
2333 android.OptionalFixturePreparer(test.preparer),
2334 ).
2335 ExtendWithErrorHandler(errorHandler).
2336 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002337 })
2338 }
2339}
2340
Jooyung Han749dc692020-04-15 11:03:39 +09002341func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2342 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2343 apex {
2344 name: "myapex",
2345 key: "myapex.key",
2346 native_shared_libs: ["mylib"],
2347 min_sdk_version: "29",
2348 }
2349
2350 apex_key {
2351 name: "myapex.key",
2352 public_key: "testkey.avbpubkey",
2353 private_key: "testkey.pem",
2354 }
2355
2356 cc_library {
2357 name: "mylib",
2358 srcs: ["mylib.cpp"],
2359 shared_libs: ["mylib2"],
2360 system_shared_libs: [],
2361 stl: "none",
2362 apex_available: [
2363 "myapex",
2364 ],
2365 min_sdk_version: "29",
2366 }
2367
2368 // indirect part of the apex
2369 cc_library {
2370 name: "mylib2",
2371 srcs: ["mylib.cpp"],
2372 system_shared_libs: [],
2373 stl: "none",
2374 apex_available: [
2375 "myapex",
2376 ],
2377 min_sdk_version: "30",
2378 }
2379 `)
2380}
2381
2382func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2383 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2384 apex {
2385 name: "myapex",
2386 key: "myapex.key",
2387 apps: ["AppFoo"],
2388 min_sdk_version: "29",
2389 }
2390
2391 apex_key {
2392 name: "myapex.key",
2393 public_key: "testkey.avbpubkey",
2394 private_key: "testkey.pem",
2395 }
2396
2397 android_app {
2398 name: "AppFoo",
2399 srcs: ["foo/bar/MyClass.java"],
2400 sdk_version: "current",
2401 min_sdk_version: "29",
2402 system_modules: "none",
2403 stl: "none",
2404 static_libs: ["bar"],
2405 apex_available: [ "myapex" ],
2406 }
2407
2408 java_library {
2409 name: "bar",
2410 sdk_version: "current",
2411 srcs: ["a.java"],
2412 apex_available: [ "myapex" ],
2413 }
2414 `)
2415}
2416
2417func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002418 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002419 apex {
2420 name: "myapex",
2421 key: "myapex.key",
2422 native_shared_libs: ["mylib"],
2423 min_sdk_version: "29",
2424 }
2425
2426 apex_key {
2427 name: "myapex.key",
2428 public_key: "testkey.avbpubkey",
2429 private_key: "testkey.pem",
2430 }
2431
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002432 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002433 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2434 cc_library {
2435 name: "mylib",
2436 srcs: ["mylib.cpp"],
2437 shared_libs: ["mylib2"],
2438 system_shared_libs: [],
2439 stl: "none",
2440 apex_available: ["myapex", "otherapex"],
2441 min_sdk_version: "29",
2442 }
2443
2444 cc_library {
2445 name: "mylib2",
2446 srcs: ["mylib.cpp"],
2447 system_shared_libs: [],
2448 stl: "none",
2449 apex_available: ["otherapex"],
2450 stubs: { versions: ["29", "30"] },
2451 min_sdk_version: "30",
2452 }
2453
2454 apex {
2455 name: "otherapex",
2456 key: "myapex.key",
2457 native_shared_libs: ["mylib", "mylib2"],
2458 min_sdk_version: "30",
2459 }
2460 `)
2461 expectLink := func(from, from_variant, to, to_variant string) {
2462 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2463 libFlags := ld.Args["libFlags"]
2464 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2465 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002466 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002467 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002468}
2469
Jooyung Haned124c32021-01-26 11:43:46 +09002470func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002471 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2472 func(variables android.FixtureProductVariables) {
2473 variables.Platform_sdk_codename = proptools.StringPtr("S")
2474 variables.Platform_version_active_codenames = []string{"S"}
2475 },
2476 )
Jooyung Haned124c32021-01-26 11:43:46 +09002477 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2478 apex {
2479 name: "myapex",
2480 key: "myapex.key",
2481 native_shared_libs: ["libfoo"],
2482 min_sdk_version: "S",
2483 }
2484 apex_key {
2485 name: "myapex.key",
2486 public_key: "testkey.avbpubkey",
2487 private_key: "testkey.pem",
2488 }
2489 cc_library {
2490 name: "libfoo",
2491 shared_libs: ["libbar"],
2492 apex_available: ["myapex"],
2493 min_sdk_version: "29",
2494 }
2495 cc_library {
2496 name: "libbar",
2497 apex_available: ["myapex"],
2498 }
2499 `, withSAsActiveCodeNames)
2500}
2501
2502func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002503 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2504 variables.Platform_sdk_codename = proptools.StringPtr("S")
2505 variables.Platform_version_active_codenames = []string{"S", "T"}
2506 })
Colin Cross1c460562021-02-16 17:55:47 -08002507 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002508 apex {
2509 name: "myapex",
2510 key: "myapex.key",
2511 native_shared_libs: ["libfoo"],
2512 min_sdk_version: "S",
2513 }
2514 apex_key {
2515 name: "myapex.key",
2516 public_key: "testkey.avbpubkey",
2517 private_key: "testkey.pem",
2518 }
2519 cc_library {
2520 name: "libfoo",
2521 shared_libs: ["libbar"],
2522 apex_available: ["myapex"],
2523 min_sdk_version: "S",
2524 }
2525 cc_library {
2526 name: "libbar",
2527 stubs: {
2528 symbol_file: "libbar.map.txt",
2529 versions: ["30", "S", "T"],
2530 },
2531 }
2532 `, withSAsActiveCodeNames)
2533
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002534 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002535 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2536 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002537 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002538}
2539
Jiyong Park7c2ee712018-12-07 00:42:25 +09002540func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002541 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002542 apex {
2543 name: "myapex",
2544 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002545 native_shared_libs: ["mylib"],
2546 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002547 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002548 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002549 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002550 }
2551
2552 apex_key {
2553 name: "myapex.key",
2554 public_key: "testkey.avbpubkey",
2555 private_key: "testkey.pem",
2556 }
2557
2558 prebuilt_etc {
2559 name: "myetc",
2560 src: "myprebuilt",
2561 sub_dir: "foo/bar",
2562 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002563
2564 cc_library {
2565 name: "mylib",
2566 srcs: ["mylib.cpp"],
2567 relative_install_path: "foo/bar",
2568 system_shared_libs: [],
2569 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002570 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002571 }
2572
2573 cc_binary {
2574 name: "mybin",
2575 srcs: ["mylib.cpp"],
2576 relative_install_path: "foo/bar",
2577 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002578 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002579 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002580 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002581 `)
2582
Sundong Ahnabb64432019-10-22 13:58:29 +09002583 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002584 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002585
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002586 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002587 ensureContains(t, cmd, "/etc ")
2588 ensureContains(t, cmd, "/etc/foo ")
2589 ensureContains(t, cmd, "/etc/foo/bar ")
2590 ensureContains(t, cmd, "/lib64 ")
2591 ensureContains(t, cmd, "/lib64/foo ")
2592 ensureContains(t, cmd, "/lib64/foo/bar ")
2593 ensureContains(t, cmd, "/lib ")
2594 ensureContains(t, cmd, "/lib/foo ")
2595 ensureContains(t, cmd, "/lib/foo/bar ")
2596 ensureContains(t, cmd, "/bin ")
2597 ensureContains(t, cmd, "/bin/foo ")
2598 ensureContains(t, cmd, "/bin/foo/bar ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002599}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002600
Jooyung Han35155c42020-02-06 17:33:20 +09002601func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002602 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002603 apex {
2604 name: "myapex",
2605 key: "myapex.key",
2606 multilib: {
2607 both: {
2608 native_shared_libs: ["mylib"],
2609 binaries: ["mybin"],
2610 },
2611 },
2612 compile_multilib: "both",
2613 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002614 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002615 }
2616
2617 apex_key {
2618 name: "myapex.key",
2619 public_key: "testkey.avbpubkey",
2620 private_key: "testkey.pem",
2621 }
2622
2623 cc_library {
2624 name: "mylib",
2625 relative_install_path: "foo/bar",
2626 system_shared_libs: [],
2627 stl: "none",
2628 apex_available: [ "myapex" ],
2629 native_bridge_supported: true,
2630 }
2631
2632 cc_binary {
2633 name: "mybin",
2634 relative_install_path: "foo/bar",
2635 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002636 stl: "none",
2637 apex_available: [ "myapex" ],
2638 native_bridge_supported: true,
2639 compile_multilib: "both", // default is "first" for binary
2640 multilib: {
2641 lib64: {
2642 suffix: "64",
2643 },
2644 },
2645 }
2646 `, withNativeBridgeEnabled)
2647 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2648 "bin/foo/bar/mybin",
2649 "bin/foo/bar/mybin64",
2650 "bin/arm/foo/bar/mybin",
2651 "bin/arm64/foo/bar/mybin64",
2652 "lib/foo/bar/mylib.so",
2653 "lib/arm/foo/bar/mylib.so",
2654 "lib64/foo/bar/mylib.so",
2655 "lib64/arm64/foo/bar/mylib.so",
2656 })
2657}
2658
Jooyung Han85d61762020-06-24 23:50:26 +09002659func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08002660 result := android.GroupFixturePreparers(
2661 prepareForApexTest,
2662 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
2663 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002664 apex {
2665 name: "myapex",
2666 key: "myapex.key",
2667 binaries: ["mybin"],
2668 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002669 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002670 }
2671 apex_key {
2672 name: "myapex.key",
2673 public_key: "testkey.avbpubkey",
2674 private_key: "testkey.pem",
2675 }
2676 cc_binary {
2677 name: "mybin",
2678 vendor: true,
2679 shared_libs: ["libfoo"],
2680 }
2681 cc_library {
2682 name: "libfoo",
2683 proprietary: true,
2684 }
2685 `)
2686
Colin Crossc68db4b2021-11-11 18:59:15 -08002687 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09002688 "bin/mybin",
2689 "lib64/libfoo.so",
2690 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2691 "lib64/libc++.so",
2692 })
2693
Colin Crossc68db4b2021-11-11 18:59:15 -08002694 apexBundle := result.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2695 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002696 name := apexBundle.BaseModuleName()
2697 prefix := "TARGET_"
2698 var builder strings.Builder
2699 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08002700 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01002701 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002702 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002703
Colin Crossc68db4b2021-11-11 18:59:15 -08002704 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002705 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2706 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002707}
2708
Jooyung Handf78e212020-07-22 15:54:47 +09002709func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002710 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002711 apex {
2712 name: "myapex",
2713 key: "myapex.key",
2714 binaries: ["mybin"],
2715 vendor: true,
2716 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002717 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002718 }
2719 apex_key {
2720 name: "myapex.key",
2721 public_key: "testkey.avbpubkey",
2722 private_key: "testkey.pem",
2723 }
2724 cc_binary {
2725 name: "mybin",
2726 vendor: true,
2727 shared_libs: ["libvndk", "libvendor"],
2728 }
2729 cc_library {
2730 name: "libvndk",
2731 vndk: {
2732 enabled: true,
2733 },
2734 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002735 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002736 }
2737 cc_library {
2738 name: "libvendor",
2739 vendor: true,
2740 }
2741 `)
2742
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002743 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002744
Paul Duffina71a67a2021-03-29 00:42:57 +01002745 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002746 libs := names(ldRule.Args["libFlags"])
2747 // VNDK libs(libvndk/libc++) as they are
Paul Duffin37ba3442021-03-29 00:21:08 +01002748 ensureListContains(t, libs, "out/soong/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
2749 ensureListContains(t, libs, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002750 // non-stable Vendor libs as APEX variants
Paul Duffin37ba3442021-03-29 00:21:08 +01002751 ensureListContains(t, libs, "out/soong/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002752
2753 // VNDK libs are not included when use_vndk_as_stable: true
2754 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2755 "bin/mybin",
2756 "lib64/libvendor.so",
2757 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002758
2759 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2760 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2761 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002762}
2763
Justin Yun13decfb2021-03-08 19:25:55 +09002764func TestProductVariant(t *testing.T) {
2765 ctx := testApex(t, `
2766 apex {
2767 name: "myapex",
2768 key: "myapex.key",
2769 updatable: false,
2770 product_specific: true,
2771 binaries: ["foo"],
2772 }
2773
2774 apex_key {
2775 name: "myapex.key",
2776 public_key: "testkey.avbpubkey",
2777 private_key: "testkey.pem",
2778 }
2779
2780 cc_binary {
2781 name: "foo",
2782 product_available: true,
2783 apex_available: ["myapex"],
2784 srcs: ["foo.cpp"],
2785 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002786 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2787 variables.ProductVndkVersion = proptools.StringPtr("current")
2788 }),
2789 )
Justin Yun13decfb2021-03-08 19:25:55 +09002790
2791 cflags := strings.Fields(
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002792 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002793 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2794 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2795 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2796 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2797}
2798
Jooyung Han8e5685d2020-09-21 11:02:57 +09002799func TestApex_withPrebuiltFirmware(t *testing.T) {
2800 testCases := []struct {
2801 name string
2802 additionalProp string
2803 }{
2804 {"system apex with prebuilt_firmware", ""},
2805 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2806 }
2807 for _, tc := range testCases {
2808 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002809 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002810 apex {
2811 name: "myapex",
2812 key: "myapex.key",
2813 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002814 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002815 `+tc.additionalProp+`
2816 }
2817 apex_key {
2818 name: "myapex.key",
2819 public_key: "testkey.avbpubkey",
2820 private_key: "testkey.pem",
2821 }
2822 prebuilt_firmware {
2823 name: "myfirmware",
2824 src: "myfirmware.bin",
2825 filename_from_src: true,
2826 `+tc.additionalProp+`
2827 }
2828 `)
2829 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2830 "etc/firmware/myfirmware.bin",
2831 })
2832 })
2833 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002834}
2835
Jooyung Hanefb184e2020-06-25 17:14:25 +09002836func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002837 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002838 apex {
2839 name: "myapex",
2840 key: "myapex.key",
2841 vendor: true,
2842 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002843 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002844 }
2845
2846 apex_key {
2847 name: "myapex.key",
2848 public_key: "testkey.avbpubkey",
2849 private_key: "testkey.pem",
2850 }
2851
2852 cc_library {
2853 name: "mylib",
2854 vendor_available: true,
2855 }
2856 `)
2857
2858 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002859 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002860 name := apexBundle.BaseModuleName()
2861 prefix := "TARGET_"
2862 var builder strings.Builder
2863 data.Custom(&builder, name, prefix, "", data)
2864 androidMk := builder.String()
2865 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2866}
2867
Jooyung Han2ed99d02020-06-24 23:26:26 +09002868func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002869 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002870 apex {
2871 name: "myapex",
2872 key: "myapex.key",
2873 vintf_fragments: ["fragment.xml"],
2874 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002875 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002876 }
2877 apex_key {
2878 name: "myapex.key",
2879 public_key: "testkey.avbpubkey",
2880 private_key: "testkey.pem",
2881 }
2882 cc_binary {
2883 name: "mybin",
2884 }
2885 `)
2886
2887 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002888 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002889 name := apexBundle.BaseModuleName()
2890 prefix := "TARGET_"
2891 var builder strings.Builder
2892 data.Custom(&builder, name, prefix, "", data)
2893 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04002894 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04002895 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09002896}
2897
Jiyong Park16e91a02018-12-20 18:18:08 +09002898func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002899 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002900 apex {
2901 name: "myapex",
2902 key: "myapex.key",
2903 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002904 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002905 }
2906
2907 apex_key {
2908 name: "myapex.key",
2909 public_key: "testkey.avbpubkey",
2910 private_key: "testkey.pem",
2911 }
2912
2913 cc_library {
2914 name: "mylib",
2915 srcs: ["mylib.cpp"],
2916 system_shared_libs: [],
2917 stl: "none",
2918 stubs: {
2919 versions: ["1", "2", "3"],
2920 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002921 apex_available: [
2922 "//apex_available:platform",
2923 "myapex",
2924 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002925 }
2926
2927 cc_binary {
2928 name: "not_in_apex",
2929 srcs: ["mylib.cpp"],
2930 static_libs: ["mylib"],
2931 static_executable: true,
2932 system_shared_libs: [],
2933 stl: "none",
2934 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002935 `)
2936
Colin Cross7113d202019-11-20 16:39:12 -08002937 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002938
2939 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002940 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002941}
Jiyong Park9335a262018-12-24 11:31:58 +09002942
2943func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002944 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002945 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002946 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002947 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002948 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002949 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002950 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002951 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09002952 }
2953
2954 cc_library {
2955 name: "mylib",
2956 srcs: ["mylib.cpp"],
2957 system_shared_libs: [],
2958 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002959 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002960 }
2961
2962 apex_key {
2963 name: "myapex.key",
2964 public_key: "testkey.avbpubkey",
2965 private_key: "testkey.pem",
2966 }
2967
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002968 android_app_certificate {
2969 name: "myapex.certificate",
2970 certificate: "testkey",
2971 }
2972
2973 android_app_certificate {
2974 name: "myapex.certificate.override",
2975 certificate: "testkey.override",
2976 }
2977
Jiyong Park9335a262018-12-24 11:31:58 +09002978 `)
2979
2980 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002981 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002982
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002983 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2984 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002985 "vendor/foo/devkeys/testkey.avbpubkey")
2986 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002987 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2988 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002989 "vendor/foo/devkeys/testkey.pem")
2990 }
2991
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002992 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002993 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002994 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002995 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002996 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002997 }
2998}
Jiyong Park58e364a2019-01-19 19:24:06 +09002999
Jooyung Hanf121a652019-12-17 14:30:11 +09003000func TestCertificate(t *testing.T) {
3001 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003002 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003003 apex {
3004 name: "myapex",
3005 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003006 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003007 }
3008 apex_key {
3009 name: "myapex.key",
3010 public_key: "testkey.avbpubkey",
3011 private_key: "testkey.pem",
3012 }`)
3013 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3014 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3015 if actual := rule.Args["certificates"]; actual != expected {
3016 t.Errorf("certificates should be %q, not %q", expected, actual)
3017 }
3018 })
3019 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003020 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003021 apex {
3022 name: "myapex_keytest",
3023 key: "myapex.key",
3024 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003025 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003026 }
3027 apex_key {
3028 name: "myapex.key",
3029 public_key: "testkey.avbpubkey",
3030 private_key: "testkey.pem",
3031 }
3032 android_app_certificate {
3033 name: "myapex.certificate.override",
3034 certificate: "testkey.override",
3035 }`)
3036 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3037 expected := "testkey.override.x509.pem testkey.override.pk8"
3038 if actual := rule.Args["certificates"]; actual != expected {
3039 t.Errorf("certificates should be %q, not %q", expected, actual)
3040 }
3041 })
3042 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003043 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003044 apex {
3045 name: "myapex",
3046 key: "myapex.key",
3047 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003048 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003049 }
3050 apex_key {
3051 name: "myapex.key",
3052 public_key: "testkey.avbpubkey",
3053 private_key: "testkey.pem",
3054 }
3055 android_app_certificate {
3056 name: "myapex.certificate",
3057 certificate: "testkey",
3058 }`)
3059 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3060 expected := "testkey.x509.pem testkey.pk8"
3061 if actual := rule.Args["certificates"]; actual != expected {
3062 t.Errorf("certificates should be %q, not %q", expected, actual)
3063 }
3064 })
3065 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003066 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003067 apex {
3068 name: "myapex_keytest",
3069 key: "myapex.key",
3070 file_contexts: ":myapex-file_contexts",
3071 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003072 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003073 }
3074 apex_key {
3075 name: "myapex.key",
3076 public_key: "testkey.avbpubkey",
3077 private_key: "testkey.pem",
3078 }
3079 android_app_certificate {
3080 name: "myapex.certificate.override",
3081 certificate: "testkey.override",
3082 }`)
3083 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3084 expected := "testkey.override.x509.pem testkey.override.pk8"
3085 if actual := rule.Args["certificates"]; actual != expected {
3086 t.Errorf("certificates should be %q, not %q", expected, actual)
3087 }
3088 })
3089 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003090 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003091 apex {
3092 name: "myapex",
3093 key: "myapex.key",
3094 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003095 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003096 }
3097 apex_key {
3098 name: "myapex.key",
3099 public_key: "testkey.avbpubkey",
3100 private_key: "testkey.pem",
3101 }`)
3102 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3103 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3104 if actual := rule.Args["certificates"]; actual != expected {
3105 t.Errorf("certificates should be %q, not %q", expected, actual)
3106 }
3107 })
3108 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003109 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003110 apex {
3111 name: "myapex_keytest",
3112 key: "myapex.key",
3113 file_contexts: ":myapex-file_contexts",
3114 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003115 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003116 }
3117 apex_key {
3118 name: "myapex.key",
3119 public_key: "testkey.avbpubkey",
3120 private_key: "testkey.pem",
3121 }
3122 android_app_certificate {
3123 name: "myapex.certificate.override",
3124 certificate: "testkey.override",
3125 }`)
3126 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3127 expected := "testkey.override.x509.pem testkey.override.pk8"
3128 if actual := rule.Args["certificates"]; actual != expected {
3129 t.Errorf("certificates should be %q, not %q", expected, actual)
3130 }
3131 })
3132}
3133
Jiyong Park58e364a2019-01-19 19:24:06 +09003134func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003135 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003136 apex {
3137 name: "myapex",
3138 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003139 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003140 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003141 }
3142
3143 apex {
3144 name: "otherapex",
3145 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003146 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003147 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003148 }
3149
3150 apex_key {
3151 name: "myapex.key",
3152 public_key: "testkey.avbpubkey",
3153 private_key: "testkey.pem",
3154 }
3155
3156 cc_library {
3157 name: "mylib",
3158 srcs: ["mylib.cpp"],
3159 system_shared_libs: [],
3160 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003161 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003162 "myapex",
3163 "otherapex",
3164 ],
Jooyung Han24282772020-03-21 23:20:55 +09003165 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003166 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003167 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003168 cc_library {
3169 name: "mylib2",
3170 srcs: ["mylib.cpp"],
3171 system_shared_libs: [],
3172 stl: "none",
3173 apex_available: [
3174 "myapex",
3175 "otherapex",
3176 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003177 static_libs: ["mylib3"],
3178 recovery_available: true,
3179 min_sdk_version: "29",
3180 }
3181 cc_library {
3182 name: "mylib3",
3183 srcs: ["mylib.cpp"],
3184 system_shared_libs: [],
3185 stl: "none",
3186 apex_available: [
3187 "myapex",
3188 "otherapex",
3189 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003190 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003191 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003192 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003193 `)
3194
Jooyung Hanc87a0592020-03-02 17:44:33 +09003195 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003196 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003197 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003198 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003199
Jooyung Hanccce2f22020-03-07 03:45:53 +09003200 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003201 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003202 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003203 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003204
Jooyung Hanccce2f22020-03-07 03:45:53 +09003205 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003206 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003207 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003208 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jiyong Park58e364a2019-01-19 19:24:06 +09003209
Colin Crossaede88c2020-08-11 12:17:01 -07003210 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3211 // each variant defines additional macros to distinguish which apex variant it is built for
3212
3213 // non-APEX variant does not have __ANDROID_APEX__ defined
3214 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3215 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3216
Dan Albertb19953d2020-11-17 15:29:36 -08003217 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003218 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3219 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003220 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003221
Jooyung Hanc87a0592020-03-02 17:44:33 +09003222 // non-APEX variant does not have __ANDROID_APEX__ defined
3223 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3224 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3225
Dan Albertb19953d2020-11-17 15:29:36 -08003226 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003227 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003228 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003229 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003230}
Jiyong Park7e636d02019-01-28 16:16:54 +09003231
3232func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003233 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003234 apex {
3235 name: "myapex",
3236 key: "myapex.key",
3237 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003238 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003239 }
3240
3241 apex_key {
3242 name: "myapex.key",
3243 public_key: "testkey.avbpubkey",
3244 private_key: "testkey.pem",
3245 }
3246
3247 cc_library_headers {
3248 name: "mylib_headers",
3249 export_include_dirs: ["my_include"],
3250 system_shared_libs: [],
3251 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003252 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003253 }
3254
3255 cc_library {
3256 name: "mylib",
3257 srcs: ["mylib.cpp"],
3258 system_shared_libs: [],
3259 stl: "none",
3260 header_libs: ["mylib_headers"],
3261 export_header_lib_headers: ["mylib_headers"],
3262 stubs: {
3263 versions: ["1", "2", "3"],
3264 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003265 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003266 }
3267
3268 cc_library {
3269 name: "otherlib",
3270 srcs: ["mylib.cpp"],
3271 system_shared_libs: [],
3272 stl: "none",
3273 shared_libs: ["mylib"],
3274 }
3275 `)
3276
Colin Cross7113d202019-11-20 16:39:12 -08003277 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003278
3279 // Ensure that the include path of the header lib is exported to 'otherlib'
3280 ensureContains(t, cFlags, "-Imy_include")
3281}
Alex Light9670d332019-01-29 18:07:33 -08003282
Jiyong Park7cd10e32020-01-14 09:22:18 +09003283type fileInApex struct {
3284 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003285 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003286 isLink bool
3287}
3288
Jooyung Hana57af4a2020-01-23 05:36:59 +00003289func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003290 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003291 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003292 copyCmds := apexRule.Args["copy_commands"]
3293 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003294 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003295 for _, cmd := range strings.Split(copyCmds, "&&") {
3296 cmd = strings.TrimSpace(cmd)
3297 if cmd == "" {
3298 continue
3299 }
3300 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003301 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003302 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003303 switch terms[0] {
3304 case "mkdir":
3305 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003306 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003307 t.Fatal("copyCmds contains invalid cp command", cmd)
3308 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003309 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003310 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003311 isLink = false
3312 case "ln":
3313 if len(terms) != 3 && len(terms) != 4 {
3314 // ln LINK TARGET or ln -s LINK TARGET
3315 t.Fatal("copyCmds contains invalid ln command", cmd)
3316 }
3317 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003318 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003319 isLink = true
3320 default:
3321 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3322 }
3323 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003324 index := strings.Index(dst, imageApexDir)
3325 if index == -1 {
3326 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3327 }
3328 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003329 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003330 }
3331 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003332 return ret
3333}
3334
Jooyung Hana57af4a2020-01-23 05:36:59 +00003335func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3336 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003337 var failed bool
3338 var surplus []string
3339 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003340 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003341 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003342 for _, expected := range files {
3343 if matched, _ := path.Match(expected, file.path); matched {
3344 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003345 mactchFound = true
3346 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003347 }
3348 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003349 if !mactchFound {
3350 surplus = append(surplus, file.path)
3351 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003352 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003353
Jooyung Han31c470b2019-10-18 16:26:59 +09003354 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003355 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003356 t.Log("surplus files", surplus)
3357 failed = true
3358 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003359
3360 if len(files) > len(filesMatched) {
3361 var missing []string
3362 for _, expected := range files {
3363 if !filesMatched[expected] {
3364 missing = append(missing, expected)
3365 }
3366 }
3367 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003368 t.Log("missing files", missing)
3369 failed = true
3370 }
3371 if failed {
3372 t.Fail()
3373 }
3374}
3375
Jooyung Han344d5432019-08-23 11:17:39 +09003376func TestVndkApexCurrent(t *testing.T) {
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003377 commonFiles := []string{
Jooyung Hane6436d72020-02-27 13:31:56 +09003378 "lib/libc++.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003379 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003380 "etc/llndk.libraries.29.txt",
3381 "etc/vndkcore.libraries.29.txt",
3382 "etc/vndksp.libraries.29.txt",
3383 "etc/vndkprivate.libraries.29.txt",
3384 "etc/vndkproduct.libraries.29.txt",
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003385 }
3386 testCases := []struct {
3387 vndkVersion string
3388 expectedFiles []string
3389 }{
3390 {
3391 vndkVersion: "current",
3392 expectedFiles: append(commonFiles,
3393 "lib/libvndk.so",
3394 "lib/libvndksp.so",
3395 "lib64/libvndk.so",
3396 "lib64/libvndksp.so"),
3397 },
3398 {
3399 vndkVersion: "",
3400 expectedFiles: append(commonFiles,
3401 // Legacy VNDK APEX contains only VNDK-SP files (of core variant)
3402 "lib/libvndksp.so",
3403 "lib64/libvndksp.so"),
3404 },
3405 }
3406 for _, tc := range testCases {
3407 t.Run("VNDK.current with DeviceVndkVersion="+tc.vndkVersion, func(t *testing.T) {
3408 ctx := testApex(t, `
3409 apex_vndk {
3410 name: "com.android.vndk.current",
3411 key: "com.android.vndk.current.key",
3412 updatable: false,
3413 }
3414
3415 apex_key {
3416 name: "com.android.vndk.current.key",
3417 public_key: "testkey.avbpubkey",
3418 private_key: "testkey.pem",
3419 }
3420
3421 cc_library {
3422 name: "libvndk",
3423 srcs: ["mylib.cpp"],
3424 vendor_available: true,
3425 product_available: true,
3426 vndk: {
3427 enabled: true,
3428 },
3429 system_shared_libs: [],
3430 stl: "none",
3431 apex_available: [ "com.android.vndk.current" ],
3432 }
3433
3434 cc_library {
3435 name: "libvndksp",
3436 srcs: ["mylib.cpp"],
3437 vendor_available: true,
3438 product_available: true,
3439 vndk: {
3440 enabled: true,
3441 support_system_process: true,
3442 },
3443 system_shared_libs: [],
3444 stl: "none",
3445 apex_available: [ "com.android.vndk.current" ],
3446 }
3447
3448 // VNDK-Ext should not cause any problems
3449
3450 cc_library {
3451 name: "libvndk.ext",
3452 srcs: ["mylib2.cpp"],
3453 vendor: true,
3454 vndk: {
3455 enabled: true,
3456 extends: "libvndk",
3457 },
3458 system_shared_libs: [],
3459 stl: "none",
3460 }
3461
3462 cc_library {
3463 name: "libvndksp.ext",
3464 srcs: ["mylib2.cpp"],
3465 vendor: true,
3466 vndk: {
3467 enabled: true,
3468 support_system_process: true,
3469 extends: "libvndksp",
3470 },
3471 system_shared_libs: [],
3472 stl: "none",
3473 }
3474 `+vndkLibrariesTxtFiles("current"), android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3475 variables.DeviceVndkVersion = proptools.StringPtr(tc.vndkVersion)
3476 }))
3477 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", tc.expectedFiles)
3478 })
3479 }
Jooyung Han344d5432019-08-23 11:17:39 +09003480}
3481
3482func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003483 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003484 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003485 name: "com.android.vndk.current",
3486 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003487 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003488 }
3489
3490 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003491 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003492 public_key: "testkey.avbpubkey",
3493 private_key: "testkey.pem",
3494 }
3495
3496 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003497 name: "libvndk",
3498 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003499 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003500 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003501 vndk: {
3502 enabled: true,
3503 },
3504 system_shared_libs: [],
3505 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003506 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003507 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003508
3509 cc_prebuilt_library_shared {
3510 name: "libvndk.arm",
3511 srcs: ["libvndk.arm.so"],
3512 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003513 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003514 vndk: {
3515 enabled: true,
3516 },
3517 enabled: false,
3518 arch: {
3519 arm: {
3520 enabled: true,
3521 },
3522 },
3523 system_shared_libs: [],
3524 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003525 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003526 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003527 `+vndkLibrariesTxtFiles("current"),
3528 withFiles(map[string][]byte{
3529 "libvndk.so": nil,
3530 "libvndk.arm.so": nil,
3531 }))
Colin Cross2807f002021-03-02 10:15:29 -08003532 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003533 "lib/libvndk.so",
3534 "lib/libvndk.arm.so",
3535 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003536 "lib/libc++.so",
3537 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003538 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003539 })
Jooyung Han344d5432019-08-23 11:17:39 +09003540}
3541
Jooyung Han39edb6c2019-11-06 16:53:07 +09003542func vndkLibrariesTxtFiles(vers ...string) (result string) {
3543 for _, v := range vers {
3544 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003545 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003546 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003547 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003548 name: "` + txt + `.libraries.txt",
3549 }
3550 `
3551 }
3552 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003553 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003554 result += `
3555 prebuilt_etc {
3556 name: "` + txt + `.libraries.` + v + `.txt",
3557 src: "dummy.txt",
3558 }
3559 `
3560 }
3561 }
3562 }
3563 return
3564}
3565
Jooyung Han344d5432019-08-23 11:17:39 +09003566func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003567 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003568 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003569 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003570 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003571 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003572 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003573 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003574 }
3575
3576 apex_key {
3577 name: "myapex.key",
3578 public_key: "testkey.avbpubkey",
3579 private_key: "testkey.pem",
3580 }
3581
Jooyung Han31c470b2019-10-18 16:26:59 +09003582 vndk_prebuilt_shared {
3583 name: "libvndk27",
3584 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003585 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003586 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003587 vndk: {
3588 enabled: true,
3589 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003590 target_arch: "arm64",
3591 arch: {
3592 arm: {
3593 srcs: ["libvndk27_arm.so"],
3594 },
3595 arm64: {
3596 srcs: ["libvndk27_arm64.so"],
3597 },
3598 },
Colin Cross2807f002021-03-02 10:15:29 -08003599 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003600 }
3601
3602 vndk_prebuilt_shared {
3603 name: "libvndk27",
3604 version: "27",
3605 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003606 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003607 vndk: {
3608 enabled: true,
3609 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003610 target_arch: "x86_64",
3611 arch: {
3612 x86: {
3613 srcs: ["libvndk27_x86.so"],
3614 },
3615 x86_64: {
3616 srcs: ["libvndk27_x86_64.so"],
3617 },
3618 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003619 }
3620 `+vndkLibrariesTxtFiles("27"),
3621 withFiles(map[string][]byte{
3622 "libvndk27_arm.so": nil,
3623 "libvndk27_arm64.so": nil,
3624 "libvndk27_x86.so": nil,
3625 "libvndk27_x86_64.so": nil,
3626 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003627
Colin Cross2807f002021-03-02 10:15:29 -08003628 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003629 "lib/libvndk27_arm.so",
3630 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003631 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003632 })
Jooyung Han344d5432019-08-23 11:17:39 +09003633}
3634
Jooyung Han90eee022019-10-01 20:02:42 +09003635func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003636 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003637 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003638 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003639 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003640 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003641 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003642 }
3643 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003644 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003645 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003646 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003647 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003648 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003649 }
3650 apex_key {
3651 name: "myapex.key",
3652 public_key: "testkey.avbpubkey",
3653 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003654 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003655
3656 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003657 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003658 actual := proptools.String(bundle.properties.Apex_name)
3659 if !reflect.DeepEqual(actual, expected) {
3660 t.Errorf("Got '%v', expected '%v'", actual, expected)
3661 }
3662 }
3663
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003664 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003665 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003666}
3667
Jooyung Han344d5432019-08-23 11:17:39 +09003668func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003669 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003670 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003671 name: "com.android.vndk.current",
3672 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003673 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003674 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003675 }
3676
3677 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003678 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003679 public_key: "testkey.avbpubkey",
3680 private_key: "testkey.pem",
3681 }
3682
3683 cc_library {
3684 name: "libvndk",
3685 srcs: ["mylib.cpp"],
3686 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003687 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003688 native_bridge_supported: true,
3689 host_supported: true,
3690 vndk: {
3691 enabled: true,
3692 },
3693 system_shared_libs: [],
3694 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003695 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003696 }
Colin Cross2807f002021-03-02 10:15:29 -08003697 `+vndkLibrariesTxtFiles("current"),
3698 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003699
Colin Cross2807f002021-03-02 10:15:29 -08003700 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003701 "lib/libvndk.so",
3702 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003703 "lib/libc++.so",
3704 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003705 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003706 })
Jooyung Han344d5432019-08-23 11:17:39 +09003707}
3708
3709func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003710 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003711 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003712 name: "com.android.vndk.current",
3713 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003714 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003715 native_bridge_supported: true,
3716 }
3717
3718 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003719 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003720 public_key: "testkey.avbpubkey",
3721 private_key: "testkey.pem",
3722 }
3723
3724 cc_library {
3725 name: "libvndk",
3726 srcs: ["mylib.cpp"],
3727 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003728 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003729 native_bridge_supported: true,
3730 host_supported: true,
3731 vndk: {
3732 enabled: true,
3733 },
3734 system_shared_libs: [],
3735 stl: "none",
3736 }
3737 `)
3738}
3739
Jooyung Han31c470b2019-10-18 16:26:59 +09003740func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003741 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003742 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003743 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003744 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003745 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003746 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003747 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003748 }
3749
3750 apex_key {
3751 name: "myapex.key",
3752 public_key: "testkey.avbpubkey",
3753 private_key: "testkey.pem",
3754 }
3755
3756 vndk_prebuilt_shared {
3757 name: "libvndk27",
3758 version: "27",
3759 target_arch: "arm",
3760 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003761 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003762 vndk: {
3763 enabled: true,
3764 },
3765 arch: {
3766 arm: {
3767 srcs: ["libvndk27.so"],
3768 }
3769 },
3770 }
3771
3772 vndk_prebuilt_shared {
3773 name: "libvndk27",
3774 version: "27",
3775 target_arch: "arm",
3776 binder32bit: true,
3777 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003778 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003779 vndk: {
3780 enabled: true,
3781 },
3782 arch: {
3783 arm: {
3784 srcs: ["libvndk27binder32.so"],
3785 }
3786 },
Colin Cross2807f002021-03-02 10:15:29 -08003787 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003788 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003789 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003790 withFiles(map[string][]byte{
3791 "libvndk27.so": nil,
3792 "libvndk27binder32.so": nil,
3793 }),
3794 withBinder32bit,
3795 withTargets(map[android.OsType][]android.Target{
3796 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003797 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3798 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003799 },
3800 }),
3801 )
3802
Colin Cross2807f002021-03-02 10:15:29 -08003803 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003804 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003805 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003806 })
3807}
3808
Jooyung Han45a96772020-06-15 14:59:42 +09003809func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003810 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003811 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003812 name: "com.android.vndk.current",
3813 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003814 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003815 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003816 }
3817
3818 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003819 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003820 public_key: "testkey.avbpubkey",
3821 private_key: "testkey.pem",
3822 }
3823
3824 cc_library {
3825 name: "libz",
3826 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003827 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003828 vndk: {
3829 enabled: true,
3830 },
3831 stubs: {
3832 symbol_file: "libz.map.txt",
3833 versions: ["30"],
3834 }
3835 }
3836 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3837 "libz.map.txt": nil,
3838 }))
3839
Colin Cross2807f002021-03-02 10:15:29 -08003840 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003841 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3842 ensureListEmpty(t, provideNativeLibs)
3843}
3844
Jooyung Hane1633032019-08-01 17:41:43 +09003845func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003846 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003847 apex {
3848 name: "myapex_nodep",
3849 key: "myapex.key",
3850 native_shared_libs: ["lib_nodep"],
3851 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003852 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003853 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003854 }
3855
3856 apex {
3857 name: "myapex_dep",
3858 key: "myapex.key",
3859 native_shared_libs: ["lib_dep"],
3860 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003861 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003862 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003863 }
3864
3865 apex {
3866 name: "myapex_provider",
3867 key: "myapex.key",
3868 native_shared_libs: ["libfoo"],
3869 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003870 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003871 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003872 }
3873
3874 apex {
3875 name: "myapex_selfcontained",
3876 key: "myapex.key",
3877 native_shared_libs: ["lib_dep", "libfoo"],
3878 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003879 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003880 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003881 }
3882
3883 apex_key {
3884 name: "myapex.key",
3885 public_key: "testkey.avbpubkey",
3886 private_key: "testkey.pem",
3887 }
3888
3889 cc_library {
3890 name: "lib_nodep",
3891 srcs: ["mylib.cpp"],
3892 system_shared_libs: [],
3893 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003894 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003895 }
3896
3897 cc_library {
3898 name: "lib_dep",
3899 srcs: ["mylib.cpp"],
3900 shared_libs: ["libfoo"],
3901 system_shared_libs: [],
3902 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003903 apex_available: [
3904 "myapex_dep",
3905 "myapex_provider",
3906 "myapex_selfcontained",
3907 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003908 }
3909
3910 cc_library {
3911 name: "libfoo",
3912 srcs: ["mytest.cpp"],
3913 stubs: {
3914 versions: ["1"],
3915 },
3916 system_shared_libs: [],
3917 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003918 apex_available: [
3919 "myapex_provider",
3920 "myapex_selfcontained",
3921 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003922 }
3923 `)
3924
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003925 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003926 var provideNativeLibs, requireNativeLibs []string
3927
Sundong Ahnabb64432019-10-22 13:58:29 +09003928 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003929 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3930 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003931 ensureListEmpty(t, provideNativeLibs)
3932 ensureListEmpty(t, requireNativeLibs)
3933
Sundong Ahnabb64432019-10-22 13:58:29 +09003934 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003935 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3936 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003937 ensureListEmpty(t, provideNativeLibs)
3938 ensureListContains(t, requireNativeLibs, "libfoo.so")
3939
Sundong Ahnabb64432019-10-22 13:58:29 +09003940 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003941 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3942 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003943 ensureListContains(t, provideNativeLibs, "libfoo.so")
3944 ensureListEmpty(t, requireNativeLibs)
3945
Sundong Ahnabb64432019-10-22 13:58:29 +09003946 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003947 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3948 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003949 ensureListContains(t, provideNativeLibs, "libfoo.so")
3950 ensureListEmpty(t, requireNativeLibs)
3951}
3952
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003953func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003954 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003955 apex {
3956 name: "myapex",
3957 key: "myapex.key",
3958 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003959 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003960 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003961 }
3962
3963 apex_key {
3964 name: "myapex.key",
3965 public_key: "testkey.avbpubkey",
3966 private_key: "testkey.pem",
3967 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003968
3969 cc_library {
3970 name: "mylib",
3971 srcs: ["mylib.cpp"],
3972 system_shared_libs: [],
3973 stl: "none",
3974 apex_available: [
3975 "//apex_available:platform",
3976 "myapex",
3977 ],
3978 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003979 `)
3980
Martin Stjernholmbfffae72021-06-24 14:37:13 +01003981 module := ctx.ModuleForTests("myapex", "android_common_com.android.myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003982 apexManifestRule := module.Rule("apexManifestRule")
3983 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3984 apexRule := module.Rule("apexRule")
3985 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003986
Martin Stjernholmbfffae72021-06-24 14:37:13 +01003987 apexBundle := module.Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003988 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09003989 name := apexBundle.BaseModuleName()
3990 prefix := "TARGET_"
3991 var builder strings.Builder
3992 data.Custom(&builder, name, prefix, "", data)
3993 androidMk := builder.String()
3994 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3995 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003996}
3997
Alex Light0851b882019-02-07 13:20:53 -08003998func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003999 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004000 apex {
4001 name: "myapex",
4002 key: "myapex.key",
4003 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004004 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004005 }
4006
4007 apex_key {
4008 name: "myapex.key",
4009 public_key: "testkey.avbpubkey",
4010 private_key: "testkey.pem",
4011 }
4012
4013 cc_library {
4014 name: "mylib_common",
4015 srcs: ["mylib.cpp"],
4016 system_shared_libs: [],
4017 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004018 apex_available: [
4019 "//apex_available:platform",
4020 "myapex",
4021 ],
Alex Light0851b882019-02-07 13:20:53 -08004022 }
4023 `)
4024
Sundong Ahnabb64432019-10-22 13:58:29 +09004025 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004026 apexRule := module.Rule("apexRule")
4027 copyCmds := apexRule.Args["copy_commands"]
4028
4029 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4030 t.Log("Apex was a test apex!")
4031 t.Fail()
4032 }
4033 // Ensure that main rule creates an output
4034 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4035
4036 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004037 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004038
4039 // Ensure that both direct and indirect deps are copied into apex
4040 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4041
Colin Cross7113d202019-11-20 16:39:12 -08004042 // Ensure that the platform variant ends with _shared
4043 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004044
Colin Cross56a83212020-09-15 18:30:11 -07004045 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004046 t.Log("Found mylib_common not in any apex!")
4047 t.Fail()
4048 }
4049}
4050
4051func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004052 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004053 apex_test {
4054 name: "myapex",
4055 key: "myapex.key",
4056 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004057 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004058 }
4059
4060 apex_key {
4061 name: "myapex.key",
4062 public_key: "testkey.avbpubkey",
4063 private_key: "testkey.pem",
4064 }
4065
4066 cc_library {
4067 name: "mylib_common_test",
4068 srcs: ["mylib.cpp"],
4069 system_shared_libs: [],
4070 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004071 // TODO: remove //apex_available:platform
4072 apex_available: [
4073 "//apex_available:platform",
4074 "myapex",
4075 ],
Alex Light0851b882019-02-07 13:20:53 -08004076 }
4077 `)
4078
Sundong Ahnabb64432019-10-22 13:58:29 +09004079 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004080 apexRule := module.Rule("apexRule")
4081 copyCmds := apexRule.Args["copy_commands"]
4082
4083 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4084 t.Log("Apex was not a test apex!")
4085 t.Fail()
4086 }
4087 // Ensure that main rule creates an output
4088 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4089
4090 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004091 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004092
4093 // Ensure that both direct and indirect deps are copied into apex
4094 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4095
Colin Cross7113d202019-11-20 16:39:12 -08004096 // Ensure that the platform variant ends with _shared
4097 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004098}
4099
Alex Light9670d332019-01-29 18:07:33 -08004100func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004101 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004102 apex {
4103 name: "myapex",
4104 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004105 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004106 multilib: {
4107 first: {
4108 native_shared_libs: ["mylib_common"],
4109 }
4110 },
4111 target: {
4112 android: {
4113 multilib: {
4114 first: {
4115 native_shared_libs: ["mylib"],
4116 }
4117 }
4118 },
4119 host: {
4120 multilib: {
4121 first: {
4122 native_shared_libs: ["mylib2"],
4123 }
4124 }
4125 }
4126 }
4127 }
4128
4129 apex_key {
4130 name: "myapex.key",
4131 public_key: "testkey.avbpubkey",
4132 private_key: "testkey.pem",
4133 }
4134
4135 cc_library {
4136 name: "mylib",
4137 srcs: ["mylib.cpp"],
4138 system_shared_libs: [],
4139 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004140 // TODO: remove //apex_available:platform
4141 apex_available: [
4142 "//apex_available:platform",
4143 "myapex",
4144 ],
Alex Light9670d332019-01-29 18:07:33 -08004145 }
4146
4147 cc_library {
4148 name: "mylib_common",
4149 srcs: ["mylib.cpp"],
4150 system_shared_libs: [],
4151 stl: "none",
4152 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004153 // TODO: remove //apex_available:platform
4154 apex_available: [
4155 "//apex_available:platform",
4156 "myapex",
4157 ],
Alex Light9670d332019-01-29 18:07:33 -08004158 }
4159
4160 cc_library {
4161 name: "mylib2",
4162 srcs: ["mylib.cpp"],
4163 system_shared_libs: [],
4164 stl: "none",
4165 compile_multilib: "first",
4166 }
4167 `)
4168
Sundong Ahnabb64432019-10-22 13:58:29 +09004169 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004170 copyCmds := apexRule.Args["copy_commands"]
4171
4172 // Ensure that main rule creates an output
4173 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4174
4175 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004176 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4177 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4178 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004179
4180 // Ensure that both direct and indirect deps are copied into apex
4181 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4182 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4183 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4184
Colin Cross7113d202019-11-20 16:39:12 -08004185 // Ensure that the platform variant ends with _shared
4186 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4187 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4188 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004189}
Jiyong Park04480cf2019-02-06 00:16:29 +09004190
Jiyong Park59140302020-12-14 18:44:04 +09004191func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004192 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004193 apex {
4194 name: "myapex",
4195 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004196 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09004197 arch: {
4198 arm64: {
4199 native_shared_libs: ["mylib.arm64"],
4200 },
4201 x86_64: {
4202 native_shared_libs: ["mylib.x64"],
4203 },
4204 }
4205 }
4206
4207 apex_key {
4208 name: "myapex.key",
4209 public_key: "testkey.avbpubkey",
4210 private_key: "testkey.pem",
4211 }
4212
4213 cc_library {
4214 name: "mylib.arm64",
4215 srcs: ["mylib.cpp"],
4216 system_shared_libs: [],
4217 stl: "none",
4218 // TODO: remove //apex_available:platform
4219 apex_available: [
4220 "//apex_available:platform",
4221 "myapex",
4222 ],
4223 }
4224
4225 cc_library {
4226 name: "mylib.x64",
4227 srcs: ["mylib.cpp"],
4228 system_shared_libs: [],
4229 stl: "none",
4230 // TODO: remove //apex_available:platform
4231 apex_available: [
4232 "//apex_available:platform",
4233 "myapex",
4234 ],
4235 }
4236 `)
4237
4238 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4239 copyCmds := apexRule.Args["copy_commands"]
4240
4241 // Ensure that apex variant is created for the direct dep
4242 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4243 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4244
4245 // Ensure that both direct and indirect deps are copied into apex
4246 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4247 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4248}
4249
Jiyong Park04480cf2019-02-06 00:16:29 +09004250func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004251 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004252 apex {
4253 name: "myapex",
4254 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004255 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004256 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004257 }
4258
4259 apex_key {
4260 name: "myapex.key",
4261 public_key: "testkey.avbpubkey",
4262 private_key: "testkey.pem",
4263 }
4264
4265 sh_binary {
4266 name: "myscript",
4267 src: "mylib.cpp",
4268 filename: "myscript.sh",
4269 sub_dir: "script",
4270 }
4271 `)
4272
Sundong Ahnabb64432019-10-22 13:58:29 +09004273 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004274 copyCmds := apexRule.Args["copy_commands"]
4275
4276 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4277}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004278
Jooyung Han91df2082019-11-20 01:49:42 +09004279func TestApexInVariousPartition(t *testing.T) {
4280 testcases := []struct {
4281 propName, parition, flattenedPartition string
4282 }{
4283 {"", "system", "system_ext"},
4284 {"product_specific: true", "product", "product"},
4285 {"soc_specific: true", "vendor", "vendor"},
4286 {"proprietary: true", "vendor", "vendor"},
4287 {"vendor: true", "vendor", "vendor"},
4288 {"system_ext_specific: true", "system_ext", "system_ext"},
4289 }
4290 for _, tc := range testcases {
4291 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004292 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004293 apex {
4294 name: "myapex",
4295 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004296 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004297 `+tc.propName+`
4298 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004299
Jooyung Han91df2082019-11-20 01:49:42 +09004300 apex_key {
4301 name: "myapex.key",
4302 public_key: "testkey.avbpubkey",
4303 private_key: "testkey.pem",
4304 }
4305 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004306
Jooyung Han91df2082019-11-20 01:49:42 +09004307 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004308 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4309 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004310 if actual != expected {
4311 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4312 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004313
Jooyung Han91df2082019-11-20 01:49:42 +09004314 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004315 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4316 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004317 if actual != expected {
4318 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4319 }
4320 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004321 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004322}
Jiyong Park67882562019-03-21 01:11:21 +09004323
Jooyung Han580eb4f2020-06-24 19:33:06 +09004324func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004325 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004326 apex {
4327 name: "myapex",
4328 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004329 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004330 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004331
Jooyung Han580eb4f2020-06-24 19:33:06 +09004332 apex_key {
4333 name: "myapex.key",
4334 public_key: "testkey.avbpubkey",
4335 private_key: "testkey.pem",
4336 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004337 `)
4338 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004339 rule := module.Output("file_contexts")
4340 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4341}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004342
Jooyung Han580eb4f2020-06-24 19:33:06 +09004343func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004344 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004345 apex {
4346 name: "myapex",
4347 key: "myapex.key",
4348 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004349 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004350 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004351
Jooyung Han580eb4f2020-06-24 19:33:06 +09004352 apex_key {
4353 name: "myapex.key",
4354 public_key: "testkey.avbpubkey",
4355 private_key: "testkey.pem",
4356 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004357 `, withFiles(map[string][]byte{
4358 "my_own_file_contexts": nil,
4359 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004360}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004361
Jooyung Han580eb4f2020-06-24 19:33:06 +09004362func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004363 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004364 apex {
4365 name: "myapex",
4366 key: "myapex.key",
4367 product_specific: true,
4368 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004369 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004370 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004371
Jooyung Han580eb4f2020-06-24 19:33:06 +09004372 apex_key {
4373 name: "myapex.key",
4374 public_key: "testkey.avbpubkey",
4375 private_key: "testkey.pem",
4376 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004377 `)
4378
Colin Cross1c460562021-02-16 17:55:47 -08004379 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004380 apex {
4381 name: "myapex",
4382 key: "myapex.key",
4383 product_specific: true,
4384 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004385 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004386 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004387
Jooyung Han580eb4f2020-06-24 19:33:06 +09004388 apex_key {
4389 name: "myapex.key",
4390 public_key: "testkey.avbpubkey",
4391 private_key: "testkey.pem",
4392 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004393 `, withFiles(map[string][]byte{
4394 "product_specific_file_contexts": nil,
4395 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004396 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4397 rule := module.Output("file_contexts")
4398 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4399}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004400
Jooyung Han580eb4f2020-06-24 19:33:06 +09004401func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004402 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004403 apex {
4404 name: "myapex",
4405 key: "myapex.key",
4406 product_specific: true,
4407 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004408 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004409 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004410
Jooyung Han580eb4f2020-06-24 19:33:06 +09004411 apex_key {
4412 name: "myapex.key",
4413 public_key: "testkey.avbpubkey",
4414 private_key: "testkey.pem",
4415 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004416
Jooyung Han580eb4f2020-06-24 19:33:06 +09004417 filegroup {
4418 name: "my-file-contexts",
4419 srcs: ["product_specific_file_contexts"],
4420 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004421 `, withFiles(map[string][]byte{
4422 "product_specific_file_contexts": nil,
4423 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004424 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4425 rule := module.Output("file_contexts")
4426 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004427}
4428
Jiyong Park67882562019-03-21 01:11:21 +09004429func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004430 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004431 apex_key {
4432 name: "myapex.key",
4433 public_key: ":my.avbpubkey",
4434 private_key: ":my.pem",
4435 product_specific: true,
4436 }
4437
4438 filegroup {
4439 name: "my.avbpubkey",
4440 srcs: ["testkey2.avbpubkey"],
4441 }
4442
4443 filegroup {
4444 name: "my.pem",
4445 srcs: ["testkey2.pem"],
4446 }
4447 `)
4448
4449 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4450 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004451 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004452 if actual_pubkey != expected_pubkey {
4453 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4454 }
4455 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004456 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004457 if actual_privkey != expected_privkey {
4458 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4459 }
4460}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004461
4462func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004463 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004464 prebuilt_apex {
4465 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004466 arch: {
4467 arm64: {
4468 src: "myapex-arm64.apex",
4469 },
4470 arm: {
4471 src: "myapex-arm.apex",
4472 },
4473 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004474 }
4475 `)
4476
Paul Duffin6717d882021-06-15 19:09:41 +01004477 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004478
Jiyong Parkc95714e2019-03-29 14:23:10 +09004479 expectedInput := "myapex-arm64.apex"
4480 if prebuilt.inputApex.String() != expectedInput {
4481 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4482 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004483}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004484
Paul Duffinc0609c62021-03-01 17:27:16 +00004485func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004486 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004487 prebuilt_apex {
4488 name: "myapex",
4489 }
4490 `)
4491}
4492
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004493func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004494 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004495 prebuilt_apex {
4496 name: "myapex",
4497 src: "myapex-arm.apex",
4498 filename: "notmyapex.apex",
4499 }
4500 `)
4501
Paul Duffin6717d882021-06-15 19:09:41 +01004502 p := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004503
4504 expected := "notmyapex.apex"
4505 if p.installFilename != expected {
4506 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4507 }
4508}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004509
Samiul Islam7c02e262021-09-08 17:48:28 +01004510func TestApexSetFilenameOverride(t *testing.T) {
4511 testApex(t, `
4512 apex_set {
4513 name: "com.company.android.myapex",
4514 apex_name: "com.android.myapex",
4515 set: "company-myapex.apks",
4516 filename: "com.company.android.myapex.apex"
4517 }
4518 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4519
4520 testApex(t, `
4521 apex_set {
4522 name: "com.company.android.myapex",
4523 apex_name: "com.android.myapex",
4524 set: "company-myapex.apks",
4525 filename: "com.company.android.myapex.capex"
4526 }
4527 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4528
4529 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4530 apex_set {
4531 name: "com.company.android.myapex",
4532 apex_name: "com.android.myapex",
4533 set: "company-myapex.apks",
4534 filename: "some-random-suffix"
4535 }
4536 `)
4537}
4538
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004539func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004540 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004541 prebuilt_apex {
4542 name: "myapex.prebuilt",
4543 src: "myapex-arm.apex",
4544 overrides: [
4545 "myapex",
4546 ],
4547 }
4548 `)
4549
Paul Duffin6717d882021-06-15 19:09:41 +01004550 p := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt").Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004551
4552 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004553 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004554 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004555 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004556 }
4557}
4558
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004559func TestPrebuiltApexName(t *testing.T) {
4560 testApex(t, `
4561 prebuilt_apex {
4562 name: "com.company.android.myapex",
4563 apex_name: "com.android.myapex",
4564 src: "company-myapex-arm.apex",
4565 }
4566 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4567
4568 testApex(t, `
4569 apex_set {
4570 name: "com.company.android.myapex",
4571 apex_name: "com.android.myapex",
4572 set: "company-myapex.apks",
4573 }
4574 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4575}
4576
4577func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4578 _ = android.GroupFixturePreparers(
4579 java.PrepareForTestWithJavaDefaultModules,
4580 PrepareForTestWithApexBuildComponents,
4581 android.FixtureWithRootAndroidBp(`
4582 platform_bootclasspath {
4583 name: "platform-bootclasspath",
4584 fragments: [
4585 {
4586 apex: "com.android.art",
4587 module: "art-bootclasspath-fragment",
4588 },
4589 ],
4590 }
4591
4592 prebuilt_apex {
4593 name: "com.company.android.art",
4594 apex_name: "com.android.art",
4595 src: "com.company.android.art-arm.apex",
4596 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4597 }
4598
4599 prebuilt_bootclasspath_fragment {
4600 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004601 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004602 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004603 hidden_api: {
4604 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4605 metadata: "my-bootclasspath-fragment/metadata.csv",
4606 index: "my-bootclasspath-fragment/index.csv",
4607 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4608 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4609 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004610 }
4611
4612 java_import {
4613 name: "core-oj",
4614 jars: ["prebuilt.jar"],
4615 }
4616 `),
4617 ).RunTest(t)
4618}
4619
Paul Duffin092153d2021-01-26 11:42:39 +00004620// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4621// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004622func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004623 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004624
Paul Duffin89886cb2021-02-05 16:44:03 +00004625 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004626 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004627 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004628 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004629 dexJarBuildPath := p.DexJarBuildPath().PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004630 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004631 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
4632 ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
4633 android.NormalizePathForTesting(dexJarBuildPath))
4634 }
4635
4636 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004637 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004638 // Make sure the import has been given the correct path to the dex jar.
4639 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4640 dexJarBuildPath := p.DexJarInstallPath()
4641 stem := android.RemoveOptionalPrebuiltPrefix(name)
4642 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
4643 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
4644 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00004645 }
4646
Paul Duffin39853512021-02-26 11:09:39 +00004647 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004648 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004649 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09004650 android.AssertArrayString(t, "Check if there is no source variant",
4651 []string{"android_common"},
4652 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00004653 }
4654
4655 t.Run("prebuilt only", func(t *testing.T) {
4656 bp := `
4657 prebuilt_apex {
4658 name: "myapex",
4659 arch: {
4660 arm64: {
4661 src: "myapex-arm64.apex",
4662 },
4663 arm: {
4664 src: "myapex-arm.apex",
4665 },
4666 },
Paul Duffin39853512021-02-26 11:09:39 +00004667 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004668 }
4669
4670 java_import {
4671 name: "libfoo",
4672 jars: ["libfoo.jar"],
4673 }
Paul Duffin39853512021-02-26 11:09:39 +00004674
4675 java_sdk_library_import {
4676 name: "libbar",
4677 public: {
4678 jars: ["libbar.jar"],
4679 },
4680 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004681 `
4682
4683 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4684 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4685
Martin Stjernholm44825602021-09-17 01:44:12 +01004686 deapexerName := deapexerModuleName("myapex")
4687 android.AssertStringEquals(t, "APEX module name from deapexer name", "myapex", apexModuleName(deapexerName))
4688
Paul Duffinf6932af2021-02-26 18:21:56 +00004689 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01004690 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00004691 rule := deapexer.Rule("deapexer")
4692 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4693 t.Errorf("expected: %q, found: %q", expected, actual)
4694 }
4695
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004696 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01004697 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004698 rule = prebuiltApex.Rule("android/soong/android.Cp")
4699 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4700 t.Errorf("expected: %q, found: %q", expected, actual)
4701 }
4702
Paul Duffin89886cb2021-02-05 16:44:03 +00004703 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004704 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004705
4706 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004707 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004708 })
4709
4710 t.Run("prebuilt with source preferred", func(t *testing.T) {
4711
4712 bp := `
4713 prebuilt_apex {
4714 name: "myapex",
4715 arch: {
4716 arm64: {
4717 src: "myapex-arm64.apex",
4718 },
4719 arm: {
4720 src: "myapex-arm.apex",
4721 },
4722 },
Paul Duffin39853512021-02-26 11:09:39 +00004723 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004724 }
4725
4726 java_import {
4727 name: "libfoo",
4728 jars: ["libfoo.jar"],
4729 }
4730
4731 java_library {
4732 name: "libfoo",
4733 }
Paul Duffin39853512021-02-26 11:09:39 +00004734
4735 java_sdk_library_import {
4736 name: "libbar",
4737 public: {
4738 jars: ["libbar.jar"],
4739 },
4740 }
4741
4742 java_sdk_library {
4743 name: "libbar",
4744 srcs: ["foo/bar/MyClass.java"],
4745 unsafe_ignore_missing_latest_api: true,
4746 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004747 `
4748
4749 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4750 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4751
Paul Duffin89886cb2021-02-05 16:44:03 +00004752 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004753 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004754 ensureNoSourceVariant(t, ctx, "libfoo")
4755
4756 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004757 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00004758 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004759 })
4760
4761 t.Run("prebuilt preferred with source", func(t *testing.T) {
4762 bp := `
4763 prebuilt_apex {
4764 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004765 arch: {
4766 arm64: {
4767 src: "myapex-arm64.apex",
4768 },
4769 arm: {
4770 src: "myapex-arm.apex",
4771 },
4772 },
Paul Duffin39853512021-02-26 11:09:39 +00004773 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004774 }
4775
4776 java_import {
4777 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004778 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004779 jars: ["libfoo.jar"],
4780 }
4781
4782 java_library {
4783 name: "libfoo",
4784 }
Paul Duffin39853512021-02-26 11:09:39 +00004785
4786 java_sdk_library_import {
4787 name: "libbar",
4788 prefer: true,
4789 public: {
4790 jars: ["libbar.jar"],
4791 },
4792 }
4793
4794 java_sdk_library {
4795 name: "libbar",
4796 srcs: ["foo/bar/MyClass.java"],
4797 unsafe_ignore_missing_latest_api: true,
4798 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004799 `
4800
4801 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4802 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4803
Paul Duffin89886cb2021-02-05 16:44:03 +00004804 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004805 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004806 ensureNoSourceVariant(t, ctx, "libfoo")
4807
4808 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004809 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00004810 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004811 })
4812}
4813
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004814func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01004815 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01004816 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01004817 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
4818 // is disabled.
4819 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
4820 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004821
Paul Duffin37856732021-02-26 14:24:15 +00004822 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4823 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01004824 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004825 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004826 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004827 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004828 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004829 foundLibfooJar = true
4830 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01004831 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004832 }
4833 }
4834 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02004835 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004836 }
4837 }
4838
Paul Duffin40a3f652021-07-19 13:11:24 +01004839 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004840 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01004841 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01004842 var rule android.TestingBuildParams
4843
4844 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
4845 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00004846 }
4847
Paul Duffin40a3f652021-07-19 13:11:24 +01004848 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
4849 t.Helper()
4850 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
4851 var rule android.TestingBuildParams
4852
4853 rule = platformBootclasspath.Output("hiddenapi-index.csv")
4854 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
4855 }
4856
Paul Duffin89f570a2021-06-16 01:42:33 +01004857 fragment := java.ApexVariantReference{
4858 Apex: proptools.StringPtr("myapex"),
4859 Module: proptools.StringPtr("my-bootclasspath-fragment"),
4860 }
4861
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004862 t.Run("prebuilt only", func(t *testing.T) {
4863 bp := `
4864 prebuilt_apex {
4865 name: "myapex",
4866 arch: {
4867 arm64: {
4868 src: "myapex-arm64.apex",
4869 },
4870 arm: {
4871 src: "myapex-arm.apex",
4872 },
4873 },
Paul Duffin89f570a2021-06-16 01:42:33 +01004874 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4875 }
4876
4877 prebuilt_bootclasspath_fragment {
4878 name: "my-bootclasspath-fragment",
4879 contents: ["libfoo", "libbar"],
4880 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01004881 hidden_api: {
4882 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4883 metadata: "my-bootclasspath-fragment/metadata.csv",
4884 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01004885 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
4886 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
4887 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01004888 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004889 }
4890
4891 java_import {
4892 name: "libfoo",
4893 jars: ["libfoo.jar"],
4894 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01004895 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004896 }
Paul Duffin37856732021-02-26 14:24:15 +00004897
4898 java_sdk_library_import {
4899 name: "libbar",
4900 public: {
4901 jars: ["libbar.jar"],
4902 },
4903 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01004904 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01004905 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00004906 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004907 `
4908
Paul Duffin89f570a2021-06-16 01:42:33 +01004909 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004910 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4911 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004912
Paul Duffin537ea3d2021-05-14 10:38:00 +01004913 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01004914 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01004915 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01004916 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01004917 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
4918 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004919 })
4920
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004921 t.Run("apex_set only", func(t *testing.T) {
4922 bp := `
4923 apex_set {
4924 name: "myapex",
4925 set: "myapex.apks",
Paul Duffin89f570a2021-06-16 01:42:33 +01004926 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4927 }
4928
4929 prebuilt_bootclasspath_fragment {
4930 name: "my-bootclasspath-fragment",
4931 contents: ["libfoo", "libbar"],
4932 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01004933 hidden_api: {
4934 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4935 metadata: "my-bootclasspath-fragment/metadata.csv",
4936 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01004937 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
4938 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
4939 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01004940 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004941 }
4942
4943 java_import {
4944 name: "libfoo",
4945 jars: ["libfoo.jar"],
4946 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01004947 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004948 }
4949
4950 java_sdk_library_import {
4951 name: "libbar",
4952 public: {
4953 jars: ["libbar.jar"],
4954 },
4955 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01004956 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01004957 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004958 }
4959 `
4960
Paul Duffin89f570a2021-06-16 01:42:33 +01004961 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004962 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4963 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
4964
Paul Duffin537ea3d2021-05-14 10:38:00 +01004965 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01004966 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01004967 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01004968 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01004969 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
4970 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004971 })
4972
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004973 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4974 bp := `
4975 prebuilt_apex {
4976 name: "myapex",
4977 arch: {
4978 arm64: {
4979 src: "myapex-arm64.apex",
4980 },
4981 arm: {
4982 src: "myapex-arm.apex",
4983 },
4984 },
Paul Duffin89f570a2021-06-16 01:42:33 +01004985 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4986 }
4987
4988 prebuilt_bootclasspath_fragment {
4989 name: "my-bootclasspath-fragment",
4990 contents: ["libfoo", "libbar"],
4991 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01004992 hidden_api: {
4993 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4994 metadata: "my-bootclasspath-fragment/metadata.csv",
4995 index: "my-bootclasspath-fragment/index.csv",
4996 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4997 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4998 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004999 }
5000
5001 java_import {
5002 name: "libfoo",
5003 jars: ["libfoo.jar"],
5004 apex_available: ["myapex"],
5005 }
5006
5007 java_library {
5008 name: "libfoo",
5009 srcs: ["foo/bar/MyClass.java"],
5010 apex_available: ["myapex"],
5011 }
Paul Duffin37856732021-02-26 14:24:15 +00005012
5013 java_sdk_library_import {
5014 name: "libbar",
5015 public: {
5016 jars: ["libbar.jar"],
5017 },
5018 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005019 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005020 }
5021
5022 java_sdk_library {
5023 name: "libbar",
5024 srcs: ["foo/bar/MyClass.java"],
5025 unsafe_ignore_missing_latest_api: true,
5026 apex_available: ["myapex"],
5027 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005028 `
5029
5030 // In this test the source (java_library) libfoo is active since the
5031 // prebuilt (java_import) defaults to prefer:false. However the
5032 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5033 // find the dex boot jar in it. We either need to disable the source libfoo
5034 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005035 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005036 // dexbootjar check is skipped if AllowMissingDependencies is true
5037 preparerAllowMissingDeps := android.GroupFixturePreparers(
5038 preparer,
5039 android.PrepareForTestWithAllowMissingDependencies,
5040 )
5041 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005042 })
5043
5044 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5045 bp := `
5046 prebuilt_apex {
5047 name: "myapex",
5048 arch: {
5049 arm64: {
5050 src: "myapex-arm64.apex",
5051 },
5052 arm: {
5053 src: "myapex-arm.apex",
5054 },
5055 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005056 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5057 }
5058
5059 prebuilt_bootclasspath_fragment {
5060 name: "my-bootclasspath-fragment",
5061 contents: ["libfoo", "libbar"],
5062 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005063 hidden_api: {
5064 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5065 metadata: "my-bootclasspath-fragment/metadata.csv",
5066 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005067 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5068 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5069 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005070 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005071 }
5072
5073 java_import {
5074 name: "libfoo",
5075 prefer: true,
5076 jars: ["libfoo.jar"],
5077 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005078 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005079 }
5080
5081 java_library {
5082 name: "libfoo",
5083 srcs: ["foo/bar/MyClass.java"],
5084 apex_available: ["myapex"],
5085 }
Paul Duffin37856732021-02-26 14:24:15 +00005086
5087 java_sdk_library_import {
5088 name: "libbar",
5089 prefer: true,
5090 public: {
5091 jars: ["libbar.jar"],
5092 },
5093 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005094 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005095 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005096 }
5097
5098 java_sdk_library {
5099 name: "libbar",
5100 srcs: ["foo/bar/MyClass.java"],
5101 unsafe_ignore_missing_latest_api: true,
5102 apex_available: ["myapex"],
5103 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005104 `
5105
Paul Duffin89f570a2021-06-16 01:42:33 +01005106 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005107 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5108 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005109
Paul Duffin537ea3d2021-05-14 10:38:00 +01005110 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005111 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005112 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005113 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005114 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5115 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005116 })
5117
5118 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5119 bp := `
5120 apex {
5121 name: "myapex",
5122 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00005123 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005124 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005125 }
5126
5127 apex_key {
5128 name: "myapex.key",
5129 public_key: "testkey.avbpubkey",
5130 private_key: "testkey.pem",
5131 }
5132
5133 prebuilt_apex {
5134 name: "myapex",
5135 arch: {
5136 arm64: {
5137 src: "myapex-arm64.apex",
5138 },
5139 arm: {
5140 src: "myapex-arm.apex",
5141 },
5142 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005143 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5144 }
5145
5146 prebuilt_bootclasspath_fragment {
5147 name: "my-bootclasspath-fragment",
5148 contents: ["libfoo", "libbar"],
5149 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005150 hidden_api: {
5151 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5152 metadata: "my-bootclasspath-fragment/metadata.csv",
5153 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005154 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5155 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5156 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005157 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005158 }
5159
5160 java_import {
5161 name: "libfoo",
5162 jars: ["libfoo.jar"],
5163 apex_available: ["myapex"],
5164 }
5165
5166 java_library {
5167 name: "libfoo",
5168 srcs: ["foo/bar/MyClass.java"],
5169 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005170 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005171 }
Paul Duffin37856732021-02-26 14:24:15 +00005172
5173 java_sdk_library_import {
5174 name: "libbar",
5175 public: {
5176 jars: ["libbar.jar"],
5177 },
5178 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005179 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005180 }
5181
5182 java_sdk_library {
5183 name: "libbar",
5184 srcs: ["foo/bar/MyClass.java"],
5185 unsafe_ignore_missing_latest_api: true,
5186 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005187 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005188 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005189 `
5190
Paul Duffin89f570a2021-06-16 01:42:33 +01005191 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005192 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
5193 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005194
Paul Duffin537ea3d2021-05-14 10:38:00 +01005195 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005196 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005197 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005198 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005199 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5200 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005201 })
5202
5203 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5204 bp := `
5205 apex {
5206 name: "myapex",
5207 enabled: false,
5208 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01005209 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005210 }
5211
5212 apex_key {
5213 name: "myapex.key",
5214 public_key: "testkey.avbpubkey",
5215 private_key: "testkey.pem",
5216 }
5217
5218 prebuilt_apex {
5219 name: "myapex",
5220 arch: {
5221 arm64: {
5222 src: "myapex-arm64.apex",
5223 },
5224 arm: {
5225 src: "myapex-arm.apex",
5226 },
5227 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005228 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5229 }
5230
5231 prebuilt_bootclasspath_fragment {
5232 name: "my-bootclasspath-fragment",
5233 contents: ["libfoo", "libbar"],
5234 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005235 hidden_api: {
5236 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5237 metadata: "my-bootclasspath-fragment/metadata.csv",
5238 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005239 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5240 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5241 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005242 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005243 }
5244
5245 java_import {
5246 name: "libfoo",
5247 prefer: true,
5248 jars: ["libfoo.jar"],
5249 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005250 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005251 }
5252
5253 java_library {
5254 name: "libfoo",
5255 srcs: ["foo/bar/MyClass.java"],
5256 apex_available: ["myapex"],
5257 }
Paul Duffin37856732021-02-26 14:24:15 +00005258
5259 java_sdk_library_import {
5260 name: "libbar",
5261 prefer: true,
5262 public: {
5263 jars: ["libbar.jar"],
5264 },
5265 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005266 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005267 permitted_packages: ["bar"],
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"],
5275 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005276 `
5277
Paul Duffin89f570a2021-06-16 01:42:33 +01005278 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005279 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5280 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005281
Paul Duffin537ea3d2021-05-14 10:38:00 +01005282 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005283 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005284 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005285 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005286 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5287 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005288 })
5289}
5290
Roland Levillain630846d2019-06-26 12:48:34 +01005291func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005292 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005293 apex_test {
5294 name: "myapex",
5295 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005296 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005297 tests: [
5298 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005299 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005300 ],
5301 }
5302
5303 apex_key {
5304 name: "myapex.key",
5305 public_key: "testkey.avbpubkey",
5306 private_key: "testkey.pem",
5307 }
5308
Liz Kammer1c14a212020-05-12 15:26:55 -07005309 filegroup {
5310 name: "fg",
5311 srcs: [
5312 "baz",
5313 "bar/baz"
5314 ],
5315 }
5316
Roland Levillain630846d2019-06-26 12:48:34 +01005317 cc_test {
5318 name: "mytest",
5319 gtest: false,
5320 srcs: ["mytest.cpp"],
5321 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005322 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005323 system_shared_libs: [],
5324 static_executable: true,
5325 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005326 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005327 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005328
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005329 cc_library {
5330 name: "mylib",
5331 srcs: ["mylib.cpp"],
5332 system_shared_libs: [],
5333 stl: "none",
5334 }
5335
Liz Kammer5bd365f2020-05-27 15:15:11 -07005336 filegroup {
5337 name: "fg2",
5338 srcs: [
5339 "testdata/baz"
5340 ],
5341 }
5342
Roland Levillain9b5fde92019-06-28 15:41:19 +01005343 cc_test {
5344 name: "mytests",
5345 gtest: false,
5346 srcs: [
5347 "mytest1.cpp",
5348 "mytest2.cpp",
5349 "mytest3.cpp",
5350 ],
5351 test_per_src: true,
5352 relative_install_path: "test",
5353 system_shared_libs: [],
5354 static_executable: true,
5355 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005356 data: [
5357 ":fg",
5358 ":fg2",
5359 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005360 }
Roland Levillain630846d2019-06-26 12:48:34 +01005361 `)
5362
Sundong Ahnabb64432019-10-22 13:58:29 +09005363 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005364 copyCmds := apexRule.Args["copy_commands"]
5365
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005366 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005367 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005368 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005369
Liz Kammer1c14a212020-05-12 15:26:55 -07005370 //Ensure that test data are copied into apex.
5371 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5372 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5373
Roland Levillain9b5fde92019-06-28 15:41:19 +01005374 // Ensure that test deps built with `test_per_src` are copied into apex.
5375 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5376 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5377 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005378
5379 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07005380 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005381 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005382 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005383 prefix := "TARGET_"
5384 var builder strings.Builder
5385 data.Custom(&builder, name, prefix, "", data)
5386 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09005387 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5388 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5389 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5390 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09005391 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09005392 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005393 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005394
5395 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005396 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005397 data.Custom(&builder, name, prefix, "", data)
5398 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005399 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5400 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005401}
5402
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005403func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005404 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005405 apex {
5406 name: "myapex",
5407 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005408 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005409 }
5410 apex_key {
5411 name: "myapex.key",
5412 public_key: "testkey.avbpubkey",
5413 private_key: "testkey.pem",
5414 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005415 `,
5416 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5417 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5418 }),
5419 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005420 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09005421 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005422 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005423 var builder strings.Builder
5424 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5425 androidMk := builder.String()
5426 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
5427}
5428
Jooyung Hand48f3c32019-08-23 11:18:57 +09005429func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5430 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5431 apex {
5432 name: "myapex",
5433 key: "myapex.key",
5434 native_shared_libs: ["libfoo"],
5435 }
5436
5437 apex_key {
5438 name: "myapex.key",
5439 public_key: "testkey.avbpubkey",
5440 private_key: "testkey.pem",
5441 }
5442
5443 cc_library {
5444 name: "libfoo",
5445 stl: "none",
5446 system_shared_libs: [],
5447 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005448 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005449 }
5450 `)
5451 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5452 apex {
5453 name: "myapex",
5454 key: "myapex.key",
5455 java_libs: ["myjar"],
5456 }
5457
5458 apex_key {
5459 name: "myapex.key",
5460 public_key: "testkey.avbpubkey",
5461 private_key: "testkey.pem",
5462 }
5463
5464 java_library {
5465 name: "myjar",
5466 srcs: ["foo/bar/MyClass.java"],
5467 sdk_version: "none",
5468 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005469 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005470 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005471 }
5472 `)
5473}
5474
Bill Peckhama41a6962021-01-11 10:58:54 -08005475func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005476 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005477 apex {
5478 name: "myapex",
5479 key: "myapex.key",
5480 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005481 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005482 }
5483
5484 apex_key {
5485 name: "myapex.key",
5486 public_key: "testkey.avbpubkey",
5487 private_key: "testkey.pem",
5488 }
5489
5490 java_import {
5491 name: "myjavaimport",
5492 apex_available: ["myapex"],
5493 jars: ["my.jar"],
5494 compile_dex: true,
5495 }
5496 `)
5497
5498 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5499 apexRule := module.Rule("apexRule")
5500 copyCmds := apexRule.Args["copy_commands"]
5501 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5502}
5503
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005504func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005505 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005506 apex {
5507 name: "myapex",
5508 key: "myapex.key",
5509 apps: [
5510 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005511 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005512 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005513 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005514 }
5515
5516 apex_key {
5517 name: "myapex.key",
5518 public_key: "testkey.avbpubkey",
5519 private_key: "testkey.pem",
5520 }
5521
5522 android_app {
5523 name: "AppFoo",
5524 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005525 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005526 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005527 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005528 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005529 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005530 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005531
5532 android_app {
5533 name: "AppFooPriv",
5534 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005535 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005536 system_modules: "none",
5537 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005538 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005539 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005540 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005541
5542 cc_library_shared {
5543 name: "libjni",
5544 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005545 shared_libs: ["libfoo"],
5546 stl: "none",
5547 system_shared_libs: [],
5548 apex_available: [ "myapex" ],
5549 sdk_version: "current",
5550 }
5551
5552 cc_library_shared {
5553 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005554 stl: "none",
5555 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005556 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005557 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005558 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005559 `)
5560
Sundong Ahnabb64432019-10-22 13:58:29 +09005561 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005562 apexRule := module.Rule("apexRule")
5563 copyCmds := apexRule.Args["copy_commands"]
5564
5565 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005566 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005567
Colin Crossaede88c2020-08-11 12:17:01 -07005568 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005569 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005570 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005571 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005572 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005573 // JNI libraries including transitive deps are
5574 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005575 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005576 // ... embedded inside APK (jnilibs.zip)
5577 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5578 // ... and not directly inside the APEX
5579 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5580 }
Dario Frenicde2a032019-10-27 00:29:22 +01005581}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005582
Dario Frenicde2a032019-10-27 00:29:22 +01005583func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005584 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005585 apex {
5586 name: "myapex",
5587 key: "myapex.key",
5588 apps: [
5589 "AppFooPrebuilt",
5590 "AppFooPrivPrebuilt",
5591 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005592 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005593 }
5594
5595 apex_key {
5596 name: "myapex.key",
5597 public_key: "testkey.avbpubkey",
5598 private_key: "testkey.pem",
5599 }
5600
5601 android_app_import {
5602 name: "AppFooPrebuilt",
5603 apk: "PrebuiltAppFoo.apk",
5604 presigned: true,
5605 dex_preopt: {
5606 enabled: false,
5607 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005608 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005609 }
5610
5611 android_app_import {
5612 name: "AppFooPrivPrebuilt",
5613 apk: "PrebuiltAppFooPriv.apk",
5614 privileged: true,
5615 presigned: true,
5616 dex_preopt: {
5617 enabled: false,
5618 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005619 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005620 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005621 }
5622 `)
5623
Sundong Ahnabb64432019-10-22 13:58:29 +09005624 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005625 apexRule := module.Rule("apexRule")
5626 copyCmds := apexRule.Args["copy_commands"]
5627
5628 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005629 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5630}
5631
5632func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005633 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005634 apex {
5635 name: "myapex",
5636 key: "myapex.key",
5637 apps: [
5638 "AppFoo",
5639 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005640 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005641 }
5642
5643 apex_key {
5644 name: "myapex.key",
5645 public_key: "testkey.avbpubkey",
5646 private_key: "testkey.pem",
5647 }
5648
5649 android_app {
5650 name: "AppFoo",
5651 srcs: ["foo/bar/MyClass.java"],
5652 sdk_version: "none",
5653 system_modules: "none",
5654 apex_available: [ "myapex" ],
5655 }
5656
5657 android_app_import {
5658 name: "AppFoo",
5659 apk: "AppFooPrebuilt.apk",
5660 filename: "AppFooPrebuilt.apk",
5661 presigned: true,
5662 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005663 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005664 }
5665 `, withFiles(map[string][]byte{
5666 "AppFooPrebuilt.apk": nil,
5667 }))
5668
5669 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005670 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005671 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005672}
5673
Dario Freni6f3937c2019-12-20 22:58:03 +00005674func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005675 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005676 apex {
5677 name: "myapex",
5678 key: "myapex.key",
5679 apps: [
5680 "TesterHelpAppFoo",
5681 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005682 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005683 }
5684
5685 apex_key {
5686 name: "myapex.key",
5687 public_key: "testkey.avbpubkey",
5688 private_key: "testkey.pem",
5689 }
5690
5691 android_test_helper_app {
5692 name: "TesterHelpAppFoo",
5693 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005694 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005695 }
5696
5697 `)
5698
5699 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5700 apexRule := module.Rule("apexRule")
5701 copyCmds := apexRule.Args["copy_commands"]
5702
5703 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5704}
5705
Jooyung Han18020ea2019-11-13 10:50:48 +09005706func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5707 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005708 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005709 apex {
5710 name: "myapex",
5711 key: "myapex.key",
5712 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005713 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005714 }
5715
5716 apex_key {
5717 name: "myapex.key",
5718 public_key: "testkey.avbpubkey",
5719 private_key: "testkey.pem",
5720 }
5721
5722 apex {
5723 name: "otherapex",
5724 key: "myapex.key",
5725 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005726 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005727 }
5728
5729 cc_defaults {
5730 name: "libfoo-defaults",
5731 apex_available: ["otherapex"],
5732 }
5733
5734 cc_library {
5735 name: "libfoo",
5736 defaults: ["libfoo-defaults"],
5737 stl: "none",
5738 system_shared_libs: [],
5739 }`)
5740}
5741
Paul Duffine52e66f2020-03-30 17:54:29 +01005742func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005743 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005744 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005745 apex {
5746 name: "myapex",
5747 key: "myapex.key",
5748 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005749 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005750 }
5751
5752 apex_key {
5753 name: "myapex.key",
5754 public_key: "testkey.avbpubkey",
5755 private_key: "testkey.pem",
5756 }
5757
5758 apex {
5759 name: "otherapex",
5760 key: "otherapex.key",
5761 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005762 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005763 }
5764
5765 apex_key {
5766 name: "otherapex.key",
5767 public_key: "testkey.avbpubkey",
5768 private_key: "testkey.pem",
5769 }
5770
5771 cc_library {
5772 name: "libfoo",
5773 stl: "none",
5774 system_shared_libs: [],
5775 apex_available: ["otherapex"],
5776 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005777}
Jiyong Park127b40b2019-09-30 16:04:35 +09005778
Paul Duffine52e66f2020-03-30 17:54:29 +01005779func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005780 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005781 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005782.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005783.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005784.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005785.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005786.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005787.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005788 apex {
5789 name: "myapex",
5790 key: "myapex.key",
5791 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005792 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005793 }
5794
5795 apex_key {
5796 name: "myapex.key",
5797 public_key: "testkey.avbpubkey",
5798 private_key: "testkey.pem",
5799 }
5800
Jiyong Park127b40b2019-09-30 16:04:35 +09005801 cc_library {
5802 name: "libfoo",
5803 stl: "none",
5804 shared_libs: ["libbar"],
5805 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005806 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005807 }
5808
5809 cc_library {
5810 name: "libbar",
5811 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005812 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005813 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005814 apex_available: ["myapex"],
5815 }
5816
5817 cc_library {
5818 name: "libbaz",
5819 stl: "none",
5820 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005821 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005822}
Jiyong Park127b40b2019-09-30 16:04:35 +09005823
Paul Duffine52e66f2020-03-30 17:54:29 +01005824func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005825 testApexError(t, "\"otherapex\" is not a valid module name", `
5826 apex {
5827 name: "myapex",
5828 key: "myapex.key",
5829 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005830 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005831 }
5832
5833 apex_key {
5834 name: "myapex.key",
5835 public_key: "testkey.avbpubkey",
5836 private_key: "testkey.pem",
5837 }
5838
5839 cc_library {
5840 name: "libfoo",
5841 stl: "none",
5842 system_shared_libs: [],
5843 apex_available: ["otherapex"],
5844 }`)
5845
Paul Duffine52e66f2020-03-30 17:54:29 +01005846 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005847 apex {
5848 name: "myapex",
5849 key: "myapex.key",
5850 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005851 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005852 }
5853
5854 apex_key {
5855 name: "myapex.key",
5856 public_key: "testkey.avbpubkey",
5857 private_key: "testkey.pem",
5858 }
5859
5860 cc_library {
5861 name: "libfoo",
5862 stl: "none",
5863 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005864 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005865 apex_available: ["myapex"],
5866 }
5867
5868 cc_library {
5869 name: "libbar",
5870 stl: "none",
5871 system_shared_libs: [],
5872 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005873 }
5874
5875 cc_library {
5876 name: "libbaz",
5877 stl: "none",
5878 system_shared_libs: [],
5879 stubs: {
5880 versions: ["10", "20", "30"],
5881 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005882 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005883}
Jiyong Park127b40b2019-09-30 16:04:35 +09005884
Jiyong Park89e850a2020-04-07 16:37:39 +09005885func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005886 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005887 apex {
5888 name: "myapex",
5889 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005890 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005891 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005892 }
5893
5894 apex_key {
5895 name: "myapex.key",
5896 public_key: "testkey.avbpubkey",
5897 private_key: "testkey.pem",
5898 }
5899
5900 cc_library {
5901 name: "libfoo",
5902 stl: "none",
5903 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005904 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005905 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005906 }
5907
5908 cc_library {
5909 name: "libfoo2",
5910 stl: "none",
5911 system_shared_libs: [],
5912 shared_libs: ["libbaz"],
5913 apex_available: ["//apex_available:platform"],
5914 }
5915
5916 cc_library {
5917 name: "libbar",
5918 stl: "none",
5919 system_shared_libs: [],
5920 apex_available: ["myapex"],
5921 }
5922
5923 cc_library {
5924 name: "libbaz",
5925 stl: "none",
5926 system_shared_libs: [],
5927 apex_available: ["myapex"],
5928 stubs: {
5929 versions: ["1"],
5930 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005931 }`)
5932
Jiyong Park89e850a2020-04-07 16:37:39 +09005933 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5934 // because it depends on libbar which isn't available to platform
5935 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5936 if libfoo.NotAvailableForPlatform() != true {
5937 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5938 }
5939
5940 // libfoo2 however can be available to platform because it depends on libbaz which provides
5941 // stubs
5942 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5943 if libfoo2.NotAvailableForPlatform() == true {
5944 t.Errorf("%q should be available to platform", libfoo2.String())
5945 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005946}
Jiyong Parka90ca002019-10-07 15:47:24 +09005947
Paul Duffine52e66f2020-03-30 17:54:29 +01005948func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005949 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005950 apex {
5951 name: "myapex",
5952 key: "myapex.key",
5953 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005954 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005955 }
5956
5957 apex_key {
5958 name: "myapex.key",
5959 public_key: "testkey.avbpubkey",
5960 private_key: "testkey.pem",
5961 }
5962
5963 cc_library {
5964 name: "libfoo",
5965 stl: "none",
5966 system_shared_libs: [],
5967 apex_available: ["myapex"],
5968 static: {
5969 apex_available: ["//apex_available:platform"],
5970 },
5971 }`)
5972
Jiyong Park89e850a2020-04-07 16:37:39 +09005973 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5974 if libfooShared.NotAvailableForPlatform() != true {
5975 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5976 }
5977 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5978 if libfooStatic.NotAvailableForPlatform() != false {
5979 t.Errorf("%q should be available to platform", libfooStatic.String())
5980 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005981}
5982
Jiyong Park5d790c32019-11-15 18:40:32 +09005983func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005984 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005985 apex {
5986 name: "myapex",
5987 key: "myapex.key",
5988 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08005989 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07005990 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005991 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005992 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005993 }
5994
5995 override_apex {
5996 name: "override_myapex",
5997 base: "myapex",
5998 apps: ["override_app"],
markchien7c803b82021-08-26 22:10:06 +08005999 bpfs: ["override_bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006000 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006001 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08006002 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006003 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006004 key: "mynewapex.key",
6005 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006006 }
6007
6008 apex_key {
6009 name: "myapex.key",
6010 public_key: "testkey.avbpubkey",
6011 private_key: "testkey.pem",
6012 }
6013
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006014 apex_key {
6015 name: "mynewapex.key",
6016 public_key: "testkey2.avbpubkey",
6017 private_key: "testkey2.pem",
6018 }
6019
6020 android_app_certificate {
6021 name: "myapex.certificate",
6022 certificate: "testkey",
6023 }
6024
Jiyong Park5d790c32019-11-15 18:40:32 +09006025 android_app {
6026 name: "app",
6027 srcs: ["foo/bar/MyClass.java"],
6028 package_name: "foo",
6029 sdk_version: "none",
6030 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006031 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006032 }
6033
6034 override_android_app {
6035 name: "override_app",
6036 base: "app",
6037 package_name: "bar",
6038 }
markchien7c803b82021-08-26 22:10:06 +08006039
6040 bpf {
6041 name: "bpf",
6042 srcs: ["bpf.c"],
6043 }
6044
6045 bpf {
6046 name: "override_bpf",
6047 srcs: ["override_bpf.c"],
6048 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006049
6050 prebuilt_etc {
6051 name: "myetc",
6052 src: "myprebuilt",
6053 }
6054
6055 prebuilt_etc {
6056 name: "override_myetc",
6057 src: "override_myprebuilt",
6058 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006059 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006060
Jiyong Park317645e2019-12-05 13:20:58 +09006061 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
6062 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
6063 if originalVariant.GetOverriddenBy() != "" {
6064 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6065 }
6066 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6067 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6068 }
6069
Jiyong Park5d790c32019-11-15 18:40:32 +09006070 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
6071 apexRule := module.Rule("apexRule")
6072 copyCmds := apexRule.Args["copy_commands"]
6073
6074 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006075 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006076
markchien7c803b82021-08-26 22:10:06 +08006077 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
6078 ensureContains(t, copyCmds, "image.apex/etc/bpf/override_bpf.o")
6079
Daniel Norman5a3ce132021-08-26 15:44:43 -07006080 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6081 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6082
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006083 apexBundle := module.Module().(*apexBundle)
6084 name := apexBundle.Name()
6085 if name != "override_myapex" {
6086 t.Errorf("name should be \"override_myapex\", but was %q", name)
6087 }
6088
Baligh Uddin004d7172020-02-19 21:29:28 -08006089 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6090 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6091 }
6092
Jiyong Park20bacab2020-03-03 11:45:41 +09006093 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006094 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006095 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6096
6097 signApkRule := module.Rule("signapk")
6098 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006099
Colin Crossaa255532020-07-03 13:18:24 -07006100 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006101 var builder strings.Builder
6102 data.Custom(&builder, name, "TARGET_", "", data)
6103 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09006104 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
markchien7c803b82021-08-26 22:10:06 +08006105 ensureContains(t, androidMk, "LOCAL_MODULE := override_bpf.o.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006106 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
6107 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006108 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006109 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006110 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006111 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006112 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
6113 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006114}
6115
Jooyung Han214bf372019-11-12 13:03:50 +09006116func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006117 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09006118 apex {
6119 name: "myapex",
6120 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006121 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09006122 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09006123 }
6124
6125 apex_key {
6126 name: "myapex.key",
6127 public_key: "testkey.avbpubkey",
6128 private_key: "testkey.pem",
6129 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006130
6131 cc_library {
6132 name: "mylib",
6133 srcs: ["mylib.cpp"],
6134 stl: "libc++",
6135 system_shared_libs: [],
6136 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09006137 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006138 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006139 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09006140
6141 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6142 args := module.Rule("apexRule").Args
6143 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00006144 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006145
6146 // The copies of the libraries in the apex should have one more dependency than
6147 // the ones outside the apex, namely the unwinder. Ideally we should check
6148 // the dependency names directly here but for some reason the names are blank in
6149 // this test.
6150 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07006151 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006152 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
6153 if len(apexImplicits) != len(nonApexImplicits)+1 {
6154 t.Errorf("%q missing unwinder dep", lib)
6155 }
6156 }
Jooyung Han214bf372019-11-12 13:03:50 +09006157}
6158
Paul Duffine05480a2021-03-08 15:07:14 +00006159var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01006160 "api/current.txt": nil,
6161 "api/removed.txt": nil,
6162 "api/system-current.txt": nil,
6163 "api/system-removed.txt": nil,
6164 "api/test-current.txt": nil,
6165 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01006166
Anton Hanssondff2c782020-12-21 17:10:01 +00006167 "100/public/api/foo.txt": nil,
6168 "100/public/api/foo-removed.txt": nil,
6169 "100/system/api/foo.txt": nil,
6170 "100/system/api/foo-removed.txt": nil,
6171
Paul Duffineedc5d52020-06-12 17:46:39 +01006172 // For java_sdk_library_import
6173 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01006174}
6175
Jooyung Han58f26ab2019-12-18 15:34:32 +09006176func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006177 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09006178 apex {
6179 name: "myapex",
6180 key: "myapex.key",
6181 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006182 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09006183 }
6184
6185 apex_key {
6186 name: "myapex.key",
6187 public_key: "testkey.avbpubkey",
6188 private_key: "testkey.pem",
6189 }
6190
6191 java_sdk_library {
6192 name: "foo",
6193 srcs: ["a.java"],
6194 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006195 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09006196 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006197
6198 prebuilt_apis {
6199 name: "sdk",
6200 api_dirs: ["100"],
6201 }
Paul Duffin9b879592020-05-26 13:21:35 +01006202 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09006203
6204 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00006205 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09006206 "javalib/foo.jar",
6207 "etc/permissions/foo.xml",
6208 })
6209 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09006210 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00006211 ensureMatches(t, sdkLibrary.RuleParams.Command, `<library\\n\s+name=\\\"foo\\\"\\n\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09006212}
6213
Paul Duffin9b879592020-05-26 13:21:35 +01006214func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006215 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006216 apex {
6217 name: "myapex",
6218 key: "myapex.key",
6219 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006220 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006221 }
6222
6223 apex_key {
6224 name: "myapex.key",
6225 public_key: "testkey.avbpubkey",
6226 private_key: "testkey.pem",
6227 }
6228
6229 java_sdk_library {
6230 name: "foo",
6231 srcs: ["a.java"],
6232 api_packages: ["foo"],
6233 apex_available: ["myapex"],
6234 sdk_version: "none",
6235 system_modules: "none",
6236 }
6237
6238 java_library {
6239 name: "bar",
6240 srcs: ["a.java"],
6241 libs: ["foo"],
6242 apex_available: ["myapex"],
6243 sdk_version: "none",
6244 system_modules: "none",
6245 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006246
6247 prebuilt_apis {
6248 name: "sdk",
6249 api_dirs: ["100"],
6250 }
Paul Duffin9b879592020-05-26 13:21:35 +01006251 `, withFiles(filesForSdkLibrary))
6252
6253 // java_sdk_library installs both impl jar and permission XML
6254 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6255 "javalib/bar.jar",
6256 "javalib/foo.jar",
6257 "etc/permissions/foo.xml",
6258 })
6259
6260 // The bar library should depend on the implementation jar.
6261 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006262 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006263 t.Errorf("expected %q, found %#q", expected, actual)
6264 }
6265}
6266
6267func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006268 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006269 apex {
6270 name: "myapex",
6271 key: "myapex.key",
6272 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006273 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006274 }
6275
6276 apex_key {
6277 name: "myapex.key",
6278 public_key: "testkey.avbpubkey",
6279 private_key: "testkey.pem",
6280 }
6281
6282 java_sdk_library {
6283 name: "foo",
6284 srcs: ["a.java"],
6285 api_packages: ["foo"],
6286 apex_available: ["myapex"],
6287 sdk_version: "none",
6288 system_modules: "none",
6289 }
6290
6291 java_library {
6292 name: "bar",
6293 srcs: ["a.java"],
6294 libs: ["foo"],
6295 sdk_version: "none",
6296 system_modules: "none",
6297 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006298
6299 prebuilt_apis {
6300 name: "sdk",
6301 api_dirs: ["100"],
6302 }
Paul Duffin9b879592020-05-26 13:21:35 +01006303 `, withFiles(filesForSdkLibrary))
6304
6305 // java_sdk_library installs both impl jar and permission XML
6306 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6307 "javalib/foo.jar",
6308 "etc/permissions/foo.xml",
6309 })
6310
6311 // The bar library should depend on the stubs jar.
6312 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006313 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006314 t.Errorf("expected %q, found %#q", expected, actual)
6315 }
6316}
6317
Paul Duffineedc5d52020-06-12 17:46:39 +01006318func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006319 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00006320 prebuilt_apis {
6321 name: "sdk",
6322 api_dirs: ["100"],
6323 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01006324 withFiles(map[string][]byte{
6325 "apex/a.java": nil,
6326 "apex/apex_manifest.json": nil,
6327 "apex/Android.bp": []byte(`
6328 package {
6329 default_visibility: ["//visibility:private"],
6330 }
6331
6332 apex {
6333 name: "myapex",
6334 key: "myapex.key",
6335 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006336 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006337 }
6338
6339 apex_key {
6340 name: "myapex.key",
6341 public_key: "testkey.avbpubkey",
6342 private_key: "testkey.pem",
6343 }
6344
6345 java_library {
6346 name: "bar",
6347 srcs: ["a.java"],
6348 libs: ["foo"],
6349 apex_available: ["myapex"],
6350 sdk_version: "none",
6351 system_modules: "none",
6352 }
6353`),
6354 "source/a.java": nil,
6355 "source/api/current.txt": nil,
6356 "source/api/removed.txt": nil,
6357 "source/Android.bp": []byte(`
6358 package {
6359 default_visibility: ["//visibility:private"],
6360 }
6361
6362 java_sdk_library {
6363 name: "foo",
6364 visibility: ["//apex"],
6365 srcs: ["a.java"],
6366 api_packages: ["foo"],
6367 apex_available: ["myapex"],
6368 sdk_version: "none",
6369 system_modules: "none",
6370 public: {
6371 enabled: true,
6372 },
6373 }
6374`),
6375 "prebuilt/a.jar": nil,
6376 "prebuilt/Android.bp": []byte(`
6377 package {
6378 default_visibility: ["//visibility:private"],
6379 }
6380
6381 java_sdk_library_import {
6382 name: "foo",
6383 visibility: ["//apex", "//source"],
6384 apex_available: ["myapex"],
6385 prefer: true,
6386 public: {
6387 jars: ["a.jar"],
6388 },
6389 }
6390`),
Anton Hanssondff2c782020-12-21 17:10:01 +00006391 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01006392 )
6393
6394 // java_sdk_library installs both impl jar and permission XML
6395 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6396 "javalib/bar.jar",
6397 "javalib/foo.jar",
6398 "etc/permissions/foo.xml",
6399 })
6400
6401 // The bar library should depend on the implementation jar.
6402 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006403 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01006404 t.Errorf("expected %q, found %#q", expected, actual)
6405 }
6406}
6407
6408func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
6409 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
6410 apex {
6411 name: "myapex",
6412 key: "myapex.key",
6413 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006414 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006415 }
6416
6417 apex_key {
6418 name: "myapex.key",
6419 public_key: "testkey.avbpubkey",
6420 private_key: "testkey.pem",
6421 }
6422
6423 java_sdk_library_import {
6424 name: "foo",
6425 apex_available: ["myapex"],
6426 prefer: true,
6427 public: {
6428 jars: ["a.jar"],
6429 },
6430 }
6431
6432 `, withFiles(filesForSdkLibrary))
6433}
6434
atrost6e126252020-01-27 17:01:16 +00006435func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01006436 result := android.GroupFixturePreparers(
6437 prepareForApexTest,
6438 java.PrepareForTestWithPlatformCompatConfig,
6439 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00006440 apex {
6441 name: "myapex",
6442 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00006443 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00006444 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006445 updatable: false,
atrost6e126252020-01-27 17:01:16 +00006446 }
6447
6448 apex_key {
6449 name: "myapex.key",
6450 public_key: "testkey.avbpubkey",
6451 private_key: "testkey.pem",
6452 }
6453
6454 platform_compat_config {
6455 name: "myjar-platform-compat-config",
6456 src: ":myjar",
6457 }
6458
6459 java_library {
6460 name: "myjar",
6461 srcs: ["foo/bar/MyClass.java"],
6462 sdk_version: "none",
6463 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00006464 apex_available: [ "myapex" ],
6465 }
Paul Duffin1b29e002021-03-16 15:06:54 +00006466
6467 // Make sure that a preferred prebuilt does not affect the apex contents.
6468 prebuilt_platform_compat_config {
6469 name: "myjar-platform-compat-config",
6470 metadata: "compat-config/metadata.xml",
6471 prefer: true,
6472 }
atrost6e126252020-01-27 17:01:16 +00006473 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00006474 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00006475 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6476 "etc/compatconfig/myjar-platform-compat-config.xml",
6477 "javalib/myjar.jar",
6478 })
6479}
6480
Jiyong Park479321d2019-12-16 11:47:12 +09006481func TestRejectNonInstallableJavaLibrary(t *testing.T) {
6482 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
6483 apex {
6484 name: "myapex",
6485 key: "myapex.key",
6486 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006487 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09006488 }
6489
6490 apex_key {
6491 name: "myapex.key",
6492 public_key: "testkey.avbpubkey",
6493 private_key: "testkey.pem",
6494 }
6495
6496 java_library {
6497 name: "myjar",
6498 srcs: ["foo/bar/MyClass.java"],
6499 sdk_version: "none",
6500 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09006501 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006502 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09006503 }
6504 `)
6505}
6506
Jiyong Park7afd1072019-12-30 16:56:33 +09006507func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006508 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09006509 apex {
6510 name: "myapex",
6511 key: "myapex.key",
6512 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006513 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09006514 }
6515
6516 apex_key {
6517 name: "myapex.key",
6518 public_key: "testkey.avbpubkey",
6519 private_key: "testkey.pem",
6520 }
6521
6522 cc_library {
6523 name: "mylib",
6524 srcs: ["mylib.cpp"],
6525 system_shared_libs: [],
6526 stl: "none",
6527 required: ["a", "b"],
6528 host_required: ["c", "d"],
6529 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006530 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006531 }
6532 `)
6533
6534 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006535 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006536 name := apexBundle.BaseModuleName()
6537 prefix := "TARGET_"
6538 var builder strings.Builder
6539 data.Custom(&builder, name, prefix, "", data)
6540 androidMk := builder.String()
6541 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6542 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6543 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6544}
6545
Jiyong Park7cd10e32020-01-14 09:22:18 +09006546func TestSymlinksFromApexToSystem(t *testing.T) {
6547 bp := `
6548 apex {
6549 name: "myapex",
6550 key: "myapex.key",
6551 native_shared_libs: ["mylib"],
6552 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006553 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006554 }
6555
Jiyong Park9d677202020-02-19 16:29:35 +09006556 apex {
6557 name: "myapex.updatable",
6558 key: "myapex.key",
6559 native_shared_libs: ["mylib"],
6560 java_libs: ["myjar"],
6561 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006562 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006563 }
6564
Jiyong Park7cd10e32020-01-14 09:22:18 +09006565 apex_key {
6566 name: "myapex.key",
6567 public_key: "testkey.avbpubkey",
6568 private_key: "testkey.pem",
6569 }
6570
6571 cc_library {
6572 name: "mylib",
6573 srcs: ["mylib.cpp"],
6574 shared_libs: ["myotherlib"],
6575 system_shared_libs: [],
6576 stl: "none",
6577 apex_available: [
6578 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006579 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006580 "//apex_available:platform",
6581 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006582 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006583 }
6584
6585 cc_library {
6586 name: "myotherlib",
6587 srcs: ["mylib.cpp"],
6588 system_shared_libs: [],
6589 stl: "none",
6590 apex_available: [
6591 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006592 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006593 "//apex_available:platform",
6594 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006595 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006596 }
6597
6598 java_library {
6599 name: "myjar",
6600 srcs: ["foo/bar/MyClass.java"],
6601 sdk_version: "none",
6602 system_modules: "none",
6603 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006604 apex_available: [
6605 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006606 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006607 "//apex_available:platform",
6608 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006609 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006610 }
6611
6612 java_library {
6613 name: "myotherjar",
6614 srcs: ["foo/bar/MyClass.java"],
6615 sdk_version: "none",
6616 system_modules: "none",
6617 apex_available: [
6618 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006619 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006620 "//apex_available:platform",
6621 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006622 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006623 }
6624 `
6625
6626 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6627 for _, f := range files {
6628 if f.path == file {
6629 if f.isLink {
6630 t.Errorf("%q is not a real file", file)
6631 }
6632 return
6633 }
6634 }
6635 t.Errorf("%q is not found", file)
6636 }
6637
6638 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6639 for _, f := range files {
6640 if f.path == file {
6641 if !f.isLink {
6642 t.Errorf("%q is not a symlink", file)
6643 }
6644 return
6645 }
6646 }
6647 t.Errorf("%q is not found", file)
6648 }
6649
Jiyong Park9d677202020-02-19 16:29:35 +09006650 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6651 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006652 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006653 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006654 ensureRealfileExists(t, files, "javalib/myjar.jar")
6655 ensureRealfileExists(t, files, "lib64/mylib.so")
6656 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6657
Jiyong Park9d677202020-02-19 16:29:35 +09006658 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6659 ensureRealfileExists(t, files, "javalib/myjar.jar")
6660 ensureRealfileExists(t, files, "lib64/mylib.so")
6661 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6662
6663 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006664 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006665 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006666 ensureRealfileExists(t, files, "javalib/myjar.jar")
6667 ensureRealfileExists(t, files, "lib64/mylib.so")
6668 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006669
6670 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6671 ensureRealfileExists(t, files, "javalib/myjar.jar")
6672 ensureRealfileExists(t, files, "lib64/mylib.so")
6673 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006674}
6675
Yo Chiange8128052020-07-23 20:09:18 +08006676func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006677 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006678 apex {
6679 name: "myapex",
6680 key: "myapex.key",
6681 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006682 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006683 }
6684
6685 apex_key {
6686 name: "myapex.key",
6687 public_key: "testkey.avbpubkey",
6688 private_key: "testkey.pem",
6689 }
6690
6691 cc_library_shared {
6692 name: "mylib",
6693 srcs: ["mylib.cpp"],
6694 shared_libs: ["myotherlib"],
6695 system_shared_libs: [],
6696 stl: "none",
6697 apex_available: [
6698 "myapex",
6699 "//apex_available:platform",
6700 ],
6701 }
6702
6703 cc_prebuilt_library_shared {
6704 name: "myotherlib",
6705 srcs: ["prebuilt.so"],
6706 system_shared_libs: [],
6707 stl: "none",
6708 apex_available: [
6709 "myapex",
6710 "//apex_available:platform",
6711 ],
6712 }
6713 `)
6714
6715 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006716 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006717 var builder strings.Builder
6718 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6719 androidMk := builder.String()
6720 // `myotherlib` is added to `myapex` as symlink
6721 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6722 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6723 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6724 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006725 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 +08006726}
6727
Jooyung Han643adc42020-02-27 13:50:06 +09006728func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006729 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006730 apex {
6731 name: "myapex",
6732 key: "myapex.key",
6733 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006734 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006735 }
6736
6737 apex_key {
6738 name: "myapex.key",
6739 public_key: "testkey.avbpubkey",
6740 private_key: "testkey.pem",
6741 }
6742
6743 cc_library {
6744 name: "mylib",
6745 srcs: ["mylib.cpp"],
6746 shared_libs: ["mylib2"],
6747 system_shared_libs: [],
6748 stl: "none",
6749 apex_available: [ "myapex" ],
6750 }
6751
6752 cc_library {
6753 name: "mylib2",
6754 srcs: ["mylib.cpp"],
6755 system_shared_libs: [],
6756 stl: "none",
6757 apex_available: [ "myapex" ],
6758 }
6759 `)
6760
6761 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6762 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6763 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6764 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6765 "lib64/mylib.so",
6766 "lib64/mylib2.so",
6767 })
6768}
6769
Jooyung Han49f67012020-04-17 13:43:10 +09006770func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006771 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006772 apex {
6773 name: "myapex",
6774 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006775 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006776 }
6777 apex_key {
6778 name: "myapex.key",
6779 public_key: "testkey.avbpubkey",
6780 private_key: "testkey.pem",
6781 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006782 `,
6783 android.FixtureModifyConfig(func(config android.Config) {
6784 delete(config.Targets, android.Android)
6785 config.AndroidCommonTarget = android.Target{}
6786 }),
6787 )
Jooyung Han49f67012020-04-17 13:43:10 +09006788
6789 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6790 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6791 }
6792}
6793
Jiyong Parkbd159612020-02-28 15:22:21 +09006794func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006795 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006796 apex {
6797 name: "myapex",
6798 key: "myapex.key",
6799 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006800 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006801 }
6802
6803 apex_key {
6804 name: "myapex.key",
6805 public_key: "testkey.avbpubkey",
6806 private_key: "testkey.pem",
6807 }
6808
6809 android_app {
6810 name: "AppFoo",
6811 srcs: ["foo/bar/MyClass.java"],
6812 sdk_version: "none",
6813 system_modules: "none",
6814 apex_available: [ "myapex" ],
6815 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006816 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006817
Colin Crosscf371cc2020-11-13 11:48:42 -08006818 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006819 content := bundleConfigRule.Args["content"]
6820
6821 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006822 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 +09006823}
6824
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006825func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006826 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006827 apex {
6828 name: "myapex",
6829 key: "myapex.key",
6830 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006831 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006832 }
6833
6834 apex_key {
6835 name: "myapex.key",
6836 public_key: "testkey.avbpubkey",
6837 private_key: "testkey.pem",
6838 }
6839
6840 android_app_set {
6841 name: "AppSet",
6842 set: "AppSet.apks",
6843 }`)
6844 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006845 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006846 content := bundleConfigRule.Args["content"]
6847 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6848 s := mod.Rule("apexRule").Args["copy_commands"]
6849 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6850 if len(copyCmds) != 3 {
6851 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6852 }
6853 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6854 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6855 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6856}
6857
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006858func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01006859 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006860 apex_set {
6861 name: "myapex",
6862 filename: "foo_v2.apex",
6863 sanitized: {
6864 none: { set: "myapex.apks", },
6865 hwaddress: { set: "myapex.hwasan.apks", },
6866 },
Paul Duffin24704672021-04-06 16:09:30 +01006867 }
6868 `
6869 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006870
Paul Duffin24704672021-04-06 16:09:30 +01006871 // Check that the extractor produces the correct output file from the correct input file.
6872 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006873
Paul Duffin24704672021-04-06 16:09:30 +01006874 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
6875 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006876
Paul Duffin24704672021-04-06 16:09:30 +01006877 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
6878
6879 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01006880 m = ctx.ModuleForTests("myapex", "android_common_myapex")
6881 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01006882
6883 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006884}
6885
Paul Duffin89f570a2021-06-16 01:42:33 +01006886func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006887 t.Helper()
6888
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006889 bp := `
6890 java_library {
6891 name: "some-updatable-apex-lib",
6892 srcs: ["a.java"],
6893 sdk_version: "current",
6894 apex_available: [
6895 "some-updatable-apex",
6896 ],
satayevabcd5972021-08-06 17:49:46 +01006897 permitted_packages: ["some.updatable.apex.lib"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006898 }
6899
6900 java_library {
6901 name: "some-non-updatable-apex-lib",
6902 srcs: ["a.java"],
6903 apex_available: [
6904 "some-non-updatable-apex",
6905 ],
Paul Duffin89f570a2021-06-16 01:42:33 +01006906 compile_dex: true,
satayevabcd5972021-08-06 17:49:46 +01006907 permitted_packages: ["some.non.updatable.apex.lib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01006908 }
6909
6910 bootclasspath_fragment {
6911 name: "some-non-updatable-fragment",
6912 contents: ["some-non-updatable-apex-lib"],
6913 apex_available: [
6914 "some-non-updatable-apex",
6915 ],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006916 }
6917
6918 java_library {
6919 name: "some-platform-lib",
6920 srcs: ["a.java"],
6921 sdk_version: "current",
6922 installable: true,
6923 }
6924
6925 java_library {
6926 name: "some-art-lib",
6927 srcs: ["a.java"],
6928 sdk_version: "current",
6929 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006930 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006931 ],
6932 hostdex: true,
Paul Duffine5218812021-06-07 13:28:19 +01006933 compile_dex: true,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006934 }
6935
6936 apex {
6937 name: "some-updatable-apex",
6938 key: "some-updatable-apex.key",
6939 java_libs: ["some-updatable-apex-lib"],
6940 updatable: true,
6941 min_sdk_version: "current",
6942 }
6943
6944 apex {
6945 name: "some-non-updatable-apex",
6946 key: "some-non-updatable-apex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01006947 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006948 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006949 }
6950
6951 apex_key {
6952 name: "some-updatable-apex.key",
6953 }
6954
6955 apex_key {
6956 name: "some-non-updatable-apex.key",
6957 }
6958
6959 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006960 name: "com.android.art.debug",
6961 key: "com.android.art.debug.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01006962 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006963 updatable: true,
6964 min_sdk_version: "current",
6965 }
6966
Paul Duffinf23bc472021-04-27 12:42:20 +01006967 bootclasspath_fragment {
6968 name: "art-bootclasspath-fragment",
6969 image_name: "art",
6970 contents: ["some-art-lib"],
6971 apex_available: [
6972 "com.android.art.debug",
6973 ],
6974 }
6975
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006976 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006977 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006978 }
6979
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006980 filegroup {
6981 name: "some-updatable-apex-file_contexts",
6982 srcs: [
6983 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6984 ],
6985 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006986
6987 filegroup {
6988 name: "some-non-updatable-apex-file_contexts",
6989 srcs: [
6990 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6991 ],
6992 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006993 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006994
Paul Duffin89f570a2021-06-16 01:42:33 +01006995 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00006996}
6997
Paul Duffin89f570a2021-06-16 01:42:33 +01006998func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006999 t.Helper()
7000
Paul Duffin55607122021-03-30 23:32:51 +01007001 fs := android.MockFS{
7002 "a.java": nil,
7003 "a.jar": nil,
7004 "apex_manifest.json": nil,
7005 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007006 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007007 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7008 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7009 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007010 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007011 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007012
Paul Duffin55607122021-03-30 23:32:51 +01007013 errorHandler := android.FixtureExpectsNoErrors
7014 if errmsg != "" {
7015 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007016 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007017
Paul Duffin55607122021-03-30 23:32:51 +01007018 result := android.GroupFixturePreparers(
7019 cc.PrepareForTestWithCcDefaultModules,
7020 java.PrepareForTestWithHiddenApiBuildComponents,
7021 java.PrepareForTestWithJavaDefaultModules,
7022 java.PrepareForTestWithJavaSdkLibraryFiles,
7023 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007024 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007025 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007026 android.FixtureModifyMockFS(func(fs android.MockFS) {
7027 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7028 insert := ""
7029 for _, fragment := range fragments {
7030 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7031 }
7032 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7033 platform_bootclasspath {
7034 name: "platform-bootclasspath",
7035 fragments: [
7036 %s
7037 ],
7038 }
7039 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01007040 }
Paul Duffin89f570a2021-06-16 01:42:33 +01007041 }),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00007042 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01007043 ).
7044 ExtendWithErrorHandler(errorHandler).
7045 RunTestWithBp(t, bp)
7046
7047 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007048}
7049
Martin Stjernholm43c44b02021-06-30 16:35:07 +01007050func TestDuplicateDeapexeresFromPrebuiltApexes(t *testing.T) {
7051 preparers := android.GroupFixturePreparers(
7052 java.PrepareForTestWithJavaDefaultModules,
7053 PrepareForTestWithApexBuildComponents,
7054 ).
7055 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
7056 "Multiple installable prebuilt APEXes provide ambiguous deapexers: com.android.myapex and com.mycompany.android.myapex"))
7057
7058 bpBase := `
7059 apex_set {
7060 name: "com.android.myapex",
7061 installable: true,
7062 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7063 set: "myapex.apks",
7064 }
7065
7066 apex_set {
7067 name: "com.mycompany.android.myapex",
7068 apex_name: "com.android.myapex",
7069 installable: true,
7070 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7071 set: "company-myapex.apks",
7072 }
7073
7074 prebuilt_bootclasspath_fragment {
7075 name: "my-bootclasspath-fragment",
7076 apex_available: ["com.android.myapex"],
7077 %s
7078 }
7079 `
7080
7081 t.Run("java_import", func(t *testing.T) {
7082 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7083 java_import {
7084 name: "libfoo",
7085 jars: ["libfoo.jar"],
7086 apex_available: ["com.android.myapex"],
7087 }
7088 `)
7089 })
7090
7091 t.Run("java_sdk_library_import", func(t *testing.T) {
7092 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7093 java_sdk_library_import {
7094 name: "libfoo",
7095 public: {
7096 jars: ["libbar.jar"],
7097 },
7098 apex_available: ["com.android.myapex"],
7099 }
7100 `)
7101 })
7102
7103 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7104 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7105 image_name: "art",
7106 contents: ["libfoo"],
7107 `)+`
7108 java_sdk_library_import {
7109 name: "libfoo",
7110 public: {
7111 jars: ["libbar.jar"],
7112 },
7113 apex_available: ["com.android.myapex"],
7114 }
7115 `)
7116 })
7117}
7118
Jooyung Han548640b2020-04-27 12:10:30 +09007119func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
7120 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7121 apex {
7122 name: "myapex",
7123 key: "myapex.key",
7124 updatable: true,
7125 }
7126
7127 apex_key {
7128 name: "myapex.key",
7129 public_key: "testkey.avbpubkey",
7130 private_key: "testkey.pem",
7131 }
7132 `)
7133}
7134
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007135func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
7136 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7137 apex {
7138 name: "myapex",
7139 key: "myapex.key",
7140 }
7141
7142 apex_key {
7143 name: "myapex.key",
7144 public_key: "testkey.avbpubkey",
7145 private_key: "testkey.pem",
7146 }
7147 `)
7148}
7149
Daniel Norman69109112021-12-02 12:52:42 -08007150func TestUpdatable_cannot_be_vendor_apex(t *testing.T) {
7151 testApexError(t, `"myapex" .*: updatable: vendor APEXes are not updatable`, `
7152 apex {
7153 name: "myapex",
7154 key: "myapex.key",
7155 updatable: true,
7156 soc_specific: true,
7157 }
7158
7159 apex_key {
7160 name: "myapex.key",
7161 public_key: "testkey.avbpubkey",
7162 private_key: "testkey.pem",
7163 }
7164 `)
7165}
7166
satayevb98371c2021-06-15 16:49:50 +01007167func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
7168 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
7169 apex {
7170 name: "myapex",
7171 key: "myapex.key",
7172 systemserverclasspath_fragments: [
7173 "mysystemserverclasspathfragment",
7174 ],
7175 min_sdk_version: "29",
7176 updatable: true,
7177 }
7178
7179 apex_key {
7180 name: "myapex.key",
7181 public_key: "testkey.avbpubkey",
7182 private_key: "testkey.pem",
7183 }
7184
7185 java_library {
7186 name: "foo",
7187 srcs: ["b.java"],
7188 min_sdk_version: "29",
7189 installable: true,
7190 apex_available: [
7191 "myapex",
7192 ],
7193 }
7194
7195 systemserverclasspath_fragment {
7196 name: "mysystemserverclasspathfragment",
7197 generate_classpaths_proto: false,
7198 contents: [
7199 "foo",
7200 ],
7201 apex_available: [
7202 "myapex",
7203 ],
7204 }
satayevabcd5972021-08-06 17:49:46 +01007205 `,
7206 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
7207 )
satayevb98371c2021-06-15 16:49:50 +01007208}
7209
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007210func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007211 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
7212 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
7213 // modules to be included in the BootJars.
7214 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
7215 return android.GroupFixturePreparers(
7216 dexpreopt.FixtureSetBootJars(bootJars...),
7217 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7218 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7219 }),
7220 )
7221 }
7222
7223 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
7224 // same value. This can result in an invalid configuration as it allows non art apex jars to be
7225 // specified in the ArtApexJars configuration.
7226 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
7227 return android.GroupFixturePreparers(
7228 dexpreopt.FixtureSetArtBootJars(bootJars...),
7229 dexpreopt.FixtureSetBootJars(bootJars...),
7230 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7231 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7232 }),
7233 )
7234 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007235
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007236 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007237 preparer := android.GroupFixturePreparers(
7238 java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib"),
7239 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7240 )
7241 fragments := []java.ApexVariantReference{
7242 {
7243 Apex: proptools.StringPtr("com.android.art.debug"),
7244 Module: proptools.StringPtr("art-bootclasspath-fragment"),
7245 },
7246 {
7247 Apex: proptools.StringPtr("some-non-updatable-apex"),
7248 Module: proptools.StringPtr("some-non-updatable-fragment"),
7249 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007250 }
satayevabcd5972021-08-06 17:49:46 +01007251 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007252 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007253
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007254 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007255 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
7256 // Update the dexpreopt BootJars directly.
satayevabcd5972021-08-06 17:49:46 +01007257 preparer := android.GroupFixturePreparers(
7258 prepareSetBootJars("com.android.art.debug:some-art-lib"),
7259 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7260 )
Paul Duffin60264a02021-04-12 20:02:36 +01007261 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007262 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007263
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007264 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 +01007265 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 +01007266 // Update the dexpreopt ArtApexJars directly.
7267 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
7268 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007269 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007270
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007271 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 +01007272 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 +01007273 // Update the dexpreopt ArtApexJars directly.
7274 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
7275 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007276 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007277
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007278 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 +01007279 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 +01007280 preparer := android.GroupFixturePreparers(
7281 java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib"),
7282 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7283 )
Paul Duffin60264a02021-04-12 20:02:36 +01007284 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007285 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007286
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007287 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 +01007288 preparer := java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01007289 fragment := java.ApexVariantReference{
7290 Apex: proptools.StringPtr("some-non-updatable-apex"),
7291 Module: proptools.StringPtr("some-non-updatable-fragment"),
7292 }
7293 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007294 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007295
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007296 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007297 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007298 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7299 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007300 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007301
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007302 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007303 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007304 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7305 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007306 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007307
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007308 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01007309 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01007310 // Update the dexpreopt ArtApexJars directly.
7311 preparer := prepareSetArtJars("platform:some-platform-lib")
7312 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007313 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007314
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007315 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007316 preparer := android.GroupFixturePreparers(
7317 java.FixtureConfigureBootJars("platform:some-platform-lib"),
7318 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7319 )
7320 fragments := []java.ApexVariantReference{
7321 {
7322 Apex: proptools.StringPtr("some-non-updatable-apex"),
7323 Module: proptools.StringPtr("some-non-updatable-fragment"),
7324 },
7325 }
7326 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007327 })
Paul Duffin064b70c2020-11-02 17:32:38 +00007328}
7329
7330func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007331 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00007332 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01007333 fragment := java.ApexVariantReference{
7334 Apex: proptools.StringPtr("myapex"),
7335 Module: proptools.StringPtr("my-bootclasspath-fragment"),
7336 }
7337
Paul Duffin064b70c2020-11-02 17:32:38 +00007338 testDexpreoptWithApexes(t, `
7339 prebuilt_apex {
7340 name: "myapex" ,
7341 arch: {
7342 arm64: {
7343 src: "myapex-arm64.apex",
7344 },
7345 arm: {
7346 src: "myapex-arm.apex",
7347 },
7348 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007349 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7350 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007351
Paul Duffin89f570a2021-06-16 01:42:33 +01007352 prebuilt_bootclasspath_fragment {
7353 name: "my-bootclasspath-fragment",
7354 contents: ["libfoo"],
7355 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01007356 hidden_api: {
7357 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
7358 metadata: "my-bootclasspath-fragment/metadata.csv",
7359 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01007360 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
7361 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
7362 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01007363 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007364 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007365
Paul Duffin89f570a2021-06-16 01:42:33 +01007366 java_import {
7367 name: "libfoo",
7368 jars: ["libfoo.jar"],
7369 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01007370 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007371 }
7372 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00007373 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007374}
7375
satayevd604b212021-07-21 14:23:52 +01007376func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01007377 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01007378 bp += `
7379 apex_key {
7380 name: "myapex.key",
7381 public_key: "testkey.avbpubkey",
7382 private_key: "testkey.pem",
7383 }`
Paul Duffin45338f02021-03-30 23:07:52 +01007384 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01007385 "lib1/src/A.java": nil,
7386 "lib2/src/B.java": nil,
7387 "system/sepolicy/apex/myapex-file_contexts": nil,
7388 }
7389
Paul Duffin45338f02021-03-30 23:07:52 +01007390 errorHandler := android.FixtureExpectsNoErrors
7391 if errmsg != "" {
7392 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07007393 }
Colin Crossae8600b2020-10-29 17:09:13 -07007394
Paul Duffin45338f02021-03-30 23:07:52 +01007395 android.GroupFixturePreparers(
7396 android.PrepareForTestWithAndroidBuildComponents,
7397 java.PrepareForTestWithJavaBuildComponents,
7398 PrepareForTestWithApexBuildComponents,
7399 android.PrepareForTestWithNeverallowRules(rules),
7400 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01007401 apexBootJars := make([]string, 0, len(bootJars))
7402 for _, apexBootJar := range bootJars {
7403 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01007404 }
satayevd604b212021-07-21 14:23:52 +01007405 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01007406 }),
7407 fs.AddToFixture(),
7408 ).
7409 ExtendWithErrorHandler(errorHandler).
7410 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01007411}
7412
7413func TestApexPermittedPackagesRules(t *testing.T) {
7414 testcases := []struct {
7415 name string
7416 expectedError string
7417 bp string
7418 bootJars []string
7419 modulesPackages map[string][]string
7420 }{
7421
7422 {
7423 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
7424 expectedError: "",
7425 bp: `
7426 java_library {
7427 name: "bcp_lib1",
7428 srcs: ["lib1/src/*.java"],
7429 permitted_packages: ["foo.bar"],
7430 apex_available: ["myapex"],
7431 sdk_version: "none",
7432 system_modules: "none",
Remi NGUYEN VAN1fdd6ca2021-12-02 19:39:35 +09007433 min_sdk_version: "30",
Andrei Onea115e7e72020-06-05 21:14:03 +01007434 }
7435 java_library {
7436 name: "nonbcp_lib2",
7437 srcs: ["lib2/src/*.java"],
7438 apex_available: ["myapex"],
7439 permitted_packages: ["a.b"],
7440 sdk_version: "none",
7441 system_modules: "none",
Remi NGUYEN VAN1fdd6ca2021-12-02 19:39:35 +09007442 min_sdk_version: "30",
Andrei Onea115e7e72020-06-05 21:14:03 +01007443 }
7444 apex {
7445 name: "myapex",
Remi NGUYEN VAN1fdd6ca2021-12-02 19:39:35 +09007446 min_sdk_version: "30",
Andrei Onea115e7e72020-06-05 21:14:03 +01007447 key: "myapex.key",
7448 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007449 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01007450 }`,
7451 bootJars: []string{"bcp_lib1"},
7452 modulesPackages: map[string][]string{
7453 "myapex": []string{
7454 "foo.bar",
7455 },
7456 },
7457 },
7458 {
Remi NGUYEN VAN1fdd6ca2021-12-02 19:39:35 +09007459 name: "Bootclasspath apex jar not satisfying allowed module packages on Q.",
Andrei Onead967aee2022-01-19 15:36:40 +00007460 expectedError: `(?s)module "bcp_lib2" .* which is restricted because jars that are part of the myapex module may only use these package prefixes: foo.bar with min_sdk < T. Please consider the following alternatives:\n 1. If the offending code is from a statically linked library, consider removing that dependency and using an alternative already in the bootclasspath, or perhaps a shared library. 2. Move the offending code into an allowed package.\n 3. Jarjar the offending code. Please be mindful of the potential system health implications of bundling that code, particularly if the offending jar is part of the bootclasspath.`,
Andrei Onea115e7e72020-06-05 21:14:03 +01007461 bp: `
7462 java_library {
7463 name: "bcp_lib1",
7464 srcs: ["lib1/src/*.java"],
7465 apex_available: ["myapex"],
7466 permitted_packages: ["foo.bar"],
7467 sdk_version: "none",
7468 system_modules: "none",
Remi NGUYEN VAN1fdd6ca2021-12-02 19:39:35 +09007469 min_sdk_version: "29",
Andrei Onea115e7e72020-06-05 21:14:03 +01007470 }
7471 java_library {
7472 name: "bcp_lib2",
7473 srcs: ["lib2/src/*.java"],
7474 apex_available: ["myapex"],
7475 permitted_packages: ["foo.bar", "bar.baz"],
7476 sdk_version: "none",
7477 system_modules: "none",
Remi NGUYEN VAN1fdd6ca2021-12-02 19:39:35 +09007478 min_sdk_version: "29",
Andrei Onea115e7e72020-06-05 21:14:03 +01007479 }
7480 apex {
7481 name: "myapex",
Remi NGUYEN VAN1fdd6ca2021-12-02 19:39:35 +09007482 min_sdk_version: "29",
7483 key: "myapex.key",
7484 java_libs: ["bcp_lib1", "bcp_lib2"],
7485 updatable: false,
7486 }
7487 `,
7488 bootJars: []string{"bcp_lib1", "bcp_lib2"},
7489 modulesPackages: map[string][]string{
7490 "myapex": []string{
7491 "foo.bar",
7492 },
7493 },
7494 },
7495 {
7496 name: "Bootclasspath apex jar not satisfying allowed module packages on R.",
Andrei Onead967aee2022-01-19 15:36:40 +00007497 expectedError: `(?s)module "bcp_lib2" .* which is restricted because jars that are part of the myapex module may only use these package prefixes: foo.bar with min_sdk < T. Please consider the following alternatives:\n 1. If the offending code is from a statically linked library, consider removing that dependency and using an alternative already in the bootclasspath, or perhaps a shared library. 2. Move the offending code into an allowed package.\n 3. Jarjar the offending code. Please be mindful of the potential system health implications of bundling that code, particularly if the offending jar is part of the bootclasspath.`,
Remi NGUYEN VAN1fdd6ca2021-12-02 19:39:35 +09007498 bp: `
7499 java_library {
7500 name: "bcp_lib1",
7501 srcs: ["lib1/src/*.java"],
7502 apex_available: ["myapex"],
7503 permitted_packages: ["foo.bar"],
7504 sdk_version: "none",
7505 system_modules: "none",
7506 min_sdk_version: "30",
7507 }
7508 java_library {
7509 name: "bcp_lib2",
7510 srcs: ["lib2/src/*.java"],
7511 apex_available: ["myapex"],
7512 permitted_packages: ["foo.bar", "bar.baz"],
7513 sdk_version: "none",
7514 system_modules: "none",
7515 min_sdk_version: "30",
7516 }
7517 apex {
7518 name: "myapex",
7519 min_sdk_version: "30",
7520 key: "myapex.key",
7521 java_libs: ["bcp_lib1", "bcp_lib2"],
7522 updatable: false,
7523 }
7524 `,
7525 bootJars: []string{"bcp_lib1", "bcp_lib2"},
7526 modulesPackages: map[string][]string{
7527 "myapex": []string{
7528 "foo.bar",
7529 },
7530 },
7531 },
7532 {
7533 name: "Bootclasspath apex jar >= T not satisfying Q/R/S allowed module packages.",
7534 expectedError: "",
7535 bp: `
7536 java_library {
7537 name: "bcp_lib1",
7538 srcs: ["lib1/src/*.java"],
7539 apex_available: ["myapex"],
7540 permitted_packages: ["foo.bar"],
7541 sdk_version: "none",
7542 system_modules: "none",
7543 min_sdk_version: "current",
7544 }
7545 java_library {
7546 name: "bcp_lib2",
7547 srcs: ["lib2/src/*.java"],
7548 apex_available: ["myapex"],
7549 permitted_packages: ["foo.bar", "bar.baz"],
7550 sdk_version: "none",
7551 system_modules: "none",
7552 min_sdk_version: "current",
7553 }
7554 apex {
7555 name: "myapex",
7556 min_sdk_version: "current",
Andrei Onea115e7e72020-06-05 21:14:03 +01007557 key: "myapex.key",
7558 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007559 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01007560 }
7561 `,
7562 bootJars: []string{"bcp_lib1", "bcp_lib2"},
7563 modulesPackages: map[string][]string{
7564 "myapex": []string{
7565 "foo.bar",
7566 },
7567 },
7568 },
7569 }
7570 for _, tc := range testcases {
7571 t.Run(tc.name, func(t *testing.T) {
7572 rules := createApexPermittedPackagesRules(tc.modulesPackages)
7573 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
7574 })
7575 }
7576}
7577
Jiyong Park62304bb2020-04-13 16:19:48 +09007578func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007579 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09007580 apex {
7581 name: "myapex",
7582 key: "myapex.key",
7583 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007584 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09007585 }
7586
7587 apex_key {
7588 name: "myapex.key",
7589 public_key: "testkey.avbpubkey",
7590 private_key: "testkey.pem",
7591 }
7592
7593 cc_library {
7594 name: "mylib",
7595 srcs: ["mylib.cpp"],
7596 system_shared_libs: [],
7597 stl: "none",
7598 stubs: {
7599 versions: ["1"],
7600 },
7601 apex_available: ["myapex"],
7602 }
7603
7604 cc_library {
7605 name: "myprivlib",
7606 srcs: ["mylib.cpp"],
7607 system_shared_libs: [],
7608 stl: "none",
7609 apex_available: ["myapex"],
7610 }
7611
7612
7613 cc_test {
7614 name: "mytest",
7615 gtest: false,
7616 srcs: ["mylib.cpp"],
7617 system_shared_libs: [],
7618 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09007619 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09007620 test_for: ["myapex"]
7621 }
Jiyong Park46a512f2020-12-04 18:02:13 +09007622
7623 cc_library {
7624 name: "mytestlib",
7625 srcs: ["mylib.cpp"],
7626 system_shared_libs: [],
7627 shared_libs: ["mylib", "myprivlib"],
7628 stl: "none",
7629 test_for: ["myapex"],
7630 }
7631
7632 cc_benchmark {
7633 name: "mybench",
7634 srcs: ["mylib.cpp"],
7635 system_shared_libs: [],
7636 shared_libs: ["mylib", "myprivlib"],
7637 stl: "none",
7638 test_for: ["myapex"],
7639 }
Jiyong Park62304bb2020-04-13 16:19:48 +09007640 `)
7641
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007642 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01007643 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007644 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
7645 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
7646 }
7647
7648 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09007649 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007650 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7651 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7652 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7653}
Jiyong Park46a512f2020-12-04 18:02:13 +09007654
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007655func TestIndirectTestFor(t *testing.T) {
7656 ctx := testApex(t, `
7657 apex {
7658 name: "myapex",
7659 key: "myapex.key",
7660 native_shared_libs: ["mylib", "myprivlib"],
7661 updatable: false,
7662 }
Jiyong Park46a512f2020-12-04 18:02:13 +09007663
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007664 apex_key {
7665 name: "myapex.key",
7666 public_key: "testkey.avbpubkey",
7667 private_key: "testkey.pem",
7668 }
7669
7670 cc_library {
7671 name: "mylib",
7672 srcs: ["mylib.cpp"],
7673 system_shared_libs: [],
7674 stl: "none",
7675 stubs: {
7676 versions: ["1"],
7677 },
7678 apex_available: ["myapex"],
7679 }
7680
7681 cc_library {
7682 name: "myprivlib",
7683 srcs: ["mylib.cpp"],
7684 system_shared_libs: [],
7685 stl: "none",
7686 shared_libs: ["mylib"],
7687 apex_available: ["myapex"],
7688 }
7689
7690 cc_library {
7691 name: "mytestlib",
7692 srcs: ["mylib.cpp"],
7693 system_shared_libs: [],
7694 shared_libs: ["myprivlib"],
7695 stl: "none",
7696 test_for: ["myapex"],
7697 }
7698 `)
7699
7700 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01007701 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007702 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
7703 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
7704 }
7705
7706 // The platform variant of mytestlib links to the platform variant of the
7707 // internal myprivlib.
7708 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
7709
7710 // The platform variant of myprivlib links to the platform variant of mylib
7711 // and bypasses its stubs.
7712 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 +09007713}
7714
Martin Stjernholmec009002021-03-27 15:18:31 +00007715func TestTestForForLibInOtherApex(t *testing.T) {
7716 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
7717 _ = testApex(t, `
7718 apex {
7719 name: "com.android.art",
7720 key: "myapex.key",
7721 native_shared_libs: ["mylib"],
7722 updatable: false,
7723 }
7724
7725 apex {
7726 name: "com.android.art.debug",
7727 key: "myapex.key",
7728 native_shared_libs: ["mylib", "mytestlib"],
7729 updatable: false,
7730 }
7731
7732 apex_key {
7733 name: "myapex.key",
7734 public_key: "testkey.avbpubkey",
7735 private_key: "testkey.pem",
7736 }
7737
7738 cc_library {
7739 name: "mylib",
7740 srcs: ["mylib.cpp"],
7741 system_shared_libs: [],
7742 stl: "none",
7743 stubs: {
7744 versions: ["1"],
7745 },
7746 apex_available: ["com.android.art", "com.android.art.debug"],
7747 }
7748
7749 cc_library {
7750 name: "mytestlib",
7751 srcs: ["mylib.cpp"],
7752 system_shared_libs: [],
7753 shared_libs: ["mylib"],
7754 stl: "none",
7755 apex_available: ["com.android.art.debug"],
7756 test_for: ["com.android.art"],
7757 }
7758 `,
7759 android.MockFS{
7760 "system/sepolicy/apex/com.android.art-file_contexts": nil,
7761 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
7762 }.AddToFixture())
7763}
7764
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007765// TODO(jungjw): Move this to proptools
7766func intPtr(i int) *int {
7767 return &i
7768}
7769
7770func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007771 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007772 apex_set {
7773 name: "myapex",
7774 set: "myapex.apks",
7775 filename: "foo_v2.apex",
7776 overrides: ["foo"],
7777 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007778 `,
7779 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7780 variables.Platform_sdk_version = intPtr(30)
7781 }),
7782 android.FixtureModifyConfig(func(config android.Config) {
7783 config.Targets[android.Android] = []android.Target{
7784 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
7785 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
7786 }
7787 }),
7788 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007789
Paul Duffin24704672021-04-06 16:09:30 +01007790 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007791
7792 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01007793 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007794 actual := extractedApex.Args["abis"]
7795 expected := "ARMEABI_V7A,ARM64_V8A"
7796 if actual != expected {
7797 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7798 }
7799 actual = extractedApex.Args["sdk-version"]
7800 expected = "30"
7801 if actual != expected {
7802 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7803 }
7804
Paul Duffin6717d882021-06-15 19:09:41 +01007805 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007806 a := m.Module().(*ApexSet)
7807 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07007808 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007809 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
7810 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
7811 }
7812}
7813
Jiyong Park7d95a512020-05-10 15:16:24 +09007814func TestNoStaticLinkingToStubsLib(t *testing.T) {
7815 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
7816 apex {
7817 name: "myapex",
7818 key: "myapex.key",
7819 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007820 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09007821 }
7822
7823 apex_key {
7824 name: "myapex.key",
7825 public_key: "testkey.avbpubkey",
7826 private_key: "testkey.pem",
7827 }
7828
7829 cc_library {
7830 name: "mylib",
7831 srcs: ["mylib.cpp"],
7832 static_libs: ["otherlib"],
7833 system_shared_libs: [],
7834 stl: "none",
7835 apex_available: [ "myapex" ],
7836 }
7837
7838 cc_library {
7839 name: "otherlib",
7840 srcs: ["mylib.cpp"],
7841 system_shared_libs: [],
7842 stl: "none",
7843 stubs: {
7844 versions: ["1", "2", "3"],
7845 },
7846 apex_available: [ "myapex" ],
7847 }
7848 `)
7849}
7850
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007851func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007852 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007853 apex {
7854 name: "myapex",
7855 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007856 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09007857 custom_sign_tool: "sign_myapex",
7858 }
7859
7860 apex_key {
7861 name: "myapex.key",
7862 public_key: "testkey.avbpubkey",
7863 private_key: "testkey.pem",
7864 }
7865 `)
7866
7867 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7868 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7869 ensureContains(t, content, `name="myapex.apex" public_key="vendor/foo/devkeys/testkey.avbpubkey" private_key="vendor/foo/devkeys/testkey.pem" container_certificate="vendor/foo/devkeys/test.x509.pem" container_private_key="vendor/foo/devkeys/test.pk8" partition="system_ext" sign_tool="sign_myapex"`)
7870}
7871
7872func TestApexKeysTxtOverrides(t *testing.T) {
7873 ctx := testApex(t, `
7874 apex {
7875 name: "myapex",
7876 key: "myapex.key",
7877 updatable: false,
7878 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007879 }
7880
7881 apex_key {
7882 name: "myapex.key",
7883 public_key: "testkey.avbpubkey",
7884 private_key: "testkey.pem",
7885 }
7886
7887 prebuilt_apex {
7888 name: "myapex",
7889 prefer: true,
7890 arch: {
7891 arm64: {
7892 src: "myapex-arm64.apex",
7893 },
7894 arm: {
7895 src: "myapex-arm.apex",
7896 },
7897 },
7898 }
7899
7900 apex_set {
7901 name: "myapex_set",
7902 set: "myapex.apks",
7903 filename: "myapex_set.apex",
7904 overrides: ["myapex"],
7905 }
7906 `)
7907
7908 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7909 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7910 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 +09007911 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 +09007912}
7913
Jooyung Han938b5932020-06-20 12:47:47 +09007914func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007915 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007916 apex {
7917 name: "myapex",
7918 key: "myapex.key",
7919 apps: ["app"],
7920 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007921 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007922 }
7923
7924 apex_key {
7925 name: "myapex.key",
7926 public_key: "testkey.avbpubkey",
7927 private_key: "testkey.pem",
7928 }
7929
7930 android_app {
7931 name: "app",
7932 srcs: ["foo/bar/MyClass.java"],
7933 package_name: "foo",
7934 sdk_version: "none",
7935 system_modules: "none",
7936 apex_available: [ "myapex" ],
7937 }
7938 `, withFiles(map[string][]byte{
7939 "sub/Android.bp": []byte(`
7940 override_apex {
7941 name: "override_myapex",
7942 base: "myapex",
7943 apps: ["override_app"],
7944 allowed_files: ":allowed",
7945 }
7946 // Overridable "path" property should be referenced indirectly
7947 filegroup {
7948 name: "allowed",
7949 srcs: ["allowed.txt"],
7950 }
7951 override_android_app {
7952 name: "override_app",
7953 base: "app",
7954 package_name: "bar",
7955 }
7956 `),
7957 }))
7958
7959 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7960 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7961 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7962 }
7963
7964 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7965 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7966 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7967 }
7968}
7969
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007970func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007971 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007972 apex {
7973 name: "myapex",
7974 key: "myapex.key",
7975 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007976 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007977 }
7978
7979 apex_key {
7980 name: "myapex.key",
7981 public_key: "testkey.avbpubkey",
7982 private_key: "testkey.pem",
7983 }
7984
7985 cc_library {
7986 name: "mylib",
7987 srcs: ["mylib.cpp"],
7988 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007989 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007990 },
7991 apex_available: ["myapex"],
7992 }
7993
7994 cc_prebuilt_library_shared {
7995 name: "mylib",
7996 prefer: false,
7997 srcs: ["prebuilt.so"],
7998 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007999 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008000 },
8001 apex_available: ["myapex"],
8002 }
8003 `)
8004}
8005
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008006func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008007 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008008 apex {
8009 name: "myapex",
8010 key: "myapex.key",
8011 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008012 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008013 }
8014 apex_key {
8015 name: "myapex.key",
8016 public_key: "testkey.avbpubkey",
8017 private_key: "testkey.pem",
8018 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008019 `,
8020 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8021 variables.CompressedApex = proptools.BoolPtr(true)
8022 }),
8023 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008024
8025 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
8026 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
8027
8028 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
8029 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
8030
8031 // Make sure output of bundle is .capex
8032 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8033 ensureContains(t, ab.outputFile.String(), "myapex.capex")
8034
8035 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07008036 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008037 var builder strings.Builder
8038 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8039 androidMk := builder.String()
8040 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
8041}
8042
Martin Stjernholm2856c662020-12-02 15:03:42 +00008043func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008044 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00008045 apex {
8046 name: "myapex",
8047 key: "myapex.key",
8048 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008049 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00008050 }
8051
8052 apex_key {
8053 name: "myapex.key",
8054 public_key: "testkey.avbpubkey",
8055 private_key: "testkey.pem",
8056 }
8057
8058 cc_library {
8059 name: "mylib",
8060 srcs: ["mylib.cpp"],
8061 apex_available: ["myapex"],
8062 shared_libs: ["otherlib"],
8063 system_shared_libs: [],
8064 }
8065
8066 cc_library {
8067 name: "otherlib",
8068 srcs: ["mylib.cpp"],
8069 stubs: {
8070 versions: ["current"],
8071 },
8072 }
8073
8074 cc_prebuilt_library_shared {
8075 name: "otherlib",
8076 prefer: true,
8077 srcs: ["prebuilt.so"],
8078 stubs: {
8079 versions: ["current"],
8080 },
8081 }
8082 `)
8083
8084 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07008085 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00008086 var builder strings.Builder
8087 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8088 androidMk := builder.String()
8089
8090 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
8091 // a thing there.
8092 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
8093}
8094
Jiyong Parke3867542020-12-03 17:28:25 +09008095func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008096 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09008097 apex {
8098 name: "myapex",
8099 key: "myapex.key",
8100 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008101 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09008102 }
8103
8104 apex_key {
8105 name: "myapex.key",
8106 public_key: "testkey.avbpubkey",
8107 private_key: "testkey.pem",
8108 }
8109
8110 cc_library {
8111 name: "mylib",
8112 srcs: ["mylib.cpp"],
8113 system_shared_libs: [],
8114 stl: "none",
8115 apex_available: ["myapex"],
8116 shared_libs: ["mylib2"],
8117 target: {
8118 apex: {
8119 exclude_shared_libs: ["mylib2"],
8120 },
8121 },
8122 }
8123
8124 cc_library {
8125 name: "mylib2",
8126 srcs: ["mylib.cpp"],
8127 system_shared_libs: [],
8128 stl: "none",
8129 }
8130 `)
8131
8132 // Check if mylib is linked to mylib2 for the non-apex target
8133 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
8134 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
8135
8136 // Make sure that the link doesn't occur for the apex target
8137 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
8138 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
8139
8140 // It shouldn't appear in the copy cmd as well.
8141 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
8142 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
8143}
8144
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008145func TestPrebuiltStubLibDep(t *testing.T) {
8146 bpBase := `
8147 apex {
8148 name: "myapex",
8149 key: "myapex.key",
8150 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008151 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008152 }
8153 apex_key {
8154 name: "myapex.key",
8155 public_key: "testkey.avbpubkey",
8156 private_key: "testkey.pem",
8157 }
8158 cc_library {
8159 name: "mylib",
8160 srcs: ["mylib.cpp"],
8161 apex_available: ["myapex"],
8162 shared_libs: ["stublib"],
8163 system_shared_libs: [],
8164 }
8165 apex {
8166 name: "otherapex",
8167 enabled: %s,
8168 key: "myapex.key",
8169 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008170 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008171 }
8172 `
8173
8174 stublibSourceBp := `
8175 cc_library {
8176 name: "stublib",
8177 srcs: ["mylib.cpp"],
8178 apex_available: ["otherapex"],
8179 system_shared_libs: [],
8180 stl: "none",
8181 stubs: {
8182 versions: ["1"],
8183 },
8184 }
8185 `
8186
8187 stublibPrebuiltBp := `
8188 cc_prebuilt_library_shared {
8189 name: "stublib",
8190 srcs: ["prebuilt.so"],
8191 apex_available: ["otherapex"],
8192 stubs: {
8193 versions: ["1"],
8194 },
8195 %s
8196 }
8197 `
8198
8199 tests := []struct {
8200 name string
8201 stublibBp string
8202 usePrebuilt bool
8203 modNames []string // Modules to collect AndroidMkEntries for
8204 otherApexEnabled []string
8205 }{
8206 {
8207 name: "only_source",
8208 stublibBp: stublibSourceBp,
8209 usePrebuilt: false,
8210 modNames: []string{"stublib"},
8211 otherApexEnabled: []string{"true", "false"},
8212 },
8213 {
8214 name: "source_preferred",
8215 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
8216 usePrebuilt: false,
8217 modNames: []string{"stublib", "prebuilt_stublib"},
8218 otherApexEnabled: []string{"true", "false"},
8219 },
8220 {
8221 name: "prebuilt_preferred",
8222 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
8223 usePrebuilt: true,
8224 modNames: []string{"stublib", "prebuilt_stublib"},
8225 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8226 },
8227 {
8228 name: "only_prebuilt",
8229 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
8230 usePrebuilt: true,
8231 modNames: []string{"stublib"},
8232 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8233 },
8234 }
8235
8236 for _, test := range tests {
8237 t.Run(test.name, func(t *testing.T) {
8238 for _, otherApexEnabled := range test.otherApexEnabled {
8239 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008240 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008241
8242 type modAndMkEntries struct {
8243 mod *cc.Module
8244 mkEntries android.AndroidMkEntries
8245 }
8246 entries := []*modAndMkEntries{}
8247
8248 // Gather shared lib modules that are installable
8249 for _, modName := range test.modNames {
8250 for _, variant := range ctx.ModuleVariantsForTests(modName) {
8251 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
8252 continue
8253 }
8254 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08008255 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008256 continue
8257 }
Colin Crossaa255532020-07-03 13:18:24 -07008258 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008259 if ent.Disabled {
8260 continue
8261 }
8262 entries = append(entries, &modAndMkEntries{
8263 mod: mod,
8264 mkEntries: ent,
8265 })
8266 }
8267 }
8268 }
8269
8270 var entry *modAndMkEntries = nil
8271 for _, ent := range entries {
8272 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
8273 if entry != nil {
8274 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
8275 } else {
8276 entry = ent
8277 }
8278 }
8279 }
8280
8281 if entry == nil {
8282 t.Errorf("AndroidMk entry for \"stublib\" missing")
8283 } else {
8284 isPrebuilt := entry.mod.Prebuilt() != nil
8285 if isPrebuilt != test.usePrebuilt {
8286 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
8287 }
8288 if !entry.mod.IsStubs() {
8289 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
8290 }
8291 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
8292 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
8293 }
Jiyong Park892a98f2020-12-14 09:20:00 +09008294 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09008295 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09008296 if !android.InList(expected, cflags) {
8297 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
8298 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008299 }
8300 })
8301 }
8302 })
8303 }
8304}
8305
Martin Stjernholmdf298b32021-05-21 20:57:29 +01008306func TestHostApexInHostOnlyBuild(t *testing.T) {
8307 testApex(t, `
8308 apex {
8309 name: "myapex",
8310 host_supported: true,
8311 key: "myapex.key",
8312 updatable: false,
8313 payload_type: "zip",
8314 }
8315 apex_key {
8316 name: "myapex.key",
8317 public_key: "testkey.avbpubkey",
8318 private_key: "testkey.pem",
8319 }
8320 `,
8321 android.FixtureModifyConfig(func(config android.Config) {
8322 // We may not have device targets in all builds, e.g. in
8323 // prebuilts/build-tools/build-prebuilts.sh
8324 config.Targets[android.Android] = []android.Target{}
8325 }))
8326}
8327
Colin Crossc33e5212021-05-25 18:16:02 -07008328func TestApexJavaCoverage(t *testing.T) {
8329 bp := `
8330 apex {
8331 name: "myapex",
8332 key: "myapex.key",
8333 java_libs: ["mylib"],
8334 bootclasspath_fragments: ["mybootclasspathfragment"],
8335 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
8336 updatable: false,
8337 }
8338
8339 apex_key {
8340 name: "myapex.key",
8341 public_key: "testkey.avbpubkey",
8342 private_key: "testkey.pem",
8343 }
8344
8345 java_library {
8346 name: "mylib",
8347 srcs: ["mylib.java"],
8348 apex_available: ["myapex"],
8349 compile_dex: true,
8350 }
8351
8352 bootclasspath_fragment {
8353 name: "mybootclasspathfragment",
8354 contents: ["mybootclasspathlib"],
8355 apex_available: ["myapex"],
8356 }
8357
8358 java_library {
8359 name: "mybootclasspathlib",
8360 srcs: ["mybootclasspathlib.java"],
8361 apex_available: ["myapex"],
8362 compile_dex: true,
8363 }
8364
8365 systemserverclasspath_fragment {
8366 name: "mysystemserverclasspathfragment",
8367 contents: ["mysystemserverclasspathlib"],
8368 apex_available: ["myapex"],
8369 }
8370
8371 java_library {
8372 name: "mysystemserverclasspathlib",
8373 srcs: ["mysystemserverclasspathlib.java"],
8374 apex_available: ["myapex"],
8375 compile_dex: true,
8376 }
8377 `
8378
8379 result := android.GroupFixturePreparers(
8380 PrepareForTestWithApexBuildComponents,
8381 prepareForTestWithMyapex,
8382 java.PrepareForTestWithJavaDefaultModules,
8383 android.PrepareForTestWithAndroidBuildComponents,
8384 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01008385 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
8386 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Colin Crossc33e5212021-05-25 18:16:02 -07008387 android.FixtureMergeEnv(map[string]string{
8388 "EMMA_INSTRUMENT": "true",
8389 }),
8390 ).RunTest(t)
8391
8392 // Make sure jacoco ran on both mylib and mybootclasspathlib
8393 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8394 t.Errorf("Failed to find jacoco rule for mylib")
8395 }
8396 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8397 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
8398 }
8399 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8400 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
8401 }
8402}
8403
Jiyong Park192600a2021-08-03 07:52:17 +00008404func TestProhibitStaticExecutable(t *testing.T) {
8405 testApexError(t, `executable mybin is static`, `
8406 apex {
8407 name: "myapex",
8408 key: "myapex.key",
8409 binaries: ["mybin"],
8410 min_sdk_version: "29",
8411 }
8412
8413 apex_key {
8414 name: "myapex.key",
8415 public_key: "testkey.avbpubkey",
8416 private_key: "testkey.pem",
8417 }
8418
8419 cc_binary {
8420 name: "mybin",
8421 srcs: ["mylib.cpp"],
8422 relative_install_path: "foo/bar",
8423 static_executable: true,
8424 system_shared_libs: [],
8425 stl: "none",
8426 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09008427 min_sdk_version: "29",
8428 }
8429 `)
8430
8431 testApexError(t, `executable mybin.rust is static`, `
8432 apex {
8433 name: "myapex",
8434 key: "myapex.key",
8435 binaries: ["mybin.rust"],
8436 min_sdk_version: "29",
8437 }
8438
8439 apex_key {
8440 name: "myapex.key",
8441 public_key: "testkey.avbpubkey",
8442 private_key: "testkey.pem",
8443 }
8444
8445 rust_binary {
8446 name: "mybin.rust",
8447 srcs: ["foo.rs"],
8448 static_executable: true,
8449 apex_available: ["myapex"],
8450 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00008451 }
8452 `)
8453}
8454
Jiakai Zhang470b7e22021-09-30 09:34:26 +00008455func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
8456 ctx := testApex(t, `
8457 apex {
8458 name: "myapex",
8459 key: "myapex.key",
8460 updatable: false,
8461 java_libs: ["foo"],
8462 }
8463
8464 apex_key {
8465 name: "myapex.key",
8466 public_key: "testkey.avbpubkey",
8467 private_key: "testkey.pem",
8468 }
8469
8470 java_library {
8471 name: "foo",
8472 srcs: ["foo.java"],
8473 apex_available: ["myapex"],
8474 installable: true,
8475 }
8476 `,
8477 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8478 )
8479
8480 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8481 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
8482 var builder strings.Builder
8483 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
8484 androidMk := builder.String()
8485 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex")
8486}
8487
8488func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
8489 ctx := testApex(t, `
8490 prebuilt_apex {
8491 name: "myapex",
8492 arch: {
8493 arm64: {
8494 src: "myapex-arm64.apex",
8495 },
8496 arm: {
8497 src: "myapex-arm.apex",
8498 },
8499 },
8500 exported_java_libs: ["foo"],
8501 }
8502
8503 java_import {
8504 name: "foo",
8505 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00008506 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00008507 }
8508 `,
8509 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8510 )
8511
8512 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
8513 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
8514 mainModuleEntries := entriesList[0]
8515 android.AssertArrayString(t,
8516 "LOCAL_REQUIRED_MODULES",
8517 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
8518 []string{
8519 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
8520 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
8521 })
8522}
8523
Jiyong Parkcacc4f32021-10-28 14:26:03 +09008524func TestAndroidMk_RequiredModules(t *testing.T) {
8525 ctx := testApex(t, `
8526 apex {
8527 name: "myapex",
8528 key: "myapex.key",
8529 updatable: false,
8530 java_libs: ["foo"],
8531 required: ["otherapex"],
8532 }
8533
8534 apex {
8535 name: "otherapex",
8536 key: "myapex.key",
8537 updatable: false,
8538 java_libs: ["foo"],
8539 required: ["otherapex"],
8540 }
8541
8542 apex_key {
8543 name: "myapex.key",
8544 public_key: "testkey.avbpubkey",
8545 private_key: "testkey.pem",
8546 }
8547
8548 java_library {
8549 name: "foo",
8550 srcs: ["foo.java"],
8551 apex_available: ["myapex", "otherapex"],
8552 installable: true,
8553 }
8554 `)
8555
8556 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8557 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
8558 var builder strings.Builder
8559 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
8560 androidMk := builder.String()
8561 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherapex")
8562}
8563
satayev758968a2021-12-06 11:42:40 +00008564func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
8565 preparer := android.GroupFixturePreparers(
8566 PrepareForTestWithApexBuildComponents,
8567 prepareForTestWithMyapex,
8568 java.PrepareForTestWithJavaSdkLibraryFiles,
8569 java.PrepareForTestWithJavaDefaultModules,
8570 android.PrepareForTestWithAndroidBuildComponents,
8571 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
8572 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
8573 )
8574
8575 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
8576 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
8577 preparer.RunTestWithBp(t, `
8578 apex {
8579 name: "myapex",
8580 key: "myapex.key",
8581 bootclasspath_fragments: ["mybootclasspathfragment"],
8582 min_sdk_version: "30",
8583 updatable: false,
8584 }
8585
8586 apex_key {
8587 name: "myapex.key",
8588 public_key: "testkey.avbpubkey",
8589 private_key: "testkey.pem",
8590 }
8591
8592 bootclasspath_fragment {
8593 name: "mybootclasspathfragment",
8594 contents: ["mybootclasspathlib"],
8595 apex_available: ["myapex"],
8596 }
8597
8598 java_sdk_library {
8599 name: "mybootclasspathlib",
8600 srcs: ["mybootclasspathlib.java"],
8601 apex_available: ["myapex"],
8602 compile_dex: true,
8603 unsafe_ignore_missing_latest_api: true,
8604 min_sdk_version: "31",
8605 static_libs: ["util"],
8606 }
8607
8608 java_library {
8609 name: "util",
8610 srcs: ["a.java"],
8611 apex_available: ["myapex"],
8612 min_sdk_version: "31",
8613 static_libs: ["another_util"],
8614 }
8615
8616 java_library {
8617 name: "another_util",
8618 srcs: ["a.java"],
8619 min_sdk_version: "31",
8620 apex_available: ["myapex"],
8621 }
8622 `)
8623 })
8624
8625 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
8626 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
8627 preparer.RunTestWithBp(t, `
8628 apex {
8629 name: "myapex",
8630 key: "myapex.key",
8631 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
8632 min_sdk_version: "30",
8633 updatable: false,
8634 }
8635
8636 apex_key {
8637 name: "myapex.key",
8638 public_key: "testkey.avbpubkey",
8639 private_key: "testkey.pem",
8640 }
8641
8642 systemserverclasspath_fragment {
8643 name: "mysystemserverclasspathfragment",
8644 contents: ["mysystemserverclasspathlib"],
8645 apex_available: ["myapex"],
8646 }
8647
8648 java_sdk_library {
8649 name: "mysystemserverclasspathlib",
8650 srcs: ["mysystemserverclasspathlib.java"],
8651 apex_available: ["myapex"],
8652 compile_dex: true,
8653 min_sdk_version: "32",
8654 unsafe_ignore_missing_latest_api: true,
8655 static_libs: ["util"],
8656 }
8657
8658 java_library {
8659 name: "util",
8660 srcs: ["a.java"],
8661 apex_available: ["myapex"],
8662 min_sdk_version: "31",
8663 static_libs: ["another_util"],
8664 }
8665
8666 java_library {
8667 name: "another_util",
8668 srcs: ["a.java"],
8669 min_sdk_version: "31",
8670 apex_available: ["myapex"],
8671 }
8672 `)
8673 })
8674
8675 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
8676 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
8677 RunTestWithBp(t, `
8678 apex {
8679 name: "myapex",
8680 key: "myapex.key",
8681 bootclasspath_fragments: ["mybootclasspathfragment"],
8682 min_sdk_version: "30",
8683 updatable: false,
8684 }
8685
8686 apex_key {
8687 name: "myapex.key",
8688 public_key: "testkey.avbpubkey",
8689 private_key: "testkey.pem",
8690 }
8691
8692 bootclasspath_fragment {
8693 name: "mybootclasspathfragment",
8694 contents: ["mybootclasspathlib"],
8695 apex_available: ["myapex"],
8696 }
8697
8698 java_sdk_library {
8699 name: "mybootclasspathlib",
8700 srcs: ["mybootclasspathlib.java"],
8701 apex_available: ["myapex"],
8702 compile_dex: true,
8703 unsafe_ignore_missing_latest_api: true,
8704 }
8705 `)
8706 })
8707
8708 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
8709 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
8710 RunTestWithBp(t, `
8711 apex {
8712 name: "myapex",
8713 key: "myapex.key",
8714 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
8715 min_sdk_version: "30",
8716 updatable: false,
8717 }
8718
8719 apex_key {
8720 name: "myapex.key",
8721 public_key: "testkey.avbpubkey",
8722 private_key: "testkey.pem",
8723 }
8724
8725 systemserverclasspath_fragment {
8726 name: "mysystemserverclasspathfragment",
8727 contents: ["mysystemserverclasspathlib"],
8728 apex_available: ["myapex"],
8729 }
8730
8731 java_sdk_library {
8732 name: "mysystemserverclasspathlib",
8733 srcs: ["mysystemserverclasspathlib.java"],
8734 apex_available: ["myapex"],
8735 compile_dex: true,
8736 unsafe_ignore_missing_latest_api: true,
8737 }
8738 `)
8739 })
8740}
8741
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07008742func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01008743 os.Exit(m.Run())
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07008744}