blob: 87d551b74e87141de9734e5a9c178e4c0ec17cab [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +090018 "fmt"
Jiyong Park25fc6a92018-11-18 18:02:45 +090019 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Paul Duffin37856732021-02-26 14:24:15 +000021 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070022 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010023 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090024 "sort"
Jiyong Parkd4a3a132021-03-17 20:21:35 +090025 "strconv"
Jiyong Park25fc6a92018-11-18 18:02:45 +090026 "strings"
27 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090028
29 "github.com/google/blueprint/proptools"
30
31 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080032 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090033 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000034 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090036 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090037 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090039)
40
Jooyung Hand3639552019-08-09 12:57:43 +090041// names returns name list from white space separated string
42func names(s string) (ns []string) {
43 for _, n := range strings.Split(s, " ") {
44 if len(n) > 0 {
45 ns = append(ns, n)
46 }
47 }
48 return
49}
50
Paul Duffin40b62572021-03-20 11:39:01 +000051func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090052 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010053 android.GroupFixturePreparers(
54 prepareForApexTest,
55 android.GroupFixturePreparers(preparers...),
56 ).
Paul Duffine05480a2021-03-08 15:07:14 +000057 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000058 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090059}
60
Paul Duffin40b62572021-03-20 11:39:01 +000061func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090062 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010063
64 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000065 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010066 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000067 }
Paul Duffin284165a2021-03-29 01:50:31 +010068
69 result := android.GroupFixturePreparers(
70 prepareForApexTest,
71 android.GroupFixturePreparers(preparers...),
72 optionalBpPreparer,
73 ).RunTest(t)
74
Paul Duffine05480a2021-03-08 15:07:14 +000075 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090076}
77
Paul Duffin810f33d2021-03-09 14:12:32 +000078func withFiles(files android.MockFS) android.FixturePreparer {
79 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090080}
81
Paul Duffin810f33d2021-03-09 14:12:32 +000082func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
83 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090084 for k, v := range targets {
85 config.Targets[k] = v
86 }
Paul Duffin810f33d2021-03-09 14:12:32 +000087 })
Jooyung Han344d5432019-08-23 11:17:39 +090088}
89
Jooyung Han35155c42020-02-06 17:33:20 +090090// withNativeBridgeTargets sets configuration with targets including:
91// - X86_64 (primary)
92// - X86 (secondary)
93// - Arm64 on X86_64 (native bridge)
94// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000095var withNativeBridgeEnabled = android.FixtureModifyConfig(
96 func(config android.Config) {
97 config.Targets[android.Android] = []android.Target{
98 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
99 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
100 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
101 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
102 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
103 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
104 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
105 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
106 }
107 },
108)
109
110func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
111 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
112 variables.ManifestPackageNameOverrides = specs
113 })
Jooyung Han35155c42020-02-06 17:33:20 +0900114}
115
Paul Duffin810f33d2021-03-09 14:12:32 +0000116var withBinder32bit = android.FixtureModifyProductVariables(
117 func(variables android.FixtureProductVariables) {
118 variables.Binder32bit = proptools.BoolPtr(true)
119 },
120)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900121
Paul Duffin810f33d2021-03-09 14:12:32 +0000122var withUnbundledBuild = android.FixtureModifyProductVariables(
123 func(variables android.FixtureProductVariables) {
124 variables.Unbundled_build = proptools.BoolPtr(true)
125 },
126)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900127
Paul Duffin284165a2021-03-29 01:50:31 +0100128// Legacy preparer used for running tests within the apex package.
129//
130// This includes everything that was needed to run any test in the apex package prior to the
131// introduction of the test fixtures. Tests that are being converted to use fixtures directly
132// rather than through the testApex...() methods should avoid using this and instead use the
133// various preparers directly, using android.GroupFixturePreparers(...) to group them when
134// necessary.
135//
136// deprecated
137var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000138 // General preparers in alphabetical order as test infrastructure will enforce correct
139 // registration order.
140 android.PrepareForTestWithAndroidBuildComponents,
141 bpf.PrepareForTestWithBpf,
142 cc.PrepareForTestWithCcBuildComponents,
143 java.PrepareForTestWithJavaDefaultModules,
144 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
145 rust.PrepareForTestWithRustDefaultModules,
146 sh.PrepareForTestWithShBuildComponents,
147
148 PrepareForTestWithApexBuildComponents,
149
150 // Additional apex test specific preparers.
151 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
152 filegroup {
153 name: "myapex-file_contexts",
154 srcs: [
155 "apex/myapex-file_contexts",
156 ],
157 }
158 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000159 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000160 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000161 "a.java": nil,
162 "PrebuiltAppFoo.apk": nil,
163 "PrebuiltAppFooPriv.apk": nil,
164 "apex_manifest.json": nil,
165 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000166 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
167 "system/sepolicy/apex/myapex2-file_contexts": nil,
168 "system/sepolicy/apex/otherapex-file_contexts": nil,
169 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
170 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
171 "mylib.cpp": nil,
172 "mytest.cpp": nil,
173 "mytest1.cpp": nil,
174 "mytest2.cpp": nil,
175 "mytest3.cpp": nil,
176 "myprebuilt": nil,
177 "my_include": nil,
178 "foo/bar/MyClass.java": nil,
179 "prebuilt.jar": nil,
180 "prebuilt.so": nil,
181 "vendor/foo/devkeys/test.x509.pem": nil,
182 "vendor/foo/devkeys/test.pk8": nil,
183 "testkey.x509.pem": nil,
184 "testkey.pk8": nil,
185 "testkey.override.x509.pem": nil,
186 "testkey.override.pk8": nil,
187 "vendor/foo/devkeys/testkey.avbpubkey": nil,
188 "vendor/foo/devkeys/testkey.pem": nil,
189 "NOTICE": nil,
190 "custom_notice": nil,
191 "custom_notice_for_static_lib": nil,
192 "testkey2.avbpubkey": nil,
193 "testkey2.pem": nil,
194 "myapex-arm64.apex": nil,
195 "myapex-arm.apex": nil,
196 "myapex.apks": nil,
197 "frameworks/base/api/current.txt": nil,
198 "framework/aidl/a.aidl": nil,
199 "build/make/core/proguard.flags": nil,
200 "build/make/core/proguard_basic_keeps.flags": nil,
201 "dummy.txt": nil,
202 "baz": nil,
203 "bar/baz": nil,
204 "testdata/baz": nil,
205 "AppSet.apks": nil,
206 "foo.rs": nil,
207 "libfoo.jar": nil,
208 "libbar.jar": nil,
209 },
210 ),
211
212 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
213 variables.DeviceVndkVersion = proptools.StringPtr("current")
214 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
215 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
216 variables.Platform_sdk_codename = proptools.StringPtr("Q")
217 variables.Platform_sdk_final = proptools.BoolPtr(false)
218 variables.Platform_version_active_codenames = []string{"Q"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900219 variables.Platform_vndk_version = proptools.StringPtr("29")
Paul Duffin37aad602021-03-08 09:47:16 +0000220 }),
221)
222
Paul Duffin52bfaa42021-03-23 23:40:12 +0000223var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
224 "system/sepolicy/apex/myapex-file_contexts": nil,
225})
226
Jooyung Han643adc42020-02-27 13:50:06 +0900227// ensure that 'result' equals 'expected'
228func ensureEquals(t *testing.T, result string, expected string) {
229 t.Helper()
230 if result != expected {
231 t.Errorf("%q != %q", expected, result)
232 }
233}
234
Jiyong Park25fc6a92018-11-18 18:02:45 +0900235// ensure that 'result' contains 'expected'
236func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900237 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900238 if !strings.Contains(result, expected) {
239 t.Errorf("%q is not found in %q", expected, result)
240 }
241}
242
Liz Kammer5bd365f2020-05-27 15:15:11 -0700243// ensure that 'result' contains 'expected' exactly one time
244func ensureContainsOnce(t *testing.T, result string, expected string) {
245 t.Helper()
246 count := strings.Count(result, expected)
247 if count != 1 {
248 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
249 }
250}
251
Jiyong Park25fc6a92018-11-18 18:02:45 +0900252// ensures that 'result' does not contain 'notExpected'
253func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900254 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900255 if strings.Contains(result, notExpected) {
256 t.Errorf("%q is found in %q", notExpected, result)
257 }
258}
259
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700260func ensureMatches(t *testing.T, result string, expectedRex string) {
261 ok, err := regexp.MatchString(expectedRex, result)
262 if err != nil {
263 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
264 return
265 }
266 if !ok {
267 t.Errorf("%s does not match regular expession %s", result, expectedRex)
268 }
269}
270
Jiyong Park25fc6a92018-11-18 18:02:45 +0900271func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900272 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900273 if !android.InList(expected, result) {
274 t.Errorf("%q is not found in %v", expected, result)
275 }
276}
277
278func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900279 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 if android.InList(notExpected, result) {
281 t.Errorf("%q is found in %v", notExpected, result)
282 }
283}
284
Jooyung Hane1633032019-08-01 17:41:43 +0900285func ensureListEmpty(t *testing.T, result []string) {
286 t.Helper()
287 if len(result) > 0 {
288 t.Errorf("%q is expected to be empty", result)
289 }
290}
291
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000292func ensureListNotEmpty(t *testing.T, result []string) {
293 t.Helper()
294 if len(result) == 0 {
295 t.Errorf("%q is expected to be not empty", result)
296 }
297}
298
Jiyong Park25fc6a92018-11-18 18:02:45 +0900299// Minimal test
300func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800301 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900302 apex_defaults {
303 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900304 manifest: ":myapex.manifest",
305 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900306 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900307 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900308 native_shared_libs: [
309 "mylib",
310 "libfoo.ffi",
311 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900312 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800313 multilib: {
314 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900315 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800316 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900317 },
Jiyong Park77acec62020-06-01 21:39:15 +0900318 java_libs: [
319 "myjar",
320 "myjar_dex",
321 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000322 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323 }
324
Jiyong Park30ca9372019-02-07 16:27:23 +0900325 apex {
326 name: "myapex",
327 defaults: ["myapex-defaults"],
328 }
329
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 apex_key {
331 name: "myapex.key",
332 public_key: "testkey.avbpubkey",
333 private_key: "testkey.pem",
334 }
335
Jiyong Park809bb722019-02-13 21:33:49 +0900336 filegroup {
337 name: "myapex.manifest",
338 srcs: ["apex_manifest.json"],
339 }
340
341 filegroup {
342 name: "myapex.androidmanifest",
343 srcs: ["AndroidManifest.xml"],
344 }
345
Jiyong Park25fc6a92018-11-18 18:02:45 +0900346 cc_library {
347 name: "mylib",
348 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900349 shared_libs: [
350 "mylib2",
351 "libbar.ffi",
352 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353 system_shared_libs: [],
354 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000355 // TODO: remove //apex_available:platform
356 apex_available: [
357 "//apex_available:platform",
358 "myapex",
359 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900360 }
361
Alex Light3d673592019-01-18 14:37:31 -0800362 cc_binary {
363 name: "foo",
364 srcs: ["mylib.cpp"],
365 compile_multilib: "both",
366 multilib: {
367 lib32: {
368 suffix: "32",
369 },
370 lib64: {
371 suffix: "64",
372 },
373 },
374 symlinks: ["foo_link_"],
375 symlink_preferred_arch: true,
376 system_shared_libs: [],
377 static_executable: true,
378 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700379 apex_available: [ "myapex", "com.android.gki.*" ],
380 }
381
Jiyong Park99644e92020-11-17 22:21:02 +0900382 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000383 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900384 srcs: ["foo.rs"],
385 rlibs: ["libfoo.rlib.rust"],
386 dylibs: ["libfoo.dylib.rust"],
387 apex_available: ["myapex"],
388 }
389
390 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000391 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900392 srcs: ["foo.rs"],
393 crate_name: "foo",
394 apex_available: ["myapex"],
395 }
396
397 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000398 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900399 srcs: ["foo.rs"],
400 crate_name: "foo",
401 apex_available: ["myapex"],
402 }
403
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900404 rust_ffi_shared {
405 name: "libfoo.ffi",
406 srcs: ["foo.rs"],
407 crate_name: "foo",
408 apex_available: ["myapex"],
409 }
410
411 rust_ffi_shared {
412 name: "libbar.ffi",
413 srcs: ["foo.rs"],
414 crate_name: "bar",
415 apex_available: ["myapex"],
416 }
417
Yifan Hongd22a84a2020-07-28 17:37:46 -0700418 apex {
419 name: "com.android.gki.fake",
420 binaries: ["foo"],
421 key: "myapex.key",
422 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000423 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800424 }
425
Paul Duffindddd5462020-04-07 15:25:44 +0100426 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900427 name: "mylib2",
428 srcs: ["mylib.cpp"],
429 system_shared_libs: [],
430 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900431 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900432 static_libs: ["libstatic"],
433 // TODO: remove //apex_available:platform
434 apex_available: [
435 "//apex_available:platform",
436 "myapex",
437 ],
438 }
439
Paul Duffindddd5462020-04-07 15:25:44 +0100440 cc_prebuilt_library_shared {
441 name: "mylib2",
442 srcs: ["prebuilt.so"],
443 // TODO: remove //apex_available:platform
444 apex_available: [
445 "//apex_available:platform",
446 "myapex",
447 ],
448 }
449
Jiyong Park9918e1a2020-03-17 19:16:40 +0900450 cc_library_static {
451 name: "libstatic",
452 srcs: ["mylib.cpp"],
453 system_shared_libs: [],
454 stl: "none",
455 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000456 // TODO: remove //apex_available:platform
457 apex_available: [
458 "//apex_available:platform",
459 "myapex",
460 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900461 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900462
463 java_library {
464 name: "myjar",
465 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900466 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900467 sdk_version: "none",
468 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900469 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900470 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000471 // TODO: remove //apex_available:platform
472 apex_available: [
473 "//apex_available:platform",
474 "myapex",
475 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900476 }
477
Jiyong Park77acec62020-06-01 21:39:15 +0900478 dex_import {
479 name: "myjar_dex",
480 jars: ["prebuilt.jar"],
481 apex_available: [
482 "//apex_available:platform",
483 "myapex",
484 ],
485 }
486
Jiyong Park7f7766d2019-07-25 22:02:35 +0900487 java_library {
488 name: "myotherjar",
489 srcs: ["foo/bar/MyClass.java"],
490 sdk_version: "none",
491 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900492 // TODO: remove //apex_available:platform
493 apex_available: [
494 "//apex_available:platform",
495 "myapex",
496 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900497 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900498
499 java_library {
500 name: "mysharedjar",
501 srcs: ["foo/bar/MyClass.java"],
502 sdk_version: "none",
503 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900504 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900505 `)
506
Paul Duffina71a67a2021-03-29 00:42:57 +0100507 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900508
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900509 // Make sure that Android.mk is created
510 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700511 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900512 var builder strings.Builder
513 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
514
515 androidMk := builder.String()
516 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
517 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
518
Jiyong Park42cca6c2019-04-01 11:15:50 +0900519 optFlags := apexRule.Args["opt_flags"]
520 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700521 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100522 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900523
Jiyong Park25fc6a92018-11-18 18:02:45 +0900524 copyCmds := apexRule.Args["copy_commands"]
525
526 // Ensure that main rule creates an output
527 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
528
529 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700530 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
531 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
532 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900533 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900534 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900535
536 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700537 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
538 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900539 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
540 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900541 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900542
543 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800544 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
545 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900546 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900547 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900548 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900549 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
550 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900551 // .. but not for java libs
552 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900553 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800554
Colin Cross7113d202019-11-20 16:39:12 -0800555 // Ensure that the platform variant ends with _shared or _common
556 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
557 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900558 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
559 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900560 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
561
562 // Ensure that dynamic dependency to java libs are not included
563 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800564
565 // Ensure that all symlinks are present.
566 found_foo_link_64 := false
567 found_foo := false
568 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900569 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800570 if strings.HasSuffix(cmd, "bin/foo") {
571 found_foo = true
572 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
573 found_foo_link_64 = true
574 }
575 }
576 }
577 good := found_foo && found_foo_link_64
578 if !good {
579 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
580 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900581
Sundong Ahnabb64432019-10-22 13:58:29 +0900582 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700583 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900584 if len(noticeInputs) != 3 {
585 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900586 }
587 ensureListContains(t, noticeInputs, "NOTICE")
588 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900589 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900590
Artur Satayeva8bd1132020-04-27 18:07:06 +0100591 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100592 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100593 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
594 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
595 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100596
597 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100598 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100599 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
600 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
601 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800602}
603
Jooyung Hanf21c7972019-12-16 22:32:06 +0900604func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800605 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900606 apex_defaults {
607 name: "myapex-defaults",
608 key: "myapex.key",
609 prebuilts: ["myetc"],
610 native_shared_libs: ["mylib"],
611 java_libs: ["myjar"],
612 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900613 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800614 bpfs: ["bpf"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000615 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900616 }
617
618 prebuilt_etc {
619 name: "myetc",
620 src: "myprebuilt",
621 }
622
623 apex {
624 name: "myapex",
625 defaults: ["myapex-defaults"],
626 }
627
628 apex_key {
629 name: "myapex.key",
630 public_key: "testkey.avbpubkey",
631 private_key: "testkey.pem",
632 }
633
634 cc_library {
635 name: "mylib",
636 system_shared_libs: [],
637 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000638 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900639 }
640
641 java_library {
642 name: "myjar",
643 srcs: ["foo/bar/MyClass.java"],
644 sdk_version: "none",
645 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000646 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900647 }
648
649 android_app {
650 name: "AppFoo",
651 srcs: ["foo/bar/MyClass.java"],
652 sdk_version: "none",
653 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000654 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900655 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900656
657 runtime_resource_overlay {
658 name: "rro",
659 theme: "blue",
660 }
661
markchien2f59ec92020-09-02 16:23:38 +0800662 bpf {
663 name: "bpf",
664 srcs: ["bpf.c", "bpf2.c"],
665 }
666
Jooyung Hanf21c7972019-12-16 22:32:06 +0900667 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000668 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900669 "etc/myetc",
670 "javalib/myjar.jar",
671 "lib64/mylib.so",
672 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900673 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800674 "etc/bpf/bpf.o",
675 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900676 })
677}
678
Jooyung Han01a3ee22019-11-02 02:52:25 +0900679func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800680 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900681 apex {
682 name: "myapex",
683 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000684 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900685 }
686
687 apex_key {
688 name: "myapex.key",
689 public_key: "testkey.avbpubkey",
690 private_key: "testkey.pem",
691 }
692 `)
693
694 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900695 args := module.Rule("apexRule").Args
696 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
697 t.Error("manifest should be apex_manifest.pb, but " + manifest)
698 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900699}
700
Alex Light5098a612018-11-29 17:12:15 -0800701func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800702 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800703 apex {
704 name: "myapex",
705 key: "myapex.key",
706 payload_type: "zip",
707 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000708 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800709 }
710
711 apex_key {
712 name: "myapex.key",
713 public_key: "testkey.avbpubkey",
714 private_key: "testkey.pem",
715 }
716
717 cc_library {
718 name: "mylib",
719 srcs: ["mylib.cpp"],
720 shared_libs: ["mylib2"],
721 system_shared_libs: [],
722 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000723 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800724 }
725
726 cc_library {
727 name: "mylib2",
728 srcs: ["mylib.cpp"],
729 system_shared_libs: [],
730 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000731 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800732 }
733 `)
734
Sundong Ahnabb64432019-10-22 13:58:29 +0900735 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800736 copyCmds := zipApexRule.Args["copy_commands"]
737
738 // Ensure that main rule creates an output
739 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
740
741 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700742 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800743
744 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700745 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800746
747 // Ensure that both direct and indirect deps are copied into apex
748 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
749 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900750}
751
752func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800753 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900754 apex {
755 name: "myapex",
756 key: "myapex.key",
757 native_shared_libs: ["mylib", "mylib3"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000758 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900759 }
760
761 apex_key {
762 name: "myapex.key",
763 public_key: "testkey.avbpubkey",
764 private_key: "testkey.pem",
765 }
766
767 cc_library {
768 name: "mylib",
769 srcs: ["mylib.cpp"],
770 shared_libs: ["mylib2", "mylib3"],
771 system_shared_libs: [],
772 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000773 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900774 }
775
776 cc_library {
777 name: "mylib2",
778 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900779 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900780 system_shared_libs: [],
781 stl: "none",
782 stubs: {
783 versions: ["1", "2", "3"],
784 },
785 }
786
787 cc_library {
788 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900789 srcs: ["mylib.cpp"],
790 shared_libs: ["mylib4"],
791 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900792 stl: "none",
793 stubs: {
794 versions: ["10", "11", "12"],
795 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000796 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900797 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900798
799 cc_library {
800 name: "mylib4",
801 srcs: ["mylib.cpp"],
802 system_shared_libs: [],
803 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000804 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900805 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900806 `)
807
Sundong Ahnabb64432019-10-22 13:58:29 +0900808 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900809 copyCmds := apexRule.Args["copy_commands"]
810
811 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800812 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900813
814 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800815 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900816
817 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800818 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900819
Colin Crossaede88c2020-08-11 12:17:01 -0700820 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900821
822 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900823 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900824 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900825 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900826
827 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Crossaede88c2020-08-11 12:17:01 -0700828 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900829 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700830 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900831
832 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900833 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900834 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900835
836 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700837 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900838
Jooyung Hana57af4a2020-01-23 05:36:59 +0000839 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900840 "lib64/mylib.so",
841 "lib64/mylib3.so",
842 "lib64/mylib4.so",
843 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900844}
845
Colin Cross7812fd32020-09-25 12:35:10 -0700846func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
847 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -0800848 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -0700849 apex {
850 name: "myapex",
851 key: "myapex.key",
852 native_shared_libs: ["mylib", "mylib3"],
853 min_sdk_version: "29",
854 }
855
856 apex_key {
857 name: "myapex.key",
858 public_key: "testkey.avbpubkey",
859 private_key: "testkey.pem",
860 }
861
862 cc_library {
863 name: "mylib",
864 srcs: ["mylib.cpp"],
865 shared_libs: ["mylib2", "mylib3"],
866 system_shared_libs: [],
867 stl: "none",
868 apex_available: [ "myapex" ],
869 min_sdk_version: "28",
870 }
871
872 cc_library {
873 name: "mylib2",
874 srcs: ["mylib.cpp"],
875 cflags: ["-include mylib.h"],
876 system_shared_libs: [],
877 stl: "none",
878 stubs: {
879 versions: ["28", "29", "30", "current"],
880 },
881 min_sdk_version: "28",
882 }
883
884 cc_library {
885 name: "mylib3",
886 srcs: ["mylib.cpp"],
887 shared_libs: ["mylib4"],
888 system_shared_libs: [],
889 stl: "none",
890 stubs: {
891 versions: ["28", "29", "30", "current"],
892 },
893 apex_available: [ "myapex" ],
894 min_sdk_version: "28",
895 }
896
897 cc_library {
898 name: "mylib4",
899 srcs: ["mylib.cpp"],
900 system_shared_libs: [],
901 stl: "none",
902 apex_available: [ "myapex" ],
903 min_sdk_version: "28",
904 }
905 `)
906
907 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
908 copyCmds := apexRule.Args["copy_commands"]
909
910 // Ensure that direct non-stubs dep is always included
911 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
912
913 // Ensure that indirect stubs dep is not included
914 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
915
916 // Ensure that direct stubs dep is included
917 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
918
919 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
920
Jiyong Park55549df2021-02-26 23:57:23 +0900921 // Ensure that mylib is linking with the latest version of stub for mylib2
922 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -0700923 // ... and not linking to the non-stub (impl) variant of mylib2
924 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
925
926 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
927 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
928 // .. and not linking to the stubs variant of mylib3
929 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
930
931 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -0700932 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -0700933 ensureNotContains(t, mylib2Cflags, "-include ")
934
935 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700936 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -0700937
938 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
939 "lib64/mylib.so",
940 "lib64/mylib3.so",
941 "lib64/mylib4.so",
942 })
943}
944
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900945func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
946 t.Parallel()
947 // myapex (Z)
948 // mylib -----------------.
949 // |
950 // otherapex (29) |
951 // libstub's versions: 29 Z current
952 // |
953 // <platform> |
954 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -0800955 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900956 apex {
957 name: "myapex",
958 key: "myapex.key",
959 native_shared_libs: ["mylib"],
960 min_sdk_version: "Z", // non-final
961 }
962
963 cc_library {
964 name: "mylib",
965 srcs: ["mylib.cpp"],
966 shared_libs: ["libstub"],
967 apex_available: ["myapex"],
968 min_sdk_version: "Z",
969 }
970
971 apex_key {
972 name: "myapex.key",
973 public_key: "testkey.avbpubkey",
974 private_key: "testkey.pem",
975 }
976
977 apex {
978 name: "otherapex",
979 key: "myapex.key",
980 native_shared_libs: ["libstub"],
981 min_sdk_version: "29",
982 }
983
984 cc_library {
985 name: "libstub",
986 srcs: ["mylib.cpp"],
987 stubs: {
988 versions: ["29", "Z", "current"],
989 },
990 apex_available: ["otherapex"],
991 min_sdk_version: "29",
992 }
993
994 // platform module depending on libstub from otherapex should use the latest stub("current")
995 cc_library {
996 name: "libplatform",
997 srcs: ["mylib.cpp"],
998 shared_libs: ["libstub"],
999 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001000 `,
1001 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1002 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1003 variables.Platform_sdk_final = proptools.BoolPtr(false)
1004 variables.Platform_version_active_codenames = []string{"Z"}
1005 }),
1006 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001007
Jiyong Park55549df2021-02-26 23:57:23 +09001008 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001009 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001010 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001011 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001012 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001013
1014 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1015 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1016 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1017 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1018 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1019}
1020
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001021func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001022 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001023 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001024 name: "myapex2",
1025 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001026 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001027 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001028 }
1029
1030 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001031 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001032 public_key: "testkey.avbpubkey",
1033 private_key: "testkey.pem",
1034 }
1035
1036 cc_library {
1037 name: "mylib",
1038 srcs: ["mylib.cpp"],
1039 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001040 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001041 system_shared_libs: [],
1042 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001043 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001044 }
1045
1046 cc_library {
1047 name: "libfoo",
1048 srcs: ["mylib.cpp"],
1049 shared_libs: ["libbar"],
1050 system_shared_libs: [],
1051 stl: "none",
1052 stubs: {
1053 versions: ["10", "20", "30"],
1054 },
1055 }
1056
1057 cc_library {
1058 name: "libbar",
1059 srcs: ["mylib.cpp"],
1060 system_shared_libs: [],
1061 stl: "none",
1062 }
1063
Jiyong Park678c8812020-02-07 17:25:49 +09001064 cc_library_static {
1065 name: "libbaz",
1066 srcs: ["mylib.cpp"],
1067 system_shared_libs: [],
1068 stl: "none",
1069 apex_available: [ "myapex2" ],
1070 }
1071
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001072 `)
1073
Jiyong Park83dc74b2020-01-14 18:38:44 +09001074 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001075 copyCmds := apexRule.Args["copy_commands"]
1076
1077 // Ensure that direct non-stubs dep is always included
1078 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1079
1080 // Ensure that indirect stubs dep is not included
1081 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1082
1083 // Ensure that dependency of stubs is not included
1084 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1085
Colin Crossaede88c2020-08-11 12:17:01 -07001086 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001087
1088 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001089 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001090 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001091 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001092
Jiyong Park3ff16992019-12-27 14:11:47 +09001093 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001094
1095 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1096 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001097
Artur Satayeva8bd1132020-04-27 18:07:06 +01001098 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001099 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001100
Artur Satayeva8bd1132020-04-27 18:07:06 +01001101 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001102 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001103}
1104
Jooyung Hand3639552019-08-09 12:57:43 +09001105func TestApexWithRuntimeLibsDependency(t *testing.T) {
1106 /*
1107 myapex
1108 |
1109 v (runtime_libs)
1110 mylib ------+------> libfoo [provides stub]
1111 |
1112 `------> libbar
1113 */
Colin Cross1c460562021-02-16 17:55:47 -08001114 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001115 apex {
1116 name: "myapex",
1117 key: "myapex.key",
1118 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001119 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001120 }
1121
1122 apex_key {
1123 name: "myapex.key",
1124 public_key: "testkey.avbpubkey",
1125 private_key: "testkey.pem",
1126 }
1127
1128 cc_library {
1129 name: "mylib",
1130 srcs: ["mylib.cpp"],
1131 runtime_libs: ["libfoo", "libbar"],
1132 system_shared_libs: [],
1133 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001134 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001135 }
1136
1137 cc_library {
1138 name: "libfoo",
1139 srcs: ["mylib.cpp"],
1140 system_shared_libs: [],
1141 stl: "none",
1142 stubs: {
1143 versions: ["10", "20", "30"],
1144 },
1145 }
1146
1147 cc_library {
1148 name: "libbar",
1149 srcs: ["mylib.cpp"],
1150 system_shared_libs: [],
1151 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001152 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001153 }
1154
1155 `)
1156
Sundong Ahnabb64432019-10-22 13:58:29 +09001157 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001158 copyCmds := apexRule.Args["copy_commands"]
1159
1160 // Ensure that direct non-stubs dep is always included
1161 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1162
1163 // Ensure that indirect stubs dep is not included
1164 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1165
1166 // Ensure that runtime_libs dep in included
1167 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1168
Sundong Ahnabb64432019-10-22 13:58:29 +09001169 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001170 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1171 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001172
1173}
1174
Paul Duffina02cae32021-03-09 01:44:06 +00001175var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1176 cc.PrepareForTestWithCcBuildComponents,
1177 PrepareForTestWithApexBuildComponents,
1178 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001179 apex {
1180 name: "com.android.runtime",
1181 key: "com.android.runtime.key",
1182 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001183 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001184 }
1185
1186 apex_key {
1187 name: "com.android.runtime.key",
1188 public_key: "testkey.avbpubkey",
1189 private_key: "testkey.pem",
1190 }
Paul Duffina02cae32021-03-09 01:44:06 +00001191 `),
1192 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1193)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001194
Paul Duffina02cae32021-03-09 01:44:06 +00001195func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001196 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001197 cc_library {
1198 name: "libc",
1199 no_libcrt: true,
1200 nocrt: true,
1201 stl: "none",
1202 system_shared_libs: [],
1203 stubs: { versions: ["1"] },
1204 apex_available: ["com.android.runtime"],
1205
1206 sanitize: {
1207 hwaddress: true,
1208 }
1209 }
1210
1211 cc_prebuilt_library_shared {
1212 name: "libclang_rt.hwasan-aarch64-android",
1213 no_libcrt: true,
1214 nocrt: true,
1215 stl: "none",
1216 system_shared_libs: [],
1217 srcs: [""],
1218 stubs: { versions: ["1"] },
1219
1220 sanitize: {
1221 never: true,
1222 },
Paul Duffina02cae32021-03-09 01:44:06 +00001223 } `)
1224 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001225
1226 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1227 "lib64/bionic/libc.so",
1228 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1229 })
1230
1231 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1232
1233 installed := hwasan.Description("install libclang_rt.hwasan")
1234 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1235
1236 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1237 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1238 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1239}
1240
1241func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001242 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001243 prepareForTestOfRuntimeApexWithHwasan,
1244 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1245 variables.SanitizeDevice = []string{"hwaddress"}
1246 }),
1247 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001248 cc_library {
1249 name: "libc",
1250 no_libcrt: true,
1251 nocrt: true,
1252 stl: "none",
1253 system_shared_libs: [],
1254 stubs: { versions: ["1"] },
1255 apex_available: ["com.android.runtime"],
1256 }
1257
1258 cc_prebuilt_library_shared {
1259 name: "libclang_rt.hwasan-aarch64-android",
1260 no_libcrt: true,
1261 nocrt: true,
1262 stl: "none",
1263 system_shared_libs: [],
1264 srcs: [""],
1265 stubs: { versions: ["1"] },
1266
1267 sanitize: {
1268 never: true,
1269 },
1270 }
Paul Duffina02cae32021-03-09 01:44:06 +00001271 `)
1272 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001273
1274 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1275 "lib64/bionic/libc.so",
1276 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1277 })
1278
1279 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1280
1281 installed := hwasan.Description("install libclang_rt.hwasan")
1282 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1283
1284 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1285 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1286 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1287}
1288
Jooyung Han61b66e92020-03-21 14:21:46 +00001289func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1290 testcases := []struct {
1291 name string
1292 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001293 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001294 shouldLink string
1295 shouldNotLink []string
1296 }{
1297 {
Jiyong Park55549df2021-02-26 23:57:23 +09001298 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001299 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001300 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001301 shouldLink: "current",
1302 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001303 },
1304 {
Jiyong Park55549df2021-02-26 23:57:23 +09001305 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001306 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001307 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001308 shouldLink: "current",
1309 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001310 },
1311 }
1312 for _, tc := range testcases {
1313 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001314 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001315 apex {
1316 name: "myapex",
1317 key: "myapex.key",
1318 use_vendor: true,
1319 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001320 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001321 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001322 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001323
Jooyung Han61b66e92020-03-21 14:21:46 +00001324 apex_key {
1325 name: "myapex.key",
1326 public_key: "testkey.avbpubkey",
1327 private_key: "testkey.pem",
1328 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001329
Jooyung Han61b66e92020-03-21 14:21:46 +00001330 cc_library {
1331 name: "mylib",
1332 srcs: ["mylib.cpp"],
1333 vendor_available: true,
1334 shared_libs: ["libbar"],
1335 system_shared_libs: [],
1336 stl: "none",
1337 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001338 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001339 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001340
Jooyung Han61b66e92020-03-21 14:21:46 +00001341 cc_library {
1342 name: "libbar",
1343 srcs: ["mylib.cpp"],
1344 system_shared_libs: [],
1345 stl: "none",
1346 stubs: { versions: ["29","30"] },
Colin Cross0477b422020-10-13 18:43:54 -07001347 llndk_stubs: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001348 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001349
Jooyung Han61b66e92020-03-21 14:21:46 +00001350 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001351 name: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001352 symbol_file: "",
1353 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001354 `,
1355 setUseVendorAllowListForTest([]string{"myapex"}),
1356 withUnbundledBuild,
1357 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001358
Jooyung Han61b66e92020-03-21 14:21:46 +00001359 // Ensure that LLNDK dep is not included
1360 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1361 "lib64/mylib.so",
1362 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001363
Jooyung Han61b66e92020-03-21 14:21:46 +00001364 // Ensure that LLNDK dep is required
1365 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1366 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1367 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001368
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001369 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.29_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1370 ensureContains(t, mylibLdFlags, "libbar/android_vendor.29_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001371 for _, ver := range tc.shouldNotLink {
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001372 ensureNotContains(t, mylibLdFlags, "libbar/android_vendor.29_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001373 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001374
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001375 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.29_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001376 ver := tc.shouldLink
1377 if tc.shouldLink == "current" {
1378 ver = strconv.Itoa(android.FutureApiLevelInt)
1379 }
1380 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001381 })
1382 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001383}
1384
Jiyong Park25fc6a92018-11-18 18:02:45 +09001385func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001386 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001387 apex {
1388 name: "myapex",
1389 key: "myapex.key",
1390 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001391 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001392 }
1393
1394 apex_key {
1395 name: "myapex.key",
1396 public_key: "testkey.avbpubkey",
1397 private_key: "testkey.pem",
1398 }
1399
1400 cc_library {
1401 name: "mylib",
1402 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001403 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001404 shared_libs: ["libdl#27"],
1405 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001406 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001407 }
1408
1409 cc_library_shared {
1410 name: "mylib_shared",
1411 srcs: ["mylib.cpp"],
1412 shared_libs: ["libdl#27"],
1413 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001414 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001415 }
1416
1417 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001418 name: "libBootstrap",
1419 srcs: ["mylib.cpp"],
1420 stl: "none",
1421 bootstrap: true,
1422 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001423 `)
1424
Sundong Ahnabb64432019-10-22 13:58:29 +09001425 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001426 copyCmds := apexRule.Args["copy_commands"]
1427
1428 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001429 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001430 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1431 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001432
1433 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001434 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001435
Colin Crossaede88c2020-08-11 12:17:01 -07001436 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1437 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1438 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001439
1440 // For dependency to libc
1441 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001442 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001443 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001444 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001445 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001446 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1447 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001448
1449 // For dependency to libm
1450 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001451 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001452 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001453 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001454 // ... and is not compiling with the stub
1455 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1456 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1457
1458 // For dependency to libdl
1459 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001460 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001461 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001462 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1463 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001464 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001465 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001466 // ... Cflags from stub is correctly exported to mylib
1467 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1468 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001469
1470 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001471 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1472 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1473 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1474 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001475}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001476
Jooyung Han749dc692020-04-15 11:03:39 +09001477func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001478 // there are three links between liba --> libz.
1479 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001480 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001481 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001482 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001483 apex {
1484 name: "myapex",
1485 key: "myapex.key",
1486 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001487 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001488 }
1489
1490 apex {
1491 name: "otherapex",
1492 key: "myapex.key",
1493 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001494 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001495 }
1496
1497 apex_key {
1498 name: "myapex.key",
1499 public_key: "testkey.avbpubkey",
1500 private_key: "testkey.pem",
1501 }
1502
1503 cc_library {
1504 name: "libx",
1505 shared_libs: ["liba"],
1506 system_shared_libs: [],
1507 stl: "none",
1508 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001509 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001510 }
1511
1512 cc_library {
1513 name: "liby",
1514 shared_libs: ["liba"],
1515 system_shared_libs: [],
1516 stl: "none",
1517 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001518 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001519 }
1520
1521 cc_library {
1522 name: "liba",
1523 shared_libs: ["libz"],
1524 system_shared_libs: [],
1525 stl: "none",
1526 apex_available: [
1527 "//apex_available:anyapex",
1528 "//apex_available:platform",
1529 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001530 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001531 }
1532
1533 cc_library {
1534 name: "libz",
1535 system_shared_libs: [],
1536 stl: "none",
1537 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001538 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001539 },
1540 }
Jooyung Han749dc692020-04-15 11:03:39 +09001541 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001542
1543 expectLink := func(from, from_variant, to, to_variant string) {
1544 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1545 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1546 }
1547 expectNoLink := func(from, from_variant, to, to_variant string) {
1548 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1549 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1550 }
1551 // platform liba is linked to non-stub version
1552 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001553 // liba in myapex is linked to current
1554 expectLink("liba", "shared_apex29", "libz", "shared_current")
1555 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001556 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001557 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001558 // liba in otherapex is linked to current
1559 expectLink("liba", "shared_apex30", "libz", "shared_current")
1560 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001561 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1562 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001563}
1564
Jooyung Hanaed150d2020-04-02 01:41:41 +09001565func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001566 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001567 apex {
1568 name: "myapex",
1569 key: "myapex.key",
1570 native_shared_libs: ["libx"],
1571 min_sdk_version: "R",
1572 }
1573
1574 apex_key {
1575 name: "myapex.key",
1576 public_key: "testkey.avbpubkey",
1577 private_key: "testkey.pem",
1578 }
1579
1580 cc_library {
1581 name: "libx",
1582 shared_libs: ["libz"],
1583 system_shared_libs: [],
1584 stl: "none",
1585 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001586 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001587 }
1588
1589 cc_library {
1590 name: "libz",
1591 system_shared_libs: [],
1592 stl: "none",
1593 stubs: {
1594 versions: ["29", "R"],
1595 },
1596 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001597 `,
1598 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1599 variables.Platform_version_active_codenames = []string{"R"}
1600 }),
1601 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001602
1603 expectLink := func(from, from_variant, to, to_variant string) {
1604 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1605 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1606 }
1607 expectNoLink := func(from, from_variant, to, to_variant string) {
1608 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1609 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1610 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001611 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1612 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001613 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1614 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001615}
1616
Jooyung Han749dc692020-04-15 11:03:39 +09001617func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001618 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001619 apex {
1620 name: "myapex",
1621 key: "myapex.key",
1622 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001623 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001624 }
1625
1626 apex_key {
1627 name: "myapex.key",
1628 public_key: "testkey.avbpubkey",
1629 private_key: "testkey.pem",
1630 }
1631
1632 cc_library {
1633 name: "libx",
1634 shared_libs: ["libz"],
1635 system_shared_libs: [],
1636 stl: "none",
1637 apex_available: [ "myapex" ],
1638 }
1639
1640 cc_library {
1641 name: "libz",
1642 system_shared_libs: [],
1643 stl: "none",
1644 stubs: {
1645 versions: ["1", "2"],
1646 },
1647 }
1648 `)
1649
1650 expectLink := func(from, from_variant, to, to_variant string) {
1651 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1652 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1653 }
1654 expectNoLink := func(from, from_variant, to, to_variant string) {
1655 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1656 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1657 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001658 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001659 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001660 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001661 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001662}
1663
1664func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001665 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001666 apex {
1667 name: "myapex",
1668 key: "myapex.key",
1669 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001670 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001671 }
1672
1673 apex_key {
1674 name: "myapex.key",
1675 public_key: "testkey.avbpubkey",
1676 private_key: "testkey.pem",
1677 }
1678
1679 cc_library {
1680 name: "libx",
1681 system_shared_libs: [],
1682 stl: "none",
1683 apex_available: [ "myapex" ],
1684 stubs: {
1685 versions: ["1", "2"],
1686 },
1687 }
1688
1689 cc_library {
1690 name: "libz",
1691 shared_libs: ["libx"],
1692 system_shared_libs: [],
1693 stl: "none",
1694 }
1695 `)
1696
1697 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001698 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001699 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1700 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1701 }
1702 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001703 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001704 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1705 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1706 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001707 expectLink("libz", "shared", "libx", "shared_current")
1708 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09001709 expectNoLink("libz", "shared", "libz", "shared_1")
1710 expectNoLink("libz", "shared", "libz", "shared")
1711}
1712
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001713var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1714 func(variables android.FixtureProductVariables) {
1715 variables.SanitizeDevice = []string{"hwaddress"}
1716 },
1717)
1718
Jooyung Han75568392020-03-20 04:29:24 +09001719func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001720 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001721 apex {
1722 name: "myapex",
1723 key: "myapex.key",
1724 native_shared_libs: ["libx"],
1725 min_sdk_version: "29",
1726 }
1727
1728 apex_key {
1729 name: "myapex.key",
1730 public_key: "testkey.avbpubkey",
1731 private_key: "testkey.pem",
1732 }
1733
1734 cc_library {
1735 name: "libx",
1736 shared_libs: ["libbar"],
1737 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001738 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001739 }
1740
1741 cc_library {
1742 name: "libbar",
1743 stubs: {
1744 versions: ["29", "30"],
1745 },
1746 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001747 `,
1748 prepareForTestWithSantitizeHwaddress,
1749 )
Jooyung Han03b51852020-02-26 22:45:42 +09001750 expectLink := func(from, from_variant, to, to_variant string) {
1751 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1752 libFlags := ld.Args["libFlags"]
1753 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1754 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001755 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09001756}
1757
Jooyung Han75568392020-03-20 04:29:24 +09001758func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001759 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001760 apex {
1761 name: "myapex",
1762 key: "myapex.key",
1763 native_shared_libs: ["libx"],
1764 min_sdk_version: "29",
1765 }
1766
1767 apex_key {
1768 name: "myapex.key",
1769 public_key: "testkey.avbpubkey",
1770 private_key: "testkey.pem",
1771 }
1772
1773 cc_library {
1774 name: "libx",
1775 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001776 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001777 }
Jooyung Han75568392020-03-20 04:29:24 +09001778 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001779
1780 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001781 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001782 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001783 // note that platform variant is not.
1784 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001785 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001786}
1787
Jooyung Han749dc692020-04-15 11:03:39 +09001788func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1789 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001790 apex {
1791 name: "myapex",
1792 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001793 native_shared_libs: ["mylib"],
1794 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001795 }
1796
1797 apex_key {
1798 name: "myapex.key",
1799 public_key: "testkey.avbpubkey",
1800 private_key: "testkey.pem",
1801 }
Jooyung Han749dc692020-04-15 11:03:39 +09001802
1803 cc_library {
1804 name: "mylib",
1805 srcs: ["mylib.cpp"],
1806 system_shared_libs: [],
1807 stl: "none",
1808 apex_available: [
1809 "myapex",
1810 ],
1811 min_sdk_version: "30",
1812 }
1813 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001814
1815 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
1816 apex {
1817 name: "myapex",
1818 key: "myapex.key",
1819 native_shared_libs: ["libfoo.ffi"],
1820 min_sdk_version: "29",
1821 }
1822
1823 apex_key {
1824 name: "myapex.key",
1825 public_key: "testkey.avbpubkey",
1826 private_key: "testkey.pem",
1827 }
1828
1829 rust_ffi_shared {
1830 name: "libfoo.ffi",
1831 srcs: ["foo.rs"],
1832 crate_name: "foo",
1833 apex_available: [
1834 "myapex",
1835 ],
1836 min_sdk_version: "30",
1837 }
1838 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001839
1840 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
1841 apex {
1842 name: "myapex",
1843 key: "myapex.key",
1844 java_libs: ["libfoo"],
1845 min_sdk_version: "29",
1846 }
1847
1848 apex_key {
1849 name: "myapex.key",
1850 public_key: "testkey.avbpubkey",
1851 private_key: "testkey.pem",
1852 }
1853
1854 java_import {
1855 name: "libfoo",
1856 jars: ["libfoo.jar"],
1857 apex_available: [
1858 "myapex",
1859 ],
1860 min_sdk_version: "30",
1861 }
1862 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001863}
1864
1865func TestApexMinSdkVersion_Okay(t *testing.T) {
1866 testApex(t, `
1867 apex {
1868 name: "myapex",
1869 key: "myapex.key",
1870 native_shared_libs: ["libfoo"],
1871 java_libs: ["libbar"],
1872 min_sdk_version: "29",
1873 }
1874
1875 apex_key {
1876 name: "myapex.key",
1877 public_key: "testkey.avbpubkey",
1878 private_key: "testkey.pem",
1879 }
1880
1881 cc_library {
1882 name: "libfoo",
1883 srcs: ["mylib.cpp"],
1884 shared_libs: ["libfoo_dep"],
1885 apex_available: ["myapex"],
1886 min_sdk_version: "29",
1887 }
1888
1889 cc_library {
1890 name: "libfoo_dep",
1891 srcs: ["mylib.cpp"],
1892 apex_available: ["myapex"],
1893 min_sdk_version: "29",
1894 }
1895
1896 java_library {
1897 name: "libbar",
1898 sdk_version: "current",
1899 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001900 static_libs: [
1901 "libbar_dep",
1902 "libbar_import_dep",
1903 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001904 apex_available: ["myapex"],
1905 min_sdk_version: "29",
1906 }
1907
1908 java_library {
1909 name: "libbar_dep",
1910 sdk_version: "current",
1911 srcs: ["a.java"],
1912 apex_available: ["myapex"],
1913 min_sdk_version: "29",
1914 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001915
1916 java_import {
1917 name: "libbar_import_dep",
1918 jars: ["libbar.jar"],
1919 apex_available: ["myapex"],
1920 min_sdk_version: "29",
1921 }
Jooyung Han03b51852020-02-26 22:45:42 +09001922 `)
1923}
1924
Artur Satayev8cf899a2020-04-15 17:29:42 +01001925func TestJavaStableSdkVersion(t *testing.T) {
1926 testCases := []struct {
1927 name string
1928 expectedError string
1929 bp string
1930 }{
1931 {
1932 name: "Non-updatable apex with non-stable dep",
1933 bp: `
1934 apex {
1935 name: "myapex",
1936 java_libs: ["myjar"],
1937 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001938 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01001939 }
1940 apex_key {
1941 name: "myapex.key",
1942 public_key: "testkey.avbpubkey",
1943 private_key: "testkey.pem",
1944 }
1945 java_library {
1946 name: "myjar",
1947 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001948 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001949 apex_available: ["myapex"],
1950 }
1951 `,
1952 },
1953 {
1954 name: "Updatable apex with stable dep",
1955 bp: `
1956 apex {
1957 name: "myapex",
1958 java_libs: ["myjar"],
1959 key: "myapex.key",
1960 updatable: true,
1961 min_sdk_version: "29",
1962 }
1963 apex_key {
1964 name: "myapex.key",
1965 public_key: "testkey.avbpubkey",
1966 private_key: "testkey.pem",
1967 }
1968 java_library {
1969 name: "myjar",
1970 srcs: ["foo/bar/MyClass.java"],
1971 sdk_version: "current",
1972 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001973 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001974 }
1975 `,
1976 },
1977 {
1978 name: "Updatable apex with non-stable dep",
1979 expectedError: "cannot depend on \"myjar\"",
1980 bp: `
1981 apex {
1982 name: "myapex",
1983 java_libs: ["myjar"],
1984 key: "myapex.key",
1985 updatable: true,
1986 }
1987 apex_key {
1988 name: "myapex.key",
1989 public_key: "testkey.avbpubkey",
1990 private_key: "testkey.pem",
1991 }
1992 java_library {
1993 name: "myjar",
1994 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001995 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001996 apex_available: ["myapex"],
1997 }
1998 `,
1999 },
2000 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002001 name: "Updatable apex with non-stable transitive dep",
2002 // This is not actually detecting that the transitive dependency is unstable, rather it is
2003 // detecting that the transitive dependency is building against a wider API surface than the
2004 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002005 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002006 bp: `
2007 apex {
2008 name: "myapex",
2009 java_libs: ["myjar"],
2010 key: "myapex.key",
2011 updatable: true,
2012 }
2013 apex_key {
2014 name: "myapex.key",
2015 public_key: "testkey.avbpubkey",
2016 private_key: "testkey.pem",
2017 }
2018 java_library {
2019 name: "myjar",
2020 srcs: ["foo/bar/MyClass.java"],
2021 sdk_version: "current",
2022 apex_available: ["myapex"],
2023 static_libs: ["transitive-jar"],
2024 }
2025 java_library {
2026 name: "transitive-jar",
2027 srcs: ["foo/bar/MyClass.java"],
2028 sdk_version: "core_platform",
2029 apex_available: ["myapex"],
2030 }
2031 `,
2032 },
2033 }
2034
2035 for _, test := range testCases {
2036 t.Run(test.name, func(t *testing.T) {
2037 if test.expectedError == "" {
2038 testApex(t, test.bp)
2039 } else {
2040 testApexError(t, test.expectedError, test.bp)
2041 }
2042 })
2043 }
2044}
2045
Jooyung Han749dc692020-04-15 11:03:39 +09002046func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2047 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2048 apex {
2049 name: "myapex",
2050 key: "myapex.key",
2051 native_shared_libs: ["mylib"],
2052 min_sdk_version: "29",
2053 }
2054
2055 apex_key {
2056 name: "myapex.key",
2057 public_key: "testkey.avbpubkey",
2058 private_key: "testkey.pem",
2059 }
2060
2061 cc_library {
2062 name: "mylib",
2063 srcs: ["mylib.cpp"],
2064 shared_libs: ["mylib2"],
2065 system_shared_libs: [],
2066 stl: "none",
2067 apex_available: [
2068 "myapex",
2069 ],
2070 min_sdk_version: "29",
2071 }
2072
2073 // indirect part of the apex
2074 cc_library {
2075 name: "mylib2",
2076 srcs: ["mylib.cpp"],
2077 system_shared_libs: [],
2078 stl: "none",
2079 apex_available: [
2080 "myapex",
2081 ],
2082 min_sdk_version: "30",
2083 }
2084 `)
2085}
2086
2087func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2088 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2089 apex {
2090 name: "myapex",
2091 key: "myapex.key",
2092 apps: ["AppFoo"],
2093 min_sdk_version: "29",
2094 }
2095
2096 apex_key {
2097 name: "myapex.key",
2098 public_key: "testkey.avbpubkey",
2099 private_key: "testkey.pem",
2100 }
2101
2102 android_app {
2103 name: "AppFoo",
2104 srcs: ["foo/bar/MyClass.java"],
2105 sdk_version: "current",
2106 min_sdk_version: "29",
2107 system_modules: "none",
2108 stl: "none",
2109 static_libs: ["bar"],
2110 apex_available: [ "myapex" ],
2111 }
2112
2113 java_library {
2114 name: "bar",
2115 sdk_version: "current",
2116 srcs: ["a.java"],
2117 apex_available: [ "myapex" ],
2118 }
2119 `)
2120}
2121
2122func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002123 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002124 apex {
2125 name: "myapex",
2126 key: "myapex.key",
2127 native_shared_libs: ["mylib"],
2128 min_sdk_version: "29",
2129 }
2130
2131 apex_key {
2132 name: "myapex.key",
2133 public_key: "testkey.avbpubkey",
2134 private_key: "testkey.pem",
2135 }
2136
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002137 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002138 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2139 cc_library {
2140 name: "mylib",
2141 srcs: ["mylib.cpp"],
2142 shared_libs: ["mylib2"],
2143 system_shared_libs: [],
2144 stl: "none",
2145 apex_available: ["myapex", "otherapex"],
2146 min_sdk_version: "29",
2147 }
2148
2149 cc_library {
2150 name: "mylib2",
2151 srcs: ["mylib.cpp"],
2152 system_shared_libs: [],
2153 stl: "none",
2154 apex_available: ["otherapex"],
2155 stubs: { versions: ["29", "30"] },
2156 min_sdk_version: "30",
2157 }
2158
2159 apex {
2160 name: "otherapex",
2161 key: "myapex.key",
2162 native_shared_libs: ["mylib", "mylib2"],
2163 min_sdk_version: "30",
2164 }
2165 `)
2166 expectLink := func(from, from_variant, to, to_variant string) {
2167 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2168 libFlags := ld.Args["libFlags"]
2169 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2170 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002171 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002172 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002173}
2174
Jooyung Haned124c32021-01-26 11:43:46 +09002175func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002176 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2177 func(variables android.FixtureProductVariables) {
2178 variables.Platform_sdk_codename = proptools.StringPtr("S")
2179 variables.Platform_version_active_codenames = []string{"S"}
2180 },
2181 )
Jooyung Haned124c32021-01-26 11:43:46 +09002182 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2183 apex {
2184 name: "myapex",
2185 key: "myapex.key",
2186 native_shared_libs: ["libfoo"],
2187 min_sdk_version: "S",
2188 }
2189 apex_key {
2190 name: "myapex.key",
2191 public_key: "testkey.avbpubkey",
2192 private_key: "testkey.pem",
2193 }
2194 cc_library {
2195 name: "libfoo",
2196 shared_libs: ["libbar"],
2197 apex_available: ["myapex"],
2198 min_sdk_version: "29",
2199 }
2200 cc_library {
2201 name: "libbar",
2202 apex_available: ["myapex"],
2203 }
2204 `, withSAsActiveCodeNames)
2205}
2206
2207func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002208 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2209 variables.Platform_sdk_codename = proptools.StringPtr("S")
2210 variables.Platform_version_active_codenames = []string{"S", "T"}
2211 })
Colin Cross1c460562021-02-16 17:55:47 -08002212 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002213 apex {
2214 name: "myapex",
2215 key: "myapex.key",
2216 native_shared_libs: ["libfoo"],
2217 min_sdk_version: "S",
2218 }
2219 apex_key {
2220 name: "myapex.key",
2221 public_key: "testkey.avbpubkey",
2222 private_key: "testkey.pem",
2223 }
2224 cc_library {
2225 name: "libfoo",
2226 shared_libs: ["libbar"],
2227 apex_available: ["myapex"],
2228 min_sdk_version: "S",
2229 }
2230 cc_library {
2231 name: "libbar",
2232 stubs: {
2233 symbol_file: "libbar.map.txt",
2234 versions: ["30", "S", "T"],
2235 },
2236 }
2237 `, withSAsActiveCodeNames)
2238
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002239 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002240 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2241 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002242 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002243}
2244
Jiyong Park7c2ee712018-12-07 00:42:25 +09002245func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002246 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002247 apex {
2248 name: "myapex",
2249 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002250 native_shared_libs: ["mylib"],
2251 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002252 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002253 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002254 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002255 }
2256
2257 apex_key {
2258 name: "myapex.key",
2259 public_key: "testkey.avbpubkey",
2260 private_key: "testkey.pem",
2261 }
2262
2263 prebuilt_etc {
2264 name: "myetc",
2265 src: "myprebuilt",
2266 sub_dir: "foo/bar",
2267 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002268
2269 cc_library {
2270 name: "mylib",
2271 srcs: ["mylib.cpp"],
2272 relative_install_path: "foo/bar",
2273 system_shared_libs: [],
2274 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002275 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002276 }
2277
2278 cc_binary {
2279 name: "mybin",
2280 srcs: ["mylib.cpp"],
2281 relative_install_path: "foo/bar",
2282 system_shared_libs: [],
2283 static_executable: true,
2284 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002285 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002286 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002287 `)
2288
Sundong Ahnabb64432019-10-22 13:58:29 +09002289 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002290 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2291
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002292 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002293 ensureListContains(t, dirs, "etc")
2294 ensureListContains(t, dirs, "etc/foo")
2295 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002296 ensureListContains(t, dirs, "lib64")
2297 ensureListContains(t, dirs, "lib64/foo")
2298 ensureListContains(t, dirs, "lib64/foo/bar")
2299 ensureListContains(t, dirs, "lib")
2300 ensureListContains(t, dirs, "lib/foo")
2301 ensureListContains(t, dirs, "lib/foo/bar")
2302
Jiyong Parkbd13e442019-03-15 18:10:35 +09002303 ensureListContains(t, dirs, "bin")
2304 ensureListContains(t, dirs, "bin/foo")
2305 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002306}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002307
Jooyung Han35155c42020-02-06 17:33:20 +09002308func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002309 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002310 apex {
2311 name: "myapex",
2312 key: "myapex.key",
2313 multilib: {
2314 both: {
2315 native_shared_libs: ["mylib"],
2316 binaries: ["mybin"],
2317 },
2318 },
2319 compile_multilib: "both",
2320 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002321 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002322 }
2323
2324 apex_key {
2325 name: "myapex.key",
2326 public_key: "testkey.avbpubkey",
2327 private_key: "testkey.pem",
2328 }
2329
2330 cc_library {
2331 name: "mylib",
2332 relative_install_path: "foo/bar",
2333 system_shared_libs: [],
2334 stl: "none",
2335 apex_available: [ "myapex" ],
2336 native_bridge_supported: true,
2337 }
2338
2339 cc_binary {
2340 name: "mybin",
2341 relative_install_path: "foo/bar",
2342 system_shared_libs: [],
2343 static_executable: true,
2344 stl: "none",
2345 apex_available: [ "myapex" ],
2346 native_bridge_supported: true,
2347 compile_multilib: "both", // default is "first" for binary
2348 multilib: {
2349 lib64: {
2350 suffix: "64",
2351 },
2352 },
2353 }
2354 `, withNativeBridgeEnabled)
2355 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2356 "bin/foo/bar/mybin",
2357 "bin/foo/bar/mybin64",
2358 "bin/arm/foo/bar/mybin",
2359 "bin/arm64/foo/bar/mybin64",
2360 "lib/foo/bar/mylib.so",
2361 "lib/arm/foo/bar/mylib.so",
2362 "lib64/foo/bar/mylib.so",
2363 "lib64/arm64/foo/bar/mylib.so",
2364 })
2365}
2366
Jiyong Parkda6eb592018-12-19 17:12:36 +09002367func TestUseVendor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002368 ctx := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002369 apex {
2370 name: "myapex",
2371 key: "myapex.key",
2372 native_shared_libs: ["mylib"],
2373 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002374 updatable: false,
Jiyong Parkda6eb592018-12-19 17:12:36 +09002375 }
2376
2377 apex_key {
2378 name: "myapex.key",
2379 public_key: "testkey.avbpubkey",
2380 private_key: "testkey.pem",
2381 }
2382
2383 cc_library {
2384 name: "mylib",
2385 srcs: ["mylib.cpp"],
2386 shared_libs: ["mylib2"],
2387 system_shared_libs: [],
2388 vendor_available: true,
2389 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002390 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002391 }
2392
2393 cc_library {
2394 name: "mylib2",
2395 srcs: ["mylib.cpp"],
2396 system_shared_libs: [],
2397 vendor_available: true,
2398 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002399 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002400 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002401 `,
2402 setUseVendorAllowListForTest([]string{"myapex"}),
2403 )
Jiyong Parkda6eb592018-12-19 17:12:36 +09002404
2405 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002406 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002407 for _, implicit := range i.Implicits {
2408 inputsList = append(inputsList, implicit.String())
2409 }
2410 }
2411 inputsString := strings.Join(inputsList, " ")
2412
2413 // ensure that the apex includes vendor variants of the direct and indirect deps
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002414 ensureContains(t, inputsString, "android_vendor.29_arm64_armv8-a_shared_apex10000/mylib.so")
2415 ensureContains(t, inputsString, "android_vendor.29_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002416
2417 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002418 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2419 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002420}
Jiyong Park16e91a02018-12-20 18:18:08 +09002421
Jooyung Han85d61762020-06-24 23:50:26 +09002422func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002423 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2424 apex {
2425 name: "myapex",
2426 key: "myapex.key",
2427 use_vendor: true,
2428 }
2429 apex_key {
2430 name: "myapex.key",
2431 public_key: "testkey.avbpubkey",
2432 private_key: "testkey.pem",
2433 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002434 `,
2435 setUseVendorAllowListForTest([]string{""}),
2436 )
Colin Cross440e0d02020-06-11 11:32:11 -07002437 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002438 testApex(t, `
2439 apex {
2440 name: "myapex",
2441 key: "myapex.key",
2442 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002443 updatable: false,
Jooyung Handc782442019-11-01 03:14:38 +09002444 }
2445 apex_key {
2446 name: "myapex.key",
2447 public_key: "testkey.avbpubkey",
2448 private_key: "testkey.pem",
2449 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002450 `,
2451 setUseVendorAllowListForTest([]string{"myapex"}),
2452 )
Jooyung Handc782442019-11-01 03:14:38 +09002453}
2454
Jooyung Han5c998b92019-06-27 11:30:33 +09002455func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2456 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2457 apex {
2458 name: "myapex",
2459 key: "myapex.key",
2460 native_shared_libs: ["mylib"],
2461 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002462 updatable: false,
Jooyung Han5c998b92019-06-27 11:30:33 +09002463 }
2464
2465 apex_key {
2466 name: "myapex.key",
2467 public_key: "testkey.avbpubkey",
2468 private_key: "testkey.pem",
2469 }
2470
2471 cc_library {
2472 name: "mylib",
2473 srcs: ["mylib.cpp"],
2474 system_shared_libs: [],
2475 stl: "none",
2476 }
2477 `)
2478}
2479
Jooyung Han85d61762020-06-24 23:50:26 +09002480func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002481 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002482 apex {
2483 name: "myapex",
2484 key: "myapex.key",
2485 binaries: ["mybin"],
2486 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002487 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002488 }
2489 apex_key {
2490 name: "myapex.key",
2491 public_key: "testkey.avbpubkey",
2492 private_key: "testkey.pem",
2493 }
2494 cc_binary {
2495 name: "mybin",
2496 vendor: true,
2497 shared_libs: ["libfoo"],
2498 }
2499 cc_library {
2500 name: "libfoo",
2501 proprietary: true,
2502 }
2503 `)
2504
2505 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2506 "bin/mybin",
2507 "lib64/libfoo.so",
2508 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2509 "lib64/libc++.so",
2510 })
2511
2512 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002513 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002514 name := apexBundle.BaseModuleName()
2515 prefix := "TARGET_"
2516 var builder strings.Builder
2517 data.Custom(&builder, name, prefix, "", data)
Paul Duffin37ba3442021-03-29 00:21:08 +01002518 androidMk := android.StringRelativeToTop(ctx.Config(), builder.String())
2519 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002520 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002521
2522 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2523 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2524 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002525}
2526
Jooyung Handf78e212020-07-22 15:54:47 +09002527func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002528 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002529 apex {
2530 name: "myapex",
2531 key: "myapex.key",
2532 binaries: ["mybin"],
2533 vendor: true,
2534 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002535 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002536 }
2537 apex_key {
2538 name: "myapex.key",
2539 public_key: "testkey.avbpubkey",
2540 private_key: "testkey.pem",
2541 }
2542 cc_binary {
2543 name: "mybin",
2544 vendor: true,
2545 shared_libs: ["libvndk", "libvendor"],
2546 }
2547 cc_library {
2548 name: "libvndk",
2549 vndk: {
2550 enabled: true,
2551 },
2552 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002553 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002554 }
2555 cc_library {
2556 name: "libvendor",
2557 vendor: true,
2558 }
2559 `)
2560
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002561 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002562
Paul Duffina71a67a2021-03-29 00:42:57 +01002563 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002564 libs := names(ldRule.Args["libFlags"])
2565 // VNDK libs(libvndk/libc++) as they are
Paul Duffin37ba3442021-03-29 00:21:08 +01002566 ensureListContains(t, libs, "out/soong/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
2567 ensureListContains(t, libs, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002568 // non-stable Vendor libs as APEX variants
Paul Duffin37ba3442021-03-29 00:21:08 +01002569 ensureListContains(t, libs, "out/soong/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002570
2571 // VNDK libs are not included when use_vndk_as_stable: true
2572 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2573 "bin/mybin",
2574 "lib64/libvendor.so",
2575 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002576
2577 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2578 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2579 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002580}
2581
Justin Yun13decfb2021-03-08 19:25:55 +09002582func TestProductVariant(t *testing.T) {
2583 ctx := testApex(t, `
2584 apex {
2585 name: "myapex",
2586 key: "myapex.key",
2587 updatable: false,
2588 product_specific: true,
2589 binaries: ["foo"],
2590 }
2591
2592 apex_key {
2593 name: "myapex.key",
2594 public_key: "testkey.avbpubkey",
2595 private_key: "testkey.pem",
2596 }
2597
2598 cc_binary {
2599 name: "foo",
2600 product_available: true,
2601 apex_available: ["myapex"],
2602 srcs: ["foo.cpp"],
2603 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002604 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2605 variables.ProductVndkVersion = proptools.StringPtr("current")
2606 }),
2607 )
Justin Yun13decfb2021-03-08 19:25:55 +09002608
2609 cflags := strings.Fields(
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002610 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002611 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2612 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2613 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2614 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2615}
2616
Jooyung Han8e5685d2020-09-21 11:02:57 +09002617func TestApex_withPrebuiltFirmware(t *testing.T) {
2618 testCases := []struct {
2619 name string
2620 additionalProp string
2621 }{
2622 {"system apex with prebuilt_firmware", ""},
2623 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2624 }
2625 for _, tc := range testCases {
2626 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002627 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002628 apex {
2629 name: "myapex",
2630 key: "myapex.key",
2631 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002632 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002633 `+tc.additionalProp+`
2634 }
2635 apex_key {
2636 name: "myapex.key",
2637 public_key: "testkey.avbpubkey",
2638 private_key: "testkey.pem",
2639 }
2640 prebuilt_firmware {
2641 name: "myfirmware",
2642 src: "myfirmware.bin",
2643 filename_from_src: true,
2644 `+tc.additionalProp+`
2645 }
2646 `)
2647 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2648 "etc/firmware/myfirmware.bin",
2649 })
2650 })
2651 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002652}
2653
Jooyung Hanefb184e2020-06-25 17:14:25 +09002654func TestAndroidMk_UseVendorRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002655 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002656 apex {
2657 name: "myapex",
2658 key: "myapex.key",
2659 use_vendor: true,
2660 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002661 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002662 }
2663
2664 apex_key {
2665 name: "myapex.key",
2666 public_key: "testkey.avbpubkey",
2667 private_key: "testkey.pem",
2668 }
2669
2670 cc_library {
2671 name: "mylib",
2672 vendor_available: true,
2673 apex_available: ["myapex"],
2674 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002675 `,
2676 setUseVendorAllowListForTest([]string{"myapex"}),
2677 )
Jooyung Hanefb184e2020-06-25 17:14:25 +09002678
2679 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002680 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002681 name := apexBundle.BaseModuleName()
2682 prefix := "TARGET_"
2683 var builder strings.Builder
2684 data.Custom(&builder, name, prefix, "", data)
2685 androidMk := builder.String()
2686 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2687}
2688
2689func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002690 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002691 apex {
2692 name: "myapex",
2693 key: "myapex.key",
2694 vendor: true,
2695 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002696 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002697 }
2698
2699 apex_key {
2700 name: "myapex.key",
2701 public_key: "testkey.avbpubkey",
2702 private_key: "testkey.pem",
2703 }
2704
2705 cc_library {
2706 name: "mylib",
2707 vendor_available: true,
2708 }
2709 `)
2710
2711 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002712 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002713 name := apexBundle.BaseModuleName()
2714 prefix := "TARGET_"
2715 var builder strings.Builder
2716 data.Custom(&builder, name, prefix, "", data)
2717 androidMk := builder.String()
2718 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2719}
2720
Jooyung Han2ed99d02020-06-24 23:26:26 +09002721func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002722 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002723 apex {
2724 name: "myapex",
2725 key: "myapex.key",
2726 vintf_fragments: ["fragment.xml"],
2727 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002728 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002729 }
2730 apex_key {
2731 name: "myapex.key",
2732 public_key: "testkey.avbpubkey",
2733 private_key: "testkey.pem",
2734 }
2735 cc_binary {
2736 name: "mybin",
2737 }
2738 `)
2739
2740 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002741 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002742 name := apexBundle.BaseModuleName()
2743 prefix := "TARGET_"
2744 var builder strings.Builder
2745 data.Custom(&builder, name, prefix, "", data)
2746 androidMk := builder.String()
2747 ensureContains(t, androidMk, "LOCAL_VINTF_FRAGMENTS := fragment.xml\n")
2748 ensureContains(t, androidMk, "LOCAL_INIT_RC := init.rc\n")
2749}
2750
Jiyong Park16e91a02018-12-20 18:18:08 +09002751func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002752 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002753 apex {
2754 name: "myapex",
2755 key: "myapex.key",
2756 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002757 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002758 }
2759
2760 apex_key {
2761 name: "myapex.key",
2762 public_key: "testkey.avbpubkey",
2763 private_key: "testkey.pem",
2764 }
2765
2766 cc_library {
2767 name: "mylib",
2768 srcs: ["mylib.cpp"],
2769 system_shared_libs: [],
2770 stl: "none",
2771 stubs: {
2772 versions: ["1", "2", "3"],
2773 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002774 apex_available: [
2775 "//apex_available:platform",
2776 "myapex",
2777 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002778 }
2779
2780 cc_binary {
2781 name: "not_in_apex",
2782 srcs: ["mylib.cpp"],
2783 static_libs: ["mylib"],
2784 static_executable: true,
2785 system_shared_libs: [],
2786 stl: "none",
2787 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002788 `)
2789
Colin Cross7113d202019-11-20 16:39:12 -08002790 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002791
2792 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002793 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002794}
Jiyong Park9335a262018-12-24 11:31:58 +09002795
2796func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002797 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002798 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002799 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002800 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002801 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002802 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002803 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002804 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09002805 }
2806
2807 cc_library {
2808 name: "mylib",
2809 srcs: ["mylib.cpp"],
2810 system_shared_libs: [],
2811 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002812 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002813 }
2814
2815 apex_key {
2816 name: "myapex.key",
2817 public_key: "testkey.avbpubkey",
2818 private_key: "testkey.pem",
2819 }
2820
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002821 android_app_certificate {
2822 name: "myapex.certificate",
2823 certificate: "testkey",
2824 }
2825
2826 android_app_certificate {
2827 name: "myapex.certificate.override",
2828 certificate: "testkey.override",
2829 }
2830
Jiyong Park9335a262018-12-24 11:31:58 +09002831 `)
2832
2833 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002834 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002835
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002836 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2837 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002838 "vendor/foo/devkeys/testkey.avbpubkey")
2839 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002840 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2841 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002842 "vendor/foo/devkeys/testkey.pem")
2843 }
2844
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002845 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002846 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002847 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002848 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002849 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002850 }
2851}
Jiyong Park58e364a2019-01-19 19:24:06 +09002852
Jooyung Hanf121a652019-12-17 14:30:11 +09002853func TestCertificate(t *testing.T) {
2854 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002855 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002856 apex {
2857 name: "myapex",
2858 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002859 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002860 }
2861 apex_key {
2862 name: "myapex.key",
2863 public_key: "testkey.avbpubkey",
2864 private_key: "testkey.pem",
2865 }`)
2866 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2867 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2868 if actual := rule.Args["certificates"]; actual != expected {
2869 t.Errorf("certificates should be %q, not %q", expected, actual)
2870 }
2871 })
2872 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002873 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002874 apex {
2875 name: "myapex_keytest",
2876 key: "myapex.key",
2877 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002878 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002879 }
2880 apex_key {
2881 name: "myapex.key",
2882 public_key: "testkey.avbpubkey",
2883 private_key: "testkey.pem",
2884 }
2885 android_app_certificate {
2886 name: "myapex.certificate.override",
2887 certificate: "testkey.override",
2888 }`)
2889 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2890 expected := "testkey.override.x509.pem testkey.override.pk8"
2891 if actual := rule.Args["certificates"]; actual != expected {
2892 t.Errorf("certificates should be %q, not %q", expected, actual)
2893 }
2894 })
2895 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002896 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002897 apex {
2898 name: "myapex",
2899 key: "myapex.key",
2900 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002901 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002902 }
2903 apex_key {
2904 name: "myapex.key",
2905 public_key: "testkey.avbpubkey",
2906 private_key: "testkey.pem",
2907 }
2908 android_app_certificate {
2909 name: "myapex.certificate",
2910 certificate: "testkey",
2911 }`)
2912 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2913 expected := "testkey.x509.pem testkey.pk8"
2914 if actual := rule.Args["certificates"]; actual != expected {
2915 t.Errorf("certificates should be %q, not %q", expected, actual)
2916 }
2917 })
2918 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002919 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002920 apex {
2921 name: "myapex_keytest",
2922 key: "myapex.key",
2923 file_contexts: ":myapex-file_contexts",
2924 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002925 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002926 }
2927 apex_key {
2928 name: "myapex.key",
2929 public_key: "testkey.avbpubkey",
2930 private_key: "testkey.pem",
2931 }
2932 android_app_certificate {
2933 name: "myapex.certificate.override",
2934 certificate: "testkey.override",
2935 }`)
2936 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2937 expected := "testkey.override.x509.pem testkey.override.pk8"
2938 if actual := rule.Args["certificates"]; actual != expected {
2939 t.Errorf("certificates should be %q, not %q", expected, actual)
2940 }
2941 })
2942 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002943 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002944 apex {
2945 name: "myapex",
2946 key: "myapex.key",
2947 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002948 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002949 }
2950 apex_key {
2951 name: "myapex.key",
2952 public_key: "testkey.avbpubkey",
2953 private_key: "testkey.pem",
2954 }`)
2955 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2956 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2957 if actual := rule.Args["certificates"]; actual != expected {
2958 t.Errorf("certificates should be %q, not %q", expected, actual)
2959 }
2960 })
2961 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002962 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002963 apex {
2964 name: "myapex_keytest",
2965 key: "myapex.key",
2966 file_contexts: ":myapex-file_contexts",
2967 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002968 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002969 }
2970 apex_key {
2971 name: "myapex.key",
2972 public_key: "testkey.avbpubkey",
2973 private_key: "testkey.pem",
2974 }
2975 android_app_certificate {
2976 name: "myapex.certificate.override",
2977 certificate: "testkey.override",
2978 }`)
2979 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2980 expected := "testkey.override.x509.pem testkey.override.pk8"
2981 if actual := rule.Args["certificates"]; actual != expected {
2982 t.Errorf("certificates should be %q, not %q", expected, actual)
2983 }
2984 })
2985}
2986
Jiyong Park58e364a2019-01-19 19:24:06 +09002987func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002988 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002989 apex {
2990 name: "myapex",
2991 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002992 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002993 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09002994 }
2995
2996 apex {
2997 name: "otherapex",
2998 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002999 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003000 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003001 }
3002
3003 apex_key {
3004 name: "myapex.key",
3005 public_key: "testkey.avbpubkey",
3006 private_key: "testkey.pem",
3007 }
3008
3009 cc_library {
3010 name: "mylib",
3011 srcs: ["mylib.cpp"],
3012 system_shared_libs: [],
3013 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003014 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003015 "myapex",
3016 "otherapex",
3017 ],
Jooyung Han24282772020-03-21 23:20:55 +09003018 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003019 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003020 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003021 cc_library {
3022 name: "mylib2",
3023 srcs: ["mylib.cpp"],
3024 system_shared_libs: [],
3025 stl: "none",
3026 apex_available: [
3027 "myapex",
3028 "otherapex",
3029 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003030 static_libs: ["mylib3"],
3031 recovery_available: true,
3032 min_sdk_version: "29",
3033 }
3034 cc_library {
3035 name: "mylib3",
3036 srcs: ["mylib.cpp"],
3037 system_shared_libs: [],
3038 stl: "none",
3039 apex_available: [
3040 "myapex",
3041 "otherapex",
3042 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09003043 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07003044 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003045 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003046 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003047 `)
3048
Jooyung Hanc87a0592020-03-02 17:44:33 +09003049 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003050 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003051 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003052 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003053
Jooyung Hanccce2f22020-03-07 03:45:53 +09003054 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003055 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003056 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003057 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09003058 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003059
Jooyung Hanccce2f22020-03-07 03:45:53 +09003060 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003061 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003062 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003063 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09003064 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003065
Colin Crossaede88c2020-08-11 12:17:01 -07003066 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3067 // each variant defines additional macros to distinguish which apex variant it is built for
3068
3069 // non-APEX variant does not have __ANDROID_APEX__ defined
3070 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3071 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3072
3073 // APEX variant has __ANDROID_APEX__ defined
3074 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
3075 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3076 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3077 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3078
3079 // APEX variant has __ANDROID_APEX__ defined
3080 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
3081 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3082 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3083 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3084
Dan Albertb19953d2020-11-17 15:29:36 -08003085 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003086 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3087 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003088 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003089
3090 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
3091 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09003092
3093 // non-APEX variant does not have __ANDROID_APEX__ defined
3094 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3095 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3096
3097 // APEX variant has __ANDROID_APEX__ defined
3098 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003099 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003100 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003101 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003102
Jooyung Hanc87a0592020-03-02 17:44:33 +09003103 // APEX variant has __ANDROID_APEX__ defined
3104 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003105 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003106 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003107 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09003108
Dan Albertb19953d2020-11-17 15:29:36 -08003109 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003110 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003111 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003112 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003113}
Jiyong Park7e636d02019-01-28 16:16:54 +09003114
3115func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003116 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003117 apex {
3118 name: "myapex",
3119 key: "myapex.key",
3120 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003121 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003122 }
3123
3124 apex_key {
3125 name: "myapex.key",
3126 public_key: "testkey.avbpubkey",
3127 private_key: "testkey.pem",
3128 }
3129
3130 cc_library_headers {
3131 name: "mylib_headers",
3132 export_include_dirs: ["my_include"],
3133 system_shared_libs: [],
3134 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003135 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003136 }
3137
3138 cc_library {
3139 name: "mylib",
3140 srcs: ["mylib.cpp"],
3141 system_shared_libs: [],
3142 stl: "none",
3143 header_libs: ["mylib_headers"],
3144 export_header_lib_headers: ["mylib_headers"],
3145 stubs: {
3146 versions: ["1", "2", "3"],
3147 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003148 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003149 }
3150
3151 cc_library {
3152 name: "otherlib",
3153 srcs: ["mylib.cpp"],
3154 system_shared_libs: [],
3155 stl: "none",
3156 shared_libs: ["mylib"],
3157 }
3158 `)
3159
Colin Cross7113d202019-11-20 16:39:12 -08003160 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003161
3162 // Ensure that the include path of the header lib is exported to 'otherlib'
3163 ensureContains(t, cFlags, "-Imy_include")
3164}
Alex Light9670d332019-01-29 18:07:33 -08003165
Jiyong Park7cd10e32020-01-14 09:22:18 +09003166type fileInApex struct {
3167 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003168 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003169 isLink bool
3170}
3171
Jooyung Hana57af4a2020-01-23 05:36:59 +00003172func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003173 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003174 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003175 copyCmds := apexRule.Args["copy_commands"]
3176 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003177 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003178 for _, cmd := range strings.Split(copyCmds, "&&") {
3179 cmd = strings.TrimSpace(cmd)
3180 if cmd == "" {
3181 continue
3182 }
3183 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003184 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003185 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003186 switch terms[0] {
3187 case "mkdir":
3188 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003189 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003190 t.Fatal("copyCmds contains invalid cp command", cmd)
3191 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003192 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003193 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003194 isLink = false
3195 case "ln":
3196 if len(terms) != 3 && len(terms) != 4 {
3197 // ln LINK TARGET or ln -s LINK TARGET
3198 t.Fatal("copyCmds contains invalid ln command", cmd)
3199 }
3200 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003201 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003202 isLink = true
3203 default:
3204 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3205 }
3206 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003207 index := strings.Index(dst, imageApexDir)
3208 if index == -1 {
3209 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3210 }
3211 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003212 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003213 }
3214 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003215 return ret
3216}
3217
Jooyung Hana57af4a2020-01-23 05:36:59 +00003218func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3219 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003220 var failed bool
3221 var surplus []string
3222 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003223 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003224 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003225 for _, expected := range files {
3226 if matched, _ := path.Match(expected, file.path); matched {
3227 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003228 mactchFound = true
3229 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003230 }
3231 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003232 if !mactchFound {
3233 surplus = append(surplus, file.path)
3234 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003235 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003236
Jooyung Han31c470b2019-10-18 16:26:59 +09003237 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003238 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003239 t.Log("surplus files", surplus)
3240 failed = true
3241 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003242
3243 if len(files) > len(filesMatched) {
3244 var missing []string
3245 for _, expected := range files {
3246 if !filesMatched[expected] {
3247 missing = append(missing, expected)
3248 }
3249 }
3250 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003251 t.Log("missing files", missing)
3252 failed = true
3253 }
3254 if failed {
3255 t.Fail()
3256 }
3257}
3258
Jooyung Han344d5432019-08-23 11:17:39 +09003259func TestVndkApexCurrent(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003260 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003261 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003262 name: "com.android.vndk.current",
3263 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003264 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003265 }
3266
3267 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003268 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003269 public_key: "testkey.avbpubkey",
3270 private_key: "testkey.pem",
3271 }
3272
3273 cc_library {
3274 name: "libvndk",
3275 srcs: ["mylib.cpp"],
3276 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003277 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003278 vndk: {
3279 enabled: true,
3280 },
3281 system_shared_libs: [],
3282 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003283 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003284 }
3285
3286 cc_library {
3287 name: "libvndksp",
3288 srcs: ["mylib.cpp"],
3289 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003290 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003291 vndk: {
3292 enabled: true,
3293 support_system_process: true,
3294 },
3295 system_shared_libs: [],
3296 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003297 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003298 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003299 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003300
Colin Cross2807f002021-03-02 10:15:29 -08003301 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003302 "lib/libvndk.so",
3303 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003304 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003305 "lib64/libvndk.so",
3306 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003307 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003308 "etc/llndk.libraries.29.txt",
3309 "etc/vndkcore.libraries.29.txt",
3310 "etc/vndksp.libraries.29.txt",
3311 "etc/vndkprivate.libraries.29.txt",
3312 "etc/vndkproduct.libraries.29.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003313 })
Jooyung Han344d5432019-08-23 11:17:39 +09003314}
3315
3316func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003317 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003318 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003319 name: "com.android.vndk.current",
3320 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003321 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003322 }
3323
3324 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003325 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003326 public_key: "testkey.avbpubkey",
3327 private_key: "testkey.pem",
3328 }
3329
3330 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003331 name: "libvndk",
3332 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003333 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003334 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003335 vndk: {
3336 enabled: true,
3337 },
3338 system_shared_libs: [],
3339 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003340 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003341 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003342
3343 cc_prebuilt_library_shared {
3344 name: "libvndk.arm",
3345 srcs: ["libvndk.arm.so"],
3346 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003347 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003348 vndk: {
3349 enabled: true,
3350 },
3351 enabled: false,
3352 arch: {
3353 arm: {
3354 enabled: true,
3355 },
3356 },
3357 system_shared_libs: [],
3358 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003359 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003360 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003361 `+vndkLibrariesTxtFiles("current"),
3362 withFiles(map[string][]byte{
3363 "libvndk.so": nil,
3364 "libvndk.arm.so": nil,
3365 }))
Colin Cross2807f002021-03-02 10:15:29 -08003366 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003367 "lib/libvndk.so",
3368 "lib/libvndk.arm.so",
3369 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003370 "lib/libc++.so",
3371 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003372 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003373 })
Jooyung Han344d5432019-08-23 11:17:39 +09003374}
3375
Jooyung Han39edb6c2019-11-06 16:53:07 +09003376func vndkLibrariesTxtFiles(vers ...string) (result string) {
3377 for _, v := range vers {
3378 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003379 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003380 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003381 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003382 name: "` + txt + `.libraries.txt",
3383 }
3384 `
3385 }
3386 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003387 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003388 result += `
3389 prebuilt_etc {
3390 name: "` + txt + `.libraries.` + v + `.txt",
3391 src: "dummy.txt",
3392 }
3393 `
3394 }
3395 }
3396 }
3397 return
3398}
3399
Jooyung Han344d5432019-08-23 11:17:39 +09003400func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003401 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003402 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003403 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003404 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003405 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003406 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003407 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003408 }
3409
3410 apex_key {
3411 name: "myapex.key",
3412 public_key: "testkey.avbpubkey",
3413 private_key: "testkey.pem",
3414 }
3415
Jooyung Han31c470b2019-10-18 16:26:59 +09003416 vndk_prebuilt_shared {
3417 name: "libvndk27",
3418 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003419 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003420 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003421 vndk: {
3422 enabled: true,
3423 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003424 target_arch: "arm64",
3425 arch: {
3426 arm: {
3427 srcs: ["libvndk27_arm.so"],
3428 },
3429 arm64: {
3430 srcs: ["libvndk27_arm64.so"],
3431 },
3432 },
Colin Cross2807f002021-03-02 10:15:29 -08003433 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003434 }
3435
3436 vndk_prebuilt_shared {
3437 name: "libvndk27",
3438 version: "27",
3439 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003440 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003441 vndk: {
3442 enabled: true,
3443 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003444 target_arch: "x86_64",
3445 arch: {
3446 x86: {
3447 srcs: ["libvndk27_x86.so"],
3448 },
3449 x86_64: {
3450 srcs: ["libvndk27_x86_64.so"],
3451 },
3452 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003453 }
3454 `+vndkLibrariesTxtFiles("27"),
3455 withFiles(map[string][]byte{
3456 "libvndk27_arm.so": nil,
3457 "libvndk27_arm64.so": nil,
3458 "libvndk27_x86.so": nil,
3459 "libvndk27_x86_64.so": nil,
3460 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003461
Colin Cross2807f002021-03-02 10:15:29 -08003462 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003463 "lib/libvndk27_arm.so",
3464 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003465 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003466 })
Jooyung Han344d5432019-08-23 11:17:39 +09003467}
3468
Jooyung Han90eee022019-10-01 20:02:42 +09003469func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003470 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003471 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003472 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003473 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003474 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003475 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003476 }
3477 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003478 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003479 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003480 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003481 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003482 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003483 }
3484 apex_key {
3485 name: "myapex.key",
3486 public_key: "testkey.avbpubkey",
3487 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003488 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003489
3490 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003491 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003492 actual := proptools.String(bundle.properties.Apex_name)
3493 if !reflect.DeepEqual(actual, expected) {
3494 t.Errorf("Got '%v', expected '%v'", actual, expected)
3495 }
3496 }
3497
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003498 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003499 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003500}
3501
Jooyung Han344d5432019-08-23 11:17:39 +09003502func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003503 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003504 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003505 name: "com.android.vndk.current",
3506 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003507 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003508 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003509 }
3510
3511 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003512 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003513 public_key: "testkey.avbpubkey",
3514 private_key: "testkey.pem",
3515 }
3516
3517 cc_library {
3518 name: "libvndk",
3519 srcs: ["mylib.cpp"],
3520 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003521 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003522 native_bridge_supported: true,
3523 host_supported: true,
3524 vndk: {
3525 enabled: true,
3526 },
3527 system_shared_libs: [],
3528 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003529 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003530 }
Colin Cross2807f002021-03-02 10:15:29 -08003531 `+vndkLibrariesTxtFiles("current"),
3532 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003533
Colin Cross2807f002021-03-02 10:15:29 -08003534 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003535 "lib/libvndk.so",
3536 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003537 "lib/libc++.so",
3538 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003539 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003540 })
Jooyung Han344d5432019-08-23 11:17:39 +09003541}
3542
3543func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003544 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003545 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003546 name: "com.android.vndk.current",
3547 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003548 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003549 native_bridge_supported: true,
3550 }
3551
3552 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003553 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003554 public_key: "testkey.avbpubkey",
3555 private_key: "testkey.pem",
3556 }
3557
3558 cc_library {
3559 name: "libvndk",
3560 srcs: ["mylib.cpp"],
3561 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003562 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003563 native_bridge_supported: true,
3564 host_supported: true,
3565 vndk: {
3566 enabled: true,
3567 },
3568 system_shared_libs: [],
3569 stl: "none",
3570 }
3571 `)
3572}
3573
Jooyung Han31c470b2019-10-18 16:26:59 +09003574func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003575 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003576 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003577 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003578 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003579 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003580 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003581 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003582 }
3583
3584 apex_key {
3585 name: "myapex.key",
3586 public_key: "testkey.avbpubkey",
3587 private_key: "testkey.pem",
3588 }
3589
3590 vndk_prebuilt_shared {
3591 name: "libvndk27",
3592 version: "27",
3593 target_arch: "arm",
3594 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003595 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003596 vndk: {
3597 enabled: true,
3598 },
3599 arch: {
3600 arm: {
3601 srcs: ["libvndk27.so"],
3602 }
3603 },
3604 }
3605
3606 vndk_prebuilt_shared {
3607 name: "libvndk27",
3608 version: "27",
3609 target_arch: "arm",
3610 binder32bit: true,
3611 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003612 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003613 vndk: {
3614 enabled: true,
3615 },
3616 arch: {
3617 arm: {
3618 srcs: ["libvndk27binder32.so"],
3619 }
3620 },
Colin Cross2807f002021-03-02 10:15:29 -08003621 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003622 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003623 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003624 withFiles(map[string][]byte{
3625 "libvndk27.so": nil,
3626 "libvndk27binder32.so": nil,
3627 }),
3628 withBinder32bit,
3629 withTargets(map[android.OsType][]android.Target{
3630 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003631 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3632 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003633 },
3634 }),
3635 )
3636
Colin Cross2807f002021-03-02 10:15:29 -08003637 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003638 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003639 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003640 })
3641}
3642
Jooyung Han45a96772020-06-15 14:59:42 +09003643func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003644 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003645 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003646 name: "com.android.vndk.current",
3647 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003648 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003649 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003650 }
3651
3652 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003653 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003654 public_key: "testkey.avbpubkey",
3655 private_key: "testkey.pem",
3656 }
3657
3658 cc_library {
3659 name: "libz",
3660 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003661 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003662 vndk: {
3663 enabled: true,
3664 },
3665 stubs: {
3666 symbol_file: "libz.map.txt",
3667 versions: ["30"],
3668 }
3669 }
3670 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3671 "libz.map.txt": nil,
3672 }))
3673
Colin Cross2807f002021-03-02 10:15:29 -08003674 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003675 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3676 ensureListEmpty(t, provideNativeLibs)
3677}
3678
Jooyung Hane1633032019-08-01 17:41:43 +09003679func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003680 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003681 apex {
3682 name: "myapex_nodep",
3683 key: "myapex.key",
3684 native_shared_libs: ["lib_nodep"],
3685 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003686 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003687 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003688 }
3689
3690 apex {
3691 name: "myapex_dep",
3692 key: "myapex.key",
3693 native_shared_libs: ["lib_dep"],
3694 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003695 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003696 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003697 }
3698
3699 apex {
3700 name: "myapex_provider",
3701 key: "myapex.key",
3702 native_shared_libs: ["libfoo"],
3703 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003704 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003705 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003706 }
3707
3708 apex {
3709 name: "myapex_selfcontained",
3710 key: "myapex.key",
3711 native_shared_libs: ["lib_dep", "libfoo"],
3712 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003713 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003714 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003715 }
3716
3717 apex_key {
3718 name: "myapex.key",
3719 public_key: "testkey.avbpubkey",
3720 private_key: "testkey.pem",
3721 }
3722
3723 cc_library {
3724 name: "lib_nodep",
3725 srcs: ["mylib.cpp"],
3726 system_shared_libs: [],
3727 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003728 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003729 }
3730
3731 cc_library {
3732 name: "lib_dep",
3733 srcs: ["mylib.cpp"],
3734 shared_libs: ["libfoo"],
3735 system_shared_libs: [],
3736 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003737 apex_available: [
3738 "myapex_dep",
3739 "myapex_provider",
3740 "myapex_selfcontained",
3741 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003742 }
3743
3744 cc_library {
3745 name: "libfoo",
3746 srcs: ["mytest.cpp"],
3747 stubs: {
3748 versions: ["1"],
3749 },
3750 system_shared_libs: [],
3751 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003752 apex_available: [
3753 "myapex_provider",
3754 "myapex_selfcontained",
3755 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003756 }
3757 `)
3758
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003759 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003760 var provideNativeLibs, requireNativeLibs []string
3761
Sundong Ahnabb64432019-10-22 13:58:29 +09003762 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003763 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3764 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003765 ensureListEmpty(t, provideNativeLibs)
3766 ensureListEmpty(t, requireNativeLibs)
3767
Sundong Ahnabb64432019-10-22 13:58:29 +09003768 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003769 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3770 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003771 ensureListEmpty(t, provideNativeLibs)
3772 ensureListContains(t, requireNativeLibs, "libfoo.so")
3773
Sundong Ahnabb64432019-10-22 13:58:29 +09003774 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003775 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3776 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003777 ensureListContains(t, provideNativeLibs, "libfoo.so")
3778 ensureListEmpty(t, requireNativeLibs)
3779
Sundong Ahnabb64432019-10-22 13:58:29 +09003780 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003781 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3782 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003783 ensureListContains(t, provideNativeLibs, "libfoo.so")
3784 ensureListEmpty(t, requireNativeLibs)
3785}
3786
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003787func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003788 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003789 apex {
3790 name: "myapex",
3791 key: "myapex.key",
3792 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003793 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003794 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003795 }
3796
3797 apex_key {
3798 name: "myapex.key",
3799 public_key: "testkey.avbpubkey",
3800 private_key: "testkey.pem",
3801 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003802
3803 cc_library {
3804 name: "mylib",
3805 srcs: ["mylib.cpp"],
3806 system_shared_libs: [],
3807 stl: "none",
3808 apex_available: [
3809 "//apex_available:platform",
3810 "myapex",
3811 ],
3812 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003813 `)
3814
Sundong Ahnabb64432019-10-22 13:58:29 +09003815 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003816 apexManifestRule := module.Rule("apexManifestRule")
3817 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3818 apexRule := module.Rule("apexRule")
3819 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003820
3821 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003822 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09003823 name := apexBundle.BaseModuleName()
3824 prefix := "TARGET_"
3825 var builder strings.Builder
3826 data.Custom(&builder, name, prefix, "", data)
3827 androidMk := builder.String()
3828 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3829 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003830}
3831
Alex Light0851b882019-02-07 13:20:53 -08003832func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003833 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003834 apex {
3835 name: "myapex",
3836 key: "myapex.key",
3837 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003838 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003839 }
3840
3841 apex_key {
3842 name: "myapex.key",
3843 public_key: "testkey.avbpubkey",
3844 private_key: "testkey.pem",
3845 }
3846
3847 cc_library {
3848 name: "mylib_common",
3849 srcs: ["mylib.cpp"],
3850 system_shared_libs: [],
3851 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003852 apex_available: [
3853 "//apex_available:platform",
3854 "myapex",
3855 ],
Alex Light0851b882019-02-07 13:20:53 -08003856 }
3857 `)
3858
Sundong Ahnabb64432019-10-22 13:58:29 +09003859 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003860 apexRule := module.Rule("apexRule")
3861 copyCmds := apexRule.Args["copy_commands"]
3862
3863 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3864 t.Log("Apex was a test apex!")
3865 t.Fail()
3866 }
3867 // Ensure that main rule creates an output
3868 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3869
3870 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003871 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003872
3873 // Ensure that both direct and indirect deps are copied into apex
3874 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3875
Colin Cross7113d202019-11-20 16:39:12 -08003876 // Ensure that the platform variant ends with _shared
3877 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003878
Colin Cross56a83212020-09-15 18:30:11 -07003879 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003880 t.Log("Found mylib_common not in any apex!")
3881 t.Fail()
3882 }
3883}
3884
3885func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003886 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003887 apex_test {
3888 name: "myapex",
3889 key: "myapex.key",
3890 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003891 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003892 }
3893
3894 apex_key {
3895 name: "myapex.key",
3896 public_key: "testkey.avbpubkey",
3897 private_key: "testkey.pem",
3898 }
3899
3900 cc_library {
3901 name: "mylib_common_test",
3902 srcs: ["mylib.cpp"],
3903 system_shared_libs: [],
3904 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003905 // TODO: remove //apex_available:platform
3906 apex_available: [
3907 "//apex_available:platform",
3908 "myapex",
3909 ],
Alex Light0851b882019-02-07 13:20:53 -08003910 }
3911 `)
3912
Sundong Ahnabb64432019-10-22 13:58:29 +09003913 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003914 apexRule := module.Rule("apexRule")
3915 copyCmds := apexRule.Args["copy_commands"]
3916
3917 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3918 t.Log("Apex was not a test apex!")
3919 t.Fail()
3920 }
3921 // Ensure that main rule creates an output
3922 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3923
3924 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003925 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003926
3927 // Ensure that both direct and indirect deps are copied into apex
3928 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3929
Colin Cross7113d202019-11-20 16:39:12 -08003930 // Ensure that the platform variant ends with _shared
3931 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003932}
3933
Alex Light9670d332019-01-29 18:07:33 -08003934func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003935 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003936 apex {
3937 name: "myapex",
3938 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003939 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08003940 multilib: {
3941 first: {
3942 native_shared_libs: ["mylib_common"],
3943 }
3944 },
3945 target: {
3946 android: {
3947 multilib: {
3948 first: {
3949 native_shared_libs: ["mylib"],
3950 }
3951 }
3952 },
3953 host: {
3954 multilib: {
3955 first: {
3956 native_shared_libs: ["mylib2"],
3957 }
3958 }
3959 }
3960 }
3961 }
3962
3963 apex_key {
3964 name: "myapex.key",
3965 public_key: "testkey.avbpubkey",
3966 private_key: "testkey.pem",
3967 }
3968
3969 cc_library {
3970 name: "mylib",
3971 srcs: ["mylib.cpp"],
3972 system_shared_libs: [],
3973 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003974 // TODO: remove //apex_available:platform
3975 apex_available: [
3976 "//apex_available:platform",
3977 "myapex",
3978 ],
Alex Light9670d332019-01-29 18:07:33 -08003979 }
3980
3981 cc_library {
3982 name: "mylib_common",
3983 srcs: ["mylib.cpp"],
3984 system_shared_libs: [],
3985 stl: "none",
3986 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003987 // TODO: remove //apex_available:platform
3988 apex_available: [
3989 "//apex_available:platform",
3990 "myapex",
3991 ],
Alex Light9670d332019-01-29 18:07:33 -08003992 }
3993
3994 cc_library {
3995 name: "mylib2",
3996 srcs: ["mylib.cpp"],
3997 system_shared_libs: [],
3998 stl: "none",
3999 compile_multilib: "first",
4000 }
4001 `)
4002
Sundong Ahnabb64432019-10-22 13:58:29 +09004003 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004004 copyCmds := apexRule.Args["copy_commands"]
4005
4006 // Ensure that main rule creates an output
4007 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4008
4009 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004010 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4011 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4012 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004013
4014 // Ensure that both direct and indirect deps are copied into apex
4015 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4016 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4017 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4018
Colin Cross7113d202019-11-20 16:39:12 -08004019 // Ensure that the platform variant ends with _shared
4020 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4021 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4022 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004023}
Jiyong Park04480cf2019-02-06 00:16:29 +09004024
Jiyong Park59140302020-12-14 18:44:04 +09004025func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004026 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004027 apex {
4028 name: "myapex",
4029 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004030 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09004031 arch: {
4032 arm64: {
4033 native_shared_libs: ["mylib.arm64"],
4034 },
4035 x86_64: {
4036 native_shared_libs: ["mylib.x64"],
4037 },
4038 }
4039 }
4040
4041 apex_key {
4042 name: "myapex.key",
4043 public_key: "testkey.avbpubkey",
4044 private_key: "testkey.pem",
4045 }
4046
4047 cc_library {
4048 name: "mylib.arm64",
4049 srcs: ["mylib.cpp"],
4050 system_shared_libs: [],
4051 stl: "none",
4052 // TODO: remove //apex_available:platform
4053 apex_available: [
4054 "//apex_available:platform",
4055 "myapex",
4056 ],
4057 }
4058
4059 cc_library {
4060 name: "mylib.x64",
4061 srcs: ["mylib.cpp"],
4062 system_shared_libs: [],
4063 stl: "none",
4064 // TODO: remove //apex_available:platform
4065 apex_available: [
4066 "//apex_available:platform",
4067 "myapex",
4068 ],
4069 }
4070 `)
4071
4072 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4073 copyCmds := apexRule.Args["copy_commands"]
4074
4075 // Ensure that apex variant is created for the direct dep
4076 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4077 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4078
4079 // Ensure that both direct and indirect deps are copied into apex
4080 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4081 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4082}
4083
Jiyong Park04480cf2019-02-06 00:16:29 +09004084func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004085 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004086 apex {
4087 name: "myapex",
4088 key: "myapex.key",
4089 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004090 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004091 }
4092
4093 apex_key {
4094 name: "myapex.key",
4095 public_key: "testkey.avbpubkey",
4096 private_key: "testkey.pem",
4097 }
4098
4099 sh_binary {
4100 name: "myscript",
4101 src: "mylib.cpp",
4102 filename: "myscript.sh",
4103 sub_dir: "script",
4104 }
4105 `)
4106
Sundong Ahnabb64432019-10-22 13:58:29 +09004107 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004108 copyCmds := apexRule.Args["copy_commands"]
4109
4110 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4111}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004112
Jooyung Han91df2082019-11-20 01:49:42 +09004113func TestApexInVariousPartition(t *testing.T) {
4114 testcases := []struct {
4115 propName, parition, flattenedPartition string
4116 }{
4117 {"", "system", "system_ext"},
4118 {"product_specific: true", "product", "product"},
4119 {"soc_specific: true", "vendor", "vendor"},
4120 {"proprietary: true", "vendor", "vendor"},
4121 {"vendor: true", "vendor", "vendor"},
4122 {"system_ext_specific: true", "system_ext", "system_ext"},
4123 }
4124 for _, tc := range testcases {
4125 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004126 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004127 apex {
4128 name: "myapex",
4129 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004130 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004131 `+tc.propName+`
4132 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004133
Jooyung Han91df2082019-11-20 01:49:42 +09004134 apex_key {
4135 name: "myapex.key",
4136 public_key: "testkey.avbpubkey",
4137 private_key: "testkey.pem",
4138 }
4139 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004140
Jooyung Han91df2082019-11-20 01:49:42 +09004141 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004142 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4143 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004144 if actual != expected {
4145 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4146 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004147
Jooyung Han91df2082019-11-20 01:49:42 +09004148 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004149 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4150 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004151 if actual != expected {
4152 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4153 }
4154 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004155 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004156}
Jiyong Park67882562019-03-21 01:11:21 +09004157
Jooyung Han580eb4f2020-06-24 19:33:06 +09004158func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004159 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004160 apex {
4161 name: "myapex",
4162 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004163 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004164 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004165
Jooyung Han580eb4f2020-06-24 19:33:06 +09004166 apex_key {
4167 name: "myapex.key",
4168 public_key: "testkey.avbpubkey",
4169 private_key: "testkey.pem",
4170 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004171 `)
4172 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004173 rule := module.Output("file_contexts")
4174 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4175}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004176
Jooyung Han580eb4f2020-06-24 19:33:06 +09004177func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004178 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004179 apex {
4180 name: "myapex",
4181 key: "myapex.key",
4182 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004183 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004184 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004185
Jooyung Han580eb4f2020-06-24 19:33:06 +09004186 apex_key {
4187 name: "myapex.key",
4188 public_key: "testkey.avbpubkey",
4189 private_key: "testkey.pem",
4190 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004191 `, withFiles(map[string][]byte{
4192 "my_own_file_contexts": nil,
4193 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004194}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004195
Jooyung Han580eb4f2020-06-24 19:33:06 +09004196func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004197 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004198 apex {
4199 name: "myapex",
4200 key: "myapex.key",
4201 product_specific: true,
4202 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004203 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004204 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004205
Jooyung Han580eb4f2020-06-24 19:33:06 +09004206 apex_key {
4207 name: "myapex.key",
4208 public_key: "testkey.avbpubkey",
4209 private_key: "testkey.pem",
4210 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004211 `)
4212
Colin Cross1c460562021-02-16 17:55:47 -08004213 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004214 apex {
4215 name: "myapex",
4216 key: "myapex.key",
4217 product_specific: true,
4218 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004219 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004220 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004221
Jooyung Han580eb4f2020-06-24 19:33:06 +09004222 apex_key {
4223 name: "myapex.key",
4224 public_key: "testkey.avbpubkey",
4225 private_key: "testkey.pem",
4226 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004227 `, withFiles(map[string][]byte{
4228 "product_specific_file_contexts": nil,
4229 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004230 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4231 rule := module.Output("file_contexts")
4232 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4233}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004234
Jooyung Han580eb4f2020-06-24 19:33:06 +09004235func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004236 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004237 apex {
4238 name: "myapex",
4239 key: "myapex.key",
4240 product_specific: true,
4241 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004242 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004243 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004244
Jooyung Han580eb4f2020-06-24 19:33:06 +09004245 apex_key {
4246 name: "myapex.key",
4247 public_key: "testkey.avbpubkey",
4248 private_key: "testkey.pem",
4249 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004250
Jooyung Han580eb4f2020-06-24 19:33:06 +09004251 filegroup {
4252 name: "my-file-contexts",
4253 srcs: ["product_specific_file_contexts"],
4254 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004255 `, withFiles(map[string][]byte{
4256 "product_specific_file_contexts": nil,
4257 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004258 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4259 rule := module.Output("file_contexts")
4260 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004261}
4262
Jiyong Park67882562019-03-21 01:11:21 +09004263func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004264 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004265 apex_key {
4266 name: "myapex.key",
4267 public_key: ":my.avbpubkey",
4268 private_key: ":my.pem",
4269 product_specific: true,
4270 }
4271
4272 filegroup {
4273 name: "my.avbpubkey",
4274 srcs: ["testkey2.avbpubkey"],
4275 }
4276
4277 filegroup {
4278 name: "my.pem",
4279 srcs: ["testkey2.pem"],
4280 }
4281 `)
4282
4283 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4284 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004285 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004286 if actual_pubkey != expected_pubkey {
4287 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4288 }
4289 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004290 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004291 if actual_privkey != expected_privkey {
4292 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4293 }
4294}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004295
4296func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004297 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004298 prebuilt_apex {
4299 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004300 arch: {
4301 arm64: {
4302 src: "myapex-arm64.apex",
4303 },
4304 arm: {
4305 src: "myapex-arm.apex",
4306 },
4307 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004308 }
4309 `)
4310
4311 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4312
Jiyong Parkc95714e2019-03-29 14:23:10 +09004313 expectedInput := "myapex-arm64.apex"
4314 if prebuilt.inputApex.String() != expectedInput {
4315 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4316 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004317}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004318
4319func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004320 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004321 prebuilt_apex {
4322 name: "myapex",
4323 src: "myapex-arm.apex",
4324 filename: "notmyapex.apex",
4325 }
4326 `)
4327
4328 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4329
4330 expected := "notmyapex.apex"
4331 if p.installFilename != expected {
4332 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4333 }
4334}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004335
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004336func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004337 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004338 prebuilt_apex {
4339 name: "myapex.prebuilt",
4340 src: "myapex-arm.apex",
4341 overrides: [
4342 "myapex",
4343 ],
4344 }
4345 `)
4346
4347 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
4348
4349 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004350 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004351 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004352 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004353 }
4354}
4355
Paul Duffin092153d2021-01-26 11:42:39 +00004356// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4357// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004358func TestPrebuiltExportDexImplementationJars(t *testing.T) {
4359 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin092153d2021-01-26 11:42:39 +00004360 // Empty transformation.
Paul Duffin064b70c2020-11-02 17:32:38 +00004361 }
4362
Paul Duffin89886cb2021-02-05 16:44:03 +00004363 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004364 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004365 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004366 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004367 stem := android.RemoveOptionalPrebuiltPrefix(name)
4368 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004369 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4370 }
4371 }
4372
Paul Duffin39853512021-02-26 11:09:39 +00004373 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004374 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004375 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004376 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4377 }
4378 }
4379
4380 t.Run("prebuilt only", func(t *testing.T) {
4381 bp := `
4382 prebuilt_apex {
4383 name: "myapex",
4384 arch: {
4385 arm64: {
4386 src: "myapex-arm64.apex",
4387 },
4388 arm: {
4389 src: "myapex-arm.apex",
4390 },
4391 },
Paul Duffin39853512021-02-26 11:09:39 +00004392 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004393 }
4394
4395 java_import {
4396 name: "libfoo",
4397 jars: ["libfoo.jar"],
4398 }
Paul Duffin39853512021-02-26 11:09:39 +00004399
4400 java_sdk_library_import {
4401 name: "libbar",
4402 public: {
4403 jars: ["libbar.jar"],
4404 },
4405 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004406 `
4407
4408 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4409 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4410
Paul Duffinf6932af2021-02-26 18:21:56 +00004411 // Make sure that the deapexer has the correct input APEX.
4412 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4413 rule := deapexer.Rule("deapexer")
4414 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4415 t.Errorf("expected: %q, found: %q", expected, actual)
4416 }
4417
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004418 // Make sure that the prebuilt_apex has the correct input APEX.
4419 prebuiltApex := ctx.ModuleForTests("myapex", "android_common")
4420 rule = prebuiltApex.Rule("android/soong/android.Cp")
4421 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4422 t.Errorf("expected: %q, found: %q", expected, actual)
4423 }
4424
Paul Duffin89886cb2021-02-05 16:44:03 +00004425 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004426
4427 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004428 })
4429
4430 t.Run("prebuilt with source preferred", func(t *testing.T) {
4431
4432 bp := `
4433 prebuilt_apex {
4434 name: "myapex",
4435 arch: {
4436 arm64: {
4437 src: "myapex-arm64.apex",
4438 },
4439 arm: {
4440 src: "myapex-arm.apex",
4441 },
4442 },
Paul Duffin39853512021-02-26 11:09:39 +00004443 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004444 }
4445
4446 java_import {
4447 name: "libfoo",
4448 jars: ["libfoo.jar"],
4449 }
4450
4451 java_library {
4452 name: "libfoo",
4453 }
Paul Duffin39853512021-02-26 11:09:39 +00004454
4455 java_sdk_library_import {
4456 name: "libbar",
4457 public: {
4458 jars: ["libbar.jar"],
4459 },
4460 }
4461
4462 java_sdk_library {
4463 name: "libbar",
4464 srcs: ["foo/bar/MyClass.java"],
4465 unsafe_ignore_missing_latest_api: true,
4466 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004467 `
4468
4469 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4470 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4471
Paul Duffin89886cb2021-02-05 16:44:03 +00004472 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004473 ensureNoSourceVariant(t, ctx, "libfoo")
4474
4475 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4476 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004477 })
4478
4479 t.Run("prebuilt preferred with source", func(t *testing.T) {
4480 bp := `
4481 prebuilt_apex {
4482 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004483 arch: {
4484 arm64: {
4485 src: "myapex-arm64.apex",
4486 },
4487 arm: {
4488 src: "myapex-arm.apex",
4489 },
4490 },
Paul Duffin39853512021-02-26 11:09:39 +00004491 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004492 }
4493
4494 java_import {
4495 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004496 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004497 jars: ["libfoo.jar"],
4498 }
4499
4500 java_library {
4501 name: "libfoo",
4502 }
Paul Duffin39853512021-02-26 11:09:39 +00004503
4504 java_sdk_library_import {
4505 name: "libbar",
4506 prefer: true,
4507 public: {
4508 jars: ["libbar.jar"],
4509 },
4510 }
4511
4512 java_sdk_library {
4513 name: "libbar",
4514 srcs: ["foo/bar/MyClass.java"],
4515 unsafe_ignore_missing_latest_api: true,
4516 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004517 `
4518
4519 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4520 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4521
Paul Duffin89886cb2021-02-05 16:44:03 +00004522 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004523 ensureNoSourceVariant(t, ctx, "libfoo")
4524
4525 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4526 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004527 })
4528}
4529
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004530func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
4531 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin37856732021-02-26 14:24:15 +00004532 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo", "myapex:libbar"})
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004533 }
4534
Paul Duffin37856732021-02-26 14:24:15 +00004535 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4536 t.Helper()
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004537 s := ctx.SingletonForTests("dex_bootjars")
4538 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004539 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004540 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004541 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004542 foundLibfooJar = true
4543 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01004544 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004545 }
4546 }
4547 if !foundLibfooJar {
Paul Duffin55607122021-03-30 23:32:51 +01004548 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 +00004549 }
4550 }
4551
Paul Duffin4fd997b2021-02-03 20:06:33 +00004552 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004553 t.Helper()
Paul Duffin4fd997b2021-02-03 20:06:33 +00004554 hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
4555 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
4556 java.CheckHiddenAPIRuleInputs(t, expectedInputs, indexRule)
4557 }
4558
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004559 t.Run("prebuilt only", func(t *testing.T) {
4560 bp := `
4561 prebuilt_apex {
4562 name: "myapex",
4563 arch: {
4564 arm64: {
4565 src: "myapex-arm64.apex",
4566 },
4567 arm: {
4568 src: "myapex-arm.apex",
4569 },
4570 },
Paul Duffin37856732021-02-26 14:24:15 +00004571 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004572 }
4573
4574 java_import {
4575 name: "libfoo",
4576 jars: ["libfoo.jar"],
4577 apex_available: ["myapex"],
4578 }
Paul Duffin37856732021-02-26 14:24:15 +00004579
4580 java_sdk_library_import {
4581 name: "libbar",
4582 public: {
4583 jars: ["libbar.jar"],
4584 },
4585 apex_available: ["myapex"],
4586 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004587 `
4588
4589 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin55607122021-03-30 23:32:51 +01004590 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4591 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004592
Paul Duffin9d67ca62021-02-03 20:06:33 +00004593 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4594 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004595.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004596.intermediates/libfoo/android_common_myapex/hiddenapi/index.csv
4597`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004598 })
4599
4600 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4601 bp := `
4602 prebuilt_apex {
4603 name: "myapex",
4604 arch: {
4605 arm64: {
4606 src: "myapex-arm64.apex",
4607 },
4608 arm: {
4609 src: "myapex-arm.apex",
4610 },
4611 },
Paul Duffin37856732021-02-26 14:24:15 +00004612 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004613 }
4614
4615 java_import {
4616 name: "libfoo",
4617 jars: ["libfoo.jar"],
4618 apex_available: ["myapex"],
4619 }
4620
4621 java_library {
4622 name: "libfoo",
4623 srcs: ["foo/bar/MyClass.java"],
4624 apex_available: ["myapex"],
4625 }
Paul Duffin37856732021-02-26 14:24:15 +00004626
4627 java_sdk_library_import {
4628 name: "libbar",
4629 public: {
4630 jars: ["libbar.jar"],
4631 },
4632 apex_available: ["myapex"],
4633 }
4634
4635 java_sdk_library {
4636 name: "libbar",
4637 srcs: ["foo/bar/MyClass.java"],
4638 unsafe_ignore_missing_latest_api: true,
4639 apex_available: ["myapex"],
4640 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004641 `
4642
4643 // In this test the source (java_library) libfoo is active since the
4644 // prebuilt (java_import) defaults to prefer:false. However the
4645 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4646 // find the dex boot jar in it. We either need to disable the source libfoo
4647 // or make the prebuilt libfoo preferred.
4648 testDexpreoptWithApexes(t, bp, "failed to find a dex jar path for module 'libfoo'", transform)
4649 })
4650
4651 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4652 bp := `
4653 prebuilt_apex {
4654 name: "myapex",
4655 arch: {
4656 arm64: {
4657 src: "myapex-arm64.apex",
4658 },
4659 arm: {
4660 src: "myapex-arm.apex",
4661 },
4662 },
Paul Duffin37856732021-02-26 14:24:15 +00004663 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004664 }
4665
4666 java_import {
4667 name: "libfoo",
4668 prefer: true,
4669 jars: ["libfoo.jar"],
4670 apex_available: ["myapex"],
4671 }
4672
4673 java_library {
4674 name: "libfoo",
4675 srcs: ["foo/bar/MyClass.java"],
4676 apex_available: ["myapex"],
4677 }
Paul Duffin37856732021-02-26 14:24:15 +00004678
4679 java_sdk_library_import {
4680 name: "libbar",
4681 prefer: true,
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 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin55607122021-03-30 23:32:51 +01004697 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4698 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004699
Paul Duffin9d67ca62021-02-03 20:06:33 +00004700 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4701 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004702.intermediates/prebuilt_libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004703.intermediates/prebuilt_libfoo/android_common_myapex/hiddenapi/index.csv
4704`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004705 })
4706
4707 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4708 bp := `
4709 apex {
4710 name: "myapex",
4711 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004712 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004713 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004714 }
4715
4716 apex_key {
4717 name: "myapex.key",
4718 public_key: "testkey.avbpubkey",
4719 private_key: "testkey.pem",
4720 }
4721
4722 prebuilt_apex {
4723 name: "myapex",
4724 arch: {
4725 arm64: {
4726 src: "myapex-arm64.apex",
4727 },
4728 arm: {
4729 src: "myapex-arm.apex",
4730 },
4731 },
Paul Duffin37856732021-02-26 14:24:15 +00004732 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004733 }
4734
4735 java_import {
4736 name: "libfoo",
4737 jars: ["libfoo.jar"],
4738 apex_available: ["myapex"],
4739 }
4740
4741 java_library {
4742 name: "libfoo",
4743 srcs: ["foo/bar/MyClass.java"],
4744 apex_available: ["myapex"],
4745 }
Paul Duffin37856732021-02-26 14:24:15 +00004746
4747 java_sdk_library_import {
4748 name: "libbar",
4749 public: {
4750 jars: ["libbar.jar"],
4751 },
4752 apex_available: ["myapex"],
4753 }
4754
4755 java_sdk_library {
4756 name: "libbar",
4757 srcs: ["foo/bar/MyClass.java"],
4758 unsafe_ignore_missing_latest_api: true,
4759 apex_available: ["myapex"],
4760 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004761 `
4762
4763 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin55607122021-03-30 23:32:51 +01004764 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4765 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004766
4767 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4768 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004769.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin4fd997b2021-02-03 20:06:33 +00004770.intermediates/libfoo/android_common_apex10000/hiddenapi/index.csv
4771`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004772 })
4773
4774 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4775 bp := `
4776 apex {
4777 name: "myapex",
4778 enabled: false,
4779 key: "myapex.key",
4780 java_libs: ["libfoo"],
4781 }
4782
4783 apex_key {
4784 name: "myapex.key",
4785 public_key: "testkey.avbpubkey",
4786 private_key: "testkey.pem",
4787 }
4788
4789 prebuilt_apex {
4790 name: "myapex",
4791 arch: {
4792 arm64: {
4793 src: "myapex-arm64.apex",
4794 },
4795 arm: {
4796 src: "myapex-arm.apex",
4797 },
4798 },
Paul Duffin37856732021-02-26 14:24:15 +00004799 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004800 }
4801
4802 java_import {
4803 name: "libfoo",
4804 prefer: true,
4805 jars: ["libfoo.jar"],
4806 apex_available: ["myapex"],
4807 }
4808
4809 java_library {
4810 name: "libfoo",
4811 srcs: ["foo/bar/MyClass.java"],
4812 apex_available: ["myapex"],
4813 }
Paul Duffin37856732021-02-26 14:24:15 +00004814
4815 java_sdk_library_import {
4816 name: "libbar",
4817 prefer: true,
4818 public: {
4819 jars: ["libbar.jar"],
4820 },
4821 apex_available: ["myapex"],
4822 }
4823
4824 java_sdk_library {
4825 name: "libbar",
4826 srcs: ["foo/bar/MyClass.java"],
4827 unsafe_ignore_missing_latest_api: true,
4828 apex_available: ["myapex"],
4829 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004830 `
4831
4832 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin55607122021-03-30 23:32:51 +01004833 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4834 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004835
Paul Duffin9d67ca62021-02-03 20:06:33 +00004836 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4837 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004838.intermediates/prebuilt_libbar/android_common_prebuilt_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004839.intermediates/prebuilt_libfoo/android_common_prebuilt_myapex/hiddenapi/index.csv
4840`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004841 })
4842}
4843
Roland Levillain630846d2019-06-26 12:48:34 +01004844func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004845 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004846 apex_test {
4847 name: "myapex",
4848 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004849 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01004850 tests: [
4851 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004852 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004853 ],
4854 }
4855
4856 apex_key {
4857 name: "myapex.key",
4858 public_key: "testkey.avbpubkey",
4859 private_key: "testkey.pem",
4860 }
4861
Liz Kammer1c14a212020-05-12 15:26:55 -07004862 filegroup {
4863 name: "fg",
4864 srcs: [
4865 "baz",
4866 "bar/baz"
4867 ],
4868 }
4869
Roland Levillain630846d2019-06-26 12:48:34 +01004870 cc_test {
4871 name: "mytest",
4872 gtest: false,
4873 srcs: ["mytest.cpp"],
4874 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004875 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004876 system_shared_libs: [],
4877 static_executable: true,
4878 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004879 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004880 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004881
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004882 cc_library {
4883 name: "mylib",
4884 srcs: ["mylib.cpp"],
4885 system_shared_libs: [],
4886 stl: "none",
4887 }
4888
Liz Kammer5bd365f2020-05-27 15:15:11 -07004889 filegroup {
4890 name: "fg2",
4891 srcs: [
4892 "testdata/baz"
4893 ],
4894 }
4895
Roland Levillain9b5fde92019-06-28 15:41:19 +01004896 cc_test {
4897 name: "mytests",
4898 gtest: false,
4899 srcs: [
4900 "mytest1.cpp",
4901 "mytest2.cpp",
4902 "mytest3.cpp",
4903 ],
4904 test_per_src: true,
4905 relative_install_path: "test",
4906 system_shared_libs: [],
4907 static_executable: true,
4908 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004909 data: [
4910 ":fg",
4911 ":fg2",
4912 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004913 }
Roland Levillain630846d2019-06-26 12:48:34 +01004914 `)
4915
Sundong Ahnabb64432019-10-22 13:58:29 +09004916 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004917 copyCmds := apexRule.Args["copy_commands"]
4918
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004919 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004920 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004921 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004922
Liz Kammer1c14a212020-05-12 15:26:55 -07004923 //Ensure that test data are copied into apex.
4924 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4925 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4926
Roland Levillain9b5fde92019-06-28 15:41:19 +01004927 // Ensure that test deps built with `test_per_src` are copied into apex.
4928 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4929 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4930 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004931
4932 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004933 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004934 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004935 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004936 prefix := "TARGET_"
4937 var builder strings.Builder
4938 data.Custom(&builder, name, prefix, "", data)
4939 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004940 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4941 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4942 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4943 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004944 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004945 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004946 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004947
4948 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004949 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004950 data.Custom(&builder, name, prefix, "", data)
4951 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004952 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4953 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004954}
4955
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004956func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004957 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004958 apex {
4959 name: "myapex",
4960 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004961 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004962 }
4963 apex_key {
4964 name: "myapex.key",
4965 public_key: "testkey.avbpubkey",
4966 private_key: "testkey.pem",
4967 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00004968 `,
4969 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4970 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4971 }),
4972 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004973 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004974 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07004975 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004976 var builder strings.Builder
4977 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4978 androidMk := builder.String()
4979 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4980}
4981
Jooyung Hand48f3c32019-08-23 11:18:57 +09004982func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4983 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4984 apex {
4985 name: "myapex",
4986 key: "myapex.key",
4987 native_shared_libs: ["libfoo"],
4988 }
4989
4990 apex_key {
4991 name: "myapex.key",
4992 public_key: "testkey.avbpubkey",
4993 private_key: "testkey.pem",
4994 }
4995
4996 cc_library {
4997 name: "libfoo",
4998 stl: "none",
4999 system_shared_libs: [],
5000 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005001 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005002 }
5003 `)
5004 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5005 apex {
5006 name: "myapex",
5007 key: "myapex.key",
5008 java_libs: ["myjar"],
5009 }
5010
5011 apex_key {
5012 name: "myapex.key",
5013 public_key: "testkey.avbpubkey",
5014 private_key: "testkey.pem",
5015 }
5016
5017 java_library {
5018 name: "myjar",
5019 srcs: ["foo/bar/MyClass.java"],
5020 sdk_version: "none",
5021 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005022 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005023 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005024 }
5025 `)
5026}
5027
Bill Peckhama41a6962021-01-11 10:58:54 -08005028func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005029 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005030 apex {
5031 name: "myapex",
5032 key: "myapex.key",
5033 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005034 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005035 }
5036
5037 apex_key {
5038 name: "myapex.key",
5039 public_key: "testkey.avbpubkey",
5040 private_key: "testkey.pem",
5041 }
5042
5043 java_import {
5044 name: "myjavaimport",
5045 apex_available: ["myapex"],
5046 jars: ["my.jar"],
5047 compile_dex: true,
5048 }
5049 `)
5050
5051 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5052 apexRule := module.Rule("apexRule")
5053 copyCmds := apexRule.Args["copy_commands"]
5054 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5055}
5056
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005057func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005058 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005059 apex {
5060 name: "myapex",
5061 key: "myapex.key",
5062 apps: [
5063 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005064 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005065 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005066 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005067 }
5068
5069 apex_key {
5070 name: "myapex.key",
5071 public_key: "testkey.avbpubkey",
5072 private_key: "testkey.pem",
5073 }
5074
5075 android_app {
5076 name: "AppFoo",
5077 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005078 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005079 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005080 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005081 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005082 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005083 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005084
5085 android_app {
5086 name: "AppFooPriv",
5087 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005088 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005089 system_modules: "none",
5090 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005091 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005092 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005093 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005094
5095 cc_library_shared {
5096 name: "libjni",
5097 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005098 shared_libs: ["libfoo"],
5099 stl: "none",
5100 system_shared_libs: [],
5101 apex_available: [ "myapex" ],
5102 sdk_version: "current",
5103 }
5104
5105 cc_library_shared {
5106 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005107 stl: "none",
5108 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005109 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005110 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005111 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005112 `)
5113
Sundong Ahnabb64432019-10-22 13:58:29 +09005114 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005115 apexRule := module.Rule("apexRule")
5116 copyCmds := apexRule.Args["copy_commands"]
5117
5118 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005119 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005120
Colin Crossaede88c2020-08-11 12:17:01 -07005121 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005122 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005123 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005124 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005125 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005126 // JNI libraries including transitive deps are
5127 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005128 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005129 // ... embedded inside APK (jnilibs.zip)
5130 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5131 // ... and not directly inside the APEX
5132 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5133 }
Dario Frenicde2a032019-10-27 00:29:22 +01005134}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005135
Dario Frenicde2a032019-10-27 00:29:22 +01005136func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005137 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005138 apex {
5139 name: "myapex",
5140 key: "myapex.key",
5141 apps: [
5142 "AppFooPrebuilt",
5143 "AppFooPrivPrebuilt",
5144 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005145 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005146 }
5147
5148 apex_key {
5149 name: "myapex.key",
5150 public_key: "testkey.avbpubkey",
5151 private_key: "testkey.pem",
5152 }
5153
5154 android_app_import {
5155 name: "AppFooPrebuilt",
5156 apk: "PrebuiltAppFoo.apk",
5157 presigned: true,
5158 dex_preopt: {
5159 enabled: false,
5160 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005161 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005162 }
5163
5164 android_app_import {
5165 name: "AppFooPrivPrebuilt",
5166 apk: "PrebuiltAppFooPriv.apk",
5167 privileged: true,
5168 presigned: true,
5169 dex_preopt: {
5170 enabled: false,
5171 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005172 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005173 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005174 }
5175 `)
5176
Sundong Ahnabb64432019-10-22 13:58:29 +09005177 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005178 apexRule := module.Rule("apexRule")
5179 copyCmds := apexRule.Args["copy_commands"]
5180
5181 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005182 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5183}
5184
5185func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005186 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005187 apex {
5188 name: "myapex",
5189 key: "myapex.key",
5190 apps: [
5191 "AppFoo",
5192 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005193 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005194 }
5195
5196 apex_key {
5197 name: "myapex.key",
5198 public_key: "testkey.avbpubkey",
5199 private_key: "testkey.pem",
5200 }
5201
5202 android_app {
5203 name: "AppFoo",
5204 srcs: ["foo/bar/MyClass.java"],
5205 sdk_version: "none",
5206 system_modules: "none",
5207 apex_available: [ "myapex" ],
5208 }
5209
5210 android_app_import {
5211 name: "AppFoo",
5212 apk: "AppFooPrebuilt.apk",
5213 filename: "AppFooPrebuilt.apk",
5214 presigned: true,
5215 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005216 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005217 }
5218 `, withFiles(map[string][]byte{
5219 "AppFooPrebuilt.apk": nil,
5220 }))
5221
5222 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005223 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005224 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005225}
5226
Dario Freni6f3937c2019-12-20 22:58:03 +00005227func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005228 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005229 apex {
5230 name: "myapex",
5231 key: "myapex.key",
5232 apps: [
5233 "TesterHelpAppFoo",
5234 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005235 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005236 }
5237
5238 apex_key {
5239 name: "myapex.key",
5240 public_key: "testkey.avbpubkey",
5241 private_key: "testkey.pem",
5242 }
5243
5244 android_test_helper_app {
5245 name: "TesterHelpAppFoo",
5246 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005247 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005248 }
5249
5250 `)
5251
5252 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5253 apexRule := module.Rule("apexRule")
5254 copyCmds := apexRule.Args["copy_commands"]
5255
5256 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5257}
5258
Jooyung Han18020ea2019-11-13 10:50:48 +09005259func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5260 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005261 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005262 apex {
5263 name: "myapex",
5264 key: "myapex.key",
5265 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005266 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005267 }
5268
5269 apex_key {
5270 name: "myapex.key",
5271 public_key: "testkey.avbpubkey",
5272 private_key: "testkey.pem",
5273 }
5274
5275 apex {
5276 name: "otherapex",
5277 key: "myapex.key",
5278 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005279 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005280 }
5281
5282 cc_defaults {
5283 name: "libfoo-defaults",
5284 apex_available: ["otherapex"],
5285 }
5286
5287 cc_library {
5288 name: "libfoo",
5289 defaults: ["libfoo-defaults"],
5290 stl: "none",
5291 system_shared_libs: [],
5292 }`)
5293}
5294
Paul Duffine52e66f2020-03-30 17:54:29 +01005295func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005296 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005297 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005298 apex {
5299 name: "myapex",
5300 key: "myapex.key",
5301 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005302 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005303 }
5304
5305 apex_key {
5306 name: "myapex.key",
5307 public_key: "testkey.avbpubkey",
5308 private_key: "testkey.pem",
5309 }
5310
5311 apex {
5312 name: "otherapex",
5313 key: "otherapex.key",
5314 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005315 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005316 }
5317
5318 apex_key {
5319 name: "otherapex.key",
5320 public_key: "testkey.avbpubkey",
5321 private_key: "testkey.pem",
5322 }
5323
5324 cc_library {
5325 name: "libfoo",
5326 stl: "none",
5327 system_shared_libs: [],
5328 apex_available: ["otherapex"],
5329 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005330}
Jiyong Park127b40b2019-09-30 16:04:35 +09005331
Paul Duffine52e66f2020-03-30 17:54:29 +01005332func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005333 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005334 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005335.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005336.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005337.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005338.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005339.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005340.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005341 apex {
5342 name: "myapex",
5343 key: "myapex.key",
5344 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005345 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005346 }
5347
5348 apex_key {
5349 name: "myapex.key",
5350 public_key: "testkey.avbpubkey",
5351 private_key: "testkey.pem",
5352 }
5353
Jiyong Park127b40b2019-09-30 16:04:35 +09005354 cc_library {
5355 name: "libfoo",
5356 stl: "none",
5357 shared_libs: ["libbar"],
5358 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005359 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005360 }
5361
5362 cc_library {
5363 name: "libbar",
5364 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005365 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005366 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005367 apex_available: ["myapex"],
5368 }
5369
5370 cc_library {
5371 name: "libbaz",
5372 stl: "none",
5373 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005374 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005375}
Jiyong Park127b40b2019-09-30 16:04:35 +09005376
Paul Duffine52e66f2020-03-30 17:54:29 +01005377func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005378 testApexError(t, "\"otherapex\" is not a valid module name", `
5379 apex {
5380 name: "myapex",
5381 key: "myapex.key",
5382 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005383 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005384 }
5385
5386 apex_key {
5387 name: "myapex.key",
5388 public_key: "testkey.avbpubkey",
5389 private_key: "testkey.pem",
5390 }
5391
5392 cc_library {
5393 name: "libfoo",
5394 stl: "none",
5395 system_shared_libs: [],
5396 apex_available: ["otherapex"],
5397 }`)
5398
Paul Duffine52e66f2020-03-30 17:54:29 +01005399 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005400 apex {
5401 name: "myapex",
5402 key: "myapex.key",
5403 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005404 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005405 }
5406
5407 apex_key {
5408 name: "myapex.key",
5409 public_key: "testkey.avbpubkey",
5410 private_key: "testkey.pem",
5411 }
5412
5413 cc_library {
5414 name: "libfoo",
5415 stl: "none",
5416 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005417 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005418 apex_available: ["myapex"],
5419 }
5420
5421 cc_library {
5422 name: "libbar",
5423 stl: "none",
5424 system_shared_libs: [],
5425 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005426 }
5427
5428 cc_library {
5429 name: "libbaz",
5430 stl: "none",
5431 system_shared_libs: [],
5432 stubs: {
5433 versions: ["10", "20", "30"],
5434 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005435 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005436}
Jiyong Park127b40b2019-09-30 16:04:35 +09005437
Jiyong Park89e850a2020-04-07 16:37:39 +09005438func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005439 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005440 apex {
5441 name: "myapex",
5442 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005443 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005444 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005445 }
5446
5447 apex_key {
5448 name: "myapex.key",
5449 public_key: "testkey.avbpubkey",
5450 private_key: "testkey.pem",
5451 }
5452
5453 cc_library {
5454 name: "libfoo",
5455 stl: "none",
5456 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005457 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005458 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005459 }
5460
5461 cc_library {
5462 name: "libfoo2",
5463 stl: "none",
5464 system_shared_libs: [],
5465 shared_libs: ["libbaz"],
5466 apex_available: ["//apex_available:platform"],
5467 }
5468
5469 cc_library {
5470 name: "libbar",
5471 stl: "none",
5472 system_shared_libs: [],
5473 apex_available: ["myapex"],
5474 }
5475
5476 cc_library {
5477 name: "libbaz",
5478 stl: "none",
5479 system_shared_libs: [],
5480 apex_available: ["myapex"],
5481 stubs: {
5482 versions: ["1"],
5483 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005484 }`)
5485
Jiyong Park89e850a2020-04-07 16:37:39 +09005486 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5487 // because it depends on libbar which isn't available to platform
5488 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5489 if libfoo.NotAvailableForPlatform() != true {
5490 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5491 }
5492
5493 // libfoo2 however can be available to platform because it depends on libbaz which provides
5494 // stubs
5495 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5496 if libfoo2.NotAvailableForPlatform() == true {
5497 t.Errorf("%q should be available to platform", libfoo2.String())
5498 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005499}
Jiyong Parka90ca002019-10-07 15:47:24 +09005500
Paul Duffine52e66f2020-03-30 17:54:29 +01005501func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005502 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005503 apex {
5504 name: "myapex",
5505 key: "myapex.key",
5506 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005507 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005508 }
5509
5510 apex_key {
5511 name: "myapex.key",
5512 public_key: "testkey.avbpubkey",
5513 private_key: "testkey.pem",
5514 }
5515
5516 cc_library {
5517 name: "libfoo",
5518 stl: "none",
5519 system_shared_libs: [],
5520 apex_available: ["myapex"],
5521 static: {
5522 apex_available: ["//apex_available:platform"],
5523 },
5524 }`)
5525
Jiyong Park89e850a2020-04-07 16:37:39 +09005526 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5527 if libfooShared.NotAvailableForPlatform() != true {
5528 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5529 }
5530 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5531 if libfooStatic.NotAvailableForPlatform() != false {
5532 t.Errorf("%q should be available to platform", libfooStatic.String())
5533 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005534}
5535
Jiyong Park5d790c32019-11-15 18:40:32 +09005536func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005537 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005538 apex {
5539 name: "myapex",
5540 key: "myapex.key",
5541 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005542 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005543 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005544 }
5545
5546 override_apex {
5547 name: "override_myapex",
5548 base: "myapex",
5549 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005550 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005551 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005552 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09005553 }
5554
5555 apex_key {
5556 name: "myapex.key",
5557 public_key: "testkey.avbpubkey",
5558 private_key: "testkey.pem",
5559 }
5560
5561 android_app {
5562 name: "app",
5563 srcs: ["foo/bar/MyClass.java"],
5564 package_name: "foo",
5565 sdk_version: "none",
5566 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005567 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005568 }
5569
5570 override_android_app {
5571 name: "override_app",
5572 base: "app",
5573 package_name: "bar",
5574 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005575 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005576
Jiyong Park317645e2019-12-05 13:20:58 +09005577 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5578 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5579 if originalVariant.GetOverriddenBy() != "" {
5580 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5581 }
5582 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5583 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5584 }
5585
Jiyong Park5d790c32019-11-15 18:40:32 +09005586 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5587 apexRule := module.Rule("apexRule")
5588 copyCmds := apexRule.Args["copy_commands"]
5589
5590 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005591 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005592
5593 apexBundle := module.Module().(*apexBundle)
5594 name := apexBundle.Name()
5595 if name != "override_myapex" {
5596 t.Errorf("name should be \"override_myapex\", but was %q", name)
5597 }
5598
Baligh Uddin004d7172020-02-19 21:29:28 -08005599 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5600 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5601 }
5602
Jiyong Park20bacab2020-03-03 11:45:41 +09005603 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005604 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09005605
Colin Crossaa255532020-07-03 13:18:24 -07005606 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005607 var builder strings.Builder
5608 data.Custom(&builder, name, "TARGET_", "", data)
5609 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005610 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005611 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5612 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005613 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005614 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005615 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005616 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5617 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005618}
5619
Jooyung Han214bf372019-11-12 13:03:50 +09005620func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005621 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005622 apex {
5623 name: "myapex",
5624 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005625 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005626 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005627 }
5628
5629 apex_key {
5630 name: "myapex.key",
5631 public_key: "testkey.avbpubkey",
5632 private_key: "testkey.pem",
5633 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005634
5635 cc_library {
5636 name: "mylib",
5637 srcs: ["mylib.cpp"],
5638 stl: "libc++",
5639 system_shared_libs: [],
5640 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005641 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005642 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005643 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005644
5645 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5646 args := module.Rule("apexRule").Args
5647 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005648 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005649
5650 // The copies of the libraries in the apex should have one more dependency than
5651 // the ones outside the apex, namely the unwinder. Ideally we should check
5652 // the dependency names directly here but for some reason the names are blank in
5653 // this test.
5654 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005655 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005656 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5657 if len(apexImplicits) != len(nonApexImplicits)+1 {
5658 t.Errorf("%q missing unwinder dep", lib)
5659 }
5660 }
Jooyung Han214bf372019-11-12 13:03:50 +09005661}
5662
Paul Duffine05480a2021-03-08 15:07:14 +00005663var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01005664 "api/current.txt": nil,
5665 "api/removed.txt": nil,
5666 "api/system-current.txt": nil,
5667 "api/system-removed.txt": nil,
5668 "api/test-current.txt": nil,
5669 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005670
Anton Hanssondff2c782020-12-21 17:10:01 +00005671 "100/public/api/foo.txt": nil,
5672 "100/public/api/foo-removed.txt": nil,
5673 "100/system/api/foo.txt": nil,
5674 "100/system/api/foo-removed.txt": nil,
5675
Paul Duffineedc5d52020-06-12 17:46:39 +01005676 // For java_sdk_library_import
5677 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005678}
5679
Jooyung Han58f26ab2019-12-18 15:34:32 +09005680func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005681 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09005682 apex {
5683 name: "myapex",
5684 key: "myapex.key",
5685 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005686 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09005687 }
5688
5689 apex_key {
5690 name: "myapex.key",
5691 public_key: "testkey.avbpubkey",
5692 private_key: "testkey.pem",
5693 }
5694
5695 java_sdk_library {
5696 name: "foo",
5697 srcs: ["a.java"],
5698 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005699 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005700 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005701
5702 prebuilt_apis {
5703 name: "sdk",
5704 api_dirs: ["100"],
5705 }
Paul Duffin9b879592020-05-26 13:21:35 +01005706 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005707
5708 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005709 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005710 "javalib/foo.jar",
5711 "etc/permissions/foo.xml",
5712 })
5713 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005714 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5715 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005716}
5717
Paul Duffin9b879592020-05-26 13:21:35 +01005718func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005719 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005720 apex {
5721 name: "myapex",
5722 key: "myapex.key",
5723 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005724 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005725 }
5726
5727 apex_key {
5728 name: "myapex.key",
5729 public_key: "testkey.avbpubkey",
5730 private_key: "testkey.pem",
5731 }
5732
5733 java_sdk_library {
5734 name: "foo",
5735 srcs: ["a.java"],
5736 api_packages: ["foo"],
5737 apex_available: ["myapex"],
5738 sdk_version: "none",
5739 system_modules: "none",
5740 }
5741
5742 java_library {
5743 name: "bar",
5744 srcs: ["a.java"],
5745 libs: ["foo"],
5746 apex_available: ["myapex"],
5747 sdk_version: "none",
5748 system_modules: "none",
5749 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005750
5751 prebuilt_apis {
5752 name: "sdk",
5753 api_dirs: ["100"],
5754 }
Paul Duffin9b879592020-05-26 13:21:35 +01005755 `, withFiles(filesForSdkLibrary))
5756
5757 // java_sdk_library installs both impl jar and permission XML
5758 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5759 "javalib/bar.jar",
5760 "javalib/foo.jar",
5761 "etc/permissions/foo.xml",
5762 })
5763
5764 // The bar library should depend on the implementation jar.
5765 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005766 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005767 t.Errorf("expected %q, found %#q", expected, actual)
5768 }
5769}
5770
5771func TestJavaSDKLibrary_CrossBoundary(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"],
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 sdk_version: "none",
5800 system_modules: "none",
5801 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005802
5803 prebuilt_apis {
5804 name: "sdk",
5805 api_dirs: ["100"],
5806 }
Paul Duffin9b879592020-05-26 13:21:35 +01005807 `, withFiles(filesForSdkLibrary))
5808
5809 // java_sdk_library installs both impl jar and permission XML
5810 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5811 "javalib/foo.jar",
5812 "etc/permissions/foo.xml",
5813 })
5814
5815 // The bar library should depend on the stubs jar.
5816 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005817 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005818 t.Errorf("expected %q, found %#q", expected, actual)
5819 }
5820}
5821
Paul Duffineedc5d52020-06-12 17:46:39 +01005822func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005823 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00005824 prebuilt_apis {
5825 name: "sdk",
5826 api_dirs: ["100"],
5827 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005828 withFiles(map[string][]byte{
5829 "apex/a.java": nil,
5830 "apex/apex_manifest.json": nil,
5831 "apex/Android.bp": []byte(`
5832 package {
5833 default_visibility: ["//visibility:private"],
5834 }
5835
5836 apex {
5837 name: "myapex",
5838 key: "myapex.key",
5839 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005840 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005841 }
5842
5843 apex_key {
5844 name: "myapex.key",
5845 public_key: "testkey.avbpubkey",
5846 private_key: "testkey.pem",
5847 }
5848
5849 java_library {
5850 name: "bar",
5851 srcs: ["a.java"],
5852 libs: ["foo"],
5853 apex_available: ["myapex"],
5854 sdk_version: "none",
5855 system_modules: "none",
5856 }
5857`),
5858 "source/a.java": nil,
5859 "source/api/current.txt": nil,
5860 "source/api/removed.txt": nil,
5861 "source/Android.bp": []byte(`
5862 package {
5863 default_visibility: ["//visibility:private"],
5864 }
5865
5866 java_sdk_library {
5867 name: "foo",
5868 visibility: ["//apex"],
5869 srcs: ["a.java"],
5870 api_packages: ["foo"],
5871 apex_available: ["myapex"],
5872 sdk_version: "none",
5873 system_modules: "none",
5874 public: {
5875 enabled: true,
5876 },
5877 }
5878`),
5879 "prebuilt/a.jar": nil,
5880 "prebuilt/Android.bp": []byte(`
5881 package {
5882 default_visibility: ["//visibility:private"],
5883 }
5884
5885 java_sdk_library_import {
5886 name: "foo",
5887 visibility: ["//apex", "//source"],
5888 apex_available: ["myapex"],
5889 prefer: true,
5890 public: {
5891 jars: ["a.jar"],
5892 },
5893 }
5894`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005895 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005896 )
5897
5898 // java_sdk_library installs both impl jar and permission XML
5899 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5900 "javalib/bar.jar",
5901 "javalib/foo.jar",
5902 "etc/permissions/foo.xml",
5903 })
5904
5905 // The bar library should depend on the implementation jar.
5906 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005907 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01005908 t.Errorf("expected %q, found %#q", expected, actual)
5909 }
5910}
5911
5912func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5913 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5914 apex {
5915 name: "myapex",
5916 key: "myapex.key",
5917 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005918 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005919 }
5920
5921 apex_key {
5922 name: "myapex.key",
5923 public_key: "testkey.avbpubkey",
5924 private_key: "testkey.pem",
5925 }
5926
5927 java_sdk_library_import {
5928 name: "foo",
5929 apex_available: ["myapex"],
5930 prefer: true,
5931 public: {
5932 jars: ["a.jar"],
5933 },
5934 }
5935
5936 `, withFiles(filesForSdkLibrary))
5937}
5938
atrost6e126252020-01-27 17:01:16 +00005939func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01005940 result := android.GroupFixturePreparers(
5941 prepareForApexTest,
5942 java.PrepareForTestWithPlatformCompatConfig,
5943 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00005944 apex {
5945 name: "myapex",
5946 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00005947 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00005948 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005949 updatable: false,
atrost6e126252020-01-27 17:01:16 +00005950 }
5951
5952 apex_key {
5953 name: "myapex.key",
5954 public_key: "testkey.avbpubkey",
5955 private_key: "testkey.pem",
5956 }
5957
5958 platform_compat_config {
5959 name: "myjar-platform-compat-config",
5960 src: ":myjar",
5961 }
5962
5963 java_library {
5964 name: "myjar",
5965 srcs: ["foo/bar/MyClass.java"],
5966 sdk_version: "none",
5967 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005968 apex_available: [ "myapex" ],
5969 }
Paul Duffin1b29e002021-03-16 15:06:54 +00005970
5971 // Make sure that a preferred prebuilt does not affect the apex contents.
5972 prebuilt_platform_compat_config {
5973 name: "myjar-platform-compat-config",
5974 metadata: "compat-config/metadata.xml",
5975 prefer: true,
5976 }
atrost6e126252020-01-27 17:01:16 +00005977 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00005978 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00005979 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5980 "etc/compatconfig/myjar-platform-compat-config.xml",
5981 "javalib/myjar.jar",
5982 })
5983}
5984
Jiyong Park479321d2019-12-16 11:47:12 +09005985func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5986 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5987 apex {
5988 name: "myapex",
5989 key: "myapex.key",
5990 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005991 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09005992 }
5993
5994 apex_key {
5995 name: "myapex.key",
5996 public_key: "testkey.avbpubkey",
5997 private_key: "testkey.pem",
5998 }
5999
6000 java_library {
6001 name: "myjar",
6002 srcs: ["foo/bar/MyClass.java"],
6003 sdk_version: "none",
6004 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09006005 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006006 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09006007 }
6008 `)
6009}
6010
Jiyong Park7afd1072019-12-30 16:56:33 +09006011func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006012 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09006013 apex {
6014 name: "myapex",
6015 key: "myapex.key",
6016 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006017 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09006018 }
6019
6020 apex_key {
6021 name: "myapex.key",
6022 public_key: "testkey.avbpubkey",
6023 private_key: "testkey.pem",
6024 }
6025
6026 cc_library {
6027 name: "mylib",
6028 srcs: ["mylib.cpp"],
6029 system_shared_libs: [],
6030 stl: "none",
6031 required: ["a", "b"],
6032 host_required: ["c", "d"],
6033 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006034 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006035 }
6036 `)
6037
6038 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006039 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006040 name := apexBundle.BaseModuleName()
6041 prefix := "TARGET_"
6042 var builder strings.Builder
6043 data.Custom(&builder, name, prefix, "", data)
6044 androidMk := builder.String()
6045 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6046 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6047 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6048}
6049
Jiyong Park7cd10e32020-01-14 09:22:18 +09006050func TestSymlinksFromApexToSystem(t *testing.T) {
6051 bp := `
6052 apex {
6053 name: "myapex",
6054 key: "myapex.key",
6055 native_shared_libs: ["mylib"],
6056 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006057 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006058 }
6059
Jiyong Park9d677202020-02-19 16:29:35 +09006060 apex {
6061 name: "myapex.updatable",
6062 key: "myapex.key",
6063 native_shared_libs: ["mylib"],
6064 java_libs: ["myjar"],
6065 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006066 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006067 }
6068
Jiyong Park7cd10e32020-01-14 09:22:18 +09006069 apex_key {
6070 name: "myapex.key",
6071 public_key: "testkey.avbpubkey",
6072 private_key: "testkey.pem",
6073 }
6074
6075 cc_library {
6076 name: "mylib",
6077 srcs: ["mylib.cpp"],
6078 shared_libs: ["myotherlib"],
6079 system_shared_libs: [],
6080 stl: "none",
6081 apex_available: [
6082 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006083 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006084 "//apex_available:platform",
6085 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006086 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006087 }
6088
6089 cc_library {
6090 name: "myotherlib",
6091 srcs: ["mylib.cpp"],
6092 system_shared_libs: [],
6093 stl: "none",
6094 apex_available: [
6095 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006096 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006097 "//apex_available:platform",
6098 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006099 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006100 }
6101
6102 java_library {
6103 name: "myjar",
6104 srcs: ["foo/bar/MyClass.java"],
6105 sdk_version: "none",
6106 system_modules: "none",
6107 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006108 apex_available: [
6109 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006110 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006111 "//apex_available:platform",
6112 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006113 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006114 }
6115
6116 java_library {
6117 name: "myotherjar",
6118 srcs: ["foo/bar/MyClass.java"],
6119 sdk_version: "none",
6120 system_modules: "none",
6121 apex_available: [
6122 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006123 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006124 "//apex_available:platform",
6125 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006126 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006127 }
6128 `
6129
6130 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6131 for _, f := range files {
6132 if f.path == file {
6133 if f.isLink {
6134 t.Errorf("%q is not a real file", file)
6135 }
6136 return
6137 }
6138 }
6139 t.Errorf("%q is not found", file)
6140 }
6141
6142 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6143 for _, f := range files {
6144 if f.path == file {
6145 if !f.isLink {
6146 t.Errorf("%q is not a symlink", file)
6147 }
6148 return
6149 }
6150 }
6151 t.Errorf("%q is not found", file)
6152 }
6153
Jiyong Park9d677202020-02-19 16:29:35 +09006154 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6155 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006156 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006157 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006158 ensureRealfileExists(t, files, "javalib/myjar.jar")
6159 ensureRealfileExists(t, files, "lib64/mylib.so")
6160 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6161
Jiyong Park9d677202020-02-19 16:29:35 +09006162 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6163 ensureRealfileExists(t, files, "javalib/myjar.jar")
6164 ensureRealfileExists(t, files, "lib64/mylib.so")
6165 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6166
6167 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006168 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006169 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006170 ensureRealfileExists(t, files, "javalib/myjar.jar")
6171 ensureRealfileExists(t, files, "lib64/mylib.so")
6172 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006173
6174 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6175 ensureRealfileExists(t, files, "javalib/myjar.jar")
6176 ensureRealfileExists(t, files, "lib64/mylib.so")
6177 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006178}
6179
Yo Chiange8128052020-07-23 20:09:18 +08006180func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006181 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006182 apex {
6183 name: "myapex",
6184 key: "myapex.key",
6185 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006186 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006187 }
6188
6189 apex_key {
6190 name: "myapex.key",
6191 public_key: "testkey.avbpubkey",
6192 private_key: "testkey.pem",
6193 }
6194
6195 cc_library_shared {
6196 name: "mylib",
6197 srcs: ["mylib.cpp"],
6198 shared_libs: ["myotherlib"],
6199 system_shared_libs: [],
6200 stl: "none",
6201 apex_available: [
6202 "myapex",
6203 "//apex_available:platform",
6204 ],
6205 }
6206
6207 cc_prebuilt_library_shared {
6208 name: "myotherlib",
6209 srcs: ["prebuilt.so"],
6210 system_shared_libs: [],
6211 stl: "none",
6212 apex_available: [
6213 "myapex",
6214 "//apex_available:platform",
6215 ],
6216 }
6217 `)
6218
6219 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006220 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006221 var builder strings.Builder
6222 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6223 androidMk := builder.String()
6224 // `myotherlib` is added to `myapex` as symlink
6225 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6226 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6227 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6228 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006229 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 +08006230}
6231
Jooyung Han643adc42020-02-27 13:50:06 +09006232func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006233 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006234 apex {
6235 name: "myapex",
6236 key: "myapex.key",
6237 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006238 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006239 }
6240
6241 apex_key {
6242 name: "myapex.key",
6243 public_key: "testkey.avbpubkey",
6244 private_key: "testkey.pem",
6245 }
6246
6247 cc_library {
6248 name: "mylib",
6249 srcs: ["mylib.cpp"],
6250 shared_libs: ["mylib2"],
6251 system_shared_libs: [],
6252 stl: "none",
6253 apex_available: [ "myapex" ],
6254 }
6255
6256 cc_library {
6257 name: "mylib2",
6258 srcs: ["mylib.cpp"],
6259 system_shared_libs: [],
6260 stl: "none",
6261 apex_available: [ "myapex" ],
6262 }
6263 `)
6264
6265 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6266 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6267 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6268 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6269 "lib64/mylib.so",
6270 "lib64/mylib2.so",
6271 })
6272}
6273
Jooyung Han49f67012020-04-17 13:43:10 +09006274func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006275 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006276 apex {
6277 name: "myapex",
6278 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006279 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006280 }
6281 apex_key {
6282 name: "myapex.key",
6283 public_key: "testkey.avbpubkey",
6284 private_key: "testkey.pem",
6285 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006286 `,
6287 android.FixtureModifyConfig(func(config android.Config) {
6288 delete(config.Targets, android.Android)
6289 config.AndroidCommonTarget = android.Target{}
6290 }),
6291 )
Jooyung Han49f67012020-04-17 13:43:10 +09006292
6293 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6294 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6295 }
6296}
6297
Jiyong Parkbd159612020-02-28 15:22:21 +09006298func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006299 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006300 apex {
6301 name: "myapex",
6302 key: "myapex.key",
6303 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006304 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006305 }
6306
6307 apex_key {
6308 name: "myapex.key",
6309 public_key: "testkey.avbpubkey",
6310 private_key: "testkey.pem",
6311 }
6312
6313 android_app {
6314 name: "AppFoo",
6315 srcs: ["foo/bar/MyClass.java"],
6316 sdk_version: "none",
6317 system_modules: "none",
6318 apex_available: [ "myapex" ],
6319 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006320 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006321
Colin Crosscf371cc2020-11-13 11:48:42 -08006322 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006323 content := bundleConfigRule.Args["content"]
6324
6325 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006326 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 +09006327}
6328
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006329func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006330 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006331 apex {
6332 name: "myapex",
6333 key: "myapex.key",
6334 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006335 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006336 }
6337
6338 apex_key {
6339 name: "myapex.key",
6340 public_key: "testkey.avbpubkey",
6341 private_key: "testkey.pem",
6342 }
6343
6344 android_app_set {
6345 name: "AppSet",
6346 set: "AppSet.apks",
6347 }`)
6348 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006349 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006350 content := bundleConfigRule.Args["content"]
6351 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6352 s := mod.Rule("apexRule").Args["copy_commands"]
6353 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6354 if len(copyCmds) != 3 {
6355 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6356 }
6357 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6358 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6359 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6360}
6361
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006362func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006363 ctx := testApex(t, "", android.FixtureModifyMockFS(func(fs android.MockFS) {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006364 bp := `
6365 apex_set {
6366 name: "myapex",
6367 filename: "foo_v2.apex",
6368 sanitized: {
6369 none: { set: "myapex.apks", },
6370 hwaddress: { set: "myapex.hwasan.apks", },
6371 },
6372 }`
6373 fs["Android.bp"] = []byte(bp)
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006374 }),
6375 prepareForTestWithSantitizeHwaddress,
6376 )
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006377
6378 m := ctx.ModuleForTests("myapex", "android_common")
Paul Duffin37ba3442021-03-29 00:21:08 +01006379 extractedApex := m.Output("out/soong/.intermediates/myapex/android_common/foo_v2.apex")
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006380
6381 actual := extractedApex.Inputs
6382 if len(actual) != 1 {
6383 t.Errorf("expected a single input")
6384 }
6385
6386 expected := "myapex.hwasan.apks"
6387 if actual[0].String() != expected {
6388 t.Errorf("expected %s, got %s", expected, actual[0].String())
6389 }
6390}
6391
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006392func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006393 t.Helper()
6394
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006395 bp := `
6396 java_library {
6397 name: "some-updatable-apex-lib",
6398 srcs: ["a.java"],
6399 sdk_version: "current",
6400 apex_available: [
6401 "some-updatable-apex",
6402 ],
6403 }
6404
6405 java_library {
6406 name: "some-non-updatable-apex-lib",
6407 srcs: ["a.java"],
6408 apex_available: [
6409 "some-non-updatable-apex",
6410 ],
6411 }
6412
6413 java_library {
6414 name: "some-platform-lib",
6415 srcs: ["a.java"],
6416 sdk_version: "current",
6417 installable: true,
6418 }
6419
6420 java_library {
6421 name: "some-art-lib",
6422 srcs: ["a.java"],
6423 sdk_version: "current",
6424 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006425 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006426 ],
6427 hostdex: true,
6428 }
6429
6430 apex {
6431 name: "some-updatable-apex",
6432 key: "some-updatable-apex.key",
6433 java_libs: ["some-updatable-apex-lib"],
6434 updatable: true,
6435 min_sdk_version: "current",
6436 }
6437
6438 apex {
6439 name: "some-non-updatable-apex",
6440 key: "some-non-updatable-apex.key",
6441 java_libs: ["some-non-updatable-apex-lib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006442 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006443 }
6444
6445 apex_key {
6446 name: "some-updatable-apex.key",
6447 }
6448
6449 apex_key {
6450 name: "some-non-updatable-apex.key",
6451 }
6452
6453 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006454 name: "com.android.art.debug",
6455 key: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006456 java_libs: ["some-art-lib"],
6457 updatable: true,
6458 min_sdk_version: "current",
6459 }
6460
6461 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006462 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006463 }
6464
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006465 filegroup {
6466 name: "some-updatable-apex-file_contexts",
6467 srcs: [
6468 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6469 ],
6470 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006471
6472 filegroup {
6473 name: "some-non-updatable-apex-file_contexts",
6474 srcs: [
6475 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6476 ],
6477 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006478 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006479
6480 testDexpreoptWithApexes(t, bp, errmsg, transformDexpreoptConfig)
6481}
6482
Paul Duffin064b70c2020-11-02 17:32:38 +00006483func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006484 t.Helper()
6485
Paul Duffin55607122021-03-30 23:32:51 +01006486 fs := android.MockFS{
6487 "a.java": nil,
6488 "a.jar": nil,
6489 "apex_manifest.json": nil,
6490 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006491 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006492 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6493 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6494 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006495 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006496 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006497
Paul Duffin55607122021-03-30 23:32:51 +01006498 errorHandler := android.FixtureExpectsNoErrors
6499 if errmsg != "" {
6500 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006501 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006502
Paul Duffin55607122021-03-30 23:32:51 +01006503 result := android.GroupFixturePreparers(
6504 cc.PrepareForTestWithCcDefaultModules,
6505 java.PrepareForTestWithHiddenApiBuildComponents,
6506 java.PrepareForTestWithJavaDefaultModules,
6507 java.PrepareForTestWithJavaSdkLibraryFiles,
6508 PrepareForTestWithApexBuildComponents,
6509 android.FixtureModifyConfig(func(config android.Config) {
6510 pathCtx := android.PathContextForTesting(config)
6511 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
6512 transformDexpreoptConfig(dexpreoptConfig)
6513 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
6514
6515 // Make sure that any changes to these dexpreopt properties are mirrored in the corresponding
6516 // product variables.
6517 config.TestProductVariables.BootJars = dexpreoptConfig.BootJars
6518 config.TestProductVariables.UpdatableBootJars = dexpreoptConfig.UpdatableBootJars
6519 }),
6520 fs.AddToFixture(),
6521 ).
6522 ExtendWithErrorHandler(errorHandler).
6523 RunTestWithBp(t, bp)
6524
6525 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006526}
6527
Jooyung Han548640b2020-04-27 12:10:30 +09006528func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6529 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6530 apex {
6531 name: "myapex",
6532 key: "myapex.key",
6533 updatable: true,
6534 }
6535
6536 apex_key {
6537 name: "myapex.key",
6538 public_key: "testkey.avbpubkey",
6539 private_key: "testkey.pem",
6540 }
6541 `)
6542}
6543
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006544func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6545 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6546 apex {
6547 name: "myapex",
6548 key: "myapex.key",
6549 }
6550
6551 apex_key {
6552 name: "myapex.key",
6553 public_key: "testkey.avbpubkey",
6554 private_key: "testkey.pem",
6555 }
6556 `)
6557}
6558
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006559func TestNoUpdatableJarsInBootImage(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006560 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006561 var transform func(*dexpreopt.GlobalConfig)
6562
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006563 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
6564 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006565 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006566 }
6567 testNoUpdatableJarsInBootImage(t, "", transform)
6568 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006569
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006570 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffind376f792021-01-26 11:59:35 +00006571 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 +01006572 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006573 config.BootJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006574 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006575 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006576 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006577
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006578 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 -07006579 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 +01006580 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006581 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006582 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006583 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006584 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006585
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006586 t.Run("non-updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006587 err = `module "some-non-updatable-apex-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006588 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006589 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006590 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006591 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006592 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006593
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006594 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 -07006595 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 +01006596 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006597 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006598 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006599 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006600 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006601
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006602 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
6603 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006604 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006605 }
6606 testNoUpdatableJarsInBootImage(t, "", transform)
6607 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006608
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006609 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006610 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006611 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006612 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006613 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006614 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006615 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006616
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006617 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006618 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006619 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006620 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006621 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006622 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006623 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006624
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006625 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006626 err = `module "some-platform-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006627 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006628 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006629 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006630 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006631 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006632
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006633 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
6634 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006635 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006636 }
6637 testNoUpdatableJarsInBootImage(t, "", transform)
6638 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006639
6640}
6641
6642func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
6643 transform := func(config *dexpreopt.GlobalConfig) {
6644 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
6645 }
6646 t.Run("prebuilt no source", func(t *testing.T) {
6647 testDexpreoptWithApexes(t, `
6648 prebuilt_apex {
6649 name: "myapex" ,
6650 arch: {
6651 arm64: {
6652 src: "myapex-arm64.apex",
6653 },
6654 arm: {
6655 src: "myapex-arm.apex",
6656 },
6657 },
6658 exported_java_libs: ["libfoo"],
6659 }
6660
6661 java_import {
6662 name: "libfoo",
6663 jars: ["libfoo.jar"],
6664 }
6665`, "", transform)
6666 })
6667
6668 t.Run("prebuilt no source", func(t *testing.T) {
6669 testDexpreoptWithApexes(t, `
6670 prebuilt_apex {
6671 name: "myapex" ,
6672 arch: {
6673 arm64: {
6674 src: "myapex-arm64.apex",
6675 },
6676 arm: {
6677 src: "myapex-arm.apex",
6678 },
6679 },
6680 exported_java_libs: ["libfoo"],
6681 }
6682
6683 java_import {
6684 name: "libfoo",
6685 jars: ["libfoo.jar"],
6686 }
6687`, "", transform)
6688 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006689}
6690
Andrei Onea115e7e72020-06-05 21:14:03 +01006691func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6692 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006693 bp += `
6694 apex_key {
6695 name: "myapex.key",
6696 public_key: "testkey.avbpubkey",
6697 private_key: "testkey.pem",
6698 }`
Paul Duffin45338f02021-03-30 23:07:52 +01006699 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01006700 "lib1/src/A.java": nil,
6701 "lib2/src/B.java": nil,
6702 "system/sepolicy/apex/myapex-file_contexts": nil,
6703 }
6704
Paul Duffin45338f02021-03-30 23:07:52 +01006705 errorHandler := android.FixtureExpectsNoErrors
6706 if errmsg != "" {
6707 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07006708 }
Colin Crossae8600b2020-10-29 17:09:13 -07006709
Paul Duffin45338f02021-03-30 23:07:52 +01006710 android.GroupFixturePreparers(
6711 android.PrepareForTestWithAndroidBuildComponents,
6712 java.PrepareForTestWithJavaBuildComponents,
6713 PrepareForTestWithApexBuildComponents,
6714 android.PrepareForTestWithNeverallowRules(rules),
6715 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6716 updatableBootJars := make([]string, 0, len(apexBootJars))
6717 for _, apexBootJar := range apexBootJars {
6718 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6719 }
6720 variables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6721 }),
6722 fs.AddToFixture(),
6723 ).
6724 ExtendWithErrorHandler(errorHandler).
6725 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01006726}
6727
6728func TestApexPermittedPackagesRules(t *testing.T) {
6729 testcases := []struct {
6730 name string
6731 expectedError string
6732 bp string
6733 bootJars []string
6734 modulesPackages map[string][]string
6735 }{
6736
6737 {
6738 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6739 expectedError: "",
6740 bp: `
6741 java_library {
6742 name: "bcp_lib1",
6743 srcs: ["lib1/src/*.java"],
6744 permitted_packages: ["foo.bar"],
6745 apex_available: ["myapex"],
6746 sdk_version: "none",
6747 system_modules: "none",
6748 }
6749 java_library {
6750 name: "nonbcp_lib2",
6751 srcs: ["lib2/src/*.java"],
6752 apex_available: ["myapex"],
6753 permitted_packages: ["a.b"],
6754 sdk_version: "none",
6755 system_modules: "none",
6756 }
6757 apex {
6758 name: "myapex",
6759 key: "myapex.key",
6760 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006761 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006762 }`,
6763 bootJars: []string{"bcp_lib1"},
6764 modulesPackages: map[string][]string{
6765 "myapex": []string{
6766 "foo.bar",
6767 },
6768 },
6769 },
6770 {
6771 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6772 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.`,
6773 bp: `
6774 java_library {
6775 name: "bcp_lib1",
6776 srcs: ["lib1/src/*.java"],
6777 apex_available: ["myapex"],
6778 permitted_packages: ["foo.bar"],
6779 sdk_version: "none",
6780 system_modules: "none",
6781 }
6782 java_library {
6783 name: "bcp_lib2",
6784 srcs: ["lib2/src/*.java"],
6785 apex_available: ["myapex"],
6786 permitted_packages: ["foo.bar", "bar.baz"],
6787 sdk_version: "none",
6788 system_modules: "none",
6789 }
6790 apex {
6791 name: "myapex",
6792 key: "myapex.key",
6793 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006794 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006795 }
6796 `,
6797 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6798 modulesPackages: map[string][]string{
6799 "myapex": []string{
6800 "foo.bar",
6801 },
6802 },
6803 },
6804 }
6805 for _, tc := range testcases {
6806 t.Run(tc.name, func(t *testing.T) {
6807 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6808 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6809 })
6810 }
6811}
6812
Jiyong Park62304bb2020-04-13 16:19:48 +09006813func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006814 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09006815 apex {
6816 name: "myapex",
6817 key: "myapex.key",
6818 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006819 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09006820 }
6821
6822 apex_key {
6823 name: "myapex.key",
6824 public_key: "testkey.avbpubkey",
6825 private_key: "testkey.pem",
6826 }
6827
6828 cc_library {
6829 name: "mylib",
6830 srcs: ["mylib.cpp"],
6831 system_shared_libs: [],
6832 stl: "none",
6833 stubs: {
6834 versions: ["1"],
6835 },
6836 apex_available: ["myapex"],
6837 }
6838
6839 cc_library {
6840 name: "myprivlib",
6841 srcs: ["mylib.cpp"],
6842 system_shared_libs: [],
6843 stl: "none",
6844 apex_available: ["myapex"],
6845 }
6846
6847
6848 cc_test {
6849 name: "mytest",
6850 gtest: false,
6851 srcs: ["mylib.cpp"],
6852 system_shared_libs: [],
6853 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006854 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006855 test_for: ["myapex"]
6856 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006857
6858 cc_library {
6859 name: "mytestlib",
6860 srcs: ["mylib.cpp"],
6861 system_shared_libs: [],
6862 shared_libs: ["mylib", "myprivlib"],
6863 stl: "none",
6864 test_for: ["myapex"],
6865 }
6866
6867 cc_benchmark {
6868 name: "mybench",
6869 srcs: ["mylib.cpp"],
6870 system_shared_libs: [],
6871 shared_libs: ["mylib", "myprivlib"],
6872 stl: "none",
6873 test_for: ["myapex"],
6874 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006875 `)
6876
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006877 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01006878 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006879 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6880 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6881 }
6882
6883 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09006884 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006885 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6886 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6887 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6888}
Jiyong Park46a512f2020-12-04 18:02:13 +09006889
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006890func TestIndirectTestFor(t *testing.T) {
6891 ctx := testApex(t, `
6892 apex {
6893 name: "myapex",
6894 key: "myapex.key",
6895 native_shared_libs: ["mylib", "myprivlib"],
6896 updatable: false,
6897 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006898
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006899 apex_key {
6900 name: "myapex.key",
6901 public_key: "testkey.avbpubkey",
6902 private_key: "testkey.pem",
6903 }
6904
6905 cc_library {
6906 name: "mylib",
6907 srcs: ["mylib.cpp"],
6908 system_shared_libs: [],
6909 stl: "none",
6910 stubs: {
6911 versions: ["1"],
6912 },
6913 apex_available: ["myapex"],
6914 }
6915
6916 cc_library {
6917 name: "myprivlib",
6918 srcs: ["mylib.cpp"],
6919 system_shared_libs: [],
6920 stl: "none",
6921 shared_libs: ["mylib"],
6922 apex_available: ["myapex"],
6923 }
6924
6925 cc_library {
6926 name: "mytestlib",
6927 srcs: ["mylib.cpp"],
6928 system_shared_libs: [],
6929 shared_libs: ["myprivlib"],
6930 stl: "none",
6931 test_for: ["myapex"],
6932 }
6933 `)
6934
6935 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01006936 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006937 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6938 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6939 }
6940
6941 // The platform variant of mytestlib links to the platform variant of the
6942 // internal myprivlib.
6943 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
6944
6945 // The platform variant of myprivlib links to the platform variant of mylib
6946 // and bypasses its stubs.
6947 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 +09006948}
6949
Martin Stjernholmec009002021-03-27 15:18:31 +00006950func TestTestForForLibInOtherApex(t *testing.T) {
6951 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
6952 _ = testApex(t, `
6953 apex {
6954 name: "com.android.art",
6955 key: "myapex.key",
6956 native_shared_libs: ["mylib"],
6957 updatable: false,
6958 }
6959
6960 apex {
6961 name: "com.android.art.debug",
6962 key: "myapex.key",
6963 native_shared_libs: ["mylib", "mytestlib"],
6964 updatable: false,
6965 }
6966
6967 apex_key {
6968 name: "myapex.key",
6969 public_key: "testkey.avbpubkey",
6970 private_key: "testkey.pem",
6971 }
6972
6973 cc_library {
6974 name: "mylib",
6975 srcs: ["mylib.cpp"],
6976 system_shared_libs: [],
6977 stl: "none",
6978 stubs: {
6979 versions: ["1"],
6980 },
6981 apex_available: ["com.android.art", "com.android.art.debug"],
6982 }
6983
6984 cc_library {
6985 name: "mytestlib",
6986 srcs: ["mylib.cpp"],
6987 system_shared_libs: [],
6988 shared_libs: ["mylib"],
6989 stl: "none",
6990 apex_available: ["com.android.art.debug"],
6991 test_for: ["com.android.art"],
6992 }
6993 `,
6994 android.MockFS{
6995 "system/sepolicy/apex/com.android.art-file_contexts": nil,
6996 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
6997 }.AddToFixture())
6998}
6999
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007000// TODO(jungjw): Move this to proptools
7001func intPtr(i int) *int {
7002 return &i
7003}
7004
7005func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007006 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007007 apex_set {
7008 name: "myapex",
7009 set: "myapex.apks",
7010 filename: "foo_v2.apex",
7011 overrides: ["foo"],
7012 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007013 `,
7014 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7015 variables.Platform_sdk_version = intPtr(30)
7016 }),
7017 android.FixtureModifyConfig(func(config android.Config) {
7018 config.Targets[android.Android] = []android.Target{
7019 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
7020 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
7021 }
7022 }),
7023 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007024
7025 m := ctx.ModuleForTests("myapex", "android_common")
7026
7027 // Check extract_apks tool parameters.
Paul Duffin37ba3442021-03-29 00:21:08 +01007028 extractedApex := m.Output("out/soong/.intermediates/myapex/android_common/foo_v2.apex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007029 actual := extractedApex.Args["abis"]
7030 expected := "ARMEABI_V7A,ARM64_V8A"
7031 if actual != expected {
7032 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7033 }
7034 actual = extractedApex.Args["sdk-version"]
7035 expected = "30"
7036 if actual != expected {
7037 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7038 }
7039
7040 a := m.Module().(*ApexSet)
7041 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07007042 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007043 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
7044 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
7045 }
7046}
7047
Jiyong Park7d95a512020-05-10 15:16:24 +09007048func TestNoStaticLinkingToStubsLib(t *testing.T) {
7049 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
7050 apex {
7051 name: "myapex",
7052 key: "myapex.key",
7053 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007054 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09007055 }
7056
7057 apex_key {
7058 name: "myapex.key",
7059 public_key: "testkey.avbpubkey",
7060 private_key: "testkey.pem",
7061 }
7062
7063 cc_library {
7064 name: "mylib",
7065 srcs: ["mylib.cpp"],
7066 static_libs: ["otherlib"],
7067 system_shared_libs: [],
7068 stl: "none",
7069 apex_available: [ "myapex" ],
7070 }
7071
7072 cc_library {
7073 name: "otherlib",
7074 srcs: ["mylib.cpp"],
7075 system_shared_libs: [],
7076 stl: "none",
7077 stubs: {
7078 versions: ["1", "2", "3"],
7079 },
7080 apex_available: [ "myapex" ],
7081 }
7082 `)
7083}
7084
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007085func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007086 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007087 apex {
7088 name: "myapex",
7089 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007090 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007091 }
7092
7093 apex_key {
7094 name: "myapex.key",
7095 public_key: "testkey.avbpubkey",
7096 private_key: "testkey.pem",
7097 }
7098
7099 prebuilt_apex {
7100 name: "myapex",
7101 prefer: true,
7102 arch: {
7103 arm64: {
7104 src: "myapex-arm64.apex",
7105 },
7106 arm: {
7107 src: "myapex-arm.apex",
7108 },
7109 },
7110 }
7111
7112 apex_set {
7113 name: "myapex_set",
7114 set: "myapex.apks",
7115 filename: "myapex_set.apex",
7116 overrides: ["myapex"],
7117 }
7118 `)
7119
7120 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7121 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7122 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 +09007123 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 +09007124}
7125
Jooyung Han938b5932020-06-20 12:47:47 +09007126func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007127 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007128 apex {
7129 name: "myapex",
7130 key: "myapex.key",
7131 apps: ["app"],
7132 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007133 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007134 }
7135
7136 apex_key {
7137 name: "myapex.key",
7138 public_key: "testkey.avbpubkey",
7139 private_key: "testkey.pem",
7140 }
7141
7142 android_app {
7143 name: "app",
7144 srcs: ["foo/bar/MyClass.java"],
7145 package_name: "foo",
7146 sdk_version: "none",
7147 system_modules: "none",
7148 apex_available: [ "myapex" ],
7149 }
7150 `, withFiles(map[string][]byte{
7151 "sub/Android.bp": []byte(`
7152 override_apex {
7153 name: "override_myapex",
7154 base: "myapex",
7155 apps: ["override_app"],
7156 allowed_files: ":allowed",
7157 }
7158 // Overridable "path" property should be referenced indirectly
7159 filegroup {
7160 name: "allowed",
7161 srcs: ["allowed.txt"],
7162 }
7163 override_android_app {
7164 name: "override_app",
7165 base: "app",
7166 package_name: "bar",
7167 }
7168 `),
7169 }))
7170
7171 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7172 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7173 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7174 }
7175
7176 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7177 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7178 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7179 }
7180}
7181
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007182func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007183 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007184 apex {
7185 name: "myapex",
7186 key: "myapex.key",
7187 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007188 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007189 }
7190
7191 apex_key {
7192 name: "myapex.key",
7193 public_key: "testkey.avbpubkey",
7194 private_key: "testkey.pem",
7195 }
7196
7197 cc_library {
7198 name: "mylib",
7199 srcs: ["mylib.cpp"],
7200 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007201 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007202 },
7203 apex_available: ["myapex"],
7204 }
7205
7206 cc_prebuilt_library_shared {
7207 name: "mylib",
7208 prefer: false,
7209 srcs: ["prebuilt.so"],
7210 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007211 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007212 },
7213 apex_available: ["myapex"],
7214 }
7215 `)
7216}
7217
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007218func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007219 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007220 apex {
7221 name: "myapex",
7222 key: "myapex.key",
7223 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007224 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007225 }
7226 apex_key {
7227 name: "myapex.key",
7228 public_key: "testkey.avbpubkey",
7229 private_key: "testkey.pem",
7230 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007231 `,
7232 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7233 variables.CompressedApex = proptools.BoolPtr(true)
7234 }),
7235 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007236
7237 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7238 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7239
7240 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7241 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7242
7243 // Make sure output of bundle is .capex
7244 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7245 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7246
7247 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007248 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007249 var builder strings.Builder
7250 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7251 androidMk := builder.String()
7252 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7253}
7254
Martin Stjernholm2856c662020-12-02 15:03:42 +00007255func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007256 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007257 apex {
7258 name: "myapex",
7259 key: "myapex.key",
7260 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007261 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007262 }
7263
7264 apex_key {
7265 name: "myapex.key",
7266 public_key: "testkey.avbpubkey",
7267 private_key: "testkey.pem",
7268 }
7269
7270 cc_library {
7271 name: "mylib",
7272 srcs: ["mylib.cpp"],
7273 apex_available: ["myapex"],
7274 shared_libs: ["otherlib"],
7275 system_shared_libs: [],
7276 }
7277
7278 cc_library {
7279 name: "otherlib",
7280 srcs: ["mylib.cpp"],
7281 stubs: {
7282 versions: ["current"],
7283 },
7284 }
7285
7286 cc_prebuilt_library_shared {
7287 name: "otherlib",
7288 prefer: true,
7289 srcs: ["prebuilt.so"],
7290 stubs: {
7291 versions: ["current"],
7292 },
7293 }
7294 `)
7295
7296 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007297 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007298 var builder strings.Builder
7299 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7300 androidMk := builder.String()
7301
7302 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7303 // a thing there.
7304 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7305}
7306
Jiyong Parke3867542020-12-03 17:28:25 +09007307func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007308 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007309 apex {
7310 name: "myapex",
7311 key: "myapex.key",
7312 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007313 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007314 }
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 system_shared_libs: [],
7326 stl: "none",
7327 apex_available: ["myapex"],
7328 shared_libs: ["mylib2"],
7329 target: {
7330 apex: {
7331 exclude_shared_libs: ["mylib2"],
7332 },
7333 },
7334 }
7335
7336 cc_library {
7337 name: "mylib2",
7338 srcs: ["mylib.cpp"],
7339 system_shared_libs: [],
7340 stl: "none",
7341 }
7342 `)
7343
7344 // Check if mylib is linked to mylib2 for the non-apex target
7345 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7346 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7347
7348 // Make sure that the link doesn't occur for the apex target
7349 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7350 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7351
7352 // It shouldn't appear in the copy cmd as well.
7353 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7354 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7355}
7356
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007357func TestPrebuiltStubLibDep(t *testing.T) {
7358 bpBase := `
7359 apex {
7360 name: "myapex",
7361 key: "myapex.key",
7362 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007363 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007364 }
7365 apex_key {
7366 name: "myapex.key",
7367 public_key: "testkey.avbpubkey",
7368 private_key: "testkey.pem",
7369 }
7370 cc_library {
7371 name: "mylib",
7372 srcs: ["mylib.cpp"],
7373 apex_available: ["myapex"],
7374 shared_libs: ["stublib"],
7375 system_shared_libs: [],
7376 }
7377 apex {
7378 name: "otherapex",
7379 enabled: %s,
7380 key: "myapex.key",
7381 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007382 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007383 }
7384 `
7385
7386 stublibSourceBp := `
7387 cc_library {
7388 name: "stublib",
7389 srcs: ["mylib.cpp"],
7390 apex_available: ["otherapex"],
7391 system_shared_libs: [],
7392 stl: "none",
7393 stubs: {
7394 versions: ["1"],
7395 },
7396 }
7397 `
7398
7399 stublibPrebuiltBp := `
7400 cc_prebuilt_library_shared {
7401 name: "stublib",
7402 srcs: ["prebuilt.so"],
7403 apex_available: ["otherapex"],
7404 stubs: {
7405 versions: ["1"],
7406 },
7407 %s
7408 }
7409 `
7410
7411 tests := []struct {
7412 name string
7413 stublibBp string
7414 usePrebuilt bool
7415 modNames []string // Modules to collect AndroidMkEntries for
7416 otherApexEnabled []string
7417 }{
7418 {
7419 name: "only_source",
7420 stublibBp: stublibSourceBp,
7421 usePrebuilt: false,
7422 modNames: []string{"stublib"},
7423 otherApexEnabled: []string{"true", "false"},
7424 },
7425 {
7426 name: "source_preferred",
7427 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7428 usePrebuilt: false,
7429 modNames: []string{"stublib", "prebuilt_stublib"},
7430 otherApexEnabled: []string{"true", "false"},
7431 },
7432 {
7433 name: "prebuilt_preferred",
7434 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7435 usePrebuilt: true,
7436 modNames: []string{"stublib", "prebuilt_stublib"},
7437 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7438 },
7439 {
7440 name: "only_prebuilt",
7441 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7442 usePrebuilt: true,
7443 modNames: []string{"stublib"},
7444 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7445 },
7446 }
7447
7448 for _, test := range tests {
7449 t.Run(test.name, func(t *testing.T) {
7450 for _, otherApexEnabled := range test.otherApexEnabled {
7451 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007452 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007453
7454 type modAndMkEntries struct {
7455 mod *cc.Module
7456 mkEntries android.AndroidMkEntries
7457 }
7458 entries := []*modAndMkEntries{}
7459
7460 // Gather shared lib modules that are installable
7461 for _, modName := range test.modNames {
7462 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7463 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7464 continue
7465 }
7466 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007467 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007468 continue
7469 }
Colin Crossaa255532020-07-03 13:18:24 -07007470 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007471 if ent.Disabled {
7472 continue
7473 }
7474 entries = append(entries, &modAndMkEntries{
7475 mod: mod,
7476 mkEntries: ent,
7477 })
7478 }
7479 }
7480 }
7481
7482 var entry *modAndMkEntries = nil
7483 for _, ent := range entries {
7484 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7485 if entry != nil {
7486 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7487 } else {
7488 entry = ent
7489 }
7490 }
7491 }
7492
7493 if entry == nil {
7494 t.Errorf("AndroidMk entry for \"stublib\" missing")
7495 } else {
7496 isPrebuilt := entry.mod.Prebuilt() != nil
7497 if isPrebuilt != test.usePrebuilt {
7498 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7499 }
7500 if !entry.mod.IsStubs() {
7501 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7502 }
7503 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7504 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7505 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007506 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09007507 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09007508 if !android.InList(expected, cflags) {
7509 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7510 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007511 }
7512 })
7513 }
7514 })
7515 }
7516}
7517
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007518func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01007519 os.Exit(m.Run())
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007520}