blob: c50c4a94fed080e548be3f00d0b2c1db48ca1ee3 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +090018 "fmt"
Jiyong Park25fc6a92018-11-18 18:02:45 +090019 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Paul Duffin37856732021-02-26 14:24:15 +000021 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070022 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010023 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090024 "sort"
Jiyong Parkd4a3a132021-03-17 20:21:35 +090025 "strconv"
Jiyong Park25fc6a92018-11-18 18:02:45 +090026 "strings"
27 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090028
29 "github.com/google/blueprint/proptools"
30
31 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080032 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090033 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000034 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090036 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090037 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090039)
40
Jooyung Hand3639552019-08-09 12:57:43 +090041// names returns name list from white space separated string
42func names(s string) (ns []string) {
43 for _, n := range strings.Split(s, " ") {
44 if len(n) > 0 {
45 ns = append(ns, n)
46 }
47 }
48 return
49}
50
Paul Duffin40b62572021-03-20 11:39:01 +000051func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090052 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010053 android.GroupFixturePreparers(
54 prepareForApexTest,
55 android.GroupFixturePreparers(preparers...),
56 ).
Paul Duffine05480a2021-03-08 15:07:14 +000057 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000058 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090059}
60
Paul Duffin40b62572021-03-20 11:39:01 +000061func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090062 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010063
64 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000065 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010066 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000067 }
Paul Duffin284165a2021-03-29 01:50:31 +010068
69 result := android.GroupFixturePreparers(
70 prepareForApexTest,
71 android.GroupFixturePreparers(preparers...),
72 optionalBpPreparer,
73 ).RunTest(t)
74
Paul Duffine05480a2021-03-08 15:07:14 +000075 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090076}
77
Paul Duffin810f33d2021-03-09 14:12:32 +000078func withFiles(files android.MockFS) android.FixturePreparer {
79 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090080}
81
Paul Duffin810f33d2021-03-09 14:12:32 +000082func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
83 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090084 for k, v := range targets {
85 config.Targets[k] = v
86 }
Paul Duffin810f33d2021-03-09 14:12:32 +000087 })
Jooyung Han344d5432019-08-23 11:17:39 +090088}
89
Jooyung Han35155c42020-02-06 17:33:20 +090090// withNativeBridgeTargets sets configuration with targets including:
91// - X86_64 (primary)
92// - X86 (secondary)
93// - Arm64 on X86_64 (native bridge)
94// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000095var withNativeBridgeEnabled = android.FixtureModifyConfig(
96 func(config android.Config) {
97 config.Targets[android.Android] = []android.Target{
98 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
99 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
100 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
101 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
102 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
103 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
104 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
105 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
106 }
107 },
108)
109
110func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
111 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
112 variables.ManifestPackageNameOverrides = specs
113 })
Jooyung Han35155c42020-02-06 17:33:20 +0900114}
115
Paul Duffin810f33d2021-03-09 14:12:32 +0000116var withBinder32bit = android.FixtureModifyProductVariables(
117 func(variables android.FixtureProductVariables) {
118 variables.Binder32bit = proptools.BoolPtr(true)
119 },
120)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900121
Paul Duffin810f33d2021-03-09 14:12:32 +0000122var withUnbundledBuild = android.FixtureModifyProductVariables(
123 func(variables android.FixtureProductVariables) {
124 variables.Unbundled_build = proptools.BoolPtr(true)
125 },
126)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900127
Paul Duffin284165a2021-03-29 01:50:31 +0100128// Legacy preparer used for running tests within the apex package.
129//
130// This includes everything that was needed to run any test in the apex package prior to the
131// introduction of the test fixtures. Tests that are being converted to use fixtures directly
132// rather than through the testApex...() methods should avoid using this and instead use the
133// various preparers directly, using android.GroupFixturePreparers(...) to group them when
134// necessary.
135//
136// deprecated
137var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000138 // General preparers in alphabetical order as test infrastructure will enforce correct
139 // registration order.
140 android.PrepareForTestWithAndroidBuildComponents,
141 bpf.PrepareForTestWithBpf,
142 cc.PrepareForTestWithCcBuildComponents,
143 java.PrepareForTestWithJavaDefaultModules,
144 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
145 rust.PrepareForTestWithRustDefaultModules,
146 sh.PrepareForTestWithShBuildComponents,
147
148 PrepareForTestWithApexBuildComponents,
149
150 // Additional apex test specific preparers.
151 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
152 filegroup {
153 name: "myapex-file_contexts",
154 srcs: [
155 "apex/myapex-file_contexts",
156 ],
157 }
158 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000159 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000160 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000161 "a.java": nil,
162 "PrebuiltAppFoo.apk": nil,
163 "PrebuiltAppFooPriv.apk": nil,
164 "apex_manifest.json": nil,
165 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000166 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
167 "system/sepolicy/apex/myapex2-file_contexts": nil,
168 "system/sepolicy/apex/otherapex-file_contexts": nil,
169 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
170 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
171 "mylib.cpp": nil,
172 "mytest.cpp": nil,
173 "mytest1.cpp": nil,
174 "mytest2.cpp": nil,
175 "mytest3.cpp": nil,
176 "myprebuilt": nil,
177 "my_include": nil,
178 "foo/bar/MyClass.java": nil,
179 "prebuilt.jar": nil,
180 "prebuilt.so": nil,
181 "vendor/foo/devkeys/test.x509.pem": nil,
182 "vendor/foo/devkeys/test.pk8": nil,
183 "testkey.x509.pem": nil,
184 "testkey.pk8": nil,
185 "testkey.override.x509.pem": nil,
186 "testkey.override.pk8": nil,
187 "vendor/foo/devkeys/testkey.avbpubkey": nil,
188 "vendor/foo/devkeys/testkey.pem": nil,
189 "NOTICE": nil,
190 "custom_notice": nil,
191 "custom_notice_for_static_lib": nil,
192 "testkey2.avbpubkey": nil,
193 "testkey2.pem": nil,
194 "myapex-arm64.apex": nil,
195 "myapex-arm.apex": nil,
196 "myapex.apks": nil,
197 "frameworks/base/api/current.txt": nil,
198 "framework/aidl/a.aidl": nil,
199 "build/make/core/proguard.flags": nil,
200 "build/make/core/proguard_basic_keeps.flags": nil,
201 "dummy.txt": nil,
202 "baz": nil,
203 "bar/baz": nil,
204 "testdata/baz": nil,
205 "AppSet.apks": nil,
206 "foo.rs": nil,
207 "libfoo.jar": nil,
208 "libbar.jar": nil,
209 },
210 ),
211
212 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
213 variables.DeviceVndkVersion = proptools.StringPtr("current")
214 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
215 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
216 variables.Platform_sdk_codename = proptools.StringPtr("Q")
217 variables.Platform_sdk_final = proptools.BoolPtr(false)
218 variables.Platform_version_active_codenames = []string{"Q"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900219 variables.Platform_vndk_version = proptools.StringPtr("29")
Paul Duffin37aad602021-03-08 09:47:16 +0000220 }),
221)
222
Paul Duffin52bfaa42021-03-23 23:40:12 +0000223var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
224 "system/sepolicy/apex/myapex-file_contexts": nil,
225})
226
Jooyung Han643adc42020-02-27 13:50:06 +0900227// ensure that 'result' equals 'expected'
228func ensureEquals(t *testing.T, result string, expected string) {
229 t.Helper()
230 if result != expected {
231 t.Errorf("%q != %q", expected, result)
232 }
233}
234
Jiyong Park25fc6a92018-11-18 18:02:45 +0900235// ensure that 'result' contains 'expected'
236func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900237 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900238 if !strings.Contains(result, expected) {
239 t.Errorf("%q is not found in %q", expected, result)
240 }
241}
242
Liz Kammer5bd365f2020-05-27 15:15:11 -0700243// ensure that 'result' contains 'expected' exactly one time
244func ensureContainsOnce(t *testing.T, result string, expected string) {
245 t.Helper()
246 count := strings.Count(result, expected)
247 if count != 1 {
248 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
249 }
250}
251
Jiyong Park25fc6a92018-11-18 18:02:45 +0900252// ensures that 'result' does not contain 'notExpected'
253func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900254 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900255 if strings.Contains(result, notExpected) {
256 t.Errorf("%q is found in %q", notExpected, result)
257 }
258}
259
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700260func ensureMatches(t *testing.T, result string, expectedRex string) {
261 ok, err := regexp.MatchString(expectedRex, result)
262 if err != nil {
263 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
264 return
265 }
266 if !ok {
267 t.Errorf("%s does not match regular expession %s", result, expectedRex)
268 }
269}
270
Jiyong Park25fc6a92018-11-18 18:02:45 +0900271func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900272 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900273 if !android.InList(expected, result) {
274 t.Errorf("%q is not found in %v", expected, result)
275 }
276}
277
278func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900279 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 if android.InList(notExpected, result) {
281 t.Errorf("%q is found in %v", notExpected, result)
282 }
283}
284
Jooyung Hane1633032019-08-01 17:41:43 +0900285func ensureListEmpty(t *testing.T, result []string) {
286 t.Helper()
287 if len(result) > 0 {
288 t.Errorf("%q is expected to be empty", result)
289 }
290}
291
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000292func ensureListNotEmpty(t *testing.T, result []string) {
293 t.Helper()
294 if len(result) == 0 {
295 t.Errorf("%q is expected to be not empty", result)
296 }
297}
298
Jiyong Park25fc6a92018-11-18 18:02:45 +0900299// Minimal test
300func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800301 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900302 apex_defaults {
303 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900304 manifest: ":myapex.manifest",
305 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900306 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900307 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900308 native_shared_libs: [
309 "mylib",
310 "libfoo.ffi",
311 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900312 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800313 multilib: {
314 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900315 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800316 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900317 },
Jiyong Park77acec62020-06-01 21:39:15 +0900318 java_libs: [
319 "myjar",
320 "myjar_dex",
321 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000322 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323 }
324
Jiyong Park30ca9372019-02-07 16:27:23 +0900325 apex {
326 name: "myapex",
327 defaults: ["myapex-defaults"],
328 }
329
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 apex_key {
331 name: "myapex.key",
332 public_key: "testkey.avbpubkey",
333 private_key: "testkey.pem",
334 }
335
Jiyong Park809bb722019-02-13 21:33:49 +0900336 filegroup {
337 name: "myapex.manifest",
338 srcs: ["apex_manifest.json"],
339 }
340
341 filegroup {
342 name: "myapex.androidmanifest",
343 srcs: ["AndroidManifest.xml"],
344 }
345
Jiyong Park25fc6a92018-11-18 18:02:45 +0900346 cc_library {
347 name: "mylib",
348 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900349 shared_libs: [
350 "mylib2",
351 "libbar.ffi",
352 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353 system_shared_libs: [],
354 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000355 // TODO: remove //apex_available:platform
356 apex_available: [
357 "//apex_available:platform",
358 "myapex",
359 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900360 }
361
Alex Light3d673592019-01-18 14:37:31 -0800362 cc_binary {
363 name: "foo",
364 srcs: ["mylib.cpp"],
365 compile_multilib: "both",
366 multilib: {
367 lib32: {
368 suffix: "32",
369 },
370 lib64: {
371 suffix: "64",
372 },
373 },
374 symlinks: ["foo_link_"],
375 symlink_preferred_arch: true,
376 system_shared_libs: [],
377 static_executable: true,
378 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700379 apex_available: [ "myapex", "com.android.gki.*" ],
380 }
381
Jiyong Park99644e92020-11-17 22:21:02 +0900382 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000383 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900384 srcs: ["foo.rs"],
385 rlibs: ["libfoo.rlib.rust"],
386 dylibs: ["libfoo.dylib.rust"],
387 apex_available: ["myapex"],
388 }
389
390 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000391 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900392 srcs: ["foo.rs"],
393 crate_name: "foo",
394 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900395 shared_libs: ["libfoo.shared_from_rust"],
396 }
397
398 cc_library_shared {
399 name: "libfoo.shared_from_rust",
400 srcs: ["mylib.cpp"],
401 system_shared_libs: [],
402 stl: "none",
403 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900404 }
405
406 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000407 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900408 srcs: ["foo.rs"],
409 crate_name: "foo",
410 apex_available: ["myapex"],
411 }
412
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900413 rust_ffi_shared {
414 name: "libfoo.ffi",
415 srcs: ["foo.rs"],
416 crate_name: "foo",
417 apex_available: ["myapex"],
418 }
419
420 rust_ffi_shared {
421 name: "libbar.ffi",
422 srcs: ["foo.rs"],
423 crate_name: "bar",
424 apex_available: ["myapex"],
425 }
426
Yifan Hongd22a84a2020-07-28 17:37:46 -0700427 apex {
428 name: "com.android.gki.fake",
429 binaries: ["foo"],
430 key: "myapex.key",
431 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000432 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800433 }
434
Paul Duffindddd5462020-04-07 15:25:44 +0100435 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900436 name: "mylib2",
437 srcs: ["mylib.cpp"],
438 system_shared_libs: [],
439 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900440 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900441 static_libs: ["libstatic"],
442 // TODO: remove //apex_available:platform
443 apex_available: [
444 "//apex_available:platform",
445 "myapex",
446 ],
447 }
448
Paul Duffindddd5462020-04-07 15:25:44 +0100449 cc_prebuilt_library_shared {
450 name: "mylib2",
451 srcs: ["prebuilt.so"],
452 // TODO: remove //apex_available:platform
453 apex_available: [
454 "//apex_available:platform",
455 "myapex",
456 ],
457 }
458
Jiyong Park9918e1a2020-03-17 19:16:40 +0900459 cc_library_static {
460 name: "libstatic",
461 srcs: ["mylib.cpp"],
462 system_shared_libs: [],
463 stl: "none",
464 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000465 // TODO: remove //apex_available:platform
466 apex_available: [
467 "//apex_available:platform",
468 "myapex",
469 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900470 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900471
472 java_library {
473 name: "myjar",
474 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900475 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900476 sdk_version: "none",
477 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900478 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900479 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000480 // TODO: remove //apex_available:platform
481 apex_available: [
482 "//apex_available:platform",
483 "myapex",
484 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900485 }
486
Jiyong Park77acec62020-06-01 21:39:15 +0900487 dex_import {
488 name: "myjar_dex",
489 jars: ["prebuilt.jar"],
490 apex_available: [
491 "//apex_available:platform",
492 "myapex",
493 ],
494 }
495
Jiyong Park7f7766d2019-07-25 22:02:35 +0900496 java_library {
497 name: "myotherjar",
498 srcs: ["foo/bar/MyClass.java"],
499 sdk_version: "none",
500 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900501 // TODO: remove //apex_available:platform
502 apex_available: [
503 "//apex_available:platform",
504 "myapex",
505 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900506 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900507
508 java_library {
509 name: "mysharedjar",
510 srcs: ["foo/bar/MyClass.java"],
511 sdk_version: "none",
512 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900513 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900514 `)
515
Paul Duffina71a67a2021-03-29 00:42:57 +0100516 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900517
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900518 // Make sure that Android.mk is created
519 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700520 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900521 var builder strings.Builder
522 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
523
524 androidMk := builder.String()
525 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
526 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
527
Jiyong Park42cca6c2019-04-01 11:15:50 +0900528 optFlags := apexRule.Args["opt_flags"]
529 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700530 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100531 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900532
Jiyong Park25fc6a92018-11-18 18:02:45 +0900533 copyCmds := apexRule.Args["copy_commands"]
534
535 // Ensure that main rule creates an output
536 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
537
538 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700539 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
540 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
541 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900542 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900543 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900544
545 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700546 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
547 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900548 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
549 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900550 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900551 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900552
553 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800554 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
555 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900556 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900557 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900558 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900559 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
560 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900561 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900562 // .. but not for java libs
563 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900564 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800565
Colin Cross7113d202019-11-20 16:39:12 -0800566 // Ensure that the platform variant ends with _shared or _common
567 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
568 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900569 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
570 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900571 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
572
573 // Ensure that dynamic dependency to java libs are not included
574 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800575
576 // Ensure that all symlinks are present.
577 found_foo_link_64 := false
578 found_foo := false
579 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900580 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800581 if strings.HasSuffix(cmd, "bin/foo") {
582 found_foo = true
583 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
584 found_foo_link_64 = true
585 }
586 }
587 }
588 good := found_foo && found_foo_link_64
589 if !good {
590 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
591 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900592
Sundong Ahnabb64432019-10-22 13:58:29 +0900593 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700594 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900595 if len(noticeInputs) != 3 {
596 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900597 }
598 ensureListContains(t, noticeInputs, "NOTICE")
599 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900600 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900601
Artur Satayeva8bd1132020-04-27 18:07:06 +0100602 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100603 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100604 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
605 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
606 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100607
608 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100609 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100610 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
611 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
612 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800613}
614
Jooyung Hanf21c7972019-12-16 22:32:06 +0900615func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800616 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900617 apex_defaults {
618 name: "myapex-defaults",
619 key: "myapex.key",
620 prebuilts: ["myetc"],
621 native_shared_libs: ["mylib"],
622 java_libs: ["myjar"],
623 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900624 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800625 bpfs: ["bpf"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000626 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900627 }
628
629 prebuilt_etc {
630 name: "myetc",
631 src: "myprebuilt",
632 }
633
634 apex {
635 name: "myapex",
636 defaults: ["myapex-defaults"],
637 }
638
639 apex_key {
640 name: "myapex.key",
641 public_key: "testkey.avbpubkey",
642 private_key: "testkey.pem",
643 }
644
645 cc_library {
646 name: "mylib",
647 system_shared_libs: [],
648 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000649 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900650 }
651
652 java_library {
653 name: "myjar",
654 srcs: ["foo/bar/MyClass.java"],
655 sdk_version: "none",
656 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000657 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900658 }
659
660 android_app {
661 name: "AppFoo",
662 srcs: ["foo/bar/MyClass.java"],
663 sdk_version: "none",
664 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000665 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900666 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900667
668 runtime_resource_overlay {
669 name: "rro",
670 theme: "blue",
671 }
672
markchien2f59ec92020-09-02 16:23:38 +0800673 bpf {
674 name: "bpf",
675 srcs: ["bpf.c", "bpf2.c"],
676 }
677
Jooyung Hanf21c7972019-12-16 22:32:06 +0900678 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000679 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900680 "etc/myetc",
681 "javalib/myjar.jar",
682 "lib64/mylib.so",
683 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900684 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800685 "etc/bpf/bpf.o",
686 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900687 })
688}
689
Jooyung Han01a3ee22019-11-02 02:52:25 +0900690func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800691 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900692 apex {
693 name: "myapex",
694 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000695 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900696 }
697
698 apex_key {
699 name: "myapex.key",
700 public_key: "testkey.avbpubkey",
701 private_key: "testkey.pem",
702 }
703 `)
704
705 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900706 args := module.Rule("apexRule").Args
707 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
708 t.Error("manifest should be apex_manifest.pb, but " + manifest)
709 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900710}
711
Alex Light5098a612018-11-29 17:12:15 -0800712func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800713 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800714 apex {
715 name: "myapex",
716 key: "myapex.key",
717 payload_type: "zip",
718 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000719 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800720 }
721
722 apex_key {
723 name: "myapex.key",
724 public_key: "testkey.avbpubkey",
725 private_key: "testkey.pem",
726 }
727
728 cc_library {
729 name: "mylib",
730 srcs: ["mylib.cpp"],
731 shared_libs: ["mylib2"],
732 system_shared_libs: [],
733 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000734 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800735 }
736
737 cc_library {
738 name: "mylib2",
739 srcs: ["mylib.cpp"],
740 system_shared_libs: [],
741 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000742 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800743 }
744 `)
745
Sundong Ahnabb64432019-10-22 13:58:29 +0900746 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800747 copyCmds := zipApexRule.Args["copy_commands"]
748
749 // Ensure that main rule creates an output
750 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
751
752 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700753 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800754
755 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700756 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800757
758 // Ensure that both direct and indirect deps are copied into apex
759 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
760 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900761}
762
763func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800764 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900765 apex {
766 name: "myapex",
767 key: "myapex.key",
768 native_shared_libs: ["mylib", "mylib3"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000769 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900770 }
771
772 apex_key {
773 name: "myapex.key",
774 public_key: "testkey.avbpubkey",
775 private_key: "testkey.pem",
776 }
777
778 cc_library {
779 name: "mylib",
780 srcs: ["mylib.cpp"],
781 shared_libs: ["mylib2", "mylib3"],
782 system_shared_libs: [],
783 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000784 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900785 }
786
787 cc_library {
788 name: "mylib2",
789 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900790 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900791 system_shared_libs: [],
792 stl: "none",
793 stubs: {
794 versions: ["1", "2", "3"],
795 },
796 }
797
798 cc_library {
799 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900800 srcs: ["mylib.cpp"],
801 shared_libs: ["mylib4"],
802 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900803 stl: "none",
804 stubs: {
805 versions: ["10", "11", "12"],
806 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000807 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900808 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900809
810 cc_library {
811 name: "mylib4",
812 srcs: ["mylib.cpp"],
813 system_shared_libs: [],
814 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000815 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900816 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900817 `)
818
Sundong Ahnabb64432019-10-22 13:58:29 +0900819 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900820 copyCmds := apexRule.Args["copy_commands"]
821
822 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800823 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900824
825 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800826 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900827
828 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800829 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900830
Colin Crossaede88c2020-08-11 12:17:01 -0700831 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900832
833 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900834 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900835 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900836 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900837
838 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Crossaede88c2020-08-11 12:17:01 -0700839 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900840 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700841 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900842
843 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900844 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900845 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900846
847 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700848 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900849
Jooyung Hana57af4a2020-01-23 05:36:59 +0000850 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900851 "lib64/mylib.so",
852 "lib64/mylib3.so",
853 "lib64/mylib4.so",
854 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900855}
856
Colin Cross7812fd32020-09-25 12:35:10 -0700857func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
858 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -0800859 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -0700860 apex {
861 name: "myapex",
862 key: "myapex.key",
863 native_shared_libs: ["mylib", "mylib3"],
864 min_sdk_version: "29",
865 }
866
867 apex_key {
868 name: "myapex.key",
869 public_key: "testkey.avbpubkey",
870 private_key: "testkey.pem",
871 }
872
873 cc_library {
874 name: "mylib",
875 srcs: ["mylib.cpp"],
876 shared_libs: ["mylib2", "mylib3"],
877 system_shared_libs: [],
878 stl: "none",
879 apex_available: [ "myapex" ],
880 min_sdk_version: "28",
881 }
882
883 cc_library {
884 name: "mylib2",
885 srcs: ["mylib.cpp"],
886 cflags: ["-include mylib.h"],
887 system_shared_libs: [],
888 stl: "none",
889 stubs: {
890 versions: ["28", "29", "30", "current"],
891 },
892 min_sdk_version: "28",
893 }
894
895 cc_library {
896 name: "mylib3",
897 srcs: ["mylib.cpp"],
898 shared_libs: ["mylib4"],
899 system_shared_libs: [],
900 stl: "none",
901 stubs: {
902 versions: ["28", "29", "30", "current"],
903 },
904 apex_available: [ "myapex" ],
905 min_sdk_version: "28",
906 }
907
908 cc_library {
909 name: "mylib4",
910 srcs: ["mylib.cpp"],
911 system_shared_libs: [],
912 stl: "none",
913 apex_available: [ "myapex" ],
914 min_sdk_version: "28",
915 }
916 `)
917
918 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
919 copyCmds := apexRule.Args["copy_commands"]
920
921 // Ensure that direct non-stubs dep is always included
922 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
923
924 // Ensure that indirect stubs dep is not included
925 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
926
927 // Ensure that direct stubs dep is included
928 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
929
930 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
931
Jiyong Park55549df2021-02-26 23:57:23 +0900932 // Ensure that mylib is linking with the latest version of stub for mylib2
933 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -0700934 // ... and not linking to the non-stub (impl) variant of mylib2
935 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
936
937 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
938 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
939 // .. and not linking to the stubs variant of mylib3
940 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
941
942 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -0700943 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -0700944 ensureNotContains(t, mylib2Cflags, "-include ")
945
946 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700947 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -0700948
949 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
950 "lib64/mylib.so",
951 "lib64/mylib3.so",
952 "lib64/mylib4.so",
953 })
954}
955
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900956func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
957 t.Parallel()
958 // myapex (Z)
959 // mylib -----------------.
960 // |
961 // otherapex (29) |
962 // libstub's versions: 29 Z current
963 // |
964 // <platform> |
965 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -0800966 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900967 apex {
968 name: "myapex",
969 key: "myapex.key",
970 native_shared_libs: ["mylib"],
971 min_sdk_version: "Z", // non-final
972 }
973
974 cc_library {
975 name: "mylib",
976 srcs: ["mylib.cpp"],
977 shared_libs: ["libstub"],
978 apex_available: ["myapex"],
979 min_sdk_version: "Z",
980 }
981
982 apex_key {
983 name: "myapex.key",
984 public_key: "testkey.avbpubkey",
985 private_key: "testkey.pem",
986 }
987
988 apex {
989 name: "otherapex",
990 key: "myapex.key",
991 native_shared_libs: ["libstub"],
992 min_sdk_version: "29",
993 }
994
995 cc_library {
996 name: "libstub",
997 srcs: ["mylib.cpp"],
998 stubs: {
999 versions: ["29", "Z", "current"],
1000 },
1001 apex_available: ["otherapex"],
1002 min_sdk_version: "29",
1003 }
1004
1005 // platform module depending on libstub from otherapex should use the latest stub("current")
1006 cc_library {
1007 name: "libplatform",
1008 srcs: ["mylib.cpp"],
1009 shared_libs: ["libstub"],
1010 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001011 `,
1012 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1013 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1014 variables.Platform_sdk_final = proptools.BoolPtr(false)
1015 variables.Platform_version_active_codenames = []string{"Z"}
1016 }),
1017 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001018
Jiyong Park55549df2021-02-26 23:57:23 +09001019 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001020 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001021 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001022 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001023 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001024
1025 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1026 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1027 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1028 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1029 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1030}
1031
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001032func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001033 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001034 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001035 name: "myapex2",
1036 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001037 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001038 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001039 }
1040
1041 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001042 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001043 public_key: "testkey.avbpubkey",
1044 private_key: "testkey.pem",
1045 }
1046
1047 cc_library {
1048 name: "mylib",
1049 srcs: ["mylib.cpp"],
1050 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001051 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001052 system_shared_libs: [],
1053 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001054 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001055 }
1056
1057 cc_library {
1058 name: "libfoo",
1059 srcs: ["mylib.cpp"],
1060 shared_libs: ["libbar"],
1061 system_shared_libs: [],
1062 stl: "none",
1063 stubs: {
1064 versions: ["10", "20", "30"],
1065 },
1066 }
1067
1068 cc_library {
1069 name: "libbar",
1070 srcs: ["mylib.cpp"],
1071 system_shared_libs: [],
1072 stl: "none",
1073 }
1074
Jiyong Park678c8812020-02-07 17:25:49 +09001075 cc_library_static {
1076 name: "libbaz",
1077 srcs: ["mylib.cpp"],
1078 system_shared_libs: [],
1079 stl: "none",
1080 apex_available: [ "myapex2" ],
1081 }
1082
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001083 `)
1084
Jiyong Park83dc74b2020-01-14 18:38:44 +09001085 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001086 copyCmds := apexRule.Args["copy_commands"]
1087
1088 // Ensure that direct non-stubs dep is always included
1089 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1090
1091 // Ensure that indirect stubs dep is not included
1092 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1093
1094 // Ensure that dependency of stubs is not included
1095 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1096
Colin Crossaede88c2020-08-11 12:17:01 -07001097 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001098
1099 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001100 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001101 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001102 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001103
Jiyong Park3ff16992019-12-27 14:11:47 +09001104 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001105
1106 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1107 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001108
Artur Satayeva8bd1132020-04-27 18:07:06 +01001109 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001110 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001111
Artur Satayeva8bd1132020-04-27 18:07:06 +01001112 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001113 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001114}
1115
Jooyung Hand3639552019-08-09 12:57:43 +09001116func TestApexWithRuntimeLibsDependency(t *testing.T) {
1117 /*
1118 myapex
1119 |
1120 v (runtime_libs)
1121 mylib ------+------> libfoo [provides stub]
1122 |
1123 `------> libbar
1124 */
Colin Cross1c460562021-02-16 17:55:47 -08001125 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001126 apex {
1127 name: "myapex",
1128 key: "myapex.key",
1129 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001130 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001131 }
1132
1133 apex_key {
1134 name: "myapex.key",
1135 public_key: "testkey.avbpubkey",
1136 private_key: "testkey.pem",
1137 }
1138
1139 cc_library {
1140 name: "mylib",
1141 srcs: ["mylib.cpp"],
1142 runtime_libs: ["libfoo", "libbar"],
1143 system_shared_libs: [],
1144 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001145 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001146 }
1147
1148 cc_library {
1149 name: "libfoo",
1150 srcs: ["mylib.cpp"],
1151 system_shared_libs: [],
1152 stl: "none",
1153 stubs: {
1154 versions: ["10", "20", "30"],
1155 },
1156 }
1157
1158 cc_library {
1159 name: "libbar",
1160 srcs: ["mylib.cpp"],
1161 system_shared_libs: [],
1162 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001163 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001164 }
1165
1166 `)
1167
Sundong Ahnabb64432019-10-22 13:58:29 +09001168 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001169 copyCmds := apexRule.Args["copy_commands"]
1170
1171 // Ensure that direct non-stubs dep is always included
1172 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1173
1174 // Ensure that indirect stubs dep is not included
1175 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1176
1177 // Ensure that runtime_libs dep in included
1178 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1179
Sundong Ahnabb64432019-10-22 13:58:29 +09001180 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001181 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1182 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001183
1184}
1185
Paul Duffina02cae32021-03-09 01:44:06 +00001186var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1187 cc.PrepareForTestWithCcBuildComponents,
1188 PrepareForTestWithApexBuildComponents,
1189 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001190 apex {
1191 name: "com.android.runtime",
1192 key: "com.android.runtime.key",
1193 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001194 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001195 }
1196
1197 apex_key {
1198 name: "com.android.runtime.key",
1199 public_key: "testkey.avbpubkey",
1200 private_key: "testkey.pem",
1201 }
Paul Duffina02cae32021-03-09 01:44:06 +00001202 `),
1203 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1204)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001205
Paul Duffina02cae32021-03-09 01:44:06 +00001206func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001207 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001208 cc_library {
1209 name: "libc",
1210 no_libcrt: true,
1211 nocrt: true,
1212 stl: "none",
1213 system_shared_libs: [],
1214 stubs: { versions: ["1"] },
1215 apex_available: ["com.android.runtime"],
1216
1217 sanitize: {
1218 hwaddress: true,
1219 }
1220 }
1221
1222 cc_prebuilt_library_shared {
1223 name: "libclang_rt.hwasan-aarch64-android",
1224 no_libcrt: true,
1225 nocrt: true,
1226 stl: "none",
1227 system_shared_libs: [],
1228 srcs: [""],
1229 stubs: { versions: ["1"] },
1230
1231 sanitize: {
1232 never: true,
1233 },
Paul Duffina02cae32021-03-09 01:44:06 +00001234 } `)
1235 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001236
1237 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1238 "lib64/bionic/libc.so",
1239 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1240 })
1241
1242 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1243
1244 installed := hwasan.Description("install libclang_rt.hwasan")
1245 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1246
1247 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1248 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1249 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1250}
1251
1252func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001253 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001254 prepareForTestOfRuntimeApexWithHwasan,
1255 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1256 variables.SanitizeDevice = []string{"hwaddress"}
1257 }),
1258 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001259 cc_library {
1260 name: "libc",
1261 no_libcrt: true,
1262 nocrt: true,
1263 stl: "none",
1264 system_shared_libs: [],
1265 stubs: { versions: ["1"] },
1266 apex_available: ["com.android.runtime"],
1267 }
1268
1269 cc_prebuilt_library_shared {
1270 name: "libclang_rt.hwasan-aarch64-android",
1271 no_libcrt: true,
1272 nocrt: true,
1273 stl: "none",
1274 system_shared_libs: [],
1275 srcs: [""],
1276 stubs: { versions: ["1"] },
1277
1278 sanitize: {
1279 never: true,
1280 },
1281 }
Paul Duffina02cae32021-03-09 01:44:06 +00001282 `)
1283 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001284
1285 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1286 "lib64/bionic/libc.so",
1287 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1288 })
1289
1290 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1291
1292 installed := hwasan.Description("install libclang_rt.hwasan")
1293 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1294
1295 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1296 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1297 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1298}
1299
Jooyung Han61b66e92020-03-21 14:21:46 +00001300func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1301 testcases := []struct {
1302 name string
1303 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001304 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001305 shouldLink string
1306 shouldNotLink []string
1307 }{
1308 {
Jiyong Park55549df2021-02-26 23:57:23 +09001309 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001310 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001311 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001312 shouldLink: "current",
1313 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001314 },
1315 {
Jiyong Park55549df2021-02-26 23:57:23 +09001316 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001317 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001318 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001319 shouldLink: "current",
1320 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001321 },
1322 }
1323 for _, tc := range testcases {
1324 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001325 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001326 apex {
1327 name: "myapex",
1328 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001329 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001330 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001331 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001332 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001333
Jooyung Han61b66e92020-03-21 14:21:46 +00001334 apex_key {
1335 name: "myapex.key",
1336 public_key: "testkey.avbpubkey",
1337 private_key: "testkey.pem",
1338 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001339
Jooyung Han61b66e92020-03-21 14:21:46 +00001340 cc_library {
1341 name: "mylib",
1342 srcs: ["mylib.cpp"],
1343 vendor_available: true,
1344 shared_libs: ["libbar"],
1345 system_shared_libs: [],
1346 stl: "none",
1347 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001348 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001349 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001350
Jooyung Han61b66e92020-03-21 14:21:46 +00001351 cc_library {
1352 name: "libbar",
1353 srcs: ["mylib.cpp"],
1354 system_shared_libs: [],
1355 stl: "none",
1356 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001357 llndk: {
1358 symbol_file: "libbar.map.txt",
1359 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001360 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001361 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001362 withUnbundledBuild,
1363 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001364
Jooyung Han61b66e92020-03-21 14:21:46 +00001365 // Ensure that LLNDK dep is not included
1366 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1367 "lib64/mylib.so",
1368 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001369
Jooyung Han61b66e92020-03-21 14:21:46 +00001370 // Ensure that LLNDK dep is required
1371 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1372 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1373 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001374
Steven Moreland2c4000c2021-04-27 02:08:49 +00001375 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1376 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001377 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001378 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001379 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001380
Steven Moreland2c4000c2021-04-27 02:08:49 +00001381 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001382 ver := tc.shouldLink
1383 if tc.shouldLink == "current" {
1384 ver = strconv.Itoa(android.FutureApiLevelInt)
1385 }
1386 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001387 })
1388 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001389}
1390
Jiyong Park25fc6a92018-11-18 18:02:45 +09001391func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001392 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001393 apex {
1394 name: "myapex",
1395 key: "myapex.key",
1396 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001397 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001398 }
1399
1400 apex_key {
1401 name: "myapex.key",
1402 public_key: "testkey.avbpubkey",
1403 private_key: "testkey.pem",
1404 }
1405
1406 cc_library {
1407 name: "mylib",
1408 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001409 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001410 shared_libs: ["libdl#27"],
1411 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001412 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001413 }
1414
1415 cc_library_shared {
1416 name: "mylib_shared",
1417 srcs: ["mylib.cpp"],
1418 shared_libs: ["libdl#27"],
1419 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001420 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001421 }
1422
1423 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001424 name: "libBootstrap",
1425 srcs: ["mylib.cpp"],
1426 stl: "none",
1427 bootstrap: true,
1428 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001429 `)
1430
Sundong Ahnabb64432019-10-22 13:58:29 +09001431 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001432 copyCmds := apexRule.Args["copy_commands"]
1433
1434 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001435 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001436 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1437 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001438
1439 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001440 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001441
Colin Crossaede88c2020-08-11 12:17:01 -07001442 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1443 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1444 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001445
1446 // For dependency to libc
1447 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001448 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001449 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001450 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001451 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001452 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1453 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001454
1455 // For dependency to libm
1456 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001457 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001458 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001459 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001460 // ... and is not compiling with the stub
1461 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1462 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1463
1464 // For dependency to libdl
1465 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001466 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001467 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001468 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1469 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001470 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001471 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001472 // ... Cflags from stub is correctly exported to mylib
1473 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1474 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001475
1476 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001477 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1478 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1479 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1480 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001481}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001482
Jooyung Han749dc692020-04-15 11:03:39 +09001483func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001484 // there are three links between liba --> libz.
1485 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001486 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001487 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001488 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001489 apex {
1490 name: "myapex",
1491 key: "myapex.key",
1492 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001493 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001494 }
1495
1496 apex {
1497 name: "otherapex",
1498 key: "myapex.key",
1499 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001500 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001501 }
1502
1503 apex_key {
1504 name: "myapex.key",
1505 public_key: "testkey.avbpubkey",
1506 private_key: "testkey.pem",
1507 }
1508
1509 cc_library {
1510 name: "libx",
1511 shared_libs: ["liba"],
1512 system_shared_libs: [],
1513 stl: "none",
1514 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001515 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001516 }
1517
1518 cc_library {
1519 name: "liby",
1520 shared_libs: ["liba"],
1521 system_shared_libs: [],
1522 stl: "none",
1523 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001524 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001525 }
1526
1527 cc_library {
1528 name: "liba",
1529 shared_libs: ["libz"],
1530 system_shared_libs: [],
1531 stl: "none",
1532 apex_available: [
1533 "//apex_available:anyapex",
1534 "//apex_available:platform",
1535 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001536 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001537 }
1538
1539 cc_library {
1540 name: "libz",
1541 system_shared_libs: [],
1542 stl: "none",
1543 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001544 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001545 },
1546 }
Jooyung Han749dc692020-04-15 11:03:39 +09001547 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001548
1549 expectLink := func(from, from_variant, to, to_variant string) {
1550 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1551 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1552 }
1553 expectNoLink := func(from, from_variant, to, to_variant string) {
1554 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1555 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1556 }
1557 // platform liba is linked to non-stub version
1558 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001559 // liba in myapex is linked to current
1560 expectLink("liba", "shared_apex29", "libz", "shared_current")
1561 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001562 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001563 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001564 // liba in otherapex is linked to current
1565 expectLink("liba", "shared_apex30", "libz", "shared_current")
1566 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001567 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1568 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001569}
1570
Jooyung Hanaed150d2020-04-02 01:41:41 +09001571func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001572 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001573 apex {
1574 name: "myapex",
1575 key: "myapex.key",
1576 native_shared_libs: ["libx"],
1577 min_sdk_version: "R",
1578 }
1579
1580 apex_key {
1581 name: "myapex.key",
1582 public_key: "testkey.avbpubkey",
1583 private_key: "testkey.pem",
1584 }
1585
1586 cc_library {
1587 name: "libx",
1588 shared_libs: ["libz"],
1589 system_shared_libs: [],
1590 stl: "none",
1591 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001592 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001593 }
1594
1595 cc_library {
1596 name: "libz",
1597 system_shared_libs: [],
1598 stl: "none",
1599 stubs: {
1600 versions: ["29", "R"],
1601 },
1602 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001603 `,
1604 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1605 variables.Platform_version_active_codenames = []string{"R"}
1606 }),
1607 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001608
1609 expectLink := func(from, from_variant, to, to_variant string) {
1610 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1611 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1612 }
1613 expectNoLink := func(from, from_variant, to, to_variant string) {
1614 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1615 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1616 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001617 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1618 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001619 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1620 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001621}
1622
Jooyung Han749dc692020-04-15 11:03:39 +09001623func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001624 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001625 apex {
1626 name: "myapex",
1627 key: "myapex.key",
1628 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001629 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001630 }
1631
1632 apex_key {
1633 name: "myapex.key",
1634 public_key: "testkey.avbpubkey",
1635 private_key: "testkey.pem",
1636 }
1637
1638 cc_library {
1639 name: "libx",
1640 shared_libs: ["libz"],
1641 system_shared_libs: [],
1642 stl: "none",
1643 apex_available: [ "myapex" ],
1644 }
1645
1646 cc_library {
1647 name: "libz",
1648 system_shared_libs: [],
1649 stl: "none",
1650 stubs: {
1651 versions: ["1", "2"],
1652 },
1653 }
1654 `)
1655
1656 expectLink := func(from, from_variant, to, to_variant string) {
1657 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1658 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1659 }
1660 expectNoLink := func(from, from_variant, to, to_variant string) {
1661 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1662 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1663 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001664 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001665 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001666 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001667 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001668}
1669
1670func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001671 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001672 apex {
1673 name: "myapex",
1674 key: "myapex.key",
1675 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001676 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001677 }
1678
1679 apex_key {
1680 name: "myapex.key",
1681 public_key: "testkey.avbpubkey",
1682 private_key: "testkey.pem",
1683 }
1684
1685 cc_library {
1686 name: "libx",
1687 system_shared_libs: [],
1688 stl: "none",
1689 apex_available: [ "myapex" ],
1690 stubs: {
1691 versions: ["1", "2"],
1692 },
1693 }
1694
1695 cc_library {
1696 name: "libz",
1697 shared_libs: ["libx"],
1698 system_shared_libs: [],
1699 stl: "none",
1700 }
1701 `)
1702
1703 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001704 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001705 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1706 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1707 }
1708 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001709 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001710 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1711 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1712 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001713 expectLink("libz", "shared", "libx", "shared_current")
1714 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09001715 expectNoLink("libz", "shared", "libz", "shared_1")
1716 expectNoLink("libz", "shared", "libz", "shared")
1717}
1718
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001719var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1720 func(variables android.FixtureProductVariables) {
1721 variables.SanitizeDevice = []string{"hwaddress"}
1722 },
1723)
1724
Jooyung Han75568392020-03-20 04:29:24 +09001725func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001726 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001727 apex {
1728 name: "myapex",
1729 key: "myapex.key",
1730 native_shared_libs: ["libx"],
1731 min_sdk_version: "29",
1732 }
1733
1734 apex_key {
1735 name: "myapex.key",
1736 public_key: "testkey.avbpubkey",
1737 private_key: "testkey.pem",
1738 }
1739
1740 cc_library {
1741 name: "libx",
1742 shared_libs: ["libbar"],
1743 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001744 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001745 }
1746
1747 cc_library {
1748 name: "libbar",
1749 stubs: {
1750 versions: ["29", "30"],
1751 },
1752 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001753 `,
1754 prepareForTestWithSantitizeHwaddress,
1755 )
Jooyung Han03b51852020-02-26 22:45:42 +09001756 expectLink := func(from, from_variant, to, to_variant string) {
1757 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1758 libFlags := ld.Args["libFlags"]
1759 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1760 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001761 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09001762}
1763
Jooyung Han75568392020-03-20 04:29:24 +09001764func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001765 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001766 apex {
1767 name: "myapex",
1768 key: "myapex.key",
1769 native_shared_libs: ["libx"],
1770 min_sdk_version: "29",
1771 }
1772
1773 apex_key {
1774 name: "myapex.key",
1775 public_key: "testkey.avbpubkey",
1776 private_key: "testkey.pem",
1777 }
1778
1779 cc_library {
1780 name: "libx",
1781 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001782 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001783 }
Jooyung Han75568392020-03-20 04:29:24 +09001784 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001785
1786 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001787 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001788 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001789 // note that platform variant is not.
1790 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001791 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001792}
1793
Jooyung Han749dc692020-04-15 11:03:39 +09001794func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1795 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001796 apex {
1797 name: "myapex",
1798 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001799 native_shared_libs: ["mylib"],
1800 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001801 }
1802
1803 apex_key {
1804 name: "myapex.key",
1805 public_key: "testkey.avbpubkey",
1806 private_key: "testkey.pem",
1807 }
Jooyung Han749dc692020-04-15 11:03:39 +09001808
1809 cc_library {
1810 name: "mylib",
1811 srcs: ["mylib.cpp"],
1812 system_shared_libs: [],
1813 stl: "none",
1814 apex_available: [
1815 "myapex",
1816 ],
1817 min_sdk_version: "30",
1818 }
1819 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001820
1821 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
1822 apex {
1823 name: "myapex",
1824 key: "myapex.key",
1825 native_shared_libs: ["libfoo.ffi"],
1826 min_sdk_version: "29",
1827 }
1828
1829 apex_key {
1830 name: "myapex.key",
1831 public_key: "testkey.avbpubkey",
1832 private_key: "testkey.pem",
1833 }
1834
1835 rust_ffi_shared {
1836 name: "libfoo.ffi",
1837 srcs: ["foo.rs"],
1838 crate_name: "foo",
1839 apex_available: [
1840 "myapex",
1841 ],
1842 min_sdk_version: "30",
1843 }
1844 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001845
1846 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
1847 apex {
1848 name: "myapex",
1849 key: "myapex.key",
1850 java_libs: ["libfoo"],
1851 min_sdk_version: "29",
1852 }
1853
1854 apex_key {
1855 name: "myapex.key",
1856 public_key: "testkey.avbpubkey",
1857 private_key: "testkey.pem",
1858 }
1859
1860 java_import {
1861 name: "libfoo",
1862 jars: ["libfoo.jar"],
1863 apex_available: [
1864 "myapex",
1865 ],
1866 min_sdk_version: "30",
1867 }
1868 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001869}
1870
1871func TestApexMinSdkVersion_Okay(t *testing.T) {
1872 testApex(t, `
1873 apex {
1874 name: "myapex",
1875 key: "myapex.key",
1876 native_shared_libs: ["libfoo"],
1877 java_libs: ["libbar"],
1878 min_sdk_version: "29",
1879 }
1880
1881 apex_key {
1882 name: "myapex.key",
1883 public_key: "testkey.avbpubkey",
1884 private_key: "testkey.pem",
1885 }
1886
1887 cc_library {
1888 name: "libfoo",
1889 srcs: ["mylib.cpp"],
1890 shared_libs: ["libfoo_dep"],
1891 apex_available: ["myapex"],
1892 min_sdk_version: "29",
1893 }
1894
1895 cc_library {
1896 name: "libfoo_dep",
1897 srcs: ["mylib.cpp"],
1898 apex_available: ["myapex"],
1899 min_sdk_version: "29",
1900 }
1901
1902 java_library {
1903 name: "libbar",
1904 sdk_version: "current",
1905 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001906 static_libs: [
1907 "libbar_dep",
1908 "libbar_import_dep",
1909 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001910 apex_available: ["myapex"],
1911 min_sdk_version: "29",
1912 }
1913
1914 java_library {
1915 name: "libbar_dep",
1916 sdk_version: "current",
1917 srcs: ["a.java"],
1918 apex_available: ["myapex"],
1919 min_sdk_version: "29",
1920 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001921
1922 java_import {
1923 name: "libbar_import_dep",
1924 jars: ["libbar.jar"],
1925 apex_available: ["myapex"],
1926 min_sdk_version: "29",
1927 }
Jooyung Han03b51852020-02-26 22:45:42 +09001928 `)
1929}
1930
Artur Satayev8cf899a2020-04-15 17:29:42 +01001931func TestJavaStableSdkVersion(t *testing.T) {
1932 testCases := []struct {
1933 name string
1934 expectedError string
1935 bp string
1936 }{
1937 {
1938 name: "Non-updatable apex with non-stable dep",
1939 bp: `
1940 apex {
1941 name: "myapex",
1942 java_libs: ["myjar"],
1943 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001944 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01001945 }
1946 apex_key {
1947 name: "myapex.key",
1948 public_key: "testkey.avbpubkey",
1949 private_key: "testkey.pem",
1950 }
1951 java_library {
1952 name: "myjar",
1953 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001954 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001955 apex_available: ["myapex"],
1956 }
1957 `,
1958 },
1959 {
1960 name: "Updatable apex with stable dep",
1961 bp: `
1962 apex {
1963 name: "myapex",
1964 java_libs: ["myjar"],
1965 key: "myapex.key",
1966 updatable: true,
1967 min_sdk_version: "29",
1968 }
1969 apex_key {
1970 name: "myapex.key",
1971 public_key: "testkey.avbpubkey",
1972 private_key: "testkey.pem",
1973 }
1974 java_library {
1975 name: "myjar",
1976 srcs: ["foo/bar/MyClass.java"],
1977 sdk_version: "current",
1978 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001979 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001980 }
1981 `,
1982 },
1983 {
1984 name: "Updatable apex with non-stable dep",
1985 expectedError: "cannot depend on \"myjar\"",
1986 bp: `
1987 apex {
1988 name: "myapex",
1989 java_libs: ["myjar"],
1990 key: "myapex.key",
1991 updatable: true,
1992 }
1993 apex_key {
1994 name: "myapex.key",
1995 public_key: "testkey.avbpubkey",
1996 private_key: "testkey.pem",
1997 }
1998 java_library {
1999 name: "myjar",
2000 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002001 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002002 apex_available: ["myapex"],
2003 }
2004 `,
2005 },
2006 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002007 name: "Updatable apex with non-stable transitive dep",
2008 // This is not actually detecting that the transitive dependency is unstable, rather it is
2009 // detecting that the transitive dependency is building against a wider API surface than the
2010 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002011 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002012 bp: `
2013 apex {
2014 name: "myapex",
2015 java_libs: ["myjar"],
2016 key: "myapex.key",
2017 updatable: true,
2018 }
2019 apex_key {
2020 name: "myapex.key",
2021 public_key: "testkey.avbpubkey",
2022 private_key: "testkey.pem",
2023 }
2024 java_library {
2025 name: "myjar",
2026 srcs: ["foo/bar/MyClass.java"],
2027 sdk_version: "current",
2028 apex_available: ["myapex"],
2029 static_libs: ["transitive-jar"],
2030 }
2031 java_library {
2032 name: "transitive-jar",
2033 srcs: ["foo/bar/MyClass.java"],
2034 sdk_version: "core_platform",
2035 apex_available: ["myapex"],
2036 }
2037 `,
2038 },
2039 }
2040
2041 for _, test := range testCases {
2042 t.Run(test.name, func(t *testing.T) {
2043 if test.expectedError == "" {
2044 testApex(t, test.bp)
2045 } else {
2046 testApexError(t, test.expectedError, test.bp)
2047 }
2048 })
2049 }
2050}
2051
Jooyung Han749dc692020-04-15 11:03:39 +09002052func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2053 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2054 apex {
2055 name: "myapex",
2056 key: "myapex.key",
2057 native_shared_libs: ["mylib"],
2058 min_sdk_version: "29",
2059 }
2060
2061 apex_key {
2062 name: "myapex.key",
2063 public_key: "testkey.avbpubkey",
2064 private_key: "testkey.pem",
2065 }
2066
2067 cc_library {
2068 name: "mylib",
2069 srcs: ["mylib.cpp"],
2070 shared_libs: ["mylib2"],
2071 system_shared_libs: [],
2072 stl: "none",
2073 apex_available: [
2074 "myapex",
2075 ],
2076 min_sdk_version: "29",
2077 }
2078
2079 // indirect part of the apex
2080 cc_library {
2081 name: "mylib2",
2082 srcs: ["mylib.cpp"],
2083 system_shared_libs: [],
2084 stl: "none",
2085 apex_available: [
2086 "myapex",
2087 ],
2088 min_sdk_version: "30",
2089 }
2090 `)
2091}
2092
2093func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2094 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2095 apex {
2096 name: "myapex",
2097 key: "myapex.key",
2098 apps: ["AppFoo"],
2099 min_sdk_version: "29",
2100 }
2101
2102 apex_key {
2103 name: "myapex.key",
2104 public_key: "testkey.avbpubkey",
2105 private_key: "testkey.pem",
2106 }
2107
2108 android_app {
2109 name: "AppFoo",
2110 srcs: ["foo/bar/MyClass.java"],
2111 sdk_version: "current",
2112 min_sdk_version: "29",
2113 system_modules: "none",
2114 stl: "none",
2115 static_libs: ["bar"],
2116 apex_available: [ "myapex" ],
2117 }
2118
2119 java_library {
2120 name: "bar",
2121 sdk_version: "current",
2122 srcs: ["a.java"],
2123 apex_available: [ "myapex" ],
2124 }
2125 `)
2126}
2127
2128func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002129 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002130 apex {
2131 name: "myapex",
2132 key: "myapex.key",
2133 native_shared_libs: ["mylib"],
2134 min_sdk_version: "29",
2135 }
2136
2137 apex_key {
2138 name: "myapex.key",
2139 public_key: "testkey.avbpubkey",
2140 private_key: "testkey.pem",
2141 }
2142
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002143 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002144 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2145 cc_library {
2146 name: "mylib",
2147 srcs: ["mylib.cpp"],
2148 shared_libs: ["mylib2"],
2149 system_shared_libs: [],
2150 stl: "none",
2151 apex_available: ["myapex", "otherapex"],
2152 min_sdk_version: "29",
2153 }
2154
2155 cc_library {
2156 name: "mylib2",
2157 srcs: ["mylib.cpp"],
2158 system_shared_libs: [],
2159 stl: "none",
2160 apex_available: ["otherapex"],
2161 stubs: { versions: ["29", "30"] },
2162 min_sdk_version: "30",
2163 }
2164
2165 apex {
2166 name: "otherapex",
2167 key: "myapex.key",
2168 native_shared_libs: ["mylib", "mylib2"],
2169 min_sdk_version: "30",
2170 }
2171 `)
2172 expectLink := func(from, from_variant, to, to_variant string) {
2173 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2174 libFlags := ld.Args["libFlags"]
2175 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2176 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002177 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002178 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002179}
2180
Jooyung Haned124c32021-01-26 11:43:46 +09002181func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002182 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2183 func(variables android.FixtureProductVariables) {
2184 variables.Platform_sdk_codename = proptools.StringPtr("S")
2185 variables.Platform_version_active_codenames = []string{"S"}
2186 },
2187 )
Jooyung Haned124c32021-01-26 11:43:46 +09002188 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2189 apex {
2190 name: "myapex",
2191 key: "myapex.key",
2192 native_shared_libs: ["libfoo"],
2193 min_sdk_version: "S",
2194 }
2195 apex_key {
2196 name: "myapex.key",
2197 public_key: "testkey.avbpubkey",
2198 private_key: "testkey.pem",
2199 }
2200 cc_library {
2201 name: "libfoo",
2202 shared_libs: ["libbar"],
2203 apex_available: ["myapex"],
2204 min_sdk_version: "29",
2205 }
2206 cc_library {
2207 name: "libbar",
2208 apex_available: ["myapex"],
2209 }
2210 `, withSAsActiveCodeNames)
2211}
2212
2213func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002214 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2215 variables.Platform_sdk_codename = proptools.StringPtr("S")
2216 variables.Platform_version_active_codenames = []string{"S", "T"}
2217 })
Colin Cross1c460562021-02-16 17:55:47 -08002218 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002219 apex {
2220 name: "myapex",
2221 key: "myapex.key",
2222 native_shared_libs: ["libfoo"],
2223 min_sdk_version: "S",
2224 }
2225 apex_key {
2226 name: "myapex.key",
2227 public_key: "testkey.avbpubkey",
2228 private_key: "testkey.pem",
2229 }
2230 cc_library {
2231 name: "libfoo",
2232 shared_libs: ["libbar"],
2233 apex_available: ["myapex"],
2234 min_sdk_version: "S",
2235 }
2236 cc_library {
2237 name: "libbar",
2238 stubs: {
2239 symbol_file: "libbar.map.txt",
2240 versions: ["30", "S", "T"],
2241 },
2242 }
2243 `, withSAsActiveCodeNames)
2244
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002245 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002246 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2247 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002248 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002249}
2250
Jiyong Park7c2ee712018-12-07 00:42:25 +09002251func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002252 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002253 apex {
2254 name: "myapex",
2255 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002256 native_shared_libs: ["mylib"],
2257 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002258 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002259 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002260 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002261 }
2262
2263 apex_key {
2264 name: "myapex.key",
2265 public_key: "testkey.avbpubkey",
2266 private_key: "testkey.pem",
2267 }
2268
2269 prebuilt_etc {
2270 name: "myetc",
2271 src: "myprebuilt",
2272 sub_dir: "foo/bar",
2273 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002274
2275 cc_library {
2276 name: "mylib",
2277 srcs: ["mylib.cpp"],
2278 relative_install_path: "foo/bar",
2279 system_shared_libs: [],
2280 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002281 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002282 }
2283
2284 cc_binary {
2285 name: "mybin",
2286 srcs: ["mylib.cpp"],
2287 relative_install_path: "foo/bar",
2288 system_shared_libs: [],
2289 static_executable: true,
2290 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002291 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002292 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002293 `)
2294
Sundong Ahnabb64432019-10-22 13:58:29 +09002295 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002296 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2297
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002298 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002299 ensureListContains(t, dirs, "etc")
2300 ensureListContains(t, dirs, "etc/foo")
2301 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002302 ensureListContains(t, dirs, "lib64")
2303 ensureListContains(t, dirs, "lib64/foo")
2304 ensureListContains(t, dirs, "lib64/foo/bar")
2305 ensureListContains(t, dirs, "lib")
2306 ensureListContains(t, dirs, "lib/foo")
2307 ensureListContains(t, dirs, "lib/foo/bar")
2308
Jiyong Parkbd13e442019-03-15 18:10:35 +09002309 ensureListContains(t, dirs, "bin")
2310 ensureListContains(t, dirs, "bin/foo")
2311 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002312}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002313
Jooyung Han35155c42020-02-06 17:33:20 +09002314func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002315 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002316 apex {
2317 name: "myapex",
2318 key: "myapex.key",
2319 multilib: {
2320 both: {
2321 native_shared_libs: ["mylib"],
2322 binaries: ["mybin"],
2323 },
2324 },
2325 compile_multilib: "both",
2326 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002327 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002328 }
2329
2330 apex_key {
2331 name: "myapex.key",
2332 public_key: "testkey.avbpubkey",
2333 private_key: "testkey.pem",
2334 }
2335
2336 cc_library {
2337 name: "mylib",
2338 relative_install_path: "foo/bar",
2339 system_shared_libs: [],
2340 stl: "none",
2341 apex_available: [ "myapex" ],
2342 native_bridge_supported: true,
2343 }
2344
2345 cc_binary {
2346 name: "mybin",
2347 relative_install_path: "foo/bar",
2348 system_shared_libs: [],
2349 static_executable: true,
2350 stl: "none",
2351 apex_available: [ "myapex" ],
2352 native_bridge_supported: true,
2353 compile_multilib: "both", // default is "first" for binary
2354 multilib: {
2355 lib64: {
2356 suffix: "64",
2357 },
2358 },
2359 }
2360 `, withNativeBridgeEnabled)
2361 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2362 "bin/foo/bar/mybin",
2363 "bin/foo/bar/mybin64",
2364 "bin/arm/foo/bar/mybin",
2365 "bin/arm64/foo/bar/mybin64",
2366 "lib/foo/bar/mylib.so",
2367 "lib/arm/foo/bar/mylib.so",
2368 "lib64/foo/bar/mylib.so",
2369 "lib64/arm64/foo/bar/mylib.so",
2370 })
2371}
2372
Jooyung Han85d61762020-06-24 23:50:26 +09002373func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002374 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002375 apex {
2376 name: "myapex",
2377 key: "myapex.key",
2378 binaries: ["mybin"],
2379 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002380 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002381 }
2382 apex_key {
2383 name: "myapex.key",
2384 public_key: "testkey.avbpubkey",
2385 private_key: "testkey.pem",
2386 }
2387 cc_binary {
2388 name: "mybin",
2389 vendor: true,
2390 shared_libs: ["libfoo"],
2391 }
2392 cc_library {
2393 name: "libfoo",
2394 proprietary: true,
2395 }
2396 `)
2397
2398 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2399 "bin/mybin",
2400 "lib64/libfoo.so",
2401 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2402 "lib64/libc++.so",
2403 })
2404
2405 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002406 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002407 name := apexBundle.BaseModuleName()
2408 prefix := "TARGET_"
2409 var builder strings.Builder
2410 data.Custom(&builder, name, prefix, "", data)
Paul Duffin37ba3442021-03-29 00:21:08 +01002411 androidMk := android.StringRelativeToTop(ctx.Config(), builder.String())
2412 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002413 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002414
2415 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2416 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2417 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002418}
2419
Jooyung Handf78e212020-07-22 15:54:47 +09002420func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002421 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002422 apex {
2423 name: "myapex",
2424 key: "myapex.key",
2425 binaries: ["mybin"],
2426 vendor: true,
2427 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002428 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002429 }
2430 apex_key {
2431 name: "myapex.key",
2432 public_key: "testkey.avbpubkey",
2433 private_key: "testkey.pem",
2434 }
2435 cc_binary {
2436 name: "mybin",
2437 vendor: true,
2438 shared_libs: ["libvndk", "libvendor"],
2439 }
2440 cc_library {
2441 name: "libvndk",
2442 vndk: {
2443 enabled: true,
2444 },
2445 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002446 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002447 }
2448 cc_library {
2449 name: "libvendor",
2450 vendor: true,
2451 }
2452 `)
2453
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002454 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002455
Paul Duffina71a67a2021-03-29 00:42:57 +01002456 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002457 libs := names(ldRule.Args["libFlags"])
2458 // VNDK libs(libvndk/libc++) as they are
Paul Duffin37ba3442021-03-29 00:21:08 +01002459 ensureListContains(t, libs, "out/soong/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
2460 ensureListContains(t, libs, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002461 // non-stable Vendor libs as APEX variants
Paul Duffin37ba3442021-03-29 00:21:08 +01002462 ensureListContains(t, libs, "out/soong/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002463
2464 // VNDK libs are not included when use_vndk_as_stable: true
2465 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2466 "bin/mybin",
2467 "lib64/libvendor.so",
2468 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002469
2470 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2471 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2472 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002473}
2474
Justin Yun13decfb2021-03-08 19:25:55 +09002475func TestProductVariant(t *testing.T) {
2476 ctx := testApex(t, `
2477 apex {
2478 name: "myapex",
2479 key: "myapex.key",
2480 updatable: false,
2481 product_specific: true,
2482 binaries: ["foo"],
2483 }
2484
2485 apex_key {
2486 name: "myapex.key",
2487 public_key: "testkey.avbpubkey",
2488 private_key: "testkey.pem",
2489 }
2490
2491 cc_binary {
2492 name: "foo",
2493 product_available: true,
2494 apex_available: ["myapex"],
2495 srcs: ["foo.cpp"],
2496 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002497 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2498 variables.ProductVndkVersion = proptools.StringPtr("current")
2499 }),
2500 )
Justin Yun13decfb2021-03-08 19:25:55 +09002501
2502 cflags := strings.Fields(
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002503 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002504 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2505 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2506 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2507 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2508}
2509
Jooyung Han8e5685d2020-09-21 11:02:57 +09002510func TestApex_withPrebuiltFirmware(t *testing.T) {
2511 testCases := []struct {
2512 name string
2513 additionalProp string
2514 }{
2515 {"system apex with prebuilt_firmware", ""},
2516 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2517 }
2518 for _, tc := range testCases {
2519 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002520 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002521 apex {
2522 name: "myapex",
2523 key: "myapex.key",
2524 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002525 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002526 `+tc.additionalProp+`
2527 }
2528 apex_key {
2529 name: "myapex.key",
2530 public_key: "testkey.avbpubkey",
2531 private_key: "testkey.pem",
2532 }
2533 prebuilt_firmware {
2534 name: "myfirmware",
2535 src: "myfirmware.bin",
2536 filename_from_src: true,
2537 `+tc.additionalProp+`
2538 }
2539 `)
2540 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2541 "etc/firmware/myfirmware.bin",
2542 })
2543 })
2544 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002545}
2546
Jooyung Hanefb184e2020-06-25 17:14:25 +09002547func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002548 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002549 apex {
2550 name: "myapex",
2551 key: "myapex.key",
2552 vendor: true,
2553 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002554 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002555 }
2556
2557 apex_key {
2558 name: "myapex.key",
2559 public_key: "testkey.avbpubkey",
2560 private_key: "testkey.pem",
2561 }
2562
2563 cc_library {
2564 name: "mylib",
2565 vendor_available: true,
2566 }
2567 `)
2568
2569 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002570 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002571 name := apexBundle.BaseModuleName()
2572 prefix := "TARGET_"
2573 var builder strings.Builder
2574 data.Custom(&builder, name, prefix, "", data)
2575 androidMk := builder.String()
2576 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2577}
2578
Jooyung Han2ed99d02020-06-24 23:26:26 +09002579func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002580 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002581 apex {
2582 name: "myapex",
2583 key: "myapex.key",
2584 vintf_fragments: ["fragment.xml"],
2585 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002586 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002587 }
2588 apex_key {
2589 name: "myapex.key",
2590 public_key: "testkey.avbpubkey",
2591 private_key: "testkey.pem",
2592 }
2593 cc_binary {
2594 name: "mybin",
2595 }
2596 `)
2597
2598 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002599 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002600 name := apexBundle.BaseModuleName()
2601 prefix := "TARGET_"
2602 var builder strings.Builder
2603 data.Custom(&builder, name, prefix, "", data)
2604 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04002605 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04002606 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09002607}
2608
Jiyong Park16e91a02018-12-20 18:18:08 +09002609func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002610 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002611 apex {
2612 name: "myapex",
2613 key: "myapex.key",
2614 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002615 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002616 }
2617
2618 apex_key {
2619 name: "myapex.key",
2620 public_key: "testkey.avbpubkey",
2621 private_key: "testkey.pem",
2622 }
2623
2624 cc_library {
2625 name: "mylib",
2626 srcs: ["mylib.cpp"],
2627 system_shared_libs: [],
2628 stl: "none",
2629 stubs: {
2630 versions: ["1", "2", "3"],
2631 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002632 apex_available: [
2633 "//apex_available:platform",
2634 "myapex",
2635 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002636 }
2637
2638 cc_binary {
2639 name: "not_in_apex",
2640 srcs: ["mylib.cpp"],
2641 static_libs: ["mylib"],
2642 static_executable: true,
2643 system_shared_libs: [],
2644 stl: "none",
2645 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002646 `)
2647
Colin Cross7113d202019-11-20 16:39:12 -08002648 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002649
2650 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002651 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002652}
Jiyong Park9335a262018-12-24 11:31:58 +09002653
2654func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002655 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002656 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002657 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002658 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002659 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002660 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002661 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002662 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09002663 }
2664
2665 cc_library {
2666 name: "mylib",
2667 srcs: ["mylib.cpp"],
2668 system_shared_libs: [],
2669 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002670 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002671 }
2672
2673 apex_key {
2674 name: "myapex.key",
2675 public_key: "testkey.avbpubkey",
2676 private_key: "testkey.pem",
2677 }
2678
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002679 android_app_certificate {
2680 name: "myapex.certificate",
2681 certificate: "testkey",
2682 }
2683
2684 android_app_certificate {
2685 name: "myapex.certificate.override",
2686 certificate: "testkey.override",
2687 }
2688
Jiyong Park9335a262018-12-24 11:31:58 +09002689 `)
2690
2691 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002692 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002693
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002694 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2695 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002696 "vendor/foo/devkeys/testkey.avbpubkey")
2697 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002698 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2699 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002700 "vendor/foo/devkeys/testkey.pem")
2701 }
2702
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002703 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002704 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002705 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002706 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002707 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002708 }
2709}
Jiyong Park58e364a2019-01-19 19:24:06 +09002710
Jooyung Hanf121a652019-12-17 14:30:11 +09002711func TestCertificate(t *testing.T) {
2712 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002713 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002714 apex {
2715 name: "myapex",
2716 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002717 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002718 }
2719 apex_key {
2720 name: "myapex.key",
2721 public_key: "testkey.avbpubkey",
2722 private_key: "testkey.pem",
2723 }`)
2724 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2725 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2726 if actual := rule.Args["certificates"]; actual != expected {
2727 t.Errorf("certificates should be %q, not %q", expected, actual)
2728 }
2729 })
2730 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002731 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002732 apex {
2733 name: "myapex_keytest",
2734 key: "myapex.key",
2735 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002736 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002737 }
2738 apex_key {
2739 name: "myapex.key",
2740 public_key: "testkey.avbpubkey",
2741 private_key: "testkey.pem",
2742 }
2743 android_app_certificate {
2744 name: "myapex.certificate.override",
2745 certificate: "testkey.override",
2746 }`)
2747 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2748 expected := "testkey.override.x509.pem testkey.override.pk8"
2749 if actual := rule.Args["certificates"]; actual != expected {
2750 t.Errorf("certificates should be %q, not %q", expected, actual)
2751 }
2752 })
2753 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002754 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002755 apex {
2756 name: "myapex",
2757 key: "myapex.key",
2758 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002759 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002760 }
2761 apex_key {
2762 name: "myapex.key",
2763 public_key: "testkey.avbpubkey",
2764 private_key: "testkey.pem",
2765 }
2766 android_app_certificate {
2767 name: "myapex.certificate",
2768 certificate: "testkey",
2769 }`)
2770 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2771 expected := "testkey.x509.pem testkey.pk8"
2772 if actual := rule.Args["certificates"]; actual != expected {
2773 t.Errorf("certificates should be %q, not %q", expected, actual)
2774 }
2775 })
2776 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002777 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002778 apex {
2779 name: "myapex_keytest",
2780 key: "myapex.key",
2781 file_contexts: ":myapex-file_contexts",
2782 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002783 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002784 }
2785 apex_key {
2786 name: "myapex.key",
2787 public_key: "testkey.avbpubkey",
2788 private_key: "testkey.pem",
2789 }
2790 android_app_certificate {
2791 name: "myapex.certificate.override",
2792 certificate: "testkey.override",
2793 }`)
2794 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2795 expected := "testkey.override.x509.pem testkey.override.pk8"
2796 if actual := rule.Args["certificates"]; actual != expected {
2797 t.Errorf("certificates should be %q, not %q", expected, actual)
2798 }
2799 })
2800 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002801 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002802 apex {
2803 name: "myapex",
2804 key: "myapex.key",
2805 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002806 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002807 }
2808 apex_key {
2809 name: "myapex.key",
2810 public_key: "testkey.avbpubkey",
2811 private_key: "testkey.pem",
2812 }`)
2813 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2814 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2815 if actual := rule.Args["certificates"]; actual != expected {
2816 t.Errorf("certificates should be %q, not %q", expected, actual)
2817 }
2818 })
2819 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002820 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002821 apex {
2822 name: "myapex_keytest",
2823 key: "myapex.key",
2824 file_contexts: ":myapex-file_contexts",
2825 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002826 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002827 }
2828 apex_key {
2829 name: "myapex.key",
2830 public_key: "testkey.avbpubkey",
2831 private_key: "testkey.pem",
2832 }
2833 android_app_certificate {
2834 name: "myapex.certificate.override",
2835 certificate: "testkey.override",
2836 }`)
2837 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2838 expected := "testkey.override.x509.pem testkey.override.pk8"
2839 if actual := rule.Args["certificates"]; actual != expected {
2840 t.Errorf("certificates should be %q, not %q", expected, actual)
2841 }
2842 })
2843}
2844
Jiyong Park58e364a2019-01-19 19:24:06 +09002845func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002846 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002847 apex {
2848 name: "myapex",
2849 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002850 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002851 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09002852 }
2853
2854 apex {
2855 name: "otherapex",
2856 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002857 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002858 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002859 }
2860
2861 apex_key {
2862 name: "myapex.key",
2863 public_key: "testkey.avbpubkey",
2864 private_key: "testkey.pem",
2865 }
2866
2867 cc_library {
2868 name: "mylib",
2869 srcs: ["mylib.cpp"],
2870 system_shared_libs: [],
2871 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002872 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002873 "myapex",
2874 "otherapex",
2875 ],
Jooyung Han24282772020-03-21 23:20:55 +09002876 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002877 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002878 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09002879 cc_library {
2880 name: "mylib2",
2881 srcs: ["mylib.cpp"],
2882 system_shared_libs: [],
2883 stl: "none",
2884 apex_available: [
2885 "myapex",
2886 "otherapex",
2887 ],
Colin Crossaede88c2020-08-11 12:17:01 -07002888 static_libs: ["mylib3"],
2889 recovery_available: true,
2890 min_sdk_version: "29",
2891 }
2892 cc_library {
2893 name: "mylib3",
2894 srcs: ["mylib.cpp"],
2895 system_shared_libs: [],
2896 stl: "none",
2897 apex_available: [
2898 "myapex",
2899 "otherapex",
2900 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09002901 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07002902 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002903 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002904 }
Jiyong Park58e364a2019-01-19 19:24:06 +09002905 `)
2906
Jooyung Hanc87a0592020-03-02 17:44:33 +09002907 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08002908 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002909 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002910 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002911
Jooyung Hanccce2f22020-03-07 03:45:53 +09002912 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07002913 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09002914 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002915 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09002916 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002917
Jooyung Hanccce2f22020-03-07 03:45:53 +09002918 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07002919 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09002920 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002921 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09002922 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002923
Colin Crossaede88c2020-08-11 12:17:01 -07002924 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
2925 // each variant defines additional macros to distinguish which apex variant it is built for
2926
2927 // non-APEX variant does not have __ANDROID_APEX__ defined
2928 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2929 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2930
2931 // APEX variant has __ANDROID_APEX__ defined
2932 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
2933 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2934 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2935 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
2936
2937 // APEX variant has __ANDROID_APEX__ defined
2938 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
2939 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2940 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
2941 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
2942
Dan Albertb19953d2020-11-17 15:29:36 -08002943 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07002944 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2945 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002946 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07002947
2948 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
2949 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09002950
2951 // non-APEX variant does not have __ANDROID_APEX__ defined
2952 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
2953 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
2954
2955 // APEX variant has __ANDROID_APEX__ defined
2956 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002957 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07002958 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002959 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002960
Jooyung Hanc87a0592020-03-02 17:44:33 +09002961 // APEX variant has __ANDROID_APEX__ defined
2962 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002963 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09002964 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07002965 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09002966
Dan Albertb19953d2020-11-17 15:29:36 -08002967 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07002968 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09002969 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002970 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09002971}
Jiyong Park7e636d02019-01-28 16:16:54 +09002972
2973func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002974 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09002975 apex {
2976 name: "myapex",
2977 key: "myapex.key",
2978 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002979 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09002980 }
2981
2982 apex_key {
2983 name: "myapex.key",
2984 public_key: "testkey.avbpubkey",
2985 private_key: "testkey.pem",
2986 }
2987
2988 cc_library_headers {
2989 name: "mylib_headers",
2990 export_include_dirs: ["my_include"],
2991 system_shared_libs: [],
2992 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09002993 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09002994 }
2995
2996 cc_library {
2997 name: "mylib",
2998 srcs: ["mylib.cpp"],
2999 system_shared_libs: [],
3000 stl: "none",
3001 header_libs: ["mylib_headers"],
3002 export_header_lib_headers: ["mylib_headers"],
3003 stubs: {
3004 versions: ["1", "2", "3"],
3005 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003006 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003007 }
3008
3009 cc_library {
3010 name: "otherlib",
3011 srcs: ["mylib.cpp"],
3012 system_shared_libs: [],
3013 stl: "none",
3014 shared_libs: ["mylib"],
3015 }
3016 `)
3017
Colin Cross7113d202019-11-20 16:39:12 -08003018 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003019
3020 // Ensure that the include path of the header lib is exported to 'otherlib'
3021 ensureContains(t, cFlags, "-Imy_include")
3022}
Alex Light9670d332019-01-29 18:07:33 -08003023
Jiyong Park7cd10e32020-01-14 09:22:18 +09003024type fileInApex struct {
3025 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003026 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003027 isLink bool
3028}
3029
Jooyung Hana57af4a2020-01-23 05:36:59 +00003030func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003031 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003032 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003033 copyCmds := apexRule.Args["copy_commands"]
3034 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003035 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003036 for _, cmd := range strings.Split(copyCmds, "&&") {
3037 cmd = strings.TrimSpace(cmd)
3038 if cmd == "" {
3039 continue
3040 }
3041 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003042 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003043 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003044 switch terms[0] {
3045 case "mkdir":
3046 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003047 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003048 t.Fatal("copyCmds contains invalid cp command", cmd)
3049 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003050 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003051 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003052 isLink = false
3053 case "ln":
3054 if len(terms) != 3 && len(terms) != 4 {
3055 // ln LINK TARGET or ln -s LINK TARGET
3056 t.Fatal("copyCmds contains invalid ln command", cmd)
3057 }
3058 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003059 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003060 isLink = true
3061 default:
3062 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3063 }
3064 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003065 index := strings.Index(dst, imageApexDir)
3066 if index == -1 {
3067 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3068 }
3069 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003070 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003071 }
3072 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003073 return ret
3074}
3075
Jooyung Hana57af4a2020-01-23 05:36:59 +00003076func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3077 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003078 var failed bool
3079 var surplus []string
3080 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003081 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003082 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003083 for _, expected := range files {
3084 if matched, _ := path.Match(expected, file.path); matched {
3085 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003086 mactchFound = true
3087 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003088 }
3089 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003090 if !mactchFound {
3091 surplus = append(surplus, file.path)
3092 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003093 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003094
Jooyung Han31c470b2019-10-18 16:26:59 +09003095 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003096 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003097 t.Log("surplus files", surplus)
3098 failed = true
3099 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003100
3101 if len(files) > len(filesMatched) {
3102 var missing []string
3103 for _, expected := range files {
3104 if !filesMatched[expected] {
3105 missing = append(missing, expected)
3106 }
3107 }
3108 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003109 t.Log("missing files", missing)
3110 failed = true
3111 }
3112 if failed {
3113 t.Fail()
3114 }
3115}
3116
Jooyung Han344d5432019-08-23 11:17:39 +09003117func TestVndkApexCurrent(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003118 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003119 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003120 name: "com.android.vndk.current",
3121 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003122 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003123 }
3124
3125 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003126 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003127 public_key: "testkey.avbpubkey",
3128 private_key: "testkey.pem",
3129 }
3130
3131 cc_library {
3132 name: "libvndk",
3133 srcs: ["mylib.cpp"],
3134 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003135 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003136 vndk: {
3137 enabled: true,
3138 },
3139 system_shared_libs: [],
3140 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003141 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003142 }
3143
3144 cc_library {
3145 name: "libvndksp",
3146 srcs: ["mylib.cpp"],
3147 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003148 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003149 vndk: {
3150 enabled: true,
3151 support_system_process: true,
3152 },
3153 system_shared_libs: [],
3154 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003155 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003156 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003157 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003158
Colin Cross2807f002021-03-02 10:15:29 -08003159 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003160 "lib/libvndk.so",
3161 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003162 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003163 "lib64/libvndk.so",
3164 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003165 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003166 "etc/llndk.libraries.29.txt",
3167 "etc/vndkcore.libraries.29.txt",
3168 "etc/vndksp.libraries.29.txt",
3169 "etc/vndkprivate.libraries.29.txt",
3170 "etc/vndkproduct.libraries.29.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003171 })
Jooyung Han344d5432019-08-23 11:17:39 +09003172}
3173
3174func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003175 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003176 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003177 name: "com.android.vndk.current",
3178 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003179 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003180 }
3181
3182 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003183 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003184 public_key: "testkey.avbpubkey",
3185 private_key: "testkey.pem",
3186 }
3187
3188 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003189 name: "libvndk",
3190 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003191 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003192 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003193 vndk: {
3194 enabled: true,
3195 },
3196 system_shared_libs: [],
3197 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003198 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003199 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003200
3201 cc_prebuilt_library_shared {
3202 name: "libvndk.arm",
3203 srcs: ["libvndk.arm.so"],
3204 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003205 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003206 vndk: {
3207 enabled: true,
3208 },
3209 enabled: false,
3210 arch: {
3211 arm: {
3212 enabled: true,
3213 },
3214 },
3215 system_shared_libs: [],
3216 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003217 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003218 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003219 `+vndkLibrariesTxtFiles("current"),
3220 withFiles(map[string][]byte{
3221 "libvndk.so": nil,
3222 "libvndk.arm.so": nil,
3223 }))
Colin Cross2807f002021-03-02 10:15:29 -08003224 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003225 "lib/libvndk.so",
3226 "lib/libvndk.arm.so",
3227 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003228 "lib/libc++.so",
3229 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003230 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003231 })
Jooyung Han344d5432019-08-23 11:17:39 +09003232}
3233
Jooyung Han39edb6c2019-11-06 16:53:07 +09003234func vndkLibrariesTxtFiles(vers ...string) (result string) {
3235 for _, v := range vers {
3236 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003237 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003238 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003239 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003240 name: "` + txt + `.libraries.txt",
3241 }
3242 `
3243 }
3244 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003245 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003246 result += `
3247 prebuilt_etc {
3248 name: "` + txt + `.libraries.` + v + `.txt",
3249 src: "dummy.txt",
3250 }
3251 `
3252 }
3253 }
3254 }
3255 return
3256}
3257
Jooyung Han344d5432019-08-23 11:17:39 +09003258func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003259 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003260 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003261 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003262 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003263 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003264 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003265 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003266 }
3267
3268 apex_key {
3269 name: "myapex.key",
3270 public_key: "testkey.avbpubkey",
3271 private_key: "testkey.pem",
3272 }
3273
Jooyung Han31c470b2019-10-18 16:26:59 +09003274 vndk_prebuilt_shared {
3275 name: "libvndk27",
3276 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003277 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003278 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003279 vndk: {
3280 enabled: true,
3281 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003282 target_arch: "arm64",
3283 arch: {
3284 arm: {
3285 srcs: ["libvndk27_arm.so"],
3286 },
3287 arm64: {
3288 srcs: ["libvndk27_arm64.so"],
3289 },
3290 },
Colin Cross2807f002021-03-02 10:15:29 -08003291 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003292 }
3293
3294 vndk_prebuilt_shared {
3295 name: "libvndk27",
3296 version: "27",
3297 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003298 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003299 vndk: {
3300 enabled: true,
3301 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003302 target_arch: "x86_64",
3303 arch: {
3304 x86: {
3305 srcs: ["libvndk27_x86.so"],
3306 },
3307 x86_64: {
3308 srcs: ["libvndk27_x86_64.so"],
3309 },
3310 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003311 }
3312 `+vndkLibrariesTxtFiles("27"),
3313 withFiles(map[string][]byte{
3314 "libvndk27_arm.so": nil,
3315 "libvndk27_arm64.so": nil,
3316 "libvndk27_x86.so": nil,
3317 "libvndk27_x86_64.so": nil,
3318 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003319
Colin Cross2807f002021-03-02 10:15:29 -08003320 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003321 "lib/libvndk27_arm.so",
3322 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003323 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003324 })
Jooyung Han344d5432019-08-23 11:17:39 +09003325}
3326
Jooyung Han90eee022019-10-01 20:02:42 +09003327func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003328 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003329 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003330 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003331 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003332 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003333 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003334 }
3335 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003336 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003337 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003338 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003339 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003340 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003341 }
3342 apex_key {
3343 name: "myapex.key",
3344 public_key: "testkey.avbpubkey",
3345 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003346 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003347
3348 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003349 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003350 actual := proptools.String(bundle.properties.Apex_name)
3351 if !reflect.DeepEqual(actual, expected) {
3352 t.Errorf("Got '%v', expected '%v'", actual, expected)
3353 }
3354 }
3355
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003356 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003357 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003358}
3359
Jooyung Han344d5432019-08-23 11:17:39 +09003360func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003361 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003362 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003363 name: "com.android.vndk.current",
3364 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003365 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003366 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003367 }
3368
3369 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003370 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003371 public_key: "testkey.avbpubkey",
3372 private_key: "testkey.pem",
3373 }
3374
3375 cc_library {
3376 name: "libvndk",
3377 srcs: ["mylib.cpp"],
3378 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003379 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003380 native_bridge_supported: true,
3381 host_supported: true,
3382 vndk: {
3383 enabled: true,
3384 },
3385 system_shared_libs: [],
3386 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003387 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003388 }
Colin Cross2807f002021-03-02 10:15:29 -08003389 `+vndkLibrariesTxtFiles("current"),
3390 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003391
Colin Cross2807f002021-03-02 10:15:29 -08003392 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003393 "lib/libvndk.so",
3394 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003395 "lib/libc++.so",
3396 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003397 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003398 })
Jooyung Han344d5432019-08-23 11:17:39 +09003399}
3400
3401func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003402 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003403 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003404 name: "com.android.vndk.current",
3405 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003406 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003407 native_bridge_supported: true,
3408 }
3409
3410 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003411 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003412 public_key: "testkey.avbpubkey",
3413 private_key: "testkey.pem",
3414 }
3415
3416 cc_library {
3417 name: "libvndk",
3418 srcs: ["mylib.cpp"],
3419 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003420 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003421 native_bridge_supported: true,
3422 host_supported: true,
3423 vndk: {
3424 enabled: true,
3425 },
3426 system_shared_libs: [],
3427 stl: "none",
3428 }
3429 `)
3430}
3431
Jooyung Han31c470b2019-10-18 16:26:59 +09003432func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003433 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003434 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003435 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003436 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003437 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003438 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003439 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003440 }
3441
3442 apex_key {
3443 name: "myapex.key",
3444 public_key: "testkey.avbpubkey",
3445 private_key: "testkey.pem",
3446 }
3447
3448 vndk_prebuilt_shared {
3449 name: "libvndk27",
3450 version: "27",
3451 target_arch: "arm",
3452 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003453 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003454 vndk: {
3455 enabled: true,
3456 },
3457 arch: {
3458 arm: {
3459 srcs: ["libvndk27.so"],
3460 }
3461 },
3462 }
3463
3464 vndk_prebuilt_shared {
3465 name: "libvndk27",
3466 version: "27",
3467 target_arch: "arm",
3468 binder32bit: true,
3469 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003470 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003471 vndk: {
3472 enabled: true,
3473 },
3474 arch: {
3475 arm: {
3476 srcs: ["libvndk27binder32.so"],
3477 }
3478 },
Colin Cross2807f002021-03-02 10:15:29 -08003479 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003480 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003481 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003482 withFiles(map[string][]byte{
3483 "libvndk27.so": nil,
3484 "libvndk27binder32.so": nil,
3485 }),
3486 withBinder32bit,
3487 withTargets(map[android.OsType][]android.Target{
3488 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003489 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3490 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003491 },
3492 }),
3493 )
3494
Colin Cross2807f002021-03-02 10:15:29 -08003495 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003496 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003497 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003498 })
3499}
3500
Jooyung Han45a96772020-06-15 14:59:42 +09003501func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003502 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003503 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003504 name: "com.android.vndk.current",
3505 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003506 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003507 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003508 }
3509
3510 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003511 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003512 public_key: "testkey.avbpubkey",
3513 private_key: "testkey.pem",
3514 }
3515
3516 cc_library {
3517 name: "libz",
3518 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003519 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003520 vndk: {
3521 enabled: true,
3522 },
3523 stubs: {
3524 symbol_file: "libz.map.txt",
3525 versions: ["30"],
3526 }
3527 }
3528 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3529 "libz.map.txt": nil,
3530 }))
3531
Colin Cross2807f002021-03-02 10:15:29 -08003532 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003533 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3534 ensureListEmpty(t, provideNativeLibs)
3535}
3536
Jooyung Hane1633032019-08-01 17:41:43 +09003537func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003538 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003539 apex {
3540 name: "myapex_nodep",
3541 key: "myapex.key",
3542 native_shared_libs: ["lib_nodep"],
3543 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003544 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003545 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003546 }
3547
3548 apex {
3549 name: "myapex_dep",
3550 key: "myapex.key",
3551 native_shared_libs: ["lib_dep"],
3552 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003553 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003554 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003555 }
3556
3557 apex {
3558 name: "myapex_provider",
3559 key: "myapex.key",
3560 native_shared_libs: ["libfoo"],
3561 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003562 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003563 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003564 }
3565
3566 apex {
3567 name: "myapex_selfcontained",
3568 key: "myapex.key",
3569 native_shared_libs: ["lib_dep", "libfoo"],
3570 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003571 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003572 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003573 }
3574
3575 apex_key {
3576 name: "myapex.key",
3577 public_key: "testkey.avbpubkey",
3578 private_key: "testkey.pem",
3579 }
3580
3581 cc_library {
3582 name: "lib_nodep",
3583 srcs: ["mylib.cpp"],
3584 system_shared_libs: [],
3585 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003586 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003587 }
3588
3589 cc_library {
3590 name: "lib_dep",
3591 srcs: ["mylib.cpp"],
3592 shared_libs: ["libfoo"],
3593 system_shared_libs: [],
3594 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003595 apex_available: [
3596 "myapex_dep",
3597 "myapex_provider",
3598 "myapex_selfcontained",
3599 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003600 }
3601
3602 cc_library {
3603 name: "libfoo",
3604 srcs: ["mytest.cpp"],
3605 stubs: {
3606 versions: ["1"],
3607 },
3608 system_shared_libs: [],
3609 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003610 apex_available: [
3611 "myapex_provider",
3612 "myapex_selfcontained",
3613 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003614 }
3615 `)
3616
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003617 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003618 var provideNativeLibs, requireNativeLibs []string
3619
Sundong Ahnabb64432019-10-22 13:58:29 +09003620 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003621 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3622 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003623 ensureListEmpty(t, provideNativeLibs)
3624 ensureListEmpty(t, requireNativeLibs)
3625
Sundong Ahnabb64432019-10-22 13:58:29 +09003626 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003627 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3628 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003629 ensureListEmpty(t, provideNativeLibs)
3630 ensureListContains(t, requireNativeLibs, "libfoo.so")
3631
Sundong Ahnabb64432019-10-22 13:58:29 +09003632 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003633 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3634 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003635 ensureListContains(t, provideNativeLibs, "libfoo.so")
3636 ensureListEmpty(t, requireNativeLibs)
3637
Sundong Ahnabb64432019-10-22 13:58:29 +09003638 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003639 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3640 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003641 ensureListContains(t, provideNativeLibs, "libfoo.so")
3642 ensureListEmpty(t, requireNativeLibs)
3643}
3644
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003645func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003646 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003647 apex {
3648 name: "myapex",
3649 key: "myapex.key",
3650 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003651 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003652 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003653 }
3654
3655 apex_key {
3656 name: "myapex.key",
3657 public_key: "testkey.avbpubkey",
3658 private_key: "testkey.pem",
3659 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003660
3661 cc_library {
3662 name: "mylib",
3663 srcs: ["mylib.cpp"],
3664 system_shared_libs: [],
3665 stl: "none",
3666 apex_available: [
3667 "//apex_available:platform",
3668 "myapex",
3669 ],
3670 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003671 `)
3672
Sundong Ahnabb64432019-10-22 13:58:29 +09003673 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003674 apexManifestRule := module.Rule("apexManifestRule")
3675 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3676 apexRule := module.Rule("apexRule")
3677 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003678
3679 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003680 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09003681 name := apexBundle.BaseModuleName()
3682 prefix := "TARGET_"
3683 var builder strings.Builder
3684 data.Custom(&builder, name, prefix, "", data)
3685 androidMk := builder.String()
3686 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3687 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003688}
3689
Alex Light0851b882019-02-07 13:20:53 -08003690func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003691 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003692 apex {
3693 name: "myapex",
3694 key: "myapex.key",
3695 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003696 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003697 }
3698
3699 apex_key {
3700 name: "myapex.key",
3701 public_key: "testkey.avbpubkey",
3702 private_key: "testkey.pem",
3703 }
3704
3705 cc_library {
3706 name: "mylib_common",
3707 srcs: ["mylib.cpp"],
3708 system_shared_libs: [],
3709 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003710 apex_available: [
3711 "//apex_available:platform",
3712 "myapex",
3713 ],
Alex Light0851b882019-02-07 13:20:53 -08003714 }
3715 `)
3716
Sundong Ahnabb64432019-10-22 13:58:29 +09003717 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003718 apexRule := module.Rule("apexRule")
3719 copyCmds := apexRule.Args["copy_commands"]
3720
3721 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3722 t.Log("Apex was a test apex!")
3723 t.Fail()
3724 }
3725 // Ensure that main rule creates an output
3726 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3727
3728 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003729 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003730
3731 // Ensure that both direct and indirect deps are copied into apex
3732 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3733
Colin Cross7113d202019-11-20 16:39:12 -08003734 // Ensure that the platform variant ends with _shared
3735 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003736
Colin Cross56a83212020-09-15 18:30:11 -07003737 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003738 t.Log("Found mylib_common not in any apex!")
3739 t.Fail()
3740 }
3741}
3742
3743func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003744 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003745 apex_test {
3746 name: "myapex",
3747 key: "myapex.key",
3748 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003749 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003750 }
3751
3752 apex_key {
3753 name: "myapex.key",
3754 public_key: "testkey.avbpubkey",
3755 private_key: "testkey.pem",
3756 }
3757
3758 cc_library {
3759 name: "mylib_common_test",
3760 srcs: ["mylib.cpp"],
3761 system_shared_libs: [],
3762 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003763 // TODO: remove //apex_available:platform
3764 apex_available: [
3765 "//apex_available:platform",
3766 "myapex",
3767 ],
Alex Light0851b882019-02-07 13:20:53 -08003768 }
3769 `)
3770
Sundong Ahnabb64432019-10-22 13:58:29 +09003771 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003772 apexRule := module.Rule("apexRule")
3773 copyCmds := apexRule.Args["copy_commands"]
3774
3775 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3776 t.Log("Apex was not a test apex!")
3777 t.Fail()
3778 }
3779 // Ensure that main rule creates an output
3780 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3781
3782 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003783 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003784
3785 // Ensure that both direct and indirect deps are copied into apex
3786 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3787
Colin Cross7113d202019-11-20 16:39:12 -08003788 // Ensure that the platform variant ends with _shared
3789 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003790}
3791
Alex Light9670d332019-01-29 18:07:33 -08003792func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003793 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003794 apex {
3795 name: "myapex",
3796 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003797 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08003798 multilib: {
3799 first: {
3800 native_shared_libs: ["mylib_common"],
3801 }
3802 },
3803 target: {
3804 android: {
3805 multilib: {
3806 first: {
3807 native_shared_libs: ["mylib"],
3808 }
3809 }
3810 },
3811 host: {
3812 multilib: {
3813 first: {
3814 native_shared_libs: ["mylib2"],
3815 }
3816 }
3817 }
3818 }
3819 }
3820
3821 apex_key {
3822 name: "myapex.key",
3823 public_key: "testkey.avbpubkey",
3824 private_key: "testkey.pem",
3825 }
3826
3827 cc_library {
3828 name: "mylib",
3829 srcs: ["mylib.cpp"],
3830 system_shared_libs: [],
3831 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003832 // TODO: remove //apex_available:platform
3833 apex_available: [
3834 "//apex_available:platform",
3835 "myapex",
3836 ],
Alex Light9670d332019-01-29 18:07:33 -08003837 }
3838
3839 cc_library {
3840 name: "mylib_common",
3841 srcs: ["mylib.cpp"],
3842 system_shared_libs: [],
3843 stl: "none",
3844 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003845 // TODO: remove //apex_available:platform
3846 apex_available: [
3847 "//apex_available:platform",
3848 "myapex",
3849 ],
Alex Light9670d332019-01-29 18:07:33 -08003850 }
3851
3852 cc_library {
3853 name: "mylib2",
3854 srcs: ["mylib.cpp"],
3855 system_shared_libs: [],
3856 stl: "none",
3857 compile_multilib: "first",
3858 }
3859 `)
3860
Sundong Ahnabb64432019-10-22 13:58:29 +09003861 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003862 copyCmds := apexRule.Args["copy_commands"]
3863
3864 // Ensure that main rule creates an output
3865 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3866
3867 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003868 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
3869 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
3870 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08003871
3872 // Ensure that both direct and indirect deps are copied into apex
3873 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3874 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3875 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3876
Colin Cross7113d202019-11-20 16:39:12 -08003877 // Ensure that the platform variant ends with _shared
3878 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
3879 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
3880 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08003881}
Jiyong Park04480cf2019-02-06 00:16:29 +09003882
Jiyong Park59140302020-12-14 18:44:04 +09003883func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003884 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09003885 apex {
3886 name: "myapex",
3887 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003888 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09003889 arch: {
3890 arm64: {
3891 native_shared_libs: ["mylib.arm64"],
3892 },
3893 x86_64: {
3894 native_shared_libs: ["mylib.x64"],
3895 },
3896 }
3897 }
3898
3899 apex_key {
3900 name: "myapex.key",
3901 public_key: "testkey.avbpubkey",
3902 private_key: "testkey.pem",
3903 }
3904
3905 cc_library {
3906 name: "mylib.arm64",
3907 srcs: ["mylib.cpp"],
3908 system_shared_libs: [],
3909 stl: "none",
3910 // TODO: remove //apex_available:platform
3911 apex_available: [
3912 "//apex_available:platform",
3913 "myapex",
3914 ],
3915 }
3916
3917 cc_library {
3918 name: "mylib.x64",
3919 srcs: ["mylib.cpp"],
3920 system_shared_libs: [],
3921 stl: "none",
3922 // TODO: remove //apex_available:platform
3923 apex_available: [
3924 "//apex_available:platform",
3925 "myapex",
3926 ],
3927 }
3928 `)
3929
3930 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
3931 copyCmds := apexRule.Args["copy_commands"]
3932
3933 // Ensure that apex variant is created for the direct dep
3934 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
3935 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
3936
3937 // Ensure that both direct and indirect deps are copied into apex
3938 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
3939 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
3940}
3941
Jiyong Park04480cf2019-02-06 00:16:29 +09003942func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003943 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09003944 apex {
3945 name: "myapex",
3946 key: "myapex.key",
3947 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003948 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09003949 }
3950
3951 apex_key {
3952 name: "myapex.key",
3953 public_key: "testkey.avbpubkey",
3954 private_key: "testkey.pem",
3955 }
3956
3957 sh_binary {
3958 name: "myscript",
3959 src: "mylib.cpp",
3960 filename: "myscript.sh",
3961 sub_dir: "script",
3962 }
3963 `)
3964
Sundong Ahnabb64432019-10-22 13:58:29 +09003965 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09003966 copyCmds := apexRule.Args["copy_commands"]
3967
3968 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
3969}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003970
Jooyung Han91df2082019-11-20 01:49:42 +09003971func TestApexInVariousPartition(t *testing.T) {
3972 testcases := []struct {
3973 propName, parition, flattenedPartition string
3974 }{
3975 {"", "system", "system_ext"},
3976 {"product_specific: true", "product", "product"},
3977 {"soc_specific: true", "vendor", "vendor"},
3978 {"proprietary: true", "vendor", "vendor"},
3979 {"vendor: true", "vendor", "vendor"},
3980 {"system_ext_specific: true", "system_ext", "system_ext"},
3981 }
3982 for _, tc := range testcases {
3983 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003984 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09003985 apex {
3986 name: "myapex",
3987 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003988 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09003989 `+tc.propName+`
3990 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003991
Jooyung Han91df2082019-11-20 01:49:42 +09003992 apex_key {
3993 name: "myapex.key",
3994 public_key: "testkey.avbpubkey",
3995 private_key: "testkey.pem",
3996 }
3997 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003998
Jooyung Han91df2082019-11-20 01:49:42 +09003999 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004000 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4001 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004002 if actual != expected {
4003 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4004 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004005
Jooyung Han91df2082019-11-20 01:49:42 +09004006 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004007 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4008 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004009 if actual != expected {
4010 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4011 }
4012 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004013 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004014}
Jiyong Park67882562019-03-21 01:11:21 +09004015
Jooyung Han580eb4f2020-06-24 19:33:06 +09004016func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004017 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004018 apex {
4019 name: "myapex",
4020 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004021 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004022 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004023
Jooyung Han580eb4f2020-06-24 19:33:06 +09004024 apex_key {
4025 name: "myapex.key",
4026 public_key: "testkey.avbpubkey",
4027 private_key: "testkey.pem",
4028 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004029 `)
4030 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004031 rule := module.Output("file_contexts")
4032 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4033}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004034
Jooyung Han580eb4f2020-06-24 19:33:06 +09004035func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004036 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004037 apex {
4038 name: "myapex",
4039 key: "myapex.key",
4040 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004041 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004042 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004043
Jooyung Han580eb4f2020-06-24 19:33:06 +09004044 apex_key {
4045 name: "myapex.key",
4046 public_key: "testkey.avbpubkey",
4047 private_key: "testkey.pem",
4048 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004049 `, withFiles(map[string][]byte{
4050 "my_own_file_contexts": nil,
4051 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004052}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004053
Jooyung Han580eb4f2020-06-24 19:33:06 +09004054func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004055 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004056 apex {
4057 name: "myapex",
4058 key: "myapex.key",
4059 product_specific: true,
4060 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004061 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004062 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004063
Jooyung Han580eb4f2020-06-24 19:33:06 +09004064 apex_key {
4065 name: "myapex.key",
4066 public_key: "testkey.avbpubkey",
4067 private_key: "testkey.pem",
4068 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004069 `)
4070
Colin Cross1c460562021-02-16 17:55:47 -08004071 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004072 apex {
4073 name: "myapex",
4074 key: "myapex.key",
4075 product_specific: true,
4076 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004077 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004078 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004079
Jooyung Han580eb4f2020-06-24 19:33:06 +09004080 apex_key {
4081 name: "myapex.key",
4082 public_key: "testkey.avbpubkey",
4083 private_key: "testkey.pem",
4084 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004085 `, withFiles(map[string][]byte{
4086 "product_specific_file_contexts": nil,
4087 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004088 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4089 rule := module.Output("file_contexts")
4090 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4091}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004092
Jooyung Han580eb4f2020-06-24 19:33:06 +09004093func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004094 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004095 apex {
4096 name: "myapex",
4097 key: "myapex.key",
4098 product_specific: true,
4099 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004100 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004101 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004102
Jooyung Han580eb4f2020-06-24 19:33:06 +09004103 apex_key {
4104 name: "myapex.key",
4105 public_key: "testkey.avbpubkey",
4106 private_key: "testkey.pem",
4107 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004108
Jooyung Han580eb4f2020-06-24 19:33:06 +09004109 filegroup {
4110 name: "my-file-contexts",
4111 srcs: ["product_specific_file_contexts"],
4112 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004113 `, withFiles(map[string][]byte{
4114 "product_specific_file_contexts": nil,
4115 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004116 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4117 rule := module.Output("file_contexts")
4118 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004119}
4120
Jiyong Park67882562019-03-21 01:11:21 +09004121func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004122 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004123 apex_key {
4124 name: "myapex.key",
4125 public_key: ":my.avbpubkey",
4126 private_key: ":my.pem",
4127 product_specific: true,
4128 }
4129
4130 filegroup {
4131 name: "my.avbpubkey",
4132 srcs: ["testkey2.avbpubkey"],
4133 }
4134
4135 filegroup {
4136 name: "my.pem",
4137 srcs: ["testkey2.pem"],
4138 }
4139 `)
4140
4141 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4142 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004143 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004144 if actual_pubkey != expected_pubkey {
4145 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4146 }
4147 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004148 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004149 if actual_privkey != expected_privkey {
4150 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4151 }
4152}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004153
4154func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004155 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004156 prebuilt_apex {
4157 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004158 arch: {
4159 arm64: {
4160 src: "myapex-arm64.apex",
4161 },
4162 arm: {
4163 src: "myapex-arm.apex",
4164 },
4165 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004166 }
4167 `)
4168
Paul Duffin5ec165d2021-06-15 19:09:41 +01004169 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004170
Jiyong Parkc95714e2019-03-29 14:23:10 +09004171 expectedInput := "myapex-arm64.apex"
4172 if prebuilt.inputApex.String() != expectedInput {
4173 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4174 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004175}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004176
Paul Duffinc0609c62021-03-01 17:27:16 +00004177func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin5ec165d2021-06-15 19:09:41 +01004178 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004179 prebuilt_apex {
4180 name: "myapex",
4181 }
4182 `)
4183}
4184
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004185func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004186 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004187 prebuilt_apex {
4188 name: "myapex",
4189 src: "myapex-arm.apex",
4190 filename: "notmyapex.apex",
4191 }
4192 `)
4193
Paul Duffin5ec165d2021-06-15 19:09:41 +01004194 p := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004195
4196 expected := "notmyapex.apex"
4197 if p.installFilename != expected {
4198 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4199 }
4200}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004201
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004202func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004203 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004204 prebuilt_apex {
4205 name: "myapex.prebuilt",
4206 src: "myapex-arm.apex",
4207 overrides: [
4208 "myapex",
4209 ],
4210 }
4211 `)
4212
Paul Duffin5ec165d2021-06-15 19:09:41 +01004213 p := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt").Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004214
4215 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004216 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004217 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004218 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004219 }
4220}
4221
Paul Duffin092153d2021-01-26 11:42:39 +00004222// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4223// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004224func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004225 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004226
Paul Duffin89886cb2021-02-05 16:44:03 +00004227 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004228 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004229 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004230 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004231 stem := android.RemoveOptionalPrebuiltPrefix(name)
4232 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004233 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4234 }
4235 }
4236
Paul Duffin39853512021-02-26 11:09:39 +00004237 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004238 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004239 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004240 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4241 }
4242 }
4243
4244 t.Run("prebuilt only", func(t *testing.T) {
4245 bp := `
4246 prebuilt_apex {
4247 name: "myapex",
4248 arch: {
4249 arm64: {
4250 src: "myapex-arm64.apex",
4251 },
4252 arm: {
4253 src: "myapex-arm.apex",
4254 },
4255 },
Paul Duffin39853512021-02-26 11:09:39 +00004256 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004257 }
4258
4259 java_import {
4260 name: "libfoo",
4261 jars: ["libfoo.jar"],
4262 }
Paul Duffin39853512021-02-26 11:09:39 +00004263
4264 java_sdk_library_import {
4265 name: "libbar",
4266 public: {
4267 jars: ["libbar.jar"],
4268 },
4269 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004270 `
4271
4272 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4273 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4274
Paul Duffinf6932af2021-02-26 18:21:56 +00004275 // Make sure that the deapexer has the correct input APEX.
4276 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4277 rule := deapexer.Rule("deapexer")
4278 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4279 t.Errorf("expected: %q, found: %q", expected, actual)
4280 }
4281
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004282 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin5ec165d2021-06-15 19:09:41 +01004283 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004284 rule = prebuiltApex.Rule("android/soong/android.Cp")
4285 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4286 t.Errorf("expected: %q, found: %q", expected, actual)
4287 }
4288
Paul Duffin89886cb2021-02-05 16:44:03 +00004289 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004290
4291 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004292 })
4293
4294 t.Run("prebuilt with source preferred", func(t *testing.T) {
4295
4296 bp := `
4297 prebuilt_apex {
4298 name: "myapex",
4299 arch: {
4300 arm64: {
4301 src: "myapex-arm64.apex",
4302 },
4303 arm: {
4304 src: "myapex-arm.apex",
4305 },
4306 },
Paul Duffin39853512021-02-26 11:09:39 +00004307 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004308 }
4309
4310 java_import {
4311 name: "libfoo",
4312 jars: ["libfoo.jar"],
4313 }
4314
4315 java_library {
4316 name: "libfoo",
4317 }
Paul Duffin39853512021-02-26 11:09:39 +00004318
4319 java_sdk_library_import {
4320 name: "libbar",
4321 public: {
4322 jars: ["libbar.jar"],
4323 },
4324 }
4325
4326 java_sdk_library {
4327 name: "libbar",
4328 srcs: ["foo/bar/MyClass.java"],
4329 unsafe_ignore_missing_latest_api: true,
4330 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004331 `
4332
4333 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4334 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4335
Paul Duffin89886cb2021-02-05 16:44:03 +00004336 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004337 ensureNoSourceVariant(t, ctx, "libfoo")
4338
4339 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4340 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004341 })
4342
4343 t.Run("prebuilt preferred with source", func(t *testing.T) {
4344 bp := `
4345 prebuilt_apex {
4346 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004347 arch: {
4348 arm64: {
4349 src: "myapex-arm64.apex",
4350 },
4351 arm: {
4352 src: "myapex-arm.apex",
4353 },
4354 },
Paul Duffin39853512021-02-26 11:09:39 +00004355 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004356 }
4357
4358 java_import {
4359 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004360 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004361 jars: ["libfoo.jar"],
4362 }
4363
4364 java_library {
4365 name: "libfoo",
4366 }
Paul Duffin39853512021-02-26 11:09:39 +00004367
4368 java_sdk_library_import {
4369 name: "libbar",
4370 prefer: true,
4371 public: {
4372 jars: ["libbar.jar"],
4373 },
4374 }
4375
4376 java_sdk_library {
4377 name: "libbar",
4378 srcs: ["foo/bar/MyClass.java"],
4379 unsafe_ignore_missing_latest_api: true,
4380 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004381 `
4382
4383 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4384 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4385
Paul Duffin89886cb2021-02-05 16:44:03 +00004386 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004387 ensureNoSourceVariant(t, ctx, "libfoo")
4388
4389 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4390 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004391 })
4392}
4393
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004394func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01004395 preparer := android.GroupFixturePreparers(
4396 java.FixtureConfigureBootJars("myapex:libfoo", "myapex:libbar"),
4397 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
4398 // is disabled.
4399 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
4400 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004401
Paul Duffin37856732021-02-26 14:24:15 +00004402 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4403 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01004404 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004405 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004406 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004407 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004408 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004409 foundLibfooJar = true
4410 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01004411 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004412 }
4413 }
4414 if !foundLibfooJar {
Paul Duffin55607122021-03-30 23:32:51 +01004415 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().BuildDir(), s.AllOutputs()))
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004416 }
4417 }
4418
Paul Duffin2a999332021-06-07 21:36:01 +01004419 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004420 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01004421 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffin2a999332021-06-07 21:36:01 +01004422 var rule android.TestingBuildParams
4423
4424 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
4425 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00004426 }
4427
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004428 fragment := java.ApexVariantReference{
4429 Apex: proptools.StringPtr("myapex"),
4430 Module: proptools.StringPtr("my-bootclasspath-fragment"),
4431 }
4432
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004433 t.Run("prebuilt only", func(t *testing.T) {
4434 bp := `
4435 prebuilt_apex {
4436 name: "myapex",
4437 arch: {
4438 arm64: {
4439 src: "myapex-arm64.apex",
4440 },
4441 arm: {
4442 src: "myapex-arm.apex",
4443 },
4444 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004445 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4446 }
4447
4448 prebuilt_bootclasspath_fragment {
4449 name: "my-bootclasspath-fragment",
4450 contents: ["libfoo", "libbar"],
4451 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004452 }
4453
4454 java_import {
4455 name: "libfoo",
4456 jars: ["libfoo.jar"],
4457 apex_available: ["myapex"],
4458 }
Paul Duffin37856732021-02-26 14:24:15 +00004459
4460 java_sdk_library_import {
4461 name: "libbar",
4462 public: {
4463 jars: ["libbar.jar"],
4464 },
4465 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004466 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004467 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004468 `
4469
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004470 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004471 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4472 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004473
Paul Duffin537ea3d2021-05-14 10:38:00 +01004474 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin9d67ca62021-02-03 20:06:33 +00004475 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004476 out/soong/.intermediates/libbar.stubs/android_common/combined/libbar.stubs.jar
4477 out/soong/.intermediates/libfoo/android_common_myapex/combined/libfoo.jar
4478 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004479 })
4480
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004481 t.Run("apex_set only", func(t *testing.T) {
4482 bp := `
4483 apex_set {
4484 name: "myapex",
4485 set: "myapex.apks",
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004486 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4487 }
4488
4489 prebuilt_bootclasspath_fragment {
4490 name: "my-bootclasspath-fragment",
4491 contents: ["libfoo", "libbar"],
4492 apex_available: ["myapex"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004493 }
4494
4495 java_import {
4496 name: "libfoo",
4497 jars: ["libfoo.jar"],
4498 apex_available: ["myapex"],
4499 }
4500
4501 java_sdk_library_import {
4502 name: "libbar",
4503 public: {
4504 jars: ["libbar.jar"],
4505 },
4506 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004507 shared_library: false,
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004508 }
4509 `
4510
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004511 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004512 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4513 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
4514
Paul Duffin537ea3d2021-05-14 10:38:00 +01004515 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004516 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004517 out/soong/.intermediates/libbar.stubs/android_common/combined/libbar.stubs.jar
4518 out/soong/.intermediates/libfoo/android_common_myapex/combined/libfoo.jar
4519 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004520 })
4521
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004522 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4523 bp := `
4524 prebuilt_apex {
4525 name: "myapex",
4526 arch: {
4527 arm64: {
4528 src: "myapex-arm64.apex",
4529 },
4530 arm: {
4531 src: "myapex-arm.apex",
4532 },
4533 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004534 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4535 }
4536
4537 prebuilt_bootclasspath_fragment {
4538 name: "my-bootclasspath-fragment",
4539 contents: ["libfoo", "libbar"],
4540 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004541 }
4542
4543 java_import {
4544 name: "libfoo",
4545 jars: ["libfoo.jar"],
4546 apex_available: ["myapex"],
4547 }
4548
4549 java_library {
4550 name: "libfoo",
4551 srcs: ["foo/bar/MyClass.java"],
4552 apex_available: ["myapex"],
4553 }
Paul Duffin37856732021-02-26 14:24:15 +00004554
4555 java_sdk_library_import {
4556 name: "libbar",
4557 public: {
4558 jars: ["libbar.jar"],
4559 },
4560 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004561 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004562 }
4563
4564 java_sdk_library {
4565 name: "libbar",
4566 srcs: ["foo/bar/MyClass.java"],
4567 unsafe_ignore_missing_latest_api: true,
4568 apex_available: ["myapex"],
4569 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004570 `
4571
4572 // In this test the source (java_library) libfoo is active since the
4573 // prebuilt (java_import) defaults to prefer:false. However the
4574 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4575 // find the dex boot jar in it. We either need to disable the source libfoo
4576 // or make the prebuilt libfoo preferred.
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004577 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004578 })
4579
4580 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4581 bp := `
4582 prebuilt_apex {
4583 name: "myapex",
4584 arch: {
4585 arm64: {
4586 src: "myapex-arm64.apex",
4587 },
4588 arm: {
4589 src: "myapex-arm.apex",
4590 },
4591 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004592 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4593 }
4594
4595 prebuilt_bootclasspath_fragment {
4596 name: "my-bootclasspath-fragment",
4597 contents: ["libfoo", "libbar"],
4598 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004599 }
4600
4601 java_import {
4602 name: "libfoo",
4603 prefer: true,
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 prefer: true,
4617 public: {
4618 jars: ["libbar.jar"],
4619 },
4620 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004621 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004622 }
4623
4624 java_sdk_library {
4625 name: "libbar",
4626 srcs: ["foo/bar/MyClass.java"],
4627 unsafe_ignore_missing_latest_api: true,
4628 apex_available: ["myapex"],
4629 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004630 `
4631
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004632 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004633 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4634 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004635
Paul Duffin537ea3d2021-05-14 10:38:00 +01004636 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin9d67ca62021-02-03 20:06:33 +00004637 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004638 out/soong/.intermediates/prebuilt_libbar.stubs/android_common/combined/libbar.stubs.jar
4639 out/soong/.intermediates/prebuilt_libfoo/android_common_myapex/combined/libfoo.jar
4640 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004641 })
4642
4643 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4644 bp := `
4645 apex {
4646 name: "myapex",
4647 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004648 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004649 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004650 }
4651
4652 apex_key {
4653 name: "myapex.key",
4654 public_key: "testkey.avbpubkey",
4655 private_key: "testkey.pem",
4656 }
4657
4658 prebuilt_apex {
4659 name: "myapex",
4660 arch: {
4661 arm64: {
4662 src: "myapex-arm64.apex",
4663 },
4664 arm: {
4665 src: "myapex-arm.apex",
4666 },
4667 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004668 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4669 }
4670
4671 prebuilt_bootclasspath_fragment {
4672 name: "my-bootclasspath-fragment",
4673 contents: ["libfoo", "libbar"],
4674 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004675 }
4676
4677 java_import {
4678 name: "libfoo",
4679 jars: ["libfoo.jar"],
4680 apex_available: ["myapex"],
4681 }
4682
4683 java_library {
4684 name: "libfoo",
4685 srcs: ["foo/bar/MyClass.java"],
4686 apex_available: ["myapex"],
4687 }
Paul Duffin37856732021-02-26 14:24:15 +00004688
4689 java_sdk_library_import {
4690 name: "libbar",
4691 public: {
4692 jars: ["libbar.jar"],
4693 },
4694 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004695 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004696 }
4697
4698 java_sdk_library {
4699 name: "libbar",
4700 srcs: ["foo/bar/MyClass.java"],
4701 unsafe_ignore_missing_latest_api: true,
4702 apex_available: ["myapex"],
4703 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004704 `
4705
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004706 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004707 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4708 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004709
Paul Duffin537ea3d2021-05-14 10:38:00 +01004710 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin4fd997b2021-02-03 20:06:33 +00004711 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004712 out/soong/.intermediates/libbar/android_common_myapex/javac/libbar.jar
4713 out/soong/.intermediates/libfoo/android_common_apex10000/javac/libfoo.jar
4714 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004715 })
4716
4717 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4718 bp := `
4719 apex {
4720 name: "myapex",
4721 enabled: false,
4722 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01004723 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004724 }
4725
4726 apex_key {
4727 name: "myapex.key",
4728 public_key: "testkey.avbpubkey",
4729 private_key: "testkey.pem",
4730 }
4731
4732 prebuilt_apex {
4733 name: "myapex",
4734 arch: {
4735 arm64: {
4736 src: "myapex-arm64.apex",
4737 },
4738 arm: {
4739 src: "myapex-arm.apex",
4740 },
4741 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004742 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4743 }
4744
4745 prebuilt_bootclasspath_fragment {
4746 name: "my-bootclasspath-fragment",
4747 contents: ["libfoo", "libbar"],
4748 apex_available: ["myapex"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004749 }
4750
4751 java_import {
4752 name: "libfoo",
4753 prefer: true,
4754 jars: ["libfoo.jar"],
4755 apex_available: ["myapex"],
4756 }
4757
4758 java_library {
4759 name: "libfoo",
4760 srcs: ["foo/bar/MyClass.java"],
4761 apex_available: ["myapex"],
4762 }
Paul Duffin37856732021-02-26 14:24:15 +00004763
4764 java_sdk_library_import {
4765 name: "libbar",
4766 prefer: true,
4767 public: {
4768 jars: ["libbar.jar"],
4769 },
4770 apex_available: ["myapex"],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004771 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00004772 }
4773
4774 java_sdk_library {
4775 name: "libbar",
4776 srcs: ["foo/bar/MyClass.java"],
4777 unsafe_ignore_missing_latest_api: true,
4778 apex_available: ["myapex"],
4779 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004780 `
4781
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01004782 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004783 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4784 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004785
Paul Duffin537ea3d2021-05-14 10:38:00 +01004786 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin9d67ca62021-02-03 20:06:33 +00004787 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin4c2f78b2021-06-16 02:04:13 +01004788 out/soong/.intermediates/prebuilt_libbar.stubs/android_common/combined/libbar.stubs.jar
4789 out/soong/.intermediates/prebuilt_libfoo/android_common_myapex/combined/libfoo.jar
4790 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004791 })
4792}
4793
Roland Levillain630846d2019-06-26 12:48:34 +01004794func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004795 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004796 apex_test {
4797 name: "myapex",
4798 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004799 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01004800 tests: [
4801 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004802 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004803 ],
4804 }
4805
4806 apex_key {
4807 name: "myapex.key",
4808 public_key: "testkey.avbpubkey",
4809 private_key: "testkey.pem",
4810 }
4811
Liz Kammer1c14a212020-05-12 15:26:55 -07004812 filegroup {
4813 name: "fg",
4814 srcs: [
4815 "baz",
4816 "bar/baz"
4817 ],
4818 }
4819
Roland Levillain630846d2019-06-26 12:48:34 +01004820 cc_test {
4821 name: "mytest",
4822 gtest: false,
4823 srcs: ["mytest.cpp"],
4824 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004825 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004826 system_shared_libs: [],
4827 static_executable: true,
4828 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004829 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004830 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004831
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004832 cc_library {
4833 name: "mylib",
4834 srcs: ["mylib.cpp"],
4835 system_shared_libs: [],
4836 stl: "none",
4837 }
4838
Liz Kammer5bd365f2020-05-27 15:15:11 -07004839 filegroup {
4840 name: "fg2",
4841 srcs: [
4842 "testdata/baz"
4843 ],
4844 }
4845
Roland Levillain9b5fde92019-06-28 15:41:19 +01004846 cc_test {
4847 name: "mytests",
4848 gtest: false,
4849 srcs: [
4850 "mytest1.cpp",
4851 "mytest2.cpp",
4852 "mytest3.cpp",
4853 ],
4854 test_per_src: true,
4855 relative_install_path: "test",
4856 system_shared_libs: [],
4857 static_executable: true,
4858 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004859 data: [
4860 ":fg",
4861 ":fg2",
4862 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004863 }
Roland Levillain630846d2019-06-26 12:48:34 +01004864 `)
4865
Sundong Ahnabb64432019-10-22 13:58:29 +09004866 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004867 copyCmds := apexRule.Args["copy_commands"]
4868
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004869 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004870 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004871 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004872
Liz Kammer1c14a212020-05-12 15:26:55 -07004873 //Ensure that test data are copied into apex.
4874 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4875 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4876
Roland Levillain9b5fde92019-06-28 15:41:19 +01004877 // Ensure that test deps built with `test_per_src` are copied into apex.
4878 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4879 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4880 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004881
4882 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004883 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004884 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004885 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004886 prefix := "TARGET_"
4887 var builder strings.Builder
4888 data.Custom(&builder, name, prefix, "", data)
4889 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004890 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4891 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4892 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4893 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004894 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004895 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004896 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004897
4898 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004899 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004900 data.Custom(&builder, name, prefix, "", data)
4901 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004902 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4903 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004904}
4905
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004906func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004907 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004908 apex {
4909 name: "myapex",
4910 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004911 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004912 }
4913 apex_key {
4914 name: "myapex.key",
4915 public_key: "testkey.avbpubkey",
4916 private_key: "testkey.pem",
4917 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00004918 `,
4919 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4920 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4921 }),
4922 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004923 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004924 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07004925 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004926 var builder strings.Builder
4927 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4928 androidMk := builder.String()
4929 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4930}
4931
Jooyung Hand48f3c32019-08-23 11:18:57 +09004932func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4933 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4934 apex {
4935 name: "myapex",
4936 key: "myapex.key",
4937 native_shared_libs: ["libfoo"],
4938 }
4939
4940 apex_key {
4941 name: "myapex.key",
4942 public_key: "testkey.avbpubkey",
4943 private_key: "testkey.pem",
4944 }
4945
4946 cc_library {
4947 name: "libfoo",
4948 stl: "none",
4949 system_shared_libs: [],
4950 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004951 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004952 }
4953 `)
4954 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
4955 apex {
4956 name: "myapex",
4957 key: "myapex.key",
4958 java_libs: ["myjar"],
4959 }
4960
4961 apex_key {
4962 name: "myapex.key",
4963 public_key: "testkey.avbpubkey",
4964 private_key: "testkey.pem",
4965 }
4966
4967 java_library {
4968 name: "myjar",
4969 srcs: ["foo/bar/MyClass.java"],
4970 sdk_version: "none",
4971 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09004972 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004973 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004974 }
4975 `)
4976}
4977
Bill Peckhama41a6962021-01-11 10:58:54 -08004978func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004979 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08004980 apex {
4981 name: "myapex",
4982 key: "myapex.key",
4983 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004984 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08004985 }
4986
4987 apex_key {
4988 name: "myapex.key",
4989 public_key: "testkey.avbpubkey",
4990 private_key: "testkey.pem",
4991 }
4992
4993 java_import {
4994 name: "myjavaimport",
4995 apex_available: ["myapex"],
4996 jars: ["my.jar"],
4997 compile_dex: true,
4998 }
4999 `)
5000
5001 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5002 apexRule := module.Rule("apexRule")
5003 copyCmds := apexRule.Args["copy_commands"]
5004 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5005}
5006
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005007func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005008 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005009 apex {
5010 name: "myapex",
5011 key: "myapex.key",
5012 apps: [
5013 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005014 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005015 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005016 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005017 }
5018
5019 apex_key {
5020 name: "myapex.key",
5021 public_key: "testkey.avbpubkey",
5022 private_key: "testkey.pem",
5023 }
5024
5025 android_app {
5026 name: "AppFoo",
5027 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005028 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005029 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005030 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005031 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005032 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005033 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005034
5035 android_app {
5036 name: "AppFooPriv",
5037 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005038 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005039 system_modules: "none",
5040 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005041 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005042 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005043 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005044
5045 cc_library_shared {
5046 name: "libjni",
5047 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005048 shared_libs: ["libfoo"],
5049 stl: "none",
5050 system_shared_libs: [],
5051 apex_available: [ "myapex" ],
5052 sdk_version: "current",
5053 }
5054
5055 cc_library_shared {
5056 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005057 stl: "none",
5058 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005059 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005060 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005061 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005062 `)
5063
Sundong Ahnabb64432019-10-22 13:58:29 +09005064 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005065 apexRule := module.Rule("apexRule")
5066 copyCmds := apexRule.Args["copy_commands"]
5067
5068 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005069 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005070
Colin Crossaede88c2020-08-11 12:17:01 -07005071 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005072 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005073 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005074 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005075 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005076 // JNI libraries including transitive deps are
5077 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005078 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005079 // ... embedded inside APK (jnilibs.zip)
5080 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5081 // ... and not directly inside the APEX
5082 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5083 }
Dario Frenicde2a032019-10-27 00:29:22 +01005084}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005085
Dario Frenicde2a032019-10-27 00:29:22 +01005086func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005087 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005088 apex {
5089 name: "myapex",
5090 key: "myapex.key",
5091 apps: [
5092 "AppFooPrebuilt",
5093 "AppFooPrivPrebuilt",
5094 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005095 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005096 }
5097
5098 apex_key {
5099 name: "myapex.key",
5100 public_key: "testkey.avbpubkey",
5101 private_key: "testkey.pem",
5102 }
5103
5104 android_app_import {
5105 name: "AppFooPrebuilt",
5106 apk: "PrebuiltAppFoo.apk",
5107 presigned: true,
5108 dex_preopt: {
5109 enabled: false,
5110 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005111 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005112 }
5113
5114 android_app_import {
5115 name: "AppFooPrivPrebuilt",
5116 apk: "PrebuiltAppFooPriv.apk",
5117 privileged: true,
5118 presigned: true,
5119 dex_preopt: {
5120 enabled: false,
5121 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005122 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005123 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005124 }
5125 `)
5126
Sundong Ahnabb64432019-10-22 13:58:29 +09005127 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005128 apexRule := module.Rule("apexRule")
5129 copyCmds := apexRule.Args["copy_commands"]
5130
5131 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005132 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5133}
5134
5135func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005136 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005137 apex {
5138 name: "myapex",
5139 key: "myapex.key",
5140 apps: [
5141 "AppFoo",
5142 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005143 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005144 }
5145
5146 apex_key {
5147 name: "myapex.key",
5148 public_key: "testkey.avbpubkey",
5149 private_key: "testkey.pem",
5150 }
5151
5152 android_app {
5153 name: "AppFoo",
5154 srcs: ["foo/bar/MyClass.java"],
5155 sdk_version: "none",
5156 system_modules: "none",
5157 apex_available: [ "myapex" ],
5158 }
5159
5160 android_app_import {
5161 name: "AppFoo",
5162 apk: "AppFooPrebuilt.apk",
5163 filename: "AppFooPrebuilt.apk",
5164 presigned: true,
5165 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005166 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005167 }
5168 `, withFiles(map[string][]byte{
5169 "AppFooPrebuilt.apk": nil,
5170 }))
5171
5172 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005173 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005174 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005175}
5176
Dario Freni6f3937c2019-12-20 22:58:03 +00005177func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005178 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005179 apex {
5180 name: "myapex",
5181 key: "myapex.key",
5182 apps: [
5183 "TesterHelpAppFoo",
5184 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005185 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005186 }
5187
5188 apex_key {
5189 name: "myapex.key",
5190 public_key: "testkey.avbpubkey",
5191 private_key: "testkey.pem",
5192 }
5193
5194 android_test_helper_app {
5195 name: "TesterHelpAppFoo",
5196 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005197 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005198 }
5199
5200 `)
5201
5202 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5203 apexRule := module.Rule("apexRule")
5204 copyCmds := apexRule.Args["copy_commands"]
5205
5206 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5207}
5208
Jooyung Han18020ea2019-11-13 10:50:48 +09005209func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5210 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005211 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005212 apex {
5213 name: "myapex",
5214 key: "myapex.key",
5215 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005216 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005217 }
5218
5219 apex_key {
5220 name: "myapex.key",
5221 public_key: "testkey.avbpubkey",
5222 private_key: "testkey.pem",
5223 }
5224
5225 apex {
5226 name: "otherapex",
5227 key: "myapex.key",
5228 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005229 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005230 }
5231
5232 cc_defaults {
5233 name: "libfoo-defaults",
5234 apex_available: ["otherapex"],
5235 }
5236
5237 cc_library {
5238 name: "libfoo",
5239 defaults: ["libfoo-defaults"],
5240 stl: "none",
5241 system_shared_libs: [],
5242 }`)
5243}
5244
Paul Duffine52e66f2020-03-30 17:54:29 +01005245func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005246 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005247 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005248 apex {
5249 name: "myapex",
5250 key: "myapex.key",
5251 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005252 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005253 }
5254
5255 apex_key {
5256 name: "myapex.key",
5257 public_key: "testkey.avbpubkey",
5258 private_key: "testkey.pem",
5259 }
5260
5261 apex {
5262 name: "otherapex",
5263 key: "otherapex.key",
5264 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005265 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005266 }
5267
5268 apex_key {
5269 name: "otherapex.key",
5270 public_key: "testkey.avbpubkey",
5271 private_key: "testkey.pem",
5272 }
5273
5274 cc_library {
5275 name: "libfoo",
5276 stl: "none",
5277 system_shared_libs: [],
5278 apex_available: ["otherapex"],
5279 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005280}
Jiyong Park127b40b2019-09-30 16:04:35 +09005281
Paul Duffine52e66f2020-03-30 17:54:29 +01005282func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005283 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005284 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005285.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005286.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005287.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005288.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005289.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005290.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005291 apex {
5292 name: "myapex",
5293 key: "myapex.key",
5294 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005295 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005296 }
5297
5298 apex_key {
5299 name: "myapex.key",
5300 public_key: "testkey.avbpubkey",
5301 private_key: "testkey.pem",
5302 }
5303
Jiyong Park127b40b2019-09-30 16:04:35 +09005304 cc_library {
5305 name: "libfoo",
5306 stl: "none",
5307 shared_libs: ["libbar"],
5308 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005309 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005310 }
5311
5312 cc_library {
5313 name: "libbar",
5314 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005315 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005316 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005317 apex_available: ["myapex"],
5318 }
5319
5320 cc_library {
5321 name: "libbaz",
5322 stl: "none",
5323 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005324 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005325}
Jiyong Park127b40b2019-09-30 16:04:35 +09005326
Paul Duffine52e66f2020-03-30 17:54:29 +01005327func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005328 testApexError(t, "\"otherapex\" is not a valid module name", `
5329 apex {
5330 name: "myapex",
5331 key: "myapex.key",
5332 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005333 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005334 }
5335
5336 apex_key {
5337 name: "myapex.key",
5338 public_key: "testkey.avbpubkey",
5339 private_key: "testkey.pem",
5340 }
5341
5342 cc_library {
5343 name: "libfoo",
5344 stl: "none",
5345 system_shared_libs: [],
5346 apex_available: ["otherapex"],
5347 }`)
5348
Paul Duffine52e66f2020-03-30 17:54:29 +01005349 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005350 apex {
5351 name: "myapex",
5352 key: "myapex.key",
5353 native_shared_libs: ["libfoo", "libbar"],
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
5363 cc_library {
5364 name: "libfoo",
5365 stl: "none",
5366 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005367 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005368 apex_available: ["myapex"],
5369 }
5370
5371 cc_library {
5372 name: "libbar",
5373 stl: "none",
5374 system_shared_libs: [],
5375 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005376 }
5377
5378 cc_library {
5379 name: "libbaz",
5380 stl: "none",
5381 system_shared_libs: [],
5382 stubs: {
5383 versions: ["10", "20", "30"],
5384 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005385 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005386}
Jiyong Park127b40b2019-09-30 16:04:35 +09005387
Jiyong Park89e850a2020-04-07 16:37:39 +09005388func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005389 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005390 apex {
5391 name: "myapex",
5392 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005393 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005394 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005395 }
5396
5397 apex_key {
5398 name: "myapex.key",
5399 public_key: "testkey.avbpubkey",
5400 private_key: "testkey.pem",
5401 }
5402
5403 cc_library {
5404 name: "libfoo",
5405 stl: "none",
5406 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005407 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005408 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005409 }
5410
5411 cc_library {
5412 name: "libfoo2",
5413 stl: "none",
5414 system_shared_libs: [],
5415 shared_libs: ["libbaz"],
5416 apex_available: ["//apex_available:platform"],
5417 }
5418
5419 cc_library {
5420 name: "libbar",
5421 stl: "none",
5422 system_shared_libs: [],
5423 apex_available: ["myapex"],
5424 }
5425
5426 cc_library {
5427 name: "libbaz",
5428 stl: "none",
5429 system_shared_libs: [],
5430 apex_available: ["myapex"],
5431 stubs: {
5432 versions: ["1"],
5433 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005434 }`)
5435
Jiyong Park89e850a2020-04-07 16:37:39 +09005436 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5437 // because it depends on libbar which isn't available to platform
5438 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5439 if libfoo.NotAvailableForPlatform() != true {
5440 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5441 }
5442
5443 // libfoo2 however can be available to platform because it depends on libbaz which provides
5444 // stubs
5445 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5446 if libfoo2.NotAvailableForPlatform() == true {
5447 t.Errorf("%q should be available to platform", libfoo2.String())
5448 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005449}
Jiyong Parka90ca002019-10-07 15:47:24 +09005450
Paul Duffine52e66f2020-03-30 17:54:29 +01005451func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005452 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005453 apex {
5454 name: "myapex",
5455 key: "myapex.key",
5456 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005457 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005458 }
5459
5460 apex_key {
5461 name: "myapex.key",
5462 public_key: "testkey.avbpubkey",
5463 private_key: "testkey.pem",
5464 }
5465
5466 cc_library {
5467 name: "libfoo",
5468 stl: "none",
5469 system_shared_libs: [],
5470 apex_available: ["myapex"],
5471 static: {
5472 apex_available: ["//apex_available:platform"],
5473 },
5474 }`)
5475
Jiyong Park89e850a2020-04-07 16:37:39 +09005476 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5477 if libfooShared.NotAvailableForPlatform() != true {
5478 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5479 }
5480 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5481 if libfooStatic.NotAvailableForPlatform() != false {
5482 t.Errorf("%q should be available to platform", libfooStatic.String())
5483 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005484}
5485
Jiyong Park5d790c32019-11-15 18:40:32 +09005486func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005487 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005488 apex {
5489 name: "myapex",
5490 key: "myapex.key",
5491 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005492 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005493 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005494 }
5495
5496 override_apex {
5497 name: "override_myapex",
5498 base: "myapex",
5499 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005500 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005501 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005502 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07005503 key: "mynewapex.key",
5504 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09005505 }
5506
5507 apex_key {
5508 name: "myapex.key",
5509 public_key: "testkey.avbpubkey",
5510 private_key: "testkey.pem",
5511 }
5512
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07005513 apex_key {
5514 name: "mynewapex.key",
5515 public_key: "testkey2.avbpubkey",
5516 private_key: "testkey2.pem",
5517 }
5518
5519 android_app_certificate {
5520 name: "myapex.certificate",
5521 certificate: "testkey",
5522 }
5523
Jiyong Park5d790c32019-11-15 18:40:32 +09005524 android_app {
5525 name: "app",
5526 srcs: ["foo/bar/MyClass.java"],
5527 package_name: "foo",
5528 sdk_version: "none",
5529 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005530 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005531 }
5532
5533 override_android_app {
5534 name: "override_app",
5535 base: "app",
5536 package_name: "bar",
5537 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005538 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005539
Jiyong Park317645e2019-12-05 13:20:58 +09005540 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5541 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5542 if originalVariant.GetOverriddenBy() != "" {
5543 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5544 }
5545 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5546 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5547 }
5548
Jiyong Park5d790c32019-11-15 18:40:32 +09005549 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5550 apexRule := module.Rule("apexRule")
5551 copyCmds := apexRule.Args["copy_commands"]
5552
5553 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005554 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005555
5556 apexBundle := module.Module().(*apexBundle)
5557 name := apexBundle.Name()
5558 if name != "override_myapex" {
5559 t.Errorf("name should be \"override_myapex\", but was %q", name)
5560 }
5561
Baligh Uddin004d7172020-02-19 21:29:28 -08005562 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5563 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5564 }
5565
Jiyong Park20bacab2020-03-03 11:45:41 +09005566 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005567 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07005568 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
5569
5570 signApkRule := module.Rule("signapk")
5571 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09005572
Colin Crossaa255532020-07-03 13:18:24 -07005573 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005574 var builder strings.Builder
5575 data.Custom(&builder, name, "TARGET_", "", data)
5576 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005577 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005578 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5579 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005580 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005581 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005582 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005583 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5584 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005585}
5586
Jooyung Han214bf372019-11-12 13:03:50 +09005587func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005588 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005589 apex {
5590 name: "myapex",
5591 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005592 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005593 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005594 }
5595
5596 apex_key {
5597 name: "myapex.key",
5598 public_key: "testkey.avbpubkey",
5599 private_key: "testkey.pem",
5600 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005601
5602 cc_library {
5603 name: "mylib",
5604 srcs: ["mylib.cpp"],
5605 stl: "libc++",
5606 system_shared_libs: [],
5607 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005608 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005609 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005610 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005611
5612 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5613 args := module.Rule("apexRule").Args
5614 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005615 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005616
5617 // The copies of the libraries in the apex should have one more dependency than
5618 // the ones outside the apex, namely the unwinder. Ideally we should check
5619 // the dependency names directly here but for some reason the names are blank in
5620 // this test.
5621 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005622 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005623 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5624 if len(apexImplicits) != len(nonApexImplicits)+1 {
5625 t.Errorf("%q missing unwinder dep", lib)
5626 }
5627 }
Jooyung Han214bf372019-11-12 13:03:50 +09005628}
5629
Paul Duffine05480a2021-03-08 15:07:14 +00005630var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01005631 "api/current.txt": nil,
5632 "api/removed.txt": nil,
5633 "api/system-current.txt": nil,
5634 "api/system-removed.txt": nil,
5635 "api/test-current.txt": nil,
5636 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005637
Anton Hanssondff2c782020-12-21 17:10:01 +00005638 "100/public/api/foo.txt": nil,
5639 "100/public/api/foo-removed.txt": nil,
5640 "100/system/api/foo.txt": nil,
5641 "100/system/api/foo-removed.txt": nil,
5642
Paul Duffineedc5d52020-06-12 17:46:39 +01005643 // For java_sdk_library_import
5644 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005645}
5646
Jooyung Han58f26ab2019-12-18 15:34:32 +09005647func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005648 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09005649 apex {
5650 name: "myapex",
5651 key: "myapex.key",
5652 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005653 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09005654 }
5655
5656 apex_key {
5657 name: "myapex.key",
5658 public_key: "testkey.avbpubkey",
5659 private_key: "testkey.pem",
5660 }
5661
5662 java_sdk_library {
5663 name: "foo",
5664 srcs: ["a.java"],
5665 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005666 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005667 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005668
5669 prebuilt_apis {
5670 name: "sdk",
5671 api_dirs: ["100"],
5672 }
Paul Duffin9b879592020-05-26 13:21:35 +01005673 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005674
5675 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005676 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005677 "javalib/foo.jar",
5678 "etc/permissions/foo.xml",
5679 })
5680 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005681 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5682 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005683}
5684
Paul Duffin9b879592020-05-26 13:21:35 +01005685func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005686 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005687 apex {
5688 name: "myapex",
5689 key: "myapex.key",
5690 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005691 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005692 }
5693
5694 apex_key {
5695 name: "myapex.key",
5696 public_key: "testkey.avbpubkey",
5697 private_key: "testkey.pem",
5698 }
5699
5700 java_sdk_library {
5701 name: "foo",
5702 srcs: ["a.java"],
5703 api_packages: ["foo"],
5704 apex_available: ["myapex"],
5705 sdk_version: "none",
5706 system_modules: "none",
5707 }
5708
5709 java_library {
5710 name: "bar",
5711 srcs: ["a.java"],
5712 libs: ["foo"],
5713 apex_available: ["myapex"],
5714 sdk_version: "none",
5715 system_modules: "none",
5716 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005717
5718 prebuilt_apis {
5719 name: "sdk",
5720 api_dirs: ["100"],
5721 }
Paul Duffin9b879592020-05-26 13:21:35 +01005722 `, withFiles(filesForSdkLibrary))
5723
5724 // java_sdk_library installs both impl jar and permission XML
5725 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5726 "javalib/bar.jar",
5727 "javalib/foo.jar",
5728 "etc/permissions/foo.xml",
5729 })
5730
5731 // The bar library should depend on the implementation jar.
5732 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005733 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005734 t.Errorf("expected %q, found %#q", expected, actual)
5735 }
5736}
5737
5738func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005739 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005740 apex {
5741 name: "myapex",
5742 key: "myapex.key",
5743 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005744 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005745 }
5746
5747 apex_key {
5748 name: "myapex.key",
5749 public_key: "testkey.avbpubkey",
5750 private_key: "testkey.pem",
5751 }
5752
5753 java_sdk_library {
5754 name: "foo",
5755 srcs: ["a.java"],
5756 api_packages: ["foo"],
5757 apex_available: ["myapex"],
5758 sdk_version: "none",
5759 system_modules: "none",
5760 }
5761
5762 java_library {
5763 name: "bar",
5764 srcs: ["a.java"],
5765 libs: ["foo"],
5766 sdk_version: "none",
5767 system_modules: "none",
5768 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005769
5770 prebuilt_apis {
5771 name: "sdk",
5772 api_dirs: ["100"],
5773 }
Paul Duffin9b879592020-05-26 13:21:35 +01005774 `, withFiles(filesForSdkLibrary))
5775
5776 // java_sdk_library installs both impl jar and permission XML
5777 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5778 "javalib/foo.jar",
5779 "etc/permissions/foo.xml",
5780 })
5781
5782 // The bar library should depend on the stubs jar.
5783 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005784 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005785 t.Errorf("expected %q, found %#q", expected, actual)
5786 }
5787}
5788
Paul Duffineedc5d52020-06-12 17:46:39 +01005789func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005790 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00005791 prebuilt_apis {
5792 name: "sdk",
5793 api_dirs: ["100"],
5794 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005795 withFiles(map[string][]byte{
5796 "apex/a.java": nil,
5797 "apex/apex_manifest.json": nil,
5798 "apex/Android.bp": []byte(`
5799 package {
5800 default_visibility: ["//visibility:private"],
5801 }
5802
5803 apex {
5804 name: "myapex",
5805 key: "myapex.key",
5806 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005807 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005808 }
5809
5810 apex_key {
5811 name: "myapex.key",
5812 public_key: "testkey.avbpubkey",
5813 private_key: "testkey.pem",
5814 }
5815
5816 java_library {
5817 name: "bar",
5818 srcs: ["a.java"],
5819 libs: ["foo"],
5820 apex_available: ["myapex"],
5821 sdk_version: "none",
5822 system_modules: "none",
5823 }
5824`),
5825 "source/a.java": nil,
5826 "source/api/current.txt": nil,
5827 "source/api/removed.txt": nil,
5828 "source/Android.bp": []byte(`
5829 package {
5830 default_visibility: ["//visibility:private"],
5831 }
5832
5833 java_sdk_library {
5834 name: "foo",
5835 visibility: ["//apex"],
5836 srcs: ["a.java"],
5837 api_packages: ["foo"],
5838 apex_available: ["myapex"],
5839 sdk_version: "none",
5840 system_modules: "none",
5841 public: {
5842 enabled: true,
5843 },
5844 }
5845`),
5846 "prebuilt/a.jar": nil,
5847 "prebuilt/Android.bp": []byte(`
5848 package {
5849 default_visibility: ["//visibility:private"],
5850 }
5851
5852 java_sdk_library_import {
5853 name: "foo",
5854 visibility: ["//apex", "//source"],
5855 apex_available: ["myapex"],
5856 prefer: true,
5857 public: {
5858 jars: ["a.jar"],
5859 },
5860 }
5861`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005862 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005863 )
5864
5865 // java_sdk_library installs both impl jar and permission XML
5866 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5867 "javalib/bar.jar",
5868 "javalib/foo.jar",
5869 "etc/permissions/foo.xml",
5870 })
5871
5872 // The bar library should depend on the implementation jar.
5873 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005874 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01005875 t.Errorf("expected %q, found %#q", expected, actual)
5876 }
5877}
5878
5879func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5880 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5881 apex {
5882 name: "myapex",
5883 key: "myapex.key",
5884 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005885 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005886 }
5887
5888 apex_key {
5889 name: "myapex.key",
5890 public_key: "testkey.avbpubkey",
5891 private_key: "testkey.pem",
5892 }
5893
5894 java_sdk_library_import {
5895 name: "foo",
5896 apex_available: ["myapex"],
5897 prefer: true,
5898 public: {
5899 jars: ["a.jar"],
5900 },
5901 }
5902
5903 `, withFiles(filesForSdkLibrary))
5904}
5905
atrost6e126252020-01-27 17:01:16 +00005906func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01005907 result := android.GroupFixturePreparers(
5908 prepareForApexTest,
5909 java.PrepareForTestWithPlatformCompatConfig,
5910 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00005911 apex {
5912 name: "myapex",
5913 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00005914 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00005915 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005916 updatable: false,
atrost6e126252020-01-27 17:01:16 +00005917 }
5918
5919 apex_key {
5920 name: "myapex.key",
5921 public_key: "testkey.avbpubkey",
5922 private_key: "testkey.pem",
5923 }
5924
5925 platform_compat_config {
5926 name: "myjar-platform-compat-config",
5927 src: ":myjar",
5928 }
5929
5930 java_library {
5931 name: "myjar",
5932 srcs: ["foo/bar/MyClass.java"],
5933 sdk_version: "none",
5934 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005935 apex_available: [ "myapex" ],
5936 }
Paul Duffin1b29e002021-03-16 15:06:54 +00005937
5938 // Make sure that a preferred prebuilt does not affect the apex contents.
5939 prebuilt_platform_compat_config {
5940 name: "myjar-platform-compat-config",
5941 metadata: "compat-config/metadata.xml",
5942 prefer: true,
5943 }
atrost6e126252020-01-27 17:01:16 +00005944 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00005945 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00005946 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5947 "etc/compatconfig/myjar-platform-compat-config.xml",
5948 "javalib/myjar.jar",
5949 })
5950}
5951
Jiyong Park479321d2019-12-16 11:47:12 +09005952func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5953 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5954 apex {
5955 name: "myapex",
5956 key: "myapex.key",
5957 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005958 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09005959 }
5960
5961 apex_key {
5962 name: "myapex.key",
5963 public_key: "testkey.avbpubkey",
5964 private_key: "testkey.pem",
5965 }
5966
5967 java_library {
5968 name: "myjar",
5969 srcs: ["foo/bar/MyClass.java"],
5970 sdk_version: "none",
5971 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09005972 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005973 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09005974 }
5975 `)
5976}
5977
Jiyong Park7afd1072019-12-30 16:56:33 +09005978func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005979 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09005980 apex {
5981 name: "myapex",
5982 key: "myapex.key",
5983 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005984 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09005985 }
5986
5987 apex_key {
5988 name: "myapex.key",
5989 public_key: "testkey.avbpubkey",
5990 private_key: "testkey.pem",
5991 }
5992
5993 cc_library {
5994 name: "mylib",
5995 srcs: ["mylib.cpp"],
5996 system_shared_libs: [],
5997 stl: "none",
5998 required: ["a", "b"],
5999 host_required: ["c", "d"],
6000 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006001 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006002 }
6003 `)
6004
6005 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006006 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006007 name := apexBundle.BaseModuleName()
6008 prefix := "TARGET_"
6009 var builder strings.Builder
6010 data.Custom(&builder, name, prefix, "", data)
6011 androidMk := builder.String()
6012 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6013 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6014 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6015}
6016
Jiyong Park7cd10e32020-01-14 09:22:18 +09006017func TestSymlinksFromApexToSystem(t *testing.T) {
6018 bp := `
6019 apex {
6020 name: "myapex",
6021 key: "myapex.key",
6022 native_shared_libs: ["mylib"],
6023 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006024 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006025 }
6026
Jiyong Park9d677202020-02-19 16:29:35 +09006027 apex {
6028 name: "myapex.updatable",
6029 key: "myapex.key",
6030 native_shared_libs: ["mylib"],
6031 java_libs: ["myjar"],
6032 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006033 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006034 }
6035
Jiyong Park7cd10e32020-01-14 09:22:18 +09006036 apex_key {
6037 name: "myapex.key",
6038 public_key: "testkey.avbpubkey",
6039 private_key: "testkey.pem",
6040 }
6041
6042 cc_library {
6043 name: "mylib",
6044 srcs: ["mylib.cpp"],
6045 shared_libs: ["myotherlib"],
6046 system_shared_libs: [],
6047 stl: "none",
6048 apex_available: [
6049 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006050 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006051 "//apex_available:platform",
6052 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006053 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006054 }
6055
6056 cc_library {
6057 name: "myotherlib",
6058 srcs: ["mylib.cpp"],
6059 system_shared_libs: [],
6060 stl: "none",
6061 apex_available: [
6062 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006063 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006064 "//apex_available:platform",
6065 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006066 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006067 }
6068
6069 java_library {
6070 name: "myjar",
6071 srcs: ["foo/bar/MyClass.java"],
6072 sdk_version: "none",
6073 system_modules: "none",
6074 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006075 apex_available: [
6076 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006077 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006078 "//apex_available:platform",
6079 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006080 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006081 }
6082
6083 java_library {
6084 name: "myotherjar",
6085 srcs: ["foo/bar/MyClass.java"],
6086 sdk_version: "none",
6087 system_modules: "none",
6088 apex_available: [
6089 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006090 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006091 "//apex_available:platform",
6092 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006093 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006094 }
6095 `
6096
6097 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6098 for _, f := range files {
6099 if f.path == file {
6100 if f.isLink {
6101 t.Errorf("%q is not a real file", file)
6102 }
6103 return
6104 }
6105 }
6106 t.Errorf("%q is not found", file)
6107 }
6108
6109 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6110 for _, f := range files {
6111 if f.path == file {
6112 if !f.isLink {
6113 t.Errorf("%q is not a symlink", file)
6114 }
6115 return
6116 }
6117 }
6118 t.Errorf("%q is not found", file)
6119 }
6120
Jiyong Park9d677202020-02-19 16:29:35 +09006121 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6122 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006123 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006124 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006125 ensureRealfileExists(t, files, "javalib/myjar.jar")
6126 ensureRealfileExists(t, files, "lib64/mylib.so")
6127 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6128
Jiyong Park9d677202020-02-19 16:29:35 +09006129 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6130 ensureRealfileExists(t, files, "javalib/myjar.jar")
6131 ensureRealfileExists(t, files, "lib64/mylib.so")
6132 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6133
6134 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006135 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006136 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006137 ensureRealfileExists(t, files, "javalib/myjar.jar")
6138 ensureRealfileExists(t, files, "lib64/mylib.so")
6139 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006140
6141 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6142 ensureRealfileExists(t, files, "javalib/myjar.jar")
6143 ensureRealfileExists(t, files, "lib64/mylib.so")
6144 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006145}
6146
Yo Chiange8128052020-07-23 20:09:18 +08006147func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006148 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006149 apex {
6150 name: "myapex",
6151 key: "myapex.key",
6152 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006153 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006154 }
6155
6156 apex_key {
6157 name: "myapex.key",
6158 public_key: "testkey.avbpubkey",
6159 private_key: "testkey.pem",
6160 }
6161
6162 cc_library_shared {
6163 name: "mylib",
6164 srcs: ["mylib.cpp"],
6165 shared_libs: ["myotherlib"],
6166 system_shared_libs: [],
6167 stl: "none",
6168 apex_available: [
6169 "myapex",
6170 "//apex_available:platform",
6171 ],
6172 }
6173
6174 cc_prebuilt_library_shared {
6175 name: "myotherlib",
6176 srcs: ["prebuilt.so"],
6177 system_shared_libs: [],
6178 stl: "none",
6179 apex_available: [
6180 "myapex",
6181 "//apex_available:platform",
6182 ],
6183 }
6184 `)
6185
6186 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006187 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006188 var builder strings.Builder
6189 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6190 androidMk := builder.String()
6191 // `myotherlib` is added to `myapex` as symlink
6192 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6193 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6194 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6195 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006196 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 +08006197}
6198
Jooyung Han643adc42020-02-27 13:50:06 +09006199func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006200 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006201 apex {
6202 name: "myapex",
6203 key: "myapex.key",
6204 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006205 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006206 }
6207
6208 apex_key {
6209 name: "myapex.key",
6210 public_key: "testkey.avbpubkey",
6211 private_key: "testkey.pem",
6212 }
6213
6214 cc_library {
6215 name: "mylib",
6216 srcs: ["mylib.cpp"],
6217 shared_libs: ["mylib2"],
6218 system_shared_libs: [],
6219 stl: "none",
6220 apex_available: [ "myapex" ],
6221 }
6222
6223 cc_library {
6224 name: "mylib2",
6225 srcs: ["mylib.cpp"],
6226 system_shared_libs: [],
6227 stl: "none",
6228 apex_available: [ "myapex" ],
6229 }
6230 `)
6231
6232 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6233 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6234 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6235 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6236 "lib64/mylib.so",
6237 "lib64/mylib2.so",
6238 })
6239}
6240
Jooyung Han49f67012020-04-17 13:43:10 +09006241func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006242 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006243 apex {
6244 name: "myapex",
6245 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006246 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006247 }
6248 apex_key {
6249 name: "myapex.key",
6250 public_key: "testkey.avbpubkey",
6251 private_key: "testkey.pem",
6252 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006253 `,
6254 android.FixtureModifyConfig(func(config android.Config) {
6255 delete(config.Targets, android.Android)
6256 config.AndroidCommonTarget = android.Target{}
6257 }),
6258 )
Jooyung Han49f67012020-04-17 13:43:10 +09006259
6260 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6261 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6262 }
6263}
6264
Jiyong Parkbd159612020-02-28 15:22:21 +09006265func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006266 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006267 apex {
6268 name: "myapex",
6269 key: "myapex.key",
6270 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006271 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006272 }
6273
6274 apex_key {
6275 name: "myapex.key",
6276 public_key: "testkey.avbpubkey",
6277 private_key: "testkey.pem",
6278 }
6279
6280 android_app {
6281 name: "AppFoo",
6282 srcs: ["foo/bar/MyClass.java"],
6283 sdk_version: "none",
6284 system_modules: "none",
6285 apex_available: [ "myapex" ],
6286 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006287 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006288
Colin Crosscf371cc2020-11-13 11:48:42 -08006289 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006290 content := bundleConfigRule.Args["content"]
6291
6292 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006293 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 +09006294}
6295
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006296func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006297 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006298 apex {
6299 name: "myapex",
6300 key: "myapex.key",
6301 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006302 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006303 }
6304
6305 apex_key {
6306 name: "myapex.key",
6307 public_key: "testkey.avbpubkey",
6308 private_key: "testkey.pem",
6309 }
6310
6311 android_app_set {
6312 name: "AppSet",
6313 set: "AppSet.apks",
6314 }`)
6315 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006316 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006317 content := bundleConfigRule.Args["content"]
6318 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6319 s := mod.Rule("apexRule").Args["copy_commands"]
6320 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6321 if len(copyCmds) != 3 {
6322 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6323 }
6324 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6325 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6326 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6327}
6328
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006329func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01006330 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006331 apex_set {
6332 name: "myapex",
6333 filename: "foo_v2.apex",
6334 sanitized: {
6335 none: { set: "myapex.apks", },
6336 hwaddress: { set: "myapex.hwasan.apks", },
6337 },
Paul Duffin24704672021-04-06 16:09:30 +01006338 }
6339 `
6340 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006341
Paul Duffin24704672021-04-06 16:09:30 +01006342 // Check that the extractor produces the correct output file from the correct input file.
6343 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006344
Paul Duffin24704672021-04-06 16:09:30 +01006345 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
6346 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006347
Paul Duffin24704672021-04-06 16:09:30 +01006348 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
6349
6350 // Ditto for the apex.
Paul Duffin5ec165d2021-06-15 19:09:41 +01006351 m = ctx.ModuleForTests("myapex", "android_common_myapex")
6352 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01006353
6354 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006355}
6356
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006357func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006358 t.Helper()
6359
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006360 bp := `
6361 java_library {
6362 name: "some-updatable-apex-lib",
6363 srcs: ["a.java"],
6364 sdk_version: "current",
6365 apex_available: [
6366 "some-updatable-apex",
6367 ],
6368 }
6369
6370 java_library {
6371 name: "some-non-updatable-apex-lib",
6372 srcs: ["a.java"],
6373 apex_available: [
6374 "some-non-updatable-apex",
6375 ],
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006376 compile_dex: true,
6377 }
6378
6379 bootclasspath_fragment {
6380 name: "some-non-updatable-fragment",
6381 contents: ["some-non-updatable-apex-lib"],
6382 apex_available: [
6383 "some-non-updatable-apex",
6384 ],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006385 }
6386
6387 java_library {
6388 name: "some-platform-lib",
6389 srcs: ["a.java"],
6390 sdk_version: "current",
6391 installable: true,
6392 }
6393
6394 java_library {
6395 name: "some-art-lib",
6396 srcs: ["a.java"],
6397 sdk_version: "current",
6398 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006399 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006400 ],
6401 hostdex: true,
Paul Duffinc75bbce2021-06-07 13:28:19 +01006402 compile_dex: true,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006403 }
6404
6405 apex {
6406 name: "some-updatable-apex",
6407 key: "some-updatable-apex.key",
6408 java_libs: ["some-updatable-apex-lib"],
6409 updatable: true,
6410 min_sdk_version: "current",
6411 }
6412
6413 apex {
6414 name: "some-non-updatable-apex",
6415 key: "some-non-updatable-apex.key",
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006416 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006417 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006418 }
6419
6420 apex_key {
6421 name: "some-updatable-apex.key",
6422 }
6423
6424 apex_key {
6425 name: "some-non-updatable-apex.key",
6426 }
6427
6428 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006429 name: "com.android.art.debug",
6430 key: "com.android.art.debug.key",
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006431 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006432 updatable: true,
6433 min_sdk_version: "current",
6434 }
6435
Paul Duffinf23bc472021-04-27 12:42:20 +01006436 bootclasspath_fragment {
6437 name: "art-bootclasspath-fragment",
6438 image_name: "art",
6439 contents: ["some-art-lib"],
6440 apex_available: [
6441 "com.android.art.debug",
6442 ],
6443 }
6444
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006445 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006446 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006447 }
6448
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006449 filegroup {
6450 name: "some-updatable-apex-file_contexts",
6451 srcs: [
6452 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6453 ],
6454 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006455
6456 filegroup {
6457 name: "some-non-updatable-apex-file_contexts",
6458 srcs: [
6459 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6460 ],
6461 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006462 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006463
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006464 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00006465}
6466
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006467func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006468 t.Helper()
6469
Paul Duffin55607122021-03-30 23:32:51 +01006470 fs := android.MockFS{
6471 "a.java": nil,
6472 "a.jar": nil,
6473 "apex_manifest.json": nil,
6474 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006475 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006476 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6477 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6478 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006479 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006480 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006481
Paul Duffin55607122021-03-30 23:32:51 +01006482 errorHandler := android.FixtureExpectsNoErrors
6483 if errmsg != "" {
6484 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006485 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006486
Paul Duffin55607122021-03-30 23:32:51 +01006487 result := android.GroupFixturePreparers(
6488 cc.PrepareForTestWithCcDefaultModules,
6489 java.PrepareForTestWithHiddenApiBuildComponents,
6490 java.PrepareForTestWithJavaDefaultModules,
6491 java.PrepareForTestWithJavaSdkLibraryFiles,
6492 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01006493 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01006494 fs.AddToFixture(),
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006495 android.FixtureModifyMockFS(func(fs android.MockFS) {
6496 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
6497 insert := ""
6498 for _, fragment := range fragments {
6499 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
6500 }
6501 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
6502 platform_bootclasspath {
6503 name: "platform-bootclasspath",
6504 fragments: [
6505 %s
6506 ],
6507 }
6508 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01006509 }
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006510 }),
Paul Duffin55607122021-03-30 23:32:51 +01006511 ).
6512 ExtendWithErrorHandler(errorHandler).
6513 RunTestWithBp(t, bp)
6514
6515 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006516}
6517
Jooyung Han548640b2020-04-27 12:10:30 +09006518func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6519 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6520 apex {
6521 name: "myapex",
6522 key: "myapex.key",
6523 updatable: true,
6524 }
6525
6526 apex_key {
6527 name: "myapex.key",
6528 public_key: "testkey.avbpubkey",
6529 private_key: "testkey.pem",
6530 }
6531 `)
6532}
6533
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006534func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6535 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6536 apex {
6537 name: "myapex",
6538 key: "myapex.key",
6539 }
6540
6541 apex_key {
6542 name: "myapex.key",
6543 public_key: "testkey.avbpubkey",
6544 private_key: "testkey.pem",
6545 }
6546 `)
6547}
6548
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006549func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006550 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
6551 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
6552 // modules to be included in the BootJars.
6553 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
6554 return android.GroupFixturePreparers(
6555 dexpreopt.FixtureSetBootJars(bootJars...),
6556 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6557 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
6558 }),
6559 )
6560 }
6561
6562 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
6563 // same value. This can result in an invalid configuration as it allows non art apex jars to be
6564 // specified in the ArtApexJars configuration.
6565 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
6566 return android.GroupFixturePreparers(
6567 dexpreopt.FixtureSetArtBootJars(bootJars...),
6568 dexpreopt.FixtureSetBootJars(bootJars...),
6569 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6570 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
6571 }),
6572 )
6573 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006574
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006575 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006576 preparer := java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib")
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006577 fragment := java.ApexVariantReference{
6578 Apex: proptools.StringPtr("com.android.art.debug"),
6579 Module: proptools.StringPtr("art-bootclasspath-fragment"),
6580 }
6581 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006582 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006583
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006584 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006585 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
6586 // Update the dexpreopt BootJars directly.
6587 preparer := prepareSetBootJars("com.android.art.debug:some-art-lib")
6588 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006589 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006590
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006591 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 +01006592 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 +01006593 // Update the dexpreopt ArtApexJars directly.
6594 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
6595 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006596 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006597
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006598 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 +01006599 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 +01006600 // Update the dexpreopt ArtApexJars directly.
6601 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
6602 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006603 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006604
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006605 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 +01006606 err := `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the framework boot image`
6607 preparer := java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib")
6608 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006609 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006610
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006611 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 +01006612 preparer := java.FixtureConfigureBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006613 fragment := java.ApexVariantReference{
6614 Apex: proptools.StringPtr("some-non-updatable-apex"),
6615 Module: proptools.StringPtr("some-non-updatable-fragment"),
6616 }
6617 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006618 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006619
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006620 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01006621 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01006622 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
6623 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006624 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006625
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006626 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01006627 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01006628 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
6629 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006630 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006631
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006632 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01006633 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01006634 // Update the dexpreopt ArtApexJars directly.
6635 preparer := prepareSetArtJars("platform:some-platform-lib")
6636 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006637 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006638
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006639 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006640 preparer := java.FixtureConfigureBootJars("platform:some-platform-lib")
6641 testNoUpdatableJarsInBootImage(t, "", preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006642 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006643}
6644
6645func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006646 preparer := java.FixtureConfigureBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00006647 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006648 fragment := java.ApexVariantReference{
6649 Apex: proptools.StringPtr("myapex"),
6650 Module: proptools.StringPtr("my-bootclasspath-fragment"),
6651 }
6652
Paul Duffin064b70c2020-11-02 17:32:38 +00006653 testDexpreoptWithApexes(t, `
6654 prebuilt_apex {
6655 name: "myapex" ,
6656 arch: {
6657 arm64: {
6658 src: "myapex-arm64.apex",
6659 },
6660 arm: {
6661 src: "myapex-arm.apex",
6662 },
6663 },
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006664 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
6665 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006666
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006667 prebuilt_bootclasspath_fragment {
6668 name: "my-bootclasspath-fragment",
6669 contents: ["libfoo"],
6670 apex_available: ["myapex"],
6671 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006672
Paul Duffin3ae9e2c2021-06-16 01:42:33 +01006673 java_import {
6674 name: "libfoo",
6675 jars: ["libfoo.jar"],
6676 apex_available: ["myapex"],
6677 }
6678 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00006679 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006680}
6681
Andrei Onea115e7e72020-06-05 21:14:03 +01006682func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6683 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006684 bp += `
6685 apex_key {
6686 name: "myapex.key",
6687 public_key: "testkey.avbpubkey",
6688 private_key: "testkey.pem",
6689 }`
Paul Duffin45338f02021-03-30 23:07:52 +01006690 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01006691 "lib1/src/A.java": nil,
6692 "lib2/src/B.java": nil,
6693 "system/sepolicy/apex/myapex-file_contexts": nil,
6694 }
6695
Paul Duffin45338f02021-03-30 23:07:52 +01006696 errorHandler := android.FixtureExpectsNoErrors
6697 if errmsg != "" {
6698 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07006699 }
Colin Crossae8600b2020-10-29 17:09:13 -07006700
Paul Duffin45338f02021-03-30 23:07:52 +01006701 android.GroupFixturePreparers(
6702 android.PrepareForTestWithAndroidBuildComponents,
6703 java.PrepareForTestWithJavaBuildComponents,
6704 PrepareForTestWithApexBuildComponents,
6705 android.PrepareForTestWithNeverallowRules(rules),
6706 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6707 updatableBootJars := make([]string, 0, len(apexBootJars))
6708 for _, apexBootJar := range apexBootJars {
6709 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6710 }
6711 variables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6712 }),
6713 fs.AddToFixture(),
6714 ).
6715 ExtendWithErrorHandler(errorHandler).
6716 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01006717}
6718
6719func TestApexPermittedPackagesRules(t *testing.T) {
6720 testcases := []struct {
6721 name string
6722 expectedError string
6723 bp string
6724 bootJars []string
6725 modulesPackages map[string][]string
6726 }{
6727
6728 {
6729 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6730 expectedError: "",
6731 bp: `
6732 java_library {
6733 name: "bcp_lib1",
6734 srcs: ["lib1/src/*.java"],
6735 permitted_packages: ["foo.bar"],
6736 apex_available: ["myapex"],
6737 sdk_version: "none",
6738 system_modules: "none",
6739 }
6740 java_library {
6741 name: "nonbcp_lib2",
6742 srcs: ["lib2/src/*.java"],
6743 apex_available: ["myapex"],
6744 permitted_packages: ["a.b"],
6745 sdk_version: "none",
6746 system_modules: "none",
6747 }
6748 apex {
6749 name: "myapex",
6750 key: "myapex.key",
6751 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006752 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006753 }`,
6754 bootJars: []string{"bcp_lib1"},
6755 modulesPackages: map[string][]string{
6756 "myapex": []string{
6757 "foo.bar",
6758 },
6759 },
6760 },
6761 {
6762 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6763 expectedError: `module "bcp_lib2" .* which is restricted because jars that are part of the myapex module may only allow these packages: foo.bar. Please jarjar or move code around.`,
6764 bp: `
6765 java_library {
6766 name: "bcp_lib1",
6767 srcs: ["lib1/src/*.java"],
6768 apex_available: ["myapex"],
6769 permitted_packages: ["foo.bar"],
6770 sdk_version: "none",
6771 system_modules: "none",
6772 }
6773 java_library {
6774 name: "bcp_lib2",
6775 srcs: ["lib2/src/*.java"],
6776 apex_available: ["myapex"],
6777 permitted_packages: ["foo.bar", "bar.baz"],
6778 sdk_version: "none",
6779 system_modules: "none",
6780 }
6781 apex {
6782 name: "myapex",
6783 key: "myapex.key",
6784 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006785 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006786 }
6787 `,
6788 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6789 modulesPackages: map[string][]string{
6790 "myapex": []string{
6791 "foo.bar",
6792 },
6793 },
6794 },
6795 }
6796 for _, tc := range testcases {
6797 t.Run(tc.name, func(t *testing.T) {
6798 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6799 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6800 })
6801 }
6802}
6803
Jiyong Park62304bb2020-04-13 16:19:48 +09006804func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006805 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09006806 apex {
6807 name: "myapex",
6808 key: "myapex.key",
6809 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006810 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09006811 }
6812
6813 apex_key {
6814 name: "myapex.key",
6815 public_key: "testkey.avbpubkey",
6816 private_key: "testkey.pem",
6817 }
6818
6819 cc_library {
6820 name: "mylib",
6821 srcs: ["mylib.cpp"],
6822 system_shared_libs: [],
6823 stl: "none",
6824 stubs: {
6825 versions: ["1"],
6826 },
6827 apex_available: ["myapex"],
6828 }
6829
6830 cc_library {
6831 name: "myprivlib",
6832 srcs: ["mylib.cpp"],
6833 system_shared_libs: [],
6834 stl: "none",
6835 apex_available: ["myapex"],
6836 }
6837
6838
6839 cc_test {
6840 name: "mytest",
6841 gtest: false,
6842 srcs: ["mylib.cpp"],
6843 system_shared_libs: [],
6844 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006845 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006846 test_for: ["myapex"]
6847 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006848
6849 cc_library {
6850 name: "mytestlib",
6851 srcs: ["mylib.cpp"],
6852 system_shared_libs: [],
6853 shared_libs: ["mylib", "myprivlib"],
6854 stl: "none",
6855 test_for: ["myapex"],
6856 }
6857
6858 cc_benchmark {
6859 name: "mybench",
6860 srcs: ["mylib.cpp"],
6861 system_shared_libs: [],
6862 shared_libs: ["mylib", "myprivlib"],
6863 stl: "none",
6864 test_for: ["myapex"],
6865 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006866 `)
6867
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006868 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01006869 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006870 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6871 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6872 }
6873
6874 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09006875 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006876 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6877 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6878 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6879}
Jiyong Park46a512f2020-12-04 18:02:13 +09006880
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006881func TestIndirectTestFor(t *testing.T) {
6882 ctx := testApex(t, `
6883 apex {
6884 name: "myapex",
6885 key: "myapex.key",
6886 native_shared_libs: ["mylib", "myprivlib"],
6887 updatable: false,
6888 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006889
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006890 apex_key {
6891 name: "myapex.key",
6892 public_key: "testkey.avbpubkey",
6893 private_key: "testkey.pem",
6894 }
6895
6896 cc_library {
6897 name: "mylib",
6898 srcs: ["mylib.cpp"],
6899 system_shared_libs: [],
6900 stl: "none",
6901 stubs: {
6902 versions: ["1"],
6903 },
6904 apex_available: ["myapex"],
6905 }
6906
6907 cc_library {
6908 name: "myprivlib",
6909 srcs: ["mylib.cpp"],
6910 system_shared_libs: [],
6911 stl: "none",
6912 shared_libs: ["mylib"],
6913 apex_available: ["myapex"],
6914 }
6915
6916 cc_library {
6917 name: "mytestlib",
6918 srcs: ["mylib.cpp"],
6919 system_shared_libs: [],
6920 shared_libs: ["myprivlib"],
6921 stl: "none",
6922 test_for: ["myapex"],
6923 }
6924 `)
6925
6926 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01006927 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006928 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6929 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6930 }
6931
6932 // The platform variant of mytestlib links to the platform variant of the
6933 // internal myprivlib.
6934 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
6935
6936 // The platform variant of myprivlib links to the platform variant of mylib
6937 // and bypasses its stubs.
6938 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 +09006939}
6940
Martin Stjernholmec009002021-03-27 15:18:31 +00006941func TestTestForForLibInOtherApex(t *testing.T) {
6942 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
6943 _ = testApex(t, `
6944 apex {
6945 name: "com.android.art",
6946 key: "myapex.key",
6947 native_shared_libs: ["mylib"],
6948 updatable: false,
6949 }
6950
6951 apex {
6952 name: "com.android.art.debug",
6953 key: "myapex.key",
6954 native_shared_libs: ["mylib", "mytestlib"],
6955 updatable: false,
6956 }
6957
6958 apex_key {
6959 name: "myapex.key",
6960 public_key: "testkey.avbpubkey",
6961 private_key: "testkey.pem",
6962 }
6963
6964 cc_library {
6965 name: "mylib",
6966 srcs: ["mylib.cpp"],
6967 system_shared_libs: [],
6968 stl: "none",
6969 stubs: {
6970 versions: ["1"],
6971 },
6972 apex_available: ["com.android.art", "com.android.art.debug"],
6973 }
6974
6975 cc_library {
6976 name: "mytestlib",
6977 srcs: ["mylib.cpp"],
6978 system_shared_libs: [],
6979 shared_libs: ["mylib"],
6980 stl: "none",
6981 apex_available: ["com.android.art.debug"],
6982 test_for: ["com.android.art"],
6983 }
6984 `,
6985 android.MockFS{
6986 "system/sepolicy/apex/com.android.art-file_contexts": nil,
6987 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
6988 }.AddToFixture())
6989}
6990
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006991// TODO(jungjw): Move this to proptools
6992func intPtr(i int) *int {
6993 return &i
6994}
6995
6996func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006997 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006998 apex_set {
6999 name: "myapex",
7000 set: "myapex.apks",
7001 filename: "foo_v2.apex",
7002 overrides: ["foo"],
7003 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007004 `,
7005 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7006 variables.Platform_sdk_version = intPtr(30)
7007 }),
7008 android.FixtureModifyConfig(func(config android.Config) {
7009 config.Targets[android.Android] = []android.Target{
7010 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
7011 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
7012 }
7013 }),
7014 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007015
Paul Duffin24704672021-04-06 16:09:30 +01007016 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007017
7018 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01007019 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007020 actual := extractedApex.Args["abis"]
7021 expected := "ARMEABI_V7A,ARM64_V8A"
7022 if actual != expected {
7023 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7024 }
7025 actual = extractedApex.Args["sdk-version"]
7026 expected = "30"
7027 if actual != expected {
7028 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7029 }
7030
Paul Duffin5ec165d2021-06-15 19:09:41 +01007031 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007032 a := m.Module().(*ApexSet)
7033 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07007034 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007035 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
7036 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
7037 }
7038}
7039
Jiyong Park7d95a512020-05-10 15:16:24 +09007040func TestNoStaticLinkingToStubsLib(t *testing.T) {
7041 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
7042 apex {
7043 name: "myapex",
7044 key: "myapex.key",
7045 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007046 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09007047 }
7048
7049 apex_key {
7050 name: "myapex.key",
7051 public_key: "testkey.avbpubkey",
7052 private_key: "testkey.pem",
7053 }
7054
7055 cc_library {
7056 name: "mylib",
7057 srcs: ["mylib.cpp"],
7058 static_libs: ["otherlib"],
7059 system_shared_libs: [],
7060 stl: "none",
7061 apex_available: [ "myapex" ],
7062 }
7063
7064 cc_library {
7065 name: "otherlib",
7066 srcs: ["mylib.cpp"],
7067 system_shared_libs: [],
7068 stl: "none",
7069 stubs: {
7070 versions: ["1", "2", "3"],
7071 },
7072 apex_available: [ "myapex" ],
7073 }
7074 `)
7075}
7076
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007077func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007078 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007079 apex {
7080 name: "myapex",
7081 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007082 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007083 }
7084
7085 apex_key {
7086 name: "myapex.key",
7087 public_key: "testkey.avbpubkey",
7088 private_key: "testkey.pem",
7089 }
7090
7091 prebuilt_apex {
7092 name: "myapex",
7093 prefer: true,
7094 arch: {
7095 arm64: {
7096 src: "myapex-arm64.apex",
7097 },
7098 arm: {
7099 src: "myapex-arm.apex",
7100 },
7101 },
7102 }
7103
7104 apex_set {
7105 name: "myapex_set",
7106 set: "myapex.apks",
7107 filename: "myapex_set.apex",
7108 overrides: ["myapex"],
7109 }
7110 `)
7111
7112 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7113 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7114 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 +09007115 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 +09007116}
7117
Jooyung Han938b5932020-06-20 12:47:47 +09007118func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007119 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007120 apex {
7121 name: "myapex",
7122 key: "myapex.key",
7123 apps: ["app"],
7124 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007125 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007126 }
7127
7128 apex_key {
7129 name: "myapex.key",
7130 public_key: "testkey.avbpubkey",
7131 private_key: "testkey.pem",
7132 }
7133
7134 android_app {
7135 name: "app",
7136 srcs: ["foo/bar/MyClass.java"],
7137 package_name: "foo",
7138 sdk_version: "none",
7139 system_modules: "none",
7140 apex_available: [ "myapex" ],
7141 }
7142 `, withFiles(map[string][]byte{
7143 "sub/Android.bp": []byte(`
7144 override_apex {
7145 name: "override_myapex",
7146 base: "myapex",
7147 apps: ["override_app"],
7148 allowed_files: ":allowed",
7149 }
7150 // Overridable "path" property should be referenced indirectly
7151 filegroup {
7152 name: "allowed",
7153 srcs: ["allowed.txt"],
7154 }
7155 override_android_app {
7156 name: "override_app",
7157 base: "app",
7158 package_name: "bar",
7159 }
7160 `),
7161 }))
7162
7163 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7164 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7165 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7166 }
7167
7168 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7169 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7170 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7171 }
7172}
7173
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007174func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007175 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007176 apex {
7177 name: "myapex",
7178 key: "myapex.key",
7179 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007180 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007181 }
7182
7183 apex_key {
7184 name: "myapex.key",
7185 public_key: "testkey.avbpubkey",
7186 private_key: "testkey.pem",
7187 }
7188
7189 cc_library {
7190 name: "mylib",
7191 srcs: ["mylib.cpp"],
7192 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007193 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007194 },
7195 apex_available: ["myapex"],
7196 }
7197
7198 cc_prebuilt_library_shared {
7199 name: "mylib",
7200 prefer: false,
7201 srcs: ["prebuilt.so"],
7202 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007203 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007204 },
7205 apex_available: ["myapex"],
7206 }
7207 `)
7208}
7209
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007210func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007211 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007212 apex {
7213 name: "myapex",
7214 key: "myapex.key",
7215 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007216 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007217 }
7218 apex_key {
7219 name: "myapex.key",
7220 public_key: "testkey.avbpubkey",
7221 private_key: "testkey.pem",
7222 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007223 `,
7224 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7225 variables.CompressedApex = proptools.BoolPtr(true)
7226 }),
7227 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007228
7229 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7230 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7231
7232 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7233 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7234
7235 // Make sure output of bundle is .capex
7236 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7237 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7238
7239 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007240 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007241 var builder strings.Builder
7242 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7243 androidMk := builder.String()
7244 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7245}
7246
Martin Stjernholm2856c662020-12-02 15:03:42 +00007247func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007248 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007249 apex {
7250 name: "myapex",
7251 key: "myapex.key",
7252 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007253 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007254 }
7255
7256 apex_key {
7257 name: "myapex.key",
7258 public_key: "testkey.avbpubkey",
7259 private_key: "testkey.pem",
7260 }
7261
7262 cc_library {
7263 name: "mylib",
7264 srcs: ["mylib.cpp"],
7265 apex_available: ["myapex"],
7266 shared_libs: ["otherlib"],
7267 system_shared_libs: [],
7268 }
7269
7270 cc_library {
7271 name: "otherlib",
7272 srcs: ["mylib.cpp"],
7273 stubs: {
7274 versions: ["current"],
7275 },
7276 }
7277
7278 cc_prebuilt_library_shared {
7279 name: "otherlib",
7280 prefer: true,
7281 srcs: ["prebuilt.so"],
7282 stubs: {
7283 versions: ["current"],
7284 },
7285 }
7286 `)
7287
7288 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007289 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007290 var builder strings.Builder
7291 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7292 androidMk := builder.String()
7293
7294 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7295 // a thing there.
7296 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7297}
7298
Jiyong Parke3867542020-12-03 17:28:25 +09007299func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007300 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007301 apex {
7302 name: "myapex",
7303 key: "myapex.key",
7304 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007305 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007306 }
7307
7308 apex_key {
7309 name: "myapex.key",
7310 public_key: "testkey.avbpubkey",
7311 private_key: "testkey.pem",
7312 }
7313
7314 cc_library {
7315 name: "mylib",
7316 srcs: ["mylib.cpp"],
7317 system_shared_libs: [],
7318 stl: "none",
7319 apex_available: ["myapex"],
7320 shared_libs: ["mylib2"],
7321 target: {
7322 apex: {
7323 exclude_shared_libs: ["mylib2"],
7324 },
7325 },
7326 }
7327
7328 cc_library {
7329 name: "mylib2",
7330 srcs: ["mylib.cpp"],
7331 system_shared_libs: [],
7332 stl: "none",
7333 }
7334 `)
7335
7336 // Check if mylib is linked to mylib2 for the non-apex target
7337 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7338 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7339
7340 // Make sure that the link doesn't occur for the apex target
7341 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7342 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7343
7344 // It shouldn't appear in the copy cmd as well.
7345 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7346 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7347}
7348
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007349func TestPrebuiltStubLibDep(t *testing.T) {
7350 bpBase := `
7351 apex {
7352 name: "myapex",
7353 key: "myapex.key",
7354 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007355 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007356 }
7357 apex_key {
7358 name: "myapex.key",
7359 public_key: "testkey.avbpubkey",
7360 private_key: "testkey.pem",
7361 }
7362 cc_library {
7363 name: "mylib",
7364 srcs: ["mylib.cpp"],
7365 apex_available: ["myapex"],
7366 shared_libs: ["stublib"],
7367 system_shared_libs: [],
7368 }
7369 apex {
7370 name: "otherapex",
7371 enabled: %s,
7372 key: "myapex.key",
7373 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007374 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007375 }
7376 `
7377
7378 stublibSourceBp := `
7379 cc_library {
7380 name: "stublib",
7381 srcs: ["mylib.cpp"],
7382 apex_available: ["otherapex"],
7383 system_shared_libs: [],
7384 stl: "none",
7385 stubs: {
7386 versions: ["1"],
7387 },
7388 }
7389 `
7390
7391 stublibPrebuiltBp := `
7392 cc_prebuilt_library_shared {
7393 name: "stublib",
7394 srcs: ["prebuilt.so"],
7395 apex_available: ["otherapex"],
7396 stubs: {
7397 versions: ["1"],
7398 },
7399 %s
7400 }
7401 `
7402
7403 tests := []struct {
7404 name string
7405 stublibBp string
7406 usePrebuilt bool
7407 modNames []string // Modules to collect AndroidMkEntries for
7408 otherApexEnabled []string
7409 }{
7410 {
7411 name: "only_source",
7412 stublibBp: stublibSourceBp,
7413 usePrebuilt: false,
7414 modNames: []string{"stublib"},
7415 otherApexEnabled: []string{"true", "false"},
7416 },
7417 {
7418 name: "source_preferred",
7419 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7420 usePrebuilt: false,
7421 modNames: []string{"stublib", "prebuilt_stublib"},
7422 otherApexEnabled: []string{"true", "false"},
7423 },
7424 {
7425 name: "prebuilt_preferred",
7426 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7427 usePrebuilt: true,
7428 modNames: []string{"stublib", "prebuilt_stublib"},
7429 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7430 },
7431 {
7432 name: "only_prebuilt",
7433 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7434 usePrebuilt: true,
7435 modNames: []string{"stublib"},
7436 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7437 },
7438 }
7439
7440 for _, test := range tests {
7441 t.Run(test.name, func(t *testing.T) {
7442 for _, otherApexEnabled := range test.otherApexEnabled {
7443 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007444 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007445
7446 type modAndMkEntries struct {
7447 mod *cc.Module
7448 mkEntries android.AndroidMkEntries
7449 }
7450 entries := []*modAndMkEntries{}
7451
7452 // Gather shared lib modules that are installable
7453 for _, modName := range test.modNames {
7454 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7455 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7456 continue
7457 }
7458 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007459 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007460 continue
7461 }
Colin Crossaa255532020-07-03 13:18:24 -07007462 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007463 if ent.Disabled {
7464 continue
7465 }
7466 entries = append(entries, &modAndMkEntries{
7467 mod: mod,
7468 mkEntries: ent,
7469 })
7470 }
7471 }
7472 }
7473
7474 var entry *modAndMkEntries = nil
7475 for _, ent := range entries {
7476 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7477 if entry != nil {
7478 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7479 } else {
7480 entry = ent
7481 }
7482 }
7483 }
7484
7485 if entry == nil {
7486 t.Errorf("AndroidMk entry for \"stublib\" missing")
7487 } else {
7488 isPrebuilt := entry.mod.Prebuilt() != nil
7489 if isPrebuilt != test.usePrebuilt {
7490 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7491 }
7492 if !entry.mod.IsStubs() {
7493 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7494 }
7495 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7496 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7497 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007498 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09007499 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09007500 if !android.InList(expected, cflags) {
7501 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7502 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007503 }
7504 })
7505 }
7506 })
7507 }
7508}
7509
Colin Crosscbb2b812021-05-25 18:16:02 -07007510func TestApexJavaCoverage(t *testing.T) {
7511 bp := `
7512 apex {
7513 name: "myapex",
7514 key: "myapex.key",
7515 java_libs: ["mylib"],
7516 bootclasspath_fragments: ["mybootclasspathfragment"],
7517 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
7518 updatable: false,
7519 }
7520
7521 apex_key {
7522 name: "myapex.key",
7523 public_key: "testkey.avbpubkey",
7524 private_key: "testkey.pem",
7525 }
7526
7527 java_library {
7528 name: "mylib",
7529 srcs: ["mylib.java"],
7530 apex_available: ["myapex"],
7531 compile_dex: true,
7532 }
7533
7534 bootclasspath_fragment {
7535 name: "mybootclasspathfragment",
7536 contents: ["mybootclasspathlib"],
7537 apex_available: ["myapex"],
7538 }
7539
7540 java_library {
7541 name: "mybootclasspathlib",
7542 srcs: ["mybootclasspathlib.java"],
7543 apex_available: ["myapex"],
7544 compile_dex: true,
7545 }
7546
7547 systemserverclasspath_fragment {
7548 name: "mysystemserverclasspathfragment",
7549 contents: ["mysystemserverclasspathlib"],
7550 apex_available: ["myapex"],
7551 }
7552
7553 java_library {
7554 name: "mysystemserverclasspathlib",
7555 srcs: ["mysystemserverclasspathlib.java"],
7556 apex_available: ["myapex"],
7557 compile_dex: true,
7558 }
7559 `
7560
7561 result := android.GroupFixturePreparers(
7562 PrepareForTestWithApexBuildComponents,
7563 prepareForTestWithMyapex,
7564 java.PrepareForTestWithJavaDefaultModules,
7565 android.PrepareForTestWithAndroidBuildComponents,
7566 android.FixtureWithRootAndroidBp(bp),
7567 android.FixtureMergeEnv(map[string]string{
7568 "EMMA_INSTRUMENT": "true",
7569 }),
7570 ).RunTest(t)
7571
7572 // Make sure jacoco ran on both mylib and mybootclasspathlib
7573 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
7574 t.Errorf("Failed to find jacoco rule for mylib")
7575 }
7576 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
7577 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
7578 }
7579 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
7580 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
7581 }
7582}
7583
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007584func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01007585 os.Exit(m.Run())
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007586}