blob: 663a9a0b4368ed2ae1707dd5be3f0b882cca3850 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +090018 "fmt"
Jiyong Park25fc6a92018-11-18 18:02:45 +090019 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Paul Duffin37856732021-02-26 14:24:15 +000021 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070022 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010023 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090024 "sort"
Jiyong Parkd4a3a132021-03-17 20:21:35 +090025 "strconv"
Jiyong Park25fc6a92018-11-18 18:02:45 +090026 "strings"
27 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090028
29 "github.com/google/blueprint/proptools"
30
31 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080032 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090033 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000034 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090036 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090037 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090039)
40
Jooyung Hand3639552019-08-09 12:57:43 +090041// names returns name list from white space separated string
42func names(s string) (ns []string) {
43 for _, n := range strings.Split(s, " ") {
44 if len(n) > 0 {
45 ns = append(ns, n)
46 }
47 }
48 return
49}
50
Paul Duffin40b62572021-03-20 11:39:01 +000051func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090052 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010053 android.GroupFixturePreparers(
54 prepareForApexTest,
55 android.GroupFixturePreparers(preparers...),
56 ).
Paul Duffine05480a2021-03-08 15:07:14 +000057 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000058 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090059}
60
Paul Duffin40b62572021-03-20 11:39:01 +000061func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090062 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010063
64 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000065 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010066 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000067 }
Paul Duffin284165a2021-03-29 01:50:31 +010068
69 result := android.GroupFixturePreparers(
70 prepareForApexTest,
71 android.GroupFixturePreparers(preparers...),
72 optionalBpPreparer,
73 ).RunTest(t)
74
Paul Duffine05480a2021-03-08 15:07:14 +000075 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090076}
77
Paul Duffin810f33d2021-03-09 14:12:32 +000078func withFiles(files android.MockFS) android.FixturePreparer {
79 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090080}
81
Paul Duffin810f33d2021-03-09 14:12:32 +000082func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
83 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090084 for k, v := range targets {
85 config.Targets[k] = v
86 }
Paul Duffin810f33d2021-03-09 14:12:32 +000087 })
Jooyung Han344d5432019-08-23 11:17:39 +090088}
89
Jooyung Han35155c42020-02-06 17:33:20 +090090// withNativeBridgeTargets sets configuration with targets including:
91// - X86_64 (primary)
92// - X86 (secondary)
93// - Arm64 on X86_64 (native bridge)
94// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000095var withNativeBridgeEnabled = android.FixtureModifyConfig(
96 func(config android.Config) {
97 config.Targets[android.Android] = []android.Target{
98 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
99 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
100 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
101 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
102 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
103 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
104 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
105 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
106 }
107 },
108)
109
110func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
111 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
112 variables.ManifestPackageNameOverrides = specs
113 })
Jooyung Han35155c42020-02-06 17:33:20 +0900114}
115
Paul Duffin810f33d2021-03-09 14:12:32 +0000116var withBinder32bit = android.FixtureModifyProductVariables(
117 func(variables android.FixtureProductVariables) {
118 variables.Binder32bit = proptools.BoolPtr(true)
119 },
120)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900121
Paul Duffin810f33d2021-03-09 14:12:32 +0000122var withUnbundledBuild = android.FixtureModifyProductVariables(
123 func(variables android.FixtureProductVariables) {
124 variables.Unbundled_build = proptools.BoolPtr(true)
125 },
126)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900127
Paul Duffin284165a2021-03-29 01:50:31 +0100128// Legacy preparer used for running tests within the apex package.
129//
130// This includes everything that was needed to run any test in the apex package prior to the
131// introduction of the test fixtures. Tests that are being converted to use fixtures directly
132// rather than through the testApex...() methods should avoid using this and instead use the
133// various preparers directly, using android.GroupFixturePreparers(...) to group them when
134// necessary.
135//
136// deprecated
137var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000138 // General preparers in alphabetical order as test infrastructure will enforce correct
139 // registration order.
140 android.PrepareForTestWithAndroidBuildComponents,
141 bpf.PrepareForTestWithBpf,
142 cc.PrepareForTestWithCcBuildComponents,
143 java.PrepareForTestWithJavaDefaultModules,
144 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
145 rust.PrepareForTestWithRustDefaultModules,
146 sh.PrepareForTestWithShBuildComponents,
147
148 PrepareForTestWithApexBuildComponents,
149
150 // Additional apex test specific preparers.
151 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
152 filegroup {
153 name: "myapex-file_contexts",
154 srcs: [
155 "apex/myapex-file_contexts",
156 ],
157 }
158 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000159 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000160 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000161 "a.java": nil,
162 "PrebuiltAppFoo.apk": nil,
163 "PrebuiltAppFooPriv.apk": nil,
164 "apex_manifest.json": nil,
165 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000166 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
167 "system/sepolicy/apex/myapex2-file_contexts": nil,
168 "system/sepolicy/apex/otherapex-file_contexts": nil,
169 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
170 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
171 "mylib.cpp": nil,
172 "mytest.cpp": nil,
173 "mytest1.cpp": nil,
174 "mytest2.cpp": nil,
175 "mytest3.cpp": nil,
176 "myprebuilt": nil,
177 "my_include": nil,
178 "foo/bar/MyClass.java": nil,
179 "prebuilt.jar": nil,
180 "prebuilt.so": nil,
181 "vendor/foo/devkeys/test.x509.pem": nil,
182 "vendor/foo/devkeys/test.pk8": nil,
183 "testkey.x509.pem": nil,
184 "testkey.pk8": nil,
185 "testkey.override.x509.pem": nil,
186 "testkey.override.pk8": nil,
187 "vendor/foo/devkeys/testkey.avbpubkey": nil,
188 "vendor/foo/devkeys/testkey.pem": nil,
189 "NOTICE": nil,
190 "custom_notice": nil,
191 "custom_notice_for_static_lib": nil,
192 "testkey2.avbpubkey": nil,
193 "testkey2.pem": nil,
194 "myapex-arm64.apex": nil,
195 "myapex-arm.apex": nil,
196 "myapex.apks": nil,
197 "frameworks/base/api/current.txt": nil,
198 "framework/aidl/a.aidl": nil,
199 "build/make/core/proguard.flags": nil,
200 "build/make/core/proguard_basic_keeps.flags": nil,
201 "dummy.txt": nil,
202 "baz": nil,
203 "bar/baz": nil,
204 "testdata/baz": nil,
205 "AppSet.apks": nil,
206 "foo.rs": nil,
207 "libfoo.jar": nil,
208 "libbar.jar": nil,
209 },
210 ),
211
212 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
213 variables.DeviceVndkVersion = proptools.StringPtr("current")
214 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
215 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
216 variables.Platform_sdk_codename = proptools.StringPtr("Q")
217 variables.Platform_sdk_final = proptools.BoolPtr(false)
218 variables.Platform_version_active_codenames = []string{"Q"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900219 variables.Platform_vndk_version = proptools.StringPtr("29")
Paul Duffin37aad602021-03-08 09:47:16 +0000220 }),
221)
222
Paul Duffin52bfaa42021-03-23 23:40:12 +0000223var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
224 "system/sepolicy/apex/myapex-file_contexts": nil,
225})
226
Jooyung Han643adc42020-02-27 13:50:06 +0900227// ensure that 'result' equals 'expected'
228func ensureEquals(t *testing.T, result string, expected string) {
229 t.Helper()
230 if result != expected {
231 t.Errorf("%q != %q", expected, result)
232 }
233}
234
Jiyong Park25fc6a92018-11-18 18:02:45 +0900235// ensure that 'result' contains 'expected'
236func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900237 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900238 if !strings.Contains(result, expected) {
239 t.Errorf("%q is not found in %q", expected, result)
240 }
241}
242
Liz Kammer5bd365f2020-05-27 15:15:11 -0700243// ensure that 'result' contains 'expected' exactly one time
244func ensureContainsOnce(t *testing.T, result string, expected string) {
245 t.Helper()
246 count := strings.Count(result, expected)
247 if count != 1 {
248 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
249 }
250}
251
Jiyong Park25fc6a92018-11-18 18:02:45 +0900252// ensures that 'result' does not contain 'notExpected'
253func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900254 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900255 if strings.Contains(result, notExpected) {
256 t.Errorf("%q is found in %q", notExpected, result)
257 }
258}
259
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700260func ensureMatches(t *testing.T, result string, expectedRex string) {
261 ok, err := regexp.MatchString(expectedRex, result)
262 if err != nil {
263 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
264 return
265 }
266 if !ok {
267 t.Errorf("%s does not match regular expession %s", result, expectedRex)
268 }
269}
270
Jiyong Park25fc6a92018-11-18 18:02:45 +0900271func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900272 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900273 if !android.InList(expected, result) {
274 t.Errorf("%q is not found in %v", expected, result)
275 }
276}
277
278func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900279 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 if android.InList(notExpected, result) {
281 t.Errorf("%q is found in %v", notExpected, result)
282 }
283}
284
Jooyung Hane1633032019-08-01 17:41:43 +0900285func ensureListEmpty(t *testing.T, result []string) {
286 t.Helper()
287 if len(result) > 0 {
288 t.Errorf("%q is expected to be empty", result)
289 }
290}
291
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000292func ensureListNotEmpty(t *testing.T, result []string) {
293 t.Helper()
294 if len(result) == 0 {
295 t.Errorf("%q is expected to be not empty", result)
296 }
297}
298
Jiyong Park25fc6a92018-11-18 18:02:45 +0900299// Minimal test
300func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800301 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900302 apex_defaults {
303 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900304 manifest: ":myapex.manifest",
305 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900306 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900307 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900308 native_shared_libs: [
309 "mylib",
310 "libfoo.ffi",
311 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900312 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800313 multilib: {
314 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900315 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800316 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900317 },
Jiyong Park77acec62020-06-01 21:39:15 +0900318 java_libs: [
319 "myjar",
320 "myjar_dex",
321 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000322 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323 }
324
Jiyong Park30ca9372019-02-07 16:27:23 +0900325 apex {
326 name: "myapex",
327 defaults: ["myapex-defaults"],
328 }
329
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 apex_key {
331 name: "myapex.key",
332 public_key: "testkey.avbpubkey",
333 private_key: "testkey.pem",
334 }
335
Jiyong Park809bb722019-02-13 21:33:49 +0900336 filegroup {
337 name: "myapex.manifest",
338 srcs: ["apex_manifest.json"],
339 }
340
341 filegroup {
342 name: "myapex.androidmanifest",
343 srcs: ["AndroidManifest.xml"],
344 }
345
Jiyong Park25fc6a92018-11-18 18:02:45 +0900346 cc_library {
347 name: "mylib",
348 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900349 shared_libs: [
350 "mylib2",
351 "libbar.ffi",
352 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353 system_shared_libs: [],
354 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000355 // TODO: remove //apex_available:platform
356 apex_available: [
357 "//apex_available:platform",
358 "myapex",
359 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900360 }
361
Alex Light3d673592019-01-18 14:37:31 -0800362 cc_binary {
363 name: "foo",
364 srcs: ["mylib.cpp"],
365 compile_multilib: "both",
366 multilib: {
367 lib32: {
368 suffix: "32",
369 },
370 lib64: {
371 suffix: "64",
372 },
373 },
374 symlinks: ["foo_link_"],
375 symlink_preferred_arch: true,
376 system_shared_libs: [],
377 static_executable: true,
378 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700379 apex_available: [ "myapex", "com.android.gki.*" ],
380 }
381
Jiyong Park99644e92020-11-17 22:21:02 +0900382 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000383 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900384 srcs: ["foo.rs"],
385 rlibs: ["libfoo.rlib.rust"],
386 dylibs: ["libfoo.dylib.rust"],
387 apex_available: ["myapex"],
388 }
389
390 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000391 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900392 srcs: ["foo.rs"],
393 crate_name: "foo",
394 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900395 shared_libs: ["libfoo.shared_from_rust"],
396 }
397
398 cc_library_shared {
399 name: "libfoo.shared_from_rust",
400 srcs: ["mylib.cpp"],
401 system_shared_libs: [],
402 stl: "none",
403 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900404 }
405
406 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000407 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900408 srcs: ["foo.rs"],
409 crate_name: "foo",
410 apex_available: ["myapex"],
411 }
412
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900413 rust_ffi_shared {
414 name: "libfoo.ffi",
415 srcs: ["foo.rs"],
416 crate_name: "foo",
417 apex_available: ["myapex"],
418 }
419
420 rust_ffi_shared {
421 name: "libbar.ffi",
422 srcs: ["foo.rs"],
423 crate_name: "bar",
424 apex_available: ["myapex"],
425 }
426
Yifan Hongd22a84a2020-07-28 17:37:46 -0700427 apex {
428 name: "com.android.gki.fake",
429 binaries: ["foo"],
430 key: "myapex.key",
431 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000432 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800433 }
434
Paul Duffindddd5462020-04-07 15:25:44 +0100435 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900436 name: "mylib2",
437 srcs: ["mylib.cpp"],
438 system_shared_libs: [],
439 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900440 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900441 static_libs: ["libstatic"],
442 // TODO: remove //apex_available:platform
443 apex_available: [
444 "//apex_available:platform",
445 "myapex",
446 ],
447 }
448
Paul Duffindddd5462020-04-07 15:25:44 +0100449 cc_prebuilt_library_shared {
450 name: "mylib2",
451 srcs: ["prebuilt.so"],
452 // TODO: remove //apex_available:platform
453 apex_available: [
454 "//apex_available:platform",
455 "myapex",
456 ],
457 }
458
Jiyong Park9918e1a2020-03-17 19:16:40 +0900459 cc_library_static {
460 name: "libstatic",
461 srcs: ["mylib.cpp"],
462 system_shared_libs: [],
463 stl: "none",
464 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000465 // TODO: remove //apex_available:platform
466 apex_available: [
467 "//apex_available:platform",
468 "myapex",
469 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900470 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900471
472 java_library {
473 name: "myjar",
474 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900475 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900476 sdk_version: "none",
477 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900478 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900479 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000480 // TODO: remove //apex_available:platform
481 apex_available: [
482 "//apex_available:platform",
483 "myapex",
484 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900485 }
486
Jiyong Park77acec62020-06-01 21:39:15 +0900487 dex_import {
488 name: "myjar_dex",
489 jars: ["prebuilt.jar"],
490 apex_available: [
491 "//apex_available:platform",
492 "myapex",
493 ],
494 }
495
Jiyong Park7f7766d2019-07-25 22:02:35 +0900496 java_library {
497 name: "myotherjar",
498 srcs: ["foo/bar/MyClass.java"],
499 sdk_version: "none",
500 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900501 // TODO: remove //apex_available:platform
502 apex_available: [
503 "//apex_available:platform",
504 "myapex",
505 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900506 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900507
508 java_library {
509 name: "mysharedjar",
510 srcs: ["foo/bar/MyClass.java"],
511 sdk_version: "none",
512 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900513 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900514 `)
515
Paul Duffina71a67a2021-03-29 00:42:57 +0100516 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900517
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900518 // Make sure that Android.mk is created
519 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700520 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900521 var builder strings.Builder
522 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
523
524 androidMk := builder.String()
525 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
526 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
527
Jiyong Park42cca6c2019-04-01 11:15:50 +0900528 optFlags := apexRule.Args["opt_flags"]
529 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700530 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100531 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900532
Jiyong Park25fc6a92018-11-18 18:02:45 +0900533 copyCmds := apexRule.Args["copy_commands"]
534
535 // Ensure that main rule creates an output
536 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
537
538 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700539 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
540 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
541 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900542 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900543 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900544
545 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700546 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
547 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900548 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
549 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900550 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900551 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900552
553 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800554 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
555 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900556 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900557 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900558 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900559 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
560 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900561 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900562 // .. but not for java libs
563 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900564 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800565
Colin Cross7113d202019-11-20 16:39:12 -0800566 // Ensure that the platform variant ends with _shared or _common
567 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
568 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900569 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
570 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900571 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
572
573 // Ensure that dynamic dependency to java libs are not included
574 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800575
576 // Ensure that all symlinks are present.
577 found_foo_link_64 := false
578 found_foo := false
579 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900580 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800581 if strings.HasSuffix(cmd, "bin/foo") {
582 found_foo = true
583 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
584 found_foo_link_64 = true
585 }
586 }
587 }
588 good := found_foo && found_foo_link_64
589 if !good {
590 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
591 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900592
Sundong Ahnabb64432019-10-22 13:58:29 +0900593 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700594 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900595 if len(noticeInputs) != 3 {
596 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900597 }
598 ensureListContains(t, noticeInputs, "NOTICE")
599 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900600 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900601
Artur Satayeva8bd1132020-04-27 18:07:06 +0100602 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100603 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100604 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
605 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
606 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100607
608 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100609 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100610 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
611 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
612 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800613}
614
Jooyung Hanf21c7972019-12-16 22:32:06 +0900615func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800616 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900617 apex_defaults {
618 name: "myapex-defaults",
619 key: "myapex.key",
620 prebuilts: ["myetc"],
621 native_shared_libs: ["mylib"],
622 java_libs: ["myjar"],
623 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900624 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800625 bpfs: ["bpf"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000626 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900627 }
628
629 prebuilt_etc {
630 name: "myetc",
631 src: "myprebuilt",
632 }
633
634 apex {
635 name: "myapex",
636 defaults: ["myapex-defaults"],
637 }
638
639 apex_key {
640 name: "myapex.key",
641 public_key: "testkey.avbpubkey",
642 private_key: "testkey.pem",
643 }
644
645 cc_library {
646 name: "mylib",
647 system_shared_libs: [],
648 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000649 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900650 }
651
652 java_library {
653 name: "myjar",
654 srcs: ["foo/bar/MyClass.java"],
655 sdk_version: "none",
656 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000657 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900658 }
659
660 android_app {
661 name: "AppFoo",
662 srcs: ["foo/bar/MyClass.java"],
663 sdk_version: "none",
664 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000665 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900666 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900667
668 runtime_resource_overlay {
669 name: "rro",
670 theme: "blue",
671 }
672
markchien2f59ec92020-09-02 16:23:38 +0800673 bpf {
674 name: "bpf",
675 srcs: ["bpf.c", "bpf2.c"],
676 }
677
Jooyung Hanf21c7972019-12-16 22:32:06 +0900678 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000679 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900680 "etc/myetc",
681 "javalib/myjar.jar",
682 "lib64/mylib.so",
683 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900684 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800685 "etc/bpf/bpf.o",
686 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900687 })
688}
689
Jooyung Han01a3ee22019-11-02 02:52:25 +0900690func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800691 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900692 apex {
693 name: "myapex",
694 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000695 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900696 }
697
698 apex_key {
699 name: "myapex.key",
700 public_key: "testkey.avbpubkey",
701 private_key: "testkey.pem",
702 }
703 `)
704
705 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900706 args := module.Rule("apexRule").Args
707 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
708 t.Error("manifest should be apex_manifest.pb, but " + manifest)
709 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900710}
711
Alex Light5098a612018-11-29 17:12:15 -0800712func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800713 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800714 apex {
715 name: "myapex",
716 key: "myapex.key",
717 payload_type: "zip",
718 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000719 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800720 }
721
722 apex_key {
723 name: "myapex.key",
724 public_key: "testkey.avbpubkey",
725 private_key: "testkey.pem",
726 }
727
728 cc_library {
729 name: "mylib",
730 srcs: ["mylib.cpp"],
731 shared_libs: ["mylib2"],
732 system_shared_libs: [],
733 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000734 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800735 }
736
737 cc_library {
738 name: "mylib2",
739 srcs: ["mylib.cpp"],
740 system_shared_libs: [],
741 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000742 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800743 }
744 `)
745
Sundong Ahnabb64432019-10-22 13:58:29 +0900746 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800747 copyCmds := zipApexRule.Args["copy_commands"]
748
749 // Ensure that main rule creates an output
750 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
751
752 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700753 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800754
755 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700756 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800757
758 // Ensure that both direct and indirect deps are copied into apex
759 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
760 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900761}
762
763func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800764 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900765 apex {
766 name: "myapex",
767 key: "myapex.key",
768 native_shared_libs: ["mylib", "mylib3"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000769 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900770 }
771
772 apex_key {
773 name: "myapex.key",
774 public_key: "testkey.avbpubkey",
775 private_key: "testkey.pem",
776 }
777
778 cc_library {
779 name: "mylib",
780 srcs: ["mylib.cpp"],
781 shared_libs: ["mylib2", "mylib3"],
782 system_shared_libs: [],
783 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000784 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900785 }
786
787 cc_library {
788 name: "mylib2",
789 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900790 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900791 system_shared_libs: [],
792 stl: "none",
793 stubs: {
794 versions: ["1", "2", "3"],
795 },
796 }
797
798 cc_library {
799 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900800 srcs: ["mylib.cpp"],
801 shared_libs: ["mylib4"],
802 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900803 stl: "none",
804 stubs: {
805 versions: ["10", "11", "12"],
806 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000807 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900808 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900809
810 cc_library {
811 name: "mylib4",
812 srcs: ["mylib.cpp"],
813 system_shared_libs: [],
814 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000815 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900816 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900817 `)
818
Sundong Ahnabb64432019-10-22 13:58:29 +0900819 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900820 copyCmds := apexRule.Args["copy_commands"]
821
822 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800823 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900824
825 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800826 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900827
828 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800829 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900830
Colin Crossaede88c2020-08-11 12:17:01 -0700831 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900832
833 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900834 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900835 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900836 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900837
838 // 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 -0700839 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900840 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700841 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900842
843 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900844 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900845 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900846
847 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700848 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900849
Jooyung Hana57af4a2020-01-23 05:36:59 +0000850 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900851 "lib64/mylib.so",
852 "lib64/mylib3.so",
853 "lib64/mylib4.so",
854 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900855}
856
Colin Cross7812fd32020-09-25 12:35:10 -0700857func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
858 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -0800859 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -0700860 apex {
861 name: "myapex",
862 key: "myapex.key",
863 native_shared_libs: ["mylib", "mylib3"],
864 min_sdk_version: "29",
865 }
866
867 apex_key {
868 name: "myapex.key",
869 public_key: "testkey.avbpubkey",
870 private_key: "testkey.pem",
871 }
872
873 cc_library {
874 name: "mylib",
875 srcs: ["mylib.cpp"],
876 shared_libs: ["mylib2", "mylib3"],
877 system_shared_libs: [],
878 stl: "none",
879 apex_available: [ "myapex" ],
880 min_sdk_version: "28",
881 }
882
883 cc_library {
884 name: "mylib2",
885 srcs: ["mylib.cpp"],
886 cflags: ["-include mylib.h"],
887 system_shared_libs: [],
888 stl: "none",
889 stubs: {
890 versions: ["28", "29", "30", "current"],
891 },
892 min_sdk_version: "28",
893 }
894
895 cc_library {
896 name: "mylib3",
897 srcs: ["mylib.cpp"],
898 shared_libs: ["mylib4"],
899 system_shared_libs: [],
900 stl: "none",
901 stubs: {
902 versions: ["28", "29", "30", "current"],
903 },
904 apex_available: [ "myapex" ],
905 min_sdk_version: "28",
906 }
907
908 cc_library {
909 name: "mylib4",
910 srcs: ["mylib.cpp"],
911 system_shared_libs: [],
912 stl: "none",
913 apex_available: [ "myapex" ],
914 min_sdk_version: "28",
915 }
916 `)
917
918 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
919 copyCmds := apexRule.Args["copy_commands"]
920
921 // Ensure that direct non-stubs dep is always included
922 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
923
924 // Ensure that indirect stubs dep is not included
925 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
926
927 // Ensure that direct stubs dep is included
928 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
929
930 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
931
Jiyong Park55549df2021-02-26 23:57:23 +0900932 // Ensure that mylib is linking with the latest version of stub for mylib2
933 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -0700934 // ... and not linking to the non-stub (impl) variant of mylib2
935 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
936
937 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
938 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
939 // .. and not linking to the stubs variant of mylib3
940 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
941
942 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -0700943 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -0700944 ensureNotContains(t, mylib2Cflags, "-include ")
945
946 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700947 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -0700948
949 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
950 "lib64/mylib.so",
951 "lib64/mylib3.so",
952 "lib64/mylib4.so",
953 })
954}
955
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900956func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
957 t.Parallel()
958 // myapex (Z)
959 // mylib -----------------.
960 // |
961 // otherapex (29) |
962 // libstub's versions: 29 Z current
963 // |
964 // <platform> |
965 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -0800966 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900967 apex {
968 name: "myapex",
969 key: "myapex.key",
970 native_shared_libs: ["mylib"],
971 min_sdk_version: "Z", // non-final
972 }
973
974 cc_library {
975 name: "mylib",
976 srcs: ["mylib.cpp"],
977 shared_libs: ["libstub"],
978 apex_available: ["myapex"],
979 min_sdk_version: "Z",
980 }
981
982 apex_key {
983 name: "myapex.key",
984 public_key: "testkey.avbpubkey",
985 private_key: "testkey.pem",
986 }
987
988 apex {
989 name: "otherapex",
990 key: "myapex.key",
991 native_shared_libs: ["libstub"],
992 min_sdk_version: "29",
993 }
994
995 cc_library {
996 name: "libstub",
997 srcs: ["mylib.cpp"],
998 stubs: {
999 versions: ["29", "Z", "current"],
1000 },
1001 apex_available: ["otherapex"],
1002 min_sdk_version: "29",
1003 }
1004
1005 // platform module depending on libstub from otherapex should use the latest stub("current")
1006 cc_library {
1007 name: "libplatform",
1008 srcs: ["mylib.cpp"],
1009 shared_libs: ["libstub"],
1010 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001011 `,
1012 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1013 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1014 variables.Platform_sdk_final = proptools.BoolPtr(false)
1015 variables.Platform_version_active_codenames = []string{"Z"}
1016 }),
1017 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001018
Jiyong Park55549df2021-02-26 23:57:23 +09001019 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001020 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001021 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001022 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001023 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001024
1025 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1026 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1027 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1028 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1029 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1030}
1031
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001032func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001033 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001034 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001035 name: "myapex2",
1036 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001037 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001038 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001039 }
1040
1041 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001042 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001043 public_key: "testkey.avbpubkey",
1044 private_key: "testkey.pem",
1045 }
1046
1047 cc_library {
1048 name: "mylib",
1049 srcs: ["mylib.cpp"],
1050 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001051 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001052 system_shared_libs: [],
1053 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001054 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001055 }
1056
1057 cc_library {
1058 name: "libfoo",
1059 srcs: ["mylib.cpp"],
1060 shared_libs: ["libbar"],
1061 system_shared_libs: [],
1062 stl: "none",
1063 stubs: {
1064 versions: ["10", "20", "30"],
1065 },
1066 }
1067
1068 cc_library {
1069 name: "libbar",
1070 srcs: ["mylib.cpp"],
1071 system_shared_libs: [],
1072 stl: "none",
1073 }
1074
Jiyong Park678c8812020-02-07 17:25:49 +09001075 cc_library_static {
1076 name: "libbaz",
1077 srcs: ["mylib.cpp"],
1078 system_shared_libs: [],
1079 stl: "none",
1080 apex_available: [ "myapex2" ],
1081 }
1082
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001083 `)
1084
Jiyong Park83dc74b2020-01-14 18:38:44 +09001085 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001086 copyCmds := apexRule.Args["copy_commands"]
1087
1088 // Ensure that direct non-stubs dep is always included
1089 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1090
1091 // Ensure that indirect stubs dep is not included
1092 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1093
1094 // Ensure that dependency of stubs is not included
1095 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1096
Colin Crossaede88c2020-08-11 12:17:01 -07001097 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001098
1099 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001100 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001101 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001102 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001103
Jiyong Park3ff16992019-12-27 14:11:47 +09001104 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001105
1106 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1107 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001108
Artur Satayeva8bd1132020-04-27 18:07:06 +01001109 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001110 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001111
Artur Satayeva8bd1132020-04-27 18:07:06 +01001112 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001113 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001114}
1115
Jooyung Hand3639552019-08-09 12:57:43 +09001116func TestApexWithRuntimeLibsDependency(t *testing.T) {
1117 /*
1118 myapex
1119 |
1120 v (runtime_libs)
1121 mylib ------+------> libfoo [provides stub]
1122 |
1123 `------> libbar
1124 */
Colin Cross1c460562021-02-16 17:55:47 -08001125 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001126 apex {
1127 name: "myapex",
1128 key: "myapex.key",
1129 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001130 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001131 }
1132
1133 apex_key {
1134 name: "myapex.key",
1135 public_key: "testkey.avbpubkey",
1136 private_key: "testkey.pem",
1137 }
1138
1139 cc_library {
1140 name: "mylib",
1141 srcs: ["mylib.cpp"],
1142 runtime_libs: ["libfoo", "libbar"],
1143 system_shared_libs: [],
1144 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001145 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001146 }
1147
1148 cc_library {
1149 name: "libfoo",
1150 srcs: ["mylib.cpp"],
1151 system_shared_libs: [],
1152 stl: "none",
1153 stubs: {
1154 versions: ["10", "20", "30"],
1155 },
1156 }
1157
1158 cc_library {
1159 name: "libbar",
1160 srcs: ["mylib.cpp"],
1161 system_shared_libs: [],
1162 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001163 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001164 }
1165
1166 `)
1167
Sundong Ahnabb64432019-10-22 13:58:29 +09001168 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001169 copyCmds := apexRule.Args["copy_commands"]
1170
1171 // Ensure that direct non-stubs dep is always included
1172 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1173
1174 // Ensure that indirect stubs dep is not included
1175 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1176
1177 // Ensure that runtime_libs dep in included
1178 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1179
Sundong Ahnabb64432019-10-22 13:58:29 +09001180 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001181 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1182 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001183
1184}
1185
Paul Duffina02cae32021-03-09 01:44:06 +00001186var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1187 cc.PrepareForTestWithCcBuildComponents,
1188 PrepareForTestWithApexBuildComponents,
1189 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001190 apex {
1191 name: "com.android.runtime",
1192 key: "com.android.runtime.key",
1193 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001194 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001195 }
1196
1197 apex_key {
1198 name: "com.android.runtime.key",
1199 public_key: "testkey.avbpubkey",
1200 private_key: "testkey.pem",
1201 }
Paul Duffina02cae32021-03-09 01:44:06 +00001202 `),
1203 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1204)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001205
Paul Duffina02cae32021-03-09 01:44:06 +00001206func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001207 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001208 cc_library {
1209 name: "libc",
1210 no_libcrt: true,
1211 nocrt: true,
1212 stl: "none",
1213 system_shared_libs: [],
1214 stubs: { versions: ["1"] },
1215 apex_available: ["com.android.runtime"],
1216
1217 sanitize: {
1218 hwaddress: true,
1219 }
1220 }
1221
1222 cc_prebuilt_library_shared {
1223 name: "libclang_rt.hwasan-aarch64-android",
1224 no_libcrt: true,
1225 nocrt: true,
1226 stl: "none",
1227 system_shared_libs: [],
1228 srcs: [""],
1229 stubs: { versions: ["1"] },
1230
1231 sanitize: {
1232 never: true,
1233 },
Paul Duffina02cae32021-03-09 01:44:06 +00001234 } `)
1235 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001236
1237 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1238 "lib64/bionic/libc.so",
1239 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1240 })
1241
1242 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1243
1244 installed := hwasan.Description("install libclang_rt.hwasan")
1245 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1246
1247 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1248 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1249 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1250}
1251
1252func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001253 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001254 prepareForTestOfRuntimeApexWithHwasan,
1255 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1256 variables.SanitizeDevice = []string{"hwaddress"}
1257 }),
1258 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001259 cc_library {
1260 name: "libc",
1261 no_libcrt: true,
1262 nocrt: true,
1263 stl: "none",
1264 system_shared_libs: [],
1265 stubs: { versions: ["1"] },
1266 apex_available: ["com.android.runtime"],
1267 }
1268
1269 cc_prebuilt_library_shared {
1270 name: "libclang_rt.hwasan-aarch64-android",
1271 no_libcrt: true,
1272 nocrt: true,
1273 stl: "none",
1274 system_shared_libs: [],
1275 srcs: [""],
1276 stubs: { versions: ["1"] },
1277
1278 sanitize: {
1279 never: true,
1280 },
1281 }
Paul Duffina02cae32021-03-09 01:44:06 +00001282 `)
1283 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001284
1285 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1286 "lib64/bionic/libc.so",
1287 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1288 })
1289
1290 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1291
1292 installed := hwasan.Description("install libclang_rt.hwasan")
1293 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1294
1295 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1296 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1297 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1298}
1299
Jooyung Han61b66e92020-03-21 14:21:46 +00001300func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1301 testcases := []struct {
1302 name string
1303 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001304 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001305 shouldLink string
1306 shouldNotLink []string
1307 }{
1308 {
Jiyong Park55549df2021-02-26 23:57:23 +09001309 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001310 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001311 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001312 shouldLink: "current",
1313 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001314 },
1315 {
Jiyong Park55549df2021-02-26 23:57:23 +09001316 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001317 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001318 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001319 shouldLink: "current",
1320 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001321 },
1322 }
1323 for _, tc := range testcases {
1324 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001325 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001326 apex {
1327 name: "myapex",
1328 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001329 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001330 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001331 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001332 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001333
Jooyung Han61b66e92020-03-21 14:21:46 +00001334 apex_key {
1335 name: "myapex.key",
1336 public_key: "testkey.avbpubkey",
1337 private_key: "testkey.pem",
1338 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001339
Jooyung Han61b66e92020-03-21 14:21:46 +00001340 cc_library {
1341 name: "mylib",
1342 srcs: ["mylib.cpp"],
1343 vendor_available: true,
1344 shared_libs: ["libbar"],
1345 system_shared_libs: [],
1346 stl: "none",
1347 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001348 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001349 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001350
Jooyung Han61b66e92020-03-21 14:21:46 +00001351 cc_library {
1352 name: "libbar",
1353 srcs: ["mylib.cpp"],
1354 system_shared_libs: [],
1355 stl: "none",
1356 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001357 llndk: {
1358 symbol_file: "libbar.map.txt",
1359 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001360 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001361 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001362 withUnbundledBuild,
1363 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001364
Jooyung Han61b66e92020-03-21 14:21:46 +00001365 // Ensure that LLNDK dep is not included
1366 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1367 "lib64/mylib.so",
1368 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001369
Jooyung Han61b66e92020-03-21 14:21:46 +00001370 // Ensure that LLNDK dep is required
1371 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1372 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1373 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001374
Steven Moreland2c4000c2021-04-27 02:08:49 +00001375 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1376 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001377 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001378 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001379 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001380
Steven Moreland2c4000c2021-04-27 02:08:49 +00001381 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001382 ver := tc.shouldLink
1383 if tc.shouldLink == "current" {
1384 ver = strconv.Itoa(android.FutureApiLevelInt)
1385 }
1386 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001387 })
1388 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001389}
1390
Jiyong Park25fc6a92018-11-18 18:02:45 +09001391func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001392 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001393 apex {
1394 name: "myapex",
1395 key: "myapex.key",
1396 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001397 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001398 }
1399
1400 apex_key {
1401 name: "myapex.key",
1402 public_key: "testkey.avbpubkey",
1403 private_key: "testkey.pem",
1404 }
1405
1406 cc_library {
1407 name: "mylib",
1408 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001409 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001410 shared_libs: ["libdl#27"],
1411 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001412 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001413 }
1414
1415 cc_library_shared {
1416 name: "mylib_shared",
1417 srcs: ["mylib.cpp"],
1418 shared_libs: ["libdl#27"],
1419 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001420 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001421 }
1422
1423 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001424 name: "libBootstrap",
1425 srcs: ["mylib.cpp"],
1426 stl: "none",
1427 bootstrap: true,
1428 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001429 `)
1430
Sundong Ahnabb64432019-10-22 13:58:29 +09001431 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001432 copyCmds := apexRule.Args["copy_commands"]
1433
1434 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001435 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001436 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1437 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001438
1439 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001440 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001441
Colin Crossaede88c2020-08-11 12:17:01 -07001442 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1443 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1444 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001445
1446 // For dependency to libc
1447 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001448 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001449 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001450 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001451 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001452 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1453 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001454
1455 // For dependency to libm
1456 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001457 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001458 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001459 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001460 // ... and is not compiling with the stub
1461 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1462 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1463
1464 // For dependency to libdl
1465 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001466 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001467 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001468 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1469 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001470 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001471 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001472 // ... Cflags from stub is correctly exported to mylib
1473 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1474 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001475
1476 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001477 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1478 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1479 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1480 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001481}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001482
Jooyung Han749dc692020-04-15 11:03:39 +09001483func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001484 // there are three links between liba --> libz.
1485 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001486 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001487 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001488 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001489 apex {
1490 name: "myapex",
1491 key: "myapex.key",
1492 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001493 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001494 }
1495
1496 apex {
1497 name: "otherapex",
1498 key: "myapex.key",
1499 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001500 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001501 }
1502
1503 apex_key {
1504 name: "myapex.key",
1505 public_key: "testkey.avbpubkey",
1506 private_key: "testkey.pem",
1507 }
1508
1509 cc_library {
1510 name: "libx",
1511 shared_libs: ["liba"],
1512 system_shared_libs: [],
1513 stl: "none",
1514 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001515 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001516 }
1517
1518 cc_library {
1519 name: "liby",
1520 shared_libs: ["liba"],
1521 system_shared_libs: [],
1522 stl: "none",
1523 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001524 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001525 }
1526
1527 cc_library {
1528 name: "liba",
1529 shared_libs: ["libz"],
1530 system_shared_libs: [],
1531 stl: "none",
1532 apex_available: [
1533 "//apex_available:anyapex",
1534 "//apex_available:platform",
1535 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001536 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001537 }
1538
1539 cc_library {
1540 name: "libz",
1541 system_shared_libs: [],
1542 stl: "none",
1543 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001544 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001545 },
1546 }
Jooyung Han749dc692020-04-15 11:03:39 +09001547 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001548
1549 expectLink := func(from, from_variant, to, to_variant string) {
1550 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1551 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1552 }
1553 expectNoLink := func(from, from_variant, to, to_variant string) {
1554 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1555 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1556 }
1557 // platform liba is linked to non-stub version
1558 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001559 // liba in myapex is linked to current
1560 expectLink("liba", "shared_apex29", "libz", "shared_current")
1561 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001562 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001563 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001564 // liba in otherapex is linked to current
1565 expectLink("liba", "shared_apex30", "libz", "shared_current")
1566 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001567 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1568 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001569}
1570
Jooyung Hanaed150d2020-04-02 01:41:41 +09001571func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001572 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001573 apex {
1574 name: "myapex",
1575 key: "myapex.key",
1576 native_shared_libs: ["libx"],
1577 min_sdk_version: "R",
1578 }
1579
1580 apex_key {
1581 name: "myapex.key",
1582 public_key: "testkey.avbpubkey",
1583 private_key: "testkey.pem",
1584 }
1585
1586 cc_library {
1587 name: "libx",
1588 shared_libs: ["libz"],
1589 system_shared_libs: [],
1590 stl: "none",
1591 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001592 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001593 }
1594
1595 cc_library {
1596 name: "libz",
1597 system_shared_libs: [],
1598 stl: "none",
1599 stubs: {
1600 versions: ["29", "R"],
1601 },
1602 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001603 `,
1604 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1605 variables.Platform_version_active_codenames = []string{"R"}
1606 }),
1607 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001608
1609 expectLink := func(from, from_variant, to, to_variant string) {
1610 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1611 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1612 }
1613 expectNoLink := func(from, from_variant, to, to_variant string) {
1614 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1615 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1616 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001617 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1618 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001619 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1620 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001621}
1622
Jooyung Han749dc692020-04-15 11:03:39 +09001623func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001624 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001625 apex {
1626 name: "myapex",
1627 key: "myapex.key",
1628 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001629 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001630 }
1631
1632 apex_key {
1633 name: "myapex.key",
1634 public_key: "testkey.avbpubkey",
1635 private_key: "testkey.pem",
1636 }
1637
1638 cc_library {
1639 name: "libx",
1640 shared_libs: ["libz"],
1641 system_shared_libs: [],
1642 stl: "none",
1643 apex_available: [ "myapex" ],
1644 }
1645
1646 cc_library {
1647 name: "libz",
1648 system_shared_libs: [],
1649 stl: "none",
1650 stubs: {
1651 versions: ["1", "2"],
1652 },
1653 }
1654 `)
1655
1656 expectLink := func(from, from_variant, to, to_variant string) {
1657 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1658 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1659 }
1660 expectNoLink := func(from, from_variant, to, to_variant string) {
1661 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1662 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1663 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001664 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001665 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001666 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001667 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001668}
1669
1670func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001671 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001672 apex {
1673 name: "myapex",
1674 key: "myapex.key",
1675 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001676 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001677 }
1678
1679 apex_key {
1680 name: "myapex.key",
1681 public_key: "testkey.avbpubkey",
1682 private_key: "testkey.pem",
1683 }
1684
1685 cc_library {
1686 name: "libx",
1687 system_shared_libs: [],
1688 stl: "none",
1689 apex_available: [ "myapex" ],
1690 stubs: {
1691 versions: ["1", "2"],
1692 },
1693 }
1694
1695 cc_library {
1696 name: "libz",
1697 shared_libs: ["libx"],
1698 system_shared_libs: [],
1699 stl: "none",
1700 }
1701 `)
1702
1703 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001704 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001705 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1706 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1707 }
1708 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001709 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001710 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1711 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1712 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001713 expectLink("libz", "shared", "libx", "shared_current")
1714 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09001715 expectNoLink("libz", "shared", "libz", "shared_1")
1716 expectNoLink("libz", "shared", "libz", "shared")
1717}
1718
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001719var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1720 func(variables android.FixtureProductVariables) {
1721 variables.SanitizeDevice = []string{"hwaddress"}
1722 },
1723)
1724
Jooyung Han75568392020-03-20 04:29:24 +09001725func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001726 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001727 apex {
1728 name: "myapex",
1729 key: "myapex.key",
1730 native_shared_libs: ["libx"],
1731 min_sdk_version: "29",
1732 }
1733
1734 apex_key {
1735 name: "myapex.key",
1736 public_key: "testkey.avbpubkey",
1737 private_key: "testkey.pem",
1738 }
1739
1740 cc_library {
1741 name: "libx",
1742 shared_libs: ["libbar"],
1743 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001744 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001745 }
1746
1747 cc_library {
1748 name: "libbar",
1749 stubs: {
1750 versions: ["29", "30"],
1751 },
1752 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001753 `,
1754 prepareForTestWithSantitizeHwaddress,
1755 )
Jooyung Han03b51852020-02-26 22:45:42 +09001756 expectLink := func(from, from_variant, to, to_variant string) {
1757 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1758 libFlags := ld.Args["libFlags"]
1759 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1760 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001761 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09001762}
1763
Jooyung Han75568392020-03-20 04:29:24 +09001764func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001765 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001766 apex {
1767 name: "myapex",
1768 key: "myapex.key",
1769 native_shared_libs: ["libx"],
1770 min_sdk_version: "29",
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 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001782 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001783 }
Jooyung Han75568392020-03-20 04:29:24 +09001784 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001785
1786 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001787 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001788 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001789 // note that platform variant is not.
1790 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001791 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001792}
1793
Jooyung Han749dc692020-04-15 11:03:39 +09001794func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1795 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001796 apex {
1797 name: "myapex",
1798 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001799 native_shared_libs: ["mylib"],
1800 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001801 }
1802
1803 apex_key {
1804 name: "myapex.key",
1805 public_key: "testkey.avbpubkey",
1806 private_key: "testkey.pem",
1807 }
Jooyung Han749dc692020-04-15 11:03:39 +09001808
1809 cc_library {
1810 name: "mylib",
1811 srcs: ["mylib.cpp"],
1812 system_shared_libs: [],
1813 stl: "none",
1814 apex_available: [
1815 "myapex",
1816 ],
1817 min_sdk_version: "30",
1818 }
1819 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001820
1821 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
1822 apex {
1823 name: "myapex",
1824 key: "myapex.key",
1825 native_shared_libs: ["libfoo.ffi"],
1826 min_sdk_version: "29",
1827 }
1828
1829 apex_key {
1830 name: "myapex.key",
1831 public_key: "testkey.avbpubkey",
1832 private_key: "testkey.pem",
1833 }
1834
1835 rust_ffi_shared {
1836 name: "libfoo.ffi",
1837 srcs: ["foo.rs"],
1838 crate_name: "foo",
1839 apex_available: [
1840 "myapex",
1841 ],
1842 min_sdk_version: "30",
1843 }
1844 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001845
1846 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
1847 apex {
1848 name: "myapex",
1849 key: "myapex.key",
1850 java_libs: ["libfoo"],
1851 min_sdk_version: "29",
1852 }
1853
1854 apex_key {
1855 name: "myapex.key",
1856 public_key: "testkey.avbpubkey",
1857 private_key: "testkey.pem",
1858 }
1859
1860 java_import {
1861 name: "libfoo",
1862 jars: ["libfoo.jar"],
1863 apex_available: [
1864 "myapex",
1865 ],
1866 min_sdk_version: "30",
1867 }
1868 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001869}
1870
1871func TestApexMinSdkVersion_Okay(t *testing.T) {
1872 testApex(t, `
1873 apex {
1874 name: "myapex",
1875 key: "myapex.key",
1876 native_shared_libs: ["libfoo"],
1877 java_libs: ["libbar"],
1878 min_sdk_version: "29",
1879 }
1880
1881 apex_key {
1882 name: "myapex.key",
1883 public_key: "testkey.avbpubkey",
1884 private_key: "testkey.pem",
1885 }
1886
1887 cc_library {
1888 name: "libfoo",
1889 srcs: ["mylib.cpp"],
1890 shared_libs: ["libfoo_dep"],
1891 apex_available: ["myapex"],
1892 min_sdk_version: "29",
1893 }
1894
1895 cc_library {
1896 name: "libfoo_dep",
1897 srcs: ["mylib.cpp"],
1898 apex_available: ["myapex"],
1899 min_sdk_version: "29",
1900 }
1901
1902 java_library {
1903 name: "libbar",
1904 sdk_version: "current",
1905 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001906 static_libs: [
1907 "libbar_dep",
1908 "libbar_import_dep",
1909 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001910 apex_available: ["myapex"],
1911 min_sdk_version: "29",
1912 }
1913
1914 java_library {
1915 name: "libbar_dep",
1916 sdk_version: "current",
1917 srcs: ["a.java"],
1918 apex_available: ["myapex"],
1919 min_sdk_version: "29",
1920 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001921
1922 java_import {
1923 name: "libbar_import_dep",
1924 jars: ["libbar.jar"],
1925 apex_available: ["myapex"],
1926 min_sdk_version: "29",
1927 }
Jooyung Han03b51852020-02-26 22:45:42 +09001928 `)
1929}
1930
Artur Satayev8cf899a2020-04-15 17:29:42 +01001931func TestJavaStableSdkVersion(t *testing.T) {
1932 testCases := []struct {
1933 name string
1934 expectedError string
1935 bp string
1936 }{
1937 {
1938 name: "Non-updatable apex with non-stable dep",
1939 bp: `
1940 apex {
1941 name: "myapex",
1942 java_libs: ["myjar"],
1943 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001944 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01001945 }
1946 apex_key {
1947 name: "myapex.key",
1948 public_key: "testkey.avbpubkey",
1949 private_key: "testkey.pem",
1950 }
1951 java_library {
1952 name: "myjar",
1953 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001954 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001955 apex_available: ["myapex"],
1956 }
1957 `,
1958 },
1959 {
1960 name: "Updatable apex with stable dep",
1961 bp: `
1962 apex {
1963 name: "myapex",
1964 java_libs: ["myjar"],
1965 key: "myapex.key",
1966 updatable: true,
1967 min_sdk_version: "29",
1968 }
1969 apex_key {
1970 name: "myapex.key",
1971 public_key: "testkey.avbpubkey",
1972 private_key: "testkey.pem",
1973 }
1974 java_library {
1975 name: "myjar",
1976 srcs: ["foo/bar/MyClass.java"],
1977 sdk_version: "current",
1978 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001979 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001980 }
1981 `,
1982 },
1983 {
1984 name: "Updatable apex with non-stable dep",
1985 expectedError: "cannot depend on \"myjar\"",
1986 bp: `
1987 apex {
1988 name: "myapex",
1989 java_libs: ["myjar"],
1990 key: "myapex.key",
1991 updatable: true,
1992 }
1993 apex_key {
1994 name: "myapex.key",
1995 public_key: "testkey.avbpubkey",
1996 private_key: "testkey.pem",
1997 }
1998 java_library {
1999 name: "myjar",
2000 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002001 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002002 apex_available: ["myapex"],
2003 }
2004 `,
2005 },
2006 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002007 name: "Updatable apex with non-stable transitive dep",
2008 // This is not actually detecting that the transitive dependency is unstable, rather it is
2009 // detecting that the transitive dependency is building against a wider API surface than the
2010 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002011 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002012 bp: `
2013 apex {
2014 name: "myapex",
2015 java_libs: ["myjar"],
2016 key: "myapex.key",
2017 updatable: true,
2018 }
2019 apex_key {
2020 name: "myapex.key",
2021 public_key: "testkey.avbpubkey",
2022 private_key: "testkey.pem",
2023 }
2024 java_library {
2025 name: "myjar",
2026 srcs: ["foo/bar/MyClass.java"],
2027 sdk_version: "current",
2028 apex_available: ["myapex"],
2029 static_libs: ["transitive-jar"],
2030 }
2031 java_library {
2032 name: "transitive-jar",
2033 srcs: ["foo/bar/MyClass.java"],
2034 sdk_version: "core_platform",
2035 apex_available: ["myapex"],
2036 }
2037 `,
2038 },
2039 }
2040
2041 for _, test := range testCases {
2042 t.Run(test.name, func(t *testing.T) {
2043 if test.expectedError == "" {
2044 testApex(t, test.bp)
2045 } else {
2046 testApexError(t, test.expectedError, test.bp)
2047 }
2048 })
2049 }
2050}
2051
Jooyung Han749dc692020-04-15 11:03:39 +09002052func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2053 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2054 apex {
2055 name: "myapex",
2056 key: "myapex.key",
2057 native_shared_libs: ["mylib"],
2058 min_sdk_version: "29",
2059 }
2060
2061 apex_key {
2062 name: "myapex.key",
2063 public_key: "testkey.avbpubkey",
2064 private_key: "testkey.pem",
2065 }
2066
2067 cc_library {
2068 name: "mylib",
2069 srcs: ["mylib.cpp"],
2070 shared_libs: ["mylib2"],
2071 system_shared_libs: [],
2072 stl: "none",
2073 apex_available: [
2074 "myapex",
2075 ],
2076 min_sdk_version: "29",
2077 }
2078
2079 // indirect part of the apex
2080 cc_library {
2081 name: "mylib2",
2082 srcs: ["mylib.cpp"],
2083 system_shared_libs: [],
2084 stl: "none",
2085 apex_available: [
2086 "myapex",
2087 ],
2088 min_sdk_version: "30",
2089 }
2090 `)
2091}
2092
2093func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2094 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2095 apex {
2096 name: "myapex",
2097 key: "myapex.key",
2098 apps: ["AppFoo"],
2099 min_sdk_version: "29",
2100 }
2101
2102 apex_key {
2103 name: "myapex.key",
2104 public_key: "testkey.avbpubkey",
2105 private_key: "testkey.pem",
2106 }
2107
2108 android_app {
2109 name: "AppFoo",
2110 srcs: ["foo/bar/MyClass.java"],
2111 sdk_version: "current",
2112 min_sdk_version: "29",
2113 system_modules: "none",
2114 stl: "none",
2115 static_libs: ["bar"],
2116 apex_available: [ "myapex" ],
2117 }
2118
2119 java_library {
2120 name: "bar",
2121 sdk_version: "current",
2122 srcs: ["a.java"],
2123 apex_available: [ "myapex" ],
2124 }
2125 `)
2126}
2127
2128func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002129 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002130 apex {
2131 name: "myapex",
2132 key: "myapex.key",
2133 native_shared_libs: ["mylib"],
2134 min_sdk_version: "29",
2135 }
2136
2137 apex_key {
2138 name: "myapex.key",
2139 public_key: "testkey.avbpubkey",
2140 private_key: "testkey.pem",
2141 }
2142
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002143 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002144 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2145 cc_library {
2146 name: "mylib",
2147 srcs: ["mylib.cpp"],
2148 shared_libs: ["mylib2"],
2149 system_shared_libs: [],
2150 stl: "none",
2151 apex_available: ["myapex", "otherapex"],
2152 min_sdk_version: "29",
2153 }
2154
2155 cc_library {
2156 name: "mylib2",
2157 srcs: ["mylib.cpp"],
2158 system_shared_libs: [],
2159 stl: "none",
2160 apex_available: ["otherapex"],
2161 stubs: { versions: ["29", "30"] },
2162 min_sdk_version: "30",
2163 }
2164
2165 apex {
2166 name: "otherapex",
2167 key: "myapex.key",
2168 native_shared_libs: ["mylib", "mylib2"],
2169 min_sdk_version: "30",
2170 }
2171 `)
2172 expectLink := func(from, from_variant, to, to_variant string) {
2173 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2174 libFlags := ld.Args["libFlags"]
2175 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2176 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002177 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002178 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002179}
2180
Jooyung Haned124c32021-01-26 11:43:46 +09002181func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002182 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2183 func(variables android.FixtureProductVariables) {
2184 variables.Platform_sdk_codename = proptools.StringPtr("S")
2185 variables.Platform_version_active_codenames = []string{"S"}
2186 },
2187 )
Jooyung Haned124c32021-01-26 11:43:46 +09002188 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2189 apex {
2190 name: "myapex",
2191 key: "myapex.key",
2192 native_shared_libs: ["libfoo"],
2193 min_sdk_version: "S",
2194 }
2195 apex_key {
2196 name: "myapex.key",
2197 public_key: "testkey.avbpubkey",
2198 private_key: "testkey.pem",
2199 }
2200 cc_library {
2201 name: "libfoo",
2202 shared_libs: ["libbar"],
2203 apex_available: ["myapex"],
2204 min_sdk_version: "29",
2205 }
2206 cc_library {
2207 name: "libbar",
2208 apex_available: ["myapex"],
2209 }
2210 `, withSAsActiveCodeNames)
2211}
2212
2213func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002214 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2215 variables.Platform_sdk_codename = proptools.StringPtr("S")
2216 variables.Platform_version_active_codenames = []string{"S", "T"}
2217 })
Colin Cross1c460562021-02-16 17:55:47 -08002218 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002219 apex {
2220 name: "myapex",
2221 key: "myapex.key",
2222 native_shared_libs: ["libfoo"],
2223 min_sdk_version: "S",
2224 }
2225 apex_key {
2226 name: "myapex.key",
2227 public_key: "testkey.avbpubkey",
2228 private_key: "testkey.pem",
2229 }
2230 cc_library {
2231 name: "libfoo",
2232 shared_libs: ["libbar"],
2233 apex_available: ["myapex"],
2234 min_sdk_version: "S",
2235 }
2236 cc_library {
2237 name: "libbar",
2238 stubs: {
2239 symbol_file: "libbar.map.txt",
2240 versions: ["30", "S", "T"],
2241 },
2242 }
2243 `, withSAsActiveCodeNames)
2244
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002245 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002246 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2247 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002248 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002249}
2250
Jiyong Park7c2ee712018-12-07 00:42:25 +09002251func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002252 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002253 apex {
2254 name: "myapex",
2255 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002256 native_shared_libs: ["mylib"],
2257 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002258 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002259 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002260 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002261 }
2262
2263 apex_key {
2264 name: "myapex.key",
2265 public_key: "testkey.avbpubkey",
2266 private_key: "testkey.pem",
2267 }
2268
2269 prebuilt_etc {
2270 name: "myetc",
2271 src: "myprebuilt",
2272 sub_dir: "foo/bar",
2273 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002274
2275 cc_library {
2276 name: "mylib",
2277 srcs: ["mylib.cpp"],
2278 relative_install_path: "foo/bar",
2279 system_shared_libs: [],
2280 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002281 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002282 }
2283
2284 cc_binary {
2285 name: "mybin",
2286 srcs: ["mylib.cpp"],
2287 relative_install_path: "foo/bar",
2288 system_shared_libs: [],
2289 static_executable: true,
2290 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002291 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002292 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002293 `)
2294
Sundong Ahnabb64432019-10-22 13:58:29 +09002295 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Parkb90099d2021-12-15 11:08:07 +00002296 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002297
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002298 // Ensure that the subdirectories are all listed
Jiyong Parkb90099d2021-12-15 11:08:07 +00002299 ensureContains(t, cmd, "/etc ")
2300 ensureContains(t, cmd, "/etc/foo ")
2301 ensureContains(t, cmd, "/etc/foo/bar ")
2302 ensureContains(t, cmd, "/lib64 ")
2303 ensureContains(t, cmd, "/lib64/foo ")
2304 ensureContains(t, cmd, "/lib64/foo/bar ")
2305 ensureContains(t, cmd, "/lib ")
2306 ensureContains(t, cmd, "/lib/foo ")
2307 ensureContains(t, cmd, "/lib/foo/bar ")
2308 ensureContains(t, cmd, "/bin ")
2309 ensureContains(t, cmd, "/bin/foo ")
2310 ensureContains(t, cmd, "/bin/foo/bar ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002311}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002312
Jooyung Han35155c42020-02-06 17:33:20 +09002313func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002314 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002315 apex {
2316 name: "myapex",
2317 key: "myapex.key",
2318 multilib: {
2319 both: {
2320 native_shared_libs: ["mylib"],
2321 binaries: ["mybin"],
2322 },
2323 },
2324 compile_multilib: "both",
2325 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002326 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002327 }
2328
2329 apex_key {
2330 name: "myapex.key",
2331 public_key: "testkey.avbpubkey",
2332 private_key: "testkey.pem",
2333 }
2334
2335 cc_library {
2336 name: "mylib",
2337 relative_install_path: "foo/bar",
2338 system_shared_libs: [],
2339 stl: "none",
2340 apex_available: [ "myapex" ],
2341 native_bridge_supported: true,
2342 }
2343
2344 cc_binary {
2345 name: "mybin",
2346 relative_install_path: "foo/bar",
2347 system_shared_libs: [],
2348 static_executable: true,
2349 stl: "none",
2350 apex_available: [ "myapex" ],
2351 native_bridge_supported: true,
2352 compile_multilib: "both", // default is "first" for binary
2353 multilib: {
2354 lib64: {
2355 suffix: "64",
2356 },
2357 },
2358 }
2359 `, withNativeBridgeEnabled)
2360 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2361 "bin/foo/bar/mybin",
2362 "bin/foo/bar/mybin64",
2363 "bin/arm/foo/bar/mybin",
2364 "bin/arm64/foo/bar/mybin64",
2365 "lib/foo/bar/mylib.so",
2366 "lib/arm/foo/bar/mylib.so",
2367 "lib64/foo/bar/mylib.so",
2368 "lib64/arm64/foo/bar/mylib.so",
2369 })
2370}
2371
Jooyung Han85d61762020-06-24 23:50:26 +09002372func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002373 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002374 apex {
2375 name: "myapex",
2376 key: "myapex.key",
2377 binaries: ["mybin"],
2378 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002379 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002380 }
2381 apex_key {
2382 name: "myapex.key",
2383 public_key: "testkey.avbpubkey",
2384 private_key: "testkey.pem",
2385 }
2386 cc_binary {
2387 name: "mybin",
2388 vendor: true,
2389 shared_libs: ["libfoo"],
2390 }
2391 cc_library {
2392 name: "libfoo",
2393 proprietary: true,
2394 }
2395 `)
2396
2397 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2398 "bin/mybin",
2399 "lib64/libfoo.so",
2400 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2401 "lib64/libc++.so",
2402 })
2403
2404 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002405 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002406 name := apexBundle.BaseModuleName()
2407 prefix := "TARGET_"
2408 var builder strings.Builder
2409 data.Custom(&builder, name, prefix, "", data)
Paul Duffin37ba3442021-03-29 00:21:08 +01002410 androidMk := android.StringRelativeToTop(ctx.Config(), builder.String())
2411 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002412 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002413
2414 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2415 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2416 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002417}
2418
Jooyung Handf78e212020-07-22 15:54:47 +09002419func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002420 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002421 apex {
2422 name: "myapex",
2423 key: "myapex.key",
2424 binaries: ["mybin"],
2425 vendor: true,
2426 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002427 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002428 }
2429 apex_key {
2430 name: "myapex.key",
2431 public_key: "testkey.avbpubkey",
2432 private_key: "testkey.pem",
2433 }
2434 cc_binary {
2435 name: "mybin",
2436 vendor: true,
2437 shared_libs: ["libvndk", "libvendor"],
2438 }
2439 cc_library {
2440 name: "libvndk",
2441 vndk: {
2442 enabled: true,
2443 },
2444 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002445 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002446 }
2447 cc_library {
2448 name: "libvendor",
2449 vendor: true,
2450 }
2451 `)
2452
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002453 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002454
Paul Duffina71a67a2021-03-29 00:42:57 +01002455 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002456 libs := names(ldRule.Args["libFlags"])
2457 // VNDK libs(libvndk/libc++) as they are
Paul Duffin37ba3442021-03-29 00:21:08 +01002458 ensureListContains(t, libs, "out/soong/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
2459 ensureListContains(t, libs, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002460 // non-stable Vendor libs as APEX variants
Paul Duffin37ba3442021-03-29 00:21:08 +01002461 ensureListContains(t, libs, "out/soong/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002462
2463 // VNDK libs are not included when use_vndk_as_stable: true
2464 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2465 "bin/mybin",
2466 "lib64/libvendor.so",
2467 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002468
2469 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2470 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2471 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002472}
2473
Justin Yun13decfb2021-03-08 19:25:55 +09002474func TestProductVariant(t *testing.T) {
2475 ctx := testApex(t, `
2476 apex {
2477 name: "myapex",
2478 key: "myapex.key",
2479 updatable: false,
2480 product_specific: true,
2481 binaries: ["foo"],
2482 }
2483
2484 apex_key {
2485 name: "myapex.key",
2486 public_key: "testkey.avbpubkey",
2487 private_key: "testkey.pem",
2488 }
2489
2490 cc_binary {
2491 name: "foo",
2492 product_available: true,
2493 apex_available: ["myapex"],
2494 srcs: ["foo.cpp"],
2495 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002496 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2497 variables.ProductVndkVersion = proptools.StringPtr("current")
2498 }),
2499 )
Justin Yun13decfb2021-03-08 19:25:55 +09002500
2501 cflags := strings.Fields(
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002502 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002503 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2504 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2505 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2506 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2507}
2508
Jooyung Han8e5685d2020-09-21 11:02:57 +09002509func TestApex_withPrebuiltFirmware(t *testing.T) {
2510 testCases := []struct {
2511 name string
2512 additionalProp string
2513 }{
2514 {"system apex with prebuilt_firmware", ""},
2515 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2516 }
2517 for _, tc := range testCases {
2518 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002519 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002520 apex {
2521 name: "myapex",
2522 key: "myapex.key",
2523 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002524 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002525 `+tc.additionalProp+`
2526 }
2527 apex_key {
2528 name: "myapex.key",
2529 public_key: "testkey.avbpubkey",
2530 private_key: "testkey.pem",
2531 }
2532 prebuilt_firmware {
2533 name: "myfirmware",
2534 src: "myfirmware.bin",
2535 filename_from_src: true,
2536 `+tc.additionalProp+`
2537 }
2538 `)
2539 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2540 "etc/firmware/myfirmware.bin",
2541 })
2542 })
2543 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002544}
2545
Jooyung Hanefb184e2020-06-25 17:14:25 +09002546func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002547 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002548 apex {
2549 name: "myapex",
2550 key: "myapex.key",
2551 vendor: true,
2552 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002553 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002554 }
2555
2556 apex_key {
2557 name: "myapex.key",
2558 public_key: "testkey.avbpubkey",
2559 private_key: "testkey.pem",
2560 }
2561
2562 cc_library {
2563 name: "mylib",
2564 vendor_available: true,
2565 }
2566 `)
2567
2568 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002569 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002570 name := apexBundle.BaseModuleName()
2571 prefix := "TARGET_"
2572 var builder strings.Builder
2573 data.Custom(&builder, name, prefix, "", data)
2574 androidMk := builder.String()
2575 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2576}
2577
Jooyung Han2ed99d02020-06-24 23:26:26 +09002578func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002579 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002580 apex {
2581 name: "myapex",
2582 key: "myapex.key",
2583 vintf_fragments: ["fragment.xml"],
2584 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002585 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002586 }
2587 apex_key {
2588 name: "myapex.key",
2589 public_key: "testkey.avbpubkey",
2590 private_key: "testkey.pem",
2591 }
2592 cc_binary {
2593 name: "mybin",
2594 }
2595 `)
2596
2597 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002598 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002599 name := apexBundle.BaseModuleName()
2600 prefix := "TARGET_"
2601 var builder strings.Builder
2602 data.Custom(&builder, name, prefix, "", data)
2603 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04002604 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04002605 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09002606}
2607
Jiyong Park16e91a02018-12-20 18:18:08 +09002608func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002609 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002610 apex {
2611 name: "myapex",
2612 key: "myapex.key",
2613 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002614 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002615 }
2616
2617 apex_key {
2618 name: "myapex.key",
2619 public_key: "testkey.avbpubkey",
2620 private_key: "testkey.pem",
2621 }
2622
2623 cc_library {
2624 name: "mylib",
2625 srcs: ["mylib.cpp"],
2626 system_shared_libs: [],
2627 stl: "none",
2628 stubs: {
2629 versions: ["1", "2", "3"],
2630 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002631 apex_available: [
2632 "//apex_available:platform",
2633 "myapex",
2634 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002635 }
2636
2637 cc_binary {
2638 name: "not_in_apex",
2639 srcs: ["mylib.cpp"],
2640 static_libs: ["mylib"],
2641 static_executable: true,
2642 system_shared_libs: [],
2643 stl: "none",
2644 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002645 `)
2646
Colin Cross7113d202019-11-20 16:39:12 -08002647 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002648
2649 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002650 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002651}
Jiyong Park9335a262018-12-24 11:31:58 +09002652
2653func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002654 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002655 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002656 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002657 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002658 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002659 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002660 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002661 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09002662 }
2663
2664 cc_library {
2665 name: "mylib",
2666 srcs: ["mylib.cpp"],
2667 system_shared_libs: [],
2668 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002669 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002670 }
2671
2672 apex_key {
2673 name: "myapex.key",
2674 public_key: "testkey.avbpubkey",
2675 private_key: "testkey.pem",
2676 }
2677
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002678 android_app_certificate {
2679 name: "myapex.certificate",
2680 certificate: "testkey",
2681 }
2682
2683 android_app_certificate {
2684 name: "myapex.certificate.override",
2685 certificate: "testkey.override",
2686 }
2687
Jiyong Park9335a262018-12-24 11:31:58 +09002688 `)
2689
2690 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002691 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002692
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002693 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2694 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002695 "vendor/foo/devkeys/testkey.avbpubkey")
2696 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002697 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2698 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002699 "vendor/foo/devkeys/testkey.pem")
2700 }
2701
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002702 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002703 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002704 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002705 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002706 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002707 }
2708}
Jiyong Park58e364a2019-01-19 19:24:06 +09002709
Jooyung Hanf121a652019-12-17 14:30:11 +09002710func TestCertificate(t *testing.T) {
2711 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002712 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002713 apex {
2714 name: "myapex",
2715 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002716 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002717 }
2718 apex_key {
2719 name: "myapex.key",
2720 public_key: "testkey.avbpubkey",
2721 private_key: "testkey.pem",
2722 }`)
2723 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2724 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2725 if actual := rule.Args["certificates"]; actual != expected {
2726 t.Errorf("certificates should be %q, not %q", expected, actual)
2727 }
2728 })
2729 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002730 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002731 apex {
2732 name: "myapex_keytest",
2733 key: "myapex.key",
2734 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002735 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002736 }
2737 apex_key {
2738 name: "myapex.key",
2739 public_key: "testkey.avbpubkey",
2740 private_key: "testkey.pem",
2741 }
2742 android_app_certificate {
2743 name: "myapex.certificate.override",
2744 certificate: "testkey.override",
2745 }`)
2746 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2747 expected := "testkey.override.x509.pem testkey.override.pk8"
2748 if actual := rule.Args["certificates"]; actual != expected {
2749 t.Errorf("certificates should be %q, not %q", expected, actual)
2750 }
2751 })
2752 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002753 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002754 apex {
2755 name: "myapex",
2756 key: "myapex.key",
2757 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002758 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002759 }
2760 apex_key {
2761 name: "myapex.key",
2762 public_key: "testkey.avbpubkey",
2763 private_key: "testkey.pem",
2764 }
2765 android_app_certificate {
2766 name: "myapex.certificate",
2767 certificate: "testkey",
2768 }`)
2769 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2770 expected := "testkey.x509.pem testkey.pk8"
2771 if actual := rule.Args["certificates"]; actual != expected {
2772 t.Errorf("certificates should be %q, not %q", expected, actual)
2773 }
2774 })
2775 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002776 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002777 apex {
2778 name: "myapex_keytest",
2779 key: "myapex.key",
2780 file_contexts: ":myapex-file_contexts",
2781 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002782 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002783 }
2784 apex_key {
2785 name: "myapex.key",
2786 public_key: "testkey.avbpubkey",
2787 private_key: "testkey.pem",
2788 }
2789 android_app_certificate {
2790 name: "myapex.certificate.override",
2791 certificate: "testkey.override",
2792 }`)
2793 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2794 expected := "testkey.override.x509.pem testkey.override.pk8"
2795 if actual := rule.Args["certificates"]; actual != expected {
2796 t.Errorf("certificates should be %q, not %q", expected, actual)
2797 }
2798 })
2799 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002800 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002801 apex {
2802 name: "myapex",
2803 key: "myapex.key",
2804 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002805 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002806 }
2807 apex_key {
2808 name: "myapex.key",
2809 public_key: "testkey.avbpubkey",
2810 private_key: "testkey.pem",
2811 }`)
2812 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2813 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2814 if actual := rule.Args["certificates"]; actual != expected {
2815 t.Errorf("certificates should be %q, not %q", expected, actual)
2816 }
2817 })
2818 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002819 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002820 apex {
2821 name: "myapex_keytest",
2822 key: "myapex.key",
2823 file_contexts: ":myapex-file_contexts",
2824 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002825 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002826 }
2827 apex_key {
2828 name: "myapex.key",
2829 public_key: "testkey.avbpubkey",
2830 private_key: "testkey.pem",
2831 }
2832 android_app_certificate {
2833 name: "myapex.certificate.override",
2834 certificate: "testkey.override",
2835 }`)
2836 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2837 expected := "testkey.override.x509.pem testkey.override.pk8"
2838 if actual := rule.Args["certificates"]; actual != expected {
2839 t.Errorf("certificates should be %q, not %q", expected, actual)
2840 }
2841 })
2842}
2843
Jiyong Park58e364a2019-01-19 19:24:06 +09002844func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002845 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002846 apex {
2847 name: "myapex",
2848 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002849 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002850 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09002851 }
2852
2853 apex {
2854 name: "otherapex",
2855 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002856 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002857 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002858 }
2859
2860 apex_key {
2861 name: "myapex.key",
2862 public_key: "testkey.avbpubkey",
2863 private_key: "testkey.pem",
2864 }
2865
2866 cc_library {
2867 name: "mylib",
2868 srcs: ["mylib.cpp"],
2869 system_shared_libs: [],
2870 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002871 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002872 "myapex",
2873 "otherapex",
2874 ],
Jooyung Han24282772020-03-21 23:20:55 +09002875 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002876 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002877 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09002878 cc_library {
2879 name: "mylib2",
2880 srcs: ["mylib.cpp"],
2881 system_shared_libs: [],
2882 stl: "none",
2883 apex_available: [
2884 "myapex",
2885 "otherapex",
2886 ],
Colin Crossaede88c2020-08-11 12:17:01 -07002887 static_libs: ["mylib3"],
2888 recovery_available: true,
2889 min_sdk_version: "29",
2890 }
2891 cc_library {
2892 name: "mylib3",
2893 srcs: ["mylib.cpp"],
2894 system_shared_libs: [],
2895 stl: "none",
2896 apex_available: [
2897 "myapex",
2898 "otherapex",
2899 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09002900 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07002901 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002902 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002903 }
Jiyong Park58e364a2019-01-19 19:24:06 +09002904 `)
2905
Jooyung Hanc87a0592020-03-02 17:44:33 +09002906 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08002907 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002908 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002909 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002910
Jooyung Hanccce2f22020-03-07 03:45:53 +09002911 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07002912 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09002913 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002914 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09002915 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002916
Jooyung Hanccce2f22020-03-07 03:45:53 +09002917 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07002918 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09002919 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002920 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09002921 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002922
Colin Crossaede88c2020-08-11 12:17:01 -07002923 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
2924 // each variant defines additional macros to distinguish which apex variant it is built for
2925
2926 // non-APEX variant does not have __ANDROID_APEX__ defined
2927 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2928 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2929
2930 // APEX variant has __ANDROID_APEX__ defined
2931 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
2932 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2933 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2934 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
2935
2936 // APEX variant has __ANDROID_APEX__ defined
2937 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
2938 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2939 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2940 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
2941
Dan Albertb19953d2020-11-17 15:29:36 -08002942 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07002943 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2944 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002945 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07002946
2947 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
2948 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09002949
2950 // non-APEX variant does not have __ANDROID_APEX__ defined
2951 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2952 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2953
2954 // APEX variant has __ANDROID_APEX__ defined
2955 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002956 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07002957 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002958 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002959
Jooyung Hanc87a0592020-03-02 17:44:33 +09002960 // APEX variant has __ANDROID_APEX__ defined
2961 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002962 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002963 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07002964 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09002965
Dan Albertb19953d2020-11-17 15:29:36 -08002966 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07002967 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09002968 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002969 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002970}
Jiyong Park7e636d02019-01-28 16:16:54 +09002971
2972func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002973 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09002974 apex {
2975 name: "myapex",
2976 key: "myapex.key",
2977 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002978 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09002979 }
2980
2981 apex_key {
2982 name: "myapex.key",
2983 public_key: "testkey.avbpubkey",
2984 private_key: "testkey.pem",
2985 }
2986
2987 cc_library_headers {
2988 name: "mylib_headers",
2989 export_include_dirs: ["my_include"],
2990 system_shared_libs: [],
2991 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09002992 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09002993 }
2994
2995 cc_library {
2996 name: "mylib",
2997 srcs: ["mylib.cpp"],
2998 system_shared_libs: [],
2999 stl: "none",
3000 header_libs: ["mylib_headers"],
3001 export_header_lib_headers: ["mylib_headers"],
3002 stubs: {
3003 versions: ["1", "2", "3"],
3004 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003005 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003006 }
3007
3008 cc_library {
3009 name: "otherlib",
3010 srcs: ["mylib.cpp"],
3011 system_shared_libs: [],
3012 stl: "none",
3013 shared_libs: ["mylib"],
3014 }
3015 `)
3016
Colin Cross7113d202019-11-20 16:39:12 -08003017 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003018
3019 // Ensure that the include path of the header lib is exported to 'otherlib'
3020 ensureContains(t, cFlags, "-Imy_include")
3021}
Alex Light9670d332019-01-29 18:07:33 -08003022
Jiyong Park7cd10e32020-01-14 09:22:18 +09003023type fileInApex struct {
3024 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003025 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003026 isLink bool
3027}
3028
Jooyung Hana57af4a2020-01-23 05:36:59 +00003029func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003030 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003031 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003032 copyCmds := apexRule.Args["copy_commands"]
3033 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003034 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003035 for _, cmd := range strings.Split(copyCmds, "&&") {
3036 cmd = strings.TrimSpace(cmd)
3037 if cmd == "" {
3038 continue
3039 }
3040 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003041 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003042 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003043 switch terms[0] {
3044 case "mkdir":
3045 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003046 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003047 t.Fatal("copyCmds contains invalid cp command", cmd)
3048 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003049 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003050 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003051 isLink = false
3052 case "ln":
3053 if len(terms) != 3 && len(terms) != 4 {
3054 // ln LINK TARGET or ln -s LINK TARGET
3055 t.Fatal("copyCmds contains invalid ln command", cmd)
3056 }
3057 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003058 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003059 isLink = true
3060 default:
3061 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3062 }
3063 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003064 index := strings.Index(dst, imageApexDir)
3065 if index == -1 {
3066 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3067 }
3068 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003069 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003070 }
3071 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003072 return ret
3073}
3074
Jooyung Hana57af4a2020-01-23 05:36:59 +00003075func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3076 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003077 var failed bool
3078 var surplus []string
3079 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003080 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003081 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003082 for _, expected := range files {
3083 if matched, _ := path.Match(expected, file.path); matched {
3084 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003085 mactchFound = true
3086 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003087 }
3088 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003089 if !mactchFound {
3090 surplus = append(surplus, file.path)
3091 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003092 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003093
Jooyung Han31c470b2019-10-18 16:26:59 +09003094 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003095 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003096 t.Log("surplus files", surplus)
3097 failed = true
3098 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003099
3100 if len(files) > len(filesMatched) {
3101 var missing []string
3102 for _, expected := range files {
3103 if !filesMatched[expected] {
3104 missing = append(missing, expected)
3105 }
3106 }
3107 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003108 t.Log("missing files", missing)
3109 failed = true
3110 }
3111 if failed {
3112 t.Fail()
3113 }
3114}
3115
Jooyung Han344d5432019-08-23 11:17:39 +09003116func TestVndkApexCurrent(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003117 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003118 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003119 name: "com.android.vndk.current",
3120 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003121 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003122 }
3123
3124 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003125 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003126 public_key: "testkey.avbpubkey",
3127 private_key: "testkey.pem",
3128 }
3129
3130 cc_library {
3131 name: "libvndk",
3132 srcs: ["mylib.cpp"],
3133 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003134 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003135 vndk: {
3136 enabled: true,
3137 },
3138 system_shared_libs: [],
3139 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003140 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003141 }
3142
3143 cc_library {
3144 name: "libvndksp",
3145 srcs: ["mylib.cpp"],
3146 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003147 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003148 vndk: {
3149 enabled: true,
3150 support_system_process: true,
3151 },
3152 system_shared_libs: [],
3153 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003154 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003155 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003156 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003157
Colin Cross2807f002021-03-02 10:15:29 -08003158 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003159 "lib/libvndk.so",
3160 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003161 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003162 "lib64/libvndk.so",
3163 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003164 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003165 "etc/llndk.libraries.29.txt",
3166 "etc/vndkcore.libraries.29.txt",
3167 "etc/vndksp.libraries.29.txt",
3168 "etc/vndkprivate.libraries.29.txt",
3169 "etc/vndkproduct.libraries.29.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003170 })
Jooyung Han344d5432019-08-23 11:17:39 +09003171}
3172
3173func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003174 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003175 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003176 name: "com.android.vndk.current",
3177 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003178 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003179 }
3180
3181 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003182 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003183 public_key: "testkey.avbpubkey",
3184 private_key: "testkey.pem",
3185 }
3186
3187 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003188 name: "libvndk",
3189 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003190 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003191 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003192 vndk: {
3193 enabled: true,
3194 },
3195 system_shared_libs: [],
3196 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003197 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003198 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003199
3200 cc_prebuilt_library_shared {
3201 name: "libvndk.arm",
3202 srcs: ["libvndk.arm.so"],
3203 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003204 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003205 vndk: {
3206 enabled: true,
3207 },
3208 enabled: false,
3209 arch: {
3210 arm: {
3211 enabled: true,
3212 },
3213 },
3214 system_shared_libs: [],
3215 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003216 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003217 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003218 `+vndkLibrariesTxtFiles("current"),
3219 withFiles(map[string][]byte{
3220 "libvndk.so": nil,
3221 "libvndk.arm.so": nil,
3222 }))
Colin Cross2807f002021-03-02 10:15:29 -08003223 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003224 "lib/libvndk.so",
3225 "lib/libvndk.arm.so",
3226 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003227 "lib/libc++.so",
3228 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003229 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003230 })
Jooyung Han344d5432019-08-23 11:17:39 +09003231}
3232
Jooyung Han39edb6c2019-11-06 16:53:07 +09003233func vndkLibrariesTxtFiles(vers ...string) (result string) {
3234 for _, v := range vers {
3235 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003236 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003237 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003238 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003239 name: "` + txt + `.libraries.txt",
3240 }
3241 `
3242 }
3243 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003244 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003245 result += `
3246 prebuilt_etc {
3247 name: "` + txt + `.libraries.` + v + `.txt",
3248 src: "dummy.txt",
3249 }
3250 `
3251 }
3252 }
3253 }
3254 return
3255}
3256
Jooyung Han344d5432019-08-23 11:17:39 +09003257func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003258 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003259 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003260 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003261 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003262 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003263 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003264 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003265 }
3266
3267 apex_key {
3268 name: "myapex.key",
3269 public_key: "testkey.avbpubkey",
3270 private_key: "testkey.pem",
3271 }
3272
Jooyung Han31c470b2019-10-18 16:26:59 +09003273 vndk_prebuilt_shared {
3274 name: "libvndk27",
3275 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003276 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003277 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003278 vndk: {
3279 enabled: true,
3280 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003281 target_arch: "arm64",
3282 arch: {
3283 arm: {
3284 srcs: ["libvndk27_arm.so"],
3285 },
3286 arm64: {
3287 srcs: ["libvndk27_arm64.so"],
3288 },
3289 },
Colin Cross2807f002021-03-02 10:15:29 -08003290 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003291 }
3292
3293 vndk_prebuilt_shared {
3294 name: "libvndk27",
3295 version: "27",
3296 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003297 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003298 vndk: {
3299 enabled: true,
3300 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003301 target_arch: "x86_64",
3302 arch: {
3303 x86: {
3304 srcs: ["libvndk27_x86.so"],
3305 },
3306 x86_64: {
3307 srcs: ["libvndk27_x86_64.so"],
3308 },
3309 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003310 }
3311 `+vndkLibrariesTxtFiles("27"),
3312 withFiles(map[string][]byte{
3313 "libvndk27_arm.so": nil,
3314 "libvndk27_arm64.so": nil,
3315 "libvndk27_x86.so": nil,
3316 "libvndk27_x86_64.so": nil,
3317 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003318
Colin Cross2807f002021-03-02 10:15:29 -08003319 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003320 "lib/libvndk27_arm.so",
3321 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003322 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003323 })
Jooyung Han344d5432019-08-23 11:17:39 +09003324}
3325
Jooyung Han90eee022019-10-01 20:02:42 +09003326func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003327 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003328 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003329 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003330 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003331 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003332 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003333 }
3334 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003335 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003336 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003337 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003338 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003339 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003340 }
3341 apex_key {
3342 name: "myapex.key",
3343 public_key: "testkey.avbpubkey",
3344 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003345 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003346
3347 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003348 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003349 actual := proptools.String(bundle.properties.Apex_name)
3350 if !reflect.DeepEqual(actual, expected) {
3351 t.Errorf("Got '%v', expected '%v'", actual, expected)
3352 }
3353 }
3354
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003355 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003356 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003357}
3358
Jooyung Han344d5432019-08-23 11:17:39 +09003359func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003360 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003361 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003362 name: "com.android.vndk.current",
3363 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003364 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003365 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003366 }
3367
3368 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003369 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003370 public_key: "testkey.avbpubkey",
3371 private_key: "testkey.pem",
3372 }
3373
3374 cc_library {
3375 name: "libvndk",
3376 srcs: ["mylib.cpp"],
3377 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003378 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003379 native_bridge_supported: true,
3380 host_supported: true,
3381 vndk: {
3382 enabled: true,
3383 },
3384 system_shared_libs: [],
3385 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003386 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003387 }
Colin Cross2807f002021-03-02 10:15:29 -08003388 `+vndkLibrariesTxtFiles("current"),
3389 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003390
Colin Cross2807f002021-03-02 10:15:29 -08003391 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003392 "lib/libvndk.so",
3393 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003394 "lib/libc++.so",
3395 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003396 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003397 })
Jooyung Han344d5432019-08-23 11:17:39 +09003398}
3399
3400func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003401 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003402 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003403 name: "com.android.vndk.current",
3404 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003405 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003406 native_bridge_supported: true,
3407 }
3408
3409 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003410 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003411 public_key: "testkey.avbpubkey",
3412 private_key: "testkey.pem",
3413 }
3414
3415 cc_library {
3416 name: "libvndk",
3417 srcs: ["mylib.cpp"],
3418 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003419 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003420 native_bridge_supported: true,
3421 host_supported: true,
3422 vndk: {
3423 enabled: true,
3424 },
3425 system_shared_libs: [],
3426 stl: "none",
3427 }
3428 `)
3429}
3430
Jooyung Han31c470b2019-10-18 16:26:59 +09003431func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003432 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003433 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003434 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003435 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003436 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003437 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003438 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003439 }
3440
3441 apex_key {
3442 name: "myapex.key",
3443 public_key: "testkey.avbpubkey",
3444 private_key: "testkey.pem",
3445 }
3446
3447 vndk_prebuilt_shared {
3448 name: "libvndk27",
3449 version: "27",
3450 target_arch: "arm",
3451 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003452 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003453 vndk: {
3454 enabled: true,
3455 },
3456 arch: {
3457 arm: {
3458 srcs: ["libvndk27.so"],
3459 }
3460 },
3461 }
3462
3463 vndk_prebuilt_shared {
3464 name: "libvndk27",
3465 version: "27",
3466 target_arch: "arm",
3467 binder32bit: true,
3468 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003469 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003470 vndk: {
3471 enabled: true,
3472 },
3473 arch: {
3474 arm: {
3475 srcs: ["libvndk27binder32.so"],
3476 }
3477 },
Colin Cross2807f002021-03-02 10:15:29 -08003478 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003479 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003480 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003481 withFiles(map[string][]byte{
3482 "libvndk27.so": nil,
3483 "libvndk27binder32.so": nil,
3484 }),
3485 withBinder32bit,
3486 withTargets(map[android.OsType][]android.Target{
3487 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003488 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3489 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003490 },
3491 }),
3492 )
3493
Colin Cross2807f002021-03-02 10:15:29 -08003494 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003495 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003496 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003497 })
3498}
3499
Jooyung Han45a96772020-06-15 14:59:42 +09003500func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003501 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003502 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003503 name: "com.android.vndk.current",
3504 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003505 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003506 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003507 }
3508
3509 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003510 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003511 public_key: "testkey.avbpubkey",
3512 private_key: "testkey.pem",
3513 }
3514
3515 cc_library {
3516 name: "libz",
3517 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003518 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003519 vndk: {
3520 enabled: true,
3521 },
3522 stubs: {
3523 symbol_file: "libz.map.txt",
3524 versions: ["30"],
3525 }
3526 }
3527 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3528 "libz.map.txt": nil,
3529 }))
3530
Colin Cross2807f002021-03-02 10:15:29 -08003531 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003532 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3533 ensureListEmpty(t, provideNativeLibs)
3534}
3535
Jooyung Hane1633032019-08-01 17:41:43 +09003536func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003537 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003538 apex {
3539 name: "myapex_nodep",
3540 key: "myapex.key",
3541 native_shared_libs: ["lib_nodep"],
3542 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003543 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003544 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003545 }
3546
3547 apex {
3548 name: "myapex_dep",
3549 key: "myapex.key",
3550 native_shared_libs: ["lib_dep"],
3551 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003552 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003553 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003554 }
3555
3556 apex {
3557 name: "myapex_provider",
3558 key: "myapex.key",
3559 native_shared_libs: ["libfoo"],
3560 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003561 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003562 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003563 }
3564
3565 apex {
3566 name: "myapex_selfcontained",
3567 key: "myapex.key",
3568 native_shared_libs: ["lib_dep", "libfoo"],
3569 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003570 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003571 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003572 }
3573
3574 apex_key {
3575 name: "myapex.key",
3576 public_key: "testkey.avbpubkey",
3577 private_key: "testkey.pem",
3578 }
3579
3580 cc_library {
3581 name: "lib_nodep",
3582 srcs: ["mylib.cpp"],
3583 system_shared_libs: [],
3584 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003585 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003586 }
3587
3588 cc_library {
3589 name: "lib_dep",
3590 srcs: ["mylib.cpp"],
3591 shared_libs: ["libfoo"],
3592 system_shared_libs: [],
3593 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003594 apex_available: [
3595 "myapex_dep",
3596 "myapex_provider",
3597 "myapex_selfcontained",
3598 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003599 }
3600
3601 cc_library {
3602 name: "libfoo",
3603 srcs: ["mytest.cpp"],
3604 stubs: {
3605 versions: ["1"],
3606 },
3607 system_shared_libs: [],
3608 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003609 apex_available: [
3610 "myapex_provider",
3611 "myapex_selfcontained",
3612 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003613 }
3614 `)
3615
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003616 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003617 var provideNativeLibs, requireNativeLibs []string
3618
Sundong Ahnabb64432019-10-22 13:58:29 +09003619 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003620 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3621 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003622 ensureListEmpty(t, provideNativeLibs)
3623 ensureListEmpty(t, requireNativeLibs)
3624
Sundong Ahnabb64432019-10-22 13:58:29 +09003625 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003626 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3627 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003628 ensureListEmpty(t, provideNativeLibs)
3629 ensureListContains(t, requireNativeLibs, "libfoo.so")
3630
Sundong Ahnabb64432019-10-22 13:58:29 +09003631 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003632 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3633 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003634 ensureListContains(t, provideNativeLibs, "libfoo.so")
3635 ensureListEmpty(t, requireNativeLibs)
3636
Sundong Ahnabb64432019-10-22 13:58:29 +09003637 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003638 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3639 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003640 ensureListContains(t, provideNativeLibs, "libfoo.so")
3641 ensureListEmpty(t, requireNativeLibs)
3642}
3643
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003644func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003645 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003646 apex {
3647 name: "myapex",
3648 key: "myapex.key",
3649 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003650 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003651 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003652 }
3653
3654 apex_key {
3655 name: "myapex.key",
3656 public_key: "testkey.avbpubkey",
3657 private_key: "testkey.pem",
3658 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003659
3660 cc_library {
3661 name: "mylib",
3662 srcs: ["mylib.cpp"],
3663 system_shared_libs: [],
3664 stl: "none",
3665 apex_available: [
3666 "//apex_available:platform",
3667 "myapex",
3668 ],
3669 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003670 `)
3671
Martin Stjernholmd8da28e2021-06-24 14:37:13 +01003672 module := ctx.ModuleForTests("myapex", "android_common_com.android.myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003673 apexManifestRule := module.Rule("apexManifestRule")
3674 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3675 apexRule := module.Rule("apexRule")
3676 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003677
Martin Stjernholmd8da28e2021-06-24 14:37:13 +01003678 apexBundle := module.Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003679 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09003680 name := apexBundle.BaseModuleName()
3681 prefix := "TARGET_"
3682 var builder strings.Builder
3683 data.Custom(&builder, name, prefix, "", data)
3684 androidMk := builder.String()
3685 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3686 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003687}
3688
Alex Light0851b882019-02-07 13:20:53 -08003689func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003690 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003691 apex {
3692 name: "myapex",
3693 key: "myapex.key",
3694 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003695 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003696 }
3697
3698 apex_key {
3699 name: "myapex.key",
3700 public_key: "testkey.avbpubkey",
3701 private_key: "testkey.pem",
3702 }
3703
3704 cc_library {
3705 name: "mylib_common",
3706 srcs: ["mylib.cpp"],
3707 system_shared_libs: [],
3708 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003709 apex_available: [
3710 "//apex_available:platform",
3711 "myapex",
3712 ],
Alex Light0851b882019-02-07 13:20:53 -08003713 }
3714 `)
3715
Sundong Ahnabb64432019-10-22 13:58:29 +09003716 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003717 apexRule := module.Rule("apexRule")
3718 copyCmds := apexRule.Args["copy_commands"]
3719
3720 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3721 t.Log("Apex was a test apex!")
3722 t.Fail()
3723 }
3724 // Ensure that main rule creates an output
3725 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3726
3727 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003728 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003729
3730 // Ensure that both direct and indirect deps are copied into apex
3731 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3732
Colin Cross7113d202019-11-20 16:39:12 -08003733 // Ensure that the platform variant ends with _shared
3734 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003735
Colin Cross56a83212020-09-15 18:30:11 -07003736 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003737 t.Log("Found mylib_common not in any apex!")
3738 t.Fail()
3739 }
3740}
3741
3742func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003743 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003744 apex_test {
3745 name: "myapex",
3746 key: "myapex.key",
3747 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003748 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003749 }
3750
3751 apex_key {
3752 name: "myapex.key",
3753 public_key: "testkey.avbpubkey",
3754 private_key: "testkey.pem",
3755 }
3756
3757 cc_library {
3758 name: "mylib_common_test",
3759 srcs: ["mylib.cpp"],
3760 system_shared_libs: [],
3761 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003762 // TODO: remove //apex_available:platform
3763 apex_available: [
3764 "//apex_available:platform",
3765 "myapex",
3766 ],
Alex Light0851b882019-02-07 13:20:53 -08003767 }
3768 `)
3769
Sundong Ahnabb64432019-10-22 13:58:29 +09003770 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003771 apexRule := module.Rule("apexRule")
3772 copyCmds := apexRule.Args["copy_commands"]
3773
3774 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3775 t.Log("Apex was not a test apex!")
3776 t.Fail()
3777 }
3778 // Ensure that main rule creates an output
3779 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3780
3781 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003782 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003783
3784 // Ensure that both direct and indirect deps are copied into apex
3785 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3786
Colin Cross7113d202019-11-20 16:39:12 -08003787 // Ensure that the platform variant ends with _shared
3788 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003789}
3790
Alex Light9670d332019-01-29 18:07:33 -08003791func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003792 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003793 apex {
3794 name: "myapex",
3795 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003796 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08003797 multilib: {
3798 first: {
3799 native_shared_libs: ["mylib_common"],
3800 }
3801 },
3802 target: {
3803 android: {
3804 multilib: {
3805 first: {
3806 native_shared_libs: ["mylib"],
3807 }
3808 }
3809 },
3810 host: {
3811 multilib: {
3812 first: {
3813 native_shared_libs: ["mylib2"],
3814 }
3815 }
3816 }
3817 }
3818 }
3819
3820 apex_key {
3821 name: "myapex.key",
3822 public_key: "testkey.avbpubkey",
3823 private_key: "testkey.pem",
3824 }
3825
3826 cc_library {
3827 name: "mylib",
3828 srcs: ["mylib.cpp"],
3829 system_shared_libs: [],
3830 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003831 // TODO: remove //apex_available:platform
3832 apex_available: [
3833 "//apex_available:platform",
3834 "myapex",
3835 ],
Alex Light9670d332019-01-29 18:07:33 -08003836 }
3837
3838 cc_library {
3839 name: "mylib_common",
3840 srcs: ["mylib.cpp"],
3841 system_shared_libs: [],
3842 stl: "none",
3843 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003844 // TODO: remove //apex_available:platform
3845 apex_available: [
3846 "//apex_available:platform",
3847 "myapex",
3848 ],
Alex Light9670d332019-01-29 18:07:33 -08003849 }
3850
3851 cc_library {
3852 name: "mylib2",
3853 srcs: ["mylib.cpp"],
3854 system_shared_libs: [],
3855 stl: "none",
3856 compile_multilib: "first",
3857 }
3858 `)
3859
Sundong Ahnabb64432019-10-22 13:58:29 +09003860 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003861 copyCmds := apexRule.Args["copy_commands"]
3862
3863 // Ensure that main rule creates an output
3864 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3865
3866 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003867 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
3868 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
3869 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08003870
3871 // Ensure that both direct and indirect deps are copied into apex
3872 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3873 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3874 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3875
Colin Cross7113d202019-11-20 16:39:12 -08003876 // Ensure that the platform variant ends with _shared
3877 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
3878 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
3879 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08003880}
Jiyong Park04480cf2019-02-06 00:16:29 +09003881
Jiyong Park59140302020-12-14 18:44:04 +09003882func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003883 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09003884 apex {
3885 name: "myapex",
3886 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003887 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09003888 arch: {
3889 arm64: {
3890 native_shared_libs: ["mylib.arm64"],
3891 },
3892 x86_64: {
3893 native_shared_libs: ["mylib.x64"],
3894 },
3895 }
3896 }
3897
3898 apex_key {
3899 name: "myapex.key",
3900 public_key: "testkey.avbpubkey",
3901 private_key: "testkey.pem",
3902 }
3903
3904 cc_library {
3905 name: "mylib.arm64",
3906 srcs: ["mylib.cpp"],
3907 system_shared_libs: [],
3908 stl: "none",
3909 // TODO: remove //apex_available:platform
3910 apex_available: [
3911 "//apex_available:platform",
3912 "myapex",
3913 ],
3914 }
3915
3916 cc_library {
3917 name: "mylib.x64",
3918 srcs: ["mylib.cpp"],
3919 system_shared_libs: [],
3920 stl: "none",
3921 // TODO: remove //apex_available:platform
3922 apex_available: [
3923 "//apex_available:platform",
3924 "myapex",
3925 ],
3926 }
3927 `)
3928
3929 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
3930 copyCmds := apexRule.Args["copy_commands"]
3931
3932 // Ensure that apex variant is created for the direct dep
3933 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
3934 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
3935
3936 // Ensure that both direct and indirect deps are copied into apex
3937 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
3938 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
3939}
3940
Jiyong Park04480cf2019-02-06 00:16:29 +09003941func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003942 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09003943 apex {
3944 name: "myapex",
3945 key: "myapex.key",
3946 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003947 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09003948 }
3949
3950 apex_key {
3951 name: "myapex.key",
3952 public_key: "testkey.avbpubkey",
3953 private_key: "testkey.pem",
3954 }
3955
3956 sh_binary {
3957 name: "myscript",
3958 src: "mylib.cpp",
3959 filename: "myscript.sh",
3960 sub_dir: "script",
3961 }
3962 `)
3963
Sundong Ahnabb64432019-10-22 13:58:29 +09003964 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09003965 copyCmds := apexRule.Args["copy_commands"]
3966
3967 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
3968}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003969
Jooyung Han91df2082019-11-20 01:49:42 +09003970func TestApexInVariousPartition(t *testing.T) {
3971 testcases := []struct {
3972 propName, parition, flattenedPartition string
3973 }{
3974 {"", "system", "system_ext"},
3975 {"product_specific: true", "product", "product"},
3976 {"soc_specific: true", "vendor", "vendor"},
3977 {"proprietary: true", "vendor", "vendor"},
3978 {"vendor: true", "vendor", "vendor"},
3979 {"system_ext_specific: true", "system_ext", "system_ext"},
3980 }
3981 for _, tc := range testcases {
3982 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003983 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09003984 apex {
3985 name: "myapex",
3986 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003987 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09003988 `+tc.propName+`
3989 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003990
Jooyung Han91df2082019-11-20 01:49:42 +09003991 apex_key {
3992 name: "myapex.key",
3993 public_key: "testkey.avbpubkey",
3994 private_key: "testkey.pem",
3995 }
3996 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003997
Jooyung Han91df2082019-11-20 01:49:42 +09003998 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01003999 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4000 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004001 if actual != expected {
4002 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4003 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004004
Jooyung Han91df2082019-11-20 01:49:42 +09004005 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004006 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4007 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004008 if actual != expected {
4009 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4010 }
4011 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004012 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004013}
Jiyong Park67882562019-03-21 01:11:21 +09004014
Jooyung Han580eb4f2020-06-24 19:33:06 +09004015func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004016 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004017 apex {
4018 name: "myapex",
4019 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004020 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004021 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004022
Jooyung Han580eb4f2020-06-24 19:33:06 +09004023 apex_key {
4024 name: "myapex.key",
4025 public_key: "testkey.avbpubkey",
4026 private_key: "testkey.pem",
4027 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004028 `)
4029 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004030 rule := module.Output("file_contexts")
4031 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4032}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004033
Jooyung Han580eb4f2020-06-24 19:33:06 +09004034func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004035 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004036 apex {
4037 name: "myapex",
4038 key: "myapex.key",
4039 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004040 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004041 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004042
Jooyung Han580eb4f2020-06-24 19:33:06 +09004043 apex_key {
4044 name: "myapex.key",
4045 public_key: "testkey.avbpubkey",
4046 private_key: "testkey.pem",
4047 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004048 `, withFiles(map[string][]byte{
4049 "my_own_file_contexts": nil,
4050 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004051}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004052
Jooyung Han580eb4f2020-06-24 19:33:06 +09004053func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004054 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004055 apex {
4056 name: "myapex",
4057 key: "myapex.key",
4058 product_specific: true,
4059 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004060 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004061 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004062
Jooyung Han580eb4f2020-06-24 19:33:06 +09004063 apex_key {
4064 name: "myapex.key",
4065 public_key: "testkey.avbpubkey",
4066 private_key: "testkey.pem",
4067 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004068 `)
4069
Colin Cross1c460562021-02-16 17:55:47 -08004070 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004071 apex {
4072 name: "myapex",
4073 key: "myapex.key",
4074 product_specific: true,
4075 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004076 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004077 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004078
Jooyung Han580eb4f2020-06-24 19:33:06 +09004079 apex_key {
4080 name: "myapex.key",
4081 public_key: "testkey.avbpubkey",
4082 private_key: "testkey.pem",
4083 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004084 `, withFiles(map[string][]byte{
4085 "product_specific_file_contexts": nil,
4086 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004087 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4088 rule := module.Output("file_contexts")
4089 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4090}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004091
Jooyung Han580eb4f2020-06-24 19:33:06 +09004092func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004093 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004094 apex {
4095 name: "myapex",
4096 key: "myapex.key",
4097 product_specific: true,
4098 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004099 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004100 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004101
Jooyung Han580eb4f2020-06-24 19:33:06 +09004102 apex_key {
4103 name: "myapex.key",
4104 public_key: "testkey.avbpubkey",
4105 private_key: "testkey.pem",
4106 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004107
Jooyung Han580eb4f2020-06-24 19:33:06 +09004108 filegroup {
4109 name: "my-file-contexts",
4110 srcs: ["product_specific_file_contexts"],
4111 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004112 `, withFiles(map[string][]byte{
4113 "product_specific_file_contexts": nil,
4114 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004115 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4116 rule := module.Output("file_contexts")
4117 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004118}
4119
Jiyong Park67882562019-03-21 01:11:21 +09004120func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004121 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004122 apex_key {
4123 name: "myapex.key",
4124 public_key: ":my.avbpubkey",
4125 private_key: ":my.pem",
4126 product_specific: true,
4127 }
4128
4129 filegroup {
4130 name: "my.avbpubkey",
4131 srcs: ["testkey2.avbpubkey"],
4132 }
4133
4134 filegroup {
4135 name: "my.pem",
4136 srcs: ["testkey2.pem"],
4137 }
4138 `)
4139
4140 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4141 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004142 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004143 if actual_pubkey != expected_pubkey {
4144 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4145 }
4146 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004147 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004148 if actual_privkey != expected_privkey {
4149 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4150 }
4151}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004152
4153func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004154 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004155 prebuilt_apex {
4156 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004157 arch: {
4158 arm64: {
4159 src: "myapex-arm64.apex",
4160 },
4161 arm: {
4162 src: "myapex-arm.apex",
4163 },
4164 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004165 }
4166 `)
4167
Paul Duffin5ec165d2021-06-15 19:09:41 +01004168 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004169
Jiyong Parkc95714e2019-03-29 14:23:10 +09004170 expectedInput := "myapex-arm64.apex"
4171 if prebuilt.inputApex.String() != expectedInput {
4172 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4173 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004174}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004175
Paul Duffinc0609c62021-03-01 17:27:16 +00004176func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin5ec165d2021-06-15 19:09:41 +01004177 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004178 prebuilt_apex {
4179 name: "myapex",
4180 }
4181 `)
4182}
4183
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004184func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004185 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004186 prebuilt_apex {
4187 name: "myapex",
4188 src: "myapex-arm.apex",
4189 filename: "notmyapex.apex",
4190 }
4191 `)
4192
Paul Duffin5ec165d2021-06-15 19:09:41 +01004193 p := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004194
4195 expected := "notmyapex.apex"
4196 if p.installFilename != expected {
4197 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4198 }
4199}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004200
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004201func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004202 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004203 prebuilt_apex {
4204 name: "myapex.prebuilt",
4205 src: "myapex-arm.apex",
4206 overrides: [
4207 "myapex",
4208 ],
4209 }
4210 `)
4211
Paul Duffin5ec165d2021-06-15 19:09:41 +01004212 p := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt").Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004213
4214 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004215 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004216 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004217 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004218 }
4219}
4220
Martin Stjernholmd8da28e2021-06-24 14:37:13 +01004221func TestPrebuiltApexName(t *testing.T) {
4222 testApex(t, `
4223 prebuilt_apex {
4224 name: "com.company.android.myapex",
4225 apex_name: "com.android.myapex",
4226 src: "company-myapex-arm.apex",
4227 }
4228 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4229
4230 testApex(t, `
4231 apex_set {
4232 name: "com.company.android.myapex",
4233 apex_name: "com.android.myapex",
4234 set: "company-myapex.apks",
4235 }
4236 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4237}
4238
4239func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4240 _ = android.GroupFixturePreparers(
4241 java.PrepareForTestWithJavaDefaultModules,
4242 PrepareForTestWithApexBuildComponents,
4243 android.FixtureWithRootAndroidBp(`
4244 platform_bootclasspath {
4245 name: "platform-bootclasspath",
4246 fragments: [
4247 {
4248 apex: "com.android.art",
4249 module: "art-bootclasspath-fragment",
4250 },
4251 ],
4252 }
4253
4254 prebuilt_apex {
4255 name: "com.company.android.art",
4256 apex_name: "com.android.art",
4257 src: "com.company.android.art-arm.apex",
4258 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4259 }
4260
4261 prebuilt_bootclasspath_fragment {
4262 name: "art-bootclasspath-fragment",
4263 contents: ["core-oj"],
4264 }
4265
4266 java_import {
4267 name: "core-oj",
4268 jars: ["prebuilt.jar"],
4269 }
4270 `),
4271 ).RunTest(t)
4272}
4273
Paul Duffin092153d2021-01-26 11:42:39 +00004274// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4275// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004276func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004277 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004278
Paul Duffin89886cb2021-02-05 16:44:03 +00004279 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004280 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004281 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004282 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004283 stem := android.RemoveOptionalPrebuiltPrefix(name)
4284 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004285 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4286 }
4287 }
4288
Paul Duffin39853512021-02-26 11:09:39 +00004289 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004290 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004291 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004292 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4293 }
4294 }
4295
4296 t.Run("prebuilt only", func(t *testing.T) {
4297 bp := `
4298 prebuilt_apex {
4299 name: "myapex",
4300 arch: {
4301 arm64: {
4302 src: "myapex-arm64.apex",
4303 },
4304 arm: {
4305 src: "myapex-arm.apex",
4306 },
4307 },
Paul Duffin39853512021-02-26 11:09:39 +00004308 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004309 }
4310
4311 java_import {
4312 name: "libfoo",
4313 jars: ["libfoo.jar"],
4314 }
Paul Duffin39853512021-02-26 11:09:39 +00004315
4316 java_sdk_library_import {
4317 name: "libbar",
4318 public: {
4319 jars: ["libbar.jar"],
4320 },
4321 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004322 `
4323
4324 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4325 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4326
Paul Duffinf6932af2021-02-26 18:21:56 +00004327 // Make sure that the deapexer has the correct input APEX.
4328 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4329 rule := deapexer.Rule("deapexer")
4330 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4331 t.Errorf("expected: %q, found: %q", expected, actual)
4332 }
4333
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004334 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin5ec165d2021-06-15 19:09:41 +01004335 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004336 rule = prebuiltApex.Rule("android/soong/android.Cp")
4337 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4338 t.Errorf("expected: %q, found: %q", expected, actual)
4339 }
4340
Paul Duffin89886cb2021-02-05 16:44:03 +00004341 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004342
4343 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004344 })
4345
4346 t.Run("prebuilt with source preferred", func(t *testing.T) {
4347
4348 bp := `
4349 prebuilt_apex {
4350 name: "myapex",
4351 arch: {
4352 arm64: {
4353 src: "myapex-arm64.apex",
4354 },
4355 arm: {
4356 src: "myapex-arm.apex",
4357 },
4358 },
Paul Duffin39853512021-02-26 11:09:39 +00004359 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004360 }
4361
4362 java_import {
4363 name: "libfoo",
4364 jars: ["libfoo.jar"],
4365 }
4366
4367 java_library {
4368 name: "libfoo",
4369 }
Paul Duffin39853512021-02-26 11:09:39 +00004370
4371 java_sdk_library_import {
4372 name: "libbar",
4373 public: {
4374 jars: ["libbar.jar"],
4375 },
4376 }
4377
4378 java_sdk_library {
4379 name: "libbar",
4380 srcs: ["foo/bar/MyClass.java"],
4381 unsafe_ignore_missing_latest_api: true,
4382 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004383 `
4384
4385 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4386 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4387
Paul Duffin89886cb2021-02-05 16:44:03 +00004388 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004389 ensureNoSourceVariant(t, ctx, "libfoo")
4390
4391 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4392 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004393 })
4394
4395 t.Run("prebuilt preferred with source", func(t *testing.T) {
4396 bp := `
4397 prebuilt_apex {
4398 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004399 arch: {
4400 arm64: {
4401 src: "myapex-arm64.apex",
4402 },
4403 arm: {
4404 src: "myapex-arm.apex",
4405 },
4406 },
Paul Duffin39853512021-02-26 11:09:39 +00004407 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004408 }
4409
4410 java_import {
4411 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004412 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004413 jars: ["libfoo.jar"],
4414 }
4415
4416 java_library {
4417 name: "libfoo",
4418 }
Paul Duffin39853512021-02-26 11:09:39 +00004419
4420 java_sdk_library_import {
4421 name: "libbar",
4422 prefer: true,
4423 public: {
4424 jars: ["libbar.jar"],
4425 },
4426 }
4427
4428 java_sdk_library {
4429 name: "libbar",
4430 srcs: ["foo/bar/MyClass.java"],
4431 unsafe_ignore_missing_latest_api: true,
4432 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004433 `
4434
4435 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4436 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4437
Paul Duffin89886cb2021-02-05 16:44:03 +00004438 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004439 ensureNoSourceVariant(t, ctx, "libfoo")
4440
4441 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4442 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004443 })
4444}
4445
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004446func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01004447 preparer := android.GroupFixturePreparers(
4448 java.FixtureConfigureBootJars("myapex:libfoo", "myapex:libbar"),
4449 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
4450 // is disabled.
4451 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
4452 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004453
Paul Duffin37856732021-02-26 14:24:15 +00004454 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4455 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01004456 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004457 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004458 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004459 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004460 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004461 foundLibfooJar = true
4462 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01004463 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004464 }
4465 }
4466 if !foundLibfooJar {
Paul Duffin55607122021-03-30 23:32:51 +01004467 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().BuildDir(), s.AllOutputs()))
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004468 }
4469 }
4470
Paul Duffin2a999332021-06-07 21:36:01 +01004471 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004472 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01004473 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffin2a999332021-06-07 21:36:01 +01004474 var rule android.TestingBuildParams
4475
4476 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
4477 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00004478 }
4479
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004480 fragment := java.ApexVariantReference{
4481 Apex: proptools.StringPtr("myapex"),
4482 Module: proptools.StringPtr("my-bootclasspath-fragment"),
4483 }
4484
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004485 t.Run("prebuilt only", func(t *testing.T) {
4486 bp := `
4487 prebuilt_apex {
4488 name: "myapex",
4489 arch: {
4490 arm64: {
4491 src: "myapex-arm64.apex",
4492 },
4493 arm: {
4494 src: "myapex-arm.apex",
4495 },
4496 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004497 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4498 }
4499
4500 prebuilt_bootclasspath_fragment {
4501 name: "my-bootclasspath-fragment",
4502 contents: ["libfoo", "libbar"],
4503 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004504 }
4505
4506 java_import {
4507 name: "libfoo",
4508 jars: ["libfoo.jar"],
4509 apex_available: ["myapex"],
4510 }
Paul Duffin37856732021-02-26 14:24:15 +00004511
4512 java_sdk_library_import {
4513 name: "libbar",
4514 public: {
4515 jars: ["libbar.jar"],
4516 },
4517 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004518 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004519 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004520 `
4521
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004522 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004523 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4524 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004525
Paul Duffin537ea3d2021-05-14 10:38:00 +01004526 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin9d67ca62021-02-03 20:06:33 +00004527 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004528 out/soong/.intermediates/libbar.stubs/android_common/combined/libbar.stubs.jar
4529 out/soong/.intermediates/libfoo/android_common_myapex/combined/libfoo.jar
4530 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004531 })
4532
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004533 t.Run("apex_set only", func(t *testing.T) {
4534 bp := `
4535 apex_set {
4536 name: "myapex",
4537 set: "myapex.apks",
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004538 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4539 }
4540
4541 prebuilt_bootclasspath_fragment {
4542 name: "my-bootclasspath-fragment",
4543 contents: ["libfoo", "libbar"],
4544 apex_available: ["myapex"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004545 }
4546
4547 java_import {
4548 name: "libfoo",
4549 jars: ["libfoo.jar"],
4550 apex_available: ["myapex"],
4551 }
4552
4553 java_sdk_library_import {
4554 name: "libbar",
4555 public: {
4556 jars: ["libbar.jar"],
4557 },
4558 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004559 shared_library: false,
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004560 }
4561 `
4562
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004563 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004564 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4565 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
4566
Paul Duffin537ea3d2021-05-14 10:38:00 +01004567 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004568 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004569 out/soong/.intermediates/libbar.stubs/android_common/combined/libbar.stubs.jar
4570 out/soong/.intermediates/libfoo/android_common_myapex/combined/libfoo.jar
4571 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004572 })
4573
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004574 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4575 bp := `
4576 prebuilt_apex {
4577 name: "myapex",
4578 arch: {
4579 arm64: {
4580 src: "myapex-arm64.apex",
4581 },
4582 arm: {
4583 src: "myapex-arm.apex",
4584 },
4585 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004586 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4587 }
4588
4589 prebuilt_bootclasspath_fragment {
4590 name: "my-bootclasspath-fragment",
4591 contents: ["libfoo", "libbar"],
4592 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004593 }
4594
4595 java_import {
4596 name: "libfoo",
4597 jars: ["libfoo.jar"],
4598 apex_available: ["myapex"],
4599 }
4600
4601 java_library {
4602 name: "libfoo",
4603 srcs: ["foo/bar/MyClass.java"],
4604 apex_available: ["myapex"],
4605 }
Paul Duffin37856732021-02-26 14:24:15 +00004606
4607 java_sdk_library_import {
4608 name: "libbar",
4609 public: {
4610 jars: ["libbar.jar"],
4611 },
4612 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004613 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004614 }
4615
4616 java_sdk_library {
4617 name: "libbar",
4618 srcs: ["foo/bar/MyClass.java"],
4619 unsafe_ignore_missing_latest_api: true,
4620 apex_available: ["myapex"],
4621 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004622 `
4623
4624 // In this test the source (java_library) libfoo is active since the
4625 // prebuilt (java_import) defaults to prefer:false. However the
4626 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4627 // find the dex boot jar in it. We either need to disable the source libfoo
4628 // or make the prebuilt libfoo preferred.
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004629 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004630 })
4631
4632 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4633 bp := `
4634 prebuilt_apex {
4635 name: "myapex",
4636 arch: {
4637 arm64: {
4638 src: "myapex-arm64.apex",
4639 },
4640 arm: {
4641 src: "myapex-arm.apex",
4642 },
4643 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004644 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4645 }
4646
4647 prebuilt_bootclasspath_fragment {
4648 name: "my-bootclasspath-fragment",
4649 contents: ["libfoo", "libbar"],
4650 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004651 }
4652
4653 java_import {
4654 name: "libfoo",
4655 prefer: true,
4656 jars: ["libfoo.jar"],
4657 apex_available: ["myapex"],
4658 }
4659
4660 java_library {
4661 name: "libfoo",
4662 srcs: ["foo/bar/MyClass.java"],
4663 apex_available: ["myapex"],
4664 }
Paul Duffin37856732021-02-26 14:24:15 +00004665
4666 java_sdk_library_import {
4667 name: "libbar",
4668 prefer: true,
4669 public: {
4670 jars: ["libbar.jar"],
4671 },
4672 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004673 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004674 }
4675
4676 java_sdk_library {
4677 name: "libbar",
4678 srcs: ["foo/bar/MyClass.java"],
4679 unsafe_ignore_missing_latest_api: true,
4680 apex_available: ["myapex"],
4681 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004682 `
4683
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004684 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004685 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4686 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004687
Paul Duffin537ea3d2021-05-14 10:38:00 +01004688 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin9d67ca62021-02-03 20:06:33 +00004689 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004690 out/soong/.intermediates/prebuilt_libbar.stubs/android_common/combined/libbar.stubs.jar
4691 out/soong/.intermediates/prebuilt_libfoo/android_common_myapex/combined/libfoo.jar
4692 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004693 })
4694
4695 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4696 bp := `
4697 apex {
4698 name: "myapex",
4699 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004700 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004701 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004702 }
4703
4704 apex_key {
4705 name: "myapex.key",
4706 public_key: "testkey.avbpubkey",
4707 private_key: "testkey.pem",
4708 }
4709
4710 prebuilt_apex {
4711 name: "myapex",
4712 arch: {
4713 arm64: {
4714 src: "myapex-arm64.apex",
4715 },
4716 arm: {
4717 src: "myapex-arm.apex",
4718 },
4719 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004720 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4721 }
4722
4723 prebuilt_bootclasspath_fragment {
4724 name: "my-bootclasspath-fragment",
4725 contents: ["libfoo", "libbar"],
4726 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004727 }
4728
4729 java_import {
4730 name: "libfoo",
4731 jars: ["libfoo.jar"],
4732 apex_available: ["myapex"],
4733 }
4734
4735 java_library {
4736 name: "libfoo",
4737 srcs: ["foo/bar/MyClass.java"],
4738 apex_available: ["myapex"],
4739 }
Paul Duffin37856732021-02-26 14:24:15 +00004740
4741 java_sdk_library_import {
4742 name: "libbar",
4743 public: {
4744 jars: ["libbar.jar"],
4745 },
4746 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004747 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004748 }
4749
4750 java_sdk_library {
4751 name: "libbar",
4752 srcs: ["foo/bar/MyClass.java"],
4753 unsafe_ignore_missing_latest_api: true,
4754 apex_available: ["myapex"],
4755 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004756 `
4757
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004758 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004759 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4760 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004761
Paul Duffin537ea3d2021-05-14 10:38:00 +01004762 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin4fd997b2021-02-03 20:06:33 +00004763 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004764 out/soong/.intermediates/libbar/android_common_myapex/javac/libbar.jar
4765 out/soong/.intermediates/libfoo/android_common_apex10000/javac/libfoo.jar
4766 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004767 })
4768
4769 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4770 bp := `
4771 apex {
4772 name: "myapex",
4773 enabled: false,
4774 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01004775 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004776 }
4777
4778 apex_key {
4779 name: "myapex.key",
4780 public_key: "testkey.avbpubkey",
4781 private_key: "testkey.pem",
4782 }
4783
4784 prebuilt_apex {
4785 name: "myapex",
4786 arch: {
4787 arm64: {
4788 src: "myapex-arm64.apex",
4789 },
4790 arm: {
4791 src: "myapex-arm.apex",
4792 },
4793 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004794 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4795 }
4796
4797 prebuilt_bootclasspath_fragment {
4798 name: "my-bootclasspath-fragment",
4799 contents: ["libfoo", "libbar"],
4800 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004801 }
4802
4803 java_import {
4804 name: "libfoo",
4805 prefer: true,
4806 jars: ["libfoo.jar"],
4807 apex_available: ["myapex"],
4808 }
4809
4810 java_library {
4811 name: "libfoo",
4812 srcs: ["foo/bar/MyClass.java"],
4813 apex_available: ["myapex"],
4814 }
Paul Duffin37856732021-02-26 14:24:15 +00004815
4816 java_sdk_library_import {
4817 name: "libbar",
4818 prefer: true,
4819 public: {
4820 jars: ["libbar.jar"],
4821 },
4822 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004823 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004824 }
4825
4826 java_sdk_library {
4827 name: "libbar",
4828 srcs: ["foo/bar/MyClass.java"],
4829 unsafe_ignore_missing_latest_api: true,
4830 apex_available: ["myapex"],
4831 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004832 `
4833
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004834 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004835 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4836 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004837
Paul Duffin537ea3d2021-05-14 10:38:00 +01004838 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin9d67ca62021-02-03 20:06:33 +00004839 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004840 out/soong/.intermediates/prebuilt_libbar.stubs/android_common/combined/libbar.stubs.jar
4841 out/soong/.intermediates/prebuilt_libfoo/android_common_myapex/combined/libfoo.jar
4842 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004843 })
4844}
4845
Roland Levillain630846d2019-06-26 12:48:34 +01004846func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004847 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004848 apex_test {
4849 name: "myapex",
4850 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004851 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01004852 tests: [
4853 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004854 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004855 ],
4856 }
4857
4858 apex_key {
4859 name: "myapex.key",
4860 public_key: "testkey.avbpubkey",
4861 private_key: "testkey.pem",
4862 }
4863
Liz Kammer1c14a212020-05-12 15:26:55 -07004864 filegroup {
4865 name: "fg",
4866 srcs: [
4867 "baz",
4868 "bar/baz"
4869 ],
4870 }
4871
Roland Levillain630846d2019-06-26 12:48:34 +01004872 cc_test {
4873 name: "mytest",
4874 gtest: false,
4875 srcs: ["mytest.cpp"],
4876 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004877 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004878 system_shared_libs: [],
4879 static_executable: true,
4880 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004881 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004882 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004883
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004884 cc_library {
4885 name: "mylib",
4886 srcs: ["mylib.cpp"],
4887 system_shared_libs: [],
4888 stl: "none",
4889 }
4890
Liz Kammer5bd365f2020-05-27 15:15:11 -07004891 filegroup {
4892 name: "fg2",
4893 srcs: [
4894 "testdata/baz"
4895 ],
4896 }
4897
Roland Levillain9b5fde92019-06-28 15:41:19 +01004898 cc_test {
4899 name: "mytests",
4900 gtest: false,
4901 srcs: [
4902 "mytest1.cpp",
4903 "mytest2.cpp",
4904 "mytest3.cpp",
4905 ],
4906 test_per_src: true,
4907 relative_install_path: "test",
4908 system_shared_libs: [],
4909 static_executable: true,
4910 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004911 data: [
4912 ":fg",
4913 ":fg2",
4914 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004915 }
Roland Levillain630846d2019-06-26 12:48:34 +01004916 `)
4917
Sundong Ahnabb64432019-10-22 13:58:29 +09004918 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004919 copyCmds := apexRule.Args["copy_commands"]
4920
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004921 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004922 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004923 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004924
Liz Kammer1c14a212020-05-12 15:26:55 -07004925 //Ensure that test data are copied into apex.
4926 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4927 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4928
Roland Levillain9b5fde92019-06-28 15:41:19 +01004929 // Ensure that test deps built with `test_per_src` are copied into apex.
4930 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4931 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4932 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004933
4934 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004935 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004936 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004937 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004938 prefix := "TARGET_"
4939 var builder strings.Builder
4940 data.Custom(&builder, name, prefix, "", data)
4941 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004942 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4943 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4944 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4945 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004946 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004947 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004948 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004949
4950 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004951 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004952 data.Custom(&builder, name, prefix, "", data)
4953 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004954 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4955 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004956}
4957
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004958func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004959 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004960 apex {
4961 name: "myapex",
4962 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004963 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004964 }
4965 apex_key {
4966 name: "myapex.key",
4967 public_key: "testkey.avbpubkey",
4968 private_key: "testkey.pem",
4969 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00004970 `,
4971 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4972 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4973 }),
4974 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004975 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004976 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07004977 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004978 var builder strings.Builder
4979 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4980 androidMk := builder.String()
4981 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4982}
4983
Jooyung Hand48f3c32019-08-23 11:18:57 +09004984func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4985 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4986 apex {
4987 name: "myapex",
4988 key: "myapex.key",
4989 native_shared_libs: ["libfoo"],
4990 }
4991
4992 apex_key {
4993 name: "myapex.key",
4994 public_key: "testkey.avbpubkey",
4995 private_key: "testkey.pem",
4996 }
4997
4998 cc_library {
4999 name: "libfoo",
5000 stl: "none",
5001 system_shared_libs: [],
5002 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005003 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005004 }
5005 `)
5006 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5007 apex {
5008 name: "myapex",
5009 key: "myapex.key",
5010 java_libs: ["myjar"],
5011 }
5012
5013 apex_key {
5014 name: "myapex.key",
5015 public_key: "testkey.avbpubkey",
5016 private_key: "testkey.pem",
5017 }
5018
5019 java_library {
5020 name: "myjar",
5021 srcs: ["foo/bar/MyClass.java"],
5022 sdk_version: "none",
5023 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005024 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005025 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005026 }
5027 `)
5028}
5029
Bill Peckhama41a6962021-01-11 10:58:54 -08005030func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005031 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005032 apex {
5033 name: "myapex",
5034 key: "myapex.key",
5035 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005036 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005037 }
5038
5039 apex_key {
5040 name: "myapex.key",
5041 public_key: "testkey.avbpubkey",
5042 private_key: "testkey.pem",
5043 }
5044
5045 java_import {
5046 name: "myjavaimport",
5047 apex_available: ["myapex"],
5048 jars: ["my.jar"],
5049 compile_dex: true,
5050 }
5051 `)
5052
5053 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5054 apexRule := module.Rule("apexRule")
5055 copyCmds := apexRule.Args["copy_commands"]
5056 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5057}
5058
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005059func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005060 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005061 apex {
5062 name: "myapex",
5063 key: "myapex.key",
5064 apps: [
5065 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005066 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005067 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005068 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005069 }
5070
5071 apex_key {
5072 name: "myapex.key",
5073 public_key: "testkey.avbpubkey",
5074 private_key: "testkey.pem",
5075 }
5076
5077 android_app {
5078 name: "AppFoo",
5079 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005080 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005081 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005082 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005083 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005084 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005085 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005086
5087 android_app {
5088 name: "AppFooPriv",
5089 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005090 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005091 system_modules: "none",
5092 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005093 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005094 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005095 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005096
5097 cc_library_shared {
5098 name: "libjni",
5099 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005100 shared_libs: ["libfoo"],
5101 stl: "none",
5102 system_shared_libs: [],
5103 apex_available: [ "myapex" ],
5104 sdk_version: "current",
5105 }
5106
5107 cc_library_shared {
5108 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005109 stl: "none",
5110 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005111 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005112 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005113 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005114 `)
5115
Sundong Ahnabb64432019-10-22 13:58:29 +09005116 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005117 apexRule := module.Rule("apexRule")
5118 copyCmds := apexRule.Args["copy_commands"]
5119
5120 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005121 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005122
Colin Crossaede88c2020-08-11 12:17:01 -07005123 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005124 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005125 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005126 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005127 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005128 // JNI libraries including transitive deps are
5129 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005130 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005131 // ... embedded inside APK (jnilibs.zip)
5132 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5133 // ... and not directly inside the APEX
5134 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5135 }
Dario Frenicde2a032019-10-27 00:29:22 +01005136}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005137
Dario Frenicde2a032019-10-27 00:29:22 +01005138func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005139 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005140 apex {
5141 name: "myapex",
5142 key: "myapex.key",
5143 apps: [
5144 "AppFooPrebuilt",
5145 "AppFooPrivPrebuilt",
5146 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005147 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005148 }
5149
5150 apex_key {
5151 name: "myapex.key",
5152 public_key: "testkey.avbpubkey",
5153 private_key: "testkey.pem",
5154 }
5155
5156 android_app_import {
5157 name: "AppFooPrebuilt",
5158 apk: "PrebuiltAppFoo.apk",
5159 presigned: true,
5160 dex_preopt: {
5161 enabled: false,
5162 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005163 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005164 }
5165
5166 android_app_import {
5167 name: "AppFooPrivPrebuilt",
5168 apk: "PrebuiltAppFooPriv.apk",
5169 privileged: true,
5170 presigned: true,
5171 dex_preopt: {
5172 enabled: false,
5173 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005174 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005175 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005176 }
5177 `)
5178
Sundong Ahnabb64432019-10-22 13:58:29 +09005179 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005180 apexRule := module.Rule("apexRule")
5181 copyCmds := apexRule.Args["copy_commands"]
5182
5183 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005184 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5185}
5186
5187func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005188 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005189 apex {
5190 name: "myapex",
5191 key: "myapex.key",
5192 apps: [
5193 "AppFoo",
5194 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005195 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005196 }
5197
5198 apex_key {
5199 name: "myapex.key",
5200 public_key: "testkey.avbpubkey",
5201 private_key: "testkey.pem",
5202 }
5203
5204 android_app {
5205 name: "AppFoo",
5206 srcs: ["foo/bar/MyClass.java"],
5207 sdk_version: "none",
5208 system_modules: "none",
5209 apex_available: [ "myapex" ],
5210 }
5211
5212 android_app_import {
5213 name: "AppFoo",
5214 apk: "AppFooPrebuilt.apk",
5215 filename: "AppFooPrebuilt.apk",
5216 presigned: true,
5217 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005218 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005219 }
5220 `, withFiles(map[string][]byte{
5221 "AppFooPrebuilt.apk": nil,
5222 }))
5223
5224 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005225 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005226 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005227}
5228
Dario Freni6f3937c2019-12-20 22:58:03 +00005229func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005230 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005231 apex {
5232 name: "myapex",
5233 key: "myapex.key",
5234 apps: [
5235 "TesterHelpAppFoo",
5236 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005237 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005238 }
5239
5240 apex_key {
5241 name: "myapex.key",
5242 public_key: "testkey.avbpubkey",
5243 private_key: "testkey.pem",
5244 }
5245
5246 android_test_helper_app {
5247 name: "TesterHelpAppFoo",
5248 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005249 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005250 }
5251
5252 `)
5253
5254 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5255 apexRule := module.Rule("apexRule")
5256 copyCmds := apexRule.Args["copy_commands"]
5257
5258 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5259}
5260
Jooyung Han18020ea2019-11-13 10:50:48 +09005261func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5262 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005263 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005264 apex {
5265 name: "myapex",
5266 key: "myapex.key",
5267 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005268 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005269 }
5270
5271 apex_key {
5272 name: "myapex.key",
5273 public_key: "testkey.avbpubkey",
5274 private_key: "testkey.pem",
5275 }
5276
5277 apex {
5278 name: "otherapex",
5279 key: "myapex.key",
5280 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005281 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005282 }
5283
5284 cc_defaults {
5285 name: "libfoo-defaults",
5286 apex_available: ["otherapex"],
5287 }
5288
5289 cc_library {
5290 name: "libfoo",
5291 defaults: ["libfoo-defaults"],
5292 stl: "none",
5293 system_shared_libs: [],
5294 }`)
5295}
5296
Paul Duffine52e66f2020-03-30 17:54:29 +01005297func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005298 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005299 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005300 apex {
5301 name: "myapex",
5302 key: "myapex.key",
5303 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005304 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005305 }
5306
5307 apex_key {
5308 name: "myapex.key",
5309 public_key: "testkey.avbpubkey",
5310 private_key: "testkey.pem",
5311 }
5312
5313 apex {
5314 name: "otherapex",
5315 key: "otherapex.key",
5316 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005317 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005318 }
5319
5320 apex_key {
5321 name: "otherapex.key",
5322 public_key: "testkey.avbpubkey",
5323 private_key: "testkey.pem",
5324 }
5325
5326 cc_library {
5327 name: "libfoo",
5328 stl: "none",
5329 system_shared_libs: [],
5330 apex_available: ["otherapex"],
5331 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005332}
Jiyong Park127b40b2019-09-30 16:04:35 +09005333
Paul Duffine52e66f2020-03-30 17:54:29 +01005334func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005335 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005336 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005337.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005338.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005339.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005340.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005341.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005342.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005343 apex {
5344 name: "myapex",
5345 key: "myapex.key",
5346 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005347 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005348 }
5349
5350 apex_key {
5351 name: "myapex.key",
5352 public_key: "testkey.avbpubkey",
5353 private_key: "testkey.pem",
5354 }
5355
Jiyong Park127b40b2019-09-30 16:04:35 +09005356 cc_library {
5357 name: "libfoo",
5358 stl: "none",
5359 shared_libs: ["libbar"],
5360 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005361 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005362 }
5363
5364 cc_library {
5365 name: "libbar",
5366 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005367 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005368 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005369 apex_available: ["myapex"],
5370 }
5371
5372 cc_library {
5373 name: "libbaz",
5374 stl: "none",
5375 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005376 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005377}
Jiyong Park127b40b2019-09-30 16:04:35 +09005378
Paul Duffine52e66f2020-03-30 17:54:29 +01005379func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005380 testApexError(t, "\"otherapex\" is not a valid module name", `
5381 apex {
5382 name: "myapex",
5383 key: "myapex.key",
5384 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005385 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005386 }
5387
5388 apex_key {
5389 name: "myapex.key",
5390 public_key: "testkey.avbpubkey",
5391 private_key: "testkey.pem",
5392 }
5393
5394 cc_library {
5395 name: "libfoo",
5396 stl: "none",
5397 system_shared_libs: [],
5398 apex_available: ["otherapex"],
5399 }`)
5400
Paul Duffine52e66f2020-03-30 17:54:29 +01005401 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005402 apex {
5403 name: "myapex",
5404 key: "myapex.key",
5405 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005406 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005407 }
5408
5409 apex_key {
5410 name: "myapex.key",
5411 public_key: "testkey.avbpubkey",
5412 private_key: "testkey.pem",
5413 }
5414
5415 cc_library {
5416 name: "libfoo",
5417 stl: "none",
5418 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005419 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005420 apex_available: ["myapex"],
5421 }
5422
5423 cc_library {
5424 name: "libbar",
5425 stl: "none",
5426 system_shared_libs: [],
5427 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005428 }
5429
5430 cc_library {
5431 name: "libbaz",
5432 stl: "none",
5433 system_shared_libs: [],
5434 stubs: {
5435 versions: ["10", "20", "30"],
5436 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005437 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005438}
Jiyong Park127b40b2019-09-30 16:04:35 +09005439
Jiyong Park89e850a2020-04-07 16:37:39 +09005440func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005441 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005442 apex {
5443 name: "myapex",
5444 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005445 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005446 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005447 }
5448
5449 apex_key {
5450 name: "myapex.key",
5451 public_key: "testkey.avbpubkey",
5452 private_key: "testkey.pem",
5453 }
5454
5455 cc_library {
5456 name: "libfoo",
5457 stl: "none",
5458 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005459 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005460 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005461 }
5462
5463 cc_library {
5464 name: "libfoo2",
5465 stl: "none",
5466 system_shared_libs: [],
5467 shared_libs: ["libbaz"],
5468 apex_available: ["//apex_available:platform"],
5469 }
5470
5471 cc_library {
5472 name: "libbar",
5473 stl: "none",
5474 system_shared_libs: [],
5475 apex_available: ["myapex"],
5476 }
5477
5478 cc_library {
5479 name: "libbaz",
5480 stl: "none",
5481 system_shared_libs: [],
5482 apex_available: ["myapex"],
5483 stubs: {
5484 versions: ["1"],
5485 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005486 }`)
5487
Jiyong Park89e850a2020-04-07 16:37:39 +09005488 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5489 // because it depends on libbar which isn't available to platform
5490 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5491 if libfoo.NotAvailableForPlatform() != true {
5492 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5493 }
5494
5495 // libfoo2 however can be available to platform because it depends on libbaz which provides
5496 // stubs
5497 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5498 if libfoo2.NotAvailableForPlatform() == true {
5499 t.Errorf("%q should be available to platform", libfoo2.String())
5500 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005501}
Jiyong Parka90ca002019-10-07 15:47:24 +09005502
Paul Duffine52e66f2020-03-30 17:54:29 +01005503func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005504 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005505 apex {
5506 name: "myapex",
5507 key: "myapex.key",
5508 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005509 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005510 }
5511
5512 apex_key {
5513 name: "myapex.key",
5514 public_key: "testkey.avbpubkey",
5515 private_key: "testkey.pem",
5516 }
5517
5518 cc_library {
5519 name: "libfoo",
5520 stl: "none",
5521 system_shared_libs: [],
5522 apex_available: ["myapex"],
5523 static: {
5524 apex_available: ["//apex_available:platform"],
5525 },
5526 }`)
5527
Jiyong Park89e850a2020-04-07 16:37:39 +09005528 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5529 if libfooShared.NotAvailableForPlatform() != true {
5530 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5531 }
5532 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5533 if libfooStatic.NotAvailableForPlatform() != false {
5534 t.Errorf("%q should be available to platform", libfooStatic.String())
5535 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005536}
5537
Jiyong Park5d790c32019-11-15 18:40:32 +09005538func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005539 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005540 apex {
5541 name: "myapex",
5542 key: "myapex.key",
5543 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005544 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005545 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005546 }
5547
5548 override_apex {
5549 name: "override_myapex",
5550 base: "myapex",
5551 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005552 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005553 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005554 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07005555 key: "mynewapex.key",
5556 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09005557 }
5558
5559 apex_key {
5560 name: "myapex.key",
5561 public_key: "testkey.avbpubkey",
5562 private_key: "testkey.pem",
5563 }
5564
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07005565 apex_key {
5566 name: "mynewapex.key",
5567 public_key: "testkey2.avbpubkey",
5568 private_key: "testkey2.pem",
5569 }
5570
5571 android_app_certificate {
5572 name: "myapex.certificate",
5573 certificate: "testkey",
5574 }
5575
Jiyong Park5d790c32019-11-15 18:40:32 +09005576 android_app {
5577 name: "app",
5578 srcs: ["foo/bar/MyClass.java"],
5579 package_name: "foo",
5580 sdk_version: "none",
5581 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005582 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005583 }
5584
5585 override_android_app {
5586 name: "override_app",
5587 base: "app",
5588 package_name: "bar",
5589 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005590 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005591
Jiyong Park317645e2019-12-05 13:20:58 +09005592 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5593 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5594 if originalVariant.GetOverriddenBy() != "" {
5595 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5596 }
5597 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5598 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5599 }
5600
Jiyong Park5d790c32019-11-15 18:40:32 +09005601 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5602 apexRule := module.Rule("apexRule")
5603 copyCmds := apexRule.Args["copy_commands"]
5604
5605 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005606 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005607
5608 apexBundle := module.Module().(*apexBundle)
5609 name := apexBundle.Name()
5610 if name != "override_myapex" {
5611 t.Errorf("name should be \"override_myapex\", but was %q", name)
5612 }
5613
Baligh Uddin004d7172020-02-19 21:29:28 -08005614 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5615 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5616 }
5617
Jiyong Park20bacab2020-03-03 11:45:41 +09005618 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005619 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07005620 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
5621
5622 signApkRule := module.Rule("signapk")
5623 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09005624
Colin Crossaa255532020-07-03 13:18:24 -07005625 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005626 var builder strings.Builder
5627 data.Custom(&builder, name, "TARGET_", "", data)
5628 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005629 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005630 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5631 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005632 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005633 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005634 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005635 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5636 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005637}
5638
Jooyung Han214bf372019-11-12 13:03:50 +09005639func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005640 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005641 apex {
5642 name: "myapex",
5643 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005644 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005645 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005646 }
5647
5648 apex_key {
5649 name: "myapex.key",
5650 public_key: "testkey.avbpubkey",
5651 private_key: "testkey.pem",
5652 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005653
5654 cc_library {
5655 name: "mylib",
5656 srcs: ["mylib.cpp"],
5657 stl: "libc++",
5658 system_shared_libs: [],
5659 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005660 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005661 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005662 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005663
5664 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5665 args := module.Rule("apexRule").Args
5666 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005667 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005668
5669 // The copies of the libraries in the apex should have one more dependency than
5670 // the ones outside the apex, namely the unwinder. Ideally we should check
5671 // the dependency names directly here but for some reason the names are blank in
5672 // this test.
5673 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005674 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005675 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5676 if len(apexImplicits) != len(nonApexImplicits)+1 {
5677 t.Errorf("%q missing unwinder dep", lib)
5678 }
5679 }
Jooyung Han214bf372019-11-12 13:03:50 +09005680}
5681
Paul Duffine05480a2021-03-08 15:07:14 +00005682var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01005683 "api/current.txt": nil,
5684 "api/removed.txt": nil,
5685 "api/system-current.txt": nil,
5686 "api/system-removed.txt": nil,
5687 "api/test-current.txt": nil,
5688 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005689
Anton Hanssondff2c782020-12-21 17:10:01 +00005690 "100/public/api/foo.txt": nil,
5691 "100/public/api/foo-removed.txt": nil,
5692 "100/system/api/foo.txt": nil,
5693 "100/system/api/foo-removed.txt": nil,
5694
Paul Duffineedc5d52020-06-12 17:46:39 +01005695 // For java_sdk_library_import
5696 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005697}
5698
Jooyung Han58f26ab2019-12-18 15:34:32 +09005699func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005700 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09005701 apex {
5702 name: "myapex",
5703 key: "myapex.key",
5704 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005705 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09005706 }
5707
5708 apex_key {
5709 name: "myapex.key",
5710 public_key: "testkey.avbpubkey",
5711 private_key: "testkey.pem",
5712 }
5713
5714 java_sdk_library {
5715 name: "foo",
5716 srcs: ["a.java"],
5717 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005718 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005719 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005720
5721 prebuilt_apis {
5722 name: "sdk",
5723 api_dirs: ["100"],
5724 }
Paul Duffin9b879592020-05-26 13:21:35 +01005725 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005726
5727 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005728 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005729 "javalib/foo.jar",
5730 "etc/permissions/foo.xml",
5731 })
5732 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005733 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5734 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005735}
5736
Paul Duffin9b879592020-05-26 13:21:35 +01005737func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005738 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005739 apex {
5740 name: "myapex",
5741 key: "myapex.key",
5742 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005743 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005744 }
5745
5746 apex_key {
5747 name: "myapex.key",
5748 public_key: "testkey.avbpubkey",
5749 private_key: "testkey.pem",
5750 }
5751
5752 java_sdk_library {
5753 name: "foo",
5754 srcs: ["a.java"],
5755 api_packages: ["foo"],
5756 apex_available: ["myapex"],
5757 sdk_version: "none",
5758 system_modules: "none",
5759 }
5760
5761 java_library {
5762 name: "bar",
5763 srcs: ["a.java"],
5764 libs: ["foo"],
5765 apex_available: ["myapex"],
5766 sdk_version: "none",
5767 system_modules: "none",
5768 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005769
5770 prebuilt_apis {
5771 name: "sdk",
5772 api_dirs: ["100"],
5773 }
Paul Duffin9b879592020-05-26 13:21:35 +01005774 `, withFiles(filesForSdkLibrary))
5775
5776 // java_sdk_library installs both impl jar and permission XML
5777 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5778 "javalib/bar.jar",
5779 "javalib/foo.jar",
5780 "etc/permissions/foo.xml",
5781 })
5782
5783 // The bar library should depend on the implementation jar.
5784 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005785 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005786 t.Errorf("expected %q, found %#q", expected, actual)
5787 }
5788}
5789
5790func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005791 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005792 apex {
5793 name: "myapex",
5794 key: "myapex.key",
5795 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005796 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005797 }
5798
5799 apex_key {
5800 name: "myapex.key",
5801 public_key: "testkey.avbpubkey",
5802 private_key: "testkey.pem",
5803 }
5804
5805 java_sdk_library {
5806 name: "foo",
5807 srcs: ["a.java"],
5808 api_packages: ["foo"],
5809 apex_available: ["myapex"],
5810 sdk_version: "none",
5811 system_modules: "none",
5812 }
5813
5814 java_library {
5815 name: "bar",
5816 srcs: ["a.java"],
5817 libs: ["foo"],
5818 sdk_version: "none",
5819 system_modules: "none",
5820 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005821
5822 prebuilt_apis {
5823 name: "sdk",
5824 api_dirs: ["100"],
5825 }
Paul Duffin9b879592020-05-26 13:21:35 +01005826 `, withFiles(filesForSdkLibrary))
5827
5828 // java_sdk_library installs both impl jar and permission XML
5829 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5830 "javalib/foo.jar",
5831 "etc/permissions/foo.xml",
5832 })
5833
5834 // The bar library should depend on the stubs jar.
5835 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005836 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005837 t.Errorf("expected %q, found %#q", expected, actual)
5838 }
5839}
5840
Paul Duffineedc5d52020-06-12 17:46:39 +01005841func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005842 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00005843 prebuilt_apis {
5844 name: "sdk",
5845 api_dirs: ["100"],
5846 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005847 withFiles(map[string][]byte{
5848 "apex/a.java": nil,
5849 "apex/apex_manifest.json": nil,
5850 "apex/Android.bp": []byte(`
5851 package {
5852 default_visibility: ["//visibility:private"],
5853 }
5854
5855 apex {
5856 name: "myapex",
5857 key: "myapex.key",
5858 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005859 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005860 }
5861
5862 apex_key {
5863 name: "myapex.key",
5864 public_key: "testkey.avbpubkey",
5865 private_key: "testkey.pem",
5866 }
5867
5868 java_library {
5869 name: "bar",
5870 srcs: ["a.java"],
5871 libs: ["foo"],
5872 apex_available: ["myapex"],
5873 sdk_version: "none",
5874 system_modules: "none",
5875 }
5876`),
5877 "source/a.java": nil,
5878 "source/api/current.txt": nil,
5879 "source/api/removed.txt": nil,
5880 "source/Android.bp": []byte(`
5881 package {
5882 default_visibility: ["//visibility:private"],
5883 }
5884
5885 java_sdk_library {
5886 name: "foo",
5887 visibility: ["//apex"],
5888 srcs: ["a.java"],
5889 api_packages: ["foo"],
5890 apex_available: ["myapex"],
5891 sdk_version: "none",
5892 system_modules: "none",
5893 public: {
5894 enabled: true,
5895 },
5896 }
5897`),
5898 "prebuilt/a.jar": nil,
5899 "prebuilt/Android.bp": []byte(`
5900 package {
5901 default_visibility: ["//visibility:private"],
5902 }
5903
5904 java_sdk_library_import {
5905 name: "foo",
5906 visibility: ["//apex", "//source"],
5907 apex_available: ["myapex"],
5908 prefer: true,
5909 public: {
5910 jars: ["a.jar"],
5911 },
5912 }
5913`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005914 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005915 )
5916
5917 // java_sdk_library installs both impl jar and permission XML
5918 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5919 "javalib/bar.jar",
5920 "javalib/foo.jar",
5921 "etc/permissions/foo.xml",
5922 })
5923
5924 // The bar library should depend on the implementation jar.
5925 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005926 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01005927 t.Errorf("expected %q, found %#q", expected, actual)
5928 }
5929}
5930
5931func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5932 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5933 apex {
5934 name: "myapex",
5935 key: "myapex.key",
5936 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005937 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005938 }
5939
5940 apex_key {
5941 name: "myapex.key",
5942 public_key: "testkey.avbpubkey",
5943 private_key: "testkey.pem",
5944 }
5945
5946 java_sdk_library_import {
5947 name: "foo",
5948 apex_available: ["myapex"],
5949 prefer: true,
5950 public: {
5951 jars: ["a.jar"],
5952 },
5953 }
5954
5955 `, withFiles(filesForSdkLibrary))
5956}
5957
atrost6e126252020-01-27 17:01:16 +00005958func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01005959 result := android.GroupFixturePreparers(
5960 prepareForApexTest,
5961 java.PrepareForTestWithPlatformCompatConfig,
5962 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00005963 apex {
5964 name: "myapex",
5965 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00005966 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00005967 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005968 updatable: false,
atrost6e126252020-01-27 17:01:16 +00005969 }
5970
5971 apex_key {
5972 name: "myapex.key",
5973 public_key: "testkey.avbpubkey",
5974 private_key: "testkey.pem",
5975 }
5976
5977 platform_compat_config {
5978 name: "myjar-platform-compat-config",
5979 src: ":myjar",
5980 }
5981
5982 java_library {
5983 name: "myjar",
5984 srcs: ["foo/bar/MyClass.java"],
5985 sdk_version: "none",
5986 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005987 apex_available: [ "myapex" ],
5988 }
Paul Duffin1b29e002021-03-16 15:06:54 +00005989
5990 // Make sure that a preferred prebuilt does not affect the apex contents.
5991 prebuilt_platform_compat_config {
5992 name: "myjar-platform-compat-config",
5993 metadata: "compat-config/metadata.xml",
5994 prefer: true,
5995 }
atrost6e126252020-01-27 17:01:16 +00005996 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00005997 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00005998 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5999 "etc/compatconfig/myjar-platform-compat-config.xml",
6000 "javalib/myjar.jar",
6001 })
6002}
6003
Jiyong Park479321d2019-12-16 11:47:12 +09006004func TestRejectNonInstallableJavaLibrary(t *testing.T) {
6005 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
6006 apex {
6007 name: "myapex",
6008 key: "myapex.key",
6009 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006010 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09006011 }
6012
6013 apex_key {
6014 name: "myapex.key",
6015 public_key: "testkey.avbpubkey",
6016 private_key: "testkey.pem",
6017 }
6018
6019 java_library {
6020 name: "myjar",
6021 srcs: ["foo/bar/MyClass.java"],
6022 sdk_version: "none",
6023 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09006024 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006025 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09006026 }
6027 `)
6028}
6029
Jiyong Park7afd1072019-12-30 16:56:33 +09006030func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006031 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09006032 apex {
6033 name: "myapex",
6034 key: "myapex.key",
6035 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006036 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09006037 }
6038
6039 apex_key {
6040 name: "myapex.key",
6041 public_key: "testkey.avbpubkey",
6042 private_key: "testkey.pem",
6043 }
6044
6045 cc_library {
6046 name: "mylib",
6047 srcs: ["mylib.cpp"],
6048 system_shared_libs: [],
6049 stl: "none",
6050 required: ["a", "b"],
6051 host_required: ["c", "d"],
6052 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006053 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006054 }
6055 `)
6056
6057 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006058 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006059 name := apexBundle.BaseModuleName()
6060 prefix := "TARGET_"
6061 var builder strings.Builder
6062 data.Custom(&builder, name, prefix, "", data)
6063 androidMk := builder.String()
6064 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6065 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6066 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6067}
6068
Jiyong Park7cd10e32020-01-14 09:22:18 +09006069func TestSymlinksFromApexToSystem(t *testing.T) {
6070 bp := `
6071 apex {
6072 name: "myapex",
6073 key: "myapex.key",
6074 native_shared_libs: ["mylib"],
6075 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006076 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006077 }
6078
Jiyong Park9d677202020-02-19 16:29:35 +09006079 apex {
6080 name: "myapex.updatable",
6081 key: "myapex.key",
6082 native_shared_libs: ["mylib"],
6083 java_libs: ["myjar"],
6084 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006085 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006086 }
6087
Jiyong Park7cd10e32020-01-14 09:22:18 +09006088 apex_key {
6089 name: "myapex.key",
6090 public_key: "testkey.avbpubkey",
6091 private_key: "testkey.pem",
6092 }
6093
6094 cc_library {
6095 name: "mylib",
6096 srcs: ["mylib.cpp"],
6097 shared_libs: ["myotherlib"],
6098 system_shared_libs: [],
6099 stl: "none",
6100 apex_available: [
6101 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006102 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006103 "//apex_available:platform",
6104 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006105 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006106 }
6107
6108 cc_library {
6109 name: "myotherlib",
6110 srcs: ["mylib.cpp"],
6111 system_shared_libs: [],
6112 stl: "none",
6113 apex_available: [
6114 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006115 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006116 "//apex_available:platform",
6117 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006118 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006119 }
6120
6121 java_library {
6122 name: "myjar",
6123 srcs: ["foo/bar/MyClass.java"],
6124 sdk_version: "none",
6125 system_modules: "none",
6126 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006127 apex_available: [
6128 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006129 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006130 "//apex_available:platform",
6131 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006132 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006133 }
6134
6135 java_library {
6136 name: "myotherjar",
6137 srcs: ["foo/bar/MyClass.java"],
6138 sdk_version: "none",
6139 system_modules: "none",
6140 apex_available: [
6141 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006142 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006143 "//apex_available:platform",
6144 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006145 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006146 }
6147 `
6148
6149 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6150 for _, f := range files {
6151 if f.path == file {
6152 if f.isLink {
6153 t.Errorf("%q is not a real file", file)
6154 }
6155 return
6156 }
6157 }
6158 t.Errorf("%q is not found", file)
6159 }
6160
6161 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6162 for _, f := range files {
6163 if f.path == file {
6164 if !f.isLink {
6165 t.Errorf("%q is not a symlink", file)
6166 }
6167 return
6168 }
6169 }
6170 t.Errorf("%q is not found", file)
6171 }
6172
Jiyong Park9d677202020-02-19 16:29:35 +09006173 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6174 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006175 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006176 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006177 ensureRealfileExists(t, files, "javalib/myjar.jar")
6178 ensureRealfileExists(t, files, "lib64/mylib.so")
6179 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6180
Jiyong Park9d677202020-02-19 16:29:35 +09006181 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6182 ensureRealfileExists(t, files, "javalib/myjar.jar")
6183 ensureRealfileExists(t, files, "lib64/mylib.so")
6184 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6185
6186 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006187 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006188 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006189 ensureRealfileExists(t, files, "javalib/myjar.jar")
6190 ensureRealfileExists(t, files, "lib64/mylib.so")
6191 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006192
6193 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6194 ensureRealfileExists(t, files, "javalib/myjar.jar")
6195 ensureRealfileExists(t, files, "lib64/mylib.so")
6196 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006197}
6198
Yo Chiange8128052020-07-23 20:09:18 +08006199func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006200 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006201 apex {
6202 name: "myapex",
6203 key: "myapex.key",
6204 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006205 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006206 }
6207
6208 apex_key {
6209 name: "myapex.key",
6210 public_key: "testkey.avbpubkey",
6211 private_key: "testkey.pem",
6212 }
6213
6214 cc_library_shared {
6215 name: "mylib",
6216 srcs: ["mylib.cpp"],
6217 shared_libs: ["myotherlib"],
6218 system_shared_libs: [],
6219 stl: "none",
6220 apex_available: [
6221 "myapex",
6222 "//apex_available:platform",
6223 ],
6224 }
6225
6226 cc_prebuilt_library_shared {
6227 name: "myotherlib",
6228 srcs: ["prebuilt.so"],
6229 system_shared_libs: [],
6230 stl: "none",
6231 apex_available: [
6232 "myapex",
6233 "//apex_available:platform",
6234 ],
6235 }
6236 `)
6237
6238 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006239 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006240 var builder strings.Builder
6241 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6242 androidMk := builder.String()
6243 // `myotherlib` is added to `myapex` as symlink
6244 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6245 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6246 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6247 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006248 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 +08006249}
6250
Jooyung Han643adc42020-02-27 13:50:06 +09006251func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006252 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006253 apex {
6254 name: "myapex",
6255 key: "myapex.key",
6256 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006257 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006258 }
6259
6260 apex_key {
6261 name: "myapex.key",
6262 public_key: "testkey.avbpubkey",
6263 private_key: "testkey.pem",
6264 }
6265
6266 cc_library {
6267 name: "mylib",
6268 srcs: ["mylib.cpp"],
6269 shared_libs: ["mylib2"],
6270 system_shared_libs: [],
6271 stl: "none",
6272 apex_available: [ "myapex" ],
6273 }
6274
6275 cc_library {
6276 name: "mylib2",
6277 srcs: ["mylib.cpp"],
6278 system_shared_libs: [],
6279 stl: "none",
6280 apex_available: [ "myapex" ],
6281 }
6282 `)
6283
6284 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6285 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6286 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6287 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6288 "lib64/mylib.so",
6289 "lib64/mylib2.so",
6290 })
6291}
6292
Jooyung Han49f67012020-04-17 13:43:10 +09006293func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006294 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006295 apex {
6296 name: "myapex",
6297 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006298 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006299 }
6300 apex_key {
6301 name: "myapex.key",
6302 public_key: "testkey.avbpubkey",
6303 private_key: "testkey.pem",
6304 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006305 `,
6306 android.FixtureModifyConfig(func(config android.Config) {
6307 delete(config.Targets, android.Android)
6308 config.AndroidCommonTarget = android.Target{}
6309 }),
6310 )
Jooyung Han49f67012020-04-17 13:43:10 +09006311
6312 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6313 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6314 }
6315}
6316
Jiyong Parkbd159612020-02-28 15:22:21 +09006317func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006318 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006319 apex {
6320 name: "myapex",
6321 key: "myapex.key",
6322 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006323 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006324 }
6325
6326 apex_key {
6327 name: "myapex.key",
6328 public_key: "testkey.avbpubkey",
6329 private_key: "testkey.pem",
6330 }
6331
6332 android_app {
6333 name: "AppFoo",
6334 srcs: ["foo/bar/MyClass.java"],
6335 sdk_version: "none",
6336 system_modules: "none",
6337 apex_available: [ "myapex" ],
6338 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006339 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006340
Colin Crosscf371cc2020-11-13 11:48:42 -08006341 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006342 content := bundleConfigRule.Args["content"]
6343
6344 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006345 ensureContains(t, content, `"apex_config":{"apex_embedded_apk_config":[{"package_name":"com.android.foo","path":"app/AppFoo/AppFoo.apk"}]}`)
Jiyong Parkbd159612020-02-28 15:22:21 +09006346}
6347
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006348func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006349 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006350 apex {
6351 name: "myapex",
6352 key: "myapex.key",
6353 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006354 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006355 }
6356
6357 apex_key {
6358 name: "myapex.key",
6359 public_key: "testkey.avbpubkey",
6360 private_key: "testkey.pem",
6361 }
6362
6363 android_app_set {
6364 name: "AppSet",
6365 set: "AppSet.apks",
6366 }`)
6367 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006368 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006369 content := bundleConfigRule.Args["content"]
6370 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6371 s := mod.Rule("apexRule").Args["copy_commands"]
6372 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6373 if len(copyCmds) != 3 {
6374 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6375 }
6376 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6377 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6378 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6379}
6380
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006381func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01006382 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006383 apex_set {
6384 name: "myapex",
6385 filename: "foo_v2.apex",
6386 sanitized: {
6387 none: { set: "myapex.apks", },
6388 hwaddress: { set: "myapex.hwasan.apks", },
6389 },
Paul Duffin24704672021-04-06 16:09:30 +01006390 }
6391 `
6392 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006393
Paul Duffin24704672021-04-06 16:09:30 +01006394 // Check that the extractor produces the correct output file from the correct input file.
6395 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006396
Paul Duffin24704672021-04-06 16:09:30 +01006397 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
6398 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006399
Paul Duffin24704672021-04-06 16:09:30 +01006400 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
6401
6402 // Ditto for the apex.
Paul Duffin5ec165d2021-06-15 19:09:41 +01006403 m = ctx.ModuleForTests("myapex", "android_common_myapex")
6404 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01006405
6406 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006407}
6408
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006409func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006410 t.Helper()
6411
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006412 bp := `
6413 java_library {
6414 name: "some-updatable-apex-lib",
6415 srcs: ["a.java"],
6416 sdk_version: "current",
6417 apex_available: [
6418 "some-updatable-apex",
6419 ],
6420 }
6421
6422 java_library {
6423 name: "some-non-updatable-apex-lib",
6424 srcs: ["a.java"],
6425 apex_available: [
6426 "some-non-updatable-apex",
6427 ],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006428 compile_dex: true,
6429 }
6430
6431 bootclasspath_fragment {
6432 name: "some-non-updatable-fragment",
6433 contents: ["some-non-updatable-apex-lib"],
6434 apex_available: [
6435 "some-non-updatable-apex",
6436 ],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006437 }
6438
6439 java_library {
6440 name: "some-platform-lib",
6441 srcs: ["a.java"],
6442 sdk_version: "current",
6443 installable: true,
6444 }
6445
6446 java_library {
6447 name: "some-art-lib",
6448 srcs: ["a.java"],
6449 sdk_version: "current",
6450 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006451 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006452 ],
6453 hostdex: true,
Paul Duffinc75bbce2021-06-07 13:28:19 +01006454 compile_dex: true,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006455 }
6456
6457 apex {
6458 name: "some-updatable-apex",
6459 key: "some-updatable-apex.key",
6460 java_libs: ["some-updatable-apex-lib"],
6461 updatable: true,
6462 min_sdk_version: "current",
6463 }
6464
6465 apex {
6466 name: "some-non-updatable-apex",
6467 key: "some-non-updatable-apex.key",
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006468 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006469 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006470 }
6471
6472 apex_key {
6473 name: "some-updatable-apex.key",
6474 }
6475
6476 apex_key {
6477 name: "some-non-updatable-apex.key",
6478 }
6479
6480 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006481 name: "com.android.art.debug",
6482 key: "com.android.art.debug.key",
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006483 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006484 updatable: true,
6485 min_sdk_version: "current",
6486 }
6487
Paul Duffinf23bc472021-04-27 12:42:20 +01006488 bootclasspath_fragment {
6489 name: "art-bootclasspath-fragment",
6490 image_name: "art",
6491 contents: ["some-art-lib"],
6492 apex_available: [
6493 "com.android.art.debug",
6494 ],
6495 }
6496
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006497 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006498 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006499 }
6500
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006501 filegroup {
6502 name: "some-updatable-apex-file_contexts",
6503 srcs: [
6504 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6505 ],
6506 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006507
6508 filegroup {
6509 name: "some-non-updatable-apex-file_contexts",
6510 srcs: [
6511 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6512 ],
6513 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006514 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006515
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006516 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00006517}
6518
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006519func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006520 t.Helper()
6521
Paul Duffin55607122021-03-30 23:32:51 +01006522 fs := android.MockFS{
6523 "a.java": nil,
6524 "a.jar": nil,
6525 "apex_manifest.json": nil,
6526 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006527 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006528 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6529 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6530 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006531 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006532 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006533
Paul Duffin55607122021-03-30 23:32:51 +01006534 errorHandler := android.FixtureExpectsNoErrors
6535 if errmsg != "" {
6536 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006537 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006538
Paul Duffin55607122021-03-30 23:32:51 +01006539 result := android.GroupFixturePreparers(
6540 cc.PrepareForTestWithCcDefaultModules,
6541 java.PrepareForTestWithHiddenApiBuildComponents,
6542 java.PrepareForTestWithJavaDefaultModules,
6543 java.PrepareForTestWithJavaSdkLibraryFiles,
6544 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01006545 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01006546 fs.AddToFixture(),
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006547 android.FixtureModifyMockFS(func(fs android.MockFS) {
6548 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
6549 insert := ""
6550 for _, fragment := range fragments {
6551 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
6552 }
6553 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
6554 platform_bootclasspath {
6555 name: "platform-bootclasspath",
6556 fragments: [
6557 %s
6558 ],
6559 }
6560 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01006561 }
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006562 }),
Paul Duffin55607122021-03-30 23:32:51 +01006563 ).
6564 ExtendWithErrorHandler(errorHandler).
6565 RunTestWithBp(t, bp)
6566
6567 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006568}
6569
Martin Stjernholm95994062021-06-30 16:35:07 +01006570func TestDuplicateDeapexeresFromPrebuiltApexes(t *testing.T) {
6571 preparers := android.GroupFixturePreparers(
6572 java.PrepareForTestWithJavaDefaultModules,
6573 PrepareForTestWithApexBuildComponents,
6574 ).
6575 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
6576 `Ambiguous duplicate deapexer module dependencies "com.android.myapex.deapexer" and "com.mycompany.android.myapex.deapexer"`))
6577
6578 bpBase := `
6579 apex_set {
6580 name: "com.android.myapex",
6581 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
6582 set: "myapex.apks",
6583 }
6584
6585 apex_set {
6586 name: "com.mycompany.android.myapex",
6587 apex_name: "com.android.myapex",
6588 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
6589 set: "company-myapex.apks",
6590 }
6591
6592 prebuilt_bootclasspath_fragment {
6593 name: "my-bootclasspath-fragment",
6594 apex_available: ["com.android.myapex"],
6595 %s
6596 }
6597 `
6598
6599 t.Run("java_import", func(t *testing.T) {
6600 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
6601 java_import {
6602 name: "libfoo",
6603 jars: ["libfoo.jar"],
6604 apex_available: ["com.android.myapex"],
6605 }
6606 `)
6607 })
6608
6609 t.Run("java_sdk_library_import", func(t *testing.T) {
6610 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
6611 java_sdk_library_import {
6612 name: "libfoo",
6613 public: {
6614 jars: ["libbar.jar"],
6615 },
6616 apex_available: ["com.android.myapex"],
6617 }
6618 `)
6619 })
6620
6621 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
6622 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
6623 image_name: "art",
6624 contents: ["libfoo"],
6625 `)+`
6626 java_sdk_library_import {
6627 name: "libfoo",
6628 public: {
6629 jars: ["libbar.jar"],
6630 },
6631 apex_available: ["com.android.myapex"],
6632 }
6633 `)
6634 })
6635}
6636
Jooyung Han548640b2020-04-27 12:10:30 +09006637func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6638 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6639 apex {
6640 name: "myapex",
6641 key: "myapex.key",
6642 updatable: true,
6643 }
6644
6645 apex_key {
6646 name: "myapex.key",
6647 public_key: "testkey.avbpubkey",
6648 private_key: "testkey.pem",
6649 }
6650 `)
6651}
6652
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006653func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6654 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6655 apex {
6656 name: "myapex",
6657 key: "myapex.key",
6658 }
6659
6660 apex_key {
6661 name: "myapex.key",
6662 public_key: "testkey.avbpubkey",
6663 private_key: "testkey.pem",
6664 }
6665 `)
6666}
6667
satayevffe79712021-06-15 16:49:50 +01006668func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
6669 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
6670 apex {
6671 name: "myapex",
6672 key: "myapex.key",
6673 systemserverclasspath_fragments: [
6674 "mysystemserverclasspathfragment",
6675 ],
6676 min_sdk_version: "29",
6677 updatable: true,
6678 }
6679
6680 apex_key {
6681 name: "myapex.key",
6682 public_key: "testkey.avbpubkey",
6683 private_key: "testkey.pem",
6684 }
6685
6686 java_library {
6687 name: "foo",
6688 srcs: ["b.java"],
6689 min_sdk_version: "29",
6690 installable: true,
6691 apex_available: [
6692 "myapex",
6693 ],
6694 }
6695
6696 systemserverclasspath_fragment {
6697 name: "mysystemserverclasspathfragment",
6698 generate_classpaths_proto: false,
6699 contents: [
6700 "foo",
6701 ],
6702 apex_available: [
6703 "myapex",
6704 ],
6705 }
6706 `)
6707}
6708
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006709func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006710 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
6711 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
6712 // modules to be included in the BootJars.
6713 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
6714 return android.GroupFixturePreparers(
6715 dexpreopt.FixtureSetBootJars(bootJars...),
6716 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6717 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
6718 }),
6719 )
6720 }
6721
6722 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
6723 // same value. This can result in an invalid configuration as it allows non art apex jars to be
6724 // specified in the ArtApexJars configuration.
6725 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
6726 return android.GroupFixturePreparers(
6727 dexpreopt.FixtureSetArtBootJars(bootJars...),
6728 dexpreopt.FixtureSetBootJars(bootJars...),
6729 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6730 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
6731 }),
6732 )
6733 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006734
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006735 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006736 preparer := java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib")
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006737 fragment := java.ApexVariantReference{
6738 Apex: proptools.StringPtr("com.android.art.debug"),
6739 Module: proptools.StringPtr("art-bootclasspath-fragment"),
6740 }
6741 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006742 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006743
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006744 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006745 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
6746 // Update the dexpreopt BootJars directly.
6747 preparer := prepareSetBootJars("com.android.art.debug:some-art-lib")
6748 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006749 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006750
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006751 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 +01006752 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 +01006753 // Update the dexpreopt ArtApexJars directly.
6754 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
6755 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006756 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006757
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006758 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 +01006759 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 +01006760 // Update the dexpreopt ArtApexJars directly.
6761 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
6762 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006763 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006764
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006765 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 +01006766 err := `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the framework boot image`
6767 preparer := java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib")
6768 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006769 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006770
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006771 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006772 preparer := java.FixtureConfigureBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006773 fragment := java.ApexVariantReference{
6774 Apex: proptools.StringPtr("some-non-updatable-apex"),
6775 Module: proptools.StringPtr("some-non-updatable-fragment"),
6776 }
6777 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006778 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006779
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006780 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01006781 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01006782 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
6783 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006784 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006785
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006786 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01006787 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01006788 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
6789 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006790 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006791
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006792 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01006793 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01006794 // Update the dexpreopt ArtApexJars directly.
6795 preparer := prepareSetArtJars("platform:some-platform-lib")
6796 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006797 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006798
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006799 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006800 preparer := java.FixtureConfigureBootJars("platform:some-platform-lib")
6801 testNoUpdatableJarsInBootImage(t, "", preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006802 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006803}
6804
6805func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006806 preparer := java.FixtureConfigureBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00006807 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006808 fragment := java.ApexVariantReference{
6809 Apex: proptools.StringPtr("myapex"),
6810 Module: proptools.StringPtr("my-bootclasspath-fragment"),
6811 }
6812
Paul Duffin064b70c2020-11-02 17:32:38 +00006813 testDexpreoptWithApexes(t, `
6814 prebuilt_apex {
6815 name: "myapex" ,
6816 arch: {
6817 arm64: {
6818 src: "myapex-arm64.apex",
6819 },
6820 arm: {
6821 src: "myapex-arm.apex",
6822 },
6823 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006824 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
6825 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006826
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006827 prebuilt_bootclasspath_fragment {
6828 name: "my-bootclasspath-fragment",
6829 contents: ["libfoo"],
6830 apex_available: ["myapex"],
6831 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006832
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006833 java_import {
6834 name: "libfoo",
6835 jars: ["libfoo.jar"],
6836 apex_available: ["myapex"],
6837 }
6838 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00006839 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006840}
6841
satayev7a552ba2021-07-21 14:23:52 +01006842func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01006843 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006844 bp += `
6845 apex_key {
6846 name: "myapex.key",
6847 public_key: "testkey.avbpubkey",
6848 private_key: "testkey.pem",
6849 }`
Paul Duffin45338f02021-03-30 23:07:52 +01006850 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01006851 "lib1/src/A.java": nil,
6852 "lib2/src/B.java": nil,
6853 "system/sepolicy/apex/myapex-file_contexts": nil,
6854 }
6855
Paul Duffin45338f02021-03-30 23:07:52 +01006856 errorHandler := android.FixtureExpectsNoErrors
6857 if errmsg != "" {
6858 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07006859 }
Colin Crossae8600b2020-10-29 17:09:13 -07006860
Paul Duffin45338f02021-03-30 23:07:52 +01006861 android.GroupFixturePreparers(
6862 android.PrepareForTestWithAndroidBuildComponents,
6863 java.PrepareForTestWithJavaBuildComponents,
6864 PrepareForTestWithApexBuildComponents,
6865 android.PrepareForTestWithNeverallowRules(rules),
6866 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayev7a552ba2021-07-21 14:23:52 +01006867 apexBootJars := make([]string, 0, len(bootJars))
6868 for _, apexBootJar := range bootJars {
6869 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01006870 }
satayev7a552ba2021-07-21 14:23:52 +01006871 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01006872 }),
6873 fs.AddToFixture(),
6874 ).
6875 ExtendWithErrorHandler(errorHandler).
6876 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01006877}
6878
6879func TestApexPermittedPackagesRules(t *testing.T) {
6880 testcases := []struct {
6881 name string
6882 expectedError string
6883 bp string
6884 bootJars []string
6885 modulesPackages map[string][]string
6886 }{
6887
6888 {
6889 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6890 expectedError: "",
6891 bp: `
6892 java_library {
6893 name: "bcp_lib1",
6894 srcs: ["lib1/src/*.java"],
6895 permitted_packages: ["foo.bar"],
6896 apex_available: ["myapex"],
6897 sdk_version: "none",
6898 system_modules: "none",
Remi NGUYEN VANefb49af2021-12-10 00:17:57 +00006899 min_sdk_version: "30",
Andrei Onea115e7e72020-06-05 21:14:03 +01006900 }
6901 java_library {
6902 name: "nonbcp_lib2",
6903 srcs: ["lib2/src/*.java"],
6904 apex_available: ["myapex"],
6905 permitted_packages: ["a.b"],
6906 sdk_version: "none",
6907 system_modules: "none",
Remi NGUYEN VANefb49af2021-12-10 00:17:57 +00006908 min_sdk_version: "30",
Andrei Onea115e7e72020-06-05 21:14:03 +01006909 }
6910 apex {
6911 name: "myapex",
Remi NGUYEN VANefb49af2021-12-10 00:17:57 +00006912 min_sdk_version: "30",
Andrei Onea115e7e72020-06-05 21:14:03 +01006913 key: "myapex.key",
6914 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006915 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006916 }`,
6917 bootJars: []string{"bcp_lib1"},
6918 modulesPackages: map[string][]string{
6919 "myapex": []string{
6920 "foo.bar",
6921 },
6922 },
6923 },
6924 {
Remi NGUYEN VANefb49af2021-12-10 00:17:57 +00006925 name: "Bootclasspath apex jar not satisfying allowed module packages on Q.",
6926 expectedError: `module "bcp_lib2" .* which is restricted because jars that are part of the myapex module may only allow these packages: foo.bar with min_sdk < T. Please jarjar or move code around.`,
Andrei Onea115e7e72020-06-05 21:14:03 +01006927 bp: `
6928 java_library {
6929 name: "bcp_lib1",
6930 srcs: ["lib1/src/*.java"],
6931 apex_available: ["myapex"],
6932 permitted_packages: ["foo.bar"],
6933 sdk_version: "none",
6934 system_modules: "none",
Remi NGUYEN VANefb49af2021-12-10 00:17:57 +00006935 min_sdk_version: "29",
Andrei Onea115e7e72020-06-05 21:14:03 +01006936 }
6937 java_library {
6938 name: "bcp_lib2",
6939 srcs: ["lib2/src/*.java"],
6940 apex_available: ["myapex"],
6941 permitted_packages: ["foo.bar", "bar.baz"],
6942 sdk_version: "none",
6943 system_modules: "none",
Remi NGUYEN VANefb49af2021-12-10 00:17:57 +00006944 min_sdk_version: "29",
Andrei Onea115e7e72020-06-05 21:14:03 +01006945 }
6946 apex {
6947 name: "myapex",
Remi NGUYEN VANefb49af2021-12-10 00:17:57 +00006948 min_sdk_version: "29",
6949 key: "myapex.key",
6950 java_libs: ["bcp_lib1", "bcp_lib2"],
6951 updatable: false,
6952 }
6953 `,
6954 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6955 modulesPackages: map[string][]string{
6956 "myapex": []string{
6957 "foo.bar",
6958 },
6959 },
6960 },
6961 {
6962 name: "Bootclasspath apex jar not satisfying allowed module packages on R.",
6963 expectedError: `module "bcp_lib2" .* which is restricted because jars that are part of the myapex module may only allow these packages: foo.bar with min_sdk < T. Please jarjar or move code around.`,
6964 bp: `
6965 java_library {
6966 name: "bcp_lib1",
6967 srcs: ["lib1/src/*.java"],
6968 apex_available: ["myapex"],
6969 permitted_packages: ["foo.bar"],
6970 sdk_version: "none",
6971 system_modules: "none",
6972 min_sdk_version: "30",
6973 }
6974 java_library {
6975 name: "bcp_lib2",
6976 srcs: ["lib2/src/*.java"],
6977 apex_available: ["myapex"],
6978 permitted_packages: ["foo.bar", "bar.baz"],
6979 sdk_version: "none",
6980 system_modules: "none",
6981 min_sdk_version: "30",
6982 }
6983 apex {
6984 name: "myapex",
6985 min_sdk_version: "30",
6986 key: "myapex.key",
6987 java_libs: ["bcp_lib1", "bcp_lib2"],
6988 updatable: false,
6989 }
6990 `,
6991 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6992 modulesPackages: map[string][]string{
6993 "myapex": []string{
6994 "foo.bar",
6995 },
6996 },
6997 },
6998 {
6999 name: "Bootclasspath apex jar >= T not satisfying Q/R/S allowed module packages.",
7000 expectedError: "",
7001 bp: `
7002 java_library {
7003 name: "bcp_lib1",
7004 srcs: ["lib1/src/*.java"],
7005 apex_available: ["myapex"],
7006 permitted_packages: ["foo.bar"],
7007 sdk_version: "none",
7008 system_modules: "none",
7009 min_sdk_version: "current",
7010 }
7011 java_library {
7012 name: "bcp_lib2",
7013 srcs: ["lib2/src/*.java"],
7014 apex_available: ["myapex"],
7015 permitted_packages: ["foo.bar", "bar.baz"],
7016 sdk_version: "none",
7017 system_modules: "none",
7018 min_sdk_version: "current",
7019 }
7020 apex {
7021 name: "myapex",
7022 min_sdk_version: "current",
Andrei Onea115e7e72020-06-05 21:14:03 +01007023 key: "myapex.key",
7024 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007025 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01007026 }
7027 `,
7028 bootJars: []string{"bcp_lib1", "bcp_lib2"},
7029 modulesPackages: map[string][]string{
7030 "myapex": []string{
7031 "foo.bar",
7032 },
7033 },
7034 },
7035 }
7036 for _, tc := range testcases {
7037 t.Run(tc.name, func(t *testing.T) {
7038 rules := createApexPermittedPackagesRules(tc.modulesPackages)
7039 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
7040 })
7041 }
7042}
7043
Jiyong Park62304bb2020-04-13 16:19:48 +09007044func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007045 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09007046 apex {
7047 name: "myapex",
7048 key: "myapex.key",
7049 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007050 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09007051 }
7052
7053 apex_key {
7054 name: "myapex.key",
7055 public_key: "testkey.avbpubkey",
7056 private_key: "testkey.pem",
7057 }
7058
7059 cc_library {
7060 name: "mylib",
7061 srcs: ["mylib.cpp"],
7062 system_shared_libs: [],
7063 stl: "none",
7064 stubs: {
7065 versions: ["1"],
7066 },
7067 apex_available: ["myapex"],
7068 }
7069
7070 cc_library {
7071 name: "myprivlib",
7072 srcs: ["mylib.cpp"],
7073 system_shared_libs: [],
7074 stl: "none",
7075 apex_available: ["myapex"],
7076 }
7077
7078
7079 cc_test {
7080 name: "mytest",
7081 gtest: false,
7082 srcs: ["mylib.cpp"],
7083 system_shared_libs: [],
7084 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09007085 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09007086 test_for: ["myapex"]
7087 }
Jiyong Park46a512f2020-12-04 18:02:13 +09007088
7089 cc_library {
7090 name: "mytestlib",
7091 srcs: ["mylib.cpp"],
7092 system_shared_libs: [],
7093 shared_libs: ["mylib", "myprivlib"],
7094 stl: "none",
7095 test_for: ["myapex"],
7096 }
7097
7098 cc_benchmark {
7099 name: "mybench",
7100 srcs: ["mylib.cpp"],
7101 system_shared_libs: [],
7102 shared_libs: ["mylib", "myprivlib"],
7103 stl: "none",
7104 test_for: ["myapex"],
7105 }
Jiyong Park62304bb2020-04-13 16:19:48 +09007106 `)
7107
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007108 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01007109 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007110 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
7111 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
7112 }
7113
7114 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09007115 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007116 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7117 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7118 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7119}
Jiyong Park46a512f2020-12-04 18:02:13 +09007120
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007121func TestIndirectTestFor(t *testing.T) {
7122 ctx := testApex(t, `
7123 apex {
7124 name: "myapex",
7125 key: "myapex.key",
7126 native_shared_libs: ["mylib", "myprivlib"],
7127 updatable: false,
7128 }
Jiyong Park46a512f2020-12-04 18:02:13 +09007129
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007130 apex_key {
7131 name: "myapex.key",
7132 public_key: "testkey.avbpubkey",
7133 private_key: "testkey.pem",
7134 }
7135
7136 cc_library {
7137 name: "mylib",
7138 srcs: ["mylib.cpp"],
7139 system_shared_libs: [],
7140 stl: "none",
7141 stubs: {
7142 versions: ["1"],
7143 },
7144 apex_available: ["myapex"],
7145 }
7146
7147 cc_library {
7148 name: "myprivlib",
7149 srcs: ["mylib.cpp"],
7150 system_shared_libs: [],
7151 stl: "none",
7152 shared_libs: ["mylib"],
7153 apex_available: ["myapex"],
7154 }
7155
7156 cc_library {
7157 name: "mytestlib",
7158 srcs: ["mylib.cpp"],
7159 system_shared_libs: [],
7160 shared_libs: ["myprivlib"],
7161 stl: "none",
7162 test_for: ["myapex"],
7163 }
7164 `)
7165
7166 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01007167 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007168 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
7169 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
7170 }
7171
7172 // The platform variant of mytestlib links to the platform variant of the
7173 // internal myprivlib.
7174 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
7175
7176 // The platform variant of myprivlib links to the platform variant of mylib
7177 // and bypasses its stubs.
7178 ensureLinkedLibIs("myprivlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
Jiyong Park62304bb2020-04-13 16:19:48 +09007179}
7180
Martin Stjernholmec009002021-03-27 15:18:31 +00007181func TestTestForForLibInOtherApex(t *testing.T) {
7182 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
7183 _ = testApex(t, `
7184 apex {
7185 name: "com.android.art",
7186 key: "myapex.key",
7187 native_shared_libs: ["mylib"],
7188 updatable: false,
7189 }
7190
7191 apex {
7192 name: "com.android.art.debug",
7193 key: "myapex.key",
7194 native_shared_libs: ["mylib", "mytestlib"],
7195 updatable: false,
7196 }
7197
7198 apex_key {
7199 name: "myapex.key",
7200 public_key: "testkey.avbpubkey",
7201 private_key: "testkey.pem",
7202 }
7203
7204 cc_library {
7205 name: "mylib",
7206 srcs: ["mylib.cpp"],
7207 system_shared_libs: [],
7208 stl: "none",
7209 stubs: {
7210 versions: ["1"],
7211 },
7212 apex_available: ["com.android.art", "com.android.art.debug"],
7213 }
7214
7215 cc_library {
7216 name: "mytestlib",
7217 srcs: ["mylib.cpp"],
7218 system_shared_libs: [],
7219 shared_libs: ["mylib"],
7220 stl: "none",
7221 apex_available: ["com.android.art.debug"],
7222 test_for: ["com.android.art"],
7223 }
7224 `,
7225 android.MockFS{
7226 "system/sepolicy/apex/com.android.art-file_contexts": nil,
7227 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
7228 }.AddToFixture())
7229}
7230
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007231// TODO(jungjw): Move this to proptools
7232func intPtr(i int) *int {
7233 return &i
7234}
7235
7236func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007237 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007238 apex_set {
7239 name: "myapex",
7240 set: "myapex.apks",
7241 filename: "foo_v2.apex",
7242 overrides: ["foo"],
7243 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007244 `,
7245 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7246 variables.Platform_sdk_version = intPtr(30)
7247 }),
7248 android.FixtureModifyConfig(func(config android.Config) {
7249 config.Targets[android.Android] = []android.Target{
7250 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
7251 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
7252 }
7253 }),
7254 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007255
Paul Duffin24704672021-04-06 16:09:30 +01007256 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007257
7258 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01007259 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007260 actual := extractedApex.Args["abis"]
7261 expected := "ARMEABI_V7A,ARM64_V8A"
7262 if actual != expected {
7263 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7264 }
7265 actual = extractedApex.Args["sdk-version"]
7266 expected = "30"
7267 if actual != expected {
7268 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7269 }
7270
Paul Duffin5ec165d2021-06-15 19:09:41 +01007271 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007272 a := m.Module().(*ApexSet)
7273 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07007274 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007275 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
7276 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
7277 }
7278}
7279
Jiyong Park7d95a512020-05-10 15:16:24 +09007280func TestNoStaticLinkingToStubsLib(t *testing.T) {
7281 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
7282 apex {
7283 name: "myapex",
7284 key: "myapex.key",
7285 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007286 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09007287 }
7288
7289 apex_key {
7290 name: "myapex.key",
7291 public_key: "testkey.avbpubkey",
7292 private_key: "testkey.pem",
7293 }
7294
7295 cc_library {
7296 name: "mylib",
7297 srcs: ["mylib.cpp"],
7298 static_libs: ["otherlib"],
7299 system_shared_libs: [],
7300 stl: "none",
7301 apex_available: [ "myapex" ],
7302 }
7303
7304 cc_library {
7305 name: "otherlib",
7306 srcs: ["mylib.cpp"],
7307 system_shared_libs: [],
7308 stl: "none",
7309 stubs: {
7310 versions: ["1", "2", "3"],
7311 },
7312 apex_available: [ "myapex" ],
7313 }
7314 `)
7315}
7316
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007317func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007318 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007319 apex {
7320 name: "myapex",
7321 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007322 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007323 }
7324
7325 apex_key {
7326 name: "myapex.key",
7327 public_key: "testkey.avbpubkey",
7328 private_key: "testkey.pem",
7329 }
7330
7331 prebuilt_apex {
7332 name: "myapex",
7333 prefer: true,
7334 arch: {
7335 arm64: {
7336 src: "myapex-arm64.apex",
7337 },
7338 arm: {
7339 src: "myapex-arm.apex",
7340 },
7341 },
7342 }
7343
7344 apex_set {
7345 name: "myapex_set",
7346 set: "myapex.apks",
7347 filename: "myapex_set.apex",
7348 overrides: ["myapex"],
7349 }
7350 `)
7351
7352 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7353 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7354 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 +09007355 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 +09007356}
7357
Jooyung Han938b5932020-06-20 12:47:47 +09007358func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007359 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007360 apex {
7361 name: "myapex",
7362 key: "myapex.key",
7363 apps: ["app"],
7364 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007365 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007366 }
7367
7368 apex_key {
7369 name: "myapex.key",
7370 public_key: "testkey.avbpubkey",
7371 private_key: "testkey.pem",
7372 }
7373
7374 android_app {
7375 name: "app",
7376 srcs: ["foo/bar/MyClass.java"],
7377 package_name: "foo",
7378 sdk_version: "none",
7379 system_modules: "none",
7380 apex_available: [ "myapex" ],
7381 }
7382 `, withFiles(map[string][]byte{
7383 "sub/Android.bp": []byte(`
7384 override_apex {
7385 name: "override_myapex",
7386 base: "myapex",
7387 apps: ["override_app"],
7388 allowed_files: ":allowed",
7389 }
7390 // Overridable "path" property should be referenced indirectly
7391 filegroup {
7392 name: "allowed",
7393 srcs: ["allowed.txt"],
7394 }
7395 override_android_app {
7396 name: "override_app",
7397 base: "app",
7398 package_name: "bar",
7399 }
7400 `),
7401 }))
7402
7403 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7404 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7405 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7406 }
7407
7408 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7409 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7410 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7411 }
7412}
7413
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007414func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007415 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007416 apex {
7417 name: "myapex",
7418 key: "myapex.key",
7419 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007420 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007421 }
7422
7423 apex_key {
7424 name: "myapex.key",
7425 public_key: "testkey.avbpubkey",
7426 private_key: "testkey.pem",
7427 }
7428
7429 cc_library {
7430 name: "mylib",
7431 srcs: ["mylib.cpp"],
7432 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007433 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007434 },
7435 apex_available: ["myapex"],
7436 }
7437
7438 cc_prebuilt_library_shared {
7439 name: "mylib",
7440 prefer: false,
7441 srcs: ["prebuilt.so"],
7442 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007443 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007444 },
7445 apex_available: ["myapex"],
7446 }
7447 `)
7448}
7449
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007450func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007451 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007452 apex {
7453 name: "myapex",
7454 key: "myapex.key",
7455 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007456 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007457 }
7458 apex_key {
7459 name: "myapex.key",
7460 public_key: "testkey.avbpubkey",
7461 private_key: "testkey.pem",
7462 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007463 `,
7464 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7465 variables.CompressedApex = proptools.BoolPtr(true)
7466 }),
7467 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007468
7469 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7470 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7471
7472 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7473 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7474
7475 // Make sure output of bundle is .capex
7476 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7477 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7478
7479 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007480 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007481 var builder strings.Builder
7482 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7483 androidMk := builder.String()
7484 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7485}
7486
Martin Stjernholm2856c662020-12-02 15:03:42 +00007487func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007488 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007489 apex {
7490 name: "myapex",
7491 key: "myapex.key",
7492 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007493 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007494 }
7495
7496 apex_key {
7497 name: "myapex.key",
7498 public_key: "testkey.avbpubkey",
7499 private_key: "testkey.pem",
7500 }
7501
7502 cc_library {
7503 name: "mylib",
7504 srcs: ["mylib.cpp"],
7505 apex_available: ["myapex"],
7506 shared_libs: ["otherlib"],
7507 system_shared_libs: [],
7508 }
7509
7510 cc_library {
7511 name: "otherlib",
7512 srcs: ["mylib.cpp"],
7513 stubs: {
7514 versions: ["current"],
7515 },
7516 }
7517
7518 cc_prebuilt_library_shared {
7519 name: "otherlib",
7520 prefer: true,
7521 srcs: ["prebuilt.so"],
7522 stubs: {
7523 versions: ["current"],
7524 },
7525 }
7526 `)
7527
7528 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007529 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007530 var builder strings.Builder
7531 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7532 androidMk := builder.String()
7533
7534 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7535 // a thing there.
7536 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7537}
7538
Jiyong Parke3867542020-12-03 17:28:25 +09007539func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007540 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007541 apex {
7542 name: "myapex",
7543 key: "myapex.key",
7544 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007545 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007546 }
7547
7548 apex_key {
7549 name: "myapex.key",
7550 public_key: "testkey.avbpubkey",
7551 private_key: "testkey.pem",
7552 }
7553
7554 cc_library {
7555 name: "mylib",
7556 srcs: ["mylib.cpp"],
7557 system_shared_libs: [],
7558 stl: "none",
7559 apex_available: ["myapex"],
7560 shared_libs: ["mylib2"],
7561 target: {
7562 apex: {
7563 exclude_shared_libs: ["mylib2"],
7564 },
7565 },
7566 }
7567
7568 cc_library {
7569 name: "mylib2",
7570 srcs: ["mylib.cpp"],
7571 system_shared_libs: [],
7572 stl: "none",
7573 }
7574 `)
7575
7576 // Check if mylib is linked to mylib2 for the non-apex target
7577 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7578 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7579
7580 // Make sure that the link doesn't occur for the apex target
7581 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7582 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7583
7584 // It shouldn't appear in the copy cmd as well.
7585 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7586 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7587}
7588
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007589func TestPrebuiltStubLibDep(t *testing.T) {
7590 bpBase := `
7591 apex {
7592 name: "myapex",
7593 key: "myapex.key",
7594 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007595 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007596 }
7597 apex_key {
7598 name: "myapex.key",
7599 public_key: "testkey.avbpubkey",
7600 private_key: "testkey.pem",
7601 }
7602 cc_library {
7603 name: "mylib",
7604 srcs: ["mylib.cpp"],
7605 apex_available: ["myapex"],
7606 shared_libs: ["stublib"],
7607 system_shared_libs: [],
7608 }
7609 apex {
7610 name: "otherapex",
7611 enabled: %s,
7612 key: "myapex.key",
7613 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007614 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007615 }
7616 `
7617
7618 stublibSourceBp := `
7619 cc_library {
7620 name: "stublib",
7621 srcs: ["mylib.cpp"],
7622 apex_available: ["otherapex"],
7623 system_shared_libs: [],
7624 stl: "none",
7625 stubs: {
7626 versions: ["1"],
7627 },
7628 }
7629 `
7630
7631 stublibPrebuiltBp := `
7632 cc_prebuilt_library_shared {
7633 name: "stublib",
7634 srcs: ["prebuilt.so"],
7635 apex_available: ["otherapex"],
7636 stubs: {
7637 versions: ["1"],
7638 },
7639 %s
7640 }
7641 `
7642
7643 tests := []struct {
7644 name string
7645 stublibBp string
7646 usePrebuilt bool
7647 modNames []string // Modules to collect AndroidMkEntries for
7648 otherApexEnabled []string
7649 }{
7650 {
7651 name: "only_source",
7652 stublibBp: stublibSourceBp,
7653 usePrebuilt: false,
7654 modNames: []string{"stublib"},
7655 otherApexEnabled: []string{"true", "false"},
7656 },
7657 {
7658 name: "source_preferred",
7659 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7660 usePrebuilt: false,
7661 modNames: []string{"stublib", "prebuilt_stublib"},
7662 otherApexEnabled: []string{"true", "false"},
7663 },
7664 {
7665 name: "prebuilt_preferred",
7666 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7667 usePrebuilt: true,
7668 modNames: []string{"stublib", "prebuilt_stublib"},
7669 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7670 },
7671 {
7672 name: "only_prebuilt",
7673 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7674 usePrebuilt: true,
7675 modNames: []string{"stublib"},
7676 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7677 },
7678 }
7679
7680 for _, test := range tests {
7681 t.Run(test.name, func(t *testing.T) {
7682 for _, otherApexEnabled := range test.otherApexEnabled {
7683 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007684 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007685
7686 type modAndMkEntries struct {
7687 mod *cc.Module
7688 mkEntries android.AndroidMkEntries
7689 }
7690 entries := []*modAndMkEntries{}
7691
7692 // Gather shared lib modules that are installable
7693 for _, modName := range test.modNames {
7694 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7695 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7696 continue
7697 }
7698 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007699 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007700 continue
7701 }
Colin Crossaa255532020-07-03 13:18:24 -07007702 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007703 if ent.Disabled {
7704 continue
7705 }
7706 entries = append(entries, &modAndMkEntries{
7707 mod: mod,
7708 mkEntries: ent,
7709 })
7710 }
7711 }
7712 }
7713
7714 var entry *modAndMkEntries = nil
7715 for _, ent := range entries {
7716 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7717 if entry != nil {
7718 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7719 } else {
7720 entry = ent
7721 }
7722 }
7723 }
7724
7725 if entry == nil {
7726 t.Errorf("AndroidMk entry for \"stublib\" missing")
7727 } else {
7728 isPrebuilt := entry.mod.Prebuilt() != nil
7729 if isPrebuilt != test.usePrebuilt {
7730 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7731 }
7732 if !entry.mod.IsStubs() {
7733 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7734 }
7735 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7736 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7737 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007738 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09007739 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09007740 if !android.InList(expected, cflags) {
7741 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7742 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007743 }
7744 })
7745 }
7746 })
7747 }
7748}
7749
Colin Crosscbb2b812021-05-25 18:16:02 -07007750func TestApexJavaCoverage(t *testing.T) {
7751 bp := `
7752 apex {
7753 name: "myapex",
7754 key: "myapex.key",
7755 java_libs: ["mylib"],
7756 bootclasspath_fragments: ["mybootclasspathfragment"],
7757 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
7758 updatable: false,
7759 }
7760
7761 apex_key {
7762 name: "myapex.key",
7763 public_key: "testkey.avbpubkey",
7764 private_key: "testkey.pem",
7765 }
7766
7767 java_library {
7768 name: "mylib",
7769 srcs: ["mylib.java"],
7770 apex_available: ["myapex"],
7771 compile_dex: true,
7772 }
7773
7774 bootclasspath_fragment {
7775 name: "mybootclasspathfragment",
7776 contents: ["mybootclasspathlib"],
7777 apex_available: ["myapex"],
7778 }
7779
7780 java_library {
7781 name: "mybootclasspathlib",
7782 srcs: ["mybootclasspathlib.java"],
7783 apex_available: ["myapex"],
7784 compile_dex: true,
7785 }
7786
7787 systemserverclasspath_fragment {
7788 name: "mysystemserverclasspathfragment",
7789 contents: ["mysystemserverclasspathlib"],
7790 apex_available: ["myapex"],
7791 }
7792
7793 java_library {
7794 name: "mysystemserverclasspathlib",
7795 srcs: ["mysystemserverclasspathlib.java"],
7796 apex_available: ["myapex"],
7797 compile_dex: true,
7798 }
7799 `
7800
7801 result := android.GroupFixturePreparers(
7802 PrepareForTestWithApexBuildComponents,
7803 prepareForTestWithMyapex,
7804 java.PrepareForTestWithJavaDefaultModules,
7805 android.PrepareForTestWithAndroidBuildComponents,
7806 android.FixtureWithRootAndroidBp(bp),
7807 android.FixtureMergeEnv(map[string]string{
7808 "EMMA_INSTRUMENT": "true",
7809 }),
7810 ).RunTest(t)
7811
7812 // Make sure jacoco ran on both mylib and mybootclasspathlib
7813 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
7814 t.Errorf("Failed to find jacoco rule for mylib")
7815 }
7816 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
7817 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
7818 }
7819 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
7820 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
7821 }
7822}
7823
satayev812683e2021-12-06 11:42:40 +00007824func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
7825 preparer := android.GroupFixturePreparers(
7826 PrepareForTestWithApexBuildComponents,
7827 prepareForTestWithMyapex,
7828 java.PrepareForTestWithJavaSdkLibraryFiles,
7829 java.PrepareForTestWithJavaDefaultModules,
7830 android.PrepareForTestWithAndroidBuildComponents,
7831 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
7832 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
7833 )
7834
7835 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
7836 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
7837 preparer.RunTestWithBp(t, `
7838 apex {
7839 name: "myapex",
7840 key: "myapex.key",
7841 bootclasspath_fragments: ["mybootclasspathfragment"],
7842 min_sdk_version: "30",
7843 updatable: false,
7844 }
7845
7846 apex_key {
7847 name: "myapex.key",
7848 public_key: "testkey.avbpubkey",
7849 private_key: "testkey.pem",
7850 }
7851
7852 bootclasspath_fragment {
7853 name: "mybootclasspathfragment",
7854 contents: ["mybootclasspathlib"],
7855 apex_available: ["myapex"],
7856 }
7857
7858 java_sdk_library {
7859 name: "mybootclasspathlib",
7860 srcs: ["mybootclasspathlib.java"],
7861 apex_available: ["myapex"],
7862 compile_dex: true,
7863 unsafe_ignore_missing_latest_api: true,
7864 min_sdk_version: "31",
7865 static_libs: ["util"],
7866 }
7867
7868 java_library {
7869 name: "util",
7870 srcs: ["a.java"],
7871 apex_available: ["myapex"],
7872 min_sdk_version: "31",
7873 static_libs: ["another_util"],
7874 }
7875
7876 java_library {
7877 name: "another_util",
7878 srcs: ["a.java"],
7879 min_sdk_version: "31",
7880 apex_available: ["myapex"],
7881 }
7882 `)
7883 })
7884
7885 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
7886 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
7887 preparer.RunTestWithBp(t, `
7888 apex {
7889 name: "myapex",
7890 key: "myapex.key",
7891 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
7892 min_sdk_version: "30",
7893 updatable: false,
7894 }
7895
7896 apex_key {
7897 name: "myapex.key",
7898 public_key: "testkey.avbpubkey",
7899 private_key: "testkey.pem",
7900 }
7901
7902 systemserverclasspath_fragment {
7903 name: "mysystemserverclasspathfragment",
7904 contents: ["mysystemserverclasspathlib"],
7905 apex_available: ["myapex"],
7906 }
7907
7908 java_sdk_library {
7909 name: "mysystemserverclasspathlib",
7910 srcs: ["mysystemserverclasspathlib.java"],
7911 apex_available: ["myapex"],
7912 compile_dex: true,
7913 min_sdk_version: "32",
7914 unsafe_ignore_missing_latest_api: true,
7915 static_libs: ["util"],
7916 }
7917
7918 java_library {
7919 name: "util",
7920 srcs: ["a.java"],
7921 apex_available: ["myapex"],
7922 min_sdk_version: "31",
7923 static_libs: ["another_util"],
7924 }
7925
7926 java_library {
7927 name: "another_util",
7928 srcs: ["a.java"],
7929 min_sdk_version: "31",
7930 apex_available: ["myapex"],
7931 }
7932 `)
7933 })
7934
7935 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
7936 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
7937 RunTestWithBp(t, `
7938 apex {
7939 name: "myapex",
7940 key: "myapex.key",
7941 bootclasspath_fragments: ["mybootclasspathfragment"],
7942 min_sdk_version: "30",
7943 updatable: false,
7944 }
7945
7946 apex_key {
7947 name: "myapex.key",
7948 public_key: "testkey.avbpubkey",
7949 private_key: "testkey.pem",
7950 }
7951
7952 bootclasspath_fragment {
7953 name: "mybootclasspathfragment",
7954 contents: ["mybootclasspathlib"],
7955 apex_available: ["myapex"],
7956 }
7957
7958 java_sdk_library {
7959 name: "mybootclasspathlib",
7960 srcs: ["mybootclasspathlib.java"],
7961 apex_available: ["myapex"],
7962 compile_dex: true,
7963 unsafe_ignore_missing_latest_api: true,
7964 }
7965 `)
7966 })
7967
7968 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
7969 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
7970 RunTestWithBp(t, `
7971 apex {
7972 name: "myapex",
7973 key: "myapex.key",
7974 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
7975 min_sdk_version: "30",
7976 updatable: false,
7977 }
7978
7979 apex_key {
7980 name: "myapex.key",
7981 public_key: "testkey.avbpubkey",
7982 private_key: "testkey.pem",
7983 }
7984
7985 systemserverclasspath_fragment {
7986 name: "mysystemserverclasspathfragment",
7987 contents: ["mysystemserverclasspathlib"],
7988 apex_available: ["myapex"],
7989 }
7990
7991 java_sdk_library {
7992 name: "mysystemserverclasspathlib",
7993 srcs: ["mysystemserverclasspathlib.java"],
7994 apex_available: ["myapex"],
7995 compile_dex: true,
7996 unsafe_ignore_missing_latest_api: true,
7997 }
7998 `)
7999 })
8000}
8001
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07008002func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01008003 os.Exit(m.Run())
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07008004}