blob: 36d53064fa892b9421cc47f31154c96a1bb97697 [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
Albert Martin55ccba22022-03-21 20:11:16 +0000116func withApexGlobalMinSdkVersionOverride(minSdkOverride *string) android.FixturePreparer {
117 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
118 variables.ApexGlobalMinSdkVersionOverride = minSdkOverride
119 })
120}
121
Paul Duffin810f33d2021-03-09 14:12:32 +0000122var withBinder32bit = android.FixtureModifyProductVariables(
123 func(variables android.FixtureProductVariables) {
124 variables.Binder32bit = proptools.BoolPtr(true)
125 },
126)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900127
Paul Duffin810f33d2021-03-09 14:12:32 +0000128var withUnbundledBuild = android.FixtureModifyProductVariables(
129 func(variables android.FixtureProductVariables) {
130 variables.Unbundled_build = proptools.BoolPtr(true)
131 },
132)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900133
Paul Duffin284165a2021-03-29 01:50:31 +0100134// Legacy preparer used for running tests within the apex package.
135//
136// This includes everything that was needed to run any test in the apex package prior to the
137// introduction of the test fixtures. Tests that are being converted to use fixtures directly
138// rather than through the testApex...() methods should avoid using this and instead use the
139// various preparers directly, using android.GroupFixturePreparers(...) to group them when
140// necessary.
141//
142// deprecated
143var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000144 // General preparers in alphabetical order as test infrastructure will enforce correct
145 // registration order.
146 android.PrepareForTestWithAndroidBuildComponents,
147 bpf.PrepareForTestWithBpf,
148 cc.PrepareForTestWithCcBuildComponents,
149 java.PrepareForTestWithJavaDefaultModules,
150 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
151 rust.PrepareForTestWithRustDefaultModules,
152 sh.PrepareForTestWithShBuildComponents,
153
154 PrepareForTestWithApexBuildComponents,
155
156 // Additional apex test specific preparers.
157 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
158 filegroup {
159 name: "myapex-file_contexts",
160 srcs: [
161 "apex/myapex-file_contexts",
162 ],
163 }
164 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000165 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000166 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000167 "a.java": nil,
168 "PrebuiltAppFoo.apk": nil,
169 "PrebuiltAppFooPriv.apk": nil,
170 "apex_manifest.json": nil,
171 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000172 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
173 "system/sepolicy/apex/myapex2-file_contexts": nil,
174 "system/sepolicy/apex/otherapex-file_contexts": nil,
175 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
176 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
Colin Crossabc0dab2022-04-07 17:39:21 -0700177 "mylib.cpp": nil,
178 "mytest.cpp": nil,
179 "mytest1.cpp": nil,
180 "mytest2.cpp": nil,
181 "mytest3.cpp": nil,
182 "myprebuilt": nil,
183 "my_include": nil,
184 "foo/bar/MyClass.java": nil,
185 "prebuilt.jar": nil,
186 "prebuilt.so": nil,
187 "vendor/foo/devkeys/test.x509.pem": nil,
188 "vendor/foo/devkeys/test.pk8": nil,
189 "testkey.x509.pem": nil,
190 "testkey.pk8": nil,
191 "testkey.override.x509.pem": nil,
192 "testkey.override.pk8": nil,
193 "vendor/foo/devkeys/testkey.avbpubkey": nil,
194 "vendor/foo/devkeys/testkey.pem": nil,
195 "NOTICE": nil,
196 "custom_notice": nil,
197 "custom_notice_for_static_lib": nil,
198 "testkey2.avbpubkey": nil,
199 "testkey2.pem": nil,
200 "myapex-arm64.apex": nil,
201 "myapex-arm.apex": nil,
202 "myapex.apks": nil,
203 "frameworks/base/api/current.txt": nil,
204 "framework/aidl/a.aidl": nil,
205 "dummy.txt": nil,
206 "baz": nil,
207 "bar/baz": nil,
208 "testdata/baz": nil,
209 "AppSet.apks": nil,
210 "foo.rs": nil,
211 "libfoo.jar": nil,
212 "libbar.jar": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000213 },
214 ),
215
216 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
217 variables.DeviceVndkVersion = proptools.StringPtr("current")
218 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
219 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
220 variables.Platform_sdk_codename = proptools.StringPtr("Q")
221 variables.Platform_sdk_final = proptools.BoolPtr(false)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000222 // "Tiramisu" needs to be in the next line for compatibility with soong code,
223 // not because of these tests specifically (it's not used by the tests)
224 variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900225 variables.Platform_vndk_version = proptools.StringPtr("29")
Oriol Prieto Gascóa70425f2022-05-20 13:05:34 +0000226 variables.BuildId = proptools.StringPtr("TEST.BUILD_ID")
Paul Duffin37aad602021-03-08 09:47:16 +0000227 }),
228)
229
Paul Duffin52bfaa42021-03-23 23:40:12 +0000230var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
231 "system/sepolicy/apex/myapex-file_contexts": nil,
232})
233
Jooyung Han643adc42020-02-27 13:50:06 +0900234// ensure that 'result' equals 'expected'
235func ensureEquals(t *testing.T, result string, expected string) {
236 t.Helper()
237 if result != expected {
238 t.Errorf("%q != %q", expected, result)
239 }
240}
241
Jiyong Park25fc6a92018-11-18 18:02:45 +0900242// ensure that 'result' contains 'expected'
243func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900244 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900245 if !strings.Contains(result, expected) {
246 t.Errorf("%q is not found in %q", expected, result)
247 }
248}
249
Liz Kammer5bd365f2020-05-27 15:15:11 -0700250// ensure that 'result' contains 'expected' exactly one time
251func ensureContainsOnce(t *testing.T, result string, expected string) {
252 t.Helper()
253 count := strings.Count(result, expected)
254 if count != 1 {
255 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
256 }
257}
258
Jiyong Park25fc6a92018-11-18 18:02:45 +0900259// ensures that 'result' does not contain 'notExpected'
260func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900261 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900262 if strings.Contains(result, notExpected) {
263 t.Errorf("%q is found in %q", notExpected, result)
264 }
265}
266
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700267func ensureMatches(t *testing.T, result string, expectedRex string) {
268 ok, err := regexp.MatchString(expectedRex, result)
269 if err != nil {
270 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
271 return
272 }
273 if !ok {
274 t.Errorf("%s does not match regular expession %s", result, expectedRex)
275 }
276}
277
Jiyong Park25fc6a92018-11-18 18:02:45 +0900278func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900279 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 if !android.InList(expected, result) {
281 t.Errorf("%q is not found in %v", expected, result)
282 }
283}
284
285func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900286 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287 if android.InList(notExpected, result) {
288 t.Errorf("%q is found in %v", notExpected, result)
289 }
290}
291
Jooyung Hane1633032019-08-01 17:41:43 +0900292func ensureListEmpty(t *testing.T, result []string) {
293 t.Helper()
294 if len(result) > 0 {
295 t.Errorf("%q is expected to be empty", result)
296 }
297}
298
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000299func ensureListNotEmpty(t *testing.T, result []string) {
300 t.Helper()
301 if len(result) == 0 {
302 t.Errorf("%q is expected to be not empty", result)
303 }
304}
305
Jiyong Park25fc6a92018-11-18 18:02:45 +0900306// Minimal test
307func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800308 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900309 apex_defaults {
310 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900311 manifest: ":myapex.manifest",
312 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900313 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900314 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900315 native_shared_libs: [
316 "mylib",
317 "libfoo.ffi",
318 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900319 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800320 multilib: {
321 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900322 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800323 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900324 },
Jiyong Park77acec62020-06-01 21:39:15 +0900325 java_libs: [
326 "myjar",
327 "myjar_dex",
328 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000329 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 }
331
Jiyong Park30ca9372019-02-07 16:27:23 +0900332 apex {
333 name: "myapex",
334 defaults: ["myapex-defaults"],
335 }
336
Jiyong Park25fc6a92018-11-18 18:02:45 +0900337 apex_key {
338 name: "myapex.key",
339 public_key: "testkey.avbpubkey",
340 private_key: "testkey.pem",
341 }
342
Jiyong Park809bb722019-02-13 21:33:49 +0900343 filegroup {
344 name: "myapex.manifest",
345 srcs: ["apex_manifest.json"],
346 }
347
348 filegroup {
349 name: "myapex.androidmanifest",
350 srcs: ["AndroidManifest.xml"],
351 }
352
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353 cc_library {
354 name: "mylib",
355 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900356 shared_libs: [
357 "mylib2",
358 "libbar.ffi",
359 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900360 system_shared_libs: [],
361 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000362 // TODO: remove //apex_available:platform
363 apex_available: [
364 "//apex_available:platform",
365 "myapex",
366 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900367 }
368
Alex Light3d673592019-01-18 14:37:31 -0800369 cc_binary {
370 name: "foo",
371 srcs: ["mylib.cpp"],
372 compile_multilib: "both",
373 multilib: {
374 lib32: {
375 suffix: "32",
376 },
377 lib64: {
378 suffix: "64",
379 },
380 },
381 symlinks: ["foo_link_"],
382 symlink_preferred_arch: true,
383 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800384 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700385 apex_available: [ "myapex", "com.android.gki.*" ],
386 }
387
Jiyong Park99644e92020-11-17 22:21:02 +0900388 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000389 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900390 srcs: ["foo.rs"],
391 rlibs: ["libfoo.rlib.rust"],
392 dylibs: ["libfoo.dylib.rust"],
393 apex_available: ["myapex"],
394 }
395
396 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000397 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900398 srcs: ["foo.rs"],
399 crate_name: "foo",
400 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900401 shared_libs: ["libfoo.shared_from_rust"],
402 }
403
404 cc_library_shared {
405 name: "libfoo.shared_from_rust",
406 srcs: ["mylib.cpp"],
407 system_shared_libs: [],
408 stl: "none",
409 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900410 }
411
412 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000413 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900414 srcs: ["foo.rs"],
415 crate_name: "foo",
416 apex_available: ["myapex"],
417 }
418
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900419 rust_ffi_shared {
420 name: "libfoo.ffi",
421 srcs: ["foo.rs"],
422 crate_name: "foo",
423 apex_available: ["myapex"],
424 }
425
426 rust_ffi_shared {
427 name: "libbar.ffi",
428 srcs: ["foo.rs"],
429 crate_name: "bar",
430 apex_available: ["myapex"],
431 }
432
Yifan Hongd22a84a2020-07-28 17:37:46 -0700433 apex {
434 name: "com.android.gki.fake",
435 binaries: ["foo"],
436 key: "myapex.key",
437 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000438 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800439 }
440
Paul Duffindddd5462020-04-07 15:25:44 +0100441 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900442 name: "mylib2",
443 srcs: ["mylib.cpp"],
444 system_shared_libs: [],
445 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900446 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900447 static_libs: ["libstatic"],
448 // TODO: remove //apex_available:platform
449 apex_available: [
450 "//apex_available:platform",
451 "myapex",
452 ],
453 }
454
Paul Duffindddd5462020-04-07 15:25:44 +0100455 cc_prebuilt_library_shared {
456 name: "mylib2",
457 srcs: ["prebuilt.so"],
458 // TODO: remove //apex_available:platform
459 apex_available: [
460 "//apex_available:platform",
461 "myapex",
462 ],
463 }
464
Jiyong Park9918e1a2020-03-17 19:16:40 +0900465 cc_library_static {
466 name: "libstatic",
467 srcs: ["mylib.cpp"],
468 system_shared_libs: [],
469 stl: "none",
470 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000471 // TODO: remove //apex_available:platform
472 apex_available: [
473 "//apex_available:platform",
474 "myapex",
475 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900476 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900477
478 java_library {
479 name: "myjar",
480 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900481 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900482 sdk_version: "none",
483 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900484 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900485 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000486 // TODO: remove //apex_available:platform
487 apex_available: [
488 "//apex_available:platform",
489 "myapex",
490 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900491 }
492
Jiyong Park77acec62020-06-01 21:39:15 +0900493 dex_import {
494 name: "myjar_dex",
495 jars: ["prebuilt.jar"],
496 apex_available: [
497 "//apex_available:platform",
498 "myapex",
499 ],
500 }
501
Jiyong Park7f7766d2019-07-25 22:02:35 +0900502 java_library {
503 name: "myotherjar",
504 srcs: ["foo/bar/MyClass.java"],
505 sdk_version: "none",
506 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900507 // TODO: remove //apex_available:platform
508 apex_available: [
509 "//apex_available:platform",
510 "myapex",
511 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900512 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900513
514 java_library {
515 name: "mysharedjar",
516 srcs: ["foo/bar/MyClass.java"],
517 sdk_version: "none",
518 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900519 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900520 `)
521
Paul Duffina71a67a2021-03-29 00:42:57 +0100522 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900523
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900524 // Make sure that Android.mk is created
525 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700526 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900527 var builder strings.Builder
528 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
529
530 androidMk := builder.String()
531 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
532 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
533
Jiyong Park42cca6c2019-04-01 11:15:50 +0900534 optFlags := apexRule.Args["opt_flags"]
535 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700536 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100537 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900538
Jiyong Park25fc6a92018-11-18 18:02:45 +0900539 copyCmds := apexRule.Args["copy_commands"]
540
541 // Ensure that main rule creates an output
542 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
543
544 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700545 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
546 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
547 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900548 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900549 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900550
551 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700552 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
553 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900554 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
555 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900556 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900557 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900558
559 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800560 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
561 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900562 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900563 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900564 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900565 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
566 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900567 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900568 // .. but not for java libs
569 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900570 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800571
Colin Cross7113d202019-11-20 16:39:12 -0800572 // Ensure that the platform variant ends with _shared or _common
573 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
574 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900575 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
576 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900577 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
578
579 // Ensure that dynamic dependency to java libs are not included
580 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800581
582 // Ensure that all symlinks are present.
583 found_foo_link_64 := false
584 found_foo := false
585 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900586 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800587 if strings.HasSuffix(cmd, "bin/foo") {
588 found_foo = true
589 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
590 found_foo_link_64 = true
591 }
592 }
593 }
594 good := found_foo && found_foo_link_64
595 if !good {
596 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
597 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900598
Artur Satayeva8bd1132020-04-27 18:07:06 +0100599 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100600 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100601 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
602 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
603 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100604
605 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100606 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100607 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
608 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
609 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800610}
611
Jooyung Hanf21c7972019-12-16 22:32:06 +0900612func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800613 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900614 apex_defaults {
615 name: "myapex-defaults",
616 key: "myapex.key",
617 prebuilts: ["myetc"],
618 native_shared_libs: ["mylib"],
619 java_libs: ["myjar"],
620 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900621 rros: ["rro"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800622 bpfs: ["bpf", "netd_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000623 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900624 }
625
626 prebuilt_etc {
627 name: "myetc",
628 src: "myprebuilt",
629 }
630
631 apex {
632 name: "myapex",
633 defaults: ["myapex-defaults"],
634 }
635
636 apex_key {
637 name: "myapex.key",
638 public_key: "testkey.avbpubkey",
639 private_key: "testkey.pem",
640 }
641
642 cc_library {
643 name: "mylib",
644 system_shared_libs: [],
645 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000646 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900647 }
648
649 java_library {
650 name: "myjar",
651 srcs: ["foo/bar/MyClass.java"],
652 sdk_version: "none",
653 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000654 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900655 }
656
657 android_app {
658 name: "AppFoo",
659 srcs: ["foo/bar/MyClass.java"],
660 sdk_version: "none",
661 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000662 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900663 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900664
665 runtime_resource_overlay {
666 name: "rro",
667 theme: "blue",
668 }
669
markchien2f59ec92020-09-02 16:23:38 +0800670 bpf {
671 name: "bpf",
672 srcs: ["bpf.c", "bpf2.c"],
673 }
674
Ken Chenfad7f9d2021-11-10 22:02:57 +0800675 bpf {
676 name: "netd_test",
677 srcs: ["netd_test.c"],
678 sub_dir: "netd",
679 }
680
Jooyung Hanf21c7972019-12-16 22:32:06 +0900681 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000682 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900683 "etc/myetc",
684 "javalib/myjar.jar",
685 "lib64/mylib.so",
Oriol Prieto Gascóa70425f2022-05-20 13:05:34 +0000686 "app/AppFoo@TEST.BUILD_ID/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900687 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800688 "etc/bpf/bpf.o",
689 "etc/bpf/bpf2.o",
Ken Chenfad7f9d2021-11-10 22:02:57 +0800690 "etc/bpf/netd/netd_test.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900691 })
692}
693
Jooyung Han01a3ee22019-11-02 02:52:25 +0900694func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800695 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900696 apex {
697 name: "myapex",
698 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000699 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900700 }
701
702 apex_key {
703 name: "myapex.key",
704 public_key: "testkey.avbpubkey",
705 private_key: "testkey.pem",
706 }
707 `)
708
709 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900710 args := module.Rule("apexRule").Args
711 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
712 t.Error("manifest should be apex_manifest.pb, but " + manifest)
713 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900714}
715
Liz Kammer4854a7d2021-05-27 14:28:27 -0400716func TestApexManifestMinSdkVersion(t *testing.T) {
717 ctx := testApex(t, `
718 apex_defaults {
719 name: "my_defaults",
720 key: "myapex.key",
721 product_specific: true,
722 file_contexts: ":my-file-contexts",
723 updatable: false,
724 }
725 apex {
726 name: "myapex_30",
727 min_sdk_version: "30",
728 defaults: ["my_defaults"],
729 }
730
731 apex {
732 name: "myapex_current",
733 min_sdk_version: "current",
734 defaults: ["my_defaults"],
735 }
736
737 apex {
738 name: "myapex_none",
739 defaults: ["my_defaults"],
740 }
741
742 apex_key {
743 name: "myapex.key",
744 public_key: "testkey.avbpubkey",
745 private_key: "testkey.pem",
746 }
747
748 filegroup {
749 name: "my-file-contexts",
750 srcs: ["product_specific_file_contexts"],
751 }
752 `, withFiles(map[string][]byte{
753 "product_specific_file_contexts": nil,
754 }), android.FixtureModifyProductVariables(
755 func(variables android.FixtureProductVariables) {
756 variables.Unbundled_build = proptools.BoolPtr(true)
757 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
758 }), android.FixtureMergeEnv(map[string]string{
759 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
760 }))
761
762 testCases := []struct {
763 module string
764 minSdkVersion string
765 }{
766 {
767 module: "myapex_30",
768 minSdkVersion: "30",
769 },
770 {
771 module: "myapex_current",
772 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
773 },
774 {
775 module: "myapex_none",
776 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
777 },
778 }
779 for _, tc := range testCases {
780 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module+"_image")
781 args := module.Rule("apexRule").Args
782 optFlags := args["opt_flags"]
783 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
784 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
785 }
786 }
787}
788
Alex Light5098a612018-11-29 17:12:15 -0800789func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800790 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800791 apex {
792 name: "myapex",
793 key: "myapex.key",
794 payload_type: "zip",
795 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000796 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800797 }
798
799 apex_key {
800 name: "myapex.key",
801 public_key: "testkey.avbpubkey",
802 private_key: "testkey.pem",
803 }
804
805 cc_library {
806 name: "mylib",
807 srcs: ["mylib.cpp"],
808 shared_libs: ["mylib2"],
809 system_shared_libs: [],
810 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000811 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800812 }
813
814 cc_library {
815 name: "mylib2",
816 srcs: ["mylib.cpp"],
817 system_shared_libs: [],
818 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000819 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800820 }
821 `)
822
Sundong Ahnabb64432019-10-22 13:58:29 +0900823 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800824 copyCmds := zipApexRule.Args["copy_commands"]
825
826 // Ensure that main rule creates an output
827 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
828
829 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700830 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800831
832 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700833 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800834
835 // Ensure that both direct and indirect deps are copied into apex
836 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
837 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900838}
839
840func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800841 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900842 apex {
843 name: "myapex",
844 key: "myapex.key",
845 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900846 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000847 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900848 }
849
850 apex_key {
851 name: "myapex.key",
852 public_key: "testkey.avbpubkey",
853 private_key: "testkey.pem",
854 }
855
856 cc_library {
857 name: "mylib",
858 srcs: ["mylib.cpp"],
859 shared_libs: ["mylib2", "mylib3"],
860 system_shared_libs: [],
861 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000862 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900863 }
864
865 cc_library {
866 name: "mylib2",
867 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900868 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900869 system_shared_libs: [],
870 stl: "none",
871 stubs: {
872 versions: ["1", "2", "3"],
873 },
874 }
875
876 cc_library {
877 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900878 srcs: ["mylib.cpp"],
879 shared_libs: ["mylib4"],
880 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900881 stl: "none",
882 stubs: {
883 versions: ["10", "11", "12"],
884 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000885 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900886 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900887
888 cc_library {
889 name: "mylib4",
890 srcs: ["mylib.cpp"],
891 system_shared_libs: [],
892 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000893 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900894 }
Jiyong Park105dc322021-06-11 17:22:09 +0900895
896 rust_binary {
897 name: "foo.rust",
898 srcs: ["foo.rs"],
899 shared_libs: ["libfoo.shared_from_rust"],
900 prefer_rlib: true,
901 apex_available: ["myapex"],
902 }
903
904 cc_library_shared {
905 name: "libfoo.shared_from_rust",
906 srcs: ["mylib.cpp"],
907 system_shared_libs: [],
908 stl: "none",
909 stubs: {
910 versions: ["10", "11", "12"],
911 },
912 }
913
Jiyong Park25fc6a92018-11-18 18:02:45 +0900914 `)
915
Sundong Ahnabb64432019-10-22 13:58:29 +0900916 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900917 copyCmds := apexRule.Args["copy_commands"]
918
919 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800920 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900921
922 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800923 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900924
925 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800926 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900927
Colin Crossaede88c2020-08-11 12:17:01 -0700928 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900929
930 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900931 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900932 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900933 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900934
935 // 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 -0700936 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900937 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700938 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900939
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700940 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
941 // is replaced by sharing of "cFlags" in cc/builder.go.
942 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
943 // module variable representing "cflags". So it was not detected by ensureNotContains.
944 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
945 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
946 // including the original cflags's "-include mylib.h".
947 //
Jiyong Park64379952018-12-13 18:37:29 +0900948 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700949 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
950 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900951
952 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700953 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900954
Jooyung Hana57af4a2020-01-23 05:36:59 +0000955 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900956 "lib64/mylib.so",
957 "lib64/mylib3.so",
958 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +0900959 "bin/foo.rust",
960 "lib64/libc++.so", // by the implicit dependency from foo.rust
961 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +0900962 })
Jiyong Park105dc322021-06-11 17:22:09 +0900963
964 // Ensure that stub dependency from a rust module is not included
965 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
966 // The rust module is linked to the stub cc library
967 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
968 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
969 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +0900970
971 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
972 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900973}
974
Jiyong Park1bc84122021-06-22 20:23:05 +0900975func TestApexCanUsePrivateApis(t *testing.T) {
976 ctx := testApex(t, `
977 apex {
978 name: "myapex",
979 key: "myapex.key",
980 native_shared_libs: ["mylib"],
981 binaries: ["foo.rust"],
982 updatable: false,
983 platform_apis: true,
984 }
985
986 apex_key {
987 name: "myapex.key",
988 public_key: "testkey.avbpubkey",
989 private_key: "testkey.pem",
990 }
991
992 cc_library {
993 name: "mylib",
994 srcs: ["mylib.cpp"],
995 shared_libs: ["mylib2"],
996 system_shared_libs: [],
997 stl: "none",
998 apex_available: [ "myapex" ],
999 }
1000
1001 cc_library {
1002 name: "mylib2",
1003 srcs: ["mylib.cpp"],
1004 cflags: ["-include mylib.h"],
1005 system_shared_libs: [],
1006 stl: "none",
1007 stubs: {
1008 versions: ["1", "2", "3"],
1009 },
1010 }
1011
1012 rust_binary {
1013 name: "foo.rust",
1014 srcs: ["foo.rs"],
1015 shared_libs: ["libfoo.shared_from_rust"],
1016 prefer_rlib: true,
1017 apex_available: ["myapex"],
1018 }
1019
1020 cc_library_shared {
1021 name: "libfoo.shared_from_rust",
1022 srcs: ["mylib.cpp"],
1023 system_shared_libs: [],
1024 stl: "none",
1025 stubs: {
1026 versions: ["10", "11", "12"],
1027 },
1028 }
1029 `)
1030
1031 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1032 copyCmds := apexRule.Args["copy_commands"]
1033
1034 // Ensure that indirect stubs dep is not included
1035 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1036 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1037
1038 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1039 // of the platform_apis: true)
Jiyong Parkd4a00632022-04-12 12:23:20 +09001040 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001041 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1042 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Parkd4a00632022-04-12 12:23:20 +09001043 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001044 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1045 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1046}
1047
Colin Cross7812fd32020-09-25 12:35:10 -07001048func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1049 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001050 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001051 apex {
1052 name: "myapex",
1053 key: "myapex.key",
1054 native_shared_libs: ["mylib", "mylib3"],
1055 min_sdk_version: "29",
1056 }
1057
1058 apex_key {
1059 name: "myapex.key",
1060 public_key: "testkey.avbpubkey",
1061 private_key: "testkey.pem",
1062 }
1063
1064 cc_library {
1065 name: "mylib",
1066 srcs: ["mylib.cpp"],
1067 shared_libs: ["mylib2", "mylib3"],
1068 system_shared_libs: [],
1069 stl: "none",
1070 apex_available: [ "myapex" ],
1071 min_sdk_version: "28",
1072 }
1073
1074 cc_library {
1075 name: "mylib2",
1076 srcs: ["mylib.cpp"],
1077 cflags: ["-include mylib.h"],
1078 system_shared_libs: [],
1079 stl: "none",
1080 stubs: {
1081 versions: ["28", "29", "30", "current"],
1082 },
1083 min_sdk_version: "28",
1084 }
1085
1086 cc_library {
1087 name: "mylib3",
1088 srcs: ["mylib.cpp"],
1089 shared_libs: ["mylib4"],
1090 system_shared_libs: [],
1091 stl: "none",
1092 stubs: {
1093 versions: ["28", "29", "30", "current"],
1094 },
1095 apex_available: [ "myapex" ],
1096 min_sdk_version: "28",
1097 }
1098
1099 cc_library {
1100 name: "mylib4",
1101 srcs: ["mylib.cpp"],
1102 system_shared_libs: [],
1103 stl: "none",
1104 apex_available: [ "myapex" ],
1105 min_sdk_version: "28",
1106 }
1107 `)
1108
1109 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1110 copyCmds := apexRule.Args["copy_commands"]
1111
1112 // Ensure that direct non-stubs dep is always included
1113 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1114
1115 // Ensure that indirect stubs dep is not included
1116 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1117
1118 // Ensure that direct stubs dep is included
1119 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1120
1121 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1122
Jiyong Park55549df2021-02-26 23:57:23 +09001123 // Ensure that mylib is linking with the latest version of stub for mylib2
1124 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001125 // ... and not linking to the non-stub (impl) variant of mylib2
1126 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1127
1128 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1129 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1130 // .. and not linking to the stubs variant of mylib3
1131 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1132
1133 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001134 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001135 ensureNotContains(t, mylib2Cflags, "-include ")
1136
1137 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -07001138 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -07001139
1140 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1141 "lib64/mylib.so",
1142 "lib64/mylib3.so",
1143 "lib64/mylib4.so",
1144 })
1145}
1146
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001147func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1148 t.Parallel()
1149 // myapex (Z)
1150 // mylib -----------------.
1151 // |
1152 // otherapex (29) |
1153 // libstub's versions: 29 Z current
1154 // |
1155 // <platform> |
1156 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001157 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001158 apex {
1159 name: "myapex",
1160 key: "myapex.key",
1161 native_shared_libs: ["mylib"],
1162 min_sdk_version: "Z", // non-final
1163 }
1164
1165 cc_library {
1166 name: "mylib",
1167 srcs: ["mylib.cpp"],
1168 shared_libs: ["libstub"],
1169 apex_available: ["myapex"],
1170 min_sdk_version: "Z",
1171 }
1172
1173 apex_key {
1174 name: "myapex.key",
1175 public_key: "testkey.avbpubkey",
1176 private_key: "testkey.pem",
1177 }
1178
1179 apex {
1180 name: "otherapex",
1181 key: "myapex.key",
1182 native_shared_libs: ["libstub"],
1183 min_sdk_version: "29",
1184 }
1185
1186 cc_library {
1187 name: "libstub",
1188 srcs: ["mylib.cpp"],
1189 stubs: {
1190 versions: ["29", "Z", "current"],
1191 },
1192 apex_available: ["otherapex"],
1193 min_sdk_version: "29",
1194 }
1195
1196 // platform module depending on libstub from otherapex should use the latest stub("current")
1197 cc_library {
1198 name: "libplatform",
1199 srcs: ["mylib.cpp"],
1200 shared_libs: ["libstub"],
1201 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001202 `,
1203 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1204 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1205 variables.Platform_sdk_final = proptools.BoolPtr(false)
1206 variables.Platform_version_active_codenames = []string{"Z"}
1207 }),
1208 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001209
Jiyong Park55549df2021-02-26 23:57:23 +09001210 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001211 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001212 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001213 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001214 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001215
1216 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1217 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1218 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1219 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1220 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1221}
1222
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001223func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001224 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001225 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001226 name: "myapex2",
1227 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001228 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001229 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001230 }
1231
1232 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001233 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001234 public_key: "testkey.avbpubkey",
1235 private_key: "testkey.pem",
1236 }
1237
1238 cc_library {
1239 name: "mylib",
1240 srcs: ["mylib.cpp"],
1241 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001242 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001243 system_shared_libs: [],
1244 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001245 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001246 }
1247
1248 cc_library {
1249 name: "libfoo",
1250 srcs: ["mylib.cpp"],
1251 shared_libs: ["libbar"],
1252 system_shared_libs: [],
1253 stl: "none",
1254 stubs: {
1255 versions: ["10", "20", "30"],
1256 },
1257 }
1258
1259 cc_library {
1260 name: "libbar",
1261 srcs: ["mylib.cpp"],
1262 system_shared_libs: [],
1263 stl: "none",
1264 }
1265
Jiyong Park678c8812020-02-07 17:25:49 +09001266 cc_library_static {
1267 name: "libbaz",
1268 srcs: ["mylib.cpp"],
1269 system_shared_libs: [],
1270 stl: "none",
1271 apex_available: [ "myapex2" ],
1272 }
1273
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001274 `)
1275
Jiyong Park83dc74b2020-01-14 18:38:44 +09001276 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001277 copyCmds := apexRule.Args["copy_commands"]
1278
1279 // Ensure that direct non-stubs dep is always included
1280 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1281
1282 // Ensure that indirect stubs dep is not included
1283 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1284
1285 // Ensure that dependency of stubs is not included
1286 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1287
Colin Crossaede88c2020-08-11 12:17:01 -07001288 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001289
1290 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001291 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001292 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001293 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001294
Jiyong Park3ff16992019-12-27 14:11:47 +09001295 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001296
1297 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1298 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001299
Artur Satayeva8bd1132020-04-27 18:07:06 +01001300 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001301 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001302
Artur Satayeva8bd1132020-04-27 18:07:06 +01001303 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001304 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001305}
1306
Jooyung Hand3639552019-08-09 12:57:43 +09001307func TestApexWithRuntimeLibsDependency(t *testing.T) {
1308 /*
1309 myapex
1310 |
1311 v (runtime_libs)
1312 mylib ------+------> libfoo [provides stub]
1313 |
1314 `------> libbar
1315 */
Colin Cross1c460562021-02-16 17:55:47 -08001316 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001317 apex {
1318 name: "myapex",
1319 key: "myapex.key",
1320 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001321 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001322 }
1323
1324 apex_key {
1325 name: "myapex.key",
1326 public_key: "testkey.avbpubkey",
1327 private_key: "testkey.pem",
1328 }
1329
1330 cc_library {
1331 name: "mylib",
1332 srcs: ["mylib.cpp"],
1333 runtime_libs: ["libfoo", "libbar"],
1334 system_shared_libs: [],
1335 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001336 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001337 }
1338
1339 cc_library {
1340 name: "libfoo",
1341 srcs: ["mylib.cpp"],
1342 system_shared_libs: [],
1343 stl: "none",
1344 stubs: {
1345 versions: ["10", "20", "30"],
1346 },
1347 }
1348
1349 cc_library {
1350 name: "libbar",
1351 srcs: ["mylib.cpp"],
1352 system_shared_libs: [],
1353 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001354 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001355 }
1356
1357 `)
1358
Sundong Ahnabb64432019-10-22 13:58:29 +09001359 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001360 copyCmds := apexRule.Args["copy_commands"]
1361
1362 // Ensure that direct non-stubs dep is always included
1363 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1364
1365 // Ensure that indirect stubs dep is not included
1366 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1367
1368 // Ensure that runtime_libs dep in included
1369 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1370
Sundong Ahnabb64432019-10-22 13:58:29 +09001371 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001372 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1373 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001374
1375}
1376
Paul Duffina02cae32021-03-09 01:44:06 +00001377var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1378 cc.PrepareForTestWithCcBuildComponents,
1379 PrepareForTestWithApexBuildComponents,
1380 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001381 apex {
1382 name: "com.android.runtime",
1383 key: "com.android.runtime.key",
1384 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001385 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001386 }
1387
1388 apex_key {
1389 name: "com.android.runtime.key",
1390 public_key: "testkey.avbpubkey",
1391 private_key: "testkey.pem",
1392 }
Paul Duffina02cae32021-03-09 01:44:06 +00001393 `),
1394 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1395)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001396
Paul Duffina02cae32021-03-09 01:44:06 +00001397func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001398 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001399 cc_library {
1400 name: "libc",
1401 no_libcrt: true,
1402 nocrt: true,
1403 stl: "none",
1404 system_shared_libs: [],
1405 stubs: { versions: ["1"] },
1406 apex_available: ["com.android.runtime"],
1407
1408 sanitize: {
1409 hwaddress: true,
1410 }
1411 }
1412
1413 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001414 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001415 no_libcrt: true,
1416 nocrt: true,
1417 stl: "none",
1418 system_shared_libs: [],
1419 srcs: [""],
1420 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001421 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001422
1423 sanitize: {
1424 never: true,
1425 },
Paul Duffina02cae32021-03-09 01:44:06 +00001426 } `)
1427 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001428
1429 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1430 "lib64/bionic/libc.so",
1431 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1432 })
1433
Colin Cross4c4c1be2022-02-10 11:41:18 -08001434 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001435
1436 installed := hwasan.Description("install libclang_rt.hwasan")
1437 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1438
1439 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1440 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1441 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1442}
1443
1444func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001445 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001446 prepareForTestOfRuntimeApexWithHwasan,
1447 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1448 variables.SanitizeDevice = []string{"hwaddress"}
1449 }),
1450 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001451 cc_library {
1452 name: "libc",
1453 no_libcrt: true,
1454 nocrt: true,
1455 stl: "none",
1456 system_shared_libs: [],
1457 stubs: { versions: ["1"] },
1458 apex_available: ["com.android.runtime"],
1459 }
1460
1461 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001462 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001463 no_libcrt: true,
1464 nocrt: true,
1465 stl: "none",
1466 system_shared_libs: [],
1467 srcs: [""],
1468 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001469 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001470
1471 sanitize: {
1472 never: true,
1473 },
1474 }
Paul Duffina02cae32021-03-09 01:44:06 +00001475 `)
1476 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001477
1478 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1479 "lib64/bionic/libc.so",
1480 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1481 })
1482
Colin Cross4c4c1be2022-02-10 11:41:18 -08001483 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001484
1485 installed := hwasan.Description("install libclang_rt.hwasan")
1486 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1487
1488 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1489 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1490 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1491}
1492
Jooyung Han61b66e92020-03-21 14:21:46 +00001493func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1494 testcases := []struct {
1495 name string
1496 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001497 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001498 shouldLink string
1499 shouldNotLink []string
1500 }{
1501 {
Jiyong Park55549df2021-02-26 23:57:23 +09001502 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001503 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001504 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001505 shouldLink: "current",
1506 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001507 },
1508 {
Jiyong Park55549df2021-02-26 23:57:23 +09001509 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001510 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001511 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001512 shouldLink: "current",
1513 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001514 },
1515 }
1516 for _, tc := range testcases {
1517 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001518 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001519 apex {
1520 name: "myapex",
1521 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001522 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001523 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001524 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001525 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001526
Jooyung Han61b66e92020-03-21 14:21:46 +00001527 apex_key {
1528 name: "myapex.key",
1529 public_key: "testkey.avbpubkey",
1530 private_key: "testkey.pem",
1531 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001532
Jooyung Han61b66e92020-03-21 14:21:46 +00001533 cc_library {
1534 name: "mylib",
1535 srcs: ["mylib.cpp"],
1536 vendor_available: true,
1537 shared_libs: ["libbar"],
1538 system_shared_libs: [],
1539 stl: "none",
1540 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001541 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001542 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001543
Jooyung Han61b66e92020-03-21 14:21:46 +00001544 cc_library {
1545 name: "libbar",
1546 srcs: ["mylib.cpp"],
1547 system_shared_libs: [],
1548 stl: "none",
1549 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001550 llndk: {
1551 symbol_file: "libbar.map.txt",
1552 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001553 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001554 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001555 withUnbundledBuild,
1556 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001557
Jooyung Han61b66e92020-03-21 14:21:46 +00001558 // Ensure that LLNDK dep is not included
1559 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1560 "lib64/mylib.so",
1561 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001562
Jooyung Han61b66e92020-03-21 14:21:46 +00001563 // Ensure that LLNDK dep is required
1564 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1565 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1566 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001567
Steven Moreland2c4000c2021-04-27 02:08:49 +00001568 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1569 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001570 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001571 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001572 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001573
Steven Moreland2c4000c2021-04-27 02:08:49 +00001574 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001575 ver := tc.shouldLink
1576 if tc.shouldLink == "current" {
1577 ver = strconv.Itoa(android.FutureApiLevelInt)
1578 }
1579 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001580 })
1581 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001582}
1583
Jiyong Park25fc6a92018-11-18 18:02:45 +09001584func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001585 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001586 apex {
1587 name: "myapex",
1588 key: "myapex.key",
1589 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001590 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001591 }
1592
1593 apex_key {
1594 name: "myapex.key",
1595 public_key: "testkey.avbpubkey",
1596 private_key: "testkey.pem",
1597 }
1598
1599 cc_library {
1600 name: "mylib",
1601 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001602 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001603 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_shared {
1609 name: "mylib_shared",
1610 srcs: ["mylib.cpp"],
1611 shared_libs: ["libdl#27"],
1612 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001613 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001614 }
1615
1616 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001617 name: "libBootstrap",
1618 srcs: ["mylib.cpp"],
1619 stl: "none",
1620 bootstrap: true,
1621 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001622 `)
1623
Sundong Ahnabb64432019-10-22 13:58:29 +09001624 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001625 copyCmds := apexRule.Args["copy_commands"]
1626
1627 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001628 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001629 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1630 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001631
1632 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001633 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001634
Colin Crossaede88c2020-08-11 12:17:01 -07001635 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1636 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1637 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001638
1639 // For dependency to libc
1640 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001641 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001642 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001643 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001644 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001645 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1646 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001647
1648 // For dependency to libm
1649 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001650 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001651 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001652 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001653 // ... and is not compiling with the stub
1654 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1655 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1656
1657 // For dependency to libdl
1658 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001659 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001660 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001661 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1662 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001663 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001664 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001665 // ... Cflags from stub is correctly exported to mylib
1666 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1667 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001668
1669 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001670 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1671 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1672 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1673 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001674}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001675
Jooyung Han749dc692020-04-15 11:03:39 +09001676func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001677 // there are three links between liba --> libz.
1678 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001679 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001680 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001681 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001682 apex {
1683 name: "myapex",
1684 key: "myapex.key",
1685 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001686 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001687 }
1688
1689 apex {
1690 name: "otherapex",
1691 key: "myapex.key",
1692 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001693 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001694 }
1695
1696 apex_key {
1697 name: "myapex.key",
1698 public_key: "testkey.avbpubkey",
1699 private_key: "testkey.pem",
1700 }
1701
1702 cc_library {
1703 name: "libx",
1704 shared_libs: ["liba"],
1705 system_shared_libs: [],
1706 stl: "none",
1707 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001708 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001709 }
1710
1711 cc_library {
1712 name: "liby",
1713 shared_libs: ["liba"],
1714 system_shared_libs: [],
1715 stl: "none",
1716 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001717 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001718 }
1719
1720 cc_library {
1721 name: "liba",
1722 shared_libs: ["libz"],
1723 system_shared_libs: [],
1724 stl: "none",
1725 apex_available: [
1726 "//apex_available:anyapex",
1727 "//apex_available:platform",
1728 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001729 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001730 }
1731
1732 cc_library {
1733 name: "libz",
1734 system_shared_libs: [],
1735 stl: "none",
1736 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001737 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001738 },
1739 }
Jooyung Han749dc692020-04-15 11:03:39 +09001740 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001741
1742 expectLink := func(from, from_variant, to, to_variant string) {
1743 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1744 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1745 }
1746 expectNoLink := func(from, from_variant, to, to_variant string) {
1747 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1748 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1749 }
1750 // platform liba is linked to non-stub version
1751 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001752 // liba in myapex is linked to current
1753 expectLink("liba", "shared_apex29", "libz", "shared_current")
1754 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001755 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001756 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001757 // liba in otherapex is linked to current
1758 expectLink("liba", "shared_apex30", "libz", "shared_current")
1759 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001760 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1761 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001762}
1763
Jooyung Hanaed150d2020-04-02 01:41:41 +09001764func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001765 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001766 apex {
1767 name: "myapex",
1768 key: "myapex.key",
1769 native_shared_libs: ["libx"],
1770 min_sdk_version: "R",
1771 }
1772
1773 apex_key {
1774 name: "myapex.key",
1775 public_key: "testkey.avbpubkey",
1776 private_key: "testkey.pem",
1777 }
1778
1779 cc_library {
1780 name: "libx",
1781 shared_libs: ["libz"],
1782 system_shared_libs: [],
1783 stl: "none",
1784 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001785 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001786 }
1787
1788 cc_library {
1789 name: "libz",
1790 system_shared_libs: [],
1791 stl: "none",
1792 stubs: {
1793 versions: ["29", "R"],
1794 },
1795 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001796 `,
1797 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1798 variables.Platform_version_active_codenames = []string{"R"}
1799 }),
1800 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001801
1802 expectLink := func(from, from_variant, to, to_variant string) {
1803 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1804 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1805 }
1806 expectNoLink := func(from, from_variant, to, to_variant string) {
1807 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1808 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1809 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001810 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1811 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001812 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1813 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001814}
1815
Jooyung Han4c4da062021-06-23 10:23:16 +09001816func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1817 testApex(t, `
1818 apex {
1819 name: "myapex",
1820 key: "myapex.key",
1821 java_libs: ["libx"],
1822 min_sdk_version: "S",
1823 }
1824
1825 apex_key {
1826 name: "myapex.key",
1827 public_key: "testkey.avbpubkey",
1828 private_key: "testkey.pem",
1829 }
1830
1831 java_library {
1832 name: "libx",
1833 srcs: ["a.java"],
1834 apex_available: [ "myapex" ],
1835 sdk_version: "current",
1836 min_sdk_version: "S", // should be okay
1837 }
1838 `,
1839 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1840 variables.Platform_version_active_codenames = []string{"S"}
1841 variables.Platform_sdk_codename = proptools.StringPtr("S")
1842 }),
1843 )
1844}
1845
Jooyung Han749dc692020-04-15 11:03:39 +09001846func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001847 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001848 apex {
1849 name: "myapex",
1850 key: "myapex.key",
1851 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001852 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001853 }
1854
1855 apex_key {
1856 name: "myapex.key",
1857 public_key: "testkey.avbpubkey",
1858 private_key: "testkey.pem",
1859 }
1860
1861 cc_library {
1862 name: "libx",
1863 shared_libs: ["libz"],
1864 system_shared_libs: [],
1865 stl: "none",
1866 apex_available: [ "myapex" ],
1867 }
1868
1869 cc_library {
1870 name: "libz",
1871 system_shared_libs: [],
1872 stl: "none",
1873 stubs: {
1874 versions: ["1", "2"],
1875 },
1876 }
1877 `)
1878
1879 expectLink := func(from, from_variant, to, to_variant string) {
1880 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1881 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1882 }
1883 expectNoLink := func(from, from_variant, to, to_variant string) {
1884 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1885 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1886 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001887 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001888 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001889 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001890 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001891}
1892
Jiyong Park5df7bd32021-08-25 16:18:46 +09001893func TestApexMinSdkVersion_crtobjectInVendorApex(t *testing.T) {
1894 ctx := testApex(t, `
1895 apex {
1896 name: "myapex",
1897 key: "myapex.key",
1898 native_shared_libs: ["mylib"],
1899 updatable: false,
1900 vendor: true,
1901 min_sdk_version: "29",
1902 }
1903
1904 apex_key {
1905 name: "myapex.key",
1906 public_key: "testkey.avbpubkey",
1907 private_key: "testkey.pem",
1908 }
1909
1910 cc_library {
1911 name: "mylib",
1912 vendor_available: true,
1913 system_shared_libs: [],
1914 stl: "none",
1915 apex_available: [ "myapex" ],
1916 min_sdk_version: "29",
1917 }
1918 `)
1919
1920 vendorVariant := "android_vendor.29_arm64_armv8-a"
1921
1922 // First check that the correct variant of crtbegin_so is used.
1923 ldRule := ctx.ModuleForTests("mylib", vendorVariant+"_shared_apex29").Rule("ld")
1924 crtBegin := names(ldRule.Args["crtBegin"])
1925 ensureListContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
1926
1927 // Ensure that the crtbegin_so used by the APEX is targeting 29
1928 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
1929 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
1930}
1931
Jooyung Han03b51852020-02-26 22:45:42 +09001932func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001933 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001934 apex {
1935 name: "myapex",
1936 key: "myapex.key",
1937 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001938 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001939 }
1940
1941 apex_key {
1942 name: "myapex.key",
1943 public_key: "testkey.avbpubkey",
1944 private_key: "testkey.pem",
1945 }
1946
1947 cc_library {
1948 name: "libx",
1949 system_shared_libs: [],
1950 stl: "none",
1951 apex_available: [ "myapex" ],
1952 stubs: {
1953 versions: ["1", "2"],
1954 },
1955 }
1956
1957 cc_library {
1958 name: "libz",
1959 shared_libs: ["libx"],
1960 system_shared_libs: [],
1961 stl: "none",
1962 }
1963 `)
1964
1965 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001966 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001967 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1968 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1969 }
1970 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001971 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001972 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1973 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1974 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001975 expectLink("libz", "shared", "libx", "shared_current")
1976 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09001977 expectNoLink("libz", "shared", "libz", "shared_1")
1978 expectNoLink("libz", "shared", "libz", "shared")
1979}
1980
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001981var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1982 func(variables android.FixtureProductVariables) {
1983 variables.SanitizeDevice = []string{"hwaddress"}
1984 },
1985)
1986
Jooyung Han75568392020-03-20 04:29:24 +09001987func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001988 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001989 apex {
1990 name: "myapex",
1991 key: "myapex.key",
1992 native_shared_libs: ["libx"],
1993 min_sdk_version: "29",
1994 }
1995
1996 apex_key {
1997 name: "myapex.key",
1998 public_key: "testkey.avbpubkey",
1999 private_key: "testkey.pem",
2000 }
2001
2002 cc_library {
2003 name: "libx",
2004 shared_libs: ["libbar"],
2005 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002006 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002007 }
2008
2009 cc_library {
2010 name: "libbar",
2011 stubs: {
2012 versions: ["29", "30"],
2013 },
2014 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002015 `,
2016 prepareForTestWithSantitizeHwaddress,
2017 )
Jooyung Han03b51852020-02-26 22:45:42 +09002018 expectLink := func(from, from_variant, to, to_variant string) {
2019 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2020 libFlags := ld.Args["libFlags"]
2021 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2022 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002023 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002024}
2025
Jooyung Han75568392020-03-20 04:29:24 +09002026func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002027 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002028 apex {
2029 name: "myapex",
2030 key: "myapex.key",
2031 native_shared_libs: ["libx"],
2032 min_sdk_version: "29",
2033 }
2034
2035 apex_key {
2036 name: "myapex.key",
2037 public_key: "testkey.avbpubkey",
2038 private_key: "testkey.pem",
2039 }
2040
2041 cc_library {
2042 name: "libx",
2043 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002044 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002045 }
Jooyung Han75568392020-03-20 04:29:24 +09002046 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002047
2048 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002049 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002050 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002051 // note that platform variant is not.
2052 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002053 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002054}
2055
Jooyung Han749dc692020-04-15 11:03:39 +09002056func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2057 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002058 apex {
2059 name: "myapex",
2060 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002061 native_shared_libs: ["mylib"],
2062 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002063 }
2064
2065 apex_key {
2066 name: "myapex.key",
2067 public_key: "testkey.avbpubkey",
2068 private_key: "testkey.pem",
2069 }
Jooyung Han749dc692020-04-15 11:03:39 +09002070
2071 cc_library {
2072 name: "mylib",
2073 srcs: ["mylib.cpp"],
2074 system_shared_libs: [],
2075 stl: "none",
2076 apex_available: [
2077 "myapex",
2078 ],
2079 min_sdk_version: "30",
2080 }
2081 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002082
2083 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2084 apex {
2085 name: "myapex",
2086 key: "myapex.key",
2087 native_shared_libs: ["libfoo.ffi"],
2088 min_sdk_version: "29",
2089 }
2090
2091 apex_key {
2092 name: "myapex.key",
2093 public_key: "testkey.avbpubkey",
2094 private_key: "testkey.pem",
2095 }
2096
2097 rust_ffi_shared {
2098 name: "libfoo.ffi",
2099 srcs: ["foo.rs"],
2100 crate_name: "foo",
2101 apex_available: [
2102 "myapex",
2103 ],
2104 min_sdk_version: "30",
2105 }
2106 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002107
2108 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2109 apex {
2110 name: "myapex",
2111 key: "myapex.key",
2112 java_libs: ["libfoo"],
2113 min_sdk_version: "29",
2114 }
2115
2116 apex_key {
2117 name: "myapex.key",
2118 public_key: "testkey.avbpubkey",
2119 private_key: "testkey.pem",
2120 }
2121
2122 java_import {
2123 name: "libfoo",
2124 jars: ["libfoo.jar"],
2125 apex_available: [
2126 "myapex",
2127 ],
2128 min_sdk_version: "30",
2129 }
2130 `)
Jooyung Han749dc692020-04-15 11:03:39 +09002131}
2132
2133func TestApexMinSdkVersion_Okay(t *testing.T) {
2134 testApex(t, `
2135 apex {
2136 name: "myapex",
2137 key: "myapex.key",
2138 native_shared_libs: ["libfoo"],
2139 java_libs: ["libbar"],
2140 min_sdk_version: "29",
2141 }
2142
2143 apex_key {
2144 name: "myapex.key",
2145 public_key: "testkey.avbpubkey",
2146 private_key: "testkey.pem",
2147 }
2148
2149 cc_library {
2150 name: "libfoo",
2151 srcs: ["mylib.cpp"],
2152 shared_libs: ["libfoo_dep"],
2153 apex_available: ["myapex"],
2154 min_sdk_version: "29",
2155 }
2156
2157 cc_library {
2158 name: "libfoo_dep",
2159 srcs: ["mylib.cpp"],
2160 apex_available: ["myapex"],
2161 min_sdk_version: "29",
2162 }
2163
2164 java_library {
2165 name: "libbar",
2166 sdk_version: "current",
2167 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002168 static_libs: [
2169 "libbar_dep",
2170 "libbar_import_dep",
2171 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002172 apex_available: ["myapex"],
2173 min_sdk_version: "29",
2174 }
2175
2176 java_library {
2177 name: "libbar_dep",
2178 sdk_version: "current",
2179 srcs: ["a.java"],
2180 apex_available: ["myapex"],
2181 min_sdk_version: "29",
2182 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002183
2184 java_import {
2185 name: "libbar_import_dep",
2186 jars: ["libbar.jar"],
2187 apex_available: ["myapex"],
2188 min_sdk_version: "29",
2189 }
Jooyung Han03b51852020-02-26 22:45:42 +09002190 `)
2191}
2192
Artur Satayev8cf899a2020-04-15 17:29:42 +01002193func TestJavaStableSdkVersion(t *testing.T) {
2194 testCases := []struct {
2195 name string
2196 expectedError string
2197 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002198 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002199 }{
2200 {
2201 name: "Non-updatable apex with non-stable dep",
2202 bp: `
2203 apex {
2204 name: "myapex",
2205 java_libs: ["myjar"],
2206 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002207 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002208 }
2209 apex_key {
2210 name: "myapex.key",
2211 public_key: "testkey.avbpubkey",
2212 private_key: "testkey.pem",
2213 }
2214 java_library {
2215 name: "myjar",
2216 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002217 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002218 apex_available: ["myapex"],
2219 }
2220 `,
2221 },
2222 {
2223 name: "Updatable apex with stable dep",
2224 bp: `
2225 apex {
2226 name: "myapex",
2227 java_libs: ["myjar"],
2228 key: "myapex.key",
2229 updatable: true,
2230 min_sdk_version: "29",
2231 }
2232 apex_key {
2233 name: "myapex.key",
2234 public_key: "testkey.avbpubkey",
2235 private_key: "testkey.pem",
2236 }
2237 java_library {
2238 name: "myjar",
2239 srcs: ["foo/bar/MyClass.java"],
2240 sdk_version: "current",
2241 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002242 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002243 }
2244 `,
2245 },
2246 {
2247 name: "Updatable apex with non-stable dep",
2248 expectedError: "cannot depend on \"myjar\"",
2249 bp: `
2250 apex {
2251 name: "myapex",
2252 java_libs: ["myjar"],
2253 key: "myapex.key",
2254 updatable: true,
2255 }
2256 apex_key {
2257 name: "myapex.key",
2258 public_key: "testkey.avbpubkey",
2259 private_key: "testkey.pem",
2260 }
2261 java_library {
2262 name: "myjar",
2263 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002264 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002265 apex_available: ["myapex"],
2266 }
2267 `,
2268 },
2269 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002270 name: "Updatable apex with non-stable legacy core platform dep",
2271 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2272 bp: `
2273 apex {
2274 name: "myapex",
2275 java_libs: ["myjar-uses-legacy"],
2276 key: "myapex.key",
2277 updatable: true,
2278 }
2279 apex_key {
2280 name: "myapex.key",
2281 public_key: "testkey.avbpubkey",
2282 private_key: "testkey.pem",
2283 }
2284 java_library {
2285 name: "myjar-uses-legacy",
2286 srcs: ["foo/bar/MyClass.java"],
2287 sdk_version: "core_platform",
2288 apex_available: ["myapex"],
2289 }
2290 `,
2291 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2292 },
2293 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002294 name: "Updatable apex with non-stable transitive dep",
2295 // This is not actually detecting that the transitive dependency is unstable, rather it is
2296 // detecting that the transitive dependency is building against a wider API surface than the
2297 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002298 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002299 bp: `
2300 apex {
2301 name: "myapex",
2302 java_libs: ["myjar"],
2303 key: "myapex.key",
2304 updatable: true,
2305 }
2306 apex_key {
2307 name: "myapex.key",
2308 public_key: "testkey.avbpubkey",
2309 private_key: "testkey.pem",
2310 }
2311 java_library {
2312 name: "myjar",
2313 srcs: ["foo/bar/MyClass.java"],
2314 sdk_version: "current",
2315 apex_available: ["myapex"],
2316 static_libs: ["transitive-jar"],
2317 }
2318 java_library {
2319 name: "transitive-jar",
2320 srcs: ["foo/bar/MyClass.java"],
2321 sdk_version: "core_platform",
2322 apex_available: ["myapex"],
2323 }
2324 `,
2325 },
2326 }
2327
2328 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002329 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2330 continue
2331 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002332 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002333 errorHandler := android.FixtureExpectsNoErrors
2334 if test.expectedError != "" {
2335 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002336 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002337 android.GroupFixturePreparers(
2338 java.PrepareForTestWithJavaDefaultModules,
2339 PrepareForTestWithApexBuildComponents,
2340 prepareForTestWithMyapex,
2341 android.OptionalFixturePreparer(test.preparer),
2342 ).
2343 ExtendWithErrorHandler(errorHandler).
2344 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002345 })
2346 }
2347}
2348
Jooyung Han749dc692020-04-15 11:03:39 +09002349func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2350 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2351 apex {
2352 name: "myapex",
2353 key: "myapex.key",
2354 native_shared_libs: ["mylib"],
2355 min_sdk_version: "29",
2356 }
2357
2358 apex_key {
2359 name: "myapex.key",
2360 public_key: "testkey.avbpubkey",
2361 private_key: "testkey.pem",
2362 }
2363
2364 cc_library {
2365 name: "mylib",
2366 srcs: ["mylib.cpp"],
2367 shared_libs: ["mylib2"],
2368 system_shared_libs: [],
2369 stl: "none",
2370 apex_available: [
2371 "myapex",
2372 ],
2373 min_sdk_version: "29",
2374 }
2375
2376 // indirect part of the apex
2377 cc_library {
2378 name: "mylib2",
2379 srcs: ["mylib.cpp"],
2380 system_shared_libs: [],
2381 stl: "none",
2382 apex_available: [
2383 "myapex",
2384 ],
2385 min_sdk_version: "30",
2386 }
2387 `)
2388}
2389
2390func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2391 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2392 apex {
2393 name: "myapex",
2394 key: "myapex.key",
2395 apps: ["AppFoo"],
2396 min_sdk_version: "29",
Spandan Das91250b12022-05-06 22:12:55 +00002397 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002398 }
2399
2400 apex_key {
2401 name: "myapex.key",
2402 public_key: "testkey.avbpubkey",
2403 private_key: "testkey.pem",
2404 }
2405
2406 android_app {
2407 name: "AppFoo",
2408 srcs: ["foo/bar/MyClass.java"],
2409 sdk_version: "current",
2410 min_sdk_version: "29",
2411 system_modules: "none",
2412 stl: "none",
2413 static_libs: ["bar"],
2414 apex_available: [ "myapex" ],
2415 }
2416
2417 java_library {
2418 name: "bar",
2419 sdk_version: "current",
2420 srcs: ["a.java"],
2421 apex_available: [ "myapex" ],
2422 }
2423 `)
2424}
2425
2426func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002427 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002428 apex {
2429 name: "myapex",
2430 key: "myapex.key",
2431 native_shared_libs: ["mylib"],
2432 min_sdk_version: "29",
2433 }
2434
2435 apex_key {
2436 name: "myapex.key",
2437 public_key: "testkey.avbpubkey",
2438 private_key: "testkey.pem",
2439 }
2440
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002441 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002442 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2443 cc_library {
2444 name: "mylib",
2445 srcs: ["mylib.cpp"],
2446 shared_libs: ["mylib2"],
2447 system_shared_libs: [],
2448 stl: "none",
2449 apex_available: ["myapex", "otherapex"],
2450 min_sdk_version: "29",
2451 }
2452
2453 cc_library {
2454 name: "mylib2",
2455 srcs: ["mylib.cpp"],
2456 system_shared_libs: [],
2457 stl: "none",
2458 apex_available: ["otherapex"],
2459 stubs: { versions: ["29", "30"] },
2460 min_sdk_version: "30",
2461 }
2462
2463 apex {
2464 name: "otherapex",
2465 key: "myapex.key",
2466 native_shared_libs: ["mylib", "mylib2"],
2467 min_sdk_version: "30",
2468 }
2469 `)
2470 expectLink := func(from, from_variant, to, to_variant string) {
2471 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2472 libFlags := ld.Args["libFlags"]
2473 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2474 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002475 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002476 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002477}
2478
Jooyung Haned124c32021-01-26 11:43:46 +09002479func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002480 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2481 func(variables android.FixtureProductVariables) {
2482 variables.Platform_sdk_codename = proptools.StringPtr("S")
2483 variables.Platform_version_active_codenames = []string{"S"}
2484 },
2485 )
Jooyung Haned124c32021-01-26 11:43:46 +09002486 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2487 apex {
2488 name: "myapex",
2489 key: "myapex.key",
2490 native_shared_libs: ["libfoo"],
2491 min_sdk_version: "S",
2492 }
2493 apex_key {
2494 name: "myapex.key",
2495 public_key: "testkey.avbpubkey",
2496 private_key: "testkey.pem",
2497 }
2498 cc_library {
2499 name: "libfoo",
2500 shared_libs: ["libbar"],
2501 apex_available: ["myapex"],
2502 min_sdk_version: "29",
2503 }
2504 cc_library {
2505 name: "libbar",
2506 apex_available: ["myapex"],
2507 }
2508 `, withSAsActiveCodeNames)
2509}
2510
2511func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002512 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2513 variables.Platform_sdk_codename = proptools.StringPtr("S")
2514 variables.Platform_version_active_codenames = []string{"S", "T"}
2515 })
Colin Cross1c460562021-02-16 17:55:47 -08002516 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002517 apex {
2518 name: "myapex",
2519 key: "myapex.key",
2520 native_shared_libs: ["libfoo"],
2521 min_sdk_version: "S",
2522 }
2523 apex_key {
2524 name: "myapex.key",
2525 public_key: "testkey.avbpubkey",
2526 private_key: "testkey.pem",
2527 }
2528 cc_library {
2529 name: "libfoo",
2530 shared_libs: ["libbar"],
2531 apex_available: ["myapex"],
2532 min_sdk_version: "S",
2533 }
2534 cc_library {
2535 name: "libbar",
2536 stubs: {
2537 symbol_file: "libbar.map.txt",
2538 versions: ["30", "S", "T"],
2539 },
2540 }
2541 `, withSAsActiveCodeNames)
2542
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002543 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002544 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2545 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002546 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002547}
2548
Jiyong Park7c2ee712018-12-07 00:42:25 +09002549func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002550 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002551 apex {
2552 name: "myapex",
2553 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002554 native_shared_libs: ["mylib"],
2555 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002556 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002557 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002558 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002559 }
2560
2561 apex_key {
2562 name: "myapex.key",
2563 public_key: "testkey.avbpubkey",
2564 private_key: "testkey.pem",
2565 }
2566
2567 prebuilt_etc {
2568 name: "myetc",
2569 src: "myprebuilt",
2570 sub_dir: "foo/bar",
2571 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002572
2573 cc_library {
2574 name: "mylib",
2575 srcs: ["mylib.cpp"],
2576 relative_install_path: "foo/bar",
2577 system_shared_libs: [],
2578 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002579 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002580 }
2581
2582 cc_binary {
2583 name: "mybin",
2584 srcs: ["mylib.cpp"],
2585 relative_install_path: "foo/bar",
2586 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002587 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002588 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002589 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002590 `)
2591
Sundong Ahnabb64432019-10-22 13:58:29 +09002592 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002593 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002594
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002595 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002596 ensureContains(t, cmd, "/etc ")
2597 ensureContains(t, cmd, "/etc/foo ")
2598 ensureContains(t, cmd, "/etc/foo/bar ")
2599 ensureContains(t, cmd, "/lib64 ")
2600 ensureContains(t, cmd, "/lib64/foo ")
2601 ensureContains(t, cmd, "/lib64/foo/bar ")
2602 ensureContains(t, cmd, "/lib ")
2603 ensureContains(t, cmd, "/lib/foo ")
2604 ensureContains(t, cmd, "/lib/foo/bar ")
2605 ensureContains(t, cmd, "/bin ")
2606 ensureContains(t, cmd, "/bin/foo ")
2607 ensureContains(t, cmd, "/bin/foo/bar ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002608}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002609
Jooyung Han35155c42020-02-06 17:33:20 +09002610func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002611 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002612 apex {
2613 name: "myapex",
2614 key: "myapex.key",
2615 multilib: {
2616 both: {
2617 native_shared_libs: ["mylib"],
2618 binaries: ["mybin"],
2619 },
2620 },
2621 compile_multilib: "both",
2622 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002623 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002624 }
2625
2626 apex_key {
2627 name: "myapex.key",
2628 public_key: "testkey.avbpubkey",
2629 private_key: "testkey.pem",
2630 }
2631
2632 cc_library {
2633 name: "mylib",
2634 relative_install_path: "foo/bar",
2635 system_shared_libs: [],
2636 stl: "none",
2637 apex_available: [ "myapex" ],
2638 native_bridge_supported: true,
2639 }
2640
2641 cc_binary {
2642 name: "mybin",
2643 relative_install_path: "foo/bar",
2644 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002645 stl: "none",
2646 apex_available: [ "myapex" ],
2647 native_bridge_supported: true,
2648 compile_multilib: "both", // default is "first" for binary
2649 multilib: {
2650 lib64: {
2651 suffix: "64",
2652 },
2653 },
2654 }
2655 `, withNativeBridgeEnabled)
2656 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2657 "bin/foo/bar/mybin",
2658 "bin/foo/bar/mybin64",
2659 "bin/arm/foo/bar/mybin",
2660 "bin/arm64/foo/bar/mybin64",
2661 "lib/foo/bar/mylib.so",
2662 "lib/arm/foo/bar/mylib.so",
2663 "lib64/foo/bar/mylib.so",
2664 "lib64/arm64/foo/bar/mylib.so",
2665 })
2666}
2667
Jooyung Han85d61762020-06-24 23:50:26 +09002668func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08002669 result := android.GroupFixturePreparers(
2670 prepareForApexTest,
2671 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
2672 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002673 apex {
2674 name: "myapex",
2675 key: "myapex.key",
2676 binaries: ["mybin"],
2677 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002678 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002679 }
2680 apex_key {
2681 name: "myapex.key",
2682 public_key: "testkey.avbpubkey",
2683 private_key: "testkey.pem",
2684 }
2685 cc_binary {
2686 name: "mybin",
2687 vendor: true,
2688 shared_libs: ["libfoo"],
2689 }
2690 cc_library {
2691 name: "libfoo",
2692 proprietary: true,
2693 }
2694 `)
2695
Colin Crossc68db4b2021-11-11 18:59:15 -08002696 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09002697 "bin/mybin",
2698 "lib64/libfoo.so",
2699 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2700 "lib64/libc++.so",
2701 })
2702
Colin Crossc68db4b2021-11-11 18:59:15 -08002703 apexBundle := result.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2704 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002705 name := apexBundle.BaseModuleName()
2706 prefix := "TARGET_"
2707 var builder strings.Builder
2708 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08002709 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01002710 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002711 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002712
Colin Crossc68db4b2021-11-11 18:59:15 -08002713 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002714 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2715 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002716}
2717
Jooyung Hanc5a96762022-02-04 11:54:50 +09002718func TestVendorApex_use_vndk_as_stable_TryingToIncludeVNDKLib(t *testing.T) {
2719 testApexError(t, `Trying to include a VNDK library`, `
2720 apex {
2721 name: "myapex",
2722 key: "myapex.key",
2723 native_shared_libs: ["libc++"], // libc++ is a VNDK lib
2724 vendor: true,
2725 use_vndk_as_stable: true,
2726 updatable: false,
2727 }
2728 apex_key {
2729 name: "myapex.key",
2730 public_key: "testkey.avbpubkey",
2731 private_key: "testkey.pem",
2732 }`)
2733}
2734
Jooyung Handf78e212020-07-22 15:54:47 +09002735func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Jooyung Han91f92032022-02-04 12:36:33 +09002736 // myapex myapex2
2737 // | |
2738 // mybin ------. mybin2
2739 // \ \ / |
2740 // (stable) .---\--------` |
2741 // \ / \ |
2742 // \ / \ /
2743 // libvndk libvendor
2744 // (vndk)
Colin Cross1c460562021-02-16 17:55:47 -08002745 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002746 apex {
2747 name: "myapex",
2748 key: "myapex.key",
2749 binaries: ["mybin"],
2750 vendor: true,
2751 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002752 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002753 }
2754 apex_key {
2755 name: "myapex.key",
2756 public_key: "testkey.avbpubkey",
2757 private_key: "testkey.pem",
2758 }
2759 cc_binary {
2760 name: "mybin",
2761 vendor: true,
2762 shared_libs: ["libvndk", "libvendor"],
2763 }
2764 cc_library {
2765 name: "libvndk",
2766 vndk: {
2767 enabled: true,
2768 },
2769 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002770 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002771 }
2772 cc_library {
2773 name: "libvendor",
2774 vendor: true,
Jooyung Han91f92032022-02-04 12:36:33 +09002775 stl: "none",
2776 }
2777 apex {
2778 name: "myapex2",
2779 key: "myapex.key",
2780 binaries: ["mybin2"],
2781 vendor: true,
2782 use_vndk_as_stable: false,
2783 updatable: false,
2784 }
2785 cc_binary {
2786 name: "mybin2",
2787 vendor: true,
2788 shared_libs: ["libvndk", "libvendor"],
Jooyung Handf78e212020-07-22 15:54:47 +09002789 }
2790 `)
2791
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002792 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002793
Jooyung Han91f92032022-02-04 12:36:33 +09002794 for _, tc := range []struct {
2795 name string
2796 apexName string
2797 moduleName string
2798 moduleVariant string
2799 libs []string
2800 contents []string
2801 requireVndkNamespace bool
2802 }{
2803 {
2804 name: "use_vndk_as_stable",
2805 apexName: "myapex",
2806 moduleName: "mybin",
2807 moduleVariant: vendorVariant + "_apex10000",
2808 libs: []string{
2809 // should link with vendor variants of VNDK libs(libvndk/libc++)
2810 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared/libvndk.so",
2811 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared/libc++.so",
2812 // unstable Vendor libs as APEX variant
2813 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2814 },
2815 contents: []string{
2816 "bin/mybin",
2817 "lib64/libvendor.so",
2818 // VNDK libs (libvndk/libc++) are not included
2819 },
2820 requireVndkNamespace: true,
2821 },
2822 {
2823 name: "!use_vndk_as_stable",
2824 apexName: "myapex2",
2825 moduleName: "mybin2",
2826 moduleVariant: vendorVariant + "_myapex2",
2827 libs: []string{
2828 // should link with "unique" APEX(myapex2) variant of VNDK libs(libvndk/libc++)
2829 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared_myapex2/libvndk.so",
2830 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared_myapex2/libc++.so",
2831 // unstable vendor libs have "merged" APEX variants
2832 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2833 },
2834 contents: []string{
2835 "bin/mybin2",
2836 "lib64/libvendor.so",
2837 // VNDK libs are included as well
2838 "lib64/libvndk.so",
2839 "lib64/libc++.so",
2840 },
2841 requireVndkNamespace: false,
2842 },
2843 } {
2844 t.Run(tc.name, func(t *testing.T) {
2845 // Check linked libs
2846 ldRule := ctx.ModuleForTests(tc.moduleName, tc.moduleVariant).Rule("ld")
2847 libs := names(ldRule.Args["libFlags"])
2848 for _, lib := range tc.libs {
2849 ensureListContains(t, libs, lib)
2850 }
2851 // Check apex contents
2852 ensureExactContents(t, ctx, tc.apexName, "android_common_"+tc.apexName+"_image", tc.contents)
Jooyung Handf78e212020-07-22 15:54:47 +09002853
Jooyung Han91f92032022-02-04 12:36:33 +09002854 // Check "requireNativeLibs"
2855 apexManifestRule := ctx.ModuleForTests(tc.apexName, "android_common_"+tc.apexName+"_image").Rule("apexManifestRule")
2856 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2857 if tc.requireVndkNamespace {
2858 ensureListContains(t, requireNativeLibs, ":vndk")
2859 } else {
2860 ensureListNotContains(t, requireNativeLibs, ":vndk")
2861 }
2862 })
2863 }
Jooyung Handf78e212020-07-22 15:54:47 +09002864}
2865
Justin Yun13decfb2021-03-08 19:25:55 +09002866func TestProductVariant(t *testing.T) {
2867 ctx := testApex(t, `
2868 apex {
2869 name: "myapex",
2870 key: "myapex.key",
2871 updatable: false,
2872 product_specific: true,
2873 binaries: ["foo"],
2874 }
2875
2876 apex_key {
2877 name: "myapex.key",
2878 public_key: "testkey.avbpubkey",
2879 private_key: "testkey.pem",
2880 }
2881
2882 cc_binary {
2883 name: "foo",
2884 product_available: true,
2885 apex_available: ["myapex"],
2886 srcs: ["foo.cpp"],
2887 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002888 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2889 variables.ProductVndkVersion = proptools.StringPtr("current")
2890 }),
2891 )
Justin Yun13decfb2021-03-08 19:25:55 +09002892
2893 cflags := strings.Fields(
Jooyung Han91f92032022-02-04 12:36:33 +09002894 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_myapex").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002895 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2896 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2897 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2898 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2899}
2900
Jooyung Han8e5685d2020-09-21 11:02:57 +09002901func TestApex_withPrebuiltFirmware(t *testing.T) {
2902 testCases := []struct {
2903 name string
2904 additionalProp string
2905 }{
2906 {"system apex with prebuilt_firmware", ""},
2907 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2908 }
2909 for _, tc := range testCases {
2910 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002911 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002912 apex {
2913 name: "myapex",
2914 key: "myapex.key",
2915 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002916 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002917 `+tc.additionalProp+`
2918 }
2919 apex_key {
2920 name: "myapex.key",
2921 public_key: "testkey.avbpubkey",
2922 private_key: "testkey.pem",
2923 }
2924 prebuilt_firmware {
2925 name: "myfirmware",
2926 src: "myfirmware.bin",
2927 filename_from_src: true,
2928 `+tc.additionalProp+`
2929 }
2930 `)
2931 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2932 "etc/firmware/myfirmware.bin",
2933 })
2934 })
2935 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002936}
2937
Jooyung Hanefb184e2020-06-25 17:14:25 +09002938func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002939 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002940 apex {
2941 name: "myapex",
2942 key: "myapex.key",
2943 vendor: true,
2944 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002945 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002946 }
2947
2948 apex_key {
2949 name: "myapex.key",
2950 public_key: "testkey.avbpubkey",
2951 private_key: "testkey.pem",
2952 }
2953
2954 cc_library {
2955 name: "mylib",
2956 vendor_available: true,
2957 }
2958 `)
2959
2960 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002961 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002962 name := apexBundle.BaseModuleName()
2963 prefix := "TARGET_"
2964 var builder strings.Builder
2965 data.Custom(&builder, name, prefix, "", data)
2966 androidMk := builder.String()
2967 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2968}
2969
Jooyung Han2ed99d02020-06-24 23:26:26 +09002970func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002971 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002972 apex {
2973 name: "myapex",
2974 key: "myapex.key",
2975 vintf_fragments: ["fragment.xml"],
2976 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002977 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002978 }
2979 apex_key {
2980 name: "myapex.key",
2981 public_key: "testkey.avbpubkey",
2982 private_key: "testkey.pem",
2983 }
2984 cc_binary {
2985 name: "mybin",
2986 }
2987 `)
2988
2989 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002990 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002991 name := apexBundle.BaseModuleName()
2992 prefix := "TARGET_"
2993 var builder strings.Builder
2994 data.Custom(&builder, name, prefix, "", data)
2995 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04002996 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04002997 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09002998}
2999
Jiyong Park16e91a02018-12-20 18:18:08 +09003000func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003001 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003002 apex {
3003 name: "myapex",
3004 key: "myapex.key",
3005 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003006 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003007 }
3008
3009 apex_key {
3010 name: "myapex.key",
3011 public_key: "testkey.avbpubkey",
3012 private_key: "testkey.pem",
3013 }
3014
3015 cc_library {
3016 name: "mylib",
3017 srcs: ["mylib.cpp"],
3018 system_shared_libs: [],
3019 stl: "none",
3020 stubs: {
3021 versions: ["1", "2", "3"],
3022 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003023 apex_available: [
3024 "//apex_available:platform",
3025 "myapex",
3026 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09003027 }
3028
3029 cc_binary {
3030 name: "not_in_apex",
3031 srcs: ["mylib.cpp"],
3032 static_libs: ["mylib"],
3033 static_executable: true,
3034 system_shared_libs: [],
3035 stl: "none",
3036 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003037 `)
3038
Colin Cross7113d202019-11-20 16:39:12 -08003039 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003040
3041 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003042 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003043}
Jiyong Park9335a262018-12-24 11:31:58 +09003044
3045func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003046 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003047 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003048 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003049 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003050 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003051 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003052 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003053 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003054 }
3055
3056 cc_library {
3057 name: "mylib",
3058 srcs: ["mylib.cpp"],
3059 system_shared_libs: [],
3060 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003061 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003062 }
3063
3064 apex_key {
3065 name: "myapex.key",
3066 public_key: "testkey.avbpubkey",
3067 private_key: "testkey.pem",
3068 }
3069
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003070 android_app_certificate {
3071 name: "myapex.certificate",
3072 certificate: "testkey",
3073 }
3074
3075 android_app_certificate {
3076 name: "myapex.certificate.override",
3077 certificate: "testkey.override",
3078 }
3079
Jiyong Park9335a262018-12-24 11:31:58 +09003080 `)
3081
3082 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003083 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003084
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003085 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3086 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003087 "vendor/foo/devkeys/testkey.avbpubkey")
3088 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003089 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3090 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003091 "vendor/foo/devkeys/testkey.pem")
3092 }
3093
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003094 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09003095 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003096 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003097 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003098 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003099 }
3100}
Jiyong Park58e364a2019-01-19 19:24:06 +09003101
Jooyung Hanf121a652019-12-17 14:30:11 +09003102func TestCertificate(t *testing.T) {
3103 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003104 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003105 apex {
3106 name: "myapex",
3107 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003108 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003109 }
3110 apex_key {
3111 name: "myapex.key",
3112 public_key: "testkey.avbpubkey",
3113 private_key: "testkey.pem",
3114 }`)
3115 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3116 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3117 if actual := rule.Args["certificates"]; actual != expected {
3118 t.Errorf("certificates should be %q, not %q", expected, actual)
3119 }
3120 })
3121 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003122 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003123 apex {
3124 name: "myapex_keytest",
3125 key: "myapex.key",
3126 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003127 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003128 }
3129 apex_key {
3130 name: "myapex.key",
3131 public_key: "testkey.avbpubkey",
3132 private_key: "testkey.pem",
3133 }
3134 android_app_certificate {
3135 name: "myapex.certificate.override",
3136 certificate: "testkey.override",
3137 }`)
3138 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3139 expected := "testkey.override.x509.pem testkey.override.pk8"
3140 if actual := rule.Args["certificates"]; actual != expected {
3141 t.Errorf("certificates should be %q, not %q", expected, actual)
3142 }
3143 })
3144 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003145 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003146 apex {
3147 name: "myapex",
3148 key: "myapex.key",
3149 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003150 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003151 }
3152 apex_key {
3153 name: "myapex.key",
3154 public_key: "testkey.avbpubkey",
3155 private_key: "testkey.pem",
3156 }
3157 android_app_certificate {
3158 name: "myapex.certificate",
3159 certificate: "testkey",
3160 }`)
3161 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3162 expected := "testkey.x509.pem testkey.pk8"
3163 if actual := rule.Args["certificates"]; actual != expected {
3164 t.Errorf("certificates should be %q, not %q", expected, actual)
3165 }
3166 })
3167 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003168 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003169 apex {
3170 name: "myapex_keytest",
3171 key: "myapex.key",
3172 file_contexts: ":myapex-file_contexts",
3173 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003174 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003175 }
3176 apex_key {
3177 name: "myapex.key",
3178 public_key: "testkey.avbpubkey",
3179 private_key: "testkey.pem",
3180 }
3181 android_app_certificate {
3182 name: "myapex.certificate.override",
3183 certificate: "testkey.override",
3184 }`)
3185 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3186 expected := "testkey.override.x509.pem testkey.override.pk8"
3187 if actual := rule.Args["certificates"]; actual != expected {
3188 t.Errorf("certificates should be %q, not %q", expected, actual)
3189 }
3190 })
3191 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003192 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003193 apex {
3194 name: "myapex",
3195 key: "myapex.key",
3196 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003197 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003198 }
3199 apex_key {
3200 name: "myapex.key",
3201 public_key: "testkey.avbpubkey",
3202 private_key: "testkey.pem",
3203 }`)
3204 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3205 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3206 if actual := rule.Args["certificates"]; actual != expected {
3207 t.Errorf("certificates should be %q, not %q", expected, actual)
3208 }
3209 })
3210 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003211 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003212 apex {
3213 name: "myapex_keytest",
3214 key: "myapex.key",
3215 file_contexts: ":myapex-file_contexts",
3216 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003217 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003218 }
3219 apex_key {
3220 name: "myapex.key",
3221 public_key: "testkey.avbpubkey",
3222 private_key: "testkey.pem",
3223 }
3224 android_app_certificate {
3225 name: "myapex.certificate.override",
3226 certificate: "testkey.override",
3227 }`)
3228 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3229 expected := "testkey.override.x509.pem testkey.override.pk8"
3230 if actual := rule.Args["certificates"]; actual != expected {
3231 t.Errorf("certificates should be %q, not %q", expected, actual)
3232 }
3233 })
3234}
3235
Jiyong Park58e364a2019-01-19 19:24:06 +09003236func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003237 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003238 apex {
3239 name: "myapex",
3240 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003241 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003242 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003243 }
3244
3245 apex {
3246 name: "otherapex",
3247 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003248 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003249 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003250 }
3251
3252 apex_key {
3253 name: "myapex.key",
3254 public_key: "testkey.avbpubkey",
3255 private_key: "testkey.pem",
3256 }
3257
3258 cc_library {
3259 name: "mylib",
3260 srcs: ["mylib.cpp"],
3261 system_shared_libs: [],
3262 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003263 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003264 "myapex",
3265 "otherapex",
3266 ],
Jooyung Han24282772020-03-21 23:20:55 +09003267 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003268 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003269 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003270 cc_library {
3271 name: "mylib2",
3272 srcs: ["mylib.cpp"],
3273 system_shared_libs: [],
3274 stl: "none",
3275 apex_available: [
3276 "myapex",
3277 "otherapex",
3278 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003279 static_libs: ["mylib3"],
3280 recovery_available: true,
3281 min_sdk_version: "29",
3282 }
3283 cc_library {
3284 name: "mylib3",
3285 srcs: ["mylib.cpp"],
3286 system_shared_libs: [],
3287 stl: "none",
3288 apex_available: [
3289 "myapex",
3290 "otherapex",
3291 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003292 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003293 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003294 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003295 `)
3296
Jooyung Hanc87a0592020-03-02 17:44:33 +09003297 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003298 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003299 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003300 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003301
Jooyung Hanccce2f22020-03-07 03:45:53 +09003302 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003303 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003304 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003305 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003306
Jooyung Hanccce2f22020-03-07 03:45:53 +09003307 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003308 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003309 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003310 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jiyong Park58e364a2019-01-19 19:24:06 +09003311
Colin Crossaede88c2020-08-11 12:17:01 -07003312 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3313 // each variant defines additional macros to distinguish which apex variant it is built for
3314
3315 // non-APEX variant does not have __ANDROID_APEX__ defined
3316 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3317 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3318
Dan Albertb19953d2020-11-17 15:29:36 -08003319 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003320 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3321 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003322 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003323
Jooyung Hanc87a0592020-03-02 17:44:33 +09003324 // non-APEX variant does not have __ANDROID_APEX__ defined
3325 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3326 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3327
Dan Albertb19953d2020-11-17 15:29:36 -08003328 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003329 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003330 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003331 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003332}
Jiyong Park7e636d02019-01-28 16:16:54 +09003333
3334func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003335 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003336 apex {
3337 name: "myapex",
3338 key: "myapex.key",
3339 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003340 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003341 }
3342
3343 apex_key {
3344 name: "myapex.key",
3345 public_key: "testkey.avbpubkey",
3346 private_key: "testkey.pem",
3347 }
3348
3349 cc_library_headers {
3350 name: "mylib_headers",
3351 export_include_dirs: ["my_include"],
3352 system_shared_libs: [],
3353 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003354 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003355 }
3356
3357 cc_library {
3358 name: "mylib",
3359 srcs: ["mylib.cpp"],
3360 system_shared_libs: [],
3361 stl: "none",
3362 header_libs: ["mylib_headers"],
3363 export_header_lib_headers: ["mylib_headers"],
3364 stubs: {
3365 versions: ["1", "2", "3"],
3366 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003367 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003368 }
3369
3370 cc_library {
3371 name: "otherlib",
3372 srcs: ["mylib.cpp"],
3373 system_shared_libs: [],
3374 stl: "none",
3375 shared_libs: ["mylib"],
3376 }
3377 `)
3378
Colin Cross7113d202019-11-20 16:39:12 -08003379 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003380
3381 // Ensure that the include path of the header lib is exported to 'otherlib'
3382 ensureContains(t, cFlags, "-Imy_include")
3383}
Alex Light9670d332019-01-29 18:07:33 -08003384
Jiyong Park7cd10e32020-01-14 09:22:18 +09003385type fileInApex struct {
3386 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003387 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003388 isLink bool
3389}
3390
Jooyung Hana57af4a2020-01-23 05:36:59 +00003391func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003392 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003393 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003394 copyCmds := apexRule.Args["copy_commands"]
3395 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003396 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003397 for _, cmd := range strings.Split(copyCmds, "&&") {
3398 cmd = strings.TrimSpace(cmd)
3399 if cmd == "" {
3400 continue
3401 }
3402 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003403 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003404 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003405 switch terms[0] {
3406 case "mkdir":
3407 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003408 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003409 t.Fatal("copyCmds contains invalid cp command", cmd)
3410 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003411 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003412 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003413 isLink = false
3414 case "ln":
3415 if len(terms) != 3 && len(terms) != 4 {
3416 // ln LINK TARGET or ln -s LINK TARGET
3417 t.Fatal("copyCmds contains invalid ln command", cmd)
3418 }
3419 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003420 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003421 isLink = true
3422 default:
3423 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3424 }
3425 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003426 index := strings.Index(dst, imageApexDir)
3427 if index == -1 {
3428 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3429 }
3430 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003431 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003432 }
3433 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003434 return ret
3435}
3436
Jooyung Hana57af4a2020-01-23 05:36:59 +00003437func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3438 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003439 var failed bool
3440 var surplus []string
3441 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003442 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003443 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003444 for _, expected := range files {
3445 if matched, _ := path.Match(expected, file.path); matched {
3446 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003447 mactchFound = true
3448 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003449 }
3450 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003451 if !mactchFound {
3452 surplus = append(surplus, file.path)
3453 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003454 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003455
Jooyung Han31c470b2019-10-18 16:26:59 +09003456 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003457 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003458 t.Log("surplus files", surplus)
3459 failed = true
3460 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003461
3462 if len(files) > len(filesMatched) {
3463 var missing []string
3464 for _, expected := range files {
3465 if !filesMatched[expected] {
3466 missing = append(missing, expected)
3467 }
3468 }
3469 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003470 t.Log("missing files", missing)
3471 failed = true
3472 }
3473 if failed {
3474 t.Fail()
3475 }
3476}
3477
Jooyung Han344d5432019-08-23 11:17:39 +09003478func TestVndkApexCurrent(t *testing.T) {
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003479 commonFiles := []string{
Jooyung Hane6436d72020-02-27 13:31:56 +09003480 "lib/libc++.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003481 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003482 "etc/llndk.libraries.29.txt",
3483 "etc/vndkcore.libraries.29.txt",
3484 "etc/vndksp.libraries.29.txt",
3485 "etc/vndkprivate.libraries.29.txt",
3486 "etc/vndkproduct.libraries.29.txt",
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003487 }
3488 testCases := []struct {
3489 vndkVersion string
3490 expectedFiles []string
3491 }{
3492 {
3493 vndkVersion: "current",
3494 expectedFiles: append(commonFiles,
3495 "lib/libvndk.so",
3496 "lib/libvndksp.so",
3497 "lib64/libvndk.so",
3498 "lib64/libvndksp.so"),
3499 },
3500 {
3501 vndkVersion: "",
3502 expectedFiles: append(commonFiles,
3503 // Legacy VNDK APEX contains only VNDK-SP files (of core variant)
3504 "lib/libvndksp.so",
3505 "lib64/libvndksp.so"),
3506 },
3507 }
3508 for _, tc := range testCases {
3509 t.Run("VNDK.current with DeviceVndkVersion="+tc.vndkVersion, func(t *testing.T) {
3510 ctx := testApex(t, `
3511 apex_vndk {
3512 name: "com.android.vndk.current",
3513 key: "com.android.vndk.current.key",
3514 updatable: false,
3515 }
3516
3517 apex_key {
3518 name: "com.android.vndk.current.key",
3519 public_key: "testkey.avbpubkey",
3520 private_key: "testkey.pem",
3521 }
3522
3523 cc_library {
3524 name: "libvndk",
3525 srcs: ["mylib.cpp"],
3526 vendor_available: true,
3527 product_available: true,
3528 vndk: {
3529 enabled: true,
3530 },
3531 system_shared_libs: [],
3532 stl: "none",
3533 apex_available: [ "com.android.vndk.current" ],
3534 }
3535
3536 cc_library {
3537 name: "libvndksp",
3538 srcs: ["mylib.cpp"],
3539 vendor_available: true,
3540 product_available: true,
3541 vndk: {
3542 enabled: true,
3543 support_system_process: true,
3544 },
3545 system_shared_libs: [],
3546 stl: "none",
3547 apex_available: [ "com.android.vndk.current" ],
3548 }
3549
3550 // VNDK-Ext should not cause any problems
3551
3552 cc_library {
3553 name: "libvndk.ext",
3554 srcs: ["mylib2.cpp"],
3555 vendor: true,
3556 vndk: {
3557 enabled: true,
3558 extends: "libvndk",
3559 },
3560 system_shared_libs: [],
3561 stl: "none",
3562 }
3563
3564 cc_library {
3565 name: "libvndksp.ext",
3566 srcs: ["mylib2.cpp"],
3567 vendor: true,
3568 vndk: {
3569 enabled: true,
3570 support_system_process: true,
3571 extends: "libvndksp",
3572 },
3573 system_shared_libs: [],
3574 stl: "none",
3575 }
3576 `+vndkLibrariesTxtFiles("current"), android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3577 variables.DeviceVndkVersion = proptools.StringPtr(tc.vndkVersion)
3578 }))
3579 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", tc.expectedFiles)
3580 })
3581 }
Jooyung Han344d5432019-08-23 11:17:39 +09003582}
3583
3584func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003585 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003586 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003587 name: "com.android.vndk.current",
3588 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003589 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003590 }
3591
3592 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003593 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003594 public_key: "testkey.avbpubkey",
3595 private_key: "testkey.pem",
3596 }
3597
3598 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003599 name: "libvndk",
3600 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003601 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003602 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003603 vndk: {
3604 enabled: true,
3605 },
3606 system_shared_libs: [],
3607 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003608 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003609 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003610
3611 cc_prebuilt_library_shared {
3612 name: "libvndk.arm",
3613 srcs: ["libvndk.arm.so"],
3614 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003615 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003616 vndk: {
3617 enabled: true,
3618 },
3619 enabled: false,
3620 arch: {
3621 arm: {
3622 enabled: true,
3623 },
3624 },
3625 system_shared_libs: [],
3626 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003627 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003628 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003629 `+vndkLibrariesTxtFiles("current"),
3630 withFiles(map[string][]byte{
3631 "libvndk.so": nil,
3632 "libvndk.arm.so": nil,
3633 }))
Colin Cross2807f002021-03-02 10:15:29 -08003634 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003635 "lib/libvndk.so",
3636 "lib/libvndk.arm.so",
3637 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003638 "lib/libc++.so",
3639 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003640 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003641 })
Jooyung Han344d5432019-08-23 11:17:39 +09003642}
3643
Jooyung Han39edb6c2019-11-06 16:53:07 +09003644func vndkLibrariesTxtFiles(vers ...string) (result string) {
3645 for _, v := range vers {
3646 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003647 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003648 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003649 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003650 name: "` + txt + `.libraries.txt",
3651 }
3652 `
3653 }
3654 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003655 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003656 result += `
3657 prebuilt_etc {
3658 name: "` + txt + `.libraries.` + v + `.txt",
3659 src: "dummy.txt",
3660 }
3661 `
3662 }
3663 }
3664 }
3665 return
3666}
3667
Jooyung Han344d5432019-08-23 11:17:39 +09003668func TestVndkApexVersion(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.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003672 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003673 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003674 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003675 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003676 }
3677
3678 apex_key {
3679 name: "myapex.key",
3680 public_key: "testkey.avbpubkey",
3681 private_key: "testkey.pem",
3682 }
3683
Jooyung Han31c470b2019-10-18 16:26:59 +09003684 vndk_prebuilt_shared {
3685 name: "libvndk27",
3686 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003687 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003688 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003689 vndk: {
3690 enabled: true,
3691 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003692 target_arch: "arm64",
3693 arch: {
3694 arm: {
3695 srcs: ["libvndk27_arm.so"],
3696 },
3697 arm64: {
3698 srcs: ["libvndk27_arm64.so"],
3699 },
3700 },
Colin Cross2807f002021-03-02 10:15:29 -08003701 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003702 }
3703
3704 vndk_prebuilt_shared {
3705 name: "libvndk27",
3706 version: "27",
3707 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003708 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003709 vndk: {
3710 enabled: true,
3711 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003712 target_arch: "x86_64",
3713 arch: {
3714 x86: {
3715 srcs: ["libvndk27_x86.so"],
3716 },
3717 x86_64: {
3718 srcs: ["libvndk27_x86_64.so"],
3719 },
3720 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003721 }
3722 `+vndkLibrariesTxtFiles("27"),
3723 withFiles(map[string][]byte{
3724 "libvndk27_arm.so": nil,
3725 "libvndk27_arm64.so": nil,
3726 "libvndk27_x86.so": nil,
3727 "libvndk27_x86_64.so": nil,
3728 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003729
Colin Cross2807f002021-03-02 10:15:29 -08003730 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003731 "lib/libvndk27_arm.so",
3732 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003733 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003734 })
Jooyung Han344d5432019-08-23 11:17:39 +09003735}
3736
Jooyung Han90eee022019-10-01 20:02:42 +09003737func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003738 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003739 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003740 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003741 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003742 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003743 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003744 }
3745 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003746 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003747 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003748 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003749 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003750 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003751 }
3752 apex_key {
3753 name: "myapex.key",
3754 public_key: "testkey.avbpubkey",
3755 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003756 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003757
3758 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003759 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003760 actual := proptools.String(bundle.properties.Apex_name)
3761 if !reflect.DeepEqual(actual, expected) {
3762 t.Errorf("Got '%v', expected '%v'", actual, expected)
3763 }
3764 }
3765
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003766 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003767 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003768}
3769
Jooyung Han344d5432019-08-23 11:17:39 +09003770func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003771 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003772 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003773 name: "com.android.vndk.current",
3774 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003775 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003776 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003777 }
3778
3779 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003780 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003781 public_key: "testkey.avbpubkey",
3782 private_key: "testkey.pem",
3783 }
3784
3785 cc_library {
3786 name: "libvndk",
3787 srcs: ["mylib.cpp"],
3788 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003789 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003790 native_bridge_supported: true,
3791 host_supported: true,
3792 vndk: {
3793 enabled: true,
3794 },
3795 system_shared_libs: [],
3796 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003797 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003798 }
Colin Cross2807f002021-03-02 10:15:29 -08003799 `+vndkLibrariesTxtFiles("current"),
3800 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003801
Colin Cross2807f002021-03-02 10:15:29 -08003802 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003803 "lib/libvndk.so",
3804 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003805 "lib/libc++.so",
3806 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003807 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003808 })
Jooyung Han344d5432019-08-23 11:17:39 +09003809}
3810
3811func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003812 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003813 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003814 name: "com.android.vndk.current",
3815 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003816 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003817 native_bridge_supported: true,
3818 }
3819
3820 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003821 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003822 public_key: "testkey.avbpubkey",
3823 private_key: "testkey.pem",
3824 }
3825
3826 cc_library {
3827 name: "libvndk",
3828 srcs: ["mylib.cpp"],
3829 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003830 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003831 native_bridge_supported: true,
3832 host_supported: true,
3833 vndk: {
3834 enabled: true,
3835 },
3836 system_shared_libs: [],
3837 stl: "none",
3838 }
3839 `)
3840}
3841
Jooyung Han31c470b2019-10-18 16:26:59 +09003842func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003843 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003844 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003845 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003846 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003847 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003848 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003849 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003850 }
3851
3852 apex_key {
3853 name: "myapex.key",
3854 public_key: "testkey.avbpubkey",
3855 private_key: "testkey.pem",
3856 }
3857
3858 vndk_prebuilt_shared {
3859 name: "libvndk27",
3860 version: "27",
3861 target_arch: "arm",
3862 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003863 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003864 vndk: {
3865 enabled: true,
3866 },
3867 arch: {
3868 arm: {
3869 srcs: ["libvndk27.so"],
3870 }
3871 },
3872 }
3873
3874 vndk_prebuilt_shared {
3875 name: "libvndk27",
3876 version: "27",
3877 target_arch: "arm",
3878 binder32bit: true,
3879 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003880 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003881 vndk: {
3882 enabled: true,
3883 },
3884 arch: {
3885 arm: {
3886 srcs: ["libvndk27binder32.so"],
3887 }
3888 },
Colin Cross2807f002021-03-02 10:15:29 -08003889 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003890 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003891 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003892 withFiles(map[string][]byte{
3893 "libvndk27.so": nil,
3894 "libvndk27binder32.so": nil,
3895 }),
3896 withBinder32bit,
3897 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07003898 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09003899 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3900 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003901 },
3902 }),
3903 )
3904
Colin Cross2807f002021-03-02 10:15:29 -08003905 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003906 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003907 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003908 })
3909}
3910
Jooyung Han45a96772020-06-15 14:59:42 +09003911func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003912 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003913 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003914 name: "com.android.vndk.current",
3915 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003916 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003917 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003918 }
3919
3920 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003921 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003922 public_key: "testkey.avbpubkey",
3923 private_key: "testkey.pem",
3924 }
3925
3926 cc_library {
3927 name: "libz",
3928 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003929 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003930 vndk: {
3931 enabled: true,
3932 },
3933 stubs: {
3934 symbol_file: "libz.map.txt",
3935 versions: ["30"],
3936 }
3937 }
3938 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3939 "libz.map.txt": nil,
3940 }))
3941
Colin Cross2807f002021-03-02 10:15:29 -08003942 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003943 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3944 ensureListEmpty(t, provideNativeLibs)
3945}
3946
Jooyung Hane1633032019-08-01 17:41:43 +09003947func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003948 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003949 apex {
3950 name: "myapex_nodep",
3951 key: "myapex.key",
3952 native_shared_libs: ["lib_nodep"],
3953 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003954 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003955 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003956 }
3957
3958 apex {
3959 name: "myapex_dep",
3960 key: "myapex.key",
3961 native_shared_libs: ["lib_dep"],
3962 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003963 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003964 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003965 }
3966
3967 apex {
3968 name: "myapex_provider",
3969 key: "myapex.key",
3970 native_shared_libs: ["libfoo"],
3971 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003972 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003973 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003974 }
3975
3976 apex {
3977 name: "myapex_selfcontained",
3978 key: "myapex.key",
3979 native_shared_libs: ["lib_dep", "libfoo"],
3980 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003981 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003982 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003983 }
3984
3985 apex_key {
3986 name: "myapex.key",
3987 public_key: "testkey.avbpubkey",
3988 private_key: "testkey.pem",
3989 }
3990
3991 cc_library {
3992 name: "lib_nodep",
3993 srcs: ["mylib.cpp"],
3994 system_shared_libs: [],
3995 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003996 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003997 }
3998
3999 cc_library {
4000 name: "lib_dep",
4001 srcs: ["mylib.cpp"],
4002 shared_libs: ["libfoo"],
4003 system_shared_libs: [],
4004 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004005 apex_available: [
4006 "myapex_dep",
4007 "myapex_provider",
4008 "myapex_selfcontained",
4009 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004010 }
4011
4012 cc_library {
4013 name: "libfoo",
4014 srcs: ["mytest.cpp"],
4015 stubs: {
4016 versions: ["1"],
4017 },
4018 system_shared_libs: [],
4019 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004020 apex_available: [
4021 "myapex_provider",
4022 "myapex_selfcontained",
4023 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004024 }
4025 `)
4026
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004027 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004028 var provideNativeLibs, requireNativeLibs []string
4029
Sundong Ahnabb64432019-10-22 13:58:29 +09004030 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004031 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4032 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004033 ensureListEmpty(t, provideNativeLibs)
4034 ensureListEmpty(t, requireNativeLibs)
4035
Sundong Ahnabb64432019-10-22 13:58:29 +09004036 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004037 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4038 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004039 ensureListEmpty(t, provideNativeLibs)
4040 ensureListContains(t, requireNativeLibs, "libfoo.so")
4041
Sundong Ahnabb64432019-10-22 13:58:29 +09004042 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004043 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4044 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004045 ensureListContains(t, provideNativeLibs, "libfoo.so")
4046 ensureListEmpty(t, requireNativeLibs)
4047
Sundong Ahnabb64432019-10-22 13:58:29 +09004048 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004049 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4050 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004051 ensureListContains(t, provideNativeLibs, "libfoo.so")
4052 ensureListEmpty(t, requireNativeLibs)
4053}
4054
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004055func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004056 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004057 apex {
4058 name: "myapex",
4059 key: "myapex.key",
4060 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09004061 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004062 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004063 }
4064
4065 apex_key {
4066 name: "myapex.key",
4067 public_key: "testkey.avbpubkey",
4068 private_key: "testkey.pem",
4069 }
Jiyong Parkdb334862020-02-05 17:19:28 +09004070
4071 cc_library {
4072 name: "mylib",
4073 srcs: ["mylib.cpp"],
4074 system_shared_libs: [],
4075 stl: "none",
4076 apex_available: [
4077 "//apex_available:platform",
4078 "myapex",
4079 ],
4080 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004081 `)
4082
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004083 module := ctx.ModuleForTests("myapex", "android_common_com.android.myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004084 apexManifestRule := module.Rule("apexManifestRule")
4085 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
4086 apexRule := module.Rule("apexRule")
4087 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09004088
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004089 apexBundle := module.Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004090 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09004091 name := apexBundle.BaseModuleName()
4092 prefix := "TARGET_"
4093 var builder strings.Builder
4094 data.Custom(&builder, name, prefix, "", data)
4095 androidMk := builder.String()
4096 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
4097 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004098}
4099
Alex Light0851b882019-02-07 13:20:53 -08004100func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004101 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004102 apex {
4103 name: "myapex",
4104 key: "myapex.key",
4105 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004106 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004107 }
4108
4109 apex_key {
4110 name: "myapex.key",
4111 public_key: "testkey.avbpubkey",
4112 private_key: "testkey.pem",
4113 }
4114
4115 cc_library {
4116 name: "mylib_common",
4117 srcs: ["mylib.cpp"],
4118 system_shared_libs: [],
4119 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004120 apex_available: [
4121 "//apex_available:platform",
4122 "myapex",
4123 ],
Alex Light0851b882019-02-07 13:20:53 -08004124 }
4125 `)
4126
Sundong Ahnabb64432019-10-22 13:58:29 +09004127 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004128 apexRule := module.Rule("apexRule")
4129 copyCmds := apexRule.Args["copy_commands"]
4130
4131 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4132 t.Log("Apex was a test apex!")
4133 t.Fail()
4134 }
4135 // Ensure that main rule creates an output
4136 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4137
4138 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004139 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004140
4141 // Ensure that both direct and indirect deps are copied into apex
4142 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4143
Colin Cross7113d202019-11-20 16:39:12 -08004144 // Ensure that the platform variant ends with _shared
4145 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004146
Colin Cross56a83212020-09-15 18:30:11 -07004147 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004148 t.Log("Found mylib_common not in any apex!")
4149 t.Fail()
4150 }
4151}
4152
4153func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004154 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004155 apex_test {
4156 name: "myapex",
4157 key: "myapex.key",
4158 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004159 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004160 }
4161
4162 apex_key {
4163 name: "myapex.key",
4164 public_key: "testkey.avbpubkey",
4165 private_key: "testkey.pem",
4166 }
4167
4168 cc_library {
4169 name: "mylib_common_test",
4170 srcs: ["mylib.cpp"],
4171 system_shared_libs: [],
4172 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004173 // TODO: remove //apex_available:platform
4174 apex_available: [
4175 "//apex_available:platform",
4176 "myapex",
4177 ],
Alex Light0851b882019-02-07 13:20:53 -08004178 }
4179 `)
4180
Sundong Ahnabb64432019-10-22 13:58:29 +09004181 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004182 apexRule := module.Rule("apexRule")
4183 copyCmds := apexRule.Args["copy_commands"]
4184
4185 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4186 t.Log("Apex was not a test apex!")
4187 t.Fail()
4188 }
4189 // Ensure that main rule creates an output
4190 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4191
4192 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004193 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004194
4195 // Ensure that both direct and indirect deps are copied into apex
4196 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4197
Colin Cross7113d202019-11-20 16:39:12 -08004198 // Ensure that the platform variant ends with _shared
4199 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004200}
4201
Alex Light9670d332019-01-29 18:07:33 -08004202func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004203 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004204 apex {
4205 name: "myapex",
4206 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004207 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004208 multilib: {
4209 first: {
4210 native_shared_libs: ["mylib_common"],
4211 }
4212 },
4213 target: {
4214 android: {
4215 multilib: {
4216 first: {
4217 native_shared_libs: ["mylib"],
4218 }
4219 }
4220 },
4221 host: {
4222 multilib: {
4223 first: {
4224 native_shared_libs: ["mylib2"],
4225 }
4226 }
4227 }
4228 }
4229 }
4230
4231 apex_key {
4232 name: "myapex.key",
4233 public_key: "testkey.avbpubkey",
4234 private_key: "testkey.pem",
4235 }
4236
4237 cc_library {
4238 name: "mylib",
4239 srcs: ["mylib.cpp"],
4240 system_shared_libs: [],
4241 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004242 // TODO: remove //apex_available:platform
4243 apex_available: [
4244 "//apex_available:platform",
4245 "myapex",
4246 ],
Alex Light9670d332019-01-29 18:07:33 -08004247 }
4248
4249 cc_library {
4250 name: "mylib_common",
4251 srcs: ["mylib.cpp"],
4252 system_shared_libs: [],
4253 stl: "none",
4254 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004255 // TODO: remove //apex_available:platform
4256 apex_available: [
4257 "//apex_available:platform",
4258 "myapex",
4259 ],
Alex Light9670d332019-01-29 18:07:33 -08004260 }
4261
4262 cc_library {
4263 name: "mylib2",
4264 srcs: ["mylib.cpp"],
4265 system_shared_libs: [],
4266 stl: "none",
4267 compile_multilib: "first",
4268 }
4269 `)
4270
Sundong Ahnabb64432019-10-22 13:58:29 +09004271 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004272 copyCmds := apexRule.Args["copy_commands"]
4273
4274 // Ensure that main rule creates an output
4275 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4276
4277 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004278 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4279 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4280 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004281
4282 // Ensure that both direct and indirect deps are copied into apex
4283 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4284 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4285 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4286
Colin Cross7113d202019-11-20 16:39:12 -08004287 // Ensure that the platform variant ends with _shared
4288 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4289 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4290 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004291}
Jiyong Park04480cf2019-02-06 00:16:29 +09004292
Jiyong Park59140302020-12-14 18:44:04 +09004293func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004294 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004295 apex {
4296 name: "myapex",
4297 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004298 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09004299 arch: {
4300 arm64: {
4301 native_shared_libs: ["mylib.arm64"],
4302 },
4303 x86_64: {
4304 native_shared_libs: ["mylib.x64"],
4305 },
4306 }
4307 }
4308
4309 apex_key {
4310 name: "myapex.key",
4311 public_key: "testkey.avbpubkey",
4312 private_key: "testkey.pem",
4313 }
4314
4315 cc_library {
4316 name: "mylib.arm64",
4317 srcs: ["mylib.cpp"],
4318 system_shared_libs: [],
4319 stl: "none",
4320 // TODO: remove //apex_available:platform
4321 apex_available: [
4322 "//apex_available:platform",
4323 "myapex",
4324 ],
4325 }
4326
4327 cc_library {
4328 name: "mylib.x64",
4329 srcs: ["mylib.cpp"],
4330 system_shared_libs: [],
4331 stl: "none",
4332 // TODO: remove //apex_available:platform
4333 apex_available: [
4334 "//apex_available:platform",
4335 "myapex",
4336 ],
4337 }
4338 `)
4339
4340 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4341 copyCmds := apexRule.Args["copy_commands"]
4342
4343 // Ensure that apex variant is created for the direct dep
4344 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4345 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4346
4347 // Ensure that both direct and indirect deps are copied into apex
4348 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4349 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4350}
4351
Jiyong Park04480cf2019-02-06 00:16:29 +09004352func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004353 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004354 apex {
4355 name: "myapex",
4356 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004357 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004358 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004359 }
4360
4361 apex_key {
4362 name: "myapex.key",
4363 public_key: "testkey.avbpubkey",
4364 private_key: "testkey.pem",
4365 }
4366
4367 sh_binary {
4368 name: "myscript",
4369 src: "mylib.cpp",
4370 filename: "myscript.sh",
4371 sub_dir: "script",
4372 }
4373 `)
4374
Sundong Ahnabb64432019-10-22 13:58:29 +09004375 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004376 copyCmds := apexRule.Args["copy_commands"]
4377
4378 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4379}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004380
Jooyung Han91df2082019-11-20 01:49:42 +09004381func TestApexInVariousPartition(t *testing.T) {
4382 testcases := []struct {
4383 propName, parition, flattenedPartition string
4384 }{
4385 {"", "system", "system_ext"},
4386 {"product_specific: true", "product", "product"},
4387 {"soc_specific: true", "vendor", "vendor"},
4388 {"proprietary: true", "vendor", "vendor"},
4389 {"vendor: true", "vendor", "vendor"},
4390 {"system_ext_specific: true", "system_ext", "system_ext"},
4391 }
4392 for _, tc := range testcases {
4393 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004394 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004395 apex {
4396 name: "myapex",
4397 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004398 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004399 `+tc.propName+`
4400 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004401
Jooyung Han91df2082019-11-20 01:49:42 +09004402 apex_key {
4403 name: "myapex.key",
4404 public_key: "testkey.avbpubkey",
4405 private_key: "testkey.pem",
4406 }
4407 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004408
Jooyung Han91df2082019-11-20 01:49:42 +09004409 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004410 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4411 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004412 if actual != expected {
4413 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4414 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004415
Jooyung Han91df2082019-11-20 01:49:42 +09004416 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004417 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4418 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004419 if actual != expected {
4420 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4421 }
4422 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004423 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004424}
Jiyong Park67882562019-03-21 01:11:21 +09004425
Jooyung Han580eb4f2020-06-24 19:33:06 +09004426func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004427 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004428 apex {
4429 name: "myapex",
4430 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004431 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004432 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004433
Jooyung Han580eb4f2020-06-24 19:33:06 +09004434 apex_key {
4435 name: "myapex.key",
4436 public_key: "testkey.avbpubkey",
4437 private_key: "testkey.pem",
4438 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004439 `)
4440 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004441 rule := module.Output("file_contexts")
4442 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4443}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004444
Jooyung Han580eb4f2020-06-24 19:33:06 +09004445func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004446 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004447 apex {
4448 name: "myapex",
4449 key: "myapex.key",
4450 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004451 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004452 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004453
Jooyung Han580eb4f2020-06-24 19:33:06 +09004454 apex_key {
4455 name: "myapex.key",
4456 public_key: "testkey.avbpubkey",
4457 private_key: "testkey.pem",
4458 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004459 `, withFiles(map[string][]byte{
4460 "my_own_file_contexts": nil,
4461 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004462}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004463
Jooyung Han580eb4f2020-06-24 19:33:06 +09004464func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004465 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004466 apex {
4467 name: "myapex",
4468 key: "myapex.key",
4469 product_specific: true,
4470 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004471 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004472 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004473
Jooyung Han580eb4f2020-06-24 19:33:06 +09004474 apex_key {
4475 name: "myapex.key",
4476 public_key: "testkey.avbpubkey",
4477 private_key: "testkey.pem",
4478 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004479 `)
4480
Colin Cross1c460562021-02-16 17:55:47 -08004481 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004482 apex {
4483 name: "myapex",
4484 key: "myapex.key",
4485 product_specific: true,
4486 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004487 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004488 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004489
Jooyung Han580eb4f2020-06-24 19:33:06 +09004490 apex_key {
4491 name: "myapex.key",
4492 public_key: "testkey.avbpubkey",
4493 private_key: "testkey.pem",
4494 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004495 `, withFiles(map[string][]byte{
4496 "product_specific_file_contexts": nil,
4497 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004498 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4499 rule := module.Output("file_contexts")
4500 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4501}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004502
Jooyung Han580eb4f2020-06-24 19:33:06 +09004503func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004504 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004505 apex {
4506 name: "myapex",
4507 key: "myapex.key",
4508 product_specific: true,
4509 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004510 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004511 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004512
Jooyung Han580eb4f2020-06-24 19:33:06 +09004513 apex_key {
4514 name: "myapex.key",
4515 public_key: "testkey.avbpubkey",
4516 private_key: "testkey.pem",
4517 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004518
Jooyung Han580eb4f2020-06-24 19:33:06 +09004519 filegroup {
4520 name: "my-file-contexts",
4521 srcs: ["product_specific_file_contexts"],
4522 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004523 `, withFiles(map[string][]byte{
4524 "product_specific_file_contexts": nil,
4525 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004526 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4527 rule := module.Output("file_contexts")
4528 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004529}
4530
Jiyong Park67882562019-03-21 01:11:21 +09004531func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004532 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004533 apex_key {
4534 name: "myapex.key",
4535 public_key: ":my.avbpubkey",
4536 private_key: ":my.pem",
4537 product_specific: true,
4538 }
4539
4540 filegroup {
4541 name: "my.avbpubkey",
4542 srcs: ["testkey2.avbpubkey"],
4543 }
4544
4545 filegroup {
4546 name: "my.pem",
4547 srcs: ["testkey2.pem"],
4548 }
4549 `)
4550
4551 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4552 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004553 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004554 if actual_pubkey != expected_pubkey {
4555 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4556 }
4557 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004558 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004559 if actual_privkey != expected_privkey {
4560 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4561 }
4562}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004563
4564func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004565 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004566 prebuilt_apex {
4567 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004568 arch: {
4569 arm64: {
4570 src: "myapex-arm64.apex",
4571 },
4572 arm: {
4573 src: "myapex-arm.apex",
4574 },
4575 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004576 }
4577 `)
4578
Wei Li340ee8e2022-03-18 17:33:24 -07004579 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4580 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004581
Jiyong Parkc95714e2019-03-29 14:23:10 +09004582 expectedInput := "myapex-arm64.apex"
4583 if prebuilt.inputApex.String() != expectedInput {
4584 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4585 }
Wei Li340ee8e2022-03-18 17:33:24 -07004586 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4587 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4588 rule := testingModule.Rule("genProvenanceMetaData")
4589 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4590 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4591 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4592 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004593}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004594
Paul Duffinc0609c62021-03-01 17:27:16 +00004595func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004596 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004597 prebuilt_apex {
4598 name: "myapex",
4599 }
4600 `)
4601}
4602
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004603func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004604 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004605 prebuilt_apex {
4606 name: "myapex",
4607 src: "myapex-arm.apex",
4608 filename: "notmyapex.apex",
4609 }
4610 `)
4611
Wei Li340ee8e2022-03-18 17:33:24 -07004612 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4613 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004614
4615 expected := "notmyapex.apex"
4616 if p.installFilename != expected {
4617 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4618 }
Wei Li340ee8e2022-03-18 17:33:24 -07004619 rule := testingModule.Rule("genProvenanceMetaData")
4620 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4621 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4622 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4623 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004624}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004625
Samiul Islam7c02e262021-09-08 17:48:28 +01004626func TestApexSetFilenameOverride(t *testing.T) {
4627 testApex(t, `
4628 apex_set {
4629 name: "com.company.android.myapex",
4630 apex_name: "com.android.myapex",
4631 set: "company-myapex.apks",
4632 filename: "com.company.android.myapex.apex"
4633 }
4634 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4635
4636 testApex(t, `
4637 apex_set {
4638 name: "com.company.android.myapex",
4639 apex_name: "com.android.myapex",
4640 set: "company-myapex.apks",
4641 filename: "com.company.android.myapex.capex"
4642 }
4643 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4644
4645 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4646 apex_set {
4647 name: "com.company.android.myapex",
4648 apex_name: "com.android.myapex",
4649 set: "company-myapex.apks",
4650 filename: "some-random-suffix"
4651 }
4652 `)
4653}
4654
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004655func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004656 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004657 prebuilt_apex {
4658 name: "myapex.prebuilt",
4659 src: "myapex-arm.apex",
4660 overrides: [
4661 "myapex",
4662 ],
4663 }
4664 `)
4665
Wei Li340ee8e2022-03-18 17:33:24 -07004666 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4667 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004668
4669 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004670 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004671 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004672 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004673 }
Wei Li340ee8e2022-03-18 17:33:24 -07004674 rule := testingModule.Rule("genProvenanceMetaData")
4675 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4676 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4677 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4678 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004679}
4680
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004681func TestPrebuiltApexName(t *testing.T) {
4682 testApex(t, `
4683 prebuilt_apex {
4684 name: "com.company.android.myapex",
4685 apex_name: "com.android.myapex",
4686 src: "company-myapex-arm.apex",
4687 }
4688 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4689
4690 testApex(t, `
4691 apex_set {
4692 name: "com.company.android.myapex",
4693 apex_name: "com.android.myapex",
4694 set: "company-myapex.apks",
4695 }
4696 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4697}
4698
4699func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4700 _ = android.GroupFixturePreparers(
4701 java.PrepareForTestWithJavaDefaultModules,
4702 PrepareForTestWithApexBuildComponents,
4703 android.FixtureWithRootAndroidBp(`
4704 platform_bootclasspath {
4705 name: "platform-bootclasspath",
4706 fragments: [
4707 {
4708 apex: "com.android.art",
4709 module: "art-bootclasspath-fragment",
4710 },
4711 ],
4712 }
4713
4714 prebuilt_apex {
4715 name: "com.company.android.art",
4716 apex_name: "com.android.art",
4717 src: "com.company.android.art-arm.apex",
4718 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4719 }
4720
4721 prebuilt_bootclasspath_fragment {
4722 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004723 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004724 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004725 hidden_api: {
4726 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4727 metadata: "my-bootclasspath-fragment/metadata.csv",
4728 index: "my-bootclasspath-fragment/index.csv",
4729 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4730 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4731 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004732 }
4733
4734 java_import {
4735 name: "core-oj",
4736 jars: ["prebuilt.jar"],
4737 }
4738 `),
4739 ).RunTest(t)
4740}
4741
Paul Duffin092153d2021-01-26 11:42:39 +00004742// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4743// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004744func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004745 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004746
Paul Duffin89886cb2021-02-05 16:44:03 +00004747 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004748 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004749 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004750 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004751 dexJarBuildPath := p.DexJarBuildPath().PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004752 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004753 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
4754 ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
4755 android.NormalizePathForTesting(dexJarBuildPath))
4756 }
4757
4758 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004759 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004760 // Make sure the import has been given the correct path to the dex jar.
4761 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4762 dexJarBuildPath := p.DexJarInstallPath()
4763 stem := android.RemoveOptionalPrebuiltPrefix(name)
4764 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
4765 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
4766 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00004767 }
4768
Paul Duffin39853512021-02-26 11:09:39 +00004769 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004770 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004771 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09004772 android.AssertArrayString(t, "Check if there is no source variant",
4773 []string{"android_common"},
4774 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00004775 }
4776
4777 t.Run("prebuilt only", func(t *testing.T) {
4778 bp := `
4779 prebuilt_apex {
4780 name: "myapex",
4781 arch: {
4782 arm64: {
4783 src: "myapex-arm64.apex",
4784 },
4785 arm: {
4786 src: "myapex-arm.apex",
4787 },
4788 },
Paul Duffin39853512021-02-26 11:09:39 +00004789 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004790 }
4791
4792 java_import {
4793 name: "libfoo",
4794 jars: ["libfoo.jar"],
4795 }
Paul Duffin39853512021-02-26 11:09:39 +00004796
4797 java_sdk_library_import {
4798 name: "libbar",
4799 public: {
4800 jars: ["libbar.jar"],
4801 },
4802 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004803 `
4804
4805 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4806 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4807
Martin Stjernholm44825602021-09-17 01:44:12 +01004808 deapexerName := deapexerModuleName("myapex")
4809 android.AssertStringEquals(t, "APEX module name from deapexer name", "myapex", apexModuleName(deapexerName))
4810
Paul Duffinf6932af2021-02-26 18:21:56 +00004811 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01004812 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00004813 rule := deapexer.Rule("deapexer")
4814 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4815 t.Errorf("expected: %q, found: %q", expected, actual)
4816 }
4817
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004818 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01004819 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004820 rule = prebuiltApex.Rule("android/soong/android.Cp")
4821 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4822 t.Errorf("expected: %q, found: %q", expected, actual)
4823 }
4824
Paul Duffin89886cb2021-02-05 16:44:03 +00004825 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004826 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004827
4828 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004829 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004830 })
4831
4832 t.Run("prebuilt with source preferred", func(t *testing.T) {
4833
4834 bp := `
4835 prebuilt_apex {
4836 name: "myapex",
4837 arch: {
4838 arm64: {
4839 src: "myapex-arm64.apex",
4840 },
4841 arm: {
4842 src: "myapex-arm.apex",
4843 },
4844 },
Paul Duffin39853512021-02-26 11:09:39 +00004845 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004846 }
4847
4848 java_import {
4849 name: "libfoo",
4850 jars: ["libfoo.jar"],
4851 }
4852
4853 java_library {
4854 name: "libfoo",
4855 }
Paul Duffin39853512021-02-26 11:09:39 +00004856
4857 java_sdk_library_import {
4858 name: "libbar",
4859 public: {
4860 jars: ["libbar.jar"],
4861 },
4862 }
4863
4864 java_sdk_library {
4865 name: "libbar",
4866 srcs: ["foo/bar/MyClass.java"],
4867 unsafe_ignore_missing_latest_api: true,
4868 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004869 `
4870
4871 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4872 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4873
Paul Duffin89886cb2021-02-05 16:44:03 +00004874 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004875 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004876 ensureNoSourceVariant(t, ctx, "libfoo")
4877
4878 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004879 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00004880 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004881 })
4882
4883 t.Run("prebuilt preferred with source", func(t *testing.T) {
4884 bp := `
4885 prebuilt_apex {
4886 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004887 arch: {
4888 arm64: {
4889 src: "myapex-arm64.apex",
4890 },
4891 arm: {
4892 src: "myapex-arm.apex",
4893 },
4894 },
Paul Duffin39853512021-02-26 11:09:39 +00004895 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004896 }
4897
4898 java_import {
4899 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004900 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004901 jars: ["libfoo.jar"],
4902 }
4903
4904 java_library {
4905 name: "libfoo",
4906 }
Paul Duffin39853512021-02-26 11:09:39 +00004907
4908 java_sdk_library_import {
4909 name: "libbar",
4910 prefer: true,
4911 public: {
4912 jars: ["libbar.jar"],
4913 },
4914 }
4915
4916 java_sdk_library {
4917 name: "libbar",
4918 srcs: ["foo/bar/MyClass.java"],
4919 unsafe_ignore_missing_latest_api: true,
4920 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004921 `
4922
4923 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4924 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4925
Paul Duffin89886cb2021-02-05 16:44:03 +00004926 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004927 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004928 ensureNoSourceVariant(t, ctx, "libfoo")
4929
4930 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004931 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00004932 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004933 })
4934}
4935
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004936func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01004937 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01004938 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01004939 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
4940 // is disabled.
4941 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
4942 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004943
Paul Duffin37856732021-02-26 14:24:15 +00004944 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4945 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01004946 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004947 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004948 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004949 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004950 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004951 foundLibfooJar = true
4952 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01004953 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004954 }
4955 }
4956 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02004957 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 +00004958 }
4959 }
4960
Paul Duffin40a3f652021-07-19 13:11:24 +01004961 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004962 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01004963 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01004964 var rule android.TestingBuildParams
4965
4966 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
4967 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00004968 }
4969
Paul Duffin40a3f652021-07-19 13:11:24 +01004970 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
4971 t.Helper()
4972 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
4973 var rule android.TestingBuildParams
4974
4975 rule = platformBootclasspath.Output("hiddenapi-index.csv")
4976 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
4977 }
4978
Paul Duffin89f570a2021-06-16 01:42:33 +01004979 fragment := java.ApexVariantReference{
4980 Apex: proptools.StringPtr("myapex"),
4981 Module: proptools.StringPtr("my-bootclasspath-fragment"),
4982 }
4983
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004984 t.Run("prebuilt only", func(t *testing.T) {
4985 bp := `
4986 prebuilt_apex {
4987 name: "myapex",
4988 arch: {
4989 arm64: {
4990 src: "myapex-arm64.apex",
4991 },
4992 arm: {
4993 src: "myapex-arm.apex",
4994 },
4995 },
Paul Duffin89f570a2021-06-16 01:42:33 +01004996 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4997 }
4998
4999 prebuilt_bootclasspath_fragment {
5000 name: "my-bootclasspath-fragment",
5001 contents: ["libfoo", "libbar"],
5002 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005003 hidden_api: {
5004 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5005 metadata: "my-bootclasspath-fragment/metadata.csv",
5006 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005007 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5008 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5009 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005010 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005011 }
5012
5013 java_import {
5014 name: "libfoo",
5015 jars: ["libfoo.jar"],
5016 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005017 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005018 }
Paul Duffin37856732021-02-26 14:24:15 +00005019
5020 java_sdk_library_import {
5021 name: "libbar",
5022 public: {
5023 jars: ["libbar.jar"],
5024 },
5025 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005026 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005027 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005028 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005029 `
5030
Paul Duffin89f570a2021-06-16 01:42:33 +01005031 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005032 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5033 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005034
Paul Duffin537ea3d2021-05-14 10:38:00 +01005035 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005036 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005037 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005038 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005039 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5040 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005041 })
5042
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005043 t.Run("apex_set only", func(t *testing.T) {
5044 bp := `
5045 apex_set {
5046 name: "myapex",
5047 set: "myapex.apks",
Paul Duffin89f570a2021-06-16 01:42:33 +01005048 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5049 }
5050
5051 prebuilt_bootclasspath_fragment {
5052 name: "my-bootclasspath-fragment",
5053 contents: ["libfoo", "libbar"],
5054 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005055 hidden_api: {
5056 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5057 metadata: "my-bootclasspath-fragment/metadata.csv",
5058 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005059 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5060 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5061 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005062 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005063 }
5064
5065 java_import {
5066 name: "libfoo",
5067 jars: ["libfoo.jar"],
5068 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005069 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005070 }
5071
5072 java_sdk_library_import {
5073 name: "libbar",
5074 public: {
5075 jars: ["libbar.jar"],
5076 },
5077 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005078 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005079 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005080 }
5081 `
5082
Paul Duffin89f570a2021-06-16 01:42:33 +01005083 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005084 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5085 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
5086
Paul Duffin537ea3d2021-05-14 10:38:00 +01005087 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005088 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005089 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005090 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005091 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5092 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005093 })
5094
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005095 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5096 bp := `
5097 prebuilt_apex {
5098 name: "myapex",
5099 arch: {
5100 arm64: {
5101 src: "myapex-arm64.apex",
5102 },
5103 arm: {
5104 src: "myapex-arm.apex",
5105 },
5106 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005107 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5108 }
5109
5110 prebuilt_bootclasspath_fragment {
5111 name: "my-bootclasspath-fragment",
5112 contents: ["libfoo", "libbar"],
5113 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005114 hidden_api: {
5115 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5116 metadata: "my-bootclasspath-fragment/metadata.csv",
5117 index: "my-bootclasspath-fragment/index.csv",
5118 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5119 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5120 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005121 }
5122
5123 java_import {
5124 name: "libfoo",
5125 jars: ["libfoo.jar"],
5126 apex_available: ["myapex"],
5127 }
5128
5129 java_library {
5130 name: "libfoo",
5131 srcs: ["foo/bar/MyClass.java"],
5132 apex_available: ["myapex"],
5133 }
Paul Duffin37856732021-02-26 14:24:15 +00005134
5135 java_sdk_library_import {
5136 name: "libbar",
5137 public: {
5138 jars: ["libbar.jar"],
5139 },
5140 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005141 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005142 }
5143
5144 java_sdk_library {
5145 name: "libbar",
5146 srcs: ["foo/bar/MyClass.java"],
5147 unsafe_ignore_missing_latest_api: true,
5148 apex_available: ["myapex"],
5149 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005150 `
5151
5152 // In this test the source (java_library) libfoo is active since the
5153 // prebuilt (java_import) defaults to prefer:false. However the
5154 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5155 // find the dex boot jar in it. We either need to disable the source libfoo
5156 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005157 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005158 // dexbootjar check is skipped if AllowMissingDependencies is true
5159 preparerAllowMissingDeps := android.GroupFixturePreparers(
5160 preparer,
5161 android.PrepareForTestWithAllowMissingDependencies,
5162 )
5163 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005164 })
5165
5166 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5167 bp := `
5168 prebuilt_apex {
5169 name: "myapex",
5170 arch: {
5171 arm64: {
5172 src: "myapex-arm64.apex",
5173 },
5174 arm: {
5175 src: "myapex-arm.apex",
5176 },
5177 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005178 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5179 }
5180
5181 prebuilt_bootclasspath_fragment {
5182 name: "my-bootclasspath-fragment",
5183 contents: ["libfoo", "libbar"],
5184 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005185 hidden_api: {
5186 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5187 metadata: "my-bootclasspath-fragment/metadata.csv",
5188 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005189 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5190 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5191 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005192 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005193 }
5194
5195 java_import {
5196 name: "libfoo",
5197 prefer: true,
5198 jars: ["libfoo.jar"],
5199 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005200 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005201 }
5202
5203 java_library {
5204 name: "libfoo",
5205 srcs: ["foo/bar/MyClass.java"],
5206 apex_available: ["myapex"],
5207 }
Paul Duffin37856732021-02-26 14:24:15 +00005208
5209 java_sdk_library_import {
5210 name: "libbar",
5211 prefer: true,
5212 public: {
5213 jars: ["libbar.jar"],
5214 },
5215 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005216 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005217 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005218 }
5219
5220 java_sdk_library {
5221 name: "libbar",
5222 srcs: ["foo/bar/MyClass.java"],
5223 unsafe_ignore_missing_latest_api: true,
5224 apex_available: ["myapex"],
5225 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005226 `
5227
Paul Duffin89f570a2021-06-16 01:42:33 +01005228 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005229 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5230 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005231
Paul Duffin537ea3d2021-05-14 10:38:00 +01005232 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005233 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005234 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005235 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005236 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5237 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005238 })
5239
5240 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5241 bp := `
5242 apex {
5243 name: "myapex",
5244 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00005245 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005246 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005247 }
5248
5249 apex_key {
5250 name: "myapex.key",
5251 public_key: "testkey.avbpubkey",
5252 private_key: "testkey.pem",
5253 }
5254
5255 prebuilt_apex {
5256 name: "myapex",
5257 arch: {
5258 arm64: {
5259 src: "myapex-arm64.apex",
5260 },
5261 arm: {
5262 src: "myapex-arm.apex",
5263 },
5264 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005265 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5266 }
5267
5268 prebuilt_bootclasspath_fragment {
5269 name: "my-bootclasspath-fragment",
5270 contents: ["libfoo", "libbar"],
5271 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005272 hidden_api: {
5273 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5274 metadata: "my-bootclasspath-fragment/metadata.csv",
5275 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005276 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5277 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5278 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005279 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005280 }
5281
5282 java_import {
5283 name: "libfoo",
5284 jars: ["libfoo.jar"],
5285 apex_available: ["myapex"],
5286 }
5287
5288 java_library {
5289 name: "libfoo",
5290 srcs: ["foo/bar/MyClass.java"],
5291 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005292 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005293 }
Paul Duffin37856732021-02-26 14:24:15 +00005294
5295 java_sdk_library_import {
5296 name: "libbar",
5297 public: {
5298 jars: ["libbar.jar"],
5299 },
5300 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005301 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005302 }
5303
5304 java_sdk_library {
5305 name: "libbar",
5306 srcs: ["foo/bar/MyClass.java"],
5307 unsafe_ignore_missing_latest_api: true,
5308 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005309 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005310 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005311 `
5312
Paul Duffin89f570a2021-06-16 01:42:33 +01005313 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005314 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
5315 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005316
Paul Duffin537ea3d2021-05-14 10:38:00 +01005317 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005318 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005319 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005320 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005321 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5322 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005323 })
5324
5325 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5326 bp := `
5327 apex {
5328 name: "myapex",
5329 enabled: false,
5330 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01005331 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005332 }
5333
5334 apex_key {
5335 name: "myapex.key",
5336 public_key: "testkey.avbpubkey",
5337 private_key: "testkey.pem",
5338 }
5339
5340 prebuilt_apex {
5341 name: "myapex",
5342 arch: {
5343 arm64: {
5344 src: "myapex-arm64.apex",
5345 },
5346 arm: {
5347 src: "myapex-arm.apex",
5348 },
5349 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005350 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5351 }
5352
5353 prebuilt_bootclasspath_fragment {
5354 name: "my-bootclasspath-fragment",
5355 contents: ["libfoo", "libbar"],
5356 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005357 hidden_api: {
5358 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5359 metadata: "my-bootclasspath-fragment/metadata.csv",
5360 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005361 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5362 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5363 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005364 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005365 }
5366
5367 java_import {
5368 name: "libfoo",
5369 prefer: true,
5370 jars: ["libfoo.jar"],
5371 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005372 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005373 }
5374
5375 java_library {
5376 name: "libfoo",
5377 srcs: ["foo/bar/MyClass.java"],
5378 apex_available: ["myapex"],
5379 }
Paul Duffin37856732021-02-26 14:24:15 +00005380
5381 java_sdk_library_import {
5382 name: "libbar",
5383 prefer: true,
5384 public: {
5385 jars: ["libbar.jar"],
5386 },
5387 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005388 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005389 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005390 }
5391
5392 java_sdk_library {
5393 name: "libbar",
5394 srcs: ["foo/bar/MyClass.java"],
5395 unsafe_ignore_missing_latest_api: true,
5396 apex_available: ["myapex"],
5397 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005398 `
5399
Paul Duffin89f570a2021-06-16 01:42:33 +01005400 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005401 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5402 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005403
Paul Duffin537ea3d2021-05-14 10:38:00 +01005404 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005405 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005406 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005407 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005408 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5409 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005410 })
5411}
5412
Roland Levillain630846d2019-06-26 12:48:34 +01005413func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005414 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005415 apex_test {
5416 name: "myapex",
5417 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005418 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005419 tests: [
5420 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005421 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005422 ],
5423 }
5424
5425 apex_key {
5426 name: "myapex.key",
5427 public_key: "testkey.avbpubkey",
5428 private_key: "testkey.pem",
5429 }
5430
Liz Kammer1c14a212020-05-12 15:26:55 -07005431 filegroup {
5432 name: "fg",
5433 srcs: [
5434 "baz",
5435 "bar/baz"
5436 ],
5437 }
5438
Roland Levillain630846d2019-06-26 12:48:34 +01005439 cc_test {
5440 name: "mytest",
5441 gtest: false,
5442 srcs: ["mytest.cpp"],
5443 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005444 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005445 system_shared_libs: [],
5446 static_executable: true,
5447 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005448 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005449 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005450
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005451 cc_library {
5452 name: "mylib",
5453 srcs: ["mylib.cpp"],
5454 system_shared_libs: [],
5455 stl: "none",
5456 }
5457
Liz Kammer5bd365f2020-05-27 15:15:11 -07005458 filegroup {
5459 name: "fg2",
5460 srcs: [
5461 "testdata/baz"
5462 ],
5463 }
5464
Roland Levillain9b5fde92019-06-28 15:41:19 +01005465 cc_test {
5466 name: "mytests",
5467 gtest: false,
5468 srcs: [
5469 "mytest1.cpp",
5470 "mytest2.cpp",
5471 "mytest3.cpp",
5472 ],
5473 test_per_src: true,
5474 relative_install_path: "test",
5475 system_shared_libs: [],
5476 static_executable: true,
5477 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005478 data: [
5479 ":fg",
5480 ":fg2",
5481 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005482 }
Roland Levillain630846d2019-06-26 12:48:34 +01005483 `)
5484
Sundong Ahnabb64432019-10-22 13:58:29 +09005485 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005486 copyCmds := apexRule.Args["copy_commands"]
5487
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005488 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005489 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005490 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005491
Liz Kammer1c14a212020-05-12 15:26:55 -07005492 //Ensure that test data are copied into apex.
5493 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5494 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5495
Roland Levillain9b5fde92019-06-28 15:41:19 +01005496 // Ensure that test deps built with `test_per_src` are copied into apex.
5497 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5498 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5499 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005500
5501 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07005502 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005503 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005504 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005505 prefix := "TARGET_"
5506 var builder strings.Builder
5507 data.Custom(&builder, name, prefix, "", data)
5508 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09005509 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5510 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5511 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5512 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09005513 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09005514 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005515 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005516
5517 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005518 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005519 data.Custom(&builder, name, prefix, "", data)
5520 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005521 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5522 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005523}
5524
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005525func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005526 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005527 apex {
5528 name: "myapex",
5529 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005530 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005531 }
5532 apex_key {
5533 name: "myapex.key",
5534 public_key: "testkey.avbpubkey",
5535 private_key: "testkey.pem",
5536 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005537 `,
5538 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5539 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5540 }),
5541 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005542 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09005543 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005544 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005545 var builder strings.Builder
5546 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5547 androidMk := builder.String()
5548 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
5549}
5550
Jooyung Hand48f3c32019-08-23 11:18:57 +09005551func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5552 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5553 apex {
5554 name: "myapex",
5555 key: "myapex.key",
5556 native_shared_libs: ["libfoo"],
5557 }
5558
5559 apex_key {
5560 name: "myapex.key",
5561 public_key: "testkey.avbpubkey",
5562 private_key: "testkey.pem",
5563 }
5564
5565 cc_library {
5566 name: "libfoo",
5567 stl: "none",
5568 system_shared_libs: [],
5569 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005570 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005571 }
5572 `)
5573 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5574 apex {
5575 name: "myapex",
5576 key: "myapex.key",
5577 java_libs: ["myjar"],
5578 }
5579
5580 apex_key {
5581 name: "myapex.key",
5582 public_key: "testkey.avbpubkey",
5583 private_key: "testkey.pem",
5584 }
5585
5586 java_library {
5587 name: "myjar",
5588 srcs: ["foo/bar/MyClass.java"],
5589 sdk_version: "none",
5590 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005591 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005592 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005593 }
5594 `)
5595}
5596
Bill Peckhama41a6962021-01-11 10:58:54 -08005597func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005598 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005599 apex {
5600 name: "myapex",
5601 key: "myapex.key",
5602 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005603 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005604 }
5605
5606 apex_key {
5607 name: "myapex.key",
5608 public_key: "testkey.avbpubkey",
5609 private_key: "testkey.pem",
5610 }
5611
5612 java_import {
5613 name: "myjavaimport",
5614 apex_available: ["myapex"],
5615 jars: ["my.jar"],
5616 compile_dex: true,
5617 }
5618 `)
5619
5620 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5621 apexRule := module.Rule("apexRule")
5622 copyCmds := apexRule.Args["copy_commands"]
5623 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5624}
5625
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005626func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005627 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005628 apex {
5629 name: "myapex",
5630 key: "myapex.key",
5631 apps: [
5632 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005633 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005634 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005635 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005636 }
5637
5638 apex_key {
5639 name: "myapex.key",
5640 public_key: "testkey.avbpubkey",
5641 private_key: "testkey.pem",
5642 }
5643
5644 android_app {
5645 name: "AppFoo",
5646 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005647 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005648 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005649 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005650 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005651 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005652 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005653
5654 android_app {
5655 name: "AppFooPriv",
5656 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005657 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005658 system_modules: "none",
5659 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005660 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005661 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005662 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005663
5664 cc_library_shared {
5665 name: "libjni",
5666 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005667 shared_libs: ["libfoo"],
5668 stl: "none",
5669 system_shared_libs: [],
5670 apex_available: [ "myapex" ],
5671 sdk_version: "current",
5672 }
5673
5674 cc_library_shared {
5675 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005676 stl: "none",
5677 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005678 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005679 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005680 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005681 `)
5682
Sundong Ahnabb64432019-10-22 13:58:29 +09005683 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005684 apexRule := module.Rule("apexRule")
5685 copyCmds := apexRule.Args["copy_commands"]
5686
Oriol Prieto Gascóa70425f2022-05-20 13:05:34 +00005687 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5688 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005689
Colin Crossaede88c2020-08-11 12:17:01 -07005690 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005691 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005692 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005693 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005694 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005695 // JNI libraries including transitive deps are
5696 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005697 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005698 // ... embedded inside APK (jnilibs.zip)
5699 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5700 // ... and not directly inside the APEX
5701 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5702 }
Dario Frenicde2a032019-10-27 00:29:22 +01005703}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005704
Oriol Prieto Gascóa70425f2022-05-20 13:05:34 +00005705func TestApexWithAppImportBuildId(t *testing.T) {
5706 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
5707 for _, id := range invalidBuildIds {
5708 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
5709 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5710 variables.BuildId = proptools.StringPtr(id)
5711 })
5712 testApexError(t, message, `apex {
5713 name: "myapex",
5714 key: "myapex.key",
5715 apps: ["AppFooPrebuilt"],
5716 updatable: false,
5717 }
5718
5719 apex_key {
5720 name: "myapex.key",
5721 public_key: "testkey.avbpubkey",
5722 private_key: "testkey.pem",
5723 }
5724
5725 android_app_import {
5726 name: "AppFooPrebuilt",
5727 apk: "PrebuiltAppFoo.apk",
5728 presigned: true,
5729 apex_available: ["myapex"],
5730 }
5731 `, fixture)
5732 }
5733}
5734
Dario Frenicde2a032019-10-27 00:29:22 +01005735func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005736 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005737 apex {
5738 name: "myapex",
5739 key: "myapex.key",
5740 apps: [
5741 "AppFooPrebuilt",
5742 "AppFooPrivPrebuilt",
5743 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005744 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005745 }
5746
5747 apex_key {
5748 name: "myapex.key",
5749 public_key: "testkey.avbpubkey",
5750 private_key: "testkey.pem",
5751 }
5752
5753 android_app_import {
5754 name: "AppFooPrebuilt",
5755 apk: "PrebuiltAppFoo.apk",
5756 presigned: true,
5757 dex_preopt: {
5758 enabled: false,
5759 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005760 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005761 }
5762
5763 android_app_import {
5764 name: "AppFooPrivPrebuilt",
5765 apk: "PrebuiltAppFooPriv.apk",
5766 privileged: true,
5767 presigned: true,
5768 dex_preopt: {
5769 enabled: false,
5770 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005771 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005772 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005773 }
5774 `)
5775
Sundong Ahnabb64432019-10-22 13:58:29 +09005776 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005777 apexRule := module.Rule("apexRule")
5778 copyCmds := apexRule.Args["copy_commands"]
5779
Oriol Prieto Gascóa70425f2022-05-20 13:05:34 +00005780 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
5781 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005782}
5783
5784func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005785 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005786 apex {
5787 name: "myapex",
5788 key: "myapex.key",
5789 apps: [
5790 "AppFoo",
5791 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005792 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005793 }
5794
5795 apex_key {
5796 name: "myapex.key",
5797 public_key: "testkey.avbpubkey",
5798 private_key: "testkey.pem",
5799 }
5800
5801 android_app {
5802 name: "AppFoo",
5803 srcs: ["foo/bar/MyClass.java"],
5804 sdk_version: "none",
5805 system_modules: "none",
5806 apex_available: [ "myapex" ],
5807 }
5808
5809 android_app_import {
5810 name: "AppFoo",
5811 apk: "AppFooPrebuilt.apk",
5812 filename: "AppFooPrebuilt.apk",
5813 presigned: true,
5814 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005815 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005816 }
5817 `, withFiles(map[string][]byte{
5818 "AppFooPrebuilt.apk": nil,
5819 }))
5820
5821 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Oriol Prieto Gascóa70425f2022-05-20 13:05:34 +00005822 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005823 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005824}
5825
Dario Freni6f3937c2019-12-20 22:58:03 +00005826func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005827 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005828 apex {
5829 name: "myapex",
5830 key: "myapex.key",
5831 apps: [
5832 "TesterHelpAppFoo",
5833 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005834 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005835 }
5836
5837 apex_key {
5838 name: "myapex.key",
5839 public_key: "testkey.avbpubkey",
5840 private_key: "testkey.pem",
5841 }
5842
5843 android_test_helper_app {
5844 name: "TesterHelpAppFoo",
5845 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005846 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005847 }
5848
5849 `)
5850
5851 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5852 apexRule := module.Rule("apexRule")
5853 copyCmds := apexRule.Args["copy_commands"]
5854
Oriol Prieto Gascóa70425f2022-05-20 13:05:34 +00005855 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00005856}
5857
Jooyung Han18020ea2019-11-13 10:50:48 +09005858func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5859 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005860 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005861 apex {
5862 name: "myapex",
5863 key: "myapex.key",
5864 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005865 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005866 }
5867
5868 apex_key {
5869 name: "myapex.key",
5870 public_key: "testkey.avbpubkey",
5871 private_key: "testkey.pem",
5872 }
5873
5874 apex {
5875 name: "otherapex",
5876 key: "myapex.key",
5877 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005878 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005879 }
5880
5881 cc_defaults {
5882 name: "libfoo-defaults",
5883 apex_available: ["otherapex"],
5884 }
5885
5886 cc_library {
5887 name: "libfoo",
5888 defaults: ["libfoo-defaults"],
5889 stl: "none",
5890 system_shared_libs: [],
5891 }`)
5892}
5893
Paul Duffine52e66f2020-03-30 17:54:29 +01005894func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005895 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005896 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005897 apex {
5898 name: "myapex",
5899 key: "myapex.key",
5900 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005901 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005902 }
5903
5904 apex_key {
5905 name: "myapex.key",
5906 public_key: "testkey.avbpubkey",
5907 private_key: "testkey.pem",
5908 }
5909
5910 apex {
5911 name: "otherapex",
5912 key: "otherapex.key",
5913 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005914 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005915 }
5916
5917 apex_key {
5918 name: "otherapex.key",
5919 public_key: "testkey.avbpubkey",
5920 private_key: "testkey.pem",
5921 }
5922
5923 cc_library {
5924 name: "libfoo",
5925 stl: "none",
5926 system_shared_libs: [],
5927 apex_available: ["otherapex"],
5928 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005929}
Jiyong Park127b40b2019-09-30 16:04:35 +09005930
Paul Duffine52e66f2020-03-30 17:54:29 +01005931func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005932 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005933 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005934.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005935.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005936.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005937.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005938.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005939.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005940 apex {
5941 name: "myapex",
5942 key: "myapex.key",
5943 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005944 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005945 }
5946
5947 apex_key {
5948 name: "myapex.key",
5949 public_key: "testkey.avbpubkey",
5950 private_key: "testkey.pem",
5951 }
5952
Jiyong Park127b40b2019-09-30 16:04:35 +09005953 cc_library {
5954 name: "libfoo",
5955 stl: "none",
5956 shared_libs: ["libbar"],
5957 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005958 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005959 }
5960
5961 cc_library {
5962 name: "libbar",
5963 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005964 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005965 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005966 apex_available: ["myapex"],
5967 }
5968
5969 cc_library {
5970 name: "libbaz",
5971 stl: "none",
5972 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005973 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005974}
Jiyong Park127b40b2019-09-30 16:04:35 +09005975
Paul Duffine52e66f2020-03-30 17:54:29 +01005976func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005977 testApexError(t, "\"otherapex\" is not a valid module name", `
5978 apex {
5979 name: "myapex",
5980 key: "myapex.key",
5981 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005982 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005983 }
5984
5985 apex_key {
5986 name: "myapex.key",
5987 public_key: "testkey.avbpubkey",
5988 private_key: "testkey.pem",
5989 }
5990
5991 cc_library {
5992 name: "libfoo",
5993 stl: "none",
5994 system_shared_libs: [],
5995 apex_available: ["otherapex"],
5996 }`)
5997
Paul Duffine52e66f2020-03-30 17:54:29 +01005998 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005999 apex {
6000 name: "myapex",
6001 key: "myapex.key",
6002 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006003 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006004 }
6005
6006 apex_key {
6007 name: "myapex.key",
6008 public_key: "testkey.avbpubkey",
6009 private_key: "testkey.pem",
6010 }
6011
6012 cc_library {
6013 name: "libfoo",
6014 stl: "none",
6015 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006016 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006017 apex_available: ["myapex"],
6018 }
6019
6020 cc_library {
6021 name: "libbar",
6022 stl: "none",
6023 system_shared_libs: [],
6024 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006025 }
6026
6027 cc_library {
6028 name: "libbaz",
6029 stl: "none",
6030 system_shared_libs: [],
6031 stubs: {
6032 versions: ["10", "20", "30"],
6033 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006034 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006035}
Jiyong Park127b40b2019-09-30 16:04:35 +09006036
Jiyong Park89e850a2020-04-07 16:37:39 +09006037func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006038 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006039 apex {
6040 name: "myapex",
6041 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006042 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006043 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006044 }
6045
6046 apex_key {
6047 name: "myapex.key",
6048 public_key: "testkey.avbpubkey",
6049 private_key: "testkey.pem",
6050 }
6051
6052 cc_library {
6053 name: "libfoo",
6054 stl: "none",
6055 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006056 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006057 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006058 }
6059
6060 cc_library {
6061 name: "libfoo2",
6062 stl: "none",
6063 system_shared_libs: [],
6064 shared_libs: ["libbaz"],
6065 apex_available: ["//apex_available:platform"],
6066 }
6067
6068 cc_library {
6069 name: "libbar",
6070 stl: "none",
6071 system_shared_libs: [],
6072 apex_available: ["myapex"],
6073 }
6074
6075 cc_library {
6076 name: "libbaz",
6077 stl: "none",
6078 system_shared_libs: [],
6079 apex_available: ["myapex"],
6080 stubs: {
6081 versions: ["1"],
6082 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006083 }`)
6084
Jiyong Park89e850a2020-04-07 16:37:39 +09006085 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6086 // because it depends on libbar which isn't available to platform
6087 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6088 if libfoo.NotAvailableForPlatform() != true {
6089 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6090 }
6091
6092 // libfoo2 however can be available to platform because it depends on libbaz which provides
6093 // stubs
6094 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6095 if libfoo2.NotAvailableForPlatform() == true {
6096 t.Errorf("%q should be available to platform", libfoo2.String())
6097 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006098}
Jiyong Parka90ca002019-10-07 15:47:24 +09006099
Paul Duffine52e66f2020-03-30 17:54:29 +01006100func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006101 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006102 apex {
6103 name: "myapex",
6104 key: "myapex.key",
6105 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006106 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006107 }
6108
6109 apex_key {
6110 name: "myapex.key",
6111 public_key: "testkey.avbpubkey",
6112 private_key: "testkey.pem",
6113 }
6114
6115 cc_library {
6116 name: "libfoo",
6117 stl: "none",
6118 system_shared_libs: [],
6119 apex_available: ["myapex"],
6120 static: {
6121 apex_available: ["//apex_available:platform"],
6122 },
6123 }`)
6124
Jiyong Park89e850a2020-04-07 16:37:39 +09006125 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6126 if libfooShared.NotAvailableForPlatform() != true {
6127 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6128 }
6129 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6130 if libfooStatic.NotAvailableForPlatform() != false {
6131 t.Errorf("%q should be available to platform", libfooStatic.String())
6132 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006133}
6134
Jiyong Park5d790c32019-11-15 18:40:32 +09006135func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006136 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006137 apex {
6138 name: "myapex",
6139 key: "myapex.key",
6140 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006141 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006142 prebuilts: ["myetc"],
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006143 bootclasspath_fragments: ["mybootclasspath_fragment"],
6144 systemserverclasspath_fragments: ["mysystemserverclasspath_fragment"],
6145 java_libs: ["myjava_library"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006146 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006147 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006148 }
6149
6150 override_apex {
6151 name: "override_myapex",
6152 base: "myapex",
6153 apps: ["override_app"],
markchien7c803b82021-08-26 22:10:06 +08006154 bpfs: ["override_bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006155 prebuilts: ["override_myetc"],
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006156 bootclasspath_fragments: ["override_bootclasspath_fragment"],
6157 systemserverclasspath_fragments: ["override_systemserverclasspath_fragment"],
6158 java_libs: ["override_java_library"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006159 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08006160 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006161 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006162 key: "mynewapex.key",
6163 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006164 }
6165
6166 apex_key {
6167 name: "myapex.key",
6168 public_key: "testkey.avbpubkey",
6169 private_key: "testkey.pem",
6170 }
6171
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006172 apex_key {
6173 name: "mynewapex.key",
6174 public_key: "testkey2.avbpubkey",
6175 private_key: "testkey2.pem",
6176 }
6177
6178 android_app_certificate {
6179 name: "myapex.certificate",
6180 certificate: "testkey",
6181 }
6182
Jiyong Park5d790c32019-11-15 18:40:32 +09006183 android_app {
6184 name: "app",
6185 srcs: ["foo/bar/MyClass.java"],
6186 package_name: "foo",
6187 sdk_version: "none",
6188 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006189 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006190 }
6191
6192 override_android_app {
6193 name: "override_app",
6194 base: "app",
6195 package_name: "bar",
6196 }
markchien7c803b82021-08-26 22:10:06 +08006197
6198 bpf {
6199 name: "bpf",
6200 srcs: ["bpf.c"],
6201 }
6202
6203 bpf {
6204 name: "override_bpf",
6205 srcs: ["override_bpf.c"],
6206 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006207
6208 prebuilt_etc {
6209 name: "myetc",
6210 src: "myprebuilt",
6211 }
6212
6213 prebuilt_etc {
6214 name: "override_myetc",
6215 src: "override_myprebuilt",
6216 }
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006217
6218 java_library {
6219 name: "bcplib",
6220 srcs: ["a.java"],
6221 compile_dex: true,
6222 apex_available: ["myapex"],
6223 permitted_packages: ["bcp.lib"],
6224 }
6225
6226 bootclasspath_fragment {
6227 name: "mybootclasspath_fragment",
6228 contents: ["bcplib"],
6229 apex_available: ["myapex"],
6230 }
6231
6232 java_library {
6233 name: "override_bcplib",
6234 srcs: ["a.java"],
6235 compile_dex: true,
6236 apex_available: ["myapex"],
6237 permitted_packages: ["override.bcp.lib"],
6238 }
6239
6240 bootclasspath_fragment {
6241 name: "override_bootclasspath_fragment",
6242 contents: ["override_bcplib"],
6243 apex_available: ["myapex"],
6244 }
6245
6246 java_library {
6247 name: "systemserverlib",
6248 srcs: ["a.java"],
6249 apex_available: ["myapex"],
6250 }
6251
6252 systemserverclasspath_fragment {
6253 name: "mysystemserverclasspath_fragment",
6254 standalone_contents: ["systemserverlib"],
6255 apex_available: ["myapex"],
6256 }
6257
6258 java_library {
6259 name: "override_systemserverlib",
6260 srcs: ["a.java"],
6261 apex_available: ["myapex"],
6262 }
6263
6264 systemserverclasspath_fragment {
6265 name: "override_systemserverclasspath_fragment",
6266 standalone_contents: ["override_systemserverlib"],
6267 apex_available: ["myapex"],
6268 }
6269
6270 java_library {
6271 name: "myjava_library",
6272 srcs: ["a.java"],
6273 compile_dex: true,
6274 apex_available: ["myapex"],
6275 }
6276
6277 java_library {
6278 name: "override_java_library",
6279 srcs: ["a.java"],
6280 compile_dex: true,
6281 apex_available: ["myapex"],
6282 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006283 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006284
Jiyong Park317645e2019-12-05 13:20:58 +09006285 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
6286 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
6287 if originalVariant.GetOverriddenBy() != "" {
6288 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6289 }
6290 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6291 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6292 }
6293
Jiyong Park5d790c32019-11-15 18:40:32 +09006294 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
6295 apexRule := module.Rule("apexRule")
6296 copyCmds := apexRule.Args["copy_commands"]
6297
Oriol Prieto Gascóa70425f2022-05-20 13:05:34 +00006298 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
6299 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006300
markchien7c803b82021-08-26 22:10:06 +08006301 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
6302 ensureContains(t, copyCmds, "image.apex/etc/bpf/override_bpf.o")
6303
Daniel Norman5a3ce132021-08-26 15:44:43 -07006304 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6305 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6306
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006307 apexBundle := module.Module().(*apexBundle)
6308 name := apexBundle.Name()
6309 if name != "override_myapex" {
6310 t.Errorf("name should be \"override_myapex\", but was %q", name)
6311 }
6312
Baligh Uddin004d7172020-02-19 21:29:28 -08006313 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6314 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6315 }
6316
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006317 android.AssertArrayString(t, "Bootclasspath_fragments does not match",
6318 []string{"override_bootclasspath_fragment"}, apexBundle.overridableProperties.Bootclasspath_fragments)
6319 android.AssertArrayString(t, "Systemserverclasspath_fragments does not match",
6320 []string{"override_systemserverclasspath_fragment"}, apexBundle.overridableProperties.Systemserverclasspath_fragments)
6321 android.AssertArrayString(t, "Java_libs does not match",
6322 []string{"override_java_library"}, apexBundle.overridableProperties.Java_libs)
6323
Jiyong Park20bacab2020-03-03 11:45:41 +09006324 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006325 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006326 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6327
6328 signApkRule := module.Rule("signapk")
6329 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006330
Colin Crossaa255532020-07-03 13:18:24 -07006331 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006332 var builder strings.Builder
6333 data.Custom(&builder, name, "TARGET_", "", data)
6334 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09006335 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
markchien7c803b82021-08-26 22:10:06 +08006336 ensureContains(t, androidMk, "LOCAL_MODULE := override_bpf.o.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006337 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006338 ensureContains(t, androidMk, "LOCAL_MODULE := override_bcplib.override_myapex")
6339 ensureContains(t, androidMk, "LOCAL_MODULE := override_systemserverlib.override_myapex")
6340 ensureContains(t, androidMk, "LOCAL_MODULE := override_java_library.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006341 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006342 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006343 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006344 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006345 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006346 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006347 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_bcplib.myapex")
6348 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_systemserverlib.myapex")
6349 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_java_library.pb.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006350 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006351}
6352
Albert Martin55ccba22022-03-21 20:11:16 +00006353func TestMinSdkVersionOverride(t *testing.T) {
6354 // Override from 29 to 31
6355 minSdkOverride31 := "31"
6356 ctx := testApex(t, `
6357 apex {
6358 name: "myapex",
6359 key: "myapex.key",
6360 native_shared_libs: ["mylib"],
6361 updatable: true,
6362 min_sdk_version: "29"
6363 }
6364
6365 override_apex {
6366 name: "override_myapex",
6367 base: "myapex",
6368 logging_parent: "com.foo.bar",
6369 package_name: "test.overridden.package"
6370 }
6371
6372 apex_key {
6373 name: "myapex.key",
6374 public_key: "testkey.avbpubkey",
6375 private_key: "testkey.pem",
6376 }
6377
6378 cc_library {
6379 name: "mylib",
6380 srcs: ["mylib.cpp"],
6381 runtime_libs: ["libbar"],
6382 system_shared_libs: [],
6383 stl: "none",
6384 apex_available: [ "myapex" ],
6385 min_sdk_version: "apex_inherit"
6386 }
6387
6388 cc_library {
6389 name: "libbar",
6390 srcs: ["mylib.cpp"],
6391 system_shared_libs: [],
6392 stl: "none",
6393 apex_available: [ "myapex" ],
6394 min_sdk_version: "apex_inherit"
6395 }
6396
6397 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6398
6399 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6400 copyCmds := apexRule.Args["copy_commands"]
6401
6402 // Ensure that direct non-stubs dep is always included
6403 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6404
6405 // Ensure that runtime_libs dep in included
6406 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6407
6408 // Ensure libraries target overridden min_sdk_version value
6409 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6410}
6411
6412func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6413 // Attempt to override from 31 to 29, should be a NOOP
6414 minSdkOverride29 := "29"
6415 ctx := testApex(t, `
6416 apex {
6417 name: "myapex",
6418 key: "myapex.key",
6419 native_shared_libs: ["mylib"],
6420 updatable: true,
6421 min_sdk_version: "31"
6422 }
6423
6424 override_apex {
6425 name: "override_myapex",
6426 base: "myapex",
6427 logging_parent: "com.foo.bar",
6428 package_name: "test.overridden.package"
6429 }
6430
6431 apex_key {
6432 name: "myapex.key",
6433 public_key: "testkey.avbpubkey",
6434 private_key: "testkey.pem",
6435 }
6436
6437 cc_library {
6438 name: "mylib",
6439 srcs: ["mylib.cpp"],
6440 runtime_libs: ["libbar"],
6441 system_shared_libs: [],
6442 stl: "none",
6443 apex_available: [ "myapex" ],
6444 min_sdk_version: "apex_inherit"
6445 }
6446
6447 cc_library {
6448 name: "libbar",
6449 srcs: ["mylib.cpp"],
6450 system_shared_libs: [],
6451 stl: "none",
6452 apex_available: [ "myapex" ],
6453 min_sdk_version: "apex_inherit"
6454 }
6455
6456 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
6457
6458 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6459 copyCmds := apexRule.Args["copy_commands"]
6460
6461 // Ensure that direct non-stubs dep is always included
6462 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6463
6464 // Ensure that runtime_libs dep in included
6465 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6466
6467 // Ensure libraries target the original min_sdk_version value rather than the overridden
6468 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6469}
6470
Jooyung Han214bf372019-11-12 13:03:50 +09006471func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006472 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09006473 apex {
6474 name: "myapex",
6475 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006476 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09006477 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09006478 }
6479
6480 apex_key {
6481 name: "myapex.key",
6482 public_key: "testkey.avbpubkey",
6483 private_key: "testkey.pem",
6484 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006485
6486 cc_library {
6487 name: "mylib",
6488 srcs: ["mylib.cpp"],
6489 stl: "libc++",
6490 system_shared_libs: [],
6491 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09006492 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006493 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006494 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09006495
6496 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6497 args := module.Rule("apexRule").Args
6498 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00006499 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006500
6501 // The copies of the libraries in the apex should have one more dependency than
6502 // the ones outside the apex, namely the unwinder. Ideally we should check
6503 // the dependency names directly here but for some reason the names are blank in
6504 // this test.
6505 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07006506 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006507 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
6508 if len(apexImplicits) != len(nonApexImplicits)+1 {
6509 t.Errorf("%q missing unwinder dep", lib)
6510 }
6511 }
Jooyung Han214bf372019-11-12 13:03:50 +09006512}
6513
Paul Duffine05480a2021-03-08 15:07:14 +00006514var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01006515 "api/current.txt": nil,
6516 "api/removed.txt": nil,
6517 "api/system-current.txt": nil,
6518 "api/system-removed.txt": nil,
6519 "api/test-current.txt": nil,
6520 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01006521
Anton Hanssondff2c782020-12-21 17:10:01 +00006522 "100/public/api/foo.txt": nil,
6523 "100/public/api/foo-removed.txt": nil,
6524 "100/system/api/foo.txt": nil,
6525 "100/system/api/foo-removed.txt": nil,
6526
Paul Duffineedc5d52020-06-12 17:46:39 +01006527 // For java_sdk_library_import
6528 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01006529}
6530
Jooyung Han58f26ab2019-12-18 15:34:32 +09006531func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006532 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09006533 apex {
6534 name: "myapex",
6535 key: "myapex.key",
6536 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006537 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09006538 }
6539
6540 apex_key {
6541 name: "myapex.key",
6542 public_key: "testkey.avbpubkey",
6543 private_key: "testkey.pem",
6544 }
6545
6546 java_sdk_library {
6547 name: "foo",
6548 srcs: ["a.java"],
6549 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006550 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09006551 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006552
6553 prebuilt_apis {
6554 name: "sdk",
6555 api_dirs: ["100"],
6556 }
Paul Duffin9b879592020-05-26 13:21:35 +01006557 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09006558
6559 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00006560 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09006561 "javalib/foo.jar",
6562 "etc/permissions/foo.xml",
6563 })
6564 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09006565 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00006566 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 +09006567}
6568
Paul Duffin9b879592020-05-26 13:21:35 +01006569func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006570 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006571 apex {
6572 name: "myapex",
6573 key: "myapex.key",
6574 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006575 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006576 }
6577
6578 apex_key {
6579 name: "myapex.key",
6580 public_key: "testkey.avbpubkey",
6581 private_key: "testkey.pem",
6582 }
6583
6584 java_sdk_library {
6585 name: "foo",
6586 srcs: ["a.java"],
6587 api_packages: ["foo"],
6588 apex_available: ["myapex"],
6589 sdk_version: "none",
6590 system_modules: "none",
6591 }
6592
6593 java_library {
6594 name: "bar",
6595 srcs: ["a.java"],
6596 libs: ["foo"],
6597 apex_available: ["myapex"],
6598 sdk_version: "none",
6599 system_modules: "none",
6600 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006601
6602 prebuilt_apis {
6603 name: "sdk",
6604 api_dirs: ["100"],
6605 }
Paul Duffin9b879592020-05-26 13:21:35 +01006606 `, withFiles(filesForSdkLibrary))
6607
6608 // java_sdk_library installs both impl jar and permission XML
6609 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6610 "javalib/bar.jar",
6611 "javalib/foo.jar",
6612 "etc/permissions/foo.xml",
6613 })
6614
6615 // The bar library should depend on the implementation jar.
6616 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006617 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006618 t.Errorf("expected %q, found %#q", expected, actual)
6619 }
6620}
6621
6622func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006623 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006624 apex {
6625 name: "myapex",
6626 key: "myapex.key",
6627 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006628 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006629 }
6630
6631 apex_key {
6632 name: "myapex.key",
6633 public_key: "testkey.avbpubkey",
6634 private_key: "testkey.pem",
6635 }
6636
6637 java_sdk_library {
6638 name: "foo",
6639 srcs: ["a.java"],
6640 api_packages: ["foo"],
6641 apex_available: ["myapex"],
6642 sdk_version: "none",
6643 system_modules: "none",
6644 }
6645
6646 java_library {
6647 name: "bar",
6648 srcs: ["a.java"],
6649 libs: ["foo"],
6650 sdk_version: "none",
6651 system_modules: "none",
6652 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006653
6654 prebuilt_apis {
6655 name: "sdk",
6656 api_dirs: ["100"],
6657 }
Paul Duffin9b879592020-05-26 13:21:35 +01006658 `, withFiles(filesForSdkLibrary))
6659
6660 // java_sdk_library installs both impl jar and permission XML
6661 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6662 "javalib/foo.jar",
6663 "etc/permissions/foo.xml",
6664 })
6665
6666 // The bar library should depend on the stubs jar.
6667 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006668 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006669 t.Errorf("expected %q, found %#q", expected, actual)
6670 }
6671}
6672
Paul Duffineedc5d52020-06-12 17:46:39 +01006673func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006674 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00006675 prebuilt_apis {
6676 name: "sdk",
6677 api_dirs: ["100"],
6678 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01006679 withFiles(map[string][]byte{
6680 "apex/a.java": nil,
6681 "apex/apex_manifest.json": nil,
6682 "apex/Android.bp": []byte(`
6683 package {
6684 default_visibility: ["//visibility:private"],
6685 }
6686
6687 apex {
6688 name: "myapex",
6689 key: "myapex.key",
6690 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006691 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006692 }
6693
6694 apex_key {
6695 name: "myapex.key",
6696 public_key: "testkey.avbpubkey",
6697 private_key: "testkey.pem",
6698 }
6699
6700 java_library {
6701 name: "bar",
6702 srcs: ["a.java"],
6703 libs: ["foo"],
6704 apex_available: ["myapex"],
6705 sdk_version: "none",
6706 system_modules: "none",
6707 }
6708`),
6709 "source/a.java": nil,
6710 "source/api/current.txt": nil,
6711 "source/api/removed.txt": nil,
6712 "source/Android.bp": []byte(`
6713 package {
6714 default_visibility: ["//visibility:private"],
6715 }
6716
6717 java_sdk_library {
6718 name: "foo",
6719 visibility: ["//apex"],
6720 srcs: ["a.java"],
6721 api_packages: ["foo"],
6722 apex_available: ["myapex"],
6723 sdk_version: "none",
6724 system_modules: "none",
6725 public: {
6726 enabled: true,
6727 },
6728 }
6729`),
6730 "prebuilt/a.jar": nil,
6731 "prebuilt/Android.bp": []byte(`
6732 package {
6733 default_visibility: ["//visibility:private"],
6734 }
6735
6736 java_sdk_library_import {
6737 name: "foo",
6738 visibility: ["//apex", "//source"],
6739 apex_available: ["myapex"],
6740 prefer: true,
6741 public: {
6742 jars: ["a.jar"],
6743 },
6744 }
6745`),
Anton Hanssondff2c782020-12-21 17:10:01 +00006746 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01006747 )
6748
6749 // java_sdk_library installs both impl jar and permission XML
6750 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6751 "javalib/bar.jar",
6752 "javalib/foo.jar",
6753 "etc/permissions/foo.xml",
6754 })
6755
6756 // The bar library should depend on the implementation jar.
6757 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006758 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01006759 t.Errorf("expected %q, found %#q", expected, actual)
6760 }
6761}
6762
6763func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
6764 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
6765 apex {
6766 name: "myapex",
6767 key: "myapex.key",
6768 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006769 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006770 }
6771
6772 apex_key {
6773 name: "myapex.key",
6774 public_key: "testkey.avbpubkey",
6775 private_key: "testkey.pem",
6776 }
6777
6778 java_sdk_library_import {
6779 name: "foo",
6780 apex_available: ["myapex"],
6781 prefer: true,
6782 public: {
6783 jars: ["a.jar"],
6784 },
6785 }
6786
6787 `, withFiles(filesForSdkLibrary))
6788}
6789
atrost6e126252020-01-27 17:01:16 +00006790func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01006791 result := android.GroupFixturePreparers(
6792 prepareForApexTest,
6793 java.PrepareForTestWithPlatformCompatConfig,
6794 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00006795 apex {
6796 name: "myapex",
6797 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00006798 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00006799 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006800 updatable: false,
atrost6e126252020-01-27 17:01:16 +00006801 }
6802
6803 apex_key {
6804 name: "myapex.key",
6805 public_key: "testkey.avbpubkey",
6806 private_key: "testkey.pem",
6807 }
6808
6809 platform_compat_config {
6810 name: "myjar-platform-compat-config",
6811 src: ":myjar",
6812 }
6813
6814 java_library {
6815 name: "myjar",
6816 srcs: ["foo/bar/MyClass.java"],
6817 sdk_version: "none",
6818 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00006819 apex_available: [ "myapex" ],
6820 }
Paul Duffin1b29e002021-03-16 15:06:54 +00006821
6822 // Make sure that a preferred prebuilt does not affect the apex contents.
6823 prebuilt_platform_compat_config {
6824 name: "myjar-platform-compat-config",
6825 metadata: "compat-config/metadata.xml",
6826 prefer: true,
6827 }
atrost6e126252020-01-27 17:01:16 +00006828 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00006829 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00006830 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6831 "etc/compatconfig/myjar-platform-compat-config.xml",
6832 "javalib/myjar.jar",
6833 })
6834}
6835
Jiyong Park479321d2019-12-16 11:47:12 +09006836func TestRejectNonInstallableJavaLibrary(t *testing.T) {
6837 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
6838 apex {
6839 name: "myapex",
6840 key: "myapex.key",
6841 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006842 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09006843 }
6844
6845 apex_key {
6846 name: "myapex.key",
6847 public_key: "testkey.avbpubkey",
6848 private_key: "testkey.pem",
6849 }
6850
6851 java_library {
6852 name: "myjar",
6853 srcs: ["foo/bar/MyClass.java"],
6854 sdk_version: "none",
6855 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09006856 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006857 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09006858 }
6859 `)
6860}
6861
Jiyong Park7afd1072019-12-30 16:56:33 +09006862func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006863 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09006864 apex {
6865 name: "myapex",
6866 key: "myapex.key",
6867 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006868 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09006869 }
6870
6871 apex_key {
6872 name: "myapex.key",
6873 public_key: "testkey.avbpubkey",
6874 private_key: "testkey.pem",
6875 }
6876
6877 cc_library {
6878 name: "mylib",
6879 srcs: ["mylib.cpp"],
6880 system_shared_libs: [],
6881 stl: "none",
6882 required: ["a", "b"],
6883 host_required: ["c", "d"],
6884 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006885 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006886 }
6887 `)
6888
6889 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006890 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006891 name := apexBundle.BaseModuleName()
6892 prefix := "TARGET_"
6893 var builder strings.Builder
6894 data.Custom(&builder, name, prefix, "", data)
6895 androidMk := builder.String()
6896 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6897 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6898 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6899}
6900
Jiyong Park7cd10e32020-01-14 09:22:18 +09006901func TestSymlinksFromApexToSystem(t *testing.T) {
6902 bp := `
6903 apex {
6904 name: "myapex",
6905 key: "myapex.key",
6906 native_shared_libs: ["mylib"],
6907 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006908 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006909 }
6910
Jiyong Park9d677202020-02-19 16:29:35 +09006911 apex {
6912 name: "myapex.updatable",
6913 key: "myapex.key",
6914 native_shared_libs: ["mylib"],
6915 java_libs: ["myjar"],
6916 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006917 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006918 }
6919
Jiyong Park7cd10e32020-01-14 09:22:18 +09006920 apex_key {
6921 name: "myapex.key",
6922 public_key: "testkey.avbpubkey",
6923 private_key: "testkey.pem",
6924 }
6925
6926 cc_library {
6927 name: "mylib",
6928 srcs: ["mylib.cpp"],
6929 shared_libs: ["myotherlib"],
6930 system_shared_libs: [],
6931 stl: "none",
6932 apex_available: [
6933 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006934 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006935 "//apex_available:platform",
6936 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006937 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006938 }
6939
6940 cc_library {
6941 name: "myotherlib",
6942 srcs: ["mylib.cpp"],
6943 system_shared_libs: [],
6944 stl: "none",
6945 apex_available: [
6946 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006947 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006948 "//apex_available:platform",
6949 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006950 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006951 }
6952
6953 java_library {
6954 name: "myjar",
6955 srcs: ["foo/bar/MyClass.java"],
6956 sdk_version: "none",
6957 system_modules: "none",
6958 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006959 apex_available: [
6960 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006961 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006962 "//apex_available:platform",
6963 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006964 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006965 }
6966
6967 java_library {
6968 name: "myotherjar",
6969 srcs: ["foo/bar/MyClass.java"],
6970 sdk_version: "none",
6971 system_modules: "none",
6972 apex_available: [
6973 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006974 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006975 "//apex_available:platform",
6976 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006977 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006978 }
6979 `
6980
6981 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6982 for _, f := range files {
6983 if f.path == file {
6984 if f.isLink {
6985 t.Errorf("%q is not a real file", file)
6986 }
6987 return
6988 }
6989 }
6990 t.Errorf("%q is not found", file)
6991 }
6992
6993 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6994 for _, f := range files {
6995 if f.path == file {
6996 if !f.isLink {
6997 t.Errorf("%q is not a symlink", file)
6998 }
6999 return
7000 }
7001 }
7002 t.Errorf("%q is not found", file)
7003 }
7004
Jiyong Park9d677202020-02-19 16:29:35 +09007005 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7006 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007007 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007008 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007009 ensureRealfileExists(t, files, "javalib/myjar.jar")
7010 ensureRealfileExists(t, files, "lib64/mylib.so")
7011 ensureRealfileExists(t, files, "lib64/myotherlib.so")
7012
Jiyong Park9d677202020-02-19 16:29:35 +09007013 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7014 ensureRealfileExists(t, files, "javalib/myjar.jar")
7015 ensureRealfileExists(t, files, "lib64/mylib.so")
7016 ensureRealfileExists(t, files, "lib64/myotherlib.so")
7017
7018 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007019 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007020 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007021 ensureRealfileExists(t, files, "javalib/myjar.jar")
7022 ensureRealfileExists(t, files, "lib64/mylib.so")
7023 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007024
7025 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7026 ensureRealfileExists(t, files, "javalib/myjar.jar")
7027 ensureRealfileExists(t, files, "lib64/mylib.so")
7028 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007029}
7030
Yo Chiange8128052020-07-23 20:09:18 +08007031func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007032 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007033 apex {
7034 name: "myapex",
7035 key: "myapex.key",
7036 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007037 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007038 }
7039
7040 apex_key {
7041 name: "myapex.key",
7042 public_key: "testkey.avbpubkey",
7043 private_key: "testkey.pem",
7044 }
7045
7046 cc_library_shared {
7047 name: "mylib",
7048 srcs: ["mylib.cpp"],
7049 shared_libs: ["myotherlib"],
7050 system_shared_libs: [],
7051 stl: "none",
7052 apex_available: [
7053 "myapex",
7054 "//apex_available:platform",
7055 ],
7056 }
7057
7058 cc_prebuilt_library_shared {
7059 name: "myotherlib",
7060 srcs: ["prebuilt.so"],
7061 system_shared_libs: [],
7062 stl: "none",
7063 apex_available: [
7064 "myapex",
7065 "//apex_available:platform",
7066 ],
7067 }
7068 `)
7069
7070 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007071 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007072 var builder strings.Builder
7073 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7074 androidMk := builder.String()
7075 // `myotherlib` is added to `myapex` as symlink
7076 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
7077 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7078 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7079 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09007080 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 +08007081}
7082
Jooyung Han643adc42020-02-27 13:50:06 +09007083func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007084 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007085 apex {
7086 name: "myapex",
7087 key: "myapex.key",
Jiyong Park34d5c332022-02-24 18:02:44 +09007088 jni_libs: ["mylib", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007089 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007090 }
7091
7092 apex_key {
7093 name: "myapex.key",
7094 public_key: "testkey.avbpubkey",
7095 private_key: "testkey.pem",
7096 }
7097
7098 cc_library {
7099 name: "mylib",
7100 srcs: ["mylib.cpp"],
7101 shared_libs: ["mylib2"],
7102 system_shared_libs: [],
7103 stl: "none",
7104 apex_available: [ "myapex" ],
7105 }
7106
7107 cc_library {
7108 name: "mylib2",
7109 srcs: ["mylib.cpp"],
7110 system_shared_libs: [],
7111 stl: "none",
7112 apex_available: [ "myapex" ],
7113 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007114
7115 rust_ffi_shared {
7116 name: "libfoo.rust",
7117 crate_name: "foo",
7118 srcs: ["foo.rs"],
7119 shared_libs: ["libfoo.shared_from_rust"],
7120 prefer_rlib: true,
7121 apex_available: ["myapex"],
7122 }
7123
7124 cc_library_shared {
7125 name: "libfoo.shared_from_rust",
7126 srcs: ["mylib.cpp"],
7127 system_shared_libs: [],
7128 stl: "none",
7129 stubs: {
7130 versions: ["10", "11", "12"],
7131 },
7132 }
7133
Jooyung Han643adc42020-02-27 13:50:06 +09007134 `)
7135
7136 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
7137 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiyong Park34d5c332022-02-24 18:02:44 +09007138 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007139 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7140 "lib64/mylib.so",
7141 "lib64/mylib2.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007142 "lib64/libfoo.rust.so",
7143 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7144 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007145 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007146
7147 // b/220397949
7148 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007149}
7150
Jooyung Han49f67012020-04-17 13:43:10 +09007151func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007152 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007153 apex {
7154 name: "myapex",
7155 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007156 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007157 }
7158 apex_key {
7159 name: "myapex.key",
7160 public_key: "testkey.avbpubkey",
7161 private_key: "testkey.pem",
7162 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007163 `,
7164 android.FixtureModifyConfig(func(config android.Config) {
7165 delete(config.Targets, android.Android)
7166 config.AndroidCommonTarget = android.Target{}
7167 }),
7168 )
Jooyung Han49f67012020-04-17 13:43:10 +09007169
7170 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7171 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7172 }
7173}
7174
Jiyong Parkbd159612020-02-28 15:22:21 +09007175func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007176 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007177 apex {
7178 name: "myapex",
7179 key: "myapex.key",
7180 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007181 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007182 }
7183
7184 apex_key {
7185 name: "myapex.key",
7186 public_key: "testkey.avbpubkey",
7187 private_key: "testkey.pem",
7188 }
7189
7190 android_app {
7191 name: "AppFoo",
7192 srcs: ["foo/bar/MyClass.java"],
7193 sdk_version: "none",
7194 system_modules: "none",
7195 apex_available: [ "myapex" ],
7196 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007197 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007198
Colin Crosscf371cc2020-11-13 11:48:42 -08007199 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09007200 content := bundleConfigRule.Args["content"]
7201
7202 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gascóa70425f2022-05-20 13:05:34 +00007203 ensureContains(t, content, `"apex_config":{"apex_embedded_apk_config":[{"package_name":"com.android.foo","path":"app/AppFoo@TEST.BUILD_ID/AppFoo.apk"}]}`)
Jiyong Parkbd159612020-02-28 15:22:21 +09007204}
7205
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007206func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007207 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007208 apex {
7209 name: "myapex",
7210 key: "myapex.key",
7211 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007212 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007213 }
7214
7215 apex_key {
7216 name: "myapex.key",
7217 public_key: "testkey.avbpubkey",
7218 private_key: "testkey.pem",
7219 }
7220
7221 android_app_set {
7222 name: "AppSet",
7223 set: "AppSet.apks",
7224 }`)
7225 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08007226 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007227 content := bundleConfigRule.Args["content"]
7228 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7229 s := mod.Rule("apexRule").Args["copy_commands"]
7230 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
7231 if len(copyCmds) != 3 {
7232 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
7233 }
Oriol Prieto Gascóa70425f2022-05-20 13:05:34 +00007234 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
7235 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
7236 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007237}
7238
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007239func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007240 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007241 apex_set {
7242 name: "myapex",
7243 filename: "foo_v2.apex",
7244 sanitized: {
7245 none: { set: "myapex.apks", },
7246 hwaddress: { set: "myapex.hwasan.apks", },
7247 },
Paul Duffin24704672021-04-06 16:09:30 +01007248 }
7249 `
7250 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007251
Paul Duffin24704672021-04-06 16:09:30 +01007252 // Check that the extractor produces the correct output file from the correct input file.
7253 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007254
Paul Duffin24704672021-04-06 16:09:30 +01007255 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7256 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007257
Paul Duffin24704672021-04-06 16:09:30 +01007258 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7259
7260 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007261 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7262 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007263
7264 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007265}
7266
Pranav Gupta8fa3a2e2022-09-27 00:27:08 +00007267func TestApexSetApksModuleAssignment(t *testing.T) {
7268 ctx := testApex(t, `
7269 apex_set {
7270 name: "myapex",
7271 set: ":myapex_apks_file",
7272 }
7273
7274 filegroup {
7275 name: "myapex_apks_file",
7276 srcs: ["myapex.apks"],
7277 }
7278 `)
7279
7280 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7281
7282 // Check that the extractor produces the correct apks file from the input module
7283 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.apks"
7284 extractedApex := m.Output(extractorOutput)
7285
7286 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
7287}
7288
Paul Duffin89f570a2021-06-16 01:42:33 +01007289func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007290 t.Helper()
7291
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007292 bp := `
7293 java_library {
7294 name: "some-updatable-apex-lib",
7295 srcs: ["a.java"],
7296 sdk_version: "current",
7297 apex_available: [
7298 "some-updatable-apex",
7299 ],
satayevabcd5972021-08-06 17:49:46 +01007300 permitted_packages: ["some.updatable.apex.lib"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007301 }
7302
7303 java_library {
7304 name: "some-non-updatable-apex-lib",
7305 srcs: ["a.java"],
7306 apex_available: [
7307 "some-non-updatable-apex",
7308 ],
Paul Duffin89f570a2021-06-16 01:42:33 +01007309 compile_dex: true,
satayevabcd5972021-08-06 17:49:46 +01007310 permitted_packages: ["some.non.updatable.apex.lib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007311 }
7312
7313 bootclasspath_fragment {
7314 name: "some-non-updatable-fragment",
7315 contents: ["some-non-updatable-apex-lib"],
7316 apex_available: [
7317 "some-non-updatable-apex",
7318 ],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007319 }
7320
7321 java_library {
7322 name: "some-platform-lib",
7323 srcs: ["a.java"],
7324 sdk_version: "current",
7325 installable: true,
7326 }
7327
7328 java_library {
7329 name: "some-art-lib",
7330 srcs: ["a.java"],
7331 sdk_version: "current",
7332 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00007333 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007334 ],
7335 hostdex: true,
Paul Duffine5218812021-06-07 13:28:19 +01007336 compile_dex: true,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007337 }
7338
7339 apex {
7340 name: "some-updatable-apex",
7341 key: "some-updatable-apex.key",
7342 java_libs: ["some-updatable-apex-lib"],
7343 updatable: true,
7344 min_sdk_version: "current",
7345 }
7346
7347 apex {
7348 name: "some-non-updatable-apex",
7349 key: "some-non-updatable-apex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007350 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007351 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007352 }
7353
7354 apex_key {
7355 name: "some-updatable-apex.key",
7356 }
7357
7358 apex_key {
7359 name: "some-non-updatable-apex.key",
7360 }
7361
7362 apex {
Paul Duffind376f792021-01-26 11:59:35 +00007363 name: "com.android.art.debug",
7364 key: "com.android.art.debug.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007365 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007366 updatable: true,
7367 min_sdk_version: "current",
7368 }
7369
Paul Duffinf23bc472021-04-27 12:42:20 +01007370 bootclasspath_fragment {
7371 name: "art-bootclasspath-fragment",
7372 image_name: "art",
7373 contents: ["some-art-lib"],
7374 apex_available: [
7375 "com.android.art.debug",
7376 ],
7377 }
7378
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007379 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00007380 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007381 }
7382
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007383 filegroup {
7384 name: "some-updatable-apex-file_contexts",
7385 srcs: [
7386 "system/sepolicy/apex/some-updatable-apex-file_contexts",
7387 ],
7388 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007389
7390 filegroup {
7391 name: "some-non-updatable-apex-file_contexts",
7392 srcs: [
7393 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
7394 ],
7395 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007396 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00007397
Paul Duffin89f570a2021-06-16 01:42:33 +01007398 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00007399}
7400
Paul Duffin89f570a2021-06-16 01:42:33 +01007401func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007402 t.Helper()
7403
Paul Duffin55607122021-03-30 23:32:51 +01007404 fs := android.MockFS{
7405 "a.java": nil,
7406 "a.jar": nil,
7407 "apex_manifest.json": nil,
7408 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007409 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007410 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7411 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7412 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007413 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007414 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007415
Paul Duffin55607122021-03-30 23:32:51 +01007416 errorHandler := android.FixtureExpectsNoErrors
7417 if errmsg != "" {
7418 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007419 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007420
Paul Duffin55607122021-03-30 23:32:51 +01007421 result := android.GroupFixturePreparers(
7422 cc.PrepareForTestWithCcDefaultModules,
7423 java.PrepareForTestWithHiddenApiBuildComponents,
7424 java.PrepareForTestWithJavaDefaultModules,
7425 java.PrepareForTestWithJavaSdkLibraryFiles,
7426 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007427 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007428 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007429 android.FixtureModifyMockFS(func(fs android.MockFS) {
7430 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7431 insert := ""
7432 for _, fragment := range fragments {
7433 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7434 }
7435 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7436 platform_bootclasspath {
7437 name: "platform-bootclasspath",
7438 fragments: [
7439 %s
7440 ],
7441 }
7442 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01007443 }
Paul Duffin89f570a2021-06-16 01:42:33 +01007444 }),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00007445 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01007446 ).
7447 ExtendWithErrorHandler(errorHandler).
7448 RunTestWithBp(t, bp)
7449
7450 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007451}
7452
Paul Duffin1aa50562022-06-09 17:32:21 +00007453func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01007454 preparers := android.GroupFixturePreparers(
7455 java.PrepareForTestWithJavaDefaultModules,
7456 PrepareForTestWithApexBuildComponents,
7457 ).
7458 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
7459 "Multiple installable prebuilt APEXes provide ambiguous deapexers: com.android.myapex and com.mycompany.android.myapex"))
7460
7461 bpBase := `
7462 apex_set {
7463 name: "com.android.myapex",
7464 installable: true,
7465 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7466 set: "myapex.apks",
7467 }
7468
7469 apex_set {
7470 name: "com.mycompany.android.myapex",
7471 apex_name: "com.android.myapex",
7472 installable: true,
7473 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7474 set: "company-myapex.apks",
7475 }
7476
7477 prebuilt_bootclasspath_fragment {
7478 name: "my-bootclasspath-fragment",
7479 apex_available: ["com.android.myapex"],
7480 %s
7481 }
7482 `
7483
7484 t.Run("java_import", func(t *testing.T) {
7485 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7486 java_import {
7487 name: "libfoo",
7488 jars: ["libfoo.jar"],
7489 apex_available: ["com.android.myapex"],
7490 }
7491 `)
7492 })
7493
7494 t.Run("java_sdk_library_import", func(t *testing.T) {
7495 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7496 java_sdk_library_import {
7497 name: "libfoo",
7498 public: {
7499 jars: ["libbar.jar"],
7500 },
7501 apex_available: ["com.android.myapex"],
7502 }
7503 `)
7504 })
7505
7506 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7507 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7508 image_name: "art",
7509 contents: ["libfoo"],
7510 `)+`
7511 java_sdk_library_import {
7512 name: "libfoo",
7513 public: {
7514 jars: ["libbar.jar"],
7515 },
7516 apex_available: ["com.android.myapex"],
7517 }
7518 `)
7519 })
7520}
7521
Paul Duffin1aa50562022-06-09 17:32:21 +00007522func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
7523 preparers := android.GroupFixturePreparers(
7524 java.PrepareForTestWithJavaDefaultModules,
7525 PrepareForTestWithApexBuildComponents,
7526 )
7527
7528 bpBase := `
7529 apex_set {
7530 name: "com.android.myapex",
7531 installable: true,
7532 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7533 set: "myapex.apks",
7534 }
7535
7536 apex_set {
7537 name: "com.android.myapex_compressed",
7538 apex_name: "com.android.myapex",
7539 installable: true,
7540 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7541 set: "myapex_compressed.apks",
7542 }
7543
7544 prebuilt_bootclasspath_fragment {
7545 name: "my-bootclasspath-fragment",
7546 apex_available: [
7547 "com.android.myapex",
7548 "com.android.myapex_compressed",
7549 ],
7550 hidden_api: {
7551 annotation_flags: "annotation-flags.csv",
7552 metadata: "metadata.csv",
7553 index: "index.csv",
7554 signature_patterns: "signature_patterns.csv",
7555 },
7556 %s
7557 }
7558 `
7559
7560 t.Run("java_import", func(t *testing.T) {
7561 result := preparers.RunTestWithBp(t,
7562 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7563 java_import {
7564 name: "libfoo",
7565 jars: ["libfoo.jar"],
7566 apex_available: [
7567 "com.android.myapex",
7568 "com.android.myapex_compressed",
7569 ],
7570 }
7571 `)
7572
7573 module := result.Module("libfoo", "android_common_com.android.myapex")
7574 usesLibraryDep := module.(java.UsesLibraryDependency)
7575 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7576 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7577 usesLibraryDep.DexJarBuildPath().Path())
7578 })
7579
7580 t.Run("java_sdk_library_import", func(t *testing.T) {
7581 result := preparers.RunTestWithBp(t,
7582 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7583 java_sdk_library_import {
7584 name: "libfoo",
7585 public: {
7586 jars: ["libbar.jar"],
7587 },
7588 apex_available: [
7589 "com.android.myapex",
7590 "com.android.myapex_compressed",
7591 ],
7592 compile_dex: true,
7593 }
7594 `)
7595
7596 module := result.Module("libfoo", "android_common_com.android.myapex")
7597 usesLibraryDep := module.(java.UsesLibraryDependency)
7598 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7599 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7600 usesLibraryDep.DexJarBuildPath().Path())
7601 })
7602
7603 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7604 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7605 image_name: "art",
7606 contents: ["libfoo"],
7607 `)+`
7608 java_sdk_library_import {
7609 name: "libfoo",
7610 public: {
7611 jars: ["libbar.jar"],
7612 },
7613 apex_available: [
7614 "com.android.myapex",
7615 "com.android.myapex_compressed",
7616 ],
7617 compile_dex: true,
7618 }
7619 `)
7620 })
7621}
7622
Jooyung Han548640b2020-04-27 12:10:30 +09007623func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
7624 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7625 apex {
7626 name: "myapex",
7627 key: "myapex.key",
7628 updatable: true,
7629 }
7630
7631 apex_key {
7632 name: "myapex.key",
7633 public_key: "testkey.avbpubkey",
7634 private_key: "testkey.pem",
7635 }
7636 `)
7637}
7638
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007639func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
7640 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7641 apex {
7642 name: "myapex",
7643 key: "myapex.key",
7644 }
7645
7646 apex_key {
7647 name: "myapex.key",
7648 public_key: "testkey.avbpubkey",
7649 private_key: "testkey.pem",
7650 }
7651 `)
7652}
7653
Daniel Norman69109112021-12-02 12:52:42 -08007654func TestUpdatable_cannot_be_vendor_apex(t *testing.T) {
7655 testApexError(t, `"myapex" .*: updatable: vendor APEXes are not updatable`, `
7656 apex {
7657 name: "myapex",
7658 key: "myapex.key",
7659 updatable: true,
7660 soc_specific: true,
7661 }
7662
7663 apex_key {
7664 name: "myapex.key",
7665 public_key: "testkey.avbpubkey",
7666 private_key: "testkey.pem",
7667 }
7668 `)
7669}
7670
satayevb98371c2021-06-15 16:49:50 +01007671func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
7672 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
7673 apex {
7674 name: "myapex",
7675 key: "myapex.key",
7676 systemserverclasspath_fragments: [
7677 "mysystemserverclasspathfragment",
7678 ],
7679 min_sdk_version: "29",
7680 updatable: true,
7681 }
7682
7683 apex_key {
7684 name: "myapex.key",
7685 public_key: "testkey.avbpubkey",
7686 private_key: "testkey.pem",
7687 }
7688
7689 java_library {
7690 name: "foo",
7691 srcs: ["b.java"],
7692 min_sdk_version: "29",
7693 installable: true,
7694 apex_available: [
7695 "myapex",
7696 ],
7697 }
7698
7699 systemserverclasspath_fragment {
7700 name: "mysystemserverclasspathfragment",
7701 generate_classpaths_proto: false,
7702 contents: [
7703 "foo",
7704 ],
7705 apex_available: [
7706 "myapex",
7707 ],
7708 }
satayevabcd5972021-08-06 17:49:46 +01007709 `,
7710 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
7711 )
satayevb98371c2021-06-15 16:49:50 +01007712}
7713
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007714func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007715 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
7716 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
7717 // modules to be included in the BootJars.
7718 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
7719 return android.GroupFixturePreparers(
7720 dexpreopt.FixtureSetBootJars(bootJars...),
7721 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7722 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7723 }),
7724 )
7725 }
7726
7727 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
7728 // same value. This can result in an invalid configuration as it allows non art apex jars to be
7729 // specified in the ArtApexJars configuration.
7730 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
7731 return android.GroupFixturePreparers(
7732 dexpreopt.FixtureSetArtBootJars(bootJars...),
7733 dexpreopt.FixtureSetBootJars(bootJars...),
7734 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7735 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7736 }),
7737 )
7738 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007739
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007740 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007741 preparer := android.GroupFixturePreparers(
7742 java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib"),
7743 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7744 )
7745 fragments := []java.ApexVariantReference{
7746 {
7747 Apex: proptools.StringPtr("com.android.art.debug"),
7748 Module: proptools.StringPtr("art-bootclasspath-fragment"),
7749 },
7750 {
7751 Apex: proptools.StringPtr("some-non-updatable-apex"),
7752 Module: proptools.StringPtr("some-non-updatable-fragment"),
7753 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007754 }
satayevabcd5972021-08-06 17:49:46 +01007755 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007756 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007757
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007758 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007759 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
7760 // Update the dexpreopt BootJars directly.
satayevabcd5972021-08-06 17:49:46 +01007761 preparer := android.GroupFixturePreparers(
7762 prepareSetBootJars("com.android.art.debug:some-art-lib"),
7763 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7764 )
Paul Duffin60264a02021-04-12 20:02:36 +01007765 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007766 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007767
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007768 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 +01007769 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 +01007770 // Update the dexpreopt ArtApexJars directly.
7771 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
7772 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007773 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007774
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007775 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 +01007776 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 +01007777 // Update the dexpreopt ArtApexJars directly.
7778 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
7779 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007780 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007781
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007782 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 +01007783 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 +01007784 preparer := android.GroupFixturePreparers(
7785 java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib"),
7786 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7787 )
Paul Duffin60264a02021-04-12 20:02:36 +01007788 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007789 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007790
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007791 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 +01007792 preparer := java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01007793 fragment := java.ApexVariantReference{
7794 Apex: proptools.StringPtr("some-non-updatable-apex"),
7795 Module: proptools.StringPtr("some-non-updatable-fragment"),
7796 }
7797 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007798 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007799
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007800 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007801 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007802 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7803 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007804 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007805
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007806 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007807 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007808 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7809 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007810 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007811
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007812 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01007813 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01007814 // Update the dexpreopt ArtApexJars directly.
7815 preparer := prepareSetArtJars("platform:some-platform-lib")
7816 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007817 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007818
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007819 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007820 preparer := android.GroupFixturePreparers(
7821 java.FixtureConfigureBootJars("platform:some-platform-lib"),
7822 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7823 )
7824 fragments := []java.ApexVariantReference{
7825 {
7826 Apex: proptools.StringPtr("some-non-updatable-apex"),
7827 Module: proptools.StringPtr("some-non-updatable-fragment"),
7828 },
7829 }
7830 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007831 })
Paul Duffin064b70c2020-11-02 17:32:38 +00007832}
7833
7834func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007835 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00007836 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01007837 fragment := java.ApexVariantReference{
7838 Apex: proptools.StringPtr("myapex"),
7839 Module: proptools.StringPtr("my-bootclasspath-fragment"),
7840 }
7841
Paul Duffin064b70c2020-11-02 17:32:38 +00007842 testDexpreoptWithApexes(t, `
7843 prebuilt_apex {
7844 name: "myapex" ,
7845 arch: {
7846 arm64: {
7847 src: "myapex-arm64.apex",
7848 },
7849 arm: {
7850 src: "myapex-arm.apex",
7851 },
7852 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007853 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7854 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007855
Paul Duffin89f570a2021-06-16 01:42:33 +01007856 prebuilt_bootclasspath_fragment {
7857 name: "my-bootclasspath-fragment",
7858 contents: ["libfoo"],
7859 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01007860 hidden_api: {
7861 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
7862 metadata: "my-bootclasspath-fragment/metadata.csv",
7863 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01007864 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
7865 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
7866 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01007867 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007868 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007869
Paul Duffin89f570a2021-06-16 01:42:33 +01007870 java_import {
7871 name: "libfoo",
7872 jars: ["libfoo.jar"],
7873 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01007874 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007875 }
7876 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00007877 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007878}
7879
Spandan Das440ff962021-11-12 00:01:37 +00007880func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01007881 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01007882 bp += `
7883 apex_key {
7884 name: "myapex.key",
7885 public_key: "testkey.avbpubkey",
7886 private_key: "testkey.pem",
7887 }`
Paul Duffin45338f02021-03-30 23:07:52 +01007888 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01007889 "lib1/src/A.java": nil,
7890 "lib2/src/B.java": nil,
7891 "system/sepolicy/apex/myapex-file_contexts": nil,
7892 }
7893
Paul Duffin45338f02021-03-30 23:07:52 +01007894 errorHandler := android.FixtureExpectsNoErrors
7895 if errmsg != "" {
7896 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07007897 }
Colin Crossae8600b2020-10-29 17:09:13 -07007898
Paul Duffin45338f02021-03-30 23:07:52 +01007899 android.GroupFixturePreparers(
7900 android.PrepareForTestWithAndroidBuildComponents,
7901 java.PrepareForTestWithJavaBuildComponents,
7902 PrepareForTestWithApexBuildComponents,
7903 android.PrepareForTestWithNeverallowRules(rules),
7904 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01007905 apexBootJars := make([]string, 0, len(bootJars))
7906 for _, apexBootJar := range bootJars {
7907 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01007908 }
satayevd604b212021-07-21 14:23:52 +01007909 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01007910 }),
7911 fs.AddToFixture(),
7912 ).
7913 ExtendWithErrorHandler(errorHandler).
7914 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01007915}
7916
7917func TestApexPermittedPackagesRules(t *testing.T) {
7918 testcases := []struct {
Spandan Das440ff962021-11-12 00:01:37 +00007919 name string
7920 expectedError string
7921 bp string
7922 bootJars []string
7923 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01007924 }{
7925
7926 {
7927 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
7928 expectedError: "",
7929 bp: `
7930 java_library {
7931 name: "bcp_lib1",
7932 srcs: ["lib1/src/*.java"],
7933 permitted_packages: ["foo.bar"],
7934 apex_available: ["myapex"],
7935 sdk_version: "none",
7936 system_modules: "none",
7937 }
7938 java_library {
7939 name: "nonbcp_lib2",
7940 srcs: ["lib2/src/*.java"],
7941 apex_available: ["myapex"],
7942 permitted_packages: ["a.b"],
7943 sdk_version: "none",
7944 system_modules: "none",
7945 }
7946 apex {
7947 name: "myapex",
7948 key: "myapex.key",
7949 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007950 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01007951 }`,
7952 bootJars: []string{"bcp_lib1"},
Spandan Das440ff962021-11-12 00:01:37 +00007953 bcpPermittedPackages: map[string][]string{
7954 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01007955 "foo.bar",
7956 },
7957 },
7958 },
7959 {
Anton Hanssonddf8c1b2021-12-23 15:05:38 +00007960 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Das440ff962021-11-12 00:01:37 +00007961 expectedError: `(?s)module "bcp_lib2" .* which is restricted because bcp_lib2 bootjar may only use these package prefixes: foo.bar. 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 +01007962 bp: `
7963 java_library {
7964 name: "bcp_lib1",
7965 srcs: ["lib1/src/*.java"],
7966 apex_available: ["myapex"],
7967 permitted_packages: ["foo.bar"],
7968 sdk_version: "none",
7969 system_modules: "none",
7970 }
7971 java_library {
7972 name: "bcp_lib2",
7973 srcs: ["lib2/src/*.java"],
7974 apex_available: ["myapex"],
7975 permitted_packages: ["foo.bar", "bar.baz"],
7976 sdk_version: "none",
7977 system_modules: "none",
7978 }
7979 apex {
7980 name: "myapex",
Remi NGUYEN VAN1fdd6ca2021-12-02 19:39:35 +09007981 key: "myapex.key",
7982 java_libs: ["bcp_lib1", "bcp_lib2"],
7983 updatable: false,
7984 }
7985 `,
7986 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Das440ff962021-11-12 00:01:37 +00007987 bcpPermittedPackages: map[string][]string{
7988 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01007989 "foo.bar",
7990 },
Spandan Das440ff962021-11-12 00:01:37 +00007991 "bcp_lib2": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01007992 "foo.bar",
7993 },
7994 },
7995 },
7996 {
Spandan Das440ff962021-11-12 00:01:37 +00007997 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
Jiyong Park62304bb2020-04-13 16:19:48 +09007998 expectedError: "",
7999 bp: `
8000 java_library {
Spandan Das440ff962021-11-12 00:01:37 +00008001 name: "bcp_lib_restricted",
Jiyong Park62304bb2020-04-13 16:19:48 +09008002 srcs: ["lib1/src/*.java"],
Jiyong Park46a512f2020-12-04 18:02:13 +09008003 apex_available: ["myapex"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008004 permitted_packages: ["foo.bar"],
8005 sdk_version: "none",
Spandan Das440ff962021-11-12 00:01:37 +00008006 min_sdk_version: "29",
Jiyong Park46a512f2020-12-04 18:02:13 +09008007 system_modules: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008008 }
8009 java_library {
Spandan Das440ff962021-11-12 00:01:37 +00008010 name: "bcp_lib_unrestricted",
Jiyong Park46a512f2020-12-04 18:02:13 +09008011 srcs: ["lib2/src/*.java"],
8012 apex_available: ["myapex"],
8013 permitted_packages: ["foo.bar", "bar.baz"],
8014 sdk_version: "none",
Spandan Das440ff962021-11-12 00:01:37 +00008015 min_sdk_version: "29",
Jiyong Park46a512f2020-12-04 18:02:13 +09008016 system_modules: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008017 }
8018 apex {
8019 name: "myapex",
Jiyong Park62304bb2020-04-13 16:19:48 +09008020 key: "myapex.key",
Spandan Das440ff962021-11-12 00:01:37 +00008021 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8022 updatable: true,
8023 min_sdk_version: "29",
Jiyong Park62304bb2020-04-13 16:19:48 +09008024 }
8025 `,
8026 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Das440ff962021-11-12 00:01:37 +00008027 bcpPermittedPackages: map[string][]string{
8028 "bcp_lib1_non_updateable": []string{
Jiyong Park62304bb2020-04-13 16:19:48 +09008029 "foo.bar",
8030 },
Spandan Das440ff962021-11-12 00:01:37 +00008031 // bcp_lib2_updateable has no entry here since updateable bcp can contain new packages - tracking via an allowlist is not necessary
Jiyong Park25fc6a92018-11-18 18:02:45 +09008032 },
Jiyong Park62304bb2020-04-13 16:19:48 +09008033 },
8034 }
8035 for _, tc := range testcases {
8036 t.Run(tc.name, func(t *testing.T) {
Spandan Das440ff962021-11-12 00:01:37 +00008037 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8038 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Jiyong Park62304bb2020-04-13 16:19:48 +09008039 })
8040 }
8041}
8042
8043func TestTestFor(t *testing.T) {
8044 ctx := testApex(t, `
8045 apex {
8046 name: "myapex",
8047 key: "myapex.key",
8048 native_shared_libs: ["mylib", "myprivlib"],
8049 updatable: false,
8050 }
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 system_shared_libs: [],
8062 stl: "none",
8063 stubs: {
8064 versions: ["1"],
8065 },
8066 apex_available: ["myapex"],
8067 }
8068
8069 cc_library {
8070 name: "myprivlib",
8071 srcs: ["mylib.cpp"],
8072 system_shared_libs: [],
8073 stl: "none",
8074 apex_available: ["myapex"],
8075 }
8076
8077
8078 cc_test {
8079 name: "mytest",
8080 gtest: false,
8081 srcs: ["mylib.cpp"],
8082 system_shared_libs: [],
8083 stl: "none",
8084 shared_libs: ["mylib", "myprivlib", "mytestlib"],
8085 test_for: ["myapex"]
8086 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008087
8088 cc_library {
8089 name: "mytestlib",
8090 srcs: ["mylib.cpp"],
8091 system_shared_libs: [],
8092 shared_libs: ["mylib", "myprivlib"],
8093 stl: "none",
8094 test_for: ["myapex"],
8095 }
8096
8097 cc_benchmark {
8098 name: "mybench",
8099 srcs: ["mylib.cpp"],
8100 system_shared_libs: [],
8101 shared_libs: ["mylib", "myprivlib"],
8102 stl: "none",
8103 test_for: ["myapex"],
8104 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09008105 `)
8106
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008107 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008108 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008109 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8110 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8111 }
8112
8113 // These modules are tests for the apex, therefore are linked to the
Jiyong Park25fc6a92018-11-18 18:02:45 +09008114 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008115 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8116 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8117 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8118}
Jiyong Park46a512f2020-12-04 18:02:13 +09008119
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008120func TestIndirectTestFor(t *testing.T) {
8121 ctx := testApex(t, `
8122 apex {
8123 name: "myapex",
8124 key: "myapex.key",
8125 native_shared_libs: ["mylib", "myprivlib"],
8126 updatable: false,
8127 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008128
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008129 apex_key {
8130 name: "myapex.key",
8131 public_key: "testkey.avbpubkey",
8132 private_key: "testkey.pem",
8133 }
8134
8135 cc_library {
8136 name: "mylib",
8137 srcs: ["mylib.cpp"],
8138 system_shared_libs: [],
8139 stl: "none",
8140 stubs: {
8141 versions: ["1"],
8142 },
8143 apex_available: ["myapex"],
8144 }
8145
8146 cc_library {
8147 name: "myprivlib",
8148 srcs: ["mylib.cpp"],
8149 system_shared_libs: [],
8150 stl: "none",
8151 shared_libs: ["mylib"],
8152 apex_available: ["myapex"],
8153 }
8154
8155 cc_library {
8156 name: "mytestlib",
8157 srcs: ["mylib.cpp"],
8158 system_shared_libs: [],
8159 shared_libs: ["myprivlib"],
8160 stl: "none",
8161 test_for: ["myapex"],
8162 }
8163 `)
8164
8165 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008166 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008167 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8168 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8169 }
8170
8171 // The platform variant of mytestlib links to the platform variant of the
8172 // internal myprivlib.
8173 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8174
8175 // The platform variant of myprivlib links to the platform variant of mylib
8176 // and bypasses its stubs.
8177 ensureLinkedLibIs("myprivlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09008178}
8179
Martin Stjernholmec009002021-03-27 15:18:31 +00008180func TestTestForForLibInOtherApex(t *testing.T) {
8181 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8182 _ = testApex(t, `
8183 apex {
8184 name: "com.android.art",
8185 key: "myapex.key",
8186 native_shared_libs: ["mylib"],
8187 updatable: false,
8188 }
8189
8190 apex {
8191 name: "com.android.art.debug",
8192 key: "myapex.key",
8193 native_shared_libs: ["mylib", "mytestlib"],
8194 updatable: false,
8195 }
8196
8197 apex_key {
8198 name: "myapex.key",
8199 public_key: "testkey.avbpubkey",
8200 private_key: "testkey.pem",
8201 }
8202
8203 cc_library {
8204 name: "mylib",
8205 srcs: ["mylib.cpp"],
8206 system_shared_libs: [],
8207 stl: "none",
8208 stubs: {
8209 versions: ["1"],
8210 },
8211 apex_available: ["com.android.art", "com.android.art.debug"],
8212 }
8213
8214 cc_library {
8215 name: "mytestlib",
8216 srcs: ["mylib.cpp"],
8217 system_shared_libs: [],
8218 shared_libs: ["mylib"],
8219 stl: "none",
8220 apex_available: ["com.android.art.debug"],
8221 test_for: ["com.android.art"],
8222 }
8223 `,
8224 android.MockFS{
8225 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8226 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8227 }.AddToFixture())
8228}
8229
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008230// TODO(jungjw): Move this to proptools
8231func intPtr(i int) *int {
8232 return &i
8233}
8234
8235func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008236 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008237 apex_set {
8238 name: "myapex",
8239 set: "myapex.apks",
8240 filename: "foo_v2.apex",
8241 overrides: ["foo"],
8242 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008243 `,
8244 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8245 variables.Platform_sdk_version = intPtr(30)
8246 }),
8247 android.FixtureModifyConfig(func(config android.Config) {
8248 config.Targets[android.Android] = []android.Target{
8249 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8250 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8251 }
8252 }),
8253 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008254
Paul Duffin24704672021-04-06 16:09:30 +01008255 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008256
8257 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008258 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008259 actual := extractedApex.Args["abis"]
8260 expected := "ARMEABI_V7A,ARM64_V8A"
8261 if actual != expected {
8262 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8263 }
8264 actual = extractedApex.Args["sdk-version"]
8265 expected = "30"
8266 if actual != expected {
8267 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8268 }
8269
Paul Duffin6717d882021-06-15 19:09:41 +01008270 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008271 a := m.Module().(*ApexSet)
8272 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008273 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008274 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8275 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8276 }
8277}
8278
Anton Hansson144be1f2022-11-25 14:06:46 +00008279func TestApexSet_NativeBridge(t *testing.T) {
8280 ctx := testApex(t, `
8281 apex_set {
8282 name: "myapex",
8283 set: "myapex.apks",
8284 filename: "foo_v2.apex",
8285 overrides: ["foo"],
8286 }
8287 `,
8288 android.FixtureModifyConfig(func(config android.Config) {
8289 config.Targets[android.Android] = []android.Target{
8290 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8291 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8292 }
8293 }),
8294 )
8295
8296 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
8297
8298 // Check extract_apks tool parameters. No native bridge arch expected
8299 extractedApex := m.Output("extracted/myapex.apks")
8300 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8301}
8302
Jiyong Park7d95a512020-05-10 15:16:24 +09008303func TestNoStaticLinkingToStubsLib(t *testing.T) {
8304 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8305 apex {
8306 name: "myapex",
8307 key: "myapex.key",
8308 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008309 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008310 }
8311
8312 apex_key {
8313 name: "myapex.key",
8314 public_key: "testkey.avbpubkey",
8315 private_key: "testkey.pem",
8316 }
8317
8318 cc_library {
8319 name: "mylib",
8320 srcs: ["mylib.cpp"],
8321 static_libs: ["otherlib"],
8322 system_shared_libs: [],
8323 stl: "none",
8324 apex_available: [ "myapex" ],
8325 }
8326
8327 cc_library {
8328 name: "otherlib",
8329 srcs: ["mylib.cpp"],
8330 system_shared_libs: [],
8331 stl: "none",
8332 stubs: {
8333 versions: ["1", "2", "3"],
8334 },
8335 apex_available: [ "myapex" ],
8336 }
8337 `)
8338}
8339
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008340func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008341 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008342 apex {
8343 name: "myapex",
8344 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008345 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008346 custom_sign_tool: "sign_myapex",
8347 }
8348
8349 apex_key {
8350 name: "myapex.key",
8351 public_key: "testkey.avbpubkey",
8352 private_key: "testkey.pem",
8353 }
8354 `)
8355
8356 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8357 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8358 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"`)
8359}
8360
8361func TestApexKeysTxtOverrides(t *testing.T) {
8362 ctx := testApex(t, `
8363 apex {
8364 name: "myapex",
8365 key: "myapex.key",
8366 updatable: false,
8367 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008368 }
8369
8370 apex_key {
8371 name: "myapex.key",
8372 public_key: "testkey.avbpubkey",
8373 private_key: "testkey.pem",
8374 }
8375
8376 prebuilt_apex {
8377 name: "myapex",
8378 prefer: true,
8379 arch: {
8380 arm64: {
8381 src: "myapex-arm64.apex",
8382 },
8383 arm: {
8384 src: "myapex-arm.apex",
8385 },
8386 },
8387 }
8388
8389 apex_set {
8390 name: "myapex_set",
8391 set: "myapex.apks",
8392 filename: "myapex_set.apex",
8393 overrides: ["myapex"],
8394 }
8395 `)
8396
8397 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8398 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8399 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 +09008400 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 +09008401}
8402
Jooyung Han938b5932020-06-20 12:47:47 +09008403func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008404 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008405 apex {
8406 name: "myapex",
8407 key: "myapex.key",
8408 apps: ["app"],
8409 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008410 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008411 }
8412
8413 apex_key {
8414 name: "myapex.key",
8415 public_key: "testkey.avbpubkey",
8416 private_key: "testkey.pem",
8417 }
8418
8419 android_app {
8420 name: "app",
8421 srcs: ["foo/bar/MyClass.java"],
8422 package_name: "foo",
8423 sdk_version: "none",
8424 system_modules: "none",
8425 apex_available: [ "myapex" ],
8426 }
8427 `, withFiles(map[string][]byte{
8428 "sub/Android.bp": []byte(`
8429 override_apex {
8430 name: "override_myapex",
8431 base: "myapex",
8432 apps: ["override_app"],
8433 allowed_files: ":allowed",
8434 }
8435 // Overridable "path" property should be referenced indirectly
8436 filegroup {
8437 name: "allowed",
8438 srcs: ["allowed.txt"],
8439 }
8440 override_android_app {
8441 name: "override_app",
8442 base: "app",
8443 package_name: "bar",
8444 }
8445 `),
8446 }))
8447
8448 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
8449 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8450 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8451 }
8452
8453 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
8454 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8455 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8456 }
8457}
8458
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008459func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008460 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008461 apex {
8462 name: "myapex",
8463 key: "myapex.key",
8464 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008465 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008466 }
8467
8468 apex_key {
8469 name: "myapex.key",
8470 public_key: "testkey.avbpubkey",
8471 private_key: "testkey.pem",
8472 }
8473
8474 cc_library {
8475 name: "mylib",
8476 srcs: ["mylib.cpp"],
8477 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008478 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008479 },
8480 apex_available: ["myapex"],
8481 }
8482
8483 cc_prebuilt_library_shared {
8484 name: "mylib",
8485 prefer: false,
8486 srcs: ["prebuilt.so"],
8487 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008488 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008489 },
8490 apex_available: ["myapex"],
8491 }
8492 `)
8493}
8494
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008495func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008496 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008497 apex {
8498 name: "myapex",
8499 key: "myapex.key",
8500 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008501 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008502 }
8503 apex_key {
8504 name: "myapex.key",
8505 public_key: "testkey.avbpubkey",
8506 private_key: "testkey.pem",
8507 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008508 `,
8509 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8510 variables.CompressedApex = proptools.BoolPtr(true)
8511 }),
8512 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008513
8514 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
8515 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
8516
8517 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
8518 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
8519
8520 // Make sure output of bundle is .capex
8521 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8522 ensureContains(t, ab.outputFile.String(), "myapex.capex")
8523
8524 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07008525 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008526 var builder strings.Builder
8527 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8528 androidMk := builder.String()
8529 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
8530}
8531
Martin Stjernholm2856c662020-12-02 15:03:42 +00008532func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008533 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00008534 apex {
8535 name: "myapex",
8536 key: "myapex.key",
8537 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008538 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00008539 }
8540
8541 apex_key {
8542 name: "myapex.key",
8543 public_key: "testkey.avbpubkey",
8544 private_key: "testkey.pem",
8545 }
8546
8547 cc_library {
8548 name: "mylib",
8549 srcs: ["mylib.cpp"],
8550 apex_available: ["myapex"],
8551 shared_libs: ["otherlib"],
8552 system_shared_libs: [],
8553 }
8554
8555 cc_library {
8556 name: "otherlib",
8557 srcs: ["mylib.cpp"],
8558 stubs: {
8559 versions: ["current"],
8560 },
8561 }
8562
8563 cc_prebuilt_library_shared {
8564 name: "otherlib",
8565 prefer: true,
8566 srcs: ["prebuilt.so"],
8567 stubs: {
8568 versions: ["current"],
8569 },
8570 }
8571 `)
8572
8573 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07008574 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00008575 var builder strings.Builder
8576 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8577 androidMk := builder.String()
8578
8579 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
8580 // a thing there.
8581 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
8582}
8583
Jiyong Parke3867542020-12-03 17:28:25 +09008584func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008585 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09008586 apex {
8587 name: "myapex",
8588 key: "myapex.key",
8589 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008590 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09008591 }
8592
8593 apex_key {
8594 name: "myapex.key",
8595 public_key: "testkey.avbpubkey",
8596 private_key: "testkey.pem",
8597 }
8598
8599 cc_library {
8600 name: "mylib",
8601 srcs: ["mylib.cpp"],
8602 system_shared_libs: [],
8603 stl: "none",
8604 apex_available: ["myapex"],
8605 shared_libs: ["mylib2"],
8606 target: {
8607 apex: {
8608 exclude_shared_libs: ["mylib2"],
8609 },
8610 },
8611 }
8612
8613 cc_library {
8614 name: "mylib2",
8615 srcs: ["mylib.cpp"],
8616 system_shared_libs: [],
8617 stl: "none",
8618 }
8619 `)
8620
8621 // Check if mylib is linked to mylib2 for the non-apex target
8622 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
8623 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
8624
8625 // Make sure that the link doesn't occur for the apex target
8626 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
8627 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
8628
8629 // It shouldn't appear in the copy cmd as well.
8630 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
8631 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
8632}
8633
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008634func TestPrebuiltStubLibDep(t *testing.T) {
8635 bpBase := `
8636 apex {
8637 name: "myapex",
8638 key: "myapex.key",
8639 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008640 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008641 }
8642 apex_key {
8643 name: "myapex.key",
8644 public_key: "testkey.avbpubkey",
8645 private_key: "testkey.pem",
8646 }
8647 cc_library {
8648 name: "mylib",
8649 srcs: ["mylib.cpp"],
8650 apex_available: ["myapex"],
8651 shared_libs: ["stublib"],
8652 system_shared_libs: [],
8653 }
8654 apex {
8655 name: "otherapex",
8656 enabled: %s,
8657 key: "myapex.key",
8658 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008659 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008660 }
8661 `
8662
8663 stublibSourceBp := `
8664 cc_library {
8665 name: "stublib",
8666 srcs: ["mylib.cpp"],
8667 apex_available: ["otherapex"],
8668 system_shared_libs: [],
8669 stl: "none",
8670 stubs: {
8671 versions: ["1"],
8672 },
8673 }
8674 `
8675
8676 stublibPrebuiltBp := `
8677 cc_prebuilt_library_shared {
8678 name: "stublib",
8679 srcs: ["prebuilt.so"],
8680 apex_available: ["otherapex"],
8681 stubs: {
8682 versions: ["1"],
8683 },
8684 %s
8685 }
8686 `
8687
8688 tests := []struct {
8689 name string
8690 stublibBp string
8691 usePrebuilt bool
8692 modNames []string // Modules to collect AndroidMkEntries for
8693 otherApexEnabled []string
8694 }{
8695 {
8696 name: "only_source",
8697 stublibBp: stublibSourceBp,
8698 usePrebuilt: false,
8699 modNames: []string{"stublib"},
8700 otherApexEnabled: []string{"true", "false"},
8701 },
8702 {
8703 name: "source_preferred",
8704 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
8705 usePrebuilt: false,
8706 modNames: []string{"stublib", "prebuilt_stublib"},
8707 otherApexEnabled: []string{"true", "false"},
8708 },
8709 {
8710 name: "prebuilt_preferred",
8711 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
8712 usePrebuilt: true,
8713 modNames: []string{"stublib", "prebuilt_stublib"},
8714 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8715 },
8716 {
8717 name: "only_prebuilt",
8718 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
8719 usePrebuilt: true,
8720 modNames: []string{"stublib"},
8721 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8722 },
8723 }
8724
8725 for _, test := range tests {
8726 t.Run(test.name, func(t *testing.T) {
8727 for _, otherApexEnabled := range test.otherApexEnabled {
8728 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008729 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008730
8731 type modAndMkEntries struct {
8732 mod *cc.Module
8733 mkEntries android.AndroidMkEntries
8734 }
8735 entries := []*modAndMkEntries{}
8736
8737 // Gather shared lib modules that are installable
8738 for _, modName := range test.modNames {
8739 for _, variant := range ctx.ModuleVariantsForTests(modName) {
8740 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
8741 continue
8742 }
8743 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08008744 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008745 continue
8746 }
Colin Crossaa255532020-07-03 13:18:24 -07008747 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008748 if ent.Disabled {
8749 continue
8750 }
8751 entries = append(entries, &modAndMkEntries{
8752 mod: mod,
8753 mkEntries: ent,
8754 })
8755 }
8756 }
8757 }
8758
8759 var entry *modAndMkEntries = nil
8760 for _, ent := range entries {
8761 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
8762 if entry != nil {
8763 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
8764 } else {
8765 entry = ent
8766 }
8767 }
8768 }
8769
8770 if entry == nil {
8771 t.Errorf("AndroidMk entry for \"stublib\" missing")
8772 } else {
8773 isPrebuilt := entry.mod.Prebuilt() != nil
8774 if isPrebuilt != test.usePrebuilt {
8775 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
8776 }
8777 if !entry.mod.IsStubs() {
8778 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
8779 }
8780 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
8781 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
8782 }
Jiyong Park892a98f2020-12-14 09:20:00 +09008783 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09008784 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09008785 if !android.InList(expected, cflags) {
8786 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
8787 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008788 }
8789 })
8790 }
8791 })
8792 }
8793}
8794
Martin Stjernholmdf298b32021-05-21 20:57:29 +01008795func TestHostApexInHostOnlyBuild(t *testing.T) {
8796 testApex(t, `
8797 apex {
8798 name: "myapex",
8799 host_supported: true,
8800 key: "myapex.key",
8801 updatable: false,
8802 payload_type: "zip",
8803 }
8804 apex_key {
8805 name: "myapex.key",
8806 public_key: "testkey.avbpubkey",
8807 private_key: "testkey.pem",
8808 }
8809 `,
8810 android.FixtureModifyConfig(func(config android.Config) {
8811 // We may not have device targets in all builds, e.g. in
8812 // prebuilts/build-tools/build-prebuilts.sh
8813 config.Targets[android.Android] = []android.Target{}
8814 }))
8815}
8816
Colin Crossc33e5212021-05-25 18:16:02 -07008817func TestApexJavaCoverage(t *testing.T) {
8818 bp := `
8819 apex {
8820 name: "myapex",
8821 key: "myapex.key",
8822 java_libs: ["mylib"],
8823 bootclasspath_fragments: ["mybootclasspathfragment"],
8824 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
8825 updatable: false,
8826 }
8827
8828 apex_key {
8829 name: "myapex.key",
8830 public_key: "testkey.avbpubkey",
8831 private_key: "testkey.pem",
8832 }
8833
8834 java_library {
8835 name: "mylib",
8836 srcs: ["mylib.java"],
8837 apex_available: ["myapex"],
8838 compile_dex: true,
8839 }
8840
8841 bootclasspath_fragment {
8842 name: "mybootclasspathfragment",
8843 contents: ["mybootclasspathlib"],
8844 apex_available: ["myapex"],
8845 }
8846
8847 java_library {
8848 name: "mybootclasspathlib",
8849 srcs: ["mybootclasspathlib.java"],
8850 apex_available: ["myapex"],
8851 compile_dex: true,
8852 }
8853
8854 systemserverclasspath_fragment {
8855 name: "mysystemserverclasspathfragment",
8856 contents: ["mysystemserverclasspathlib"],
8857 apex_available: ["myapex"],
8858 }
8859
8860 java_library {
8861 name: "mysystemserverclasspathlib",
8862 srcs: ["mysystemserverclasspathlib.java"],
8863 apex_available: ["myapex"],
8864 compile_dex: true,
8865 }
8866 `
8867
8868 result := android.GroupFixturePreparers(
8869 PrepareForTestWithApexBuildComponents,
8870 prepareForTestWithMyapex,
8871 java.PrepareForTestWithJavaDefaultModules,
8872 android.PrepareForTestWithAndroidBuildComponents,
8873 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01008874 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
8875 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Colin Crossc33e5212021-05-25 18:16:02 -07008876 android.FixtureMergeEnv(map[string]string{
8877 "EMMA_INSTRUMENT": "true",
8878 }),
8879 ).RunTest(t)
8880
8881 // Make sure jacoco ran on both mylib and mybootclasspathlib
8882 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8883 t.Errorf("Failed to find jacoco rule for mylib")
8884 }
8885 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8886 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
8887 }
8888 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8889 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
8890 }
8891}
8892
Jiyong Park192600a2021-08-03 07:52:17 +00008893func TestProhibitStaticExecutable(t *testing.T) {
8894 testApexError(t, `executable mybin is static`, `
8895 apex {
8896 name: "myapex",
8897 key: "myapex.key",
8898 binaries: ["mybin"],
8899 min_sdk_version: "29",
8900 }
8901
8902 apex_key {
8903 name: "myapex.key",
8904 public_key: "testkey.avbpubkey",
8905 private_key: "testkey.pem",
8906 }
8907
8908 cc_binary {
8909 name: "mybin",
8910 srcs: ["mylib.cpp"],
8911 relative_install_path: "foo/bar",
8912 static_executable: true,
8913 system_shared_libs: [],
8914 stl: "none",
8915 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09008916 min_sdk_version: "29",
8917 }
8918 `)
8919
8920 testApexError(t, `executable mybin.rust is static`, `
8921 apex {
8922 name: "myapex",
8923 key: "myapex.key",
8924 binaries: ["mybin.rust"],
8925 min_sdk_version: "29",
8926 }
8927
8928 apex_key {
8929 name: "myapex.key",
8930 public_key: "testkey.avbpubkey",
8931 private_key: "testkey.pem",
8932 }
8933
8934 rust_binary {
8935 name: "mybin.rust",
8936 srcs: ["foo.rs"],
8937 static_executable: true,
8938 apex_available: ["myapex"],
8939 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00008940 }
8941 `)
8942}
8943
Jiakai Zhang470b7e22021-09-30 09:34:26 +00008944func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
8945 ctx := testApex(t, `
8946 apex {
8947 name: "myapex",
8948 key: "myapex.key",
8949 updatable: false,
8950 java_libs: ["foo"],
8951 }
8952
8953 apex_key {
8954 name: "myapex.key",
8955 public_key: "testkey.avbpubkey",
8956 private_key: "testkey.pem",
8957 }
8958
8959 java_library {
8960 name: "foo",
8961 srcs: ["foo.java"],
8962 apex_available: ["myapex"],
8963 installable: true,
8964 }
8965 `,
8966 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8967 )
8968
8969 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8970 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
8971 var builder strings.Builder
8972 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
8973 androidMk := builder.String()
8974 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")
8975}
8976
8977func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
8978 ctx := testApex(t, `
8979 prebuilt_apex {
8980 name: "myapex",
8981 arch: {
8982 arm64: {
8983 src: "myapex-arm64.apex",
8984 },
8985 arm: {
8986 src: "myapex-arm.apex",
8987 },
8988 },
8989 exported_java_libs: ["foo"],
8990 }
8991
8992 java_import {
8993 name: "foo",
8994 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00008995 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00008996 }
8997 `,
8998 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8999 )
9000
9001 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9002 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9003 mainModuleEntries := entriesList[0]
9004 android.AssertArrayString(t,
9005 "LOCAL_REQUIRED_MODULES",
9006 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9007 []string{
9008 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9009 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9010 })
9011}
9012
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009013func TestAndroidMk_RequiredModules(t *testing.T) {
9014 ctx := testApex(t, `
9015 apex {
9016 name: "myapex",
9017 key: "myapex.key",
9018 updatable: false,
9019 java_libs: ["foo"],
9020 required: ["otherapex"],
9021 }
9022
9023 apex {
9024 name: "otherapex",
9025 key: "myapex.key",
9026 updatable: false,
9027 java_libs: ["foo"],
9028 required: ["otherapex"],
9029 }
9030
9031 apex_key {
9032 name: "myapex.key",
9033 public_key: "testkey.avbpubkey",
9034 private_key: "testkey.pem",
9035 }
9036
9037 java_library {
9038 name: "foo",
9039 srcs: ["foo.java"],
9040 apex_available: ["myapex", "otherapex"],
9041 installable: true,
9042 }
9043 `)
9044
9045 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9046 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9047 var builder strings.Builder
9048 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9049 androidMk := builder.String()
9050 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherapex")
9051}
9052
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009053func TestAndroidMk_RequiredDeps(t *testing.T) {
9054 ctx := testApex(t, `
9055 apex {
9056 name: "myapex",
9057 key: "myapex.key",
9058 updatable: false,
9059 }
9060
9061 apex_key {
9062 name: "myapex.key",
9063 public_key: "testkey.avbpubkey",
9064 private_key: "testkey.pem",
9065 }
9066 `)
9067
9068 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9069 bundle.requiredDeps = append(bundle.requiredDeps, "foo")
9070 data := android.AndroidMkDataForTest(t, ctx, bundle)
9071 var builder strings.Builder
9072 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9073 androidMk := builder.String()
9074 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += foo")
9075
9076 flattenedBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
9077 flattenedBundle.requiredDeps = append(flattenedBundle.requiredDeps, "foo")
9078 flattenedData := android.AndroidMkDataForTest(t, ctx, flattenedBundle)
9079 var flattenedBuilder strings.Builder
9080 flattenedData.Custom(&flattenedBuilder, flattenedBundle.BaseModuleName(), "TARGET_", "", flattenedData)
9081 flattenedAndroidMk := flattenedBuilder.String()
9082 ensureContains(t, flattenedAndroidMk, "LOCAL_REQUIRED_MODULES += foo")
9083}
9084
Jooyung Hana6d36672022-02-24 13:58:07 +09009085func TestApexOutputFileProducer(t *testing.T) {
9086 for _, tc := range []struct {
9087 name string
9088 ref string
9089 expected_data []string
9090 }{
9091 {
9092 name: "test_using_output",
9093 ref: ":myapex",
9094 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.capex:myapex.capex"},
9095 },
9096 {
9097 name: "test_using_apex",
9098 ref: ":myapex{.apex}",
9099 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.apex:myapex.apex"},
9100 },
9101 } {
9102 t.Run(tc.name, func(t *testing.T) {
9103 ctx := testApex(t, `
9104 apex {
9105 name: "myapex",
9106 key: "myapex.key",
9107 compressible: true,
9108 updatable: false,
9109 }
9110
9111 apex_key {
9112 name: "myapex.key",
9113 public_key: "testkey.avbpubkey",
9114 private_key: "testkey.pem",
9115 }
9116
9117 java_test {
9118 name: "`+tc.name+`",
9119 srcs: ["a.java"],
9120 data: ["`+tc.ref+`"],
9121 }
9122 `,
9123 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9124 variables.CompressedApex = proptools.BoolPtr(true)
9125 }))
9126 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9127 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9128 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9129 })
9130 }
9131}
9132
satayev758968a2021-12-06 11:42:40 +00009133func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9134 preparer := android.GroupFixturePreparers(
9135 PrepareForTestWithApexBuildComponents,
9136 prepareForTestWithMyapex,
9137 java.PrepareForTestWithJavaSdkLibraryFiles,
9138 java.PrepareForTestWithJavaDefaultModules,
9139 android.PrepareForTestWithAndroidBuildComponents,
9140 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9141 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9142 )
9143
9144 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9145 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9146 preparer.RunTestWithBp(t, `
9147 apex {
9148 name: "myapex",
9149 key: "myapex.key",
9150 bootclasspath_fragments: ["mybootclasspathfragment"],
9151 min_sdk_version: "30",
9152 updatable: false,
9153 }
9154
9155 apex_key {
9156 name: "myapex.key",
9157 public_key: "testkey.avbpubkey",
9158 private_key: "testkey.pem",
9159 }
9160
9161 bootclasspath_fragment {
9162 name: "mybootclasspathfragment",
9163 contents: ["mybootclasspathlib"],
9164 apex_available: ["myapex"],
9165 }
9166
9167 java_sdk_library {
9168 name: "mybootclasspathlib",
9169 srcs: ["mybootclasspathlib.java"],
9170 apex_available: ["myapex"],
9171 compile_dex: true,
9172 unsafe_ignore_missing_latest_api: true,
9173 min_sdk_version: "31",
9174 static_libs: ["util"],
9175 }
9176
9177 java_library {
9178 name: "util",
9179 srcs: ["a.java"],
9180 apex_available: ["myapex"],
9181 min_sdk_version: "31",
9182 static_libs: ["another_util"],
9183 }
9184
9185 java_library {
9186 name: "another_util",
9187 srcs: ["a.java"],
9188 min_sdk_version: "31",
9189 apex_available: ["myapex"],
9190 }
9191 `)
9192 })
9193
9194 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9195 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9196 preparer.RunTestWithBp(t, `
9197 apex {
9198 name: "myapex",
9199 key: "myapex.key",
9200 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9201 min_sdk_version: "30",
9202 updatable: false,
9203 }
9204
9205 apex_key {
9206 name: "myapex.key",
9207 public_key: "testkey.avbpubkey",
9208 private_key: "testkey.pem",
9209 }
9210
9211 systemserverclasspath_fragment {
9212 name: "mysystemserverclasspathfragment",
9213 contents: ["mysystemserverclasspathlib"],
9214 apex_available: ["myapex"],
9215 }
9216
9217 java_sdk_library {
9218 name: "mysystemserverclasspathlib",
9219 srcs: ["mysystemserverclasspathlib.java"],
9220 apex_available: ["myapex"],
9221 compile_dex: true,
9222 min_sdk_version: "32",
9223 unsafe_ignore_missing_latest_api: true,
9224 static_libs: ["util"],
9225 }
9226
9227 java_library {
9228 name: "util",
9229 srcs: ["a.java"],
9230 apex_available: ["myapex"],
9231 min_sdk_version: "31",
9232 static_libs: ["another_util"],
9233 }
9234
9235 java_library {
9236 name: "another_util",
9237 srcs: ["a.java"],
9238 min_sdk_version: "31",
9239 apex_available: ["myapex"],
9240 }
9241 `)
9242 })
9243
9244 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9245 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9246 RunTestWithBp(t, `
9247 apex {
9248 name: "myapex",
9249 key: "myapex.key",
9250 bootclasspath_fragments: ["mybootclasspathfragment"],
9251 min_sdk_version: "30",
9252 updatable: false,
9253 }
9254
9255 apex_key {
9256 name: "myapex.key",
9257 public_key: "testkey.avbpubkey",
9258 private_key: "testkey.pem",
9259 }
9260
9261 bootclasspath_fragment {
9262 name: "mybootclasspathfragment",
9263 contents: ["mybootclasspathlib"],
9264 apex_available: ["myapex"],
9265 }
9266
9267 java_sdk_library {
9268 name: "mybootclasspathlib",
9269 srcs: ["mybootclasspathlib.java"],
9270 apex_available: ["myapex"],
9271 compile_dex: true,
9272 unsafe_ignore_missing_latest_api: true,
9273 }
9274 `)
9275 })
9276
9277 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9278 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9279 RunTestWithBp(t, `
9280 apex {
9281 name: "myapex",
9282 key: "myapex.key",
9283 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9284 min_sdk_version: "30",
9285 updatable: false,
9286 }
9287
9288 apex_key {
9289 name: "myapex.key",
9290 public_key: "testkey.avbpubkey",
9291 private_key: "testkey.pem",
9292 }
9293
9294 systemserverclasspath_fragment {
9295 name: "mysystemserverclasspathfragment",
9296 contents: ["mysystemserverclasspathlib"],
9297 apex_available: ["myapex"],
9298 }
9299
9300 java_sdk_library {
9301 name: "mysystemserverclasspathlib",
9302 srcs: ["mysystemserverclasspathlib.java"],
9303 apex_available: ["myapex"],
9304 compile_dex: true,
9305 unsafe_ignore_missing_latest_api: true,
9306 }
9307 `)
9308 })
9309}
9310
Jiakai Zhang6decef92022-01-12 17:56:19 +00009311// Verifies that the APEX depends on all the Make modules in the list.
9312func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9313 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9314 for _, dep := range deps {
9315 android.AssertStringListContains(t, "", a.requiredDeps, dep)
9316 }
9317}
9318
9319// Verifies that the APEX does not depend on any of the Make modules in the list.
9320func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9321 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9322 for _, dep := range deps {
9323 android.AssertStringListDoesNotContain(t, "", a.requiredDeps, dep)
9324 }
9325}
9326
Spandan Das66773252022-01-15 00:23:18 +00009327func TestApexStrictUpdtabilityLint(t *testing.T) {
9328 bpTemplate := `
9329 apex {
9330 name: "myapex",
9331 key: "myapex.key",
9332 java_libs: ["myjavalib"],
9333 updatable: %v,
9334 min_sdk_version: "29",
9335 }
9336 apex_key {
9337 name: "myapex.key",
9338 }
9339 java_library {
9340 name: "myjavalib",
9341 srcs: ["MyClass.java"],
9342 apex_available: [ "myapex" ],
9343 lint: {
9344 strict_updatability_linting: %v,
9345 },
9346 sdk_version: "current",
9347 min_sdk_version: "29",
9348 }
9349 `
9350 fs := android.MockFS{
9351 "lint-baseline.xml": nil,
9352 }
9353
9354 testCases := []struct {
9355 testCaseName string
9356 apexUpdatable bool
9357 javaStrictUpdtabilityLint bool
9358 lintFileExists bool
9359 disallowedFlagExpected bool
9360 }{
9361 {
9362 testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9363 apexUpdatable: true,
9364 javaStrictUpdtabilityLint: true,
9365 lintFileExists: false,
9366 disallowedFlagExpected: false,
9367 },
9368 {
9369 testCaseName: "non-updatable apex respects strict_updatability of javalib",
9370 apexUpdatable: false,
9371 javaStrictUpdtabilityLint: false,
9372 lintFileExists: true,
9373 disallowedFlagExpected: false,
9374 },
9375 {
9376 testCaseName: "non-updatable apex respects strict updatability of javalib",
9377 apexUpdatable: false,
9378 javaStrictUpdtabilityLint: true,
9379 lintFileExists: true,
9380 disallowedFlagExpected: true,
9381 },
9382 {
9383 testCaseName: "updatable apex sets strict updatability of javalib to true",
9384 apexUpdatable: true,
9385 javaStrictUpdtabilityLint: false, // will be set to true by mutator
9386 lintFileExists: true,
9387 disallowedFlagExpected: true,
9388 },
9389 }
9390
9391 for _, testCase := range testCases {
9392 bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
9393 fixtures := []android.FixturePreparer{}
9394 if testCase.lintFileExists {
9395 fixtures = append(fixtures, fs.AddToFixture())
9396 }
9397
9398 result := testApex(t, bp, fixtures...)
9399 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9400 sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9401 disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9402
9403 if disallowedFlagActual != testCase.disallowedFlagExpected {
9404 t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9405 }
9406 }
9407}
9408
Spandan Dasd9c23ab2022-02-10 02:34:13 +00009409func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9410 bp := `
9411 apex {
9412 name: "myapex",
9413 key: "myapex.key",
9414 java_libs: ["myjavalib"],
9415 updatable: true,
9416 min_sdk_version: "29",
9417 }
9418 apex_key {
9419 name: "myapex.key",
9420 }
9421 java_library {
9422 name: "myjavalib",
9423 srcs: ["MyClass.java"],
9424 apex_available: [ "myapex" ],
9425 sdk_version: "current",
9426 min_sdk_version: "29",
9427 }
9428 `
9429
9430 testCases := []struct {
9431 testCaseName string
9432 moduleDirectory string
9433 disallowedFlagExpected bool
9434 }{
9435 {
9436 testCaseName: "lintable module defined outside libcore",
9437 moduleDirectory: "",
9438 disallowedFlagExpected: true,
9439 },
9440 {
9441 testCaseName: "lintable module defined in libcore root directory",
9442 moduleDirectory: "libcore/",
9443 disallowedFlagExpected: false,
9444 },
9445 {
9446 testCaseName: "lintable module defined in libcore child directory",
9447 moduleDirectory: "libcore/childdir/",
9448 disallowedFlagExpected: true,
9449 },
9450 }
9451
9452 for _, testCase := range testCases {
9453 lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9454 bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9455 result := testApex(t, "", lintFileCreator, bpFileCreator)
9456 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9457 sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9458 cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9459 disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9460
9461 if disallowedFlagActual != testCase.disallowedFlagExpected {
9462 t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9463 }
9464 }
9465}
9466
Spandan Das66773252022-01-15 00:23:18 +00009467// checks transtive deps of an apex coming from bootclasspath_fragment
9468func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9469 bp := `
9470 apex {
9471 name: "myapex",
9472 key: "myapex.key",
9473 bootclasspath_fragments: ["mybootclasspathfragment"],
9474 updatable: true,
9475 min_sdk_version: "29",
9476 }
9477 apex_key {
9478 name: "myapex.key",
9479 }
9480 bootclasspath_fragment {
9481 name: "mybootclasspathfragment",
9482 contents: ["myjavalib"],
9483 apex_available: ["myapex"],
9484 }
9485 java_library {
9486 name: "myjavalib",
9487 srcs: ["MyClass.java"],
9488 apex_available: [ "myapex" ],
9489 sdk_version: "current",
9490 min_sdk_version: "29",
9491 compile_dex: true,
9492 }
9493 `
9494 fs := android.MockFS{
9495 "lint-baseline.xml": nil,
9496 }
9497
9498 result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
9499 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9500 sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9501 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
9502 t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
9503 }
9504}
9505
Spandan Das91250b12022-05-06 22:12:55 +00009506// updatable apexes should propagate updatable=true to its apps
9507func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
9508 bp := `
9509 apex {
9510 name: "myapex",
9511 key: "myapex.key",
9512 updatable: %v,
9513 apps: [
9514 "myapp",
9515 ],
9516 min_sdk_version: "30",
9517 }
9518 apex_key {
9519 name: "myapex.key",
9520 }
9521 android_app {
9522 name: "myapp",
9523 updatable: %v,
9524 apex_available: [
9525 "myapex",
9526 ],
9527 sdk_version: "current",
9528 min_sdk_version: "30",
9529 }
9530 `
9531 testCases := []struct {
9532 name string
9533 apex_is_updatable_bp bool
9534 app_is_updatable_bp bool
9535 app_is_updatable_expected bool
9536 }{
9537 {
9538 name: "Non-updatable apex respects updatable property of non-updatable app",
9539 apex_is_updatable_bp: false,
9540 app_is_updatable_bp: false,
9541 app_is_updatable_expected: false,
9542 },
9543 {
9544 name: "Non-updatable apex respects updatable property of updatable app",
9545 apex_is_updatable_bp: false,
9546 app_is_updatable_bp: true,
9547 app_is_updatable_expected: true,
9548 },
9549 {
9550 name: "Updatable apex respects updatable property of updatable app",
9551 apex_is_updatable_bp: true,
9552 app_is_updatable_bp: true,
9553 app_is_updatable_expected: true,
9554 },
9555 {
9556 name: "Updatable apex sets updatable=true on non-updatable app",
9557 apex_is_updatable_bp: true,
9558 app_is_updatable_bp: false,
9559 app_is_updatable_expected: true,
9560 },
9561 }
9562 for _, testCase := range testCases {
9563 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
9564 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
9565 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
9566 }
9567}
9568
Jiyong Park25fc6a92018-11-18 18:02:45 +09009569func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01009570 os.Exit(m.Run())
Jiyong Park25fc6a92018-11-18 18:02:45 +09009571}