blob: 977a9544ceba88708f3584a236856021116dace4 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +090018 "fmt"
Jiyong Park25fc6a92018-11-18 18:02:45 +090019 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Paul Duffin37856732021-02-26 14:24:15 +000021 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070022 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010023 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090024 "sort"
Jiyong Parkd4a3a132021-03-17 20:21:35 +090025 "strconv"
Jiyong Park25fc6a92018-11-18 18:02:45 +090026 "strings"
27 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090028
29 "github.com/google/blueprint/proptools"
30
31 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080032 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090033 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000034 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090036 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090037 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090039)
40
Jooyung Hand3639552019-08-09 12:57:43 +090041// names returns name list from white space separated string
42func names(s string) (ns []string) {
43 for _, n := range strings.Split(s, " ") {
44 if len(n) > 0 {
45 ns = append(ns, n)
46 }
47 }
48 return
49}
50
Paul Duffin40b62572021-03-20 11:39:01 +000051func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090052 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010053 android.GroupFixturePreparers(
54 prepareForApexTest,
55 android.GroupFixturePreparers(preparers...),
56 ).
Paul Duffine05480a2021-03-08 15:07:14 +000057 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000058 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090059}
60
Paul Duffin40b62572021-03-20 11:39:01 +000061func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090062 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010063
64 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000065 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010066 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000067 }
Paul Duffin284165a2021-03-29 01:50:31 +010068
69 result := android.GroupFixturePreparers(
70 prepareForApexTest,
71 android.GroupFixturePreparers(preparers...),
72 optionalBpPreparer,
73 ).RunTest(t)
74
Paul Duffine05480a2021-03-08 15:07:14 +000075 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090076}
77
Paul Duffin810f33d2021-03-09 14:12:32 +000078func withFiles(files android.MockFS) android.FixturePreparer {
79 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090080}
81
Paul Duffin810f33d2021-03-09 14:12:32 +000082func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
83 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090084 for k, v := range targets {
85 config.Targets[k] = v
86 }
Paul Duffin810f33d2021-03-09 14:12:32 +000087 })
Jooyung Han344d5432019-08-23 11:17:39 +090088}
89
Jooyung Han35155c42020-02-06 17:33:20 +090090// withNativeBridgeTargets sets configuration with targets including:
91// - X86_64 (primary)
92// - X86 (secondary)
93// - Arm64 on X86_64 (native bridge)
94// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000095var withNativeBridgeEnabled = android.FixtureModifyConfig(
96 func(config android.Config) {
97 config.Targets[android.Android] = []android.Target{
98 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
99 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
100 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
101 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
102 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
103 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
104 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
105 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
106 }
107 },
108)
109
110func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
111 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
112 variables.ManifestPackageNameOverrides = specs
113 })
Jooyung Han35155c42020-02-06 17:33:20 +0900114}
115
Paul Duffin810f33d2021-03-09 14:12:32 +0000116var withBinder32bit = android.FixtureModifyProductVariables(
117 func(variables android.FixtureProductVariables) {
118 variables.Binder32bit = proptools.BoolPtr(true)
119 },
120)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900121
Paul Duffin810f33d2021-03-09 14:12:32 +0000122var withUnbundledBuild = android.FixtureModifyProductVariables(
123 func(variables android.FixtureProductVariables) {
124 variables.Unbundled_build = proptools.BoolPtr(true)
125 },
126)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900127
Paul Duffin284165a2021-03-29 01:50:31 +0100128// Legacy preparer used for running tests within the apex package.
129//
130// This includes everything that was needed to run any test in the apex package prior to the
131// introduction of the test fixtures. Tests that are being converted to use fixtures directly
132// rather than through the testApex...() methods should avoid using this and instead use the
133// various preparers directly, using android.GroupFixturePreparers(...) to group them when
134// necessary.
135//
136// deprecated
137var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000138 // General preparers in alphabetical order as test infrastructure will enforce correct
139 // registration order.
140 android.PrepareForTestWithAndroidBuildComponents,
141 bpf.PrepareForTestWithBpf,
142 cc.PrepareForTestWithCcBuildComponents,
143 java.PrepareForTestWithJavaDefaultModules,
144 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
145 rust.PrepareForTestWithRustDefaultModules,
146 sh.PrepareForTestWithShBuildComponents,
147
148 PrepareForTestWithApexBuildComponents,
149
150 // Additional apex test specific preparers.
151 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
152 filegroup {
153 name: "myapex-file_contexts",
154 srcs: [
155 "apex/myapex-file_contexts",
156 ],
157 }
158 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000159 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000160 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000161 "a.java": nil,
162 "PrebuiltAppFoo.apk": nil,
163 "PrebuiltAppFooPriv.apk": nil,
164 "apex_manifest.json": nil,
165 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000166 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
167 "system/sepolicy/apex/myapex2-file_contexts": nil,
168 "system/sepolicy/apex/otherapex-file_contexts": nil,
169 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
170 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
171 "mylib.cpp": nil,
172 "mytest.cpp": nil,
173 "mytest1.cpp": nil,
174 "mytest2.cpp": nil,
175 "mytest3.cpp": nil,
176 "myprebuilt": nil,
177 "my_include": nil,
178 "foo/bar/MyClass.java": nil,
179 "prebuilt.jar": nil,
180 "prebuilt.so": nil,
181 "vendor/foo/devkeys/test.x509.pem": nil,
182 "vendor/foo/devkeys/test.pk8": nil,
183 "testkey.x509.pem": nil,
184 "testkey.pk8": nil,
185 "testkey.override.x509.pem": nil,
186 "testkey.override.pk8": nil,
187 "vendor/foo/devkeys/testkey.avbpubkey": nil,
188 "vendor/foo/devkeys/testkey.pem": nil,
189 "NOTICE": nil,
190 "custom_notice": nil,
191 "custom_notice_for_static_lib": nil,
192 "testkey2.avbpubkey": nil,
193 "testkey2.pem": nil,
194 "myapex-arm64.apex": nil,
195 "myapex-arm.apex": nil,
196 "myapex.apks": nil,
197 "frameworks/base/api/current.txt": nil,
198 "framework/aidl/a.aidl": nil,
199 "build/make/core/proguard.flags": nil,
200 "build/make/core/proguard_basic_keeps.flags": nil,
201 "dummy.txt": nil,
202 "baz": nil,
203 "bar/baz": nil,
204 "testdata/baz": nil,
205 "AppSet.apks": nil,
206 "foo.rs": nil,
207 "libfoo.jar": nil,
208 "libbar.jar": nil,
209 },
210 ),
211
212 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
213 variables.DeviceVndkVersion = proptools.StringPtr("current")
214 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
215 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
216 variables.Platform_sdk_codename = proptools.StringPtr("Q")
217 variables.Platform_sdk_final = proptools.BoolPtr(false)
218 variables.Platform_version_active_codenames = []string{"Q"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900219 variables.Platform_vndk_version = proptools.StringPtr("29")
Paul Duffin37aad602021-03-08 09:47:16 +0000220 }),
221)
222
Paul Duffin52bfaa42021-03-23 23:40:12 +0000223var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
224 "system/sepolicy/apex/myapex-file_contexts": nil,
225})
226
Jooyung Han643adc42020-02-27 13:50:06 +0900227// ensure that 'result' equals 'expected'
228func ensureEquals(t *testing.T, result string, expected string) {
229 t.Helper()
230 if result != expected {
231 t.Errorf("%q != %q", expected, result)
232 }
233}
234
Jiyong Park25fc6a92018-11-18 18:02:45 +0900235// ensure that 'result' contains 'expected'
236func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900237 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900238 if !strings.Contains(result, expected) {
239 t.Errorf("%q is not found in %q", expected, result)
240 }
241}
242
Liz Kammer5bd365f2020-05-27 15:15:11 -0700243// ensure that 'result' contains 'expected' exactly one time
244func ensureContainsOnce(t *testing.T, result string, expected string) {
245 t.Helper()
246 count := strings.Count(result, expected)
247 if count != 1 {
248 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
249 }
250}
251
Jiyong Park25fc6a92018-11-18 18:02:45 +0900252// ensures that 'result' does not contain 'notExpected'
253func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900254 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900255 if strings.Contains(result, notExpected) {
256 t.Errorf("%q is found in %q", notExpected, result)
257 }
258}
259
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700260func ensureMatches(t *testing.T, result string, expectedRex string) {
261 ok, err := regexp.MatchString(expectedRex, result)
262 if err != nil {
263 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
264 return
265 }
266 if !ok {
267 t.Errorf("%s does not match regular expession %s", result, expectedRex)
268 }
269}
270
Jiyong Park25fc6a92018-11-18 18:02:45 +0900271func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900272 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900273 if !android.InList(expected, result) {
274 t.Errorf("%q is not found in %v", expected, result)
275 }
276}
277
278func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900279 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 if android.InList(notExpected, result) {
281 t.Errorf("%q is found in %v", notExpected, result)
282 }
283}
284
Jooyung Hane1633032019-08-01 17:41:43 +0900285func ensureListEmpty(t *testing.T, result []string) {
286 t.Helper()
287 if len(result) > 0 {
288 t.Errorf("%q is expected to be empty", result)
289 }
290}
291
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000292func ensureListNotEmpty(t *testing.T, result []string) {
293 t.Helper()
294 if len(result) == 0 {
295 t.Errorf("%q is expected to be not empty", result)
296 }
297}
298
Jiyong Park25fc6a92018-11-18 18:02:45 +0900299// Minimal test
300func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800301 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900302 apex_defaults {
303 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900304 manifest: ":myapex.manifest",
305 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900306 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900307 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900308 native_shared_libs: [
309 "mylib",
310 "libfoo.ffi",
311 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900312 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800313 multilib: {
314 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900315 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800316 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900317 },
Jiyong Park77acec62020-06-01 21:39:15 +0900318 java_libs: [
319 "myjar",
320 "myjar_dex",
321 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000322 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323 }
324
Jiyong Park30ca9372019-02-07 16:27:23 +0900325 apex {
326 name: "myapex",
327 defaults: ["myapex-defaults"],
328 }
329
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 apex_key {
331 name: "myapex.key",
332 public_key: "testkey.avbpubkey",
333 private_key: "testkey.pem",
334 }
335
Jiyong Park809bb722019-02-13 21:33:49 +0900336 filegroup {
337 name: "myapex.manifest",
338 srcs: ["apex_manifest.json"],
339 }
340
341 filegroup {
342 name: "myapex.androidmanifest",
343 srcs: ["AndroidManifest.xml"],
344 }
345
Jiyong Park25fc6a92018-11-18 18:02:45 +0900346 cc_library {
347 name: "mylib",
348 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900349 shared_libs: [
350 "mylib2",
351 "libbar.ffi",
352 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353 system_shared_libs: [],
354 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000355 // TODO: remove //apex_available:platform
356 apex_available: [
357 "//apex_available:platform",
358 "myapex",
359 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900360 }
361
Alex Light3d673592019-01-18 14:37:31 -0800362 cc_binary {
363 name: "foo",
364 srcs: ["mylib.cpp"],
365 compile_multilib: "both",
366 multilib: {
367 lib32: {
368 suffix: "32",
369 },
370 lib64: {
371 suffix: "64",
372 },
373 },
374 symlinks: ["foo_link_"],
375 symlink_preferred_arch: true,
376 system_shared_libs: [],
377 static_executable: true,
378 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700379 apex_available: [ "myapex", "com.android.gki.*" ],
380 }
381
Jiyong Park99644e92020-11-17 22:21:02 +0900382 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000383 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900384 srcs: ["foo.rs"],
385 rlibs: ["libfoo.rlib.rust"],
386 dylibs: ["libfoo.dylib.rust"],
387 apex_available: ["myapex"],
388 }
389
390 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000391 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900392 srcs: ["foo.rs"],
393 crate_name: "foo",
394 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900395 shared_libs: ["libfoo.shared_from_rust"],
396 }
397
398 cc_library_shared {
399 name: "libfoo.shared_from_rust",
400 srcs: ["mylib.cpp"],
401 system_shared_libs: [],
402 stl: "none",
403 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900404 }
405
406 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000407 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900408 srcs: ["foo.rs"],
409 crate_name: "foo",
410 apex_available: ["myapex"],
411 }
412
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900413 rust_ffi_shared {
414 name: "libfoo.ffi",
415 srcs: ["foo.rs"],
416 crate_name: "foo",
417 apex_available: ["myapex"],
418 }
419
420 rust_ffi_shared {
421 name: "libbar.ffi",
422 srcs: ["foo.rs"],
423 crate_name: "bar",
424 apex_available: ["myapex"],
425 }
426
Yifan Hongd22a84a2020-07-28 17:37:46 -0700427 apex {
428 name: "com.android.gki.fake",
429 binaries: ["foo"],
430 key: "myapex.key",
431 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000432 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800433 }
434
Paul Duffindddd5462020-04-07 15:25:44 +0100435 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900436 name: "mylib2",
437 srcs: ["mylib.cpp"],
438 system_shared_libs: [],
439 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900440 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900441 static_libs: ["libstatic"],
442 // TODO: remove //apex_available:platform
443 apex_available: [
444 "//apex_available:platform",
445 "myapex",
446 ],
447 }
448
Paul Duffindddd5462020-04-07 15:25:44 +0100449 cc_prebuilt_library_shared {
450 name: "mylib2",
451 srcs: ["prebuilt.so"],
452 // TODO: remove //apex_available:platform
453 apex_available: [
454 "//apex_available:platform",
455 "myapex",
456 ],
457 }
458
Jiyong Park9918e1a2020-03-17 19:16:40 +0900459 cc_library_static {
460 name: "libstatic",
461 srcs: ["mylib.cpp"],
462 system_shared_libs: [],
463 stl: "none",
464 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000465 // TODO: remove //apex_available:platform
466 apex_available: [
467 "//apex_available:platform",
468 "myapex",
469 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900470 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900471
472 java_library {
473 name: "myjar",
474 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900475 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900476 sdk_version: "none",
477 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900478 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900479 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000480 // TODO: remove //apex_available:platform
481 apex_available: [
482 "//apex_available:platform",
483 "myapex",
484 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900485 }
486
Jiyong Park77acec62020-06-01 21:39:15 +0900487 dex_import {
488 name: "myjar_dex",
489 jars: ["prebuilt.jar"],
490 apex_available: [
491 "//apex_available:platform",
492 "myapex",
493 ],
494 }
495
Jiyong Park7f7766d2019-07-25 22:02:35 +0900496 java_library {
497 name: "myotherjar",
498 srcs: ["foo/bar/MyClass.java"],
499 sdk_version: "none",
500 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900501 // TODO: remove //apex_available:platform
502 apex_available: [
503 "//apex_available:platform",
504 "myapex",
505 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900506 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900507
508 java_library {
509 name: "mysharedjar",
510 srcs: ["foo/bar/MyClass.java"],
511 sdk_version: "none",
512 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900513 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900514 `)
515
Paul Duffina71a67a2021-03-29 00:42:57 +0100516 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900517
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900518 // Make sure that Android.mk is created
519 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700520 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900521 var builder strings.Builder
522 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
523
524 androidMk := builder.String()
525 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
526 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
527
Jiyong Park42cca6c2019-04-01 11:15:50 +0900528 optFlags := apexRule.Args["opt_flags"]
529 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700530 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100531 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900532
Jiyong Park25fc6a92018-11-18 18:02:45 +0900533 copyCmds := apexRule.Args["copy_commands"]
534
535 // Ensure that main rule creates an output
536 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
537
538 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700539 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
540 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
541 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900542 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900543 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900544
545 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700546 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
547 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900548 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
549 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900550 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900551 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900552
553 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800554 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
555 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900556 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900557 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900558 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900559 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
560 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900561 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900562 // .. but not for java libs
563 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900564 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800565
Colin Cross7113d202019-11-20 16:39:12 -0800566 // Ensure that the platform variant ends with _shared or _common
567 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
568 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900569 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
570 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900571 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
572
573 // Ensure that dynamic dependency to java libs are not included
574 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800575
576 // Ensure that all symlinks are present.
577 found_foo_link_64 := false
578 found_foo := false
579 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900580 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800581 if strings.HasSuffix(cmd, "bin/foo") {
582 found_foo = true
583 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
584 found_foo_link_64 = true
585 }
586 }
587 }
588 good := found_foo && found_foo_link_64
589 if !good {
590 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
591 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900592
Sundong Ahnabb64432019-10-22 13:58:29 +0900593 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700594 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900595 if len(noticeInputs) != 3 {
596 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900597 }
598 ensureListContains(t, noticeInputs, "NOTICE")
599 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900600 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900601
Artur Satayeva8bd1132020-04-27 18:07:06 +0100602 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100603 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100604 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
605 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
606 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100607
608 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100609 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100610 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
611 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
612 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800613}
614
Jooyung Hanf21c7972019-12-16 22:32:06 +0900615func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800616 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900617 apex_defaults {
618 name: "myapex-defaults",
619 key: "myapex.key",
620 prebuilts: ["myetc"],
621 native_shared_libs: ["mylib"],
622 java_libs: ["myjar"],
623 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900624 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800625 bpfs: ["bpf"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000626 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900627 }
628
629 prebuilt_etc {
630 name: "myetc",
631 src: "myprebuilt",
632 }
633
634 apex {
635 name: "myapex",
636 defaults: ["myapex-defaults"],
637 }
638
639 apex_key {
640 name: "myapex.key",
641 public_key: "testkey.avbpubkey",
642 private_key: "testkey.pem",
643 }
644
645 cc_library {
646 name: "mylib",
647 system_shared_libs: [],
648 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000649 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900650 }
651
652 java_library {
653 name: "myjar",
654 srcs: ["foo/bar/MyClass.java"],
655 sdk_version: "none",
656 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000657 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900658 }
659
660 android_app {
661 name: "AppFoo",
662 srcs: ["foo/bar/MyClass.java"],
663 sdk_version: "none",
664 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000665 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900666 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900667
668 runtime_resource_overlay {
669 name: "rro",
670 theme: "blue",
671 }
672
markchien2f59ec92020-09-02 16:23:38 +0800673 bpf {
674 name: "bpf",
675 srcs: ["bpf.c", "bpf2.c"],
676 }
677
Jooyung Hanf21c7972019-12-16 22:32:06 +0900678 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000679 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900680 "etc/myetc",
681 "javalib/myjar.jar",
682 "lib64/mylib.so",
683 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900684 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800685 "etc/bpf/bpf.o",
686 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900687 })
688}
689
Jooyung Han01a3ee22019-11-02 02:52:25 +0900690func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800691 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900692 apex {
693 name: "myapex",
694 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000695 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900696 }
697
698 apex_key {
699 name: "myapex.key",
700 public_key: "testkey.avbpubkey",
701 private_key: "testkey.pem",
702 }
703 `)
704
705 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900706 args := module.Rule("apexRule").Args
707 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
708 t.Error("manifest should be apex_manifest.pb, but " + manifest)
709 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900710}
711
Alex Light5098a612018-11-29 17:12:15 -0800712func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800713 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800714 apex {
715 name: "myapex",
716 key: "myapex.key",
717 payload_type: "zip",
718 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000719 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800720 }
721
722 apex_key {
723 name: "myapex.key",
724 public_key: "testkey.avbpubkey",
725 private_key: "testkey.pem",
726 }
727
728 cc_library {
729 name: "mylib",
730 srcs: ["mylib.cpp"],
731 shared_libs: ["mylib2"],
732 system_shared_libs: [],
733 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000734 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800735 }
736
737 cc_library {
738 name: "mylib2",
739 srcs: ["mylib.cpp"],
740 system_shared_libs: [],
741 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000742 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800743 }
744 `)
745
Sundong Ahnabb64432019-10-22 13:58:29 +0900746 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800747 copyCmds := zipApexRule.Args["copy_commands"]
748
749 // Ensure that main rule creates an output
750 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
751
752 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700753 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800754
755 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700756 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800757
758 // Ensure that both direct and indirect deps are copied into apex
759 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
760 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900761}
762
763func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800764 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900765 apex {
766 name: "myapex",
767 key: "myapex.key",
768 native_shared_libs: ["mylib", "mylib3"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000769 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900770 }
771
772 apex_key {
773 name: "myapex.key",
774 public_key: "testkey.avbpubkey",
775 private_key: "testkey.pem",
776 }
777
778 cc_library {
779 name: "mylib",
780 srcs: ["mylib.cpp"],
781 shared_libs: ["mylib2", "mylib3"],
782 system_shared_libs: [],
783 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000784 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900785 }
786
787 cc_library {
788 name: "mylib2",
789 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900790 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900791 system_shared_libs: [],
792 stl: "none",
793 stubs: {
794 versions: ["1", "2", "3"],
795 },
796 }
797
798 cc_library {
799 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900800 srcs: ["mylib.cpp"],
801 shared_libs: ["mylib4"],
802 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900803 stl: "none",
804 stubs: {
805 versions: ["10", "11", "12"],
806 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000807 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900808 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900809
810 cc_library {
811 name: "mylib4",
812 srcs: ["mylib.cpp"],
813 system_shared_libs: [],
814 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000815 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900816 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900817 `)
818
Sundong Ahnabb64432019-10-22 13:58:29 +0900819 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900820 copyCmds := apexRule.Args["copy_commands"]
821
822 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800823 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900824
825 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800826 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900827
828 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800829 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900830
Colin Crossaede88c2020-08-11 12:17:01 -0700831 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900832
833 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900834 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900835 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900836 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900837
838 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Crossaede88c2020-08-11 12:17:01 -0700839 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900840 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700841 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900842
843 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900844 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900845 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900846
847 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700848 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900849
Jooyung Hana57af4a2020-01-23 05:36:59 +0000850 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900851 "lib64/mylib.so",
852 "lib64/mylib3.so",
853 "lib64/mylib4.so",
854 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900855}
856
Colin Cross7812fd32020-09-25 12:35:10 -0700857func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
858 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -0800859 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -0700860 apex {
861 name: "myapex",
862 key: "myapex.key",
863 native_shared_libs: ["mylib", "mylib3"],
864 min_sdk_version: "29",
865 }
866
867 apex_key {
868 name: "myapex.key",
869 public_key: "testkey.avbpubkey",
870 private_key: "testkey.pem",
871 }
872
873 cc_library {
874 name: "mylib",
875 srcs: ["mylib.cpp"],
876 shared_libs: ["mylib2", "mylib3"],
877 system_shared_libs: [],
878 stl: "none",
879 apex_available: [ "myapex" ],
880 min_sdk_version: "28",
881 }
882
883 cc_library {
884 name: "mylib2",
885 srcs: ["mylib.cpp"],
886 cflags: ["-include mylib.h"],
887 system_shared_libs: [],
888 stl: "none",
889 stubs: {
890 versions: ["28", "29", "30", "current"],
891 },
892 min_sdk_version: "28",
893 }
894
895 cc_library {
896 name: "mylib3",
897 srcs: ["mylib.cpp"],
898 shared_libs: ["mylib4"],
899 system_shared_libs: [],
900 stl: "none",
901 stubs: {
902 versions: ["28", "29", "30", "current"],
903 },
904 apex_available: [ "myapex" ],
905 min_sdk_version: "28",
906 }
907
908 cc_library {
909 name: "mylib4",
910 srcs: ["mylib.cpp"],
911 system_shared_libs: [],
912 stl: "none",
913 apex_available: [ "myapex" ],
914 min_sdk_version: "28",
915 }
916 `)
917
918 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
919 copyCmds := apexRule.Args["copy_commands"]
920
921 // Ensure that direct non-stubs dep is always included
922 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
923
924 // Ensure that indirect stubs dep is not included
925 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
926
927 // Ensure that direct stubs dep is included
928 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
929
930 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
931
Jiyong Park55549df2021-02-26 23:57:23 +0900932 // Ensure that mylib is linking with the latest version of stub for mylib2
933 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -0700934 // ... and not linking to the non-stub (impl) variant of mylib2
935 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
936
937 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
938 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
939 // .. and not linking to the stubs variant of mylib3
940 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
941
942 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -0700943 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -0700944 ensureNotContains(t, mylib2Cflags, "-include ")
945
946 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700947 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -0700948
949 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
950 "lib64/mylib.so",
951 "lib64/mylib3.so",
952 "lib64/mylib4.so",
953 })
954}
955
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900956func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
957 t.Parallel()
958 // myapex (Z)
959 // mylib -----------------.
960 // |
961 // otherapex (29) |
962 // libstub's versions: 29 Z current
963 // |
964 // <platform> |
965 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -0800966 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900967 apex {
968 name: "myapex",
969 key: "myapex.key",
970 native_shared_libs: ["mylib"],
971 min_sdk_version: "Z", // non-final
972 }
973
974 cc_library {
975 name: "mylib",
976 srcs: ["mylib.cpp"],
977 shared_libs: ["libstub"],
978 apex_available: ["myapex"],
979 min_sdk_version: "Z",
980 }
981
982 apex_key {
983 name: "myapex.key",
984 public_key: "testkey.avbpubkey",
985 private_key: "testkey.pem",
986 }
987
988 apex {
989 name: "otherapex",
990 key: "myapex.key",
991 native_shared_libs: ["libstub"],
992 min_sdk_version: "29",
993 }
994
995 cc_library {
996 name: "libstub",
997 srcs: ["mylib.cpp"],
998 stubs: {
999 versions: ["29", "Z", "current"],
1000 },
1001 apex_available: ["otherapex"],
1002 min_sdk_version: "29",
1003 }
1004
1005 // platform module depending on libstub from otherapex should use the latest stub("current")
1006 cc_library {
1007 name: "libplatform",
1008 srcs: ["mylib.cpp"],
1009 shared_libs: ["libstub"],
1010 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001011 `,
1012 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1013 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1014 variables.Platform_sdk_final = proptools.BoolPtr(false)
1015 variables.Platform_version_active_codenames = []string{"Z"}
1016 }),
1017 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001018
Jiyong Park55549df2021-02-26 23:57:23 +09001019 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001020 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001021 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001022 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001023 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001024
1025 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1026 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1027 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1028 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1029 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1030}
1031
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001032func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001033 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001034 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001035 name: "myapex2",
1036 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001037 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001038 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001039 }
1040
1041 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001042 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001043 public_key: "testkey.avbpubkey",
1044 private_key: "testkey.pem",
1045 }
1046
1047 cc_library {
1048 name: "mylib",
1049 srcs: ["mylib.cpp"],
1050 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001051 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001052 system_shared_libs: [],
1053 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001054 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001055 }
1056
1057 cc_library {
1058 name: "libfoo",
1059 srcs: ["mylib.cpp"],
1060 shared_libs: ["libbar"],
1061 system_shared_libs: [],
1062 stl: "none",
1063 stubs: {
1064 versions: ["10", "20", "30"],
1065 },
1066 }
1067
1068 cc_library {
1069 name: "libbar",
1070 srcs: ["mylib.cpp"],
1071 system_shared_libs: [],
1072 stl: "none",
1073 }
1074
Jiyong Park678c8812020-02-07 17:25:49 +09001075 cc_library_static {
1076 name: "libbaz",
1077 srcs: ["mylib.cpp"],
1078 system_shared_libs: [],
1079 stl: "none",
1080 apex_available: [ "myapex2" ],
1081 }
1082
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001083 `)
1084
Jiyong Park83dc74b2020-01-14 18:38:44 +09001085 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001086 copyCmds := apexRule.Args["copy_commands"]
1087
1088 // Ensure that direct non-stubs dep is always included
1089 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1090
1091 // Ensure that indirect stubs dep is not included
1092 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1093
1094 // Ensure that dependency of stubs is not included
1095 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1096
Colin Crossaede88c2020-08-11 12:17:01 -07001097 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001098
1099 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001100 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001101 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001102 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001103
Jiyong Park3ff16992019-12-27 14:11:47 +09001104 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001105
1106 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1107 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001108
Artur Satayeva8bd1132020-04-27 18:07:06 +01001109 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001110 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001111
Artur Satayeva8bd1132020-04-27 18:07:06 +01001112 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001113 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001114}
1115
Jooyung Hand3639552019-08-09 12:57:43 +09001116func TestApexWithRuntimeLibsDependency(t *testing.T) {
1117 /*
1118 myapex
1119 |
1120 v (runtime_libs)
1121 mylib ------+------> libfoo [provides stub]
1122 |
1123 `------> libbar
1124 */
Colin Cross1c460562021-02-16 17:55:47 -08001125 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001126 apex {
1127 name: "myapex",
1128 key: "myapex.key",
1129 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001130 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001131 }
1132
1133 apex_key {
1134 name: "myapex.key",
1135 public_key: "testkey.avbpubkey",
1136 private_key: "testkey.pem",
1137 }
1138
1139 cc_library {
1140 name: "mylib",
1141 srcs: ["mylib.cpp"],
1142 runtime_libs: ["libfoo", "libbar"],
1143 system_shared_libs: [],
1144 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001145 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001146 }
1147
1148 cc_library {
1149 name: "libfoo",
1150 srcs: ["mylib.cpp"],
1151 system_shared_libs: [],
1152 stl: "none",
1153 stubs: {
1154 versions: ["10", "20", "30"],
1155 },
1156 }
1157
1158 cc_library {
1159 name: "libbar",
1160 srcs: ["mylib.cpp"],
1161 system_shared_libs: [],
1162 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001163 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001164 }
1165
1166 `)
1167
Sundong Ahnabb64432019-10-22 13:58:29 +09001168 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001169 copyCmds := apexRule.Args["copy_commands"]
1170
1171 // Ensure that direct non-stubs dep is always included
1172 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1173
1174 // Ensure that indirect stubs dep is not included
1175 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1176
1177 // Ensure that runtime_libs dep in included
1178 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1179
Sundong Ahnabb64432019-10-22 13:58:29 +09001180 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001181 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1182 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001183
1184}
1185
Paul Duffina02cae32021-03-09 01:44:06 +00001186var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1187 cc.PrepareForTestWithCcBuildComponents,
1188 PrepareForTestWithApexBuildComponents,
1189 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001190 apex {
1191 name: "com.android.runtime",
1192 key: "com.android.runtime.key",
1193 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001194 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001195 }
1196
1197 apex_key {
1198 name: "com.android.runtime.key",
1199 public_key: "testkey.avbpubkey",
1200 private_key: "testkey.pem",
1201 }
Paul Duffina02cae32021-03-09 01:44:06 +00001202 `),
1203 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1204)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001205
Paul Duffina02cae32021-03-09 01:44:06 +00001206func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001207 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001208 cc_library {
1209 name: "libc",
1210 no_libcrt: true,
1211 nocrt: true,
1212 stl: "none",
1213 system_shared_libs: [],
1214 stubs: { versions: ["1"] },
1215 apex_available: ["com.android.runtime"],
1216
1217 sanitize: {
1218 hwaddress: true,
1219 }
1220 }
1221
1222 cc_prebuilt_library_shared {
1223 name: "libclang_rt.hwasan-aarch64-android",
1224 no_libcrt: true,
1225 nocrt: true,
1226 stl: "none",
1227 system_shared_libs: [],
1228 srcs: [""],
1229 stubs: { versions: ["1"] },
1230
1231 sanitize: {
1232 never: true,
1233 },
Paul Duffina02cae32021-03-09 01:44:06 +00001234 } `)
1235 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001236
1237 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1238 "lib64/bionic/libc.so",
1239 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1240 })
1241
1242 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1243
1244 installed := hwasan.Description("install libclang_rt.hwasan")
1245 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1246
1247 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1248 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1249 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1250}
1251
1252func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001253 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001254 prepareForTestOfRuntimeApexWithHwasan,
1255 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1256 variables.SanitizeDevice = []string{"hwaddress"}
1257 }),
1258 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001259 cc_library {
1260 name: "libc",
1261 no_libcrt: true,
1262 nocrt: true,
1263 stl: "none",
1264 system_shared_libs: [],
1265 stubs: { versions: ["1"] },
1266 apex_available: ["com.android.runtime"],
1267 }
1268
1269 cc_prebuilt_library_shared {
1270 name: "libclang_rt.hwasan-aarch64-android",
1271 no_libcrt: true,
1272 nocrt: true,
1273 stl: "none",
1274 system_shared_libs: [],
1275 srcs: [""],
1276 stubs: { versions: ["1"] },
1277
1278 sanitize: {
1279 never: true,
1280 },
1281 }
Paul Duffina02cae32021-03-09 01:44:06 +00001282 `)
1283 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001284
1285 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1286 "lib64/bionic/libc.so",
1287 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1288 })
1289
1290 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1291
1292 installed := hwasan.Description("install libclang_rt.hwasan")
1293 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1294
1295 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1296 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1297 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1298}
1299
Jooyung Han61b66e92020-03-21 14:21:46 +00001300func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1301 testcases := []struct {
1302 name string
1303 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001304 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001305 shouldLink string
1306 shouldNotLink []string
1307 }{
1308 {
Jiyong Park55549df2021-02-26 23:57:23 +09001309 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001310 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001311 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001312 shouldLink: "current",
1313 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001314 },
1315 {
Jiyong Park55549df2021-02-26 23:57:23 +09001316 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001317 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001318 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001319 shouldLink: "current",
1320 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001321 },
1322 }
1323 for _, tc := range testcases {
1324 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001325 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001326 apex {
1327 name: "myapex",
1328 key: "myapex.key",
1329 use_vendor: true,
1330 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001331 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001332 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001333 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001334
Jooyung Han61b66e92020-03-21 14:21:46 +00001335 apex_key {
1336 name: "myapex.key",
1337 public_key: "testkey.avbpubkey",
1338 private_key: "testkey.pem",
1339 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001340
Jooyung Han61b66e92020-03-21 14:21:46 +00001341 cc_library {
1342 name: "mylib",
1343 srcs: ["mylib.cpp"],
1344 vendor_available: true,
1345 shared_libs: ["libbar"],
1346 system_shared_libs: [],
1347 stl: "none",
1348 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001349 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001350 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001351
Jooyung Han61b66e92020-03-21 14:21:46 +00001352 cc_library {
1353 name: "libbar",
1354 srcs: ["mylib.cpp"],
1355 system_shared_libs: [],
1356 stl: "none",
1357 stubs: { versions: ["29","30"] },
Colin Cross0477b422020-10-13 18:43:54 -07001358 llndk_stubs: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001359 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001360
Jooyung Han61b66e92020-03-21 14:21:46 +00001361 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001362 name: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001363 symbol_file: "",
1364 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001365 `,
1366 setUseVendorAllowListForTest([]string{"myapex"}),
1367 withUnbundledBuild,
1368 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001369
Jooyung Han61b66e92020-03-21 14:21:46 +00001370 // Ensure that LLNDK dep is not included
1371 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1372 "lib64/mylib.so",
1373 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001374
Jooyung Han61b66e92020-03-21 14:21:46 +00001375 // Ensure that LLNDK dep is required
1376 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1377 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1378 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001379
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001380 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.29_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1381 ensureContains(t, mylibLdFlags, "libbar/android_vendor.29_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001382 for _, ver := range tc.shouldNotLink {
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001383 ensureNotContains(t, mylibLdFlags, "libbar/android_vendor.29_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001384 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001385
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001386 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.29_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001387 ver := tc.shouldLink
1388 if tc.shouldLink == "current" {
1389 ver = strconv.Itoa(android.FutureApiLevelInt)
1390 }
1391 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001392 })
1393 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001394}
1395
Jiyong Park25fc6a92018-11-18 18:02:45 +09001396func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001397 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001398 apex {
1399 name: "myapex",
1400 key: "myapex.key",
1401 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001402 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001403 }
1404
1405 apex_key {
1406 name: "myapex.key",
1407 public_key: "testkey.avbpubkey",
1408 private_key: "testkey.pem",
1409 }
1410
1411 cc_library {
1412 name: "mylib",
1413 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001414 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001415 shared_libs: ["libdl#27"],
1416 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001417 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001418 }
1419
1420 cc_library_shared {
1421 name: "mylib_shared",
1422 srcs: ["mylib.cpp"],
1423 shared_libs: ["libdl#27"],
1424 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001425 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001426 }
1427
1428 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001429 name: "libBootstrap",
1430 srcs: ["mylib.cpp"],
1431 stl: "none",
1432 bootstrap: true,
1433 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001434 `)
1435
Sundong Ahnabb64432019-10-22 13:58:29 +09001436 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001437 copyCmds := apexRule.Args["copy_commands"]
1438
1439 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001440 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001441 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1442 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001443
1444 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001445 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001446
Colin Crossaede88c2020-08-11 12:17:01 -07001447 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1448 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1449 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001450
1451 // For dependency to libc
1452 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001453 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001454 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001455 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001456 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001457 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1458 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001459
1460 // For dependency to libm
1461 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001462 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001463 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001464 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001465 // ... and is not compiling with the stub
1466 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1467 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1468
1469 // For dependency to libdl
1470 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001471 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001472 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001473 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1474 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001475 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001476 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001477 // ... Cflags from stub is correctly exported to mylib
1478 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1479 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001480
1481 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001482 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1483 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1484 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1485 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001486}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001487
Jooyung Han749dc692020-04-15 11:03:39 +09001488func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001489 // there are three links between liba --> libz.
1490 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001491 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001492 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001493 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001494 apex {
1495 name: "myapex",
1496 key: "myapex.key",
1497 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001498 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001499 }
1500
1501 apex {
1502 name: "otherapex",
1503 key: "myapex.key",
1504 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001505 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001506 }
1507
1508 apex_key {
1509 name: "myapex.key",
1510 public_key: "testkey.avbpubkey",
1511 private_key: "testkey.pem",
1512 }
1513
1514 cc_library {
1515 name: "libx",
1516 shared_libs: ["liba"],
1517 system_shared_libs: [],
1518 stl: "none",
1519 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001520 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001521 }
1522
1523 cc_library {
1524 name: "liby",
1525 shared_libs: ["liba"],
1526 system_shared_libs: [],
1527 stl: "none",
1528 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001529 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001530 }
1531
1532 cc_library {
1533 name: "liba",
1534 shared_libs: ["libz"],
1535 system_shared_libs: [],
1536 stl: "none",
1537 apex_available: [
1538 "//apex_available:anyapex",
1539 "//apex_available:platform",
1540 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001541 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001542 }
1543
1544 cc_library {
1545 name: "libz",
1546 system_shared_libs: [],
1547 stl: "none",
1548 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001549 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001550 },
1551 }
Jooyung Han749dc692020-04-15 11:03:39 +09001552 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001553
1554 expectLink := func(from, from_variant, to, to_variant string) {
1555 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1556 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1557 }
1558 expectNoLink := func(from, from_variant, to, to_variant string) {
1559 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1560 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1561 }
1562 // platform liba is linked to non-stub version
1563 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001564 // liba in myapex is linked to current
1565 expectLink("liba", "shared_apex29", "libz", "shared_current")
1566 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001567 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001568 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001569 // liba in otherapex is linked to current
1570 expectLink("liba", "shared_apex30", "libz", "shared_current")
1571 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001572 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1573 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001574}
1575
Jooyung Hanaed150d2020-04-02 01:41:41 +09001576func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001577 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001578 apex {
1579 name: "myapex",
1580 key: "myapex.key",
1581 native_shared_libs: ["libx"],
1582 min_sdk_version: "R",
1583 }
1584
1585 apex_key {
1586 name: "myapex.key",
1587 public_key: "testkey.avbpubkey",
1588 private_key: "testkey.pem",
1589 }
1590
1591 cc_library {
1592 name: "libx",
1593 shared_libs: ["libz"],
1594 system_shared_libs: [],
1595 stl: "none",
1596 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001597 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001598 }
1599
1600 cc_library {
1601 name: "libz",
1602 system_shared_libs: [],
1603 stl: "none",
1604 stubs: {
1605 versions: ["29", "R"],
1606 },
1607 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001608 `,
1609 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1610 variables.Platform_version_active_codenames = []string{"R"}
1611 }),
1612 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001613
1614 expectLink := func(from, from_variant, to, to_variant string) {
1615 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1616 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1617 }
1618 expectNoLink := func(from, from_variant, to, to_variant string) {
1619 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1620 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1621 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001622 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1623 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001624 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1625 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001626}
1627
Jooyung Han749dc692020-04-15 11:03:39 +09001628func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001629 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001630 apex {
1631 name: "myapex",
1632 key: "myapex.key",
1633 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001634 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001635 }
1636
1637 apex_key {
1638 name: "myapex.key",
1639 public_key: "testkey.avbpubkey",
1640 private_key: "testkey.pem",
1641 }
1642
1643 cc_library {
1644 name: "libx",
1645 shared_libs: ["libz"],
1646 system_shared_libs: [],
1647 stl: "none",
1648 apex_available: [ "myapex" ],
1649 }
1650
1651 cc_library {
1652 name: "libz",
1653 system_shared_libs: [],
1654 stl: "none",
1655 stubs: {
1656 versions: ["1", "2"],
1657 },
1658 }
1659 `)
1660
1661 expectLink := func(from, from_variant, to, to_variant string) {
1662 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1663 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1664 }
1665 expectNoLink := func(from, from_variant, to, to_variant string) {
1666 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1667 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1668 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001669 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001670 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001671 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001672 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001673}
1674
1675func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001676 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001677 apex {
1678 name: "myapex",
1679 key: "myapex.key",
1680 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001681 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001682 }
1683
1684 apex_key {
1685 name: "myapex.key",
1686 public_key: "testkey.avbpubkey",
1687 private_key: "testkey.pem",
1688 }
1689
1690 cc_library {
1691 name: "libx",
1692 system_shared_libs: [],
1693 stl: "none",
1694 apex_available: [ "myapex" ],
1695 stubs: {
1696 versions: ["1", "2"],
1697 },
1698 }
1699
1700 cc_library {
1701 name: "libz",
1702 shared_libs: ["libx"],
1703 system_shared_libs: [],
1704 stl: "none",
1705 }
1706 `)
1707
1708 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001709 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001710 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1711 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1712 }
1713 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001714 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001715 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1716 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1717 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001718 expectLink("libz", "shared", "libx", "shared_current")
1719 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09001720 expectNoLink("libz", "shared", "libz", "shared_1")
1721 expectNoLink("libz", "shared", "libz", "shared")
1722}
1723
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001724var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1725 func(variables android.FixtureProductVariables) {
1726 variables.SanitizeDevice = []string{"hwaddress"}
1727 },
1728)
1729
Jooyung Han75568392020-03-20 04:29:24 +09001730func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001731 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001732 apex {
1733 name: "myapex",
1734 key: "myapex.key",
1735 native_shared_libs: ["libx"],
1736 min_sdk_version: "29",
1737 }
1738
1739 apex_key {
1740 name: "myapex.key",
1741 public_key: "testkey.avbpubkey",
1742 private_key: "testkey.pem",
1743 }
1744
1745 cc_library {
1746 name: "libx",
1747 shared_libs: ["libbar"],
1748 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001749 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001750 }
1751
1752 cc_library {
1753 name: "libbar",
1754 stubs: {
1755 versions: ["29", "30"],
1756 },
1757 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001758 `,
1759 prepareForTestWithSantitizeHwaddress,
1760 )
Jooyung Han03b51852020-02-26 22:45:42 +09001761 expectLink := func(from, from_variant, to, to_variant string) {
1762 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1763 libFlags := ld.Args["libFlags"]
1764 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1765 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001766 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09001767}
1768
Jooyung Han75568392020-03-20 04:29:24 +09001769func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001770 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001771 apex {
1772 name: "myapex",
1773 key: "myapex.key",
1774 native_shared_libs: ["libx"],
1775 min_sdk_version: "29",
1776 }
1777
1778 apex_key {
1779 name: "myapex.key",
1780 public_key: "testkey.avbpubkey",
1781 private_key: "testkey.pem",
1782 }
1783
1784 cc_library {
1785 name: "libx",
1786 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001787 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001788 }
Jooyung Han75568392020-03-20 04:29:24 +09001789 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001790
1791 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001792 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001793 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001794 // note that platform variant is not.
1795 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001796 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001797}
1798
Jooyung Han749dc692020-04-15 11:03:39 +09001799func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1800 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001801 apex {
1802 name: "myapex",
1803 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001804 native_shared_libs: ["mylib"],
1805 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001806 }
1807
1808 apex_key {
1809 name: "myapex.key",
1810 public_key: "testkey.avbpubkey",
1811 private_key: "testkey.pem",
1812 }
Jooyung Han749dc692020-04-15 11:03:39 +09001813
1814 cc_library {
1815 name: "mylib",
1816 srcs: ["mylib.cpp"],
1817 system_shared_libs: [],
1818 stl: "none",
1819 apex_available: [
1820 "myapex",
1821 ],
1822 min_sdk_version: "30",
1823 }
1824 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001825
1826 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
1827 apex {
1828 name: "myapex",
1829 key: "myapex.key",
1830 native_shared_libs: ["libfoo.ffi"],
1831 min_sdk_version: "29",
1832 }
1833
1834 apex_key {
1835 name: "myapex.key",
1836 public_key: "testkey.avbpubkey",
1837 private_key: "testkey.pem",
1838 }
1839
1840 rust_ffi_shared {
1841 name: "libfoo.ffi",
1842 srcs: ["foo.rs"],
1843 crate_name: "foo",
1844 apex_available: [
1845 "myapex",
1846 ],
1847 min_sdk_version: "30",
1848 }
1849 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001850
1851 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
1852 apex {
1853 name: "myapex",
1854 key: "myapex.key",
1855 java_libs: ["libfoo"],
1856 min_sdk_version: "29",
1857 }
1858
1859 apex_key {
1860 name: "myapex.key",
1861 public_key: "testkey.avbpubkey",
1862 private_key: "testkey.pem",
1863 }
1864
1865 java_import {
1866 name: "libfoo",
1867 jars: ["libfoo.jar"],
1868 apex_available: [
1869 "myapex",
1870 ],
1871 min_sdk_version: "30",
1872 }
1873 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001874}
1875
1876func TestApexMinSdkVersion_Okay(t *testing.T) {
1877 testApex(t, `
1878 apex {
1879 name: "myapex",
1880 key: "myapex.key",
1881 native_shared_libs: ["libfoo"],
1882 java_libs: ["libbar"],
1883 min_sdk_version: "29",
1884 }
1885
1886 apex_key {
1887 name: "myapex.key",
1888 public_key: "testkey.avbpubkey",
1889 private_key: "testkey.pem",
1890 }
1891
1892 cc_library {
1893 name: "libfoo",
1894 srcs: ["mylib.cpp"],
1895 shared_libs: ["libfoo_dep"],
1896 apex_available: ["myapex"],
1897 min_sdk_version: "29",
1898 }
1899
1900 cc_library {
1901 name: "libfoo_dep",
1902 srcs: ["mylib.cpp"],
1903 apex_available: ["myapex"],
1904 min_sdk_version: "29",
1905 }
1906
1907 java_library {
1908 name: "libbar",
1909 sdk_version: "current",
1910 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001911 static_libs: [
1912 "libbar_dep",
1913 "libbar_import_dep",
1914 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001915 apex_available: ["myapex"],
1916 min_sdk_version: "29",
1917 }
1918
1919 java_library {
1920 name: "libbar_dep",
1921 sdk_version: "current",
1922 srcs: ["a.java"],
1923 apex_available: ["myapex"],
1924 min_sdk_version: "29",
1925 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001926
1927 java_import {
1928 name: "libbar_import_dep",
1929 jars: ["libbar.jar"],
1930 apex_available: ["myapex"],
1931 min_sdk_version: "29",
1932 }
Jooyung Han03b51852020-02-26 22:45:42 +09001933 `)
1934}
1935
Artur Satayev8cf899a2020-04-15 17:29:42 +01001936func TestJavaStableSdkVersion(t *testing.T) {
1937 testCases := []struct {
1938 name string
1939 expectedError string
1940 bp string
1941 }{
1942 {
1943 name: "Non-updatable apex with non-stable dep",
1944 bp: `
1945 apex {
1946 name: "myapex",
1947 java_libs: ["myjar"],
1948 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001949 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01001950 }
1951 apex_key {
1952 name: "myapex.key",
1953 public_key: "testkey.avbpubkey",
1954 private_key: "testkey.pem",
1955 }
1956 java_library {
1957 name: "myjar",
1958 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001959 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001960 apex_available: ["myapex"],
1961 }
1962 `,
1963 },
1964 {
1965 name: "Updatable apex with stable dep",
1966 bp: `
1967 apex {
1968 name: "myapex",
1969 java_libs: ["myjar"],
1970 key: "myapex.key",
1971 updatable: true,
1972 min_sdk_version: "29",
1973 }
1974 apex_key {
1975 name: "myapex.key",
1976 public_key: "testkey.avbpubkey",
1977 private_key: "testkey.pem",
1978 }
1979 java_library {
1980 name: "myjar",
1981 srcs: ["foo/bar/MyClass.java"],
1982 sdk_version: "current",
1983 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001984 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001985 }
1986 `,
1987 },
1988 {
1989 name: "Updatable apex with non-stable dep",
1990 expectedError: "cannot depend on \"myjar\"",
1991 bp: `
1992 apex {
1993 name: "myapex",
1994 java_libs: ["myjar"],
1995 key: "myapex.key",
1996 updatable: true,
1997 }
1998 apex_key {
1999 name: "myapex.key",
2000 public_key: "testkey.avbpubkey",
2001 private_key: "testkey.pem",
2002 }
2003 java_library {
2004 name: "myjar",
2005 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002006 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002007 apex_available: ["myapex"],
2008 }
2009 `,
2010 },
2011 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002012 name: "Updatable apex with non-stable transitive dep",
2013 // This is not actually detecting that the transitive dependency is unstable, rather it is
2014 // detecting that the transitive dependency is building against a wider API surface than the
2015 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002016 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002017 bp: `
2018 apex {
2019 name: "myapex",
2020 java_libs: ["myjar"],
2021 key: "myapex.key",
2022 updatable: true,
2023 }
2024 apex_key {
2025 name: "myapex.key",
2026 public_key: "testkey.avbpubkey",
2027 private_key: "testkey.pem",
2028 }
2029 java_library {
2030 name: "myjar",
2031 srcs: ["foo/bar/MyClass.java"],
2032 sdk_version: "current",
2033 apex_available: ["myapex"],
2034 static_libs: ["transitive-jar"],
2035 }
2036 java_library {
2037 name: "transitive-jar",
2038 srcs: ["foo/bar/MyClass.java"],
2039 sdk_version: "core_platform",
2040 apex_available: ["myapex"],
2041 }
2042 `,
2043 },
2044 }
2045
2046 for _, test := range testCases {
2047 t.Run(test.name, func(t *testing.T) {
2048 if test.expectedError == "" {
2049 testApex(t, test.bp)
2050 } else {
2051 testApexError(t, test.expectedError, test.bp)
2052 }
2053 })
2054 }
2055}
2056
Jooyung Han749dc692020-04-15 11:03:39 +09002057func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2058 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2059 apex {
2060 name: "myapex",
2061 key: "myapex.key",
2062 native_shared_libs: ["mylib"],
2063 min_sdk_version: "29",
2064 }
2065
2066 apex_key {
2067 name: "myapex.key",
2068 public_key: "testkey.avbpubkey",
2069 private_key: "testkey.pem",
2070 }
2071
2072 cc_library {
2073 name: "mylib",
2074 srcs: ["mylib.cpp"],
2075 shared_libs: ["mylib2"],
2076 system_shared_libs: [],
2077 stl: "none",
2078 apex_available: [
2079 "myapex",
2080 ],
2081 min_sdk_version: "29",
2082 }
2083
2084 // indirect part of the apex
2085 cc_library {
2086 name: "mylib2",
2087 srcs: ["mylib.cpp"],
2088 system_shared_libs: [],
2089 stl: "none",
2090 apex_available: [
2091 "myapex",
2092 ],
2093 min_sdk_version: "30",
2094 }
2095 `)
2096}
2097
2098func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2099 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2100 apex {
2101 name: "myapex",
2102 key: "myapex.key",
2103 apps: ["AppFoo"],
2104 min_sdk_version: "29",
2105 }
2106
2107 apex_key {
2108 name: "myapex.key",
2109 public_key: "testkey.avbpubkey",
2110 private_key: "testkey.pem",
2111 }
2112
2113 android_app {
2114 name: "AppFoo",
2115 srcs: ["foo/bar/MyClass.java"],
2116 sdk_version: "current",
2117 min_sdk_version: "29",
2118 system_modules: "none",
2119 stl: "none",
2120 static_libs: ["bar"],
2121 apex_available: [ "myapex" ],
2122 }
2123
2124 java_library {
2125 name: "bar",
2126 sdk_version: "current",
2127 srcs: ["a.java"],
2128 apex_available: [ "myapex" ],
2129 }
2130 `)
2131}
2132
2133func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002134 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002135 apex {
2136 name: "myapex",
2137 key: "myapex.key",
2138 native_shared_libs: ["mylib"],
2139 min_sdk_version: "29",
2140 }
2141
2142 apex_key {
2143 name: "myapex.key",
2144 public_key: "testkey.avbpubkey",
2145 private_key: "testkey.pem",
2146 }
2147
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002148 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002149 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2150 cc_library {
2151 name: "mylib",
2152 srcs: ["mylib.cpp"],
2153 shared_libs: ["mylib2"],
2154 system_shared_libs: [],
2155 stl: "none",
2156 apex_available: ["myapex", "otherapex"],
2157 min_sdk_version: "29",
2158 }
2159
2160 cc_library {
2161 name: "mylib2",
2162 srcs: ["mylib.cpp"],
2163 system_shared_libs: [],
2164 stl: "none",
2165 apex_available: ["otherapex"],
2166 stubs: { versions: ["29", "30"] },
2167 min_sdk_version: "30",
2168 }
2169
2170 apex {
2171 name: "otherapex",
2172 key: "myapex.key",
2173 native_shared_libs: ["mylib", "mylib2"],
2174 min_sdk_version: "30",
2175 }
2176 `)
2177 expectLink := func(from, from_variant, to, to_variant string) {
2178 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2179 libFlags := ld.Args["libFlags"]
2180 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2181 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002182 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002183 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002184}
2185
Jooyung Haned124c32021-01-26 11:43:46 +09002186func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002187 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2188 func(variables android.FixtureProductVariables) {
2189 variables.Platform_sdk_codename = proptools.StringPtr("S")
2190 variables.Platform_version_active_codenames = []string{"S"}
2191 },
2192 )
Jooyung Haned124c32021-01-26 11:43:46 +09002193 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2194 apex {
2195 name: "myapex",
2196 key: "myapex.key",
2197 native_shared_libs: ["libfoo"],
2198 min_sdk_version: "S",
2199 }
2200 apex_key {
2201 name: "myapex.key",
2202 public_key: "testkey.avbpubkey",
2203 private_key: "testkey.pem",
2204 }
2205 cc_library {
2206 name: "libfoo",
2207 shared_libs: ["libbar"],
2208 apex_available: ["myapex"],
2209 min_sdk_version: "29",
2210 }
2211 cc_library {
2212 name: "libbar",
2213 apex_available: ["myapex"],
2214 }
2215 `, withSAsActiveCodeNames)
2216}
2217
2218func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002219 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2220 variables.Platform_sdk_codename = proptools.StringPtr("S")
2221 variables.Platform_version_active_codenames = []string{"S", "T"}
2222 })
Colin Cross1c460562021-02-16 17:55:47 -08002223 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002224 apex {
2225 name: "myapex",
2226 key: "myapex.key",
2227 native_shared_libs: ["libfoo"],
2228 min_sdk_version: "S",
2229 }
2230 apex_key {
2231 name: "myapex.key",
2232 public_key: "testkey.avbpubkey",
2233 private_key: "testkey.pem",
2234 }
2235 cc_library {
2236 name: "libfoo",
2237 shared_libs: ["libbar"],
2238 apex_available: ["myapex"],
2239 min_sdk_version: "S",
2240 }
2241 cc_library {
2242 name: "libbar",
2243 stubs: {
2244 symbol_file: "libbar.map.txt",
2245 versions: ["30", "S", "T"],
2246 },
2247 }
2248 `, withSAsActiveCodeNames)
2249
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002250 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002251 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2252 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002253 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002254}
2255
Jiyong Park7c2ee712018-12-07 00:42:25 +09002256func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002257 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002258 apex {
2259 name: "myapex",
2260 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002261 native_shared_libs: ["mylib"],
2262 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002263 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002264 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002265 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002266 }
2267
2268 apex_key {
2269 name: "myapex.key",
2270 public_key: "testkey.avbpubkey",
2271 private_key: "testkey.pem",
2272 }
2273
2274 prebuilt_etc {
2275 name: "myetc",
2276 src: "myprebuilt",
2277 sub_dir: "foo/bar",
2278 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002279
2280 cc_library {
2281 name: "mylib",
2282 srcs: ["mylib.cpp"],
2283 relative_install_path: "foo/bar",
2284 system_shared_libs: [],
2285 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002286 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002287 }
2288
2289 cc_binary {
2290 name: "mybin",
2291 srcs: ["mylib.cpp"],
2292 relative_install_path: "foo/bar",
2293 system_shared_libs: [],
2294 static_executable: true,
2295 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002296 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002297 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002298 `)
2299
Sundong Ahnabb64432019-10-22 13:58:29 +09002300 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002301 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2302
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002303 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002304 ensureListContains(t, dirs, "etc")
2305 ensureListContains(t, dirs, "etc/foo")
2306 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002307 ensureListContains(t, dirs, "lib64")
2308 ensureListContains(t, dirs, "lib64/foo")
2309 ensureListContains(t, dirs, "lib64/foo/bar")
2310 ensureListContains(t, dirs, "lib")
2311 ensureListContains(t, dirs, "lib/foo")
2312 ensureListContains(t, dirs, "lib/foo/bar")
2313
Jiyong Parkbd13e442019-03-15 18:10:35 +09002314 ensureListContains(t, dirs, "bin")
2315 ensureListContains(t, dirs, "bin/foo")
2316 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002317}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002318
Jooyung Han35155c42020-02-06 17:33:20 +09002319func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002320 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002321 apex {
2322 name: "myapex",
2323 key: "myapex.key",
2324 multilib: {
2325 both: {
2326 native_shared_libs: ["mylib"],
2327 binaries: ["mybin"],
2328 },
2329 },
2330 compile_multilib: "both",
2331 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002332 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002333 }
2334
2335 apex_key {
2336 name: "myapex.key",
2337 public_key: "testkey.avbpubkey",
2338 private_key: "testkey.pem",
2339 }
2340
2341 cc_library {
2342 name: "mylib",
2343 relative_install_path: "foo/bar",
2344 system_shared_libs: [],
2345 stl: "none",
2346 apex_available: [ "myapex" ],
2347 native_bridge_supported: true,
2348 }
2349
2350 cc_binary {
2351 name: "mybin",
2352 relative_install_path: "foo/bar",
2353 system_shared_libs: [],
2354 static_executable: true,
2355 stl: "none",
2356 apex_available: [ "myapex" ],
2357 native_bridge_supported: true,
2358 compile_multilib: "both", // default is "first" for binary
2359 multilib: {
2360 lib64: {
2361 suffix: "64",
2362 },
2363 },
2364 }
2365 `, withNativeBridgeEnabled)
2366 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2367 "bin/foo/bar/mybin",
2368 "bin/foo/bar/mybin64",
2369 "bin/arm/foo/bar/mybin",
2370 "bin/arm64/foo/bar/mybin64",
2371 "lib/foo/bar/mylib.so",
2372 "lib/arm/foo/bar/mylib.so",
2373 "lib64/foo/bar/mylib.so",
2374 "lib64/arm64/foo/bar/mylib.so",
2375 })
2376}
2377
Jiyong Parkda6eb592018-12-19 17:12:36 +09002378func TestUseVendor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002379 ctx := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002380 apex {
2381 name: "myapex",
2382 key: "myapex.key",
2383 native_shared_libs: ["mylib"],
2384 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002385 updatable: false,
Jiyong Parkda6eb592018-12-19 17:12:36 +09002386 }
2387
2388 apex_key {
2389 name: "myapex.key",
2390 public_key: "testkey.avbpubkey",
2391 private_key: "testkey.pem",
2392 }
2393
2394 cc_library {
2395 name: "mylib",
2396 srcs: ["mylib.cpp"],
2397 shared_libs: ["mylib2"],
2398 system_shared_libs: [],
2399 vendor_available: true,
2400 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002401 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002402 }
2403
2404 cc_library {
2405 name: "mylib2",
2406 srcs: ["mylib.cpp"],
2407 system_shared_libs: [],
2408 vendor_available: true,
2409 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002410 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002411 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002412 `,
2413 setUseVendorAllowListForTest([]string{"myapex"}),
2414 )
Jiyong Parkda6eb592018-12-19 17:12:36 +09002415
2416 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002417 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002418 for _, implicit := range i.Implicits {
2419 inputsList = append(inputsList, implicit.String())
2420 }
2421 }
2422 inputsString := strings.Join(inputsList, " ")
2423
2424 // ensure that the apex includes vendor variants of the direct and indirect deps
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002425 ensureContains(t, inputsString, "android_vendor.29_arm64_armv8-a_shared_apex10000/mylib.so")
2426 ensureContains(t, inputsString, "android_vendor.29_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002427
2428 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002429 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2430 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002431}
Jiyong Park16e91a02018-12-20 18:18:08 +09002432
Jooyung Han85d61762020-06-24 23:50:26 +09002433func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002434 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2435 apex {
2436 name: "myapex",
2437 key: "myapex.key",
2438 use_vendor: true,
2439 }
2440 apex_key {
2441 name: "myapex.key",
2442 public_key: "testkey.avbpubkey",
2443 private_key: "testkey.pem",
2444 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002445 `,
2446 setUseVendorAllowListForTest([]string{""}),
2447 )
Colin Cross440e0d02020-06-11 11:32:11 -07002448 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002449 testApex(t, `
2450 apex {
2451 name: "myapex",
2452 key: "myapex.key",
2453 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002454 updatable: false,
Jooyung Handc782442019-11-01 03:14:38 +09002455 }
2456 apex_key {
2457 name: "myapex.key",
2458 public_key: "testkey.avbpubkey",
2459 private_key: "testkey.pem",
2460 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002461 `,
2462 setUseVendorAllowListForTest([]string{"myapex"}),
2463 )
Jooyung Handc782442019-11-01 03:14:38 +09002464}
2465
Jooyung Han5c998b92019-06-27 11:30:33 +09002466func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2467 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2468 apex {
2469 name: "myapex",
2470 key: "myapex.key",
2471 native_shared_libs: ["mylib"],
2472 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002473 updatable: false,
Jooyung Han5c998b92019-06-27 11:30:33 +09002474 }
2475
2476 apex_key {
2477 name: "myapex.key",
2478 public_key: "testkey.avbpubkey",
2479 private_key: "testkey.pem",
2480 }
2481
2482 cc_library {
2483 name: "mylib",
2484 srcs: ["mylib.cpp"],
2485 system_shared_libs: [],
2486 stl: "none",
2487 }
2488 `)
2489}
2490
Jooyung Han85d61762020-06-24 23:50:26 +09002491func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002492 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002493 apex {
2494 name: "myapex",
2495 key: "myapex.key",
2496 binaries: ["mybin"],
2497 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002498 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002499 }
2500 apex_key {
2501 name: "myapex.key",
2502 public_key: "testkey.avbpubkey",
2503 private_key: "testkey.pem",
2504 }
2505 cc_binary {
2506 name: "mybin",
2507 vendor: true,
2508 shared_libs: ["libfoo"],
2509 }
2510 cc_library {
2511 name: "libfoo",
2512 proprietary: true,
2513 }
2514 `)
2515
2516 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2517 "bin/mybin",
2518 "lib64/libfoo.so",
2519 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2520 "lib64/libc++.so",
2521 })
2522
2523 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002524 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002525 name := apexBundle.BaseModuleName()
2526 prefix := "TARGET_"
2527 var builder strings.Builder
2528 data.Custom(&builder, name, prefix, "", data)
Paul Duffin37ba3442021-03-29 00:21:08 +01002529 androidMk := android.StringRelativeToTop(ctx.Config(), builder.String())
2530 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002531 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002532
2533 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2534 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2535 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002536}
2537
Jooyung Handf78e212020-07-22 15:54:47 +09002538func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002539 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002540 apex {
2541 name: "myapex",
2542 key: "myapex.key",
2543 binaries: ["mybin"],
2544 vendor: true,
2545 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002546 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002547 }
2548 apex_key {
2549 name: "myapex.key",
2550 public_key: "testkey.avbpubkey",
2551 private_key: "testkey.pem",
2552 }
2553 cc_binary {
2554 name: "mybin",
2555 vendor: true,
2556 shared_libs: ["libvndk", "libvendor"],
2557 }
2558 cc_library {
2559 name: "libvndk",
2560 vndk: {
2561 enabled: true,
2562 },
2563 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002564 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002565 }
2566 cc_library {
2567 name: "libvendor",
2568 vendor: true,
2569 }
2570 `)
2571
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002572 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002573
Paul Duffina71a67a2021-03-29 00:42:57 +01002574 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002575 libs := names(ldRule.Args["libFlags"])
2576 // VNDK libs(libvndk/libc++) as they are
Paul Duffin37ba3442021-03-29 00:21:08 +01002577 ensureListContains(t, libs, "out/soong/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
2578 ensureListContains(t, libs, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002579 // non-stable Vendor libs as APEX variants
Paul Duffin37ba3442021-03-29 00:21:08 +01002580 ensureListContains(t, libs, "out/soong/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002581
2582 // VNDK libs are not included when use_vndk_as_stable: true
2583 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2584 "bin/mybin",
2585 "lib64/libvendor.so",
2586 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002587
2588 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2589 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2590 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002591}
2592
Justin Yun13decfb2021-03-08 19:25:55 +09002593func TestProductVariant(t *testing.T) {
2594 ctx := testApex(t, `
2595 apex {
2596 name: "myapex",
2597 key: "myapex.key",
2598 updatable: false,
2599 product_specific: true,
2600 binaries: ["foo"],
2601 }
2602
2603 apex_key {
2604 name: "myapex.key",
2605 public_key: "testkey.avbpubkey",
2606 private_key: "testkey.pem",
2607 }
2608
2609 cc_binary {
2610 name: "foo",
2611 product_available: true,
2612 apex_available: ["myapex"],
2613 srcs: ["foo.cpp"],
2614 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002615 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2616 variables.ProductVndkVersion = proptools.StringPtr("current")
2617 }),
2618 )
Justin Yun13decfb2021-03-08 19:25:55 +09002619
2620 cflags := strings.Fields(
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002621 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002622 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2623 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2624 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2625 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2626}
2627
Jooyung Han8e5685d2020-09-21 11:02:57 +09002628func TestApex_withPrebuiltFirmware(t *testing.T) {
2629 testCases := []struct {
2630 name string
2631 additionalProp string
2632 }{
2633 {"system apex with prebuilt_firmware", ""},
2634 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2635 }
2636 for _, tc := range testCases {
2637 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002638 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002639 apex {
2640 name: "myapex",
2641 key: "myapex.key",
2642 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002643 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002644 `+tc.additionalProp+`
2645 }
2646 apex_key {
2647 name: "myapex.key",
2648 public_key: "testkey.avbpubkey",
2649 private_key: "testkey.pem",
2650 }
2651 prebuilt_firmware {
2652 name: "myfirmware",
2653 src: "myfirmware.bin",
2654 filename_from_src: true,
2655 `+tc.additionalProp+`
2656 }
2657 `)
2658 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2659 "etc/firmware/myfirmware.bin",
2660 })
2661 })
2662 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002663}
2664
Jooyung Hanefb184e2020-06-25 17:14:25 +09002665func TestAndroidMk_UseVendorRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002666 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002667 apex {
2668 name: "myapex",
2669 key: "myapex.key",
2670 use_vendor: true,
2671 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002672 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002673 }
2674
2675 apex_key {
2676 name: "myapex.key",
2677 public_key: "testkey.avbpubkey",
2678 private_key: "testkey.pem",
2679 }
2680
2681 cc_library {
2682 name: "mylib",
2683 vendor_available: true,
2684 apex_available: ["myapex"],
2685 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002686 `,
2687 setUseVendorAllowListForTest([]string{"myapex"}),
2688 )
Jooyung Hanefb184e2020-06-25 17:14:25 +09002689
2690 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002691 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002692 name := apexBundle.BaseModuleName()
2693 prefix := "TARGET_"
2694 var builder strings.Builder
2695 data.Custom(&builder, name, prefix, "", data)
2696 androidMk := builder.String()
2697 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2698}
2699
2700func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002701 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002702 apex {
2703 name: "myapex",
2704 key: "myapex.key",
2705 vendor: true,
2706 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002707 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002708 }
2709
2710 apex_key {
2711 name: "myapex.key",
2712 public_key: "testkey.avbpubkey",
2713 private_key: "testkey.pem",
2714 }
2715
2716 cc_library {
2717 name: "mylib",
2718 vendor_available: true,
2719 }
2720 `)
2721
2722 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002723 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002724 name := apexBundle.BaseModuleName()
2725 prefix := "TARGET_"
2726 var builder strings.Builder
2727 data.Custom(&builder, name, prefix, "", data)
2728 androidMk := builder.String()
2729 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2730}
2731
Jooyung Han2ed99d02020-06-24 23:26:26 +09002732func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002733 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002734 apex {
2735 name: "myapex",
2736 key: "myapex.key",
2737 vintf_fragments: ["fragment.xml"],
2738 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002739 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002740 }
2741 apex_key {
2742 name: "myapex.key",
2743 public_key: "testkey.avbpubkey",
2744 private_key: "testkey.pem",
2745 }
2746 cc_binary {
2747 name: "mybin",
2748 }
2749 `)
2750
2751 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002752 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002753 name := apexBundle.BaseModuleName()
2754 prefix := "TARGET_"
2755 var builder strings.Builder
2756 data.Custom(&builder, name, prefix, "", data)
2757 androidMk := builder.String()
2758 ensureContains(t, androidMk, "LOCAL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04002759 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09002760}
2761
Jiyong Park16e91a02018-12-20 18:18:08 +09002762func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002763 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002764 apex {
2765 name: "myapex",
2766 key: "myapex.key",
2767 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002768 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002769 }
2770
2771 apex_key {
2772 name: "myapex.key",
2773 public_key: "testkey.avbpubkey",
2774 private_key: "testkey.pem",
2775 }
2776
2777 cc_library {
2778 name: "mylib",
2779 srcs: ["mylib.cpp"],
2780 system_shared_libs: [],
2781 stl: "none",
2782 stubs: {
2783 versions: ["1", "2", "3"],
2784 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002785 apex_available: [
2786 "//apex_available:platform",
2787 "myapex",
2788 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002789 }
2790
2791 cc_binary {
2792 name: "not_in_apex",
2793 srcs: ["mylib.cpp"],
2794 static_libs: ["mylib"],
2795 static_executable: true,
2796 system_shared_libs: [],
2797 stl: "none",
2798 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002799 `)
2800
Colin Cross7113d202019-11-20 16:39:12 -08002801 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002802
2803 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002804 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002805}
Jiyong Park9335a262018-12-24 11:31:58 +09002806
2807func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002808 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002809 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002810 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002811 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002812 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002813 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002814 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002815 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09002816 }
2817
2818 cc_library {
2819 name: "mylib",
2820 srcs: ["mylib.cpp"],
2821 system_shared_libs: [],
2822 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002823 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002824 }
2825
2826 apex_key {
2827 name: "myapex.key",
2828 public_key: "testkey.avbpubkey",
2829 private_key: "testkey.pem",
2830 }
2831
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002832 android_app_certificate {
2833 name: "myapex.certificate",
2834 certificate: "testkey",
2835 }
2836
2837 android_app_certificate {
2838 name: "myapex.certificate.override",
2839 certificate: "testkey.override",
2840 }
2841
Jiyong Park9335a262018-12-24 11:31:58 +09002842 `)
2843
2844 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002845 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002846
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002847 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2848 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002849 "vendor/foo/devkeys/testkey.avbpubkey")
2850 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002851 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2852 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002853 "vendor/foo/devkeys/testkey.pem")
2854 }
2855
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002856 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002857 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002858 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002859 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002860 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002861 }
2862}
Jiyong Park58e364a2019-01-19 19:24:06 +09002863
Jooyung Hanf121a652019-12-17 14:30:11 +09002864func TestCertificate(t *testing.T) {
2865 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002866 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002867 apex {
2868 name: "myapex",
2869 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002870 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002871 }
2872 apex_key {
2873 name: "myapex.key",
2874 public_key: "testkey.avbpubkey",
2875 private_key: "testkey.pem",
2876 }`)
2877 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2878 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2879 if actual := rule.Args["certificates"]; actual != expected {
2880 t.Errorf("certificates should be %q, not %q", expected, actual)
2881 }
2882 })
2883 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002884 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002885 apex {
2886 name: "myapex_keytest",
2887 key: "myapex.key",
2888 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002889 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002890 }
2891 apex_key {
2892 name: "myapex.key",
2893 public_key: "testkey.avbpubkey",
2894 private_key: "testkey.pem",
2895 }
2896 android_app_certificate {
2897 name: "myapex.certificate.override",
2898 certificate: "testkey.override",
2899 }`)
2900 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2901 expected := "testkey.override.x509.pem testkey.override.pk8"
2902 if actual := rule.Args["certificates"]; actual != expected {
2903 t.Errorf("certificates should be %q, not %q", expected, actual)
2904 }
2905 })
2906 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002907 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002908 apex {
2909 name: "myapex",
2910 key: "myapex.key",
2911 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002912 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002913 }
2914 apex_key {
2915 name: "myapex.key",
2916 public_key: "testkey.avbpubkey",
2917 private_key: "testkey.pem",
2918 }
2919 android_app_certificate {
2920 name: "myapex.certificate",
2921 certificate: "testkey",
2922 }`)
2923 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2924 expected := "testkey.x509.pem testkey.pk8"
2925 if actual := rule.Args["certificates"]; actual != expected {
2926 t.Errorf("certificates should be %q, not %q", expected, actual)
2927 }
2928 })
2929 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002930 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002931 apex {
2932 name: "myapex_keytest",
2933 key: "myapex.key",
2934 file_contexts: ":myapex-file_contexts",
2935 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002936 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002937 }
2938 apex_key {
2939 name: "myapex.key",
2940 public_key: "testkey.avbpubkey",
2941 private_key: "testkey.pem",
2942 }
2943 android_app_certificate {
2944 name: "myapex.certificate.override",
2945 certificate: "testkey.override",
2946 }`)
2947 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2948 expected := "testkey.override.x509.pem testkey.override.pk8"
2949 if actual := rule.Args["certificates"]; actual != expected {
2950 t.Errorf("certificates should be %q, not %q", expected, actual)
2951 }
2952 })
2953 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002954 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002955 apex {
2956 name: "myapex",
2957 key: "myapex.key",
2958 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002959 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002960 }
2961 apex_key {
2962 name: "myapex.key",
2963 public_key: "testkey.avbpubkey",
2964 private_key: "testkey.pem",
2965 }`)
2966 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2967 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2968 if actual := rule.Args["certificates"]; actual != expected {
2969 t.Errorf("certificates should be %q, not %q", expected, actual)
2970 }
2971 })
2972 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002973 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002974 apex {
2975 name: "myapex_keytest",
2976 key: "myapex.key",
2977 file_contexts: ":myapex-file_contexts",
2978 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002979 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002980 }
2981 apex_key {
2982 name: "myapex.key",
2983 public_key: "testkey.avbpubkey",
2984 private_key: "testkey.pem",
2985 }
2986 android_app_certificate {
2987 name: "myapex.certificate.override",
2988 certificate: "testkey.override",
2989 }`)
2990 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2991 expected := "testkey.override.x509.pem testkey.override.pk8"
2992 if actual := rule.Args["certificates"]; actual != expected {
2993 t.Errorf("certificates should be %q, not %q", expected, actual)
2994 }
2995 })
2996}
2997
Jiyong Park58e364a2019-01-19 19:24:06 +09002998func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002999 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003000 apex {
3001 name: "myapex",
3002 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003003 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003004 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003005 }
3006
3007 apex {
3008 name: "otherapex",
3009 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003010 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003011 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003012 }
3013
3014 apex_key {
3015 name: "myapex.key",
3016 public_key: "testkey.avbpubkey",
3017 private_key: "testkey.pem",
3018 }
3019
3020 cc_library {
3021 name: "mylib",
3022 srcs: ["mylib.cpp"],
3023 system_shared_libs: [],
3024 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003025 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003026 "myapex",
3027 "otherapex",
3028 ],
Jooyung Han24282772020-03-21 23:20:55 +09003029 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003030 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003031 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003032 cc_library {
3033 name: "mylib2",
3034 srcs: ["mylib.cpp"],
3035 system_shared_libs: [],
3036 stl: "none",
3037 apex_available: [
3038 "myapex",
3039 "otherapex",
3040 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003041 static_libs: ["mylib3"],
3042 recovery_available: true,
3043 min_sdk_version: "29",
3044 }
3045 cc_library {
3046 name: "mylib3",
3047 srcs: ["mylib.cpp"],
3048 system_shared_libs: [],
3049 stl: "none",
3050 apex_available: [
3051 "myapex",
3052 "otherapex",
3053 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09003054 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07003055 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003056 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003057 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003058 `)
3059
Jooyung Hanc87a0592020-03-02 17:44:33 +09003060 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003061 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003062 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003063 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003064
Jooyung Hanccce2f22020-03-07 03:45:53 +09003065 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003066 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003067 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003068 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09003069 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003070
Jooyung Hanccce2f22020-03-07 03:45:53 +09003071 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003072 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003073 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003074 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09003075 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003076
Colin Crossaede88c2020-08-11 12:17:01 -07003077 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3078 // each variant defines additional macros to distinguish which apex variant it is built for
3079
3080 // non-APEX variant does not have __ANDROID_APEX__ defined
3081 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3082 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3083
3084 // APEX variant has __ANDROID_APEX__ defined
3085 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
3086 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3087 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3088 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3089
3090 // APEX variant has __ANDROID_APEX__ defined
3091 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
3092 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3093 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3094 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3095
Dan Albertb19953d2020-11-17 15:29:36 -08003096 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003097 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3098 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003099 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003100
3101 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
3102 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09003103
3104 // non-APEX variant does not have __ANDROID_APEX__ defined
3105 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3106 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3107
3108 // APEX variant has __ANDROID_APEX__ defined
3109 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003110 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003111 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003112 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003113
Jooyung Hanc87a0592020-03-02 17:44:33 +09003114 // APEX variant has __ANDROID_APEX__ defined
3115 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003116 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003117 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003118 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09003119
Dan Albertb19953d2020-11-17 15:29:36 -08003120 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003121 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003122 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003123 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003124}
Jiyong Park7e636d02019-01-28 16:16:54 +09003125
3126func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003127 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003128 apex {
3129 name: "myapex",
3130 key: "myapex.key",
3131 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003132 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003133 }
3134
3135 apex_key {
3136 name: "myapex.key",
3137 public_key: "testkey.avbpubkey",
3138 private_key: "testkey.pem",
3139 }
3140
3141 cc_library_headers {
3142 name: "mylib_headers",
3143 export_include_dirs: ["my_include"],
3144 system_shared_libs: [],
3145 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003146 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003147 }
3148
3149 cc_library {
3150 name: "mylib",
3151 srcs: ["mylib.cpp"],
3152 system_shared_libs: [],
3153 stl: "none",
3154 header_libs: ["mylib_headers"],
3155 export_header_lib_headers: ["mylib_headers"],
3156 stubs: {
3157 versions: ["1", "2", "3"],
3158 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003159 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003160 }
3161
3162 cc_library {
3163 name: "otherlib",
3164 srcs: ["mylib.cpp"],
3165 system_shared_libs: [],
3166 stl: "none",
3167 shared_libs: ["mylib"],
3168 }
3169 `)
3170
Colin Cross7113d202019-11-20 16:39:12 -08003171 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003172
3173 // Ensure that the include path of the header lib is exported to 'otherlib'
3174 ensureContains(t, cFlags, "-Imy_include")
3175}
Alex Light9670d332019-01-29 18:07:33 -08003176
Jiyong Park7cd10e32020-01-14 09:22:18 +09003177type fileInApex struct {
3178 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003179 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003180 isLink bool
3181}
3182
Jooyung Hana57af4a2020-01-23 05:36:59 +00003183func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003184 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003185 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003186 copyCmds := apexRule.Args["copy_commands"]
3187 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003188 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003189 for _, cmd := range strings.Split(copyCmds, "&&") {
3190 cmd = strings.TrimSpace(cmd)
3191 if cmd == "" {
3192 continue
3193 }
3194 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003195 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003196 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003197 switch terms[0] {
3198 case "mkdir":
3199 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003200 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003201 t.Fatal("copyCmds contains invalid cp command", cmd)
3202 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003203 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003204 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003205 isLink = false
3206 case "ln":
3207 if len(terms) != 3 && len(terms) != 4 {
3208 // ln LINK TARGET or ln -s LINK TARGET
3209 t.Fatal("copyCmds contains invalid ln command", cmd)
3210 }
3211 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003212 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003213 isLink = true
3214 default:
3215 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3216 }
3217 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003218 index := strings.Index(dst, imageApexDir)
3219 if index == -1 {
3220 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3221 }
3222 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003223 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003224 }
3225 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003226 return ret
3227}
3228
Jooyung Hana57af4a2020-01-23 05:36:59 +00003229func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3230 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003231 var failed bool
3232 var surplus []string
3233 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003234 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003235 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003236 for _, expected := range files {
3237 if matched, _ := path.Match(expected, file.path); matched {
3238 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003239 mactchFound = true
3240 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003241 }
3242 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003243 if !mactchFound {
3244 surplus = append(surplus, file.path)
3245 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003246 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003247
Jooyung Han31c470b2019-10-18 16:26:59 +09003248 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003249 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003250 t.Log("surplus files", surplus)
3251 failed = true
3252 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003253
3254 if len(files) > len(filesMatched) {
3255 var missing []string
3256 for _, expected := range files {
3257 if !filesMatched[expected] {
3258 missing = append(missing, expected)
3259 }
3260 }
3261 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003262 t.Log("missing files", missing)
3263 failed = true
3264 }
3265 if failed {
3266 t.Fail()
3267 }
3268}
3269
Jooyung Han344d5432019-08-23 11:17:39 +09003270func TestVndkApexCurrent(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003271 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003272 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003273 name: "com.android.vndk.current",
3274 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003275 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003276 }
3277
3278 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003279 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003280 public_key: "testkey.avbpubkey",
3281 private_key: "testkey.pem",
3282 }
3283
3284 cc_library {
3285 name: "libvndk",
3286 srcs: ["mylib.cpp"],
3287 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003288 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003289 vndk: {
3290 enabled: true,
3291 },
3292 system_shared_libs: [],
3293 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003294 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003295 }
3296
3297 cc_library {
3298 name: "libvndksp",
3299 srcs: ["mylib.cpp"],
3300 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003301 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003302 vndk: {
3303 enabled: true,
3304 support_system_process: true,
3305 },
3306 system_shared_libs: [],
3307 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003308 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003309 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003310 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003311
Colin Cross2807f002021-03-02 10:15:29 -08003312 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003313 "lib/libvndk.so",
3314 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003315 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003316 "lib64/libvndk.so",
3317 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003318 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003319 "etc/llndk.libraries.29.txt",
3320 "etc/vndkcore.libraries.29.txt",
3321 "etc/vndksp.libraries.29.txt",
3322 "etc/vndkprivate.libraries.29.txt",
3323 "etc/vndkproduct.libraries.29.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003324 })
Jooyung Han344d5432019-08-23 11:17:39 +09003325}
3326
3327func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003328 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003329 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003330 name: "com.android.vndk.current",
3331 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003332 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003333 }
3334
3335 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003336 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003337 public_key: "testkey.avbpubkey",
3338 private_key: "testkey.pem",
3339 }
3340
3341 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003342 name: "libvndk",
3343 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003344 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003345 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003346 vndk: {
3347 enabled: true,
3348 },
3349 system_shared_libs: [],
3350 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003351 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003352 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003353
3354 cc_prebuilt_library_shared {
3355 name: "libvndk.arm",
3356 srcs: ["libvndk.arm.so"],
3357 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003358 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003359 vndk: {
3360 enabled: true,
3361 },
3362 enabled: false,
3363 arch: {
3364 arm: {
3365 enabled: true,
3366 },
3367 },
3368 system_shared_libs: [],
3369 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003370 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003371 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003372 `+vndkLibrariesTxtFiles("current"),
3373 withFiles(map[string][]byte{
3374 "libvndk.so": nil,
3375 "libvndk.arm.so": nil,
3376 }))
Colin Cross2807f002021-03-02 10:15:29 -08003377 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003378 "lib/libvndk.so",
3379 "lib/libvndk.arm.so",
3380 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003381 "lib/libc++.so",
3382 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003383 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003384 })
Jooyung Han344d5432019-08-23 11:17:39 +09003385}
3386
Jooyung Han39edb6c2019-11-06 16:53:07 +09003387func vndkLibrariesTxtFiles(vers ...string) (result string) {
3388 for _, v := range vers {
3389 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003390 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003391 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003392 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003393 name: "` + txt + `.libraries.txt",
3394 }
3395 `
3396 }
3397 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003398 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003399 result += `
3400 prebuilt_etc {
3401 name: "` + txt + `.libraries.` + v + `.txt",
3402 src: "dummy.txt",
3403 }
3404 `
3405 }
3406 }
3407 }
3408 return
3409}
3410
Jooyung Han344d5432019-08-23 11:17:39 +09003411func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003412 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003413 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003414 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003415 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003416 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003417 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003418 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003419 }
3420
3421 apex_key {
3422 name: "myapex.key",
3423 public_key: "testkey.avbpubkey",
3424 private_key: "testkey.pem",
3425 }
3426
Jooyung Han31c470b2019-10-18 16:26:59 +09003427 vndk_prebuilt_shared {
3428 name: "libvndk27",
3429 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003430 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003431 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003432 vndk: {
3433 enabled: true,
3434 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003435 target_arch: "arm64",
3436 arch: {
3437 arm: {
3438 srcs: ["libvndk27_arm.so"],
3439 },
3440 arm64: {
3441 srcs: ["libvndk27_arm64.so"],
3442 },
3443 },
Colin Cross2807f002021-03-02 10:15:29 -08003444 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003445 }
3446
3447 vndk_prebuilt_shared {
3448 name: "libvndk27",
3449 version: "27",
3450 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003451 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003452 vndk: {
3453 enabled: true,
3454 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003455 target_arch: "x86_64",
3456 arch: {
3457 x86: {
3458 srcs: ["libvndk27_x86.so"],
3459 },
3460 x86_64: {
3461 srcs: ["libvndk27_x86_64.so"],
3462 },
3463 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003464 }
3465 `+vndkLibrariesTxtFiles("27"),
3466 withFiles(map[string][]byte{
3467 "libvndk27_arm.so": nil,
3468 "libvndk27_arm64.so": nil,
3469 "libvndk27_x86.so": nil,
3470 "libvndk27_x86_64.so": nil,
3471 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003472
Colin Cross2807f002021-03-02 10:15:29 -08003473 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003474 "lib/libvndk27_arm.so",
3475 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003476 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003477 })
Jooyung Han344d5432019-08-23 11:17:39 +09003478}
3479
Jooyung Han90eee022019-10-01 20:02:42 +09003480func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003481 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003482 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003483 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003484 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003485 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003486 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003487 }
3488 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003489 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003490 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003491 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003492 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003493 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003494 }
3495 apex_key {
3496 name: "myapex.key",
3497 public_key: "testkey.avbpubkey",
3498 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003499 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003500
3501 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003502 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003503 actual := proptools.String(bundle.properties.Apex_name)
3504 if !reflect.DeepEqual(actual, expected) {
3505 t.Errorf("Got '%v', expected '%v'", actual, expected)
3506 }
3507 }
3508
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003509 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003510 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003511}
3512
Jooyung Han344d5432019-08-23 11:17:39 +09003513func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003514 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003515 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003516 name: "com.android.vndk.current",
3517 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003518 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003519 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003520 }
3521
3522 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003523 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003524 public_key: "testkey.avbpubkey",
3525 private_key: "testkey.pem",
3526 }
3527
3528 cc_library {
3529 name: "libvndk",
3530 srcs: ["mylib.cpp"],
3531 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003532 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003533 native_bridge_supported: true,
3534 host_supported: true,
3535 vndk: {
3536 enabled: true,
3537 },
3538 system_shared_libs: [],
3539 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003540 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003541 }
Colin Cross2807f002021-03-02 10:15:29 -08003542 `+vndkLibrariesTxtFiles("current"),
3543 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003544
Colin Cross2807f002021-03-02 10:15:29 -08003545 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003546 "lib/libvndk.so",
3547 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003548 "lib/libc++.so",
3549 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003550 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003551 })
Jooyung Han344d5432019-08-23 11:17:39 +09003552}
3553
3554func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003555 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003556 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003557 name: "com.android.vndk.current",
3558 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003559 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003560 native_bridge_supported: true,
3561 }
3562
3563 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003564 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003565 public_key: "testkey.avbpubkey",
3566 private_key: "testkey.pem",
3567 }
3568
3569 cc_library {
3570 name: "libvndk",
3571 srcs: ["mylib.cpp"],
3572 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003573 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003574 native_bridge_supported: true,
3575 host_supported: true,
3576 vndk: {
3577 enabled: true,
3578 },
3579 system_shared_libs: [],
3580 stl: "none",
3581 }
3582 `)
3583}
3584
Jooyung Han31c470b2019-10-18 16:26:59 +09003585func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003586 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003587 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003588 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003589 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003590 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003591 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003592 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003593 }
3594
3595 apex_key {
3596 name: "myapex.key",
3597 public_key: "testkey.avbpubkey",
3598 private_key: "testkey.pem",
3599 }
3600
3601 vndk_prebuilt_shared {
3602 name: "libvndk27",
3603 version: "27",
3604 target_arch: "arm",
3605 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003606 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003607 vndk: {
3608 enabled: true,
3609 },
3610 arch: {
3611 arm: {
3612 srcs: ["libvndk27.so"],
3613 }
3614 },
3615 }
3616
3617 vndk_prebuilt_shared {
3618 name: "libvndk27",
3619 version: "27",
3620 target_arch: "arm",
3621 binder32bit: true,
3622 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003623 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003624 vndk: {
3625 enabled: true,
3626 },
3627 arch: {
3628 arm: {
3629 srcs: ["libvndk27binder32.so"],
3630 }
3631 },
Colin Cross2807f002021-03-02 10:15:29 -08003632 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003633 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003634 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003635 withFiles(map[string][]byte{
3636 "libvndk27.so": nil,
3637 "libvndk27binder32.so": nil,
3638 }),
3639 withBinder32bit,
3640 withTargets(map[android.OsType][]android.Target{
3641 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003642 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3643 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003644 },
3645 }),
3646 )
3647
Colin Cross2807f002021-03-02 10:15:29 -08003648 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003649 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003650 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003651 })
3652}
3653
Jooyung Han45a96772020-06-15 14:59:42 +09003654func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003655 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003656 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003657 name: "com.android.vndk.current",
3658 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003659 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003660 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003661 }
3662
3663 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003664 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003665 public_key: "testkey.avbpubkey",
3666 private_key: "testkey.pem",
3667 }
3668
3669 cc_library {
3670 name: "libz",
3671 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003672 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003673 vndk: {
3674 enabled: true,
3675 },
3676 stubs: {
3677 symbol_file: "libz.map.txt",
3678 versions: ["30"],
3679 }
3680 }
3681 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3682 "libz.map.txt": nil,
3683 }))
3684
Colin Cross2807f002021-03-02 10:15:29 -08003685 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003686 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3687 ensureListEmpty(t, provideNativeLibs)
3688}
3689
Jooyung Hane1633032019-08-01 17:41:43 +09003690func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003691 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003692 apex {
3693 name: "myapex_nodep",
3694 key: "myapex.key",
3695 native_shared_libs: ["lib_nodep"],
3696 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003697 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003698 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003699 }
3700
3701 apex {
3702 name: "myapex_dep",
3703 key: "myapex.key",
3704 native_shared_libs: ["lib_dep"],
3705 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003706 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003707 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003708 }
3709
3710 apex {
3711 name: "myapex_provider",
3712 key: "myapex.key",
3713 native_shared_libs: ["libfoo"],
3714 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003715 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003716 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003717 }
3718
3719 apex {
3720 name: "myapex_selfcontained",
3721 key: "myapex.key",
3722 native_shared_libs: ["lib_dep", "libfoo"],
3723 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003724 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003725 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003726 }
3727
3728 apex_key {
3729 name: "myapex.key",
3730 public_key: "testkey.avbpubkey",
3731 private_key: "testkey.pem",
3732 }
3733
3734 cc_library {
3735 name: "lib_nodep",
3736 srcs: ["mylib.cpp"],
3737 system_shared_libs: [],
3738 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003739 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003740 }
3741
3742 cc_library {
3743 name: "lib_dep",
3744 srcs: ["mylib.cpp"],
3745 shared_libs: ["libfoo"],
3746 system_shared_libs: [],
3747 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003748 apex_available: [
3749 "myapex_dep",
3750 "myapex_provider",
3751 "myapex_selfcontained",
3752 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003753 }
3754
3755 cc_library {
3756 name: "libfoo",
3757 srcs: ["mytest.cpp"],
3758 stubs: {
3759 versions: ["1"],
3760 },
3761 system_shared_libs: [],
3762 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003763 apex_available: [
3764 "myapex_provider",
3765 "myapex_selfcontained",
3766 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003767 }
3768 `)
3769
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003770 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003771 var provideNativeLibs, requireNativeLibs []string
3772
Sundong Ahnabb64432019-10-22 13:58:29 +09003773 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003774 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3775 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003776 ensureListEmpty(t, provideNativeLibs)
3777 ensureListEmpty(t, requireNativeLibs)
3778
Sundong Ahnabb64432019-10-22 13:58:29 +09003779 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003780 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3781 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003782 ensureListEmpty(t, provideNativeLibs)
3783 ensureListContains(t, requireNativeLibs, "libfoo.so")
3784
Sundong Ahnabb64432019-10-22 13:58:29 +09003785 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003786 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3787 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003788 ensureListContains(t, provideNativeLibs, "libfoo.so")
3789 ensureListEmpty(t, requireNativeLibs)
3790
Sundong Ahnabb64432019-10-22 13:58:29 +09003791 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003792 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3793 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003794 ensureListContains(t, provideNativeLibs, "libfoo.so")
3795 ensureListEmpty(t, requireNativeLibs)
3796}
3797
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003798func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003799 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003800 apex {
3801 name: "myapex",
3802 key: "myapex.key",
3803 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003804 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003805 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003806 }
3807
3808 apex_key {
3809 name: "myapex.key",
3810 public_key: "testkey.avbpubkey",
3811 private_key: "testkey.pem",
3812 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003813
3814 cc_library {
3815 name: "mylib",
3816 srcs: ["mylib.cpp"],
3817 system_shared_libs: [],
3818 stl: "none",
3819 apex_available: [
3820 "//apex_available:platform",
3821 "myapex",
3822 ],
3823 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003824 `)
3825
Sundong Ahnabb64432019-10-22 13:58:29 +09003826 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003827 apexManifestRule := module.Rule("apexManifestRule")
3828 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3829 apexRule := module.Rule("apexRule")
3830 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003831
3832 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003833 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09003834 name := apexBundle.BaseModuleName()
3835 prefix := "TARGET_"
3836 var builder strings.Builder
3837 data.Custom(&builder, name, prefix, "", data)
3838 androidMk := builder.String()
3839 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3840 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003841}
3842
Alex Light0851b882019-02-07 13:20:53 -08003843func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003844 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003845 apex {
3846 name: "myapex",
3847 key: "myapex.key",
3848 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003849 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003850 }
3851
3852 apex_key {
3853 name: "myapex.key",
3854 public_key: "testkey.avbpubkey",
3855 private_key: "testkey.pem",
3856 }
3857
3858 cc_library {
3859 name: "mylib_common",
3860 srcs: ["mylib.cpp"],
3861 system_shared_libs: [],
3862 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003863 apex_available: [
3864 "//apex_available:platform",
3865 "myapex",
3866 ],
Alex Light0851b882019-02-07 13:20:53 -08003867 }
3868 `)
3869
Sundong Ahnabb64432019-10-22 13:58:29 +09003870 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003871 apexRule := module.Rule("apexRule")
3872 copyCmds := apexRule.Args["copy_commands"]
3873
3874 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3875 t.Log("Apex was a test apex!")
3876 t.Fail()
3877 }
3878 // Ensure that main rule creates an output
3879 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3880
3881 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003882 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003883
3884 // Ensure that both direct and indirect deps are copied into apex
3885 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3886
Colin Cross7113d202019-11-20 16:39:12 -08003887 // Ensure that the platform variant ends with _shared
3888 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003889
Colin Cross56a83212020-09-15 18:30:11 -07003890 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003891 t.Log("Found mylib_common not in any apex!")
3892 t.Fail()
3893 }
3894}
3895
3896func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003897 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003898 apex_test {
3899 name: "myapex",
3900 key: "myapex.key",
3901 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003902 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003903 }
3904
3905 apex_key {
3906 name: "myapex.key",
3907 public_key: "testkey.avbpubkey",
3908 private_key: "testkey.pem",
3909 }
3910
3911 cc_library {
3912 name: "mylib_common_test",
3913 srcs: ["mylib.cpp"],
3914 system_shared_libs: [],
3915 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003916 // TODO: remove //apex_available:platform
3917 apex_available: [
3918 "//apex_available:platform",
3919 "myapex",
3920 ],
Alex Light0851b882019-02-07 13:20:53 -08003921 }
3922 `)
3923
Sundong Ahnabb64432019-10-22 13:58:29 +09003924 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003925 apexRule := module.Rule("apexRule")
3926 copyCmds := apexRule.Args["copy_commands"]
3927
3928 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3929 t.Log("Apex was not a test apex!")
3930 t.Fail()
3931 }
3932 // Ensure that main rule creates an output
3933 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3934
3935 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003936 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003937
3938 // Ensure that both direct and indirect deps are copied into apex
3939 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3940
Colin Cross7113d202019-11-20 16:39:12 -08003941 // Ensure that the platform variant ends with _shared
3942 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003943}
3944
Alex Light9670d332019-01-29 18:07:33 -08003945func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003946 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003947 apex {
3948 name: "myapex",
3949 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003950 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08003951 multilib: {
3952 first: {
3953 native_shared_libs: ["mylib_common"],
3954 }
3955 },
3956 target: {
3957 android: {
3958 multilib: {
3959 first: {
3960 native_shared_libs: ["mylib"],
3961 }
3962 }
3963 },
3964 host: {
3965 multilib: {
3966 first: {
3967 native_shared_libs: ["mylib2"],
3968 }
3969 }
3970 }
3971 }
3972 }
3973
3974 apex_key {
3975 name: "myapex.key",
3976 public_key: "testkey.avbpubkey",
3977 private_key: "testkey.pem",
3978 }
3979
3980 cc_library {
3981 name: "mylib",
3982 srcs: ["mylib.cpp"],
3983 system_shared_libs: [],
3984 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003985 // TODO: remove //apex_available:platform
3986 apex_available: [
3987 "//apex_available:platform",
3988 "myapex",
3989 ],
Alex Light9670d332019-01-29 18:07:33 -08003990 }
3991
3992 cc_library {
3993 name: "mylib_common",
3994 srcs: ["mylib.cpp"],
3995 system_shared_libs: [],
3996 stl: "none",
3997 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003998 // TODO: remove //apex_available:platform
3999 apex_available: [
4000 "//apex_available:platform",
4001 "myapex",
4002 ],
Alex Light9670d332019-01-29 18:07:33 -08004003 }
4004
4005 cc_library {
4006 name: "mylib2",
4007 srcs: ["mylib.cpp"],
4008 system_shared_libs: [],
4009 stl: "none",
4010 compile_multilib: "first",
4011 }
4012 `)
4013
Sundong Ahnabb64432019-10-22 13:58:29 +09004014 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004015 copyCmds := apexRule.Args["copy_commands"]
4016
4017 // Ensure that main rule creates an output
4018 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4019
4020 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004021 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4022 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4023 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004024
4025 // Ensure that both direct and indirect deps are copied into apex
4026 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4027 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4028 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4029
Colin Cross7113d202019-11-20 16:39:12 -08004030 // Ensure that the platform variant ends with _shared
4031 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4032 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4033 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004034}
Jiyong Park04480cf2019-02-06 00:16:29 +09004035
Jiyong Park59140302020-12-14 18:44:04 +09004036func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004037 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004038 apex {
4039 name: "myapex",
4040 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004041 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09004042 arch: {
4043 arm64: {
4044 native_shared_libs: ["mylib.arm64"],
4045 },
4046 x86_64: {
4047 native_shared_libs: ["mylib.x64"],
4048 },
4049 }
4050 }
4051
4052 apex_key {
4053 name: "myapex.key",
4054 public_key: "testkey.avbpubkey",
4055 private_key: "testkey.pem",
4056 }
4057
4058 cc_library {
4059 name: "mylib.arm64",
4060 srcs: ["mylib.cpp"],
4061 system_shared_libs: [],
4062 stl: "none",
4063 // TODO: remove //apex_available:platform
4064 apex_available: [
4065 "//apex_available:platform",
4066 "myapex",
4067 ],
4068 }
4069
4070 cc_library {
4071 name: "mylib.x64",
4072 srcs: ["mylib.cpp"],
4073 system_shared_libs: [],
4074 stl: "none",
4075 // TODO: remove //apex_available:platform
4076 apex_available: [
4077 "//apex_available:platform",
4078 "myapex",
4079 ],
4080 }
4081 `)
4082
4083 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4084 copyCmds := apexRule.Args["copy_commands"]
4085
4086 // Ensure that apex variant is created for the direct dep
4087 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4088 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4089
4090 // Ensure that both direct and indirect deps are copied into apex
4091 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4092 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4093}
4094
Jiyong Park04480cf2019-02-06 00:16:29 +09004095func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004096 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004097 apex {
4098 name: "myapex",
4099 key: "myapex.key",
4100 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004101 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004102 }
4103
4104 apex_key {
4105 name: "myapex.key",
4106 public_key: "testkey.avbpubkey",
4107 private_key: "testkey.pem",
4108 }
4109
4110 sh_binary {
4111 name: "myscript",
4112 src: "mylib.cpp",
4113 filename: "myscript.sh",
4114 sub_dir: "script",
4115 }
4116 `)
4117
Sundong Ahnabb64432019-10-22 13:58:29 +09004118 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004119 copyCmds := apexRule.Args["copy_commands"]
4120
4121 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4122}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004123
Jooyung Han91df2082019-11-20 01:49:42 +09004124func TestApexInVariousPartition(t *testing.T) {
4125 testcases := []struct {
4126 propName, parition, flattenedPartition string
4127 }{
4128 {"", "system", "system_ext"},
4129 {"product_specific: true", "product", "product"},
4130 {"soc_specific: true", "vendor", "vendor"},
4131 {"proprietary: true", "vendor", "vendor"},
4132 {"vendor: true", "vendor", "vendor"},
4133 {"system_ext_specific: true", "system_ext", "system_ext"},
4134 }
4135 for _, tc := range testcases {
4136 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004137 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004138 apex {
4139 name: "myapex",
4140 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004141 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004142 `+tc.propName+`
4143 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004144
Jooyung Han91df2082019-11-20 01:49:42 +09004145 apex_key {
4146 name: "myapex.key",
4147 public_key: "testkey.avbpubkey",
4148 private_key: "testkey.pem",
4149 }
4150 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004151
Jooyung Han91df2082019-11-20 01:49:42 +09004152 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004153 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4154 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004155 if actual != expected {
4156 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4157 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004158
Jooyung Han91df2082019-11-20 01:49:42 +09004159 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004160 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4161 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004162 if actual != expected {
4163 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4164 }
4165 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004166 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004167}
Jiyong Park67882562019-03-21 01:11:21 +09004168
Jooyung Han580eb4f2020-06-24 19:33:06 +09004169func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004170 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004171 apex {
4172 name: "myapex",
4173 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004174 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004175 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004176
Jooyung Han580eb4f2020-06-24 19:33:06 +09004177 apex_key {
4178 name: "myapex.key",
4179 public_key: "testkey.avbpubkey",
4180 private_key: "testkey.pem",
4181 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004182 `)
4183 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004184 rule := module.Output("file_contexts")
4185 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4186}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004187
Jooyung Han580eb4f2020-06-24 19:33:06 +09004188func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004189 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004190 apex {
4191 name: "myapex",
4192 key: "myapex.key",
4193 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004194 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004195 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004196
Jooyung Han580eb4f2020-06-24 19:33:06 +09004197 apex_key {
4198 name: "myapex.key",
4199 public_key: "testkey.avbpubkey",
4200 private_key: "testkey.pem",
4201 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004202 `, withFiles(map[string][]byte{
4203 "my_own_file_contexts": nil,
4204 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004205}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004206
Jooyung Han580eb4f2020-06-24 19:33:06 +09004207func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004208 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004209 apex {
4210 name: "myapex",
4211 key: "myapex.key",
4212 product_specific: true,
4213 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004214 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004215 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004216
Jooyung Han580eb4f2020-06-24 19:33:06 +09004217 apex_key {
4218 name: "myapex.key",
4219 public_key: "testkey.avbpubkey",
4220 private_key: "testkey.pem",
4221 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004222 `)
4223
Colin Cross1c460562021-02-16 17:55:47 -08004224 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004225 apex {
4226 name: "myapex",
4227 key: "myapex.key",
4228 product_specific: true,
4229 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004230 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004231 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004232
Jooyung Han580eb4f2020-06-24 19:33:06 +09004233 apex_key {
4234 name: "myapex.key",
4235 public_key: "testkey.avbpubkey",
4236 private_key: "testkey.pem",
4237 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004238 `, withFiles(map[string][]byte{
4239 "product_specific_file_contexts": nil,
4240 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004241 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4242 rule := module.Output("file_contexts")
4243 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4244}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004245
Jooyung Han580eb4f2020-06-24 19:33:06 +09004246func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004247 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004248 apex {
4249 name: "myapex",
4250 key: "myapex.key",
4251 product_specific: true,
4252 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004253 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004254 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004255
Jooyung Han580eb4f2020-06-24 19:33:06 +09004256 apex_key {
4257 name: "myapex.key",
4258 public_key: "testkey.avbpubkey",
4259 private_key: "testkey.pem",
4260 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004261
Jooyung Han580eb4f2020-06-24 19:33:06 +09004262 filegroup {
4263 name: "my-file-contexts",
4264 srcs: ["product_specific_file_contexts"],
4265 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004266 `, withFiles(map[string][]byte{
4267 "product_specific_file_contexts": nil,
4268 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004269 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4270 rule := module.Output("file_contexts")
4271 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004272}
4273
Jiyong Park67882562019-03-21 01:11:21 +09004274func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004275 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004276 apex_key {
4277 name: "myapex.key",
4278 public_key: ":my.avbpubkey",
4279 private_key: ":my.pem",
4280 product_specific: true,
4281 }
4282
4283 filegroup {
4284 name: "my.avbpubkey",
4285 srcs: ["testkey2.avbpubkey"],
4286 }
4287
4288 filegroup {
4289 name: "my.pem",
4290 srcs: ["testkey2.pem"],
4291 }
4292 `)
4293
4294 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4295 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004296 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004297 if actual_pubkey != expected_pubkey {
4298 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4299 }
4300 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004301 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004302 if actual_privkey != expected_privkey {
4303 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4304 }
4305}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004306
4307func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004308 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004309 prebuilt_apex {
4310 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004311 arch: {
4312 arm64: {
4313 src: "myapex-arm64.apex",
4314 },
4315 arm: {
4316 src: "myapex-arm.apex",
4317 },
4318 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004319 }
4320 `)
4321
4322 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4323
Jiyong Parkc95714e2019-03-29 14:23:10 +09004324 expectedInput := "myapex-arm64.apex"
4325 if prebuilt.inputApex.String() != expectedInput {
4326 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4327 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004328}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004329
Paul Duffinc0609c62021-03-01 17:27:16 +00004330func TestPrebuiltMissingSrc(t *testing.T) {
4331 testApexError(t, `module "myapex" variant "android_common".*: prebuilt_apex does not support "arm64_armv8-a"`, `
4332 prebuilt_apex {
4333 name: "myapex",
4334 }
4335 `)
4336}
4337
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004338func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004339 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004340 prebuilt_apex {
4341 name: "myapex",
4342 src: "myapex-arm.apex",
4343 filename: "notmyapex.apex",
4344 }
4345 `)
4346
4347 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4348
4349 expected := "notmyapex.apex"
4350 if p.installFilename != expected {
4351 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4352 }
4353}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004354
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004355func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004356 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004357 prebuilt_apex {
4358 name: "myapex.prebuilt",
4359 src: "myapex-arm.apex",
4360 overrides: [
4361 "myapex",
4362 ],
4363 }
4364 `)
4365
4366 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
4367
4368 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004369 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004370 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004371 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004372 }
4373}
4374
Paul Duffin092153d2021-01-26 11:42:39 +00004375// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4376// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004377func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004378 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004379
Paul Duffin89886cb2021-02-05 16:44:03 +00004380 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004381 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004382 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004383 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004384 stem := android.RemoveOptionalPrebuiltPrefix(name)
4385 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004386 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4387 }
4388 }
4389
Paul Duffin39853512021-02-26 11:09:39 +00004390 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004391 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004392 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004393 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4394 }
4395 }
4396
4397 t.Run("prebuilt only", func(t *testing.T) {
4398 bp := `
4399 prebuilt_apex {
4400 name: "myapex",
4401 arch: {
4402 arm64: {
4403 src: "myapex-arm64.apex",
4404 },
4405 arm: {
4406 src: "myapex-arm.apex",
4407 },
4408 },
Paul Duffin39853512021-02-26 11:09:39 +00004409 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004410 }
4411
4412 java_import {
4413 name: "libfoo",
4414 jars: ["libfoo.jar"],
4415 }
Paul Duffin39853512021-02-26 11:09:39 +00004416
4417 java_sdk_library_import {
4418 name: "libbar",
4419 public: {
4420 jars: ["libbar.jar"],
4421 },
4422 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004423 `
4424
4425 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4426 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4427
Paul Duffinf6932af2021-02-26 18:21:56 +00004428 // Make sure that the deapexer has the correct input APEX.
4429 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4430 rule := deapexer.Rule("deapexer")
4431 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4432 t.Errorf("expected: %q, found: %q", expected, actual)
4433 }
4434
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004435 // Make sure that the prebuilt_apex has the correct input APEX.
4436 prebuiltApex := ctx.ModuleForTests("myapex", "android_common")
4437 rule = prebuiltApex.Rule("android/soong/android.Cp")
4438 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4439 t.Errorf("expected: %q, found: %q", expected, actual)
4440 }
4441
Paul Duffin89886cb2021-02-05 16:44:03 +00004442 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004443
4444 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004445 })
4446
4447 t.Run("prebuilt with source preferred", func(t *testing.T) {
4448
4449 bp := `
4450 prebuilt_apex {
4451 name: "myapex",
4452 arch: {
4453 arm64: {
4454 src: "myapex-arm64.apex",
4455 },
4456 arm: {
4457 src: "myapex-arm.apex",
4458 },
4459 },
Paul Duffin39853512021-02-26 11:09:39 +00004460 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004461 }
4462
4463 java_import {
4464 name: "libfoo",
4465 jars: ["libfoo.jar"],
4466 }
4467
4468 java_library {
4469 name: "libfoo",
4470 }
Paul Duffin39853512021-02-26 11:09:39 +00004471
4472 java_sdk_library_import {
4473 name: "libbar",
4474 public: {
4475 jars: ["libbar.jar"],
4476 },
4477 }
4478
4479 java_sdk_library {
4480 name: "libbar",
4481 srcs: ["foo/bar/MyClass.java"],
4482 unsafe_ignore_missing_latest_api: true,
4483 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004484 `
4485
4486 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4487 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4488
Paul Duffin89886cb2021-02-05 16:44:03 +00004489 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004490 ensureNoSourceVariant(t, ctx, "libfoo")
4491
4492 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4493 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004494 })
4495
4496 t.Run("prebuilt preferred with source", func(t *testing.T) {
4497 bp := `
4498 prebuilt_apex {
4499 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004500 arch: {
4501 arm64: {
4502 src: "myapex-arm64.apex",
4503 },
4504 arm: {
4505 src: "myapex-arm.apex",
4506 },
4507 },
Paul Duffin39853512021-02-26 11:09:39 +00004508 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004509 }
4510
4511 java_import {
4512 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004513 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004514 jars: ["libfoo.jar"],
4515 }
4516
4517 java_library {
4518 name: "libfoo",
4519 }
Paul Duffin39853512021-02-26 11:09:39 +00004520
4521 java_sdk_library_import {
4522 name: "libbar",
4523 prefer: true,
4524 public: {
4525 jars: ["libbar.jar"],
4526 },
4527 }
4528
4529 java_sdk_library {
4530 name: "libbar",
4531 srcs: ["foo/bar/MyClass.java"],
4532 unsafe_ignore_missing_latest_api: true,
4533 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004534 `
4535
4536 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4537 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4538
Paul Duffin89886cb2021-02-05 16:44:03 +00004539 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004540 ensureNoSourceVariant(t, ctx, "libfoo")
4541
4542 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4543 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004544 })
4545}
4546
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004547func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004548 preparer := java.FixtureConfigureBootJars("myapex:libfoo", "myapex:libbar")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004549
Paul Duffin37856732021-02-26 14:24:15 +00004550 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4551 t.Helper()
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004552 s := ctx.SingletonForTests("dex_bootjars")
4553 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004554 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004555 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004556 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004557 foundLibfooJar = true
4558 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01004559 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004560 }
4561 }
4562 if !foundLibfooJar {
Paul Duffin55607122021-03-30 23:32:51 +01004563 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 +00004564 }
4565 }
4566
Paul Duffin4fd997b2021-02-03 20:06:33 +00004567 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004568 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01004569 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
4570 indexRule := platformBootclasspath.Rule("platform-bootclasspath-monolithic-hiddenapi-index")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004571 java.CheckHiddenAPIRuleInputs(t, expectedInputs, indexRule)
4572 }
4573
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004574 t.Run("prebuilt only", func(t *testing.T) {
4575 bp := `
4576 prebuilt_apex {
4577 name: "myapex",
4578 arch: {
4579 arm64: {
4580 src: "myapex-arm64.apex",
4581 },
4582 arm: {
4583 src: "myapex-arm.apex",
4584 },
4585 },
Paul Duffin37856732021-02-26 14:24:15 +00004586 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004587 }
4588
4589 java_import {
4590 name: "libfoo",
4591 jars: ["libfoo.jar"],
4592 apex_available: ["myapex"],
4593 }
Paul Duffin37856732021-02-26 14:24:15 +00004594
4595 java_sdk_library_import {
4596 name: "libbar",
4597 public: {
4598 jars: ["libbar.jar"],
4599 },
4600 apex_available: ["myapex"],
4601 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004602 `
4603
Paul Duffin60264a02021-04-12 20:02:36 +01004604 ctx := testDexpreoptWithApexes(t, bp, "", preparer)
Paul Duffin55607122021-03-30 23:32:51 +01004605 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4606 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004607
Paul Duffin9d67ca62021-02-03 20:06:33 +00004608 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4609 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004610.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004611.intermediates/libfoo/android_common_myapex/hiddenapi/index.csv
4612`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004613 })
4614
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004615 t.Run("apex_set only", func(t *testing.T) {
4616 bp := `
4617 apex_set {
4618 name: "myapex",
4619 set: "myapex.apks",
4620 exported_java_libs: ["libfoo", "libbar"],
4621 }
4622
4623 java_import {
4624 name: "libfoo",
4625 jars: ["libfoo.jar"],
4626 apex_available: ["myapex"],
4627 }
4628
4629 java_sdk_library_import {
4630 name: "libbar",
4631 public: {
4632 jars: ["libbar.jar"],
4633 },
4634 apex_available: ["myapex"],
4635 }
4636 `
4637
Paul Duffin60264a02021-04-12 20:02:36 +01004638 ctx := testDexpreoptWithApexes(t, bp, "", preparer)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004639 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4640 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
4641
4642 // Make sure that the dex file from the apex_set contributes to the hiddenapi index file.
4643 checkHiddenAPIIndexInputs(t, ctx, `
4644.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
4645.intermediates/libfoo/android_common_myapex/hiddenapi/index.csv
4646`)
4647 })
4648
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004649 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4650 bp := `
4651 prebuilt_apex {
4652 name: "myapex",
4653 arch: {
4654 arm64: {
4655 src: "myapex-arm64.apex",
4656 },
4657 arm: {
4658 src: "myapex-arm.apex",
4659 },
4660 },
Paul Duffin37856732021-02-26 14:24:15 +00004661 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004662 }
4663
4664 java_import {
4665 name: "libfoo",
4666 jars: ["libfoo.jar"],
4667 apex_available: ["myapex"],
4668 }
4669
4670 java_library {
4671 name: "libfoo",
4672 srcs: ["foo/bar/MyClass.java"],
4673 apex_available: ["myapex"],
4674 }
Paul Duffin37856732021-02-26 14:24:15 +00004675
4676 java_sdk_library_import {
4677 name: "libbar",
4678 public: {
4679 jars: ["libbar.jar"],
4680 },
4681 apex_available: ["myapex"],
4682 }
4683
4684 java_sdk_library {
4685 name: "libbar",
4686 srcs: ["foo/bar/MyClass.java"],
4687 unsafe_ignore_missing_latest_api: true,
4688 apex_available: ["myapex"],
4689 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004690 `
4691
4692 // In this test the source (java_library) libfoo is active since the
4693 // prebuilt (java_import) defaults to prefer:false. However the
4694 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4695 // find the dex boot jar in it. We either need to disable the source libfoo
4696 // or make the prebuilt libfoo preferred.
Paul Duffin60264a02021-04-12 20:02:36 +01004697 testDexpreoptWithApexes(t, bp, "failed to find a dex jar path for module 'libfoo'", preparer)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004698 })
4699
4700 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4701 bp := `
4702 prebuilt_apex {
4703 name: "myapex",
4704 arch: {
4705 arm64: {
4706 src: "myapex-arm64.apex",
4707 },
4708 arm: {
4709 src: "myapex-arm.apex",
4710 },
4711 },
Paul Duffin37856732021-02-26 14:24:15 +00004712 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004713 }
4714
4715 java_import {
4716 name: "libfoo",
4717 prefer: true,
4718 jars: ["libfoo.jar"],
4719 apex_available: ["myapex"],
4720 }
4721
4722 java_library {
4723 name: "libfoo",
4724 srcs: ["foo/bar/MyClass.java"],
4725 apex_available: ["myapex"],
4726 }
Paul Duffin37856732021-02-26 14:24:15 +00004727
4728 java_sdk_library_import {
4729 name: "libbar",
4730 prefer: true,
4731 public: {
4732 jars: ["libbar.jar"],
4733 },
4734 apex_available: ["myapex"],
4735 }
4736
4737 java_sdk_library {
4738 name: "libbar",
4739 srcs: ["foo/bar/MyClass.java"],
4740 unsafe_ignore_missing_latest_api: true,
4741 apex_available: ["myapex"],
4742 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004743 `
4744
Paul Duffin60264a02021-04-12 20:02:36 +01004745 ctx := testDexpreoptWithApexes(t, bp, "", preparer)
Paul Duffin55607122021-03-30 23:32:51 +01004746 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4747 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004748
Paul Duffin9d67ca62021-02-03 20:06:33 +00004749 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4750 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004751.intermediates/prebuilt_libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004752.intermediates/prebuilt_libfoo/android_common_myapex/hiddenapi/index.csv
4753`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004754 })
4755
4756 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4757 bp := `
4758 apex {
4759 name: "myapex",
4760 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004761 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004762 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004763 }
4764
4765 apex_key {
4766 name: "myapex.key",
4767 public_key: "testkey.avbpubkey",
4768 private_key: "testkey.pem",
4769 }
4770
4771 prebuilt_apex {
4772 name: "myapex",
4773 arch: {
4774 arm64: {
4775 src: "myapex-arm64.apex",
4776 },
4777 arm: {
4778 src: "myapex-arm.apex",
4779 },
4780 },
Paul Duffin37856732021-02-26 14:24:15 +00004781 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004782 }
4783
4784 java_import {
4785 name: "libfoo",
4786 jars: ["libfoo.jar"],
4787 apex_available: ["myapex"],
4788 }
4789
4790 java_library {
4791 name: "libfoo",
4792 srcs: ["foo/bar/MyClass.java"],
4793 apex_available: ["myapex"],
4794 }
Paul Duffin37856732021-02-26 14:24:15 +00004795
4796 java_sdk_library_import {
4797 name: "libbar",
4798 public: {
4799 jars: ["libbar.jar"],
4800 },
4801 apex_available: ["myapex"],
4802 }
4803
4804 java_sdk_library {
4805 name: "libbar",
4806 srcs: ["foo/bar/MyClass.java"],
4807 unsafe_ignore_missing_latest_api: true,
4808 apex_available: ["myapex"],
4809 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004810 `
4811
Paul Duffin60264a02021-04-12 20:02:36 +01004812 ctx := testDexpreoptWithApexes(t, bp, "", preparer)
Paul Duffin55607122021-03-30 23:32:51 +01004813 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4814 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004815
4816 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4817 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004818.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin4fd997b2021-02-03 20:06:33 +00004819.intermediates/libfoo/android_common_apex10000/hiddenapi/index.csv
4820`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004821 })
4822
4823 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4824 bp := `
4825 apex {
4826 name: "myapex",
4827 enabled: false,
4828 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01004829 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004830 }
4831
4832 apex_key {
4833 name: "myapex.key",
4834 public_key: "testkey.avbpubkey",
4835 private_key: "testkey.pem",
4836 }
4837
4838 prebuilt_apex {
4839 name: "myapex",
4840 arch: {
4841 arm64: {
4842 src: "myapex-arm64.apex",
4843 },
4844 arm: {
4845 src: "myapex-arm.apex",
4846 },
4847 },
Paul Duffin37856732021-02-26 14:24:15 +00004848 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004849 }
4850
4851 java_import {
4852 name: "libfoo",
4853 prefer: true,
4854 jars: ["libfoo.jar"],
4855 apex_available: ["myapex"],
4856 }
4857
4858 java_library {
4859 name: "libfoo",
4860 srcs: ["foo/bar/MyClass.java"],
4861 apex_available: ["myapex"],
4862 }
Paul Duffin37856732021-02-26 14:24:15 +00004863
4864 java_sdk_library_import {
4865 name: "libbar",
4866 prefer: true,
4867 public: {
4868 jars: ["libbar.jar"],
4869 },
4870 apex_available: ["myapex"],
4871 }
4872
4873 java_sdk_library {
4874 name: "libbar",
4875 srcs: ["foo/bar/MyClass.java"],
4876 unsafe_ignore_missing_latest_api: true,
4877 apex_available: ["myapex"],
4878 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004879 `
4880
Paul Duffin60264a02021-04-12 20:02:36 +01004881 ctx := testDexpreoptWithApexes(t, bp, "", preparer)
Paul Duffin55607122021-03-30 23:32:51 +01004882 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4883 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004884
Paul Duffin9d67ca62021-02-03 20:06:33 +00004885 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4886 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin8f146b92021-04-12 17:24:18 +01004887.intermediates/prebuilt_libbar/android_common_myapex/hiddenapi/index.csv
4888.intermediates/prebuilt_libfoo/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004889`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004890 })
4891}
4892
Roland Levillain630846d2019-06-26 12:48:34 +01004893func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004894 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004895 apex_test {
4896 name: "myapex",
4897 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004898 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01004899 tests: [
4900 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004901 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004902 ],
4903 }
4904
4905 apex_key {
4906 name: "myapex.key",
4907 public_key: "testkey.avbpubkey",
4908 private_key: "testkey.pem",
4909 }
4910
Liz Kammer1c14a212020-05-12 15:26:55 -07004911 filegroup {
4912 name: "fg",
4913 srcs: [
4914 "baz",
4915 "bar/baz"
4916 ],
4917 }
4918
Roland Levillain630846d2019-06-26 12:48:34 +01004919 cc_test {
4920 name: "mytest",
4921 gtest: false,
4922 srcs: ["mytest.cpp"],
4923 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004924 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004925 system_shared_libs: [],
4926 static_executable: true,
4927 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004928 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004929 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004930
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004931 cc_library {
4932 name: "mylib",
4933 srcs: ["mylib.cpp"],
4934 system_shared_libs: [],
4935 stl: "none",
4936 }
4937
Liz Kammer5bd365f2020-05-27 15:15:11 -07004938 filegroup {
4939 name: "fg2",
4940 srcs: [
4941 "testdata/baz"
4942 ],
4943 }
4944
Roland Levillain9b5fde92019-06-28 15:41:19 +01004945 cc_test {
4946 name: "mytests",
4947 gtest: false,
4948 srcs: [
4949 "mytest1.cpp",
4950 "mytest2.cpp",
4951 "mytest3.cpp",
4952 ],
4953 test_per_src: true,
4954 relative_install_path: "test",
4955 system_shared_libs: [],
4956 static_executable: true,
4957 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004958 data: [
4959 ":fg",
4960 ":fg2",
4961 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004962 }
Roland Levillain630846d2019-06-26 12:48:34 +01004963 `)
4964
Sundong Ahnabb64432019-10-22 13:58:29 +09004965 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004966 copyCmds := apexRule.Args["copy_commands"]
4967
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004968 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004969 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004970 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004971
Liz Kammer1c14a212020-05-12 15:26:55 -07004972 //Ensure that test data are copied into apex.
4973 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4974 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4975
Roland Levillain9b5fde92019-06-28 15:41:19 +01004976 // Ensure that test deps built with `test_per_src` are copied into apex.
4977 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4978 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4979 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004980
4981 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004982 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004983 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004984 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004985 prefix := "TARGET_"
4986 var builder strings.Builder
4987 data.Custom(&builder, name, prefix, "", data)
4988 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004989 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4990 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4991 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4992 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004993 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004994 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004995 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004996
4997 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004998 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004999 data.Custom(&builder, name, prefix, "", data)
5000 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005001 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5002 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005003}
5004
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005005func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005006 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005007 apex {
5008 name: "myapex",
5009 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005010 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005011 }
5012 apex_key {
5013 name: "myapex.key",
5014 public_key: "testkey.avbpubkey",
5015 private_key: "testkey.pem",
5016 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005017 `,
5018 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5019 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5020 }),
5021 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005022 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09005023 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005024 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005025 var builder strings.Builder
5026 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5027 androidMk := builder.String()
5028 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
5029}
5030
Jooyung Hand48f3c32019-08-23 11:18:57 +09005031func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5032 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5033 apex {
5034 name: "myapex",
5035 key: "myapex.key",
5036 native_shared_libs: ["libfoo"],
5037 }
5038
5039 apex_key {
5040 name: "myapex.key",
5041 public_key: "testkey.avbpubkey",
5042 private_key: "testkey.pem",
5043 }
5044
5045 cc_library {
5046 name: "libfoo",
5047 stl: "none",
5048 system_shared_libs: [],
5049 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005050 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005051 }
5052 `)
5053 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5054 apex {
5055 name: "myapex",
5056 key: "myapex.key",
5057 java_libs: ["myjar"],
5058 }
5059
5060 apex_key {
5061 name: "myapex.key",
5062 public_key: "testkey.avbpubkey",
5063 private_key: "testkey.pem",
5064 }
5065
5066 java_library {
5067 name: "myjar",
5068 srcs: ["foo/bar/MyClass.java"],
5069 sdk_version: "none",
5070 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005071 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005072 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005073 }
5074 `)
5075}
5076
Bill Peckhama41a6962021-01-11 10:58:54 -08005077func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005078 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005079 apex {
5080 name: "myapex",
5081 key: "myapex.key",
5082 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005083 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005084 }
5085
5086 apex_key {
5087 name: "myapex.key",
5088 public_key: "testkey.avbpubkey",
5089 private_key: "testkey.pem",
5090 }
5091
5092 java_import {
5093 name: "myjavaimport",
5094 apex_available: ["myapex"],
5095 jars: ["my.jar"],
5096 compile_dex: true,
5097 }
5098 `)
5099
5100 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5101 apexRule := module.Rule("apexRule")
5102 copyCmds := apexRule.Args["copy_commands"]
5103 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5104}
5105
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005106func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005107 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005108 apex {
5109 name: "myapex",
5110 key: "myapex.key",
5111 apps: [
5112 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005113 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005114 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005115 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005116 }
5117
5118 apex_key {
5119 name: "myapex.key",
5120 public_key: "testkey.avbpubkey",
5121 private_key: "testkey.pem",
5122 }
5123
5124 android_app {
5125 name: "AppFoo",
5126 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005127 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005128 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005129 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005130 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005131 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005132 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005133
5134 android_app {
5135 name: "AppFooPriv",
5136 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005137 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005138 system_modules: "none",
5139 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005140 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005141 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005142 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005143
5144 cc_library_shared {
5145 name: "libjni",
5146 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005147 shared_libs: ["libfoo"],
5148 stl: "none",
5149 system_shared_libs: [],
5150 apex_available: [ "myapex" ],
5151 sdk_version: "current",
5152 }
5153
5154 cc_library_shared {
5155 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005156 stl: "none",
5157 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005158 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005159 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005160 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005161 `)
5162
Sundong Ahnabb64432019-10-22 13:58:29 +09005163 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005164 apexRule := module.Rule("apexRule")
5165 copyCmds := apexRule.Args["copy_commands"]
5166
5167 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005168 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005169
Colin Crossaede88c2020-08-11 12:17:01 -07005170 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005171 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005172 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005173 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005174 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005175 // JNI libraries including transitive deps are
5176 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005177 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005178 // ... embedded inside APK (jnilibs.zip)
5179 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5180 // ... and not directly inside the APEX
5181 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5182 }
Dario Frenicde2a032019-10-27 00:29:22 +01005183}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005184
Dario Frenicde2a032019-10-27 00:29:22 +01005185func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005186 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005187 apex {
5188 name: "myapex",
5189 key: "myapex.key",
5190 apps: [
5191 "AppFooPrebuilt",
5192 "AppFooPrivPrebuilt",
5193 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005194 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005195 }
5196
5197 apex_key {
5198 name: "myapex.key",
5199 public_key: "testkey.avbpubkey",
5200 private_key: "testkey.pem",
5201 }
5202
5203 android_app_import {
5204 name: "AppFooPrebuilt",
5205 apk: "PrebuiltAppFoo.apk",
5206 presigned: true,
5207 dex_preopt: {
5208 enabled: false,
5209 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005210 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005211 }
5212
5213 android_app_import {
5214 name: "AppFooPrivPrebuilt",
5215 apk: "PrebuiltAppFooPriv.apk",
5216 privileged: true,
5217 presigned: true,
5218 dex_preopt: {
5219 enabled: false,
5220 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005221 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005222 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005223 }
5224 `)
5225
Sundong Ahnabb64432019-10-22 13:58:29 +09005226 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005227 apexRule := module.Rule("apexRule")
5228 copyCmds := apexRule.Args["copy_commands"]
5229
5230 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005231 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5232}
5233
5234func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005235 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005236 apex {
5237 name: "myapex",
5238 key: "myapex.key",
5239 apps: [
5240 "AppFoo",
5241 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005242 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005243 }
5244
5245 apex_key {
5246 name: "myapex.key",
5247 public_key: "testkey.avbpubkey",
5248 private_key: "testkey.pem",
5249 }
5250
5251 android_app {
5252 name: "AppFoo",
5253 srcs: ["foo/bar/MyClass.java"],
5254 sdk_version: "none",
5255 system_modules: "none",
5256 apex_available: [ "myapex" ],
5257 }
5258
5259 android_app_import {
5260 name: "AppFoo",
5261 apk: "AppFooPrebuilt.apk",
5262 filename: "AppFooPrebuilt.apk",
5263 presigned: true,
5264 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005265 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005266 }
5267 `, withFiles(map[string][]byte{
5268 "AppFooPrebuilt.apk": nil,
5269 }))
5270
5271 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005272 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005273 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005274}
5275
Dario Freni6f3937c2019-12-20 22:58:03 +00005276func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005277 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005278 apex {
5279 name: "myapex",
5280 key: "myapex.key",
5281 apps: [
5282 "TesterHelpAppFoo",
5283 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005284 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005285 }
5286
5287 apex_key {
5288 name: "myapex.key",
5289 public_key: "testkey.avbpubkey",
5290 private_key: "testkey.pem",
5291 }
5292
5293 android_test_helper_app {
5294 name: "TesterHelpAppFoo",
5295 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005296 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005297 }
5298
5299 `)
5300
5301 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5302 apexRule := module.Rule("apexRule")
5303 copyCmds := apexRule.Args["copy_commands"]
5304
5305 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5306}
5307
Jooyung Han18020ea2019-11-13 10:50:48 +09005308func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5309 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005310 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005311 apex {
5312 name: "myapex",
5313 key: "myapex.key",
5314 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005315 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005316 }
5317
5318 apex_key {
5319 name: "myapex.key",
5320 public_key: "testkey.avbpubkey",
5321 private_key: "testkey.pem",
5322 }
5323
5324 apex {
5325 name: "otherapex",
5326 key: "myapex.key",
5327 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005328 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005329 }
5330
5331 cc_defaults {
5332 name: "libfoo-defaults",
5333 apex_available: ["otherapex"],
5334 }
5335
5336 cc_library {
5337 name: "libfoo",
5338 defaults: ["libfoo-defaults"],
5339 stl: "none",
5340 system_shared_libs: [],
5341 }`)
5342}
5343
Paul Duffine52e66f2020-03-30 17:54:29 +01005344func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005345 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005346 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005347 apex {
5348 name: "myapex",
5349 key: "myapex.key",
5350 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005351 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005352 }
5353
5354 apex_key {
5355 name: "myapex.key",
5356 public_key: "testkey.avbpubkey",
5357 private_key: "testkey.pem",
5358 }
5359
5360 apex {
5361 name: "otherapex",
5362 key: "otherapex.key",
5363 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005364 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005365 }
5366
5367 apex_key {
5368 name: "otherapex.key",
5369 public_key: "testkey.avbpubkey",
5370 private_key: "testkey.pem",
5371 }
5372
5373 cc_library {
5374 name: "libfoo",
5375 stl: "none",
5376 system_shared_libs: [],
5377 apex_available: ["otherapex"],
5378 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005379}
Jiyong Park127b40b2019-09-30 16:04:35 +09005380
Paul Duffine52e66f2020-03-30 17:54:29 +01005381func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005382 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005383 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005384.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005385.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005386.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005387.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005388.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005389.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005390 apex {
5391 name: "myapex",
5392 key: "myapex.key",
5393 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005394 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005395 }
5396
5397 apex_key {
5398 name: "myapex.key",
5399 public_key: "testkey.avbpubkey",
5400 private_key: "testkey.pem",
5401 }
5402
Jiyong Park127b40b2019-09-30 16:04:35 +09005403 cc_library {
5404 name: "libfoo",
5405 stl: "none",
5406 shared_libs: ["libbar"],
5407 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005408 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005409 }
5410
5411 cc_library {
5412 name: "libbar",
5413 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005414 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005415 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005416 apex_available: ["myapex"],
5417 }
5418
5419 cc_library {
5420 name: "libbaz",
5421 stl: "none",
5422 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005423 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005424}
Jiyong Park127b40b2019-09-30 16:04:35 +09005425
Paul Duffine52e66f2020-03-30 17:54:29 +01005426func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005427 testApexError(t, "\"otherapex\" is not a valid module name", `
5428 apex {
5429 name: "myapex",
5430 key: "myapex.key",
5431 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005432 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005433 }
5434
5435 apex_key {
5436 name: "myapex.key",
5437 public_key: "testkey.avbpubkey",
5438 private_key: "testkey.pem",
5439 }
5440
5441 cc_library {
5442 name: "libfoo",
5443 stl: "none",
5444 system_shared_libs: [],
5445 apex_available: ["otherapex"],
5446 }`)
5447
Paul Duffine52e66f2020-03-30 17:54:29 +01005448 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005449 apex {
5450 name: "myapex",
5451 key: "myapex.key",
5452 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005453 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005454 }
5455
5456 apex_key {
5457 name: "myapex.key",
5458 public_key: "testkey.avbpubkey",
5459 private_key: "testkey.pem",
5460 }
5461
5462 cc_library {
5463 name: "libfoo",
5464 stl: "none",
5465 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005466 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005467 apex_available: ["myapex"],
5468 }
5469
5470 cc_library {
5471 name: "libbar",
5472 stl: "none",
5473 system_shared_libs: [],
5474 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005475 }
5476
5477 cc_library {
5478 name: "libbaz",
5479 stl: "none",
5480 system_shared_libs: [],
5481 stubs: {
5482 versions: ["10", "20", "30"],
5483 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005484 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005485}
Jiyong Park127b40b2019-09-30 16:04:35 +09005486
Jiyong Park89e850a2020-04-07 16:37:39 +09005487func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005488 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005489 apex {
5490 name: "myapex",
5491 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005492 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005493 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005494 }
5495
5496 apex_key {
5497 name: "myapex.key",
5498 public_key: "testkey.avbpubkey",
5499 private_key: "testkey.pem",
5500 }
5501
5502 cc_library {
5503 name: "libfoo",
5504 stl: "none",
5505 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005506 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005507 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005508 }
5509
5510 cc_library {
5511 name: "libfoo2",
5512 stl: "none",
5513 system_shared_libs: [],
5514 shared_libs: ["libbaz"],
5515 apex_available: ["//apex_available:platform"],
5516 }
5517
5518 cc_library {
5519 name: "libbar",
5520 stl: "none",
5521 system_shared_libs: [],
5522 apex_available: ["myapex"],
5523 }
5524
5525 cc_library {
5526 name: "libbaz",
5527 stl: "none",
5528 system_shared_libs: [],
5529 apex_available: ["myapex"],
5530 stubs: {
5531 versions: ["1"],
5532 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005533 }`)
5534
Jiyong Park89e850a2020-04-07 16:37:39 +09005535 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5536 // because it depends on libbar which isn't available to platform
5537 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5538 if libfoo.NotAvailableForPlatform() != true {
5539 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5540 }
5541
5542 // libfoo2 however can be available to platform because it depends on libbaz which provides
5543 // stubs
5544 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5545 if libfoo2.NotAvailableForPlatform() == true {
5546 t.Errorf("%q should be available to platform", libfoo2.String())
5547 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005548}
Jiyong Parka90ca002019-10-07 15:47:24 +09005549
Paul Duffine52e66f2020-03-30 17:54:29 +01005550func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005551 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005552 apex {
5553 name: "myapex",
5554 key: "myapex.key",
5555 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005556 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005557 }
5558
5559 apex_key {
5560 name: "myapex.key",
5561 public_key: "testkey.avbpubkey",
5562 private_key: "testkey.pem",
5563 }
5564
5565 cc_library {
5566 name: "libfoo",
5567 stl: "none",
5568 system_shared_libs: [],
5569 apex_available: ["myapex"],
5570 static: {
5571 apex_available: ["//apex_available:platform"],
5572 },
5573 }`)
5574
Jiyong Park89e850a2020-04-07 16:37:39 +09005575 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5576 if libfooShared.NotAvailableForPlatform() != true {
5577 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5578 }
5579 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5580 if libfooStatic.NotAvailableForPlatform() != false {
5581 t.Errorf("%q should be available to platform", libfooStatic.String())
5582 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005583}
5584
Jiyong Park5d790c32019-11-15 18:40:32 +09005585func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005586 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005587 apex {
5588 name: "myapex",
5589 key: "myapex.key",
5590 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005591 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005592 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005593 }
5594
5595 override_apex {
5596 name: "override_myapex",
5597 base: "myapex",
5598 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005599 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005600 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005601 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09005602 }
5603
5604 apex_key {
5605 name: "myapex.key",
5606 public_key: "testkey.avbpubkey",
5607 private_key: "testkey.pem",
5608 }
5609
5610 android_app {
5611 name: "app",
5612 srcs: ["foo/bar/MyClass.java"],
5613 package_name: "foo",
5614 sdk_version: "none",
5615 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005616 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005617 }
5618
5619 override_android_app {
5620 name: "override_app",
5621 base: "app",
5622 package_name: "bar",
5623 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005624 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005625
Jiyong Park317645e2019-12-05 13:20:58 +09005626 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5627 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5628 if originalVariant.GetOverriddenBy() != "" {
5629 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5630 }
5631 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5632 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5633 }
5634
Jiyong Park5d790c32019-11-15 18:40:32 +09005635 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5636 apexRule := module.Rule("apexRule")
5637 copyCmds := apexRule.Args["copy_commands"]
5638
5639 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005640 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005641
5642 apexBundle := module.Module().(*apexBundle)
5643 name := apexBundle.Name()
5644 if name != "override_myapex" {
5645 t.Errorf("name should be \"override_myapex\", but was %q", name)
5646 }
5647
Baligh Uddin004d7172020-02-19 21:29:28 -08005648 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5649 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5650 }
5651
Jiyong Park20bacab2020-03-03 11:45:41 +09005652 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005653 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09005654
Colin Crossaa255532020-07-03 13:18:24 -07005655 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005656 var builder strings.Builder
5657 data.Custom(&builder, name, "TARGET_", "", data)
5658 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005659 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005660 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5661 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005662 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005663 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005664 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005665 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5666 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005667}
5668
Jooyung Han214bf372019-11-12 13:03:50 +09005669func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005670 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005671 apex {
5672 name: "myapex",
5673 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005674 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005675 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005676 }
5677
5678 apex_key {
5679 name: "myapex.key",
5680 public_key: "testkey.avbpubkey",
5681 private_key: "testkey.pem",
5682 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005683
5684 cc_library {
5685 name: "mylib",
5686 srcs: ["mylib.cpp"],
5687 stl: "libc++",
5688 system_shared_libs: [],
5689 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005690 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005691 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005692 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005693
5694 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5695 args := module.Rule("apexRule").Args
5696 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005697 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005698
5699 // The copies of the libraries in the apex should have one more dependency than
5700 // the ones outside the apex, namely the unwinder. Ideally we should check
5701 // the dependency names directly here but for some reason the names are blank in
5702 // this test.
5703 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005704 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005705 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5706 if len(apexImplicits) != len(nonApexImplicits)+1 {
5707 t.Errorf("%q missing unwinder dep", lib)
5708 }
5709 }
Jooyung Han214bf372019-11-12 13:03:50 +09005710}
5711
Paul Duffine05480a2021-03-08 15:07:14 +00005712var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01005713 "api/current.txt": nil,
5714 "api/removed.txt": nil,
5715 "api/system-current.txt": nil,
5716 "api/system-removed.txt": nil,
5717 "api/test-current.txt": nil,
5718 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005719
Anton Hanssondff2c782020-12-21 17:10:01 +00005720 "100/public/api/foo.txt": nil,
5721 "100/public/api/foo-removed.txt": nil,
5722 "100/system/api/foo.txt": nil,
5723 "100/system/api/foo-removed.txt": nil,
5724
Paul Duffineedc5d52020-06-12 17:46:39 +01005725 // For java_sdk_library_import
5726 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005727}
5728
Jooyung Han58f26ab2019-12-18 15:34:32 +09005729func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005730 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09005731 apex {
5732 name: "myapex",
5733 key: "myapex.key",
5734 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005735 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09005736 }
5737
5738 apex_key {
5739 name: "myapex.key",
5740 public_key: "testkey.avbpubkey",
5741 private_key: "testkey.pem",
5742 }
5743
5744 java_sdk_library {
5745 name: "foo",
5746 srcs: ["a.java"],
5747 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005748 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005749 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005750
5751 prebuilt_apis {
5752 name: "sdk",
5753 api_dirs: ["100"],
5754 }
Paul Duffin9b879592020-05-26 13:21:35 +01005755 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005756
5757 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005758 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005759 "javalib/foo.jar",
5760 "etc/permissions/foo.xml",
5761 })
5762 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005763 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5764 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005765}
5766
Paul Duffin9b879592020-05-26 13:21:35 +01005767func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005768 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005769 apex {
5770 name: "myapex",
5771 key: "myapex.key",
5772 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005773 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005774 }
5775
5776 apex_key {
5777 name: "myapex.key",
5778 public_key: "testkey.avbpubkey",
5779 private_key: "testkey.pem",
5780 }
5781
5782 java_sdk_library {
5783 name: "foo",
5784 srcs: ["a.java"],
5785 api_packages: ["foo"],
5786 apex_available: ["myapex"],
5787 sdk_version: "none",
5788 system_modules: "none",
5789 }
5790
5791 java_library {
5792 name: "bar",
5793 srcs: ["a.java"],
5794 libs: ["foo"],
5795 apex_available: ["myapex"],
5796 sdk_version: "none",
5797 system_modules: "none",
5798 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005799
5800 prebuilt_apis {
5801 name: "sdk",
5802 api_dirs: ["100"],
5803 }
Paul Duffin9b879592020-05-26 13:21:35 +01005804 `, withFiles(filesForSdkLibrary))
5805
5806 // java_sdk_library installs both impl jar and permission XML
5807 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5808 "javalib/bar.jar",
5809 "javalib/foo.jar",
5810 "etc/permissions/foo.xml",
5811 })
5812
5813 // The bar library should depend on the implementation jar.
5814 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005815 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005816 t.Errorf("expected %q, found %#q", expected, actual)
5817 }
5818}
5819
5820func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005821 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005822 apex {
5823 name: "myapex",
5824 key: "myapex.key",
5825 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005826 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005827 }
5828
5829 apex_key {
5830 name: "myapex.key",
5831 public_key: "testkey.avbpubkey",
5832 private_key: "testkey.pem",
5833 }
5834
5835 java_sdk_library {
5836 name: "foo",
5837 srcs: ["a.java"],
5838 api_packages: ["foo"],
5839 apex_available: ["myapex"],
5840 sdk_version: "none",
5841 system_modules: "none",
5842 }
5843
5844 java_library {
5845 name: "bar",
5846 srcs: ["a.java"],
5847 libs: ["foo"],
5848 sdk_version: "none",
5849 system_modules: "none",
5850 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005851
5852 prebuilt_apis {
5853 name: "sdk",
5854 api_dirs: ["100"],
5855 }
Paul Duffin9b879592020-05-26 13:21:35 +01005856 `, withFiles(filesForSdkLibrary))
5857
5858 // java_sdk_library installs both impl jar and permission XML
5859 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5860 "javalib/foo.jar",
5861 "etc/permissions/foo.xml",
5862 })
5863
5864 // The bar library should depend on the stubs jar.
5865 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005866 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005867 t.Errorf("expected %q, found %#q", expected, actual)
5868 }
5869}
5870
Paul Duffineedc5d52020-06-12 17:46:39 +01005871func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005872 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00005873 prebuilt_apis {
5874 name: "sdk",
5875 api_dirs: ["100"],
5876 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005877 withFiles(map[string][]byte{
5878 "apex/a.java": nil,
5879 "apex/apex_manifest.json": nil,
5880 "apex/Android.bp": []byte(`
5881 package {
5882 default_visibility: ["//visibility:private"],
5883 }
5884
5885 apex {
5886 name: "myapex",
5887 key: "myapex.key",
5888 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005889 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005890 }
5891
5892 apex_key {
5893 name: "myapex.key",
5894 public_key: "testkey.avbpubkey",
5895 private_key: "testkey.pem",
5896 }
5897
5898 java_library {
5899 name: "bar",
5900 srcs: ["a.java"],
5901 libs: ["foo"],
5902 apex_available: ["myapex"],
5903 sdk_version: "none",
5904 system_modules: "none",
5905 }
5906`),
5907 "source/a.java": nil,
5908 "source/api/current.txt": nil,
5909 "source/api/removed.txt": nil,
5910 "source/Android.bp": []byte(`
5911 package {
5912 default_visibility: ["//visibility:private"],
5913 }
5914
5915 java_sdk_library {
5916 name: "foo",
5917 visibility: ["//apex"],
5918 srcs: ["a.java"],
5919 api_packages: ["foo"],
5920 apex_available: ["myapex"],
5921 sdk_version: "none",
5922 system_modules: "none",
5923 public: {
5924 enabled: true,
5925 },
5926 }
5927`),
5928 "prebuilt/a.jar": nil,
5929 "prebuilt/Android.bp": []byte(`
5930 package {
5931 default_visibility: ["//visibility:private"],
5932 }
5933
5934 java_sdk_library_import {
5935 name: "foo",
5936 visibility: ["//apex", "//source"],
5937 apex_available: ["myapex"],
5938 prefer: true,
5939 public: {
5940 jars: ["a.jar"],
5941 },
5942 }
5943`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005944 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005945 )
5946
5947 // java_sdk_library installs both impl jar and permission XML
5948 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5949 "javalib/bar.jar",
5950 "javalib/foo.jar",
5951 "etc/permissions/foo.xml",
5952 })
5953
5954 // The bar library should depend on the implementation jar.
5955 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005956 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01005957 t.Errorf("expected %q, found %#q", expected, actual)
5958 }
5959}
5960
5961func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5962 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5963 apex {
5964 name: "myapex",
5965 key: "myapex.key",
5966 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005967 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005968 }
5969
5970 apex_key {
5971 name: "myapex.key",
5972 public_key: "testkey.avbpubkey",
5973 private_key: "testkey.pem",
5974 }
5975
5976 java_sdk_library_import {
5977 name: "foo",
5978 apex_available: ["myapex"],
5979 prefer: true,
5980 public: {
5981 jars: ["a.jar"],
5982 },
5983 }
5984
5985 `, withFiles(filesForSdkLibrary))
5986}
5987
atrost6e126252020-01-27 17:01:16 +00005988func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01005989 result := android.GroupFixturePreparers(
5990 prepareForApexTest,
5991 java.PrepareForTestWithPlatformCompatConfig,
5992 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00005993 apex {
5994 name: "myapex",
5995 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00005996 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00005997 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005998 updatable: false,
atrost6e126252020-01-27 17:01:16 +00005999 }
6000
6001 apex_key {
6002 name: "myapex.key",
6003 public_key: "testkey.avbpubkey",
6004 private_key: "testkey.pem",
6005 }
6006
6007 platform_compat_config {
6008 name: "myjar-platform-compat-config",
6009 src: ":myjar",
6010 }
6011
6012 java_library {
6013 name: "myjar",
6014 srcs: ["foo/bar/MyClass.java"],
6015 sdk_version: "none",
6016 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00006017 apex_available: [ "myapex" ],
6018 }
Paul Duffin1b29e002021-03-16 15:06:54 +00006019
6020 // Make sure that a preferred prebuilt does not affect the apex contents.
6021 prebuilt_platform_compat_config {
6022 name: "myjar-platform-compat-config",
6023 metadata: "compat-config/metadata.xml",
6024 prefer: true,
6025 }
atrost6e126252020-01-27 17:01:16 +00006026 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00006027 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00006028 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6029 "etc/compatconfig/myjar-platform-compat-config.xml",
6030 "javalib/myjar.jar",
6031 })
6032}
6033
Jiyong Park479321d2019-12-16 11:47:12 +09006034func TestRejectNonInstallableJavaLibrary(t *testing.T) {
6035 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
6036 apex {
6037 name: "myapex",
6038 key: "myapex.key",
6039 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006040 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09006041 }
6042
6043 apex_key {
6044 name: "myapex.key",
6045 public_key: "testkey.avbpubkey",
6046 private_key: "testkey.pem",
6047 }
6048
6049 java_library {
6050 name: "myjar",
6051 srcs: ["foo/bar/MyClass.java"],
6052 sdk_version: "none",
6053 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09006054 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006055 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09006056 }
6057 `)
6058}
6059
Jiyong Park7afd1072019-12-30 16:56:33 +09006060func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006061 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09006062 apex {
6063 name: "myapex",
6064 key: "myapex.key",
6065 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006066 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09006067 }
6068
6069 apex_key {
6070 name: "myapex.key",
6071 public_key: "testkey.avbpubkey",
6072 private_key: "testkey.pem",
6073 }
6074
6075 cc_library {
6076 name: "mylib",
6077 srcs: ["mylib.cpp"],
6078 system_shared_libs: [],
6079 stl: "none",
6080 required: ["a", "b"],
6081 host_required: ["c", "d"],
6082 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006083 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006084 }
6085 `)
6086
6087 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006088 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006089 name := apexBundle.BaseModuleName()
6090 prefix := "TARGET_"
6091 var builder strings.Builder
6092 data.Custom(&builder, name, prefix, "", data)
6093 androidMk := builder.String()
6094 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6095 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6096 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6097}
6098
Jiyong Park7cd10e32020-01-14 09:22:18 +09006099func TestSymlinksFromApexToSystem(t *testing.T) {
6100 bp := `
6101 apex {
6102 name: "myapex",
6103 key: "myapex.key",
6104 native_shared_libs: ["mylib"],
6105 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006106 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006107 }
6108
Jiyong Park9d677202020-02-19 16:29:35 +09006109 apex {
6110 name: "myapex.updatable",
6111 key: "myapex.key",
6112 native_shared_libs: ["mylib"],
6113 java_libs: ["myjar"],
6114 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006115 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006116 }
6117
Jiyong Park7cd10e32020-01-14 09:22:18 +09006118 apex_key {
6119 name: "myapex.key",
6120 public_key: "testkey.avbpubkey",
6121 private_key: "testkey.pem",
6122 }
6123
6124 cc_library {
6125 name: "mylib",
6126 srcs: ["mylib.cpp"],
6127 shared_libs: ["myotherlib"],
6128 system_shared_libs: [],
6129 stl: "none",
6130 apex_available: [
6131 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006132 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006133 "//apex_available:platform",
6134 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006135 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006136 }
6137
6138 cc_library {
6139 name: "myotherlib",
6140 srcs: ["mylib.cpp"],
6141 system_shared_libs: [],
6142 stl: "none",
6143 apex_available: [
6144 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006145 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006146 "//apex_available:platform",
6147 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006148 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006149 }
6150
6151 java_library {
6152 name: "myjar",
6153 srcs: ["foo/bar/MyClass.java"],
6154 sdk_version: "none",
6155 system_modules: "none",
6156 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006157 apex_available: [
6158 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006159 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006160 "//apex_available:platform",
6161 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006162 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006163 }
6164
6165 java_library {
6166 name: "myotherjar",
6167 srcs: ["foo/bar/MyClass.java"],
6168 sdk_version: "none",
6169 system_modules: "none",
6170 apex_available: [
6171 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006172 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006173 "//apex_available:platform",
6174 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006175 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006176 }
6177 `
6178
6179 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6180 for _, f := range files {
6181 if f.path == file {
6182 if f.isLink {
6183 t.Errorf("%q is not a real file", file)
6184 }
6185 return
6186 }
6187 }
6188 t.Errorf("%q is not found", file)
6189 }
6190
6191 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6192 for _, f := range files {
6193 if f.path == file {
6194 if !f.isLink {
6195 t.Errorf("%q is not a symlink", file)
6196 }
6197 return
6198 }
6199 }
6200 t.Errorf("%q is not found", file)
6201 }
6202
Jiyong Park9d677202020-02-19 16:29:35 +09006203 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6204 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006205 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006206 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006207 ensureRealfileExists(t, files, "javalib/myjar.jar")
6208 ensureRealfileExists(t, files, "lib64/mylib.so")
6209 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6210
Jiyong Park9d677202020-02-19 16:29:35 +09006211 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6212 ensureRealfileExists(t, files, "javalib/myjar.jar")
6213 ensureRealfileExists(t, files, "lib64/mylib.so")
6214 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6215
6216 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006217 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006218 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006219 ensureRealfileExists(t, files, "javalib/myjar.jar")
6220 ensureRealfileExists(t, files, "lib64/mylib.so")
6221 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006222
6223 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6224 ensureRealfileExists(t, files, "javalib/myjar.jar")
6225 ensureRealfileExists(t, files, "lib64/mylib.so")
6226 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006227}
6228
Yo Chiange8128052020-07-23 20:09:18 +08006229func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006230 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006231 apex {
6232 name: "myapex",
6233 key: "myapex.key",
6234 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006235 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006236 }
6237
6238 apex_key {
6239 name: "myapex.key",
6240 public_key: "testkey.avbpubkey",
6241 private_key: "testkey.pem",
6242 }
6243
6244 cc_library_shared {
6245 name: "mylib",
6246 srcs: ["mylib.cpp"],
6247 shared_libs: ["myotherlib"],
6248 system_shared_libs: [],
6249 stl: "none",
6250 apex_available: [
6251 "myapex",
6252 "//apex_available:platform",
6253 ],
6254 }
6255
6256 cc_prebuilt_library_shared {
6257 name: "myotherlib",
6258 srcs: ["prebuilt.so"],
6259 system_shared_libs: [],
6260 stl: "none",
6261 apex_available: [
6262 "myapex",
6263 "//apex_available:platform",
6264 ],
6265 }
6266 `)
6267
6268 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006269 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006270 var builder strings.Builder
6271 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6272 androidMk := builder.String()
6273 // `myotherlib` is added to `myapex` as symlink
6274 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6275 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6276 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6277 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006278 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 +08006279}
6280
Jooyung Han643adc42020-02-27 13:50:06 +09006281func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006282 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006283 apex {
6284 name: "myapex",
6285 key: "myapex.key",
6286 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006287 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006288 }
6289
6290 apex_key {
6291 name: "myapex.key",
6292 public_key: "testkey.avbpubkey",
6293 private_key: "testkey.pem",
6294 }
6295
6296 cc_library {
6297 name: "mylib",
6298 srcs: ["mylib.cpp"],
6299 shared_libs: ["mylib2"],
6300 system_shared_libs: [],
6301 stl: "none",
6302 apex_available: [ "myapex" ],
6303 }
6304
6305 cc_library {
6306 name: "mylib2",
6307 srcs: ["mylib.cpp"],
6308 system_shared_libs: [],
6309 stl: "none",
6310 apex_available: [ "myapex" ],
6311 }
6312 `)
6313
6314 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6315 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6316 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6317 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6318 "lib64/mylib.so",
6319 "lib64/mylib2.so",
6320 })
6321}
6322
Jooyung Han49f67012020-04-17 13:43:10 +09006323func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006324 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006325 apex {
6326 name: "myapex",
6327 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006328 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006329 }
6330 apex_key {
6331 name: "myapex.key",
6332 public_key: "testkey.avbpubkey",
6333 private_key: "testkey.pem",
6334 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006335 `,
6336 android.FixtureModifyConfig(func(config android.Config) {
6337 delete(config.Targets, android.Android)
6338 config.AndroidCommonTarget = android.Target{}
6339 }),
6340 )
Jooyung Han49f67012020-04-17 13:43:10 +09006341
6342 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6343 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6344 }
6345}
6346
Jiyong Parkbd159612020-02-28 15:22:21 +09006347func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006348 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006349 apex {
6350 name: "myapex",
6351 key: "myapex.key",
6352 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006353 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006354 }
6355
6356 apex_key {
6357 name: "myapex.key",
6358 public_key: "testkey.avbpubkey",
6359 private_key: "testkey.pem",
6360 }
6361
6362 android_app {
6363 name: "AppFoo",
6364 srcs: ["foo/bar/MyClass.java"],
6365 sdk_version: "none",
6366 system_modules: "none",
6367 apex_available: [ "myapex" ],
6368 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006369 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006370
Colin Crosscf371cc2020-11-13 11:48:42 -08006371 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006372 content := bundleConfigRule.Args["content"]
6373
6374 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006375 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 +09006376}
6377
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006378func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006379 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006380 apex {
6381 name: "myapex",
6382 key: "myapex.key",
6383 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006384 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006385 }
6386
6387 apex_key {
6388 name: "myapex.key",
6389 public_key: "testkey.avbpubkey",
6390 private_key: "testkey.pem",
6391 }
6392
6393 android_app_set {
6394 name: "AppSet",
6395 set: "AppSet.apks",
6396 }`)
6397 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006398 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006399 content := bundleConfigRule.Args["content"]
6400 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6401 s := mod.Rule("apexRule").Args["copy_commands"]
6402 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6403 if len(copyCmds) != 3 {
6404 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6405 }
6406 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6407 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6408 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6409}
6410
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006411func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01006412 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006413 apex_set {
6414 name: "myapex",
6415 filename: "foo_v2.apex",
6416 sanitized: {
6417 none: { set: "myapex.apks", },
6418 hwaddress: { set: "myapex.hwasan.apks", },
6419 },
Paul Duffin24704672021-04-06 16:09:30 +01006420 }
6421 `
6422 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006423
Paul Duffin24704672021-04-06 16:09:30 +01006424 // Check that the extractor produces the correct output file from the correct input file.
6425 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006426
Paul Duffin24704672021-04-06 16:09:30 +01006427 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
6428 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006429
Paul Duffin24704672021-04-06 16:09:30 +01006430 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
6431
6432 // Ditto for the apex.
6433 m = ctx.ModuleForTests("myapex", "android_common")
6434 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common/foo_v2.apex")
6435
6436 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006437}
6438
Paul Duffin60264a02021-04-12 20:02:36 +01006439func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006440 t.Helper()
6441
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006442 bp := `
6443 java_library {
6444 name: "some-updatable-apex-lib",
6445 srcs: ["a.java"],
6446 sdk_version: "current",
6447 apex_available: [
6448 "some-updatable-apex",
6449 ],
6450 }
6451
6452 java_library {
6453 name: "some-non-updatable-apex-lib",
6454 srcs: ["a.java"],
6455 apex_available: [
6456 "some-non-updatable-apex",
6457 ],
6458 }
6459
6460 java_library {
6461 name: "some-platform-lib",
6462 srcs: ["a.java"],
6463 sdk_version: "current",
6464 installable: true,
6465 }
6466
6467 java_library {
6468 name: "some-art-lib",
6469 srcs: ["a.java"],
6470 sdk_version: "current",
6471 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006472 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006473 ],
6474 hostdex: true,
6475 }
6476
6477 apex {
6478 name: "some-updatable-apex",
6479 key: "some-updatable-apex.key",
6480 java_libs: ["some-updatable-apex-lib"],
6481 updatable: true,
6482 min_sdk_version: "current",
6483 }
6484
6485 apex {
6486 name: "some-non-updatable-apex",
6487 key: "some-non-updatable-apex.key",
6488 java_libs: ["some-non-updatable-apex-lib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006489 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006490 }
6491
6492 apex_key {
6493 name: "some-updatable-apex.key",
6494 }
6495
6496 apex_key {
6497 name: "some-non-updatable-apex.key",
6498 }
6499
6500 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006501 name: "com.android.art.debug",
6502 key: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006503 java_libs: ["some-art-lib"],
6504 updatable: true,
6505 min_sdk_version: "current",
6506 }
6507
6508 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006509 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006510 }
6511
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006512 filegroup {
6513 name: "some-updatable-apex-file_contexts",
6514 srcs: [
6515 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6516 ],
6517 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006518
6519 filegroup {
6520 name: "some-non-updatable-apex-file_contexts",
6521 srcs: [
6522 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6523 ],
6524 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006525 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006526
Paul Duffin60264a02021-04-12 20:02:36 +01006527 testDexpreoptWithApexes(t, bp, errmsg, preparer)
Paul Duffinc3bbb962020-12-10 19:15:49 +00006528}
6529
Paul Duffin60264a02021-04-12 20:02:36 +01006530func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006531 t.Helper()
6532
Paul Duffin55607122021-03-30 23:32:51 +01006533 fs := android.MockFS{
6534 "a.java": nil,
6535 "a.jar": nil,
6536 "apex_manifest.json": nil,
6537 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006538 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006539 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6540 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6541 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006542 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006543 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006544
Paul Duffin55607122021-03-30 23:32:51 +01006545 errorHandler := android.FixtureExpectsNoErrors
6546 if errmsg != "" {
6547 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006548 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006549
Paul Duffin55607122021-03-30 23:32:51 +01006550 result := android.GroupFixturePreparers(
6551 cc.PrepareForTestWithCcDefaultModules,
6552 java.PrepareForTestWithHiddenApiBuildComponents,
6553 java.PrepareForTestWithJavaDefaultModules,
6554 java.PrepareForTestWithJavaSdkLibraryFiles,
6555 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01006556 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01006557 fs.AddToFixture(),
Paul Duffin8f146b92021-04-12 17:24:18 +01006558 android.FixtureAddTextFile("frameworks/base/boot/Android.bp", `
6559 platform_bootclasspath {
6560 name: "platform-bootclasspath",
6561 }
6562 `),
Paul Duffin55607122021-03-30 23:32:51 +01006563 ).
6564 ExtendWithErrorHandler(errorHandler).
6565 RunTestWithBp(t, bp)
6566
6567 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006568}
6569
Jooyung Han548640b2020-04-27 12:10:30 +09006570func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6571 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6572 apex {
6573 name: "myapex",
6574 key: "myapex.key",
6575 updatable: true,
6576 }
6577
6578 apex_key {
6579 name: "myapex.key",
6580 public_key: "testkey.avbpubkey",
6581 private_key: "testkey.pem",
6582 }
6583 `)
6584}
6585
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006586func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6587 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6588 apex {
6589 name: "myapex",
6590 key: "myapex.key",
6591 }
6592
6593 apex_key {
6594 name: "myapex.key",
6595 public_key: "testkey.avbpubkey",
6596 private_key: "testkey.pem",
6597 }
6598 `)
6599}
6600
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006601func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006602 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
6603 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
6604 // modules to be included in the BootJars.
6605 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
6606 return android.GroupFixturePreparers(
6607 dexpreopt.FixtureSetBootJars(bootJars...),
6608 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6609 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
6610 }),
6611 )
6612 }
6613
6614 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
6615 // same value. This can result in an invalid configuration as it allows non art apex jars to be
6616 // specified in the ArtApexJars configuration.
6617 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
6618 return android.GroupFixturePreparers(
6619 dexpreopt.FixtureSetArtBootJars(bootJars...),
6620 dexpreopt.FixtureSetBootJars(bootJars...),
6621 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6622 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
6623 }),
6624 )
6625 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006626
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006627 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006628 preparer := java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib")
6629 testNoUpdatableJarsInBootImage(t, "", preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006630 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006631
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006632 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006633 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
6634 // Update the dexpreopt BootJars directly.
6635 preparer := prepareSetBootJars("com.android.art.debug:some-art-lib")
6636 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006637 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006638
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006639 t.Run("updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006640 err := `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the ART boot image`
6641 // Update the dexpreopt ArtApexJars directly.
6642 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
6643 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006644 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006645
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006646 t.Run("non-updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006647 err := `module "some-non-updatable-apex-lib" is not allowed in the ART boot image`
6648 // Update the dexpreopt ArtApexJars directly.
6649 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
6650 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006651 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006652
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006653 t.Run("updatable jar from some other apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006654 err := `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the framework boot image`
6655 preparer := java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib")
6656 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006657 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006658
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006659 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006660 preparer := java.FixtureConfigureBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
6661 testNoUpdatableJarsInBootImage(t, "", preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006662 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006663
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006664 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01006665 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01006666 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
6667 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006668 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006669
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006670 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01006671 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01006672 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
6673 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006674 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006675
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006676 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006677 err := `module "some-platform-lib" is not allowed in the ART boot image`
6678 // Update the dexpreopt ArtApexJars directly.
6679 preparer := prepareSetArtJars("platform:some-platform-lib")
6680 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006681 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006682
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006683 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006684 preparer := java.FixtureConfigureBootJars("platform:some-platform-lib")
6685 testNoUpdatableJarsInBootImage(t, "", preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006686 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006687}
6688
6689func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01006690 preparer := java.FixtureConfigureBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00006691 t.Run("prebuilt no source", func(t *testing.T) {
6692 testDexpreoptWithApexes(t, `
6693 prebuilt_apex {
6694 name: "myapex" ,
6695 arch: {
6696 arm64: {
6697 src: "myapex-arm64.apex",
6698 },
6699 arm: {
6700 src: "myapex-arm.apex",
6701 },
6702 },
6703 exported_java_libs: ["libfoo"],
6704 }
6705
6706 java_import {
6707 name: "libfoo",
6708 jars: ["libfoo.jar"],
6709 }
Paul Duffin60264a02021-04-12 20:02:36 +01006710`, "", preparer)
Paul Duffin064b70c2020-11-02 17:32:38 +00006711 })
6712
6713 t.Run("prebuilt no source", func(t *testing.T) {
6714 testDexpreoptWithApexes(t, `
6715 prebuilt_apex {
6716 name: "myapex" ,
6717 arch: {
6718 arm64: {
6719 src: "myapex-arm64.apex",
6720 },
6721 arm: {
6722 src: "myapex-arm.apex",
6723 },
6724 },
6725 exported_java_libs: ["libfoo"],
6726 }
6727
6728 java_import {
6729 name: "libfoo",
6730 jars: ["libfoo.jar"],
6731 }
Paul Duffin60264a02021-04-12 20:02:36 +01006732`, "", preparer)
Paul Duffin064b70c2020-11-02 17:32:38 +00006733 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006734}
6735
Andrei Onea115e7e72020-06-05 21:14:03 +01006736func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6737 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006738 bp += `
6739 apex_key {
6740 name: "myapex.key",
6741 public_key: "testkey.avbpubkey",
6742 private_key: "testkey.pem",
6743 }`
Paul Duffin45338f02021-03-30 23:07:52 +01006744 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01006745 "lib1/src/A.java": nil,
6746 "lib2/src/B.java": nil,
6747 "system/sepolicy/apex/myapex-file_contexts": nil,
6748 }
6749
Paul Duffin45338f02021-03-30 23:07:52 +01006750 errorHandler := android.FixtureExpectsNoErrors
6751 if errmsg != "" {
6752 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07006753 }
Colin Crossae8600b2020-10-29 17:09:13 -07006754
Paul Duffin45338f02021-03-30 23:07:52 +01006755 android.GroupFixturePreparers(
6756 android.PrepareForTestWithAndroidBuildComponents,
6757 java.PrepareForTestWithJavaBuildComponents,
6758 PrepareForTestWithApexBuildComponents,
6759 android.PrepareForTestWithNeverallowRules(rules),
6760 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6761 updatableBootJars := make([]string, 0, len(apexBootJars))
6762 for _, apexBootJar := range apexBootJars {
6763 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6764 }
6765 variables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6766 }),
6767 fs.AddToFixture(),
6768 ).
6769 ExtendWithErrorHandler(errorHandler).
6770 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01006771}
6772
6773func TestApexPermittedPackagesRules(t *testing.T) {
6774 testcases := []struct {
6775 name string
6776 expectedError string
6777 bp string
6778 bootJars []string
6779 modulesPackages map[string][]string
6780 }{
6781
6782 {
6783 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6784 expectedError: "",
6785 bp: `
6786 java_library {
6787 name: "bcp_lib1",
6788 srcs: ["lib1/src/*.java"],
6789 permitted_packages: ["foo.bar"],
6790 apex_available: ["myapex"],
6791 sdk_version: "none",
6792 system_modules: "none",
6793 }
6794 java_library {
6795 name: "nonbcp_lib2",
6796 srcs: ["lib2/src/*.java"],
6797 apex_available: ["myapex"],
6798 permitted_packages: ["a.b"],
6799 sdk_version: "none",
6800 system_modules: "none",
6801 }
6802 apex {
6803 name: "myapex",
6804 key: "myapex.key",
6805 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006806 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006807 }`,
6808 bootJars: []string{"bcp_lib1"},
6809 modulesPackages: map[string][]string{
6810 "myapex": []string{
6811 "foo.bar",
6812 },
6813 },
6814 },
6815 {
6816 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6817 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.`,
6818 bp: `
6819 java_library {
6820 name: "bcp_lib1",
6821 srcs: ["lib1/src/*.java"],
6822 apex_available: ["myapex"],
6823 permitted_packages: ["foo.bar"],
6824 sdk_version: "none",
6825 system_modules: "none",
6826 }
6827 java_library {
6828 name: "bcp_lib2",
6829 srcs: ["lib2/src/*.java"],
6830 apex_available: ["myapex"],
6831 permitted_packages: ["foo.bar", "bar.baz"],
6832 sdk_version: "none",
6833 system_modules: "none",
6834 }
6835 apex {
6836 name: "myapex",
6837 key: "myapex.key",
6838 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006839 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006840 }
6841 `,
6842 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6843 modulesPackages: map[string][]string{
6844 "myapex": []string{
6845 "foo.bar",
6846 },
6847 },
6848 },
6849 }
6850 for _, tc := range testcases {
6851 t.Run(tc.name, func(t *testing.T) {
6852 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6853 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6854 })
6855 }
6856}
6857
Jiyong Park62304bb2020-04-13 16:19:48 +09006858func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006859 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09006860 apex {
6861 name: "myapex",
6862 key: "myapex.key",
6863 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006864 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09006865 }
6866
6867 apex_key {
6868 name: "myapex.key",
6869 public_key: "testkey.avbpubkey",
6870 private_key: "testkey.pem",
6871 }
6872
6873 cc_library {
6874 name: "mylib",
6875 srcs: ["mylib.cpp"],
6876 system_shared_libs: [],
6877 stl: "none",
6878 stubs: {
6879 versions: ["1"],
6880 },
6881 apex_available: ["myapex"],
6882 }
6883
6884 cc_library {
6885 name: "myprivlib",
6886 srcs: ["mylib.cpp"],
6887 system_shared_libs: [],
6888 stl: "none",
6889 apex_available: ["myapex"],
6890 }
6891
6892
6893 cc_test {
6894 name: "mytest",
6895 gtest: false,
6896 srcs: ["mylib.cpp"],
6897 system_shared_libs: [],
6898 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006899 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006900 test_for: ["myapex"]
6901 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006902
6903 cc_library {
6904 name: "mytestlib",
6905 srcs: ["mylib.cpp"],
6906 system_shared_libs: [],
6907 shared_libs: ["mylib", "myprivlib"],
6908 stl: "none",
6909 test_for: ["myapex"],
6910 }
6911
6912 cc_benchmark {
6913 name: "mybench",
6914 srcs: ["mylib.cpp"],
6915 system_shared_libs: [],
6916 shared_libs: ["mylib", "myprivlib"],
6917 stl: "none",
6918 test_for: ["myapex"],
6919 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006920 `)
6921
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006922 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01006923 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006924 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6925 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6926 }
6927
6928 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09006929 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006930 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6931 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6932 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6933}
Jiyong Park46a512f2020-12-04 18:02:13 +09006934
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006935func TestIndirectTestFor(t *testing.T) {
6936 ctx := testApex(t, `
6937 apex {
6938 name: "myapex",
6939 key: "myapex.key",
6940 native_shared_libs: ["mylib", "myprivlib"],
6941 updatable: false,
6942 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006943
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006944 apex_key {
6945 name: "myapex.key",
6946 public_key: "testkey.avbpubkey",
6947 private_key: "testkey.pem",
6948 }
6949
6950 cc_library {
6951 name: "mylib",
6952 srcs: ["mylib.cpp"],
6953 system_shared_libs: [],
6954 stl: "none",
6955 stubs: {
6956 versions: ["1"],
6957 },
6958 apex_available: ["myapex"],
6959 }
6960
6961 cc_library {
6962 name: "myprivlib",
6963 srcs: ["mylib.cpp"],
6964 system_shared_libs: [],
6965 stl: "none",
6966 shared_libs: ["mylib"],
6967 apex_available: ["myapex"],
6968 }
6969
6970 cc_library {
6971 name: "mytestlib",
6972 srcs: ["mylib.cpp"],
6973 system_shared_libs: [],
6974 shared_libs: ["myprivlib"],
6975 stl: "none",
6976 test_for: ["myapex"],
6977 }
6978 `)
6979
6980 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01006981 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006982 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6983 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6984 }
6985
6986 // The platform variant of mytestlib links to the platform variant of the
6987 // internal myprivlib.
6988 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
6989
6990 // The platform variant of myprivlib links to the platform variant of mylib
6991 // and bypasses its stubs.
6992 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 +09006993}
6994
Martin Stjernholmec009002021-03-27 15:18:31 +00006995func TestTestForForLibInOtherApex(t *testing.T) {
6996 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
6997 _ = testApex(t, `
6998 apex {
6999 name: "com.android.art",
7000 key: "myapex.key",
7001 native_shared_libs: ["mylib"],
7002 updatable: false,
7003 }
7004
7005 apex {
7006 name: "com.android.art.debug",
7007 key: "myapex.key",
7008 native_shared_libs: ["mylib", "mytestlib"],
7009 updatable: false,
7010 }
7011
7012 apex_key {
7013 name: "myapex.key",
7014 public_key: "testkey.avbpubkey",
7015 private_key: "testkey.pem",
7016 }
7017
7018 cc_library {
7019 name: "mylib",
7020 srcs: ["mylib.cpp"],
7021 system_shared_libs: [],
7022 stl: "none",
7023 stubs: {
7024 versions: ["1"],
7025 },
7026 apex_available: ["com.android.art", "com.android.art.debug"],
7027 }
7028
7029 cc_library {
7030 name: "mytestlib",
7031 srcs: ["mylib.cpp"],
7032 system_shared_libs: [],
7033 shared_libs: ["mylib"],
7034 stl: "none",
7035 apex_available: ["com.android.art.debug"],
7036 test_for: ["com.android.art"],
7037 }
7038 `,
7039 android.MockFS{
7040 "system/sepolicy/apex/com.android.art-file_contexts": nil,
7041 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
7042 }.AddToFixture())
7043}
7044
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007045// TODO(jungjw): Move this to proptools
7046func intPtr(i int) *int {
7047 return &i
7048}
7049
7050func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007051 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007052 apex_set {
7053 name: "myapex",
7054 set: "myapex.apks",
7055 filename: "foo_v2.apex",
7056 overrides: ["foo"],
7057 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007058 `,
7059 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7060 variables.Platform_sdk_version = intPtr(30)
7061 }),
7062 android.FixtureModifyConfig(func(config android.Config) {
7063 config.Targets[android.Android] = []android.Target{
7064 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
7065 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
7066 }
7067 }),
7068 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007069
Paul Duffin24704672021-04-06 16:09:30 +01007070 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007071
7072 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01007073 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007074 actual := extractedApex.Args["abis"]
7075 expected := "ARMEABI_V7A,ARM64_V8A"
7076 if actual != expected {
7077 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7078 }
7079 actual = extractedApex.Args["sdk-version"]
7080 expected = "30"
7081 if actual != expected {
7082 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7083 }
7084
Paul Duffin24704672021-04-06 16:09:30 +01007085 m = ctx.ModuleForTests("myapex", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007086 a := m.Module().(*ApexSet)
7087 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07007088 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007089 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
7090 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
7091 }
7092}
7093
Jiyong Park7d95a512020-05-10 15:16:24 +09007094func TestNoStaticLinkingToStubsLib(t *testing.T) {
7095 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
7096 apex {
7097 name: "myapex",
7098 key: "myapex.key",
7099 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007100 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09007101 }
7102
7103 apex_key {
7104 name: "myapex.key",
7105 public_key: "testkey.avbpubkey",
7106 private_key: "testkey.pem",
7107 }
7108
7109 cc_library {
7110 name: "mylib",
7111 srcs: ["mylib.cpp"],
7112 static_libs: ["otherlib"],
7113 system_shared_libs: [],
7114 stl: "none",
7115 apex_available: [ "myapex" ],
7116 }
7117
7118 cc_library {
7119 name: "otherlib",
7120 srcs: ["mylib.cpp"],
7121 system_shared_libs: [],
7122 stl: "none",
7123 stubs: {
7124 versions: ["1", "2", "3"],
7125 },
7126 apex_available: [ "myapex" ],
7127 }
7128 `)
7129}
7130
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007131func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007132 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007133 apex {
7134 name: "myapex",
7135 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007136 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007137 }
7138
7139 apex_key {
7140 name: "myapex.key",
7141 public_key: "testkey.avbpubkey",
7142 private_key: "testkey.pem",
7143 }
7144
7145 prebuilt_apex {
7146 name: "myapex",
7147 prefer: true,
7148 arch: {
7149 arm64: {
7150 src: "myapex-arm64.apex",
7151 },
7152 arm: {
7153 src: "myapex-arm.apex",
7154 },
7155 },
7156 }
7157
7158 apex_set {
7159 name: "myapex_set",
7160 set: "myapex.apks",
7161 filename: "myapex_set.apex",
7162 overrides: ["myapex"],
7163 }
7164 `)
7165
7166 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7167 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7168 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 +09007169 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 +09007170}
7171
Jooyung Han938b5932020-06-20 12:47:47 +09007172func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007173 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007174 apex {
7175 name: "myapex",
7176 key: "myapex.key",
7177 apps: ["app"],
7178 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007179 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007180 }
7181
7182 apex_key {
7183 name: "myapex.key",
7184 public_key: "testkey.avbpubkey",
7185 private_key: "testkey.pem",
7186 }
7187
7188 android_app {
7189 name: "app",
7190 srcs: ["foo/bar/MyClass.java"],
7191 package_name: "foo",
7192 sdk_version: "none",
7193 system_modules: "none",
7194 apex_available: [ "myapex" ],
7195 }
7196 `, withFiles(map[string][]byte{
7197 "sub/Android.bp": []byte(`
7198 override_apex {
7199 name: "override_myapex",
7200 base: "myapex",
7201 apps: ["override_app"],
7202 allowed_files: ":allowed",
7203 }
7204 // Overridable "path" property should be referenced indirectly
7205 filegroup {
7206 name: "allowed",
7207 srcs: ["allowed.txt"],
7208 }
7209 override_android_app {
7210 name: "override_app",
7211 base: "app",
7212 package_name: "bar",
7213 }
7214 `),
7215 }))
7216
7217 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7218 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7219 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7220 }
7221
7222 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7223 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7224 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7225 }
7226}
7227
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007228func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007229 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007230 apex {
7231 name: "myapex",
7232 key: "myapex.key",
7233 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007234 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007235 }
7236
7237 apex_key {
7238 name: "myapex.key",
7239 public_key: "testkey.avbpubkey",
7240 private_key: "testkey.pem",
7241 }
7242
7243 cc_library {
7244 name: "mylib",
7245 srcs: ["mylib.cpp"],
7246 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007247 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007248 },
7249 apex_available: ["myapex"],
7250 }
7251
7252 cc_prebuilt_library_shared {
7253 name: "mylib",
7254 prefer: false,
7255 srcs: ["prebuilt.so"],
7256 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007257 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007258 },
7259 apex_available: ["myapex"],
7260 }
7261 `)
7262}
7263
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007264func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007265 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007266 apex {
7267 name: "myapex",
7268 key: "myapex.key",
7269 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007270 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007271 }
7272 apex_key {
7273 name: "myapex.key",
7274 public_key: "testkey.avbpubkey",
7275 private_key: "testkey.pem",
7276 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007277 `,
7278 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7279 variables.CompressedApex = proptools.BoolPtr(true)
7280 }),
7281 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007282
7283 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7284 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7285
7286 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7287 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7288
7289 // Make sure output of bundle is .capex
7290 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7291 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7292
7293 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007294 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007295 var builder strings.Builder
7296 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7297 androidMk := builder.String()
7298 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7299}
7300
Martin Stjernholm2856c662020-12-02 15:03:42 +00007301func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007302 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007303 apex {
7304 name: "myapex",
7305 key: "myapex.key",
7306 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007307 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007308 }
7309
7310 apex_key {
7311 name: "myapex.key",
7312 public_key: "testkey.avbpubkey",
7313 private_key: "testkey.pem",
7314 }
7315
7316 cc_library {
7317 name: "mylib",
7318 srcs: ["mylib.cpp"],
7319 apex_available: ["myapex"],
7320 shared_libs: ["otherlib"],
7321 system_shared_libs: [],
7322 }
7323
7324 cc_library {
7325 name: "otherlib",
7326 srcs: ["mylib.cpp"],
7327 stubs: {
7328 versions: ["current"],
7329 },
7330 }
7331
7332 cc_prebuilt_library_shared {
7333 name: "otherlib",
7334 prefer: true,
7335 srcs: ["prebuilt.so"],
7336 stubs: {
7337 versions: ["current"],
7338 },
7339 }
7340 `)
7341
7342 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007343 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007344 var builder strings.Builder
7345 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7346 androidMk := builder.String()
7347
7348 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7349 // a thing there.
7350 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7351}
7352
Jiyong Parke3867542020-12-03 17:28:25 +09007353func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007354 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007355 apex {
7356 name: "myapex",
7357 key: "myapex.key",
7358 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007359 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007360 }
7361
7362 apex_key {
7363 name: "myapex.key",
7364 public_key: "testkey.avbpubkey",
7365 private_key: "testkey.pem",
7366 }
7367
7368 cc_library {
7369 name: "mylib",
7370 srcs: ["mylib.cpp"],
7371 system_shared_libs: [],
7372 stl: "none",
7373 apex_available: ["myapex"],
7374 shared_libs: ["mylib2"],
7375 target: {
7376 apex: {
7377 exclude_shared_libs: ["mylib2"],
7378 },
7379 },
7380 }
7381
7382 cc_library {
7383 name: "mylib2",
7384 srcs: ["mylib.cpp"],
7385 system_shared_libs: [],
7386 stl: "none",
7387 }
7388 `)
7389
7390 // Check if mylib is linked to mylib2 for the non-apex target
7391 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7392 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7393
7394 // Make sure that the link doesn't occur for the apex target
7395 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7396 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7397
7398 // It shouldn't appear in the copy cmd as well.
7399 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7400 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7401}
7402
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007403func TestPrebuiltStubLibDep(t *testing.T) {
7404 bpBase := `
7405 apex {
7406 name: "myapex",
7407 key: "myapex.key",
7408 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007409 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007410 }
7411 apex_key {
7412 name: "myapex.key",
7413 public_key: "testkey.avbpubkey",
7414 private_key: "testkey.pem",
7415 }
7416 cc_library {
7417 name: "mylib",
7418 srcs: ["mylib.cpp"],
7419 apex_available: ["myapex"],
7420 shared_libs: ["stublib"],
7421 system_shared_libs: [],
7422 }
7423 apex {
7424 name: "otherapex",
7425 enabled: %s,
7426 key: "myapex.key",
7427 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007428 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007429 }
7430 `
7431
7432 stublibSourceBp := `
7433 cc_library {
7434 name: "stublib",
7435 srcs: ["mylib.cpp"],
7436 apex_available: ["otherapex"],
7437 system_shared_libs: [],
7438 stl: "none",
7439 stubs: {
7440 versions: ["1"],
7441 },
7442 }
7443 `
7444
7445 stublibPrebuiltBp := `
7446 cc_prebuilt_library_shared {
7447 name: "stublib",
7448 srcs: ["prebuilt.so"],
7449 apex_available: ["otherapex"],
7450 stubs: {
7451 versions: ["1"],
7452 },
7453 %s
7454 }
7455 `
7456
7457 tests := []struct {
7458 name string
7459 stublibBp string
7460 usePrebuilt bool
7461 modNames []string // Modules to collect AndroidMkEntries for
7462 otherApexEnabled []string
7463 }{
7464 {
7465 name: "only_source",
7466 stublibBp: stublibSourceBp,
7467 usePrebuilt: false,
7468 modNames: []string{"stublib"},
7469 otherApexEnabled: []string{"true", "false"},
7470 },
7471 {
7472 name: "source_preferred",
7473 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7474 usePrebuilt: false,
7475 modNames: []string{"stublib", "prebuilt_stublib"},
7476 otherApexEnabled: []string{"true", "false"},
7477 },
7478 {
7479 name: "prebuilt_preferred",
7480 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7481 usePrebuilt: true,
7482 modNames: []string{"stublib", "prebuilt_stublib"},
7483 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7484 },
7485 {
7486 name: "only_prebuilt",
7487 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7488 usePrebuilt: true,
7489 modNames: []string{"stublib"},
7490 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7491 },
7492 }
7493
7494 for _, test := range tests {
7495 t.Run(test.name, func(t *testing.T) {
7496 for _, otherApexEnabled := range test.otherApexEnabled {
7497 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007498 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007499
7500 type modAndMkEntries struct {
7501 mod *cc.Module
7502 mkEntries android.AndroidMkEntries
7503 }
7504 entries := []*modAndMkEntries{}
7505
7506 // Gather shared lib modules that are installable
7507 for _, modName := range test.modNames {
7508 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7509 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7510 continue
7511 }
7512 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007513 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007514 continue
7515 }
Colin Crossaa255532020-07-03 13:18:24 -07007516 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007517 if ent.Disabled {
7518 continue
7519 }
7520 entries = append(entries, &modAndMkEntries{
7521 mod: mod,
7522 mkEntries: ent,
7523 })
7524 }
7525 }
7526 }
7527
7528 var entry *modAndMkEntries = nil
7529 for _, ent := range entries {
7530 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7531 if entry != nil {
7532 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7533 } else {
7534 entry = ent
7535 }
7536 }
7537 }
7538
7539 if entry == nil {
7540 t.Errorf("AndroidMk entry for \"stublib\" missing")
7541 } else {
7542 isPrebuilt := entry.mod.Prebuilt() != nil
7543 if isPrebuilt != test.usePrebuilt {
7544 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7545 }
7546 if !entry.mod.IsStubs() {
7547 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7548 }
7549 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7550 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7551 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007552 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09007553 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09007554 if !android.InList(expected, cflags) {
7555 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7556 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007557 }
7558 })
7559 }
7560 })
7561 }
7562}
7563
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007564func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01007565 os.Exit(m.Run())
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007566}