blob: ecc585ee45bfd139b84731f003ec280459d268f8 [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 Park7c2ee712018-12-07 00:42:25 +09002296 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2297
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002298 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002299 ensureListContains(t, dirs, "etc")
2300 ensureListContains(t, dirs, "etc/foo")
2301 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002302 ensureListContains(t, dirs, "lib64")
2303 ensureListContains(t, dirs, "lib64/foo")
2304 ensureListContains(t, dirs, "lib64/foo/bar")
2305 ensureListContains(t, dirs, "lib")
2306 ensureListContains(t, dirs, "lib/foo")
2307 ensureListContains(t, dirs, "lib/foo/bar")
2308
Jiyong Parkbd13e442019-03-15 18:10:35 +09002309 ensureListContains(t, dirs, "bin")
2310 ensureListContains(t, dirs, "bin/foo")
2311 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002312}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002313
Jooyung Han35155c42020-02-06 17:33:20 +09002314func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002315 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002316 apex {
2317 name: "myapex",
2318 key: "myapex.key",
2319 multilib: {
2320 both: {
2321 native_shared_libs: ["mylib"],
2322 binaries: ["mybin"],
2323 },
2324 },
2325 compile_multilib: "both",
2326 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002327 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002328 }
2329
2330 apex_key {
2331 name: "myapex.key",
2332 public_key: "testkey.avbpubkey",
2333 private_key: "testkey.pem",
2334 }
2335
2336 cc_library {
2337 name: "mylib",
2338 relative_install_path: "foo/bar",
2339 system_shared_libs: [],
2340 stl: "none",
2341 apex_available: [ "myapex" ],
2342 native_bridge_supported: true,
2343 }
2344
2345 cc_binary {
2346 name: "mybin",
2347 relative_install_path: "foo/bar",
2348 system_shared_libs: [],
2349 static_executable: true,
2350 stl: "none",
2351 apex_available: [ "myapex" ],
2352 native_bridge_supported: true,
2353 compile_multilib: "both", // default is "first" for binary
2354 multilib: {
2355 lib64: {
2356 suffix: "64",
2357 },
2358 },
2359 }
2360 `, withNativeBridgeEnabled)
2361 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2362 "bin/foo/bar/mybin",
2363 "bin/foo/bar/mybin64",
2364 "bin/arm/foo/bar/mybin",
2365 "bin/arm64/foo/bar/mybin64",
2366 "lib/foo/bar/mylib.so",
2367 "lib/arm/foo/bar/mylib.so",
2368 "lib64/foo/bar/mylib.so",
2369 "lib64/arm64/foo/bar/mylib.so",
2370 })
2371}
2372
Jooyung Han85d61762020-06-24 23:50:26 +09002373func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002374 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002375 apex {
2376 name: "myapex",
2377 key: "myapex.key",
2378 binaries: ["mybin"],
2379 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002380 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002381 }
2382 apex_key {
2383 name: "myapex.key",
2384 public_key: "testkey.avbpubkey",
2385 private_key: "testkey.pem",
2386 }
2387 cc_binary {
2388 name: "mybin",
2389 vendor: true,
2390 shared_libs: ["libfoo"],
2391 }
2392 cc_library {
2393 name: "libfoo",
2394 proprietary: true,
2395 }
2396 `)
2397
2398 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2399 "bin/mybin",
2400 "lib64/libfoo.so",
2401 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2402 "lib64/libc++.so",
2403 })
2404
2405 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002406 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002407 name := apexBundle.BaseModuleName()
2408 prefix := "TARGET_"
2409 var builder strings.Builder
2410 data.Custom(&builder, name, prefix, "", data)
Paul Duffin37ba3442021-03-29 00:21:08 +01002411 androidMk := android.StringRelativeToTop(ctx.Config(), builder.String())
2412 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002413 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002414
2415 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2416 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2417 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002418}
2419
Jooyung Handf78e212020-07-22 15:54:47 +09002420func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002421 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002422 apex {
2423 name: "myapex",
2424 key: "myapex.key",
2425 binaries: ["mybin"],
2426 vendor: true,
2427 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002428 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002429 }
2430 apex_key {
2431 name: "myapex.key",
2432 public_key: "testkey.avbpubkey",
2433 private_key: "testkey.pem",
2434 }
2435 cc_binary {
2436 name: "mybin",
2437 vendor: true,
2438 shared_libs: ["libvndk", "libvendor"],
2439 }
2440 cc_library {
2441 name: "libvndk",
2442 vndk: {
2443 enabled: true,
2444 },
2445 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002446 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002447 }
2448 cc_library {
2449 name: "libvendor",
2450 vendor: true,
2451 }
2452 `)
2453
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002454 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002455
Paul Duffina71a67a2021-03-29 00:42:57 +01002456 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002457 libs := names(ldRule.Args["libFlags"])
2458 // VNDK libs(libvndk/libc++) as they are
Paul Duffin37ba3442021-03-29 00:21:08 +01002459 ensureListContains(t, libs, "out/soong/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
2460 ensureListContains(t, libs, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002461 // non-stable Vendor libs as APEX variants
Paul Duffin37ba3442021-03-29 00:21:08 +01002462 ensureListContains(t, libs, "out/soong/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002463
2464 // VNDK libs are not included when use_vndk_as_stable: true
2465 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2466 "bin/mybin",
2467 "lib64/libvendor.so",
2468 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002469
2470 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2471 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2472 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002473}
2474
Justin Yun13decfb2021-03-08 19:25:55 +09002475func TestProductVariant(t *testing.T) {
2476 ctx := testApex(t, `
2477 apex {
2478 name: "myapex",
2479 key: "myapex.key",
2480 updatable: false,
2481 product_specific: true,
2482 binaries: ["foo"],
2483 }
2484
2485 apex_key {
2486 name: "myapex.key",
2487 public_key: "testkey.avbpubkey",
2488 private_key: "testkey.pem",
2489 }
2490
2491 cc_binary {
2492 name: "foo",
2493 product_available: true,
2494 apex_available: ["myapex"],
2495 srcs: ["foo.cpp"],
2496 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002497 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2498 variables.ProductVndkVersion = proptools.StringPtr("current")
2499 }),
2500 )
Justin Yun13decfb2021-03-08 19:25:55 +09002501
2502 cflags := strings.Fields(
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002503 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002504 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2505 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2506 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2507 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2508}
2509
Jooyung Han8e5685d2020-09-21 11:02:57 +09002510func TestApex_withPrebuiltFirmware(t *testing.T) {
2511 testCases := []struct {
2512 name string
2513 additionalProp string
2514 }{
2515 {"system apex with prebuilt_firmware", ""},
2516 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2517 }
2518 for _, tc := range testCases {
2519 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002520 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002521 apex {
2522 name: "myapex",
2523 key: "myapex.key",
2524 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002525 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002526 `+tc.additionalProp+`
2527 }
2528 apex_key {
2529 name: "myapex.key",
2530 public_key: "testkey.avbpubkey",
2531 private_key: "testkey.pem",
2532 }
2533 prebuilt_firmware {
2534 name: "myfirmware",
2535 src: "myfirmware.bin",
2536 filename_from_src: true,
2537 `+tc.additionalProp+`
2538 }
2539 `)
2540 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2541 "etc/firmware/myfirmware.bin",
2542 })
2543 })
2544 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002545}
2546
Jooyung Hanefb184e2020-06-25 17:14:25 +09002547func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002548 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002549 apex {
2550 name: "myapex",
2551 key: "myapex.key",
2552 vendor: true,
2553 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002554 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002555 }
2556
2557 apex_key {
2558 name: "myapex.key",
2559 public_key: "testkey.avbpubkey",
2560 private_key: "testkey.pem",
2561 }
2562
2563 cc_library {
2564 name: "mylib",
2565 vendor_available: true,
2566 }
2567 `)
2568
2569 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002570 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002571 name := apexBundle.BaseModuleName()
2572 prefix := "TARGET_"
2573 var builder strings.Builder
2574 data.Custom(&builder, name, prefix, "", data)
2575 androidMk := builder.String()
2576 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2577}
2578
Jooyung Han2ed99d02020-06-24 23:26:26 +09002579func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002580 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002581 apex {
2582 name: "myapex",
2583 key: "myapex.key",
2584 vintf_fragments: ["fragment.xml"],
2585 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002586 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002587 }
2588 apex_key {
2589 name: "myapex.key",
2590 public_key: "testkey.avbpubkey",
2591 private_key: "testkey.pem",
2592 }
2593 cc_binary {
2594 name: "mybin",
2595 }
2596 `)
2597
2598 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002599 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002600 name := apexBundle.BaseModuleName()
2601 prefix := "TARGET_"
2602 var builder strings.Builder
2603 data.Custom(&builder, name, prefix, "", data)
2604 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04002605 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04002606 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09002607}
2608
Jiyong Park16e91a02018-12-20 18:18:08 +09002609func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002610 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002611 apex {
2612 name: "myapex",
2613 key: "myapex.key",
2614 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002615 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002616 }
2617
2618 apex_key {
2619 name: "myapex.key",
2620 public_key: "testkey.avbpubkey",
2621 private_key: "testkey.pem",
2622 }
2623
2624 cc_library {
2625 name: "mylib",
2626 srcs: ["mylib.cpp"],
2627 system_shared_libs: [],
2628 stl: "none",
2629 stubs: {
2630 versions: ["1", "2", "3"],
2631 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002632 apex_available: [
2633 "//apex_available:platform",
2634 "myapex",
2635 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002636 }
2637
2638 cc_binary {
2639 name: "not_in_apex",
2640 srcs: ["mylib.cpp"],
2641 static_libs: ["mylib"],
2642 static_executable: true,
2643 system_shared_libs: [],
2644 stl: "none",
2645 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002646 `)
2647
Colin Cross7113d202019-11-20 16:39:12 -08002648 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002649
2650 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002651 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002652}
Jiyong Park9335a262018-12-24 11:31:58 +09002653
2654func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002655 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002656 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002657 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002658 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002659 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002660 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002661 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002662 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09002663 }
2664
2665 cc_library {
2666 name: "mylib",
2667 srcs: ["mylib.cpp"],
2668 system_shared_libs: [],
2669 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002670 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002671 }
2672
2673 apex_key {
2674 name: "myapex.key",
2675 public_key: "testkey.avbpubkey",
2676 private_key: "testkey.pem",
2677 }
2678
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002679 android_app_certificate {
2680 name: "myapex.certificate",
2681 certificate: "testkey",
2682 }
2683
2684 android_app_certificate {
2685 name: "myapex.certificate.override",
2686 certificate: "testkey.override",
2687 }
2688
Jiyong Park9335a262018-12-24 11:31:58 +09002689 `)
2690
2691 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002692 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002693
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002694 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2695 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002696 "vendor/foo/devkeys/testkey.avbpubkey")
2697 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002698 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2699 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002700 "vendor/foo/devkeys/testkey.pem")
2701 }
2702
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002703 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002704 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002705 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002706 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002707 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002708 }
2709}
Jiyong Park58e364a2019-01-19 19:24:06 +09002710
Jooyung Hanf121a652019-12-17 14:30:11 +09002711func TestCertificate(t *testing.T) {
2712 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002713 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002714 apex {
2715 name: "myapex",
2716 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002717 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002718 }
2719 apex_key {
2720 name: "myapex.key",
2721 public_key: "testkey.avbpubkey",
2722 private_key: "testkey.pem",
2723 }`)
2724 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2725 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2726 if actual := rule.Args["certificates"]; actual != expected {
2727 t.Errorf("certificates should be %q, not %q", expected, actual)
2728 }
2729 })
2730 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002731 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002732 apex {
2733 name: "myapex_keytest",
2734 key: "myapex.key",
2735 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002736 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002737 }
2738 apex_key {
2739 name: "myapex.key",
2740 public_key: "testkey.avbpubkey",
2741 private_key: "testkey.pem",
2742 }
2743 android_app_certificate {
2744 name: "myapex.certificate.override",
2745 certificate: "testkey.override",
2746 }`)
2747 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2748 expected := "testkey.override.x509.pem testkey.override.pk8"
2749 if actual := rule.Args["certificates"]; actual != expected {
2750 t.Errorf("certificates should be %q, not %q", expected, actual)
2751 }
2752 })
2753 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002754 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002755 apex {
2756 name: "myapex",
2757 key: "myapex.key",
2758 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002759 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002760 }
2761 apex_key {
2762 name: "myapex.key",
2763 public_key: "testkey.avbpubkey",
2764 private_key: "testkey.pem",
2765 }
2766 android_app_certificate {
2767 name: "myapex.certificate",
2768 certificate: "testkey",
2769 }`)
2770 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2771 expected := "testkey.x509.pem testkey.pk8"
2772 if actual := rule.Args["certificates"]; actual != expected {
2773 t.Errorf("certificates should be %q, not %q", expected, actual)
2774 }
2775 })
2776 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002777 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002778 apex {
2779 name: "myapex_keytest",
2780 key: "myapex.key",
2781 file_contexts: ":myapex-file_contexts",
2782 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002783 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002784 }
2785 apex_key {
2786 name: "myapex.key",
2787 public_key: "testkey.avbpubkey",
2788 private_key: "testkey.pem",
2789 }
2790 android_app_certificate {
2791 name: "myapex.certificate.override",
2792 certificate: "testkey.override",
2793 }`)
2794 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2795 expected := "testkey.override.x509.pem testkey.override.pk8"
2796 if actual := rule.Args["certificates"]; actual != expected {
2797 t.Errorf("certificates should be %q, not %q", expected, actual)
2798 }
2799 })
2800 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002801 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002802 apex {
2803 name: "myapex",
2804 key: "myapex.key",
2805 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002806 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002807 }
2808 apex_key {
2809 name: "myapex.key",
2810 public_key: "testkey.avbpubkey",
2811 private_key: "testkey.pem",
2812 }`)
2813 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2814 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2815 if actual := rule.Args["certificates"]; actual != expected {
2816 t.Errorf("certificates should be %q, not %q", expected, actual)
2817 }
2818 })
2819 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002820 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002821 apex {
2822 name: "myapex_keytest",
2823 key: "myapex.key",
2824 file_contexts: ":myapex-file_contexts",
2825 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002826 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002827 }
2828 apex_key {
2829 name: "myapex.key",
2830 public_key: "testkey.avbpubkey",
2831 private_key: "testkey.pem",
2832 }
2833 android_app_certificate {
2834 name: "myapex.certificate.override",
2835 certificate: "testkey.override",
2836 }`)
2837 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2838 expected := "testkey.override.x509.pem testkey.override.pk8"
2839 if actual := rule.Args["certificates"]; actual != expected {
2840 t.Errorf("certificates should be %q, not %q", expected, actual)
2841 }
2842 })
2843}
2844
Jiyong Park58e364a2019-01-19 19:24:06 +09002845func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002846 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002847 apex {
2848 name: "myapex",
2849 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002850 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002851 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09002852 }
2853
2854 apex {
2855 name: "otherapex",
2856 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002857 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002858 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002859 }
2860
2861 apex_key {
2862 name: "myapex.key",
2863 public_key: "testkey.avbpubkey",
2864 private_key: "testkey.pem",
2865 }
2866
2867 cc_library {
2868 name: "mylib",
2869 srcs: ["mylib.cpp"],
2870 system_shared_libs: [],
2871 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002872 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002873 "myapex",
2874 "otherapex",
2875 ],
Jooyung Han24282772020-03-21 23:20:55 +09002876 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002877 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002878 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09002879 cc_library {
2880 name: "mylib2",
2881 srcs: ["mylib.cpp"],
2882 system_shared_libs: [],
2883 stl: "none",
2884 apex_available: [
2885 "myapex",
2886 "otherapex",
2887 ],
Colin Crossaede88c2020-08-11 12:17:01 -07002888 static_libs: ["mylib3"],
2889 recovery_available: true,
2890 min_sdk_version: "29",
2891 }
2892 cc_library {
2893 name: "mylib3",
2894 srcs: ["mylib.cpp"],
2895 system_shared_libs: [],
2896 stl: "none",
2897 apex_available: [
2898 "myapex",
2899 "otherapex",
2900 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09002901 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07002902 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002903 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002904 }
Jiyong Park58e364a2019-01-19 19:24:06 +09002905 `)
2906
Jooyung Hanc87a0592020-03-02 17:44:33 +09002907 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08002908 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002909 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002910 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002911
Jooyung Hanccce2f22020-03-07 03:45:53 +09002912 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07002913 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09002914 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002915 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09002916 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002917
Jooyung Hanccce2f22020-03-07 03:45:53 +09002918 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07002919 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09002920 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002921 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09002922 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002923
Colin Crossaede88c2020-08-11 12:17:01 -07002924 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
2925 // each variant defines additional macros to distinguish which apex variant it is built for
2926
2927 // non-APEX variant does not have __ANDROID_APEX__ defined
2928 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2929 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2930
2931 // APEX variant has __ANDROID_APEX__ defined
2932 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
2933 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2934 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2935 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
2936
2937 // APEX variant has __ANDROID_APEX__ defined
2938 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
2939 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2940 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2941 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
2942
Dan Albertb19953d2020-11-17 15:29:36 -08002943 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07002944 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2945 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002946 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07002947
2948 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
2949 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09002950
2951 // non-APEX variant does not have __ANDROID_APEX__ defined
2952 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2953 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2954
2955 // APEX variant has __ANDROID_APEX__ defined
2956 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002957 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07002958 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002959 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002960
Jooyung Hanc87a0592020-03-02 17:44:33 +09002961 // APEX variant has __ANDROID_APEX__ defined
2962 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002963 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002964 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07002965 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09002966
Dan Albertb19953d2020-11-17 15:29:36 -08002967 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07002968 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09002969 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002970 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002971}
Jiyong Park7e636d02019-01-28 16:16:54 +09002972
2973func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002974 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09002975 apex {
2976 name: "myapex",
2977 key: "myapex.key",
2978 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002979 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09002980 }
2981
2982 apex_key {
2983 name: "myapex.key",
2984 public_key: "testkey.avbpubkey",
2985 private_key: "testkey.pem",
2986 }
2987
2988 cc_library_headers {
2989 name: "mylib_headers",
2990 export_include_dirs: ["my_include"],
2991 system_shared_libs: [],
2992 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09002993 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09002994 }
2995
2996 cc_library {
2997 name: "mylib",
2998 srcs: ["mylib.cpp"],
2999 system_shared_libs: [],
3000 stl: "none",
3001 header_libs: ["mylib_headers"],
3002 export_header_lib_headers: ["mylib_headers"],
3003 stubs: {
3004 versions: ["1", "2", "3"],
3005 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003006 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003007 }
3008
3009 cc_library {
3010 name: "otherlib",
3011 srcs: ["mylib.cpp"],
3012 system_shared_libs: [],
3013 stl: "none",
3014 shared_libs: ["mylib"],
3015 }
3016 `)
3017
Colin Cross7113d202019-11-20 16:39:12 -08003018 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003019
3020 // Ensure that the include path of the header lib is exported to 'otherlib'
3021 ensureContains(t, cFlags, "-Imy_include")
3022}
Alex Light9670d332019-01-29 18:07:33 -08003023
Jiyong Park7cd10e32020-01-14 09:22:18 +09003024type fileInApex struct {
3025 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003026 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003027 isLink bool
3028}
3029
Jooyung Hana57af4a2020-01-23 05:36:59 +00003030func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003031 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003032 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003033 copyCmds := apexRule.Args["copy_commands"]
3034 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003035 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003036 for _, cmd := range strings.Split(copyCmds, "&&") {
3037 cmd = strings.TrimSpace(cmd)
3038 if cmd == "" {
3039 continue
3040 }
3041 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003042 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003043 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003044 switch terms[0] {
3045 case "mkdir":
3046 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003047 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003048 t.Fatal("copyCmds contains invalid cp command", cmd)
3049 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003050 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003051 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003052 isLink = false
3053 case "ln":
3054 if len(terms) != 3 && len(terms) != 4 {
3055 // ln LINK TARGET or ln -s LINK TARGET
3056 t.Fatal("copyCmds contains invalid ln command", cmd)
3057 }
3058 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003059 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003060 isLink = true
3061 default:
3062 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3063 }
3064 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003065 index := strings.Index(dst, imageApexDir)
3066 if index == -1 {
3067 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3068 }
3069 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003070 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003071 }
3072 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003073 return ret
3074}
3075
Jooyung Hana57af4a2020-01-23 05:36:59 +00003076func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3077 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003078 var failed bool
3079 var surplus []string
3080 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003081 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003082 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003083 for _, expected := range files {
3084 if matched, _ := path.Match(expected, file.path); matched {
3085 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003086 mactchFound = true
3087 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003088 }
3089 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003090 if !mactchFound {
3091 surplus = append(surplus, file.path)
3092 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003093 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003094
Jooyung Han31c470b2019-10-18 16:26:59 +09003095 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003096 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003097 t.Log("surplus files", surplus)
3098 failed = true
3099 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003100
3101 if len(files) > len(filesMatched) {
3102 var missing []string
3103 for _, expected := range files {
3104 if !filesMatched[expected] {
3105 missing = append(missing, expected)
3106 }
3107 }
3108 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003109 t.Log("missing files", missing)
3110 failed = true
3111 }
3112 if failed {
3113 t.Fail()
3114 }
3115}
3116
Jooyung Han344d5432019-08-23 11:17:39 +09003117func TestVndkApexCurrent(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003118 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003119 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003120 name: "com.android.vndk.current",
3121 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003122 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003123 }
3124
3125 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003126 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003127 public_key: "testkey.avbpubkey",
3128 private_key: "testkey.pem",
3129 }
3130
3131 cc_library {
3132 name: "libvndk",
3133 srcs: ["mylib.cpp"],
3134 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003135 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003136 vndk: {
3137 enabled: true,
3138 },
3139 system_shared_libs: [],
3140 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003141 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003142 }
3143
3144 cc_library {
3145 name: "libvndksp",
3146 srcs: ["mylib.cpp"],
3147 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003148 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003149 vndk: {
3150 enabled: true,
3151 support_system_process: true,
3152 },
3153 system_shared_libs: [],
3154 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003155 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003156 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003157 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003158
Colin Cross2807f002021-03-02 10:15:29 -08003159 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003160 "lib/libvndk.so",
3161 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003162 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003163 "lib64/libvndk.so",
3164 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003165 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003166 "etc/llndk.libraries.29.txt",
3167 "etc/vndkcore.libraries.29.txt",
3168 "etc/vndksp.libraries.29.txt",
3169 "etc/vndkprivate.libraries.29.txt",
3170 "etc/vndkproduct.libraries.29.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003171 })
Jooyung Han344d5432019-08-23 11:17:39 +09003172}
3173
3174func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003175 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003176 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003177 name: "com.android.vndk.current",
3178 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003179 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003180 }
3181
3182 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003183 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003184 public_key: "testkey.avbpubkey",
3185 private_key: "testkey.pem",
3186 }
3187
3188 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003189 name: "libvndk",
3190 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003191 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003192 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003193 vndk: {
3194 enabled: true,
3195 },
3196 system_shared_libs: [],
3197 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003198 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003199 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003200
3201 cc_prebuilt_library_shared {
3202 name: "libvndk.arm",
3203 srcs: ["libvndk.arm.so"],
3204 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003205 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003206 vndk: {
3207 enabled: true,
3208 },
3209 enabled: false,
3210 arch: {
3211 arm: {
3212 enabled: true,
3213 },
3214 },
3215 system_shared_libs: [],
3216 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003217 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003218 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003219 `+vndkLibrariesTxtFiles("current"),
3220 withFiles(map[string][]byte{
3221 "libvndk.so": nil,
3222 "libvndk.arm.so": nil,
3223 }))
Colin Cross2807f002021-03-02 10:15:29 -08003224 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003225 "lib/libvndk.so",
3226 "lib/libvndk.arm.so",
3227 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003228 "lib/libc++.so",
3229 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003230 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003231 })
Jooyung Han344d5432019-08-23 11:17:39 +09003232}
3233
Jooyung Han39edb6c2019-11-06 16:53:07 +09003234func vndkLibrariesTxtFiles(vers ...string) (result string) {
3235 for _, v := range vers {
3236 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003237 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003238 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003239 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003240 name: "` + txt + `.libraries.txt",
3241 }
3242 `
3243 }
3244 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003245 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003246 result += `
3247 prebuilt_etc {
3248 name: "` + txt + `.libraries.` + v + `.txt",
3249 src: "dummy.txt",
3250 }
3251 `
3252 }
3253 }
3254 }
3255 return
3256}
3257
Jooyung Han344d5432019-08-23 11:17:39 +09003258func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003259 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003260 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003261 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003262 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003263 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003264 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003265 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003266 }
3267
3268 apex_key {
3269 name: "myapex.key",
3270 public_key: "testkey.avbpubkey",
3271 private_key: "testkey.pem",
3272 }
3273
Jooyung Han31c470b2019-10-18 16:26:59 +09003274 vndk_prebuilt_shared {
3275 name: "libvndk27",
3276 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003277 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003278 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003279 vndk: {
3280 enabled: true,
3281 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003282 target_arch: "arm64",
3283 arch: {
3284 arm: {
3285 srcs: ["libvndk27_arm.so"],
3286 },
3287 arm64: {
3288 srcs: ["libvndk27_arm64.so"],
3289 },
3290 },
Colin Cross2807f002021-03-02 10:15:29 -08003291 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003292 }
3293
3294 vndk_prebuilt_shared {
3295 name: "libvndk27",
3296 version: "27",
3297 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003298 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003299 vndk: {
3300 enabled: true,
3301 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003302 target_arch: "x86_64",
3303 arch: {
3304 x86: {
3305 srcs: ["libvndk27_x86.so"],
3306 },
3307 x86_64: {
3308 srcs: ["libvndk27_x86_64.so"],
3309 },
3310 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003311 }
3312 `+vndkLibrariesTxtFiles("27"),
3313 withFiles(map[string][]byte{
3314 "libvndk27_arm.so": nil,
3315 "libvndk27_arm64.so": nil,
3316 "libvndk27_x86.so": nil,
3317 "libvndk27_x86_64.so": nil,
3318 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003319
Colin Cross2807f002021-03-02 10:15:29 -08003320 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003321 "lib/libvndk27_arm.so",
3322 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003323 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003324 })
Jooyung Han344d5432019-08-23 11:17:39 +09003325}
3326
Jooyung Han90eee022019-10-01 20:02:42 +09003327func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003328 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003329 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003330 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003331 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003332 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003333 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003334 }
3335 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003336 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003337 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003338 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003339 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003340 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003341 }
3342 apex_key {
3343 name: "myapex.key",
3344 public_key: "testkey.avbpubkey",
3345 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003346 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003347
3348 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003349 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003350 actual := proptools.String(bundle.properties.Apex_name)
3351 if !reflect.DeepEqual(actual, expected) {
3352 t.Errorf("Got '%v', expected '%v'", actual, expected)
3353 }
3354 }
3355
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003356 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003357 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003358}
3359
Jooyung Han344d5432019-08-23 11:17:39 +09003360func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003361 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003362 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003363 name: "com.android.vndk.current",
3364 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003365 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003366 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003367 }
3368
3369 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003370 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003371 public_key: "testkey.avbpubkey",
3372 private_key: "testkey.pem",
3373 }
3374
3375 cc_library {
3376 name: "libvndk",
3377 srcs: ["mylib.cpp"],
3378 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003379 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003380 native_bridge_supported: true,
3381 host_supported: true,
3382 vndk: {
3383 enabled: true,
3384 },
3385 system_shared_libs: [],
3386 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003387 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003388 }
Colin Cross2807f002021-03-02 10:15:29 -08003389 `+vndkLibrariesTxtFiles("current"),
3390 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003391
Colin Cross2807f002021-03-02 10:15:29 -08003392 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003393 "lib/libvndk.so",
3394 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003395 "lib/libc++.so",
3396 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003397 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003398 })
Jooyung Han344d5432019-08-23 11:17:39 +09003399}
3400
3401func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003402 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003403 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003404 name: "com.android.vndk.current",
3405 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003406 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003407 native_bridge_supported: true,
3408 }
3409
3410 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003411 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003412 public_key: "testkey.avbpubkey",
3413 private_key: "testkey.pem",
3414 }
3415
3416 cc_library {
3417 name: "libvndk",
3418 srcs: ["mylib.cpp"],
3419 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003420 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003421 native_bridge_supported: true,
3422 host_supported: true,
3423 vndk: {
3424 enabled: true,
3425 },
3426 system_shared_libs: [],
3427 stl: "none",
3428 }
3429 `)
3430}
3431
Jooyung Han31c470b2019-10-18 16:26:59 +09003432func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003433 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003434 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003435 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003436 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003437 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003438 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003439 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003440 }
3441
3442 apex_key {
3443 name: "myapex.key",
3444 public_key: "testkey.avbpubkey",
3445 private_key: "testkey.pem",
3446 }
3447
3448 vndk_prebuilt_shared {
3449 name: "libvndk27",
3450 version: "27",
3451 target_arch: "arm",
3452 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003453 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003454 vndk: {
3455 enabled: true,
3456 },
3457 arch: {
3458 arm: {
3459 srcs: ["libvndk27.so"],
3460 }
3461 },
3462 }
3463
3464 vndk_prebuilt_shared {
3465 name: "libvndk27",
3466 version: "27",
3467 target_arch: "arm",
3468 binder32bit: true,
3469 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003470 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003471 vndk: {
3472 enabled: true,
3473 },
3474 arch: {
3475 arm: {
3476 srcs: ["libvndk27binder32.so"],
3477 }
3478 },
Colin Cross2807f002021-03-02 10:15:29 -08003479 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003480 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003481 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003482 withFiles(map[string][]byte{
3483 "libvndk27.so": nil,
3484 "libvndk27binder32.so": nil,
3485 }),
3486 withBinder32bit,
3487 withTargets(map[android.OsType][]android.Target{
3488 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003489 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3490 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003491 },
3492 }),
3493 )
3494
Colin Cross2807f002021-03-02 10:15:29 -08003495 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003496 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003497 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003498 })
3499}
3500
Jooyung Han45a96772020-06-15 14:59:42 +09003501func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003502 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003503 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003504 name: "com.android.vndk.current",
3505 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003506 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003507 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003508 }
3509
3510 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003511 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003512 public_key: "testkey.avbpubkey",
3513 private_key: "testkey.pem",
3514 }
3515
3516 cc_library {
3517 name: "libz",
3518 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003519 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003520 vndk: {
3521 enabled: true,
3522 },
3523 stubs: {
3524 symbol_file: "libz.map.txt",
3525 versions: ["30"],
3526 }
3527 }
3528 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3529 "libz.map.txt": nil,
3530 }))
3531
Colin Cross2807f002021-03-02 10:15:29 -08003532 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003533 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3534 ensureListEmpty(t, provideNativeLibs)
3535}
3536
Jooyung Hane1633032019-08-01 17:41:43 +09003537func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003538 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003539 apex {
3540 name: "myapex_nodep",
3541 key: "myapex.key",
3542 native_shared_libs: ["lib_nodep"],
3543 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003544 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003545 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003546 }
3547
3548 apex {
3549 name: "myapex_dep",
3550 key: "myapex.key",
3551 native_shared_libs: ["lib_dep"],
3552 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003553 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003554 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003555 }
3556
3557 apex {
3558 name: "myapex_provider",
3559 key: "myapex.key",
3560 native_shared_libs: ["libfoo"],
3561 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003562 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003563 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003564 }
3565
3566 apex {
3567 name: "myapex_selfcontained",
3568 key: "myapex.key",
3569 native_shared_libs: ["lib_dep", "libfoo"],
3570 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003571 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003572 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003573 }
3574
3575 apex_key {
3576 name: "myapex.key",
3577 public_key: "testkey.avbpubkey",
3578 private_key: "testkey.pem",
3579 }
3580
3581 cc_library {
3582 name: "lib_nodep",
3583 srcs: ["mylib.cpp"],
3584 system_shared_libs: [],
3585 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003586 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003587 }
3588
3589 cc_library {
3590 name: "lib_dep",
3591 srcs: ["mylib.cpp"],
3592 shared_libs: ["libfoo"],
3593 system_shared_libs: [],
3594 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003595 apex_available: [
3596 "myapex_dep",
3597 "myapex_provider",
3598 "myapex_selfcontained",
3599 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003600 }
3601
3602 cc_library {
3603 name: "libfoo",
3604 srcs: ["mytest.cpp"],
3605 stubs: {
3606 versions: ["1"],
3607 },
3608 system_shared_libs: [],
3609 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003610 apex_available: [
3611 "myapex_provider",
3612 "myapex_selfcontained",
3613 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003614 }
3615 `)
3616
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003617 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003618 var provideNativeLibs, requireNativeLibs []string
3619
Sundong Ahnabb64432019-10-22 13:58:29 +09003620 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003621 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3622 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003623 ensureListEmpty(t, provideNativeLibs)
3624 ensureListEmpty(t, requireNativeLibs)
3625
Sundong Ahnabb64432019-10-22 13:58:29 +09003626 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003627 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3628 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003629 ensureListEmpty(t, provideNativeLibs)
3630 ensureListContains(t, requireNativeLibs, "libfoo.so")
3631
Sundong Ahnabb64432019-10-22 13:58:29 +09003632 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003633 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3634 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003635 ensureListContains(t, provideNativeLibs, "libfoo.so")
3636 ensureListEmpty(t, requireNativeLibs)
3637
Sundong Ahnabb64432019-10-22 13:58:29 +09003638 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003639 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3640 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003641 ensureListContains(t, provideNativeLibs, "libfoo.so")
3642 ensureListEmpty(t, requireNativeLibs)
3643}
3644
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003645func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003646 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003647 apex {
3648 name: "myapex",
3649 key: "myapex.key",
3650 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003651 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003652 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003653 }
3654
3655 apex_key {
3656 name: "myapex.key",
3657 public_key: "testkey.avbpubkey",
3658 private_key: "testkey.pem",
3659 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003660
3661 cc_library {
3662 name: "mylib",
3663 srcs: ["mylib.cpp"],
3664 system_shared_libs: [],
3665 stl: "none",
3666 apex_available: [
3667 "//apex_available:platform",
3668 "myapex",
3669 ],
3670 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003671 `)
3672
Sundong Ahnabb64432019-10-22 13:58:29 +09003673 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003674 apexManifestRule := module.Rule("apexManifestRule")
3675 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3676 apexRule := module.Rule("apexRule")
3677 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003678
3679 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003680 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09003681 name := apexBundle.BaseModuleName()
3682 prefix := "TARGET_"
3683 var builder strings.Builder
3684 data.Custom(&builder, name, prefix, "", data)
3685 androidMk := builder.String()
3686 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3687 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003688}
3689
Alex Light0851b882019-02-07 13:20:53 -08003690func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003691 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003692 apex {
3693 name: "myapex",
3694 key: "myapex.key",
3695 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003696 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003697 }
3698
3699 apex_key {
3700 name: "myapex.key",
3701 public_key: "testkey.avbpubkey",
3702 private_key: "testkey.pem",
3703 }
3704
3705 cc_library {
3706 name: "mylib_common",
3707 srcs: ["mylib.cpp"],
3708 system_shared_libs: [],
3709 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003710 apex_available: [
3711 "//apex_available:platform",
3712 "myapex",
3713 ],
Alex Light0851b882019-02-07 13:20:53 -08003714 }
3715 `)
3716
Sundong Ahnabb64432019-10-22 13:58:29 +09003717 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003718 apexRule := module.Rule("apexRule")
3719 copyCmds := apexRule.Args["copy_commands"]
3720
3721 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3722 t.Log("Apex was a test apex!")
3723 t.Fail()
3724 }
3725 // Ensure that main rule creates an output
3726 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3727
3728 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003729 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003730
3731 // Ensure that both direct and indirect deps are copied into apex
3732 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3733
Colin Cross7113d202019-11-20 16:39:12 -08003734 // Ensure that the platform variant ends with _shared
3735 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003736
Colin Cross56a83212020-09-15 18:30:11 -07003737 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003738 t.Log("Found mylib_common not in any apex!")
3739 t.Fail()
3740 }
3741}
3742
3743func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003744 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003745 apex_test {
3746 name: "myapex",
3747 key: "myapex.key",
3748 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003749 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003750 }
3751
3752 apex_key {
3753 name: "myapex.key",
3754 public_key: "testkey.avbpubkey",
3755 private_key: "testkey.pem",
3756 }
3757
3758 cc_library {
3759 name: "mylib_common_test",
3760 srcs: ["mylib.cpp"],
3761 system_shared_libs: [],
3762 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003763 // TODO: remove //apex_available:platform
3764 apex_available: [
3765 "//apex_available:platform",
3766 "myapex",
3767 ],
Alex Light0851b882019-02-07 13:20:53 -08003768 }
3769 `)
3770
Sundong Ahnabb64432019-10-22 13:58:29 +09003771 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003772 apexRule := module.Rule("apexRule")
3773 copyCmds := apexRule.Args["copy_commands"]
3774
3775 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3776 t.Log("Apex was not a test apex!")
3777 t.Fail()
3778 }
3779 // Ensure that main rule creates an output
3780 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3781
3782 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003783 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003784
3785 // Ensure that both direct and indirect deps are copied into apex
3786 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3787
Colin Cross7113d202019-11-20 16:39:12 -08003788 // Ensure that the platform variant ends with _shared
3789 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003790}
3791
Alex Light9670d332019-01-29 18:07:33 -08003792func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003793 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003794 apex {
3795 name: "myapex",
3796 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003797 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08003798 multilib: {
3799 first: {
3800 native_shared_libs: ["mylib_common"],
3801 }
3802 },
3803 target: {
3804 android: {
3805 multilib: {
3806 first: {
3807 native_shared_libs: ["mylib"],
3808 }
3809 }
3810 },
3811 host: {
3812 multilib: {
3813 first: {
3814 native_shared_libs: ["mylib2"],
3815 }
3816 }
3817 }
3818 }
3819 }
3820
3821 apex_key {
3822 name: "myapex.key",
3823 public_key: "testkey.avbpubkey",
3824 private_key: "testkey.pem",
3825 }
3826
3827 cc_library {
3828 name: "mylib",
3829 srcs: ["mylib.cpp"],
3830 system_shared_libs: [],
3831 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003832 // TODO: remove //apex_available:platform
3833 apex_available: [
3834 "//apex_available:platform",
3835 "myapex",
3836 ],
Alex Light9670d332019-01-29 18:07:33 -08003837 }
3838
3839 cc_library {
3840 name: "mylib_common",
3841 srcs: ["mylib.cpp"],
3842 system_shared_libs: [],
3843 stl: "none",
3844 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003845 // TODO: remove //apex_available:platform
3846 apex_available: [
3847 "//apex_available:platform",
3848 "myapex",
3849 ],
Alex Light9670d332019-01-29 18:07:33 -08003850 }
3851
3852 cc_library {
3853 name: "mylib2",
3854 srcs: ["mylib.cpp"],
3855 system_shared_libs: [],
3856 stl: "none",
3857 compile_multilib: "first",
3858 }
3859 `)
3860
Sundong Ahnabb64432019-10-22 13:58:29 +09003861 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003862 copyCmds := apexRule.Args["copy_commands"]
3863
3864 // Ensure that main rule creates an output
3865 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3866
3867 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003868 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
3869 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
3870 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08003871
3872 // Ensure that both direct and indirect deps are copied into apex
3873 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3874 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3875 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3876
Colin Cross7113d202019-11-20 16:39:12 -08003877 // Ensure that the platform variant ends with _shared
3878 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
3879 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
3880 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08003881}
Jiyong Park04480cf2019-02-06 00:16:29 +09003882
Jiyong Park59140302020-12-14 18:44:04 +09003883func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003884 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09003885 apex {
3886 name: "myapex",
3887 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003888 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09003889 arch: {
3890 arm64: {
3891 native_shared_libs: ["mylib.arm64"],
3892 },
3893 x86_64: {
3894 native_shared_libs: ["mylib.x64"],
3895 },
3896 }
3897 }
3898
3899 apex_key {
3900 name: "myapex.key",
3901 public_key: "testkey.avbpubkey",
3902 private_key: "testkey.pem",
3903 }
3904
3905 cc_library {
3906 name: "mylib.arm64",
3907 srcs: ["mylib.cpp"],
3908 system_shared_libs: [],
3909 stl: "none",
3910 // TODO: remove //apex_available:platform
3911 apex_available: [
3912 "//apex_available:platform",
3913 "myapex",
3914 ],
3915 }
3916
3917 cc_library {
3918 name: "mylib.x64",
3919 srcs: ["mylib.cpp"],
3920 system_shared_libs: [],
3921 stl: "none",
3922 // TODO: remove //apex_available:platform
3923 apex_available: [
3924 "//apex_available:platform",
3925 "myapex",
3926 ],
3927 }
3928 `)
3929
3930 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
3931 copyCmds := apexRule.Args["copy_commands"]
3932
3933 // Ensure that apex variant is created for the direct dep
3934 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
3935 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
3936
3937 // Ensure that both direct and indirect deps are copied into apex
3938 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
3939 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
3940}
3941
Jiyong Park04480cf2019-02-06 00:16:29 +09003942func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003943 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09003944 apex {
3945 name: "myapex",
3946 key: "myapex.key",
3947 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003948 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09003949 }
3950
3951 apex_key {
3952 name: "myapex.key",
3953 public_key: "testkey.avbpubkey",
3954 private_key: "testkey.pem",
3955 }
3956
3957 sh_binary {
3958 name: "myscript",
3959 src: "mylib.cpp",
3960 filename: "myscript.sh",
3961 sub_dir: "script",
3962 }
3963 `)
3964
Sundong Ahnabb64432019-10-22 13:58:29 +09003965 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09003966 copyCmds := apexRule.Args["copy_commands"]
3967
3968 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
3969}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003970
Jooyung Han91df2082019-11-20 01:49:42 +09003971func TestApexInVariousPartition(t *testing.T) {
3972 testcases := []struct {
3973 propName, parition, flattenedPartition string
3974 }{
3975 {"", "system", "system_ext"},
3976 {"product_specific: true", "product", "product"},
3977 {"soc_specific: true", "vendor", "vendor"},
3978 {"proprietary: true", "vendor", "vendor"},
3979 {"vendor: true", "vendor", "vendor"},
3980 {"system_ext_specific: true", "system_ext", "system_ext"},
3981 }
3982 for _, tc := range testcases {
3983 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003984 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09003985 apex {
3986 name: "myapex",
3987 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003988 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09003989 `+tc.propName+`
3990 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003991
Jooyung Han91df2082019-11-20 01:49:42 +09003992 apex_key {
3993 name: "myapex.key",
3994 public_key: "testkey.avbpubkey",
3995 private_key: "testkey.pem",
3996 }
3997 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003998
Jooyung Han91df2082019-11-20 01:49:42 +09003999 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004000 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4001 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004002 if actual != expected {
4003 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4004 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004005
Jooyung Han91df2082019-11-20 01:49:42 +09004006 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004007 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4008 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004009 if actual != expected {
4010 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4011 }
4012 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004013 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004014}
Jiyong Park67882562019-03-21 01:11:21 +09004015
Jooyung Han580eb4f2020-06-24 19:33:06 +09004016func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004017 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004018 apex {
4019 name: "myapex",
4020 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004021 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004022 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004023
Jooyung Han580eb4f2020-06-24 19:33:06 +09004024 apex_key {
4025 name: "myapex.key",
4026 public_key: "testkey.avbpubkey",
4027 private_key: "testkey.pem",
4028 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004029 `)
4030 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004031 rule := module.Output("file_contexts")
4032 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4033}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004034
Jooyung Han580eb4f2020-06-24 19:33:06 +09004035func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004036 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004037 apex {
4038 name: "myapex",
4039 key: "myapex.key",
4040 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004041 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004042 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004043
Jooyung Han580eb4f2020-06-24 19:33:06 +09004044 apex_key {
4045 name: "myapex.key",
4046 public_key: "testkey.avbpubkey",
4047 private_key: "testkey.pem",
4048 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004049 `, withFiles(map[string][]byte{
4050 "my_own_file_contexts": nil,
4051 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004052}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004053
Jooyung Han580eb4f2020-06-24 19:33:06 +09004054func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004055 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004056 apex {
4057 name: "myapex",
4058 key: "myapex.key",
4059 product_specific: true,
4060 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004061 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004062 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004063
Jooyung Han580eb4f2020-06-24 19:33:06 +09004064 apex_key {
4065 name: "myapex.key",
4066 public_key: "testkey.avbpubkey",
4067 private_key: "testkey.pem",
4068 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004069 `)
4070
Colin Cross1c460562021-02-16 17:55:47 -08004071 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004072 apex {
4073 name: "myapex",
4074 key: "myapex.key",
4075 product_specific: true,
4076 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004077 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004078 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004079
Jooyung Han580eb4f2020-06-24 19:33:06 +09004080 apex_key {
4081 name: "myapex.key",
4082 public_key: "testkey.avbpubkey",
4083 private_key: "testkey.pem",
4084 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004085 `, withFiles(map[string][]byte{
4086 "product_specific_file_contexts": nil,
4087 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004088 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4089 rule := module.Output("file_contexts")
4090 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4091}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004092
Jooyung Han580eb4f2020-06-24 19:33:06 +09004093func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004094 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004095 apex {
4096 name: "myapex",
4097 key: "myapex.key",
4098 product_specific: true,
4099 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004100 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004101 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004102
Jooyung Han580eb4f2020-06-24 19:33:06 +09004103 apex_key {
4104 name: "myapex.key",
4105 public_key: "testkey.avbpubkey",
4106 private_key: "testkey.pem",
4107 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004108
Jooyung Han580eb4f2020-06-24 19:33:06 +09004109 filegroup {
4110 name: "my-file-contexts",
4111 srcs: ["product_specific_file_contexts"],
4112 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004113 `, withFiles(map[string][]byte{
4114 "product_specific_file_contexts": nil,
4115 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004116 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4117 rule := module.Output("file_contexts")
4118 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004119}
4120
Jiyong Park67882562019-03-21 01:11:21 +09004121func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004122 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004123 apex_key {
4124 name: "myapex.key",
4125 public_key: ":my.avbpubkey",
4126 private_key: ":my.pem",
4127 product_specific: true,
4128 }
4129
4130 filegroup {
4131 name: "my.avbpubkey",
4132 srcs: ["testkey2.avbpubkey"],
4133 }
4134
4135 filegroup {
4136 name: "my.pem",
4137 srcs: ["testkey2.pem"],
4138 }
4139 `)
4140
4141 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4142 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004143 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004144 if actual_pubkey != expected_pubkey {
4145 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4146 }
4147 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004148 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004149 if actual_privkey != expected_privkey {
4150 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4151 }
4152}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004153
4154func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004155 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004156 prebuilt_apex {
4157 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004158 arch: {
4159 arm64: {
4160 src: "myapex-arm64.apex",
4161 },
4162 arm: {
4163 src: "myapex-arm.apex",
4164 },
4165 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004166 }
4167 `)
4168
Paul Duffin5ec165d2021-06-15 19:09:41 +01004169 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004170
Jiyong Parkc95714e2019-03-29 14:23:10 +09004171 expectedInput := "myapex-arm64.apex"
4172 if prebuilt.inputApex.String() != expectedInput {
4173 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4174 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004175}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004176
Paul Duffinc0609c62021-03-01 17:27:16 +00004177func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin5ec165d2021-06-15 19:09:41 +01004178 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004179 prebuilt_apex {
4180 name: "myapex",
4181 }
4182 `)
4183}
4184
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004185func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004186 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004187 prebuilt_apex {
4188 name: "myapex",
4189 src: "myapex-arm.apex",
4190 filename: "notmyapex.apex",
4191 }
4192 `)
4193
Paul Duffin5ec165d2021-06-15 19:09:41 +01004194 p := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004195
4196 expected := "notmyapex.apex"
4197 if p.installFilename != expected {
4198 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4199 }
4200}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004201
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004202func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004203 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004204 prebuilt_apex {
4205 name: "myapex.prebuilt",
4206 src: "myapex-arm.apex",
4207 overrides: [
4208 "myapex",
4209 ],
4210 }
4211 `)
4212
Paul Duffin5ec165d2021-06-15 19:09:41 +01004213 p := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt").Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004214
4215 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004216 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004217 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004218 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004219 }
4220}
4221
Paul Duffin092153d2021-01-26 11:42:39 +00004222// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4223// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004224func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004225 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004226
Paul Duffin89886cb2021-02-05 16:44:03 +00004227 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004228 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004229 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004230 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004231 stem := android.RemoveOptionalPrebuiltPrefix(name)
4232 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004233 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4234 }
4235 }
4236
Paul Duffin39853512021-02-26 11:09:39 +00004237 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004238 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004239 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004240 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4241 }
4242 }
4243
4244 t.Run("prebuilt only", func(t *testing.T) {
4245 bp := `
4246 prebuilt_apex {
4247 name: "myapex",
4248 arch: {
4249 arm64: {
4250 src: "myapex-arm64.apex",
4251 },
4252 arm: {
4253 src: "myapex-arm.apex",
4254 },
4255 },
Paul Duffin39853512021-02-26 11:09:39 +00004256 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004257 }
4258
4259 java_import {
4260 name: "libfoo",
4261 jars: ["libfoo.jar"],
4262 }
Paul Duffin39853512021-02-26 11:09:39 +00004263
4264 java_sdk_library_import {
4265 name: "libbar",
4266 public: {
4267 jars: ["libbar.jar"],
4268 },
4269 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004270 `
4271
4272 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4273 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4274
Paul Duffinf6932af2021-02-26 18:21:56 +00004275 // Make sure that the deapexer has the correct input APEX.
4276 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4277 rule := deapexer.Rule("deapexer")
4278 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4279 t.Errorf("expected: %q, found: %q", expected, actual)
4280 }
4281
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004282 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin5ec165d2021-06-15 19:09:41 +01004283 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004284 rule = prebuiltApex.Rule("android/soong/android.Cp")
4285 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4286 t.Errorf("expected: %q, found: %q", expected, actual)
4287 }
4288
Paul Duffin89886cb2021-02-05 16:44:03 +00004289 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004290
4291 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004292 })
4293
4294 t.Run("prebuilt with source preferred", func(t *testing.T) {
4295
4296 bp := `
4297 prebuilt_apex {
4298 name: "myapex",
4299 arch: {
4300 arm64: {
4301 src: "myapex-arm64.apex",
4302 },
4303 arm: {
4304 src: "myapex-arm.apex",
4305 },
4306 },
Paul Duffin39853512021-02-26 11:09:39 +00004307 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004308 }
4309
4310 java_import {
4311 name: "libfoo",
4312 jars: ["libfoo.jar"],
4313 }
4314
4315 java_library {
4316 name: "libfoo",
4317 }
Paul Duffin39853512021-02-26 11:09:39 +00004318
4319 java_sdk_library_import {
4320 name: "libbar",
4321 public: {
4322 jars: ["libbar.jar"],
4323 },
4324 }
4325
4326 java_sdk_library {
4327 name: "libbar",
4328 srcs: ["foo/bar/MyClass.java"],
4329 unsafe_ignore_missing_latest_api: true,
4330 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004331 `
4332
4333 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4334 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4335
Paul Duffin89886cb2021-02-05 16:44:03 +00004336 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004337 ensureNoSourceVariant(t, ctx, "libfoo")
4338
4339 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4340 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004341 })
4342
4343 t.Run("prebuilt preferred with source", func(t *testing.T) {
4344 bp := `
4345 prebuilt_apex {
4346 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004347 arch: {
4348 arm64: {
4349 src: "myapex-arm64.apex",
4350 },
4351 arm: {
4352 src: "myapex-arm.apex",
4353 },
4354 },
Paul Duffin39853512021-02-26 11:09:39 +00004355 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004356 }
4357
4358 java_import {
4359 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004360 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004361 jars: ["libfoo.jar"],
4362 }
4363
4364 java_library {
4365 name: "libfoo",
4366 }
Paul Duffin39853512021-02-26 11:09:39 +00004367
4368 java_sdk_library_import {
4369 name: "libbar",
4370 prefer: true,
4371 public: {
4372 jars: ["libbar.jar"],
4373 },
4374 }
4375
4376 java_sdk_library {
4377 name: "libbar",
4378 srcs: ["foo/bar/MyClass.java"],
4379 unsafe_ignore_missing_latest_api: true,
4380 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004381 `
4382
4383 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4384 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4385
Paul Duffin89886cb2021-02-05 16:44:03 +00004386 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004387 ensureNoSourceVariant(t, ctx, "libfoo")
4388
4389 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4390 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004391 })
4392}
4393
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004394func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01004395 preparer := android.GroupFixturePreparers(
4396 java.FixtureConfigureBootJars("myapex:libfoo", "myapex:libbar"),
4397 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
4398 // is disabled.
4399 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
4400 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004401
Paul Duffin37856732021-02-26 14:24:15 +00004402 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4403 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01004404 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004405 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004406 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004407 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004408 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004409 foundLibfooJar = true
4410 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01004411 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004412 }
4413 }
4414 if !foundLibfooJar {
Paul Duffin55607122021-03-30 23:32:51 +01004415 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 +00004416 }
4417 }
4418
Paul Duffin4fd997b2021-02-03 20:06:33 +00004419 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004420 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01004421 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffin537ea3d2021-05-14 10:38:00 +01004422 indexRule := platformBootclasspath.Rule("monolithic_hidden_API_index")
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004423 java.CheckHiddenAPIRuleInputs(t, "index", expectedInputs, indexRule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00004424 }
4425
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004426 fragment := java.ApexVariantReference{
4427 Apex: proptools.StringPtr("myapex"),
4428 Module: proptools.StringPtr("my-bootclasspath-fragment"),
4429 }
4430
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004431 t.Run("prebuilt only", func(t *testing.T) {
4432 bp := `
4433 prebuilt_apex {
4434 name: "myapex",
4435 arch: {
4436 arm64: {
4437 src: "myapex-arm64.apex",
4438 },
4439 arm: {
4440 src: "myapex-arm.apex",
4441 },
4442 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004443 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4444 }
4445
4446 prebuilt_bootclasspath_fragment {
4447 name: "my-bootclasspath-fragment",
4448 contents: ["libfoo", "libbar"],
4449 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004450 }
4451
4452 java_import {
4453 name: "libfoo",
4454 jars: ["libfoo.jar"],
4455 apex_available: ["myapex"],
4456 }
Paul Duffin37856732021-02-26 14:24:15 +00004457
4458 java_sdk_library_import {
4459 name: "libbar",
4460 public: {
4461 jars: ["libbar.jar"],
4462 },
4463 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004464 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004465 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004466 `
4467
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004468 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004469 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4470 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004471
Paul Duffin537ea3d2021-05-14 10:38:00 +01004472 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin9d67ca62021-02-03 20:06:33 +00004473 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004474 out/soong/.intermediates/libbar.stubs/android_common/combined/libbar.stubs.jar
4475 out/soong/.intermediates/libfoo/android_common_myapex/combined/libfoo.jar
4476 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004477 })
4478
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004479 t.Run("apex_set only", func(t *testing.T) {
4480 bp := `
4481 apex_set {
4482 name: "myapex",
4483 set: "myapex.apks",
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004484 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4485 }
4486
4487 prebuilt_bootclasspath_fragment {
4488 name: "my-bootclasspath-fragment",
4489 contents: ["libfoo", "libbar"],
4490 apex_available: ["myapex"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004491 }
4492
4493 java_import {
4494 name: "libfoo",
4495 jars: ["libfoo.jar"],
4496 apex_available: ["myapex"],
4497 }
4498
4499 java_sdk_library_import {
4500 name: "libbar",
4501 public: {
4502 jars: ["libbar.jar"],
4503 },
4504 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004505 shared_library: false,
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004506 }
4507 `
4508
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004509 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004510 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4511 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
4512
Paul Duffin537ea3d2021-05-14 10:38:00 +01004513 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004514 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004515 out/soong/.intermediates/libbar.stubs/android_common/combined/libbar.stubs.jar
4516 out/soong/.intermediates/libfoo/android_common_myapex/combined/libfoo.jar
4517 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004518 })
4519
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004520 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4521 bp := `
4522 prebuilt_apex {
4523 name: "myapex",
4524 arch: {
4525 arm64: {
4526 src: "myapex-arm64.apex",
4527 },
4528 arm: {
4529 src: "myapex-arm.apex",
4530 },
4531 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004532 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4533 }
4534
4535 prebuilt_bootclasspath_fragment {
4536 name: "my-bootclasspath-fragment",
4537 contents: ["libfoo", "libbar"],
4538 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004539 }
4540
4541 java_import {
4542 name: "libfoo",
4543 jars: ["libfoo.jar"],
4544 apex_available: ["myapex"],
4545 }
4546
4547 java_library {
4548 name: "libfoo",
4549 srcs: ["foo/bar/MyClass.java"],
4550 apex_available: ["myapex"],
4551 }
Paul Duffin37856732021-02-26 14:24:15 +00004552
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 Duffin37856732021-02-26 14:24:15 +00004560 }
4561
4562 java_sdk_library {
4563 name: "libbar",
4564 srcs: ["foo/bar/MyClass.java"],
4565 unsafe_ignore_missing_latest_api: true,
4566 apex_available: ["myapex"],
4567 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004568 `
4569
4570 // In this test the source (java_library) libfoo is active since the
4571 // prebuilt (java_import) defaults to prefer:false. However the
4572 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4573 // find the dex boot jar in it. We either need to disable the source libfoo
4574 // or make the prebuilt libfoo preferred.
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004575 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004576 })
4577
4578 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4579 bp := `
4580 prebuilt_apex {
4581 name: "myapex",
4582 arch: {
4583 arm64: {
4584 src: "myapex-arm64.apex",
4585 },
4586 arm: {
4587 src: "myapex-arm.apex",
4588 },
4589 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004590 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4591 }
4592
4593 prebuilt_bootclasspath_fragment {
4594 name: "my-bootclasspath-fragment",
4595 contents: ["libfoo", "libbar"],
4596 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004597 }
4598
4599 java_import {
4600 name: "libfoo",
4601 prefer: true,
4602 jars: ["libfoo.jar"],
4603 apex_available: ["myapex"],
4604 }
4605
4606 java_library {
4607 name: "libfoo",
4608 srcs: ["foo/bar/MyClass.java"],
4609 apex_available: ["myapex"],
4610 }
Paul Duffin37856732021-02-26 14:24:15 +00004611
4612 java_sdk_library_import {
4613 name: "libbar",
4614 prefer: true,
4615 public: {
4616 jars: ["libbar.jar"],
4617 },
4618 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004619 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004620 }
4621
4622 java_sdk_library {
4623 name: "libbar",
4624 srcs: ["foo/bar/MyClass.java"],
4625 unsafe_ignore_missing_latest_api: true,
4626 apex_available: ["myapex"],
4627 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004628 `
4629
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004630 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004631 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4632 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004633
Paul Duffin537ea3d2021-05-14 10:38:00 +01004634 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin9d67ca62021-02-03 20:06:33 +00004635 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004636 out/soong/.intermediates/prebuilt_libbar.stubs/android_common/combined/libbar.stubs.jar
4637 out/soong/.intermediates/prebuilt_libfoo/android_common_myapex/combined/libfoo.jar
4638 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004639 })
4640
4641 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4642 bp := `
4643 apex {
4644 name: "myapex",
4645 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004646 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004647 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004648 }
4649
4650 apex_key {
4651 name: "myapex.key",
4652 public_key: "testkey.avbpubkey",
4653 private_key: "testkey.pem",
4654 }
4655
4656 prebuilt_apex {
4657 name: "myapex",
4658 arch: {
4659 arm64: {
4660 src: "myapex-arm64.apex",
4661 },
4662 arm: {
4663 src: "myapex-arm.apex",
4664 },
4665 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004666 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4667 }
4668
4669 prebuilt_bootclasspath_fragment {
4670 name: "my-bootclasspath-fragment",
4671 contents: ["libfoo", "libbar"],
4672 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004673 }
4674
4675 java_import {
4676 name: "libfoo",
4677 jars: ["libfoo.jar"],
4678 apex_available: ["myapex"],
4679 }
4680
4681 java_library {
4682 name: "libfoo",
4683 srcs: ["foo/bar/MyClass.java"],
4684 apex_available: ["myapex"],
4685 }
Paul Duffin37856732021-02-26 14:24:15 +00004686
4687 java_sdk_library_import {
4688 name: "libbar",
4689 public: {
4690 jars: ["libbar.jar"],
4691 },
4692 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004693 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004694 }
4695
4696 java_sdk_library {
4697 name: "libbar",
4698 srcs: ["foo/bar/MyClass.java"],
4699 unsafe_ignore_missing_latest_api: true,
4700 apex_available: ["myapex"],
4701 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004702 `
4703
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004704 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004705 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4706 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004707
Paul Duffin537ea3d2021-05-14 10:38:00 +01004708 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin4fd997b2021-02-03 20:06:33 +00004709 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004710 out/soong/.intermediates/libbar/android_common_myapex/javac/libbar.jar
4711 out/soong/.intermediates/libfoo/android_common_apex10000/javac/libfoo.jar
4712 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004713 })
4714
4715 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4716 bp := `
4717 apex {
4718 name: "myapex",
4719 enabled: false,
4720 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01004721 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004722 }
4723
4724 apex_key {
4725 name: "myapex.key",
4726 public_key: "testkey.avbpubkey",
4727 private_key: "testkey.pem",
4728 }
4729
4730 prebuilt_apex {
4731 name: "myapex",
4732 arch: {
4733 arm64: {
4734 src: "myapex-arm64.apex",
4735 },
4736 arm: {
4737 src: "myapex-arm.apex",
4738 },
4739 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004740 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4741 }
4742
4743 prebuilt_bootclasspath_fragment {
4744 name: "my-bootclasspath-fragment",
4745 contents: ["libfoo", "libbar"],
4746 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004747 }
4748
4749 java_import {
4750 name: "libfoo",
4751 prefer: true,
4752 jars: ["libfoo.jar"],
4753 apex_available: ["myapex"],
4754 }
4755
4756 java_library {
4757 name: "libfoo",
4758 srcs: ["foo/bar/MyClass.java"],
4759 apex_available: ["myapex"],
4760 }
Paul Duffin37856732021-02-26 14:24:15 +00004761
4762 java_sdk_library_import {
4763 name: "libbar",
4764 prefer: true,
4765 public: {
4766 jars: ["libbar.jar"],
4767 },
4768 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004769 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004770 }
4771
4772 java_sdk_library {
4773 name: "libbar",
4774 srcs: ["foo/bar/MyClass.java"],
4775 unsafe_ignore_missing_latest_api: true,
4776 apex_available: ["myapex"],
4777 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004778 `
4779
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004780 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004781 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4782 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004783
Paul Duffin537ea3d2021-05-14 10:38:00 +01004784 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin9d67ca62021-02-03 20:06:33 +00004785 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004786 out/soong/.intermediates/prebuilt_libbar.stubs/android_common/combined/libbar.stubs.jar
4787 out/soong/.intermediates/prebuilt_libfoo/android_common_myapex/combined/libfoo.jar
4788 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004789 })
4790}
4791
Roland Levillain630846d2019-06-26 12:48:34 +01004792func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004793 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004794 apex_test {
4795 name: "myapex",
4796 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004797 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01004798 tests: [
4799 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004800 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004801 ],
4802 }
4803
4804 apex_key {
4805 name: "myapex.key",
4806 public_key: "testkey.avbpubkey",
4807 private_key: "testkey.pem",
4808 }
4809
Liz Kammer1c14a212020-05-12 15:26:55 -07004810 filegroup {
4811 name: "fg",
4812 srcs: [
4813 "baz",
4814 "bar/baz"
4815 ],
4816 }
4817
Roland Levillain630846d2019-06-26 12:48:34 +01004818 cc_test {
4819 name: "mytest",
4820 gtest: false,
4821 srcs: ["mytest.cpp"],
4822 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004823 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004824 system_shared_libs: [],
4825 static_executable: true,
4826 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004827 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004828 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004829
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004830 cc_library {
4831 name: "mylib",
4832 srcs: ["mylib.cpp"],
4833 system_shared_libs: [],
4834 stl: "none",
4835 }
4836
Liz Kammer5bd365f2020-05-27 15:15:11 -07004837 filegroup {
4838 name: "fg2",
4839 srcs: [
4840 "testdata/baz"
4841 ],
4842 }
4843
Roland Levillain9b5fde92019-06-28 15:41:19 +01004844 cc_test {
4845 name: "mytests",
4846 gtest: false,
4847 srcs: [
4848 "mytest1.cpp",
4849 "mytest2.cpp",
4850 "mytest3.cpp",
4851 ],
4852 test_per_src: true,
4853 relative_install_path: "test",
4854 system_shared_libs: [],
4855 static_executable: true,
4856 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004857 data: [
4858 ":fg",
4859 ":fg2",
4860 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004861 }
Roland Levillain630846d2019-06-26 12:48:34 +01004862 `)
4863
Sundong Ahnabb64432019-10-22 13:58:29 +09004864 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004865 copyCmds := apexRule.Args["copy_commands"]
4866
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004867 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004868 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004869 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004870
Liz Kammer1c14a212020-05-12 15:26:55 -07004871 //Ensure that test data are copied into apex.
4872 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4873 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4874
Roland Levillain9b5fde92019-06-28 15:41:19 +01004875 // Ensure that test deps built with `test_per_src` are copied into apex.
4876 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4877 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4878 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004879
4880 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004881 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004882 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004883 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004884 prefix := "TARGET_"
4885 var builder strings.Builder
4886 data.Custom(&builder, name, prefix, "", data)
4887 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004888 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4889 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4890 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4891 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004892 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004893 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004894 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004895
4896 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004897 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004898 data.Custom(&builder, name, prefix, "", data)
4899 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004900 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4901 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004902}
4903
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004904func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004905 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004906 apex {
4907 name: "myapex",
4908 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004909 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004910 }
4911 apex_key {
4912 name: "myapex.key",
4913 public_key: "testkey.avbpubkey",
4914 private_key: "testkey.pem",
4915 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00004916 `,
4917 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4918 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4919 }),
4920 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004921 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004922 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07004923 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004924 var builder strings.Builder
4925 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4926 androidMk := builder.String()
4927 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4928}
4929
Jooyung Hand48f3c32019-08-23 11:18:57 +09004930func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4931 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4932 apex {
4933 name: "myapex",
4934 key: "myapex.key",
4935 native_shared_libs: ["libfoo"],
4936 }
4937
4938 apex_key {
4939 name: "myapex.key",
4940 public_key: "testkey.avbpubkey",
4941 private_key: "testkey.pem",
4942 }
4943
4944 cc_library {
4945 name: "libfoo",
4946 stl: "none",
4947 system_shared_libs: [],
4948 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004949 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004950 }
4951 `)
4952 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
4953 apex {
4954 name: "myapex",
4955 key: "myapex.key",
4956 java_libs: ["myjar"],
4957 }
4958
4959 apex_key {
4960 name: "myapex.key",
4961 public_key: "testkey.avbpubkey",
4962 private_key: "testkey.pem",
4963 }
4964
4965 java_library {
4966 name: "myjar",
4967 srcs: ["foo/bar/MyClass.java"],
4968 sdk_version: "none",
4969 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09004970 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004971 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004972 }
4973 `)
4974}
4975
Bill Peckhama41a6962021-01-11 10:58:54 -08004976func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004977 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08004978 apex {
4979 name: "myapex",
4980 key: "myapex.key",
4981 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004982 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08004983 }
4984
4985 apex_key {
4986 name: "myapex.key",
4987 public_key: "testkey.avbpubkey",
4988 private_key: "testkey.pem",
4989 }
4990
4991 java_import {
4992 name: "myjavaimport",
4993 apex_available: ["myapex"],
4994 jars: ["my.jar"],
4995 compile_dex: true,
4996 }
4997 `)
4998
4999 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5000 apexRule := module.Rule("apexRule")
5001 copyCmds := apexRule.Args["copy_commands"]
5002 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5003}
5004
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005005func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005006 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005007 apex {
5008 name: "myapex",
5009 key: "myapex.key",
5010 apps: [
5011 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005012 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005013 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005014 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005015 }
5016
5017 apex_key {
5018 name: "myapex.key",
5019 public_key: "testkey.avbpubkey",
5020 private_key: "testkey.pem",
5021 }
5022
5023 android_app {
5024 name: "AppFoo",
5025 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005026 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005027 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005028 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005029 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005030 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005031 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005032
5033 android_app {
5034 name: "AppFooPriv",
5035 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005036 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005037 system_modules: "none",
5038 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005039 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005040 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005041 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005042
5043 cc_library_shared {
5044 name: "libjni",
5045 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005046 shared_libs: ["libfoo"],
5047 stl: "none",
5048 system_shared_libs: [],
5049 apex_available: [ "myapex" ],
5050 sdk_version: "current",
5051 }
5052
5053 cc_library_shared {
5054 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005055 stl: "none",
5056 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005057 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005058 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005059 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005060 `)
5061
Sundong Ahnabb64432019-10-22 13:58:29 +09005062 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005063 apexRule := module.Rule("apexRule")
5064 copyCmds := apexRule.Args["copy_commands"]
5065
5066 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005067 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005068
Colin Crossaede88c2020-08-11 12:17:01 -07005069 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005070 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005071 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005072 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005073 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005074 // JNI libraries including transitive deps are
5075 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005076 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005077 // ... embedded inside APK (jnilibs.zip)
5078 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5079 // ... and not directly inside the APEX
5080 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5081 }
Dario Frenicde2a032019-10-27 00:29:22 +01005082}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005083
Dario Frenicde2a032019-10-27 00:29:22 +01005084func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005085 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005086 apex {
5087 name: "myapex",
5088 key: "myapex.key",
5089 apps: [
5090 "AppFooPrebuilt",
5091 "AppFooPrivPrebuilt",
5092 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005093 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005094 }
5095
5096 apex_key {
5097 name: "myapex.key",
5098 public_key: "testkey.avbpubkey",
5099 private_key: "testkey.pem",
5100 }
5101
5102 android_app_import {
5103 name: "AppFooPrebuilt",
5104 apk: "PrebuiltAppFoo.apk",
5105 presigned: true,
5106 dex_preopt: {
5107 enabled: false,
5108 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005109 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005110 }
5111
5112 android_app_import {
5113 name: "AppFooPrivPrebuilt",
5114 apk: "PrebuiltAppFooPriv.apk",
5115 privileged: true,
5116 presigned: true,
5117 dex_preopt: {
5118 enabled: false,
5119 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005120 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005121 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005122 }
5123 `)
5124
Sundong Ahnabb64432019-10-22 13:58:29 +09005125 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005126 apexRule := module.Rule("apexRule")
5127 copyCmds := apexRule.Args["copy_commands"]
5128
5129 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005130 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5131}
5132
5133func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005134 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005135 apex {
5136 name: "myapex",
5137 key: "myapex.key",
5138 apps: [
5139 "AppFoo",
5140 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005141 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005142 }
5143
5144 apex_key {
5145 name: "myapex.key",
5146 public_key: "testkey.avbpubkey",
5147 private_key: "testkey.pem",
5148 }
5149
5150 android_app {
5151 name: "AppFoo",
5152 srcs: ["foo/bar/MyClass.java"],
5153 sdk_version: "none",
5154 system_modules: "none",
5155 apex_available: [ "myapex" ],
5156 }
5157
5158 android_app_import {
5159 name: "AppFoo",
5160 apk: "AppFooPrebuilt.apk",
5161 filename: "AppFooPrebuilt.apk",
5162 presigned: true,
5163 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005164 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005165 }
5166 `, withFiles(map[string][]byte{
5167 "AppFooPrebuilt.apk": nil,
5168 }))
5169
5170 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005171 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005172 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005173}
5174
Dario Freni6f3937c2019-12-20 22:58:03 +00005175func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005176 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005177 apex {
5178 name: "myapex",
5179 key: "myapex.key",
5180 apps: [
5181 "TesterHelpAppFoo",
5182 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005183 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005184 }
5185
5186 apex_key {
5187 name: "myapex.key",
5188 public_key: "testkey.avbpubkey",
5189 private_key: "testkey.pem",
5190 }
5191
5192 android_test_helper_app {
5193 name: "TesterHelpAppFoo",
5194 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005195 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005196 }
5197
5198 `)
5199
5200 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5201 apexRule := module.Rule("apexRule")
5202 copyCmds := apexRule.Args["copy_commands"]
5203
5204 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5205}
5206
Jooyung Han18020ea2019-11-13 10:50:48 +09005207func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5208 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005209 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005210 apex {
5211 name: "myapex",
5212 key: "myapex.key",
5213 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005214 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005215 }
5216
5217 apex_key {
5218 name: "myapex.key",
5219 public_key: "testkey.avbpubkey",
5220 private_key: "testkey.pem",
5221 }
5222
5223 apex {
5224 name: "otherapex",
5225 key: "myapex.key",
5226 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005227 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005228 }
5229
5230 cc_defaults {
5231 name: "libfoo-defaults",
5232 apex_available: ["otherapex"],
5233 }
5234
5235 cc_library {
5236 name: "libfoo",
5237 defaults: ["libfoo-defaults"],
5238 stl: "none",
5239 system_shared_libs: [],
5240 }`)
5241}
5242
Paul Duffine52e66f2020-03-30 17:54:29 +01005243func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005244 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005245 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005246 apex {
5247 name: "myapex",
5248 key: "myapex.key",
5249 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005250 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005251 }
5252
5253 apex_key {
5254 name: "myapex.key",
5255 public_key: "testkey.avbpubkey",
5256 private_key: "testkey.pem",
5257 }
5258
5259 apex {
5260 name: "otherapex",
5261 key: "otherapex.key",
5262 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005263 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005264 }
5265
5266 apex_key {
5267 name: "otherapex.key",
5268 public_key: "testkey.avbpubkey",
5269 private_key: "testkey.pem",
5270 }
5271
5272 cc_library {
5273 name: "libfoo",
5274 stl: "none",
5275 system_shared_libs: [],
5276 apex_available: ["otherapex"],
5277 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005278}
Jiyong Park127b40b2019-09-30 16:04:35 +09005279
Paul Duffine52e66f2020-03-30 17:54:29 +01005280func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005281 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005282 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005283.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005284.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005285.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005286.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005287.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005288.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005289 apex {
5290 name: "myapex",
5291 key: "myapex.key",
5292 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005293 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005294 }
5295
5296 apex_key {
5297 name: "myapex.key",
5298 public_key: "testkey.avbpubkey",
5299 private_key: "testkey.pem",
5300 }
5301
Jiyong Park127b40b2019-09-30 16:04:35 +09005302 cc_library {
5303 name: "libfoo",
5304 stl: "none",
5305 shared_libs: ["libbar"],
5306 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005307 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005308 }
5309
5310 cc_library {
5311 name: "libbar",
5312 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005313 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005314 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005315 apex_available: ["myapex"],
5316 }
5317
5318 cc_library {
5319 name: "libbaz",
5320 stl: "none",
5321 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005322 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005323}
Jiyong Park127b40b2019-09-30 16:04:35 +09005324
Paul Duffine52e66f2020-03-30 17:54:29 +01005325func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005326 testApexError(t, "\"otherapex\" is not a valid module name", `
5327 apex {
5328 name: "myapex",
5329 key: "myapex.key",
5330 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005331 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005332 }
5333
5334 apex_key {
5335 name: "myapex.key",
5336 public_key: "testkey.avbpubkey",
5337 private_key: "testkey.pem",
5338 }
5339
5340 cc_library {
5341 name: "libfoo",
5342 stl: "none",
5343 system_shared_libs: [],
5344 apex_available: ["otherapex"],
5345 }`)
5346
Paul Duffine52e66f2020-03-30 17:54:29 +01005347 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005348 apex {
5349 name: "myapex",
5350 key: "myapex.key",
5351 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005352 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005353 }
5354
5355 apex_key {
5356 name: "myapex.key",
5357 public_key: "testkey.avbpubkey",
5358 private_key: "testkey.pem",
5359 }
5360
5361 cc_library {
5362 name: "libfoo",
5363 stl: "none",
5364 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005365 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005366 apex_available: ["myapex"],
5367 }
5368
5369 cc_library {
5370 name: "libbar",
5371 stl: "none",
5372 system_shared_libs: [],
5373 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005374 }
5375
5376 cc_library {
5377 name: "libbaz",
5378 stl: "none",
5379 system_shared_libs: [],
5380 stubs: {
5381 versions: ["10", "20", "30"],
5382 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005383 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005384}
Jiyong Park127b40b2019-09-30 16:04:35 +09005385
Jiyong Park89e850a2020-04-07 16:37:39 +09005386func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005387 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005388 apex {
5389 name: "myapex",
5390 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005391 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005392 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005393 }
5394
5395 apex_key {
5396 name: "myapex.key",
5397 public_key: "testkey.avbpubkey",
5398 private_key: "testkey.pem",
5399 }
5400
5401 cc_library {
5402 name: "libfoo",
5403 stl: "none",
5404 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005405 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005406 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005407 }
5408
5409 cc_library {
5410 name: "libfoo2",
5411 stl: "none",
5412 system_shared_libs: [],
5413 shared_libs: ["libbaz"],
5414 apex_available: ["//apex_available:platform"],
5415 }
5416
5417 cc_library {
5418 name: "libbar",
5419 stl: "none",
5420 system_shared_libs: [],
5421 apex_available: ["myapex"],
5422 }
5423
5424 cc_library {
5425 name: "libbaz",
5426 stl: "none",
5427 system_shared_libs: [],
5428 apex_available: ["myapex"],
5429 stubs: {
5430 versions: ["1"],
5431 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005432 }`)
5433
Jiyong Park89e850a2020-04-07 16:37:39 +09005434 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5435 // because it depends on libbar which isn't available to platform
5436 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5437 if libfoo.NotAvailableForPlatform() != true {
5438 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5439 }
5440
5441 // libfoo2 however can be available to platform because it depends on libbaz which provides
5442 // stubs
5443 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5444 if libfoo2.NotAvailableForPlatform() == true {
5445 t.Errorf("%q should be available to platform", libfoo2.String())
5446 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005447}
Jiyong Parka90ca002019-10-07 15:47:24 +09005448
Paul Duffine52e66f2020-03-30 17:54:29 +01005449func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005450 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005451 apex {
5452 name: "myapex",
5453 key: "myapex.key",
5454 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005455 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005456 }
5457
5458 apex_key {
5459 name: "myapex.key",
5460 public_key: "testkey.avbpubkey",
5461 private_key: "testkey.pem",
5462 }
5463
5464 cc_library {
5465 name: "libfoo",
5466 stl: "none",
5467 system_shared_libs: [],
5468 apex_available: ["myapex"],
5469 static: {
5470 apex_available: ["//apex_available:platform"],
5471 },
5472 }`)
5473
Jiyong Park89e850a2020-04-07 16:37:39 +09005474 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5475 if libfooShared.NotAvailableForPlatform() != true {
5476 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5477 }
5478 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5479 if libfooStatic.NotAvailableForPlatform() != false {
5480 t.Errorf("%q should be available to platform", libfooStatic.String())
5481 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005482}
5483
Jiyong Park5d790c32019-11-15 18:40:32 +09005484func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005485 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005486 apex {
5487 name: "myapex",
5488 key: "myapex.key",
5489 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005490 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005491 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005492 }
5493
5494 override_apex {
5495 name: "override_myapex",
5496 base: "myapex",
5497 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005498 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005499 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005500 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07005501 key: "mynewapex.key",
5502 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09005503 }
5504
5505 apex_key {
5506 name: "myapex.key",
5507 public_key: "testkey.avbpubkey",
5508 private_key: "testkey.pem",
5509 }
5510
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07005511 apex_key {
5512 name: "mynewapex.key",
5513 public_key: "testkey2.avbpubkey",
5514 private_key: "testkey2.pem",
5515 }
5516
5517 android_app_certificate {
5518 name: "myapex.certificate",
5519 certificate: "testkey",
5520 }
5521
Jiyong Park5d790c32019-11-15 18:40:32 +09005522 android_app {
5523 name: "app",
5524 srcs: ["foo/bar/MyClass.java"],
5525 package_name: "foo",
5526 sdk_version: "none",
5527 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005528 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005529 }
5530
5531 override_android_app {
5532 name: "override_app",
5533 base: "app",
5534 package_name: "bar",
5535 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005536 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005537
Jiyong Park317645e2019-12-05 13:20:58 +09005538 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5539 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5540 if originalVariant.GetOverriddenBy() != "" {
5541 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5542 }
5543 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5544 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5545 }
5546
Jiyong Park5d790c32019-11-15 18:40:32 +09005547 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5548 apexRule := module.Rule("apexRule")
5549 copyCmds := apexRule.Args["copy_commands"]
5550
5551 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005552 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005553
5554 apexBundle := module.Module().(*apexBundle)
5555 name := apexBundle.Name()
5556 if name != "override_myapex" {
5557 t.Errorf("name should be \"override_myapex\", but was %q", name)
5558 }
5559
Baligh Uddin004d7172020-02-19 21:29:28 -08005560 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5561 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5562 }
5563
Jiyong Park20bacab2020-03-03 11:45:41 +09005564 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005565 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07005566 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
5567
5568 signApkRule := module.Rule("signapk")
5569 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09005570
Colin Crossaa255532020-07-03 13:18:24 -07005571 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005572 var builder strings.Builder
5573 data.Custom(&builder, name, "TARGET_", "", data)
5574 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005575 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005576 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5577 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005578 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005579 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005580 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005581 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5582 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005583}
5584
Jooyung Han214bf372019-11-12 13:03:50 +09005585func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005586 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005587 apex {
5588 name: "myapex",
5589 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005590 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005591 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005592 }
5593
5594 apex_key {
5595 name: "myapex.key",
5596 public_key: "testkey.avbpubkey",
5597 private_key: "testkey.pem",
5598 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005599
5600 cc_library {
5601 name: "mylib",
5602 srcs: ["mylib.cpp"],
5603 stl: "libc++",
5604 system_shared_libs: [],
5605 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005606 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005607 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005608 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005609
5610 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5611 args := module.Rule("apexRule").Args
5612 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005613 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005614
5615 // The copies of the libraries in the apex should have one more dependency than
5616 // the ones outside the apex, namely the unwinder. Ideally we should check
5617 // the dependency names directly here but for some reason the names are blank in
5618 // this test.
5619 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005620 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005621 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5622 if len(apexImplicits) != len(nonApexImplicits)+1 {
5623 t.Errorf("%q missing unwinder dep", lib)
5624 }
5625 }
Jooyung Han214bf372019-11-12 13:03:50 +09005626}
5627
Paul Duffine05480a2021-03-08 15:07:14 +00005628var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01005629 "api/current.txt": nil,
5630 "api/removed.txt": nil,
5631 "api/system-current.txt": nil,
5632 "api/system-removed.txt": nil,
5633 "api/test-current.txt": nil,
5634 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005635
Anton Hanssondff2c782020-12-21 17:10:01 +00005636 "100/public/api/foo.txt": nil,
5637 "100/public/api/foo-removed.txt": nil,
5638 "100/system/api/foo.txt": nil,
5639 "100/system/api/foo-removed.txt": nil,
5640
Paul Duffineedc5d52020-06-12 17:46:39 +01005641 // For java_sdk_library_import
5642 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005643}
5644
Jooyung Han58f26ab2019-12-18 15:34:32 +09005645func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005646 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09005647 apex {
5648 name: "myapex",
5649 key: "myapex.key",
5650 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005651 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09005652 }
5653
5654 apex_key {
5655 name: "myapex.key",
5656 public_key: "testkey.avbpubkey",
5657 private_key: "testkey.pem",
5658 }
5659
5660 java_sdk_library {
5661 name: "foo",
5662 srcs: ["a.java"],
5663 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005664 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005665 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005666
5667 prebuilt_apis {
5668 name: "sdk",
5669 api_dirs: ["100"],
5670 }
Paul Duffin9b879592020-05-26 13:21:35 +01005671 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005672
5673 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005674 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005675 "javalib/foo.jar",
5676 "etc/permissions/foo.xml",
5677 })
5678 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005679 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5680 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005681}
5682
Paul Duffin9b879592020-05-26 13:21:35 +01005683func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005684 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005685 apex {
5686 name: "myapex",
5687 key: "myapex.key",
5688 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005689 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005690 }
5691
5692 apex_key {
5693 name: "myapex.key",
5694 public_key: "testkey.avbpubkey",
5695 private_key: "testkey.pem",
5696 }
5697
5698 java_sdk_library {
5699 name: "foo",
5700 srcs: ["a.java"],
5701 api_packages: ["foo"],
5702 apex_available: ["myapex"],
5703 sdk_version: "none",
5704 system_modules: "none",
5705 }
5706
5707 java_library {
5708 name: "bar",
5709 srcs: ["a.java"],
5710 libs: ["foo"],
5711 apex_available: ["myapex"],
5712 sdk_version: "none",
5713 system_modules: "none",
5714 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005715
5716 prebuilt_apis {
5717 name: "sdk",
5718 api_dirs: ["100"],
5719 }
Paul Duffin9b879592020-05-26 13:21:35 +01005720 `, withFiles(filesForSdkLibrary))
5721
5722 // java_sdk_library installs both impl jar and permission XML
5723 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5724 "javalib/bar.jar",
5725 "javalib/foo.jar",
5726 "etc/permissions/foo.xml",
5727 })
5728
5729 // The bar library should depend on the implementation jar.
5730 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005731 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005732 t.Errorf("expected %q, found %#q", expected, actual)
5733 }
5734}
5735
5736func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005737 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005738 apex {
5739 name: "myapex",
5740 key: "myapex.key",
5741 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005742 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005743 }
5744
5745 apex_key {
5746 name: "myapex.key",
5747 public_key: "testkey.avbpubkey",
5748 private_key: "testkey.pem",
5749 }
5750
5751 java_sdk_library {
5752 name: "foo",
5753 srcs: ["a.java"],
5754 api_packages: ["foo"],
5755 apex_available: ["myapex"],
5756 sdk_version: "none",
5757 system_modules: "none",
5758 }
5759
5760 java_library {
5761 name: "bar",
5762 srcs: ["a.java"],
5763 libs: ["foo"],
5764 sdk_version: "none",
5765 system_modules: "none",
5766 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005767
5768 prebuilt_apis {
5769 name: "sdk",
5770 api_dirs: ["100"],
5771 }
Paul Duffin9b879592020-05-26 13:21:35 +01005772 `, withFiles(filesForSdkLibrary))
5773
5774 // java_sdk_library installs both impl jar and permission XML
5775 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5776 "javalib/foo.jar",
5777 "etc/permissions/foo.xml",
5778 })
5779
5780 // The bar library should depend on the stubs jar.
5781 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005782 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005783 t.Errorf("expected %q, found %#q", expected, actual)
5784 }
5785}
5786
Paul Duffineedc5d52020-06-12 17:46:39 +01005787func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005788 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00005789 prebuilt_apis {
5790 name: "sdk",
5791 api_dirs: ["100"],
5792 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005793 withFiles(map[string][]byte{
5794 "apex/a.java": nil,
5795 "apex/apex_manifest.json": nil,
5796 "apex/Android.bp": []byte(`
5797 package {
5798 default_visibility: ["//visibility:private"],
5799 }
5800
5801 apex {
5802 name: "myapex",
5803 key: "myapex.key",
5804 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005805 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005806 }
5807
5808 apex_key {
5809 name: "myapex.key",
5810 public_key: "testkey.avbpubkey",
5811 private_key: "testkey.pem",
5812 }
5813
5814 java_library {
5815 name: "bar",
5816 srcs: ["a.java"],
5817 libs: ["foo"],
5818 apex_available: ["myapex"],
5819 sdk_version: "none",
5820 system_modules: "none",
5821 }
5822`),
5823 "source/a.java": nil,
5824 "source/api/current.txt": nil,
5825 "source/api/removed.txt": nil,
5826 "source/Android.bp": []byte(`
5827 package {
5828 default_visibility: ["//visibility:private"],
5829 }
5830
5831 java_sdk_library {
5832 name: "foo",
5833 visibility: ["//apex"],
5834 srcs: ["a.java"],
5835 api_packages: ["foo"],
5836 apex_available: ["myapex"],
5837 sdk_version: "none",
5838 system_modules: "none",
5839 public: {
5840 enabled: true,
5841 },
5842 }
5843`),
5844 "prebuilt/a.jar": nil,
5845 "prebuilt/Android.bp": []byte(`
5846 package {
5847 default_visibility: ["//visibility:private"],
5848 }
5849
5850 java_sdk_library_import {
5851 name: "foo",
5852 visibility: ["//apex", "//source"],
5853 apex_available: ["myapex"],
5854 prefer: true,
5855 public: {
5856 jars: ["a.jar"],
5857 },
5858 }
5859`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005860 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005861 )
5862
5863 // java_sdk_library installs both impl jar and permission XML
5864 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5865 "javalib/bar.jar",
5866 "javalib/foo.jar",
5867 "etc/permissions/foo.xml",
5868 })
5869
5870 // The bar library should depend on the implementation jar.
5871 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005872 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01005873 t.Errorf("expected %q, found %#q", expected, actual)
5874 }
5875}
5876
5877func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5878 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5879 apex {
5880 name: "myapex",
5881 key: "myapex.key",
5882 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005883 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005884 }
5885
5886 apex_key {
5887 name: "myapex.key",
5888 public_key: "testkey.avbpubkey",
5889 private_key: "testkey.pem",
5890 }
5891
5892 java_sdk_library_import {
5893 name: "foo",
5894 apex_available: ["myapex"],
5895 prefer: true,
5896 public: {
5897 jars: ["a.jar"],
5898 },
5899 }
5900
5901 `, withFiles(filesForSdkLibrary))
5902}
5903
atrost6e126252020-01-27 17:01:16 +00005904func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01005905 result := android.GroupFixturePreparers(
5906 prepareForApexTest,
5907 java.PrepareForTestWithPlatformCompatConfig,
5908 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00005909 apex {
5910 name: "myapex",
5911 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00005912 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00005913 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005914 updatable: false,
atrost6e126252020-01-27 17:01:16 +00005915 }
5916
5917 apex_key {
5918 name: "myapex.key",
5919 public_key: "testkey.avbpubkey",
5920 private_key: "testkey.pem",
5921 }
5922
5923 platform_compat_config {
5924 name: "myjar-platform-compat-config",
5925 src: ":myjar",
5926 }
5927
5928 java_library {
5929 name: "myjar",
5930 srcs: ["foo/bar/MyClass.java"],
5931 sdk_version: "none",
5932 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005933 apex_available: [ "myapex" ],
5934 }
Paul Duffin1b29e002021-03-16 15:06:54 +00005935
5936 // Make sure that a preferred prebuilt does not affect the apex contents.
5937 prebuilt_platform_compat_config {
5938 name: "myjar-platform-compat-config",
5939 metadata: "compat-config/metadata.xml",
5940 prefer: true,
5941 }
atrost6e126252020-01-27 17:01:16 +00005942 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00005943 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00005944 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5945 "etc/compatconfig/myjar-platform-compat-config.xml",
5946 "javalib/myjar.jar",
5947 })
5948}
5949
Jiyong Park479321d2019-12-16 11:47:12 +09005950func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5951 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5952 apex {
5953 name: "myapex",
5954 key: "myapex.key",
5955 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005956 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09005957 }
5958
5959 apex_key {
5960 name: "myapex.key",
5961 public_key: "testkey.avbpubkey",
5962 private_key: "testkey.pem",
5963 }
5964
5965 java_library {
5966 name: "myjar",
5967 srcs: ["foo/bar/MyClass.java"],
5968 sdk_version: "none",
5969 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09005970 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005971 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09005972 }
5973 `)
5974}
5975
Jiyong Park7afd1072019-12-30 16:56:33 +09005976func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005977 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09005978 apex {
5979 name: "myapex",
5980 key: "myapex.key",
5981 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005982 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09005983 }
5984
5985 apex_key {
5986 name: "myapex.key",
5987 public_key: "testkey.avbpubkey",
5988 private_key: "testkey.pem",
5989 }
5990
5991 cc_library {
5992 name: "mylib",
5993 srcs: ["mylib.cpp"],
5994 system_shared_libs: [],
5995 stl: "none",
5996 required: ["a", "b"],
5997 host_required: ["c", "d"],
5998 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005999 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006000 }
6001 `)
6002
6003 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006004 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006005 name := apexBundle.BaseModuleName()
6006 prefix := "TARGET_"
6007 var builder strings.Builder
6008 data.Custom(&builder, name, prefix, "", data)
6009 androidMk := builder.String()
6010 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6011 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6012 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6013}
6014
Jiyong Park7cd10e32020-01-14 09:22:18 +09006015func TestSymlinksFromApexToSystem(t *testing.T) {
6016 bp := `
6017 apex {
6018 name: "myapex",
6019 key: "myapex.key",
6020 native_shared_libs: ["mylib"],
6021 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006022 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006023 }
6024
Jiyong Park9d677202020-02-19 16:29:35 +09006025 apex {
6026 name: "myapex.updatable",
6027 key: "myapex.key",
6028 native_shared_libs: ["mylib"],
6029 java_libs: ["myjar"],
6030 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006031 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006032 }
6033
Jiyong Park7cd10e32020-01-14 09:22:18 +09006034 apex_key {
6035 name: "myapex.key",
6036 public_key: "testkey.avbpubkey",
6037 private_key: "testkey.pem",
6038 }
6039
6040 cc_library {
6041 name: "mylib",
6042 srcs: ["mylib.cpp"],
6043 shared_libs: ["myotherlib"],
6044 system_shared_libs: [],
6045 stl: "none",
6046 apex_available: [
6047 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006048 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006049 "//apex_available:platform",
6050 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006051 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006052 }
6053
6054 cc_library {
6055 name: "myotherlib",
6056 srcs: ["mylib.cpp"],
6057 system_shared_libs: [],
6058 stl: "none",
6059 apex_available: [
6060 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006061 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006062 "//apex_available:platform",
6063 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006064 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006065 }
6066
6067 java_library {
6068 name: "myjar",
6069 srcs: ["foo/bar/MyClass.java"],
6070 sdk_version: "none",
6071 system_modules: "none",
6072 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006073 apex_available: [
6074 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006075 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006076 "//apex_available:platform",
6077 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006078 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006079 }
6080
6081 java_library {
6082 name: "myotherjar",
6083 srcs: ["foo/bar/MyClass.java"],
6084 sdk_version: "none",
6085 system_modules: "none",
6086 apex_available: [
6087 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006088 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006089 "//apex_available:platform",
6090 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006091 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006092 }
6093 `
6094
6095 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6096 for _, f := range files {
6097 if f.path == file {
6098 if f.isLink {
6099 t.Errorf("%q is not a real file", file)
6100 }
6101 return
6102 }
6103 }
6104 t.Errorf("%q is not found", file)
6105 }
6106
6107 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6108 for _, f := range files {
6109 if f.path == file {
6110 if !f.isLink {
6111 t.Errorf("%q is not a symlink", file)
6112 }
6113 return
6114 }
6115 }
6116 t.Errorf("%q is not found", file)
6117 }
6118
Jiyong Park9d677202020-02-19 16:29:35 +09006119 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6120 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006121 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006122 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006123 ensureRealfileExists(t, files, "javalib/myjar.jar")
6124 ensureRealfileExists(t, files, "lib64/mylib.so")
6125 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6126
Jiyong Park9d677202020-02-19 16:29:35 +09006127 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6128 ensureRealfileExists(t, files, "javalib/myjar.jar")
6129 ensureRealfileExists(t, files, "lib64/mylib.so")
6130 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6131
6132 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006133 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006134 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006135 ensureRealfileExists(t, files, "javalib/myjar.jar")
6136 ensureRealfileExists(t, files, "lib64/mylib.so")
6137 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006138
6139 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6140 ensureRealfileExists(t, files, "javalib/myjar.jar")
6141 ensureRealfileExists(t, files, "lib64/mylib.so")
6142 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006143}
6144
Yo Chiange8128052020-07-23 20:09:18 +08006145func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006146 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006147 apex {
6148 name: "myapex",
6149 key: "myapex.key",
6150 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006151 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006152 }
6153
6154 apex_key {
6155 name: "myapex.key",
6156 public_key: "testkey.avbpubkey",
6157 private_key: "testkey.pem",
6158 }
6159
6160 cc_library_shared {
6161 name: "mylib",
6162 srcs: ["mylib.cpp"],
6163 shared_libs: ["myotherlib"],
6164 system_shared_libs: [],
6165 stl: "none",
6166 apex_available: [
6167 "myapex",
6168 "//apex_available:platform",
6169 ],
6170 }
6171
6172 cc_prebuilt_library_shared {
6173 name: "myotherlib",
6174 srcs: ["prebuilt.so"],
6175 system_shared_libs: [],
6176 stl: "none",
6177 apex_available: [
6178 "myapex",
6179 "//apex_available:platform",
6180 ],
6181 }
6182 `)
6183
6184 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006185 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006186 var builder strings.Builder
6187 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6188 androidMk := builder.String()
6189 // `myotherlib` is added to `myapex` as symlink
6190 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6191 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6192 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6193 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006194 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 +08006195}
6196
Jooyung Han643adc42020-02-27 13:50:06 +09006197func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006198 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006199 apex {
6200 name: "myapex",
6201 key: "myapex.key",
6202 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006203 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006204 }
6205
6206 apex_key {
6207 name: "myapex.key",
6208 public_key: "testkey.avbpubkey",
6209 private_key: "testkey.pem",
6210 }
6211
6212 cc_library {
6213 name: "mylib",
6214 srcs: ["mylib.cpp"],
6215 shared_libs: ["mylib2"],
6216 system_shared_libs: [],
6217 stl: "none",
6218 apex_available: [ "myapex" ],
6219 }
6220
6221 cc_library {
6222 name: "mylib2",
6223 srcs: ["mylib.cpp"],
6224 system_shared_libs: [],
6225 stl: "none",
6226 apex_available: [ "myapex" ],
6227 }
6228 `)
6229
6230 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6231 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6232 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6233 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6234 "lib64/mylib.so",
6235 "lib64/mylib2.so",
6236 })
6237}
6238
Jooyung Han49f67012020-04-17 13:43:10 +09006239func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006240 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006241 apex {
6242 name: "myapex",
6243 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006244 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006245 }
6246 apex_key {
6247 name: "myapex.key",
6248 public_key: "testkey.avbpubkey",
6249 private_key: "testkey.pem",
6250 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006251 `,
6252 android.FixtureModifyConfig(func(config android.Config) {
6253 delete(config.Targets, android.Android)
6254 config.AndroidCommonTarget = android.Target{}
6255 }),
6256 )
Jooyung Han49f67012020-04-17 13:43:10 +09006257
6258 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6259 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6260 }
6261}
6262
Jiyong Parkbd159612020-02-28 15:22:21 +09006263func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006264 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006265 apex {
6266 name: "myapex",
6267 key: "myapex.key",
6268 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006269 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006270 }
6271
6272 apex_key {
6273 name: "myapex.key",
6274 public_key: "testkey.avbpubkey",
6275 private_key: "testkey.pem",
6276 }
6277
6278 android_app {
6279 name: "AppFoo",
6280 srcs: ["foo/bar/MyClass.java"],
6281 sdk_version: "none",
6282 system_modules: "none",
6283 apex_available: [ "myapex" ],
6284 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006285 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006286
Colin Crosscf371cc2020-11-13 11:48:42 -08006287 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006288 content := bundleConfigRule.Args["content"]
6289
6290 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006291 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 +09006292}
6293
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006294func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006295 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006296 apex {
6297 name: "myapex",
6298 key: "myapex.key",
6299 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006300 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006301 }
6302
6303 apex_key {
6304 name: "myapex.key",
6305 public_key: "testkey.avbpubkey",
6306 private_key: "testkey.pem",
6307 }
6308
6309 android_app_set {
6310 name: "AppSet",
6311 set: "AppSet.apks",
6312 }`)
6313 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006314 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006315 content := bundleConfigRule.Args["content"]
6316 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6317 s := mod.Rule("apexRule").Args["copy_commands"]
6318 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6319 if len(copyCmds) != 3 {
6320 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6321 }
6322 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6323 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6324 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6325}
6326
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006327func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01006328 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006329 apex_set {
6330 name: "myapex",
6331 filename: "foo_v2.apex",
6332 sanitized: {
6333 none: { set: "myapex.apks", },
6334 hwaddress: { set: "myapex.hwasan.apks", },
6335 },
Paul Duffin24704672021-04-06 16:09:30 +01006336 }
6337 `
6338 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006339
Paul Duffin24704672021-04-06 16:09:30 +01006340 // Check that the extractor produces the correct output file from the correct input file.
6341 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006342
Paul Duffin24704672021-04-06 16:09:30 +01006343 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
6344 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006345
Paul Duffin24704672021-04-06 16:09:30 +01006346 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
6347
6348 // Ditto for the apex.
Paul Duffin5ec165d2021-06-15 19:09:41 +01006349 m = ctx.ModuleForTests("myapex", "android_common_myapex")
6350 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01006351
6352 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006353}
6354
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006355func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006356 t.Helper()
6357
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006358 bp := `
6359 java_library {
6360 name: "some-updatable-apex-lib",
6361 srcs: ["a.java"],
6362 sdk_version: "current",
6363 apex_available: [
6364 "some-updatable-apex",
6365 ],
6366 }
6367
6368 java_library {
6369 name: "some-non-updatable-apex-lib",
6370 srcs: ["a.java"],
6371 apex_available: [
6372 "some-non-updatable-apex",
6373 ],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006374 compile_dex: true,
6375 }
6376
6377 bootclasspath_fragment {
6378 name: "some-non-updatable-fragment",
6379 contents: ["some-non-updatable-apex-lib"],
6380 apex_available: [
6381 "some-non-updatable-apex",
6382 ],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006383 }
6384
6385 java_library {
6386 name: "some-platform-lib",
6387 srcs: ["a.java"],
6388 sdk_version: "current",
6389 installable: true,
6390 }
6391
6392 java_library {
6393 name: "some-art-lib",
6394 srcs: ["a.java"],
6395 sdk_version: "current",
6396 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006397 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006398 ],
6399 hostdex: true,
Paul Duffinc75bbce2021-06-07 13:28:19 +01006400 compile_dex: true,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006401 }
6402
6403 apex {
6404 name: "some-updatable-apex",
6405 key: "some-updatable-apex.key",
6406 java_libs: ["some-updatable-apex-lib"],
6407 updatable: true,
6408 min_sdk_version: "current",
6409 }
6410
6411 apex {
6412 name: "some-non-updatable-apex",
6413 key: "some-non-updatable-apex.key",
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006414 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006415 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006416 }
6417
6418 apex_key {
6419 name: "some-updatable-apex.key",
6420 }
6421
6422 apex_key {
6423 name: "some-non-updatable-apex.key",
6424 }
6425
6426 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006427 name: "com.android.art.debug",
6428 key: "com.android.art.debug.key",
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006429 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006430 updatable: true,
6431 min_sdk_version: "current",
6432 }
6433
Paul Duffinf23bc472021-04-27 12:42:20 +01006434 bootclasspath_fragment {
6435 name: "art-bootclasspath-fragment",
6436 image_name: "art",
6437 contents: ["some-art-lib"],
6438 apex_available: [
6439 "com.android.art.debug",
6440 ],
6441 }
6442
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006443 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006444 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006445 }
6446
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006447 filegroup {
6448 name: "some-updatable-apex-file_contexts",
6449 srcs: [
6450 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6451 ],
6452 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006453
6454 filegroup {
6455 name: "some-non-updatable-apex-file_contexts",
6456 srcs: [
6457 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6458 ],
6459 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006460 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006461
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006462 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00006463}
6464
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006465func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006466 t.Helper()
6467
Paul Duffin55607122021-03-30 23:32:51 +01006468 fs := android.MockFS{
6469 "a.java": nil,
6470 "a.jar": nil,
6471 "apex_manifest.json": nil,
6472 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006473 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006474 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6475 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6476 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006477 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006478 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006479
Paul Duffin55607122021-03-30 23:32:51 +01006480 errorHandler := android.FixtureExpectsNoErrors
6481 if errmsg != "" {
6482 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006483 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006484
Paul Duffin55607122021-03-30 23:32:51 +01006485 result := android.GroupFixturePreparers(
6486 cc.PrepareForTestWithCcDefaultModules,
6487 java.PrepareForTestWithHiddenApiBuildComponents,
6488 java.PrepareForTestWithJavaDefaultModules,
6489 java.PrepareForTestWithJavaSdkLibraryFiles,
6490 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01006491 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01006492 fs.AddToFixture(),
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006493 android.FixtureModifyMockFS(func(fs android.MockFS) {
6494 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
6495 insert := ""
6496 for _, fragment := range fragments {
6497 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
6498 }
6499 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
6500 platform_bootclasspath {
6501 name: "platform-bootclasspath",
6502 fragments: [
6503 %s
6504 ],
6505 }
6506 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01006507 }
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006508 }),
Paul Duffin55607122021-03-30 23:32:51 +01006509 ).
6510 ExtendWithErrorHandler(errorHandler).
6511 RunTestWithBp(t, bp)
6512
6513 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006514}
6515
Jooyung Han548640b2020-04-27 12:10:30 +09006516func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6517 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6518 apex {
6519 name: "myapex",
6520 key: "myapex.key",
6521 updatable: true,
6522 }
6523
6524 apex_key {
6525 name: "myapex.key",
6526 public_key: "testkey.avbpubkey",
6527 private_key: "testkey.pem",
6528 }
6529 `)
6530}
6531
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006532func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6533 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6534 apex {
6535 name: "myapex",
6536 key: "myapex.key",
6537 }
6538
6539 apex_key {
6540 name: "myapex.key",
6541 public_key: "testkey.avbpubkey",
6542 private_key: "testkey.pem",
6543 }
6544 `)
6545}
6546
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006547func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006548 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
6549 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
6550 // modules to be included in the BootJars.
6551 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
6552 return android.GroupFixturePreparers(
6553 dexpreopt.FixtureSetBootJars(bootJars...),
6554 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6555 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
6556 }),
6557 )
6558 }
6559
6560 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
6561 // same value. This can result in an invalid configuration as it allows non art apex jars to be
6562 // specified in the ArtApexJars configuration.
6563 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
6564 return android.GroupFixturePreparers(
6565 dexpreopt.FixtureSetArtBootJars(bootJars...),
6566 dexpreopt.FixtureSetBootJars(bootJars...),
6567 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6568 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
6569 }),
6570 )
6571 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006572
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006573 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006574 preparer := java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib")
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006575 fragment := java.ApexVariantReference{
6576 Apex: proptools.StringPtr("com.android.art.debug"),
6577 Module: proptools.StringPtr("art-bootclasspath-fragment"),
6578 }
6579 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006580 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006581
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006582 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006583 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
6584 // Update the dexpreopt BootJars directly.
6585 preparer := prepareSetBootJars("com.android.art.debug:some-art-lib")
6586 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006587 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006588
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006589 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 +01006590 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 +01006591 // Update the dexpreopt ArtApexJars directly.
6592 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
6593 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006594 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006595
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006596 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 +01006597 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 +01006598 // Update the dexpreopt ArtApexJars directly.
6599 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
6600 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006601 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006602
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006603 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 +01006604 err := `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the framework boot image`
6605 preparer := java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib")
6606 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006607 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006608
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006609 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 +01006610 preparer := java.FixtureConfigureBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006611 fragment := java.ApexVariantReference{
6612 Apex: proptools.StringPtr("some-non-updatable-apex"),
6613 Module: proptools.StringPtr("some-non-updatable-fragment"),
6614 }
6615 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006616 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006617
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006618 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01006619 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01006620 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
6621 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006622 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006623
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006624 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01006625 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01006626 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
6627 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006628 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006629
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006630 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01006631 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01006632 // Update the dexpreopt ArtApexJars directly.
6633 preparer := prepareSetArtJars("platform:some-platform-lib")
6634 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006635 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006636
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006637 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006638 preparer := java.FixtureConfigureBootJars("platform:some-platform-lib")
6639 testNoUpdatableJarsInBootImage(t, "", preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006640 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006641}
6642
6643func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006644 preparer := java.FixtureConfigureBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00006645 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006646 fragment := java.ApexVariantReference{
6647 Apex: proptools.StringPtr("myapex"),
6648 Module: proptools.StringPtr("my-bootclasspath-fragment"),
6649 }
6650
Paul Duffin064b70c2020-11-02 17:32:38 +00006651 testDexpreoptWithApexes(t, `
6652 prebuilt_apex {
6653 name: "myapex" ,
6654 arch: {
6655 arm64: {
6656 src: "myapex-arm64.apex",
6657 },
6658 arm: {
6659 src: "myapex-arm.apex",
6660 },
6661 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006662 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
6663 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006664
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006665 prebuilt_bootclasspath_fragment {
6666 name: "my-bootclasspath-fragment",
6667 contents: ["libfoo"],
6668 apex_available: ["myapex"],
6669 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006670
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006671 java_import {
6672 name: "libfoo",
6673 jars: ["libfoo.jar"],
6674 apex_available: ["myapex"],
6675 }
6676 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00006677 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006678}
6679
Andrei Onea115e7e72020-06-05 21:14:03 +01006680func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6681 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006682 bp += `
6683 apex_key {
6684 name: "myapex.key",
6685 public_key: "testkey.avbpubkey",
6686 private_key: "testkey.pem",
6687 }`
Paul Duffin45338f02021-03-30 23:07:52 +01006688 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01006689 "lib1/src/A.java": nil,
6690 "lib2/src/B.java": nil,
6691 "system/sepolicy/apex/myapex-file_contexts": nil,
6692 }
6693
Paul Duffin45338f02021-03-30 23:07:52 +01006694 errorHandler := android.FixtureExpectsNoErrors
6695 if errmsg != "" {
6696 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07006697 }
Colin Crossae8600b2020-10-29 17:09:13 -07006698
Paul Duffin45338f02021-03-30 23:07:52 +01006699 android.GroupFixturePreparers(
6700 android.PrepareForTestWithAndroidBuildComponents,
6701 java.PrepareForTestWithJavaBuildComponents,
6702 PrepareForTestWithApexBuildComponents,
6703 android.PrepareForTestWithNeverallowRules(rules),
6704 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6705 updatableBootJars := make([]string, 0, len(apexBootJars))
6706 for _, apexBootJar := range apexBootJars {
6707 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6708 }
6709 variables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6710 }),
6711 fs.AddToFixture(),
6712 ).
6713 ExtendWithErrorHandler(errorHandler).
6714 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01006715}
6716
6717func TestApexPermittedPackagesRules(t *testing.T) {
6718 testcases := []struct {
6719 name string
6720 expectedError string
6721 bp string
6722 bootJars []string
6723 modulesPackages map[string][]string
6724 }{
6725
6726 {
6727 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6728 expectedError: "",
6729 bp: `
6730 java_library {
6731 name: "bcp_lib1",
6732 srcs: ["lib1/src/*.java"],
6733 permitted_packages: ["foo.bar"],
6734 apex_available: ["myapex"],
6735 sdk_version: "none",
6736 system_modules: "none",
6737 }
6738 java_library {
6739 name: "nonbcp_lib2",
6740 srcs: ["lib2/src/*.java"],
6741 apex_available: ["myapex"],
6742 permitted_packages: ["a.b"],
6743 sdk_version: "none",
6744 system_modules: "none",
6745 }
6746 apex {
6747 name: "myapex",
6748 key: "myapex.key",
6749 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006750 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006751 }`,
6752 bootJars: []string{"bcp_lib1"},
6753 modulesPackages: map[string][]string{
6754 "myapex": []string{
6755 "foo.bar",
6756 },
6757 },
6758 },
6759 {
6760 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6761 expectedError: `module "bcp_lib2" .* which is restricted because jars that are part of the myapex module may only allow these packages: foo.bar. Please jarjar or move code around.`,
6762 bp: `
6763 java_library {
6764 name: "bcp_lib1",
6765 srcs: ["lib1/src/*.java"],
6766 apex_available: ["myapex"],
6767 permitted_packages: ["foo.bar"],
6768 sdk_version: "none",
6769 system_modules: "none",
6770 }
6771 java_library {
6772 name: "bcp_lib2",
6773 srcs: ["lib2/src/*.java"],
6774 apex_available: ["myapex"],
6775 permitted_packages: ["foo.bar", "bar.baz"],
6776 sdk_version: "none",
6777 system_modules: "none",
6778 }
6779 apex {
6780 name: "myapex",
6781 key: "myapex.key",
6782 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006783 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006784 }
6785 `,
6786 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6787 modulesPackages: map[string][]string{
6788 "myapex": []string{
6789 "foo.bar",
6790 },
6791 },
6792 },
6793 }
6794 for _, tc := range testcases {
6795 t.Run(tc.name, func(t *testing.T) {
6796 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6797 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6798 })
6799 }
6800}
6801
Jiyong Park62304bb2020-04-13 16:19:48 +09006802func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006803 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09006804 apex {
6805 name: "myapex",
6806 key: "myapex.key",
6807 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006808 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09006809 }
6810
6811 apex_key {
6812 name: "myapex.key",
6813 public_key: "testkey.avbpubkey",
6814 private_key: "testkey.pem",
6815 }
6816
6817 cc_library {
6818 name: "mylib",
6819 srcs: ["mylib.cpp"],
6820 system_shared_libs: [],
6821 stl: "none",
6822 stubs: {
6823 versions: ["1"],
6824 },
6825 apex_available: ["myapex"],
6826 }
6827
6828 cc_library {
6829 name: "myprivlib",
6830 srcs: ["mylib.cpp"],
6831 system_shared_libs: [],
6832 stl: "none",
6833 apex_available: ["myapex"],
6834 }
6835
6836
6837 cc_test {
6838 name: "mytest",
6839 gtest: false,
6840 srcs: ["mylib.cpp"],
6841 system_shared_libs: [],
6842 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006843 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006844 test_for: ["myapex"]
6845 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006846
6847 cc_library {
6848 name: "mytestlib",
6849 srcs: ["mylib.cpp"],
6850 system_shared_libs: [],
6851 shared_libs: ["mylib", "myprivlib"],
6852 stl: "none",
6853 test_for: ["myapex"],
6854 }
6855
6856 cc_benchmark {
6857 name: "mybench",
6858 srcs: ["mylib.cpp"],
6859 system_shared_libs: [],
6860 shared_libs: ["mylib", "myprivlib"],
6861 stl: "none",
6862 test_for: ["myapex"],
6863 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006864 `)
6865
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006866 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01006867 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006868 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6869 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6870 }
6871
6872 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09006873 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006874 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6875 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6876 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6877}
Jiyong Park46a512f2020-12-04 18:02:13 +09006878
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006879func TestIndirectTestFor(t *testing.T) {
6880 ctx := testApex(t, `
6881 apex {
6882 name: "myapex",
6883 key: "myapex.key",
6884 native_shared_libs: ["mylib", "myprivlib"],
6885 updatable: false,
6886 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006887
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006888 apex_key {
6889 name: "myapex.key",
6890 public_key: "testkey.avbpubkey",
6891 private_key: "testkey.pem",
6892 }
6893
6894 cc_library {
6895 name: "mylib",
6896 srcs: ["mylib.cpp"],
6897 system_shared_libs: [],
6898 stl: "none",
6899 stubs: {
6900 versions: ["1"],
6901 },
6902 apex_available: ["myapex"],
6903 }
6904
6905 cc_library {
6906 name: "myprivlib",
6907 srcs: ["mylib.cpp"],
6908 system_shared_libs: [],
6909 stl: "none",
6910 shared_libs: ["mylib"],
6911 apex_available: ["myapex"],
6912 }
6913
6914 cc_library {
6915 name: "mytestlib",
6916 srcs: ["mylib.cpp"],
6917 system_shared_libs: [],
6918 shared_libs: ["myprivlib"],
6919 stl: "none",
6920 test_for: ["myapex"],
6921 }
6922 `)
6923
6924 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01006925 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006926 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6927 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6928 }
6929
6930 // The platform variant of mytestlib links to the platform variant of the
6931 // internal myprivlib.
6932 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
6933
6934 // The platform variant of myprivlib links to the platform variant of mylib
6935 // and bypasses its stubs.
6936 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 +09006937}
6938
Martin Stjernholmec009002021-03-27 15:18:31 +00006939func TestTestForForLibInOtherApex(t *testing.T) {
6940 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
6941 _ = testApex(t, `
6942 apex {
6943 name: "com.android.art",
6944 key: "myapex.key",
6945 native_shared_libs: ["mylib"],
6946 updatable: false,
6947 }
6948
6949 apex {
6950 name: "com.android.art.debug",
6951 key: "myapex.key",
6952 native_shared_libs: ["mylib", "mytestlib"],
6953 updatable: false,
6954 }
6955
6956 apex_key {
6957 name: "myapex.key",
6958 public_key: "testkey.avbpubkey",
6959 private_key: "testkey.pem",
6960 }
6961
6962 cc_library {
6963 name: "mylib",
6964 srcs: ["mylib.cpp"],
6965 system_shared_libs: [],
6966 stl: "none",
6967 stubs: {
6968 versions: ["1"],
6969 },
6970 apex_available: ["com.android.art", "com.android.art.debug"],
6971 }
6972
6973 cc_library {
6974 name: "mytestlib",
6975 srcs: ["mylib.cpp"],
6976 system_shared_libs: [],
6977 shared_libs: ["mylib"],
6978 stl: "none",
6979 apex_available: ["com.android.art.debug"],
6980 test_for: ["com.android.art"],
6981 }
6982 `,
6983 android.MockFS{
6984 "system/sepolicy/apex/com.android.art-file_contexts": nil,
6985 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
6986 }.AddToFixture())
6987}
6988
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006989// TODO(jungjw): Move this to proptools
6990func intPtr(i int) *int {
6991 return &i
6992}
6993
6994func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006995 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006996 apex_set {
6997 name: "myapex",
6998 set: "myapex.apks",
6999 filename: "foo_v2.apex",
7000 overrides: ["foo"],
7001 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007002 `,
7003 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7004 variables.Platform_sdk_version = intPtr(30)
7005 }),
7006 android.FixtureModifyConfig(func(config android.Config) {
7007 config.Targets[android.Android] = []android.Target{
7008 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
7009 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
7010 }
7011 }),
7012 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007013
Paul Duffin24704672021-04-06 16:09:30 +01007014 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007015
7016 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01007017 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007018 actual := extractedApex.Args["abis"]
7019 expected := "ARMEABI_V7A,ARM64_V8A"
7020 if actual != expected {
7021 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7022 }
7023 actual = extractedApex.Args["sdk-version"]
7024 expected = "30"
7025 if actual != expected {
7026 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7027 }
7028
Paul Duffin5ec165d2021-06-15 19:09:41 +01007029 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007030 a := m.Module().(*ApexSet)
7031 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07007032 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007033 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
7034 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
7035 }
7036}
7037
Jiyong Park7d95a512020-05-10 15:16:24 +09007038func TestNoStaticLinkingToStubsLib(t *testing.T) {
7039 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
7040 apex {
7041 name: "myapex",
7042 key: "myapex.key",
7043 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007044 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09007045 }
7046
7047 apex_key {
7048 name: "myapex.key",
7049 public_key: "testkey.avbpubkey",
7050 private_key: "testkey.pem",
7051 }
7052
7053 cc_library {
7054 name: "mylib",
7055 srcs: ["mylib.cpp"],
7056 static_libs: ["otherlib"],
7057 system_shared_libs: [],
7058 stl: "none",
7059 apex_available: [ "myapex" ],
7060 }
7061
7062 cc_library {
7063 name: "otherlib",
7064 srcs: ["mylib.cpp"],
7065 system_shared_libs: [],
7066 stl: "none",
7067 stubs: {
7068 versions: ["1", "2", "3"],
7069 },
7070 apex_available: [ "myapex" ],
7071 }
7072 `)
7073}
7074
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007075func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007076 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007077 apex {
7078 name: "myapex",
7079 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007080 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007081 }
7082
7083 apex_key {
7084 name: "myapex.key",
7085 public_key: "testkey.avbpubkey",
7086 private_key: "testkey.pem",
7087 }
7088
7089 prebuilt_apex {
7090 name: "myapex",
7091 prefer: true,
7092 arch: {
7093 arm64: {
7094 src: "myapex-arm64.apex",
7095 },
7096 arm: {
7097 src: "myapex-arm.apex",
7098 },
7099 },
7100 }
7101
7102 apex_set {
7103 name: "myapex_set",
7104 set: "myapex.apks",
7105 filename: "myapex_set.apex",
7106 overrides: ["myapex"],
7107 }
7108 `)
7109
7110 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7111 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7112 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 +09007113 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 +09007114}
7115
Jooyung Han938b5932020-06-20 12:47:47 +09007116func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007117 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007118 apex {
7119 name: "myapex",
7120 key: "myapex.key",
7121 apps: ["app"],
7122 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007123 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007124 }
7125
7126 apex_key {
7127 name: "myapex.key",
7128 public_key: "testkey.avbpubkey",
7129 private_key: "testkey.pem",
7130 }
7131
7132 android_app {
7133 name: "app",
7134 srcs: ["foo/bar/MyClass.java"],
7135 package_name: "foo",
7136 sdk_version: "none",
7137 system_modules: "none",
7138 apex_available: [ "myapex" ],
7139 }
7140 `, withFiles(map[string][]byte{
7141 "sub/Android.bp": []byte(`
7142 override_apex {
7143 name: "override_myapex",
7144 base: "myapex",
7145 apps: ["override_app"],
7146 allowed_files: ":allowed",
7147 }
7148 // Overridable "path" property should be referenced indirectly
7149 filegroup {
7150 name: "allowed",
7151 srcs: ["allowed.txt"],
7152 }
7153 override_android_app {
7154 name: "override_app",
7155 base: "app",
7156 package_name: "bar",
7157 }
7158 `),
7159 }))
7160
7161 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7162 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7163 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7164 }
7165
7166 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7167 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7168 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7169 }
7170}
7171
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007172func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007173 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007174 apex {
7175 name: "myapex",
7176 key: "myapex.key",
7177 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007178 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007179 }
7180
7181 apex_key {
7182 name: "myapex.key",
7183 public_key: "testkey.avbpubkey",
7184 private_key: "testkey.pem",
7185 }
7186
7187 cc_library {
7188 name: "mylib",
7189 srcs: ["mylib.cpp"],
7190 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007191 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007192 },
7193 apex_available: ["myapex"],
7194 }
7195
7196 cc_prebuilt_library_shared {
7197 name: "mylib",
7198 prefer: false,
7199 srcs: ["prebuilt.so"],
7200 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007201 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007202 },
7203 apex_available: ["myapex"],
7204 }
7205 `)
7206}
7207
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007208func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007209 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007210 apex {
7211 name: "myapex",
7212 key: "myapex.key",
7213 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007214 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007215 }
7216 apex_key {
7217 name: "myapex.key",
7218 public_key: "testkey.avbpubkey",
7219 private_key: "testkey.pem",
7220 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007221 `,
7222 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7223 variables.CompressedApex = proptools.BoolPtr(true)
7224 }),
7225 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007226
7227 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7228 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7229
7230 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7231 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7232
7233 // Make sure output of bundle is .capex
7234 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7235 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7236
7237 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007238 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007239 var builder strings.Builder
7240 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7241 androidMk := builder.String()
7242 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7243}
7244
Martin Stjernholm2856c662020-12-02 15:03:42 +00007245func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007246 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007247 apex {
7248 name: "myapex",
7249 key: "myapex.key",
7250 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007251 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007252 }
7253
7254 apex_key {
7255 name: "myapex.key",
7256 public_key: "testkey.avbpubkey",
7257 private_key: "testkey.pem",
7258 }
7259
7260 cc_library {
7261 name: "mylib",
7262 srcs: ["mylib.cpp"],
7263 apex_available: ["myapex"],
7264 shared_libs: ["otherlib"],
7265 system_shared_libs: [],
7266 }
7267
7268 cc_library {
7269 name: "otherlib",
7270 srcs: ["mylib.cpp"],
7271 stubs: {
7272 versions: ["current"],
7273 },
7274 }
7275
7276 cc_prebuilt_library_shared {
7277 name: "otherlib",
7278 prefer: true,
7279 srcs: ["prebuilt.so"],
7280 stubs: {
7281 versions: ["current"],
7282 },
7283 }
7284 `)
7285
7286 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007287 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007288 var builder strings.Builder
7289 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7290 androidMk := builder.String()
7291
7292 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7293 // a thing there.
7294 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7295}
7296
Jiyong Parke3867542020-12-03 17:28:25 +09007297func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007298 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007299 apex {
7300 name: "myapex",
7301 key: "myapex.key",
7302 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007303 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007304 }
7305
7306 apex_key {
7307 name: "myapex.key",
7308 public_key: "testkey.avbpubkey",
7309 private_key: "testkey.pem",
7310 }
7311
7312 cc_library {
7313 name: "mylib",
7314 srcs: ["mylib.cpp"],
7315 system_shared_libs: [],
7316 stl: "none",
7317 apex_available: ["myapex"],
7318 shared_libs: ["mylib2"],
7319 target: {
7320 apex: {
7321 exclude_shared_libs: ["mylib2"],
7322 },
7323 },
7324 }
7325
7326 cc_library {
7327 name: "mylib2",
7328 srcs: ["mylib.cpp"],
7329 system_shared_libs: [],
7330 stl: "none",
7331 }
7332 `)
7333
7334 // Check if mylib is linked to mylib2 for the non-apex target
7335 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7336 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7337
7338 // Make sure that the link doesn't occur for the apex target
7339 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7340 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7341
7342 // It shouldn't appear in the copy cmd as well.
7343 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7344 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7345}
7346
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007347func TestPrebuiltStubLibDep(t *testing.T) {
7348 bpBase := `
7349 apex {
7350 name: "myapex",
7351 key: "myapex.key",
7352 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007353 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007354 }
7355 apex_key {
7356 name: "myapex.key",
7357 public_key: "testkey.avbpubkey",
7358 private_key: "testkey.pem",
7359 }
7360 cc_library {
7361 name: "mylib",
7362 srcs: ["mylib.cpp"],
7363 apex_available: ["myapex"],
7364 shared_libs: ["stublib"],
7365 system_shared_libs: [],
7366 }
7367 apex {
7368 name: "otherapex",
7369 enabled: %s,
7370 key: "myapex.key",
7371 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007372 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007373 }
7374 `
7375
7376 stublibSourceBp := `
7377 cc_library {
7378 name: "stublib",
7379 srcs: ["mylib.cpp"],
7380 apex_available: ["otherapex"],
7381 system_shared_libs: [],
7382 stl: "none",
7383 stubs: {
7384 versions: ["1"],
7385 },
7386 }
7387 `
7388
7389 stublibPrebuiltBp := `
7390 cc_prebuilt_library_shared {
7391 name: "stublib",
7392 srcs: ["prebuilt.so"],
7393 apex_available: ["otherapex"],
7394 stubs: {
7395 versions: ["1"],
7396 },
7397 %s
7398 }
7399 `
7400
7401 tests := []struct {
7402 name string
7403 stublibBp string
7404 usePrebuilt bool
7405 modNames []string // Modules to collect AndroidMkEntries for
7406 otherApexEnabled []string
7407 }{
7408 {
7409 name: "only_source",
7410 stublibBp: stublibSourceBp,
7411 usePrebuilt: false,
7412 modNames: []string{"stublib"},
7413 otherApexEnabled: []string{"true", "false"},
7414 },
7415 {
7416 name: "source_preferred",
7417 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7418 usePrebuilt: false,
7419 modNames: []string{"stublib", "prebuilt_stublib"},
7420 otherApexEnabled: []string{"true", "false"},
7421 },
7422 {
7423 name: "prebuilt_preferred",
7424 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7425 usePrebuilt: true,
7426 modNames: []string{"stublib", "prebuilt_stublib"},
7427 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7428 },
7429 {
7430 name: "only_prebuilt",
7431 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7432 usePrebuilt: true,
7433 modNames: []string{"stublib"},
7434 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7435 },
7436 }
7437
7438 for _, test := range tests {
7439 t.Run(test.name, func(t *testing.T) {
7440 for _, otherApexEnabled := range test.otherApexEnabled {
7441 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007442 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007443
7444 type modAndMkEntries struct {
7445 mod *cc.Module
7446 mkEntries android.AndroidMkEntries
7447 }
7448 entries := []*modAndMkEntries{}
7449
7450 // Gather shared lib modules that are installable
7451 for _, modName := range test.modNames {
7452 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7453 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7454 continue
7455 }
7456 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007457 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007458 continue
7459 }
Colin Crossaa255532020-07-03 13:18:24 -07007460 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007461 if ent.Disabled {
7462 continue
7463 }
7464 entries = append(entries, &modAndMkEntries{
7465 mod: mod,
7466 mkEntries: ent,
7467 })
7468 }
7469 }
7470 }
7471
7472 var entry *modAndMkEntries = nil
7473 for _, ent := range entries {
7474 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7475 if entry != nil {
7476 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7477 } else {
7478 entry = ent
7479 }
7480 }
7481 }
7482
7483 if entry == nil {
7484 t.Errorf("AndroidMk entry for \"stublib\" missing")
7485 } else {
7486 isPrebuilt := entry.mod.Prebuilt() != nil
7487 if isPrebuilt != test.usePrebuilt {
7488 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7489 }
7490 if !entry.mod.IsStubs() {
7491 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7492 }
7493 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7494 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7495 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007496 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09007497 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09007498 if !android.InList(expected, cflags) {
7499 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7500 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007501 }
7502 })
7503 }
7504 })
7505 }
7506}
7507
Colin Crosscbb2b812021-05-25 18:16:02 -07007508func TestApexJavaCoverage(t *testing.T) {
7509 bp := `
7510 apex {
7511 name: "myapex",
7512 key: "myapex.key",
7513 java_libs: ["mylib"],
7514 bootclasspath_fragments: ["mybootclasspathfragment"],
7515 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
7516 updatable: false,
7517 }
7518
7519 apex_key {
7520 name: "myapex.key",
7521 public_key: "testkey.avbpubkey",
7522 private_key: "testkey.pem",
7523 }
7524
7525 java_library {
7526 name: "mylib",
7527 srcs: ["mylib.java"],
7528 apex_available: ["myapex"],
7529 compile_dex: true,
7530 }
7531
7532 bootclasspath_fragment {
7533 name: "mybootclasspathfragment",
7534 contents: ["mybootclasspathlib"],
7535 apex_available: ["myapex"],
7536 }
7537
7538 java_library {
7539 name: "mybootclasspathlib",
7540 srcs: ["mybootclasspathlib.java"],
7541 apex_available: ["myapex"],
7542 compile_dex: true,
7543 }
7544
7545 systemserverclasspath_fragment {
7546 name: "mysystemserverclasspathfragment",
7547 contents: ["mysystemserverclasspathlib"],
7548 apex_available: ["myapex"],
7549 }
7550
7551 java_library {
7552 name: "mysystemserverclasspathlib",
7553 srcs: ["mysystemserverclasspathlib.java"],
7554 apex_available: ["myapex"],
7555 compile_dex: true,
7556 }
7557 `
7558
7559 result := android.GroupFixturePreparers(
7560 PrepareForTestWithApexBuildComponents,
7561 prepareForTestWithMyapex,
7562 java.PrepareForTestWithJavaDefaultModules,
7563 android.PrepareForTestWithAndroidBuildComponents,
7564 android.FixtureWithRootAndroidBp(bp),
7565 android.FixtureMergeEnv(map[string]string{
7566 "EMMA_INSTRUMENT": "true",
7567 }),
7568 ).RunTest(t)
7569
7570 // Make sure jacoco ran on both mylib and mybootclasspathlib
7571 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
7572 t.Errorf("Failed to find jacoco rule for mylib")
7573 }
7574 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
7575 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
7576 }
7577 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
7578 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
7579 }
7580}
7581
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007582func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01007583 os.Exit(m.Run())
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007584}