blob: 54aead7219c5317e644d863a236af87ff0752e20 [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"],
395 }
396
397 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000398 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900399 srcs: ["foo.rs"],
400 crate_name: "foo",
401 apex_available: ["myapex"],
402 }
403
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900404 rust_ffi_shared {
405 name: "libfoo.ffi",
406 srcs: ["foo.rs"],
407 crate_name: "foo",
408 apex_available: ["myapex"],
409 }
410
411 rust_ffi_shared {
412 name: "libbar.ffi",
413 srcs: ["foo.rs"],
414 crate_name: "bar",
415 apex_available: ["myapex"],
416 }
417
Yifan Hongd22a84a2020-07-28 17:37:46 -0700418 apex {
419 name: "com.android.gki.fake",
420 binaries: ["foo"],
421 key: "myapex.key",
422 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000423 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800424 }
425
Paul Duffindddd5462020-04-07 15:25:44 +0100426 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900427 name: "mylib2",
428 srcs: ["mylib.cpp"],
429 system_shared_libs: [],
430 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900431 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900432 static_libs: ["libstatic"],
433 // TODO: remove //apex_available:platform
434 apex_available: [
435 "//apex_available:platform",
436 "myapex",
437 ],
438 }
439
Paul Duffindddd5462020-04-07 15:25:44 +0100440 cc_prebuilt_library_shared {
441 name: "mylib2",
442 srcs: ["prebuilt.so"],
443 // TODO: remove //apex_available:platform
444 apex_available: [
445 "//apex_available:platform",
446 "myapex",
447 ],
448 }
449
Jiyong Park9918e1a2020-03-17 19:16:40 +0900450 cc_library_static {
451 name: "libstatic",
452 srcs: ["mylib.cpp"],
453 system_shared_libs: [],
454 stl: "none",
455 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000456 // TODO: remove //apex_available:platform
457 apex_available: [
458 "//apex_available:platform",
459 "myapex",
460 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900461 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900462
463 java_library {
464 name: "myjar",
465 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900466 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900467 sdk_version: "none",
468 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900469 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900470 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000471 // TODO: remove //apex_available:platform
472 apex_available: [
473 "//apex_available:platform",
474 "myapex",
475 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900476 }
477
Jiyong Park77acec62020-06-01 21:39:15 +0900478 dex_import {
479 name: "myjar_dex",
480 jars: ["prebuilt.jar"],
481 apex_available: [
482 "//apex_available:platform",
483 "myapex",
484 ],
485 }
486
Jiyong Park7f7766d2019-07-25 22:02:35 +0900487 java_library {
488 name: "myotherjar",
489 srcs: ["foo/bar/MyClass.java"],
490 sdk_version: "none",
491 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900492 // TODO: remove //apex_available:platform
493 apex_available: [
494 "//apex_available:platform",
495 "myapex",
496 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900497 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900498
499 java_library {
500 name: "mysharedjar",
501 srcs: ["foo/bar/MyClass.java"],
502 sdk_version: "none",
503 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900504 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900505 `)
506
Paul Duffina71a67a2021-03-29 00:42:57 +0100507 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900508
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900509 // Make sure that Android.mk is created
510 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700511 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900512 var builder strings.Builder
513 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
514
515 androidMk := builder.String()
516 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
517 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
518
Jiyong Park42cca6c2019-04-01 11:15:50 +0900519 optFlags := apexRule.Args["opt_flags"]
520 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700521 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100522 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900523
Jiyong Park25fc6a92018-11-18 18:02:45 +0900524 copyCmds := apexRule.Args["copy_commands"]
525
526 // Ensure that main rule creates an output
527 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
528
529 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700530 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
531 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
532 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900533 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900534 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900535
536 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700537 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
538 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900539 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
540 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900541 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900542
543 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800544 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
545 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900546 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900547 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900548 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900549 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
550 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900551 // .. but not for java libs
552 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900553 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800554
Colin Cross7113d202019-11-20 16:39:12 -0800555 // Ensure that the platform variant ends with _shared or _common
556 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
557 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900558 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
559 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900560 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
561
562 // Ensure that dynamic dependency to java libs are not included
563 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800564
565 // Ensure that all symlinks are present.
566 found_foo_link_64 := false
567 found_foo := false
568 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900569 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800570 if strings.HasSuffix(cmd, "bin/foo") {
571 found_foo = true
572 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
573 found_foo_link_64 = true
574 }
575 }
576 }
577 good := found_foo && found_foo_link_64
578 if !good {
579 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
580 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900581
Sundong Ahnabb64432019-10-22 13:58:29 +0900582 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700583 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900584 if len(noticeInputs) != 3 {
585 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900586 }
587 ensureListContains(t, noticeInputs, "NOTICE")
588 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900589 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900590
Artur Satayeva8bd1132020-04-27 18:07:06 +0100591 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100592 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100593 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
594 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
595 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100596
597 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100598 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100599 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
600 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
601 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800602}
603
Jooyung Hanf21c7972019-12-16 22:32:06 +0900604func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800605 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900606 apex_defaults {
607 name: "myapex-defaults",
608 key: "myapex.key",
609 prebuilts: ["myetc"],
610 native_shared_libs: ["mylib"],
611 java_libs: ["myjar"],
612 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900613 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800614 bpfs: ["bpf"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000615 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900616 }
617
618 prebuilt_etc {
619 name: "myetc",
620 src: "myprebuilt",
621 }
622
623 apex {
624 name: "myapex",
625 defaults: ["myapex-defaults"],
626 }
627
628 apex_key {
629 name: "myapex.key",
630 public_key: "testkey.avbpubkey",
631 private_key: "testkey.pem",
632 }
633
634 cc_library {
635 name: "mylib",
636 system_shared_libs: [],
637 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000638 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900639 }
640
641 java_library {
642 name: "myjar",
643 srcs: ["foo/bar/MyClass.java"],
644 sdk_version: "none",
645 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000646 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900647 }
648
649 android_app {
650 name: "AppFoo",
651 srcs: ["foo/bar/MyClass.java"],
652 sdk_version: "none",
653 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000654 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900655 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900656
657 runtime_resource_overlay {
658 name: "rro",
659 theme: "blue",
660 }
661
markchien2f59ec92020-09-02 16:23:38 +0800662 bpf {
663 name: "bpf",
664 srcs: ["bpf.c", "bpf2.c"],
665 }
666
Jooyung Hanf21c7972019-12-16 22:32:06 +0900667 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000668 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900669 "etc/myetc",
670 "javalib/myjar.jar",
671 "lib64/mylib.so",
672 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900673 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800674 "etc/bpf/bpf.o",
675 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900676 })
677}
678
Jooyung Han01a3ee22019-11-02 02:52:25 +0900679func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800680 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900681 apex {
682 name: "myapex",
683 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000684 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900685 }
686
687 apex_key {
688 name: "myapex.key",
689 public_key: "testkey.avbpubkey",
690 private_key: "testkey.pem",
691 }
692 `)
693
694 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900695 args := module.Rule("apexRule").Args
696 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
697 t.Error("manifest should be apex_manifest.pb, but " + manifest)
698 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900699}
700
Alex Light5098a612018-11-29 17:12:15 -0800701func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800702 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800703 apex {
704 name: "myapex",
705 key: "myapex.key",
706 payload_type: "zip",
707 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000708 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800709 }
710
711 apex_key {
712 name: "myapex.key",
713 public_key: "testkey.avbpubkey",
714 private_key: "testkey.pem",
715 }
716
717 cc_library {
718 name: "mylib",
719 srcs: ["mylib.cpp"],
720 shared_libs: ["mylib2"],
721 system_shared_libs: [],
722 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000723 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800724 }
725
726 cc_library {
727 name: "mylib2",
728 srcs: ["mylib.cpp"],
729 system_shared_libs: [],
730 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000731 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800732 }
733 `)
734
Sundong Ahnabb64432019-10-22 13:58:29 +0900735 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800736 copyCmds := zipApexRule.Args["copy_commands"]
737
738 // Ensure that main rule creates an output
739 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
740
741 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700742 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800743
744 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700745 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800746
747 // Ensure that both direct and indirect deps are copied into apex
748 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
749 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900750}
751
752func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800753 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900754 apex {
755 name: "myapex",
756 key: "myapex.key",
757 native_shared_libs: ["mylib", "mylib3"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000758 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900759 }
760
761 apex_key {
762 name: "myapex.key",
763 public_key: "testkey.avbpubkey",
764 private_key: "testkey.pem",
765 }
766
767 cc_library {
768 name: "mylib",
769 srcs: ["mylib.cpp"],
770 shared_libs: ["mylib2", "mylib3"],
771 system_shared_libs: [],
772 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000773 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900774 }
775
776 cc_library {
777 name: "mylib2",
778 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900779 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900780 system_shared_libs: [],
781 stl: "none",
782 stubs: {
783 versions: ["1", "2", "3"],
784 },
785 }
786
787 cc_library {
788 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900789 srcs: ["mylib.cpp"],
790 shared_libs: ["mylib4"],
791 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900792 stl: "none",
793 stubs: {
794 versions: ["10", "11", "12"],
795 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000796 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900797 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900798
799 cc_library {
800 name: "mylib4",
801 srcs: ["mylib.cpp"],
802 system_shared_libs: [],
803 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000804 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900805 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900806 `)
807
Sundong Ahnabb64432019-10-22 13:58:29 +0900808 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900809 copyCmds := apexRule.Args["copy_commands"]
810
811 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800812 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900813
814 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800815 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900816
817 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800818 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900819
Colin Crossaede88c2020-08-11 12:17:01 -0700820 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900821
822 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900823 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900824 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900825 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900826
827 // 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 -0700828 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900829 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700830 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900831
832 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900833 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900834 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900835
836 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700837 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900838
Jooyung Hana57af4a2020-01-23 05:36:59 +0000839 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900840 "lib64/mylib.so",
841 "lib64/mylib3.so",
842 "lib64/mylib4.so",
843 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900844}
845
Colin Cross7812fd32020-09-25 12:35:10 -0700846func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
847 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -0800848 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -0700849 apex {
850 name: "myapex",
851 key: "myapex.key",
852 native_shared_libs: ["mylib", "mylib3"],
853 min_sdk_version: "29",
854 }
855
856 apex_key {
857 name: "myapex.key",
858 public_key: "testkey.avbpubkey",
859 private_key: "testkey.pem",
860 }
861
862 cc_library {
863 name: "mylib",
864 srcs: ["mylib.cpp"],
865 shared_libs: ["mylib2", "mylib3"],
866 system_shared_libs: [],
867 stl: "none",
868 apex_available: [ "myapex" ],
869 min_sdk_version: "28",
870 }
871
872 cc_library {
873 name: "mylib2",
874 srcs: ["mylib.cpp"],
875 cflags: ["-include mylib.h"],
876 system_shared_libs: [],
877 stl: "none",
878 stubs: {
879 versions: ["28", "29", "30", "current"],
880 },
881 min_sdk_version: "28",
882 }
883
884 cc_library {
885 name: "mylib3",
886 srcs: ["mylib.cpp"],
887 shared_libs: ["mylib4"],
888 system_shared_libs: [],
889 stl: "none",
890 stubs: {
891 versions: ["28", "29", "30", "current"],
892 },
893 apex_available: [ "myapex" ],
894 min_sdk_version: "28",
895 }
896
897 cc_library {
898 name: "mylib4",
899 srcs: ["mylib.cpp"],
900 system_shared_libs: [],
901 stl: "none",
902 apex_available: [ "myapex" ],
903 min_sdk_version: "28",
904 }
905 `)
906
907 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
908 copyCmds := apexRule.Args["copy_commands"]
909
910 // Ensure that direct non-stubs dep is always included
911 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
912
913 // Ensure that indirect stubs dep is not included
914 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
915
916 // Ensure that direct stubs dep is included
917 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
918
919 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
920
Jiyong Park55549df2021-02-26 23:57:23 +0900921 // Ensure that mylib is linking with the latest version of stub for mylib2
922 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -0700923 // ... and not linking to the non-stub (impl) variant of mylib2
924 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
925
926 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
927 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
928 // .. and not linking to the stubs variant of mylib3
929 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
930
931 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -0700932 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -0700933 ensureNotContains(t, mylib2Cflags, "-include ")
934
935 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700936 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -0700937
938 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
939 "lib64/mylib.so",
940 "lib64/mylib3.so",
941 "lib64/mylib4.so",
942 })
943}
944
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900945func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
946 t.Parallel()
947 // myapex (Z)
948 // mylib -----------------.
949 // |
950 // otherapex (29) |
951 // libstub's versions: 29 Z current
952 // |
953 // <platform> |
954 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -0800955 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900956 apex {
957 name: "myapex",
958 key: "myapex.key",
959 native_shared_libs: ["mylib"],
960 min_sdk_version: "Z", // non-final
961 }
962
963 cc_library {
964 name: "mylib",
965 srcs: ["mylib.cpp"],
966 shared_libs: ["libstub"],
967 apex_available: ["myapex"],
968 min_sdk_version: "Z",
969 }
970
971 apex_key {
972 name: "myapex.key",
973 public_key: "testkey.avbpubkey",
974 private_key: "testkey.pem",
975 }
976
977 apex {
978 name: "otherapex",
979 key: "myapex.key",
980 native_shared_libs: ["libstub"],
981 min_sdk_version: "29",
982 }
983
984 cc_library {
985 name: "libstub",
986 srcs: ["mylib.cpp"],
987 stubs: {
988 versions: ["29", "Z", "current"],
989 },
990 apex_available: ["otherapex"],
991 min_sdk_version: "29",
992 }
993
994 // platform module depending on libstub from otherapex should use the latest stub("current")
995 cc_library {
996 name: "libplatform",
997 srcs: ["mylib.cpp"],
998 shared_libs: ["libstub"],
999 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001000 `,
1001 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1002 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1003 variables.Platform_sdk_final = proptools.BoolPtr(false)
1004 variables.Platform_version_active_codenames = []string{"Z"}
1005 }),
1006 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001007
Jiyong Park55549df2021-02-26 23:57:23 +09001008 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001009 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001010 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001011 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001012 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001013
1014 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1015 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1016 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1017 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1018 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1019}
1020
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001021func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001022 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001023 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001024 name: "myapex2",
1025 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001026 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001027 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001028 }
1029
1030 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001031 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001032 public_key: "testkey.avbpubkey",
1033 private_key: "testkey.pem",
1034 }
1035
1036 cc_library {
1037 name: "mylib",
1038 srcs: ["mylib.cpp"],
1039 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001040 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001041 system_shared_libs: [],
1042 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001043 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001044 }
1045
1046 cc_library {
1047 name: "libfoo",
1048 srcs: ["mylib.cpp"],
1049 shared_libs: ["libbar"],
1050 system_shared_libs: [],
1051 stl: "none",
1052 stubs: {
1053 versions: ["10", "20", "30"],
1054 },
1055 }
1056
1057 cc_library {
1058 name: "libbar",
1059 srcs: ["mylib.cpp"],
1060 system_shared_libs: [],
1061 stl: "none",
1062 }
1063
Jiyong Park678c8812020-02-07 17:25:49 +09001064 cc_library_static {
1065 name: "libbaz",
1066 srcs: ["mylib.cpp"],
1067 system_shared_libs: [],
1068 stl: "none",
1069 apex_available: [ "myapex2" ],
1070 }
1071
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001072 `)
1073
Jiyong Park83dc74b2020-01-14 18:38:44 +09001074 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001075 copyCmds := apexRule.Args["copy_commands"]
1076
1077 // Ensure that direct non-stubs dep is always included
1078 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1079
1080 // Ensure that indirect stubs dep is not included
1081 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1082
1083 // Ensure that dependency of stubs is not included
1084 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1085
Colin Crossaede88c2020-08-11 12:17:01 -07001086 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001087
1088 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001089 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001090 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001091 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001092
Jiyong Park3ff16992019-12-27 14:11:47 +09001093 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001094
1095 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1096 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001097
Artur Satayeva8bd1132020-04-27 18:07:06 +01001098 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001099 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001100
Artur Satayeva8bd1132020-04-27 18:07:06 +01001101 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001102 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001103}
1104
Jooyung Hand3639552019-08-09 12:57:43 +09001105func TestApexWithRuntimeLibsDependency(t *testing.T) {
1106 /*
1107 myapex
1108 |
1109 v (runtime_libs)
1110 mylib ------+------> libfoo [provides stub]
1111 |
1112 `------> libbar
1113 */
Colin Cross1c460562021-02-16 17:55:47 -08001114 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001115 apex {
1116 name: "myapex",
1117 key: "myapex.key",
1118 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001119 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001120 }
1121
1122 apex_key {
1123 name: "myapex.key",
1124 public_key: "testkey.avbpubkey",
1125 private_key: "testkey.pem",
1126 }
1127
1128 cc_library {
1129 name: "mylib",
1130 srcs: ["mylib.cpp"],
1131 runtime_libs: ["libfoo", "libbar"],
1132 system_shared_libs: [],
1133 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001134 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001135 }
1136
1137 cc_library {
1138 name: "libfoo",
1139 srcs: ["mylib.cpp"],
1140 system_shared_libs: [],
1141 stl: "none",
1142 stubs: {
1143 versions: ["10", "20", "30"],
1144 },
1145 }
1146
1147 cc_library {
1148 name: "libbar",
1149 srcs: ["mylib.cpp"],
1150 system_shared_libs: [],
1151 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001152 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001153 }
1154
1155 `)
1156
Sundong Ahnabb64432019-10-22 13:58:29 +09001157 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001158 copyCmds := apexRule.Args["copy_commands"]
1159
1160 // Ensure that direct non-stubs dep is always included
1161 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1162
1163 // Ensure that indirect stubs dep is not included
1164 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1165
1166 // Ensure that runtime_libs dep in included
1167 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1168
Sundong Ahnabb64432019-10-22 13:58:29 +09001169 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001170 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1171 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001172
1173}
1174
Paul Duffina02cae32021-03-09 01:44:06 +00001175var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1176 cc.PrepareForTestWithCcBuildComponents,
1177 PrepareForTestWithApexBuildComponents,
1178 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001179 apex {
1180 name: "com.android.runtime",
1181 key: "com.android.runtime.key",
1182 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001183 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001184 }
1185
1186 apex_key {
1187 name: "com.android.runtime.key",
1188 public_key: "testkey.avbpubkey",
1189 private_key: "testkey.pem",
1190 }
Paul Duffina02cae32021-03-09 01:44:06 +00001191 `),
1192 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1193)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001194
Paul Duffina02cae32021-03-09 01:44:06 +00001195func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001196 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001197 cc_library {
1198 name: "libc",
1199 no_libcrt: true,
1200 nocrt: true,
1201 stl: "none",
1202 system_shared_libs: [],
1203 stubs: { versions: ["1"] },
1204 apex_available: ["com.android.runtime"],
1205
1206 sanitize: {
1207 hwaddress: true,
1208 }
1209 }
1210
1211 cc_prebuilt_library_shared {
1212 name: "libclang_rt.hwasan-aarch64-android",
1213 no_libcrt: true,
1214 nocrt: true,
1215 stl: "none",
1216 system_shared_libs: [],
1217 srcs: [""],
1218 stubs: { versions: ["1"] },
1219
1220 sanitize: {
1221 never: true,
1222 },
Paul Duffina02cae32021-03-09 01:44:06 +00001223 } `)
1224 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001225
1226 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1227 "lib64/bionic/libc.so",
1228 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1229 })
1230
1231 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1232
1233 installed := hwasan.Description("install libclang_rt.hwasan")
1234 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1235
1236 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1237 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1238 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1239}
1240
1241func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001242 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001243 prepareForTestOfRuntimeApexWithHwasan,
1244 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1245 variables.SanitizeDevice = []string{"hwaddress"}
1246 }),
1247 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001248 cc_library {
1249 name: "libc",
1250 no_libcrt: true,
1251 nocrt: true,
1252 stl: "none",
1253 system_shared_libs: [],
1254 stubs: { versions: ["1"] },
1255 apex_available: ["com.android.runtime"],
1256 }
1257
1258 cc_prebuilt_library_shared {
1259 name: "libclang_rt.hwasan-aarch64-android",
1260 no_libcrt: true,
1261 nocrt: true,
1262 stl: "none",
1263 system_shared_libs: [],
1264 srcs: [""],
1265 stubs: { versions: ["1"] },
1266
1267 sanitize: {
1268 never: true,
1269 },
1270 }
Paul Duffina02cae32021-03-09 01:44:06 +00001271 `)
1272 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001273
1274 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1275 "lib64/bionic/libc.so",
1276 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1277 })
1278
1279 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1280
1281 installed := hwasan.Description("install libclang_rt.hwasan")
1282 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1283
1284 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1285 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1286 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1287}
1288
Jooyung Han61b66e92020-03-21 14:21:46 +00001289func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1290 testcases := []struct {
1291 name string
1292 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001293 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001294 shouldLink string
1295 shouldNotLink []string
1296 }{
1297 {
Jiyong Park55549df2021-02-26 23:57:23 +09001298 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001299 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001300 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001301 shouldLink: "current",
1302 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001303 },
1304 {
Jiyong Park55549df2021-02-26 23:57:23 +09001305 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001306 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001307 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001308 shouldLink: "current",
1309 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001310 },
1311 }
1312 for _, tc := range testcases {
1313 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001314 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001315 apex {
1316 name: "myapex",
1317 key: "myapex.key",
1318 use_vendor: true,
1319 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001320 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001321 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001322 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001323
Jooyung Han61b66e92020-03-21 14:21:46 +00001324 apex_key {
1325 name: "myapex.key",
1326 public_key: "testkey.avbpubkey",
1327 private_key: "testkey.pem",
1328 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001329
Jooyung Han61b66e92020-03-21 14:21:46 +00001330 cc_library {
1331 name: "mylib",
1332 srcs: ["mylib.cpp"],
1333 vendor_available: true,
1334 shared_libs: ["libbar"],
1335 system_shared_libs: [],
1336 stl: "none",
1337 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001338 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001339 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001340
Jooyung Han61b66e92020-03-21 14:21:46 +00001341 cc_library {
1342 name: "libbar",
1343 srcs: ["mylib.cpp"],
1344 system_shared_libs: [],
1345 stl: "none",
1346 stubs: { versions: ["29","30"] },
Colin Cross0477b422020-10-13 18:43:54 -07001347 llndk_stubs: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001348 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001349
Jooyung Han61b66e92020-03-21 14:21:46 +00001350 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001351 name: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001352 symbol_file: "",
1353 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001354 `,
1355 setUseVendorAllowListForTest([]string{"myapex"}),
1356 withUnbundledBuild,
1357 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001358
Jooyung Han61b66e92020-03-21 14:21:46 +00001359 // Ensure that LLNDK dep is not included
1360 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1361 "lib64/mylib.so",
1362 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001363
Jooyung Han61b66e92020-03-21 14:21:46 +00001364 // Ensure that LLNDK dep is required
1365 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1366 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1367 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001368
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001369 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.29_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1370 ensureContains(t, mylibLdFlags, "libbar/android_vendor.29_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001371 for _, ver := range tc.shouldNotLink {
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001372 ensureNotContains(t, mylibLdFlags, "libbar/android_vendor.29_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001373 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001374
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001375 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.29_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001376 ver := tc.shouldLink
1377 if tc.shouldLink == "current" {
1378 ver = strconv.Itoa(android.FutureApiLevelInt)
1379 }
1380 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001381 })
1382 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001383}
1384
Jiyong Park25fc6a92018-11-18 18:02:45 +09001385func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001386 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001387 apex {
1388 name: "myapex",
1389 key: "myapex.key",
1390 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001391 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001392 }
1393
1394 apex_key {
1395 name: "myapex.key",
1396 public_key: "testkey.avbpubkey",
1397 private_key: "testkey.pem",
1398 }
1399
1400 cc_library {
1401 name: "mylib",
1402 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001403 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001404 shared_libs: ["libdl#27"],
1405 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001406 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001407 }
1408
1409 cc_library_shared {
1410 name: "mylib_shared",
1411 srcs: ["mylib.cpp"],
1412 shared_libs: ["libdl#27"],
1413 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001414 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001415 }
1416
1417 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001418 name: "libBootstrap",
1419 srcs: ["mylib.cpp"],
1420 stl: "none",
1421 bootstrap: true,
1422 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001423 `)
1424
Sundong Ahnabb64432019-10-22 13:58:29 +09001425 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001426 copyCmds := apexRule.Args["copy_commands"]
1427
1428 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001429 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001430 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1431 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001432
1433 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001434 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001435
Colin Crossaede88c2020-08-11 12:17:01 -07001436 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1437 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1438 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001439
1440 // For dependency to libc
1441 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001442 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001443 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001444 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001445 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001446 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1447 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001448
1449 // For dependency to libm
1450 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001451 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001452 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001453 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001454 // ... and is not compiling with the stub
1455 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1456 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1457
1458 // For dependency to libdl
1459 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001460 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001461 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001462 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1463 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001464 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001465 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001466 // ... Cflags from stub is correctly exported to mylib
1467 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1468 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001469
1470 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001471 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1472 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1473 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1474 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001475}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001476
Jooyung Han749dc692020-04-15 11:03:39 +09001477func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001478 // there are three links between liba --> libz.
1479 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001480 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001481 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001482 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001483 apex {
1484 name: "myapex",
1485 key: "myapex.key",
1486 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001487 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001488 }
1489
1490 apex {
1491 name: "otherapex",
1492 key: "myapex.key",
1493 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001494 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001495 }
1496
1497 apex_key {
1498 name: "myapex.key",
1499 public_key: "testkey.avbpubkey",
1500 private_key: "testkey.pem",
1501 }
1502
1503 cc_library {
1504 name: "libx",
1505 shared_libs: ["liba"],
1506 system_shared_libs: [],
1507 stl: "none",
1508 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001509 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001510 }
1511
1512 cc_library {
1513 name: "liby",
1514 shared_libs: ["liba"],
1515 system_shared_libs: [],
1516 stl: "none",
1517 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001518 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001519 }
1520
1521 cc_library {
1522 name: "liba",
1523 shared_libs: ["libz"],
1524 system_shared_libs: [],
1525 stl: "none",
1526 apex_available: [
1527 "//apex_available:anyapex",
1528 "//apex_available:platform",
1529 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001530 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001531 }
1532
1533 cc_library {
1534 name: "libz",
1535 system_shared_libs: [],
1536 stl: "none",
1537 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001538 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001539 },
1540 }
Jooyung Han749dc692020-04-15 11:03:39 +09001541 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001542
1543 expectLink := func(from, from_variant, to, to_variant string) {
1544 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1545 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1546 }
1547 expectNoLink := func(from, from_variant, to, to_variant string) {
1548 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1549 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1550 }
1551 // platform liba is linked to non-stub version
1552 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001553 // liba in myapex is linked to current
1554 expectLink("liba", "shared_apex29", "libz", "shared_current")
1555 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001556 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001557 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001558 // liba in otherapex is linked to current
1559 expectLink("liba", "shared_apex30", "libz", "shared_current")
1560 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001561 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1562 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001563}
1564
Jooyung Hanaed150d2020-04-02 01:41:41 +09001565func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001566 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001567 apex {
1568 name: "myapex",
1569 key: "myapex.key",
1570 native_shared_libs: ["libx"],
1571 min_sdk_version: "R",
1572 }
1573
1574 apex_key {
1575 name: "myapex.key",
1576 public_key: "testkey.avbpubkey",
1577 private_key: "testkey.pem",
1578 }
1579
1580 cc_library {
1581 name: "libx",
1582 shared_libs: ["libz"],
1583 system_shared_libs: [],
1584 stl: "none",
1585 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001586 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001587 }
1588
1589 cc_library {
1590 name: "libz",
1591 system_shared_libs: [],
1592 stl: "none",
1593 stubs: {
1594 versions: ["29", "R"],
1595 },
1596 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001597 `,
1598 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1599 variables.Platform_version_active_codenames = []string{"R"}
1600 }),
1601 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001602
1603 expectLink := func(from, from_variant, to, to_variant string) {
1604 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1605 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1606 }
1607 expectNoLink := func(from, from_variant, to, to_variant string) {
1608 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1609 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1610 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001611 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1612 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001613 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1614 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001615}
1616
Jooyung Han749dc692020-04-15 11:03:39 +09001617func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001618 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001619 apex {
1620 name: "myapex",
1621 key: "myapex.key",
1622 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001623 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001624 }
1625
1626 apex_key {
1627 name: "myapex.key",
1628 public_key: "testkey.avbpubkey",
1629 private_key: "testkey.pem",
1630 }
1631
1632 cc_library {
1633 name: "libx",
1634 shared_libs: ["libz"],
1635 system_shared_libs: [],
1636 stl: "none",
1637 apex_available: [ "myapex" ],
1638 }
1639
1640 cc_library {
1641 name: "libz",
1642 system_shared_libs: [],
1643 stl: "none",
1644 stubs: {
1645 versions: ["1", "2"],
1646 },
1647 }
1648 `)
1649
1650 expectLink := func(from, from_variant, to, to_variant string) {
1651 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1652 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1653 }
1654 expectNoLink := func(from, from_variant, to, to_variant string) {
1655 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1656 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1657 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001658 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001659 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001660 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001661 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001662}
1663
1664func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001665 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001666 apex {
1667 name: "myapex",
1668 key: "myapex.key",
1669 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001670 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001671 }
1672
1673 apex_key {
1674 name: "myapex.key",
1675 public_key: "testkey.avbpubkey",
1676 private_key: "testkey.pem",
1677 }
1678
1679 cc_library {
1680 name: "libx",
1681 system_shared_libs: [],
1682 stl: "none",
1683 apex_available: [ "myapex" ],
1684 stubs: {
1685 versions: ["1", "2"],
1686 },
1687 }
1688
1689 cc_library {
1690 name: "libz",
1691 shared_libs: ["libx"],
1692 system_shared_libs: [],
1693 stl: "none",
1694 }
1695 `)
1696
1697 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001698 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001699 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1700 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1701 }
1702 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001703 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001704 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1705 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1706 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001707 expectLink("libz", "shared", "libx", "shared_current")
1708 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09001709 expectNoLink("libz", "shared", "libz", "shared_1")
1710 expectNoLink("libz", "shared", "libz", "shared")
1711}
1712
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001713var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1714 func(variables android.FixtureProductVariables) {
1715 variables.SanitizeDevice = []string{"hwaddress"}
1716 },
1717)
1718
Jooyung Han75568392020-03-20 04:29:24 +09001719func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001720 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001721 apex {
1722 name: "myapex",
1723 key: "myapex.key",
1724 native_shared_libs: ["libx"],
1725 min_sdk_version: "29",
1726 }
1727
1728 apex_key {
1729 name: "myapex.key",
1730 public_key: "testkey.avbpubkey",
1731 private_key: "testkey.pem",
1732 }
1733
1734 cc_library {
1735 name: "libx",
1736 shared_libs: ["libbar"],
1737 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001738 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001739 }
1740
1741 cc_library {
1742 name: "libbar",
1743 stubs: {
1744 versions: ["29", "30"],
1745 },
1746 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001747 `,
1748 prepareForTestWithSantitizeHwaddress,
1749 )
Jooyung Han03b51852020-02-26 22:45:42 +09001750 expectLink := func(from, from_variant, to, to_variant string) {
1751 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1752 libFlags := ld.Args["libFlags"]
1753 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1754 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001755 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09001756}
1757
Jooyung Han75568392020-03-20 04:29:24 +09001758func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001759 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001760 apex {
1761 name: "myapex",
1762 key: "myapex.key",
1763 native_shared_libs: ["libx"],
1764 min_sdk_version: "29",
1765 }
1766
1767 apex_key {
1768 name: "myapex.key",
1769 public_key: "testkey.avbpubkey",
1770 private_key: "testkey.pem",
1771 }
1772
1773 cc_library {
1774 name: "libx",
1775 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001776 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001777 }
Jooyung Han75568392020-03-20 04:29:24 +09001778 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001779
1780 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001781 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001782 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001783 // note that platform variant is not.
1784 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001785 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001786}
1787
Jooyung Han749dc692020-04-15 11:03:39 +09001788func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1789 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001790 apex {
1791 name: "myapex",
1792 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001793 native_shared_libs: ["mylib"],
1794 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001795 }
1796
1797 apex_key {
1798 name: "myapex.key",
1799 public_key: "testkey.avbpubkey",
1800 private_key: "testkey.pem",
1801 }
Jooyung Han749dc692020-04-15 11:03:39 +09001802
1803 cc_library {
1804 name: "mylib",
1805 srcs: ["mylib.cpp"],
1806 system_shared_libs: [],
1807 stl: "none",
1808 apex_available: [
1809 "myapex",
1810 ],
1811 min_sdk_version: "30",
1812 }
1813 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001814
1815 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
1816 apex {
1817 name: "myapex",
1818 key: "myapex.key",
1819 native_shared_libs: ["libfoo.ffi"],
1820 min_sdk_version: "29",
1821 }
1822
1823 apex_key {
1824 name: "myapex.key",
1825 public_key: "testkey.avbpubkey",
1826 private_key: "testkey.pem",
1827 }
1828
1829 rust_ffi_shared {
1830 name: "libfoo.ffi",
1831 srcs: ["foo.rs"],
1832 crate_name: "foo",
1833 apex_available: [
1834 "myapex",
1835 ],
1836 min_sdk_version: "30",
1837 }
1838 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001839
1840 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
1841 apex {
1842 name: "myapex",
1843 key: "myapex.key",
1844 java_libs: ["libfoo"],
1845 min_sdk_version: "29",
1846 }
1847
1848 apex_key {
1849 name: "myapex.key",
1850 public_key: "testkey.avbpubkey",
1851 private_key: "testkey.pem",
1852 }
1853
1854 java_import {
1855 name: "libfoo",
1856 jars: ["libfoo.jar"],
1857 apex_available: [
1858 "myapex",
1859 ],
1860 min_sdk_version: "30",
1861 }
1862 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001863}
1864
1865func TestApexMinSdkVersion_Okay(t *testing.T) {
1866 testApex(t, `
1867 apex {
1868 name: "myapex",
1869 key: "myapex.key",
1870 native_shared_libs: ["libfoo"],
1871 java_libs: ["libbar"],
1872 min_sdk_version: "29",
1873 }
1874
1875 apex_key {
1876 name: "myapex.key",
1877 public_key: "testkey.avbpubkey",
1878 private_key: "testkey.pem",
1879 }
1880
1881 cc_library {
1882 name: "libfoo",
1883 srcs: ["mylib.cpp"],
1884 shared_libs: ["libfoo_dep"],
1885 apex_available: ["myapex"],
1886 min_sdk_version: "29",
1887 }
1888
1889 cc_library {
1890 name: "libfoo_dep",
1891 srcs: ["mylib.cpp"],
1892 apex_available: ["myapex"],
1893 min_sdk_version: "29",
1894 }
1895
1896 java_library {
1897 name: "libbar",
1898 sdk_version: "current",
1899 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001900 static_libs: [
1901 "libbar_dep",
1902 "libbar_import_dep",
1903 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001904 apex_available: ["myapex"],
1905 min_sdk_version: "29",
1906 }
1907
1908 java_library {
1909 name: "libbar_dep",
1910 sdk_version: "current",
1911 srcs: ["a.java"],
1912 apex_available: ["myapex"],
1913 min_sdk_version: "29",
1914 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001915
1916 java_import {
1917 name: "libbar_import_dep",
1918 jars: ["libbar.jar"],
1919 apex_available: ["myapex"],
1920 min_sdk_version: "29",
1921 }
Jooyung Han03b51852020-02-26 22:45:42 +09001922 `)
1923}
1924
Artur Satayev8cf899a2020-04-15 17:29:42 +01001925func TestJavaStableSdkVersion(t *testing.T) {
1926 testCases := []struct {
1927 name string
1928 expectedError string
1929 bp string
1930 }{
1931 {
1932 name: "Non-updatable apex with non-stable dep",
1933 bp: `
1934 apex {
1935 name: "myapex",
1936 java_libs: ["myjar"],
1937 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001938 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01001939 }
1940 apex_key {
1941 name: "myapex.key",
1942 public_key: "testkey.avbpubkey",
1943 private_key: "testkey.pem",
1944 }
1945 java_library {
1946 name: "myjar",
1947 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001948 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001949 apex_available: ["myapex"],
1950 }
1951 `,
1952 },
1953 {
1954 name: "Updatable apex with stable dep",
1955 bp: `
1956 apex {
1957 name: "myapex",
1958 java_libs: ["myjar"],
1959 key: "myapex.key",
1960 updatable: true,
1961 min_sdk_version: "29",
1962 }
1963 apex_key {
1964 name: "myapex.key",
1965 public_key: "testkey.avbpubkey",
1966 private_key: "testkey.pem",
1967 }
1968 java_library {
1969 name: "myjar",
1970 srcs: ["foo/bar/MyClass.java"],
1971 sdk_version: "current",
1972 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001973 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001974 }
1975 `,
1976 },
1977 {
1978 name: "Updatable apex with non-stable dep",
1979 expectedError: "cannot depend on \"myjar\"",
1980 bp: `
1981 apex {
1982 name: "myapex",
1983 java_libs: ["myjar"],
1984 key: "myapex.key",
1985 updatable: true,
1986 }
1987 apex_key {
1988 name: "myapex.key",
1989 public_key: "testkey.avbpubkey",
1990 private_key: "testkey.pem",
1991 }
1992 java_library {
1993 name: "myjar",
1994 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001995 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001996 apex_available: ["myapex"],
1997 }
1998 `,
1999 },
2000 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002001 name: "Updatable apex with non-stable transitive dep",
2002 // This is not actually detecting that the transitive dependency is unstable, rather it is
2003 // detecting that the transitive dependency is building against a wider API surface than the
2004 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002005 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002006 bp: `
2007 apex {
2008 name: "myapex",
2009 java_libs: ["myjar"],
2010 key: "myapex.key",
2011 updatable: true,
2012 }
2013 apex_key {
2014 name: "myapex.key",
2015 public_key: "testkey.avbpubkey",
2016 private_key: "testkey.pem",
2017 }
2018 java_library {
2019 name: "myjar",
2020 srcs: ["foo/bar/MyClass.java"],
2021 sdk_version: "current",
2022 apex_available: ["myapex"],
2023 static_libs: ["transitive-jar"],
2024 }
2025 java_library {
2026 name: "transitive-jar",
2027 srcs: ["foo/bar/MyClass.java"],
2028 sdk_version: "core_platform",
2029 apex_available: ["myapex"],
2030 }
2031 `,
2032 },
2033 }
2034
2035 for _, test := range testCases {
2036 t.Run(test.name, func(t *testing.T) {
2037 if test.expectedError == "" {
2038 testApex(t, test.bp)
2039 } else {
2040 testApexError(t, test.expectedError, test.bp)
2041 }
2042 })
2043 }
2044}
2045
Jooyung Han749dc692020-04-15 11:03:39 +09002046func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2047 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2048 apex {
2049 name: "myapex",
2050 key: "myapex.key",
2051 native_shared_libs: ["mylib"],
2052 min_sdk_version: "29",
2053 }
2054
2055 apex_key {
2056 name: "myapex.key",
2057 public_key: "testkey.avbpubkey",
2058 private_key: "testkey.pem",
2059 }
2060
2061 cc_library {
2062 name: "mylib",
2063 srcs: ["mylib.cpp"],
2064 shared_libs: ["mylib2"],
2065 system_shared_libs: [],
2066 stl: "none",
2067 apex_available: [
2068 "myapex",
2069 ],
2070 min_sdk_version: "29",
2071 }
2072
2073 // indirect part of the apex
2074 cc_library {
2075 name: "mylib2",
2076 srcs: ["mylib.cpp"],
2077 system_shared_libs: [],
2078 stl: "none",
2079 apex_available: [
2080 "myapex",
2081 ],
2082 min_sdk_version: "30",
2083 }
2084 `)
2085}
2086
2087func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2088 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2089 apex {
2090 name: "myapex",
2091 key: "myapex.key",
2092 apps: ["AppFoo"],
2093 min_sdk_version: "29",
2094 }
2095
2096 apex_key {
2097 name: "myapex.key",
2098 public_key: "testkey.avbpubkey",
2099 private_key: "testkey.pem",
2100 }
2101
2102 android_app {
2103 name: "AppFoo",
2104 srcs: ["foo/bar/MyClass.java"],
2105 sdk_version: "current",
2106 min_sdk_version: "29",
2107 system_modules: "none",
2108 stl: "none",
2109 static_libs: ["bar"],
2110 apex_available: [ "myapex" ],
2111 }
2112
2113 java_library {
2114 name: "bar",
2115 sdk_version: "current",
2116 srcs: ["a.java"],
2117 apex_available: [ "myapex" ],
2118 }
2119 `)
2120}
2121
2122func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002123 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002124 apex {
2125 name: "myapex",
2126 key: "myapex.key",
2127 native_shared_libs: ["mylib"],
2128 min_sdk_version: "29",
2129 }
2130
2131 apex_key {
2132 name: "myapex.key",
2133 public_key: "testkey.avbpubkey",
2134 private_key: "testkey.pem",
2135 }
2136
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002137 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002138 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2139 cc_library {
2140 name: "mylib",
2141 srcs: ["mylib.cpp"],
2142 shared_libs: ["mylib2"],
2143 system_shared_libs: [],
2144 stl: "none",
2145 apex_available: ["myapex", "otherapex"],
2146 min_sdk_version: "29",
2147 }
2148
2149 cc_library {
2150 name: "mylib2",
2151 srcs: ["mylib.cpp"],
2152 system_shared_libs: [],
2153 stl: "none",
2154 apex_available: ["otherapex"],
2155 stubs: { versions: ["29", "30"] },
2156 min_sdk_version: "30",
2157 }
2158
2159 apex {
2160 name: "otherapex",
2161 key: "myapex.key",
2162 native_shared_libs: ["mylib", "mylib2"],
2163 min_sdk_version: "30",
2164 }
2165 `)
2166 expectLink := func(from, from_variant, to, to_variant string) {
2167 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2168 libFlags := ld.Args["libFlags"]
2169 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2170 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002171 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002172 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002173}
2174
Jooyung Haned124c32021-01-26 11:43:46 +09002175func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002176 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2177 func(variables android.FixtureProductVariables) {
2178 variables.Platform_sdk_codename = proptools.StringPtr("S")
2179 variables.Platform_version_active_codenames = []string{"S"}
2180 },
2181 )
Jooyung Haned124c32021-01-26 11:43:46 +09002182 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2183 apex {
2184 name: "myapex",
2185 key: "myapex.key",
2186 native_shared_libs: ["libfoo"],
2187 min_sdk_version: "S",
2188 }
2189 apex_key {
2190 name: "myapex.key",
2191 public_key: "testkey.avbpubkey",
2192 private_key: "testkey.pem",
2193 }
2194 cc_library {
2195 name: "libfoo",
2196 shared_libs: ["libbar"],
2197 apex_available: ["myapex"],
2198 min_sdk_version: "29",
2199 }
2200 cc_library {
2201 name: "libbar",
2202 apex_available: ["myapex"],
2203 }
2204 `, withSAsActiveCodeNames)
2205}
2206
2207func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002208 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2209 variables.Platform_sdk_codename = proptools.StringPtr("S")
2210 variables.Platform_version_active_codenames = []string{"S", "T"}
2211 })
Colin Cross1c460562021-02-16 17:55:47 -08002212 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002213 apex {
2214 name: "myapex",
2215 key: "myapex.key",
2216 native_shared_libs: ["libfoo"],
2217 min_sdk_version: "S",
2218 }
2219 apex_key {
2220 name: "myapex.key",
2221 public_key: "testkey.avbpubkey",
2222 private_key: "testkey.pem",
2223 }
2224 cc_library {
2225 name: "libfoo",
2226 shared_libs: ["libbar"],
2227 apex_available: ["myapex"],
2228 min_sdk_version: "S",
2229 }
2230 cc_library {
2231 name: "libbar",
2232 stubs: {
2233 symbol_file: "libbar.map.txt",
2234 versions: ["30", "S", "T"],
2235 },
2236 }
2237 `, withSAsActiveCodeNames)
2238
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002239 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002240 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2241 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002242 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002243}
2244
Jiyong Park7c2ee712018-12-07 00:42:25 +09002245func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002246 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002247 apex {
2248 name: "myapex",
2249 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002250 native_shared_libs: ["mylib"],
2251 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002252 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002253 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002254 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002255 }
2256
2257 apex_key {
2258 name: "myapex.key",
2259 public_key: "testkey.avbpubkey",
2260 private_key: "testkey.pem",
2261 }
2262
2263 prebuilt_etc {
2264 name: "myetc",
2265 src: "myprebuilt",
2266 sub_dir: "foo/bar",
2267 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002268
2269 cc_library {
2270 name: "mylib",
2271 srcs: ["mylib.cpp"],
2272 relative_install_path: "foo/bar",
2273 system_shared_libs: [],
2274 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002275 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002276 }
2277
2278 cc_binary {
2279 name: "mybin",
2280 srcs: ["mylib.cpp"],
2281 relative_install_path: "foo/bar",
2282 system_shared_libs: [],
2283 static_executable: true,
2284 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002285 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002286 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002287 `)
2288
Sundong Ahnabb64432019-10-22 13:58:29 +09002289 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002290 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2291
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002292 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002293 ensureListContains(t, dirs, "etc")
2294 ensureListContains(t, dirs, "etc/foo")
2295 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002296 ensureListContains(t, dirs, "lib64")
2297 ensureListContains(t, dirs, "lib64/foo")
2298 ensureListContains(t, dirs, "lib64/foo/bar")
2299 ensureListContains(t, dirs, "lib")
2300 ensureListContains(t, dirs, "lib/foo")
2301 ensureListContains(t, dirs, "lib/foo/bar")
2302
Jiyong Parkbd13e442019-03-15 18:10:35 +09002303 ensureListContains(t, dirs, "bin")
2304 ensureListContains(t, dirs, "bin/foo")
2305 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002306}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002307
Jooyung Han35155c42020-02-06 17:33:20 +09002308func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002309 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002310 apex {
2311 name: "myapex",
2312 key: "myapex.key",
2313 multilib: {
2314 both: {
2315 native_shared_libs: ["mylib"],
2316 binaries: ["mybin"],
2317 },
2318 },
2319 compile_multilib: "both",
2320 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002321 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002322 }
2323
2324 apex_key {
2325 name: "myapex.key",
2326 public_key: "testkey.avbpubkey",
2327 private_key: "testkey.pem",
2328 }
2329
2330 cc_library {
2331 name: "mylib",
2332 relative_install_path: "foo/bar",
2333 system_shared_libs: [],
2334 stl: "none",
2335 apex_available: [ "myapex" ],
2336 native_bridge_supported: true,
2337 }
2338
2339 cc_binary {
2340 name: "mybin",
2341 relative_install_path: "foo/bar",
2342 system_shared_libs: [],
2343 static_executable: true,
2344 stl: "none",
2345 apex_available: [ "myapex" ],
2346 native_bridge_supported: true,
2347 compile_multilib: "both", // default is "first" for binary
2348 multilib: {
2349 lib64: {
2350 suffix: "64",
2351 },
2352 },
2353 }
2354 `, withNativeBridgeEnabled)
2355 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2356 "bin/foo/bar/mybin",
2357 "bin/foo/bar/mybin64",
2358 "bin/arm/foo/bar/mybin",
2359 "bin/arm64/foo/bar/mybin64",
2360 "lib/foo/bar/mylib.so",
2361 "lib/arm/foo/bar/mylib.so",
2362 "lib64/foo/bar/mylib.so",
2363 "lib64/arm64/foo/bar/mylib.so",
2364 })
2365}
2366
Jiyong Parkda6eb592018-12-19 17:12:36 +09002367func TestUseVendor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002368 ctx := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002369 apex {
2370 name: "myapex",
2371 key: "myapex.key",
2372 native_shared_libs: ["mylib"],
2373 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002374 updatable: false,
Jiyong Parkda6eb592018-12-19 17:12:36 +09002375 }
2376
2377 apex_key {
2378 name: "myapex.key",
2379 public_key: "testkey.avbpubkey",
2380 private_key: "testkey.pem",
2381 }
2382
2383 cc_library {
2384 name: "mylib",
2385 srcs: ["mylib.cpp"],
2386 shared_libs: ["mylib2"],
2387 system_shared_libs: [],
2388 vendor_available: true,
2389 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002390 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002391 }
2392
2393 cc_library {
2394 name: "mylib2",
2395 srcs: ["mylib.cpp"],
2396 system_shared_libs: [],
2397 vendor_available: true,
2398 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002399 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002400 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002401 `,
2402 setUseVendorAllowListForTest([]string{"myapex"}),
2403 )
Jiyong Parkda6eb592018-12-19 17:12:36 +09002404
2405 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002406 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002407 for _, implicit := range i.Implicits {
2408 inputsList = append(inputsList, implicit.String())
2409 }
2410 }
2411 inputsString := strings.Join(inputsList, " ")
2412
2413 // ensure that the apex includes vendor variants of the direct and indirect deps
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002414 ensureContains(t, inputsString, "android_vendor.29_arm64_armv8-a_shared_apex10000/mylib.so")
2415 ensureContains(t, inputsString, "android_vendor.29_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002416
2417 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002418 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2419 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002420}
Jiyong Park16e91a02018-12-20 18:18:08 +09002421
Jooyung Han85d61762020-06-24 23:50:26 +09002422func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002423 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2424 apex {
2425 name: "myapex",
2426 key: "myapex.key",
2427 use_vendor: true,
2428 }
2429 apex_key {
2430 name: "myapex.key",
2431 public_key: "testkey.avbpubkey",
2432 private_key: "testkey.pem",
2433 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002434 `,
2435 setUseVendorAllowListForTest([]string{""}),
2436 )
Colin Cross440e0d02020-06-11 11:32:11 -07002437 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002438 testApex(t, `
2439 apex {
2440 name: "myapex",
2441 key: "myapex.key",
2442 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002443 updatable: false,
Jooyung Handc782442019-11-01 03:14:38 +09002444 }
2445 apex_key {
2446 name: "myapex.key",
2447 public_key: "testkey.avbpubkey",
2448 private_key: "testkey.pem",
2449 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002450 `,
2451 setUseVendorAllowListForTest([]string{"myapex"}),
2452 )
Jooyung Handc782442019-11-01 03:14:38 +09002453}
2454
Jooyung Han5c998b92019-06-27 11:30:33 +09002455func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2456 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2457 apex {
2458 name: "myapex",
2459 key: "myapex.key",
2460 native_shared_libs: ["mylib"],
2461 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002462 updatable: false,
Jooyung Han5c998b92019-06-27 11:30:33 +09002463 }
2464
2465 apex_key {
2466 name: "myapex.key",
2467 public_key: "testkey.avbpubkey",
2468 private_key: "testkey.pem",
2469 }
2470
2471 cc_library {
2472 name: "mylib",
2473 srcs: ["mylib.cpp"],
2474 system_shared_libs: [],
2475 stl: "none",
2476 }
2477 `)
2478}
2479
Jooyung Han85d61762020-06-24 23:50:26 +09002480func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002481 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002482 apex {
2483 name: "myapex",
2484 key: "myapex.key",
2485 binaries: ["mybin"],
2486 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002487 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002488 }
2489 apex_key {
2490 name: "myapex.key",
2491 public_key: "testkey.avbpubkey",
2492 private_key: "testkey.pem",
2493 }
2494 cc_binary {
2495 name: "mybin",
2496 vendor: true,
2497 shared_libs: ["libfoo"],
2498 }
2499 cc_library {
2500 name: "libfoo",
2501 proprietary: true,
2502 }
2503 `)
2504
2505 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2506 "bin/mybin",
2507 "lib64/libfoo.so",
2508 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2509 "lib64/libc++.so",
2510 })
2511
2512 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002513 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002514 name := apexBundle.BaseModuleName()
2515 prefix := "TARGET_"
2516 var builder strings.Builder
2517 data.Custom(&builder, name, prefix, "", data)
Paul Duffin37ba3442021-03-29 00:21:08 +01002518 androidMk := android.StringRelativeToTop(ctx.Config(), builder.String())
2519 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002520 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002521
2522 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2523 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2524 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002525}
2526
Jooyung Handf78e212020-07-22 15:54:47 +09002527func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002528 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002529 apex {
2530 name: "myapex",
2531 key: "myapex.key",
2532 binaries: ["mybin"],
2533 vendor: true,
2534 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002535 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002536 }
2537 apex_key {
2538 name: "myapex.key",
2539 public_key: "testkey.avbpubkey",
2540 private_key: "testkey.pem",
2541 }
2542 cc_binary {
2543 name: "mybin",
2544 vendor: true,
2545 shared_libs: ["libvndk", "libvendor"],
2546 }
2547 cc_library {
2548 name: "libvndk",
2549 vndk: {
2550 enabled: true,
2551 },
2552 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002553 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002554 }
2555 cc_library {
2556 name: "libvendor",
2557 vendor: true,
2558 }
2559 `)
2560
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002561 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002562
Paul Duffina71a67a2021-03-29 00:42:57 +01002563 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002564 libs := names(ldRule.Args["libFlags"])
2565 // VNDK libs(libvndk/libc++) as they are
Paul Duffin37ba3442021-03-29 00:21:08 +01002566 ensureListContains(t, libs, "out/soong/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
2567 ensureListContains(t, libs, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002568 // non-stable Vendor libs as APEX variants
Paul Duffin37ba3442021-03-29 00:21:08 +01002569 ensureListContains(t, libs, "out/soong/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002570
2571 // VNDK libs are not included when use_vndk_as_stable: true
2572 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2573 "bin/mybin",
2574 "lib64/libvendor.so",
2575 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002576
2577 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2578 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2579 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002580}
2581
Justin Yun13decfb2021-03-08 19:25:55 +09002582func TestProductVariant(t *testing.T) {
2583 ctx := testApex(t, `
2584 apex {
2585 name: "myapex",
2586 key: "myapex.key",
2587 updatable: false,
2588 product_specific: true,
2589 binaries: ["foo"],
2590 }
2591
2592 apex_key {
2593 name: "myapex.key",
2594 public_key: "testkey.avbpubkey",
2595 private_key: "testkey.pem",
2596 }
2597
2598 cc_binary {
2599 name: "foo",
2600 product_available: true,
2601 apex_available: ["myapex"],
2602 srcs: ["foo.cpp"],
2603 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002604 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2605 variables.ProductVndkVersion = proptools.StringPtr("current")
2606 }),
2607 )
Justin Yun13decfb2021-03-08 19:25:55 +09002608
2609 cflags := strings.Fields(
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002610 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002611 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2612 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2613 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2614 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2615}
2616
Jooyung Han8e5685d2020-09-21 11:02:57 +09002617func TestApex_withPrebuiltFirmware(t *testing.T) {
2618 testCases := []struct {
2619 name string
2620 additionalProp string
2621 }{
2622 {"system apex with prebuilt_firmware", ""},
2623 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2624 }
2625 for _, tc := range testCases {
2626 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002627 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002628 apex {
2629 name: "myapex",
2630 key: "myapex.key",
2631 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002632 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002633 `+tc.additionalProp+`
2634 }
2635 apex_key {
2636 name: "myapex.key",
2637 public_key: "testkey.avbpubkey",
2638 private_key: "testkey.pem",
2639 }
2640 prebuilt_firmware {
2641 name: "myfirmware",
2642 src: "myfirmware.bin",
2643 filename_from_src: true,
2644 `+tc.additionalProp+`
2645 }
2646 `)
2647 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2648 "etc/firmware/myfirmware.bin",
2649 })
2650 })
2651 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002652}
2653
Jooyung Hanefb184e2020-06-25 17:14:25 +09002654func TestAndroidMk_UseVendorRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002655 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002656 apex {
2657 name: "myapex",
2658 key: "myapex.key",
2659 use_vendor: true,
2660 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002661 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002662 }
2663
2664 apex_key {
2665 name: "myapex.key",
2666 public_key: "testkey.avbpubkey",
2667 private_key: "testkey.pem",
2668 }
2669
2670 cc_library {
2671 name: "mylib",
2672 vendor_available: true,
2673 apex_available: ["myapex"],
2674 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002675 `,
2676 setUseVendorAllowListForTest([]string{"myapex"}),
2677 )
Jooyung Hanefb184e2020-06-25 17:14:25 +09002678
2679 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002680 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002681 name := apexBundle.BaseModuleName()
2682 prefix := "TARGET_"
2683 var builder strings.Builder
2684 data.Custom(&builder, name, prefix, "", data)
2685 androidMk := builder.String()
2686 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2687}
2688
2689func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002690 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002691 apex {
2692 name: "myapex",
2693 key: "myapex.key",
2694 vendor: true,
2695 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002696 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002697 }
2698
2699 apex_key {
2700 name: "myapex.key",
2701 public_key: "testkey.avbpubkey",
2702 private_key: "testkey.pem",
2703 }
2704
2705 cc_library {
2706 name: "mylib",
2707 vendor_available: true,
2708 }
2709 `)
2710
2711 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002712 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002713 name := apexBundle.BaseModuleName()
2714 prefix := "TARGET_"
2715 var builder strings.Builder
2716 data.Custom(&builder, name, prefix, "", data)
2717 androidMk := builder.String()
2718 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2719}
2720
Jooyung Han2ed99d02020-06-24 23:26:26 +09002721func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002722 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002723 apex {
2724 name: "myapex",
2725 key: "myapex.key",
2726 vintf_fragments: ["fragment.xml"],
2727 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002728 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002729 }
2730 apex_key {
2731 name: "myapex.key",
2732 public_key: "testkey.avbpubkey",
2733 private_key: "testkey.pem",
2734 }
2735 cc_binary {
2736 name: "mybin",
2737 }
2738 `)
2739
2740 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002741 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002742 name := apexBundle.BaseModuleName()
2743 prefix := "TARGET_"
2744 var builder strings.Builder
2745 data.Custom(&builder, name, prefix, "", data)
2746 androidMk := builder.String()
2747 ensureContains(t, androidMk, "LOCAL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04002748 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09002749}
2750
Jiyong Park16e91a02018-12-20 18:18:08 +09002751func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002752 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002753 apex {
2754 name: "myapex",
2755 key: "myapex.key",
2756 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002757 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002758 }
2759
2760 apex_key {
2761 name: "myapex.key",
2762 public_key: "testkey.avbpubkey",
2763 private_key: "testkey.pem",
2764 }
2765
2766 cc_library {
2767 name: "mylib",
2768 srcs: ["mylib.cpp"],
2769 system_shared_libs: [],
2770 stl: "none",
2771 stubs: {
2772 versions: ["1", "2", "3"],
2773 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002774 apex_available: [
2775 "//apex_available:platform",
2776 "myapex",
2777 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002778 }
2779
2780 cc_binary {
2781 name: "not_in_apex",
2782 srcs: ["mylib.cpp"],
2783 static_libs: ["mylib"],
2784 static_executable: true,
2785 system_shared_libs: [],
2786 stl: "none",
2787 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002788 `)
2789
Colin Cross7113d202019-11-20 16:39:12 -08002790 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002791
2792 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002793 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002794}
Jiyong Park9335a262018-12-24 11:31:58 +09002795
2796func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002797 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002798 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002799 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002800 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002801 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002802 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002803 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002804 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09002805 }
2806
2807 cc_library {
2808 name: "mylib",
2809 srcs: ["mylib.cpp"],
2810 system_shared_libs: [],
2811 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002812 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002813 }
2814
2815 apex_key {
2816 name: "myapex.key",
2817 public_key: "testkey.avbpubkey",
2818 private_key: "testkey.pem",
2819 }
2820
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002821 android_app_certificate {
2822 name: "myapex.certificate",
2823 certificate: "testkey",
2824 }
2825
2826 android_app_certificate {
2827 name: "myapex.certificate.override",
2828 certificate: "testkey.override",
2829 }
2830
Jiyong Park9335a262018-12-24 11:31:58 +09002831 `)
2832
2833 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002834 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002835
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002836 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2837 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002838 "vendor/foo/devkeys/testkey.avbpubkey")
2839 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002840 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2841 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002842 "vendor/foo/devkeys/testkey.pem")
2843 }
2844
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002845 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002846 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002847 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002848 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002849 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002850 }
2851}
Jiyong Park58e364a2019-01-19 19:24:06 +09002852
Jooyung Hanf121a652019-12-17 14:30:11 +09002853func TestCertificate(t *testing.T) {
2854 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002855 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002856 apex {
2857 name: "myapex",
2858 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002859 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002860 }
2861 apex_key {
2862 name: "myapex.key",
2863 public_key: "testkey.avbpubkey",
2864 private_key: "testkey.pem",
2865 }`)
2866 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2867 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2868 if actual := rule.Args["certificates"]; actual != expected {
2869 t.Errorf("certificates should be %q, not %q", expected, actual)
2870 }
2871 })
2872 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002873 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002874 apex {
2875 name: "myapex_keytest",
2876 key: "myapex.key",
2877 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002878 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002879 }
2880 apex_key {
2881 name: "myapex.key",
2882 public_key: "testkey.avbpubkey",
2883 private_key: "testkey.pem",
2884 }
2885 android_app_certificate {
2886 name: "myapex.certificate.override",
2887 certificate: "testkey.override",
2888 }`)
2889 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2890 expected := "testkey.override.x509.pem testkey.override.pk8"
2891 if actual := rule.Args["certificates"]; actual != expected {
2892 t.Errorf("certificates should be %q, not %q", expected, actual)
2893 }
2894 })
2895 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002896 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002897 apex {
2898 name: "myapex",
2899 key: "myapex.key",
2900 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002901 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002902 }
2903 apex_key {
2904 name: "myapex.key",
2905 public_key: "testkey.avbpubkey",
2906 private_key: "testkey.pem",
2907 }
2908 android_app_certificate {
2909 name: "myapex.certificate",
2910 certificate: "testkey",
2911 }`)
2912 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2913 expected := "testkey.x509.pem testkey.pk8"
2914 if actual := rule.Args["certificates"]; actual != expected {
2915 t.Errorf("certificates should be %q, not %q", expected, actual)
2916 }
2917 })
2918 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002919 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002920 apex {
2921 name: "myapex_keytest",
2922 key: "myapex.key",
2923 file_contexts: ":myapex-file_contexts",
2924 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002925 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002926 }
2927 apex_key {
2928 name: "myapex.key",
2929 public_key: "testkey.avbpubkey",
2930 private_key: "testkey.pem",
2931 }
2932 android_app_certificate {
2933 name: "myapex.certificate.override",
2934 certificate: "testkey.override",
2935 }`)
2936 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2937 expected := "testkey.override.x509.pem testkey.override.pk8"
2938 if actual := rule.Args["certificates"]; actual != expected {
2939 t.Errorf("certificates should be %q, not %q", expected, actual)
2940 }
2941 })
2942 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002943 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002944 apex {
2945 name: "myapex",
2946 key: "myapex.key",
2947 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002948 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002949 }
2950 apex_key {
2951 name: "myapex.key",
2952 public_key: "testkey.avbpubkey",
2953 private_key: "testkey.pem",
2954 }`)
2955 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2956 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2957 if actual := rule.Args["certificates"]; actual != expected {
2958 t.Errorf("certificates should be %q, not %q", expected, actual)
2959 }
2960 })
2961 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002962 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002963 apex {
2964 name: "myapex_keytest",
2965 key: "myapex.key",
2966 file_contexts: ":myapex-file_contexts",
2967 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002968 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002969 }
2970 apex_key {
2971 name: "myapex.key",
2972 public_key: "testkey.avbpubkey",
2973 private_key: "testkey.pem",
2974 }
2975 android_app_certificate {
2976 name: "myapex.certificate.override",
2977 certificate: "testkey.override",
2978 }`)
2979 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2980 expected := "testkey.override.x509.pem testkey.override.pk8"
2981 if actual := rule.Args["certificates"]; actual != expected {
2982 t.Errorf("certificates should be %q, not %q", expected, actual)
2983 }
2984 })
2985}
2986
Jiyong Park58e364a2019-01-19 19:24:06 +09002987func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002988 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002989 apex {
2990 name: "myapex",
2991 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002992 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002993 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09002994 }
2995
2996 apex {
2997 name: "otherapex",
2998 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002999 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003000 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003001 }
3002
3003 apex_key {
3004 name: "myapex.key",
3005 public_key: "testkey.avbpubkey",
3006 private_key: "testkey.pem",
3007 }
3008
3009 cc_library {
3010 name: "mylib",
3011 srcs: ["mylib.cpp"],
3012 system_shared_libs: [],
3013 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003014 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003015 "myapex",
3016 "otherapex",
3017 ],
Jooyung Han24282772020-03-21 23:20:55 +09003018 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003019 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003020 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003021 cc_library {
3022 name: "mylib2",
3023 srcs: ["mylib.cpp"],
3024 system_shared_libs: [],
3025 stl: "none",
3026 apex_available: [
3027 "myapex",
3028 "otherapex",
3029 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003030 static_libs: ["mylib3"],
3031 recovery_available: true,
3032 min_sdk_version: "29",
3033 }
3034 cc_library {
3035 name: "mylib3",
3036 srcs: ["mylib.cpp"],
3037 system_shared_libs: [],
3038 stl: "none",
3039 apex_available: [
3040 "myapex",
3041 "otherapex",
3042 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09003043 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07003044 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003045 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003046 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003047 `)
3048
Jooyung Hanc87a0592020-03-02 17:44:33 +09003049 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003050 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003051 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003052 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003053
Jooyung Hanccce2f22020-03-07 03:45:53 +09003054 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003055 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003056 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003057 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09003058 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003059
Jooyung Hanccce2f22020-03-07 03:45:53 +09003060 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003061 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003062 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003063 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09003064 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003065
Colin Crossaede88c2020-08-11 12:17:01 -07003066 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3067 // each variant defines additional macros to distinguish which apex variant it is built for
3068
3069 // non-APEX variant does not have __ANDROID_APEX__ defined
3070 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3071 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3072
3073 // APEX variant has __ANDROID_APEX__ defined
3074 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
3075 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3076 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3077 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3078
3079 // APEX variant has __ANDROID_APEX__ defined
3080 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
3081 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3082 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3083 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3084
Dan Albertb19953d2020-11-17 15:29:36 -08003085 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003086 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3087 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003088 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003089
3090 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
3091 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09003092
3093 // non-APEX variant does not have __ANDROID_APEX__ defined
3094 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3095 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3096
3097 // APEX variant has __ANDROID_APEX__ defined
3098 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003099 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003100 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003101 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003102
Jooyung Hanc87a0592020-03-02 17:44:33 +09003103 // APEX variant has __ANDROID_APEX__ defined
3104 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003105 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003106 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003107 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09003108
Dan Albertb19953d2020-11-17 15:29:36 -08003109 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003110 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003111 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003112 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003113}
Jiyong Park7e636d02019-01-28 16:16:54 +09003114
3115func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003116 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003117 apex {
3118 name: "myapex",
3119 key: "myapex.key",
3120 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003121 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003122 }
3123
3124 apex_key {
3125 name: "myapex.key",
3126 public_key: "testkey.avbpubkey",
3127 private_key: "testkey.pem",
3128 }
3129
3130 cc_library_headers {
3131 name: "mylib_headers",
3132 export_include_dirs: ["my_include"],
3133 system_shared_libs: [],
3134 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003135 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003136 }
3137
3138 cc_library {
3139 name: "mylib",
3140 srcs: ["mylib.cpp"],
3141 system_shared_libs: [],
3142 stl: "none",
3143 header_libs: ["mylib_headers"],
3144 export_header_lib_headers: ["mylib_headers"],
3145 stubs: {
3146 versions: ["1", "2", "3"],
3147 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003148 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003149 }
3150
3151 cc_library {
3152 name: "otherlib",
3153 srcs: ["mylib.cpp"],
3154 system_shared_libs: [],
3155 stl: "none",
3156 shared_libs: ["mylib"],
3157 }
3158 `)
3159
Colin Cross7113d202019-11-20 16:39:12 -08003160 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003161
3162 // Ensure that the include path of the header lib is exported to 'otherlib'
3163 ensureContains(t, cFlags, "-Imy_include")
3164}
Alex Light9670d332019-01-29 18:07:33 -08003165
Jiyong Park7cd10e32020-01-14 09:22:18 +09003166type fileInApex struct {
3167 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003168 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003169 isLink bool
3170}
3171
Jooyung Hana57af4a2020-01-23 05:36:59 +00003172func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003173 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003174 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003175 copyCmds := apexRule.Args["copy_commands"]
3176 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003177 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003178 for _, cmd := range strings.Split(copyCmds, "&&") {
3179 cmd = strings.TrimSpace(cmd)
3180 if cmd == "" {
3181 continue
3182 }
3183 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003184 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003185 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003186 switch terms[0] {
3187 case "mkdir":
3188 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003189 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003190 t.Fatal("copyCmds contains invalid cp command", cmd)
3191 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003192 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003193 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003194 isLink = false
3195 case "ln":
3196 if len(terms) != 3 && len(terms) != 4 {
3197 // ln LINK TARGET or ln -s LINK TARGET
3198 t.Fatal("copyCmds contains invalid ln command", cmd)
3199 }
3200 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003201 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003202 isLink = true
3203 default:
3204 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3205 }
3206 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003207 index := strings.Index(dst, imageApexDir)
3208 if index == -1 {
3209 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3210 }
3211 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003212 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003213 }
3214 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003215 return ret
3216}
3217
Jooyung Hana57af4a2020-01-23 05:36:59 +00003218func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3219 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003220 var failed bool
3221 var surplus []string
3222 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003223 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003224 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003225 for _, expected := range files {
3226 if matched, _ := path.Match(expected, file.path); matched {
3227 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003228 mactchFound = true
3229 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003230 }
3231 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003232 if !mactchFound {
3233 surplus = append(surplus, file.path)
3234 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003235 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003236
Jooyung Han31c470b2019-10-18 16:26:59 +09003237 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003238 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003239 t.Log("surplus files", surplus)
3240 failed = true
3241 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003242
3243 if len(files) > len(filesMatched) {
3244 var missing []string
3245 for _, expected := range files {
3246 if !filesMatched[expected] {
3247 missing = append(missing, expected)
3248 }
3249 }
3250 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003251 t.Log("missing files", missing)
3252 failed = true
3253 }
3254 if failed {
3255 t.Fail()
3256 }
3257}
3258
Jooyung Han344d5432019-08-23 11:17:39 +09003259func TestVndkApexCurrent(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003260 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003261 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003262 name: "com.android.vndk.current",
3263 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003264 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003265 }
3266
3267 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003268 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003269 public_key: "testkey.avbpubkey",
3270 private_key: "testkey.pem",
3271 }
3272
3273 cc_library {
3274 name: "libvndk",
3275 srcs: ["mylib.cpp"],
3276 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003277 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003278 vndk: {
3279 enabled: true,
3280 },
3281 system_shared_libs: [],
3282 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003283 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003284 }
3285
3286 cc_library {
3287 name: "libvndksp",
3288 srcs: ["mylib.cpp"],
3289 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003290 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003291 vndk: {
3292 enabled: true,
3293 support_system_process: true,
3294 },
3295 system_shared_libs: [],
3296 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003297 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003298 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003299 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003300
Colin Cross2807f002021-03-02 10:15:29 -08003301 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003302 "lib/libvndk.so",
3303 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003304 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003305 "lib64/libvndk.so",
3306 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003307 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003308 "etc/llndk.libraries.29.txt",
3309 "etc/vndkcore.libraries.29.txt",
3310 "etc/vndksp.libraries.29.txt",
3311 "etc/vndkprivate.libraries.29.txt",
3312 "etc/vndkproduct.libraries.29.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003313 })
Jooyung Han344d5432019-08-23 11:17:39 +09003314}
3315
3316func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003317 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003318 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003319 name: "com.android.vndk.current",
3320 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003321 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003322 }
3323
3324 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003325 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003326 public_key: "testkey.avbpubkey",
3327 private_key: "testkey.pem",
3328 }
3329
3330 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003331 name: "libvndk",
3332 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003333 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003334 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003335 vndk: {
3336 enabled: true,
3337 },
3338 system_shared_libs: [],
3339 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003340 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003341 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003342
3343 cc_prebuilt_library_shared {
3344 name: "libvndk.arm",
3345 srcs: ["libvndk.arm.so"],
3346 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003347 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003348 vndk: {
3349 enabled: true,
3350 },
3351 enabled: false,
3352 arch: {
3353 arm: {
3354 enabled: true,
3355 },
3356 },
3357 system_shared_libs: [],
3358 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003359 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003360 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003361 `+vndkLibrariesTxtFiles("current"),
3362 withFiles(map[string][]byte{
3363 "libvndk.so": nil,
3364 "libvndk.arm.so": nil,
3365 }))
Colin Cross2807f002021-03-02 10:15:29 -08003366 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003367 "lib/libvndk.so",
3368 "lib/libvndk.arm.so",
3369 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003370 "lib/libc++.so",
3371 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003372 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003373 })
Jooyung Han344d5432019-08-23 11:17:39 +09003374}
3375
Jooyung Han39edb6c2019-11-06 16:53:07 +09003376func vndkLibrariesTxtFiles(vers ...string) (result string) {
3377 for _, v := range vers {
3378 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003379 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003380 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003381 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003382 name: "` + txt + `.libraries.txt",
3383 }
3384 `
3385 }
3386 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003387 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003388 result += `
3389 prebuilt_etc {
3390 name: "` + txt + `.libraries.` + v + `.txt",
3391 src: "dummy.txt",
3392 }
3393 `
3394 }
3395 }
3396 }
3397 return
3398}
3399
Jooyung Han344d5432019-08-23 11:17:39 +09003400func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003401 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003402 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003403 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003404 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003405 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003406 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003407 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003408 }
3409
3410 apex_key {
3411 name: "myapex.key",
3412 public_key: "testkey.avbpubkey",
3413 private_key: "testkey.pem",
3414 }
3415
Jooyung Han31c470b2019-10-18 16:26:59 +09003416 vndk_prebuilt_shared {
3417 name: "libvndk27",
3418 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003419 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003420 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003421 vndk: {
3422 enabled: true,
3423 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003424 target_arch: "arm64",
3425 arch: {
3426 arm: {
3427 srcs: ["libvndk27_arm.so"],
3428 },
3429 arm64: {
3430 srcs: ["libvndk27_arm64.so"],
3431 },
3432 },
Colin Cross2807f002021-03-02 10:15:29 -08003433 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003434 }
3435
3436 vndk_prebuilt_shared {
3437 name: "libvndk27",
3438 version: "27",
3439 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003440 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003441 vndk: {
3442 enabled: true,
3443 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003444 target_arch: "x86_64",
3445 arch: {
3446 x86: {
3447 srcs: ["libvndk27_x86.so"],
3448 },
3449 x86_64: {
3450 srcs: ["libvndk27_x86_64.so"],
3451 },
3452 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003453 }
3454 `+vndkLibrariesTxtFiles("27"),
3455 withFiles(map[string][]byte{
3456 "libvndk27_arm.so": nil,
3457 "libvndk27_arm64.so": nil,
3458 "libvndk27_x86.so": nil,
3459 "libvndk27_x86_64.so": nil,
3460 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003461
Colin Cross2807f002021-03-02 10:15:29 -08003462 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003463 "lib/libvndk27_arm.so",
3464 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003465 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003466 })
Jooyung Han344d5432019-08-23 11:17:39 +09003467}
3468
Jooyung Han90eee022019-10-01 20:02:42 +09003469func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003470 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003471 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003472 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003473 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003474 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003475 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003476 }
3477 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003478 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003479 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003480 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003481 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003482 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003483 }
3484 apex_key {
3485 name: "myapex.key",
3486 public_key: "testkey.avbpubkey",
3487 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003488 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003489
3490 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003491 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003492 actual := proptools.String(bundle.properties.Apex_name)
3493 if !reflect.DeepEqual(actual, expected) {
3494 t.Errorf("Got '%v', expected '%v'", actual, expected)
3495 }
3496 }
3497
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003498 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003499 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003500}
3501
Jooyung Han344d5432019-08-23 11:17:39 +09003502func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003503 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003504 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003505 name: "com.android.vndk.current",
3506 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003507 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003508 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003509 }
3510
3511 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003512 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003513 public_key: "testkey.avbpubkey",
3514 private_key: "testkey.pem",
3515 }
3516
3517 cc_library {
3518 name: "libvndk",
3519 srcs: ["mylib.cpp"],
3520 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003521 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003522 native_bridge_supported: true,
3523 host_supported: true,
3524 vndk: {
3525 enabled: true,
3526 },
3527 system_shared_libs: [],
3528 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003529 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003530 }
Colin Cross2807f002021-03-02 10:15:29 -08003531 `+vndkLibrariesTxtFiles("current"),
3532 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003533
Colin Cross2807f002021-03-02 10:15:29 -08003534 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003535 "lib/libvndk.so",
3536 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003537 "lib/libc++.so",
3538 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003539 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003540 })
Jooyung Han344d5432019-08-23 11:17:39 +09003541}
3542
3543func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003544 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003545 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003546 name: "com.android.vndk.current",
3547 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003548 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003549 native_bridge_supported: true,
3550 }
3551
3552 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003553 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003554 public_key: "testkey.avbpubkey",
3555 private_key: "testkey.pem",
3556 }
3557
3558 cc_library {
3559 name: "libvndk",
3560 srcs: ["mylib.cpp"],
3561 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003562 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003563 native_bridge_supported: true,
3564 host_supported: true,
3565 vndk: {
3566 enabled: true,
3567 },
3568 system_shared_libs: [],
3569 stl: "none",
3570 }
3571 `)
3572}
3573
Jooyung Han31c470b2019-10-18 16:26:59 +09003574func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003575 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003576 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003577 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003578 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003579 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003580 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003581 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003582 }
3583
3584 apex_key {
3585 name: "myapex.key",
3586 public_key: "testkey.avbpubkey",
3587 private_key: "testkey.pem",
3588 }
3589
3590 vndk_prebuilt_shared {
3591 name: "libvndk27",
3592 version: "27",
3593 target_arch: "arm",
3594 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003595 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003596 vndk: {
3597 enabled: true,
3598 },
3599 arch: {
3600 arm: {
3601 srcs: ["libvndk27.so"],
3602 }
3603 },
3604 }
3605
3606 vndk_prebuilt_shared {
3607 name: "libvndk27",
3608 version: "27",
3609 target_arch: "arm",
3610 binder32bit: true,
3611 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003612 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003613 vndk: {
3614 enabled: true,
3615 },
3616 arch: {
3617 arm: {
3618 srcs: ["libvndk27binder32.so"],
3619 }
3620 },
Colin Cross2807f002021-03-02 10:15:29 -08003621 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003622 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003623 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003624 withFiles(map[string][]byte{
3625 "libvndk27.so": nil,
3626 "libvndk27binder32.so": nil,
3627 }),
3628 withBinder32bit,
3629 withTargets(map[android.OsType][]android.Target{
3630 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003631 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3632 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003633 },
3634 }),
3635 )
3636
Colin Cross2807f002021-03-02 10:15:29 -08003637 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003638 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003639 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003640 })
3641}
3642
Jooyung Han45a96772020-06-15 14:59:42 +09003643func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003644 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003645 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003646 name: "com.android.vndk.current",
3647 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003648 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003649 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003650 }
3651
3652 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003653 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003654 public_key: "testkey.avbpubkey",
3655 private_key: "testkey.pem",
3656 }
3657
3658 cc_library {
3659 name: "libz",
3660 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003661 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003662 vndk: {
3663 enabled: true,
3664 },
3665 stubs: {
3666 symbol_file: "libz.map.txt",
3667 versions: ["30"],
3668 }
3669 }
3670 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3671 "libz.map.txt": nil,
3672 }))
3673
Colin Cross2807f002021-03-02 10:15:29 -08003674 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003675 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3676 ensureListEmpty(t, provideNativeLibs)
3677}
3678
Jooyung Hane1633032019-08-01 17:41:43 +09003679func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003680 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003681 apex {
3682 name: "myapex_nodep",
3683 key: "myapex.key",
3684 native_shared_libs: ["lib_nodep"],
3685 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003686 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003687 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003688 }
3689
3690 apex {
3691 name: "myapex_dep",
3692 key: "myapex.key",
3693 native_shared_libs: ["lib_dep"],
3694 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003695 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003696 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003697 }
3698
3699 apex {
3700 name: "myapex_provider",
3701 key: "myapex.key",
3702 native_shared_libs: ["libfoo"],
3703 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003704 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003705 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003706 }
3707
3708 apex {
3709 name: "myapex_selfcontained",
3710 key: "myapex.key",
3711 native_shared_libs: ["lib_dep", "libfoo"],
3712 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003713 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003714 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003715 }
3716
3717 apex_key {
3718 name: "myapex.key",
3719 public_key: "testkey.avbpubkey",
3720 private_key: "testkey.pem",
3721 }
3722
3723 cc_library {
3724 name: "lib_nodep",
3725 srcs: ["mylib.cpp"],
3726 system_shared_libs: [],
3727 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003728 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003729 }
3730
3731 cc_library {
3732 name: "lib_dep",
3733 srcs: ["mylib.cpp"],
3734 shared_libs: ["libfoo"],
3735 system_shared_libs: [],
3736 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003737 apex_available: [
3738 "myapex_dep",
3739 "myapex_provider",
3740 "myapex_selfcontained",
3741 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003742 }
3743
3744 cc_library {
3745 name: "libfoo",
3746 srcs: ["mytest.cpp"],
3747 stubs: {
3748 versions: ["1"],
3749 },
3750 system_shared_libs: [],
3751 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003752 apex_available: [
3753 "myapex_provider",
3754 "myapex_selfcontained",
3755 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003756 }
3757 `)
3758
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003759 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003760 var provideNativeLibs, requireNativeLibs []string
3761
Sundong Ahnabb64432019-10-22 13:58:29 +09003762 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003763 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3764 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003765 ensureListEmpty(t, provideNativeLibs)
3766 ensureListEmpty(t, requireNativeLibs)
3767
Sundong Ahnabb64432019-10-22 13:58:29 +09003768 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003769 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3770 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003771 ensureListEmpty(t, provideNativeLibs)
3772 ensureListContains(t, requireNativeLibs, "libfoo.so")
3773
Sundong Ahnabb64432019-10-22 13:58:29 +09003774 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003775 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3776 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003777 ensureListContains(t, provideNativeLibs, "libfoo.so")
3778 ensureListEmpty(t, requireNativeLibs)
3779
Sundong Ahnabb64432019-10-22 13:58:29 +09003780 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003781 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3782 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003783 ensureListContains(t, provideNativeLibs, "libfoo.so")
3784 ensureListEmpty(t, requireNativeLibs)
3785}
3786
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003787func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003788 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003789 apex {
3790 name: "myapex",
3791 key: "myapex.key",
3792 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003793 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003794 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003795 }
3796
3797 apex_key {
3798 name: "myapex.key",
3799 public_key: "testkey.avbpubkey",
3800 private_key: "testkey.pem",
3801 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003802
3803 cc_library {
3804 name: "mylib",
3805 srcs: ["mylib.cpp"],
3806 system_shared_libs: [],
3807 stl: "none",
3808 apex_available: [
3809 "//apex_available:platform",
3810 "myapex",
3811 ],
3812 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003813 `)
3814
Sundong Ahnabb64432019-10-22 13:58:29 +09003815 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003816 apexManifestRule := module.Rule("apexManifestRule")
3817 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3818 apexRule := module.Rule("apexRule")
3819 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003820
3821 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003822 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09003823 name := apexBundle.BaseModuleName()
3824 prefix := "TARGET_"
3825 var builder strings.Builder
3826 data.Custom(&builder, name, prefix, "", data)
3827 androidMk := builder.String()
3828 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3829 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003830}
3831
Alex Light0851b882019-02-07 13:20:53 -08003832func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003833 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003834 apex {
3835 name: "myapex",
3836 key: "myapex.key",
3837 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003838 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003839 }
3840
3841 apex_key {
3842 name: "myapex.key",
3843 public_key: "testkey.avbpubkey",
3844 private_key: "testkey.pem",
3845 }
3846
3847 cc_library {
3848 name: "mylib_common",
3849 srcs: ["mylib.cpp"],
3850 system_shared_libs: [],
3851 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003852 apex_available: [
3853 "//apex_available:platform",
3854 "myapex",
3855 ],
Alex Light0851b882019-02-07 13:20:53 -08003856 }
3857 `)
3858
Sundong Ahnabb64432019-10-22 13:58:29 +09003859 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003860 apexRule := module.Rule("apexRule")
3861 copyCmds := apexRule.Args["copy_commands"]
3862
3863 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3864 t.Log("Apex was a test apex!")
3865 t.Fail()
3866 }
3867 // Ensure that main rule creates an output
3868 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3869
3870 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003871 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003872
3873 // Ensure that both direct and indirect deps are copied into apex
3874 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3875
Colin Cross7113d202019-11-20 16:39:12 -08003876 // Ensure that the platform variant ends with _shared
3877 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003878
Colin Cross56a83212020-09-15 18:30:11 -07003879 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003880 t.Log("Found mylib_common not in any apex!")
3881 t.Fail()
3882 }
3883}
3884
3885func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003886 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003887 apex_test {
3888 name: "myapex",
3889 key: "myapex.key",
3890 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003891 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003892 }
3893
3894 apex_key {
3895 name: "myapex.key",
3896 public_key: "testkey.avbpubkey",
3897 private_key: "testkey.pem",
3898 }
3899
3900 cc_library {
3901 name: "mylib_common_test",
3902 srcs: ["mylib.cpp"],
3903 system_shared_libs: [],
3904 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003905 // TODO: remove //apex_available:platform
3906 apex_available: [
3907 "//apex_available:platform",
3908 "myapex",
3909 ],
Alex Light0851b882019-02-07 13:20:53 -08003910 }
3911 `)
3912
Sundong Ahnabb64432019-10-22 13:58:29 +09003913 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003914 apexRule := module.Rule("apexRule")
3915 copyCmds := apexRule.Args["copy_commands"]
3916
3917 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3918 t.Log("Apex was not a test apex!")
3919 t.Fail()
3920 }
3921 // Ensure that main rule creates an output
3922 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3923
3924 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003925 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003926
3927 // Ensure that both direct and indirect deps are copied into apex
3928 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3929
Colin Cross7113d202019-11-20 16:39:12 -08003930 // Ensure that the platform variant ends with _shared
3931 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003932}
3933
Alex Light9670d332019-01-29 18:07:33 -08003934func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003935 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003936 apex {
3937 name: "myapex",
3938 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003939 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08003940 multilib: {
3941 first: {
3942 native_shared_libs: ["mylib_common"],
3943 }
3944 },
3945 target: {
3946 android: {
3947 multilib: {
3948 first: {
3949 native_shared_libs: ["mylib"],
3950 }
3951 }
3952 },
3953 host: {
3954 multilib: {
3955 first: {
3956 native_shared_libs: ["mylib2"],
3957 }
3958 }
3959 }
3960 }
3961 }
3962
3963 apex_key {
3964 name: "myapex.key",
3965 public_key: "testkey.avbpubkey",
3966 private_key: "testkey.pem",
3967 }
3968
3969 cc_library {
3970 name: "mylib",
3971 srcs: ["mylib.cpp"],
3972 system_shared_libs: [],
3973 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003974 // TODO: remove //apex_available:platform
3975 apex_available: [
3976 "//apex_available:platform",
3977 "myapex",
3978 ],
Alex Light9670d332019-01-29 18:07:33 -08003979 }
3980
3981 cc_library {
3982 name: "mylib_common",
3983 srcs: ["mylib.cpp"],
3984 system_shared_libs: [],
3985 stl: "none",
3986 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003987 // TODO: remove //apex_available:platform
3988 apex_available: [
3989 "//apex_available:platform",
3990 "myapex",
3991 ],
Alex Light9670d332019-01-29 18:07:33 -08003992 }
3993
3994 cc_library {
3995 name: "mylib2",
3996 srcs: ["mylib.cpp"],
3997 system_shared_libs: [],
3998 stl: "none",
3999 compile_multilib: "first",
4000 }
4001 `)
4002
Sundong Ahnabb64432019-10-22 13:58:29 +09004003 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004004 copyCmds := apexRule.Args["copy_commands"]
4005
4006 // Ensure that main rule creates an output
4007 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4008
4009 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004010 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4011 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4012 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004013
4014 // Ensure that both direct and indirect deps are copied into apex
4015 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4016 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4017 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4018
Colin Cross7113d202019-11-20 16:39:12 -08004019 // Ensure that the platform variant ends with _shared
4020 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4021 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4022 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004023}
Jiyong Park04480cf2019-02-06 00:16:29 +09004024
Jiyong Park59140302020-12-14 18:44:04 +09004025func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004026 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004027 apex {
4028 name: "myapex",
4029 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004030 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09004031 arch: {
4032 arm64: {
4033 native_shared_libs: ["mylib.arm64"],
4034 },
4035 x86_64: {
4036 native_shared_libs: ["mylib.x64"],
4037 },
4038 }
4039 }
4040
4041 apex_key {
4042 name: "myapex.key",
4043 public_key: "testkey.avbpubkey",
4044 private_key: "testkey.pem",
4045 }
4046
4047 cc_library {
4048 name: "mylib.arm64",
4049 srcs: ["mylib.cpp"],
4050 system_shared_libs: [],
4051 stl: "none",
4052 // TODO: remove //apex_available:platform
4053 apex_available: [
4054 "//apex_available:platform",
4055 "myapex",
4056 ],
4057 }
4058
4059 cc_library {
4060 name: "mylib.x64",
4061 srcs: ["mylib.cpp"],
4062 system_shared_libs: [],
4063 stl: "none",
4064 // TODO: remove //apex_available:platform
4065 apex_available: [
4066 "//apex_available:platform",
4067 "myapex",
4068 ],
4069 }
4070 `)
4071
4072 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4073 copyCmds := apexRule.Args["copy_commands"]
4074
4075 // Ensure that apex variant is created for the direct dep
4076 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4077 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4078
4079 // Ensure that both direct and indirect deps are copied into apex
4080 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4081 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4082}
4083
Jiyong Park04480cf2019-02-06 00:16:29 +09004084func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004085 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004086 apex {
4087 name: "myapex",
4088 key: "myapex.key",
4089 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004090 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004091 }
4092
4093 apex_key {
4094 name: "myapex.key",
4095 public_key: "testkey.avbpubkey",
4096 private_key: "testkey.pem",
4097 }
4098
4099 sh_binary {
4100 name: "myscript",
4101 src: "mylib.cpp",
4102 filename: "myscript.sh",
4103 sub_dir: "script",
4104 }
4105 `)
4106
Sundong Ahnabb64432019-10-22 13:58:29 +09004107 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004108 copyCmds := apexRule.Args["copy_commands"]
4109
4110 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4111}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004112
Jooyung Han91df2082019-11-20 01:49:42 +09004113func TestApexInVariousPartition(t *testing.T) {
4114 testcases := []struct {
4115 propName, parition, flattenedPartition string
4116 }{
4117 {"", "system", "system_ext"},
4118 {"product_specific: true", "product", "product"},
4119 {"soc_specific: true", "vendor", "vendor"},
4120 {"proprietary: true", "vendor", "vendor"},
4121 {"vendor: true", "vendor", "vendor"},
4122 {"system_ext_specific: true", "system_ext", "system_ext"},
4123 }
4124 for _, tc := range testcases {
4125 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004126 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004127 apex {
4128 name: "myapex",
4129 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004130 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004131 `+tc.propName+`
4132 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004133
Jooyung Han91df2082019-11-20 01:49:42 +09004134 apex_key {
4135 name: "myapex.key",
4136 public_key: "testkey.avbpubkey",
4137 private_key: "testkey.pem",
4138 }
4139 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004140
Jooyung Han91df2082019-11-20 01:49:42 +09004141 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004142 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4143 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004144 if actual != expected {
4145 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4146 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004147
Jooyung Han91df2082019-11-20 01:49:42 +09004148 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004149 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4150 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004151 if actual != expected {
4152 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4153 }
4154 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004155 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004156}
Jiyong Park67882562019-03-21 01:11:21 +09004157
Jooyung Han580eb4f2020-06-24 19:33:06 +09004158func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004159 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004160 apex {
4161 name: "myapex",
4162 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004163 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004164 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004165
Jooyung Han580eb4f2020-06-24 19:33:06 +09004166 apex_key {
4167 name: "myapex.key",
4168 public_key: "testkey.avbpubkey",
4169 private_key: "testkey.pem",
4170 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004171 `)
4172 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004173 rule := module.Output("file_contexts")
4174 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4175}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004176
Jooyung Han580eb4f2020-06-24 19:33:06 +09004177func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004178 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004179 apex {
4180 name: "myapex",
4181 key: "myapex.key",
4182 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004183 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004184 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004185
Jooyung Han580eb4f2020-06-24 19:33:06 +09004186 apex_key {
4187 name: "myapex.key",
4188 public_key: "testkey.avbpubkey",
4189 private_key: "testkey.pem",
4190 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004191 `, withFiles(map[string][]byte{
4192 "my_own_file_contexts": nil,
4193 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004194}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004195
Jooyung Han580eb4f2020-06-24 19:33:06 +09004196func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004197 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004198 apex {
4199 name: "myapex",
4200 key: "myapex.key",
4201 product_specific: true,
4202 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004203 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004204 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004205
Jooyung Han580eb4f2020-06-24 19:33:06 +09004206 apex_key {
4207 name: "myapex.key",
4208 public_key: "testkey.avbpubkey",
4209 private_key: "testkey.pem",
4210 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004211 `)
4212
Colin Cross1c460562021-02-16 17:55:47 -08004213 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004214 apex {
4215 name: "myapex",
4216 key: "myapex.key",
4217 product_specific: true,
4218 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004219 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004220 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004221
Jooyung Han580eb4f2020-06-24 19:33:06 +09004222 apex_key {
4223 name: "myapex.key",
4224 public_key: "testkey.avbpubkey",
4225 private_key: "testkey.pem",
4226 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004227 `, withFiles(map[string][]byte{
4228 "product_specific_file_contexts": nil,
4229 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004230 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4231 rule := module.Output("file_contexts")
4232 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4233}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004234
Jooyung Han580eb4f2020-06-24 19:33:06 +09004235func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004236 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004237 apex {
4238 name: "myapex",
4239 key: "myapex.key",
4240 product_specific: true,
4241 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004242 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004243 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004244
Jooyung Han580eb4f2020-06-24 19:33:06 +09004245 apex_key {
4246 name: "myapex.key",
4247 public_key: "testkey.avbpubkey",
4248 private_key: "testkey.pem",
4249 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004250
Jooyung Han580eb4f2020-06-24 19:33:06 +09004251 filegroup {
4252 name: "my-file-contexts",
4253 srcs: ["product_specific_file_contexts"],
4254 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004255 `, withFiles(map[string][]byte{
4256 "product_specific_file_contexts": nil,
4257 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004258 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4259 rule := module.Output("file_contexts")
4260 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004261}
4262
Jiyong Park67882562019-03-21 01:11:21 +09004263func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004264 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004265 apex_key {
4266 name: "myapex.key",
4267 public_key: ":my.avbpubkey",
4268 private_key: ":my.pem",
4269 product_specific: true,
4270 }
4271
4272 filegroup {
4273 name: "my.avbpubkey",
4274 srcs: ["testkey2.avbpubkey"],
4275 }
4276
4277 filegroup {
4278 name: "my.pem",
4279 srcs: ["testkey2.pem"],
4280 }
4281 `)
4282
4283 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4284 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004285 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004286 if actual_pubkey != expected_pubkey {
4287 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4288 }
4289 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004290 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004291 if actual_privkey != expected_privkey {
4292 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4293 }
4294}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004295
4296func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004297 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004298 prebuilt_apex {
4299 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004300 arch: {
4301 arm64: {
4302 src: "myapex-arm64.apex",
4303 },
4304 arm: {
4305 src: "myapex-arm.apex",
4306 },
4307 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004308 }
4309 `)
4310
4311 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4312
Jiyong Parkc95714e2019-03-29 14:23:10 +09004313 expectedInput := "myapex-arm64.apex"
4314 if prebuilt.inputApex.String() != expectedInput {
4315 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4316 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004317}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004318
Paul Duffinc0609c62021-03-01 17:27:16 +00004319func TestPrebuiltMissingSrc(t *testing.T) {
4320 testApexError(t, `module "myapex" variant "android_common".*: prebuilt_apex does not support "arm64_armv8-a"`, `
4321 prebuilt_apex {
4322 name: "myapex",
4323 }
4324 `)
4325}
4326
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004327func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004328 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004329 prebuilt_apex {
4330 name: "myapex",
4331 src: "myapex-arm.apex",
4332 filename: "notmyapex.apex",
4333 }
4334 `)
4335
4336 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4337
4338 expected := "notmyapex.apex"
4339 if p.installFilename != expected {
4340 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4341 }
4342}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004343
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004344func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004345 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004346 prebuilt_apex {
4347 name: "myapex.prebuilt",
4348 src: "myapex-arm.apex",
4349 overrides: [
4350 "myapex",
4351 ],
4352 }
4353 `)
4354
4355 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
4356
4357 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004358 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004359 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004360 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004361 }
4362}
4363
Paul Duffin092153d2021-01-26 11:42:39 +00004364// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4365// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004366func TestPrebuiltExportDexImplementationJars(t *testing.T) {
4367 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin092153d2021-01-26 11:42:39 +00004368 // Empty transformation.
Paul Duffin064b70c2020-11-02 17:32:38 +00004369 }
4370
Paul Duffin89886cb2021-02-05 16:44:03 +00004371 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004372 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004373 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004374 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004375 stem := android.RemoveOptionalPrebuiltPrefix(name)
4376 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004377 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4378 }
4379 }
4380
Paul Duffin39853512021-02-26 11:09:39 +00004381 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004382 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004383 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004384 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4385 }
4386 }
4387
4388 t.Run("prebuilt only", func(t *testing.T) {
4389 bp := `
4390 prebuilt_apex {
4391 name: "myapex",
4392 arch: {
4393 arm64: {
4394 src: "myapex-arm64.apex",
4395 },
4396 arm: {
4397 src: "myapex-arm.apex",
4398 },
4399 },
Paul Duffin39853512021-02-26 11:09:39 +00004400 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004401 }
4402
4403 java_import {
4404 name: "libfoo",
4405 jars: ["libfoo.jar"],
4406 }
Paul Duffin39853512021-02-26 11:09:39 +00004407
4408 java_sdk_library_import {
4409 name: "libbar",
4410 public: {
4411 jars: ["libbar.jar"],
4412 },
4413 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004414 `
4415
4416 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4417 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4418
Paul Duffinf6932af2021-02-26 18:21:56 +00004419 // Make sure that the deapexer has the correct input APEX.
4420 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4421 rule := deapexer.Rule("deapexer")
4422 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4423 t.Errorf("expected: %q, found: %q", expected, actual)
4424 }
4425
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004426 // Make sure that the prebuilt_apex has the correct input APEX.
4427 prebuiltApex := ctx.ModuleForTests("myapex", "android_common")
4428 rule = prebuiltApex.Rule("android/soong/android.Cp")
4429 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4430 t.Errorf("expected: %q, found: %q", expected, actual)
4431 }
4432
Paul Duffin89886cb2021-02-05 16:44:03 +00004433 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004434
4435 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004436 })
4437
4438 t.Run("prebuilt with source preferred", func(t *testing.T) {
4439
4440 bp := `
4441 prebuilt_apex {
4442 name: "myapex",
4443 arch: {
4444 arm64: {
4445 src: "myapex-arm64.apex",
4446 },
4447 arm: {
4448 src: "myapex-arm.apex",
4449 },
4450 },
Paul Duffin39853512021-02-26 11:09:39 +00004451 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004452 }
4453
4454 java_import {
4455 name: "libfoo",
4456 jars: ["libfoo.jar"],
4457 }
4458
4459 java_library {
4460 name: "libfoo",
4461 }
Paul Duffin39853512021-02-26 11:09:39 +00004462
4463 java_sdk_library_import {
4464 name: "libbar",
4465 public: {
4466 jars: ["libbar.jar"],
4467 },
4468 }
4469
4470 java_sdk_library {
4471 name: "libbar",
4472 srcs: ["foo/bar/MyClass.java"],
4473 unsafe_ignore_missing_latest_api: true,
4474 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004475 `
4476
4477 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4478 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4479
Paul Duffin89886cb2021-02-05 16:44:03 +00004480 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004481 ensureNoSourceVariant(t, ctx, "libfoo")
4482
4483 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4484 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004485 })
4486
4487 t.Run("prebuilt preferred with source", func(t *testing.T) {
4488 bp := `
4489 prebuilt_apex {
4490 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004491 arch: {
4492 arm64: {
4493 src: "myapex-arm64.apex",
4494 },
4495 arm: {
4496 src: "myapex-arm.apex",
4497 },
4498 },
Paul Duffin39853512021-02-26 11:09:39 +00004499 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004500 }
4501
4502 java_import {
4503 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004504 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004505 jars: ["libfoo.jar"],
4506 }
4507
4508 java_library {
4509 name: "libfoo",
4510 }
Paul Duffin39853512021-02-26 11:09:39 +00004511
4512 java_sdk_library_import {
4513 name: "libbar",
4514 prefer: true,
4515 public: {
4516 jars: ["libbar.jar"],
4517 },
4518 }
4519
4520 java_sdk_library {
4521 name: "libbar",
4522 srcs: ["foo/bar/MyClass.java"],
4523 unsafe_ignore_missing_latest_api: true,
4524 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004525 `
4526
4527 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4528 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4529
Paul Duffin89886cb2021-02-05 16:44:03 +00004530 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004531 ensureNoSourceVariant(t, ctx, "libfoo")
4532
4533 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4534 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004535 })
4536}
4537
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004538func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
4539 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin37856732021-02-26 14:24:15 +00004540 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo", "myapex:libbar"})
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004541 }
4542
Paul Duffin37856732021-02-26 14:24:15 +00004543 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4544 t.Helper()
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004545 s := ctx.SingletonForTests("dex_bootjars")
4546 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004547 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004548 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004549 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004550 foundLibfooJar = true
4551 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01004552 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004553 }
4554 }
4555 if !foundLibfooJar {
Paul Duffin55607122021-03-30 23:32:51 +01004556 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 +00004557 }
4558 }
4559
Paul Duffin4fd997b2021-02-03 20:06:33 +00004560 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004561 t.Helper()
Paul Duffin4fd997b2021-02-03 20:06:33 +00004562 hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
4563 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
4564 java.CheckHiddenAPIRuleInputs(t, expectedInputs, indexRule)
4565 }
4566
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004567 t.Run("prebuilt only", func(t *testing.T) {
4568 bp := `
4569 prebuilt_apex {
4570 name: "myapex",
4571 arch: {
4572 arm64: {
4573 src: "myapex-arm64.apex",
4574 },
4575 arm: {
4576 src: "myapex-arm.apex",
4577 },
4578 },
Paul Duffin37856732021-02-26 14:24:15 +00004579 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004580 }
4581
4582 java_import {
4583 name: "libfoo",
4584 jars: ["libfoo.jar"],
4585 apex_available: ["myapex"],
4586 }
Paul Duffin37856732021-02-26 14:24:15 +00004587
4588 java_sdk_library_import {
4589 name: "libbar",
4590 public: {
4591 jars: ["libbar.jar"],
4592 },
4593 apex_available: ["myapex"],
4594 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004595 `
4596
4597 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin55607122021-03-30 23:32:51 +01004598 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4599 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004600
Paul Duffin9d67ca62021-02-03 20:06:33 +00004601 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4602 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004603.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004604.intermediates/libfoo/android_common_myapex/hiddenapi/index.csv
4605`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004606 })
4607
4608 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4609 bp := `
4610 prebuilt_apex {
4611 name: "myapex",
4612 arch: {
4613 arm64: {
4614 src: "myapex-arm64.apex",
4615 },
4616 arm: {
4617 src: "myapex-arm.apex",
4618 },
4619 },
Paul Duffin37856732021-02-26 14:24:15 +00004620 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004621 }
4622
4623 java_import {
4624 name: "libfoo",
4625 jars: ["libfoo.jar"],
4626 apex_available: ["myapex"],
4627 }
4628
4629 java_library {
4630 name: "libfoo",
4631 srcs: ["foo/bar/MyClass.java"],
4632 apex_available: ["myapex"],
4633 }
Paul Duffin37856732021-02-26 14:24:15 +00004634
4635 java_sdk_library_import {
4636 name: "libbar",
4637 public: {
4638 jars: ["libbar.jar"],
4639 },
4640 apex_available: ["myapex"],
4641 }
4642
4643 java_sdk_library {
4644 name: "libbar",
4645 srcs: ["foo/bar/MyClass.java"],
4646 unsafe_ignore_missing_latest_api: true,
4647 apex_available: ["myapex"],
4648 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004649 `
4650
4651 // In this test the source (java_library) libfoo is active since the
4652 // prebuilt (java_import) defaults to prefer:false. However the
4653 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4654 // find the dex boot jar in it. We either need to disable the source libfoo
4655 // or make the prebuilt libfoo preferred.
4656 testDexpreoptWithApexes(t, bp, "failed to find a dex jar path for module 'libfoo'", transform)
4657 })
4658
4659 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4660 bp := `
4661 prebuilt_apex {
4662 name: "myapex",
4663 arch: {
4664 arm64: {
4665 src: "myapex-arm64.apex",
4666 },
4667 arm: {
4668 src: "myapex-arm.apex",
4669 },
4670 },
Paul Duffin37856732021-02-26 14:24:15 +00004671 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004672 }
4673
4674 java_import {
4675 name: "libfoo",
4676 prefer: true,
4677 jars: ["libfoo.jar"],
4678 apex_available: ["myapex"],
4679 }
4680
4681 java_library {
4682 name: "libfoo",
4683 srcs: ["foo/bar/MyClass.java"],
4684 apex_available: ["myapex"],
4685 }
Paul Duffin37856732021-02-26 14:24:15 +00004686
4687 java_sdk_library_import {
4688 name: "libbar",
4689 prefer: true,
4690 public: {
4691 jars: ["libbar.jar"],
4692 },
4693 apex_available: ["myapex"],
4694 }
4695
4696 java_sdk_library {
4697 name: "libbar",
4698 srcs: ["foo/bar/MyClass.java"],
4699 unsafe_ignore_missing_latest_api: true,
4700 apex_available: ["myapex"],
4701 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004702 `
4703
4704 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin55607122021-03-30 23:32:51 +01004705 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4706 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004707
Paul Duffin9d67ca62021-02-03 20:06:33 +00004708 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4709 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004710.intermediates/prebuilt_libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004711.intermediates/prebuilt_libfoo/android_common_myapex/hiddenapi/index.csv
4712`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004713 })
4714
4715 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4716 bp := `
4717 apex {
4718 name: "myapex",
4719 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004720 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004721 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004722 }
4723
4724 apex_key {
4725 name: "myapex.key",
4726 public_key: "testkey.avbpubkey",
4727 private_key: "testkey.pem",
4728 }
4729
4730 prebuilt_apex {
4731 name: "myapex",
4732 arch: {
4733 arm64: {
4734 src: "myapex-arm64.apex",
4735 },
4736 arm: {
4737 src: "myapex-arm.apex",
4738 },
4739 },
Paul Duffin37856732021-02-26 14:24:15 +00004740 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004741 }
4742
4743 java_import {
4744 name: "libfoo",
4745 jars: ["libfoo.jar"],
4746 apex_available: ["myapex"],
4747 }
4748
4749 java_library {
4750 name: "libfoo",
4751 srcs: ["foo/bar/MyClass.java"],
4752 apex_available: ["myapex"],
4753 }
Paul Duffin37856732021-02-26 14:24:15 +00004754
4755 java_sdk_library_import {
4756 name: "libbar",
4757 public: {
4758 jars: ["libbar.jar"],
4759 },
4760 apex_available: ["myapex"],
4761 }
4762
4763 java_sdk_library {
4764 name: "libbar",
4765 srcs: ["foo/bar/MyClass.java"],
4766 unsafe_ignore_missing_latest_api: true,
4767 apex_available: ["myapex"],
4768 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004769 `
4770
4771 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin55607122021-03-30 23:32:51 +01004772 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4773 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004774
4775 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4776 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004777.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin4fd997b2021-02-03 20:06:33 +00004778.intermediates/libfoo/android_common_apex10000/hiddenapi/index.csv
4779`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004780 })
4781
4782 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4783 bp := `
4784 apex {
4785 name: "myapex",
4786 enabled: false,
4787 key: "myapex.key",
4788 java_libs: ["libfoo"],
4789 }
4790
4791 apex_key {
4792 name: "myapex.key",
4793 public_key: "testkey.avbpubkey",
4794 private_key: "testkey.pem",
4795 }
4796
4797 prebuilt_apex {
4798 name: "myapex",
4799 arch: {
4800 arm64: {
4801 src: "myapex-arm64.apex",
4802 },
4803 arm: {
4804 src: "myapex-arm.apex",
4805 },
4806 },
Paul Duffin37856732021-02-26 14:24:15 +00004807 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004808 }
4809
4810 java_import {
4811 name: "libfoo",
4812 prefer: true,
4813 jars: ["libfoo.jar"],
4814 apex_available: ["myapex"],
4815 }
4816
4817 java_library {
4818 name: "libfoo",
4819 srcs: ["foo/bar/MyClass.java"],
4820 apex_available: ["myapex"],
4821 }
Paul Duffin37856732021-02-26 14:24:15 +00004822
4823 java_sdk_library_import {
4824 name: "libbar",
4825 prefer: true,
4826 public: {
4827 jars: ["libbar.jar"],
4828 },
4829 apex_available: ["myapex"],
4830 }
4831
4832 java_sdk_library {
4833 name: "libbar",
4834 srcs: ["foo/bar/MyClass.java"],
4835 unsafe_ignore_missing_latest_api: true,
4836 apex_available: ["myapex"],
4837 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004838 `
4839
4840 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin55607122021-03-30 23:32:51 +01004841 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4842 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004843
Paul Duffin9d67ca62021-02-03 20:06:33 +00004844 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4845 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004846.intermediates/prebuilt_libbar/android_common_prebuilt_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004847.intermediates/prebuilt_libfoo/android_common_prebuilt_myapex/hiddenapi/index.csv
4848`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004849 })
4850}
4851
Roland Levillain630846d2019-06-26 12:48:34 +01004852func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004853 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004854 apex_test {
4855 name: "myapex",
4856 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004857 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01004858 tests: [
4859 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004860 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004861 ],
4862 }
4863
4864 apex_key {
4865 name: "myapex.key",
4866 public_key: "testkey.avbpubkey",
4867 private_key: "testkey.pem",
4868 }
4869
Liz Kammer1c14a212020-05-12 15:26:55 -07004870 filegroup {
4871 name: "fg",
4872 srcs: [
4873 "baz",
4874 "bar/baz"
4875 ],
4876 }
4877
Roland Levillain630846d2019-06-26 12:48:34 +01004878 cc_test {
4879 name: "mytest",
4880 gtest: false,
4881 srcs: ["mytest.cpp"],
4882 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004883 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004884 system_shared_libs: [],
4885 static_executable: true,
4886 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004887 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004888 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004889
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004890 cc_library {
4891 name: "mylib",
4892 srcs: ["mylib.cpp"],
4893 system_shared_libs: [],
4894 stl: "none",
4895 }
4896
Liz Kammer5bd365f2020-05-27 15:15:11 -07004897 filegroup {
4898 name: "fg2",
4899 srcs: [
4900 "testdata/baz"
4901 ],
4902 }
4903
Roland Levillain9b5fde92019-06-28 15:41:19 +01004904 cc_test {
4905 name: "mytests",
4906 gtest: false,
4907 srcs: [
4908 "mytest1.cpp",
4909 "mytest2.cpp",
4910 "mytest3.cpp",
4911 ],
4912 test_per_src: true,
4913 relative_install_path: "test",
4914 system_shared_libs: [],
4915 static_executable: true,
4916 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004917 data: [
4918 ":fg",
4919 ":fg2",
4920 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004921 }
Roland Levillain630846d2019-06-26 12:48:34 +01004922 `)
4923
Sundong Ahnabb64432019-10-22 13:58:29 +09004924 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004925 copyCmds := apexRule.Args["copy_commands"]
4926
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004927 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004928 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004929 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004930
Liz Kammer1c14a212020-05-12 15:26:55 -07004931 //Ensure that test data are copied into apex.
4932 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4933 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4934
Roland Levillain9b5fde92019-06-28 15:41:19 +01004935 // Ensure that test deps built with `test_per_src` are copied into apex.
4936 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4937 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4938 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004939
4940 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004941 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004942 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004943 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004944 prefix := "TARGET_"
4945 var builder strings.Builder
4946 data.Custom(&builder, name, prefix, "", data)
4947 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004948 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4949 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4950 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4951 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004952 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004953 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004954 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004955
4956 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004957 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004958 data.Custom(&builder, name, prefix, "", data)
4959 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004960 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4961 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004962}
4963
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004964func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004965 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004966 apex {
4967 name: "myapex",
4968 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004969 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004970 }
4971 apex_key {
4972 name: "myapex.key",
4973 public_key: "testkey.avbpubkey",
4974 private_key: "testkey.pem",
4975 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00004976 `,
4977 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4978 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4979 }),
4980 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004981 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004982 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07004983 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004984 var builder strings.Builder
4985 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4986 androidMk := builder.String()
4987 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4988}
4989
Jooyung Hand48f3c32019-08-23 11:18:57 +09004990func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4991 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4992 apex {
4993 name: "myapex",
4994 key: "myapex.key",
4995 native_shared_libs: ["libfoo"],
4996 }
4997
4998 apex_key {
4999 name: "myapex.key",
5000 public_key: "testkey.avbpubkey",
5001 private_key: "testkey.pem",
5002 }
5003
5004 cc_library {
5005 name: "libfoo",
5006 stl: "none",
5007 system_shared_libs: [],
5008 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005009 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005010 }
5011 `)
5012 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5013 apex {
5014 name: "myapex",
5015 key: "myapex.key",
5016 java_libs: ["myjar"],
5017 }
5018
5019 apex_key {
5020 name: "myapex.key",
5021 public_key: "testkey.avbpubkey",
5022 private_key: "testkey.pem",
5023 }
5024
5025 java_library {
5026 name: "myjar",
5027 srcs: ["foo/bar/MyClass.java"],
5028 sdk_version: "none",
5029 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005030 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005031 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005032 }
5033 `)
5034}
5035
Bill Peckhama41a6962021-01-11 10:58:54 -08005036func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005037 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005038 apex {
5039 name: "myapex",
5040 key: "myapex.key",
5041 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005042 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005043 }
5044
5045 apex_key {
5046 name: "myapex.key",
5047 public_key: "testkey.avbpubkey",
5048 private_key: "testkey.pem",
5049 }
5050
5051 java_import {
5052 name: "myjavaimport",
5053 apex_available: ["myapex"],
5054 jars: ["my.jar"],
5055 compile_dex: true,
5056 }
5057 `)
5058
5059 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5060 apexRule := module.Rule("apexRule")
5061 copyCmds := apexRule.Args["copy_commands"]
5062 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5063}
5064
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005065func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005066 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005067 apex {
5068 name: "myapex",
5069 key: "myapex.key",
5070 apps: [
5071 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005072 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005073 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005074 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005075 }
5076
5077 apex_key {
5078 name: "myapex.key",
5079 public_key: "testkey.avbpubkey",
5080 private_key: "testkey.pem",
5081 }
5082
5083 android_app {
5084 name: "AppFoo",
5085 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005086 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005087 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005088 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005089 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005090 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005091 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005092
5093 android_app {
5094 name: "AppFooPriv",
5095 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005096 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005097 system_modules: "none",
5098 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005099 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005100 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005101 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005102
5103 cc_library_shared {
5104 name: "libjni",
5105 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005106 shared_libs: ["libfoo"],
5107 stl: "none",
5108 system_shared_libs: [],
5109 apex_available: [ "myapex" ],
5110 sdk_version: "current",
5111 }
5112
5113 cc_library_shared {
5114 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005115 stl: "none",
5116 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005117 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005118 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005119 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005120 `)
5121
Sundong Ahnabb64432019-10-22 13:58:29 +09005122 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005123 apexRule := module.Rule("apexRule")
5124 copyCmds := apexRule.Args["copy_commands"]
5125
5126 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005127 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005128
Colin Crossaede88c2020-08-11 12:17:01 -07005129 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005130 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005131 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005132 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005133 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005134 // JNI libraries including transitive deps are
5135 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005136 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005137 // ... embedded inside APK (jnilibs.zip)
5138 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5139 // ... and not directly inside the APEX
5140 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5141 }
Dario Frenicde2a032019-10-27 00:29:22 +01005142}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005143
Dario Frenicde2a032019-10-27 00:29:22 +01005144func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005145 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005146 apex {
5147 name: "myapex",
5148 key: "myapex.key",
5149 apps: [
5150 "AppFooPrebuilt",
5151 "AppFooPrivPrebuilt",
5152 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005153 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005154 }
5155
5156 apex_key {
5157 name: "myapex.key",
5158 public_key: "testkey.avbpubkey",
5159 private_key: "testkey.pem",
5160 }
5161
5162 android_app_import {
5163 name: "AppFooPrebuilt",
5164 apk: "PrebuiltAppFoo.apk",
5165 presigned: true,
5166 dex_preopt: {
5167 enabled: false,
5168 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005169 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005170 }
5171
5172 android_app_import {
5173 name: "AppFooPrivPrebuilt",
5174 apk: "PrebuiltAppFooPriv.apk",
5175 privileged: true,
5176 presigned: true,
5177 dex_preopt: {
5178 enabled: false,
5179 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005180 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005181 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005182 }
5183 `)
5184
Sundong Ahnabb64432019-10-22 13:58:29 +09005185 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005186 apexRule := module.Rule("apexRule")
5187 copyCmds := apexRule.Args["copy_commands"]
5188
5189 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005190 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5191}
5192
5193func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005194 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005195 apex {
5196 name: "myapex",
5197 key: "myapex.key",
5198 apps: [
5199 "AppFoo",
5200 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005201 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005202 }
5203
5204 apex_key {
5205 name: "myapex.key",
5206 public_key: "testkey.avbpubkey",
5207 private_key: "testkey.pem",
5208 }
5209
5210 android_app {
5211 name: "AppFoo",
5212 srcs: ["foo/bar/MyClass.java"],
5213 sdk_version: "none",
5214 system_modules: "none",
5215 apex_available: [ "myapex" ],
5216 }
5217
5218 android_app_import {
5219 name: "AppFoo",
5220 apk: "AppFooPrebuilt.apk",
5221 filename: "AppFooPrebuilt.apk",
5222 presigned: true,
5223 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005224 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005225 }
5226 `, withFiles(map[string][]byte{
5227 "AppFooPrebuilt.apk": nil,
5228 }))
5229
5230 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005231 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005232 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005233}
5234
Dario Freni6f3937c2019-12-20 22:58:03 +00005235func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005236 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005237 apex {
5238 name: "myapex",
5239 key: "myapex.key",
5240 apps: [
5241 "TesterHelpAppFoo",
5242 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005243 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005244 }
5245
5246 apex_key {
5247 name: "myapex.key",
5248 public_key: "testkey.avbpubkey",
5249 private_key: "testkey.pem",
5250 }
5251
5252 android_test_helper_app {
5253 name: "TesterHelpAppFoo",
5254 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005255 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005256 }
5257
5258 `)
5259
5260 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5261 apexRule := module.Rule("apexRule")
5262 copyCmds := apexRule.Args["copy_commands"]
5263
5264 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5265}
5266
Jooyung Han18020ea2019-11-13 10:50:48 +09005267func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5268 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005269 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005270 apex {
5271 name: "myapex",
5272 key: "myapex.key",
5273 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005274 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005275 }
5276
5277 apex_key {
5278 name: "myapex.key",
5279 public_key: "testkey.avbpubkey",
5280 private_key: "testkey.pem",
5281 }
5282
5283 apex {
5284 name: "otherapex",
5285 key: "myapex.key",
5286 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005287 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005288 }
5289
5290 cc_defaults {
5291 name: "libfoo-defaults",
5292 apex_available: ["otherapex"],
5293 }
5294
5295 cc_library {
5296 name: "libfoo",
5297 defaults: ["libfoo-defaults"],
5298 stl: "none",
5299 system_shared_libs: [],
5300 }`)
5301}
5302
Paul Duffine52e66f2020-03-30 17:54:29 +01005303func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005304 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005305 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005306 apex {
5307 name: "myapex",
5308 key: "myapex.key",
5309 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005310 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005311 }
5312
5313 apex_key {
5314 name: "myapex.key",
5315 public_key: "testkey.avbpubkey",
5316 private_key: "testkey.pem",
5317 }
5318
5319 apex {
5320 name: "otherapex",
5321 key: "otherapex.key",
5322 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005323 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005324 }
5325
5326 apex_key {
5327 name: "otherapex.key",
5328 public_key: "testkey.avbpubkey",
5329 private_key: "testkey.pem",
5330 }
5331
5332 cc_library {
5333 name: "libfoo",
5334 stl: "none",
5335 system_shared_libs: [],
5336 apex_available: ["otherapex"],
5337 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005338}
Jiyong Park127b40b2019-09-30 16:04:35 +09005339
Paul Duffine52e66f2020-03-30 17:54:29 +01005340func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005341 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005342 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005343.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005344.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005345.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005346.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005347.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005348.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005349 apex {
5350 name: "myapex",
5351 key: "myapex.key",
5352 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005353 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005354 }
5355
5356 apex_key {
5357 name: "myapex.key",
5358 public_key: "testkey.avbpubkey",
5359 private_key: "testkey.pem",
5360 }
5361
Jiyong Park127b40b2019-09-30 16:04:35 +09005362 cc_library {
5363 name: "libfoo",
5364 stl: "none",
5365 shared_libs: ["libbar"],
5366 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005367 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005368 }
5369
5370 cc_library {
5371 name: "libbar",
5372 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005373 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005374 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005375 apex_available: ["myapex"],
5376 }
5377
5378 cc_library {
5379 name: "libbaz",
5380 stl: "none",
5381 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005382 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005383}
Jiyong Park127b40b2019-09-30 16:04:35 +09005384
Paul Duffine52e66f2020-03-30 17:54:29 +01005385func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005386 testApexError(t, "\"otherapex\" is not a valid module name", `
5387 apex {
5388 name: "myapex",
5389 key: "myapex.key",
5390 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005391 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005392 }
5393
5394 apex_key {
5395 name: "myapex.key",
5396 public_key: "testkey.avbpubkey",
5397 private_key: "testkey.pem",
5398 }
5399
5400 cc_library {
5401 name: "libfoo",
5402 stl: "none",
5403 system_shared_libs: [],
5404 apex_available: ["otherapex"],
5405 }`)
5406
Paul Duffine52e66f2020-03-30 17:54:29 +01005407 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005408 apex {
5409 name: "myapex",
5410 key: "myapex.key",
5411 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005412 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005413 }
5414
5415 apex_key {
5416 name: "myapex.key",
5417 public_key: "testkey.avbpubkey",
5418 private_key: "testkey.pem",
5419 }
5420
5421 cc_library {
5422 name: "libfoo",
5423 stl: "none",
5424 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005425 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005426 apex_available: ["myapex"],
5427 }
5428
5429 cc_library {
5430 name: "libbar",
5431 stl: "none",
5432 system_shared_libs: [],
5433 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005434 }
5435
5436 cc_library {
5437 name: "libbaz",
5438 stl: "none",
5439 system_shared_libs: [],
5440 stubs: {
5441 versions: ["10", "20", "30"],
5442 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005443 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005444}
Jiyong Park127b40b2019-09-30 16:04:35 +09005445
Jiyong Park89e850a2020-04-07 16:37:39 +09005446func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005447 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005448 apex {
5449 name: "myapex",
5450 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005451 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005452 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005453 }
5454
5455 apex_key {
5456 name: "myapex.key",
5457 public_key: "testkey.avbpubkey",
5458 private_key: "testkey.pem",
5459 }
5460
5461 cc_library {
5462 name: "libfoo",
5463 stl: "none",
5464 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005465 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005466 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005467 }
5468
5469 cc_library {
5470 name: "libfoo2",
5471 stl: "none",
5472 system_shared_libs: [],
5473 shared_libs: ["libbaz"],
5474 apex_available: ["//apex_available:platform"],
5475 }
5476
5477 cc_library {
5478 name: "libbar",
5479 stl: "none",
5480 system_shared_libs: [],
5481 apex_available: ["myapex"],
5482 }
5483
5484 cc_library {
5485 name: "libbaz",
5486 stl: "none",
5487 system_shared_libs: [],
5488 apex_available: ["myapex"],
5489 stubs: {
5490 versions: ["1"],
5491 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005492 }`)
5493
Jiyong Park89e850a2020-04-07 16:37:39 +09005494 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5495 // because it depends on libbar which isn't available to platform
5496 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5497 if libfoo.NotAvailableForPlatform() != true {
5498 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5499 }
5500
5501 // libfoo2 however can be available to platform because it depends on libbaz which provides
5502 // stubs
5503 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5504 if libfoo2.NotAvailableForPlatform() == true {
5505 t.Errorf("%q should be available to platform", libfoo2.String())
5506 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005507}
Jiyong Parka90ca002019-10-07 15:47:24 +09005508
Paul Duffine52e66f2020-03-30 17:54:29 +01005509func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005510 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005511 apex {
5512 name: "myapex",
5513 key: "myapex.key",
5514 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005515 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005516 }
5517
5518 apex_key {
5519 name: "myapex.key",
5520 public_key: "testkey.avbpubkey",
5521 private_key: "testkey.pem",
5522 }
5523
5524 cc_library {
5525 name: "libfoo",
5526 stl: "none",
5527 system_shared_libs: [],
5528 apex_available: ["myapex"],
5529 static: {
5530 apex_available: ["//apex_available:platform"],
5531 },
5532 }`)
5533
Jiyong Park89e850a2020-04-07 16:37:39 +09005534 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5535 if libfooShared.NotAvailableForPlatform() != true {
5536 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5537 }
5538 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5539 if libfooStatic.NotAvailableForPlatform() != false {
5540 t.Errorf("%q should be available to platform", libfooStatic.String())
5541 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005542}
5543
Jiyong Park5d790c32019-11-15 18:40:32 +09005544func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005545 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005546 apex {
5547 name: "myapex",
5548 key: "myapex.key",
5549 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005550 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005551 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005552 }
5553
5554 override_apex {
5555 name: "override_myapex",
5556 base: "myapex",
5557 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005558 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005559 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005560 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09005561 }
5562
5563 apex_key {
5564 name: "myapex.key",
5565 public_key: "testkey.avbpubkey",
5566 private_key: "testkey.pem",
5567 }
5568
5569 android_app {
5570 name: "app",
5571 srcs: ["foo/bar/MyClass.java"],
5572 package_name: "foo",
5573 sdk_version: "none",
5574 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005575 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005576 }
5577
5578 override_android_app {
5579 name: "override_app",
5580 base: "app",
5581 package_name: "bar",
5582 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005583 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005584
Jiyong Park317645e2019-12-05 13:20:58 +09005585 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5586 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5587 if originalVariant.GetOverriddenBy() != "" {
5588 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5589 }
5590 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5591 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5592 }
5593
Jiyong Park5d790c32019-11-15 18:40:32 +09005594 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5595 apexRule := module.Rule("apexRule")
5596 copyCmds := apexRule.Args["copy_commands"]
5597
5598 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005599 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005600
5601 apexBundle := module.Module().(*apexBundle)
5602 name := apexBundle.Name()
5603 if name != "override_myapex" {
5604 t.Errorf("name should be \"override_myapex\", but was %q", name)
5605 }
5606
Baligh Uddin004d7172020-02-19 21:29:28 -08005607 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5608 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5609 }
5610
Jiyong Park20bacab2020-03-03 11:45:41 +09005611 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005612 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09005613
Colin Crossaa255532020-07-03 13:18:24 -07005614 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005615 var builder strings.Builder
5616 data.Custom(&builder, name, "TARGET_", "", data)
5617 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005618 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005619 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5620 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005621 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005622 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005623 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005624 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5625 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005626}
5627
Jooyung Han214bf372019-11-12 13:03:50 +09005628func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005629 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005630 apex {
5631 name: "myapex",
5632 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005633 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005634 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005635 }
5636
5637 apex_key {
5638 name: "myapex.key",
5639 public_key: "testkey.avbpubkey",
5640 private_key: "testkey.pem",
5641 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005642
5643 cc_library {
5644 name: "mylib",
5645 srcs: ["mylib.cpp"],
5646 stl: "libc++",
5647 system_shared_libs: [],
5648 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005649 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005650 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005651 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005652
5653 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5654 args := module.Rule("apexRule").Args
5655 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005656 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005657
5658 // The copies of the libraries in the apex should have one more dependency than
5659 // the ones outside the apex, namely the unwinder. Ideally we should check
5660 // the dependency names directly here but for some reason the names are blank in
5661 // this test.
5662 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005663 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005664 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5665 if len(apexImplicits) != len(nonApexImplicits)+1 {
5666 t.Errorf("%q missing unwinder dep", lib)
5667 }
5668 }
Jooyung Han214bf372019-11-12 13:03:50 +09005669}
5670
Paul Duffine05480a2021-03-08 15:07:14 +00005671var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01005672 "api/current.txt": nil,
5673 "api/removed.txt": nil,
5674 "api/system-current.txt": nil,
5675 "api/system-removed.txt": nil,
5676 "api/test-current.txt": nil,
5677 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005678
Anton Hanssondff2c782020-12-21 17:10:01 +00005679 "100/public/api/foo.txt": nil,
5680 "100/public/api/foo-removed.txt": nil,
5681 "100/system/api/foo.txt": nil,
5682 "100/system/api/foo-removed.txt": nil,
5683
Paul Duffineedc5d52020-06-12 17:46:39 +01005684 // For java_sdk_library_import
5685 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005686}
5687
Jooyung Han58f26ab2019-12-18 15:34:32 +09005688func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005689 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09005690 apex {
5691 name: "myapex",
5692 key: "myapex.key",
5693 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005694 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09005695 }
5696
5697 apex_key {
5698 name: "myapex.key",
5699 public_key: "testkey.avbpubkey",
5700 private_key: "testkey.pem",
5701 }
5702
5703 java_sdk_library {
5704 name: "foo",
5705 srcs: ["a.java"],
5706 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005707 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005708 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005709
5710 prebuilt_apis {
5711 name: "sdk",
5712 api_dirs: ["100"],
5713 }
Paul Duffin9b879592020-05-26 13:21:35 +01005714 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005715
5716 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005717 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005718 "javalib/foo.jar",
5719 "etc/permissions/foo.xml",
5720 })
5721 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005722 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5723 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005724}
5725
Paul Duffin9b879592020-05-26 13:21:35 +01005726func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005727 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005728 apex {
5729 name: "myapex",
5730 key: "myapex.key",
5731 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005732 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005733 }
5734
5735 apex_key {
5736 name: "myapex.key",
5737 public_key: "testkey.avbpubkey",
5738 private_key: "testkey.pem",
5739 }
5740
5741 java_sdk_library {
5742 name: "foo",
5743 srcs: ["a.java"],
5744 api_packages: ["foo"],
5745 apex_available: ["myapex"],
5746 sdk_version: "none",
5747 system_modules: "none",
5748 }
5749
5750 java_library {
5751 name: "bar",
5752 srcs: ["a.java"],
5753 libs: ["foo"],
5754 apex_available: ["myapex"],
5755 sdk_version: "none",
5756 system_modules: "none",
5757 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005758
5759 prebuilt_apis {
5760 name: "sdk",
5761 api_dirs: ["100"],
5762 }
Paul Duffin9b879592020-05-26 13:21:35 +01005763 `, withFiles(filesForSdkLibrary))
5764
5765 // java_sdk_library installs both impl jar and permission XML
5766 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5767 "javalib/bar.jar",
5768 "javalib/foo.jar",
5769 "etc/permissions/foo.xml",
5770 })
5771
5772 // The bar library should depend on the implementation jar.
5773 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005774 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005775 t.Errorf("expected %q, found %#q", expected, actual)
5776 }
5777}
5778
5779func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005780 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005781 apex {
5782 name: "myapex",
5783 key: "myapex.key",
5784 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005785 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005786 }
5787
5788 apex_key {
5789 name: "myapex.key",
5790 public_key: "testkey.avbpubkey",
5791 private_key: "testkey.pem",
5792 }
5793
5794 java_sdk_library {
5795 name: "foo",
5796 srcs: ["a.java"],
5797 api_packages: ["foo"],
5798 apex_available: ["myapex"],
5799 sdk_version: "none",
5800 system_modules: "none",
5801 }
5802
5803 java_library {
5804 name: "bar",
5805 srcs: ["a.java"],
5806 libs: ["foo"],
5807 sdk_version: "none",
5808 system_modules: "none",
5809 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005810
5811 prebuilt_apis {
5812 name: "sdk",
5813 api_dirs: ["100"],
5814 }
Paul Duffin9b879592020-05-26 13:21:35 +01005815 `, withFiles(filesForSdkLibrary))
5816
5817 // java_sdk_library installs both impl jar and permission XML
5818 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5819 "javalib/foo.jar",
5820 "etc/permissions/foo.xml",
5821 })
5822
5823 // The bar library should depend on the stubs jar.
5824 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005825 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005826 t.Errorf("expected %q, found %#q", expected, actual)
5827 }
5828}
5829
Paul Duffineedc5d52020-06-12 17:46:39 +01005830func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005831 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00005832 prebuilt_apis {
5833 name: "sdk",
5834 api_dirs: ["100"],
5835 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005836 withFiles(map[string][]byte{
5837 "apex/a.java": nil,
5838 "apex/apex_manifest.json": nil,
5839 "apex/Android.bp": []byte(`
5840 package {
5841 default_visibility: ["//visibility:private"],
5842 }
5843
5844 apex {
5845 name: "myapex",
5846 key: "myapex.key",
5847 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005848 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005849 }
5850
5851 apex_key {
5852 name: "myapex.key",
5853 public_key: "testkey.avbpubkey",
5854 private_key: "testkey.pem",
5855 }
5856
5857 java_library {
5858 name: "bar",
5859 srcs: ["a.java"],
5860 libs: ["foo"],
5861 apex_available: ["myapex"],
5862 sdk_version: "none",
5863 system_modules: "none",
5864 }
5865`),
5866 "source/a.java": nil,
5867 "source/api/current.txt": nil,
5868 "source/api/removed.txt": nil,
5869 "source/Android.bp": []byte(`
5870 package {
5871 default_visibility: ["//visibility:private"],
5872 }
5873
5874 java_sdk_library {
5875 name: "foo",
5876 visibility: ["//apex"],
5877 srcs: ["a.java"],
5878 api_packages: ["foo"],
5879 apex_available: ["myapex"],
5880 sdk_version: "none",
5881 system_modules: "none",
5882 public: {
5883 enabled: true,
5884 },
5885 }
5886`),
5887 "prebuilt/a.jar": nil,
5888 "prebuilt/Android.bp": []byte(`
5889 package {
5890 default_visibility: ["//visibility:private"],
5891 }
5892
5893 java_sdk_library_import {
5894 name: "foo",
5895 visibility: ["//apex", "//source"],
5896 apex_available: ["myapex"],
5897 prefer: true,
5898 public: {
5899 jars: ["a.jar"],
5900 },
5901 }
5902`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005903 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005904 )
5905
5906 // java_sdk_library installs both impl jar and permission XML
5907 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5908 "javalib/bar.jar",
5909 "javalib/foo.jar",
5910 "etc/permissions/foo.xml",
5911 })
5912
5913 // The bar library should depend on the implementation jar.
5914 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005915 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01005916 t.Errorf("expected %q, found %#q", expected, actual)
5917 }
5918}
5919
5920func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5921 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5922 apex {
5923 name: "myapex",
5924 key: "myapex.key",
5925 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005926 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005927 }
5928
5929 apex_key {
5930 name: "myapex.key",
5931 public_key: "testkey.avbpubkey",
5932 private_key: "testkey.pem",
5933 }
5934
5935 java_sdk_library_import {
5936 name: "foo",
5937 apex_available: ["myapex"],
5938 prefer: true,
5939 public: {
5940 jars: ["a.jar"],
5941 },
5942 }
5943
5944 `, withFiles(filesForSdkLibrary))
5945}
5946
atrost6e126252020-01-27 17:01:16 +00005947func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01005948 result := android.GroupFixturePreparers(
5949 prepareForApexTest,
5950 java.PrepareForTestWithPlatformCompatConfig,
5951 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00005952 apex {
5953 name: "myapex",
5954 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00005955 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00005956 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005957 updatable: false,
atrost6e126252020-01-27 17:01:16 +00005958 }
5959
5960 apex_key {
5961 name: "myapex.key",
5962 public_key: "testkey.avbpubkey",
5963 private_key: "testkey.pem",
5964 }
5965
5966 platform_compat_config {
5967 name: "myjar-platform-compat-config",
5968 src: ":myjar",
5969 }
5970
5971 java_library {
5972 name: "myjar",
5973 srcs: ["foo/bar/MyClass.java"],
5974 sdk_version: "none",
5975 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005976 apex_available: [ "myapex" ],
5977 }
Paul Duffin1b29e002021-03-16 15:06:54 +00005978
5979 // Make sure that a preferred prebuilt does not affect the apex contents.
5980 prebuilt_platform_compat_config {
5981 name: "myjar-platform-compat-config",
5982 metadata: "compat-config/metadata.xml",
5983 prefer: true,
5984 }
atrost6e126252020-01-27 17:01:16 +00005985 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00005986 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00005987 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5988 "etc/compatconfig/myjar-platform-compat-config.xml",
5989 "javalib/myjar.jar",
5990 })
5991}
5992
Jiyong Park479321d2019-12-16 11:47:12 +09005993func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5994 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5995 apex {
5996 name: "myapex",
5997 key: "myapex.key",
5998 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005999 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09006000 }
6001
6002 apex_key {
6003 name: "myapex.key",
6004 public_key: "testkey.avbpubkey",
6005 private_key: "testkey.pem",
6006 }
6007
6008 java_library {
6009 name: "myjar",
6010 srcs: ["foo/bar/MyClass.java"],
6011 sdk_version: "none",
6012 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09006013 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006014 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09006015 }
6016 `)
6017}
6018
Jiyong Park7afd1072019-12-30 16:56:33 +09006019func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006020 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09006021 apex {
6022 name: "myapex",
6023 key: "myapex.key",
6024 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006025 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09006026 }
6027
6028 apex_key {
6029 name: "myapex.key",
6030 public_key: "testkey.avbpubkey",
6031 private_key: "testkey.pem",
6032 }
6033
6034 cc_library {
6035 name: "mylib",
6036 srcs: ["mylib.cpp"],
6037 system_shared_libs: [],
6038 stl: "none",
6039 required: ["a", "b"],
6040 host_required: ["c", "d"],
6041 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006042 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006043 }
6044 `)
6045
6046 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006047 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006048 name := apexBundle.BaseModuleName()
6049 prefix := "TARGET_"
6050 var builder strings.Builder
6051 data.Custom(&builder, name, prefix, "", data)
6052 androidMk := builder.String()
6053 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6054 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6055 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6056}
6057
Jiyong Park7cd10e32020-01-14 09:22:18 +09006058func TestSymlinksFromApexToSystem(t *testing.T) {
6059 bp := `
6060 apex {
6061 name: "myapex",
6062 key: "myapex.key",
6063 native_shared_libs: ["mylib"],
6064 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006065 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006066 }
6067
Jiyong Park9d677202020-02-19 16:29:35 +09006068 apex {
6069 name: "myapex.updatable",
6070 key: "myapex.key",
6071 native_shared_libs: ["mylib"],
6072 java_libs: ["myjar"],
6073 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006074 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006075 }
6076
Jiyong Park7cd10e32020-01-14 09:22:18 +09006077 apex_key {
6078 name: "myapex.key",
6079 public_key: "testkey.avbpubkey",
6080 private_key: "testkey.pem",
6081 }
6082
6083 cc_library {
6084 name: "mylib",
6085 srcs: ["mylib.cpp"],
6086 shared_libs: ["myotherlib"],
6087 system_shared_libs: [],
6088 stl: "none",
6089 apex_available: [
6090 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006091 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006092 "//apex_available:platform",
6093 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006094 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006095 }
6096
6097 cc_library {
6098 name: "myotherlib",
6099 srcs: ["mylib.cpp"],
6100 system_shared_libs: [],
6101 stl: "none",
6102 apex_available: [
6103 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006104 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006105 "//apex_available:platform",
6106 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006107 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006108 }
6109
6110 java_library {
6111 name: "myjar",
6112 srcs: ["foo/bar/MyClass.java"],
6113 sdk_version: "none",
6114 system_modules: "none",
6115 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006116 apex_available: [
6117 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006118 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006119 "//apex_available:platform",
6120 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006121 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006122 }
6123
6124 java_library {
6125 name: "myotherjar",
6126 srcs: ["foo/bar/MyClass.java"],
6127 sdk_version: "none",
6128 system_modules: "none",
6129 apex_available: [
6130 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006131 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006132 "//apex_available:platform",
6133 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006134 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006135 }
6136 `
6137
6138 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6139 for _, f := range files {
6140 if f.path == file {
6141 if f.isLink {
6142 t.Errorf("%q is not a real file", file)
6143 }
6144 return
6145 }
6146 }
6147 t.Errorf("%q is not found", file)
6148 }
6149
6150 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6151 for _, f := range files {
6152 if f.path == file {
6153 if !f.isLink {
6154 t.Errorf("%q is not a symlink", file)
6155 }
6156 return
6157 }
6158 }
6159 t.Errorf("%q is not found", file)
6160 }
6161
Jiyong Park9d677202020-02-19 16:29:35 +09006162 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6163 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006164 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006165 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006166 ensureRealfileExists(t, files, "javalib/myjar.jar")
6167 ensureRealfileExists(t, files, "lib64/mylib.so")
6168 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6169
Jiyong Park9d677202020-02-19 16:29:35 +09006170 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6171 ensureRealfileExists(t, files, "javalib/myjar.jar")
6172 ensureRealfileExists(t, files, "lib64/mylib.so")
6173 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6174
6175 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006176 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006177 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006178 ensureRealfileExists(t, files, "javalib/myjar.jar")
6179 ensureRealfileExists(t, files, "lib64/mylib.so")
6180 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006181
6182 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6183 ensureRealfileExists(t, files, "javalib/myjar.jar")
6184 ensureRealfileExists(t, files, "lib64/mylib.so")
6185 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006186}
6187
Yo Chiange8128052020-07-23 20:09:18 +08006188func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006189 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006190 apex {
6191 name: "myapex",
6192 key: "myapex.key",
6193 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006194 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006195 }
6196
6197 apex_key {
6198 name: "myapex.key",
6199 public_key: "testkey.avbpubkey",
6200 private_key: "testkey.pem",
6201 }
6202
6203 cc_library_shared {
6204 name: "mylib",
6205 srcs: ["mylib.cpp"],
6206 shared_libs: ["myotherlib"],
6207 system_shared_libs: [],
6208 stl: "none",
6209 apex_available: [
6210 "myapex",
6211 "//apex_available:platform",
6212 ],
6213 }
6214
6215 cc_prebuilt_library_shared {
6216 name: "myotherlib",
6217 srcs: ["prebuilt.so"],
6218 system_shared_libs: [],
6219 stl: "none",
6220 apex_available: [
6221 "myapex",
6222 "//apex_available:platform",
6223 ],
6224 }
6225 `)
6226
6227 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006228 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006229 var builder strings.Builder
6230 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6231 androidMk := builder.String()
6232 // `myotherlib` is added to `myapex` as symlink
6233 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6234 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6235 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6236 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006237 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 +08006238}
6239
Jooyung Han643adc42020-02-27 13:50:06 +09006240func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006241 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006242 apex {
6243 name: "myapex",
6244 key: "myapex.key",
6245 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006246 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006247 }
6248
6249 apex_key {
6250 name: "myapex.key",
6251 public_key: "testkey.avbpubkey",
6252 private_key: "testkey.pem",
6253 }
6254
6255 cc_library {
6256 name: "mylib",
6257 srcs: ["mylib.cpp"],
6258 shared_libs: ["mylib2"],
6259 system_shared_libs: [],
6260 stl: "none",
6261 apex_available: [ "myapex" ],
6262 }
6263
6264 cc_library {
6265 name: "mylib2",
6266 srcs: ["mylib.cpp"],
6267 system_shared_libs: [],
6268 stl: "none",
6269 apex_available: [ "myapex" ],
6270 }
6271 `)
6272
6273 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6274 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6275 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6276 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6277 "lib64/mylib.so",
6278 "lib64/mylib2.so",
6279 })
6280}
6281
Jooyung Han49f67012020-04-17 13:43:10 +09006282func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006283 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006284 apex {
6285 name: "myapex",
6286 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006287 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006288 }
6289 apex_key {
6290 name: "myapex.key",
6291 public_key: "testkey.avbpubkey",
6292 private_key: "testkey.pem",
6293 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006294 `,
6295 android.FixtureModifyConfig(func(config android.Config) {
6296 delete(config.Targets, android.Android)
6297 config.AndroidCommonTarget = android.Target{}
6298 }),
6299 )
Jooyung Han49f67012020-04-17 13:43:10 +09006300
6301 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6302 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6303 }
6304}
6305
Jiyong Parkbd159612020-02-28 15:22:21 +09006306func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006307 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006308 apex {
6309 name: "myapex",
6310 key: "myapex.key",
6311 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006312 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006313 }
6314
6315 apex_key {
6316 name: "myapex.key",
6317 public_key: "testkey.avbpubkey",
6318 private_key: "testkey.pem",
6319 }
6320
6321 android_app {
6322 name: "AppFoo",
6323 srcs: ["foo/bar/MyClass.java"],
6324 sdk_version: "none",
6325 system_modules: "none",
6326 apex_available: [ "myapex" ],
6327 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006328 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006329
Colin Crosscf371cc2020-11-13 11:48:42 -08006330 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006331 content := bundleConfigRule.Args["content"]
6332
6333 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006334 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 +09006335}
6336
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006337func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006338 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006339 apex {
6340 name: "myapex",
6341 key: "myapex.key",
6342 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006343 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006344 }
6345
6346 apex_key {
6347 name: "myapex.key",
6348 public_key: "testkey.avbpubkey",
6349 private_key: "testkey.pem",
6350 }
6351
6352 android_app_set {
6353 name: "AppSet",
6354 set: "AppSet.apks",
6355 }`)
6356 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006357 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006358 content := bundleConfigRule.Args["content"]
6359 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6360 s := mod.Rule("apexRule").Args["copy_commands"]
6361 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6362 if len(copyCmds) != 3 {
6363 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6364 }
6365 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6366 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6367 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6368}
6369
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006370func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006371 ctx := testApex(t, "", android.FixtureModifyMockFS(func(fs android.MockFS) {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006372 bp := `
6373 apex_set {
6374 name: "myapex",
6375 filename: "foo_v2.apex",
6376 sanitized: {
6377 none: { set: "myapex.apks", },
6378 hwaddress: { set: "myapex.hwasan.apks", },
6379 },
6380 }`
6381 fs["Android.bp"] = []byte(bp)
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006382 }),
6383 prepareForTestWithSantitizeHwaddress,
6384 )
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006385
6386 m := ctx.ModuleForTests("myapex", "android_common")
Paul Duffin37ba3442021-03-29 00:21:08 +01006387 extractedApex := m.Output("out/soong/.intermediates/myapex/android_common/foo_v2.apex")
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006388
6389 actual := extractedApex.Inputs
6390 if len(actual) != 1 {
6391 t.Errorf("expected a single input")
6392 }
6393
6394 expected := "myapex.hwasan.apks"
6395 if actual[0].String() != expected {
6396 t.Errorf("expected %s, got %s", expected, actual[0].String())
6397 }
6398}
6399
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006400func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006401 t.Helper()
6402
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006403 bp := `
6404 java_library {
6405 name: "some-updatable-apex-lib",
6406 srcs: ["a.java"],
6407 sdk_version: "current",
6408 apex_available: [
6409 "some-updatable-apex",
6410 ],
6411 }
6412
6413 java_library {
6414 name: "some-non-updatable-apex-lib",
6415 srcs: ["a.java"],
6416 apex_available: [
6417 "some-non-updatable-apex",
6418 ],
6419 }
6420
6421 java_library {
6422 name: "some-platform-lib",
6423 srcs: ["a.java"],
6424 sdk_version: "current",
6425 installable: true,
6426 }
6427
6428 java_library {
6429 name: "some-art-lib",
6430 srcs: ["a.java"],
6431 sdk_version: "current",
6432 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006433 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006434 ],
6435 hostdex: true,
6436 }
6437
6438 apex {
6439 name: "some-updatable-apex",
6440 key: "some-updatable-apex.key",
6441 java_libs: ["some-updatable-apex-lib"],
6442 updatable: true,
6443 min_sdk_version: "current",
6444 }
6445
6446 apex {
6447 name: "some-non-updatable-apex",
6448 key: "some-non-updatable-apex.key",
6449 java_libs: ["some-non-updatable-apex-lib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006450 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006451 }
6452
6453 apex_key {
6454 name: "some-updatable-apex.key",
6455 }
6456
6457 apex_key {
6458 name: "some-non-updatable-apex.key",
6459 }
6460
6461 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006462 name: "com.android.art.debug",
6463 key: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006464 java_libs: ["some-art-lib"],
6465 updatable: true,
6466 min_sdk_version: "current",
6467 }
6468
6469 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006470 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006471 }
6472
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006473 filegroup {
6474 name: "some-updatable-apex-file_contexts",
6475 srcs: [
6476 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6477 ],
6478 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006479
6480 filegroup {
6481 name: "some-non-updatable-apex-file_contexts",
6482 srcs: [
6483 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6484 ],
6485 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006486 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006487
6488 testDexpreoptWithApexes(t, bp, errmsg, transformDexpreoptConfig)
6489}
6490
Paul Duffin064b70c2020-11-02 17:32:38 +00006491func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006492 t.Helper()
6493
Paul Duffin55607122021-03-30 23:32:51 +01006494 fs := android.MockFS{
6495 "a.java": nil,
6496 "a.jar": nil,
6497 "apex_manifest.json": nil,
6498 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006499 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006500 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6501 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6502 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006503 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006504 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006505
Paul Duffin55607122021-03-30 23:32:51 +01006506 errorHandler := android.FixtureExpectsNoErrors
6507 if errmsg != "" {
6508 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006509 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006510
Paul Duffin55607122021-03-30 23:32:51 +01006511 result := android.GroupFixturePreparers(
6512 cc.PrepareForTestWithCcDefaultModules,
6513 java.PrepareForTestWithHiddenApiBuildComponents,
6514 java.PrepareForTestWithJavaDefaultModules,
6515 java.PrepareForTestWithJavaSdkLibraryFiles,
6516 PrepareForTestWithApexBuildComponents,
6517 android.FixtureModifyConfig(func(config android.Config) {
6518 pathCtx := android.PathContextForTesting(config)
6519 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
6520 transformDexpreoptConfig(dexpreoptConfig)
6521 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
6522
6523 // Make sure that any changes to these dexpreopt properties are mirrored in the corresponding
6524 // product variables.
6525 config.TestProductVariables.BootJars = dexpreoptConfig.BootJars
6526 config.TestProductVariables.UpdatableBootJars = dexpreoptConfig.UpdatableBootJars
6527 }),
6528 fs.AddToFixture(),
6529 ).
6530 ExtendWithErrorHandler(errorHandler).
6531 RunTestWithBp(t, bp)
6532
6533 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006534}
6535
Jooyung Han548640b2020-04-27 12:10:30 +09006536func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6537 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6538 apex {
6539 name: "myapex",
6540 key: "myapex.key",
6541 updatable: true,
6542 }
6543
6544 apex_key {
6545 name: "myapex.key",
6546 public_key: "testkey.avbpubkey",
6547 private_key: "testkey.pem",
6548 }
6549 `)
6550}
6551
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006552func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6553 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6554 apex {
6555 name: "myapex",
6556 key: "myapex.key",
6557 }
6558
6559 apex_key {
6560 name: "myapex.key",
6561 public_key: "testkey.avbpubkey",
6562 private_key: "testkey.pem",
6563 }
6564 `)
6565}
6566
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006567func TestNoUpdatableJarsInBootImage(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006568 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006569 var transform func(*dexpreopt.GlobalConfig)
6570
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006571 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
6572 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006573 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006574 }
6575 testNoUpdatableJarsInBootImage(t, "", transform)
6576 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006577
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006578 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffind376f792021-01-26 11:59:35 +00006579 err = `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006580 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006581 config.BootJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006582 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006583 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006584 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006585
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006586 t.Run("updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006587 err = `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006588 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006589 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006590 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006591 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006592 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006593
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006594 t.Run("non-updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006595 err = `module "some-non-updatable-apex-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006596 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006597 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006598 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006599 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006600 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006601
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006602 t.Run("updatable jar from some other apex in the framework boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006603 err = `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the framework boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006604 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006605 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006606 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006607 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006608 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006609
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006610 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
6611 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006612 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006613 }
6614 testNoUpdatableJarsInBootImage(t, "", transform)
6615 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006616
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006617 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006618 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006619 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006620 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006621 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006622 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006623 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006624
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006625 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006626 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006627 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006628 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006629 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006630 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006631 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006632
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006633 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006634 err = `module "some-platform-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006635 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006636 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006637 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006638 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006639 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006640
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006641 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
6642 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006643 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006644 }
6645 testNoUpdatableJarsInBootImage(t, "", transform)
6646 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006647
6648}
6649
6650func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
6651 transform := func(config *dexpreopt.GlobalConfig) {
6652 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
6653 }
6654 t.Run("prebuilt no source", func(t *testing.T) {
6655 testDexpreoptWithApexes(t, `
6656 prebuilt_apex {
6657 name: "myapex" ,
6658 arch: {
6659 arm64: {
6660 src: "myapex-arm64.apex",
6661 },
6662 arm: {
6663 src: "myapex-arm.apex",
6664 },
6665 },
6666 exported_java_libs: ["libfoo"],
6667 }
6668
6669 java_import {
6670 name: "libfoo",
6671 jars: ["libfoo.jar"],
6672 }
6673`, "", transform)
6674 })
6675
6676 t.Run("prebuilt no source", func(t *testing.T) {
6677 testDexpreoptWithApexes(t, `
6678 prebuilt_apex {
6679 name: "myapex" ,
6680 arch: {
6681 arm64: {
6682 src: "myapex-arm64.apex",
6683 },
6684 arm: {
6685 src: "myapex-arm.apex",
6686 },
6687 },
6688 exported_java_libs: ["libfoo"],
6689 }
6690
6691 java_import {
6692 name: "libfoo",
6693 jars: ["libfoo.jar"],
6694 }
6695`, "", transform)
6696 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006697}
6698
Andrei Onea115e7e72020-06-05 21:14:03 +01006699func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6700 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006701 bp += `
6702 apex_key {
6703 name: "myapex.key",
6704 public_key: "testkey.avbpubkey",
6705 private_key: "testkey.pem",
6706 }`
Paul Duffin45338f02021-03-30 23:07:52 +01006707 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01006708 "lib1/src/A.java": nil,
6709 "lib2/src/B.java": nil,
6710 "system/sepolicy/apex/myapex-file_contexts": nil,
6711 }
6712
Paul Duffin45338f02021-03-30 23:07:52 +01006713 errorHandler := android.FixtureExpectsNoErrors
6714 if errmsg != "" {
6715 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07006716 }
Colin Crossae8600b2020-10-29 17:09:13 -07006717
Paul Duffin45338f02021-03-30 23:07:52 +01006718 android.GroupFixturePreparers(
6719 android.PrepareForTestWithAndroidBuildComponents,
6720 java.PrepareForTestWithJavaBuildComponents,
6721 PrepareForTestWithApexBuildComponents,
6722 android.PrepareForTestWithNeverallowRules(rules),
6723 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6724 updatableBootJars := make([]string, 0, len(apexBootJars))
6725 for _, apexBootJar := range apexBootJars {
6726 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6727 }
6728 variables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6729 }),
6730 fs.AddToFixture(),
6731 ).
6732 ExtendWithErrorHandler(errorHandler).
6733 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01006734}
6735
6736func TestApexPermittedPackagesRules(t *testing.T) {
6737 testcases := []struct {
6738 name string
6739 expectedError string
6740 bp string
6741 bootJars []string
6742 modulesPackages map[string][]string
6743 }{
6744
6745 {
6746 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6747 expectedError: "",
6748 bp: `
6749 java_library {
6750 name: "bcp_lib1",
6751 srcs: ["lib1/src/*.java"],
6752 permitted_packages: ["foo.bar"],
6753 apex_available: ["myapex"],
6754 sdk_version: "none",
6755 system_modules: "none",
6756 }
6757 java_library {
6758 name: "nonbcp_lib2",
6759 srcs: ["lib2/src/*.java"],
6760 apex_available: ["myapex"],
6761 permitted_packages: ["a.b"],
6762 sdk_version: "none",
6763 system_modules: "none",
6764 }
6765 apex {
6766 name: "myapex",
6767 key: "myapex.key",
6768 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006769 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006770 }`,
6771 bootJars: []string{"bcp_lib1"},
6772 modulesPackages: map[string][]string{
6773 "myapex": []string{
6774 "foo.bar",
6775 },
6776 },
6777 },
6778 {
6779 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6780 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.`,
6781 bp: `
6782 java_library {
6783 name: "bcp_lib1",
6784 srcs: ["lib1/src/*.java"],
6785 apex_available: ["myapex"],
6786 permitted_packages: ["foo.bar"],
6787 sdk_version: "none",
6788 system_modules: "none",
6789 }
6790 java_library {
6791 name: "bcp_lib2",
6792 srcs: ["lib2/src/*.java"],
6793 apex_available: ["myapex"],
6794 permitted_packages: ["foo.bar", "bar.baz"],
6795 sdk_version: "none",
6796 system_modules: "none",
6797 }
6798 apex {
6799 name: "myapex",
6800 key: "myapex.key",
6801 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006802 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006803 }
6804 `,
6805 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6806 modulesPackages: map[string][]string{
6807 "myapex": []string{
6808 "foo.bar",
6809 },
6810 },
6811 },
6812 }
6813 for _, tc := range testcases {
6814 t.Run(tc.name, func(t *testing.T) {
6815 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6816 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6817 })
6818 }
6819}
6820
Jiyong Park62304bb2020-04-13 16:19:48 +09006821func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006822 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09006823 apex {
6824 name: "myapex",
6825 key: "myapex.key",
6826 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006827 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09006828 }
6829
6830 apex_key {
6831 name: "myapex.key",
6832 public_key: "testkey.avbpubkey",
6833 private_key: "testkey.pem",
6834 }
6835
6836 cc_library {
6837 name: "mylib",
6838 srcs: ["mylib.cpp"],
6839 system_shared_libs: [],
6840 stl: "none",
6841 stubs: {
6842 versions: ["1"],
6843 },
6844 apex_available: ["myapex"],
6845 }
6846
6847 cc_library {
6848 name: "myprivlib",
6849 srcs: ["mylib.cpp"],
6850 system_shared_libs: [],
6851 stl: "none",
6852 apex_available: ["myapex"],
6853 }
6854
6855
6856 cc_test {
6857 name: "mytest",
6858 gtest: false,
6859 srcs: ["mylib.cpp"],
6860 system_shared_libs: [],
6861 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006862 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006863 test_for: ["myapex"]
6864 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006865
6866 cc_library {
6867 name: "mytestlib",
6868 srcs: ["mylib.cpp"],
6869 system_shared_libs: [],
6870 shared_libs: ["mylib", "myprivlib"],
6871 stl: "none",
6872 test_for: ["myapex"],
6873 }
6874
6875 cc_benchmark {
6876 name: "mybench",
6877 srcs: ["mylib.cpp"],
6878 system_shared_libs: [],
6879 shared_libs: ["mylib", "myprivlib"],
6880 stl: "none",
6881 test_for: ["myapex"],
6882 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006883 `)
6884
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006885 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01006886 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006887 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6888 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6889 }
6890
6891 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09006892 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006893 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6894 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6895 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6896}
Jiyong Park46a512f2020-12-04 18:02:13 +09006897
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006898func TestIndirectTestFor(t *testing.T) {
6899 ctx := testApex(t, `
6900 apex {
6901 name: "myapex",
6902 key: "myapex.key",
6903 native_shared_libs: ["mylib", "myprivlib"],
6904 updatable: false,
6905 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006906
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006907 apex_key {
6908 name: "myapex.key",
6909 public_key: "testkey.avbpubkey",
6910 private_key: "testkey.pem",
6911 }
6912
6913 cc_library {
6914 name: "mylib",
6915 srcs: ["mylib.cpp"],
6916 system_shared_libs: [],
6917 stl: "none",
6918 stubs: {
6919 versions: ["1"],
6920 },
6921 apex_available: ["myapex"],
6922 }
6923
6924 cc_library {
6925 name: "myprivlib",
6926 srcs: ["mylib.cpp"],
6927 system_shared_libs: [],
6928 stl: "none",
6929 shared_libs: ["mylib"],
6930 apex_available: ["myapex"],
6931 }
6932
6933 cc_library {
6934 name: "mytestlib",
6935 srcs: ["mylib.cpp"],
6936 system_shared_libs: [],
6937 shared_libs: ["myprivlib"],
6938 stl: "none",
6939 test_for: ["myapex"],
6940 }
6941 `)
6942
6943 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01006944 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006945 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6946 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6947 }
6948
6949 // The platform variant of mytestlib links to the platform variant of the
6950 // internal myprivlib.
6951 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
6952
6953 // The platform variant of myprivlib links to the platform variant of mylib
6954 // and bypasses its stubs.
6955 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 +09006956}
6957
Martin Stjernholmec009002021-03-27 15:18:31 +00006958func TestTestForForLibInOtherApex(t *testing.T) {
6959 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
6960 _ = testApex(t, `
6961 apex {
6962 name: "com.android.art",
6963 key: "myapex.key",
6964 native_shared_libs: ["mylib"],
6965 updatable: false,
6966 }
6967
6968 apex {
6969 name: "com.android.art.debug",
6970 key: "myapex.key",
6971 native_shared_libs: ["mylib", "mytestlib"],
6972 updatable: false,
6973 }
6974
6975 apex_key {
6976 name: "myapex.key",
6977 public_key: "testkey.avbpubkey",
6978 private_key: "testkey.pem",
6979 }
6980
6981 cc_library {
6982 name: "mylib",
6983 srcs: ["mylib.cpp"],
6984 system_shared_libs: [],
6985 stl: "none",
6986 stubs: {
6987 versions: ["1"],
6988 },
6989 apex_available: ["com.android.art", "com.android.art.debug"],
6990 }
6991
6992 cc_library {
6993 name: "mytestlib",
6994 srcs: ["mylib.cpp"],
6995 system_shared_libs: [],
6996 shared_libs: ["mylib"],
6997 stl: "none",
6998 apex_available: ["com.android.art.debug"],
6999 test_for: ["com.android.art"],
7000 }
7001 `,
7002 android.MockFS{
7003 "system/sepolicy/apex/com.android.art-file_contexts": nil,
7004 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
7005 }.AddToFixture())
7006}
7007
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007008// TODO(jungjw): Move this to proptools
7009func intPtr(i int) *int {
7010 return &i
7011}
7012
7013func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007014 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007015 apex_set {
7016 name: "myapex",
7017 set: "myapex.apks",
7018 filename: "foo_v2.apex",
7019 overrides: ["foo"],
7020 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007021 `,
7022 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7023 variables.Platform_sdk_version = intPtr(30)
7024 }),
7025 android.FixtureModifyConfig(func(config android.Config) {
7026 config.Targets[android.Android] = []android.Target{
7027 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
7028 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
7029 }
7030 }),
7031 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007032
7033 m := ctx.ModuleForTests("myapex", "android_common")
7034
7035 // Check extract_apks tool parameters.
Paul Duffin37ba3442021-03-29 00:21:08 +01007036 extractedApex := m.Output("out/soong/.intermediates/myapex/android_common/foo_v2.apex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007037 actual := extractedApex.Args["abis"]
7038 expected := "ARMEABI_V7A,ARM64_V8A"
7039 if actual != expected {
7040 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7041 }
7042 actual = extractedApex.Args["sdk-version"]
7043 expected = "30"
7044 if actual != expected {
7045 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7046 }
7047
7048 a := m.Module().(*ApexSet)
7049 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07007050 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007051 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
7052 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
7053 }
7054}
7055
Jiyong Park7d95a512020-05-10 15:16:24 +09007056func TestNoStaticLinkingToStubsLib(t *testing.T) {
7057 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
7058 apex {
7059 name: "myapex",
7060 key: "myapex.key",
7061 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007062 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09007063 }
7064
7065 apex_key {
7066 name: "myapex.key",
7067 public_key: "testkey.avbpubkey",
7068 private_key: "testkey.pem",
7069 }
7070
7071 cc_library {
7072 name: "mylib",
7073 srcs: ["mylib.cpp"],
7074 static_libs: ["otherlib"],
7075 system_shared_libs: [],
7076 stl: "none",
7077 apex_available: [ "myapex" ],
7078 }
7079
7080 cc_library {
7081 name: "otherlib",
7082 srcs: ["mylib.cpp"],
7083 system_shared_libs: [],
7084 stl: "none",
7085 stubs: {
7086 versions: ["1", "2", "3"],
7087 },
7088 apex_available: [ "myapex" ],
7089 }
7090 `)
7091}
7092
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007093func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007094 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007095 apex {
7096 name: "myapex",
7097 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007098 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007099 }
7100
7101 apex_key {
7102 name: "myapex.key",
7103 public_key: "testkey.avbpubkey",
7104 private_key: "testkey.pem",
7105 }
7106
7107 prebuilt_apex {
7108 name: "myapex",
7109 prefer: true,
7110 arch: {
7111 arm64: {
7112 src: "myapex-arm64.apex",
7113 },
7114 arm: {
7115 src: "myapex-arm.apex",
7116 },
7117 },
7118 }
7119
7120 apex_set {
7121 name: "myapex_set",
7122 set: "myapex.apks",
7123 filename: "myapex_set.apex",
7124 overrides: ["myapex"],
7125 }
7126 `)
7127
7128 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7129 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7130 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 +09007131 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 +09007132}
7133
Jooyung Han938b5932020-06-20 12:47:47 +09007134func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007135 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007136 apex {
7137 name: "myapex",
7138 key: "myapex.key",
7139 apps: ["app"],
7140 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007141 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007142 }
7143
7144 apex_key {
7145 name: "myapex.key",
7146 public_key: "testkey.avbpubkey",
7147 private_key: "testkey.pem",
7148 }
7149
7150 android_app {
7151 name: "app",
7152 srcs: ["foo/bar/MyClass.java"],
7153 package_name: "foo",
7154 sdk_version: "none",
7155 system_modules: "none",
7156 apex_available: [ "myapex" ],
7157 }
7158 `, withFiles(map[string][]byte{
7159 "sub/Android.bp": []byte(`
7160 override_apex {
7161 name: "override_myapex",
7162 base: "myapex",
7163 apps: ["override_app"],
7164 allowed_files: ":allowed",
7165 }
7166 // Overridable "path" property should be referenced indirectly
7167 filegroup {
7168 name: "allowed",
7169 srcs: ["allowed.txt"],
7170 }
7171 override_android_app {
7172 name: "override_app",
7173 base: "app",
7174 package_name: "bar",
7175 }
7176 `),
7177 }))
7178
7179 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7180 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7181 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7182 }
7183
7184 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7185 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7186 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7187 }
7188}
7189
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007190func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007191 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007192 apex {
7193 name: "myapex",
7194 key: "myapex.key",
7195 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007196 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007197 }
7198
7199 apex_key {
7200 name: "myapex.key",
7201 public_key: "testkey.avbpubkey",
7202 private_key: "testkey.pem",
7203 }
7204
7205 cc_library {
7206 name: "mylib",
7207 srcs: ["mylib.cpp"],
7208 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007209 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007210 },
7211 apex_available: ["myapex"],
7212 }
7213
7214 cc_prebuilt_library_shared {
7215 name: "mylib",
7216 prefer: false,
7217 srcs: ["prebuilt.so"],
7218 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007219 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007220 },
7221 apex_available: ["myapex"],
7222 }
7223 `)
7224}
7225
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007226func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007227 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007228 apex {
7229 name: "myapex",
7230 key: "myapex.key",
7231 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007232 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007233 }
7234 apex_key {
7235 name: "myapex.key",
7236 public_key: "testkey.avbpubkey",
7237 private_key: "testkey.pem",
7238 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007239 `,
7240 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7241 variables.CompressedApex = proptools.BoolPtr(true)
7242 }),
7243 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007244
7245 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7246 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7247
7248 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7249 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7250
7251 // Make sure output of bundle is .capex
7252 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7253 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7254
7255 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007256 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007257 var builder strings.Builder
7258 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7259 androidMk := builder.String()
7260 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7261}
7262
Martin Stjernholm2856c662020-12-02 15:03:42 +00007263func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007264 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007265 apex {
7266 name: "myapex",
7267 key: "myapex.key",
7268 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007269 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007270 }
7271
7272 apex_key {
7273 name: "myapex.key",
7274 public_key: "testkey.avbpubkey",
7275 private_key: "testkey.pem",
7276 }
7277
7278 cc_library {
7279 name: "mylib",
7280 srcs: ["mylib.cpp"],
7281 apex_available: ["myapex"],
7282 shared_libs: ["otherlib"],
7283 system_shared_libs: [],
7284 }
7285
7286 cc_library {
7287 name: "otherlib",
7288 srcs: ["mylib.cpp"],
7289 stubs: {
7290 versions: ["current"],
7291 },
7292 }
7293
7294 cc_prebuilt_library_shared {
7295 name: "otherlib",
7296 prefer: true,
7297 srcs: ["prebuilt.so"],
7298 stubs: {
7299 versions: ["current"],
7300 },
7301 }
7302 `)
7303
7304 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007305 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007306 var builder strings.Builder
7307 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7308 androidMk := builder.String()
7309
7310 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7311 // a thing there.
7312 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7313}
7314
Jiyong Parke3867542020-12-03 17:28:25 +09007315func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007316 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007317 apex {
7318 name: "myapex",
7319 key: "myapex.key",
7320 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007321 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007322 }
7323
7324 apex_key {
7325 name: "myapex.key",
7326 public_key: "testkey.avbpubkey",
7327 private_key: "testkey.pem",
7328 }
7329
7330 cc_library {
7331 name: "mylib",
7332 srcs: ["mylib.cpp"],
7333 system_shared_libs: [],
7334 stl: "none",
7335 apex_available: ["myapex"],
7336 shared_libs: ["mylib2"],
7337 target: {
7338 apex: {
7339 exclude_shared_libs: ["mylib2"],
7340 },
7341 },
7342 }
7343
7344 cc_library {
7345 name: "mylib2",
7346 srcs: ["mylib.cpp"],
7347 system_shared_libs: [],
7348 stl: "none",
7349 }
7350 `)
7351
7352 // Check if mylib is linked to mylib2 for the non-apex target
7353 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7354 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7355
7356 // Make sure that the link doesn't occur for the apex target
7357 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7358 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7359
7360 // It shouldn't appear in the copy cmd as well.
7361 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7362 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7363}
7364
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007365func TestPrebuiltStubLibDep(t *testing.T) {
7366 bpBase := `
7367 apex {
7368 name: "myapex",
7369 key: "myapex.key",
7370 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007371 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007372 }
7373 apex_key {
7374 name: "myapex.key",
7375 public_key: "testkey.avbpubkey",
7376 private_key: "testkey.pem",
7377 }
7378 cc_library {
7379 name: "mylib",
7380 srcs: ["mylib.cpp"],
7381 apex_available: ["myapex"],
7382 shared_libs: ["stublib"],
7383 system_shared_libs: [],
7384 }
7385 apex {
7386 name: "otherapex",
7387 enabled: %s,
7388 key: "myapex.key",
7389 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007390 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007391 }
7392 `
7393
7394 stublibSourceBp := `
7395 cc_library {
7396 name: "stublib",
7397 srcs: ["mylib.cpp"],
7398 apex_available: ["otherapex"],
7399 system_shared_libs: [],
7400 stl: "none",
7401 stubs: {
7402 versions: ["1"],
7403 },
7404 }
7405 `
7406
7407 stublibPrebuiltBp := `
7408 cc_prebuilt_library_shared {
7409 name: "stublib",
7410 srcs: ["prebuilt.so"],
7411 apex_available: ["otherapex"],
7412 stubs: {
7413 versions: ["1"],
7414 },
7415 %s
7416 }
7417 `
7418
7419 tests := []struct {
7420 name string
7421 stublibBp string
7422 usePrebuilt bool
7423 modNames []string // Modules to collect AndroidMkEntries for
7424 otherApexEnabled []string
7425 }{
7426 {
7427 name: "only_source",
7428 stublibBp: stublibSourceBp,
7429 usePrebuilt: false,
7430 modNames: []string{"stublib"},
7431 otherApexEnabled: []string{"true", "false"},
7432 },
7433 {
7434 name: "source_preferred",
7435 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7436 usePrebuilt: false,
7437 modNames: []string{"stublib", "prebuilt_stublib"},
7438 otherApexEnabled: []string{"true", "false"},
7439 },
7440 {
7441 name: "prebuilt_preferred",
7442 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7443 usePrebuilt: true,
7444 modNames: []string{"stublib", "prebuilt_stublib"},
7445 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7446 },
7447 {
7448 name: "only_prebuilt",
7449 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7450 usePrebuilt: true,
7451 modNames: []string{"stublib"},
7452 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7453 },
7454 }
7455
7456 for _, test := range tests {
7457 t.Run(test.name, func(t *testing.T) {
7458 for _, otherApexEnabled := range test.otherApexEnabled {
7459 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007460 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007461
7462 type modAndMkEntries struct {
7463 mod *cc.Module
7464 mkEntries android.AndroidMkEntries
7465 }
7466 entries := []*modAndMkEntries{}
7467
7468 // Gather shared lib modules that are installable
7469 for _, modName := range test.modNames {
7470 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7471 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7472 continue
7473 }
7474 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007475 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007476 continue
7477 }
Colin Crossaa255532020-07-03 13:18:24 -07007478 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007479 if ent.Disabled {
7480 continue
7481 }
7482 entries = append(entries, &modAndMkEntries{
7483 mod: mod,
7484 mkEntries: ent,
7485 })
7486 }
7487 }
7488 }
7489
7490 var entry *modAndMkEntries = nil
7491 for _, ent := range entries {
7492 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7493 if entry != nil {
7494 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7495 } else {
7496 entry = ent
7497 }
7498 }
7499 }
7500
7501 if entry == nil {
7502 t.Errorf("AndroidMk entry for \"stublib\" missing")
7503 } else {
7504 isPrebuilt := entry.mod.Prebuilt() != nil
7505 if isPrebuilt != test.usePrebuilt {
7506 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7507 }
7508 if !entry.mod.IsStubs() {
7509 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7510 }
7511 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7512 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7513 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007514 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09007515 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09007516 if !android.InList(expected, cflags) {
7517 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7518 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007519 }
7520 })
7521 }
7522 })
7523 }
7524}
7525
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007526func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01007527 os.Exit(m.Run())
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007528}