blob: 4829029b3a1c03b140c913c9912ba02a7877f5bd [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
Martin Stjernholmd8da28e2021-06-24 14:37:13 +01003673 module := ctx.ModuleForTests("myapex", "android_common_com.android.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
Martin Stjernholmd8da28e2021-06-24 14:37:13 +01003679 apexBundle := module.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
Martin Stjernholmd8da28e2021-06-24 14:37:13 +01004222func TestPrebuiltApexName(t *testing.T) {
4223 testApex(t, `
4224 prebuilt_apex {
4225 name: "com.company.android.myapex",
4226 apex_name: "com.android.myapex",
4227 src: "company-myapex-arm.apex",
4228 }
4229 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4230
4231 testApex(t, `
4232 apex_set {
4233 name: "com.company.android.myapex",
4234 apex_name: "com.android.myapex",
4235 set: "company-myapex.apks",
4236 }
4237 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4238}
4239
4240func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4241 _ = android.GroupFixturePreparers(
4242 java.PrepareForTestWithJavaDefaultModules,
4243 PrepareForTestWithApexBuildComponents,
4244 android.FixtureWithRootAndroidBp(`
4245 platform_bootclasspath {
4246 name: "platform-bootclasspath",
4247 fragments: [
4248 {
4249 apex: "com.android.art",
4250 module: "art-bootclasspath-fragment",
4251 },
4252 ],
4253 }
4254
4255 prebuilt_apex {
4256 name: "com.company.android.art",
4257 apex_name: "com.android.art",
4258 src: "com.company.android.art-arm.apex",
4259 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4260 }
4261
4262 prebuilt_bootclasspath_fragment {
4263 name: "art-bootclasspath-fragment",
4264 contents: ["core-oj"],
4265 }
4266
4267 java_import {
4268 name: "core-oj",
4269 jars: ["prebuilt.jar"],
4270 }
4271 `),
4272 ).RunTest(t)
4273}
4274
Paul Duffin092153d2021-01-26 11:42:39 +00004275// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4276// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004277func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004278 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004279
Paul Duffin89886cb2021-02-05 16:44:03 +00004280 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004281 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004282 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004283 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004284 stem := android.RemoveOptionalPrebuiltPrefix(name)
4285 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004286 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4287 }
4288 }
4289
Paul Duffin39853512021-02-26 11:09:39 +00004290 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004291 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004292 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004293 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4294 }
4295 }
4296
4297 t.Run("prebuilt only", func(t *testing.T) {
4298 bp := `
4299 prebuilt_apex {
4300 name: "myapex",
4301 arch: {
4302 arm64: {
4303 src: "myapex-arm64.apex",
4304 },
4305 arm: {
4306 src: "myapex-arm.apex",
4307 },
4308 },
Paul Duffin39853512021-02-26 11:09:39 +00004309 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004310 }
4311
4312 java_import {
4313 name: "libfoo",
4314 jars: ["libfoo.jar"],
4315 }
Paul Duffin39853512021-02-26 11:09:39 +00004316
4317 java_sdk_library_import {
4318 name: "libbar",
4319 public: {
4320 jars: ["libbar.jar"],
4321 },
4322 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004323 `
4324
4325 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4326 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4327
Paul Duffinf6932af2021-02-26 18:21:56 +00004328 // Make sure that the deapexer has the correct input APEX.
4329 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4330 rule := deapexer.Rule("deapexer")
4331 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4332 t.Errorf("expected: %q, found: %q", expected, actual)
4333 }
4334
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004335 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin5ec165d2021-06-15 19:09:41 +01004336 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004337 rule = prebuiltApex.Rule("android/soong/android.Cp")
4338 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4339 t.Errorf("expected: %q, found: %q", expected, actual)
4340 }
4341
Paul Duffin89886cb2021-02-05 16:44:03 +00004342 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004343
4344 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004345 })
4346
4347 t.Run("prebuilt with source preferred", func(t *testing.T) {
4348
4349 bp := `
4350 prebuilt_apex {
4351 name: "myapex",
4352 arch: {
4353 arm64: {
4354 src: "myapex-arm64.apex",
4355 },
4356 arm: {
4357 src: "myapex-arm.apex",
4358 },
4359 },
Paul Duffin39853512021-02-26 11:09:39 +00004360 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004361 }
4362
4363 java_import {
4364 name: "libfoo",
4365 jars: ["libfoo.jar"],
4366 }
4367
4368 java_library {
4369 name: "libfoo",
4370 }
Paul Duffin39853512021-02-26 11:09:39 +00004371
4372 java_sdk_library_import {
4373 name: "libbar",
4374 public: {
4375 jars: ["libbar.jar"],
4376 },
4377 }
4378
4379 java_sdk_library {
4380 name: "libbar",
4381 srcs: ["foo/bar/MyClass.java"],
4382 unsafe_ignore_missing_latest_api: true,
4383 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004384 `
4385
4386 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4387 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4388
Paul Duffin89886cb2021-02-05 16:44:03 +00004389 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004390 ensureNoSourceVariant(t, ctx, "libfoo")
4391
4392 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4393 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004394 })
4395
4396 t.Run("prebuilt preferred with source", func(t *testing.T) {
4397 bp := `
4398 prebuilt_apex {
4399 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004400 arch: {
4401 arm64: {
4402 src: "myapex-arm64.apex",
4403 },
4404 arm: {
4405 src: "myapex-arm.apex",
4406 },
4407 },
Paul Duffin39853512021-02-26 11:09:39 +00004408 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004409 }
4410
4411 java_import {
4412 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004413 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004414 jars: ["libfoo.jar"],
4415 }
4416
4417 java_library {
4418 name: "libfoo",
4419 }
Paul Duffin39853512021-02-26 11:09:39 +00004420
4421 java_sdk_library_import {
4422 name: "libbar",
4423 prefer: true,
4424 public: {
4425 jars: ["libbar.jar"],
4426 },
4427 }
4428
4429 java_sdk_library {
4430 name: "libbar",
4431 srcs: ["foo/bar/MyClass.java"],
4432 unsafe_ignore_missing_latest_api: true,
4433 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004434 `
4435
4436 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4437 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4438
Paul Duffin89886cb2021-02-05 16:44:03 +00004439 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004440 ensureNoSourceVariant(t, ctx, "libfoo")
4441
4442 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4443 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004444 })
4445}
4446
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004447func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01004448 preparer := android.GroupFixturePreparers(
4449 java.FixtureConfigureBootJars("myapex:libfoo", "myapex:libbar"),
4450 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
4451 // is disabled.
4452 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
4453 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004454
Paul Duffin37856732021-02-26 14:24:15 +00004455 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4456 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01004457 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004458 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004459 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004460 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004461 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004462 foundLibfooJar = true
4463 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01004464 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004465 }
4466 }
4467 if !foundLibfooJar {
Paul Duffin55607122021-03-30 23:32:51 +01004468 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 +00004469 }
4470 }
4471
Paul Duffin2a999332021-06-07 21:36:01 +01004472 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004473 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01004474 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffin2a999332021-06-07 21:36:01 +01004475 var rule android.TestingBuildParams
4476
4477 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
4478 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00004479 }
4480
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004481 fragment := java.ApexVariantReference{
4482 Apex: proptools.StringPtr("myapex"),
4483 Module: proptools.StringPtr("my-bootclasspath-fragment"),
4484 }
4485
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004486 t.Run("prebuilt only", func(t *testing.T) {
4487 bp := `
4488 prebuilt_apex {
4489 name: "myapex",
4490 arch: {
4491 arm64: {
4492 src: "myapex-arm64.apex",
4493 },
4494 arm: {
4495 src: "myapex-arm.apex",
4496 },
4497 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004498 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4499 }
4500
4501 prebuilt_bootclasspath_fragment {
4502 name: "my-bootclasspath-fragment",
4503 contents: ["libfoo", "libbar"],
4504 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004505 }
4506
4507 java_import {
4508 name: "libfoo",
4509 jars: ["libfoo.jar"],
4510 apex_available: ["myapex"],
4511 }
Paul Duffin37856732021-02-26 14:24:15 +00004512
4513 java_sdk_library_import {
4514 name: "libbar",
4515 public: {
4516 jars: ["libbar.jar"],
4517 },
4518 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004519 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004520 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004521 `
4522
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004523 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004524 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4525 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004526
Paul Duffin537ea3d2021-05-14 10:38:00 +01004527 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin9d67ca62021-02-03 20:06:33 +00004528 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004529 out/soong/.intermediates/libbar.stubs/android_common/combined/libbar.stubs.jar
4530 out/soong/.intermediates/libfoo/android_common_myapex/combined/libfoo.jar
4531 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004532 })
4533
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004534 t.Run("apex_set only", func(t *testing.T) {
4535 bp := `
4536 apex_set {
4537 name: "myapex",
4538 set: "myapex.apks",
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004539 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4540 }
4541
4542 prebuilt_bootclasspath_fragment {
4543 name: "my-bootclasspath-fragment",
4544 contents: ["libfoo", "libbar"],
4545 apex_available: ["myapex"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004546 }
4547
4548 java_import {
4549 name: "libfoo",
4550 jars: ["libfoo.jar"],
4551 apex_available: ["myapex"],
4552 }
4553
4554 java_sdk_library_import {
4555 name: "libbar",
4556 public: {
4557 jars: ["libbar.jar"],
4558 },
4559 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004560 shared_library: false,
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004561 }
4562 `
4563
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004564 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004565 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4566 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
4567
Paul Duffin537ea3d2021-05-14 10:38:00 +01004568 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004569 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004570 out/soong/.intermediates/libbar.stubs/android_common/combined/libbar.stubs.jar
4571 out/soong/.intermediates/libfoo/android_common_myapex/combined/libfoo.jar
4572 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004573 })
4574
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004575 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4576 bp := `
4577 prebuilt_apex {
4578 name: "myapex",
4579 arch: {
4580 arm64: {
4581 src: "myapex-arm64.apex",
4582 },
4583 arm: {
4584 src: "myapex-arm.apex",
4585 },
4586 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004587 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4588 }
4589
4590 prebuilt_bootclasspath_fragment {
4591 name: "my-bootclasspath-fragment",
4592 contents: ["libfoo", "libbar"],
4593 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004594 }
4595
4596 java_import {
4597 name: "libfoo",
4598 jars: ["libfoo.jar"],
4599 apex_available: ["myapex"],
4600 }
4601
4602 java_library {
4603 name: "libfoo",
4604 srcs: ["foo/bar/MyClass.java"],
4605 apex_available: ["myapex"],
4606 }
Paul Duffin37856732021-02-26 14:24:15 +00004607
4608 java_sdk_library_import {
4609 name: "libbar",
4610 public: {
4611 jars: ["libbar.jar"],
4612 },
4613 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004614 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004615 }
4616
4617 java_sdk_library {
4618 name: "libbar",
4619 srcs: ["foo/bar/MyClass.java"],
4620 unsafe_ignore_missing_latest_api: true,
4621 apex_available: ["myapex"],
4622 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004623 `
4624
4625 // In this test the source (java_library) libfoo is active since the
4626 // prebuilt (java_import) defaults to prefer:false. However the
4627 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4628 // find the dex boot jar in it. We either need to disable the source libfoo
4629 // or make the prebuilt libfoo preferred.
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004630 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004631 })
4632
4633 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4634 bp := `
4635 prebuilt_apex {
4636 name: "myapex",
4637 arch: {
4638 arm64: {
4639 src: "myapex-arm64.apex",
4640 },
4641 arm: {
4642 src: "myapex-arm.apex",
4643 },
4644 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004645 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4646 }
4647
4648 prebuilt_bootclasspath_fragment {
4649 name: "my-bootclasspath-fragment",
4650 contents: ["libfoo", "libbar"],
4651 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004652 }
4653
4654 java_import {
4655 name: "libfoo",
4656 prefer: true,
4657 jars: ["libfoo.jar"],
4658 apex_available: ["myapex"],
4659 }
4660
4661 java_library {
4662 name: "libfoo",
4663 srcs: ["foo/bar/MyClass.java"],
4664 apex_available: ["myapex"],
4665 }
Paul Duffin37856732021-02-26 14:24:15 +00004666
4667 java_sdk_library_import {
4668 name: "libbar",
4669 prefer: true,
4670 public: {
4671 jars: ["libbar.jar"],
4672 },
4673 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004674 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004675 }
4676
4677 java_sdk_library {
4678 name: "libbar",
4679 srcs: ["foo/bar/MyClass.java"],
4680 unsafe_ignore_missing_latest_api: true,
4681 apex_available: ["myapex"],
4682 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004683 `
4684
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004685 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004686 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4687 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004688
Paul Duffin537ea3d2021-05-14 10:38:00 +01004689 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin9d67ca62021-02-03 20:06:33 +00004690 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004691 out/soong/.intermediates/prebuilt_libbar.stubs/android_common/combined/libbar.stubs.jar
4692 out/soong/.intermediates/prebuilt_libfoo/android_common_myapex/combined/libfoo.jar
4693 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004694 })
4695
4696 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4697 bp := `
4698 apex {
4699 name: "myapex",
4700 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004701 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004702 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004703 }
4704
4705 apex_key {
4706 name: "myapex.key",
4707 public_key: "testkey.avbpubkey",
4708 private_key: "testkey.pem",
4709 }
4710
4711 prebuilt_apex {
4712 name: "myapex",
4713 arch: {
4714 arm64: {
4715 src: "myapex-arm64.apex",
4716 },
4717 arm: {
4718 src: "myapex-arm.apex",
4719 },
4720 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004721 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4722 }
4723
4724 prebuilt_bootclasspath_fragment {
4725 name: "my-bootclasspath-fragment",
4726 contents: ["libfoo", "libbar"],
4727 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004728 }
4729
4730 java_import {
4731 name: "libfoo",
4732 jars: ["libfoo.jar"],
4733 apex_available: ["myapex"],
4734 }
4735
4736 java_library {
4737 name: "libfoo",
4738 srcs: ["foo/bar/MyClass.java"],
4739 apex_available: ["myapex"],
4740 }
Paul Duffin37856732021-02-26 14:24:15 +00004741
4742 java_sdk_library_import {
4743 name: "libbar",
4744 public: {
4745 jars: ["libbar.jar"],
4746 },
4747 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004748 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004749 }
4750
4751 java_sdk_library {
4752 name: "libbar",
4753 srcs: ["foo/bar/MyClass.java"],
4754 unsafe_ignore_missing_latest_api: true,
4755 apex_available: ["myapex"],
4756 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004757 `
4758
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004759 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004760 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4761 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004762
Paul Duffin537ea3d2021-05-14 10:38:00 +01004763 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin4fd997b2021-02-03 20:06:33 +00004764 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004765 out/soong/.intermediates/libbar/android_common_myapex/javac/libbar.jar
4766 out/soong/.intermediates/libfoo/android_common_apex10000/javac/libfoo.jar
4767 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004768 })
4769
4770 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4771 bp := `
4772 apex {
4773 name: "myapex",
4774 enabled: false,
4775 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01004776 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004777 }
4778
4779 apex_key {
4780 name: "myapex.key",
4781 public_key: "testkey.avbpubkey",
4782 private_key: "testkey.pem",
4783 }
4784
4785 prebuilt_apex {
4786 name: "myapex",
4787 arch: {
4788 arm64: {
4789 src: "myapex-arm64.apex",
4790 },
4791 arm: {
4792 src: "myapex-arm.apex",
4793 },
4794 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004795 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4796 }
4797
4798 prebuilt_bootclasspath_fragment {
4799 name: "my-bootclasspath-fragment",
4800 contents: ["libfoo", "libbar"],
4801 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004802 }
4803
4804 java_import {
4805 name: "libfoo",
4806 prefer: true,
4807 jars: ["libfoo.jar"],
4808 apex_available: ["myapex"],
4809 }
4810
4811 java_library {
4812 name: "libfoo",
4813 srcs: ["foo/bar/MyClass.java"],
4814 apex_available: ["myapex"],
4815 }
Paul Duffin37856732021-02-26 14:24:15 +00004816
4817 java_sdk_library_import {
4818 name: "libbar",
4819 prefer: true,
4820 public: {
4821 jars: ["libbar.jar"],
4822 },
4823 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004824 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004825 }
4826
4827 java_sdk_library {
4828 name: "libbar",
4829 srcs: ["foo/bar/MyClass.java"],
4830 unsafe_ignore_missing_latest_api: true,
4831 apex_available: ["myapex"],
4832 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004833 `
4834
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004835 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004836 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4837 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004838
Paul Duffin537ea3d2021-05-14 10:38:00 +01004839 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin9d67ca62021-02-03 20:06:33 +00004840 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004841 out/soong/.intermediates/prebuilt_libbar.stubs/android_common/combined/libbar.stubs.jar
4842 out/soong/.intermediates/prebuilt_libfoo/android_common_myapex/combined/libfoo.jar
4843 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004844 })
4845}
4846
Roland Levillain630846d2019-06-26 12:48:34 +01004847func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004848 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004849 apex_test {
4850 name: "myapex",
4851 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004852 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01004853 tests: [
4854 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004855 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004856 ],
4857 }
4858
4859 apex_key {
4860 name: "myapex.key",
4861 public_key: "testkey.avbpubkey",
4862 private_key: "testkey.pem",
4863 }
4864
Liz Kammer1c14a212020-05-12 15:26:55 -07004865 filegroup {
4866 name: "fg",
4867 srcs: [
4868 "baz",
4869 "bar/baz"
4870 ],
4871 }
4872
Roland Levillain630846d2019-06-26 12:48:34 +01004873 cc_test {
4874 name: "mytest",
4875 gtest: false,
4876 srcs: ["mytest.cpp"],
4877 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004878 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004879 system_shared_libs: [],
4880 static_executable: true,
4881 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004882 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004883 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004884
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004885 cc_library {
4886 name: "mylib",
4887 srcs: ["mylib.cpp"],
4888 system_shared_libs: [],
4889 stl: "none",
4890 }
4891
Liz Kammer5bd365f2020-05-27 15:15:11 -07004892 filegroup {
4893 name: "fg2",
4894 srcs: [
4895 "testdata/baz"
4896 ],
4897 }
4898
Roland Levillain9b5fde92019-06-28 15:41:19 +01004899 cc_test {
4900 name: "mytests",
4901 gtest: false,
4902 srcs: [
4903 "mytest1.cpp",
4904 "mytest2.cpp",
4905 "mytest3.cpp",
4906 ],
4907 test_per_src: true,
4908 relative_install_path: "test",
4909 system_shared_libs: [],
4910 static_executable: true,
4911 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004912 data: [
4913 ":fg",
4914 ":fg2",
4915 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004916 }
Roland Levillain630846d2019-06-26 12:48:34 +01004917 `)
4918
Sundong Ahnabb64432019-10-22 13:58:29 +09004919 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004920 copyCmds := apexRule.Args["copy_commands"]
4921
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004922 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004923 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004924 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004925
Liz Kammer1c14a212020-05-12 15:26:55 -07004926 //Ensure that test data are copied into apex.
4927 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4928 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4929
Roland Levillain9b5fde92019-06-28 15:41:19 +01004930 // Ensure that test deps built with `test_per_src` are copied into apex.
4931 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4932 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4933 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004934
4935 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004936 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004937 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004938 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004939 prefix := "TARGET_"
4940 var builder strings.Builder
4941 data.Custom(&builder, name, prefix, "", data)
4942 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004943 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4944 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4945 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4946 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004947 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004948 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004949 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004950
4951 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004952 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004953 data.Custom(&builder, name, prefix, "", data)
4954 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004955 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4956 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004957}
4958
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004959func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004960 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004961 apex {
4962 name: "myapex",
4963 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004964 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004965 }
4966 apex_key {
4967 name: "myapex.key",
4968 public_key: "testkey.avbpubkey",
4969 private_key: "testkey.pem",
4970 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00004971 `,
4972 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4973 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4974 }),
4975 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004976 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004977 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07004978 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004979 var builder strings.Builder
4980 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4981 androidMk := builder.String()
4982 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4983}
4984
Jooyung Hand48f3c32019-08-23 11:18:57 +09004985func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4986 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4987 apex {
4988 name: "myapex",
4989 key: "myapex.key",
4990 native_shared_libs: ["libfoo"],
4991 }
4992
4993 apex_key {
4994 name: "myapex.key",
4995 public_key: "testkey.avbpubkey",
4996 private_key: "testkey.pem",
4997 }
4998
4999 cc_library {
5000 name: "libfoo",
5001 stl: "none",
5002 system_shared_libs: [],
5003 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005004 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005005 }
5006 `)
5007 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5008 apex {
5009 name: "myapex",
5010 key: "myapex.key",
5011 java_libs: ["myjar"],
5012 }
5013
5014 apex_key {
5015 name: "myapex.key",
5016 public_key: "testkey.avbpubkey",
5017 private_key: "testkey.pem",
5018 }
5019
5020 java_library {
5021 name: "myjar",
5022 srcs: ["foo/bar/MyClass.java"],
5023 sdk_version: "none",
5024 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005025 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005026 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005027 }
5028 `)
5029}
5030
Bill Peckhama41a6962021-01-11 10:58:54 -08005031func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005032 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005033 apex {
5034 name: "myapex",
5035 key: "myapex.key",
5036 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005037 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005038 }
5039
5040 apex_key {
5041 name: "myapex.key",
5042 public_key: "testkey.avbpubkey",
5043 private_key: "testkey.pem",
5044 }
5045
5046 java_import {
5047 name: "myjavaimport",
5048 apex_available: ["myapex"],
5049 jars: ["my.jar"],
5050 compile_dex: true,
5051 }
5052 `)
5053
5054 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5055 apexRule := module.Rule("apexRule")
5056 copyCmds := apexRule.Args["copy_commands"]
5057 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5058}
5059
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005060func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005061 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005062 apex {
5063 name: "myapex",
5064 key: "myapex.key",
5065 apps: [
5066 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005067 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005068 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005069 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005070 }
5071
5072 apex_key {
5073 name: "myapex.key",
5074 public_key: "testkey.avbpubkey",
5075 private_key: "testkey.pem",
5076 }
5077
5078 android_app {
5079 name: "AppFoo",
5080 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005081 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005082 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005083 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005084 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005085 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005086 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005087
5088 android_app {
5089 name: "AppFooPriv",
5090 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005091 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005092 system_modules: "none",
5093 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005094 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005095 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005096 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005097
5098 cc_library_shared {
5099 name: "libjni",
5100 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005101 shared_libs: ["libfoo"],
5102 stl: "none",
5103 system_shared_libs: [],
5104 apex_available: [ "myapex" ],
5105 sdk_version: "current",
5106 }
5107
5108 cc_library_shared {
5109 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005110 stl: "none",
5111 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005112 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005113 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005114 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005115 `)
5116
Sundong Ahnabb64432019-10-22 13:58:29 +09005117 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005118 apexRule := module.Rule("apexRule")
5119 copyCmds := apexRule.Args["copy_commands"]
5120
5121 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005122 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005123
Colin Crossaede88c2020-08-11 12:17:01 -07005124 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005125 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005126 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005127 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005128 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005129 // JNI libraries including transitive deps are
5130 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005131 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005132 // ... embedded inside APK (jnilibs.zip)
5133 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5134 // ... and not directly inside the APEX
5135 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5136 }
Dario Frenicde2a032019-10-27 00:29:22 +01005137}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005138
Dario Frenicde2a032019-10-27 00:29:22 +01005139func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005140 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005141 apex {
5142 name: "myapex",
5143 key: "myapex.key",
5144 apps: [
5145 "AppFooPrebuilt",
5146 "AppFooPrivPrebuilt",
5147 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005148 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005149 }
5150
5151 apex_key {
5152 name: "myapex.key",
5153 public_key: "testkey.avbpubkey",
5154 private_key: "testkey.pem",
5155 }
5156
5157 android_app_import {
5158 name: "AppFooPrebuilt",
5159 apk: "PrebuiltAppFoo.apk",
5160 presigned: true,
5161 dex_preopt: {
5162 enabled: false,
5163 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005164 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005165 }
5166
5167 android_app_import {
5168 name: "AppFooPrivPrebuilt",
5169 apk: "PrebuiltAppFooPriv.apk",
5170 privileged: true,
5171 presigned: true,
5172 dex_preopt: {
5173 enabled: false,
5174 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005175 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005176 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005177 }
5178 `)
5179
Sundong Ahnabb64432019-10-22 13:58:29 +09005180 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005181 apexRule := module.Rule("apexRule")
5182 copyCmds := apexRule.Args["copy_commands"]
5183
5184 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005185 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5186}
5187
5188func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005189 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005190 apex {
5191 name: "myapex",
5192 key: "myapex.key",
5193 apps: [
5194 "AppFoo",
5195 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005196 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005197 }
5198
5199 apex_key {
5200 name: "myapex.key",
5201 public_key: "testkey.avbpubkey",
5202 private_key: "testkey.pem",
5203 }
5204
5205 android_app {
5206 name: "AppFoo",
5207 srcs: ["foo/bar/MyClass.java"],
5208 sdk_version: "none",
5209 system_modules: "none",
5210 apex_available: [ "myapex" ],
5211 }
5212
5213 android_app_import {
5214 name: "AppFoo",
5215 apk: "AppFooPrebuilt.apk",
5216 filename: "AppFooPrebuilt.apk",
5217 presigned: true,
5218 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005219 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005220 }
5221 `, withFiles(map[string][]byte{
5222 "AppFooPrebuilt.apk": nil,
5223 }))
5224
5225 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005226 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005227 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005228}
5229
Dario Freni6f3937c2019-12-20 22:58:03 +00005230func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005231 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005232 apex {
5233 name: "myapex",
5234 key: "myapex.key",
5235 apps: [
5236 "TesterHelpAppFoo",
5237 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005238 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005239 }
5240
5241 apex_key {
5242 name: "myapex.key",
5243 public_key: "testkey.avbpubkey",
5244 private_key: "testkey.pem",
5245 }
5246
5247 android_test_helper_app {
5248 name: "TesterHelpAppFoo",
5249 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005250 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005251 }
5252
5253 `)
5254
5255 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5256 apexRule := module.Rule("apexRule")
5257 copyCmds := apexRule.Args["copy_commands"]
5258
5259 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5260}
5261
Jooyung Han18020ea2019-11-13 10:50:48 +09005262func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5263 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005264 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005265 apex {
5266 name: "myapex",
5267 key: "myapex.key",
5268 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005269 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005270 }
5271
5272 apex_key {
5273 name: "myapex.key",
5274 public_key: "testkey.avbpubkey",
5275 private_key: "testkey.pem",
5276 }
5277
5278 apex {
5279 name: "otherapex",
5280 key: "myapex.key",
5281 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005282 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005283 }
5284
5285 cc_defaults {
5286 name: "libfoo-defaults",
5287 apex_available: ["otherapex"],
5288 }
5289
5290 cc_library {
5291 name: "libfoo",
5292 defaults: ["libfoo-defaults"],
5293 stl: "none",
5294 system_shared_libs: [],
5295 }`)
5296}
5297
Paul Duffine52e66f2020-03-30 17:54:29 +01005298func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005299 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005300 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005301 apex {
5302 name: "myapex",
5303 key: "myapex.key",
5304 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005305 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005306 }
5307
5308 apex_key {
5309 name: "myapex.key",
5310 public_key: "testkey.avbpubkey",
5311 private_key: "testkey.pem",
5312 }
5313
5314 apex {
5315 name: "otherapex",
5316 key: "otherapex.key",
5317 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005318 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005319 }
5320
5321 apex_key {
5322 name: "otherapex.key",
5323 public_key: "testkey.avbpubkey",
5324 private_key: "testkey.pem",
5325 }
5326
5327 cc_library {
5328 name: "libfoo",
5329 stl: "none",
5330 system_shared_libs: [],
5331 apex_available: ["otherapex"],
5332 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005333}
Jiyong Park127b40b2019-09-30 16:04:35 +09005334
Paul Duffine52e66f2020-03-30 17:54:29 +01005335func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005336 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005337 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005338.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005339.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005340.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005341.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005342.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005343.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005344 apex {
5345 name: "myapex",
5346 key: "myapex.key",
5347 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005348 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005349 }
5350
5351 apex_key {
5352 name: "myapex.key",
5353 public_key: "testkey.avbpubkey",
5354 private_key: "testkey.pem",
5355 }
5356
Jiyong Park127b40b2019-09-30 16:04:35 +09005357 cc_library {
5358 name: "libfoo",
5359 stl: "none",
5360 shared_libs: ["libbar"],
5361 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005362 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005363 }
5364
5365 cc_library {
5366 name: "libbar",
5367 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005368 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005369 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005370 apex_available: ["myapex"],
5371 }
5372
5373 cc_library {
5374 name: "libbaz",
5375 stl: "none",
5376 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005377 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005378}
Jiyong Park127b40b2019-09-30 16:04:35 +09005379
Paul Duffine52e66f2020-03-30 17:54:29 +01005380func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005381 testApexError(t, "\"otherapex\" is not a valid module name", `
5382 apex {
5383 name: "myapex",
5384 key: "myapex.key",
5385 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005386 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005387 }
5388
5389 apex_key {
5390 name: "myapex.key",
5391 public_key: "testkey.avbpubkey",
5392 private_key: "testkey.pem",
5393 }
5394
5395 cc_library {
5396 name: "libfoo",
5397 stl: "none",
5398 system_shared_libs: [],
5399 apex_available: ["otherapex"],
5400 }`)
5401
Paul Duffine52e66f2020-03-30 17:54:29 +01005402 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005403 apex {
5404 name: "myapex",
5405 key: "myapex.key",
5406 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005407 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005408 }
5409
5410 apex_key {
5411 name: "myapex.key",
5412 public_key: "testkey.avbpubkey",
5413 private_key: "testkey.pem",
5414 }
5415
5416 cc_library {
5417 name: "libfoo",
5418 stl: "none",
5419 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005420 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005421 apex_available: ["myapex"],
5422 }
5423
5424 cc_library {
5425 name: "libbar",
5426 stl: "none",
5427 system_shared_libs: [],
5428 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005429 }
5430
5431 cc_library {
5432 name: "libbaz",
5433 stl: "none",
5434 system_shared_libs: [],
5435 stubs: {
5436 versions: ["10", "20", "30"],
5437 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005438 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005439}
Jiyong Park127b40b2019-09-30 16:04:35 +09005440
Jiyong Park89e850a2020-04-07 16:37:39 +09005441func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005442 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005443 apex {
5444 name: "myapex",
5445 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005446 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005447 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005448 }
5449
5450 apex_key {
5451 name: "myapex.key",
5452 public_key: "testkey.avbpubkey",
5453 private_key: "testkey.pem",
5454 }
5455
5456 cc_library {
5457 name: "libfoo",
5458 stl: "none",
5459 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005460 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005461 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005462 }
5463
5464 cc_library {
5465 name: "libfoo2",
5466 stl: "none",
5467 system_shared_libs: [],
5468 shared_libs: ["libbaz"],
5469 apex_available: ["//apex_available:platform"],
5470 }
5471
5472 cc_library {
5473 name: "libbar",
5474 stl: "none",
5475 system_shared_libs: [],
5476 apex_available: ["myapex"],
5477 }
5478
5479 cc_library {
5480 name: "libbaz",
5481 stl: "none",
5482 system_shared_libs: [],
5483 apex_available: ["myapex"],
5484 stubs: {
5485 versions: ["1"],
5486 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005487 }`)
5488
Jiyong Park89e850a2020-04-07 16:37:39 +09005489 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5490 // because it depends on libbar which isn't available to platform
5491 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5492 if libfoo.NotAvailableForPlatform() != true {
5493 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5494 }
5495
5496 // libfoo2 however can be available to platform because it depends on libbaz which provides
5497 // stubs
5498 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5499 if libfoo2.NotAvailableForPlatform() == true {
5500 t.Errorf("%q should be available to platform", libfoo2.String())
5501 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005502}
Jiyong Parka90ca002019-10-07 15:47:24 +09005503
Paul Duffine52e66f2020-03-30 17:54:29 +01005504func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005505 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005506 apex {
5507 name: "myapex",
5508 key: "myapex.key",
5509 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005510 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005511 }
5512
5513 apex_key {
5514 name: "myapex.key",
5515 public_key: "testkey.avbpubkey",
5516 private_key: "testkey.pem",
5517 }
5518
5519 cc_library {
5520 name: "libfoo",
5521 stl: "none",
5522 system_shared_libs: [],
5523 apex_available: ["myapex"],
5524 static: {
5525 apex_available: ["//apex_available:platform"],
5526 },
5527 }`)
5528
Jiyong Park89e850a2020-04-07 16:37:39 +09005529 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5530 if libfooShared.NotAvailableForPlatform() != true {
5531 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5532 }
5533 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5534 if libfooStatic.NotAvailableForPlatform() != false {
5535 t.Errorf("%q should be available to platform", libfooStatic.String())
5536 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005537}
5538
Jiyong Park5d790c32019-11-15 18:40:32 +09005539func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005540 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005541 apex {
5542 name: "myapex",
5543 key: "myapex.key",
5544 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005545 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005546 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005547 }
5548
5549 override_apex {
5550 name: "override_myapex",
5551 base: "myapex",
5552 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005553 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005554 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005555 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07005556 key: "mynewapex.key",
5557 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09005558 }
5559
5560 apex_key {
5561 name: "myapex.key",
5562 public_key: "testkey.avbpubkey",
5563 private_key: "testkey.pem",
5564 }
5565
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07005566 apex_key {
5567 name: "mynewapex.key",
5568 public_key: "testkey2.avbpubkey",
5569 private_key: "testkey2.pem",
5570 }
5571
5572 android_app_certificate {
5573 name: "myapex.certificate",
5574 certificate: "testkey",
5575 }
5576
Jiyong Park5d790c32019-11-15 18:40:32 +09005577 android_app {
5578 name: "app",
5579 srcs: ["foo/bar/MyClass.java"],
5580 package_name: "foo",
5581 sdk_version: "none",
5582 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005583 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005584 }
5585
5586 override_android_app {
5587 name: "override_app",
5588 base: "app",
5589 package_name: "bar",
5590 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005591 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005592
Jiyong Park317645e2019-12-05 13:20:58 +09005593 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5594 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5595 if originalVariant.GetOverriddenBy() != "" {
5596 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5597 }
5598 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5599 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5600 }
5601
Jiyong Park5d790c32019-11-15 18:40:32 +09005602 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5603 apexRule := module.Rule("apexRule")
5604 copyCmds := apexRule.Args["copy_commands"]
5605
5606 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005607 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005608
5609 apexBundle := module.Module().(*apexBundle)
5610 name := apexBundle.Name()
5611 if name != "override_myapex" {
5612 t.Errorf("name should be \"override_myapex\", but was %q", name)
5613 }
5614
Baligh Uddin004d7172020-02-19 21:29:28 -08005615 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5616 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5617 }
5618
Jiyong Park20bacab2020-03-03 11:45:41 +09005619 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005620 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07005621 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
5622
5623 signApkRule := module.Rule("signapk")
5624 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09005625
Colin Crossaa255532020-07-03 13:18:24 -07005626 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005627 var builder strings.Builder
5628 data.Custom(&builder, name, "TARGET_", "", data)
5629 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005630 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005631 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5632 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005633 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005634 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005635 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005636 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5637 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005638}
5639
Jooyung Han214bf372019-11-12 13:03:50 +09005640func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005641 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005642 apex {
5643 name: "myapex",
5644 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005645 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005646 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005647 }
5648
5649 apex_key {
5650 name: "myapex.key",
5651 public_key: "testkey.avbpubkey",
5652 private_key: "testkey.pem",
5653 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005654
5655 cc_library {
5656 name: "mylib",
5657 srcs: ["mylib.cpp"],
5658 stl: "libc++",
5659 system_shared_libs: [],
5660 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005661 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005662 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005663 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005664
5665 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5666 args := module.Rule("apexRule").Args
5667 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005668 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005669
5670 // The copies of the libraries in the apex should have one more dependency than
5671 // the ones outside the apex, namely the unwinder. Ideally we should check
5672 // the dependency names directly here but for some reason the names are blank in
5673 // this test.
5674 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005675 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005676 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5677 if len(apexImplicits) != len(nonApexImplicits)+1 {
5678 t.Errorf("%q missing unwinder dep", lib)
5679 }
5680 }
Jooyung Han214bf372019-11-12 13:03:50 +09005681}
5682
Paul Duffine05480a2021-03-08 15:07:14 +00005683var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01005684 "api/current.txt": nil,
5685 "api/removed.txt": nil,
5686 "api/system-current.txt": nil,
5687 "api/system-removed.txt": nil,
5688 "api/test-current.txt": nil,
5689 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005690
Anton Hanssondff2c782020-12-21 17:10:01 +00005691 "100/public/api/foo.txt": nil,
5692 "100/public/api/foo-removed.txt": nil,
5693 "100/system/api/foo.txt": nil,
5694 "100/system/api/foo-removed.txt": nil,
5695
Paul Duffineedc5d52020-06-12 17:46:39 +01005696 // For java_sdk_library_import
5697 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005698}
5699
Jooyung Han58f26ab2019-12-18 15:34:32 +09005700func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005701 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09005702 apex {
5703 name: "myapex",
5704 key: "myapex.key",
5705 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005706 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09005707 }
5708
5709 apex_key {
5710 name: "myapex.key",
5711 public_key: "testkey.avbpubkey",
5712 private_key: "testkey.pem",
5713 }
5714
5715 java_sdk_library {
5716 name: "foo",
5717 srcs: ["a.java"],
5718 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005719 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005720 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005721
5722 prebuilt_apis {
5723 name: "sdk",
5724 api_dirs: ["100"],
5725 }
Paul Duffin9b879592020-05-26 13:21:35 +01005726 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005727
5728 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005729 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005730 "javalib/foo.jar",
5731 "etc/permissions/foo.xml",
5732 })
5733 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005734 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5735 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005736}
5737
Paul Duffin9b879592020-05-26 13:21:35 +01005738func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005739 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005740 apex {
5741 name: "myapex",
5742 key: "myapex.key",
5743 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005744 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005745 }
5746
5747 apex_key {
5748 name: "myapex.key",
5749 public_key: "testkey.avbpubkey",
5750 private_key: "testkey.pem",
5751 }
5752
5753 java_sdk_library {
5754 name: "foo",
5755 srcs: ["a.java"],
5756 api_packages: ["foo"],
5757 apex_available: ["myapex"],
5758 sdk_version: "none",
5759 system_modules: "none",
5760 }
5761
5762 java_library {
5763 name: "bar",
5764 srcs: ["a.java"],
5765 libs: ["foo"],
5766 apex_available: ["myapex"],
5767 sdk_version: "none",
5768 system_modules: "none",
5769 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005770
5771 prebuilt_apis {
5772 name: "sdk",
5773 api_dirs: ["100"],
5774 }
Paul Duffin9b879592020-05-26 13:21:35 +01005775 `, withFiles(filesForSdkLibrary))
5776
5777 // java_sdk_library installs both impl jar and permission XML
5778 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5779 "javalib/bar.jar",
5780 "javalib/foo.jar",
5781 "etc/permissions/foo.xml",
5782 })
5783
5784 // The bar library should depend on the implementation jar.
5785 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005786 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005787 t.Errorf("expected %q, found %#q", expected, actual)
5788 }
5789}
5790
5791func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005792 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005793 apex {
5794 name: "myapex",
5795 key: "myapex.key",
5796 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005797 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005798 }
5799
5800 apex_key {
5801 name: "myapex.key",
5802 public_key: "testkey.avbpubkey",
5803 private_key: "testkey.pem",
5804 }
5805
5806 java_sdk_library {
5807 name: "foo",
5808 srcs: ["a.java"],
5809 api_packages: ["foo"],
5810 apex_available: ["myapex"],
5811 sdk_version: "none",
5812 system_modules: "none",
5813 }
5814
5815 java_library {
5816 name: "bar",
5817 srcs: ["a.java"],
5818 libs: ["foo"],
5819 sdk_version: "none",
5820 system_modules: "none",
5821 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005822
5823 prebuilt_apis {
5824 name: "sdk",
5825 api_dirs: ["100"],
5826 }
Paul Duffin9b879592020-05-26 13:21:35 +01005827 `, withFiles(filesForSdkLibrary))
5828
5829 // java_sdk_library installs both impl jar and permission XML
5830 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5831 "javalib/foo.jar",
5832 "etc/permissions/foo.xml",
5833 })
5834
5835 // The bar library should depend on the stubs jar.
5836 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005837 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005838 t.Errorf("expected %q, found %#q", expected, actual)
5839 }
5840}
5841
Paul Duffineedc5d52020-06-12 17:46:39 +01005842func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005843 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00005844 prebuilt_apis {
5845 name: "sdk",
5846 api_dirs: ["100"],
5847 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005848 withFiles(map[string][]byte{
5849 "apex/a.java": nil,
5850 "apex/apex_manifest.json": nil,
5851 "apex/Android.bp": []byte(`
5852 package {
5853 default_visibility: ["//visibility:private"],
5854 }
5855
5856 apex {
5857 name: "myapex",
5858 key: "myapex.key",
5859 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005860 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005861 }
5862
5863 apex_key {
5864 name: "myapex.key",
5865 public_key: "testkey.avbpubkey",
5866 private_key: "testkey.pem",
5867 }
5868
5869 java_library {
5870 name: "bar",
5871 srcs: ["a.java"],
5872 libs: ["foo"],
5873 apex_available: ["myapex"],
5874 sdk_version: "none",
5875 system_modules: "none",
5876 }
5877`),
5878 "source/a.java": nil,
5879 "source/api/current.txt": nil,
5880 "source/api/removed.txt": nil,
5881 "source/Android.bp": []byte(`
5882 package {
5883 default_visibility: ["//visibility:private"],
5884 }
5885
5886 java_sdk_library {
5887 name: "foo",
5888 visibility: ["//apex"],
5889 srcs: ["a.java"],
5890 api_packages: ["foo"],
5891 apex_available: ["myapex"],
5892 sdk_version: "none",
5893 system_modules: "none",
5894 public: {
5895 enabled: true,
5896 },
5897 }
5898`),
5899 "prebuilt/a.jar": nil,
5900 "prebuilt/Android.bp": []byte(`
5901 package {
5902 default_visibility: ["//visibility:private"],
5903 }
5904
5905 java_sdk_library_import {
5906 name: "foo",
5907 visibility: ["//apex", "//source"],
5908 apex_available: ["myapex"],
5909 prefer: true,
5910 public: {
5911 jars: ["a.jar"],
5912 },
5913 }
5914`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005915 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005916 )
5917
5918 // java_sdk_library installs both impl jar and permission XML
5919 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5920 "javalib/bar.jar",
5921 "javalib/foo.jar",
5922 "etc/permissions/foo.xml",
5923 })
5924
5925 // The bar library should depend on the implementation jar.
5926 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005927 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01005928 t.Errorf("expected %q, found %#q", expected, actual)
5929 }
5930}
5931
5932func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5933 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5934 apex {
5935 name: "myapex",
5936 key: "myapex.key",
5937 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005938 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005939 }
5940
5941 apex_key {
5942 name: "myapex.key",
5943 public_key: "testkey.avbpubkey",
5944 private_key: "testkey.pem",
5945 }
5946
5947 java_sdk_library_import {
5948 name: "foo",
5949 apex_available: ["myapex"],
5950 prefer: true,
5951 public: {
5952 jars: ["a.jar"],
5953 },
5954 }
5955
5956 `, withFiles(filesForSdkLibrary))
5957}
5958
atrost6e126252020-01-27 17:01:16 +00005959func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01005960 result := android.GroupFixturePreparers(
5961 prepareForApexTest,
5962 java.PrepareForTestWithPlatformCompatConfig,
5963 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00005964 apex {
5965 name: "myapex",
5966 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00005967 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00005968 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005969 updatable: false,
atrost6e126252020-01-27 17:01:16 +00005970 }
5971
5972 apex_key {
5973 name: "myapex.key",
5974 public_key: "testkey.avbpubkey",
5975 private_key: "testkey.pem",
5976 }
5977
5978 platform_compat_config {
5979 name: "myjar-platform-compat-config",
5980 src: ":myjar",
5981 }
5982
5983 java_library {
5984 name: "myjar",
5985 srcs: ["foo/bar/MyClass.java"],
5986 sdk_version: "none",
5987 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005988 apex_available: [ "myapex" ],
5989 }
Paul Duffin1b29e002021-03-16 15:06:54 +00005990
5991 // Make sure that a preferred prebuilt does not affect the apex contents.
5992 prebuilt_platform_compat_config {
5993 name: "myjar-platform-compat-config",
5994 metadata: "compat-config/metadata.xml",
5995 prefer: true,
5996 }
atrost6e126252020-01-27 17:01:16 +00005997 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00005998 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00005999 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6000 "etc/compatconfig/myjar-platform-compat-config.xml",
6001 "javalib/myjar.jar",
6002 })
6003}
6004
Jiyong Park479321d2019-12-16 11:47:12 +09006005func TestRejectNonInstallableJavaLibrary(t *testing.T) {
6006 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
6007 apex {
6008 name: "myapex",
6009 key: "myapex.key",
6010 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006011 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09006012 }
6013
6014 apex_key {
6015 name: "myapex.key",
6016 public_key: "testkey.avbpubkey",
6017 private_key: "testkey.pem",
6018 }
6019
6020 java_library {
6021 name: "myjar",
6022 srcs: ["foo/bar/MyClass.java"],
6023 sdk_version: "none",
6024 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09006025 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006026 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09006027 }
6028 `)
6029}
6030
Jiyong Park7afd1072019-12-30 16:56:33 +09006031func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006032 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09006033 apex {
6034 name: "myapex",
6035 key: "myapex.key",
6036 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006037 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09006038 }
6039
6040 apex_key {
6041 name: "myapex.key",
6042 public_key: "testkey.avbpubkey",
6043 private_key: "testkey.pem",
6044 }
6045
6046 cc_library {
6047 name: "mylib",
6048 srcs: ["mylib.cpp"],
6049 system_shared_libs: [],
6050 stl: "none",
6051 required: ["a", "b"],
6052 host_required: ["c", "d"],
6053 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006054 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006055 }
6056 `)
6057
6058 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006059 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006060 name := apexBundle.BaseModuleName()
6061 prefix := "TARGET_"
6062 var builder strings.Builder
6063 data.Custom(&builder, name, prefix, "", data)
6064 androidMk := builder.String()
6065 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6066 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6067 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6068}
6069
Jiyong Park7cd10e32020-01-14 09:22:18 +09006070func TestSymlinksFromApexToSystem(t *testing.T) {
6071 bp := `
6072 apex {
6073 name: "myapex",
6074 key: "myapex.key",
6075 native_shared_libs: ["mylib"],
6076 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006077 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006078 }
6079
Jiyong Park9d677202020-02-19 16:29:35 +09006080 apex {
6081 name: "myapex.updatable",
6082 key: "myapex.key",
6083 native_shared_libs: ["mylib"],
6084 java_libs: ["myjar"],
6085 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006086 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006087 }
6088
Jiyong Park7cd10e32020-01-14 09:22:18 +09006089 apex_key {
6090 name: "myapex.key",
6091 public_key: "testkey.avbpubkey",
6092 private_key: "testkey.pem",
6093 }
6094
6095 cc_library {
6096 name: "mylib",
6097 srcs: ["mylib.cpp"],
6098 shared_libs: ["myotherlib"],
6099 system_shared_libs: [],
6100 stl: "none",
6101 apex_available: [
6102 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006103 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006104 "//apex_available:platform",
6105 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006106 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006107 }
6108
6109 cc_library {
6110 name: "myotherlib",
6111 srcs: ["mylib.cpp"],
6112 system_shared_libs: [],
6113 stl: "none",
6114 apex_available: [
6115 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006116 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006117 "//apex_available:platform",
6118 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006119 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006120 }
6121
6122 java_library {
6123 name: "myjar",
6124 srcs: ["foo/bar/MyClass.java"],
6125 sdk_version: "none",
6126 system_modules: "none",
6127 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006128 apex_available: [
6129 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006130 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006131 "//apex_available:platform",
6132 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006133 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006134 }
6135
6136 java_library {
6137 name: "myotherjar",
6138 srcs: ["foo/bar/MyClass.java"],
6139 sdk_version: "none",
6140 system_modules: "none",
6141 apex_available: [
6142 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006143 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006144 "//apex_available:platform",
6145 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006146 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006147 }
6148 `
6149
6150 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6151 for _, f := range files {
6152 if f.path == file {
6153 if f.isLink {
6154 t.Errorf("%q is not a real file", file)
6155 }
6156 return
6157 }
6158 }
6159 t.Errorf("%q is not found", file)
6160 }
6161
6162 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6163 for _, f := range files {
6164 if f.path == file {
6165 if !f.isLink {
6166 t.Errorf("%q is not a symlink", file)
6167 }
6168 return
6169 }
6170 }
6171 t.Errorf("%q is not found", file)
6172 }
6173
Jiyong Park9d677202020-02-19 16:29:35 +09006174 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6175 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006176 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006177 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006178 ensureRealfileExists(t, files, "javalib/myjar.jar")
6179 ensureRealfileExists(t, files, "lib64/mylib.so")
6180 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6181
Jiyong Park9d677202020-02-19 16:29:35 +09006182 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6183 ensureRealfileExists(t, files, "javalib/myjar.jar")
6184 ensureRealfileExists(t, files, "lib64/mylib.so")
6185 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6186
6187 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006188 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006189 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006190 ensureRealfileExists(t, files, "javalib/myjar.jar")
6191 ensureRealfileExists(t, files, "lib64/mylib.so")
6192 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006193
6194 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6195 ensureRealfileExists(t, files, "javalib/myjar.jar")
6196 ensureRealfileExists(t, files, "lib64/mylib.so")
6197 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006198}
6199
Yo Chiange8128052020-07-23 20:09:18 +08006200func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006201 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006202 apex {
6203 name: "myapex",
6204 key: "myapex.key",
6205 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006206 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006207 }
6208
6209 apex_key {
6210 name: "myapex.key",
6211 public_key: "testkey.avbpubkey",
6212 private_key: "testkey.pem",
6213 }
6214
6215 cc_library_shared {
6216 name: "mylib",
6217 srcs: ["mylib.cpp"],
6218 shared_libs: ["myotherlib"],
6219 system_shared_libs: [],
6220 stl: "none",
6221 apex_available: [
6222 "myapex",
6223 "//apex_available:platform",
6224 ],
6225 }
6226
6227 cc_prebuilt_library_shared {
6228 name: "myotherlib",
6229 srcs: ["prebuilt.so"],
6230 system_shared_libs: [],
6231 stl: "none",
6232 apex_available: [
6233 "myapex",
6234 "//apex_available:platform",
6235 ],
6236 }
6237 `)
6238
6239 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006240 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006241 var builder strings.Builder
6242 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6243 androidMk := builder.String()
6244 // `myotherlib` is added to `myapex` as symlink
6245 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6246 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6247 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6248 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006249 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 +08006250}
6251
Jooyung Han643adc42020-02-27 13:50:06 +09006252func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006253 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006254 apex {
6255 name: "myapex",
6256 key: "myapex.key",
6257 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006258 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006259 }
6260
6261 apex_key {
6262 name: "myapex.key",
6263 public_key: "testkey.avbpubkey",
6264 private_key: "testkey.pem",
6265 }
6266
6267 cc_library {
6268 name: "mylib",
6269 srcs: ["mylib.cpp"],
6270 shared_libs: ["mylib2"],
6271 system_shared_libs: [],
6272 stl: "none",
6273 apex_available: [ "myapex" ],
6274 }
6275
6276 cc_library {
6277 name: "mylib2",
6278 srcs: ["mylib.cpp"],
6279 system_shared_libs: [],
6280 stl: "none",
6281 apex_available: [ "myapex" ],
6282 }
6283 `)
6284
6285 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6286 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6287 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6288 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6289 "lib64/mylib.so",
6290 "lib64/mylib2.so",
6291 })
6292}
6293
Jooyung Han49f67012020-04-17 13:43:10 +09006294func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006295 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006296 apex {
6297 name: "myapex",
6298 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006299 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006300 }
6301 apex_key {
6302 name: "myapex.key",
6303 public_key: "testkey.avbpubkey",
6304 private_key: "testkey.pem",
6305 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006306 `,
6307 android.FixtureModifyConfig(func(config android.Config) {
6308 delete(config.Targets, android.Android)
6309 config.AndroidCommonTarget = android.Target{}
6310 }),
6311 )
Jooyung Han49f67012020-04-17 13:43:10 +09006312
6313 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6314 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6315 }
6316}
6317
Jiyong Parkbd159612020-02-28 15:22:21 +09006318func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006319 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006320 apex {
6321 name: "myapex",
6322 key: "myapex.key",
6323 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006324 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006325 }
6326
6327 apex_key {
6328 name: "myapex.key",
6329 public_key: "testkey.avbpubkey",
6330 private_key: "testkey.pem",
6331 }
6332
6333 android_app {
6334 name: "AppFoo",
6335 srcs: ["foo/bar/MyClass.java"],
6336 sdk_version: "none",
6337 system_modules: "none",
6338 apex_available: [ "myapex" ],
6339 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006340 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006341
Colin Crosscf371cc2020-11-13 11:48:42 -08006342 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006343 content := bundleConfigRule.Args["content"]
6344
6345 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006346 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 +09006347}
6348
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006349func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006350 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006351 apex {
6352 name: "myapex",
6353 key: "myapex.key",
6354 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006355 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006356 }
6357
6358 apex_key {
6359 name: "myapex.key",
6360 public_key: "testkey.avbpubkey",
6361 private_key: "testkey.pem",
6362 }
6363
6364 android_app_set {
6365 name: "AppSet",
6366 set: "AppSet.apks",
6367 }`)
6368 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006369 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006370 content := bundleConfigRule.Args["content"]
6371 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6372 s := mod.Rule("apexRule").Args["copy_commands"]
6373 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6374 if len(copyCmds) != 3 {
6375 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6376 }
6377 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6378 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6379 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6380}
6381
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006382func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01006383 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006384 apex_set {
6385 name: "myapex",
6386 filename: "foo_v2.apex",
6387 sanitized: {
6388 none: { set: "myapex.apks", },
6389 hwaddress: { set: "myapex.hwasan.apks", },
6390 },
Paul Duffin24704672021-04-06 16:09:30 +01006391 }
6392 `
6393 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006394
Paul Duffin24704672021-04-06 16:09:30 +01006395 // Check that the extractor produces the correct output file from the correct input file.
6396 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006397
Paul Duffin24704672021-04-06 16:09:30 +01006398 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
6399 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006400
Paul Duffin24704672021-04-06 16:09:30 +01006401 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
6402
6403 // Ditto for the apex.
Paul Duffin5ec165d2021-06-15 19:09:41 +01006404 m = ctx.ModuleForTests("myapex", "android_common_myapex")
6405 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01006406
6407 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006408}
6409
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006410func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006411 t.Helper()
6412
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006413 bp := `
6414 java_library {
6415 name: "some-updatable-apex-lib",
6416 srcs: ["a.java"],
6417 sdk_version: "current",
6418 apex_available: [
6419 "some-updatable-apex",
6420 ],
6421 }
6422
6423 java_library {
6424 name: "some-non-updatable-apex-lib",
6425 srcs: ["a.java"],
6426 apex_available: [
6427 "some-non-updatable-apex",
6428 ],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006429 compile_dex: true,
6430 }
6431
6432 bootclasspath_fragment {
6433 name: "some-non-updatable-fragment",
6434 contents: ["some-non-updatable-apex-lib"],
6435 apex_available: [
6436 "some-non-updatable-apex",
6437 ],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006438 }
6439
6440 java_library {
6441 name: "some-platform-lib",
6442 srcs: ["a.java"],
6443 sdk_version: "current",
6444 installable: true,
6445 }
6446
6447 java_library {
6448 name: "some-art-lib",
6449 srcs: ["a.java"],
6450 sdk_version: "current",
6451 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006452 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006453 ],
6454 hostdex: true,
Paul Duffinc75bbce2021-06-07 13:28:19 +01006455 compile_dex: true,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006456 }
6457
6458 apex {
6459 name: "some-updatable-apex",
6460 key: "some-updatable-apex.key",
6461 java_libs: ["some-updatable-apex-lib"],
6462 updatable: true,
6463 min_sdk_version: "current",
6464 }
6465
6466 apex {
6467 name: "some-non-updatable-apex",
6468 key: "some-non-updatable-apex.key",
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006469 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006470 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006471 }
6472
6473 apex_key {
6474 name: "some-updatable-apex.key",
6475 }
6476
6477 apex_key {
6478 name: "some-non-updatable-apex.key",
6479 }
6480
6481 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006482 name: "com.android.art.debug",
6483 key: "com.android.art.debug.key",
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006484 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006485 updatable: true,
6486 min_sdk_version: "current",
6487 }
6488
Paul Duffinf23bc472021-04-27 12:42:20 +01006489 bootclasspath_fragment {
6490 name: "art-bootclasspath-fragment",
6491 image_name: "art",
6492 contents: ["some-art-lib"],
6493 apex_available: [
6494 "com.android.art.debug",
6495 ],
6496 }
6497
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006498 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006499 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006500 }
6501
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006502 filegroup {
6503 name: "some-updatable-apex-file_contexts",
6504 srcs: [
6505 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6506 ],
6507 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006508
6509 filegroup {
6510 name: "some-non-updatable-apex-file_contexts",
6511 srcs: [
6512 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6513 ],
6514 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006515 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006516
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006517 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00006518}
6519
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006520func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006521 t.Helper()
6522
Paul Duffin55607122021-03-30 23:32:51 +01006523 fs := android.MockFS{
6524 "a.java": nil,
6525 "a.jar": nil,
6526 "apex_manifest.json": nil,
6527 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006528 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006529 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6530 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6531 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006532 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006533 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006534
Paul Duffin55607122021-03-30 23:32:51 +01006535 errorHandler := android.FixtureExpectsNoErrors
6536 if errmsg != "" {
6537 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006538 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006539
Paul Duffin55607122021-03-30 23:32:51 +01006540 result := android.GroupFixturePreparers(
6541 cc.PrepareForTestWithCcDefaultModules,
6542 java.PrepareForTestWithHiddenApiBuildComponents,
6543 java.PrepareForTestWithJavaDefaultModules,
6544 java.PrepareForTestWithJavaSdkLibraryFiles,
6545 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01006546 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01006547 fs.AddToFixture(),
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006548 android.FixtureModifyMockFS(func(fs android.MockFS) {
6549 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
6550 insert := ""
6551 for _, fragment := range fragments {
6552 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
6553 }
6554 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
6555 platform_bootclasspath {
6556 name: "platform-bootclasspath",
6557 fragments: [
6558 %s
6559 ],
6560 }
6561 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01006562 }
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006563 }),
Paul Duffin55607122021-03-30 23:32:51 +01006564 ).
6565 ExtendWithErrorHandler(errorHandler).
6566 RunTestWithBp(t, bp)
6567
6568 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006569}
6570
Jooyung Han548640b2020-04-27 12:10:30 +09006571func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6572 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6573 apex {
6574 name: "myapex",
6575 key: "myapex.key",
6576 updatable: true,
6577 }
6578
6579 apex_key {
6580 name: "myapex.key",
6581 public_key: "testkey.avbpubkey",
6582 private_key: "testkey.pem",
6583 }
6584 `)
6585}
6586
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006587func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6588 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6589 apex {
6590 name: "myapex",
6591 key: "myapex.key",
6592 }
6593
6594 apex_key {
6595 name: "myapex.key",
6596 public_key: "testkey.avbpubkey",
6597 private_key: "testkey.pem",
6598 }
6599 `)
6600}
6601
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006602func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006603 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
6604 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
6605 // modules to be included in the BootJars.
6606 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
6607 return android.GroupFixturePreparers(
6608 dexpreopt.FixtureSetBootJars(bootJars...),
6609 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6610 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
6611 }),
6612 )
6613 }
6614
6615 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
6616 // same value. This can result in an invalid configuration as it allows non art apex jars to be
6617 // specified in the ArtApexJars configuration.
6618 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
6619 return android.GroupFixturePreparers(
6620 dexpreopt.FixtureSetArtBootJars(bootJars...),
6621 dexpreopt.FixtureSetBootJars(bootJars...),
6622 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6623 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
6624 }),
6625 )
6626 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006627
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006628 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006629 preparer := java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib")
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006630 fragment := java.ApexVariantReference{
6631 Apex: proptools.StringPtr("com.android.art.debug"),
6632 Module: proptools.StringPtr("art-bootclasspath-fragment"),
6633 }
6634 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
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("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006638 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
6639 // Update the dexpreopt BootJars directly.
6640 preparer := prepareSetBootJars("com.android.art.debug:some-art-lib")
6641 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006642 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006643
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006644 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 +01006645 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 +01006646 // Update the dexpreopt ArtApexJars directly.
6647 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
6648 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006649 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006650
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006651 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 +01006652 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 +01006653 // Update the dexpreopt ArtApexJars directly.
6654 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
6655 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006656 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006657
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006658 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 +01006659 err := `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the framework boot image`
6660 preparer := java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib")
6661 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006662 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006663
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006664 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 +01006665 preparer := java.FixtureConfigureBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006666 fragment := java.ApexVariantReference{
6667 Apex: proptools.StringPtr("some-non-updatable-apex"),
6668 Module: proptools.StringPtr("some-non-updatable-fragment"),
6669 }
6670 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006671 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006672
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006673 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01006674 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01006675 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
6676 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006677 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006678
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006679 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01006680 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01006681 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
6682 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006683 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006684
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006685 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01006686 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01006687 // Update the dexpreopt ArtApexJars directly.
6688 preparer := prepareSetArtJars("platform:some-platform-lib")
6689 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006690 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006691
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006692 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006693 preparer := java.FixtureConfigureBootJars("platform:some-platform-lib")
6694 testNoUpdatableJarsInBootImage(t, "", preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006695 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006696}
6697
6698func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006699 preparer := java.FixtureConfigureBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00006700 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006701 fragment := java.ApexVariantReference{
6702 Apex: proptools.StringPtr("myapex"),
6703 Module: proptools.StringPtr("my-bootclasspath-fragment"),
6704 }
6705
Paul Duffin064b70c2020-11-02 17:32:38 +00006706 testDexpreoptWithApexes(t, `
6707 prebuilt_apex {
6708 name: "myapex" ,
6709 arch: {
6710 arm64: {
6711 src: "myapex-arm64.apex",
6712 },
6713 arm: {
6714 src: "myapex-arm.apex",
6715 },
6716 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006717 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
6718 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006719
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006720 prebuilt_bootclasspath_fragment {
6721 name: "my-bootclasspath-fragment",
6722 contents: ["libfoo"],
6723 apex_available: ["myapex"],
6724 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006725
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006726 java_import {
6727 name: "libfoo",
6728 jars: ["libfoo.jar"],
6729 apex_available: ["myapex"],
6730 }
6731 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00006732 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006733}
6734
Andrei Onea115e7e72020-06-05 21:14:03 +01006735func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6736 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006737 bp += `
6738 apex_key {
6739 name: "myapex.key",
6740 public_key: "testkey.avbpubkey",
6741 private_key: "testkey.pem",
6742 }`
Paul Duffin45338f02021-03-30 23:07:52 +01006743 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01006744 "lib1/src/A.java": nil,
6745 "lib2/src/B.java": nil,
6746 "system/sepolicy/apex/myapex-file_contexts": nil,
6747 }
6748
Paul Duffin45338f02021-03-30 23:07:52 +01006749 errorHandler := android.FixtureExpectsNoErrors
6750 if errmsg != "" {
6751 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07006752 }
Colin Crossae8600b2020-10-29 17:09:13 -07006753
Paul Duffin45338f02021-03-30 23:07:52 +01006754 android.GroupFixturePreparers(
6755 android.PrepareForTestWithAndroidBuildComponents,
6756 java.PrepareForTestWithJavaBuildComponents,
6757 PrepareForTestWithApexBuildComponents,
6758 android.PrepareForTestWithNeverallowRules(rules),
6759 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6760 updatableBootJars := make([]string, 0, len(apexBootJars))
6761 for _, apexBootJar := range apexBootJars {
6762 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6763 }
6764 variables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6765 }),
6766 fs.AddToFixture(),
6767 ).
6768 ExtendWithErrorHandler(errorHandler).
6769 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01006770}
6771
6772func TestApexPermittedPackagesRules(t *testing.T) {
6773 testcases := []struct {
6774 name string
6775 expectedError string
6776 bp string
6777 bootJars []string
6778 modulesPackages map[string][]string
6779 }{
6780
6781 {
6782 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6783 expectedError: "",
6784 bp: `
6785 java_library {
6786 name: "bcp_lib1",
6787 srcs: ["lib1/src/*.java"],
6788 permitted_packages: ["foo.bar"],
6789 apex_available: ["myapex"],
6790 sdk_version: "none",
6791 system_modules: "none",
6792 }
6793 java_library {
6794 name: "nonbcp_lib2",
6795 srcs: ["lib2/src/*.java"],
6796 apex_available: ["myapex"],
6797 permitted_packages: ["a.b"],
6798 sdk_version: "none",
6799 system_modules: "none",
6800 }
6801 apex {
6802 name: "myapex",
6803 key: "myapex.key",
6804 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006805 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006806 }`,
6807 bootJars: []string{"bcp_lib1"},
6808 modulesPackages: map[string][]string{
6809 "myapex": []string{
6810 "foo.bar",
6811 },
6812 },
6813 },
6814 {
6815 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6816 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.`,
6817 bp: `
6818 java_library {
6819 name: "bcp_lib1",
6820 srcs: ["lib1/src/*.java"],
6821 apex_available: ["myapex"],
6822 permitted_packages: ["foo.bar"],
6823 sdk_version: "none",
6824 system_modules: "none",
6825 }
6826 java_library {
6827 name: "bcp_lib2",
6828 srcs: ["lib2/src/*.java"],
6829 apex_available: ["myapex"],
6830 permitted_packages: ["foo.bar", "bar.baz"],
6831 sdk_version: "none",
6832 system_modules: "none",
6833 }
6834 apex {
6835 name: "myapex",
6836 key: "myapex.key",
6837 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006838 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006839 }
6840 `,
6841 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6842 modulesPackages: map[string][]string{
6843 "myapex": []string{
6844 "foo.bar",
6845 },
6846 },
6847 },
6848 }
6849 for _, tc := range testcases {
6850 t.Run(tc.name, func(t *testing.T) {
6851 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6852 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6853 })
6854 }
6855}
6856
Jiyong Park62304bb2020-04-13 16:19:48 +09006857func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006858 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09006859 apex {
6860 name: "myapex",
6861 key: "myapex.key",
6862 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006863 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09006864 }
6865
6866 apex_key {
6867 name: "myapex.key",
6868 public_key: "testkey.avbpubkey",
6869 private_key: "testkey.pem",
6870 }
6871
6872 cc_library {
6873 name: "mylib",
6874 srcs: ["mylib.cpp"],
6875 system_shared_libs: [],
6876 stl: "none",
6877 stubs: {
6878 versions: ["1"],
6879 },
6880 apex_available: ["myapex"],
6881 }
6882
6883 cc_library {
6884 name: "myprivlib",
6885 srcs: ["mylib.cpp"],
6886 system_shared_libs: [],
6887 stl: "none",
6888 apex_available: ["myapex"],
6889 }
6890
6891
6892 cc_test {
6893 name: "mytest",
6894 gtest: false,
6895 srcs: ["mylib.cpp"],
6896 system_shared_libs: [],
6897 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006898 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006899 test_for: ["myapex"]
6900 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006901
6902 cc_library {
6903 name: "mytestlib",
6904 srcs: ["mylib.cpp"],
6905 system_shared_libs: [],
6906 shared_libs: ["mylib", "myprivlib"],
6907 stl: "none",
6908 test_for: ["myapex"],
6909 }
6910
6911 cc_benchmark {
6912 name: "mybench",
6913 srcs: ["mylib.cpp"],
6914 system_shared_libs: [],
6915 shared_libs: ["mylib", "myprivlib"],
6916 stl: "none",
6917 test_for: ["myapex"],
6918 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006919 `)
6920
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006921 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01006922 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006923 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6924 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6925 }
6926
6927 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09006928 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006929 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6930 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6931 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6932}
Jiyong Park46a512f2020-12-04 18:02:13 +09006933
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006934func TestIndirectTestFor(t *testing.T) {
6935 ctx := testApex(t, `
6936 apex {
6937 name: "myapex",
6938 key: "myapex.key",
6939 native_shared_libs: ["mylib", "myprivlib"],
6940 updatable: false,
6941 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006942
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006943 apex_key {
6944 name: "myapex.key",
6945 public_key: "testkey.avbpubkey",
6946 private_key: "testkey.pem",
6947 }
6948
6949 cc_library {
6950 name: "mylib",
6951 srcs: ["mylib.cpp"],
6952 system_shared_libs: [],
6953 stl: "none",
6954 stubs: {
6955 versions: ["1"],
6956 },
6957 apex_available: ["myapex"],
6958 }
6959
6960 cc_library {
6961 name: "myprivlib",
6962 srcs: ["mylib.cpp"],
6963 system_shared_libs: [],
6964 stl: "none",
6965 shared_libs: ["mylib"],
6966 apex_available: ["myapex"],
6967 }
6968
6969 cc_library {
6970 name: "mytestlib",
6971 srcs: ["mylib.cpp"],
6972 system_shared_libs: [],
6973 shared_libs: ["myprivlib"],
6974 stl: "none",
6975 test_for: ["myapex"],
6976 }
6977 `)
6978
6979 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01006980 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006981 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6982 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6983 }
6984
6985 // The platform variant of mytestlib links to the platform variant of the
6986 // internal myprivlib.
6987 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
6988
6989 // The platform variant of myprivlib links to the platform variant of mylib
6990 // and bypasses its stubs.
6991 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 +09006992}
6993
Martin Stjernholmec009002021-03-27 15:18:31 +00006994func TestTestForForLibInOtherApex(t *testing.T) {
6995 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
6996 _ = testApex(t, `
6997 apex {
6998 name: "com.android.art",
6999 key: "myapex.key",
7000 native_shared_libs: ["mylib"],
7001 updatable: false,
7002 }
7003
7004 apex {
7005 name: "com.android.art.debug",
7006 key: "myapex.key",
7007 native_shared_libs: ["mylib", "mytestlib"],
7008 updatable: false,
7009 }
7010
7011 apex_key {
7012 name: "myapex.key",
7013 public_key: "testkey.avbpubkey",
7014 private_key: "testkey.pem",
7015 }
7016
7017 cc_library {
7018 name: "mylib",
7019 srcs: ["mylib.cpp"],
7020 system_shared_libs: [],
7021 stl: "none",
7022 stubs: {
7023 versions: ["1"],
7024 },
7025 apex_available: ["com.android.art", "com.android.art.debug"],
7026 }
7027
7028 cc_library {
7029 name: "mytestlib",
7030 srcs: ["mylib.cpp"],
7031 system_shared_libs: [],
7032 shared_libs: ["mylib"],
7033 stl: "none",
7034 apex_available: ["com.android.art.debug"],
7035 test_for: ["com.android.art"],
7036 }
7037 `,
7038 android.MockFS{
7039 "system/sepolicy/apex/com.android.art-file_contexts": nil,
7040 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
7041 }.AddToFixture())
7042}
7043
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007044// TODO(jungjw): Move this to proptools
7045func intPtr(i int) *int {
7046 return &i
7047}
7048
7049func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007050 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007051 apex_set {
7052 name: "myapex",
7053 set: "myapex.apks",
7054 filename: "foo_v2.apex",
7055 overrides: ["foo"],
7056 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007057 `,
7058 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7059 variables.Platform_sdk_version = intPtr(30)
7060 }),
7061 android.FixtureModifyConfig(func(config android.Config) {
7062 config.Targets[android.Android] = []android.Target{
7063 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
7064 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
7065 }
7066 }),
7067 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007068
Paul Duffin24704672021-04-06 16:09:30 +01007069 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007070
7071 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01007072 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007073 actual := extractedApex.Args["abis"]
7074 expected := "ARMEABI_V7A,ARM64_V8A"
7075 if actual != expected {
7076 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7077 }
7078 actual = extractedApex.Args["sdk-version"]
7079 expected = "30"
7080 if actual != expected {
7081 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7082 }
7083
Paul Duffin5ec165d2021-06-15 19:09:41 +01007084 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007085 a := m.Module().(*ApexSet)
7086 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07007087 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007088 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
7089 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
7090 }
7091}
7092
Jiyong Park7d95a512020-05-10 15:16:24 +09007093func TestNoStaticLinkingToStubsLib(t *testing.T) {
7094 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
7095 apex {
7096 name: "myapex",
7097 key: "myapex.key",
7098 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007099 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09007100 }
7101
7102 apex_key {
7103 name: "myapex.key",
7104 public_key: "testkey.avbpubkey",
7105 private_key: "testkey.pem",
7106 }
7107
7108 cc_library {
7109 name: "mylib",
7110 srcs: ["mylib.cpp"],
7111 static_libs: ["otherlib"],
7112 system_shared_libs: [],
7113 stl: "none",
7114 apex_available: [ "myapex" ],
7115 }
7116
7117 cc_library {
7118 name: "otherlib",
7119 srcs: ["mylib.cpp"],
7120 system_shared_libs: [],
7121 stl: "none",
7122 stubs: {
7123 versions: ["1", "2", "3"],
7124 },
7125 apex_available: [ "myapex" ],
7126 }
7127 `)
7128}
7129
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007130func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007131 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007132 apex {
7133 name: "myapex",
7134 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007135 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007136 }
7137
7138 apex_key {
7139 name: "myapex.key",
7140 public_key: "testkey.avbpubkey",
7141 private_key: "testkey.pem",
7142 }
7143
7144 prebuilt_apex {
7145 name: "myapex",
7146 prefer: true,
7147 arch: {
7148 arm64: {
7149 src: "myapex-arm64.apex",
7150 },
7151 arm: {
7152 src: "myapex-arm.apex",
7153 },
7154 },
7155 }
7156
7157 apex_set {
7158 name: "myapex_set",
7159 set: "myapex.apks",
7160 filename: "myapex_set.apex",
7161 overrides: ["myapex"],
7162 }
7163 `)
7164
7165 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7166 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7167 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 +09007168 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 +09007169}
7170
Jooyung Han938b5932020-06-20 12:47:47 +09007171func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007172 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007173 apex {
7174 name: "myapex",
7175 key: "myapex.key",
7176 apps: ["app"],
7177 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007178 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007179 }
7180
7181 apex_key {
7182 name: "myapex.key",
7183 public_key: "testkey.avbpubkey",
7184 private_key: "testkey.pem",
7185 }
7186
7187 android_app {
7188 name: "app",
7189 srcs: ["foo/bar/MyClass.java"],
7190 package_name: "foo",
7191 sdk_version: "none",
7192 system_modules: "none",
7193 apex_available: [ "myapex" ],
7194 }
7195 `, withFiles(map[string][]byte{
7196 "sub/Android.bp": []byte(`
7197 override_apex {
7198 name: "override_myapex",
7199 base: "myapex",
7200 apps: ["override_app"],
7201 allowed_files: ":allowed",
7202 }
7203 // Overridable "path" property should be referenced indirectly
7204 filegroup {
7205 name: "allowed",
7206 srcs: ["allowed.txt"],
7207 }
7208 override_android_app {
7209 name: "override_app",
7210 base: "app",
7211 package_name: "bar",
7212 }
7213 `),
7214 }))
7215
7216 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7217 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7218 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7219 }
7220
7221 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7222 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7223 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7224 }
7225}
7226
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007227func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007228 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007229 apex {
7230 name: "myapex",
7231 key: "myapex.key",
7232 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007233 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007234 }
7235
7236 apex_key {
7237 name: "myapex.key",
7238 public_key: "testkey.avbpubkey",
7239 private_key: "testkey.pem",
7240 }
7241
7242 cc_library {
7243 name: "mylib",
7244 srcs: ["mylib.cpp"],
7245 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007246 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007247 },
7248 apex_available: ["myapex"],
7249 }
7250
7251 cc_prebuilt_library_shared {
7252 name: "mylib",
7253 prefer: false,
7254 srcs: ["prebuilt.so"],
7255 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007256 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007257 },
7258 apex_available: ["myapex"],
7259 }
7260 `)
7261}
7262
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007263func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007264 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007265 apex {
7266 name: "myapex",
7267 key: "myapex.key",
7268 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007269 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007270 }
7271 apex_key {
7272 name: "myapex.key",
7273 public_key: "testkey.avbpubkey",
7274 private_key: "testkey.pem",
7275 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007276 `,
7277 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7278 variables.CompressedApex = proptools.BoolPtr(true)
7279 }),
7280 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007281
7282 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7283 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7284
7285 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7286 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7287
7288 // Make sure output of bundle is .capex
7289 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7290 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7291
7292 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007293 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007294 var builder strings.Builder
7295 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7296 androidMk := builder.String()
7297 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7298}
7299
Martin Stjernholm2856c662020-12-02 15:03:42 +00007300func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007301 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007302 apex {
7303 name: "myapex",
7304 key: "myapex.key",
7305 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007306 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007307 }
7308
7309 apex_key {
7310 name: "myapex.key",
7311 public_key: "testkey.avbpubkey",
7312 private_key: "testkey.pem",
7313 }
7314
7315 cc_library {
7316 name: "mylib",
7317 srcs: ["mylib.cpp"],
7318 apex_available: ["myapex"],
7319 shared_libs: ["otherlib"],
7320 system_shared_libs: [],
7321 }
7322
7323 cc_library {
7324 name: "otherlib",
7325 srcs: ["mylib.cpp"],
7326 stubs: {
7327 versions: ["current"],
7328 },
7329 }
7330
7331 cc_prebuilt_library_shared {
7332 name: "otherlib",
7333 prefer: true,
7334 srcs: ["prebuilt.so"],
7335 stubs: {
7336 versions: ["current"],
7337 },
7338 }
7339 `)
7340
7341 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007342 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007343 var builder strings.Builder
7344 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7345 androidMk := builder.String()
7346
7347 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7348 // a thing there.
7349 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7350}
7351
Jiyong Parke3867542020-12-03 17:28:25 +09007352func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007353 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007354 apex {
7355 name: "myapex",
7356 key: "myapex.key",
7357 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007358 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007359 }
7360
7361 apex_key {
7362 name: "myapex.key",
7363 public_key: "testkey.avbpubkey",
7364 private_key: "testkey.pem",
7365 }
7366
7367 cc_library {
7368 name: "mylib",
7369 srcs: ["mylib.cpp"],
7370 system_shared_libs: [],
7371 stl: "none",
7372 apex_available: ["myapex"],
7373 shared_libs: ["mylib2"],
7374 target: {
7375 apex: {
7376 exclude_shared_libs: ["mylib2"],
7377 },
7378 },
7379 }
7380
7381 cc_library {
7382 name: "mylib2",
7383 srcs: ["mylib.cpp"],
7384 system_shared_libs: [],
7385 stl: "none",
7386 }
7387 `)
7388
7389 // Check if mylib is linked to mylib2 for the non-apex target
7390 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7391 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7392
7393 // Make sure that the link doesn't occur for the apex target
7394 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7395 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7396
7397 // It shouldn't appear in the copy cmd as well.
7398 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7399 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7400}
7401
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007402func TestPrebuiltStubLibDep(t *testing.T) {
7403 bpBase := `
7404 apex {
7405 name: "myapex",
7406 key: "myapex.key",
7407 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007408 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007409 }
7410 apex_key {
7411 name: "myapex.key",
7412 public_key: "testkey.avbpubkey",
7413 private_key: "testkey.pem",
7414 }
7415 cc_library {
7416 name: "mylib",
7417 srcs: ["mylib.cpp"],
7418 apex_available: ["myapex"],
7419 shared_libs: ["stublib"],
7420 system_shared_libs: [],
7421 }
7422 apex {
7423 name: "otherapex",
7424 enabled: %s,
7425 key: "myapex.key",
7426 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007427 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007428 }
7429 `
7430
7431 stublibSourceBp := `
7432 cc_library {
7433 name: "stublib",
7434 srcs: ["mylib.cpp"],
7435 apex_available: ["otherapex"],
7436 system_shared_libs: [],
7437 stl: "none",
7438 stubs: {
7439 versions: ["1"],
7440 },
7441 }
7442 `
7443
7444 stublibPrebuiltBp := `
7445 cc_prebuilt_library_shared {
7446 name: "stublib",
7447 srcs: ["prebuilt.so"],
7448 apex_available: ["otherapex"],
7449 stubs: {
7450 versions: ["1"],
7451 },
7452 %s
7453 }
7454 `
7455
7456 tests := []struct {
7457 name string
7458 stublibBp string
7459 usePrebuilt bool
7460 modNames []string // Modules to collect AndroidMkEntries for
7461 otherApexEnabled []string
7462 }{
7463 {
7464 name: "only_source",
7465 stublibBp: stublibSourceBp,
7466 usePrebuilt: false,
7467 modNames: []string{"stublib"},
7468 otherApexEnabled: []string{"true", "false"},
7469 },
7470 {
7471 name: "source_preferred",
7472 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7473 usePrebuilt: false,
7474 modNames: []string{"stublib", "prebuilt_stublib"},
7475 otherApexEnabled: []string{"true", "false"},
7476 },
7477 {
7478 name: "prebuilt_preferred",
7479 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7480 usePrebuilt: true,
7481 modNames: []string{"stublib", "prebuilt_stublib"},
7482 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7483 },
7484 {
7485 name: "only_prebuilt",
7486 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7487 usePrebuilt: true,
7488 modNames: []string{"stublib"},
7489 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7490 },
7491 }
7492
7493 for _, test := range tests {
7494 t.Run(test.name, func(t *testing.T) {
7495 for _, otherApexEnabled := range test.otherApexEnabled {
7496 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007497 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007498
7499 type modAndMkEntries struct {
7500 mod *cc.Module
7501 mkEntries android.AndroidMkEntries
7502 }
7503 entries := []*modAndMkEntries{}
7504
7505 // Gather shared lib modules that are installable
7506 for _, modName := range test.modNames {
7507 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7508 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7509 continue
7510 }
7511 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007512 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007513 continue
7514 }
Colin Crossaa255532020-07-03 13:18:24 -07007515 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007516 if ent.Disabled {
7517 continue
7518 }
7519 entries = append(entries, &modAndMkEntries{
7520 mod: mod,
7521 mkEntries: ent,
7522 })
7523 }
7524 }
7525 }
7526
7527 var entry *modAndMkEntries = nil
7528 for _, ent := range entries {
7529 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7530 if entry != nil {
7531 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7532 } else {
7533 entry = ent
7534 }
7535 }
7536 }
7537
7538 if entry == nil {
7539 t.Errorf("AndroidMk entry for \"stublib\" missing")
7540 } else {
7541 isPrebuilt := entry.mod.Prebuilt() != nil
7542 if isPrebuilt != test.usePrebuilt {
7543 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7544 }
7545 if !entry.mod.IsStubs() {
7546 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7547 }
7548 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7549 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7550 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007551 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09007552 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09007553 if !android.InList(expected, cflags) {
7554 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7555 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007556 }
7557 })
7558 }
7559 })
7560 }
7561}
7562
Colin Crosscbb2b812021-05-25 18:16:02 -07007563func TestApexJavaCoverage(t *testing.T) {
7564 bp := `
7565 apex {
7566 name: "myapex",
7567 key: "myapex.key",
7568 java_libs: ["mylib"],
7569 bootclasspath_fragments: ["mybootclasspathfragment"],
7570 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
7571 updatable: false,
7572 }
7573
7574 apex_key {
7575 name: "myapex.key",
7576 public_key: "testkey.avbpubkey",
7577 private_key: "testkey.pem",
7578 }
7579
7580 java_library {
7581 name: "mylib",
7582 srcs: ["mylib.java"],
7583 apex_available: ["myapex"],
7584 compile_dex: true,
7585 }
7586
7587 bootclasspath_fragment {
7588 name: "mybootclasspathfragment",
7589 contents: ["mybootclasspathlib"],
7590 apex_available: ["myapex"],
7591 }
7592
7593 java_library {
7594 name: "mybootclasspathlib",
7595 srcs: ["mybootclasspathlib.java"],
7596 apex_available: ["myapex"],
7597 compile_dex: true,
7598 }
7599
7600 systemserverclasspath_fragment {
7601 name: "mysystemserverclasspathfragment",
7602 contents: ["mysystemserverclasspathlib"],
7603 apex_available: ["myapex"],
7604 }
7605
7606 java_library {
7607 name: "mysystemserverclasspathlib",
7608 srcs: ["mysystemserverclasspathlib.java"],
7609 apex_available: ["myapex"],
7610 compile_dex: true,
7611 }
7612 `
7613
7614 result := android.GroupFixturePreparers(
7615 PrepareForTestWithApexBuildComponents,
7616 prepareForTestWithMyapex,
7617 java.PrepareForTestWithJavaDefaultModules,
7618 android.PrepareForTestWithAndroidBuildComponents,
7619 android.FixtureWithRootAndroidBp(bp),
7620 android.FixtureMergeEnv(map[string]string{
7621 "EMMA_INSTRUMENT": "true",
7622 }),
7623 ).RunTest(t)
7624
7625 // Make sure jacoco ran on both mylib and mybootclasspathlib
7626 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
7627 t.Errorf("Failed to find jacoco rule for mylib")
7628 }
7629 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
7630 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
7631 }
7632 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
7633 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
7634 }
7635}
7636
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007637func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01007638 os.Exit(m.Run())
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007639}