blob: f3c3b44c5a0292ace06848efcb2923953580ad02 [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"],
Ken Chen6320efb2021-11-10 22:02:57 +0800625 bpfs: ["bpf", "netd_test"],
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
Ken Chen6320efb2021-11-10 22:02:57 +0800678 bpf {
679 name: "netd_test",
680 srcs: ["netd_test.c"],
681 sub_dir: "netd",
682 }
683
Jooyung Hanf21c7972019-12-16 22:32:06 +0900684 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000685 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900686 "etc/myetc",
687 "javalib/myjar.jar",
688 "lib64/mylib.so",
689 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900690 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800691 "etc/bpf/bpf.o",
692 "etc/bpf/bpf2.o",
Ken Chen6320efb2021-11-10 22:02:57 +0800693 "etc/bpf/netd/netd_test.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900694 })
695}
696
Jooyung Han01a3ee22019-11-02 02:52:25 +0900697func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800698 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900699 apex {
700 name: "myapex",
701 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000702 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900703 }
704
705 apex_key {
706 name: "myapex.key",
707 public_key: "testkey.avbpubkey",
708 private_key: "testkey.pem",
709 }
710 `)
711
712 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900713 args := module.Rule("apexRule").Args
714 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
715 t.Error("manifest should be apex_manifest.pb, but " + manifest)
716 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900717}
718
Alex Light5098a612018-11-29 17:12:15 -0800719func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800720 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800721 apex {
722 name: "myapex",
723 key: "myapex.key",
724 payload_type: "zip",
725 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000726 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800727 }
728
729 apex_key {
730 name: "myapex.key",
731 public_key: "testkey.avbpubkey",
732 private_key: "testkey.pem",
733 }
734
735 cc_library {
736 name: "mylib",
737 srcs: ["mylib.cpp"],
738 shared_libs: ["mylib2"],
739 system_shared_libs: [],
740 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000741 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800742 }
743
744 cc_library {
745 name: "mylib2",
746 srcs: ["mylib.cpp"],
747 system_shared_libs: [],
748 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000749 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800750 }
751 `)
752
Sundong Ahnabb64432019-10-22 13:58:29 +0900753 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800754 copyCmds := zipApexRule.Args["copy_commands"]
755
756 // Ensure that main rule creates an output
757 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
758
759 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700760 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800761
762 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700763 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800764
765 // Ensure that both direct and indirect deps are copied into apex
766 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
767 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900768}
769
770func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800771 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900772 apex {
773 name: "myapex",
774 key: "myapex.key",
775 native_shared_libs: ["mylib", "mylib3"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000776 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900777 }
778
779 apex_key {
780 name: "myapex.key",
781 public_key: "testkey.avbpubkey",
782 private_key: "testkey.pem",
783 }
784
785 cc_library {
786 name: "mylib",
787 srcs: ["mylib.cpp"],
788 shared_libs: ["mylib2", "mylib3"],
789 system_shared_libs: [],
790 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000791 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900792 }
793
794 cc_library {
795 name: "mylib2",
796 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900797 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900798 system_shared_libs: [],
799 stl: "none",
800 stubs: {
801 versions: ["1", "2", "3"],
802 },
803 }
804
805 cc_library {
806 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900807 srcs: ["mylib.cpp"],
808 shared_libs: ["mylib4"],
809 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900810 stl: "none",
811 stubs: {
812 versions: ["10", "11", "12"],
813 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000814 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900815 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900816
817 cc_library {
818 name: "mylib4",
819 srcs: ["mylib.cpp"],
820 system_shared_libs: [],
821 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000822 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900823 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900824 `)
825
Sundong Ahnabb64432019-10-22 13:58:29 +0900826 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900827 copyCmds := apexRule.Args["copy_commands"]
828
829 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800830 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900831
832 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800833 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900834
835 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800836 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900837
Colin Crossaede88c2020-08-11 12:17:01 -0700838 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900839
840 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900841 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900842 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900843 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900844
845 // 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 -0700846 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900847 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700848 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900849
850 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900851 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900852 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900853
854 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700855 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900856
Jooyung Hana57af4a2020-01-23 05:36:59 +0000857 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900858 "lib64/mylib.so",
859 "lib64/mylib3.so",
860 "lib64/mylib4.so",
861 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900862}
863
Colin Cross7812fd32020-09-25 12:35:10 -0700864func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
865 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -0800866 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -0700867 apex {
868 name: "myapex",
869 key: "myapex.key",
870 native_shared_libs: ["mylib", "mylib3"],
871 min_sdk_version: "29",
872 }
873
874 apex_key {
875 name: "myapex.key",
876 public_key: "testkey.avbpubkey",
877 private_key: "testkey.pem",
878 }
879
880 cc_library {
881 name: "mylib",
882 srcs: ["mylib.cpp"],
883 shared_libs: ["mylib2", "mylib3"],
884 system_shared_libs: [],
885 stl: "none",
886 apex_available: [ "myapex" ],
887 min_sdk_version: "28",
888 }
889
890 cc_library {
891 name: "mylib2",
892 srcs: ["mylib.cpp"],
893 cflags: ["-include mylib.h"],
894 system_shared_libs: [],
895 stl: "none",
896 stubs: {
897 versions: ["28", "29", "30", "current"],
898 },
899 min_sdk_version: "28",
900 }
901
902 cc_library {
903 name: "mylib3",
904 srcs: ["mylib.cpp"],
905 shared_libs: ["mylib4"],
906 system_shared_libs: [],
907 stl: "none",
908 stubs: {
909 versions: ["28", "29", "30", "current"],
910 },
911 apex_available: [ "myapex" ],
912 min_sdk_version: "28",
913 }
914
915 cc_library {
916 name: "mylib4",
917 srcs: ["mylib.cpp"],
918 system_shared_libs: [],
919 stl: "none",
920 apex_available: [ "myapex" ],
921 min_sdk_version: "28",
922 }
923 `)
924
925 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
926 copyCmds := apexRule.Args["copy_commands"]
927
928 // Ensure that direct non-stubs dep is always included
929 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
930
931 // Ensure that indirect stubs dep is not included
932 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
933
934 // Ensure that direct stubs dep is included
935 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
936
937 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
938
Jiyong Park55549df2021-02-26 23:57:23 +0900939 // Ensure that mylib is linking with the latest version of stub for mylib2
940 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -0700941 // ... and not linking to the non-stub (impl) variant of mylib2
942 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
943
944 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
945 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
946 // .. and not linking to the stubs variant of mylib3
947 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
948
949 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -0700950 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -0700951 ensureNotContains(t, mylib2Cflags, "-include ")
952
953 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700954 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -0700955
956 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
957 "lib64/mylib.so",
958 "lib64/mylib3.so",
959 "lib64/mylib4.so",
960 })
961}
962
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900963func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
964 t.Parallel()
965 // myapex (Z)
966 // mylib -----------------.
967 // |
968 // otherapex (29) |
969 // libstub's versions: 29 Z current
970 // |
971 // <platform> |
972 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -0800973 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900974 apex {
975 name: "myapex",
976 key: "myapex.key",
977 native_shared_libs: ["mylib"],
978 min_sdk_version: "Z", // non-final
979 }
980
981 cc_library {
982 name: "mylib",
983 srcs: ["mylib.cpp"],
984 shared_libs: ["libstub"],
985 apex_available: ["myapex"],
986 min_sdk_version: "Z",
987 }
988
989 apex_key {
990 name: "myapex.key",
991 public_key: "testkey.avbpubkey",
992 private_key: "testkey.pem",
993 }
994
995 apex {
996 name: "otherapex",
997 key: "myapex.key",
998 native_shared_libs: ["libstub"],
999 min_sdk_version: "29",
1000 }
1001
1002 cc_library {
1003 name: "libstub",
1004 srcs: ["mylib.cpp"],
1005 stubs: {
1006 versions: ["29", "Z", "current"],
1007 },
1008 apex_available: ["otherapex"],
1009 min_sdk_version: "29",
1010 }
1011
1012 // platform module depending on libstub from otherapex should use the latest stub("current")
1013 cc_library {
1014 name: "libplatform",
1015 srcs: ["mylib.cpp"],
1016 shared_libs: ["libstub"],
1017 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001018 `,
1019 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1020 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1021 variables.Platform_sdk_final = proptools.BoolPtr(false)
1022 variables.Platform_version_active_codenames = []string{"Z"}
1023 }),
1024 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001025
Jiyong Park55549df2021-02-26 23:57:23 +09001026 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001027 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001028 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001029 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001030 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001031
1032 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1033 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1034 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1035 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1036 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1037}
1038
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001039func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001040 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001041 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001042 name: "myapex2",
1043 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001044 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001045 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001046 }
1047
1048 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001049 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001050 public_key: "testkey.avbpubkey",
1051 private_key: "testkey.pem",
1052 }
1053
1054 cc_library {
1055 name: "mylib",
1056 srcs: ["mylib.cpp"],
1057 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001058 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001059 system_shared_libs: [],
1060 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001061 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001062 }
1063
1064 cc_library {
1065 name: "libfoo",
1066 srcs: ["mylib.cpp"],
1067 shared_libs: ["libbar"],
1068 system_shared_libs: [],
1069 stl: "none",
1070 stubs: {
1071 versions: ["10", "20", "30"],
1072 },
1073 }
1074
1075 cc_library {
1076 name: "libbar",
1077 srcs: ["mylib.cpp"],
1078 system_shared_libs: [],
1079 stl: "none",
1080 }
1081
Jiyong Park678c8812020-02-07 17:25:49 +09001082 cc_library_static {
1083 name: "libbaz",
1084 srcs: ["mylib.cpp"],
1085 system_shared_libs: [],
1086 stl: "none",
1087 apex_available: [ "myapex2" ],
1088 }
1089
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001090 `)
1091
Jiyong Park83dc74b2020-01-14 18:38:44 +09001092 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001093 copyCmds := apexRule.Args["copy_commands"]
1094
1095 // Ensure that direct non-stubs dep is always included
1096 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1097
1098 // Ensure that indirect stubs dep is not included
1099 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1100
1101 // Ensure that dependency of stubs is not included
1102 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1103
Colin Crossaede88c2020-08-11 12:17:01 -07001104 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001105
1106 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001107 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001108 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001109 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001110
Jiyong Park3ff16992019-12-27 14:11:47 +09001111 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001112
1113 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1114 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001115
Artur Satayeva8bd1132020-04-27 18:07:06 +01001116 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001117 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001118
Artur Satayeva8bd1132020-04-27 18:07:06 +01001119 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001120 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001121}
1122
Jooyung Hand3639552019-08-09 12:57:43 +09001123func TestApexWithRuntimeLibsDependency(t *testing.T) {
1124 /*
1125 myapex
1126 |
1127 v (runtime_libs)
1128 mylib ------+------> libfoo [provides stub]
1129 |
1130 `------> libbar
1131 */
Colin Cross1c460562021-02-16 17:55:47 -08001132 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001133 apex {
1134 name: "myapex",
1135 key: "myapex.key",
1136 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001137 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001138 }
1139
1140 apex_key {
1141 name: "myapex.key",
1142 public_key: "testkey.avbpubkey",
1143 private_key: "testkey.pem",
1144 }
1145
1146 cc_library {
1147 name: "mylib",
1148 srcs: ["mylib.cpp"],
1149 runtime_libs: ["libfoo", "libbar"],
1150 system_shared_libs: [],
1151 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001152 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001153 }
1154
1155 cc_library {
1156 name: "libfoo",
1157 srcs: ["mylib.cpp"],
1158 system_shared_libs: [],
1159 stl: "none",
1160 stubs: {
1161 versions: ["10", "20", "30"],
1162 },
1163 }
1164
1165 cc_library {
1166 name: "libbar",
1167 srcs: ["mylib.cpp"],
1168 system_shared_libs: [],
1169 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001170 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001171 }
1172
1173 `)
1174
Sundong Ahnabb64432019-10-22 13:58:29 +09001175 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001176 copyCmds := apexRule.Args["copy_commands"]
1177
1178 // Ensure that direct non-stubs dep is always included
1179 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1180
1181 // Ensure that indirect stubs dep is not included
1182 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1183
1184 // Ensure that runtime_libs dep in included
1185 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1186
Sundong Ahnabb64432019-10-22 13:58:29 +09001187 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001188 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1189 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001190
1191}
1192
Paul Duffina02cae32021-03-09 01:44:06 +00001193var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1194 cc.PrepareForTestWithCcBuildComponents,
1195 PrepareForTestWithApexBuildComponents,
1196 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001197 apex {
1198 name: "com.android.runtime",
1199 key: "com.android.runtime.key",
1200 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001201 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001202 }
1203
1204 apex_key {
1205 name: "com.android.runtime.key",
1206 public_key: "testkey.avbpubkey",
1207 private_key: "testkey.pem",
1208 }
Paul Duffina02cae32021-03-09 01:44:06 +00001209 `),
1210 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1211)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001212
Paul Duffina02cae32021-03-09 01:44:06 +00001213func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001214 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001215 cc_library {
1216 name: "libc",
1217 no_libcrt: true,
1218 nocrt: true,
1219 stl: "none",
1220 system_shared_libs: [],
1221 stubs: { versions: ["1"] },
1222 apex_available: ["com.android.runtime"],
1223
1224 sanitize: {
1225 hwaddress: true,
1226 }
1227 }
1228
1229 cc_prebuilt_library_shared {
1230 name: "libclang_rt.hwasan-aarch64-android",
1231 no_libcrt: true,
1232 nocrt: true,
1233 stl: "none",
1234 system_shared_libs: [],
1235 srcs: [""],
1236 stubs: { versions: ["1"] },
1237
1238 sanitize: {
1239 never: true,
1240 },
Paul Duffina02cae32021-03-09 01:44:06 +00001241 } `)
1242 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001243
1244 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1245 "lib64/bionic/libc.so",
1246 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1247 })
1248
1249 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1250
1251 installed := hwasan.Description("install libclang_rt.hwasan")
1252 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1253
1254 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1255 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1256 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1257}
1258
1259func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001260 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001261 prepareForTestOfRuntimeApexWithHwasan,
1262 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1263 variables.SanitizeDevice = []string{"hwaddress"}
1264 }),
1265 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001266 cc_library {
1267 name: "libc",
1268 no_libcrt: true,
1269 nocrt: true,
1270 stl: "none",
1271 system_shared_libs: [],
1272 stubs: { versions: ["1"] },
1273 apex_available: ["com.android.runtime"],
1274 }
1275
1276 cc_prebuilt_library_shared {
1277 name: "libclang_rt.hwasan-aarch64-android",
1278 no_libcrt: true,
1279 nocrt: true,
1280 stl: "none",
1281 system_shared_libs: [],
1282 srcs: [""],
1283 stubs: { versions: ["1"] },
1284
1285 sanitize: {
1286 never: true,
1287 },
1288 }
Paul Duffina02cae32021-03-09 01:44:06 +00001289 `)
1290 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001291
1292 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1293 "lib64/bionic/libc.so",
1294 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1295 })
1296
1297 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1298
1299 installed := hwasan.Description("install libclang_rt.hwasan")
1300 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1301
1302 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1303 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1304 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1305}
1306
Jooyung Han61b66e92020-03-21 14:21:46 +00001307func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1308 testcases := []struct {
1309 name string
1310 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001311 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001312 shouldLink string
1313 shouldNotLink []string
1314 }{
1315 {
Jiyong Park55549df2021-02-26 23:57:23 +09001316 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001317 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001318 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001319 shouldLink: "current",
1320 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001321 },
1322 {
Jiyong Park55549df2021-02-26 23:57:23 +09001323 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001324 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001325 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001326 shouldLink: "current",
1327 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001328 },
1329 }
1330 for _, tc := range testcases {
1331 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001332 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001333 apex {
1334 name: "myapex",
1335 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001336 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001337 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001338 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001339 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001340
Jooyung Han61b66e92020-03-21 14:21:46 +00001341 apex_key {
1342 name: "myapex.key",
1343 public_key: "testkey.avbpubkey",
1344 private_key: "testkey.pem",
1345 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001346
Jooyung Han61b66e92020-03-21 14:21:46 +00001347 cc_library {
1348 name: "mylib",
1349 srcs: ["mylib.cpp"],
1350 vendor_available: true,
1351 shared_libs: ["libbar"],
1352 system_shared_libs: [],
1353 stl: "none",
1354 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001355 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001356 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001357
Jooyung Han61b66e92020-03-21 14:21:46 +00001358 cc_library {
1359 name: "libbar",
1360 srcs: ["mylib.cpp"],
1361 system_shared_libs: [],
1362 stl: "none",
1363 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001364 llndk: {
1365 symbol_file: "libbar.map.txt",
1366 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001367 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001368 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001369 withUnbundledBuild,
1370 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001371
Jooyung Han61b66e92020-03-21 14:21:46 +00001372 // Ensure that LLNDK dep is not included
1373 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1374 "lib64/mylib.so",
1375 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001376
Jooyung Han61b66e92020-03-21 14:21:46 +00001377 // Ensure that LLNDK dep is required
1378 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1379 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1380 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001381
Steven Moreland2c4000c2021-04-27 02:08:49 +00001382 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1383 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001384 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001385 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001386 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001387
Steven Moreland2c4000c2021-04-27 02:08:49 +00001388 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001389 ver := tc.shouldLink
1390 if tc.shouldLink == "current" {
1391 ver = strconv.Itoa(android.FutureApiLevelInt)
1392 }
1393 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001394 })
1395 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001396}
1397
Jiyong Park25fc6a92018-11-18 18:02:45 +09001398func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001399 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001400 apex {
1401 name: "myapex",
1402 key: "myapex.key",
1403 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001404 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001405 }
1406
1407 apex_key {
1408 name: "myapex.key",
1409 public_key: "testkey.avbpubkey",
1410 private_key: "testkey.pem",
1411 }
1412
1413 cc_library {
1414 name: "mylib",
1415 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001416 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001417 shared_libs: ["libdl#27"],
1418 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001419 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001420 }
1421
1422 cc_library_shared {
1423 name: "mylib_shared",
1424 srcs: ["mylib.cpp"],
1425 shared_libs: ["libdl#27"],
1426 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001427 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001428 }
1429
1430 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001431 name: "libBootstrap",
1432 srcs: ["mylib.cpp"],
1433 stl: "none",
1434 bootstrap: true,
1435 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001436 `)
1437
Sundong Ahnabb64432019-10-22 13:58:29 +09001438 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001439 copyCmds := apexRule.Args["copy_commands"]
1440
1441 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001442 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001443 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1444 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001445
1446 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001447 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001448
Colin Crossaede88c2020-08-11 12:17:01 -07001449 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1450 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1451 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001452
1453 // For dependency to libc
1454 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001455 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001456 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001457 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001458 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001459 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1460 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001461
1462 // For dependency to libm
1463 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001464 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001465 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001466 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001467 // ... and is not compiling with the stub
1468 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1469 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1470
1471 // For dependency to libdl
1472 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001473 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001474 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001475 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1476 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001477 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001478 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001479 // ... Cflags from stub is correctly exported to mylib
1480 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1481 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001482
1483 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001484 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1485 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1486 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1487 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001488}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001489
Jooyung Han749dc692020-04-15 11:03:39 +09001490func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001491 // there are three links between liba --> libz.
1492 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001493 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001494 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001495 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001496 apex {
1497 name: "myapex",
1498 key: "myapex.key",
1499 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001500 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001501 }
1502
1503 apex {
1504 name: "otherapex",
1505 key: "myapex.key",
1506 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001507 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001508 }
1509
1510 apex_key {
1511 name: "myapex.key",
1512 public_key: "testkey.avbpubkey",
1513 private_key: "testkey.pem",
1514 }
1515
1516 cc_library {
1517 name: "libx",
1518 shared_libs: ["liba"],
1519 system_shared_libs: [],
1520 stl: "none",
1521 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001522 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001523 }
1524
1525 cc_library {
1526 name: "liby",
1527 shared_libs: ["liba"],
1528 system_shared_libs: [],
1529 stl: "none",
1530 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001531 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001532 }
1533
1534 cc_library {
1535 name: "liba",
1536 shared_libs: ["libz"],
1537 system_shared_libs: [],
1538 stl: "none",
1539 apex_available: [
1540 "//apex_available:anyapex",
1541 "//apex_available:platform",
1542 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001543 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001544 }
1545
1546 cc_library {
1547 name: "libz",
1548 system_shared_libs: [],
1549 stl: "none",
1550 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001551 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001552 },
1553 }
Jooyung Han749dc692020-04-15 11:03:39 +09001554 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001555
1556 expectLink := func(from, from_variant, to, to_variant string) {
1557 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1558 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1559 }
1560 expectNoLink := func(from, from_variant, to, to_variant string) {
1561 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1562 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1563 }
1564 // platform liba is linked to non-stub version
1565 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001566 // liba in myapex is linked to current
1567 expectLink("liba", "shared_apex29", "libz", "shared_current")
1568 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001569 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001570 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001571 // liba in otherapex is linked to current
1572 expectLink("liba", "shared_apex30", "libz", "shared_current")
1573 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001574 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1575 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001576}
1577
Jooyung Hanaed150d2020-04-02 01:41:41 +09001578func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001579 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001580 apex {
1581 name: "myapex",
1582 key: "myapex.key",
1583 native_shared_libs: ["libx"],
1584 min_sdk_version: "R",
1585 }
1586
1587 apex_key {
1588 name: "myapex.key",
1589 public_key: "testkey.avbpubkey",
1590 private_key: "testkey.pem",
1591 }
1592
1593 cc_library {
1594 name: "libx",
1595 shared_libs: ["libz"],
1596 system_shared_libs: [],
1597 stl: "none",
1598 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001599 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001600 }
1601
1602 cc_library {
1603 name: "libz",
1604 system_shared_libs: [],
1605 stl: "none",
1606 stubs: {
1607 versions: ["29", "R"],
1608 },
1609 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001610 `,
1611 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1612 variables.Platform_version_active_codenames = []string{"R"}
1613 }),
1614 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001615
1616 expectLink := func(from, from_variant, to, to_variant string) {
1617 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1618 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1619 }
1620 expectNoLink := func(from, from_variant, to, to_variant string) {
1621 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1622 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1623 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001624 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1625 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001626 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1627 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001628}
1629
Jooyung Han749dc692020-04-15 11:03:39 +09001630func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001631 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001632 apex {
1633 name: "myapex",
1634 key: "myapex.key",
1635 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001636 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001637 }
1638
1639 apex_key {
1640 name: "myapex.key",
1641 public_key: "testkey.avbpubkey",
1642 private_key: "testkey.pem",
1643 }
1644
1645 cc_library {
1646 name: "libx",
1647 shared_libs: ["libz"],
1648 system_shared_libs: [],
1649 stl: "none",
1650 apex_available: [ "myapex" ],
1651 }
1652
1653 cc_library {
1654 name: "libz",
1655 system_shared_libs: [],
1656 stl: "none",
1657 stubs: {
1658 versions: ["1", "2"],
1659 },
1660 }
1661 `)
1662
1663 expectLink := func(from, from_variant, to, to_variant string) {
1664 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1665 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1666 }
1667 expectNoLink := func(from, from_variant, to, to_variant string) {
1668 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1669 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1670 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001671 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001672 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001673 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001674 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001675}
1676
1677func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001678 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001679 apex {
1680 name: "myapex",
1681 key: "myapex.key",
1682 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001683 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001684 }
1685
1686 apex_key {
1687 name: "myapex.key",
1688 public_key: "testkey.avbpubkey",
1689 private_key: "testkey.pem",
1690 }
1691
1692 cc_library {
1693 name: "libx",
1694 system_shared_libs: [],
1695 stl: "none",
1696 apex_available: [ "myapex" ],
1697 stubs: {
1698 versions: ["1", "2"],
1699 },
1700 }
1701
1702 cc_library {
1703 name: "libz",
1704 shared_libs: ["libx"],
1705 system_shared_libs: [],
1706 stl: "none",
1707 }
1708 `)
1709
1710 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001711 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001712 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1713 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1714 }
1715 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001716 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001717 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1718 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1719 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001720 expectLink("libz", "shared", "libx", "shared_current")
1721 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09001722 expectNoLink("libz", "shared", "libz", "shared_1")
1723 expectNoLink("libz", "shared", "libz", "shared")
1724}
1725
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001726var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1727 func(variables android.FixtureProductVariables) {
1728 variables.SanitizeDevice = []string{"hwaddress"}
1729 },
1730)
1731
Jooyung Han75568392020-03-20 04:29:24 +09001732func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001733 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001734 apex {
1735 name: "myapex",
1736 key: "myapex.key",
1737 native_shared_libs: ["libx"],
1738 min_sdk_version: "29",
1739 }
1740
1741 apex_key {
1742 name: "myapex.key",
1743 public_key: "testkey.avbpubkey",
1744 private_key: "testkey.pem",
1745 }
1746
1747 cc_library {
1748 name: "libx",
1749 shared_libs: ["libbar"],
1750 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001751 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001752 }
1753
1754 cc_library {
1755 name: "libbar",
1756 stubs: {
1757 versions: ["29", "30"],
1758 },
1759 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001760 `,
1761 prepareForTestWithSantitizeHwaddress,
1762 )
Jooyung Han03b51852020-02-26 22:45:42 +09001763 expectLink := func(from, from_variant, to, to_variant string) {
1764 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1765 libFlags := ld.Args["libFlags"]
1766 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1767 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001768 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09001769}
1770
Jooyung Han75568392020-03-20 04:29:24 +09001771func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001772 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001773 apex {
1774 name: "myapex",
1775 key: "myapex.key",
1776 native_shared_libs: ["libx"],
1777 min_sdk_version: "29",
1778 }
1779
1780 apex_key {
1781 name: "myapex.key",
1782 public_key: "testkey.avbpubkey",
1783 private_key: "testkey.pem",
1784 }
1785
1786 cc_library {
1787 name: "libx",
1788 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001789 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001790 }
Jooyung Han75568392020-03-20 04:29:24 +09001791 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001792
1793 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001794 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001795 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001796 // note that platform variant is not.
1797 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001798 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001799}
1800
Jooyung Han749dc692020-04-15 11:03:39 +09001801func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1802 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001803 apex {
1804 name: "myapex",
1805 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001806 native_shared_libs: ["mylib"],
1807 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001808 }
1809
1810 apex_key {
1811 name: "myapex.key",
1812 public_key: "testkey.avbpubkey",
1813 private_key: "testkey.pem",
1814 }
Jooyung Han749dc692020-04-15 11:03:39 +09001815
1816 cc_library {
1817 name: "mylib",
1818 srcs: ["mylib.cpp"],
1819 system_shared_libs: [],
1820 stl: "none",
1821 apex_available: [
1822 "myapex",
1823 ],
1824 min_sdk_version: "30",
1825 }
1826 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001827
1828 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
1829 apex {
1830 name: "myapex",
1831 key: "myapex.key",
1832 native_shared_libs: ["libfoo.ffi"],
1833 min_sdk_version: "29",
1834 }
1835
1836 apex_key {
1837 name: "myapex.key",
1838 public_key: "testkey.avbpubkey",
1839 private_key: "testkey.pem",
1840 }
1841
1842 rust_ffi_shared {
1843 name: "libfoo.ffi",
1844 srcs: ["foo.rs"],
1845 crate_name: "foo",
1846 apex_available: [
1847 "myapex",
1848 ],
1849 min_sdk_version: "30",
1850 }
1851 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001852
1853 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
1854 apex {
1855 name: "myapex",
1856 key: "myapex.key",
1857 java_libs: ["libfoo"],
1858 min_sdk_version: "29",
1859 }
1860
1861 apex_key {
1862 name: "myapex.key",
1863 public_key: "testkey.avbpubkey",
1864 private_key: "testkey.pem",
1865 }
1866
1867 java_import {
1868 name: "libfoo",
1869 jars: ["libfoo.jar"],
1870 apex_available: [
1871 "myapex",
1872 ],
1873 min_sdk_version: "30",
1874 }
1875 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001876}
1877
1878func TestApexMinSdkVersion_Okay(t *testing.T) {
1879 testApex(t, `
1880 apex {
1881 name: "myapex",
1882 key: "myapex.key",
1883 native_shared_libs: ["libfoo"],
1884 java_libs: ["libbar"],
1885 min_sdk_version: "29",
1886 }
1887
1888 apex_key {
1889 name: "myapex.key",
1890 public_key: "testkey.avbpubkey",
1891 private_key: "testkey.pem",
1892 }
1893
1894 cc_library {
1895 name: "libfoo",
1896 srcs: ["mylib.cpp"],
1897 shared_libs: ["libfoo_dep"],
1898 apex_available: ["myapex"],
1899 min_sdk_version: "29",
1900 }
1901
1902 cc_library {
1903 name: "libfoo_dep",
1904 srcs: ["mylib.cpp"],
1905 apex_available: ["myapex"],
1906 min_sdk_version: "29",
1907 }
1908
1909 java_library {
1910 name: "libbar",
1911 sdk_version: "current",
1912 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001913 static_libs: [
1914 "libbar_dep",
1915 "libbar_import_dep",
1916 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001917 apex_available: ["myapex"],
1918 min_sdk_version: "29",
1919 }
1920
1921 java_library {
1922 name: "libbar_dep",
1923 sdk_version: "current",
1924 srcs: ["a.java"],
1925 apex_available: ["myapex"],
1926 min_sdk_version: "29",
1927 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001928
1929 java_import {
1930 name: "libbar_import_dep",
1931 jars: ["libbar.jar"],
1932 apex_available: ["myapex"],
1933 min_sdk_version: "29",
1934 }
Jooyung Han03b51852020-02-26 22:45:42 +09001935 `)
1936}
1937
Artur Satayev8cf899a2020-04-15 17:29:42 +01001938func TestJavaStableSdkVersion(t *testing.T) {
1939 testCases := []struct {
1940 name string
1941 expectedError string
1942 bp string
1943 }{
1944 {
1945 name: "Non-updatable apex with non-stable dep",
1946 bp: `
1947 apex {
1948 name: "myapex",
1949 java_libs: ["myjar"],
1950 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001951 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01001952 }
1953 apex_key {
1954 name: "myapex.key",
1955 public_key: "testkey.avbpubkey",
1956 private_key: "testkey.pem",
1957 }
1958 java_library {
1959 name: "myjar",
1960 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001961 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001962 apex_available: ["myapex"],
1963 }
1964 `,
1965 },
1966 {
1967 name: "Updatable apex with stable dep",
1968 bp: `
1969 apex {
1970 name: "myapex",
1971 java_libs: ["myjar"],
1972 key: "myapex.key",
1973 updatable: true,
1974 min_sdk_version: "29",
1975 }
1976 apex_key {
1977 name: "myapex.key",
1978 public_key: "testkey.avbpubkey",
1979 private_key: "testkey.pem",
1980 }
1981 java_library {
1982 name: "myjar",
1983 srcs: ["foo/bar/MyClass.java"],
1984 sdk_version: "current",
1985 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001986 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001987 }
1988 `,
1989 },
1990 {
1991 name: "Updatable apex with non-stable dep",
1992 expectedError: "cannot depend on \"myjar\"",
1993 bp: `
1994 apex {
1995 name: "myapex",
1996 java_libs: ["myjar"],
1997 key: "myapex.key",
1998 updatable: true,
1999 }
2000 apex_key {
2001 name: "myapex.key",
2002 public_key: "testkey.avbpubkey",
2003 private_key: "testkey.pem",
2004 }
2005 java_library {
2006 name: "myjar",
2007 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002008 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002009 apex_available: ["myapex"],
2010 }
2011 `,
2012 },
2013 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002014 name: "Updatable apex with non-stable transitive dep",
2015 // This is not actually detecting that the transitive dependency is unstable, rather it is
2016 // detecting that the transitive dependency is building against a wider API surface than the
2017 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002018 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002019 bp: `
2020 apex {
2021 name: "myapex",
2022 java_libs: ["myjar"],
2023 key: "myapex.key",
2024 updatable: true,
2025 }
2026 apex_key {
2027 name: "myapex.key",
2028 public_key: "testkey.avbpubkey",
2029 private_key: "testkey.pem",
2030 }
2031 java_library {
2032 name: "myjar",
2033 srcs: ["foo/bar/MyClass.java"],
2034 sdk_version: "current",
2035 apex_available: ["myapex"],
2036 static_libs: ["transitive-jar"],
2037 }
2038 java_library {
2039 name: "transitive-jar",
2040 srcs: ["foo/bar/MyClass.java"],
2041 sdk_version: "core_platform",
2042 apex_available: ["myapex"],
2043 }
2044 `,
2045 },
2046 }
2047
2048 for _, test := range testCases {
2049 t.Run(test.name, func(t *testing.T) {
2050 if test.expectedError == "" {
2051 testApex(t, test.bp)
2052 } else {
2053 testApexError(t, test.expectedError, test.bp)
2054 }
2055 })
2056 }
2057}
2058
Jooyung Han749dc692020-04-15 11:03:39 +09002059func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2060 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2061 apex {
2062 name: "myapex",
2063 key: "myapex.key",
2064 native_shared_libs: ["mylib"],
2065 min_sdk_version: "29",
2066 }
2067
2068 apex_key {
2069 name: "myapex.key",
2070 public_key: "testkey.avbpubkey",
2071 private_key: "testkey.pem",
2072 }
2073
2074 cc_library {
2075 name: "mylib",
2076 srcs: ["mylib.cpp"],
2077 shared_libs: ["mylib2"],
2078 system_shared_libs: [],
2079 stl: "none",
2080 apex_available: [
2081 "myapex",
2082 ],
2083 min_sdk_version: "29",
2084 }
2085
2086 // indirect part of the apex
2087 cc_library {
2088 name: "mylib2",
2089 srcs: ["mylib.cpp"],
2090 system_shared_libs: [],
2091 stl: "none",
2092 apex_available: [
2093 "myapex",
2094 ],
2095 min_sdk_version: "30",
2096 }
2097 `)
2098}
2099
2100func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2101 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2102 apex {
2103 name: "myapex",
2104 key: "myapex.key",
2105 apps: ["AppFoo"],
2106 min_sdk_version: "29",
2107 }
2108
2109 apex_key {
2110 name: "myapex.key",
2111 public_key: "testkey.avbpubkey",
2112 private_key: "testkey.pem",
2113 }
2114
2115 android_app {
2116 name: "AppFoo",
2117 srcs: ["foo/bar/MyClass.java"],
2118 sdk_version: "current",
2119 min_sdk_version: "29",
2120 system_modules: "none",
2121 stl: "none",
2122 static_libs: ["bar"],
2123 apex_available: [ "myapex" ],
2124 }
2125
2126 java_library {
2127 name: "bar",
2128 sdk_version: "current",
2129 srcs: ["a.java"],
2130 apex_available: [ "myapex" ],
2131 }
2132 `)
2133}
2134
2135func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002136 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002137 apex {
2138 name: "myapex",
2139 key: "myapex.key",
2140 native_shared_libs: ["mylib"],
2141 min_sdk_version: "29",
2142 }
2143
2144 apex_key {
2145 name: "myapex.key",
2146 public_key: "testkey.avbpubkey",
2147 private_key: "testkey.pem",
2148 }
2149
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002150 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002151 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2152 cc_library {
2153 name: "mylib",
2154 srcs: ["mylib.cpp"],
2155 shared_libs: ["mylib2"],
2156 system_shared_libs: [],
2157 stl: "none",
2158 apex_available: ["myapex", "otherapex"],
2159 min_sdk_version: "29",
2160 }
2161
2162 cc_library {
2163 name: "mylib2",
2164 srcs: ["mylib.cpp"],
2165 system_shared_libs: [],
2166 stl: "none",
2167 apex_available: ["otherapex"],
2168 stubs: { versions: ["29", "30"] },
2169 min_sdk_version: "30",
2170 }
2171
2172 apex {
2173 name: "otherapex",
2174 key: "myapex.key",
2175 native_shared_libs: ["mylib", "mylib2"],
2176 min_sdk_version: "30",
2177 }
2178 `)
2179 expectLink := func(from, from_variant, to, to_variant string) {
2180 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2181 libFlags := ld.Args["libFlags"]
2182 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2183 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002184 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002185 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002186}
2187
Jooyung Haned124c32021-01-26 11:43:46 +09002188func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002189 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2190 func(variables android.FixtureProductVariables) {
2191 variables.Platform_sdk_codename = proptools.StringPtr("S")
2192 variables.Platform_version_active_codenames = []string{"S"}
2193 },
2194 )
Jooyung Haned124c32021-01-26 11:43:46 +09002195 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2196 apex {
2197 name: "myapex",
2198 key: "myapex.key",
2199 native_shared_libs: ["libfoo"],
2200 min_sdk_version: "S",
2201 }
2202 apex_key {
2203 name: "myapex.key",
2204 public_key: "testkey.avbpubkey",
2205 private_key: "testkey.pem",
2206 }
2207 cc_library {
2208 name: "libfoo",
2209 shared_libs: ["libbar"],
2210 apex_available: ["myapex"],
2211 min_sdk_version: "29",
2212 }
2213 cc_library {
2214 name: "libbar",
2215 apex_available: ["myapex"],
2216 }
2217 `, withSAsActiveCodeNames)
2218}
2219
2220func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002221 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2222 variables.Platform_sdk_codename = proptools.StringPtr("S")
2223 variables.Platform_version_active_codenames = []string{"S", "T"}
2224 })
Colin Cross1c460562021-02-16 17:55:47 -08002225 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002226 apex {
2227 name: "myapex",
2228 key: "myapex.key",
2229 native_shared_libs: ["libfoo"],
2230 min_sdk_version: "S",
2231 }
2232 apex_key {
2233 name: "myapex.key",
2234 public_key: "testkey.avbpubkey",
2235 private_key: "testkey.pem",
2236 }
2237 cc_library {
2238 name: "libfoo",
2239 shared_libs: ["libbar"],
2240 apex_available: ["myapex"],
2241 min_sdk_version: "S",
2242 }
2243 cc_library {
2244 name: "libbar",
2245 stubs: {
2246 symbol_file: "libbar.map.txt",
2247 versions: ["30", "S", "T"],
2248 },
2249 }
2250 `, withSAsActiveCodeNames)
2251
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002252 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002253 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2254 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002255 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002256}
2257
Jiyong Park7c2ee712018-12-07 00:42:25 +09002258func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002259 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002260 apex {
2261 name: "myapex",
2262 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002263 native_shared_libs: ["mylib"],
2264 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002265 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002266 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002267 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002268 }
2269
2270 apex_key {
2271 name: "myapex.key",
2272 public_key: "testkey.avbpubkey",
2273 private_key: "testkey.pem",
2274 }
2275
2276 prebuilt_etc {
2277 name: "myetc",
2278 src: "myprebuilt",
2279 sub_dir: "foo/bar",
2280 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002281
2282 cc_library {
2283 name: "mylib",
2284 srcs: ["mylib.cpp"],
2285 relative_install_path: "foo/bar",
2286 system_shared_libs: [],
2287 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002288 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002289 }
2290
2291 cc_binary {
2292 name: "mybin",
2293 srcs: ["mylib.cpp"],
2294 relative_install_path: "foo/bar",
2295 system_shared_libs: [],
2296 static_executable: true,
2297 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002298 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002299 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002300 `)
2301
Sundong Ahnabb64432019-10-22 13:58:29 +09002302 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Parkb90099d2021-12-15 11:08:07 +00002303 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002304
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002305 // Ensure that the subdirectories are all listed
Jiyong Parkb90099d2021-12-15 11:08:07 +00002306 ensureContains(t, cmd, "/etc ")
2307 ensureContains(t, cmd, "/etc/foo ")
2308 ensureContains(t, cmd, "/etc/foo/bar ")
2309 ensureContains(t, cmd, "/lib64 ")
2310 ensureContains(t, cmd, "/lib64/foo ")
2311 ensureContains(t, cmd, "/lib64/foo/bar ")
2312 ensureContains(t, cmd, "/lib ")
2313 ensureContains(t, cmd, "/lib/foo ")
2314 ensureContains(t, cmd, "/lib/foo/bar ")
2315 ensureContains(t, cmd, "/bin ")
2316 ensureContains(t, cmd, "/bin/foo ")
2317 ensureContains(t, cmd, "/bin/foo/bar ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002318}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002319
Jooyung Han35155c42020-02-06 17:33:20 +09002320func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002321 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002322 apex {
2323 name: "myapex",
2324 key: "myapex.key",
2325 multilib: {
2326 both: {
2327 native_shared_libs: ["mylib"],
2328 binaries: ["mybin"],
2329 },
2330 },
2331 compile_multilib: "both",
2332 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002333 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002334 }
2335
2336 apex_key {
2337 name: "myapex.key",
2338 public_key: "testkey.avbpubkey",
2339 private_key: "testkey.pem",
2340 }
2341
2342 cc_library {
2343 name: "mylib",
2344 relative_install_path: "foo/bar",
2345 system_shared_libs: [],
2346 stl: "none",
2347 apex_available: [ "myapex" ],
2348 native_bridge_supported: true,
2349 }
2350
2351 cc_binary {
2352 name: "mybin",
2353 relative_install_path: "foo/bar",
2354 system_shared_libs: [],
2355 static_executable: true,
2356 stl: "none",
2357 apex_available: [ "myapex" ],
2358 native_bridge_supported: true,
2359 compile_multilib: "both", // default is "first" for binary
2360 multilib: {
2361 lib64: {
2362 suffix: "64",
2363 },
2364 },
2365 }
2366 `, withNativeBridgeEnabled)
2367 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2368 "bin/foo/bar/mybin",
2369 "bin/foo/bar/mybin64",
2370 "bin/arm/foo/bar/mybin",
2371 "bin/arm64/foo/bar/mybin64",
2372 "lib/foo/bar/mylib.so",
2373 "lib/arm/foo/bar/mylib.so",
2374 "lib64/foo/bar/mylib.so",
2375 "lib64/arm64/foo/bar/mylib.so",
2376 })
2377}
2378
Jooyung Han85d61762020-06-24 23:50:26 +09002379func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002380 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002381 apex {
2382 name: "myapex",
2383 key: "myapex.key",
2384 binaries: ["mybin"],
2385 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002386 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002387 }
2388 apex_key {
2389 name: "myapex.key",
2390 public_key: "testkey.avbpubkey",
2391 private_key: "testkey.pem",
2392 }
2393 cc_binary {
2394 name: "mybin",
2395 vendor: true,
2396 shared_libs: ["libfoo"],
2397 }
2398 cc_library {
2399 name: "libfoo",
2400 proprietary: true,
2401 }
2402 `)
2403
2404 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2405 "bin/mybin",
2406 "lib64/libfoo.so",
2407 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2408 "lib64/libc++.so",
2409 })
2410
2411 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002412 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002413 name := apexBundle.BaseModuleName()
2414 prefix := "TARGET_"
2415 var builder strings.Builder
2416 data.Custom(&builder, name, prefix, "", data)
Paul Duffin37ba3442021-03-29 00:21:08 +01002417 androidMk := android.StringRelativeToTop(ctx.Config(), builder.String())
2418 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002419 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002420
2421 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2422 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2423 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002424}
2425
Jooyung Handf78e212020-07-22 15:54:47 +09002426func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002427 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002428 apex {
2429 name: "myapex",
2430 key: "myapex.key",
2431 binaries: ["mybin"],
2432 vendor: true,
2433 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002434 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002435 }
2436 apex_key {
2437 name: "myapex.key",
2438 public_key: "testkey.avbpubkey",
2439 private_key: "testkey.pem",
2440 }
2441 cc_binary {
2442 name: "mybin",
2443 vendor: true,
2444 shared_libs: ["libvndk", "libvendor"],
2445 }
2446 cc_library {
2447 name: "libvndk",
2448 vndk: {
2449 enabled: true,
2450 },
2451 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002452 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002453 }
2454 cc_library {
2455 name: "libvendor",
2456 vendor: true,
2457 }
2458 `)
2459
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002460 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002461
Paul Duffina71a67a2021-03-29 00:42:57 +01002462 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002463 libs := names(ldRule.Args["libFlags"])
2464 // VNDK libs(libvndk/libc++) as they are
Paul Duffin37ba3442021-03-29 00:21:08 +01002465 ensureListContains(t, libs, "out/soong/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
2466 ensureListContains(t, libs, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002467 // non-stable Vendor libs as APEX variants
Paul Duffin37ba3442021-03-29 00:21:08 +01002468 ensureListContains(t, libs, "out/soong/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002469
2470 // VNDK libs are not included when use_vndk_as_stable: true
2471 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2472 "bin/mybin",
2473 "lib64/libvendor.so",
2474 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002475
2476 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2477 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2478 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002479}
2480
Justin Yun13decfb2021-03-08 19:25:55 +09002481func TestProductVariant(t *testing.T) {
2482 ctx := testApex(t, `
2483 apex {
2484 name: "myapex",
2485 key: "myapex.key",
2486 updatable: false,
2487 product_specific: true,
2488 binaries: ["foo"],
2489 }
2490
2491 apex_key {
2492 name: "myapex.key",
2493 public_key: "testkey.avbpubkey",
2494 private_key: "testkey.pem",
2495 }
2496
2497 cc_binary {
2498 name: "foo",
2499 product_available: true,
2500 apex_available: ["myapex"],
2501 srcs: ["foo.cpp"],
2502 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002503 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2504 variables.ProductVndkVersion = proptools.StringPtr("current")
2505 }),
2506 )
Justin Yun13decfb2021-03-08 19:25:55 +09002507
2508 cflags := strings.Fields(
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002509 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002510 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2511 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2512 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2513 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2514}
2515
Jooyung Han8e5685d2020-09-21 11:02:57 +09002516func TestApex_withPrebuiltFirmware(t *testing.T) {
2517 testCases := []struct {
2518 name string
2519 additionalProp string
2520 }{
2521 {"system apex with prebuilt_firmware", ""},
2522 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2523 }
2524 for _, tc := range testCases {
2525 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002526 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002527 apex {
2528 name: "myapex",
2529 key: "myapex.key",
2530 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002531 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002532 `+tc.additionalProp+`
2533 }
2534 apex_key {
2535 name: "myapex.key",
2536 public_key: "testkey.avbpubkey",
2537 private_key: "testkey.pem",
2538 }
2539 prebuilt_firmware {
2540 name: "myfirmware",
2541 src: "myfirmware.bin",
2542 filename_from_src: true,
2543 `+tc.additionalProp+`
2544 }
2545 `)
2546 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2547 "etc/firmware/myfirmware.bin",
2548 })
2549 })
2550 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002551}
2552
Jooyung Hanefb184e2020-06-25 17:14:25 +09002553func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002554 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002555 apex {
2556 name: "myapex",
2557 key: "myapex.key",
2558 vendor: true,
2559 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002560 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002561 }
2562
2563 apex_key {
2564 name: "myapex.key",
2565 public_key: "testkey.avbpubkey",
2566 private_key: "testkey.pem",
2567 }
2568
2569 cc_library {
2570 name: "mylib",
2571 vendor_available: true,
2572 }
2573 `)
2574
2575 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002576 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002577 name := apexBundle.BaseModuleName()
2578 prefix := "TARGET_"
2579 var builder strings.Builder
2580 data.Custom(&builder, name, prefix, "", data)
2581 androidMk := builder.String()
2582 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2583}
2584
Jooyung Han2ed99d02020-06-24 23:26:26 +09002585func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002586 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002587 apex {
2588 name: "myapex",
2589 key: "myapex.key",
2590 vintf_fragments: ["fragment.xml"],
2591 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002592 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002593 }
2594 apex_key {
2595 name: "myapex.key",
2596 public_key: "testkey.avbpubkey",
2597 private_key: "testkey.pem",
2598 }
2599 cc_binary {
2600 name: "mybin",
2601 }
2602 `)
2603
2604 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002605 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002606 name := apexBundle.BaseModuleName()
2607 prefix := "TARGET_"
2608 var builder strings.Builder
2609 data.Custom(&builder, name, prefix, "", data)
2610 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04002611 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04002612 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09002613}
2614
Jiyong Park16e91a02018-12-20 18:18:08 +09002615func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002616 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002617 apex {
2618 name: "myapex",
2619 key: "myapex.key",
2620 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002621 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002622 }
2623
2624 apex_key {
2625 name: "myapex.key",
2626 public_key: "testkey.avbpubkey",
2627 private_key: "testkey.pem",
2628 }
2629
2630 cc_library {
2631 name: "mylib",
2632 srcs: ["mylib.cpp"],
2633 system_shared_libs: [],
2634 stl: "none",
2635 stubs: {
2636 versions: ["1", "2", "3"],
2637 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002638 apex_available: [
2639 "//apex_available:platform",
2640 "myapex",
2641 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002642 }
2643
2644 cc_binary {
2645 name: "not_in_apex",
2646 srcs: ["mylib.cpp"],
2647 static_libs: ["mylib"],
2648 static_executable: true,
2649 system_shared_libs: [],
2650 stl: "none",
2651 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002652 `)
2653
Colin Cross7113d202019-11-20 16:39:12 -08002654 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002655
2656 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002657 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002658}
Jiyong Park9335a262018-12-24 11:31:58 +09002659
2660func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002661 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002662 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002663 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002664 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002665 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002666 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002667 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002668 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09002669 }
2670
2671 cc_library {
2672 name: "mylib",
2673 srcs: ["mylib.cpp"],
2674 system_shared_libs: [],
2675 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002676 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002677 }
2678
2679 apex_key {
2680 name: "myapex.key",
2681 public_key: "testkey.avbpubkey",
2682 private_key: "testkey.pem",
2683 }
2684
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002685 android_app_certificate {
2686 name: "myapex.certificate",
2687 certificate: "testkey",
2688 }
2689
2690 android_app_certificate {
2691 name: "myapex.certificate.override",
2692 certificate: "testkey.override",
2693 }
2694
Jiyong Park9335a262018-12-24 11:31:58 +09002695 `)
2696
2697 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002698 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002699
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002700 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2701 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002702 "vendor/foo/devkeys/testkey.avbpubkey")
2703 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002704 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2705 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002706 "vendor/foo/devkeys/testkey.pem")
2707 }
2708
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002709 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002710 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002711 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002712 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002713 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002714 }
2715}
Jiyong Park58e364a2019-01-19 19:24:06 +09002716
Jooyung Hanf121a652019-12-17 14:30:11 +09002717func TestCertificate(t *testing.T) {
2718 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002719 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002720 apex {
2721 name: "myapex",
2722 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002723 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002724 }
2725 apex_key {
2726 name: "myapex.key",
2727 public_key: "testkey.avbpubkey",
2728 private_key: "testkey.pem",
2729 }`)
2730 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2731 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2732 if actual := rule.Args["certificates"]; actual != expected {
2733 t.Errorf("certificates should be %q, not %q", expected, actual)
2734 }
2735 })
2736 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002737 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002738 apex {
2739 name: "myapex_keytest",
2740 key: "myapex.key",
2741 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002742 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002743 }
2744 apex_key {
2745 name: "myapex.key",
2746 public_key: "testkey.avbpubkey",
2747 private_key: "testkey.pem",
2748 }
2749 android_app_certificate {
2750 name: "myapex.certificate.override",
2751 certificate: "testkey.override",
2752 }`)
2753 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2754 expected := "testkey.override.x509.pem testkey.override.pk8"
2755 if actual := rule.Args["certificates"]; actual != expected {
2756 t.Errorf("certificates should be %q, not %q", expected, actual)
2757 }
2758 })
2759 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002760 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002761 apex {
2762 name: "myapex",
2763 key: "myapex.key",
2764 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002765 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002766 }
2767 apex_key {
2768 name: "myapex.key",
2769 public_key: "testkey.avbpubkey",
2770 private_key: "testkey.pem",
2771 }
2772 android_app_certificate {
2773 name: "myapex.certificate",
2774 certificate: "testkey",
2775 }`)
2776 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2777 expected := "testkey.x509.pem testkey.pk8"
2778 if actual := rule.Args["certificates"]; actual != expected {
2779 t.Errorf("certificates should be %q, not %q", expected, actual)
2780 }
2781 })
2782 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002783 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002784 apex {
2785 name: "myapex_keytest",
2786 key: "myapex.key",
2787 file_contexts: ":myapex-file_contexts",
2788 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002789 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002790 }
2791 apex_key {
2792 name: "myapex.key",
2793 public_key: "testkey.avbpubkey",
2794 private_key: "testkey.pem",
2795 }
2796 android_app_certificate {
2797 name: "myapex.certificate.override",
2798 certificate: "testkey.override",
2799 }`)
2800 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2801 expected := "testkey.override.x509.pem testkey.override.pk8"
2802 if actual := rule.Args["certificates"]; actual != expected {
2803 t.Errorf("certificates should be %q, not %q", expected, actual)
2804 }
2805 })
2806 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002807 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002808 apex {
2809 name: "myapex",
2810 key: "myapex.key",
2811 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002812 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002813 }
2814 apex_key {
2815 name: "myapex.key",
2816 public_key: "testkey.avbpubkey",
2817 private_key: "testkey.pem",
2818 }`)
2819 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2820 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2821 if actual := rule.Args["certificates"]; actual != expected {
2822 t.Errorf("certificates should be %q, not %q", expected, actual)
2823 }
2824 })
2825 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002826 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002827 apex {
2828 name: "myapex_keytest",
2829 key: "myapex.key",
2830 file_contexts: ":myapex-file_contexts",
2831 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002832 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002833 }
2834 apex_key {
2835 name: "myapex.key",
2836 public_key: "testkey.avbpubkey",
2837 private_key: "testkey.pem",
2838 }
2839 android_app_certificate {
2840 name: "myapex.certificate.override",
2841 certificate: "testkey.override",
2842 }`)
2843 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2844 expected := "testkey.override.x509.pem testkey.override.pk8"
2845 if actual := rule.Args["certificates"]; actual != expected {
2846 t.Errorf("certificates should be %q, not %q", expected, actual)
2847 }
2848 })
2849}
2850
Jiyong Park58e364a2019-01-19 19:24:06 +09002851func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002852 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002853 apex {
2854 name: "myapex",
2855 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002856 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002857 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09002858 }
2859
2860 apex {
2861 name: "otherapex",
2862 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002863 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002864 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002865 }
2866
2867 apex_key {
2868 name: "myapex.key",
2869 public_key: "testkey.avbpubkey",
2870 private_key: "testkey.pem",
2871 }
2872
2873 cc_library {
2874 name: "mylib",
2875 srcs: ["mylib.cpp"],
2876 system_shared_libs: [],
2877 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002878 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002879 "myapex",
2880 "otherapex",
2881 ],
Jooyung Han24282772020-03-21 23:20:55 +09002882 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002883 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002884 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09002885 cc_library {
2886 name: "mylib2",
2887 srcs: ["mylib.cpp"],
2888 system_shared_libs: [],
2889 stl: "none",
2890 apex_available: [
2891 "myapex",
2892 "otherapex",
2893 ],
Colin Crossaede88c2020-08-11 12:17:01 -07002894 static_libs: ["mylib3"],
2895 recovery_available: true,
2896 min_sdk_version: "29",
2897 }
2898 cc_library {
2899 name: "mylib3",
2900 srcs: ["mylib.cpp"],
2901 system_shared_libs: [],
2902 stl: "none",
2903 apex_available: [
2904 "myapex",
2905 "otherapex",
2906 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09002907 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07002908 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002909 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002910 }
Jiyong Park58e364a2019-01-19 19:24:06 +09002911 `)
2912
Jooyung Hanc87a0592020-03-02 17:44:33 +09002913 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08002914 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002915 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002916 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
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_apex10000").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__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09002922 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002923
Jooyung Hanccce2f22020-03-07 03:45:53 +09002924 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07002925 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09002926 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002927 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09002928 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002929
Colin Crossaede88c2020-08-11 12:17:01 -07002930 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
2931 // each variant defines additional macros to distinguish which apex variant it is built for
2932
2933 // non-APEX variant does not have __ANDROID_APEX__ defined
2934 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2935 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2936
2937 // APEX variant has __ANDROID_APEX__ defined
2938 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
2939 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2940 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2941 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
2942
2943 // APEX variant has __ANDROID_APEX__ defined
2944 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
2945 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2946 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2947 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
2948
Dan Albertb19953d2020-11-17 15:29:36 -08002949 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07002950 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2951 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002952 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07002953
2954 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
2955 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09002956
2957 // non-APEX variant does not have __ANDROID_APEX__ defined
2958 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2959 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2960
2961 // APEX variant has __ANDROID_APEX__ defined
2962 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002963 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07002964 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002965 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002966
Jooyung Hanc87a0592020-03-02 17:44:33 +09002967 // APEX variant has __ANDROID_APEX__ defined
2968 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002969 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002970 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07002971 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09002972
Dan Albertb19953d2020-11-17 15:29:36 -08002973 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07002974 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09002975 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002976 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002977}
Jiyong Park7e636d02019-01-28 16:16:54 +09002978
2979func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002980 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09002981 apex {
2982 name: "myapex",
2983 key: "myapex.key",
2984 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002985 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09002986 }
2987
2988 apex_key {
2989 name: "myapex.key",
2990 public_key: "testkey.avbpubkey",
2991 private_key: "testkey.pem",
2992 }
2993
2994 cc_library_headers {
2995 name: "mylib_headers",
2996 export_include_dirs: ["my_include"],
2997 system_shared_libs: [],
2998 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09002999 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003000 }
3001
3002 cc_library {
3003 name: "mylib",
3004 srcs: ["mylib.cpp"],
3005 system_shared_libs: [],
3006 stl: "none",
3007 header_libs: ["mylib_headers"],
3008 export_header_lib_headers: ["mylib_headers"],
3009 stubs: {
3010 versions: ["1", "2", "3"],
3011 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003012 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003013 }
3014
3015 cc_library {
3016 name: "otherlib",
3017 srcs: ["mylib.cpp"],
3018 system_shared_libs: [],
3019 stl: "none",
3020 shared_libs: ["mylib"],
3021 }
3022 `)
3023
Colin Cross7113d202019-11-20 16:39:12 -08003024 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003025
3026 // Ensure that the include path of the header lib is exported to 'otherlib'
3027 ensureContains(t, cFlags, "-Imy_include")
3028}
Alex Light9670d332019-01-29 18:07:33 -08003029
Jiyong Park7cd10e32020-01-14 09:22:18 +09003030type fileInApex struct {
3031 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003032 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003033 isLink bool
3034}
3035
Jooyung Hana57af4a2020-01-23 05:36:59 +00003036func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003037 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003038 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003039 copyCmds := apexRule.Args["copy_commands"]
3040 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003041 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003042 for _, cmd := range strings.Split(copyCmds, "&&") {
3043 cmd = strings.TrimSpace(cmd)
3044 if cmd == "" {
3045 continue
3046 }
3047 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003048 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003049 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003050 switch terms[0] {
3051 case "mkdir":
3052 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003053 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003054 t.Fatal("copyCmds contains invalid cp command", cmd)
3055 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003056 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003057 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003058 isLink = false
3059 case "ln":
3060 if len(terms) != 3 && len(terms) != 4 {
3061 // ln LINK TARGET or ln -s LINK TARGET
3062 t.Fatal("copyCmds contains invalid ln command", cmd)
3063 }
3064 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003065 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003066 isLink = true
3067 default:
3068 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3069 }
3070 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003071 index := strings.Index(dst, imageApexDir)
3072 if index == -1 {
3073 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3074 }
3075 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003076 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003077 }
3078 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003079 return ret
3080}
3081
Jooyung Hana57af4a2020-01-23 05:36:59 +00003082func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3083 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003084 var failed bool
3085 var surplus []string
3086 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003087 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003088 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003089 for _, expected := range files {
3090 if matched, _ := path.Match(expected, file.path); matched {
3091 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003092 mactchFound = true
3093 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003094 }
3095 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003096 if !mactchFound {
3097 surplus = append(surplus, file.path)
3098 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003099 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003100
Jooyung Han31c470b2019-10-18 16:26:59 +09003101 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003102 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003103 t.Log("surplus files", surplus)
3104 failed = true
3105 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003106
3107 if len(files) > len(filesMatched) {
3108 var missing []string
3109 for _, expected := range files {
3110 if !filesMatched[expected] {
3111 missing = append(missing, expected)
3112 }
3113 }
3114 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003115 t.Log("missing files", missing)
3116 failed = true
3117 }
3118 if failed {
3119 t.Fail()
3120 }
3121}
3122
Jooyung Han344d5432019-08-23 11:17:39 +09003123func TestVndkApexCurrent(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003124 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003125 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003126 name: "com.android.vndk.current",
3127 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003128 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003129 }
3130
3131 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003132 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003133 public_key: "testkey.avbpubkey",
3134 private_key: "testkey.pem",
3135 }
3136
3137 cc_library {
3138 name: "libvndk",
3139 srcs: ["mylib.cpp"],
3140 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003141 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003142 vndk: {
3143 enabled: true,
3144 },
3145 system_shared_libs: [],
3146 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003147 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003148 }
3149
3150 cc_library {
3151 name: "libvndksp",
3152 srcs: ["mylib.cpp"],
3153 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003154 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003155 vndk: {
3156 enabled: true,
3157 support_system_process: true,
3158 },
3159 system_shared_libs: [],
3160 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003161 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003162 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003163 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003164
Colin Cross2807f002021-03-02 10:15:29 -08003165 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003166 "lib/libvndk.so",
3167 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003168 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003169 "lib64/libvndk.so",
3170 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003171 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003172 "etc/llndk.libraries.29.txt",
3173 "etc/vndkcore.libraries.29.txt",
3174 "etc/vndksp.libraries.29.txt",
3175 "etc/vndkprivate.libraries.29.txt",
3176 "etc/vndkproduct.libraries.29.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003177 })
Jooyung Han344d5432019-08-23 11:17:39 +09003178}
3179
3180func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003181 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003182 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003183 name: "com.android.vndk.current",
3184 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003185 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003186 }
3187
3188 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003189 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003190 public_key: "testkey.avbpubkey",
3191 private_key: "testkey.pem",
3192 }
3193
3194 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003195 name: "libvndk",
3196 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003197 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003198 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003199 vndk: {
3200 enabled: true,
3201 },
3202 system_shared_libs: [],
3203 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003204 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003205 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003206
3207 cc_prebuilt_library_shared {
3208 name: "libvndk.arm",
3209 srcs: ["libvndk.arm.so"],
3210 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003211 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003212 vndk: {
3213 enabled: true,
3214 },
3215 enabled: false,
3216 arch: {
3217 arm: {
3218 enabled: true,
3219 },
3220 },
3221 system_shared_libs: [],
3222 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003223 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003224 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003225 `+vndkLibrariesTxtFiles("current"),
3226 withFiles(map[string][]byte{
3227 "libvndk.so": nil,
3228 "libvndk.arm.so": nil,
3229 }))
Colin Cross2807f002021-03-02 10:15:29 -08003230 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003231 "lib/libvndk.so",
3232 "lib/libvndk.arm.so",
3233 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003234 "lib/libc++.so",
3235 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003236 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003237 })
Jooyung Han344d5432019-08-23 11:17:39 +09003238}
3239
Jooyung Han39edb6c2019-11-06 16:53:07 +09003240func vndkLibrariesTxtFiles(vers ...string) (result string) {
3241 for _, v := range vers {
3242 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003243 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003244 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003245 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003246 name: "` + txt + `.libraries.txt",
3247 }
3248 `
3249 }
3250 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003251 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003252 result += `
3253 prebuilt_etc {
3254 name: "` + txt + `.libraries.` + v + `.txt",
3255 src: "dummy.txt",
3256 }
3257 `
3258 }
3259 }
3260 }
3261 return
3262}
3263
Jooyung Han344d5432019-08-23 11:17:39 +09003264func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003265 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003266 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003267 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003268 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003269 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003270 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003271 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003272 }
3273
3274 apex_key {
3275 name: "myapex.key",
3276 public_key: "testkey.avbpubkey",
3277 private_key: "testkey.pem",
3278 }
3279
Jooyung Han31c470b2019-10-18 16:26:59 +09003280 vndk_prebuilt_shared {
3281 name: "libvndk27",
3282 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003283 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003284 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003285 vndk: {
3286 enabled: true,
3287 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003288 target_arch: "arm64",
3289 arch: {
3290 arm: {
3291 srcs: ["libvndk27_arm.so"],
3292 },
3293 arm64: {
3294 srcs: ["libvndk27_arm64.so"],
3295 },
3296 },
Colin Cross2807f002021-03-02 10:15:29 -08003297 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003298 }
3299
3300 vndk_prebuilt_shared {
3301 name: "libvndk27",
3302 version: "27",
3303 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003304 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003305 vndk: {
3306 enabled: true,
3307 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003308 target_arch: "x86_64",
3309 arch: {
3310 x86: {
3311 srcs: ["libvndk27_x86.so"],
3312 },
3313 x86_64: {
3314 srcs: ["libvndk27_x86_64.so"],
3315 },
3316 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003317 }
3318 `+vndkLibrariesTxtFiles("27"),
3319 withFiles(map[string][]byte{
3320 "libvndk27_arm.so": nil,
3321 "libvndk27_arm64.so": nil,
3322 "libvndk27_x86.so": nil,
3323 "libvndk27_x86_64.so": nil,
3324 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003325
Colin Cross2807f002021-03-02 10:15:29 -08003326 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003327 "lib/libvndk27_arm.so",
3328 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003329 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003330 })
Jooyung Han344d5432019-08-23 11:17:39 +09003331}
3332
Jooyung Han90eee022019-10-01 20:02:42 +09003333func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003334 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003335 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003336 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003337 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003338 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003339 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003340 }
3341 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003342 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003343 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003344 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003345 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003346 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003347 }
3348 apex_key {
3349 name: "myapex.key",
3350 public_key: "testkey.avbpubkey",
3351 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003352 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003353
3354 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003355 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003356 actual := proptools.String(bundle.properties.Apex_name)
3357 if !reflect.DeepEqual(actual, expected) {
3358 t.Errorf("Got '%v', expected '%v'", actual, expected)
3359 }
3360 }
3361
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003362 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003363 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003364}
3365
Jooyung Han344d5432019-08-23 11:17:39 +09003366func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003367 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003368 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003369 name: "com.android.vndk.current",
3370 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003371 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003372 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003373 }
3374
3375 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003376 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003377 public_key: "testkey.avbpubkey",
3378 private_key: "testkey.pem",
3379 }
3380
3381 cc_library {
3382 name: "libvndk",
3383 srcs: ["mylib.cpp"],
3384 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003385 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003386 native_bridge_supported: true,
3387 host_supported: true,
3388 vndk: {
3389 enabled: true,
3390 },
3391 system_shared_libs: [],
3392 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003393 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003394 }
Colin Cross2807f002021-03-02 10:15:29 -08003395 `+vndkLibrariesTxtFiles("current"),
3396 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003397
Colin Cross2807f002021-03-02 10:15:29 -08003398 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003399 "lib/libvndk.so",
3400 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003401 "lib/libc++.so",
3402 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003403 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003404 })
Jooyung Han344d5432019-08-23 11:17:39 +09003405}
3406
3407func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003408 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003409 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003410 name: "com.android.vndk.current",
3411 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003412 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003413 native_bridge_supported: true,
3414 }
3415
3416 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003417 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003418 public_key: "testkey.avbpubkey",
3419 private_key: "testkey.pem",
3420 }
3421
3422 cc_library {
3423 name: "libvndk",
3424 srcs: ["mylib.cpp"],
3425 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003426 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003427 native_bridge_supported: true,
3428 host_supported: true,
3429 vndk: {
3430 enabled: true,
3431 },
3432 system_shared_libs: [],
3433 stl: "none",
3434 }
3435 `)
3436}
3437
Jooyung Han31c470b2019-10-18 16:26:59 +09003438func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003439 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003440 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003441 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003442 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003443 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003444 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003445 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003446 }
3447
3448 apex_key {
3449 name: "myapex.key",
3450 public_key: "testkey.avbpubkey",
3451 private_key: "testkey.pem",
3452 }
3453
3454 vndk_prebuilt_shared {
3455 name: "libvndk27",
3456 version: "27",
3457 target_arch: "arm",
3458 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003459 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003460 vndk: {
3461 enabled: true,
3462 },
3463 arch: {
3464 arm: {
3465 srcs: ["libvndk27.so"],
3466 }
3467 },
3468 }
3469
3470 vndk_prebuilt_shared {
3471 name: "libvndk27",
3472 version: "27",
3473 target_arch: "arm",
3474 binder32bit: true,
3475 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003476 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003477 vndk: {
3478 enabled: true,
3479 },
3480 arch: {
3481 arm: {
3482 srcs: ["libvndk27binder32.so"],
3483 }
3484 },
Colin Cross2807f002021-03-02 10:15:29 -08003485 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003486 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003487 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003488 withFiles(map[string][]byte{
3489 "libvndk27.so": nil,
3490 "libvndk27binder32.so": nil,
3491 }),
3492 withBinder32bit,
3493 withTargets(map[android.OsType][]android.Target{
3494 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003495 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3496 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003497 },
3498 }),
3499 )
3500
Colin Cross2807f002021-03-02 10:15:29 -08003501 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003502 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003503 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003504 })
3505}
3506
Jooyung Han45a96772020-06-15 14:59:42 +09003507func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003508 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003509 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003510 name: "com.android.vndk.current",
3511 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003512 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003513 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003514 }
3515
3516 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003517 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003518 public_key: "testkey.avbpubkey",
3519 private_key: "testkey.pem",
3520 }
3521
3522 cc_library {
3523 name: "libz",
3524 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003525 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003526 vndk: {
3527 enabled: true,
3528 },
3529 stubs: {
3530 symbol_file: "libz.map.txt",
3531 versions: ["30"],
3532 }
3533 }
3534 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3535 "libz.map.txt": nil,
3536 }))
3537
Colin Cross2807f002021-03-02 10:15:29 -08003538 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003539 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3540 ensureListEmpty(t, provideNativeLibs)
3541}
3542
Jooyung Hane1633032019-08-01 17:41:43 +09003543func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003544 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003545 apex {
3546 name: "myapex_nodep",
3547 key: "myapex.key",
3548 native_shared_libs: ["lib_nodep"],
3549 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003550 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003551 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003552 }
3553
3554 apex {
3555 name: "myapex_dep",
3556 key: "myapex.key",
3557 native_shared_libs: ["lib_dep"],
3558 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003559 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003560 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003561 }
3562
3563 apex {
3564 name: "myapex_provider",
3565 key: "myapex.key",
3566 native_shared_libs: ["libfoo"],
3567 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003568 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003569 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003570 }
3571
3572 apex {
3573 name: "myapex_selfcontained",
3574 key: "myapex.key",
3575 native_shared_libs: ["lib_dep", "libfoo"],
3576 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003577 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003578 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003579 }
3580
3581 apex_key {
3582 name: "myapex.key",
3583 public_key: "testkey.avbpubkey",
3584 private_key: "testkey.pem",
3585 }
3586
3587 cc_library {
3588 name: "lib_nodep",
3589 srcs: ["mylib.cpp"],
3590 system_shared_libs: [],
3591 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003592 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003593 }
3594
3595 cc_library {
3596 name: "lib_dep",
3597 srcs: ["mylib.cpp"],
3598 shared_libs: ["libfoo"],
3599 system_shared_libs: [],
3600 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003601 apex_available: [
3602 "myapex_dep",
3603 "myapex_provider",
3604 "myapex_selfcontained",
3605 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003606 }
3607
3608 cc_library {
3609 name: "libfoo",
3610 srcs: ["mytest.cpp"],
3611 stubs: {
3612 versions: ["1"],
3613 },
3614 system_shared_libs: [],
3615 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003616 apex_available: [
3617 "myapex_provider",
3618 "myapex_selfcontained",
3619 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003620 }
3621 `)
3622
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003623 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003624 var provideNativeLibs, requireNativeLibs []string
3625
Sundong Ahnabb64432019-10-22 13:58:29 +09003626 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_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 ensureListEmpty(t, requireNativeLibs)
3631
Sundong Ahnabb64432019-10-22 13:58:29 +09003632 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_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 ensureListEmpty(t, provideNativeLibs)
3636 ensureListContains(t, requireNativeLibs, "libfoo.so")
3637
Sundong Ahnabb64432019-10-22 13:58:29 +09003638 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_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
Sundong Ahnabb64432019-10-22 13:58:29 +09003644 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003645 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3646 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003647 ensureListContains(t, provideNativeLibs, "libfoo.so")
3648 ensureListEmpty(t, requireNativeLibs)
3649}
3650
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003651func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003652 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003653 apex {
3654 name: "myapex",
3655 key: "myapex.key",
3656 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003657 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003658 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003659 }
3660
3661 apex_key {
3662 name: "myapex.key",
3663 public_key: "testkey.avbpubkey",
3664 private_key: "testkey.pem",
3665 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003666
3667 cc_library {
3668 name: "mylib",
3669 srcs: ["mylib.cpp"],
3670 system_shared_libs: [],
3671 stl: "none",
3672 apex_available: [
3673 "//apex_available:platform",
3674 "myapex",
3675 ],
3676 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003677 `)
3678
Martin Stjernholmd8da28e2021-06-24 14:37:13 +01003679 module := ctx.ModuleForTests("myapex", "android_common_com.android.myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003680 apexManifestRule := module.Rule("apexManifestRule")
3681 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3682 apexRule := module.Rule("apexRule")
3683 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003684
Martin Stjernholmd8da28e2021-06-24 14:37:13 +01003685 apexBundle := module.Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003686 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09003687 name := apexBundle.BaseModuleName()
3688 prefix := "TARGET_"
3689 var builder strings.Builder
3690 data.Custom(&builder, name, prefix, "", data)
3691 androidMk := builder.String()
3692 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3693 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003694}
3695
Alex Light0851b882019-02-07 13:20:53 -08003696func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003697 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003698 apex {
3699 name: "myapex",
3700 key: "myapex.key",
3701 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003702 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003703 }
3704
3705 apex_key {
3706 name: "myapex.key",
3707 public_key: "testkey.avbpubkey",
3708 private_key: "testkey.pem",
3709 }
3710
3711 cc_library {
3712 name: "mylib_common",
3713 srcs: ["mylib.cpp"],
3714 system_shared_libs: [],
3715 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003716 apex_available: [
3717 "//apex_available:platform",
3718 "myapex",
3719 ],
Alex Light0851b882019-02-07 13:20:53 -08003720 }
3721 `)
3722
Sundong Ahnabb64432019-10-22 13:58:29 +09003723 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003724 apexRule := module.Rule("apexRule")
3725 copyCmds := apexRule.Args["copy_commands"]
3726
3727 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3728 t.Log("Apex was a test apex!")
3729 t.Fail()
3730 }
3731 // Ensure that main rule creates an output
3732 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3733
3734 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003735 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003736
3737 // Ensure that both direct and indirect deps are copied into apex
3738 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3739
Colin Cross7113d202019-11-20 16:39:12 -08003740 // Ensure that the platform variant ends with _shared
3741 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003742
Colin Cross56a83212020-09-15 18:30:11 -07003743 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003744 t.Log("Found mylib_common not in any apex!")
3745 t.Fail()
3746 }
3747}
3748
3749func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003750 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003751 apex_test {
3752 name: "myapex",
3753 key: "myapex.key",
3754 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003755 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003756 }
3757
3758 apex_key {
3759 name: "myapex.key",
3760 public_key: "testkey.avbpubkey",
3761 private_key: "testkey.pem",
3762 }
3763
3764 cc_library {
3765 name: "mylib_common_test",
3766 srcs: ["mylib.cpp"],
3767 system_shared_libs: [],
3768 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003769 // TODO: remove //apex_available:platform
3770 apex_available: [
3771 "//apex_available:platform",
3772 "myapex",
3773 ],
Alex Light0851b882019-02-07 13:20:53 -08003774 }
3775 `)
3776
Sundong Ahnabb64432019-10-22 13:58:29 +09003777 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003778 apexRule := module.Rule("apexRule")
3779 copyCmds := apexRule.Args["copy_commands"]
3780
3781 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3782 t.Log("Apex was not a test apex!")
3783 t.Fail()
3784 }
3785 // Ensure that main rule creates an output
3786 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3787
3788 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003789 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003790
3791 // Ensure that both direct and indirect deps are copied into apex
3792 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3793
Colin Cross7113d202019-11-20 16:39:12 -08003794 // Ensure that the platform variant ends with _shared
3795 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003796}
3797
Alex Light9670d332019-01-29 18:07:33 -08003798func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003799 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003800 apex {
3801 name: "myapex",
3802 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003803 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08003804 multilib: {
3805 first: {
3806 native_shared_libs: ["mylib_common"],
3807 }
3808 },
3809 target: {
3810 android: {
3811 multilib: {
3812 first: {
3813 native_shared_libs: ["mylib"],
3814 }
3815 }
3816 },
3817 host: {
3818 multilib: {
3819 first: {
3820 native_shared_libs: ["mylib2"],
3821 }
3822 }
3823 }
3824 }
3825 }
3826
3827 apex_key {
3828 name: "myapex.key",
3829 public_key: "testkey.avbpubkey",
3830 private_key: "testkey.pem",
3831 }
3832
3833 cc_library {
3834 name: "mylib",
3835 srcs: ["mylib.cpp"],
3836 system_shared_libs: [],
3837 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003838 // TODO: remove //apex_available:platform
3839 apex_available: [
3840 "//apex_available:platform",
3841 "myapex",
3842 ],
Alex Light9670d332019-01-29 18:07:33 -08003843 }
3844
3845 cc_library {
3846 name: "mylib_common",
3847 srcs: ["mylib.cpp"],
3848 system_shared_libs: [],
3849 stl: "none",
3850 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003851 // TODO: remove //apex_available:platform
3852 apex_available: [
3853 "//apex_available:platform",
3854 "myapex",
3855 ],
Alex Light9670d332019-01-29 18:07:33 -08003856 }
3857
3858 cc_library {
3859 name: "mylib2",
3860 srcs: ["mylib.cpp"],
3861 system_shared_libs: [],
3862 stl: "none",
3863 compile_multilib: "first",
3864 }
3865 `)
3866
Sundong Ahnabb64432019-10-22 13:58:29 +09003867 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003868 copyCmds := apexRule.Args["copy_commands"]
3869
3870 // Ensure that main rule creates an output
3871 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3872
3873 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003874 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
3875 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
3876 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08003877
3878 // Ensure that both direct and indirect deps are copied into apex
3879 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3880 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3881 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3882
Colin Cross7113d202019-11-20 16:39:12 -08003883 // Ensure that the platform variant ends with _shared
3884 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
3885 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
3886 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08003887}
Jiyong Park04480cf2019-02-06 00:16:29 +09003888
Jiyong Park59140302020-12-14 18:44:04 +09003889func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003890 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09003891 apex {
3892 name: "myapex",
3893 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003894 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09003895 arch: {
3896 arm64: {
3897 native_shared_libs: ["mylib.arm64"],
3898 },
3899 x86_64: {
3900 native_shared_libs: ["mylib.x64"],
3901 },
3902 }
3903 }
3904
3905 apex_key {
3906 name: "myapex.key",
3907 public_key: "testkey.avbpubkey",
3908 private_key: "testkey.pem",
3909 }
3910
3911 cc_library {
3912 name: "mylib.arm64",
3913 srcs: ["mylib.cpp"],
3914 system_shared_libs: [],
3915 stl: "none",
3916 // TODO: remove //apex_available:platform
3917 apex_available: [
3918 "//apex_available:platform",
3919 "myapex",
3920 ],
3921 }
3922
3923 cc_library {
3924 name: "mylib.x64",
3925 srcs: ["mylib.cpp"],
3926 system_shared_libs: [],
3927 stl: "none",
3928 // TODO: remove //apex_available:platform
3929 apex_available: [
3930 "//apex_available:platform",
3931 "myapex",
3932 ],
3933 }
3934 `)
3935
3936 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
3937 copyCmds := apexRule.Args["copy_commands"]
3938
3939 // Ensure that apex variant is created for the direct dep
3940 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
3941 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
3942
3943 // Ensure that both direct and indirect deps are copied into apex
3944 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
3945 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
3946}
3947
Jiyong Park04480cf2019-02-06 00:16:29 +09003948func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003949 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09003950 apex {
3951 name: "myapex",
3952 key: "myapex.key",
3953 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003954 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09003955 }
3956
3957 apex_key {
3958 name: "myapex.key",
3959 public_key: "testkey.avbpubkey",
3960 private_key: "testkey.pem",
3961 }
3962
3963 sh_binary {
3964 name: "myscript",
3965 src: "mylib.cpp",
3966 filename: "myscript.sh",
3967 sub_dir: "script",
3968 }
3969 `)
3970
Sundong Ahnabb64432019-10-22 13:58:29 +09003971 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09003972 copyCmds := apexRule.Args["copy_commands"]
3973
3974 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
3975}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003976
Jooyung Han91df2082019-11-20 01:49:42 +09003977func TestApexInVariousPartition(t *testing.T) {
3978 testcases := []struct {
3979 propName, parition, flattenedPartition string
3980 }{
3981 {"", "system", "system_ext"},
3982 {"product_specific: true", "product", "product"},
3983 {"soc_specific: true", "vendor", "vendor"},
3984 {"proprietary: true", "vendor", "vendor"},
3985 {"vendor: true", "vendor", "vendor"},
3986 {"system_ext_specific: true", "system_ext", "system_ext"},
3987 }
3988 for _, tc := range testcases {
3989 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003990 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09003991 apex {
3992 name: "myapex",
3993 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003994 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09003995 `+tc.propName+`
3996 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003997
Jooyung Han91df2082019-11-20 01:49:42 +09003998 apex_key {
3999 name: "myapex.key",
4000 public_key: "testkey.avbpubkey",
4001 private_key: "testkey.pem",
4002 }
4003 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004004
Jooyung Han91df2082019-11-20 01:49:42 +09004005 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004006 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4007 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004008 if actual != expected {
4009 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4010 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004011
Jooyung Han91df2082019-11-20 01:49:42 +09004012 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004013 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4014 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004015 if actual != expected {
4016 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4017 }
4018 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004019 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004020}
Jiyong Park67882562019-03-21 01:11:21 +09004021
Jooyung Han580eb4f2020-06-24 19:33:06 +09004022func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004023 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004024 apex {
4025 name: "myapex",
4026 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004027 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004028 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004029
Jooyung Han580eb4f2020-06-24 19:33:06 +09004030 apex_key {
4031 name: "myapex.key",
4032 public_key: "testkey.avbpubkey",
4033 private_key: "testkey.pem",
4034 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004035 `)
4036 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004037 rule := module.Output("file_contexts")
4038 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4039}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004040
Jooyung Han580eb4f2020-06-24 19:33:06 +09004041func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004042 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004043 apex {
4044 name: "myapex",
4045 key: "myapex.key",
4046 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004047 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004048 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004049
Jooyung Han580eb4f2020-06-24 19:33:06 +09004050 apex_key {
4051 name: "myapex.key",
4052 public_key: "testkey.avbpubkey",
4053 private_key: "testkey.pem",
4054 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004055 `, withFiles(map[string][]byte{
4056 "my_own_file_contexts": nil,
4057 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004058}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004059
Jooyung Han580eb4f2020-06-24 19:33:06 +09004060func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004061 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004062 apex {
4063 name: "myapex",
4064 key: "myapex.key",
4065 product_specific: true,
4066 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004067 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004068 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004069
Jooyung Han580eb4f2020-06-24 19:33:06 +09004070 apex_key {
4071 name: "myapex.key",
4072 public_key: "testkey.avbpubkey",
4073 private_key: "testkey.pem",
4074 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004075 `)
4076
Colin Cross1c460562021-02-16 17:55:47 -08004077 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004078 apex {
4079 name: "myapex",
4080 key: "myapex.key",
4081 product_specific: true,
4082 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004083 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004084 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004085
Jooyung Han580eb4f2020-06-24 19:33:06 +09004086 apex_key {
4087 name: "myapex.key",
4088 public_key: "testkey.avbpubkey",
4089 private_key: "testkey.pem",
4090 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004091 `, withFiles(map[string][]byte{
4092 "product_specific_file_contexts": nil,
4093 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004094 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4095 rule := module.Output("file_contexts")
4096 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4097}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004098
Jooyung Han580eb4f2020-06-24 19:33:06 +09004099func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004100 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004101 apex {
4102 name: "myapex",
4103 key: "myapex.key",
4104 product_specific: true,
4105 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004106 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004107 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004108
Jooyung Han580eb4f2020-06-24 19:33:06 +09004109 apex_key {
4110 name: "myapex.key",
4111 public_key: "testkey.avbpubkey",
4112 private_key: "testkey.pem",
4113 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004114
Jooyung Han580eb4f2020-06-24 19:33:06 +09004115 filegroup {
4116 name: "my-file-contexts",
4117 srcs: ["product_specific_file_contexts"],
4118 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004119 `, withFiles(map[string][]byte{
4120 "product_specific_file_contexts": nil,
4121 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004122 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4123 rule := module.Output("file_contexts")
4124 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004125}
4126
Jiyong Park67882562019-03-21 01:11:21 +09004127func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004128 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004129 apex_key {
4130 name: "myapex.key",
4131 public_key: ":my.avbpubkey",
4132 private_key: ":my.pem",
4133 product_specific: true,
4134 }
4135
4136 filegroup {
4137 name: "my.avbpubkey",
4138 srcs: ["testkey2.avbpubkey"],
4139 }
4140
4141 filegroup {
4142 name: "my.pem",
4143 srcs: ["testkey2.pem"],
4144 }
4145 `)
4146
4147 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4148 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004149 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004150 if actual_pubkey != expected_pubkey {
4151 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4152 }
4153 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004154 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004155 if actual_privkey != expected_privkey {
4156 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4157 }
4158}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004159
4160func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004161 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004162 prebuilt_apex {
4163 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004164 arch: {
4165 arm64: {
4166 src: "myapex-arm64.apex",
4167 },
4168 arm: {
4169 src: "myapex-arm.apex",
4170 },
4171 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004172 }
4173 `)
4174
Paul Duffin5ec165d2021-06-15 19:09:41 +01004175 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004176
Jiyong Parkc95714e2019-03-29 14:23:10 +09004177 expectedInput := "myapex-arm64.apex"
4178 if prebuilt.inputApex.String() != expectedInput {
4179 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4180 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004181}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004182
Paul Duffinc0609c62021-03-01 17:27:16 +00004183func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin5ec165d2021-06-15 19:09:41 +01004184 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004185 prebuilt_apex {
4186 name: "myapex",
4187 }
4188 `)
4189}
4190
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004191func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004192 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004193 prebuilt_apex {
4194 name: "myapex",
4195 src: "myapex-arm.apex",
4196 filename: "notmyapex.apex",
4197 }
4198 `)
4199
Paul Duffin5ec165d2021-06-15 19:09:41 +01004200 p := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004201
4202 expected := "notmyapex.apex"
4203 if p.installFilename != expected {
4204 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4205 }
4206}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004207
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004208func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004209 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004210 prebuilt_apex {
4211 name: "myapex.prebuilt",
4212 src: "myapex-arm.apex",
4213 overrides: [
4214 "myapex",
4215 ],
4216 }
4217 `)
4218
Paul Duffin5ec165d2021-06-15 19:09:41 +01004219 p := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt").Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004220
4221 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004222 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004223 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004224 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004225 }
4226}
4227
Martin Stjernholmd8da28e2021-06-24 14:37:13 +01004228func TestPrebuiltApexName(t *testing.T) {
4229 testApex(t, `
4230 prebuilt_apex {
4231 name: "com.company.android.myapex",
4232 apex_name: "com.android.myapex",
4233 src: "company-myapex-arm.apex",
4234 }
4235 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4236
4237 testApex(t, `
4238 apex_set {
4239 name: "com.company.android.myapex",
4240 apex_name: "com.android.myapex",
4241 set: "company-myapex.apks",
4242 }
4243 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4244}
4245
4246func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4247 _ = android.GroupFixturePreparers(
4248 java.PrepareForTestWithJavaDefaultModules,
4249 PrepareForTestWithApexBuildComponents,
4250 android.FixtureWithRootAndroidBp(`
4251 platform_bootclasspath {
4252 name: "platform-bootclasspath",
4253 fragments: [
4254 {
4255 apex: "com.android.art",
4256 module: "art-bootclasspath-fragment",
4257 },
4258 ],
4259 }
4260
4261 prebuilt_apex {
4262 name: "com.company.android.art",
4263 apex_name: "com.android.art",
4264 src: "com.company.android.art-arm.apex",
4265 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4266 }
4267
4268 prebuilt_bootclasspath_fragment {
4269 name: "art-bootclasspath-fragment",
4270 contents: ["core-oj"],
4271 }
4272
4273 java_import {
4274 name: "core-oj",
4275 jars: ["prebuilt.jar"],
4276 }
4277 `),
4278 ).RunTest(t)
4279}
4280
Paul Duffin092153d2021-01-26 11:42:39 +00004281// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4282// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004283func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004284 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004285
Paul Duffin89886cb2021-02-05 16:44:03 +00004286 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004287 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004288 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004289 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004290 stem := android.RemoveOptionalPrebuiltPrefix(name)
4291 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004292 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4293 }
4294 }
4295
Paul Duffin39853512021-02-26 11:09:39 +00004296 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004297 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004298 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004299 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4300 }
4301 }
4302
4303 t.Run("prebuilt only", func(t *testing.T) {
4304 bp := `
4305 prebuilt_apex {
4306 name: "myapex",
4307 arch: {
4308 arm64: {
4309 src: "myapex-arm64.apex",
4310 },
4311 arm: {
4312 src: "myapex-arm.apex",
4313 },
4314 },
Paul Duffin39853512021-02-26 11:09:39 +00004315 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004316 }
4317
4318 java_import {
4319 name: "libfoo",
4320 jars: ["libfoo.jar"],
4321 }
Paul Duffin39853512021-02-26 11:09:39 +00004322
4323 java_sdk_library_import {
4324 name: "libbar",
4325 public: {
4326 jars: ["libbar.jar"],
4327 },
4328 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004329 `
4330
4331 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4332 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4333
Paul Duffinf6932af2021-02-26 18:21:56 +00004334 // Make sure that the deapexer has the correct input APEX.
4335 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4336 rule := deapexer.Rule("deapexer")
4337 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4338 t.Errorf("expected: %q, found: %q", expected, actual)
4339 }
4340
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004341 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin5ec165d2021-06-15 19:09:41 +01004342 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004343 rule = prebuiltApex.Rule("android/soong/android.Cp")
4344 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4345 t.Errorf("expected: %q, found: %q", expected, actual)
4346 }
4347
Paul Duffin89886cb2021-02-05 16:44:03 +00004348 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004349
4350 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004351 })
4352
4353 t.Run("prebuilt with source preferred", func(t *testing.T) {
4354
4355 bp := `
4356 prebuilt_apex {
4357 name: "myapex",
4358 arch: {
4359 arm64: {
4360 src: "myapex-arm64.apex",
4361 },
4362 arm: {
4363 src: "myapex-arm.apex",
4364 },
4365 },
Paul Duffin39853512021-02-26 11:09:39 +00004366 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004367 }
4368
4369 java_import {
4370 name: "libfoo",
4371 jars: ["libfoo.jar"],
4372 }
4373
4374 java_library {
4375 name: "libfoo",
4376 }
Paul Duffin39853512021-02-26 11:09:39 +00004377
4378 java_sdk_library_import {
4379 name: "libbar",
4380 public: {
4381 jars: ["libbar.jar"],
4382 },
4383 }
4384
4385 java_sdk_library {
4386 name: "libbar",
4387 srcs: ["foo/bar/MyClass.java"],
4388 unsafe_ignore_missing_latest_api: true,
4389 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004390 `
4391
4392 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4393 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4394
Paul Duffin89886cb2021-02-05 16:44:03 +00004395 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004396 ensureNoSourceVariant(t, ctx, "libfoo")
4397
4398 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4399 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004400 })
4401
4402 t.Run("prebuilt preferred with source", func(t *testing.T) {
4403 bp := `
4404 prebuilt_apex {
4405 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004406 arch: {
4407 arm64: {
4408 src: "myapex-arm64.apex",
4409 },
4410 arm: {
4411 src: "myapex-arm.apex",
4412 },
4413 },
Paul Duffin39853512021-02-26 11:09:39 +00004414 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004415 }
4416
4417 java_import {
4418 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004419 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004420 jars: ["libfoo.jar"],
4421 }
4422
4423 java_library {
4424 name: "libfoo",
4425 }
Paul Duffin39853512021-02-26 11:09:39 +00004426
4427 java_sdk_library_import {
4428 name: "libbar",
4429 prefer: true,
4430 public: {
4431 jars: ["libbar.jar"],
4432 },
4433 }
4434
4435 java_sdk_library {
4436 name: "libbar",
4437 srcs: ["foo/bar/MyClass.java"],
4438 unsafe_ignore_missing_latest_api: true,
4439 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004440 `
4441
4442 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4443 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4444
Paul Duffin89886cb2021-02-05 16:44:03 +00004445 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004446 ensureNoSourceVariant(t, ctx, "libfoo")
4447
4448 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4449 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004450 })
4451}
4452
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004453func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01004454 preparer := android.GroupFixturePreparers(
4455 java.FixtureConfigureBootJars("myapex:libfoo", "myapex:libbar"),
4456 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
4457 // is disabled.
4458 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
4459 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004460
Paul Duffin37856732021-02-26 14:24:15 +00004461 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4462 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01004463 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004464 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004465 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004466 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004467 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004468 foundLibfooJar = true
4469 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01004470 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004471 }
4472 }
4473 if !foundLibfooJar {
Paul Duffin55607122021-03-30 23:32:51 +01004474 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 +00004475 }
4476 }
4477
Paul Duffin2a999332021-06-07 21:36:01 +01004478 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004479 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01004480 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffin2a999332021-06-07 21:36:01 +01004481 var rule android.TestingBuildParams
4482
4483 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
4484 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00004485 }
4486
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004487 fragment := java.ApexVariantReference{
4488 Apex: proptools.StringPtr("myapex"),
4489 Module: proptools.StringPtr("my-bootclasspath-fragment"),
4490 }
4491
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004492 t.Run("prebuilt only", func(t *testing.T) {
4493 bp := `
4494 prebuilt_apex {
4495 name: "myapex",
4496 arch: {
4497 arm64: {
4498 src: "myapex-arm64.apex",
4499 },
4500 arm: {
4501 src: "myapex-arm.apex",
4502 },
4503 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004504 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4505 }
4506
4507 prebuilt_bootclasspath_fragment {
4508 name: "my-bootclasspath-fragment",
4509 contents: ["libfoo", "libbar"],
4510 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004511 }
4512
4513 java_import {
4514 name: "libfoo",
4515 jars: ["libfoo.jar"],
4516 apex_available: ["myapex"],
4517 }
Paul Duffin37856732021-02-26 14:24:15 +00004518
4519 java_sdk_library_import {
4520 name: "libbar",
4521 public: {
4522 jars: ["libbar.jar"],
4523 },
4524 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004525 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004526 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004527 `
4528
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004529 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004530 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4531 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004532
Paul Duffin537ea3d2021-05-14 10:38:00 +01004533 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin9d67ca62021-02-03 20:06:33 +00004534 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004535 out/soong/.intermediates/libbar.stubs/android_common/combined/libbar.stubs.jar
4536 out/soong/.intermediates/libfoo/android_common_myapex/combined/libfoo.jar
4537 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004538 })
4539
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004540 t.Run("apex_set only", func(t *testing.T) {
4541 bp := `
4542 apex_set {
4543 name: "myapex",
4544 set: "myapex.apks",
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004545 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4546 }
4547
4548 prebuilt_bootclasspath_fragment {
4549 name: "my-bootclasspath-fragment",
4550 contents: ["libfoo", "libbar"],
4551 apex_available: ["myapex"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004552 }
4553
4554 java_import {
4555 name: "libfoo",
4556 jars: ["libfoo.jar"],
4557 apex_available: ["myapex"],
4558 }
4559
4560 java_sdk_library_import {
4561 name: "libbar",
4562 public: {
4563 jars: ["libbar.jar"],
4564 },
4565 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004566 shared_library: false,
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004567 }
4568 `
4569
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004570 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004571 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4572 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
4573
Paul Duffin537ea3d2021-05-14 10:38:00 +01004574 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004575 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004576 out/soong/.intermediates/libbar.stubs/android_common/combined/libbar.stubs.jar
4577 out/soong/.intermediates/libfoo/android_common_myapex/combined/libfoo.jar
4578 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004579 })
4580
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004581 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4582 bp := `
4583 prebuilt_apex {
4584 name: "myapex",
4585 arch: {
4586 arm64: {
4587 src: "myapex-arm64.apex",
4588 },
4589 arm: {
4590 src: "myapex-arm.apex",
4591 },
4592 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004593 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4594 }
4595
4596 prebuilt_bootclasspath_fragment {
4597 name: "my-bootclasspath-fragment",
4598 contents: ["libfoo", "libbar"],
4599 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004600 }
4601
4602 java_import {
4603 name: "libfoo",
4604 jars: ["libfoo.jar"],
4605 apex_available: ["myapex"],
4606 }
4607
4608 java_library {
4609 name: "libfoo",
4610 srcs: ["foo/bar/MyClass.java"],
4611 apex_available: ["myapex"],
4612 }
Paul Duffin37856732021-02-26 14:24:15 +00004613
4614 java_sdk_library_import {
4615 name: "libbar",
4616 public: {
4617 jars: ["libbar.jar"],
4618 },
4619 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004620 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004621 }
4622
4623 java_sdk_library {
4624 name: "libbar",
4625 srcs: ["foo/bar/MyClass.java"],
4626 unsafe_ignore_missing_latest_api: true,
4627 apex_available: ["myapex"],
4628 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004629 `
4630
4631 // In this test the source (java_library) libfoo is active since the
4632 // prebuilt (java_import) defaults to prefer:false. However the
4633 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4634 // find the dex boot jar in it. We either need to disable the source libfoo
4635 // or make the prebuilt libfoo preferred.
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004636 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004637 })
4638
4639 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4640 bp := `
4641 prebuilt_apex {
4642 name: "myapex",
4643 arch: {
4644 arm64: {
4645 src: "myapex-arm64.apex",
4646 },
4647 arm: {
4648 src: "myapex-arm.apex",
4649 },
4650 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004651 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4652 }
4653
4654 prebuilt_bootclasspath_fragment {
4655 name: "my-bootclasspath-fragment",
4656 contents: ["libfoo", "libbar"],
4657 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004658 }
4659
4660 java_import {
4661 name: "libfoo",
4662 prefer: true,
4663 jars: ["libfoo.jar"],
4664 apex_available: ["myapex"],
4665 }
4666
4667 java_library {
4668 name: "libfoo",
4669 srcs: ["foo/bar/MyClass.java"],
4670 apex_available: ["myapex"],
4671 }
Paul Duffin37856732021-02-26 14:24:15 +00004672
4673 java_sdk_library_import {
4674 name: "libbar",
4675 prefer: true,
4676 public: {
4677 jars: ["libbar.jar"],
4678 },
4679 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004680 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004681 }
4682
4683 java_sdk_library {
4684 name: "libbar",
4685 srcs: ["foo/bar/MyClass.java"],
4686 unsafe_ignore_missing_latest_api: true,
4687 apex_available: ["myapex"],
4688 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004689 `
4690
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004691 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004692 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4693 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004694
Paul Duffin537ea3d2021-05-14 10:38:00 +01004695 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin9d67ca62021-02-03 20:06:33 +00004696 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004697 out/soong/.intermediates/prebuilt_libbar.stubs/android_common/combined/libbar.stubs.jar
4698 out/soong/.intermediates/prebuilt_libfoo/android_common_myapex/combined/libfoo.jar
4699 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004700 })
4701
4702 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4703 bp := `
4704 apex {
4705 name: "myapex",
4706 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004707 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004708 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004709 }
4710
4711 apex_key {
4712 name: "myapex.key",
4713 public_key: "testkey.avbpubkey",
4714 private_key: "testkey.pem",
4715 }
4716
4717 prebuilt_apex {
4718 name: "myapex",
4719 arch: {
4720 arm64: {
4721 src: "myapex-arm64.apex",
4722 },
4723 arm: {
4724 src: "myapex-arm.apex",
4725 },
4726 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004727 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4728 }
4729
4730 prebuilt_bootclasspath_fragment {
4731 name: "my-bootclasspath-fragment",
4732 contents: ["libfoo", "libbar"],
4733 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004734 }
4735
4736 java_import {
4737 name: "libfoo",
4738 jars: ["libfoo.jar"],
4739 apex_available: ["myapex"],
4740 }
4741
4742 java_library {
4743 name: "libfoo",
4744 srcs: ["foo/bar/MyClass.java"],
4745 apex_available: ["myapex"],
4746 }
Paul Duffin37856732021-02-26 14:24:15 +00004747
4748 java_sdk_library_import {
4749 name: "libbar",
4750 public: {
4751 jars: ["libbar.jar"],
4752 },
4753 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004754 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004755 }
4756
4757 java_sdk_library {
4758 name: "libbar",
4759 srcs: ["foo/bar/MyClass.java"],
4760 unsafe_ignore_missing_latest_api: true,
4761 apex_available: ["myapex"],
4762 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004763 `
4764
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004765 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004766 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4767 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004768
Paul Duffin537ea3d2021-05-14 10:38:00 +01004769 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin4fd997b2021-02-03 20:06:33 +00004770 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004771 out/soong/.intermediates/libbar/android_common_myapex/javac/libbar.jar
4772 out/soong/.intermediates/libfoo/android_common_apex10000/javac/libfoo.jar
4773 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004774 })
4775
4776 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4777 bp := `
4778 apex {
4779 name: "myapex",
4780 enabled: false,
4781 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01004782 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004783 }
4784
4785 apex_key {
4786 name: "myapex.key",
4787 public_key: "testkey.avbpubkey",
4788 private_key: "testkey.pem",
4789 }
4790
4791 prebuilt_apex {
4792 name: "myapex",
4793 arch: {
4794 arm64: {
4795 src: "myapex-arm64.apex",
4796 },
4797 arm: {
4798 src: "myapex-arm.apex",
4799 },
4800 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004801 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4802 }
4803
4804 prebuilt_bootclasspath_fragment {
4805 name: "my-bootclasspath-fragment",
4806 contents: ["libfoo", "libbar"],
4807 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004808 }
4809
4810 java_import {
4811 name: "libfoo",
4812 prefer: true,
4813 jars: ["libfoo.jar"],
4814 apex_available: ["myapex"],
4815 }
4816
4817 java_library {
4818 name: "libfoo",
4819 srcs: ["foo/bar/MyClass.java"],
4820 apex_available: ["myapex"],
4821 }
Paul Duffin37856732021-02-26 14:24:15 +00004822
4823 java_sdk_library_import {
4824 name: "libbar",
4825 prefer: true,
4826 public: {
4827 jars: ["libbar.jar"],
4828 },
4829 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004830 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004831 }
4832
4833 java_sdk_library {
4834 name: "libbar",
4835 srcs: ["foo/bar/MyClass.java"],
4836 unsafe_ignore_missing_latest_api: true,
4837 apex_available: ["myapex"],
4838 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004839 `
4840
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004841 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004842 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4843 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004844
Paul Duffin537ea3d2021-05-14 10:38:00 +01004845 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin9d67ca62021-02-03 20:06:33 +00004846 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004847 out/soong/.intermediates/prebuilt_libbar.stubs/android_common/combined/libbar.stubs.jar
4848 out/soong/.intermediates/prebuilt_libfoo/android_common_myapex/combined/libfoo.jar
4849 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004850 })
4851}
4852
Roland Levillain630846d2019-06-26 12:48:34 +01004853func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004854 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004855 apex_test {
4856 name: "myapex",
4857 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004858 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01004859 tests: [
4860 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004861 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004862 ],
4863 }
4864
4865 apex_key {
4866 name: "myapex.key",
4867 public_key: "testkey.avbpubkey",
4868 private_key: "testkey.pem",
4869 }
4870
Liz Kammer1c14a212020-05-12 15:26:55 -07004871 filegroup {
4872 name: "fg",
4873 srcs: [
4874 "baz",
4875 "bar/baz"
4876 ],
4877 }
4878
Roland Levillain630846d2019-06-26 12:48:34 +01004879 cc_test {
4880 name: "mytest",
4881 gtest: false,
4882 srcs: ["mytest.cpp"],
4883 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004884 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004885 system_shared_libs: [],
4886 static_executable: true,
4887 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004888 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004889 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004890
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004891 cc_library {
4892 name: "mylib",
4893 srcs: ["mylib.cpp"],
4894 system_shared_libs: [],
4895 stl: "none",
4896 }
4897
Liz Kammer5bd365f2020-05-27 15:15:11 -07004898 filegroup {
4899 name: "fg2",
4900 srcs: [
4901 "testdata/baz"
4902 ],
4903 }
4904
Roland Levillain9b5fde92019-06-28 15:41:19 +01004905 cc_test {
4906 name: "mytests",
4907 gtest: false,
4908 srcs: [
4909 "mytest1.cpp",
4910 "mytest2.cpp",
4911 "mytest3.cpp",
4912 ],
4913 test_per_src: true,
4914 relative_install_path: "test",
4915 system_shared_libs: [],
4916 static_executable: true,
4917 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004918 data: [
4919 ":fg",
4920 ":fg2",
4921 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004922 }
Roland Levillain630846d2019-06-26 12:48:34 +01004923 `)
4924
Sundong Ahnabb64432019-10-22 13:58:29 +09004925 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004926 copyCmds := apexRule.Args["copy_commands"]
4927
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004928 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004929 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004930 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004931
Liz Kammer1c14a212020-05-12 15:26:55 -07004932 //Ensure that test data are copied into apex.
4933 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4934 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4935
Roland Levillain9b5fde92019-06-28 15:41:19 +01004936 // Ensure that test deps built with `test_per_src` are copied into apex.
4937 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4938 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4939 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004940
4941 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004942 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004943 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004944 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004945 prefix := "TARGET_"
4946 var builder strings.Builder
4947 data.Custom(&builder, name, prefix, "", data)
4948 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004949 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4950 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4951 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4952 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004953 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004954 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004955 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004956
4957 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004958 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004959 data.Custom(&builder, name, prefix, "", data)
4960 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004961 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4962 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004963}
4964
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004965func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004966 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004967 apex {
4968 name: "myapex",
4969 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004970 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004971 }
4972 apex_key {
4973 name: "myapex.key",
4974 public_key: "testkey.avbpubkey",
4975 private_key: "testkey.pem",
4976 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00004977 `,
4978 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4979 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4980 }),
4981 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004982 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004983 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07004984 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004985 var builder strings.Builder
4986 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4987 androidMk := builder.String()
4988 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4989}
4990
Jooyung Hand48f3c32019-08-23 11:18:57 +09004991func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4992 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4993 apex {
4994 name: "myapex",
4995 key: "myapex.key",
4996 native_shared_libs: ["libfoo"],
4997 }
4998
4999 apex_key {
5000 name: "myapex.key",
5001 public_key: "testkey.avbpubkey",
5002 private_key: "testkey.pem",
5003 }
5004
5005 cc_library {
5006 name: "libfoo",
5007 stl: "none",
5008 system_shared_libs: [],
5009 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005010 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005011 }
5012 `)
5013 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5014 apex {
5015 name: "myapex",
5016 key: "myapex.key",
5017 java_libs: ["myjar"],
5018 }
5019
5020 apex_key {
5021 name: "myapex.key",
5022 public_key: "testkey.avbpubkey",
5023 private_key: "testkey.pem",
5024 }
5025
5026 java_library {
5027 name: "myjar",
5028 srcs: ["foo/bar/MyClass.java"],
5029 sdk_version: "none",
5030 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005031 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005032 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005033 }
5034 `)
5035}
5036
Bill Peckhama41a6962021-01-11 10:58:54 -08005037func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005038 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005039 apex {
5040 name: "myapex",
5041 key: "myapex.key",
5042 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005043 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005044 }
5045
5046 apex_key {
5047 name: "myapex.key",
5048 public_key: "testkey.avbpubkey",
5049 private_key: "testkey.pem",
5050 }
5051
5052 java_import {
5053 name: "myjavaimport",
5054 apex_available: ["myapex"],
5055 jars: ["my.jar"],
5056 compile_dex: true,
5057 }
5058 `)
5059
5060 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5061 apexRule := module.Rule("apexRule")
5062 copyCmds := apexRule.Args["copy_commands"]
5063 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5064}
5065
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005066func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005067 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005068 apex {
5069 name: "myapex",
5070 key: "myapex.key",
5071 apps: [
5072 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005073 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005074 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005075 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005076 }
5077
5078 apex_key {
5079 name: "myapex.key",
5080 public_key: "testkey.avbpubkey",
5081 private_key: "testkey.pem",
5082 }
5083
5084 android_app {
5085 name: "AppFoo",
5086 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005087 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005088 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005089 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005090 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005091 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005092 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005093
5094 android_app {
5095 name: "AppFooPriv",
5096 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005097 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005098 system_modules: "none",
5099 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005100 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005101 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005102 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005103
5104 cc_library_shared {
5105 name: "libjni",
5106 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005107 shared_libs: ["libfoo"],
5108 stl: "none",
5109 system_shared_libs: [],
5110 apex_available: [ "myapex" ],
5111 sdk_version: "current",
5112 }
5113
5114 cc_library_shared {
5115 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005116 stl: "none",
5117 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005118 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005119 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005120 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005121 `)
5122
Sundong Ahnabb64432019-10-22 13:58:29 +09005123 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005124 apexRule := module.Rule("apexRule")
5125 copyCmds := apexRule.Args["copy_commands"]
5126
5127 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005128 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005129
Colin Crossaede88c2020-08-11 12:17:01 -07005130 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005131 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005132 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005133 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005134 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005135 // JNI libraries including transitive deps are
5136 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005137 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005138 // ... embedded inside APK (jnilibs.zip)
5139 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5140 // ... and not directly inside the APEX
5141 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5142 }
Dario Frenicde2a032019-10-27 00:29:22 +01005143}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005144
Dario Frenicde2a032019-10-27 00:29:22 +01005145func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005146 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005147 apex {
5148 name: "myapex",
5149 key: "myapex.key",
5150 apps: [
5151 "AppFooPrebuilt",
5152 "AppFooPrivPrebuilt",
5153 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005154 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005155 }
5156
5157 apex_key {
5158 name: "myapex.key",
5159 public_key: "testkey.avbpubkey",
5160 private_key: "testkey.pem",
5161 }
5162
5163 android_app_import {
5164 name: "AppFooPrebuilt",
5165 apk: "PrebuiltAppFoo.apk",
5166 presigned: true,
5167 dex_preopt: {
5168 enabled: false,
5169 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005170 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005171 }
5172
5173 android_app_import {
5174 name: "AppFooPrivPrebuilt",
5175 apk: "PrebuiltAppFooPriv.apk",
5176 privileged: true,
5177 presigned: true,
5178 dex_preopt: {
5179 enabled: false,
5180 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005181 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005182 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005183 }
5184 `)
5185
Sundong Ahnabb64432019-10-22 13:58:29 +09005186 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005187 apexRule := module.Rule("apexRule")
5188 copyCmds := apexRule.Args["copy_commands"]
5189
5190 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005191 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5192}
5193
5194func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005195 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005196 apex {
5197 name: "myapex",
5198 key: "myapex.key",
5199 apps: [
5200 "AppFoo",
5201 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005202 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005203 }
5204
5205 apex_key {
5206 name: "myapex.key",
5207 public_key: "testkey.avbpubkey",
5208 private_key: "testkey.pem",
5209 }
5210
5211 android_app {
5212 name: "AppFoo",
5213 srcs: ["foo/bar/MyClass.java"],
5214 sdk_version: "none",
5215 system_modules: "none",
5216 apex_available: [ "myapex" ],
5217 }
5218
5219 android_app_import {
5220 name: "AppFoo",
5221 apk: "AppFooPrebuilt.apk",
5222 filename: "AppFooPrebuilt.apk",
5223 presigned: true,
5224 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005225 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005226 }
5227 `, withFiles(map[string][]byte{
5228 "AppFooPrebuilt.apk": nil,
5229 }))
5230
5231 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005232 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005233 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005234}
5235
Dario Freni6f3937c2019-12-20 22:58:03 +00005236func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005237 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005238 apex {
5239 name: "myapex",
5240 key: "myapex.key",
5241 apps: [
5242 "TesterHelpAppFoo",
5243 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005244 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005245 }
5246
5247 apex_key {
5248 name: "myapex.key",
5249 public_key: "testkey.avbpubkey",
5250 private_key: "testkey.pem",
5251 }
5252
5253 android_test_helper_app {
5254 name: "TesterHelpAppFoo",
5255 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005256 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005257 }
5258
5259 `)
5260
5261 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5262 apexRule := module.Rule("apexRule")
5263 copyCmds := apexRule.Args["copy_commands"]
5264
5265 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5266}
5267
Jooyung Han18020ea2019-11-13 10:50:48 +09005268func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5269 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005270 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005271 apex {
5272 name: "myapex",
5273 key: "myapex.key",
5274 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005275 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005276 }
5277
5278 apex_key {
5279 name: "myapex.key",
5280 public_key: "testkey.avbpubkey",
5281 private_key: "testkey.pem",
5282 }
5283
5284 apex {
5285 name: "otherapex",
5286 key: "myapex.key",
5287 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005288 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005289 }
5290
5291 cc_defaults {
5292 name: "libfoo-defaults",
5293 apex_available: ["otherapex"],
5294 }
5295
5296 cc_library {
5297 name: "libfoo",
5298 defaults: ["libfoo-defaults"],
5299 stl: "none",
5300 system_shared_libs: [],
5301 }`)
5302}
5303
Paul Duffine52e66f2020-03-30 17:54:29 +01005304func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005305 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005306 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005307 apex {
5308 name: "myapex",
5309 key: "myapex.key",
5310 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005311 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005312 }
5313
5314 apex_key {
5315 name: "myapex.key",
5316 public_key: "testkey.avbpubkey",
5317 private_key: "testkey.pem",
5318 }
5319
5320 apex {
5321 name: "otherapex",
5322 key: "otherapex.key",
5323 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005324 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005325 }
5326
5327 apex_key {
5328 name: "otherapex.key",
5329 public_key: "testkey.avbpubkey",
5330 private_key: "testkey.pem",
5331 }
5332
5333 cc_library {
5334 name: "libfoo",
5335 stl: "none",
5336 system_shared_libs: [],
5337 apex_available: ["otherapex"],
5338 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005339}
Jiyong Park127b40b2019-09-30 16:04:35 +09005340
Paul Duffine52e66f2020-03-30 17:54:29 +01005341func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005342 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005343 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005344.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005345.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005346.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005347.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005348.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005349.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005350 apex {
5351 name: "myapex",
5352 key: "myapex.key",
5353 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005354 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005355 }
5356
5357 apex_key {
5358 name: "myapex.key",
5359 public_key: "testkey.avbpubkey",
5360 private_key: "testkey.pem",
5361 }
5362
Jiyong Park127b40b2019-09-30 16:04:35 +09005363 cc_library {
5364 name: "libfoo",
5365 stl: "none",
5366 shared_libs: ["libbar"],
5367 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005368 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005369 }
5370
5371 cc_library {
5372 name: "libbar",
5373 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005374 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005375 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005376 apex_available: ["myapex"],
5377 }
5378
5379 cc_library {
5380 name: "libbaz",
5381 stl: "none",
5382 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005383 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005384}
Jiyong Park127b40b2019-09-30 16:04:35 +09005385
Paul Duffine52e66f2020-03-30 17:54:29 +01005386func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005387 testApexError(t, "\"otherapex\" is not a valid module name", `
5388 apex {
5389 name: "myapex",
5390 key: "myapex.key",
5391 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005392 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005393 }
5394
5395 apex_key {
5396 name: "myapex.key",
5397 public_key: "testkey.avbpubkey",
5398 private_key: "testkey.pem",
5399 }
5400
5401 cc_library {
5402 name: "libfoo",
5403 stl: "none",
5404 system_shared_libs: [],
5405 apex_available: ["otherapex"],
5406 }`)
5407
Paul Duffine52e66f2020-03-30 17:54:29 +01005408 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005409 apex {
5410 name: "myapex",
5411 key: "myapex.key",
5412 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005413 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005414 }
5415
5416 apex_key {
5417 name: "myapex.key",
5418 public_key: "testkey.avbpubkey",
5419 private_key: "testkey.pem",
5420 }
5421
5422 cc_library {
5423 name: "libfoo",
5424 stl: "none",
5425 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005426 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005427 apex_available: ["myapex"],
5428 }
5429
5430 cc_library {
5431 name: "libbar",
5432 stl: "none",
5433 system_shared_libs: [],
5434 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005435 }
5436
5437 cc_library {
5438 name: "libbaz",
5439 stl: "none",
5440 system_shared_libs: [],
5441 stubs: {
5442 versions: ["10", "20", "30"],
5443 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005444 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005445}
Jiyong Park127b40b2019-09-30 16:04:35 +09005446
Jiyong Park89e850a2020-04-07 16:37:39 +09005447func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005448 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005449 apex {
5450 name: "myapex",
5451 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005452 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005453 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005454 }
5455
5456 apex_key {
5457 name: "myapex.key",
5458 public_key: "testkey.avbpubkey",
5459 private_key: "testkey.pem",
5460 }
5461
5462 cc_library {
5463 name: "libfoo",
5464 stl: "none",
5465 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005466 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005467 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005468 }
5469
5470 cc_library {
5471 name: "libfoo2",
5472 stl: "none",
5473 system_shared_libs: [],
5474 shared_libs: ["libbaz"],
5475 apex_available: ["//apex_available:platform"],
5476 }
5477
5478 cc_library {
5479 name: "libbar",
5480 stl: "none",
5481 system_shared_libs: [],
5482 apex_available: ["myapex"],
5483 }
5484
5485 cc_library {
5486 name: "libbaz",
5487 stl: "none",
5488 system_shared_libs: [],
5489 apex_available: ["myapex"],
5490 stubs: {
5491 versions: ["1"],
5492 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005493 }`)
5494
Jiyong Park89e850a2020-04-07 16:37:39 +09005495 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5496 // because it depends on libbar which isn't available to platform
5497 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5498 if libfoo.NotAvailableForPlatform() != true {
5499 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5500 }
5501
5502 // libfoo2 however can be available to platform because it depends on libbaz which provides
5503 // stubs
5504 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5505 if libfoo2.NotAvailableForPlatform() == true {
5506 t.Errorf("%q should be available to platform", libfoo2.String())
5507 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005508}
Jiyong Parka90ca002019-10-07 15:47:24 +09005509
Paul Duffine52e66f2020-03-30 17:54:29 +01005510func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005511 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005512 apex {
5513 name: "myapex",
5514 key: "myapex.key",
5515 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005516 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005517 }
5518
5519 apex_key {
5520 name: "myapex.key",
5521 public_key: "testkey.avbpubkey",
5522 private_key: "testkey.pem",
5523 }
5524
5525 cc_library {
5526 name: "libfoo",
5527 stl: "none",
5528 system_shared_libs: [],
5529 apex_available: ["myapex"],
5530 static: {
5531 apex_available: ["//apex_available:platform"],
5532 },
5533 }`)
5534
Jiyong Park89e850a2020-04-07 16:37:39 +09005535 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5536 if libfooShared.NotAvailableForPlatform() != true {
5537 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5538 }
5539 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5540 if libfooStatic.NotAvailableForPlatform() != false {
5541 t.Errorf("%q should be available to platform", libfooStatic.String())
5542 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005543}
5544
Jiyong Park5d790c32019-11-15 18:40:32 +09005545func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005546 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005547 apex {
5548 name: "myapex",
5549 key: "myapex.key",
5550 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005551 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005552 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005553 }
5554
5555 override_apex {
5556 name: "override_myapex",
5557 base: "myapex",
5558 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005559 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005560 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005561 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07005562 key: "mynewapex.key",
5563 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09005564 }
5565
5566 apex_key {
5567 name: "myapex.key",
5568 public_key: "testkey.avbpubkey",
5569 private_key: "testkey.pem",
5570 }
5571
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07005572 apex_key {
5573 name: "mynewapex.key",
5574 public_key: "testkey2.avbpubkey",
5575 private_key: "testkey2.pem",
5576 }
5577
5578 android_app_certificate {
5579 name: "myapex.certificate",
5580 certificate: "testkey",
5581 }
5582
Jiyong Park5d790c32019-11-15 18:40:32 +09005583 android_app {
5584 name: "app",
5585 srcs: ["foo/bar/MyClass.java"],
5586 package_name: "foo",
5587 sdk_version: "none",
5588 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005589 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005590 }
5591
5592 override_android_app {
5593 name: "override_app",
5594 base: "app",
5595 package_name: "bar",
5596 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005597 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005598
Jiyong Park317645e2019-12-05 13:20:58 +09005599 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5600 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5601 if originalVariant.GetOverriddenBy() != "" {
5602 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5603 }
5604 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5605 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5606 }
5607
Jiyong Park5d790c32019-11-15 18:40:32 +09005608 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5609 apexRule := module.Rule("apexRule")
5610 copyCmds := apexRule.Args["copy_commands"]
5611
5612 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005613 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005614
5615 apexBundle := module.Module().(*apexBundle)
5616 name := apexBundle.Name()
5617 if name != "override_myapex" {
5618 t.Errorf("name should be \"override_myapex\", but was %q", name)
5619 }
5620
Baligh Uddin004d7172020-02-19 21:29:28 -08005621 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5622 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5623 }
5624
Jiyong Park20bacab2020-03-03 11:45:41 +09005625 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005626 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07005627 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
5628
5629 signApkRule := module.Rule("signapk")
5630 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09005631
Colin Crossaa255532020-07-03 13:18:24 -07005632 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005633 var builder strings.Builder
5634 data.Custom(&builder, name, "TARGET_", "", data)
5635 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005636 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005637 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5638 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005639 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005640 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005641 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005642 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5643 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005644}
5645
Jooyung Han214bf372019-11-12 13:03:50 +09005646func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005647 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005648 apex {
5649 name: "myapex",
5650 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005651 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005652 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005653 }
5654
5655 apex_key {
5656 name: "myapex.key",
5657 public_key: "testkey.avbpubkey",
5658 private_key: "testkey.pem",
5659 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005660
5661 cc_library {
5662 name: "mylib",
5663 srcs: ["mylib.cpp"],
5664 stl: "libc++",
5665 system_shared_libs: [],
5666 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005667 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005668 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005669 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005670
5671 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5672 args := module.Rule("apexRule").Args
5673 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005674 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005675
5676 // The copies of the libraries in the apex should have one more dependency than
5677 // the ones outside the apex, namely the unwinder. Ideally we should check
5678 // the dependency names directly here but for some reason the names are blank in
5679 // this test.
5680 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005681 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005682 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5683 if len(apexImplicits) != len(nonApexImplicits)+1 {
5684 t.Errorf("%q missing unwinder dep", lib)
5685 }
5686 }
Jooyung Han214bf372019-11-12 13:03:50 +09005687}
5688
Paul Duffine05480a2021-03-08 15:07:14 +00005689var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01005690 "api/current.txt": nil,
5691 "api/removed.txt": nil,
5692 "api/system-current.txt": nil,
5693 "api/system-removed.txt": nil,
5694 "api/test-current.txt": nil,
5695 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005696
Anton Hanssondff2c782020-12-21 17:10:01 +00005697 "100/public/api/foo.txt": nil,
5698 "100/public/api/foo-removed.txt": nil,
5699 "100/system/api/foo.txt": nil,
5700 "100/system/api/foo-removed.txt": nil,
5701
Paul Duffineedc5d52020-06-12 17:46:39 +01005702 // For java_sdk_library_import
5703 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005704}
5705
Jooyung Han58f26ab2019-12-18 15:34:32 +09005706func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005707 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09005708 apex {
5709 name: "myapex",
5710 key: "myapex.key",
5711 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005712 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09005713 }
5714
5715 apex_key {
5716 name: "myapex.key",
5717 public_key: "testkey.avbpubkey",
5718 private_key: "testkey.pem",
5719 }
5720
5721 java_sdk_library {
5722 name: "foo",
5723 srcs: ["a.java"],
5724 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005725 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005726 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005727
5728 prebuilt_apis {
5729 name: "sdk",
5730 api_dirs: ["100"],
5731 }
Paul Duffin9b879592020-05-26 13:21:35 +01005732 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005733
5734 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005735 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005736 "javalib/foo.jar",
5737 "etc/permissions/foo.xml",
5738 })
5739 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005740 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5741 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005742}
5743
Paul Duffin9b879592020-05-26 13:21:35 +01005744func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005745 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005746 apex {
5747 name: "myapex",
5748 key: "myapex.key",
5749 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005750 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005751 }
5752
5753 apex_key {
5754 name: "myapex.key",
5755 public_key: "testkey.avbpubkey",
5756 private_key: "testkey.pem",
5757 }
5758
5759 java_sdk_library {
5760 name: "foo",
5761 srcs: ["a.java"],
5762 api_packages: ["foo"],
5763 apex_available: ["myapex"],
5764 sdk_version: "none",
5765 system_modules: "none",
5766 }
5767
5768 java_library {
5769 name: "bar",
5770 srcs: ["a.java"],
5771 libs: ["foo"],
5772 apex_available: ["myapex"],
5773 sdk_version: "none",
5774 system_modules: "none",
5775 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005776
5777 prebuilt_apis {
5778 name: "sdk",
5779 api_dirs: ["100"],
5780 }
Paul Duffin9b879592020-05-26 13:21:35 +01005781 `, withFiles(filesForSdkLibrary))
5782
5783 // java_sdk_library installs both impl jar and permission XML
5784 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5785 "javalib/bar.jar",
5786 "javalib/foo.jar",
5787 "etc/permissions/foo.xml",
5788 })
5789
5790 // The bar library should depend on the implementation jar.
5791 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005792 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005793 t.Errorf("expected %q, found %#q", expected, actual)
5794 }
5795}
5796
5797func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005798 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005799 apex {
5800 name: "myapex",
5801 key: "myapex.key",
5802 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005803 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005804 }
5805
5806 apex_key {
5807 name: "myapex.key",
5808 public_key: "testkey.avbpubkey",
5809 private_key: "testkey.pem",
5810 }
5811
5812 java_sdk_library {
5813 name: "foo",
5814 srcs: ["a.java"],
5815 api_packages: ["foo"],
5816 apex_available: ["myapex"],
5817 sdk_version: "none",
5818 system_modules: "none",
5819 }
5820
5821 java_library {
5822 name: "bar",
5823 srcs: ["a.java"],
5824 libs: ["foo"],
5825 sdk_version: "none",
5826 system_modules: "none",
5827 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005828
5829 prebuilt_apis {
5830 name: "sdk",
5831 api_dirs: ["100"],
5832 }
Paul Duffin9b879592020-05-26 13:21:35 +01005833 `, withFiles(filesForSdkLibrary))
5834
5835 // java_sdk_library installs both impl jar and permission XML
5836 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5837 "javalib/foo.jar",
5838 "etc/permissions/foo.xml",
5839 })
5840
5841 // The bar library should depend on the stubs jar.
5842 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005843 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005844 t.Errorf("expected %q, found %#q", expected, actual)
5845 }
5846}
5847
Paul Duffineedc5d52020-06-12 17:46:39 +01005848func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005849 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00005850 prebuilt_apis {
5851 name: "sdk",
5852 api_dirs: ["100"],
5853 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005854 withFiles(map[string][]byte{
5855 "apex/a.java": nil,
5856 "apex/apex_manifest.json": nil,
5857 "apex/Android.bp": []byte(`
5858 package {
5859 default_visibility: ["//visibility:private"],
5860 }
5861
5862 apex {
5863 name: "myapex",
5864 key: "myapex.key",
5865 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005866 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005867 }
5868
5869 apex_key {
5870 name: "myapex.key",
5871 public_key: "testkey.avbpubkey",
5872 private_key: "testkey.pem",
5873 }
5874
5875 java_library {
5876 name: "bar",
5877 srcs: ["a.java"],
5878 libs: ["foo"],
5879 apex_available: ["myapex"],
5880 sdk_version: "none",
5881 system_modules: "none",
5882 }
5883`),
5884 "source/a.java": nil,
5885 "source/api/current.txt": nil,
5886 "source/api/removed.txt": nil,
5887 "source/Android.bp": []byte(`
5888 package {
5889 default_visibility: ["//visibility:private"],
5890 }
5891
5892 java_sdk_library {
5893 name: "foo",
5894 visibility: ["//apex"],
5895 srcs: ["a.java"],
5896 api_packages: ["foo"],
5897 apex_available: ["myapex"],
5898 sdk_version: "none",
5899 system_modules: "none",
5900 public: {
5901 enabled: true,
5902 },
5903 }
5904`),
5905 "prebuilt/a.jar": nil,
5906 "prebuilt/Android.bp": []byte(`
5907 package {
5908 default_visibility: ["//visibility:private"],
5909 }
5910
5911 java_sdk_library_import {
5912 name: "foo",
5913 visibility: ["//apex", "//source"],
5914 apex_available: ["myapex"],
5915 prefer: true,
5916 public: {
5917 jars: ["a.jar"],
5918 },
5919 }
5920`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005921 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005922 )
5923
5924 // java_sdk_library installs both impl jar and permission XML
5925 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5926 "javalib/bar.jar",
5927 "javalib/foo.jar",
5928 "etc/permissions/foo.xml",
5929 })
5930
5931 // The bar library should depend on the implementation jar.
5932 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005933 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01005934 t.Errorf("expected %q, found %#q", expected, actual)
5935 }
5936}
5937
5938func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5939 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5940 apex {
5941 name: "myapex",
5942 key: "myapex.key",
5943 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005944 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005945 }
5946
5947 apex_key {
5948 name: "myapex.key",
5949 public_key: "testkey.avbpubkey",
5950 private_key: "testkey.pem",
5951 }
5952
5953 java_sdk_library_import {
5954 name: "foo",
5955 apex_available: ["myapex"],
5956 prefer: true,
5957 public: {
5958 jars: ["a.jar"],
5959 },
5960 }
5961
5962 `, withFiles(filesForSdkLibrary))
5963}
5964
atrost6e126252020-01-27 17:01:16 +00005965func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01005966 result := android.GroupFixturePreparers(
5967 prepareForApexTest,
5968 java.PrepareForTestWithPlatformCompatConfig,
5969 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00005970 apex {
5971 name: "myapex",
5972 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00005973 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00005974 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005975 updatable: false,
atrost6e126252020-01-27 17:01:16 +00005976 }
5977
5978 apex_key {
5979 name: "myapex.key",
5980 public_key: "testkey.avbpubkey",
5981 private_key: "testkey.pem",
5982 }
5983
5984 platform_compat_config {
5985 name: "myjar-platform-compat-config",
5986 src: ":myjar",
5987 }
5988
5989 java_library {
5990 name: "myjar",
5991 srcs: ["foo/bar/MyClass.java"],
5992 sdk_version: "none",
5993 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005994 apex_available: [ "myapex" ],
5995 }
Paul Duffin1b29e002021-03-16 15:06:54 +00005996
5997 // Make sure that a preferred prebuilt does not affect the apex contents.
5998 prebuilt_platform_compat_config {
5999 name: "myjar-platform-compat-config",
6000 metadata: "compat-config/metadata.xml",
6001 prefer: true,
6002 }
atrost6e126252020-01-27 17:01:16 +00006003 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00006004 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00006005 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6006 "etc/compatconfig/myjar-platform-compat-config.xml",
6007 "javalib/myjar.jar",
6008 })
6009}
6010
Jiyong Park479321d2019-12-16 11:47:12 +09006011func TestRejectNonInstallableJavaLibrary(t *testing.T) {
6012 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
6013 apex {
6014 name: "myapex",
6015 key: "myapex.key",
6016 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006017 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09006018 }
6019
6020 apex_key {
6021 name: "myapex.key",
6022 public_key: "testkey.avbpubkey",
6023 private_key: "testkey.pem",
6024 }
6025
6026 java_library {
6027 name: "myjar",
6028 srcs: ["foo/bar/MyClass.java"],
6029 sdk_version: "none",
6030 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09006031 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006032 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09006033 }
6034 `)
6035}
6036
Jiyong Park7afd1072019-12-30 16:56:33 +09006037func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006038 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09006039 apex {
6040 name: "myapex",
6041 key: "myapex.key",
6042 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006043 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09006044 }
6045
6046 apex_key {
6047 name: "myapex.key",
6048 public_key: "testkey.avbpubkey",
6049 private_key: "testkey.pem",
6050 }
6051
6052 cc_library {
6053 name: "mylib",
6054 srcs: ["mylib.cpp"],
6055 system_shared_libs: [],
6056 stl: "none",
6057 required: ["a", "b"],
6058 host_required: ["c", "d"],
6059 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006060 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006061 }
6062 `)
6063
6064 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006065 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006066 name := apexBundle.BaseModuleName()
6067 prefix := "TARGET_"
6068 var builder strings.Builder
6069 data.Custom(&builder, name, prefix, "", data)
6070 androidMk := builder.String()
6071 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6072 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6073 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6074}
6075
Jiyong Park7cd10e32020-01-14 09:22:18 +09006076func TestSymlinksFromApexToSystem(t *testing.T) {
6077 bp := `
6078 apex {
6079 name: "myapex",
6080 key: "myapex.key",
6081 native_shared_libs: ["mylib"],
6082 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006083 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006084 }
6085
Jiyong Park9d677202020-02-19 16:29:35 +09006086 apex {
6087 name: "myapex.updatable",
6088 key: "myapex.key",
6089 native_shared_libs: ["mylib"],
6090 java_libs: ["myjar"],
6091 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006092 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006093 }
6094
Jiyong Park7cd10e32020-01-14 09:22:18 +09006095 apex_key {
6096 name: "myapex.key",
6097 public_key: "testkey.avbpubkey",
6098 private_key: "testkey.pem",
6099 }
6100
6101 cc_library {
6102 name: "mylib",
6103 srcs: ["mylib.cpp"],
6104 shared_libs: ["myotherlib"],
6105 system_shared_libs: [],
6106 stl: "none",
6107 apex_available: [
6108 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006109 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006110 "//apex_available:platform",
6111 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006112 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006113 }
6114
6115 cc_library {
6116 name: "myotherlib",
6117 srcs: ["mylib.cpp"],
6118 system_shared_libs: [],
6119 stl: "none",
6120 apex_available: [
6121 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006122 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006123 "//apex_available:platform",
6124 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006125 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006126 }
6127
6128 java_library {
6129 name: "myjar",
6130 srcs: ["foo/bar/MyClass.java"],
6131 sdk_version: "none",
6132 system_modules: "none",
6133 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006134 apex_available: [
6135 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006136 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006137 "//apex_available:platform",
6138 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006139 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006140 }
6141
6142 java_library {
6143 name: "myotherjar",
6144 srcs: ["foo/bar/MyClass.java"],
6145 sdk_version: "none",
6146 system_modules: "none",
6147 apex_available: [
6148 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006149 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006150 "//apex_available:platform",
6151 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006152 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006153 }
6154 `
6155
6156 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6157 for _, f := range files {
6158 if f.path == file {
6159 if f.isLink {
6160 t.Errorf("%q is not a real file", file)
6161 }
6162 return
6163 }
6164 }
6165 t.Errorf("%q is not found", file)
6166 }
6167
6168 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6169 for _, f := range files {
6170 if f.path == file {
6171 if !f.isLink {
6172 t.Errorf("%q is not a symlink", file)
6173 }
6174 return
6175 }
6176 }
6177 t.Errorf("%q is not found", file)
6178 }
6179
Jiyong Park9d677202020-02-19 16:29:35 +09006180 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6181 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006182 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006183 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006184 ensureRealfileExists(t, files, "javalib/myjar.jar")
6185 ensureRealfileExists(t, files, "lib64/mylib.so")
6186 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6187
Jiyong Park9d677202020-02-19 16:29:35 +09006188 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6189 ensureRealfileExists(t, files, "javalib/myjar.jar")
6190 ensureRealfileExists(t, files, "lib64/mylib.so")
6191 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6192
6193 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006194 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006195 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006196 ensureRealfileExists(t, files, "javalib/myjar.jar")
6197 ensureRealfileExists(t, files, "lib64/mylib.so")
6198 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006199
6200 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6201 ensureRealfileExists(t, files, "javalib/myjar.jar")
6202 ensureRealfileExists(t, files, "lib64/mylib.so")
6203 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006204}
6205
Yo Chiange8128052020-07-23 20:09:18 +08006206func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006207 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006208 apex {
6209 name: "myapex",
6210 key: "myapex.key",
6211 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006212 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006213 }
6214
6215 apex_key {
6216 name: "myapex.key",
6217 public_key: "testkey.avbpubkey",
6218 private_key: "testkey.pem",
6219 }
6220
6221 cc_library_shared {
6222 name: "mylib",
6223 srcs: ["mylib.cpp"],
6224 shared_libs: ["myotherlib"],
6225 system_shared_libs: [],
6226 stl: "none",
6227 apex_available: [
6228 "myapex",
6229 "//apex_available:platform",
6230 ],
6231 }
6232
6233 cc_prebuilt_library_shared {
6234 name: "myotherlib",
6235 srcs: ["prebuilt.so"],
6236 system_shared_libs: [],
6237 stl: "none",
6238 apex_available: [
6239 "myapex",
6240 "//apex_available:platform",
6241 ],
6242 }
6243 `)
6244
6245 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006246 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006247 var builder strings.Builder
6248 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6249 androidMk := builder.String()
6250 // `myotherlib` is added to `myapex` as symlink
6251 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6252 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6253 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6254 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006255 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 +08006256}
6257
Jooyung Han643adc42020-02-27 13:50:06 +09006258func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006259 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006260 apex {
6261 name: "myapex",
6262 key: "myapex.key",
6263 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006264 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006265 }
6266
6267 apex_key {
6268 name: "myapex.key",
6269 public_key: "testkey.avbpubkey",
6270 private_key: "testkey.pem",
6271 }
6272
6273 cc_library {
6274 name: "mylib",
6275 srcs: ["mylib.cpp"],
6276 shared_libs: ["mylib2"],
6277 system_shared_libs: [],
6278 stl: "none",
6279 apex_available: [ "myapex" ],
6280 }
6281
6282 cc_library {
6283 name: "mylib2",
6284 srcs: ["mylib.cpp"],
6285 system_shared_libs: [],
6286 stl: "none",
6287 apex_available: [ "myapex" ],
6288 }
6289 `)
6290
6291 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6292 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6293 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6294 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6295 "lib64/mylib.so",
6296 "lib64/mylib2.so",
6297 })
6298}
6299
Jooyung Han49f67012020-04-17 13:43:10 +09006300func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006301 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006302 apex {
6303 name: "myapex",
6304 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006305 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006306 }
6307 apex_key {
6308 name: "myapex.key",
6309 public_key: "testkey.avbpubkey",
6310 private_key: "testkey.pem",
6311 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006312 `,
6313 android.FixtureModifyConfig(func(config android.Config) {
6314 delete(config.Targets, android.Android)
6315 config.AndroidCommonTarget = android.Target{}
6316 }),
6317 )
Jooyung Han49f67012020-04-17 13:43:10 +09006318
6319 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6320 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6321 }
6322}
6323
Jiyong Parkbd159612020-02-28 15:22:21 +09006324func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006325 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006326 apex {
6327 name: "myapex",
6328 key: "myapex.key",
6329 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006330 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006331 }
6332
6333 apex_key {
6334 name: "myapex.key",
6335 public_key: "testkey.avbpubkey",
6336 private_key: "testkey.pem",
6337 }
6338
6339 android_app {
6340 name: "AppFoo",
6341 srcs: ["foo/bar/MyClass.java"],
6342 sdk_version: "none",
6343 system_modules: "none",
6344 apex_available: [ "myapex" ],
6345 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006346 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006347
Colin Crosscf371cc2020-11-13 11:48:42 -08006348 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006349 content := bundleConfigRule.Args["content"]
6350
6351 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006352 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 +09006353}
6354
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006355func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006356 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006357 apex {
6358 name: "myapex",
6359 key: "myapex.key",
6360 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006361 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006362 }
6363
6364 apex_key {
6365 name: "myapex.key",
6366 public_key: "testkey.avbpubkey",
6367 private_key: "testkey.pem",
6368 }
6369
6370 android_app_set {
6371 name: "AppSet",
6372 set: "AppSet.apks",
6373 }`)
6374 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006375 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006376 content := bundleConfigRule.Args["content"]
6377 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6378 s := mod.Rule("apexRule").Args["copy_commands"]
6379 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6380 if len(copyCmds) != 3 {
6381 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6382 }
6383 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6384 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6385 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6386}
6387
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006388func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01006389 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006390 apex_set {
6391 name: "myapex",
6392 filename: "foo_v2.apex",
6393 sanitized: {
6394 none: { set: "myapex.apks", },
6395 hwaddress: { set: "myapex.hwasan.apks", },
6396 },
Paul Duffin24704672021-04-06 16:09:30 +01006397 }
6398 `
6399 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006400
Paul Duffin24704672021-04-06 16:09:30 +01006401 // Check that the extractor produces the correct output file from the correct input file.
6402 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006403
Paul Duffin24704672021-04-06 16:09:30 +01006404 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
6405 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006406
Paul Duffin24704672021-04-06 16:09:30 +01006407 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
6408
6409 // Ditto for the apex.
Paul Duffin5ec165d2021-06-15 19:09:41 +01006410 m = ctx.ModuleForTests("myapex", "android_common_myapex")
6411 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01006412
6413 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006414}
6415
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006416func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006417 t.Helper()
6418
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006419 bp := `
6420 java_library {
6421 name: "some-updatable-apex-lib",
6422 srcs: ["a.java"],
6423 sdk_version: "current",
6424 apex_available: [
6425 "some-updatable-apex",
6426 ],
6427 }
6428
6429 java_library {
6430 name: "some-non-updatable-apex-lib",
6431 srcs: ["a.java"],
6432 apex_available: [
6433 "some-non-updatable-apex",
6434 ],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006435 compile_dex: true,
6436 }
6437
6438 bootclasspath_fragment {
6439 name: "some-non-updatable-fragment",
6440 contents: ["some-non-updatable-apex-lib"],
6441 apex_available: [
6442 "some-non-updatable-apex",
6443 ],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006444 }
6445
6446 java_library {
6447 name: "some-platform-lib",
6448 srcs: ["a.java"],
6449 sdk_version: "current",
6450 installable: true,
6451 }
6452
6453 java_library {
6454 name: "some-art-lib",
6455 srcs: ["a.java"],
6456 sdk_version: "current",
6457 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006458 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006459 ],
6460 hostdex: true,
Paul Duffinc75bbce2021-06-07 13:28:19 +01006461 compile_dex: true,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006462 }
6463
6464 apex {
6465 name: "some-updatable-apex",
6466 key: "some-updatable-apex.key",
6467 java_libs: ["some-updatable-apex-lib"],
6468 updatable: true,
6469 min_sdk_version: "current",
6470 }
6471
6472 apex {
6473 name: "some-non-updatable-apex",
6474 key: "some-non-updatable-apex.key",
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006475 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006476 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006477 }
6478
6479 apex_key {
6480 name: "some-updatable-apex.key",
6481 }
6482
6483 apex_key {
6484 name: "some-non-updatable-apex.key",
6485 }
6486
6487 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006488 name: "com.android.art.debug",
6489 key: "com.android.art.debug.key",
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006490 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006491 updatable: true,
6492 min_sdk_version: "current",
6493 }
6494
Paul Duffinf23bc472021-04-27 12:42:20 +01006495 bootclasspath_fragment {
6496 name: "art-bootclasspath-fragment",
6497 image_name: "art",
6498 contents: ["some-art-lib"],
6499 apex_available: [
6500 "com.android.art.debug",
6501 ],
6502 }
6503
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006504 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006505 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006506 }
6507
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006508 filegroup {
6509 name: "some-updatable-apex-file_contexts",
6510 srcs: [
6511 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6512 ],
6513 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006514
6515 filegroup {
6516 name: "some-non-updatable-apex-file_contexts",
6517 srcs: [
6518 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6519 ],
6520 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006521 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006522
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006523 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00006524}
6525
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006526func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006527 t.Helper()
6528
Paul Duffin55607122021-03-30 23:32:51 +01006529 fs := android.MockFS{
6530 "a.java": nil,
6531 "a.jar": nil,
6532 "apex_manifest.json": nil,
6533 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006534 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006535 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6536 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6537 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006538 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006539 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006540
Paul Duffin55607122021-03-30 23:32:51 +01006541 errorHandler := android.FixtureExpectsNoErrors
6542 if errmsg != "" {
6543 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006544 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006545
Paul Duffin55607122021-03-30 23:32:51 +01006546 result := android.GroupFixturePreparers(
6547 cc.PrepareForTestWithCcDefaultModules,
6548 java.PrepareForTestWithHiddenApiBuildComponents,
6549 java.PrepareForTestWithJavaDefaultModules,
6550 java.PrepareForTestWithJavaSdkLibraryFiles,
6551 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01006552 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01006553 fs.AddToFixture(),
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006554 android.FixtureModifyMockFS(func(fs android.MockFS) {
6555 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
6556 insert := ""
6557 for _, fragment := range fragments {
6558 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
6559 }
6560 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
6561 platform_bootclasspath {
6562 name: "platform-bootclasspath",
6563 fragments: [
6564 %s
6565 ],
6566 }
6567 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01006568 }
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006569 }),
Paul Duffin55607122021-03-30 23:32:51 +01006570 ).
6571 ExtendWithErrorHandler(errorHandler).
6572 RunTestWithBp(t, bp)
6573
6574 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006575}
6576
Martin Stjernholm95994062021-06-30 16:35:07 +01006577func TestDuplicateDeapexeresFromPrebuiltApexes(t *testing.T) {
6578 preparers := android.GroupFixturePreparers(
6579 java.PrepareForTestWithJavaDefaultModules,
6580 PrepareForTestWithApexBuildComponents,
6581 ).
6582 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
6583 `Ambiguous duplicate deapexer module dependencies "com.android.myapex.deapexer" and "com.mycompany.android.myapex.deapexer"`))
6584
6585 bpBase := `
6586 apex_set {
6587 name: "com.android.myapex",
6588 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
6589 set: "myapex.apks",
6590 }
6591
6592 apex_set {
6593 name: "com.mycompany.android.myapex",
6594 apex_name: "com.android.myapex",
6595 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
6596 set: "company-myapex.apks",
6597 }
6598
6599 prebuilt_bootclasspath_fragment {
6600 name: "my-bootclasspath-fragment",
6601 apex_available: ["com.android.myapex"],
6602 %s
6603 }
6604 `
6605
6606 t.Run("java_import", func(t *testing.T) {
6607 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
6608 java_import {
6609 name: "libfoo",
6610 jars: ["libfoo.jar"],
6611 apex_available: ["com.android.myapex"],
6612 }
6613 `)
6614 })
6615
6616 t.Run("java_sdk_library_import", func(t *testing.T) {
6617 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
6618 java_sdk_library_import {
6619 name: "libfoo",
6620 public: {
6621 jars: ["libbar.jar"],
6622 },
6623 apex_available: ["com.android.myapex"],
6624 }
6625 `)
6626 })
6627
6628 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
6629 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
6630 image_name: "art",
6631 contents: ["libfoo"],
6632 `)+`
6633 java_sdk_library_import {
6634 name: "libfoo",
6635 public: {
6636 jars: ["libbar.jar"],
6637 },
6638 apex_available: ["com.android.myapex"],
6639 }
6640 `)
6641 })
6642}
6643
Jooyung Han548640b2020-04-27 12:10:30 +09006644func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6645 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6646 apex {
6647 name: "myapex",
6648 key: "myapex.key",
6649 updatable: true,
6650 }
6651
6652 apex_key {
6653 name: "myapex.key",
6654 public_key: "testkey.avbpubkey",
6655 private_key: "testkey.pem",
6656 }
6657 `)
6658}
6659
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006660func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6661 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6662 apex {
6663 name: "myapex",
6664 key: "myapex.key",
6665 }
6666
6667 apex_key {
6668 name: "myapex.key",
6669 public_key: "testkey.avbpubkey",
6670 private_key: "testkey.pem",
6671 }
6672 `)
6673}
6674
satayevffe79712021-06-15 16:49:50 +01006675func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
6676 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
6677 apex {
6678 name: "myapex",
6679 key: "myapex.key",
6680 systemserverclasspath_fragments: [
6681 "mysystemserverclasspathfragment",
6682 ],
6683 min_sdk_version: "29",
6684 updatable: true,
6685 }
6686
6687 apex_key {
6688 name: "myapex.key",
6689 public_key: "testkey.avbpubkey",
6690 private_key: "testkey.pem",
6691 }
6692
6693 java_library {
6694 name: "foo",
6695 srcs: ["b.java"],
6696 min_sdk_version: "29",
6697 installable: true,
6698 apex_available: [
6699 "myapex",
6700 ],
6701 }
6702
6703 systemserverclasspath_fragment {
6704 name: "mysystemserverclasspathfragment",
6705 generate_classpaths_proto: false,
6706 contents: [
6707 "foo",
6708 ],
6709 apex_available: [
6710 "myapex",
6711 ],
6712 }
6713 `)
6714}
6715
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006716func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006717 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
6718 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
6719 // modules to be included in the BootJars.
6720 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
6721 return android.GroupFixturePreparers(
6722 dexpreopt.FixtureSetBootJars(bootJars...),
6723 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6724 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
6725 }),
6726 )
6727 }
6728
6729 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
6730 // same value. This can result in an invalid configuration as it allows non art apex jars to be
6731 // specified in the ArtApexJars configuration.
6732 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
6733 return android.GroupFixturePreparers(
6734 dexpreopt.FixtureSetArtBootJars(bootJars...),
6735 dexpreopt.FixtureSetBootJars(bootJars...),
6736 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6737 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
6738 }),
6739 )
6740 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006741
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006742 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006743 preparer := java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib")
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006744 fragment := java.ApexVariantReference{
6745 Apex: proptools.StringPtr("com.android.art.debug"),
6746 Module: proptools.StringPtr("art-bootclasspath-fragment"),
6747 }
6748 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006749 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006750
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006751 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006752 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
6753 // Update the dexpreopt BootJars directly.
6754 preparer := prepareSetBootJars("com.android.art.debug:some-art-lib")
6755 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006756 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006757
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006758 t.Run("updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01006759 err := `ArtApexJars expects this to be in apex "some-updatable-apex" but this is only in apexes.*"com.android.art.debug"`
Paul Duffin60264a02021-04-12 20:02:36 +01006760 // Update the dexpreopt ArtApexJars directly.
6761 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
6762 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006763 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006764
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006765 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 +01006766 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 +01006767 // Update the dexpreopt ArtApexJars directly.
6768 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
6769 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006770 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006771
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006772 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 +01006773 err := `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the framework boot image`
6774 preparer := java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib")
6775 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006776 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006777
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006778 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 +01006779 preparer := java.FixtureConfigureBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006780 fragment := java.ApexVariantReference{
6781 Apex: proptools.StringPtr("some-non-updatable-apex"),
6782 Module: proptools.StringPtr("some-non-updatable-fragment"),
6783 }
6784 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006785 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006786
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006787 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01006788 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01006789 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
6790 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006791 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006792
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006793 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01006794 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01006795 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
6796 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006797 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006798
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006799 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01006800 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01006801 // Update the dexpreopt ArtApexJars directly.
6802 preparer := prepareSetArtJars("platform:some-platform-lib")
6803 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006804 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006805
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006806 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006807 preparer := java.FixtureConfigureBootJars("platform:some-platform-lib")
6808 testNoUpdatableJarsInBootImage(t, "", preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006809 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006810}
6811
6812func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006813 preparer := java.FixtureConfigureBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00006814 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006815 fragment := java.ApexVariantReference{
6816 Apex: proptools.StringPtr("myapex"),
6817 Module: proptools.StringPtr("my-bootclasspath-fragment"),
6818 }
6819
Paul Duffin064b70c2020-11-02 17:32:38 +00006820 testDexpreoptWithApexes(t, `
6821 prebuilt_apex {
6822 name: "myapex" ,
6823 arch: {
6824 arm64: {
6825 src: "myapex-arm64.apex",
6826 },
6827 arm: {
6828 src: "myapex-arm.apex",
6829 },
6830 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006831 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
6832 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006833
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006834 prebuilt_bootclasspath_fragment {
6835 name: "my-bootclasspath-fragment",
6836 contents: ["libfoo"],
6837 apex_available: ["myapex"],
6838 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006839
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006840 java_import {
6841 name: "libfoo",
6842 jars: ["libfoo.jar"],
6843 apex_available: ["myapex"],
6844 }
6845 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00006846 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006847}
6848
satayev7a552ba2021-07-21 14:23:52 +01006849func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01006850 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006851 bp += `
6852 apex_key {
6853 name: "myapex.key",
6854 public_key: "testkey.avbpubkey",
6855 private_key: "testkey.pem",
6856 }`
Paul Duffin45338f02021-03-30 23:07:52 +01006857 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01006858 "lib1/src/A.java": nil,
6859 "lib2/src/B.java": nil,
6860 "system/sepolicy/apex/myapex-file_contexts": nil,
6861 }
6862
Paul Duffin45338f02021-03-30 23:07:52 +01006863 errorHandler := android.FixtureExpectsNoErrors
6864 if errmsg != "" {
6865 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07006866 }
Colin Crossae8600b2020-10-29 17:09:13 -07006867
Paul Duffin45338f02021-03-30 23:07:52 +01006868 android.GroupFixturePreparers(
6869 android.PrepareForTestWithAndroidBuildComponents,
6870 java.PrepareForTestWithJavaBuildComponents,
6871 PrepareForTestWithApexBuildComponents,
6872 android.PrepareForTestWithNeverallowRules(rules),
6873 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayev7a552ba2021-07-21 14:23:52 +01006874 apexBootJars := make([]string, 0, len(bootJars))
6875 for _, apexBootJar := range bootJars {
6876 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01006877 }
satayev7a552ba2021-07-21 14:23:52 +01006878 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01006879 }),
6880 fs.AddToFixture(),
6881 ).
6882 ExtendWithErrorHandler(errorHandler).
6883 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01006884}
6885
6886func TestApexPermittedPackagesRules(t *testing.T) {
6887 testcases := []struct {
6888 name string
6889 expectedError string
6890 bp string
6891 bootJars []string
6892 modulesPackages map[string][]string
6893 }{
6894
6895 {
6896 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6897 expectedError: "",
6898 bp: `
6899 java_library {
6900 name: "bcp_lib1",
6901 srcs: ["lib1/src/*.java"],
6902 permitted_packages: ["foo.bar"],
6903 apex_available: ["myapex"],
6904 sdk_version: "none",
6905 system_modules: "none",
Remi NGUYEN VANefb49af2021-12-10 00:17:57 +00006906 min_sdk_version: "30",
Andrei Onea115e7e72020-06-05 21:14:03 +01006907 }
6908 java_library {
6909 name: "nonbcp_lib2",
6910 srcs: ["lib2/src/*.java"],
6911 apex_available: ["myapex"],
6912 permitted_packages: ["a.b"],
6913 sdk_version: "none",
6914 system_modules: "none",
Remi NGUYEN VANefb49af2021-12-10 00:17:57 +00006915 min_sdk_version: "30",
Andrei Onea115e7e72020-06-05 21:14:03 +01006916 }
6917 apex {
6918 name: "myapex",
Remi NGUYEN VANefb49af2021-12-10 00:17:57 +00006919 min_sdk_version: "30",
Andrei Onea115e7e72020-06-05 21:14:03 +01006920 key: "myapex.key",
6921 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006922 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006923 }`,
6924 bootJars: []string{"bcp_lib1"},
6925 modulesPackages: map[string][]string{
6926 "myapex": []string{
6927 "foo.bar",
6928 },
6929 },
6930 },
6931 {
Remi NGUYEN VANefb49af2021-12-10 00:17:57 +00006932 name: "Bootclasspath apex jar not satisfying allowed module packages on Q.",
6933 expectedError: `module "bcp_lib2" .* which is restricted because jars that are part of the myapex module may only allow these packages: foo.bar with min_sdk < T. Please jarjar or move code around.`,
Andrei Onea115e7e72020-06-05 21:14:03 +01006934 bp: `
6935 java_library {
6936 name: "bcp_lib1",
6937 srcs: ["lib1/src/*.java"],
6938 apex_available: ["myapex"],
6939 permitted_packages: ["foo.bar"],
6940 sdk_version: "none",
6941 system_modules: "none",
Remi NGUYEN VANefb49af2021-12-10 00:17:57 +00006942 min_sdk_version: "29",
Andrei Onea115e7e72020-06-05 21:14:03 +01006943 }
6944 java_library {
6945 name: "bcp_lib2",
6946 srcs: ["lib2/src/*.java"],
6947 apex_available: ["myapex"],
6948 permitted_packages: ["foo.bar", "bar.baz"],
6949 sdk_version: "none",
6950 system_modules: "none",
Remi NGUYEN VANefb49af2021-12-10 00:17:57 +00006951 min_sdk_version: "29",
Andrei Onea115e7e72020-06-05 21:14:03 +01006952 }
6953 apex {
6954 name: "myapex",
Remi NGUYEN VANefb49af2021-12-10 00:17:57 +00006955 min_sdk_version: "29",
6956 key: "myapex.key",
6957 java_libs: ["bcp_lib1", "bcp_lib2"],
6958 updatable: false,
6959 }
6960 `,
6961 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6962 modulesPackages: map[string][]string{
6963 "myapex": []string{
6964 "foo.bar",
6965 },
6966 },
6967 },
6968 {
6969 name: "Bootclasspath apex jar not satisfying allowed module packages on R.",
6970 expectedError: `module "bcp_lib2" .* which is restricted because jars that are part of the myapex module may only allow these packages: foo.bar with min_sdk < T. Please jarjar or move code around.`,
6971 bp: `
6972 java_library {
6973 name: "bcp_lib1",
6974 srcs: ["lib1/src/*.java"],
6975 apex_available: ["myapex"],
6976 permitted_packages: ["foo.bar"],
6977 sdk_version: "none",
6978 system_modules: "none",
6979 min_sdk_version: "30",
6980 }
6981 java_library {
6982 name: "bcp_lib2",
6983 srcs: ["lib2/src/*.java"],
6984 apex_available: ["myapex"],
6985 permitted_packages: ["foo.bar", "bar.baz"],
6986 sdk_version: "none",
6987 system_modules: "none",
6988 min_sdk_version: "30",
6989 }
6990 apex {
6991 name: "myapex",
6992 min_sdk_version: "30",
6993 key: "myapex.key",
6994 java_libs: ["bcp_lib1", "bcp_lib2"],
6995 updatable: false,
6996 }
6997 `,
6998 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6999 modulesPackages: map[string][]string{
7000 "myapex": []string{
7001 "foo.bar",
7002 },
7003 },
7004 },
7005 {
7006 name: "Bootclasspath apex jar >= T not satisfying Q/R/S allowed module packages.",
7007 expectedError: "",
7008 bp: `
7009 java_library {
7010 name: "bcp_lib1",
7011 srcs: ["lib1/src/*.java"],
7012 apex_available: ["myapex"],
7013 permitted_packages: ["foo.bar"],
7014 sdk_version: "none",
7015 system_modules: "none",
7016 min_sdk_version: "current",
7017 }
7018 java_library {
7019 name: "bcp_lib2",
7020 srcs: ["lib2/src/*.java"],
7021 apex_available: ["myapex"],
7022 permitted_packages: ["foo.bar", "bar.baz"],
7023 sdk_version: "none",
7024 system_modules: "none",
7025 min_sdk_version: "current",
7026 }
7027 apex {
7028 name: "myapex",
7029 min_sdk_version: "current",
Andrei Onea115e7e72020-06-05 21:14:03 +01007030 key: "myapex.key",
7031 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007032 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01007033 }
7034 `,
7035 bootJars: []string{"bcp_lib1", "bcp_lib2"},
7036 modulesPackages: map[string][]string{
7037 "myapex": []string{
7038 "foo.bar",
7039 },
7040 },
7041 },
7042 }
7043 for _, tc := range testcases {
7044 t.Run(tc.name, func(t *testing.T) {
7045 rules := createApexPermittedPackagesRules(tc.modulesPackages)
7046 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
7047 })
7048 }
7049}
7050
Jiyong Park62304bb2020-04-13 16:19:48 +09007051func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007052 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09007053 apex {
7054 name: "myapex",
7055 key: "myapex.key",
7056 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007057 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09007058 }
7059
7060 apex_key {
7061 name: "myapex.key",
7062 public_key: "testkey.avbpubkey",
7063 private_key: "testkey.pem",
7064 }
7065
7066 cc_library {
7067 name: "mylib",
7068 srcs: ["mylib.cpp"],
7069 system_shared_libs: [],
7070 stl: "none",
7071 stubs: {
7072 versions: ["1"],
7073 },
7074 apex_available: ["myapex"],
7075 }
7076
7077 cc_library {
7078 name: "myprivlib",
7079 srcs: ["mylib.cpp"],
7080 system_shared_libs: [],
7081 stl: "none",
7082 apex_available: ["myapex"],
7083 }
7084
7085
7086 cc_test {
7087 name: "mytest",
7088 gtest: false,
7089 srcs: ["mylib.cpp"],
7090 system_shared_libs: [],
7091 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09007092 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09007093 test_for: ["myapex"]
7094 }
Jiyong Park46a512f2020-12-04 18:02:13 +09007095
7096 cc_library {
7097 name: "mytestlib",
7098 srcs: ["mylib.cpp"],
7099 system_shared_libs: [],
7100 shared_libs: ["mylib", "myprivlib"],
7101 stl: "none",
7102 test_for: ["myapex"],
7103 }
7104
7105 cc_benchmark {
7106 name: "mybench",
7107 srcs: ["mylib.cpp"],
7108 system_shared_libs: [],
7109 shared_libs: ["mylib", "myprivlib"],
7110 stl: "none",
7111 test_for: ["myapex"],
7112 }
Jiyong Park62304bb2020-04-13 16:19:48 +09007113 `)
7114
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007115 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01007116 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007117 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
7118 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
7119 }
7120
7121 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09007122 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007123 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7124 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7125 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7126}
Jiyong Park46a512f2020-12-04 18:02:13 +09007127
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007128func TestIndirectTestFor(t *testing.T) {
7129 ctx := testApex(t, `
7130 apex {
7131 name: "myapex",
7132 key: "myapex.key",
7133 native_shared_libs: ["mylib", "myprivlib"],
7134 updatable: false,
7135 }
Jiyong Park46a512f2020-12-04 18:02:13 +09007136
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007137 apex_key {
7138 name: "myapex.key",
7139 public_key: "testkey.avbpubkey",
7140 private_key: "testkey.pem",
7141 }
7142
7143 cc_library {
7144 name: "mylib",
7145 srcs: ["mylib.cpp"],
7146 system_shared_libs: [],
7147 stl: "none",
7148 stubs: {
7149 versions: ["1"],
7150 },
7151 apex_available: ["myapex"],
7152 }
7153
7154 cc_library {
7155 name: "myprivlib",
7156 srcs: ["mylib.cpp"],
7157 system_shared_libs: [],
7158 stl: "none",
7159 shared_libs: ["mylib"],
7160 apex_available: ["myapex"],
7161 }
7162
7163 cc_library {
7164 name: "mytestlib",
7165 srcs: ["mylib.cpp"],
7166 system_shared_libs: [],
7167 shared_libs: ["myprivlib"],
7168 stl: "none",
7169 test_for: ["myapex"],
7170 }
7171 `)
7172
7173 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01007174 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007175 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
7176 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
7177 }
7178
7179 // The platform variant of mytestlib links to the platform variant of the
7180 // internal myprivlib.
7181 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
7182
7183 // The platform variant of myprivlib links to the platform variant of mylib
7184 // and bypasses its stubs.
7185 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 +09007186}
7187
Martin Stjernholmec009002021-03-27 15:18:31 +00007188func TestTestForForLibInOtherApex(t *testing.T) {
7189 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
7190 _ = testApex(t, `
7191 apex {
7192 name: "com.android.art",
7193 key: "myapex.key",
7194 native_shared_libs: ["mylib"],
7195 updatable: false,
7196 }
7197
7198 apex {
7199 name: "com.android.art.debug",
7200 key: "myapex.key",
7201 native_shared_libs: ["mylib", "mytestlib"],
7202 updatable: false,
7203 }
7204
7205 apex_key {
7206 name: "myapex.key",
7207 public_key: "testkey.avbpubkey",
7208 private_key: "testkey.pem",
7209 }
7210
7211 cc_library {
7212 name: "mylib",
7213 srcs: ["mylib.cpp"],
7214 system_shared_libs: [],
7215 stl: "none",
7216 stubs: {
7217 versions: ["1"],
7218 },
7219 apex_available: ["com.android.art", "com.android.art.debug"],
7220 }
7221
7222 cc_library {
7223 name: "mytestlib",
7224 srcs: ["mylib.cpp"],
7225 system_shared_libs: [],
7226 shared_libs: ["mylib"],
7227 stl: "none",
7228 apex_available: ["com.android.art.debug"],
7229 test_for: ["com.android.art"],
7230 }
7231 `,
7232 android.MockFS{
7233 "system/sepolicy/apex/com.android.art-file_contexts": nil,
7234 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
7235 }.AddToFixture())
7236}
7237
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007238// TODO(jungjw): Move this to proptools
7239func intPtr(i int) *int {
7240 return &i
7241}
7242
7243func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007244 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007245 apex_set {
7246 name: "myapex",
7247 set: "myapex.apks",
7248 filename: "foo_v2.apex",
7249 overrides: ["foo"],
7250 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007251 `,
7252 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7253 variables.Platform_sdk_version = intPtr(30)
7254 }),
7255 android.FixtureModifyConfig(func(config android.Config) {
7256 config.Targets[android.Android] = []android.Target{
7257 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
7258 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
7259 }
7260 }),
7261 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007262
Paul Duffin24704672021-04-06 16:09:30 +01007263 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007264
7265 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01007266 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007267 actual := extractedApex.Args["abis"]
7268 expected := "ARMEABI_V7A,ARM64_V8A"
7269 if actual != expected {
7270 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7271 }
7272 actual = extractedApex.Args["sdk-version"]
7273 expected = "30"
7274 if actual != expected {
7275 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7276 }
7277
Paul Duffin5ec165d2021-06-15 19:09:41 +01007278 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007279 a := m.Module().(*ApexSet)
7280 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07007281 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007282 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
7283 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
7284 }
7285}
7286
Jiyong Park7d95a512020-05-10 15:16:24 +09007287func TestNoStaticLinkingToStubsLib(t *testing.T) {
7288 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
7289 apex {
7290 name: "myapex",
7291 key: "myapex.key",
7292 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007293 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09007294 }
7295
7296 apex_key {
7297 name: "myapex.key",
7298 public_key: "testkey.avbpubkey",
7299 private_key: "testkey.pem",
7300 }
7301
7302 cc_library {
7303 name: "mylib",
7304 srcs: ["mylib.cpp"],
7305 static_libs: ["otherlib"],
7306 system_shared_libs: [],
7307 stl: "none",
7308 apex_available: [ "myapex" ],
7309 }
7310
7311 cc_library {
7312 name: "otherlib",
7313 srcs: ["mylib.cpp"],
7314 system_shared_libs: [],
7315 stl: "none",
7316 stubs: {
7317 versions: ["1", "2", "3"],
7318 },
7319 apex_available: [ "myapex" ],
7320 }
7321 `)
7322}
7323
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007324func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007325 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007326 apex {
7327 name: "myapex",
7328 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007329 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007330 }
7331
7332 apex_key {
7333 name: "myapex.key",
7334 public_key: "testkey.avbpubkey",
7335 private_key: "testkey.pem",
7336 }
7337
7338 prebuilt_apex {
7339 name: "myapex",
7340 prefer: true,
7341 arch: {
7342 arm64: {
7343 src: "myapex-arm64.apex",
7344 },
7345 arm: {
7346 src: "myapex-arm.apex",
7347 },
7348 },
7349 }
7350
7351 apex_set {
7352 name: "myapex_set",
7353 set: "myapex.apks",
7354 filename: "myapex_set.apex",
7355 overrides: ["myapex"],
7356 }
7357 `)
7358
7359 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7360 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7361 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 +09007362 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 +09007363}
7364
Jooyung Han938b5932020-06-20 12:47:47 +09007365func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007366 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007367 apex {
7368 name: "myapex",
7369 key: "myapex.key",
7370 apps: ["app"],
7371 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007372 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007373 }
7374
7375 apex_key {
7376 name: "myapex.key",
7377 public_key: "testkey.avbpubkey",
7378 private_key: "testkey.pem",
7379 }
7380
7381 android_app {
7382 name: "app",
7383 srcs: ["foo/bar/MyClass.java"],
7384 package_name: "foo",
7385 sdk_version: "none",
7386 system_modules: "none",
7387 apex_available: [ "myapex" ],
7388 }
7389 `, withFiles(map[string][]byte{
7390 "sub/Android.bp": []byte(`
7391 override_apex {
7392 name: "override_myapex",
7393 base: "myapex",
7394 apps: ["override_app"],
7395 allowed_files: ":allowed",
7396 }
7397 // Overridable "path" property should be referenced indirectly
7398 filegroup {
7399 name: "allowed",
7400 srcs: ["allowed.txt"],
7401 }
7402 override_android_app {
7403 name: "override_app",
7404 base: "app",
7405 package_name: "bar",
7406 }
7407 `),
7408 }))
7409
7410 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7411 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7412 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7413 }
7414
7415 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7416 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7417 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7418 }
7419}
7420
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007421func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007422 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007423 apex {
7424 name: "myapex",
7425 key: "myapex.key",
7426 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007427 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007428 }
7429
7430 apex_key {
7431 name: "myapex.key",
7432 public_key: "testkey.avbpubkey",
7433 private_key: "testkey.pem",
7434 }
7435
7436 cc_library {
7437 name: "mylib",
7438 srcs: ["mylib.cpp"],
7439 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007440 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007441 },
7442 apex_available: ["myapex"],
7443 }
7444
7445 cc_prebuilt_library_shared {
7446 name: "mylib",
7447 prefer: false,
7448 srcs: ["prebuilt.so"],
7449 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007450 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007451 },
7452 apex_available: ["myapex"],
7453 }
7454 `)
7455}
7456
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007457func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007458 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007459 apex {
7460 name: "myapex",
7461 key: "myapex.key",
7462 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007463 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007464 }
7465 apex_key {
7466 name: "myapex.key",
7467 public_key: "testkey.avbpubkey",
7468 private_key: "testkey.pem",
7469 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007470 `,
7471 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7472 variables.CompressedApex = proptools.BoolPtr(true)
7473 }),
7474 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007475
7476 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7477 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7478
7479 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7480 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7481
7482 // Make sure output of bundle is .capex
7483 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7484 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7485
7486 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007487 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007488 var builder strings.Builder
7489 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7490 androidMk := builder.String()
7491 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7492}
7493
Martin Stjernholm2856c662020-12-02 15:03:42 +00007494func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007495 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007496 apex {
7497 name: "myapex",
7498 key: "myapex.key",
7499 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007500 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007501 }
7502
7503 apex_key {
7504 name: "myapex.key",
7505 public_key: "testkey.avbpubkey",
7506 private_key: "testkey.pem",
7507 }
7508
7509 cc_library {
7510 name: "mylib",
7511 srcs: ["mylib.cpp"],
7512 apex_available: ["myapex"],
7513 shared_libs: ["otherlib"],
7514 system_shared_libs: [],
7515 }
7516
7517 cc_library {
7518 name: "otherlib",
7519 srcs: ["mylib.cpp"],
7520 stubs: {
7521 versions: ["current"],
7522 },
7523 }
7524
7525 cc_prebuilt_library_shared {
7526 name: "otherlib",
7527 prefer: true,
7528 srcs: ["prebuilt.so"],
7529 stubs: {
7530 versions: ["current"],
7531 },
7532 }
7533 `)
7534
7535 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007536 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007537 var builder strings.Builder
7538 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7539 androidMk := builder.String()
7540
7541 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7542 // a thing there.
7543 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7544}
7545
Jiyong Parke3867542020-12-03 17:28:25 +09007546func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007547 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007548 apex {
7549 name: "myapex",
7550 key: "myapex.key",
7551 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007552 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007553 }
7554
7555 apex_key {
7556 name: "myapex.key",
7557 public_key: "testkey.avbpubkey",
7558 private_key: "testkey.pem",
7559 }
7560
7561 cc_library {
7562 name: "mylib",
7563 srcs: ["mylib.cpp"],
7564 system_shared_libs: [],
7565 stl: "none",
7566 apex_available: ["myapex"],
7567 shared_libs: ["mylib2"],
7568 target: {
7569 apex: {
7570 exclude_shared_libs: ["mylib2"],
7571 },
7572 },
7573 }
7574
7575 cc_library {
7576 name: "mylib2",
7577 srcs: ["mylib.cpp"],
7578 system_shared_libs: [],
7579 stl: "none",
7580 }
7581 `)
7582
7583 // Check if mylib is linked to mylib2 for the non-apex target
7584 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7585 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7586
7587 // Make sure that the link doesn't occur for the apex target
7588 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7589 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7590
7591 // It shouldn't appear in the copy cmd as well.
7592 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7593 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7594}
7595
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007596func TestPrebuiltStubLibDep(t *testing.T) {
7597 bpBase := `
7598 apex {
7599 name: "myapex",
7600 key: "myapex.key",
7601 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007602 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007603 }
7604 apex_key {
7605 name: "myapex.key",
7606 public_key: "testkey.avbpubkey",
7607 private_key: "testkey.pem",
7608 }
7609 cc_library {
7610 name: "mylib",
7611 srcs: ["mylib.cpp"],
7612 apex_available: ["myapex"],
7613 shared_libs: ["stublib"],
7614 system_shared_libs: [],
7615 }
7616 apex {
7617 name: "otherapex",
7618 enabled: %s,
7619 key: "myapex.key",
7620 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007621 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007622 }
7623 `
7624
7625 stublibSourceBp := `
7626 cc_library {
7627 name: "stublib",
7628 srcs: ["mylib.cpp"],
7629 apex_available: ["otherapex"],
7630 system_shared_libs: [],
7631 stl: "none",
7632 stubs: {
7633 versions: ["1"],
7634 },
7635 }
7636 `
7637
7638 stublibPrebuiltBp := `
7639 cc_prebuilt_library_shared {
7640 name: "stublib",
7641 srcs: ["prebuilt.so"],
7642 apex_available: ["otherapex"],
7643 stubs: {
7644 versions: ["1"],
7645 },
7646 %s
7647 }
7648 `
7649
7650 tests := []struct {
7651 name string
7652 stublibBp string
7653 usePrebuilt bool
7654 modNames []string // Modules to collect AndroidMkEntries for
7655 otherApexEnabled []string
7656 }{
7657 {
7658 name: "only_source",
7659 stublibBp: stublibSourceBp,
7660 usePrebuilt: false,
7661 modNames: []string{"stublib"},
7662 otherApexEnabled: []string{"true", "false"},
7663 },
7664 {
7665 name: "source_preferred",
7666 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7667 usePrebuilt: false,
7668 modNames: []string{"stublib", "prebuilt_stublib"},
7669 otherApexEnabled: []string{"true", "false"},
7670 },
7671 {
7672 name: "prebuilt_preferred",
7673 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7674 usePrebuilt: true,
7675 modNames: []string{"stublib", "prebuilt_stublib"},
7676 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7677 },
7678 {
7679 name: "only_prebuilt",
7680 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7681 usePrebuilt: true,
7682 modNames: []string{"stublib"},
7683 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7684 },
7685 }
7686
7687 for _, test := range tests {
7688 t.Run(test.name, func(t *testing.T) {
7689 for _, otherApexEnabled := range test.otherApexEnabled {
7690 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007691 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007692
7693 type modAndMkEntries struct {
7694 mod *cc.Module
7695 mkEntries android.AndroidMkEntries
7696 }
7697 entries := []*modAndMkEntries{}
7698
7699 // Gather shared lib modules that are installable
7700 for _, modName := range test.modNames {
7701 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7702 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7703 continue
7704 }
7705 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007706 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007707 continue
7708 }
Colin Crossaa255532020-07-03 13:18:24 -07007709 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007710 if ent.Disabled {
7711 continue
7712 }
7713 entries = append(entries, &modAndMkEntries{
7714 mod: mod,
7715 mkEntries: ent,
7716 })
7717 }
7718 }
7719 }
7720
7721 var entry *modAndMkEntries = nil
7722 for _, ent := range entries {
7723 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7724 if entry != nil {
7725 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7726 } else {
7727 entry = ent
7728 }
7729 }
7730 }
7731
7732 if entry == nil {
7733 t.Errorf("AndroidMk entry for \"stublib\" missing")
7734 } else {
7735 isPrebuilt := entry.mod.Prebuilt() != nil
7736 if isPrebuilt != test.usePrebuilt {
7737 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7738 }
7739 if !entry.mod.IsStubs() {
7740 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7741 }
7742 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7743 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7744 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007745 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09007746 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09007747 if !android.InList(expected, cflags) {
7748 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7749 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007750 }
7751 })
7752 }
7753 })
7754 }
7755}
7756
Colin Crosscbb2b812021-05-25 18:16:02 -07007757func TestApexJavaCoverage(t *testing.T) {
7758 bp := `
7759 apex {
7760 name: "myapex",
7761 key: "myapex.key",
7762 java_libs: ["mylib"],
7763 bootclasspath_fragments: ["mybootclasspathfragment"],
7764 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
7765 updatable: false,
7766 }
7767
7768 apex_key {
7769 name: "myapex.key",
7770 public_key: "testkey.avbpubkey",
7771 private_key: "testkey.pem",
7772 }
7773
7774 java_library {
7775 name: "mylib",
7776 srcs: ["mylib.java"],
7777 apex_available: ["myapex"],
7778 compile_dex: true,
7779 }
7780
7781 bootclasspath_fragment {
7782 name: "mybootclasspathfragment",
7783 contents: ["mybootclasspathlib"],
7784 apex_available: ["myapex"],
7785 }
7786
7787 java_library {
7788 name: "mybootclasspathlib",
7789 srcs: ["mybootclasspathlib.java"],
7790 apex_available: ["myapex"],
7791 compile_dex: true,
7792 }
7793
7794 systemserverclasspath_fragment {
7795 name: "mysystemserverclasspathfragment",
7796 contents: ["mysystemserverclasspathlib"],
7797 apex_available: ["myapex"],
7798 }
7799
7800 java_library {
7801 name: "mysystemserverclasspathlib",
7802 srcs: ["mysystemserverclasspathlib.java"],
7803 apex_available: ["myapex"],
7804 compile_dex: true,
7805 }
7806 `
7807
7808 result := android.GroupFixturePreparers(
7809 PrepareForTestWithApexBuildComponents,
7810 prepareForTestWithMyapex,
7811 java.PrepareForTestWithJavaDefaultModules,
7812 android.PrepareForTestWithAndroidBuildComponents,
7813 android.FixtureWithRootAndroidBp(bp),
7814 android.FixtureMergeEnv(map[string]string{
7815 "EMMA_INSTRUMENT": "true",
7816 }),
7817 ).RunTest(t)
7818
7819 // Make sure jacoco ran on both mylib and mybootclasspathlib
7820 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
7821 t.Errorf("Failed to find jacoco rule for mylib")
7822 }
7823 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
7824 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
7825 }
7826 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
7827 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
7828 }
7829}
7830
satayev812683e2021-12-06 11:42:40 +00007831func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
7832 preparer := android.GroupFixturePreparers(
7833 PrepareForTestWithApexBuildComponents,
7834 prepareForTestWithMyapex,
7835 java.PrepareForTestWithJavaSdkLibraryFiles,
7836 java.PrepareForTestWithJavaDefaultModules,
7837 android.PrepareForTestWithAndroidBuildComponents,
7838 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
7839 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
7840 )
7841
7842 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
7843 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
7844 preparer.RunTestWithBp(t, `
7845 apex {
7846 name: "myapex",
7847 key: "myapex.key",
7848 bootclasspath_fragments: ["mybootclasspathfragment"],
7849 min_sdk_version: "30",
7850 updatable: false,
7851 }
7852
7853 apex_key {
7854 name: "myapex.key",
7855 public_key: "testkey.avbpubkey",
7856 private_key: "testkey.pem",
7857 }
7858
7859 bootclasspath_fragment {
7860 name: "mybootclasspathfragment",
7861 contents: ["mybootclasspathlib"],
7862 apex_available: ["myapex"],
7863 }
7864
7865 java_sdk_library {
7866 name: "mybootclasspathlib",
7867 srcs: ["mybootclasspathlib.java"],
7868 apex_available: ["myapex"],
7869 compile_dex: true,
7870 unsafe_ignore_missing_latest_api: true,
7871 min_sdk_version: "31",
7872 static_libs: ["util"],
7873 }
7874
7875 java_library {
7876 name: "util",
7877 srcs: ["a.java"],
7878 apex_available: ["myapex"],
7879 min_sdk_version: "31",
7880 static_libs: ["another_util"],
7881 }
7882
7883 java_library {
7884 name: "another_util",
7885 srcs: ["a.java"],
7886 min_sdk_version: "31",
7887 apex_available: ["myapex"],
7888 }
7889 `)
7890 })
7891
7892 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
7893 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
7894 preparer.RunTestWithBp(t, `
7895 apex {
7896 name: "myapex",
7897 key: "myapex.key",
7898 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
7899 min_sdk_version: "30",
7900 updatable: false,
7901 }
7902
7903 apex_key {
7904 name: "myapex.key",
7905 public_key: "testkey.avbpubkey",
7906 private_key: "testkey.pem",
7907 }
7908
7909 systemserverclasspath_fragment {
7910 name: "mysystemserverclasspathfragment",
7911 contents: ["mysystemserverclasspathlib"],
7912 apex_available: ["myapex"],
7913 }
7914
7915 java_sdk_library {
7916 name: "mysystemserverclasspathlib",
7917 srcs: ["mysystemserverclasspathlib.java"],
7918 apex_available: ["myapex"],
7919 compile_dex: true,
7920 min_sdk_version: "32",
7921 unsafe_ignore_missing_latest_api: true,
7922 static_libs: ["util"],
7923 }
7924
7925 java_library {
7926 name: "util",
7927 srcs: ["a.java"],
7928 apex_available: ["myapex"],
7929 min_sdk_version: "31",
7930 static_libs: ["another_util"],
7931 }
7932
7933 java_library {
7934 name: "another_util",
7935 srcs: ["a.java"],
7936 min_sdk_version: "31",
7937 apex_available: ["myapex"],
7938 }
7939 `)
7940 })
7941
7942 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
7943 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
7944 RunTestWithBp(t, `
7945 apex {
7946 name: "myapex",
7947 key: "myapex.key",
7948 bootclasspath_fragments: ["mybootclasspathfragment"],
7949 min_sdk_version: "30",
7950 updatable: false,
7951 }
7952
7953 apex_key {
7954 name: "myapex.key",
7955 public_key: "testkey.avbpubkey",
7956 private_key: "testkey.pem",
7957 }
7958
7959 bootclasspath_fragment {
7960 name: "mybootclasspathfragment",
7961 contents: ["mybootclasspathlib"],
7962 apex_available: ["myapex"],
7963 }
7964
7965 java_sdk_library {
7966 name: "mybootclasspathlib",
7967 srcs: ["mybootclasspathlib.java"],
7968 apex_available: ["myapex"],
7969 compile_dex: true,
7970 unsafe_ignore_missing_latest_api: true,
7971 }
7972 `)
7973 })
7974
7975 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
7976 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
7977 RunTestWithBp(t, `
7978 apex {
7979 name: "myapex",
7980 key: "myapex.key",
7981 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
7982 min_sdk_version: "30",
7983 updatable: false,
7984 }
7985
7986 apex_key {
7987 name: "myapex.key",
7988 public_key: "testkey.avbpubkey",
7989 private_key: "testkey.pem",
7990 }
7991
7992 systemserverclasspath_fragment {
7993 name: "mysystemserverclasspathfragment",
7994 contents: ["mysystemserverclasspathlib"],
7995 apex_available: ["myapex"],
7996 }
7997
7998 java_sdk_library {
7999 name: "mysystemserverclasspathlib",
8000 srcs: ["mysystemserverclasspathlib.java"],
8001 apex_available: ["myapex"],
8002 compile_dex: true,
8003 unsafe_ignore_missing_latest_api: true,
8004 }
8005 `)
8006 })
8007}
8008
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07008009func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01008010 os.Exit(m.Run())
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07008011}