blob: ee4255e0c44421e87cd61a8109346433fb90904b [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) {
4378 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin092153d2021-01-26 11:42:39 +00004379 // Empty transformation.
Paul Duffin064b70c2020-11-02 17:32:38 +00004380 }
4381
Paul Duffin89886cb2021-02-05 16:44:03 +00004382 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004383 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004384 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004385 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004386 stem := android.RemoveOptionalPrebuiltPrefix(name)
4387 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004388 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4389 }
4390 }
4391
Paul Duffin39853512021-02-26 11:09:39 +00004392 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004393 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004394 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004395 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4396 }
4397 }
4398
4399 t.Run("prebuilt only", func(t *testing.T) {
4400 bp := `
4401 prebuilt_apex {
4402 name: "myapex",
4403 arch: {
4404 arm64: {
4405 src: "myapex-arm64.apex",
4406 },
4407 arm: {
4408 src: "myapex-arm.apex",
4409 },
4410 },
Paul Duffin39853512021-02-26 11:09:39 +00004411 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004412 }
4413
4414 java_import {
4415 name: "libfoo",
4416 jars: ["libfoo.jar"],
4417 }
Paul Duffin39853512021-02-26 11:09:39 +00004418
4419 java_sdk_library_import {
4420 name: "libbar",
4421 public: {
4422 jars: ["libbar.jar"],
4423 },
4424 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004425 `
4426
4427 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4428 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4429
Paul Duffinf6932af2021-02-26 18:21:56 +00004430 // Make sure that the deapexer has the correct input APEX.
4431 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4432 rule := deapexer.Rule("deapexer")
4433 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4434 t.Errorf("expected: %q, found: %q", expected, actual)
4435 }
4436
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004437 // Make sure that the prebuilt_apex has the correct input APEX.
4438 prebuiltApex := ctx.ModuleForTests("myapex", "android_common")
4439 rule = prebuiltApex.Rule("android/soong/android.Cp")
4440 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4441 t.Errorf("expected: %q, found: %q", expected, actual)
4442 }
4443
Paul Duffin89886cb2021-02-05 16:44:03 +00004444 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004445
4446 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004447 })
4448
4449 t.Run("prebuilt with source preferred", func(t *testing.T) {
4450
4451 bp := `
4452 prebuilt_apex {
4453 name: "myapex",
4454 arch: {
4455 arm64: {
4456 src: "myapex-arm64.apex",
4457 },
4458 arm: {
4459 src: "myapex-arm.apex",
4460 },
4461 },
Paul Duffin39853512021-02-26 11:09:39 +00004462 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004463 }
4464
4465 java_import {
4466 name: "libfoo",
4467 jars: ["libfoo.jar"],
4468 }
4469
4470 java_library {
4471 name: "libfoo",
4472 }
Paul Duffin39853512021-02-26 11:09:39 +00004473
4474 java_sdk_library_import {
4475 name: "libbar",
4476 public: {
4477 jars: ["libbar.jar"],
4478 },
4479 }
4480
4481 java_sdk_library {
4482 name: "libbar",
4483 srcs: ["foo/bar/MyClass.java"],
4484 unsafe_ignore_missing_latest_api: true,
4485 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004486 `
4487
4488 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4489 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4490
Paul Duffin89886cb2021-02-05 16:44:03 +00004491 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004492 ensureNoSourceVariant(t, ctx, "libfoo")
4493
4494 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4495 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004496 })
4497
4498 t.Run("prebuilt preferred with source", func(t *testing.T) {
4499 bp := `
4500 prebuilt_apex {
4501 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004502 arch: {
4503 arm64: {
4504 src: "myapex-arm64.apex",
4505 },
4506 arm: {
4507 src: "myapex-arm.apex",
4508 },
4509 },
Paul Duffin39853512021-02-26 11:09:39 +00004510 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004511 }
4512
4513 java_import {
4514 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004515 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004516 jars: ["libfoo.jar"],
4517 }
4518
4519 java_library {
4520 name: "libfoo",
4521 }
Paul Duffin39853512021-02-26 11:09:39 +00004522
4523 java_sdk_library_import {
4524 name: "libbar",
4525 prefer: true,
4526 public: {
4527 jars: ["libbar.jar"],
4528 },
4529 }
4530
4531 java_sdk_library {
4532 name: "libbar",
4533 srcs: ["foo/bar/MyClass.java"],
4534 unsafe_ignore_missing_latest_api: true,
4535 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004536 `
4537
4538 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4539 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4540
Paul Duffin89886cb2021-02-05 16:44:03 +00004541 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004542 ensureNoSourceVariant(t, ctx, "libfoo")
4543
4544 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4545 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004546 })
4547}
4548
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004549func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
4550 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin37856732021-02-26 14:24:15 +00004551 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo", "myapex:libbar"})
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004552 }
4553
Paul Duffin37856732021-02-26 14:24:15 +00004554 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4555 t.Helper()
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004556 s := ctx.SingletonForTests("dex_bootjars")
4557 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004558 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004559 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004560 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004561 foundLibfooJar = true
4562 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01004563 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004564 }
4565 }
4566 if !foundLibfooJar {
Paul Duffin55607122021-03-30 23:32:51 +01004567 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 +00004568 }
4569 }
4570
Paul Duffin4fd997b2021-02-03 20:06:33 +00004571 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004572 t.Helper()
Paul Duffin4fd997b2021-02-03 20:06:33 +00004573 hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
4574 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
4575 java.CheckHiddenAPIRuleInputs(t, expectedInputs, indexRule)
4576 }
4577
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004578 t.Run("prebuilt only", func(t *testing.T) {
4579 bp := `
4580 prebuilt_apex {
4581 name: "myapex",
4582 arch: {
4583 arm64: {
4584 src: "myapex-arm64.apex",
4585 },
4586 arm: {
4587 src: "myapex-arm.apex",
4588 },
4589 },
Paul Duffin37856732021-02-26 14:24:15 +00004590 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004591 }
4592
4593 java_import {
4594 name: "libfoo",
4595 jars: ["libfoo.jar"],
4596 apex_available: ["myapex"],
4597 }
Paul Duffin37856732021-02-26 14:24:15 +00004598
4599 java_sdk_library_import {
4600 name: "libbar",
4601 public: {
4602 jars: ["libbar.jar"],
4603 },
4604 apex_available: ["myapex"],
4605 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004606 `
4607
4608 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin55607122021-03-30 23:32:51 +01004609 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4610 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004611
Paul Duffin9d67ca62021-02-03 20:06:33 +00004612 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4613 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004614.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004615.intermediates/libfoo/android_common_myapex/hiddenapi/index.csv
4616`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004617 })
4618
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004619 t.Run("apex_set only", func(t *testing.T) {
4620 bp := `
4621 apex_set {
4622 name: "myapex",
4623 set: "myapex.apks",
4624 exported_java_libs: ["libfoo", "libbar"],
4625 }
4626
4627 java_import {
4628 name: "libfoo",
4629 jars: ["libfoo.jar"],
4630 apex_available: ["myapex"],
4631 }
4632
4633 java_sdk_library_import {
4634 name: "libbar",
4635 public: {
4636 jars: ["libbar.jar"],
4637 },
4638 apex_available: ["myapex"],
4639 }
4640 `
4641
4642 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4643 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4644 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
4645
4646 // Make sure that the dex file from the apex_set contributes to the hiddenapi index file.
4647 checkHiddenAPIIndexInputs(t, ctx, `
4648.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
4649.intermediates/libfoo/android_common_myapex/hiddenapi/index.csv
4650`)
4651 })
4652
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004653 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4654 bp := `
4655 prebuilt_apex {
4656 name: "myapex",
4657 arch: {
4658 arm64: {
4659 src: "myapex-arm64.apex",
4660 },
4661 arm: {
4662 src: "myapex-arm.apex",
4663 },
4664 },
Paul Duffin37856732021-02-26 14:24:15 +00004665 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004666 }
4667
4668 java_import {
4669 name: "libfoo",
4670 jars: ["libfoo.jar"],
4671 apex_available: ["myapex"],
4672 }
4673
4674 java_library {
4675 name: "libfoo",
4676 srcs: ["foo/bar/MyClass.java"],
4677 apex_available: ["myapex"],
4678 }
Paul Duffin37856732021-02-26 14:24:15 +00004679
4680 java_sdk_library_import {
4681 name: "libbar",
4682 public: {
4683 jars: ["libbar.jar"],
4684 },
4685 apex_available: ["myapex"],
4686 }
4687
4688 java_sdk_library {
4689 name: "libbar",
4690 srcs: ["foo/bar/MyClass.java"],
4691 unsafe_ignore_missing_latest_api: true,
4692 apex_available: ["myapex"],
4693 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004694 `
4695
4696 // In this test the source (java_library) libfoo is active since the
4697 // prebuilt (java_import) defaults to prefer:false. However the
4698 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4699 // find the dex boot jar in it. We either need to disable the source libfoo
4700 // or make the prebuilt libfoo preferred.
4701 testDexpreoptWithApexes(t, bp, "failed to find a dex jar path for module 'libfoo'", transform)
4702 })
4703
4704 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4705 bp := `
4706 prebuilt_apex {
4707 name: "myapex",
4708 arch: {
4709 arm64: {
4710 src: "myapex-arm64.apex",
4711 },
4712 arm: {
4713 src: "myapex-arm.apex",
4714 },
4715 },
Paul Duffin37856732021-02-26 14:24:15 +00004716 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004717 }
4718
4719 java_import {
4720 name: "libfoo",
4721 prefer: true,
4722 jars: ["libfoo.jar"],
4723 apex_available: ["myapex"],
4724 }
4725
4726 java_library {
4727 name: "libfoo",
4728 srcs: ["foo/bar/MyClass.java"],
4729 apex_available: ["myapex"],
4730 }
Paul Duffin37856732021-02-26 14:24:15 +00004731
4732 java_sdk_library_import {
4733 name: "libbar",
4734 prefer: true,
4735 public: {
4736 jars: ["libbar.jar"],
4737 },
4738 apex_available: ["myapex"],
4739 }
4740
4741 java_sdk_library {
4742 name: "libbar",
4743 srcs: ["foo/bar/MyClass.java"],
4744 unsafe_ignore_missing_latest_api: true,
4745 apex_available: ["myapex"],
4746 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004747 `
4748
4749 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin55607122021-03-30 23:32:51 +01004750 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4751 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004752
Paul Duffin9d67ca62021-02-03 20:06:33 +00004753 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4754 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004755.intermediates/prebuilt_libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004756.intermediates/prebuilt_libfoo/android_common_myapex/hiddenapi/index.csv
4757`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004758 })
4759
4760 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4761 bp := `
4762 apex {
4763 name: "myapex",
4764 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004765 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004766 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004767 }
4768
4769 apex_key {
4770 name: "myapex.key",
4771 public_key: "testkey.avbpubkey",
4772 private_key: "testkey.pem",
4773 }
4774
4775 prebuilt_apex {
4776 name: "myapex",
4777 arch: {
4778 arm64: {
4779 src: "myapex-arm64.apex",
4780 },
4781 arm: {
4782 src: "myapex-arm.apex",
4783 },
4784 },
Paul Duffin37856732021-02-26 14:24:15 +00004785 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004786 }
4787
4788 java_import {
4789 name: "libfoo",
4790 jars: ["libfoo.jar"],
4791 apex_available: ["myapex"],
4792 }
4793
4794 java_library {
4795 name: "libfoo",
4796 srcs: ["foo/bar/MyClass.java"],
4797 apex_available: ["myapex"],
4798 }
Paul Duffin37856732021-02-26 14:24:15 +00004799
4800 java_sdk_library_import {
4801 name: "libbar",
4802 public: {
4803 jars: ["libbar.jar"],
4804 },
4805 apex_available: ["myapex"],
4806 }
4807
4808 java_sdk_library {
4809 name: "libbar",
4810 srcs: ["foo/bar/MyClass.java"],
4811 unsafe_ignore_missing_latest_api: true,
4812 apex_available: ["myapex"],
4813 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004814 `
4815
4816 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin55607122021-03-30 23:32:51 +01004817 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4818 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004819
4820 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4821 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004822.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin4fd997b2021-02-03 20:06:33 +00004823.intermediates/libfoo/android_common_apex10000/hiddenapi/index.csv
4824`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004825 })
4826
4827 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4828 bp := `
4829 apex {
4830 name: "myapex",
4831 enabled: false,
4832 key: "myapex.key",
4833 java_libs: ["libfoo"],
4834 }
4835
4836 apex_key {
4837 name: "myapex.key",
4838 public_key: "testkey.avbpubkey",
4839 private_key: "testkey.pem",
4840 }
4841
4842 prebuilt_apex {
4843 name: "myapex",
4844 arch: {
4845 arm64: {
4846 src: "myapex-arm64.apex",
4847 },
4848 arm: {
4849 src: "myapex-arm.apex",
4850 },
4851 },
Paul Duffin37856732021-02-26 14:24:15 +00004852 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004853 }
4854
4855 java_import {
4856 name: "libfoo",
4857 prefer: true,
4858 jars: ["libfoo.jar"],
4859 apex_available: ["myapex"],
4860 }
4861
4862 java_library {
4863 name: "libfoo",
4864 srcs: ["foo/bar/MyClass.java"],
4865 apex_available: ["myapex"],
4866 }
Paul Duffin37856732021-02-26 14:24:15 +00004867
4868 java_sdk_library_import {
4869 name: "libbar",
4870 prefer: true,
4871 public: {
4872 jars: ["libbar.jar"],
4873 },
4874 apex_available: ["myapex"],
4875 }
4876
4877 java_sdk_library {
4878 name: "libbar",
4879 srcs: ["foo/bar/MyClass.java"],
4880 unsafe_ignore_missing_latest_api: true,
4881 apex_available: ["myapex"],
4882 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004883 `
4884
4885 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin55607122021-03-30 23:32:51 +01004886 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4887 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004888
Paul Duffin9d67ca62021-02-03 20:06:33 +00004889 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4890 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004891.intermediates/prebuilt_libbar/android_common_prebuilt_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004892.intermediates/prebuilt_libfoo/android_common_prebuilt_myapex/hiddenapi/index.csv
4893`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004894 })
4895}
4896
Roland Levillain630846d2019-06-26 12:48:34 +01004897func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004898 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004899 apex_test {
4900 name: "myapex",
4901 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004902 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01004903 tests: [
4904 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004905 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004906 ],
4907 }
4908
4909 apex_key {
4910 name: "myapex.key",
4911 public_key: "testkey.avbpubkey",
4912 private_key: "testkey.pem",
4913 }
4914
Liz Kammer1c14a212020-05-12 15:26:55 -07004915 filegroup {
4916 name: "fg",
4917 srcs: [
4918 "baz",
4919 "bar/baz"
4920 ],
4921 }
4922
Roland Levillain630846d2019-06-26 12:48:34 +01004923 cc_test {
4924 name: "mytest",
4925 gtest: false,
4926 srcs: ["mytest.cpp"],
4927 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004928 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004929 system_shared_libs: [],
4930 static_executable: true,
4931 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004932 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004933 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004934
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004935 cc_library {
4936 name: "mylib",
4937 srcs: ["mylib.cpp"],
4938 system_shared_libs: [],
4939 stl: "none",
4940 }
4941
Liz Kammer5bd365f2020-05-27 15:15:11 -07004942 filegroup {
4943 name: "fg2",
4944 srcs: [
4945 "testdata/baz"
4946 ],
4947 }
4948
Roland Levillain9b5fde92019-06-28 15:41:19 +01004949 cc_test {
4950 name: "mytests",
4951 gtest: false,
4952 srcs: [
4953 "mytest1.cpp",
4954 "mytest2.cpp",
4955 "mytest3.cpp",
4956 ],
4957 test_per_src: true,
4958 relative_install_path: "test",
4959 system_shared_libs: [],
4960 static_executable: true,
4961 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004962 data: [
4963 ":fg",
4964 ":fg2",
4965 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004966 }
Roland Levillain630846d2019-06-26 12:48:34 +01004967 `)
4968
Sundong Ahnabb64432019-10-22 13:58:29 +09004969 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004970 copyCmds := apexRule.Args["copy_commands"]
4971
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004972 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004973 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004974 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004975
Liz Kammer1c14a212020-05-12 15:26:55 -07004976 //Ensure that test data are copied into apex.
4977 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4978 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4979
Roland Levillain9b5fde92019-06-28 15:41:19 +01004980 // Ensure that test deps built with `test_per_src` are copied into apex.
4981 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4982 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4983 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004984
4985 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004986 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004987 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004988 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004989 prefix := "TARGET_"
4990 var builder strings.Builder
4991 data.Custom(&builder, name, prefix, "", data)
4992 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004993 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4994 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4995 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4996 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004997 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004998 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004999 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005000
5001 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005002 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005003 data.Custom(&builder, name, prefix, "", data)
5004 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005005 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5006 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005007}
5008
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005009func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005010 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005011 apex {
5012 name: "myapex",
5013 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005014 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005015 }
5016 apex_key {
5017 name: "myapex.key",
5018 public_key: "testkey.avbpubkey",
5019 private_key: "testkey.pem",
5020 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005021 `,
5022 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5023 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5024 }),
5025 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005026 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09005027 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005028 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005029 var builder strings.Builder
5030 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5031 androidMk := builder.String()
5032 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
5033}
5034
Jooyung Hand48f3c32019-08-23 11:18:57 +09005035func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5036 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5037 apex {
5038 name: "myapex",
5039 key: "myapex.key",
5040 native_shared_libs: ["libfoo"],
5041 }
5042
5043 apex_key {
5044 name: "myapex.key",
5045 public_key: "testkey.avbpubkey",
5046 private_key: "testkey.pem",
5047 }
5048
5049 cc_library {
5050 name: "libfoo",
5051 stl: "none",
5052 system_shared_libs: [],
5053 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005054 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005055 }
5056 `)
5057 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5058 apex {
5059 name: "myapex",
5060 key: "myapex.key",
5061 java_libs: ["myjar"],
5062 }
5063
5064 apex_key {
5065 name: "myapex.key",
5066 public_key: "testkey.avbpubkey",
5067 private_key: "testkey.pem",
5068 }
5069
5070 java_library {
5071 name: "myjar",
5072 srcs: ["foo/bar/MyClass.java"],
5073 sdk_version: "none",
5074 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005075 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005076 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005077 }
5078 `)
5079}
5080
Bill Peckhama41a6962021-01-11 10:58:54 -08005081func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005082 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005083 apex {
5084 name: "myapex",
5085 key: "myapex.key",
5086 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005087 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005088 }
5089
5090 apex_key {
5091 name: "myapex.key",
5092 public_key: "testkey.avbpubkey",
5093 private_key: "testkey.pem",
5094 }
5095
5096 java_import {
5097 name: "myjavaimport",
5098 apex_available: ["myapex"],
5099 jars: ["my.jar"],
5100 compile_dex: true,
5101 }
5102 `)
5103
5104 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5105 apexRule := module.Rule("apexRule")
5106 copyCmds := apexRule.Args["copy_commands"]
5107 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5108}
5109
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005110func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005111 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005112 apex {
5113 name: "myapex",
5114 key: "myapex.key",
5115 apps: [
5116 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005117 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005118 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005119 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005120 }
5121
5122 apex_key {
5123 name: "myapex.key",
5124 public_key: "testkey.avbpubkey",
5125 private_key: "testkey.pem",
5126 }
5127
5128 android_app {
5129 name: "AppFoo",
5130 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005131 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005132 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005133 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005134 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005135 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005136 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005137
5138 android_app {
5139 name: "AppFooPriv",
5140 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005141 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005142 system_modules: "none",
5143 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005144 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005145 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005146 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005147
5148 cc_library_shared {
5149 name: "libjni",
5150 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005151 shared_libs: ["libfoo"],
5152 stl: "none",
5153 system_shared_libs: [],
5154 apex_available: [ "myapex" ],
5155 sdk_version: "current",
5156 }
5157
5158 cc_library_shared {
5159 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005160 stl: "none",
5161 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005162 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005163 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005164 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005165 `)
5166
Sundong Ahnabb64432019-10-22 13:58:29 +09005167 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005168 apexRule := module.Rule("apexRule")
5169 copyCmds := apexRule.Args["copy_commands"]
5170
5171 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005172 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005173
Colin Crossaede88c2020-08-11 12:17:01 -07005174 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005175 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005176 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005177 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005178 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005179 // JNI libraries including transitive deps are
5180 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005181 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005182 // ... embedded inside APK (jnilibs.zip)
5183 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5184 // ... and not directly inside the APEX
5185 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5186 }
Dario Frenicde2a032019-10-27 00:29:22 +01005187}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005188
Dario Frenicde2a032019-10-27 00:29:22 +01005189func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005190 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005191 apex {
5192 name: "myapex",
5193 key: "myapex.key",
5194 apps: [
5195 "AppFooPrebuilt",
5196 "AppFooPrivPrebuilt",
5197 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005198 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005199 }
5200
5201 apex_key {
5202 name: "myapex.key",
5203 public_key: "testkey.avbpubkey",
5204 private_key: "testkey.pem",
5205 }
5206
5207 android_app_import {
5208 name: "AppFooPrebuilt",
5209 apk: "PrebuiltAppFoo.apk",
5210 presigned: true,
5211 dex_preopt: {
5212 enabled: false,
5213 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005214 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005215 }
5216
5217 android_app_import {
5218 name: "AppFooPrivPrebuilt",
5219 apk: "PrebuiltAppFooPriv.apk",
5220 privileged: true,
5221 presigned: true,
5222 dex_preopt: {
5223 enabled: false,
5224 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005225 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005226 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005227 }
5228 `)
5229
Sundong Ahnabb64432019-10-22 13:58:29 +09005230 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005231 apexRule := module.Rule("apexRule")
5232 copyCmds := apexRule.Args["copy_commands"]
5233
5234 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005235 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5236}
5237
5238func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005239 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005240 apex {
5241 name: "myapex",
5242 key: "myapex.key",
5243 apps: [
5244 "AppFoo",
5245 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005246 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005247 }
5248
5249 apex_key {
5250 name: "myapex.key",
5251 public_key: "testkey.avbpubkey",
5252 private_key: "testkey.pem",
5253 }
5254
5255 android_app {
5256 name: "AppFoo",
5257 srcs: ["foo/bar/MyClass.java"],
5258 sdk_version: "none",
5259 system_modules: "none",
5260 apex_available: [ "myapex" ],
5261 }
5262
5263 android_app_import {
5264 name: "AppFoo",
5265 apk: "AppFooPrebuilt.apk",
5266 filename: "AppFooPrebuilt.apk",
5267 presigned: true,
5268 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005269 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005270 }
5271 `, withFiles(map[string][]byte{
5272 "AppFooPrebuilt.apk": nil,
5273 }))
5274
5275 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005276 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005277 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005278}
5279
Dario Freni6f3937c2019-12-20 22:58:03 +00005280func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005281 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005282 apex {
5283 name: "myapex",
5284 key: "myapex.key",
5285 apps: [
5286 "TesterHelpAppFoo",
5287 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005288 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005289 }
5290
5291 apex_key {
5292 name: "myapex.key",
5293 public_key: "testkey.avbpubkey",
5294 private_key: "testkey.pem",
5295 }
5296
5297 android_test_helper_app {
5298 name: "TesterHelpAppFoo",
5299 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005300 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005301 }
5302
5303 `)
5304
5305 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5306 apexRule := module.Rule("apexRule")
5307 copyCmds := apexRule.Args["copy_commands"]
5308
5309 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5310}
5311
Jooyung Han18020ea2019-11-13 10:50:48 +09005312func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5313 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005314 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005315 apex {
5316 name: "myapex",
5317 key: "myapex.key",
5318 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005319 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005320 }
5321
5322 apex_key {
5323 name: "myapex.key",
5324 public_key: "testkey.avbpubkey",
5325 private_key: "testkey.pem",
5326 }
5327
5328 apex {
5329 name: "otherapex",
5330 key: "myapex.key",
5331 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005332 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005333 }
5334
5335 cc_defaults {
5336 name: "libfoo-defaults",
5337 apex_available: ["otherapex"],
5338 }
5339
5340 cc_library {
5341 name: "libfoo",
5342 defaults: ["libfoo-defaults"],
5343 stl: "none",
5344 system_shared_libs: [],
5345 }`)
5346}
5347
Paul Duffine52e66f2020-03-30 17:54:29 +01005348func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005349 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005350 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005351 apex {
5352 name: "myapex",
5353 key: "myapex.key",
5354 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005355 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005356 }
5357
5358 apex_key {
5359 name: "myapex.key",
5360 public_key: "testkey.avbpubkey",
5361 private_key: "testkey.pem",
5362 }
5363
5364 apex {
5365 name: "otherapex",
5366 key: "otherapex.key",
5367 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005368 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005369 }
5370
5371 apex_key {
5372 name: "otherapex.key",
5373 public_key: "testkey.avbpubkey",
5374 private_key: "testkey.pem",
5375 }
5376
5377 cc_library {
5378 name: "libfoo",
5379 stl: "none",
5380 system_shared_libs: [],
5381 apex_available: ["otherapex"],
5382 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005383}
Jiyong Park127b40b2019-09-30 16:04:35 +09005384
Paul Duffine52e66f2020-03-30 17:54:29 +01005385func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005386 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005387 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005388.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005389.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005390.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005391.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005392.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005393.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005394 apex {
5395 name: "myapex",
5396 key: "myapex.key",
5397 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005398 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005399 }
5400
5401 apex_key {
5402 name: "myapex.key",
5403 public_key: "testkey.avbpubkey",
5404 private_key: "testkey.pem",
5405 }
5406
Jiyong Park127b40b2019-09-30 16:04:35 +09005407 cc_library {
5408 name: "libfoo",
5409 stl: "none",
5410 shared_libs: ["libbar"],
5411 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005412 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005413 }
5414
5415 cc_library {
5416 name: "libbar",
5417 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005418 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005419 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005420 apex_available: ["myapex"],
5421 }
5422
5423 cc_library {
5424 name: "libbaz",
5425 stl: "none",
5426 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005427 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005428}
Jiyong Park127b40b2019-09-30 16:04:35 +09005429
Paul Duffine52e66f2020-03-30 17:54:29 +01005430func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005431 testApexError(t, "\"otherapex\" is not a valid module name", `
5432 apex {
5433 name: "myapex",
5434 key: "myapex.key",
5435 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005436 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005437 }
5438
5439 apex_key {
5440 name: "myapex.key",
5441 public_key: "testkey.avbpubkey",
5442 private_key: "testkey.pem",
5443 }
5444
5445 cc_library {
5446 name: "libfoo",
5447 stl: "none",
5448 system_shared_libs: [],
5449 apex_available: ["otherapex"],
5450 }`)
5451
Paul Duffine52e66f2020-03-30 17:54:29 +01005452 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005453 apex {
5454 name: "myapex",
5455 key: "myapex.key",
5456 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005457 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005458 }
5459
5460 apex_key {
5461 name: "myapex.key",
5462 public_key: "testkey.avbpubkey",
5463 private_key: "testkey.pem",
5464 }
5465
5466 cc_library {
5467 name: "libfoo",
5468 stl: "none",
5469 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005470 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005471 apex_available: ["myapex"],
5472 }
5473
5474 cc_library {
5475 name: "libbar",
5476 stl: "none",
5477 system_shared_libs: [],
5478 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005479 }
5480
5481 cc_library {
5482 name: "libbaz",
5483 stl: "none",
5484 system_shared_libs: [],
5485 stubs: {
5486 versions: ["10", "20", "30"],
5487 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005488 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005489}
Jiyong Park127b40b2019-09-30 16:04:35 +09005490
Jiyong Park89e850a2020-04-07 16:37:39 +09005491func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005492 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005493 apex {
5494 name: "myapex",
5495 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005496 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005497 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005498 }
5499
5500 apex_key {
5501 name: "myapex.key",
5502 public_key: "testkey.avbpubkey",
5503 private_key: "testkey.pem",
5504 }
5505
5506 cc_library {
5507 name: "libfoo",
5508 stl: "none",
5509 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005510 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005511 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005512 }
5513
5514 cc_library {
5515 name: "libfoo2",
5516 stl: "none",
5517 system_shared_libs: [],
5518 shared_libs: ["libbaz"],
5519 apex_available: ["//apex_available:platform"],
5520 }
5521
5522 cc_library {
5523 name: "libbar",
5524 stl: "none",
5525 system_shared_libs: [],
5526 apex_available: ["myapex"],
5527 }
5528
5529 cc_library {
5530 name: "libbaz",
5531 stl: "none",
5532 system_shared_libs: [],
5533 apex_available: ["myapex"],
5534 stubs: {
5535 versions: ["1"],
5536 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005537 }`)
5538
Jiyong Park89e850a2020-04-07 16:37:39 +09005539 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5540 // because it depends on libbar which isn't available to platform
5541 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5542 if libfoo.NotAvailableForPlatform() != true {
5543 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5544 }
5545
5546 // libfoo2 however can be available to platform because it depends on libbaz which provides
5547 // stubs
5548 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5549 if libfoo2.NotAvailableForPlatform() == true {
5550 t.Errorf("%q should be available to platform", libfoo2.String())
5551 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005552}
Jiyong Parka90ca002019-10-07 15:47:24 +09005553
Paul Duffine52e66f2020-03-30 17:54:29 +01005554func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005555 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005556 apex {
5557 name: "myapex",
5558 key: "myapex.key",
5559 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005560 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005561 }
5562
5563 apex_key {
5564 name: "myapex.key",
5565 public_key: "testkey.avbpubkey",
5566 private_key: "testkey.pem",
5567 }
5568
5569 cc_library {
5570 name: "libfoo",
5571 stl: "none",
5572 system_shared_libs: [],
5573 apex_available: ["myapex"],
5574 static: {
5575 apex_available: ["//apex_available:platform"],
5576 },
5577 }`)
5578
Jiyong Park89e850a2020-04-07 16:37:39 +09005579 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5580 if libfooShared.NotAvailableForPlatform() != true {
5581 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5582 }
5583 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5584 if libfooStatic.NotAvailableForPlatform() != false {
5585 t.Errorf("%q should be available to platform", libfooStatic.String())
5586 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005587}
5588
Jiyong Park5d790c32019-11-15 18:40:32 +09005589func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005590 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005591 apex {
5592 name: "myapex",
5593 key: "myapex.key",
5594 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005595 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005596 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005597 }
5598
5599 override_apex {
5600 name: "override_myapex",
5601 base: "myapex",
5602 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005603 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005604 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005605 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09005606 }
5607
5608 apex_key {
5609 name: "myapex.key",
5610 public_key: "testkey.avbpubkey",
5611 private_key: "testkey.pem",
5612 }
5613
5614 android_app {
5615 name: "app",
5616 srcs: ["foo/bar/MyClass.java"],
5617 package_name: "foo",
5618 sdk_version: "none",
5619 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005620 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005621 }
5622
5623 override_android_app {
5624 name: "override_app",
5625 base: "app",
5626 package_name: "bar",
5627 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005628 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005629
Jiyong Park317645e2019-12-05 13:20:58 +09005630 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5631 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5632 if originalVariant.GetOverriddenBy() != "" {
5633 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5634 }
5635 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5636 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5637 }
5638
Jiyong Park5d790c32019-11-15 18:40:32 +09005639 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5640 apexRule := module.Rule("apexRule")
5641 copyCmds := apexRule.Args["copy_commands"]
5642
5643 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005644 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005645
5646 apexBundle := module.Module().(*apexBundle)
5647 name := apexBundle.Name()
5648 if name != "override_myapex" {
5649 t.Errorf("name should be \"override_myapex\", but was %q", name)
5650 }
5651
Baligh Uddin004d7172020-02-19 21:29:28 -08005652 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5653 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5654 }
5655
Jiyong Park20bacab2020-03-03 11:45:41 +09005656 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005657 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09005658
Colin Crossaa255532020-07-03 13:18:24 -07005659 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005660 var builder strings.Builder
5661 data.Custom(&builder, name, "TARGET_", "", data)
5662 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005663 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005664 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5665 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005666 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005667 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005668 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005669 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5670 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005671}
5672
Jooyung Han214bf372019-11-12 13:03:50 +09005673func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005674 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005675 apex {
5676 name: "myapex",
5677 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005678 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005679 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005680 }
5681
5682 apex_key {
5683 name: "myapex.key",
5684 public_key: "testkey.avbpubkey",
5685 private_key: "testkey.pem",
5686 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005687
5688 cc_library {
5689 name: "mylib",
5690 srcs: ["mylib.cpp"],
5691 stl: "libc++",
5692 system_shared_libs: [],
5693 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005694 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005695 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005696 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005697
5698 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5699 args := module.Rule("apexRule").Args
5700 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005701 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005702
5703 // The copies of the libraries in the apex should have one more dependency than
5704 // the ones outside the apex, namely the unwinder. Ideally we should check
5705 // the dependency names directly here but for some reason the names are blank in
5706 // this test.
5707 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005708 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005709 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5710 if len(apexImplicits) != len(nonApexImplicits)+1 {
5711 t.Errorf("%q missing unwinder dep", lib)
5712 }
5713 }
Jooyung Han214bf372019-11-12 13:03:50 +09005714}
5715
Paul Duffine05480a2021-03-08 15:07:14 +00005716var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01005717 "api/current.txt": nil,
5718 "api/removed.txt": nil,
5719 "api/system-current.txt": nil,
5720 "api/system-removed.txt": nil,
5721 "api/test-current.txt": nil,
5722 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005723
Anton Hanssondff2c782020-12-21 17:10:01 +00005724 "100/public/api/foo.txt": nil,
5725 "100/public/api/foo-removed.txt": nil,
5726 "100/system/api/foo.txt": nil,
5727 "100/system/api/foo-removed.txt": nil,
5728
Paul Duffineedc5d52020-06-12 17:46:39 +01005729 // For java_sdk_library_import
5730 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005731}
5732
Jooyung Han58f26ab2019-12-18 15:34:32 +09005733func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005734 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09005735 apex {
5736 name: "myapex",
5737 key: "myapex.key",
5738 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005739 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09005740 }
5741
5742 apex_key {
5743 name: "myapex.key",
5744 public_key: "testkey.avbpubkey",
5745 private_key: "testkey.pem",
5746 }
5747
5748 java_sdk_library {
5749 name: "foo",
5750 srcs: ["a.java"],
5751 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005752 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005753 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005754
5755 prebuilt_apis {
5756 name: "sdk",
5757 api_dirs: ["100"],
5758 }
Paul Duffin9b879592020-05-26 13:21:35 +01005759 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005760
5761 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005762 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005763 "javalib/foo.jar",
5764 "etc/permissions/foo.xml",
5765 })
5766 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005767 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5768 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005769}
5770
Paul Duffin9b879592020-05-26 13:21:35 +01005771func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005772 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005773 apex {
5774 name: "myapex",
5775 key: "myapex.key",
5776 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005777 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005778 }
5779
5780 apex_key {
5781 name: "myapex.key",
5782 public_key: "testkey.avbpubkey",
5783 private_key: "testkey.pem",
5784 }
5785
5786 java_sdk_library {
5787 name: "foo",
5788 srcs: ["a.java"],
5789 api_packages: ["foo"],
5790 apex_available: ["myapex"],
5791 sdk_version: "none",
5792 system_modules: "none",
5793 }
5794
5795 java_library {
5796 name: "bar",
5797 srcs: ["a.java"],
5798 libs: ["foo"],
5799 apex_available: ["myapex"],
5800 sdk_version: "none",
5801 system_modules: "none",
5802 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005803
5804 prebuilt_apis {
5805 name: "sdk",
5806 api_dirs: ["100"],
5807 }
Paul Duffin9b879592020-05-26 13:21:35 +01005808 `, withFiles(filesForSdkLibrary))
5809
5810 // java_sdk_library installs both impl jar and permission XML
5811 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5812 "javalib/bar.jar",
5813 "javalib/foo.jar",
5814 "etc/permissions/foo.xml",
5815 })
5816
5817 // The bar library should depend on the implementation jar.
5818 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005819 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005820 t.Errorf("expected %q, found %#q", expected, actual)
5821 }
5822}
5823
5824func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005825 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005826 apex {
5827 name: "myapex",
5828 key: "myapex.key",
5829 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005830 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005831 }
5832
5833 apex_key {
5834 name: "myapex.key",
5835 public_key: "testkey.avbpubkey",
5836 private_key: "testkey.pem",
5837 }
5838
5839 java_sdk_library {
5840 name: "foo",
5841 srcs: ["a.java"],
5842 api_packages: ["foo"],
5843 apex_available: ["myapex"],
5844 sdk_version: "none",
5845 system_modules: "none",
5846 }
5847
5848 java_library {
5849 name: "bar",
5850 srcs: ["a.java"],
5851 libs: ["foo"],
5852 sdk_version: "none",
5853 system_modules: "none",
5854 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005855
5856 prebuilt_apis {
5857 name: "sdk",
5858 api_dirs: ["100"],
5859 }
Paul Duffin9b879592020-05-26 13:21:35 +01005860 `, withFiles(filesForSdkLibrary))
5861
5862 // java_sdk_library installs both impl jar and permission XML
5863 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5864 "javalib/foo.jar",
5865 "etc/permissions/foo.xml",
5866 })
5867
5868 // The bar library should depend on the stubs jar.
5869 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005870 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005871 t.Errorf("expected %q, found %#q", expected, actual)
5872 }
5873}
5874
Paul Duffineedc5d52020-06-12 17:46:39 +01005875func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005876 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00005877 prebuilt_apis {
5878 name: "sdk",
5879 api_dirs: ["100"],
5880 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005881 withFiles(map[string][]byte{
5882 "apex/a.java": nil,
5883 "apex/apex_manifest.json": nil,
5884 "apex/Android.bp": []byte(`
5885 package {
5886 default_visibility: ["//visibility:private"],
5887 }
5888
5889 apex {
5890 name: "myapex",
5891 key: "myapex.key",
5892 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005893 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005894 }
5895
5896 apex_key {
5897 name: "myapex.key",
5898 public_key: "testkey.avbpubkey",
5899 private_key: "testkey.pem",
5900 }
5901
5902 java_library {
5903 name: "bar",
5904 srcs: ["a.java"],
5905 libs: ["foo"],
5906 apex_available: ["myapex"],
5907 sdk_version: "none",
5908 system_modules: "none",
5909 }
5910`),
5911 "source/a.java": nil,
5912 "source/api/current.txt": nil,
5913 "source/api/removed.txt": nil,
5914 "source/Android.bp": []byte(`
5915 package {
5916 default_visibility: ["//visibility:private"],
5917 }
5918
5919 java_sdk_library {
5920 name: "foo",
5921 visibility: ["//apex"],
5922 srcs: ["a.java"],
5923 api_packages: ["foo"],
5924 apex_available: ["myapex"],
5925 sdk_version: "none",
5926 system_modules: "none",
5927 public: {
5928 enabled: true,
5929 },
5930 }
5931`),
5932 "prebuilt/a.jar": nil,
5933 "prebuilt/Android.bp": []byte(`
5934 package {
5935 default_visibility: ["//visibility:private"],
5936 }
5937
5938 java_sdk_library_import {
5939 name: "foo",
5940 visibility: ["//apex", "//source"],
5941 apex_available: ["myapex"],
5942 prefer: true,
5943 public: {
5944 jars: ["a.jar"],
5945 },
5946 }
5947`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005948 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005949 )
5950
5951 // java_sdk_library installs both impl jar and permission XML
5952 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5953 "javalib/bar.jar",
5954 "javalib/foo.jar",
5955 "etc/permissions/foo.xml",
5956 })
5957
5958 // The bar library should depend on the implementation jar.
5959 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005960 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01005961 t.Errorf("expected %q, found %#q", expected, actual)
5962 }
5963}
5964
5965func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5966 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5967 apex {
5968 name: "myapex",
5969 key: "myapex.key",
5970 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005971 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005972 }
5973
5974 apex_key {
5975 name: "myapex.key",
5976 public_key: "testkey.avbpubkey",
5977 private_key: "testkey.pem",
5978 }
5979
5980 java_sdk_library_import {
5981 name: "foo",
5982 apex_available: ["myapex"],
5983 prefer: true,
5984 public: {
5985 jars: ["a.jar"],
5986 },
5987 }
5988
5989 `, withFiles(filesForSdkLibrary))
5990}
5991
atrost6e126252020-01-27 17:01:16 +00005992func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01005993 result := android.GroupFixturePreparers(
5994 prepareForApexTest,
5995 java.PrepareForTestWithPlatformCompatConfig,
5996 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00005997 apex {
5998 name: "myapex",
5999 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00006000 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00006001 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006002 updatable: false,
atrost6e126252020-01-27 17:01:16 +00006003 }
6004
6005 apex_key {
6006 name: "myapex.key",
6007 public_key: "testkey.avbpubkey",
6008 private_key: "testkey.pem",
6009 }
6010
6011 platform_compat_config {
6012 name: "myjar-platform-compat-config",
6013 src: ":myjar",
6014 }
6015
6016 java_library {
6017 name: "myjar",
6018 srcs: ["foo/bar/MyClass.java"],
6019 sdk_version: "none",
6020 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00006021 apex_available: [ "myapex" ],
6022 }
Paul Duffin1b29e002021-03-16 15:06:54 +00006023
6024 // Make sure that a preferred prebuilt does not affect the apex contents.
6025 prebuilt_platform_compat_config {
6026 name: "myjar-platform-compat-config",
6027 metadata: "compat-config/metadata.xml",
6028 prefer: true,
6029 }
atrost6e126252020-01-27 17:01:16 +00006030 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00006031 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00006032 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6033 "etc/compatconfig/myjar-platform-compat-config.xml",
6034 "javalib/myjar.jar",
6035 })
6036}
6037
Jiyong Park479321d2019-12-16 11:47:12 +09006038func TestRejectNonInstallableJavaLibrary(t *testing.T) {
6039 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
6040 apex {
6041 name: "myapex",
6042 key: "myapex.key",
6043 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006044 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09006045 }
6046
6047 apex_key {
6048 name: "myapex.key",
6049 public_key: "testkey.avbpubkey",
6050 private_key: "testkey.pem",
6051 }
6052
6053 java_library {
6054 name: "myjar",
6055 srcs: ["foo/bar/MyClass.java"],
6056 sdk_version: "none",
6057 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09006058 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006059 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09006060 }
6061 `)
6062}
6063
Jiyong Park7afd1072019-12-30 16:56:33 +09006064func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006065 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09006066 apex {
6067 name: "myapex",
6068 key: "myapex.key",
6069 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006070 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09006071 }
6072
6073 apex_key {
6074 name: "myapex.key",
6075 public_key: "testkey.avbpubkey",
6076 private_key: "testkey.pem",
6077 }
6078
6079 cc_library {
6080 name: "mylib",
6081 srcs: ["mylib.cpp"],
6082 system_shared_libs: [],
6083 stl: "none",
6084 required: ["a", "b"],
6085 host_required: ["c", "d"],
6086 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006087 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006088 }
6089 `)
6090
6091 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006092 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006093 name := apexBundle.BaseModuleName()
6094 prefix := "TARGET_"
6095 var builder strings.Builder
6096 data.Custom(&builder, name, prefix, "", data)
6097 androidMk := builder.String()
6098 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6099 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6100 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6101}
6102
Jiyong Park7cd10e32020-01-14 09:22:18 +09006103func TestSymlinksFromApexToSystem(t *testing.T) {
6104 bp := `
6105 apex {
6106 name: "myapex",
6107 key: "myapex.key",
6108 native_shared_libs: ["mylib"],
6109 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006110 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006111 }
6112
Jiyong Park9d677202020-02-19 16:29:35 +09006113 apex {
6114 name: "myapex.updatable",
6115 key: "myapex.key",
6116 native_shared_libs: ["mylib"],
6117 java_libs: ["myjar"],
6118 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006119 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006120 }
6121
Jiyong Park7cd10e32020-01-14 09:22:18 +09006122 apex_key {
6123 name: "myapex.key",
6124 public_key: "testkey.avbpubkey",
6125 private_key: "testkey.pem",
6126 }
6127
6128 cc_library {
6129 name: "mylib",
6130 srcs: ["mylib.cpp"],
6131 shared_libs: ["myotherlib"],
6132 system_shared_libs: [],
6133 stl: "none",
6134 apex_available: [
6135 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006136 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006137 "//apex_available:platform",
6138 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006139 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006140 }
6141
6142 cc_library {
6143 name: "myotherlib",
6144 srcs: ["mylib.cpp"],
6145 system_shared_libs: [],
6146 stl: "none",
6147 apex_available: [
6148 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006149 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006150 "//apex_available:platform",
6151 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006152 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006153 }
6154
6155 java_library {
6156 name: "myjar",
6157 srcs: ["foo/bar/MyClass.java"],
6158 sdk_version: "none",
6159 system_modules: "none",
6160 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006161 apex_available: [
6162 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006163 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006164 "//apex_available:platform",
6165 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006166 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006167 }
6168
6169 java_library {
6170 name: "myotherjar",
6171 srcs: ["foo/bar/MyClass.java"],
6172 sdk_version: "none",
6173 system_modules: "none",
6174 apex_available: [
6175 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006176 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006177 "//apex_available:platform",
6178 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006179 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006180 }
6181 `
6182
6183 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6184 for _, f := range files {
6185 if f.path == file {
6186 if f.isLink {
6187 t.Errorf("%q is not a real file", file)
6188 }
6189 return
6190 }
6191 }
6192 t.Errorf("%q is not found", file)
6193 }
6194
6195 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6196 for _, f := range files {
6197 if f.path == file {
6198 if !f.isLink {
6199 t.Errorf("%q is not a symlink", file)
6200 }
6201 return
6202 }
6203 }
6204 t.Errorf("%q is not found", file)
6205 }
6206
Jiyong Park9d677202020-02-19 16:29:35 +09006207 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6208 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006209 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006210 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006211 ensureRealfileExists(t, files, "javalib/myjar.jar")
6212 ensureRealfileExists(t, files, "lib64/mylib.so")
6213 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6214
Jiyong Park9d677202020-02-19 16:29:35 +09006215 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6216 ensureRealfileExists(t, files, "javalib/myjar.jar")
6217 ensureRealfileExists(t, files, "lib64/mylib.so")
6218 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6219
6220 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006221 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006222 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006223 ensureRealfileExists(t, files, "javalib/myjar.jar")
6224 ensureRealfileExists(t, files, "lib64/mylib.so")
6225 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006226
6227 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6228 ensureRealfileExists(t, files, "javalib/myjar.jar")
6229 ensureRealfileExists(t, files, "lib64/mylib.so")
6230 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006231}
6232
Yo Chiange8128052020-07-23 20:09:18 +08006233func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006234 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006235 apex {
6236 name: "myapex",
6237 key: "myapex.key",
6238 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006239 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006240 }
6241
6242 apex_key {
6243 name: "myapex.key",
6244 public_key: "testkey.avbpubkey",
6245 private_key: "testkey.pem",
6246 }
6247
6248 cc_library_shared {
6249 name: "mylib",
6250 srcs: ["mylib.cpp"],
6251 shared_libs: ["myotherlib"],
6252 system_shared_libs: [],
6253 stl: "none",
6254 apex_available: [
6255 "myapex",
6256 "//apex_available:platform",
6257 ],
6258 }
6259
6260 cc_prebuilt_library_shared {
6261 name: "myotherlib",
6262 srcs: ["prebuilt.so"],
6263 system_shared_libs: [],
6264 stl: "none",
6265 apex_available: [
6266 "myapex",
6267 "//apex_available:platform",
6268 ],
6269 }
6270 `)
6271
6272 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006273 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006274 var builder strings.Builder
6275 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6276 androidMk := builder.String()
6277 // `myotherlib` is added to `myapex` as symlink
6278 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6279 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6280 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6281 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006282 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 +08006283}
6284
Jooyung Han643adc42020-02-27 13:50:06 +09006285func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006286 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006287 apex {
6288 name: "myapex",
6289 key: "myapex.key",
6290 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006291 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006292 }
6293
6294 apex_key {
6295 name: "myapex.key",
6296 public_key: "testkey.avbpubkey",
6297 private_key: "testkey.pem",
6298 }
6299
6300 cc_library {
6301 name: "mylib",
6302 srcs: ["mylib.cpp"],
6303 shared_libs: ["mylib2"],
6304 system_shared_libs: [],
6305 stl: "none",
6306 apex_available: [ "myapex" ],
6307 }
6308
6309 cc_library {
6310 name: "mylib2",
6311 srcs: ["mylib.cpp"],
6312 system_shared_libs: [],
6313 stl: "none",
6314 apex_available: [ "myapex" ],
6315 }
6316 `)
6317
6318 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6319 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6320 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6321 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6322 "lib64/mylib.so",
6323 "lib64/mylib2.so",
6324 })
6325}
6326
Jooyung Han49f67012020-04-17 13:43:10 +09006327func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006328 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006329 apex {
6330 name: "myapex",
6331 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006332 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006333 }
6334 apex_key {
6335 name: "myapex.key",
6336 public_key: "testkey.avbpubkey",
6337 private_key: "testkey.pem",
6338 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006339 `,
6340 android.FixtureModifyConfig(func(config android.Config) {
6341 delete(config.Targets, android.Android)
6342 config.AndroidCommonTarget = android.Target{}
6343 }),
6344 )
Jooyung Han49f67012020-04-17 13:43:10 +09006345
6346 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6347 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6348 }
6349}
6350
Jiyong Parkbd159612020-02-28 15:22:21 +09006351func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006352 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006353 apex {
6354 name: "myapex",
6355 key: "myapex.key",
6356 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006357 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006358 }
6359
6360 apex_key {
6361 name: "myapex.key",
6362 public_key: "testkey.avbpubkey",
6363 private_key: "testkey.pem",
6364 }
6365
6366 android_app {
6367 name: "AppFoo",
6368 srcs: ["foo/bar/MyClass.java"],
6369 sdk_version: "none",
6370 system_modules: "none",
6371 apex_available: [ "myapex" ],
6372 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006373 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006374
Colin Crosscf371cc2020-11-13 11:48:42 -08006375 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006376 content := bundleConfigRule.Args["content"]
6377
6378 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006379 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 +09006380}
6381
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006382func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006383 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006384 apex {
6385 name: "myapex",
6386 key: "myapex.key",
6387 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006388 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006389 }
6390
6391 apex_key {
6392 name: "myapex.key",
6393 public_key: "testkey.avbpubkey",
6394 private_key: "testkey.pem",
6395 }
6396
6397 android_app_set {
6398 name: "AppSet",
6399 set: "AppSet.apks",
6400 }`)
6401 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006402 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006403 content := bundleConfigRule.Args["content"]
6404 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6405 s := mod.Rule("apexRule").Args["copy_commands"]
6406 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6407 if len(copyCmds) != 3 {
6408 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6409 }
6410 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6411 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6412 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6413}
6414
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006415func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01006416 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006417 apex_set {
6418 name: "myapex",
6419 filename: "foo_v2.apex",
6420 sanitized: {
6421 none: { set: "myapex.apks", },
6422 hwaddress: { set: "myapex.hwasan.apks", },
6423 },
Paul Duffin24704672021-04-06 16:09:30 +01006424 }
6425 `
6426 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006427
Paul Duffin24704672021-04-06 16:09:30 +01006428 // Check that the extractor produces the correct output file from the correct input file.
6429 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006430
Paul Duffin24704672021-04-06 16:09:30 +01006431 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
6432 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006433
Paul Duffin24704672021-04-06 16:09:30 +01006434 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
6435
6436 // Ditto for the apex.
6437 m = ctx.ModuleForTests("myapex", "android_common")
6438 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common/foo_v2.apex")
6439
6440 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006441}
6442
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006443func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006444 t.Helper()
6445
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006446 bp := `
6447 java_library {
6448 name: "some-updatable-apex-lib",
6449 srcs: ["a.java"],
6450 sdk_version: "current",
6451 apex_available: [
6452 "some-updatable-apex",
6453 ],
6454 }
6455
6456 java_library {
6457 name: "some-non-updatable-apex-lib",
6458 srcs: ["a.java"],
6459 apex_available: [
6460 "some-non-updatable-apex",
6461 ],
6462 }
6463
6464 java_library {
6465 name: "some-platform-lib",
6466 srcs: ["a.java"],
6467 sdk_version: "current",
6468 installable: true,
6469 }
6470
6471 java_library {
6472 name: "some-art-lib",
6473 srcs: ["a.java"],
6474 sdk_version: "current",
6475 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006476 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006477 ],
6478 hostdex: true,
6479 }
6480
6481 apex {
6482 name: "some-updatable-apex",
6483 key: "some-updatable-apex.key",
6484 java_libs: ["some-updatable-apex-lib"],
6485 updatable: true,
6486 min_sdk_version: "current",
6487 }
6488
6489 apex {
6490 name: "some-non-updatable-apex",
6491 key: "some-non-updatable-apex.key",
6492 java_libs: ["some-non-updatable-apex-lib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006493 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006494 }
6495
6496 apex_key {
6497 name: "some-updatable-apex.key",
6498 }
6499
6500 apex_key {
6501 name: "some-non-updatable-apex.key",
6502 }
6503
6504 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006505 name: "com.android.art.debug",
6506 key: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006507 java_libs: ["some-art-lib"],
6508 updatable: true,
6509 min_sdk_version: "current",
6510 }
6511
6512 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006513 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006514 }
6515
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006516 filegroup {
6517 name: "some-updatable-apex-file_contexts",
6518 srcs: [
6519 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6520 ],
6521 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006522
6523 filegroup {
6524 name: "some-non-updatable-apex-file_contexts",
6525 srcs: [
6526 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6527 ],
6528 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006529 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006530
6531 testDexpreoptWithApexes(t, bp, errmsg, transformDexpreoptConfig)
6532}
6533
Paul Duffin064b70c2020-11-02 17:32:38 +00006534func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006535 t.Helper()
6536
Paul Duffin55607122021-03-30 23:32:51 +01006537 fs := android.MockFS{
6538 "a.java": nil,
6539 "a.jar": nil,
6540 "apex_manifest.json": nil,
6541 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006542 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006543 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6544 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6545 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006546 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006547 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006548
Paul Duffin55607122021-03-30 23:32:51 +01006549 errorHandler := android.FixtureExpectsNoErrors
6550 if errmsg != "" {
6551 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006552 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006553
Paul Duffin55607122021-03-30 23:32:51 +01006554 result := android.GroupFixturePreparers(
6555 cc.PrepareForTestWithCcDefaultModules,
6556 java.PrepareForTestWithHiddenApiBuildComponents,
6557 java.PrepareForTestWithJavaDefaultModules,
6558 java.PrepareForTestWithJavaSdkLibraryFiles,
6559 PrepareForTestWithApexBuildComponents,
6560 android.FixtureModifyConfig(func(config android.Config) {
6561 pathCtx := android.PathContextForTesting(config)
6562 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
6563 transformDexpreoptConfig(dexpreoptConfig)
6564 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
6565
6566 // Make sure that any changes to these dexpreopt properties are mirrored in the corresponding
6567 // product variables.
6568 config.TestProductVariables.BootJars = dexpreoptConfig.BootJars
6569 config.TestProductVariables.UpdatableBootJars = dexpreoptConfig.UpdatableBootJars
6570 }),
6571 fs.AddToFixture(),
6572 ).
6573 ExtendWithErrorHandler(errorHandler).
6574 RunTestWithBp(t, bp)
6575
6576 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006577}
6578
Jooyung Han548640b2020-04-27 12:10:30 +09006579func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6580 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6581 apex {
6582 name: "myapex",
6583 key: "myapex.key",
6584 updatable: true,
6585 }
6586
6587 apex_key {
6588 name: "myapex.key",
6589 public_key: "testkey.avbpubkey",
6590 private_key: "testkey.pem",
6591 }
6592 `)
6593}
6594
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006595func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6596 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6597 apex {
6598 name: "myapex",
6599 key: "myapex.key",
6600 }
6601
6602 apex_key {
6603 name: "myapex.key",
6604 public_key: "testkey.avbpubkey",
6605 private_key: "testkey.pem",
6606 }
6607 `)
6608}
6609
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006610func TestNoUpdatableJarsInBootImage(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006611 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006612 var transform func(*dexpreopt.GlobalConfig)
6613
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006614 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
6615 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006616 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006617 }
6618 testNoUpdatableJarsInBootImage(t, "", transform)
6619 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006620
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006621 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffind376f792021-01-26 11:59:35 +00006622 err = `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006623 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006624 config.BootJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006625 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006626 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006627 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006628
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006629 t.Run("updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006630 err = `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006631 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006632 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006633 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006634 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006635 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006636
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006637 t.Run("non-updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006638 err = `module "some-non-updatable-apex-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006639 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006640 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006641 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006642 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006643 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006644
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006645 t.Run("updatable jar from some other apex in the framework boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006646 err = `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the framework boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006647 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006648 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006649 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006650 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006651 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006652
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006653 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
6654 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006655 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006656 }
6657 testNoUpdatableJarsInBootImage(t, "", transform)
6658 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006659
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006660 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006661 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006662 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006663 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006664 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006665 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006666 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006667
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006668 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006669 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006670 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006671 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006672 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006673 testNoUpdatableJarsInBootImage(t, err, transform)
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) {
Colin Crossaede88c2020-08-11 12:17:01 -07006677 err = `module "some-platform-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006678 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006679 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006680 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006681 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006682 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006683
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006684 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
6685 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006686 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006687 }
6688 testNoUpdatableJarsInBootImage(t, "", transform)
6689 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006690
6691}
6692
6693func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
6694 transform := func(config *dexpreopt.GlobalConfig) {
6695 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
6696 }
6697 t.Run("prebuilt no source", func(t *testing.T) {
6698 testDexpreoptWithApexes(t, `
6699 prebuilt_apex {
6700 name: "myapex" ,
6701 arch: {
6702 arm64: {
6703 src: "myapex-arm64.apex",
6704 },
6705 arm: {
6706 src: "myapex-arm.apex",
6707 },
6708 },
6709 exported_java_libs: ["libfoo"],
6710 }
6711
6712 java_import {
6713 name: "libfoo",
6714 jars: ["libfoo.jar"],
6715 }
6716`, "", transform)
6717 })
6718
6719 t.Run("prebuilt no source", func(t *testing.T) {
6720 testDexpreoptWithApexes(t, `
6721 prebuilt_apex {
6722 name: "myapex" ,
6723 arch: {
6724 arm64: {
6725 src: "myapex-arm64.apex",
6726 },
6727 arm: {
6728 src: "myapex-arm.apex",
6729 },
6730 },
6731 exported_java_libs: ["libfoo"],
6732 }
6733
6734 java_import {
6735 name: "libfoo",
6736 jars: ["libfoo.jar"],
6737 }
6738`, "", transform)
6739 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006740}
6741
Andrei Onea115e7e72020-06-05 21:14:03 +01006742func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6743 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006744 bp += `
6745 apex_key {
6746 name: "myapex.key",
6747 public_key: "testkey.avbpubkey",
6748 private_key: "testkey.pem",
6749 }`
Paul Duffin45338f02021-03-30 23:07:52 +01006750 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01006751 "lib1/src/A.java": nil,
6752 "lib2/src/B.java": nil,
6753 "system/sepolicy/apex/myapex-file_contexts": nil,
6754 }
6755
Paul Duffin45338f02021-03-30 23:07:52 +01006756 errorHandler := android.FixtureExpectsNoErrors
6757 if errmsg != "" {
6758 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07006759 }
Colin Crossae8600b2020-10-29 17:09:13 -07006760
Paul Duffin45338f02021-03-30 23:07:52 +01006761 android.GroupFixturePreparers(
6762 android.PrepareForTestWithAndroidBuildComponents,
6763 java.PrepareForTestWithJavaBuildComponents,
6764 PrepareForTestWithApexBuildComponents,
6765 android.PrepareForTestWithNeverallowRules(rules),
6766 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6767 updatableBootJars := make([]string, 0, len(apexBootJars))
6768 for _, apexBootJar := range apexBootJars {
6769 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6770 }
6771 variables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6772 }),
6773 fs.AddToFixture(),
6774 ).
6775 ExtendWithErrorHandler(errorHandler).
6776 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01006777}
6778
6779func TestApexPermittedPackagesRules(t *testing.T) {
6780 testcases := []struct {
6781 name string
6782 expectedError string
6783 bp string
6784 bootJars []string
6785 modulesPackages map[string][]string
6786 }{
6787
6788 {
6789 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6790 expectedError: "",
6791 bp: `
6792 java_library {
6793 name: "bcp_lib1",
6794 srcs: ["lib1/src/*.java"],
6795 permitted_packages: ["foo.bar"],
6796 apex_available: ["myapex"],
6797 sdk_version: "none",
6798 system_modules: "none",
6799 }
6800 java_library {
6801 name: "nonbcp_lib2",
6802 srcs: ["lib2/src/*.java"],
6803 apex_available: ["myapex"],
6804 permitted_packages: ["a.b"],
6805 sdk_version: "none",
6806 system_modules: "none",
6807 }
6808 apex {
6809 name: "myapex",
6810 key: "myapex.key",
6811 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006812 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006813 }`,
6814 bootJars: []string{"bcp_lib1"},
6815 modulesPackages: map[string][]string{
6816 "myapex": []string{
6817 "foo.bar",
6818 },
6819 },
6820 },
6821 {
6822 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6823 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.`,
6824 bp: `
6825 java_library {
6826 name: "bcp_lib1",
6827 srcs: ["lib1/src/*.java"],
6828 apex_available: ["myapex"],
6829 permitted_packages: ["foo.bar"],
6830 sdk_version: "none",
6831 system_modules: "none",
6832 }
6833 java_library {
6834 name: "bcp_lib2",
6835 srcs: ["lib2/src/*.java"],
6836 apex_available: ["myapex"],
6837 permitted_packages: ["foo.bar", "bar.baz"],
6838 sdk_version: "none",
6839 system_modules: "none",
6840 }
6841 apex {
6842 name: "myapex",
6843 key: "myapex.key",
6844 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006845 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006846 }
6847 `,
6848 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6849 modulesPackages: map[string][]string{
6850 "myapex": []string{
6851 "foo.bar",
6852 },
6853 },
6854 },
6855 }
6856 for _, tc := range testcases {
6857 t.Run(tc.name, func(t *testing.T) {
6858 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6859 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6860 })
6861 }
6862}
6863
Jiyong Park62304bb2020-04-13 16:19:48 +09006864func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006865 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09006866 apex {
6867 name: "myapex",
6868 key: "myapex.key",
6869 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006870 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09006871 }
6872
6873 apex_key {
6874 name: "myapex.key",
6875 public_key: "testkey.avbpubkey",
6876 private_key: "testkey.pem",
6877 }
6878
6879 cc_library {
6880 name: "mylib",
6881 srcs: ["mylib.cpp"],
6882 system_shared_libs: [],
6883 stl: "none",
6884 stubs: {
6885 versions: ["1"],
6886 },
6887 apex_available: ["myapex"],
6888 }
6889
6890 cc_library {
6891 name: "myprivlib",
6892 srcs: ["mylib.cpp"],
6893 system_shared_libs: [],
6894 stl: "none",
6895 apex_available: ["myapex"],
6896 }
6897
6898
6899 cc_test {
6900 name: "mytest",
6901 gtest: false,
6902 srcs: ["mylib.cpp"],
6903 system_shared_libs: [],
6904 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006905 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006906 test_for: ["myapex"]
6907 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006908
6909 cc_library {
6910 name: "mytestlib",
6911 srcs: ["mylib.cpp"],
6912 system_shared_libs: [],
6913 shared_libs: ["mylib", "myprivlib"],
6914 stl: "none",
6915 test_for: ["myapex"],
6916 }
6917
6918 cc_benchmark {
6919 name: "mybench",
6920 srcs: ["mylib.cpp"],
6921 system_shared_libs: [],
6922 shared_libs: ["mylib", "myprivlib"],
6923 stl: "none",
6924 test_for: ["myapex"],
6925 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006926 `)
6927
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006928 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01006929 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006930 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6931 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6932 }
6933
6934 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09006935 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006936 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6937 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6938 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6939}
Jiyong Park46a512f2020-12-04 18:02:13 +09006940
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006941func TestIndirectTestFor(t *testing.T) {
6942 ctx := testApex(t, `
6943 apex {
6944 name: "myapex",
6945 key: "myapex.key",
6946 native_shared_libs: ["mylib", "myprivlib"],
6947 updatable: false,
6948 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006949
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006950 apex_key {
6951 name: "myapex.key",
6952 public_key: "testkey.avbpubkey",
6953 private_key: "testkey.pem",
6954 }
6955
6956 cc_library {
6957 name: "mylib",
6958 srcs: ["mylib.cpp"],
6959 system_shared_libs: [],
6960 stl: "none",
6961 stubs: {
6962 versions: ["1"],
6963 },
6964 apex_available: ["myapex"],
6965 }
6966
6967 cc_library {
6968 name: "myprivlib",
6969 srcs: ["mylib.cpp"],
6970 system_shared_libs: [],
6971 stl: "none",
6972 shared_libs: ["mylib"],
6973 apex_available: ["myapex"],
6974 }
6975
6976 cc_library {
6977 name: "mytestlib",
6978 srcs: ["mylib.cpp"],
6979 system_shared_libs: [],
6980 shared_libs: ["myprivlib"],
6981 stl: "none",
6982 test_for: ["myapex"],
6983 }
6984 `)
6985
6986 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01006987 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006988 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6989 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6990 }
6991
6992 // The platform variant of mytestlib links to the platform variant of the
6993 // internal myprivlib.
6994 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
6995
6996 // The platform variant of myprivlib links to the platform variant of mylib
6997 // and bypasses its stubs.
6998 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 +09006999}
7000
Martin Stjernholmec009002021-03-27 15:18:31 +00007001func TestTestForForLibInOtherApex(t *testing.T) {
7002 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
7003 _ = testApex(t, `
7004 apex {
7005 name: "com.android.art",
7006 key: "myapex.key",
7007 native_shared_libs: ["mylib"],
7008 updatable: false,
7009 }
7010
7011 apex {
7012 name: "com.android.art.debug",
7013 key: "myapex.key",
7014 native_shared_libs: ["mylib", "mytestlib"],
7015 updatable: false,
7016 }
7017
7018 apex_key {
7019 name: "myapex.key",
7020 public_key: "testkey.avbpubkey",
7021 private_key: "testkey.pem",
7022 }
7023
7024 cc_library {
7025 name: "mylib",
7026 srcs: ["mylib.cpp"],
7027 system_shared_libs: [],
7028 stl: "none",
7029 stubs: {
7030 versions: ["1"],
7031 },
7032 apex_available: ["com.android.art", "com.android.art.debug"],
7033 }
7034
7035 cc_library {
7036 name: "mytestlib",
7037 srcs: ["mylib.cpp"],
7038 system_shared_libs: [],
7039 shared_libs: ["mylib"],
7040 stl: "none",
7041 apex_available: ["com.android.art.debug"],
7042 test_for: ["com.android.art"],
7043 }
7044 `,
7045 android.MockFS{
7046 "system/sepolicy/apex/com.android.art-file_contexts": nil,
7047 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
7048 }.AddToFixture())
7049}
7050
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007051// TODO(jungjw): Move this to proptools
7052func intPtr(i int) *int {
7053 return &i
7054}
7055
7056func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007057 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007058 apex_set {
7059 name: "myapex",
7060 set: "myapex.apks",
7061 filename: "foo_v2.apex",
7062 overrides: ["foo"],
7063 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007064 `,
7065 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7066 variables.Platform_sdk_version = intPtr(30)
7067 }),
7068 android.FixtureModifyConfig(func(config android.Config) {
7069 config.Targets[android.Android] = []android.Target{
7070 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
7071 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
7072 }
7073 }),
7074 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007075
Paul Duffin24704672021-04-06 16:09:30 +01007076 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007077
7078 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01007079 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007080 actual := extractedApex.Args["abis"]
7081 expected := "ARMEABI_V7A,ARM64_V8A"
7082 if actual != expected {
7083 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7084 }
7085 actual = extractedApex.Args["sdk-version"]
7086 expected = "30"
7087 if actual != expected {
7088 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7089 }
7090
Paul Duffin24704672021-04-06 16:09:30 +01007091 m = ctx.ModuleForTests("myapex", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007092 a := m.Module().(*ApexSet)
7093 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07007094 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007095 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
7096 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
7097 }
7098}
7099
Jiyong Park7d95a512020-05-10 15:16:24 +09007100func TestNoStaticLinkingToStubsLib(t *testing.T) {
7101 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
7102 apex {
7103 name: "myapex",
7104 key: "myapex.key",
7105 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007106 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09007107 }
7108
7109 apex_key {
7110 name: "myapex.key",
7111 public_key: "testkey.avbpubkey",
7112 private_key: "testkey.pem",
7113 }
7114
7115 cc_library {
7116 name: "mylib",
7117 srcs: ["mylib.cpp"],
7118 static_libs: ["otherlib"],
7119 system_shared_libs: [],
7120 stl: "none",
7121 apex_available: [ "myapex" ],
7122 }
7123
7124 cc_library {
7125 name: "otherlib",
7126 srcs: ["mylib.cpp"],
7127 system_shared_libs: [],
7128 stl: "none",
7129 stubs: {
7130 versions: ["1", "2", "3"],
7131 },
7132 apex_available: [ "myapex" ],
7133 }
7134 `)
7135}
7136
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007137func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007138 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007139 apex {
7140 name: "myapex",
7141 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007142 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007143 }
7144
7145 apex_key {
7146 name: "myapex.key",
7147 public_key: "testkey.avbpubkey",
7148 private_key: "testkey.pem",
7149 }
7150
7151 prebuilt_apex {
7152 name: "myapex",
7153 prefer: true,
7154 arch: {
7155 arm64: {
7156 src: "myapex-arm64.apex",
7157 },
7158 arm: {
7159 src: "myapex-arm.apex",
7160 },
7161 },
7162 }
7163
7164 apex_set {
7165 name: "myapex_set",
7166 set: "myapex.apks",
7167 filename: "myapex_set.apex",
7168 overrides: ["myapex"],
7169 }
7170 `)
7171
7172 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7173 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7174 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 +09007175 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 +09007176}
7177
Jooyung Han938b5932020-06-20 12:47:47 +09007178func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007179 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007180 apex {
7181 name: "myapex",
7182 key: "myapex.key",
7183 apps: ["app"],
7184 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007185 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007186 }
7187
7188 apex_key {
7189 name: "myapex.key",
7190 public_key: "testkey.avbpubkey",
7191 private_key: "testkey.pem",
7192 }
7193
7194 android_app {
7195 name: "app",
7196 srcs: ["foo/bar/MyClass.java"],
7197 package_name: "foo",
7198 sdk_version: "none",
7199 system_modules: "none",
7200 apex_available: [ "myapex" ],
7201 }
7202 `, withFiles(map[string][]byte{
7203 "sub/Android.bp": []byte(`
7204 override_apex {
7205 name: "override_myapex",
7206 base: "myapex",
7207 apps: ["override_app"],
7208 allowed_files: ":allowed",
7209 }
7210 // Overridable "path" property should be referenced indirectly
7211 filegroup {
7212 name: "allowed",
7213 srcs: ["allowed.txt"],
7214 }
7215 override_android_app {
7216 name: "override_app",
7217 base: "app",
7218 package_name: "bar",
7219 }
7220 `),
7221 }))
7222
7223 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7224 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7225 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7226 }
7227
7228 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7229 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7230 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7231 }
7232}
7233
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007234func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007235 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007236 apex {
7237 name: "myapex",
7238 key: "myapex.key",
7239 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007240 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007241 }
7242
7243 apex_key {
7244 name: "myapex.key",
7245 public_key: "testkey.avbpubkey",
7246 private_key: "testkey.pem",
7247 }
7248
7249 cc_library {
7250 name: "mylib",
7251 srcs: ["mylib.cpp"],
7252 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007253 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007254 },
7255 apex_available: ["myapex"],
7256 }
7257
7258 cc_prebuilt_library_shared {
7259 name: "mylib",
7260 prefer: false,
7261 srcs: ["prebuilt.so"],
7262 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007263 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007264 },
7265 apex_available: ["myapex"],
7266 }
7267 `)
7268}
7269
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007270func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007271 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007272 apex {
7273 name: "myapex",
7274 key: "myapex.key",
7275 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007276 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007277 }
7278 apex_key {
7279 name: "myapex.key",
7280 public_key: "testkey.avbpubkey",
7281 private_key: "testkey.pem",
7282 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007283 `,
7284 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7285 variables.CompressedApex = proptools.BoolPtr(true)
7286 }),
7287 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007288
7289 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7290 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7291
7292 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7293 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7294
7295 // Make sure output of bundle is .capex
7296 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7297 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7298
7299 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007300 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007301 var builder strings.Builder
7302 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7303 androidMk := builder.String()
7304 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7305}
7306
Martin Stjernholm2856c662020-12-02 15:03:42 +00007307func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007308 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007309 apex {
7310 name: "myapex",
7311 key: "myapex.key",
7312 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007313 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007314 }
7315
7316 apex_key {
7317 name: "myapex.key",
7318 public_key: "testkey.avbpubkey",
7319 private_key: "testkey.pem",
7320 }
7321
7322 cc_library {
7323 name: "mylib",
7324 srcs: ["mylib.cpp"],
7325 apex_available: ["myapex"],
7326 shared_libs: ["otherlib"],
7327 system_shared_libs: [],
7328 }
7329
7330 cc_library {
7331 name: "otherlib",
7332 srcs: ["mylib.cpp"],
7333 stubs: {
7334 versions: ["current"],
7335 },
7336 }
7337
7338 cc_prebuilt_library_shared {
7339 name: "otherlib",
7340 prefer: true,
7341 srcs: ["prebuilt.so"],
7342 stubs: {
7343 versions: ["current"],
7344 },
7345 }
7346 `)
7347
7348 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007349 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007350 var builder strings.Builder
7351 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7352 androidMk := builder.String()
7353
7354 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7355 // a thing there.
7356 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7357}
7358
Jiyong Parke3867542020-12-03 17:28:25 +09007359func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007360 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007361 apex {
7362 name: "myapex",
7363 key: "myapex.key",
7364 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007365 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007366 }
7367
7368 apex_key {
7369 name: "myapex.key",
7370 public_key: "testkey.avbpubkey",
7371 private_key: "testkey.pem",
7372 }
7373
7374 cc_library {
7375 name: "mylib",
7376 srcs: ["mylib.cpp"],
7377 system_shared_libs: [],
7378 stl: "none",
7379 apex_available: ["myapex"],
7380 shared_libs: ["mylib2"],
7381 target: {
7382 apex: {
7383 exclude_shared_libs: ["mylib2"],
7384 },
7385 },
7386 }
7387
7388 cc_library {
7389 name: "mylib2",
7390 srcs: ["mylib.cpp"],
7391 system_shared_libs: [],
7392 stl: "none",
7393 }
7394 `)
7395
7396 // Check if mylib is linked to mylib2 for the non-apex target
7397 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7398 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7399
7400 // Make sure that the link doesn't occur for the apex target
7401 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7402 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7403
7404 // It shouldn't appear in the copy cmd as well.
7405 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7406 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7407}
7408
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007409func TestPrebuiltStubLibDep(t *testing.T) {
7410 bpBase := `
7411 apex {
7412 name: "myapex",
7413 key: "myapex.key",
7414 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007415 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007416 }
7417 apex_key {
7418 name: "myapex.key",
7419 public_key: "testkey.avbpubkey",
7420 private_key: "testkey.pem",
7421 }
7422 cc_library {
7423 name: "mylib",
7424 srcs: ["mylib.cpp"],
7425 apex_available: ["myapex"],
7426 shared_libs: ["stublib"],
7427 system_shared_libs: [],
7428 }
7429 apex {
7430 name: "otherapex",
7431 enabled: %s,
7432 key: "myapex.key",
7433 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007434 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007435 }
7436 `
7437
7438 stublibSourceBp := `
7439 cc_library {
7440 name: "stublib",
7441 srcs: ["mylib.cpp"],
7442 apex_available: ["otherapex"],
7443 system_shared_libs: [],
7444 stl: "none",
7445 stubs: {
7446 versions: ["1"],
7447 },
7448 }
7449 `
7450
7451 stublibPrebuiltBp := `
7452 cc_prebuilt_library_shared {
7453 name: "stublib",
7454 srcs: ["prebuilt.so"],
7455 apex_available: ["otherapex"],
7456 stubs: {
7457 versions: ["1"],
7458 },
7459 %s
7460 }
7461 `
7462
7463 tests := []struct {
7464 name string
7465 stublibBp string
7466 usePrebuilt bool
7467 modNames []string // Modules to collect AndroidMkEntries for
7468 otherApexEnabled []string
7469 }{
7470 {
7471 name: "only_source",
7472 stublibBp: stublibSourceBp,
7473 usePrebuilt: false,
7474 modNames: []string{"stublib"},
7475 otherApexEnabled: []string{"true", "false"},
7476 },
7477 {
7478 name: "source_preferred",
7479 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7480 usePrebuilt: false,
7481 modNames: []string{"stublib", "prebuilt_stublib"},
7482 otherApexEnabled: []string{"true", "false"},
7483 },
7484 {
7485 name: "prebuilt_preferred",
7486 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7487 usePrebuilt: true,
7488 modNames: []string{"stublib", "prebuilt_stublib"},
7489 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7490 },
7491 {
7492 name: "only_prebuilt",
7493 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7494 usePrebuilt: true,
7495 modNames: []string{"stublib"},
7496 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7497 },
7498 }
7499
7500 for _, test := range tests {
7501 t.Run(test.name, func(t *testing.T) {
7502 for _, otherApexEnabled := range test.otherApexEnabled {
7503 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007504 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007505
7506 type modAndMkEntries struct {
7507 mod *cc.Module
7508 mkEntries android.AndroidMkEntries
7509 }
7510 entries := []*modAndMkEntries{}
7511
7512 // Gather shared lib modules that are installable
7513 for _, modName := range test.modNames {
7514 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7515 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7516 continue
7517 }
7518 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007519 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007520 continue
7521 }
Colin Crossaa255532020-07-03 13:18:24 -07007522 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007523 if ent.Disabled {
7524 continue
7525 }
7526 entries = append(entries, &modAndMkEntries{
7527 mod: mod,
7528 mkEntries: ent,
7529 })
7530 }
7531 }
7532 }
7533
7534 var entry *modAndMkEntries = nil
7535 for _, ent := range entries {
7536 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7537 if entry != nil {
7538 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7539 } else {
7540 entry = ent
7541 }
7542 }
7543 }
7544
7545 if entry == nil {
7546 t.Errorf("AndroidMk entry for \"stublib\" missing")
7547 } else {
7548 isPrebuilt := entry.mod.Prebuilt() != nil
7549 if isPrebuilt != test.usePrebuilt {
7550 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7551 }
7552 if !entry.mod.IsStubs() {
7553 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7554 }
7555 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7556 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7557 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007558 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09007559 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09007560 if !android.InList(expected, cflags) {
7561 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7562 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007563 }
7564 })
7565 }
7566 })
7567 }
7568}
7569
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007570func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01007571 os.Exit(m.Run())
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007572}