blob: 3184c880d7ea71e9d1e1296a02d1b45b6efba5ae [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 "io/ioutil"
20 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090021 "path"
Paul Duffin37856732021-02-26 14:24:15 +000022 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070023 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010024 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090025 "sort"
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
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070041var buildDir string
42
Jooyung Hand3639552019-08-09 12:57:43 +090043// names returns name list from white space separated string
44func names(s string) (ns []string) {
45 for _, n := range strings.Split(s, " ") {
46 if len(n) > 0 {
47 ns = append(ns, n)
48 }
49 }
50 return
51}
52
Paul Duffin2be9dcd2021-03-09 13:18:10 +000053func testApexError(t *testing.T, pattern, bp string, handlers ...interface{}) {
Jooyung Han344d5432019-08-23 11:17:39 +090054 t.Helper()
Paul Duffine05480a2021-03-08 15:07:14 +000055 testApexFixtureFactory(bp, handlers).
56 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
57 RunTest(t)
Jooyung Han5c998b92019-06-27 11:30:33 +090058}
59
Paul Duffin2be9dcd2021-03-09 13:18:10 +000060func testApex(t *testing.T, bp string, handlers ...interface{}) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090061 t.Helper()
Paul Duffine05480a2021-03-08 15:07:14 +000062 result := testApexFixtureFactory(bp, handlers).RunTest(t)
63 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090064}
65
Paul Duffine05480a2021-03-08 15:07:14 +000066// apex package specific mechanism for customizing the test configuration.
67//
68// Use FixturePreparer instances instead.
69//
70// deprecated
Jooyung Han344d5432019-08-23 11:17:39 +090071type testCustomizer func(fs map[string][]byte, config android.Config)
72
Paul Duffin810f33d2021-03-09 14:12:32 +000073func withFiles(files android.MockFS) android.FixturePreparer {
74 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090075}
76
Paul Duffin810f33d2021-03-09 14:12:32 +000077func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
78 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090079 for k, v := range targets {
80 config.Targets[k] = v
81 }
Paul Duffin810f33d2021-03-09 14:12:32 +000082 })
Jooyung Han344d5432019-08-23 11:17:39 +090083}
84
Jooyung Han35155c42020-02-06 17:33:20 +090085// withNativeBridgeTargets sets configuration with targets including:
86// - X86_64 (primary)
87// - X86 (secondary)
88// - Arm64 on X86_64 (native bridge)
89// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000090var withNativeBridgeEnabled = android.FixtureModifyConfig(
91 func(config android.Config) {
92 config.Targets[android.Android] = []android.Target{
93 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
94 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
95 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
96 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
97 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
98 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
99 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
100 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
101 }
102 },
103)
104
105func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
106 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
107 variables.ManifestPackageNameOverrides = specs
108 })
Jooyung Han35155c42020-02-06 17:33:20 +0900109}
110
Paul Duffin810f33d2021-03-09 14:12:32 +0000111var withBinder32bit = android.FixtureModifyProductVariables(
112 func(variables android.FixtureProductVariables) {
113 variables.Binder32bit = proptools.BoolPtr(true)
114 },
115)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900116
Paul Duffin810f33d2021-03-09 14:12:32 +0000117var withUnbundledBuild = android.FixtureModifyProductVariables(
118 func(variables android.FixtureProductVariables) {
119 variables.Unbundled_build = proptools.BoolPtr(true)
120 },
121)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900122
Paul Duffina02cae32021-03-09 01:44:06 +0000123var emptyFixtureFactory = android.NewFixtureFactory(&buildDir)
124
Paul Duffin37aad602021-03-08 09:47:16 +0000125var apexFixtureFactory = android.NewFixtureFactory(
126 &buildDir,
127 // General preparers in alphabetical order as test infrastructure will enforce correct
128 // registration order.
129 android.PrepareForTestWithAndroidBuildComponents,
130 bpf.PrepareForTestWithBpf,
131 cc.PrepareForTestWithCcBuildComponents,
132 java.PrepareForTestWithJavaDefaultModules,
133 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
134 rust.PrepareForTestWithRustDefaultModules,
135 sh.PrepareForTestWithShBuildComponents,
136
137 PrepareForTestWithApexBuildComponents,
138
139 // Additional apex test specific preparers.
140 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
141 filegroup {
142 name: "myapex-file_contexts",
143 srcs: [
144 "apex/myapex-file_contexts",
145 ],
146 }
147 `),
148 android.FixtureMergeMockFs(android.MockFS{
149 "a.java": nil,
150 "PrebuiltAppFoo.apk": nil,
151 "PrebuiltAppFooPriv.apk": nil,
152 "build/make/target/product/security": nil,
153 "apex_manifest.json": nil,
154 "AndroidManifest.xml": nil,
155 "system/sepolicy/apex/myapex-file_contexts": nil,
156 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
157 "system/sepolicy/apex/myapex2-file_contexts": nil,
158 "system/sepolicy/apex/otherapex-file_contexts": nil,
159 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
160 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
161 "mylib.cpp": nil,
162 "mytest.cpp": nil,
163 "mytest1.cpp": nil,
164 "mytest2.cpp": nil,
165 "mytest3.cpp": nil,
166 "myprebuilt": nil,
167 "my_include": nil,
168 "foo/bar/MyClass.java": nil,
169 "prebuilt.jar": nil,
170 "prebuilt.so": nil,
171 "vendor/foo/devkeys/test.x509.pem": nil,
172 "vendor/foo/devkeys/test.pk8": nil,
173 "testkey.x509.pem": nil,
174 "testkey.pk8": nil,
175 "testkey.override.x509.pem": nil,
176 "testkey.override.pk8": nil,
177 "vendor/foo/devkeys/testkey.avbpubkey": nil,
178 "vendor/foo/devkeys/testkey.pem": nil,
179 "NOTICE": nil,
180 "custom_notice": nil,
181 "custom_notice_for_static_lib": nil,
182 "testkey2.avbpubkey": nil,
183 "testkey2.pem": nil,
184 "myapex-arm64.apex": nil,
185 "myapex-arm.apex": nil,
186 "myapex.apks": nil,
187 "frameworks/base/api/current.txt": nil,
188 "framework/aidl/a.aidl": nil,
189 "build/make/core/proguard.flags": nil,
190 "build/make/core/proguard_basic_keeps.flags": nil,
191 "dummy.txt": nil,
192 "baz": nil,
193 "bar/baz": nil,
194 "testdata/baz": nil,
195 "AppSet.apks": nil,
196 "foo.rs": nil,
197 "libfoo.jar": nil,
198 "libbar.jar": nil,
199 },
200 ),
201
202 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
203 variables.DeviceVndkVersion = proptools.StringPtr("current")
204 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
205 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
206 variables.Platform_sdk_codename = proptools.StringPtr("Q")
207 variables.Platform_sdk_final = proptools.BoolPtr(false)
208 variables.Platform_version_active_codenames = []string{"Q"}
209 variables.Platform_vndk_version = proptools.StringPtr("VER")
210 }),
211)
212
Paul Duffin2be9dcd2021-03-09 13:18:10 +0000213func testApexFixtureFactory(bp string, handlers []interface{}) android.FixtureFactory {
214 var preparers []android.FixturePreparer
215 for _, handler := range handlers {
216 var preparer android.FixturePreparer
217 if p, ok := handler.(android.FixturePreparer); ok {
218 preparer = p
219 } else {
220 var customizer testCustomizer
221 if c, ok := handler.(testCustomizer); ok {
222 customizer = c
223 } else {
224 customizer = handler.(func(fs map[string][]byte, config android.Config))
Paul Duffine05480a2021-03-08 15:07:14 +0000225 }
Paul Duffin2be9dcd2021-03-09 13:18:10 +0000226 preparer = android.FixtureCustomPreparer(func(fixture android.Fixture) {
227 customizer(fixture.MockFS(), fixture.Config())
228 })
229 }
230 preparers = append(preparers, preparer)
231 }
232 factory := apexFixtureFactory.Extend(preparers...)
Paul Duffine05480a2021-03-08 15:07:14 +0000233 if bp != "" {
234 factory = factory.Extend(android.FixtureWithRootAndroidBp(bp))
Jooyung Han344d5432019-08-23 11:17:39 +0900235 }
Paul Duffine05480a2021-03-08 15:07:14 +0000236 return factory
Jiyong Park25fc6a92018-11-18 18:02:45 +0900237}
238
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700239func setUp() {
240 var err error
241 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900242 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700243 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900244 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900245}
246
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700247func tearDown() {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700248 _ = os.RemoveAll(buildDir)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900249}
250
Jooyung Han643adc42020-02-27 13:50:06 +0900251// ensure that 'result' equals 'expected'
252func ensureEquals(t *testing.T, result string, expected string) {
253 t.Helper()
254 if result != expected {
255 t.Errorf("%q != %q", expected, result)
256 }
257}
258
Jiyong Park25fc6a92018-11-18 18:02:45 +0900259// ensure that 'result' contains 'expected'
260func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900261 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900262 if !strings.Contains(result, expected) {
263 t.Errorf("%q is not found in %q", expected, result)
264 }
265}
266
Liz Kammer5bd365f2020-05-27 15:15:11 -0700267// ensure that 'result' contains 'expected' exactly one time
268func ensureContainsOnce(t *testing.T, result string, expected string) {
269 t.Helper()
270 count := strings.Count(result, expected)
271 if count != 1 {
272 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
273 }
274}
275
Jiyong Park25fc6a92018-11-18 18:02:45 +0900276// ensures that 'result' does not contain 'notExpected'
277func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900278 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900279 if strings.Contains(result, notExpected) {
280 t.Errorf("%q is found in %q", notExpected, result)
281 }
282}
283
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700284func ensureMatches(t *testing.T, result string, expectedRex string) {
285 ok, err := regexp.MatchString(expectedRex, result)
286 if err != nil {
287 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
288 return
289 }
290 if !ok {
291 t.Errorf("%s does not match regular expession %s", result, expectedRex)
292 }
293}
294
Jiyong Park25fc6a92018-11-18 18:02:45 +0900295func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900296 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900297 if !android.InList(expected, result) {
298 t.Errorf("%q is not found in %v", expected, result)
299 }
300}
301
302func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900303 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900304 if android.InList(notExpected, result) {
305 t.Errorf("%q is found in %v", notExpected, result)
306 }
307}
308
Jooyung Hane1633032019-08-01 17:41:43 +0900309func ensureListEmpty(t *testing.T, result []string) {
310 t.Helper()
311 if len(result) > 0 {
312 t.Errorf("%q is expected to be empty", result)
313 }
314}
315
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000316func ensureListNotEmpty(t *testing.T, result []string) {
317 t.Helper()
318 if len(result) == 0 {
319 t.Errorf("%q is expected to be not empty", result)
320 }
321}
322
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323// Minimal test
324func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800325 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900326 apex_defaults {
327 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900328 manifest: ":myapex.manifest",
329 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900331 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900332 native_shared_libs: [
333 "mylib",
334 "libfoo.ffi",
335 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900336 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800337 multilib: {
338 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900339 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800340 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900341 },
Jiyong Park77acec62020-06-01 21:39:15 +0900342 java_libs: [
343 "myjar",
344 "myjar_dex",
345 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000346 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900347 }
348
Jiyong Park30ca9372019-02-07 16:27:23 +0900349 apex {
350 name: "myapex",
351 defaults: ["myapex-defaults"],
352 }
353
Jiyong Park25fc6a92018-11-18 18:02:45 +0900354 apex_key {
355 name: "myapex.key",
356 public_key: "testkey.avbpubkey",
357 private_key: "testkey.pem",
358 }
359
Jiyong Park809bb722019-02-13 21:33:49 +0900360 filegroup {
361 name: "myapex.manifest",
362 srcs: ["apex_manifest.json"],
363 }
364
365 filegroup {
366 name: "myapex.androidmanifest",
367 srcs: ["AndroidManifest.xml"],
368 }
369
Jiyong Park25fc6a92018-11-18 18:02:45 +0900370 cc_library {
371 name: "mylib",
372 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900373 shared_libs: [
374 "mylib2",
375 "libbar.ffi",
376 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900377 system_shared_libs: [],
378 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000379 // TODO: remove //apex_available:platform
380 apex_available: [
381 "//apex_available:platform",
382 "myapex",
383 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900384 }
385
Alex Light3d673592019-01-18 14:37:31 -0800386 cc_binary {
387 name: "foo",
388 srcs: ["mylib.cpp"],
389 compile_multilib: "both",
390 multilib: {
391 lib32: {
392 suffix: "32",
393 },
394 lib64: {
395 suffix: "64",
396 },
397 },
398 symlinks: ["foo_link_"],
399 symlink_preferred_arch: true,
400 system_shared_libs: [],
401 static_executable: true,
402 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700403 apex_available: [ "myapex", "com.android.gki.*" ],
404 }
405
Jiyong Park99644e92020-11-17 22:21:02 +0900406 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000407 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900408 srcs: ["foo.rs"],
409 rlibs: ["libfoo.rlib.rust"],
410 dylibs: ["libfoo.dylib.rust"],
411 apex_available: ["myapex"],
412 }
413
414 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000415 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900416 srcs: ["foo.rs"],
417 crate_name: "foo",
418 apex_available: ["myapex"],
419 }
420
421 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000422 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900423 srcs: ["foo.rs"],
424 crate_name: "foo",
425 apex_available: ["myapex"],
426 }
427
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900428 rust_ffi_shared {
429 name: "libfoo.ffi",
430 srcs: ["foo.rs"],
431 crate_name: "foo",
432 apex_available: ["myapex"],
433 }
434
435 rust_ffi_shared {
436 name: "libbar.ffi",
437 srcs: ["foo.rs"],
438 crate_name: "bar",
439 apex_available: ["myapex"],
440 }
441
Yifan Hongd22a84a2020-07-28 17:37:46 -0700442 apex {
443 name: "com.android.gki.fake",
444 binaries: ["foo"],
445 key: "myapex.key",
446 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000447 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800448 }
449
Paul Duffindddd5462020-04-07 15:25:44 +0100450 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900451 name: "mylib2",
452 srcs: ["mylib.cpp"],
453 system_shared_libs: [],
454 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900455 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900456 static_libs: ["libstatic"],
457 // TODO: remove //apex_available:platform
458 apex_available: [
459 "//apex_available:platform",
460 "myapex",
461 ],
462 }
463
Paul Duffindddd5462020-04-07 15:25:44 +0100464 cc_prebuilt_library_shared {
465 name: "mylib2",
466 srcs: ["prebuilt.so"],
467 // TODO: remove //apex_available:platform
468 apex_available: [
469 "//apex_available:platform",
470 "myapex",
471 ],
472 }
473
Jiyong Park9918e1a2020-03-17 19:16:40 +0900474 cc_library_static {
475 name: "libstatic",
476 srcs: ["mylib.cpp"],
477 system_shared_libs: [],
478 stl: "none",
479 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000480 // TODO: remove //apex_available:platform
481 apex_available: [
482 "//apex_available:platform",
483 "myapex",
484 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900485 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900486
487 java_library {
488 name: "myjar",
489 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900490 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900491 sdk_version: "none",
492 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900493 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900494 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000495 // TODO: remove //apex_available:platform
496 apex_available: [
497 "//apex_available:platform",
498 "myapex",
499 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900500 }
501
Jiyong Park77acec62020-06-01 21:39:15 +0900502 dex_import {
503 name: "myjar_dex",
504 jars: ["prebuilt.jar"],
505 apex_available: [
506 "//apex_available:platform",
507 "myapex",
508 ],
509 }
510
Jiyong Park7f7766d2019-07-25 22:02:35 +0900511 java_library {
512 name: "myotherjar",
513 srcs: ["foo/bar/MyClass.java"],
514 sdk_version: "none",
515 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900516 // TODO: remove //apex_available:platform
517 apex_available: [
518 "//apex_available:platform",
519 "myapex",
520 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900521 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900522
523 java_library {
524 name: "mysharedjar",
525 srcs: ["foo/bar/MyClass.java"],
526 sdk_version: "none",
527 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900528 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900529 `)
530
Sundong Ahnabb64432019-10-22 13:58:29 +0900531 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900532
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900533 // Make sure that Android.mk is created
534 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700535 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900536 var builder strings.Builder
537 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
538
539 androidMk := builder.String()
540 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
541 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
542
Jiyong Park42cca6c2019-04-01 11:15:50 +0900543 optFlags := apexRule.Args["opt_flags"]
544 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700545 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900546 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900547
Jiyong Park25fc6a92018-11-18 18:02:45 +0900548 copyCmds := apexRule.Args["copy_commands"]
549
550 // Ensure that main rule creates an output
551 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
552
553 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700554 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
555 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
556 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900557 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900558 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900559
560 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700561 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
562 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900563 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
564 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900565 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900566
567 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800568 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
569 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900570 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900571 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900572 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900573 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
574 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900575 // .. but not for java libs
576 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900577 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800578
Colin Cross7113d202019-11-20 16:39:12 -0800579 // Ensure that the platform variant ends with _shared or _common
580 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
581 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900582 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
583 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900584 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
585
586 // Ensure that dynamic dependency to java libs are not included
587 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800588
589 // Ensure that all symlinks are present.
590 found_foo_link_64 := false
591 found_foo := false
592 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900593 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800594 if strings.HasSuffix(cmd, "bin/foo") {
595 found_foo = true
596 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
597 found_foo_link_64 = true
598 }
599 }
600 }
601 good := found_foo && found_foo_link_64
602 if !good {
603 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
604 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900605
Sundong Ahnabb64432019-10-22 13:58:29 +0900606 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700607 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900608 if len(noticeInputs) != 3 {
609 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900610 }
611 ensureListContains(t, noticeInputs, "NOTICE")
612 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900613 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900614
Artur Satayeva8bd1132020-04-27 18:07:06 +0100615 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100616 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100617 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
618 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
619 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100620
621 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100622 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100623 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
624 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
625 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800626}
627
Jooyung Hanf21c7972019-12-16 22:32:06 +0900628func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800629 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900630 apex_defaults {
631 name: "myapex-defaults",
632 key: "myapex.key",
633 prebuilts: ["myetc"],
634 native_shared_libs: ["mylib"],
635 java_libs: ["myjar"],
636 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900637 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800638 bpfs: ["bpf"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000639 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900640 }
641
642 prebuilt_etc {
643 name: "myetc",
644 src: "myprebuilt",
645 }
646
647 apex {
648 name: "myapex",
649 defaults: ["myapex-defaults"],
650 }
651
652 apex_key {
653 name: "myapex.key",
654 public_key: "testkey.avbpubkey",
655 private_key: "testkey.pem",
656 }
657
658 cc_library {
659 name: "mylib",
660 system_shared_libs: [],
661 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000662 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900663 }
664
665 java_library {
666 name: "myjar",
667 srcs: ["foo/bar/MyClass.java"],
668 sdk_version: "none",
669 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000670 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900671 }
672
673 android_app {
674 name: "AppFoo",
675 srcs: ["foo/bar/MyClass.java"],
676 sdk_version: "none",
677 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000678 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900679 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900680
681 runtime_resource_overlay {
682 name: "rro",
683 theme: "blue",
684 }
685
markchien2f59ec92020-09-02 16:23:38 +0800686 bpf {
687 name: "bpf",
688 srcs: ["bpf.c", "bpf2.c"],
689 }
690
Jooyung Hanf21c7972019-12-16 22:32:06 +0900691 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000692 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900693 "etc/myetc",
694 "javalib/myjar.jar",
695 "lib64/mylib.so",
696 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900697 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800698 "etc/bpf/bpf.o",
699 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900700 })
701}
702
Jooyung Han01a3ee22019-11-02 02:52:25 +0900703func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800704 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900705 apex {
706 name: "myapex",
707 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000708 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900709 }
710
711 apex_key {
712 name: "myapex.key",
713 public_key: "testkey.avbpubkey",
714 private_key: "testkey.pem",
715 }
716 `)
717
718 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900719 args := module.Rule("apexRule").Args
720 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
721 t.Error("manifest should be apex_manifest.pb, but " + manifest)
722 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900723}
724
Alex Light5098a612018-11-29 17:12:15 -0800725func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800726 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800727 apex {
728 name: "myapex",
729 key: "myapex.key",
730 payload_type: "zip",
731 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000732 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800733 }
734
735 apex_key {
736 name: "myapex.key",
737 public_key: "testkey.avbpubkey",
738 private_key: "testkey.pem",
739 }
740
741 cc_library {
742 name: "mylib",
743 srcs: ["mylib.cpp"],
744 shared_libs: ["mylib2"],
745 system_shared_libs: [],
746 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000747 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800748 }
749
750 cc_library {
751 name: "mylib2",
752 srcs: ["mylib.cpp"],
753 system_shared_libs: [],
754 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000755 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800756 }
757 `)
758
Sundong Ahnabb64432019-10-22 13:58:29 +0900759 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800760 copyCmds := zipApexRule.Args["copy_commands"]
761
762 // Ensure that main rule creates an output
763 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
764
765 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700766 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800767
768 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700769 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800770
771 // Ensure that both direct and indirect deps are copied into apex
772 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
773 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900774}
775
776func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800777 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900778 apex {
779 name: "myapex",
780 key: "myapex.key",
781 native_shared_libs: ["mylib", "mylib3"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000782 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900783 }
784
785 apex_key {
786 name: "myapex.key",
787 public_key: "testkey.avbpubkey",
788 private_key: "testkey.pem",
789 }
790
791 cc_library {
792 name: "mylib",
793 srcs: ["mylib.cpp"],
794 shared_libs: ["mylib2", "mylib3"],
795 system_shared_libs: [],
796 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000797 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900798 }
799
800 cc_library {
801 name: "mylib2",
802 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900803 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900804 system_shared_libs: [],
805 stl: "none",
806 stubs: {
807 versions: ["1", "2", "3"],
808 },
809 }
810
811 cc_library {
812 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900813 srcs: ["mylib.cpp"],
814 shared_libs: ["mylib4"],
815 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900816 stl: "none",
817 stubs: {
818 versions: ["10", "11", "12"],
819 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000820 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900821 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900822
823 cc_library {
824 name: "mylib4",
825 srcs: ["mylib.cpp"],
826 system_shared_libs: [],
827 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000828 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900829 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900830 `)
831
Sundong Ahnabb64432019-10-22 13:58:29 +0900832 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900833 copyCmds := apexRule.Args["copy_commands"]
834
835 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800836 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900837
838 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800839 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900840
841 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800842 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900843
Colin Crossaede88c2020-08-11 12:17:01 -0700844 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900845
846 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900847 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900848 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900849 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900850
851 // 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 -0700852 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900853 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700854 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900855
856 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900857 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900858 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900859
860 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700861 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900862
Jooyung Hana57af4a2020-01-23 05:36:59 +0000863 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900864 "lib64/mylib.so",
865 "lib64/mylib3.so",
866 "lib64/mylib4.so",
867 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900868}
869
Colin Cross7812fd32020-09-25 12:35:10 -0700870func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
871 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -0800872 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -0700873 apex {
874 name: "myapex",
875 key: "myapex.key",
876 native_shared_libs: ["mylib", "mylib3"],
877 min_sdk_version: "29",
878 }
879
880 apex_key {
881 name: "myapex.key",
882 public_key: "testkey.avbpubkey",
883 private_key: "testkey.pem",
884 }
885
886 cc_library {
887 name: "mylib",
888 srcs: ["mylib.cpp"],
889 shared_libs: ["mylib2", "mylib3"],
890 system_shared_libs: [],
891 stl: "none",
892 apex_available: [ "myapex" ],
893 min_sdk_version: "28",
894 }
895
896 cc_library {
897 name: "mylib2",
898 srcs: ["mylib.cpp"],
899 cflags: ["-include mylib.h"],
900 system_shared_libs: [],
901 stl: "none",
902 stubs: {
903 versions: ["28", "29", "30", "current"],
904 },
905 min_sdk_version: "28",
906 }
907
908 cc_library {
909 name: "mylib3",
910 srcs: ["mylib.cpp"],
911 shared_libs: ["mylib4"],
912 system_shared_libs: [],
913 stl: "none",
914 stubs: {
915 versions: ["28", "29", "30", "current"],
916 },
917 apex_available: [ "myapex" ],
918 min_sdk_version: "28",
919 }
920
921 cc_library {
922 name: "mylib4",
923 srcs: ["mylib.cpp"],
924 system_shared_libs: [],
925 stl: "none",
926 apex_available: [ "myapex" ],
927 min_sdk_version: "28",
928 }
929 `)
930
931 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
932 copyCmds := apexRule.Args["copy_commands"]
933
934 // Ensure that direct non-stubs dep is always included
935 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
936
937 // Ensure that indirect stubs dep is not included
938 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
939
940 // Ensure that direct stubs dep is included
941 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
942
943 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
944
Jiyong Park55549df2021-02-26 23:57:23 +0900945 // Ensure that mylib is linking with the latest version of stub for mylib2
946 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -0700947 // ... and not linking to the non-stub (impl) variant of mylib2
948 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
949
950 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
951 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
952 // .. and not linking to the stubs variant of mylib3
953 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
954
955 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -0700956 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -0700957 ensureNotContains(t, mylib2Cflags, "-include ")
958
959 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700960 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -0700961
962 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
963 "lib64/mylib.so",
964 "lib64/mylib3.so",
965 "lib64/mylib4.so",
966 })
967}
968
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900969func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
970 t.Parallel()
971 // myapex (Z)
972 // mylib -----------------.
973 // |
974 // otherapex (29) |
975 // libstub's versions: 29 Z current
976 // |
977 // <platform> |
978 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -0800979 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900980 apex {
981 name: "myapex",
982 key: "myapex.key",
983 native_shared_libs: ["mylib"],
984 min_sdk_version: "Z", // non-final
985 }
986
987 cc_library {
988 name: "mylib",
989 srcs: ["mylib.cpp"],
990 shared_libs: ["libstub"],
991 apex_available: ["myapex"],
992 min_sdk_version: "Z",
993 }
994
995 apex_key {
996 name: "myapex.key",
997 public_key: "testkey.avbpubkey",
998 private_key: "testkey.pem",
999 }
1000
1001 apex {
1002 name: "otherapex",
1003 key: "myapex.key",
1004 native_shared_libs: ["libstub"],
1005 min_sdk_version: "29",
1006 }
1007
1008 cc_library {
1009 name: "libstub",
1010 srcs: ["mylib.cpp"],
1011 stubs: {
1012 versions: ["29", "Z", "current"],
1013 },
1014 apex_available: ["otherapex"],
1015 min_sdk_version: "29",
1016 }
1017
1018 // platform module depending on libstub from otherapex should use the latest stub("current")
1019 cc_library {
1020 name: "libplatform",
1021 srcs: ["mylib.cpp"],
1022 shared_libs: ["libstub"],
1023 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001024 `,
1025 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1026 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1027 variables.Platform_sdk_final = proptools.BoolPtr(false)
1028 variables.Platform_version_active_codenames = []string{"Z"}
1029 }),
1030 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001031
Jiyong Park55549df2021-02-26 23:57:23 +09001032 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001033 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001034 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001035 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001036 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001037
1038 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1039 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1040 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1041 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1042 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1043}
1044
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001045func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001046 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001047 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001048 name: "myapex2",
1049 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001050 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001051 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001052 }
1053
1054 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001055 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001056 public_key: "testkey.avbpubkey",
1057 private_key: "testkey.pem",
1058 }
1059
1060 cc_library {
1061 name: "mylib",
1062 srcs: ["mylib.cpp"],
1063 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001064 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001065 system_shared_libs: [],
1066 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001067 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001068 }
1069
1070 cc_library {
1071 name: "libfoo",
1072 srcs: ["mylib.cpp"],
1073 shared_libs: ["libbar"],
1074 system_shared_libs: [],
1075 stl: "none",
1076 stubs: {
1077 versions: ["10", "20", "30"],
1078 },
1079 }
1080
1081 cc_library {
1082 name: "libbar",
1083 srcs: ["mylib.cpp"],
1084 system_shared_libs: [],
1085 stl: "none",
1086 }
1087
Jiyong Park678c8812020-02-07 17:25:49 +09001088 cc_library_static {
1089 name: "libbaz",
1090 srcs: ["mylib.cpp"],
1091 system_shared_libs: [],
1092 stl: "none",
1093 apex_available: [ "myapex2" ],
1094 }
1095
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001096 `)
1097
Jiyong Park83dc74b2020-01-14 18:38:44 +09001098 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001099 copyCmds := apexRule.Args["copy_commands"]
1100
1101 // Ensure that direct non-stubs dep is always included
1102 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1103
1104 // Ensure that indirect stubs dep is not included
1105 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1106
1107 // Ensure that dependency of stubs is not included
1108 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1109
Colin Crossaede88c2020-08-11 12:17:01 -07001110 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001111
1112 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001113 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001114 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001115 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001116
Jiyong Park3ff16992019-12-27 14:11:47 +09001117 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001118
1119 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1120 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001121
Artur Satayeva8bd1132020-04-27 18:07:06 +01001122 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001123 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001124
Artur Satayeva8bd1132020-04-27 18:07:06 +01001125 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001126 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001127}
1128
Jooyung Hand3639552019-08-09 12:57:43 +09001129func TestApexWithRuntimeLibsDependency(t *testing.T) {
1130 /*
1131 myapex
1132 |
1133 v (runtime_libs)
1134 mylib ------+------> libfoo [provides stub]
1135 |
1136 `------> libbar
1137 */
Colin Cross1c460562021-02-16 17:55:47 -08001138 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001139 apex {
1140 name: "myapex",
1141 key: "myapex.key",
1142 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001143 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001144 }
1145
1146 apex_key {
1147 name: "myapex.key",
1148 public_key: "testkey.avbpubkey",
1149 private_key: "testkey.pem",
1150 }
1151
1152 cc_library {
1153 name: "mylib",
1154 srcs: ["mylib.cpp"],
1155 runtime_libs: ["libfoo", "libbar"],
1156 system_shared_libs: [],
1157 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001158 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001159 }
1160
1161 cc_library {
1162 name: "libfoo",
1163 srcs: ["mylib.cpp"],
1164 system_shared_libs: [],
1165 stl: "none",
1166 stubs: {
1167 versions: ["10", "20", "30"],
1168 },
1169 }
1170
1171 cc_library {
1172 name: "libbar",
1173 srcs: ["mylib.cpp"],
1174 system_shared_libs: [],
1175 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001176 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001177 }
1178
1179 `)
1180
Sundong Ahnabb64432019-10-22 13:58:29 +09001181 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001182 copyCmds := apexRule.Args["copy_commands"]
1183
1184 // Ensure that direct non-stubs dep is always included
1185 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1186
1187 // Ensure that indirect stubs dep is not included
1188 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1189
1190 // Ensure that runtime_libs dep in included
1191 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1192
Sundong Ahnabb64432019-10-22 13:58:29 +09001193 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001194 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1195 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001196
1197}
1198
Paul Duffina02cae32021-03-09 01:44:06 +00001199var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1200 cc.PrepareForTestWithCcBuildComponents,
1201 PrepareForTestWithApexBuildComponents,
1202 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001203 apex {
1204 name: "com.android.runtime",
1205 key: "com.android.runtime.key",
1206 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001207 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001208 }
1209
1210 apex_key {
1211 name: "com.android.runtime.key",
1212 public_key: "testkey.avbpubkey",
1213 private_key: "testkey.pem",
1214 }
Paul Duffina02cae32021-03-09 01:44:06 +00001215 `),
1216 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1217)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001218
Paul Duffina02cae32021-03-09 01:44:06 +00001219func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
1220 result := emptyFixtureFactory.Extend(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001221 cc_library {
1222 name: "libc",
1223 no_libcrt: true,
1224 nocrt: true,
1225 stl: "none",
1226 system_shared_libs: [],
1227 stubs: { versions: ["1"] },
1228 apex_available: ["com.android.runtime"],
1229
1230 sanitize: {
1231 hwaddress: true,
1232 }
1233 }
1234
1235 cc_prebuilt_library_shared {
1236 name: "libclang_rt.hwasan-aarch64-android",
1237 no_libcrt: true,
1238 nocrt: true,
1239 stl: "none",
1240 system_shared_libs: [],
1241 srcs: [""],
1242 stubs: { versions: ["1"] },
1243
1244 sanitize: {
1245 never: true,
1246 },
Paul Duffina02cae32021-03-09 01:44:06 +00001247 } `)
1248 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001249
1250 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1251 "lib64/bionic/libc.so",
1252 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1253 })
1254
1255 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1256
1257 installed := hwasan.Description("install libclang_rt.hwasan")
1258 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1259
1260 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1261 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1262 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1263}
1264
1265func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffina02cae32021-03-09 01:44:06 +00001266 result := emptyFixtureFactory.Extend(
1267 prepareForTestOfRuntimeApexWithHwasan,
1268 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1269 variables.SanitizeDevice = []string{"hwaddress"}
1270 }),
1271 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001272 cc_library {
1273 name: "libc",
1274 no_libcrt: true,
1275 nocrt: true,
1276 stl: "none",
1277 system_shared_libs: [],
1278 stubs: { versions: ["1"] },
1279 apex_available: ["com.android.runtime"],
1280 }
1281
1282 cc_prebuilt_library_shared {
1283 name: "libclang_rt.hwasan-aarch64-android",
1284 no_libcrt: true,
1285 nocrt: true,
1286 stl: "none",
1287 system_shared_libs: [],
1288 srcs: [""],
1289 stubs: { versions: ["1"] },
1290
1291 sanitize: {
1292 never: true,
1293 },
1294 }
Paul Duffina02cae32021-03-09 01:44:06 +00001295 `)
1296 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001297
1298 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1299 "lib64/bionic/libc.so",
1300 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1301 })
1302
1303 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1304
1305 installed := hwasan.Description("install libclang_rt.hwasan")
1306 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1307
1308 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1309 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1310 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1311}
1312
Jooyung Han61b66e92020-03-21 14:21:46 +00001313func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1314 testcases := []struct {
1315 name string
1316 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001317 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001318 shouldLink string
1319 shouldNotLink []string
1320 }{
1321 {
Jiyong Park55549df2021-02-26 23:57:23 +09001322 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001323 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001324 apexVariant: "apex10000",
Jooyung Han61b66e92020-03-21 14:21:46 +00001325 shouldLink: "30",
1326 shouldNotLink: []string{"29"},
1327 },
1328 {
Jiyong Park55549df2021-02-26 23:57:23 +09001329 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001330 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001331 apexVariant: "apex29",
Jiyong Park55549df2021-02-26 23:57:23 +09001332 shouldLink: "30",
1333 shouldNotLink: []string{"29"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001334 },
1335 }
1336 for _, tc := range testcases {
1337 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001338 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001339 apex {
1340 name: "myapex",
1341 key: "myapex.key",
1342 use_vendor: true,
1343 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001344 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001345 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001346 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001347
Jooyung Han61b66e92020-03-21 14:21:46 +00001348 apex_key {
1349 name: "myapex.key",
1350 public_key: "testkey.avbpubkey",
1351 private_key: "testkey.pem",
1352 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001353
Jooyung Han61b66e92020-03-21 14:21:46 +00001354 cc_library {
1355 name: "mylib",
1356 srcs: ["mylib.cpp"],
1357 vendor_available: true,
1358 shared_libs: ["libbar"],
1359 system_shared_libs: [],
1360 stl: "none",
1361 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001362 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001363 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001364
Jooyung Han61b66e92020-03-21 14:21:46 +00001365 cc_library {
1366 name: "libbar",
1367 srcs: ["mylib.cpp"],
1368 system_shared_libs: [],
1369 stl: "none",
1370 stubs: { versions: ["29","30"] },
Colin Cross0477b422020-10-13 18:43:54 -07001371 llndk_stubs: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001372 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001373
Jooyung Han61b66e92020-03-21 14:21:46 +00001374 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001375 name: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001376 symbol_file: "",
1377 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001378 `,
1379 setUseVendorAllowListForTest([]string{"myapex"}),
1380 withUnbundledBuild,
1381 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001382
Jooyung Han61b66e92020-03-21 14:21:46 +00001383 // Ensure that LLNDK dep is not included
1384 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1385 "lib64/mylib.so",
1386 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001387
Jooyung Han61b66e92020-03-21 14:21:46 +00001388 // Ensure that LLNDK dep is required
1389 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1390 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1391 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001392
Colin Crossaede88c2020-08-11 12:17:01 -07001393 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
Colin Cross127bb8b2020-12-16 16:46:01 -08001394 ensureContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001395 for _, ver := range tc.shouldNotLink {
Colin Cross127bb8b2020-12-16 16:46:01 -08001396 ensureNotContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001397 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001398
Colin Crossaede88c2020-08-11 12:17:01 -07001399 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jooyung Han61b66e92020-03-21 14:21:46 +00001400 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
1401 })
1402 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001403}
1404
Jiyong Park25fc6a92018-11-18 18:02:45 +09001405func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001406 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001407 apex {
1408 name: "myapex",
1409 key: "myapex.key",
1410 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001411 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001412 }
1413
1414 apex_key {
1415 name: "myapex.key",
1416 public_key: "testkey.avbpubkey",
1417 private_key: "testkey.pem",
1418 }
1419
1420 cc_library {
1421 name: "mylib",
1422 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001423 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001424 shared_libs: ["libdl#27"],
1425 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001426 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001427 }
1428
1429 cc_library_shared {
1430 name: "mylib_shared",
1431 srcs: ["mylib.cpp"],
1432 shared_libs: ["libdl#27"],
1433 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001434 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001435 }
1436
1437 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001438 name: "libBootstrap",
1439 srcs: ["mylib.cpp"],
1440 stl: "none",
1441 bootstrap: true,
1442 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001443 `)
1444
Sundong Ahnabb64432019-10-22 13:58:29 +09001445 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001446 copyCmds := apexRule.Args["copy_commands"]
1447
1448 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001449 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001450 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1451 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001452
1453 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001454 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001455
Colin Crossaede88c2020-08-11 12:17:01 -07001456 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1457 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1458 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001459
1460 // For dependency to libc
1461 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001462 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001463 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001464 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001465 // ... Cflags from stub is correctly exported to mylib
1466 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1467 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1468
1469 // For dependency to libm
1470 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001471 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001472 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001473 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001474 // ... and is not compiling with the stub
1475 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1476 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1477
1478 // For dependency to libdl
1479 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001480 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001481 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001482 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1483 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001484 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001485 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001486 // ... Cflags from stub is correctly exported to mylib
1487 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1488 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001489
1490 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001491 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1492 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1493 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1494 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001495}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001496
Jooyung Han749dc692020-04-15 11:03:39 +09001497func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001498 // there are three links between liba --> libz.
1499 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001500 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001501 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001502 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001503 apex {
1504 name: "myapex",
1505 key: "myapex.key",
1506 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001507 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001508 }
1509
1510 apex {
1511 name: "otherapex",
1512 key: "myapex.key",
1513 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001514 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001515 }
1516
1517 apex_key {
1518 name: "myapex.key",
1519 public_key: "testkey.avbpubkey",
1520 private_key: "testkey.pem",
1521 }
1522
1523 cc_library {
1524 name: "libx",
1525 shared_libs: ["liba"],
1526 system_shared_libs: [],
1527 stl: "none",
1528 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001529 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001530 }
1531
1532 cc_library {
1533 name: "liby",
1534 shared_libs: ["liba"],
1535 system_shared_libs: [],
1536 stl: "none",
1537 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001538 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001539 }
1540
1541 cc_library {
1542 name: "liba",
1543 shared_libs: ["libz"],
1544 system_shared_libs: [],
1545 stl: "none",
1546 apex_available: [
1547 "//apex_available:anyapex",
1548 "//apex_available:platform",
1549 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001550 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001551 }
1552
1553 cc_library {
1554 name: "libz",
1555 system_shared_libs: [],
1556 stl: "none",
1557 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001558 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001559 },
1560 }
Jooyung Han749dc692020-04-15 11:03:39 +09001561 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001562
1563 expectLink := func(from, from_variant, to, to_variant string) {
1564 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1565 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1566 }
1567 expectNoLink := func(from, from_variant, to, to_variant string) {
1568 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1569 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1570 }
1571 // platform liba is linked to non-stub version
1572 expectLink("liba", "shared", "libz", "shared")
Jiyong Park55549df2021-02-26 23:57:23 +09001573 // liba in myapex is linked to #30
1574 expectLink("liba", "shared_apex29", "libz", "shared_30")
1575 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001576 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jooyung Han749dc692020-04-15 11:03:39 +09001577 // liba in otherapex is linked to #30
Colin Crossaede88c2020-08-11 12:17:01 -07001578 expectLink("liba", "shared_apex30", "libz", "shared_30")
1579 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1580 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001581}
1582
Jooyung Hanaed150d2020-04-02 01:41:41 +09001583func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001584 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001585 apex {
1586 name: "myapex",
1587 key: "myapex.key",
1588 native_shared_libs: ["libx"],
1589 min_sdk_version: "R",
1590 }
1591
1592 apex_key {
1593 name: "myapex.key",
1594 public_key: "testkey.avbpubkey",
1595 private_key: "testkey.pem",
1596 }
1597
1598 cc_library {
1599 name: "libx",
1600 shared_libs: ["libz"],
1601 system_shared_libs: [],
1602 stl: "none",
1603 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001604 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001605 }
1606
1607 cc_library {
1608 name: "libz",
1609 system_shared_libs: [],
1610 stl: "none",
1611 stubs: {
1612 versions: ["29", "R"],
1613 },
1614 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001615 `,
1616 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1617 variables.Platform_version_active_codenames = []string{"R"}
1618 }),
1619 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001620
1621 expectLink := func(from, from_variant, to, to_variant string) {
1622 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1623 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1624 }
1625 expectNoLink := func(from, from_variant, to, to_variant string) {
1626 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1627 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1628 }
Dan Albertc8060532020-07-22 22:32:17 -07001629 expectLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001630 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1631 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001632}
1633
Jooyung Han749dc692020-04-15 11:03:39 +09001634func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001635 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001636 apex {
1637 name: "myapex",
1638 key: "myapex.key",
1639 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001640 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001641 }
1642
1643 apex_key {
1644 name: "myapex.key",
1645 public_key: "testkey.avbpubkey",
1646 private_key: "testkey.pem",
1647 }
1648
1649 cc_library {
1650 name: "libx",
1651 shared_libs: ["libz"],
1652 system_shared_libs: [],
1653 stl: "none",
1654 apex_available: [ "myapex" ],
1655 }
1656
1657 cc_library {
1658 name: "libz",
1659 system_shared_libs: [],
1660 stl: "none",
1661 stubs: {
1662 versions: ["1", "2"],
1663 },
1664 }
1665 `)
1666
1667 expectLink := func(from, from_variant, to, to_variant string) {
1668 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1669 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1670 }
1671 expectNoLink := func(from, from_variant, to, to_variant string) {
1672 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1673 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1674 }
Colin Crossaede88c2020-08-11 12:17:01 -07001675 expectLink("libx", "shared_apex10000", "libz", "shared_2")
1676 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
1677 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001678}
1679
1680func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001681 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001682 apex {
1683 name: "myapex",
1684 key: "myapex.key",
1685 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001686 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001687 }
1688
1689 apex_key {
1690 name: "myapex.key",
1691 public_key: "testkey.avbpubkey",
1692 private_key: "testkey.pem",
1693 }
1694
1695 cc_library {
1696 name: "libx",
1697 system_shared_libs: [],
1698 stl: "none",
1699 apex_available: [ "myapex" ],
1700 stubs: {
1701 versions: ["1", "2"],
1702 },
1703 }
1704
1705 cc_library {
1706 name: "libz",
1707 shared_libs: ["libx"],
1708 system_shared_libs: [],
1709 stl: "none",
1710 }
1711 `)
1712
1713 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001714 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001715 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1716 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1717 }
1718 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001719 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001720 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1721 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1722 }
1723 expectLink("libz", "shared", "libx", "shared_2")
1724 expectNoLink("libz", "shared", "libz", "shared_1")
1725 expectNoLink("libz", "shared", "libz", "shared")
1726}
1727
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001728var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1729 func(variables android.FixtureProductVariables) {
1730 variables.SanitizeDevice = []string{"hwaddress"}
1731 },
1732)
1733
Jooyung Han75568392020-03-20 04:29:24 +09001734func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001735 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001736 apex {
1737 name: "myapex",
1738 key: "myapex.key",
1739 native_shared_libs: ["libx"],
1740 min_sdk_version: "29",
1741 }
1742
1743 apex_key {
1744 name: "myapex.key",
1745 public_key: "testkey.avbpubkey",
1746 private_key: "testkey.pem",
1747 }
1748
1749 cc_library {
1750 name: "libx",
1751 shared_libs: ["libbar"],
1752 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001753 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001754 }
1755
1756 cc_library {
1757 name: "libbar",
1758 stubs: {
1759 versions: ["29", "30"],
1760 },
1761 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001762 `,
1763 prepareForTestWithSantitizeHwaddress,
1764 )
Jooyung Han03b51852020-02-26 22:45:42 +09001765 expectLink := func(from, from_variant, to, to_variant string) {
1766 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1767 libFlags := ld.Args["libFlags"]
1768 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1769 }
Colin Crossaede88c2020-08-11 12:17:01 -07001770 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_30")
Jooyung Han03b51852020-02-26 22:45:42 +09001771}
1772
Jooyung Han75568392020-03-20 04:29:24 +09001773func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001774 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001775 apex {
1776 name: "myapex",
1777 key: "myapex.key",
1778 native_shared_libs: ["libx"],
1779 min_sdk_version: "29",
1780 }
1781
1782 apex_key {
1783 name: "myapex.key",
1784 public_key: "testkey.avbpubkey",
1785 private_key: "testkey.pem",
1786 }
1787
1788 cc_library {
1789 name: "libx",
1790 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001791 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001792 }
Jooyung Han75568392020-03-20 04:29:24 +09001793 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001794
1795 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001796 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001797 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001798 // note that platform variant is not.
1799 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001800 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001801}
1802
Jooyung Han749dc692020-04-15 11:03:39 +09001803func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1804 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001805 apex {
1806 name: "myapex",
1807 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001808 native_shared_libs: ["mylib"],
1809 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001810 }
1811
1812 apex_key {
1813 name: "myapex.key",
1814 public_key: "testkey.avbpubkey",
1815 private_key: "testkey.pem",
1816 }
Jooyung Han749dc692020-04-15 11:03:39 +09001817
1818 cc_library {
1819 name: "mylib",
1820 srcs: ["mylib.cpp"],
1821 system_shared_libs: [],
1822 stl: "none",
1823 apex_available: [
1824 "myapex",
1825 ],
1826 min_sdk_version: "30",
1827 }
1828 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001829
1830 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
1831 apex {
1832 name: "myapex",
1833 key: "myapex.key",
1834 native_shared_libs: ["libfoo.ffi"],
1835 min_sdk_version: "29",
1836 }
1837
1838 apex_key {
1839 name: "myapex.key",
1840 public_key: "testkey.avbpubkey",
1841 private_key: "testkey.pem",
1842 }
1843
1844 rust_ffi_shared {
1845 name: "libfoo.ffi",
1846 srcs: ["foo.rs"],
1847 crate_name: "foo",
1848 apex_available: [
1849 "myapex",
1850 ],
1851 min_sdk_version: "30",
1852 }
1853 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001854}
1855
1856func TestApexMinSdkVersion_Okay(t *testing.T) {
1857 testApex(t, `
1858 apex {
1859 name: "myapex",
1860 key: "myapex.key",
1861 native_shared_libs: ["libfoo"],
1862 java_libs: ["libbar"],
1863 min_sdk_version: "29",
1864 }
1865
1866 apex_key {
1867 name: "myapex.key",
1868 public_key: "testkey.avbpubkey",
1869 private_key: "testkey.pem",
1870 }
1871
1872 cc_library {
1873 name: "libfoo",
1874 srcs: ["mylib.cpp"],
1875 shared_libs: ["libfoo_dep"],
1876 apex_available: ["myapex"],
1877 min_sdk_version: "29",
1878 }
1879
1880 cc_library {
1881 name: "libfoo_dep",
1882 srcs: ["mylib.cpp"],
1883 apex_available: ["myapex"],
1884 min_sdk_version: "29",
1885 }
1886
1887 java_library {
1888 name: "libbar",
1889 sdk_version: "current",
1890 srcs: ["a.java"],
1891 static_libs: ["libbar_dep"],
1892 apex_available: ["myapex"],
1893 min_sdk_version: "29",
1894 }
1895
1896 java_library {
1897 name: "libbar_dep",
1898 sdk_version: "current",
1899 srcs: ["a.java"],
1900 apex_available: ["myapex"],
1901 min_sdk_version: "29",
1902 }
Jooyung Han03b51852020-02-26 22:45:42 +09001903 `)
1904}
1905
Artur Satayev8cf899a2020-04-15 17:29:42 +01001906func TestJavaStableSdkVersion(t *testing.T) {
1907 testCases := []struct {
1908 name string
1909 expectedError string
1910 bp string
1911 }{
1912 {
1913 name: "Non-updatable apex with non-stable dep",
1914 bp: `
1915 apex {
1916 name: "myapex",
1917 java_libs: ["myjar"],
1918 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001919 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01001920 }
1921 apex_key {
1922 name: "myapex.key",
1923 public_key: "testkey.avbpubkey",
1924 private_key: "testkey.pem",
1925 }
1926 java_library {
1927 name: "myjar",
1928 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001929 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001930 apex_available: ["myapex"],
1931 }
1932 `,
1933 },
1934 {
1935 name: "Updatable apex with stable dep",
1936 bp: `
1937 apex {
1938 name: "myapex",
1939 java_libs: ["myjar"],
1940 key: "myapex.key",
1941 updatable: true,
1942 min_sdk_version: "29",
1943 }
1944 apex_key {
1945 name: "myapex.key",
1946 public_key: "testkey.avbpubkey",
1947 private_key: "testkey.pem",
1948 }
1949 java_library {
1950 name: "myjar",
1951 srcs: ["foo/bar/MyClass.java"],
1952 sdk_version: "current",
1953 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001954 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001955 }
1956 `,
1957 },
1958 {
1959 name: "Updatable apex with non-stable dep",
1960 expectedError: "cannot depend on \"myjar\"",
1961 bp: `
1962 apex {
1963 name: "myapex",
1964 java_libs: ["myjar"],
1965 key: "myapex.key",
1966 updatable: true,
1967 }
1968 apex_key {
1969 name: "myapex.key",
1970 public_key: "testkey.avbpubkey",
1971 private_key: "testkey.pem",
1972 }
1973 java_library {
1974 name: "myjar",
1975 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001976 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001977 apex_available: ["myapex"],
1978 }
1979 `,
1980 },
1981 {
Paul Duffin043f5e72021-03-05 00:00:01 +00001982 name: "Updatable apex with non-stable transitive dep",
1983 // This is not actually detecting that the transitive dependency is unstable, rather it is
1984 // detecting that the transitive dependency is building against a wider API surface than the
1985 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09001986 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001987 bp: `
1988 apex {
1989 name: "myapex",
1990 java_libs: ["myjar"],
1991 key: "myapex.key",
1992 updatable: true,
1993 }
1994 apex_key {
1995 name: "myapex.key",
1996 public_key: "testkey.avbpubkey",
1997 private_key: "testkey.pem",
1998 }
1999 java_library {
2000 name: "myjar",
2001 srcs: ["foo/bar/MyClass.java"],
2002 sdk_version: "current",
2003 apex_available: ["myapex"],
2004 static_libs: ["transitive-jar"],
2005 }
2006 java_library {
2007 name: "transitive-jar",
2008 srcs: ["foo/bar/MyClass.java"],
2009 sdk_version: "core_platform",
2010 apex_available: ["myapex"],
2011 }
2012 `,
2013 },
2014 }
2015
2016 for _, test := range testCases {
2017 t.Run(test.name, func(t *testing.T) {
2018 if test.expectedError == "" {
2019 testApex(t, test.bp)
2020 } else {
2021 testApexError(t, test.expectedError, test.bp)
2022 }
2023 })
2024 }
2025}
2026
Jooyung Han749dc692020-04-15 11:03:39 +09002027func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2028 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2029 apex {
2030 name: "myapex",
2031 key: "myapex.key",
2032 native_shared_libs: ["mylib"],
2033 min_sdk_version: "29",
2034 }
2035
2036 apex_key {
2037 name: "myapex.key",
2038 public_key: "testkey.avbpubkey",
2039 private_key: "testkey.pem",
2040 }
2041
2042 cc_library {
2043 name: "mylib",
2044 srcs: ["mylib.cpp"],
2045 shared_libs: ["mylib2"],
2046 system_shared_libs: [],
2047 stl: "none",
2048 apex_available: [
2049 "myapex",
2050 ],
2051 min_sdk_version: "29",
2052 }
2053
2054 // indirect part of the apex
2055 cc_library {
2056 name: "mylib2",
2057 srcs: ["mylib.cpp"],
2058 system_shared_libs: [],
2059 stl: "none",
2060 apex_available: [
2061 "myapex",
2062 ],
2063 min_sdk_version: "30",
2064 }
2065 `)
2066}
2067
2068func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2069 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2070 apex {
2071 name: "myapex",
2072 key: "myapex.key",
2073 apps: ["AppFoo"],
2074 min_sdk_version: "29",
2075 }
2076
2077 apex_key {
2078 name: "myapex.key",
2079 public_key: "testkey.avbpubkey",
2080 private_key: "testkey.pem",
2081 }
2082
2083 android_app {
2084 name: "AppFoo",
2085 srcs: ["foo/bar/MyClass.java"],
2086 sdk_version: "current",
2087 min_sdk_version: "29",
2088 system_modules: "none",
2089 stl: "none",
2090 static_libs: ["bar"],
2091 apex_available: [ "myapex" ],
2092 }
2093
2094 java_library {
2095 name: "bar",
2096 sdk_version: "current",
2097 srcs: ["a.java"],
2098 apex_available: [ "myapex" ],
2099 }
2100 `)
2101}
2102
2103func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002104 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002105 apex {
2106 name: "myapex",
2107 key: "myapex.key",
2108 native_shared_libs: ["mylib"],
2109 min_sdk_version: "29",
2110 }
2111
2112 apex_key {
2113 name: "myapex.key",
2114 public_key: "testkey.avbpubkey",
2115 private_key: "testkey.pem",
2116 }
2117
Jiyong Park55549df2021-02-26 23:57:23 +09002118 // mylib in myapex will link to mylib2#30
Jooyung Han749dc692020-04-15 11:03:39 +09002119 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2120 cc_library {
2121 name: "mylib",
2122 srcs: ["mylib.cpp"],
2123 shared_libs: ["mylib2"],
2124 system_shared_libs: [],
2125 stl: "none",
2126 apex_available: ["myapex", "otherapex"],
2127 min_sdk_version: "29",
2128 }
2129
2130 cc_library {
2131 name: "mylib2",
2132 srcs: ["mylib.cpp"],
2133 system_shared_libs: [],
2134 stl: "none",
2135 apex_available: ["otherapex"],
2136 stubs: { versions: ["29", "30"] },
2137 min_sdk_version: "30",
2138 }
2139
2140 apex {
2141 name: "otherapex",
2142 key: "myapex.key",
2143 native_shared_libs: ["mylib", "mylib2"],
2144 min_sdk_version: "30",
2145 }
2146 `)
2147 expectLink := func(from, from_variant, to, to_variant string) {
2148 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2149 libFlags := ld.Args["libFlags"]
2150 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2151 }
Jiyong Park55549df2021-02-26 23:57:23 +09002152 expectLink("mylib", "shared_apex29", "mylib2", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07002153 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002154}
2155
Jooyung Haned124c32021-01-26 11:43:46 +09002156func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002157 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2158 func(variables android.FixtureProductVariables) {
2159 variables.Platform_sdk_codename = proptools.StringPtr("S")
2160 variables.Platform_version_active_codenames = []string{"S"}
2161 },
2162 )
Jooyung Haned124c32021-01-26 11:43:46 +09002163 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2164 apex {
2165 name: "myapex",
2166 key: "myapex.key",
2167 native_shared_libs: ["libfoo"],
2168 min_sdk_version: "S",
2169 }
2170 apex_key {
2171 name: "myapex.key",
2172 public_key: "testkey.avbpubkey",
2173 private_key: "testkey.pem",
2174 }
2175 cc_library {
2176 name: "libfoo",
2177 shared_libs: ["libbar"],
2178 apex_available: ["myapex"],
2179 min_sdk_version: "29",
2180 }
2181 cc_library {
2182 name: "libbar",
2183 apex_available: ["myapex"],
2184 }
2185 `, withSAsActiveCodeNames)
2186}
2187
2188func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002189 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2190 variables.Platform_sdk_codename = proptools.StringPtr("S")
2191 variables.Platform_version_active_codenames = []string{"S", "T"}
2192 })
Colin Cross1c460562021-02-16 17:55:47 -08002193 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002194 apex {
2195 name: "myapex",
2196 key: "myapex.key",
2197 native_shared_libs: ["libfoo"],
2198 min_sdk_version: "S",
2199 }
2200 apex_key {
2201 name: "myapex.key",
2202 public_key: "testkey.avbpubkey",
2203 private_key: "testkey.pem",
2204 }
2205 cc_library {
2206 name: "libfoo",
2207 shared_libs: ["libbar"],
2208 apex_available: ["myapex"],
2209 min_sdk_version: "S",
2210 }
2211 cc_library {
2212 name: "libbar",
2213 stubs: {
2214 symbol_file: "libbar.map.txt",
2215 versions: ["30", "S", "T"],
2216 },
2217 }
2218 `, withSAsActiveCodeNames)
2219
2220 // ensure libfoo is linked with "S" version of libbar stub
2221 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2222 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09002223 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_T/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002224}
2225
Jiyong Park7c2ee712018-12-07 00:42:25 +09002226func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002227 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002228 apex {
2229 name: "myapex",
2230 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002231 native_shared_libs: ["mylib"],
2232 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002233 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002234 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002235 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002236 }
2237
2238 apex_key {
2239 name: "myapex.key",
2240 public_key: "testkey.avbpubkey",
2241 private_key: "testkey.pem",
2242 }
2243
2244 prebuilt_etc {
2245 name: "myetc",
2246 src: "myprebuilt",
2247 sub_dir: "foo/bar",
2248 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002249
2250 cc_library {
2251 name: "mylib",
2252 srcs: ["mylib.cpp"],
2253 relative_install_path: "foo/bar",
2254 system_shared_libs: [],
2255 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002256 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002257 }
2258
2259 cc_binary {
2260 name: "mybin",
2261 srcs: ["mylib.cpp"],
2262 relative_install_path: "foo/bar",
2263 system_shared_libs: [],
2264 static_executable: true,
2265 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002266 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002267 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002268 `)
2269
Sundong Ahnabb64432019-10-22 13:58:29 +09002270 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002271 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2272
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002273 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002274 ensureListContains(t, dirs, "etc")
2275 ensureListContains(t, dirs, "etc/foo")
2276 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002277 ensureListContains(t, dirs, "lib64")
2278 ensureListContains(t, dirs, "lib64/foo")
2279 ensureListContains(t, dirs, "lib64/foo/bar")
2280 ensureListContains(t, dirs, "lib")
2281 ensureListContains(t, dirs, "lib/foo")
2282 ensureListContains(t, dirs, "lib/foo/bar")
2283
Jiyong Parkbd13e442019-03-15 18:10:35 +09002284 ensureListContains(t, dirs, "bin")
2285 ensureListContains(t, dirs, "bin/foo")
2286 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002287}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002288
Jooyung Han35155c42020-02-06 17:33:20 +09002289func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002290 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002291 apex {
2292 name: "myapex",
2293 key: "myapex.key",
2294 multilib: {
2295 both: {
2296 native_shared_libs: ["mylib"],
2297 binaries: ["mybin"],
2298 },
2299 },
2300 compile_multilib: "both",
2301 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002302 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002303 }
2304
2305 apex_key {
2306 name: "myapex.key",
2307 public_key: "testkey.avbpubkey",
2308 private_key: "testkey.pem",
2309 }
2310
2311 cc_library {
2312 name: "mylib",
2313 relative_install_path: "foo/bar",
2314 system_shared_libs: [],
2315 stl: "none",
2316 apex_available: [ "myapex" ],
2317 native_bridge_supported: true,
2318 }
2319
2320 cc_binary {
2321 name: "mybin",
2322 relative_install_path: "foo/bar",
2323 system_shared_libs: [],
2324 static_executable: true,
2325 stl: "none",
2326 apex_available: [ "myapex" ],
2327 native_bridge_supported: true,
2328 compile_multilib: "both", // default is "first" for binary
2329 multilib: {
2330 lib64: {
2331 suffix: "64",
2332 },
2333 },
2334 }
2335 `, withNativeBridgeEnabled)
2336 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2337 "bin/foo/bar/mybin",
2338 "bin/foo/bar/mybin64",
2339 "bin/arm/foo/bar/mybin",
2340 "bin/arm64/foo/bar/mybin64",
2341 "lib/foo/bar/mylib.so",
2342 "lib/arm/foo/bar/mylib.so",
2343 "lib64/foo/bar/mylib.so",
2344 "lib64/arm64/foo/bar/mylib.so",
2345 })
2346}
2347
Jiyong Parkda6eb592018-12-19 17:12:36 +09002348func TestUseVendor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002349 ctx := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002350 apex {
2351 name: "myapex",
2352 key: "myapex.key",
2353 native_shared_libs: ["mylib"],
2354 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002355 updatable: false,
Jiyong Parkda6eb592018-12-19 17:12:36 +09002356 }
2357
2358 apex_key {
2359 name: "myapex.key",
2360 public_key: "testkey.avbpubkey",
2361 private_key: "testkey.pem",
2362 }
2363
2364 cc_library {
2365 name: "mylib",
2366 srcs: ["mylib.cpp"],
2367 shared_libs: ["mylib2"],
2368 system_shared_libs: [],
2369 vendor_available: true,
2370 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002371 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002372 }
2373
2374 cc_library {
2375 name: "mylib2",
2376 srcs: ["mylib.cpp"],
2377 system_shared_libs: [],
2378 vendor_available: true,
2379 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002380 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002381 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002382 `,
2383 setUseVendorAllowListForTest([]string{"myapex"}),
2384 )
Jiyong Parkda6eb592018-12-19 17:12:36 +09002385
2386 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002387 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002388 for _, implicit := range i.Implicits {
2389 inputsList = append(inputsList, implicit.String())
2390 }
2391 }
2392 inputsString := strings.Join(inputsList, " ")
2393
2394 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossaede88c2020-08-11 12:17:01 -07002395 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib.so")
2396 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002397
2398 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002399 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2400 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002401}
Jiyong Park16e91a02018-12-20 18:18:08 +09002402
Jooyung Han85d61762020-06-24 23:50:26 +09002403func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002404 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2405 apex {
2406 name: "myapex",
2407 key: "myapex.key",
2408 use_vendor: true,
2409 }
2410 apex_key {
2411 name: "myapex.key",
2412 public_key: "testkey.avbpubkey",
2413 private_key: "testkey.pem",
2414 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002415 `,
2416 setUseVendorAllowListForTest([]string{""}),
2417 )
Colin Cross440e0d02020-06-11 11:32:11 -07002418 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002419 testApex(t, `
2420 apex {
2421 name: "myapex",
2422 key: "myapex.key",
2423 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002424 updatable: false,
Jooyung Handc782442019-11-01 03:14:38 +09002425 }
2426 apex_key {
2427 name: "myapex.key",
2428 public_key: "testkey.avbpubkey",
2429 private_key: "testkey.pem",
2430 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002431 `,
2432 setUseVendorAllowListForTest([]string{"myapex"}),
2433 )
Jooyung Handc782442019-11-01 03:14:38 +09002434}
2435
Jooyung Han5c998b92019-06-27 11:30:33 +09002436func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2437 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2438 apex {
2439 name: "myapex",
2440 key: "myapex.key",
2441 native_shared_libs: ["mylib"],
2442 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002443 updatable: false,
Jooyung Han5c998b92019-06-27 11:30:33 +09002444 }
2445
2446 apex_key {
2447 name: "myapex.key",
2448 public_key: "testkey.avbpubkey",
2449 private_key: "testkey.pem",
2450 }
2451
2452 cc_library {
2453 name: "mylib",
2454 srcs: ["mylib.cpp"],
2455 system_shared_libs: [],
2456 stl: "none",
2457 }
2458 `)
2459}
2460
Jooyung Han85d61762020-06-24 23:50:26 +09002461func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002462 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002463 apex {
2464 name: "myapex",
2465 key: "myapex.key",
2466 binaries: ["mybin"],
2467 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002468 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002469 }
2470 apex_key {
2471 name: "myapex.key",
2472 public_key: "testkey.avbpubkey",
2473 private_key: "testkey.pem",
2474 }
2475 cc_binary {
2476 name: "mybin",
2477 vendor: true,
2478 shared_libs: ["libfoo"],
2479 }
2480 cc_library {
2481 name: "libfoo",
2482 proprietary: true,
2483 }
2484 `)
2485
2486 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2487 "bin/mybin",
2488 "lib64/libfoo.so",
2489 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2490 "lib64/libc++.so",
2491 })
2492
2493 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002494 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002495 name := apexBundle.BaseModuleName()
2496 prefix := "TARGET_"
2497 var builder strings.Builder
2498 data.Custom(&builder, name, prefix, "", data)
2499 androidMk := builder.String()
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002500 installPath := path.Join(buildDir, "../target/product/test_device/vendor/apex")
2501 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002502
2503 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2504 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2505 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002506}
2507
Jooyung Handf78e212020-07-22 15:54:47 +09002508func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002509 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002510 apex {
2511 name: "myapex",
2512 key: "myapex.key",
2513 binaries: ["mybin"],
2514 vendor: true,
2515 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002516 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002517 }
2518 apex_key {
2519 name: "myapex.key",
2520 public_key: "testkey.avbpubkey",
2521 private_key: "testkey.pem",
2522 }
2523 cc_binary {
2524 name: "mybin",
2525 vendor: true,
2526 shared_libs: ["libvndk", "libvendor"],
2527 }
2528 cc_library {
2529 name: "libvndk",
2530 vndk: {
2531 enabled: true,
2532 },
2533 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002534 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002535 }
2536 cc_library {
2537 name: "libvendor",
2538 vendor: true,
2539 }
2540 `)
2541
2542 vendorVariant := "android_vendor.VER_arm64_armv8-a"
2543
Colin Crossaede88c2020-08-11 12:17:01 -07002544 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002545 libs := names(ldRule.Args["libFlags"])
2546 // VNDK libs(libvndk/libc++) as they are
2547 ensureListContains(t, libs, buildDir+"/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
Paul Duffine05480a2021-03-08 15:07:14 +00002548 ensureListContains(t, libs, buildDir+"/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002549 // non-stable Vendor libs as APEX variants
Colin Crossaede88c2020-08-11 12:17:01 -07002550 ensureListContains(t, libs, buildDir+"/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002551
2552 // VNDK libs are not included when use_vndk_as_stable: true
2553 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2554 "bin/mybin",
2555 "lib64/libvendor.so",
2556 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002557
2558 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2559 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2560 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002561}
2562
Justin Yun13decfb2021-03-08 19:25:55 +09002563func TestProductVariant(t *testing.T) {
2564 ctx := testApex(t, `
2565 apex {
2566 name: "myapex",
2567 key: "myapex.key",
2568 updatable: false,
2569 product_specific: true,
2570 binaries: ["foo"],
2571 }
2572
2573 apex_key {
2574 name: "myapex.key",
2575 public_key: "testkey.avbpubkey",
2576 private_key: "testkey.pem",
2577 }
2578
2579 cc_binary {
2580 name: "foo",
2581 product_available: true,
2582 apex_available: ["myapex"],
2583 srcs: ["foo.cpp"],
2584 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002585 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2586 variables.ProductVndkVersion = proptools.StringPtr("current")
2587 }),
2588 )
Justin Yun13decfb2021-03-08 19:25:55 +09002589
2590 cflags := strings.Fields(
2591 ctx.ModuleForTests("foo", "android_product.VER_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
2592 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2593 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2594 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2595 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2596}
2597
Jooyung Han8e5685d2020-09-21 11:02:57 +09002598func TestApex_withPrebuiltFirmware(t *testing.T) {
2599 testCases := []struct {
2600 name string
2601 additionalProp string
2602 }{
2603 {"system apex with prebuilt_firmware", ""},
2604 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2605 }
2606 for _, tc := range testCases {
2607 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002608 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002609 apex {
2610 name: "myapex",
2611 key: "myapex.key",
2612 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002613 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002614 `+tc.additionalProp+`
2615 }
2616 apex_key {
2617 name: "myapex.key",
2618 public_key: "testkey.avbpubkey",
2619 private_key: "testkey.pem",
2620 }
2621 prebuilt_firmware {
2622 name: "myfirmware",
2623 src: "myfirmware.bin",
2624 filename_from_src: true,
2625 `+tc.additionalProp+`
2626 }
2627 `)
2628 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2629 "etc/firmware/myfirmware.bin",
2630 })
2631 })
2632 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002633}
2634
Jooyung Hanefb184e2020-06-25 17:14:25 +09002635func TestAndroidMk_UseVendorRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002636 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002637 apex {
2638 name: "myapex",
2639 key: "myapex.key",
2640 use_vendor: true,
2641 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002642 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002643 }
2644
2645 apex_key {
2646 name: "myapex.key",
2647 public_key: "testkey.avbpubkey",
2648 private_key: "testkey.pem",
2649 }
2650
2651 cc_library {
2652 name: "mylib",
2653 vendor_available: true,
2654 apex_available: ["myapex"],
2655 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002656 `,
2657 setUseVendorAllowListForTest([]string{"myapex"}),
2658 )
Jooyung Hanefb184e2020-06-25 17:14:25 +09002659
2660 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002661 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002662 name := apexBundle.BaseModuleName()
2663 prefix := "TARGET_"
2664 var builder strings.Builder
2665 data.Custom(&builder, name, prefix, "", data)
2666 androidMk := builder.String()
2667 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2668}
2669
2670func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002671 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002672 apex {
2673 name: "myapex",
2674 key: "myapex.key",
2675 vendor: true,
2676 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002677 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002678 }
2679
2680 apex_key {
2681 name: "myapex.key",
2682 public_key: "testkey.avbpubkey",
2683 private_key: "testkey.pem",
2684 }
2685
2686 cc_library {
2687 name: "mylib",
2688 vendor_available: true,
2689 }
2690 `)
2691
2692 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002693 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002694 name := apexBundle.BaseModuleName()
2695 prefix := "TARGET_"
2696 var builder strings.Builder
2697 data.Custom(&builder, name, prefix, "", data)
2698 androidMk := builder.String()
2699 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2700}
2701
Jooyung Han2ed99d02020-06-24 23:26:26 +09002702func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002703 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002704 apex {
2705 name: "myapex",
2706 key: "myapex.key",
2707 vintf_fragments: ["fragment.xml"],
2708 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002709 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002710 }
2711 apex_key {
2712 name: "myapex.key",
2713 public_key: "testkey.avbpubkey",
2714 private_key: "testkey.pem",
2715 }
2716 cc_binary {
2717 name: "mybin",
2718 }
2719 `)
2720
2721 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002722 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002723 name := apexBundle.BaseModuleName()
2724 prefix := "TARGET_"
2725 var builder strings.Builder
2726 data.Custom(&builder, name, prefix, "", data)
2727 androidMk := builder.String()
2728 ensureContains(t, androidMk, "LOCAL_VINTF_FRAGMENTS := fragment.xml\n")
2729 ensureContains(t, androidMk, "LOCAL_INIT_RC := init.rc\n")
2730}
2731
Jiyong Park16e91a02018-12-20 18:18:08 +09002732func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002733 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002734 apex {
2735 name: "myapex",
2736 key: "myapex.key",
2737 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002738 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002739 }
2740
2741 apex_key {
2742 name: "myapex.key",
2743 public_key: "testkey.avbpubkey",
2744 private_key: "testkey.pem",
2745 }
2746
2747 cc_library {
2748 name: "mylib",
2749 srcs: ["mylib.cpp"],
2750 system_shared_libs: [],
2751 stl: "none",
2752 stubs: {
2753 versions: ["1", "2", "3"],
2754 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002755 apex_available: [
2756 "//apex_available:platform",
2757 "myapex",
2758 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002759 }
2760
2761 cc_binary {
2762 name: "not_in_apex",
2763 srcs: ["mylib.cpp"],
2764 static_libs: ["mylib"],
2765 static_executable: true,
2766 system_shared_libs: [],
2767 stl: "none",
2768 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002769 `)
2770
Colin Cross7113d202019-11-20 16:39:12 -08002771 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002772
2773 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002774 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002775}
Jiyong Park9335a262018-12-24 11:31:58 +09002776
2777func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002778 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002779 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002780 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002781 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002782 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002783 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002784 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002785 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09002786 }
2787
2788 cc_library {
2789 name: "mylib",
2790 srcs: ["mylib.cpp"],
2791 system_shared_libs: [],
2792 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002793 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002794 }
2795
2796 apex_key {
2797 name: "myapex.key",
2798 public_key: "testkey.avbpubkey",
2799 private_key: "testkey.pem",
2800 }
2801
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002802 android_app_certificate {
2803 name: "myapex.certificate",
2804 certificate: "testkey",
2805 }
2806
2807 android_app_certificate {
2808 name: "myapex.certificate.override",
2809 certificate: "testkey.override",
2810 }
2811
Jiyong Park9335a262018-12-24 11:31:58 +09002812 `)
2813
2814 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002815 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002816
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002817 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2818 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002819 "vendor/foo/devkeys/testkey.avbpubkey")
2820 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002821 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2822 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002823 "vendor/foo/devkeys/testkey.pem")
2824 }
2825
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002826 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002827 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002828 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002829 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002830 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002831 }
2832}
Jiyong Park58e364a2019-01-19 19:24:06 +09002833
Jooyung Hanf121a652019-12-17 14:30:11 +09002834func TestCertificate(t *testing.T) {
2835 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002836 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002837 apex {
2838 name: "myapex",
2839 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002840 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002841 }
2842 apex_key {
2843 name: "myapex.key",
2844 public_key: "testkey.avbpubkey",
2845 private_key: "testkey.pem",
2846 }`)
2847 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2848 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2849 if actual := rule.Args["certificates"]; actual != expected {
2850 t.Errorf("certificates should be %q, not %q", expected, actual)
2851 }
2852 })
2853 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002854 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002855 apex {
2856 name: "myapex_keytest",
2857 key: "myapex.key",
2858 file_contexts: ":myapex-file_contexts",
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 android_app_certificate {
2867 name: "myapex.certificate.override",
2868 certificate: "testkey.override",
2869 }`)
2870 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2871 expected := "testkey.override.x509.pem testkey.override.pk8"
2872 if actual := rule.Args["certificates"]; actual != expected {
2873 t.Errorf("certificates should be %q, not %q", expected, actual)
2874 }
2875 })
2876 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002877 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002878 apex {
2879 name: "myapex",
2880 key: "myapex.key",
2881 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002882 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002883 }
2884 apex_key {
2885 name: "myapex.key",
2886 public_key: "testkey.avbpubkey",
2887 private_key: "testkey.pem",
2888 }
2889 android_app_certificate {
2890 name: "myapex.certificate",
2891 certificate: "testkey",
2892 }`)
2893 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2894 expected := "testkey.x509.pem testkey.pk8"
2895 if actual := rule.Args["certificates"]; actual != expected {
2896 t.Errorf("certificates should be %q, not %q", expected, actual)
2897 }
2898 })
2899 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002900 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002901 apex {
2902 name: "myapex_keytest",
2903 key: "myapex.key",
2904 file_contexts: ":myapex-file_contexts",
2905 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002906 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002907 }
2908 apex_key {
2909 name: "myapex.key",
2910 public_key: "testkey.avbpubkey",
2911 private_key: "testkey.pem",
2912 }
2913 android_app_certificate {
2914 name: "myapex.certificate.override",
2915 certificate: "testkey.override",
2916 }`)
2917 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2918 expected := "testkey.override.x509.pem testkey.override.pk8"
2919 if actual := rule.Args["certificates"]; actual != expected {
2920 t.Errorf("certificates should be %q, not %q", expected, actual)
2921 }
2922 })
2923 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002924 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002925 apex {
2926 name: "myapex",
2927 key: "myapex.key",
2928 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002929 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002930 }
2931 apex_key {
2932 name: "myapex.key",
2933 public_key: "testkey.avbpubkey",
2934 private_key: "testkey.pem",
2935 }`)
2936 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2937 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.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("override when specified as <name>", 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_keytest",
2946 key: "myapex.key",
2947 file_contexts: ":myapex-file_contexts",
2948 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002949 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002950 }
2951 apex_key {
2952 name: "myapex.key",
2953 public_key: "testkey.avbpubkey",
2954 private_key: "testkey.pem",
2955 }
2956 android_app_certificate {
2957 name: "myapex.certificate.override",
2958 certificate: "testkey.override",
2959 }`)
2960 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2961 expected := "testkey.override.x509.pem testkey.override.pk8"
2962 if actual := rule.Args["certificates"]; actual != expected {
2963 t.Errorf("certificates should be %q, not %q", expected, actual)
2964 }
2965 })
2966}
2967
Jiyong Park58e364a2019-01-19 19:24:06 +09002968func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002969 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002970 apex {
2971 name: "myapex",
2972 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002973 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002974 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09002975 }
2976
2977 apex {
2978 name: "otherapex",
2979 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002980 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002981 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002982 }
2983
2984 apex_key {
2985 name: "myapex.key",
2986 public_key: "testkey.avbpubkey",
2987 private_key: "testkey.pem",
2988 }
2989
2990 cc_library {
2991 name: "mylib",
2992 srcs: ["mylib.cpp"],
2993 system_shared_libs: [],
2994 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002995 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002996 "myapex",
2997 "otherapex",
2998 ],
Jooyung Han24282772020-03-21 23:20:55 +09002999 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003000 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003001 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003002 cc_library {
3003 name: "mylib2",
3004 srcs: ["mylib.cpp"],
3005 system_shared_libs: [],
3006 stl: "none",
3007 apex_available: [
3008 "myapex",
3009 "otherapex",
3010 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003011 static_libs: ["mylib3"],
3012 recovery_available: true,
3013 min_sdk_version: "29",
3014 }
3015 cc_library {
3016 name: "mylib3",
3017 srcs: ["mylib.cpp"],
3018 system_shared_libs: [],
3019 stl: "none",
3020 apex_available: [
3021 "myapex",
3022 "otherapex",
3023 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09003024 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07003025 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003026 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003027 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003028 `)
3029
Jooyung Hanc87a0592020-03-02 17:44:33 +09003030 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003031 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003032 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003033 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003034
Jooyung Hanccce2f22020-03-07 03:45:53 +09003035 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003036 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003037 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003038 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09003039 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003040
Jooyung Hanccce2f22020-03-07 03:45:53 +09003041 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003042 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003043 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003044 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09003045 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003046
Colin Crossaede88c2020-08-11 12:17:01 -07003047 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3048 // each variant defines additional macros to distinguish which apex variant it is built for
3049
3050 // non-APEX variant does not have __ANDROID_APEX__ defined
3051 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3052 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3053
3054 // APEX variant has __ANDROID_APEX__ defined
3055 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
3056 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3057 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3058 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3059
3060 // APEX variant has __ANDROID_APEX__ defined
3061 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
3062 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3063 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3064 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3065
Dan Albertb19953d2020-11-17 15:29:36 -08003066 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003067 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3068 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003069 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003070
3071 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
3072 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09003073
3074 // non-APEX variant does not have __ANDROID_APEX__ defined
3075 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3076 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3077
3078 // APEX variant has __ANDROID_APEX__ defined
3079 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003080 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003081 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003082 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003083
Jooyung Hanc87a0592020-03-02 17:44:33 +09003084 // APEX variant has __ANDROID_APEX__ defined
3085 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003086 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003087 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003088 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09003089
Dan Albertb19953d2020-11-17 15:29:36 -08003090 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003091 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003092 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003093 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003094}
Jiyong Park7e636d02019-01-28 16:16:54 +09003095
3096func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003097 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003098 apex {
3099 name: "myapex",
3100 key: "myapex.key",
3101 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003102 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003103 }
3104
3105 apex_key {
3106 name: "myapex.key",
3107 public_key: "testkey.avbpubkey",
3108 private_key: "testkey.pem",
3109 }
3110
3111 cc_library_headers {
3112 name: "mylib_headers",
3113 export_include_dirs: ["my_include"],
3114 system_shared_libs: [],
3115 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003116 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003117 }
3118
3119 cc_library {
3120 name: "mylib",
3121 srcs: ["mylib.cpp"],
3122 system_shared_libs: [],
3123 stl: "none",
3124 header_libs: ["mylib_headers"],
3125 export_header_lib_headers: ["mylib_headers"],
3126 stubs: {
3127 versions: ["1", "2", "3"],
3128 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003129 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003130 }
3131
3132 cc_library {
3133 name: "otherlib",
3134 srcs: ["mylib.cpp"],
3135 system_shared_libs: [],
3136 stl: "none",
3137 shared_libs: ["mylib"],
3138 }
3139 `)
3140
Colin Cross7113d202019-11-20 16:39:12 -08003141 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003142
3143 // Ensure that the include path of the header lib is exported to 'otherlib'
3144 ensureContains(t, cFlags, "-Imy_include")
3145}
Alex Light9670d332019-01-29 18:07:33 -08003146
Jiyong Park7cd10e32020-01-14 09:22:18 +09003147type fileInApex struct {
3148 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003149 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003150 isLink bool
3151}
3152
Jooyung Hana57af4a2020-01-23 05:36:59 +00003153func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003154 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003155 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003156 copyCmds := apexRule.Args["copy_commands"]
3157 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003158 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003159 for _, cmd := range strings.Split(copyCmds, "&&") {
3160 cmd = strings.TrimSpace(cmd)
3161 if cmd == "" {
3162 continue
3163 }
3164 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003165 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003166 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003167 switch terms[0] {
3168 case "mkdir":
3169 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003170 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003171 t.Fatal("copyCmds contains invalid cp command", cmd)
3172 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003173 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003174 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003175 isLink = false
3176 case "ln":
3177 if len(terms) != 3 && len(terms) != 4 {
3178 // ln LINK TARGET or ln -s LINK TARGET
3179 t.Fatal("copyCmds contains invalid ln command", cmd)
3180 }
3181 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003182 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003183 isLink = true
3184 default:
3185 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3186 }
3187 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003188 index := strings.Index(dst, imageApexDir)
3189 if index == -1 {
3190 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3191 }
3192 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003193 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003194 }
3195 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003196 return ret
3197}
3198
Jooyung Hana57af4a2020-01-23 05:36:59 +00003199func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3200 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003201 var failed bool
3202 var surplus []string
3203 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003204 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003205 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003206 for _, expected := range files {
3207 if matched, _ := path.Match(expected, file.path); matched {
3208 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003209 mactchFound = true
3210 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003211 }
3212 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003213 if !mactchFound {
3214 surplus = append(surplus, file.path)
3215 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003216 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003217
Jooyung Han31c470b2019-10-18 16:26:59 +09003218 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003219 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003220 t.Log("surplus files", surplus)
3221 failed = true
3222 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003223
3224 if len(files) > len(filesMatched) {
3225 var missing []string
3226 for _, expected := range files {
3227 if !filesMatched[expected] {
3228 missing = append(missing, expected)
3229 }
3230 }
3231 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003232 t.Log("missing files", missing)
3233 failed = true
3234 }
3235 if failed {
3236 t.Fail()
3237 }
3238}
3239
Jooyung Han344d5432019-08-23 11:17:39 +09003240func TestVndkApexCurrent(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003241 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003242 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003243 name: "com.android.vndk.current",
3244 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003245 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003246 }
3247
3248 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003249 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003250 public_key: "testkey.avbpubkey",
3251 private_key: "testkey.pem",
3252 }
3253
3254 cc_library {
3255 name: "libvndk",
3256 srcs: ["mylib.cpp"],
3257 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003258 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003259 vndk: {
3260 enabled: true,
3261 },
3262 system_shared_libs: [],
3263 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003264 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003265 }
3266
3267 cc_library {
3268 name: "libvndksp",
3269 srcs: ["mylib.cpp"],
3270 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003271 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003272 vndk: {
3273 enabled: true,
3274 support_system_process: true,
3275 },
3276 system_shared_libs: [],
3277 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003278 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003279 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003280 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003281
Colin Cross2807f002021-03-02 10:15:29 -08003282 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003283 "lib/libvndk.so",
3284 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003285 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003286 "lib64/libvndk.so",
3287 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003288 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003289 "etc/llndk.libraries.VER.txt",
3290 "etc/vndkcore.libraries.VER.txt",
3291 "etc/vndksp.libraries.VER.txt",
3292 "etc/vndkprivate.libraries.VER.txt",
Justin Yun8a2600c2020-12-07 12:44:03 +09003293 "etc/vndkproduct.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003294 })
Jooyung Han344d5432019-08-23 11:17:39 +09003295}
3296
3297func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003298 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003299 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003300 name: "com.android.vndk.current",
3301 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003302 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003303 }
3304
3305 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003306 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003307 public_key: "testkey.avbpubkey",
3308 private_key: "testkey.pem",
3309 }
3310
3311 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003312 name: "libvndk",
3313 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003314 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003315 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003316 vndk: {
3317 enabled: true,
3318 },
3319 system_shared_libs: [],
3320 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003321 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003322 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003323
3324 cc_prebuilt_library_shared {
3325 name: "libvndk.arm",
3326 srcs: ["libvndk.arm.so"],
3327 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003328 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003329 vndk: {
3330 enabled: true,
3331 },
3332 enabled: false,
3333 arch: {
3334 arm: {
3335 enabled: true,
3336 },
3337 },
3338 system_shared_libs: [],
3339 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003340 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003341 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003342 `+vndkLibrariesTxtFiles("current"),
3343 withFiles(map[string][]byte{
3344 "libvndk.so": nil,
3345 "libvndk.arm.so": nil,
3346 }))
Colin Cross2807f002021-03-02 10:15:29 -08003347 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003348 "lib/libvndk.so",
3349 "lib/libvndk.arm.so",
3350 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003351 "lib/libc++.so",
3352 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003353 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003354 })
Jooyung Han344d5432019-08-23 11:17:39 +09003355}
3356
Jooyung Han39edb6c2019-11-06 16:53:07 +09003357func vndkLibrariesTxtFiles(vers ...string) (result string) {
3358 for _, v := range vers {
3359 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003360 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003361 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003362 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003363 name: "` + txt + `.libraries.txt",
3364 }
3365 `
3366 }
3367 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003368 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003369 result += `
3370 prebuilt_etc {
3371 name: "` + txt + `.libraries.` + v + `.txt",
3372 src: "dummy.txt",
3373 }
3374 `
3375 }
3376 }
3377 }
3378 return
3379}
3380
Jooyung Han344d5432019-08-23 11:17:39 +09003381func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003382 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003383 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003384 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003385 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003386 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003387 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003388 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003389 }
3390
3391 apex_key {
3392 name: "myapex.key",
3393 public_key: "testkey.avbpubkey",
3394 private_key: "testkey.pem",
3395 }
3396
Jooyung Han31c470b2019-10-18 16:26:59 +09003397 vndk_prebuilt_shared {
3398 name: "libvndk27",
3399 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003400 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003401 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003402 vndk: {
3403 enabled: true,
3404 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003405 target_arch: "arm64",
3406 arch: {
3407 arm: {
3408 srcs: ["libvndk27_arm.so"],
3409 },
3410 arm64: {
3411 srcs: ["libvndk27_arm64.so"],
3412 },
3413 },
Colin Cross2807f002021-03-02 10:15:29 -08003414 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003415 }
3416
3417 vndk_prebuilt_shared {
3418 name: "libvndk27",
3419 version: "27",
3420 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003421 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003422 vndk: {
3423 enabled: true,
3424 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003425 target_arch: "x86_64",
3426 arch: {
3427 x86: {
3428 srcs: ["libvndk27_x86.so"],
3429 },
3430 x86_64: {
3431 srcs: ["libvndk27_x86_64.so"],
3432 },
3433 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003434 }
3435 `+vndkLibrariesTxtFiles("27"),
3436 withFiles(map[string][]byte{
3437 "libvndk27_arm.so": nil,
3438 "libvndk27_arm64.so": nil,
3439 "libvndk27_x86.so": nil,
3440 "libvndk27_x86_64.so": nil,
3441 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003442
Colin Cross2807f002021-03-02 10:15:29 -08003443 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003444 "lib/libvndk27_arm.so",
3445 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003446 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003447 })
Jooyung Han344d5432019-08-23 11:17:39 +09003448}
3449
Jooyung Han90eee022019-10-01 20:02:42 +09003450func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003451 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003452 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003453 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003454 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003455 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003456 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003457 }
3458 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003459 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003460 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003461 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003462 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003463 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003464 }
3465 apex_key {
3466 name: "myapex.key",
3467 public_key: "testkey.avbpubkey",
3468 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003469 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003470
3471 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003472 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003473 actual := proptools.String(bundle.properties.Apex_name)
3474 if !reflect.DeepEqual(actual, expected) {
3475 t.Errorf("Got '%v', expected '%v'", actual, expected)
3476 }
3477 }
3478
Colin Cross2807f002021-03-02 10:15:29 -08003479 assertApexName("com.android.vndk.vVER", "com.android.vndk.current")
3480 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003481}
3482
Jooyung Han344d5432019-08-23 11:17:39 +09003483func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003484 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003485 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003486 name: "com.android.vndk.current",
3487 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003488 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003489 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003490 }
3491
3492 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003493 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003494 public_key: "testkey.avbpubkey",
3495 private_key: "testkey.pem",
3496 }
3497
3498 cc_library {
3499 name: "libvndk",
3500 srcs: ["mylib.cpp"],
3501 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003502 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003503 native_bridge_supported: true,
3504 host_supported: true,
3505 vndk: {
3506 enabled: true,
3507 },
3508 system_shared_libs: [],
3509 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003510 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003511 }
Colin Cross2807f002021-03-02 10:15:29 -08003512 `+vndkLibrariesTxtFiles("current"),
3513 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003514
Colin Cross2807f002021-03-02 10:15:29 -08003515 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003516 "lib/libvndk.so",
3517 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003518 "lib/libc++.so",
3519 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003520 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003521 })
Jooyung Han344d5432019-08-23 11:17:39 +09003522}
3523
3524func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003525 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003526 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003527 name: "com.android.vndk.current",
3528 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003529 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003530 native_bridge_supported: true,
3531 }
3532
3533 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003534 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003535 public_key: "testkey.avbpubkey",
3536 private_key: "testkey.pem",
3537 }
3538
3539 cc_library {
3540 name: "libvndk",
3541 srcs: ["mylib.cpp"],
3542 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003543 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003544 native_bridge_supported: true,
3545 host_supported: true,
3546 vndk: {
3547 enabled: true,
3548 },
3549 system_shared_libs: [],
3550 stl: "none",
3551 }
3552 `)
3553}
3554
Jooyung Han31c470b2019-10-18 16:26:59 +09003555func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003556 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003557 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003558 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003559 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003560 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003561 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003562 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003563 }
3564
3565 apex_key {
3566 name: "myapex.key",
3567 public_key: "testkey.avbpubkey",
3568 private_key: "testkey.pem",
3569 }
3570
3571 vndk_prebuilt_shared {
3572 name: "libvndk27",
3573 version: "27",
3574 target_arch: "arm",
3575 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003576 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003577 vndk: {
3578 enabled: true,
3579 },
3580 arch: {
3581 arm: {
3582 srcs: ["libvndk27.so"],
3583 }
3584 },
3585 }
3586
3587 vndk_prebuilt_shared {
3588 name: "libvndk27",
3589 version: "27",
3590 target_arch: "arm",
3591 binder32bit: true,
3592 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003593 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003594 vndk: {
3595 enabled: true,
3596 },
3597 arch: {
3598 arm: {
3599 srcs: ["libvndk27binder32.so"],
3600 }
3601 },
Colin Cross2807f002021-03-02 10:15:29 -08003602 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003603 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003604 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003605 withFiles(map[string][]byte{
3606 "libvndk27.so": nil,
3607 "libvndk27binder32.so": nil,
3608 }),
3609 withBinder32bit,
3610 withTargets(map[android.OsType][]android.Target{
3611 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003612 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3613 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003614 },
3615 }),
3616 )
3617
Colin Cross2807f002021-03-02 10:15:29 -08003618 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003619 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003620 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003621 })
3622}
3623
Jooyung Han45a96772020-06-15 14:59:42 +09003624func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003625 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003626 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003627 name: "com.android.vndk.current",
3628 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003629 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003630 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003631 }
3632
3633 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003634 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003635 public_key: "testkey.avbpubkey",
3636 private_key: "testkey.pem",
3637 }
3638
3639 cc_library {
3640 name: "libz",
3641 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003642 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003643 vndk: {
3644 enabled: true,
3645 },
3646 stubs: {
3647 symbol_file: "libz.map.txt",
3648 versions: ["30"],
3649 }
3650 }
3651 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3652 "libz.map.txt": nil,
3653 }))
3654
Colin Cross2807f002021-03-02 10:15:29 -08003655 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003656 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3657 ensureListEmpty(t, provideNativeLibs)
3658}
3659
Jooyung Hane1633032019-08-01 17:41:43 +09003660func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003661 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003662 apex {
3663 name: "myapex_nodep",
3664 key: "myapex.key",
3665 native_shared_libs: ["lib_nodep"],
3666 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003667 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003668 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003669 }
3670
3671 apex {
3672 name: "myapex_dep",
3673 key: "myapex.key",
3674 native_shared_libs: ["lib_dep"],
3675 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003676 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003677 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003678 }
3679
3680 apex {
3681 name: "myapex_provider",
3682 key: "myapex.key",
3683 native_shared_libs: ["libfoo"],
3684 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003685 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003686 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003687 }
3688
3689 apex {
3690 name: "myapex_selfcontained",
3691 key: "myapex.key",
3692 native_shared_libs: ["lib_dep", "libfoo"],
3693 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003694 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003695 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003696 }
3697
3698 apex_key {
3699 name: "myapex.key",
3700 public_key: "testkey.avbpubkey",
3701 private_key: "testkey.pem",
3702 }
3703
3704 cc_library {
3705 name: "lib_nodep",
3706 srcs: ["mylib.cpp"],
3707 system_shared_libs: [],
3708 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003709 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003710 }
3711
3712 cc_library {
3713 name: "lib_dep",
3714 srcs: ["mylib.cpp"],
3715 shared_libs: ["libfoo"],
3716 system_shared_libs: [],
3717 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003718 apex_available: [
3719 "myapex_dep",
3720 "myapex_provider",
3721 "myapex_selfcontained",
3722 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003723 }
3724
3725 cc_library {
3726 name: "libfoo",
3727 srcs: ["mytest.cpp"],
3728 stubs: {
3729 versions: ["1"],
3730 },
3731 system_shared_libs: [],
3732 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003733 apex_available: [
3734 "myapex_provider",
3735 "myapex_selfcontained",
3736 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003737 }
3738 `)
3739
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003740 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003741 var provideNativeLibs, requireNativeLibs []string
3742
Sundong Ahnabb64432019-10-22 13:58:29 +09003743 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003744 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3745 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003746 ensureListEmpty(t, provideNativeLibs)
3747 ensureListEmpty(t, requireNativeLibs)
3748
Sundong Ahnabb64432019-10-22 13:58:29 +09003749 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003750 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3751 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003752 ensureListEmpty(t, provideNativeLibs)
3753 ensureListContains(t, requireNativeLibs, "libfoo.so")
3754
Sundong Ahnabb64432019-10-22 13:58:29 +09003755 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003756 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3757 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003758 ensureListContains(t, provideNativeLibs, "libfoo.so")
3759 ensureListEmpty(t, requireNativeLibs)
3760
Sundong Ahnabb64432019-10-22 13:58:29 +09003761 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003762 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3763 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003764 ensureListContains(t, provideNativeLibs, "libfoo.so")
3765 ensureListEmpty(t, requireNativeLibs)
3766}
3767
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003768func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003769 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003770 apex {
3771 name: "myapex",
3772 key: "myapex.key",
3773 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003774 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003775 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003776 }
3777
3778 apex_key {
3779 name: "myapex.key",
3780 public_key: "testkey.avbpubkey",
3781 private_key: "testkey.pem",
3782 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003783
3784 cc_library {
3785 name: "mylib",
3786 srcs: ["mylib.cpp"],
3787 system_shared_libs: [],
3788 stl: "none",
3789 apex_available: [
3790 "//apex_available:platform",
3791 "myapex",
3792 ],
3793 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003794 `)
3795
Sundong Ahnabb64432019-10-22 13:58:29 +09003796 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003797 apexManifestRule := module.Rule("apexManifestRule")
3798 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3799 apexRule := module.Rule("apexRule")
3800 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003801
3802 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003803 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09003804 name := apexBundle.BaseModuleName()
3805 prefix := "TARGET_"
3806 var builder strings.Builder
3807 data.Custom(&builder, name, prefix, "", data)
3808 androidMk := builder.String()
3809 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3810 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003811}
3812
Alex Light0851b882019-02-07 13:20:53 -08003813func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003814 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003815 apex {
3816 name: "myapex",
3817 key: "myapex.key",
3818 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003819 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003820 }
3821
3822 apex_key {
3823 name: "myapex.key",
3824 public_key: "testkey.avbpubkey",
3825 private_key: "testkey.pem",
3826 }
3827
3828 cc_library {
3829 name: "mylib_common",
3830 srcs: ["mylib.cpp"],
3831 system_shared_libs: [],
3832 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003833 apex_available: [
3834 "//apex_available:platform",
3835 "myapex",
3836 ],
Alex Light0851b882019-02-07 13:20:53 -08003837 }
3838 `)
3839
Sundong Ahnabb64432019-10-22 13:58:29 +09003840 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003841 apexRule := module.Rule("apexRule")
3842 copyCmds := apexRule.Args["copy_commands"]
3843
3844 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3845 t.Log("Apex was a test apex!")
3846 t.Fail()
3847 }
3848 // Ensure that main rule creates an output
3849 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3850
3851 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003852 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003853
3854 // Ensure that both direct and indirect deps are copied into apex
3855 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3856
Colin Cross7113d202019-11-20 16:39:12 -08003857 // Ensure that the platform variant ends with _shared
3858 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003859
Colin Cross56a83212020-09-15 18:30:11 -07003860 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003861 t.Log("Found mylib_common not in any apex!")
3862 t.Fail()
3863 }
3864}
3865
3866func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003867 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003868 apex_test {
3869 name: "myapex",
3870 key: "myapex.key",
3871 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003872 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003873 }
3874
3875 apex_key {
3876 name: "myapex.key",
3877 public_key: "testkey.avbpubkey",
3878 private_key: "testkey.pem",
3879 }
3880
3881 cc_library {
3882 name: "mylib_common_test",
3883 srcs: ["mylib.cpp"],
3884 system_shared_libs: [],
3885 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003886 // TODO: remove //apex_available:platform
3887 apex_available: [
3888 "//apex_available:platform",
3889 "myapex",
3890 ],
Alex Light0851b882019-02-07 13:20:53 -08003891 }
3892 `)
3893
Sundong Ahnabb64432019-10-22 13:58:29 +09003894 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003895 apexRule := module.Rule("apexRule")
3896 copyCmds := apexRule.Args["copy_commands"]
3897
3898 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3899 t.Log("Apex was not a test apex!")
3900 t.Fail()
3901 }
3902 // Ensure that main rule creates an output
3903 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3904
3905 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003906 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003907
3908 // Ensure that both direct and indirect deps are copied into apex
3909 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3910
Colin Cross7113d202019-11-20 16:39:12 -08003911 // Ensure that the platform variant ends with _shared
3912 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003913}
3914
Alex Light9670d332019-01-29 18:07:33 -08003915func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003916 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003917 apex {
3918 name: "myapex",
3919 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003920 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08003921 multilib: {
3922 first: {
3923 native_shared_libs: ["mylib_common"],
3924 }
3925 },
3926 target: {
3927 android: {
3928 multilib: {
3929 first: {
3930 native_shared_libs: ["mylib"],
3931 }
3932 }
3933 },
3934 host: {
3935 multilib: {
3936 first: {
3937 native_shared_libs: ["mylib2"],
3938 }
3939 }
3940 }
3941 }
3942 }
3943
3944 apex_key {
3945 name: "myapex.key",
3946 public_key: "testkey.avbpubkey",
3947 private_key: "testkey.pem",
3948 }
3949
3950 cc_library {
3951 name: "mylib",
3952 srcs: ["mylib.cpp"],
3953 system_shared_libs: [],
3954 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003955 // TODO: remove //apex_available:platform
3956 apex_available: [
3957 "//apex_available:platform",
3958 "myapex",
3959 ],
Alex Light9670d332019-01-29 18:07:33 -08003960 }
3961
3962 cc_library {
3963 name: "mylib_common",
3964 srcs: ["mylib.cpp"],
3965 system_shared_libs: [],
3966 stl: "none",
3967 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003968 // TODO: remove //apex_available:platform
3969 apex_available: [
3970 "//apex_available:platform",
3971 "myapex",
3972 ],
Alex Light9670d332019-01-29 18:07:33 -08003973 }
3974
3975 cc_library {
3976 name: "mylib2",
3977 srcs: ["mylib.cpp"],
3978 system_shared_libs: [],
3979 stl: "none",
3980 compile_multilib: "first",
3981 }
3982 `)
3983
Sundong Ahnabb64432019-10-22 13:58:29 +09003984 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003985 copyCmds := apexRule.Args["copy_commands"]
3986
3987 // Ensure that main rule creates an output
3988 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3989
3990 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003991 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
3992 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
3993 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08003994
3995 // Ensure that both direct and indirect deps are copied into apex
3996 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3997 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3998 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3999
Colin Cross7113d202019-11-20 16:39:12 -08004000 // Ensure that the platform variant ends with _shared
4001 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4002 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4003 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004004}
Jiyong Park04480cf2019-02-06 00:16:29 +09004005
Jiyong Park59140302020-12-14 18:44:04 +09004006func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004007 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004008 apex {
4009 name: "myapex",
4010 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004011 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09004012 arch: {
4013 arm64: {
4014 native_shared_libs: ["mylib.arm64"],
4015 },
4016 x86_64: {
4017 native_shared_libs: ["mylib.x64"],
4018 },
4019 }
4020 }
4021
4022 apex_key {
4023 name: "myapex.key",
4024 public_key: "testkey.avbpubkey",
4025 private_key: "testkey.pem",
4026 }
4027
4028 cc_library {
4029 name: "mylib.arm64",
4030 srcs: ["mylib.cpp"],
4031 system_shared_libs: [],
4032 stl: "none",
4033 // TODO: remove //apex_available:platform
4034 apex_available: [
4035 "//apex_available:platform",
4036 "myapex",
4037 ],
4038 }
4039
4040 cc_library {
4041 name: "mylib.x64",
4042 srcs: ["mylib.cpp"],
4043 system_shared_libs: [],
4044 stl: "none",
4045 // TODO: remove //apex_available:platform
4046 apex_available: [
4047 "//apex_available:platform",
4048 "myapex",
4049 ],
4050 }
4051 `)
4052
4053 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4054 copyCmds := apexRule.Args["copy_commands"]
4055
4056 // Ensure that apex variant is created for the direct dep
4057 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4058 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4059
4060 // Ensure that both direct and indirect deps are copied into apex
4061 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4062 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4063}
4064
Jiyong Park04480cf2019-02-06 00:16:29 +09004065func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004066 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004067 apex {
4068 name: "myapex",
4069 key: "myapex.key",
4070 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004071 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004072 }
4073
4074 apex_key {
4075 name: "myapex.key",
4076 public_key: "testkey.avbpubkey",
4077 private_key: "testkey.pem",
4078 }
4079
4080 sh_binary {
4081 name: "myscript",
4082 src: "mylib.cpp",
4083 filename: "myscript.sh",
4084 sub_dir: "script",
4085 }
4086 `)
4087
Sundong Ahnabb64432019-10-22 13:58:29 +09004088 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004089 copyCmds := apexRule.Args["copy_commands"]
4090
4091 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4092}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004093
Jooyung Han91df2082019-11-20 01:49:42 +09004094func TestApexInVariousPartition(t *testing.T) {
4095 testcases := []struct {
4096 propName, parition, flattenedPartition string
4097 }{
4098 {"", "system", "system_ext"},
4099 {"product_specific: true", "product", "product"},
4100 {"soc_specific: true", "vendor", "vendor"},
4101 {"proprietary: true", "vendor", "vendor"},
4102 {"vendor: true", "vendor", "vendor"},
4103 {"system_ext_specific: true", "system_ext", "system_ext"},
4104 }
4105 for _, tc := range testcases {
4106 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004107 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004108 apex {
4109 name: "myapex",
4110 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004111 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004112 `+tc.propName+`
4113 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004114
Jooyung Han91df2082019-11-20 01:49:42 +09004115 apex_key {
4116 name: "myapex.key",
4117 public_key: "testkey.avbpubkey",
4118 private_key: "testkey.pem",
4119 }
4120 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004121
Jooyung Han91df2082019-11-20 01:49:42 +09004122 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
4123 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
4124 actual := apex.installDir.String()
4125 if actual != expected {
4126 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4127 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004128
Jooyung Han91df2082019-11-20 01:49:42 +09004129 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
4130 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
4131 actual = flattened.installDir.String()
4132 if actual != expected {
4133 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4134 }
4135 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004136 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004137}
Jiyong Park67882562019-03-21 01:11:21 +09004138
Jooyung Han580eb4f2020-06-24 19:33:06 +09004139func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004140 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004141 apex {
4142 name: "myapex",
4143 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004144 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004145 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004146
Jooyung Han580eb4f2020-06-24 19:33:06 +09004147 apex_key {
4148 name: "myapex.key",
4149 public_key: "testkey.avbpubkey",
4150 private_key: "testkey.pem",
4151 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004152 `)
4153 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004154 rule := module.Output("file_contexts")
4155 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4156}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004157
Jooyung Han580eb4f2020-06-24 19:33:06 +09004158func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004159 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004160 apex {
4161 name: "myapex",
4162 key: "myapex.key",
4163 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004164 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004165 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004166
Jooyung Han580eb4f2020-06-24 19:33:06 +09004167 apex_key {
4168 name: "myapex.key",
4169 public_key: "testkey.avbpubkey",
4170 private_key: "testkey.pem",
4171 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004172 `, withFiles(map[string][]byte{
4173 "my_own_file_contexts": nil,
4174 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004175}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004176
Jooyung Han580eb4f2020-06-24 19:33:06 +09004177func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004178 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004179 apex {
4180 name: "myapex",
4181 key: "myapex.key",
4182 product_specific: true,
4183 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004184 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004185 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004186
Jooyung Han580eb4f2020-06-24 19:33:06 +09004187 apex_key {
4188 name: "myapex.key",
4189 public_key: "testkey.avbpubkey",
4190 private_key: "testkey.pem",
4191 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004192 `)
4193
Colin Cross1c460562021-02-16 17:55:47 -08004194 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004195 apex {
4196 name: "myapex",
4197 key: "myapex.key",
4198 product_specific: true,
4199 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004200 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004201 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004202
Jooyung Han580eb4f2020-06-24 19:33:06 +09004203 apex_key {
4204 name: "myapex.key",
4205 public_key: "testkey.avbpubkey",
4206 private_key: "testkey.pem",
4207 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004208 `, withFiles(map[string][]byte{
4209 "product_specific_file_contexts": nil,
4210 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004211 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4212 rule := module.Output("file_contexts")
4213 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4214}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004215
Jooyung Han580eb4f2020-06-24 19:33:06 +09004216func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004217 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004218 apex {
4219 name: "myapex",
4220 key: "myapex.key",
4221 product_specific: true,
4222 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004223 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004224 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004225
Jooyung Han580eb4f2020-06-24 19:33:06 +09004226 apex_key {
4227 name: "myapex.key",
4228 public_key: "testkey.avbpubkey",
4229 private_key: "testkey.pem",
4230 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004231
Jooyung Han580eb4f2020-06-24 19:33:06 +09004232 filegroup {
4233 name: "my-file-contexts",
4234 srcs: ["product_specific_file_contexts"],
4235 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004236 `, withFiles(map[string][]byte{
4237 "product_specific_file_contexts": nil,
4238 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004239 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4240 rule := module.Output("file_contexts")
4241 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004242}
4243
Jiyong Park67882562019-03-21 01:11:21 +09004244func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004245 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004246 apex_key {
4247 name: "myapex.key",
4248 public_key: ":my.avbpubkey",
4249 private_key: ":my.pem",
4250 product_specific: true,
4251 }
4252
4253 filegroup {
4254 name: "my.avbpubkey",
4255 srcs: ["testkey2.avbpubkey"],
4256 }
4257
4258 filegroup {
4259 name: "my.pem",
4260 srcs: ["testkey2.pem"],
4261 }
4262 `)
4263
4264 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4265 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004266 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004267 if actual_pubkey != expected_pubkey {
4268 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4269 }
4270 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004271 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004272 if actual_privkey != expected_privkey {
4273 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4274 }
4275}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004276
4277func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004278 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004279 prebuilt_apex {
4280 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004281 arch: {
4282 arm64: {
4283 src: "myapex-arm64.apex",
4284 },
4285 arm: {
4286 src: "myapex-arm.apex",
4287 },
4288 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004289 }
4290 `)
4291
4292 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4293
Jiyong Parkc95714e2019-03-29 14:23:10 +09004294 expectedInput := "myapex-arm64.apex"
4295 if prebuilt.inputApex.String() != expectedInput {
4296 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4297 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004298}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004299
4300func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004301 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004302 prebuilt_apex {
4303 name: "myapex",
4304 src: "myapex-arm.apex",
4305 filename: "notmyapex.apex",
4306 }
4307 `)
4308
4309 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4310
4311 expected := "notmyapex.apex"
4312 if p.installFilename != expected {
4313 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4314 }
4315}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004316
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004317func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004318 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004319 prebuilt_apex {
4320 name: "myapex.prebuilt",
4321 src: "myapex-arm.apex",
4322 overrides: [
4323 "myapex",
4324 ],
4325 }
4326 `)
4327
4328 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
4329
4330 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004331 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004332 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004333 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004334 }
4335}
4336
Paul Duffin092153d2021-01-26 11:42:39 +00004337// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4338// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004339func TestPrebuiltExportDexImplementationJars(t *testing.T) {
4340 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin092153d2021-01-26 11:42:39 +00004341 // Empty transformation.
Paul Duffin064b70c2020-11-02 17:32:38 +00004342 }
4343
Paul Duffin89886cb2021-02-05 16:44:03 +00004344 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004345 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004346 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004347 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004348 stem := android.RemoveOptionalPrebuiltPrefix(name)
4349 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004350 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4351 }
4352 }
4353
Paul Duffin39853512021-02-26 11:09:39 +00004354 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004355 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004356 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004357 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4358 }
4359 }
4360
4361 t.Run("prebuilt only", func(t *testing.T) {
4362 bp := `
4363 prebuilt_apex {
4364 name: "myapex",
4365 arch: {
4366 arm64: {
4367 src: "myapex-arm64.apex",
4368 },
4369 arm: {
4370 src: "myapex-arm.apex",
4371 },
4372 },
Paul Duffin39853512021-02-26 11:09:39 +00004373 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004374 }
4375
4376 java_import {
4377 name: "libfoo",
4378 jars: ["libfoo.jar"],
4379 }
Paul Duffin39853512021-02-26 11:09:39 +00004380
4381 java_sdk_library_import {
4382 name: "libbar",
4383 public: {
4384 jars: ["libbar.jar"],
4385 },
4386 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004387 `
4388
4389 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4390 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4391
Paul Duffinf6932af2021-02-26 18:21:56 +00004392 // Make sure that the deapexer has the correct input APEX.
4393 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4394 rule := deapexer.Rule("deapexer")
4395 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4396 t.Errorf("expected: %q, found: %q", expected, actual)
4397 }
4398
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004399 // Make sure that the prebuilt_apex has the correct input APEX.
4400 prebuiltApex := ctx.ModuleForTests("myapex", "android_common")
4401 rule = prebuiltApex.Rule("android/soong/android.Cp")
4402 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4403 t.Errorf("expected: %q, found: %q", expected, actual)
4404 }
4405
Paul Duffin89886cb2021-02-05 16:44:03 +00004406 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004407
4408 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004409 })
4410
4411 t.Run("prebuilt with source preferred", func(t *testing.T) {
4412
4413 bp := `
4414 prebuilt_apex {
4415 name: "myapex",
4416 arch: {
4417 arm64: {
4418 src: "myapex-arm64.apex",
4419 },
4420 arm: {
4421 src: "myapex-arm.apex",
4422 },
4423 },
Paul Duffin39853512021-02-26 11:09:39 +00004424 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004425 }
4426
4427 java_import {
4428 name: "libfoo",
4429 jars: ["libfoo.jar"],
4430 }
4431
4432 java_library {
4433 name: "libfoo",
4434 }
Paul Duffin39853512021-02-26 11:09:39 +00004435
4436 java_sdk_library_import {
4437 name: "libbar",
4438 public: {
4439 jars: ["libbar.jar"],
4440 },
4441 }
4442
4443 java_sdk_library {
4444 name: "libbar",
4445 srcs: ["foo/bar/MyClass.java"],
4446 unsafe_ignore_missing_latest_api: true,
4447 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004448 `
4449
4450 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4451 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4452
Paul Duffin89886cb2021-02-05 16:44:03 +00004453 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004454 ensureNoSourceVariant(t, ctx, "libfoo")
4455
4456 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4457 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004458 })
4459
4460 t.Run("prebuilt preferred with source", func(t *testing.T) {
4461 bp := `
4462 prebuilt_apex {
4463 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004464 arch: {
4465 arm64: {
4466 src: "myapex-arm64.apex",
4467 },
4468 arm: {
4469 src: "myapex-arm.apex",
4470 },
4471 },
Paul Duffin39853512021-02-26 11:09:39 +00004472 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004473 }
4474
4475 java_import {
4476 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004477 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004478 jars: ["libfoo.jar"],
4479 }
4480
4481 java_library {
4482 name: "libfoo",
4483 }
Paul Duffin39853512021-02-26 11:09:39 +00004484
4485 java_sdk_library_import {
4486 name: "libbar",
4487 prefer: true,
4488 public: {
4489 jars: ["libbar.jar"],
4490 },
4491 }
4492
4493 java_sdk_library {
4494 name: "libbar",
4495 srcs: ["foo/bar/MyClass.java"],
4496 unsafe_ignore_missing_latest_api: true,
4497 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004498 `
4499
4500 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4501 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4502
Paul Duffin89886cb2021-02-05 16:44:03 +00004503 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004504 ensureNoSourceVariant(t, ctx, "libfoo")
4505
4506 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4507 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004508 })
4509}
4510
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004511func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
4512 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin37856732021-02-26 14:24:15 +00004513 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo", "myapex:libbar"})
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004514 }
4515
Paul Duffin37856732021-02-26 14:24:15 +00004516 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4517 t.Helper()
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004518 s := ctx.SingletonForTests("dex_bootjars")
4519 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004520 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004521 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004522 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004523 foundLibfooJar = true
4524 buildRule := s.Output(output)
4525 actual := android.NormalizePathForTesting(buildRule.Input)
4526 if actual != bootDexJarPath {
4527 t.Errorf("Incorrect boot dex jar path '%s', expected '%s'", actual, bootDexJarPath)
4528 }
4529 }
4530 }
4531 if !foundLibfooJar {
4532 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs")
4533 }
4534 }
4535
Paul Duffin4fd997b2021-02-03 20:06:33 +00004536 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004537 t.Helper()
Paul Duffin4fd997b2021-02-03 20:06:33 +00004538 hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
4539 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
4540 java.CheckHiddenAPIRuleInputs(t, expectedInputs, indexRule)
4541 }
4542
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004543 t.Run("prebuilt only", func(t *testing.T) {
4544 bp := `
4545 prebuilt_apex {
4546 name: "myapex",
4547 arch: {
4548 arm64: {
4549 src: "myapex-arm64.apex",
4550 },
4551 arm: {
4552 src: "myapex-arm.apex",
4553 },
4554 },
Paul Duffin37856732021-02-26 14:24:15 +00004555 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004556 }
4557
4558 java_import {
4559 name: "libfoo",
4560 jars: ["libfoo.jar"],
4561 apex_available: ["myapex"],
4562 }
Paul Duffin37856732021-02-26 14:24:15 +00004563
4564 java_sdk_library_import {
4565 name: "libbar",
4566 public: {
4567 jars: ["libbar.jar"],
4568 },
4569 apex_available: ["myapex"],
4570 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004571 `
4572
4573 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004574 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4575 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004576
Paul Duffin9d67ca62021-02-03 20:06:33 +00004577 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4578 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004579.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004580.intermediates/libfoo/android_common_myapex/hiddenapi/index.csv
4581`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004582 })
4583
4584 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4585 bp := `
4586 prebuilt_apex {
4587 name: "myapex",
4588 arch: {
4589 arm64: {
4590 src: "myapex-arm64.apex",
4591 },
4592 arm: {
4593 src: "myapex-arm.apex",
4594 },
4595 },
Paul Duffin37856732021-02-26 14:24:15 +00004596 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004597 }
4598
4599 java_import {
4600 name: "libfoo",
4601 jars: ["libfoo.jar"],
4602 apex_available: ["myapex"],
4603 }
4604
4605 java_library {
4606 name: "libfoo",
4607 srcs: ["foo/bar/MyClass.java"],
4608 apex_available: ["myapex"],
4609 }
Paul Duffin37856732021-02-26 14:24:15 +00004610
4611 java_sdk_library_import {
4612 name: "libbar",
4613 public: {
4614 jars: ["libbar.jar"],
4615 },
4616 apex_available: ["myapex"],
4617 }
4618
4619 java_sdk_library {
4620 name: "libbar",
4621 srcs: ["foo/bar/MyClass.java"],
4622 unsafe_ignore_missing_latest_api: true,
4623 apex_available: ["myapex"],
4624 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004625 `
4626
4627 // In this test the source (java_library) libfoo is active since the
4628 // prebuilt (java_import) defaults to prefer:false. However the
4629 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4630 // find the dex boot jar in it. We either need to disable the source libfoo
4631 // or make the prebuilt libfoo preferred.
4632 testDexpreoptWithApexes(t, bp, "failed to find a dex jar path for module 'libfoo'", transform)
4633 })
4634
4635 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4636 bp := `
4637 prebuilt_apex {
4638 name: "myapex",
4639 arch: {
4640 arm64: {
4641 src: "myapex-arm64.apex",
4642 },
4643 arm: {
4644 src: "myapex-arm.apex",
4645 },
4646 },
Paul Duffin37856732021-02-26 14:24:15 +00004647 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004648 }
4649
4650 java_import {
4651 name: "libfoo",
4652 prefer: true,
4653 jars: ["libfoo.jar"],
4654 apex_available: ["myapex"],
4655 }
4656
4657 java_library {
4658 name: "libfoo",
4659 srcs: ["foo/bar/MyClass.java"],
4660 apex_available: ["myapex"],
4661 }
Paul Duffin37856732021-02-26 14:24:15 +00004662
4663 java_sdk_library_import {
4664 name: "libbar",
4665 prefer: true,
4666 public: {
4667 jars: ["libbar.jar"],
4668 },
4669 apex_available: ["myapex"],
4670 }
4671
4672 java_sdk_library {
4673 name: "libbar",
4674 srcs: ["foo/bar/MyClass.java"],
4675 unsafe_ignore_missing_latest_api: true,
4676 apex_available: ["myapex"],
4677 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004678 `
4679
4680 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004681 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4682 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004683
Paul Duffin9d67ca62021-02-03 20:06:33 +00004684 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4685 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004686.intermediates/prebuilt_libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004687.intermediates/prebuilt_libfoo/android_common_myapex/hiddenapi/index.csv
4688`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004689 })
4690
4691 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4692 bp := `
4693 apex {
4694 name: "myapex",
4695 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004696 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004697 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004698 }
4699
4700 apex_key {
4701 name: "myapex.key",
4702 public_key: "testkey.avbpubkey",
4703 private_key: "testkey.pem",
4704 }
4705
4706 prebuilt_apex {
4707 name: "myapex",
4708 arch: {
4709 arm64: {
4710 src: "myapex-arm64.apex",
4711 },
4712 arm: {
4713 src: "myapex-arm.apex",
4714 },
4715 },
Paul Duffin37856732021-02-26 14:24:15 +00004716 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004717 }
4718
4719 java_import {
4720 name: "libfoo",
4721 jars: ["libfoo.jar"],
4722 apex_available: ["myapex"],
4723 }
4724
4725 java_library {
4726 name: "libfoo",
4727 srcs: ["foo/bar/MyClass.java"],
4728 apex_available: ["myapex"],
4729 }
Paul Duffin37856732021-02-26 14:24:15 +00004730
4731 java_sdk_library_import {
4732 name: "libbar",
4733 public: {
4734 jars: ["libbar.jar"],
4735 },
4736 apex_available: ["myapex"],
4737 }
4738
4739 java_sdk_library {
4740 name: "libbar",
4741 srcs: ["foo/bar/MyClass.java"],
4742 unsafe_ignore_missing_latest_api: true,
4743 apex_available: ["myapex"],
4744 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004745 `
4746
4747 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004748 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4749 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004750
4751 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4752 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004753.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin4fd997b2021-02-03 20:06:33 +00004754.intermediates/libfoo/android_common_apex10000/hiddenapi/index.csv
4755`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004756 })
4757
4758 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4759 bp := `
4760 apex {
4761 name: "myapex",
4762 enabled: false,
4763 key: "myapex.key",
4764 java_libs: ["libfoo"],
4765 }
4766
4767 apex_key {
4768 name: "myapex.key",
4769 public_key: "testkey.avbpubkey",
4770 private_key: "testkey.pem",
4771 }
4772
4773 prebuilt_apex {
4774 name: "myapex",
4775 arch: {
4776 arm64: {
4777 src: "myapex-arm64.apex",
4778 },
4779 arm: {
4780 src: "myapex-arm.apex",
4781 },
4782 },
Paul Duffin37856732021-02-26 14:24:15 +00004783 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004784 }
4785
4786 java_import {
4787 name: "libfoo",
4788 prefer: true,
4789 jars: ["libfoo.jar"],
4790 apex_available: ["myapex"],
4791 }
4792
4793 java_library {
4794 name: "libfoo",
4795 srcs: ["foo/bar/MyClass.java"],
4796 apex_available: ["myapex"],
4797 }
Paul Duffin37856732021-02-26 14:24:15 +00004798
4799 java_sdk_library_import {
4800 name: "libbar",
4801 prefer: true,
4802 public: {
4803 jars: ["libbar.jar"],
4804 },
4805 apex_available: ["myapex"],
4806 }
4807
4808 java_sdk_library {
4809 name: "libbar",
4810 srcs: ["foo/bar/MyClass.java"],
4811 unsafe_ignore_missing_latest_api: true,
4812 apex_available: ["myapex"],
4813 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004814 `
4815
4816 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004817 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4818 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004819
Paul Duffin9d67ca62021-02-03 20:06:33 +00004820 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4821 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004822.intermediates/prebuilt_libbar/android_common_prebuilt_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004823.intermediates/prebuilt_libfoo/android_common_prebuilt_myapex/hiddenapi/index.csv
4824`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004825 })
4826}
4827
Roland Levillain630846d2019-06-26 12:48:34 +01004828func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004829 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004830 apex_test {
4831 name: "myapex",
4832 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004833 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01004834 tests: [
4835 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004836 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004837 ],
4838 }
4839
4840 apex_key {
4841 name: "myapex.key",
4842 public_key: "testkey.avbpubkey",
4843 private_key: "testkey.pem",
4844 }
4845
Liz Kammer1c14a212020-05-12 15:26:55 -07004846 filegroup {
4847 name: "fg",
4848 srcs: [
4849 "baz",
4850 "bar/baz"
4851 ],
4852 }
4853
Roland Levillain630846d2019-06-26 12:48:34 +01004854 cc_test {
4855 name: "mytest",
4856 gtest: false,
4857 srcs: ["mytest.cpp"],
4858 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004859 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004860 system_shared_libs: [],
4861 static_executable: true,
4862 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004863 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004864 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004865
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004866 cc_library {
4867 name: "mylib",
4868 srcs: ["mylib.cpp"],
4869 system_shared_libs: [],
4870 stl: "none",
4871 }
4872
Liz Kammer5bd365f2020-05-27 15:15:11 -07004873 filegroup {
4874 name: "fg2",
4875 srcs: [
4876 "testdata/baz"
4877 ],
4878 }
4879
Roland Levillain9b5fde92019-06-28 15:41:19 +01004880 cc_test {
4881 name: "mytests",
4882 gtest: false,
4883 srcs: [
4884 "mytest1.cpp",
4885 "mytest2.cpp",
4886 "mytest3.cpp",
4887 ],
4888 test_per_src: true,
4889 relative_install_path: "test",
4890 system_shared_libs: [],
4891 static_executable: true,
4892 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004893 data: [
4894 ":fg",
4895 ":fg2",
4896 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004897 }
Roland Levillain630846d2019-06-26 12:48:34 +01004898 `)
4899
Sundong Ahnabb64432019-10-22 13:58:29 +09004900 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004901 copyCmds := apexRule.Args["copy_commands"]
4902
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004903 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004904 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004905 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004906
Liz Kammer1c14a212020-05-12 15:26:55 -07004907 //Ensure that test data are copied into apex.
4908 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4909 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4910
Roland Levillain9b5fde92019-06-28 15:41:19 +01004911 // Ensure that test deps built with `test_per_src` are copied into apex.
4912 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4913 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4914 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004915
4916 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004917 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004918 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004919 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004920 prefix := "TARGET_"
4921 var builder strings.Builder
4922 data.Custom(&builder, name, prefix, "", data)
4923 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004924 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4925 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4926 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4927 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004928 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004929 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004930 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004931
4932 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004933 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004934 data.Custom(&builder, name, prefix, "", data)
4935 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004936 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4937 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004938}
4939
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004940func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004941 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004942 apex {
4943 name: "myapex",
4944 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004945 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004946 }
4947 apex_key {
4948 name: "myapex.key",
4949 public_key: "testkey.avbpubkey",
4950 private_key: "testkey.pem",
4951 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00004952 `,
4953 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4954 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4955 }),
4956 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004957 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004958 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07004959 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004960 var builder strings.Builder
4961 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4962 androidMk := builder.String()
4963 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4964}
4965
Jooyung Hand48f3c32019-08-23 11:18:57 +09004966func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4967 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4968 apex {
4969 name: "myapex",
4970 key: "myapex.key",
4971 native_shared_libs: ["libfoo"],
4972 }
4973
4974 apex_key {
4975 name: "myapex.key",
4976 public_key: "testkey.avbpubkey",
4977 private_key: "testkey.pem",
4978 }
4979
4980 cc_library {
4981 name: "libfoo",
4982 stl: "none",
4983 system_shared_libs: [],
4984 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004985 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004986 }
4987 `)
4988 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
4989 apex {
4990 name: "myapex",
4991 key: "myapex.key",
4992 java_libs: ["myjar"],
4993 }
4994
4995 apex_key {
4996 name: "myapex.key",
4997 public_key: "testkey.avbpubkey",
4998 private_key: "testkey.pem",
4999 }
5000
5001 java_library {
5002 name: "myjar",
5003 srcs: ["foo/bar/MyClass.java"],
5004 sdk_version: "none",
5005 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005006 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005007 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005008 }
5009 `)
5010}
5011
Bill Peckhama41a6962021-01-11 10:58:54 -08005012func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005013 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005014 apex {
5015 name: "myapex",
5016 key: "myapex.key",
5017 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005018 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005019 }
5020
5021 apex_key {
5022 name: "myapex.key",
5023 public_key: "testkey.avbpubkey",
5024 private_key: "testkey.pem",
5025 }
5026
5027 java_import {
5028 name: "myjavaimport",
5029 apex_available: ["myapex"],
5030 jars: ["my.jar"],
5031 compile_dex: true,
5032 }
5033 `)
5034
5035 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5036 apexRule := module.Rule("apexRule")
5037 copyCmds := apexRule.Args["copy_commands"]
5038 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5039}
5040
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005041func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005042 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005043 apex {
5044 name: "myapex",
5045 key: "myapex.key",
5046 apps: [
5047 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005048 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005049 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005050 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005051 }
5052
5053 apex_key {
5054 name: "myapex.key",
5055 public_key: "testkey.avbpubkey",
5056 private_key: "testkey.pem",
5057 }
5058
5059 android_app {
5060 name: "AppFoo",
5061 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005062 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005063 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005064 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005065 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005066 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005067 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005068
5069 android_app {
5070 name: "AppFooPriv",
5071 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005072 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005073 system_modules: "none",
5074 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005075 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005076 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005077 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005078
5079 cc_library_shared {
5080 name: "libjni",
5081 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005082 shared_libs: ["libfoo"],
5083 stl: "none",
5084 system_shared_libs: [],
5085 apex_available: [ "myapex" ],
5086 sdk_version: "current",
5087 }
5088
5089 cc_library_shared {
5090 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005091 stl: "none",
5092 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005093 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005094 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005095 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005096 `)
5097
Sundong Ahnabb64432019-10-22 13:58:29 +09005098 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005099 apexRule := module.Rule("apexRule")
5100 copyCmds := apexRule.Args["copy_commands"]
5101
5102 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005103 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005104
Colin Crossaede88c2020-08-11 12:17:01 -07005105 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005106 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005107 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005108 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005109 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005110 // JNI libraries including transitive deps are
5111 for _, jni := range []string{"libjni", "libfoo"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005112 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005113 // ... embedded inside APK (jnilibs.zip)
5114 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5115 // ... and not directly inside the APEX
5116 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5117 }
Dario Frenicde2a032019-10-27 00:29:22 +01005118}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005119
Dario Frenicde2a032019-10-27 00:29:22 +01005120func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005121 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005122 apex {
5123 name: "myapex",
5124 key: "myapex.key",
5125 apps: [
5126 "AppFooPrebuilt",
5127 "AppFooPrivPrebuilt",
5128 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005129 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005130 }
5131
5132 apex_key {
5133 name: "myapex.key",
5134 public_key: "testkey.avbpubkey",
5135 private_key: "testkey.pem",
5136 }
5137
5138 android_app_import {
5139 name: "AppFooPrebuilt",
5140 apk: "PrebuiltAppFoo.apk",
5141 presigned: true,
5142 dex_preopt: {
5143 enabled: false,
5144 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005145 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005146 }
5147
5148 android_app_import {
5149 name: "AppFooPrivPrebuilt",
5150 apk: "PrebuiltAppFooPriv.apk",
5151 privileged: true,
5152 presigned: true,
5153 dex_preopt: {
5154 enabled: false,
5155 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005156 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005157 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005158 }
5159 `)
5160
Sundong Ahnabb64432019-10-22 13:58:29 +09005161 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005162 apexRule := module.Rule("apexRule")
5163 copyCmds := apexRule.Args["copy_commands"]
5164
5165 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005166 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5167}
5168
5169func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005170 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005171 apex {
5172 name: "myapex",
5173 key: "myapex.key",
5174 apps: [
5175 "AppFoo",
5176 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005177 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005178 }
5179
5180 apex_key {
5181 name: "myapex.key",
5182 public_key: "testkey.avbpubkey",
5183 private_key: "testkey.pem",
5184 }
5185
5186 android_app {
5187 name: "AppFoo",
5188 srcs: ["foo/bar/MyClass.java"],
5189 sdk_version: "none",
5190 system_modules: "none",
5191 apex_available: [ "myapex" ],
5192 }
5193
5194 android_app_import {
5195 name: "AppFoo",
5196 apk: "AppFooPrebuilt.apk",
5197 filename: "AppFooPrebuilt.apk",
5198 presigned: true,
5199 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005200 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005201 }
5202 `, withFiles(map[string][]byte{
5203 "AppFooPrebuilt.apk": nil,
5204 }))
5205
5206 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005207 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005208 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005209}
5210
Dario Freni6f3937c2019-12-20 22:58:03 +00005211func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005212 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005213 apex {
5214 name: "myapex",
5215 key: "myapex.key",
5216 apps: [
5217 "TesterHelpAppFoo",
5218 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005219 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005220 }
5221
5222 apex_key {
5223 name: "myapex.key",
5224 public_key: "testkey.avbpubkey",
5225 private_key: "testkey.pem",
5226 }
5227
5228 android_test_helper_app {
5229 name: "TesterHelpAppFoo",
5230 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005231 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005232 }
5233
5234 `)
5235
5236 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5237 apexRule := module.Rule("apexRule")
5238 copyCmds := apexRule.Args["copy_commands"]
5239
5240 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5241}
5242
Jooyung Han18020ea2019-11-13 10:50:48 +09005243func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5244 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005245 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005246 apex {
5247 name: "myapex",
5248 key: "myapex.key",
5249 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005250 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005251 }
5252
5253 apex_key {
5254 name: "myapex.key",
5255 public_key: "testkey.avbpubkey",
5256 private_key: "testkey.pem",
5257 }
5258
5259 apex {
5260 name: "otherapex",
5261 key: "myapex.key",
5262 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005263 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005264 }
5265
5266 cc_defaults {
5267 name: "libfoo-defaults",
5268 apex_available: ["otherapex"],
5269 }
5270
5271 cc_library {
5272 name: "libfoo",
5273 defaults: ["libfoo-defaults"],
5274 stl: "none",
5275 system_shared_libs: [],
5276 }`)
5277}
5278
Paul Duffine52e66f2020-03-30 17:54:29 +01005279func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005280 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005281 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005282 apex {
5283 name: "myapex",
5284 key: "myapex.key",
5285 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005286 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005287 }
5288
5289 apex_key {
5290 name: "myapex.key",
5291 public_key: "testkey.avbpubkey",
5292 private_key: "testkey.pem",
5293 }
5294
5295 apex {
5296 name: "otherapex",
5297 key: "otherapex.key",
5298 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005299 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005300 }
5301
5302 apex_key {
5303 name: "otherapex.key",
5304 public_key: "testkey.avbpubkey",
5305 private_key: "testkey.pem",
5306 }
5307
5308 cc_library {
5309 name: "libfoo",
5310 stl: "none",
5311 system_shared_libs: [],
5312 apex_available: ["otherapex"],
5313 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005314}
Jiyong Park127b40b2019-09-30 16:04:35 +09005315
Paul Duffine52e66f2020-03-30 17:54:29 +01005316func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005317 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005318 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005319.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005320.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005321.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005322.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005323.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005324.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005325 apex {
5326 name: "myapex",
5327 key: "myapex.key",
5328 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005329 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005330 }
5331
5332 apex_key {
5333 name: "myapex.key",
5334 public_key: "testkey.avbpubkey",
5335 private_key: "testkey.pem",
5336 }
5337
Jiyong Park127b40b2019-09-30 16:04:35 +09005338 cc_library {
5339 name: "libfoo",
5340 stl: "none",
5341 shared_libs: ["libbar"],
5342 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005343 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005344 }
5345
5346 cc_library {
5347 name: "libbar",
5348 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005349 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005350 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005351 apex_available: ["myapex"],
5352 }
5353
5354 cc_library {
5355 name: "libbaz",
5356 stl: "none",
5357 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005358 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005359}
Jiyong Park127b40b2019-09-30 16:04:35 +09005360
Paul Duffine52e66f2020-03-30 17:54:29 +01005361func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005362 testApexError(t, "\"otherapex\" is not a valid module name", `
5363 apex {
5364 name: "myapex",
5365 key: "myapex.key",
5366 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005367 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005368 }
5369
5370 apex_key {
5371 name: "myapex.key",
5372 public_key: "testkey.avbpubkey",
5373 private_key: "testkey.pem",
5374 }
5375
5376 cc_library {
5377 name: "libfoo",
5378 stl: "none",
5379 system_shared_libs: [],
5380 apex_available: ["otherapex"],
5381 }`)
5382
Paul Duffine52e66f2020-03-30 17:54:29 +01005383 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005384 apex {
5385 name: "myapex",
5386 key: "myapex.key",
5387 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005388 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005389 }
5390
5391 apex_key {
5392 name: "myapex.key",
5393 public_key: "testkey.avbpubkey",
5394 private_key: "testkey.pem",
5395 }
5396
5397 cc_library {
5398 name: "libfoo",
5399 stl: "none",
5400 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005401 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005402 apex_available: ["myapex"],
5403 }
5404
5405 cc_library {
5406 name: "libbar",
5407 stl: "none",
5408 system_shared_libs: [],
5409 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005410 }
5411
5412 cc_library {
5413 name: "libbaz",
5414 stl: "none",
5415 system_shared_libs: [],
5416 stubs: {
5417 versions: ["10", "20", "30"],
5418 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005419 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005420}
Jiyong Park127b40b2019-09-30 16:04:35 +09005421
Jiyong Park89e850a2020-04-07 16:37:39 +09005422func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005423 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005424 apex {
5425 name: "myapex",
5426 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005427 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005428 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005429 }
5430
5431 apex_key {
5432 name: "myapex.key",
5433 public_key: "testkey.avbpubkey",
5434 private_key: "testkey.pem",
5435 }
5436
5437 cc_library {
5438 name: "libfoo",
5439 stl: "none",
5440 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005441 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005442 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005443 }
5444
5445 cc_library {
5446 name: "libfoo2",
5447 stl: "none",
5448 system_shared_libs: [],
5449 shared_libs: ["libbaz"],
5450 apex_available: ["//apex_available:platform"],
5451 }
5452
5453 cc_library {
5454 name: "libbar",
5455 stl: "none",
5456 system_shared_libs: [],
5457 apex_available: ["myapex"],
5458 }
5459
5460 cc_library {
5461 name: "libbaz",
5462 stl: "none",
5463 system_shared_libs: [],
5464 apex_available: ["myapex"],
5465 stubs: {
5466 versions: ["1"],
5467 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005468 }`)
5469
Jiyong Park89e850a2020-04-07 16:37:39 +09005470 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5471 // because it depends on libbar which isn't available to platform
5472 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5473 if libfoo.NotAvailableForPlatform() != true {
5474 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5475 }
5476
5477 // libfoo2 however can be available to platform because it depends on libbaz which provides
5478 // stubs
5479 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5480 if libfoo2.NotAvailableForPlatform() == true {
5481 t.Errorf("%q should be available to platform", libfoo2.String())
5482 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005483}
Jiyong Parka90ca002019-10-07 15:47:24 +09005484
Paul Duffine52e66f2020-03-30 17:54:29 +01005485func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005486 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005487 apex {
5488 name: "myapex",
5489 key: "myapex.key",
5490 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005491 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005492 }
5493
5494 apex_key {
5495 name: "myapex.key",
5496 public_key: "testkey.avbpubkey",
5497 private_key: "testkey.pem",
5498 }
5499
5500 cc_library {
5501 name: "libfoo",
5502 stl: "none",
5503 system_shared_libs: [],
5504 apex_available: ["myapex"],
5505 static: {
5506 apex_available: ["//apex_available:platform"],
5507 },
5508 }`)
5509
Jiyong Park89e850a2020-04-07 16:37:39 +09005510 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5511 if libfooShared.NotAvailableForPlatform() != true {
5512 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5513 }
5514 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5515 if libfooStatic.NotAvailableForPlatform() != false {
5516 t.Errorf("%q should be available to platform", libfooStatic.String())
5517 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005518}
5519
Jiyong Park5d790c32019-11-15 18:40:32 +09005520func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005521 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005522 apex {
5523 name: "myapex",
5524 key: "myapex.key",
5525 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005526 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005527 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005528 }
5529
5530 override_apex {
5531 name: "override_myapex",
5532 base: "myapex",
5533 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005534 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005535 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005536 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09005537 }
5538
5539 apex_key {
5540 name: "myapex.key",
5541 public_key: "testkey.avbpubkey",
5542 private_key: "testkey.pem",
5543 }
5544
5545 android_app {
5546 name: "app",
5547 srcs: ["foo/bar/MyClass.java"],
5548 package_name: "foo",
5549 sdk_version: "none",
5550 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005551 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005552 }
5553
5554 override_android_app {
5555 name: "override_app",
5556 base: "app",
5557 package_name: "bar",
5558 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005559 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005560
Jiyong Park317645e2019-12-05 13:20:58 +09005561 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5562 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5563 if originalVariant.GetOverriddenBy() != "" {
5564 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5565 }
5566 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5567 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5568 }
5569
Jiyong Park5d790c32019-11-15 18:40:32 +09005570 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5571 apexRule := module.Rule("apexRule")
5572 copyCmds := apexRule.Args["copy_commands"]
5573
5574 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005575 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005576
5577 apexBundle := module.Module().(*apexBundle)
5578 name := apexBundle.Name()
5579 if name != "override_myapex" {
5580 t.Errorf("name should be \"override_myapex\", but was %q", name)
5581 }
5582
Baligh Uddin004d7172020-02-19 21:29:28 -08005583 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5584 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5585 }
5586
Jiyong Park20bacab2020-03-03 11:45:41 +09005587 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005588 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09005589
Colin Crossaa255532020-07-03 13:18:24 -07005590 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005591 var builder strings.Builder
5592 data.Custom(&builder, name, "TARGET_", "", data)
5593 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005594 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005595 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5596 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005597 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005598 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005599 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005600 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5601 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005602}
5603
Jooyung Han214bf372019-11-12 13:03:50 +09005604func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005605 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005606 apex {
5607 name: "myapex",
5608 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005609 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005610 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005611 }
5612
5613 apex_key {
5614 name: "myapex.key",
5615 public_key: "testkey.avbpubkey",
5616 private_key: "testkey.pem",
5617 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005618
5619 cc_library {
5620 name: "mylib",
5621 srcs: ["mylib.cpp"],
5622 stl: "libc++",
5623 system_shared_libs: [],
5624 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005625 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005626 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005627 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005628
5629 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5630 args := module.Rule("apexRule").Args
5631 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005632 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005633
5634 // The copies of the libraries in the apex should have one more dependency than
5635 // the ones outside the apex, namely the unwinder. Ideally we should check
5636 // the dependency names directly here but for some reason the names are blank in
5637 // this test.
5638 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005639 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005640 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5641 if len(apexImplicits) != len(nonApexImplicits)+1 {
5642 t.Errorf("%q missing unwinder dep", lib)
5643 }
5644 }
Jooyung Han214bf372019-11-12 13:03:50 +09005645}
5646
Paul Duffine05480a2021-03-08 15:07:14 +00005647var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01005648 "api/current.txt": nil,
5649 "api/removed.txt": nil,
5650 "api/system-current.txt": nil,
5651 "api/system-removed.txt": nil,
5652 "api/test-current.txt": nil,
5653 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005654
Anton Hanssondff2c782020-12-21 17:10:01 +00005655 "100/public/api/foo.txt": nil,
5656 "100/public/api/foo-removed.txt": nil,
5657 "100/system/api/foo.txt": nil,
5658 "100/system/api/foo-removed.txt": nil,
5659
Paul Duffineedc5d52020-06-12 17:46:39 +01005660 // For java_sdk_library_import
5661 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005662}
5663
Jooyung Han58f26ab2019-12-18 15:34:32 +09005664func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005665 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09005666 apex {
5667 name: "myapex",
5668 key: "myapex.key",
5669 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005670 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09005671 }
5672
5673 apex_key {
5674 name: "myapex.key",
5675 public_key: "testkey.avbpubkey",
5676 private_key: "testkey.pem",
5677 }
5678
5679 java_sdk_library {
5680 name: "foo",
5681 srcs: ["a.java"],
5682 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005683 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005684 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005685
5686 prebuilt_apis {
5687 name: "sdk",
5688 api_dirs: ["100"],
5689 }
Paul Duffin9b879592020-05-26 13:21:35 +01005690 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005691
5692 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005693 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005694 "javalib/foo.jar",
5695 "etc/permissions/foo.xml",
5696 })
5697 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005698 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5699 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005700}
5701
Paul Duffin9b879592020-05-26 13:21:35 +01005702func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005703 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005704 apex {
5705 name: "myapex",
5706 key: "myapex.key",
5707 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005708 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005709 }
5710
5711 apex_key {
5712 name: "myapex.key",
5713 public_key: "testkey.avbpubkey",
5714 private_key: "testkey.pem",
5715 }
5716
5717 java_sdk_library {
5718 name: "foo",
5719 srcs: ["a.java"],
5720 api_packages: ["foo"],
5721 apex_available: ["myapex"],
5722 sdk_version: "none",
5723 system_modules: "none",
5724 }
5725
5726 java_library {
5727 name: "bar",
5728 srcs: ["a.java"],
5729 libs: ["foo"],
5730 apex_available: ["myapex"],
5731 sdk_version: "none",
5732 system_modules: "none",
5733 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005734
5735 prebuilt_apis {
5736 name: "sdk",
5737 api_dirs: ["100"],
5738 }
Paul Duffin9b879592020-05-26 13:21:35 +01005739 `, withFiles(filesForSdkLibrary))
5740
5741 // java_sdk_library installs both impl jar and permission XML
5742 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5743 "javalib/bar.jar",
5744 "javalib/foo.jar",
5745 "etc/permissions/foo.xml",
5746 })
5747
5748 // The bar library should depend on the implementation jar.
5749 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5750 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5751 t.Errorf("expected %q, found %#q", expected, actual)
5752 }
5753}
5754
5755func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005756 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005757 apex {
5758 name: "myapex",
5759 key: "myapex.key",
5760 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005761 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005762 }
5763
5764 apex_key {
5765 name: "myapex.key",
5766 public_key: "testkey.avbpubkey",
5767 private_key: "testkey.pem",
5768 }
5769
5770 java_sdk_library {
5771 name: "foo",
5772 srcs: ["a.java"],
5773 api_packages: ["foo"],
5774 apex_available: ["myapex"],
5775 sdk_version: "none",
5776 system_modules: "none",
5777 }
5778
5779 java_library {
5780 name: "bar",
5781 srcs: ["a.java"],
5782 libs: ["foo"],
5783 sdk_version: "none",
5784 system_modules: "none",
5785 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005786
5787 prebuilt_apis {
5788 name: "sdk",
5789 api_dirs: ["100"],
5790 }
Paul Duffin9b879592020-05-26 13:21:35 +01005791 `, withFiles(filesForSdkLibrary))
5792
5793 // java_sdk_library installs both impl jar and permission XML
5794 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5795 "javalib/foo.jar",
5796 "etc/permissions/foo.xml",
5797 })
5798
5799 // The bar library should depend on the stubs jar.
5800 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
5801 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5802 t.Errorf("expected %q, found %#q", expected, actual)
5803 }
5804}
5805
Paul Duffineedc5d52020-06-12 17:46:39 +01005806func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005807 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00005808 prebuilt_apis {
5809 name: "sdk",
5810 api_dirs: ["100"],
5811 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005812 withFiles(map[string][]byte{
5813 "apex/a.java": nil,
5814 "apex/apex_manifest.json": nil,
5815 "apex/Android.bp": []byte(`
5816 package {
5817 default_visibility: ["//visibility:private"],
5818 }
5819
5820 apex {
5821 name: "myapex",
5822 key: "myapex.key",
5823 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005824 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005825 }
5826
5827 apex_key {
5828 name: "myapex.key",
5829 public_key: "testkey.avbpubkey",
5830 private_key: "testkey.pem",
5831 }
5832
5833 java_library {
5834 name: "bar",
5835 srcs: ["a.java"],
5836 libs: ["foo"],
5837 apex_available: ["myapex"],
5838 sdk_version: "none",
5839 system_modules: "none",
5840 }
5841`),
5842 "source/a.java": nil,
5843 "source/api/current.txt": nil,
5844 "source/api/removed.txt": nil,
5845 "source/Android.bp": []byte(`
5846 package {
5847 default_visibility: ["//visibility:private"],
5848 }
5849
5850 java_sdk_library {
5851 name: "foo",
5852 visibility: ["//apex"],
5853 srcs: ["a.java"],
5854 api_packages: ["foo"],
5855 apex_available: ["myapex"],
5856 sdk_version: "none",
5857 system_modules: "none",
5858 public: {
5859 enabled: true,
5860 },
5861 }
5862`),
5863 "prebuilt/a.jar": nil,
5864 "prebuilt/Android.bp": []byte(`
5865 package {
5866 default_visibility: ["//visibility:private"],
5867 }
5868
5869 java_sdk_library_import {
5870 name: "foo",
5871 visibility: ["//apex", "//source"],
5872 apex_available: ["myapex"],
5873 prefer: true,
5874 public: {
5875 jars: ["a.jar"],
5876 },
5877 }
5878`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005879 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005880 )
5881
5882 // java_sdk_library installs both impl jar and permission XML
5883 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5884 "javalib/bar.jar",
5885 "javalib/foo.jar",
5886 "etc/permissions/foo.xml",
5887 })
5888
5889 // The bar library should depend on the implementation jar.
5890 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5891 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5892 t.Errorf("expected %q, found %#q", expected, actual)
5893 }
5894}
5895
5896func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5897 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5898 apex {
5899 name: "myapex",
5900 key: "myapex.key",
5901 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005902 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005903 }
5904
5905 apex_key {
5906 name: "myapex.key",
5907 public_key: "testkey.avbpubkey",
5908 private_key: "testkey.pem",
5909 }
5910
5911 java_sdk_library_import {
5912 name: "foo",
5913 apex_available: ["myapex"],
5914 prefer: true,
5915 public: {
5916 jars: ["a.jar"],
5917 },
5918 }
5919
5920 `, withFiles(filesForSdkLibrary))
5921}
5922
atrost6e126252020-01-27 17:01:16 +00005923func TestCompatConfig(t *testing.T) {
Paul Duffina369c7b2021-03-09 03:08:05 +00005924 result := apexFixtureFactory.
5925 Extend(java.PrepareForTestWithPlatformCompatConfig).
5926 RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00005927 apex {
5928 name: "myapex",
5929 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00005930 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00005931 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005932 updatable: false,
atrost6e126252020-01-27 17:01:16 +00005933 }
5934
5935 apex_key {
5936 name: "myapex.key",
5937 public_key: "testkey.avbpubkey",
5938 private_key: "testkey.pem",
5939 }
5940
5941 platform_compat_config {
5942 name: "myjar-platform-compat-config",
5943 src: ":myjar",
5944 }
5945
5946 java_library {
5947 name: "myjar",
5948 srcs: ["foo/bar/MyClass.java"],
5949 sdk_version: "none",
5950 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005951 apex_available: [ "myapex" ],
5952 }
Paul Duffin1b29e002021-03-16 15:06:54 +00005953
5954 // Make sure that a preferred prebuilt does not affect the apex contents.
5955 prebuilt_platform_compat_config {
5956 name: "myjar-platform-compat-config",
5957 metadata: "compat-config/metadata.xml",
5958 prefer: true,
5959 }
atrost6e126252020-01-27 17:01:16 +00005960 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00005961 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00005962 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5963 "etc/compatconfig/myjar-platform-compat-config.xml",
5964 "javalib/myjar.jar",
5965 })
5966}
5967
Jiyong Park479321d2019-12-16 11:47:12 +09005968func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5969 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5970 apex {
5971 name: "myapex",
5972 key: "myapex.key",
5973 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005974 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09005975 }
5976
5977 apex_key {
5978 name: "myapex.key",
5979 public_key: "testkey.avbpubkey",
5980 private_key: "testkey.pem",
5981 }
5982
5983 java_library {
5984 name: "myjar",
5985 srcs: ["foo/bar/MyClass.java"],
5986 sdk_version: "none",
5987 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09005988 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005989 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09005990 }
5991 `)
5992}
5993
Jiyong Park7afd1072019-12-30 16:56:33 +09005994func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005995 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09005996 apex {
5997 name: "myapex",
5998 key: "myapex.key",
5999 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006000 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09006001 }
6002
6003 apex_key {
6004 name: "myapex.key",
6005 public_key: "testkey.avbpubkey",
6006 private_key: "testkey.pem",
6007 }
6008
6009 cc_library {
6010 name: "mylib",
6011 srcs: ["mylib.cpp"],
6012 system_shared_libs: [],
6013 stl: "none",
6014 required: ["a", "b"],
6015 host_required: ["c", "d"],
6016 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006017 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006018 }
6019 `)
6020
6021 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006022 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006023 name := apexBundle.BaseModuleName()
6024 prefix := "TARGET_"
6025 var builder strings.Builder
6026 data.Custom(&builder, name, prefix, "", data)
6027 androidMk := builder.String()
6028 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6029 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6030 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6031}
6032
Jiyong Park7cd10e32020-01-14 09:22:18 +09006033func TestSymlinksFromApexToSystem(t *testing.T) {
6034 bp := `
6035 apex {
6036 name: "myapex",
6037 key: "myapex.key",
6038 native_shared_libs: ["mylib"],
6039 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006040 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006041 }
6042
Jiyong Park9d677202020-02-19 16:29:35 +09006043 apex {
6044 name: "myapex.updatable",
6045 key: "myapex.key",
6046 native_shared_libs: ["mylib"],
6047 java_libs: ["myjar"],
6048 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006049 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006050 }
6051
Jiyong Park7cd10e32020-01-14 09:22:18 +09006052 apex_key {
6053 name: "myapex.key",
6054 public_key: "testkey.avbpubkey",
6055 private_key: "testkey.pem",
6056 }
6057
6058 cc_library {
6059 name: "mylib",
6060 srcs: ["mylib.cpp"],
6061 shared_libs: ["myotherlib"],
6062 system_shared_libs: [],
6063 stl: "none",
6064 apex_available: [
6065 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006066 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006067 "//apex_available:platform",
6068 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006069 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006070 }
6071
6072 cc_library {
6073 name: "myotherlib",
6074 srcs: ["mylib.cpp"],
6075 system_shared_libs: [],
6076 stl: "none",
6077 apex_available: [
6078 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006079 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006080 "//apex_available:platform",
6081 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006082 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006083 }
6084
6085 java_library {
6086 name: "myjar",
6087 srcs: ["foo/bar/MyClass.java"],
6088 sdk_version: "none",
6089 system_modules: "none",
6090 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006091 apex_available: [
6092 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006093 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006094 "//apex_available:platform",
6095 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006096 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006097 }
6098
6099 java_library {
6100 name: "myotherjar",
6101 srcs: ["foo/bar/MyClass.java"],
6102 sdk_version: "none",
6103 system_modules: "none",
6104 apex_available: [
6105 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006106 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006107 "//apex_available:platform",
6108 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006109 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006110 }
6111 `
6112
6113 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6114 for _, f := range files {
6115 if f.path == file {
6116 if f.isLink {
6117 t.Errorf("%q is not a real file", file)
6118 }
6119 return
6120 }
6121 }
6122 t.Errorf("%q is not found", file)
6123 }
6124
6125 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6126 for _, f := range files {
6127 if f.path == file {
6128 if !f.isLink {
6129 t.Errorf("%q is not a symlink", file)
6130 }
6131 return
6132 }
6133 }
6134 t.Errorf("%q is not found", file)
6135 }
6136
Jiyong Park9d677202020-02-19 16:29:35 +09006137 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6138 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006139 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006140 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006141 ensureRealfileExists(t, files, "javalib/myjar.jar")
6142 ensureRealfileExists(t, files, "lib64/mylib.so")
6143 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6144
Jiyong Park9d677202020-02-19 16:29:35 +09006145 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6146 ensureRealfileExists(t, files, "javalib/myjar.jar")
6147 ensureRealfileExists(t, files, "lib64/mylib.so")
6148 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6149
6150 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006151 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006152 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006153 ensureRealfileExists(t, files, "javalib/myjar.jar")
6154 ensureRealfileExists(t, files, "lib64/mylib.so")
6155 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006156
6157 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6158 ensureRealfileExists(t, files, "javalib/myjar.jar")
6159 ensureRealfileExists(t, files, "lib64/mylib.so")
6160 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006161}
6162
Yo Chiange8128052020-07-23 20:09:18 +08006163func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006164 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006165 apex {
6166 name: "myapex",
6167 key: "myapex.key",
6168 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006169 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006170 }
6171
6172 apex_key {
6173 name: "myapex.key",
6174 public_key: "testkey.avbpubkey",
6175 private_key: "testkey.pem",
6176 }
6177
6178 cc_library_shared {
6179 name: "mylib",
6180 srcs: ["mylib.cpp"],
6181 shared_libs: ["myotherlib"],
6182 system_shared_libs: [],
6183 stl: "none",
6184 apex_available: [
6185 "myapex",
6186 "//apex_available:platform",
6187 ],
6188 }
6189
6190 cc_prebuilt_library_shared {
6191 name: "myotherlib",
6192 srcs: ["prebuilt.so"],
6193 system_shared_libs: [],
6194 stl: "none",
6195 apex_available: [
6196 "myapex",
6197 "//apex_available:platform",
6198 ],
6199 }
6200 `)
6201
6202 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006203 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006204 var builder strings.Builder
6205 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6206 androidMk := builder.String()
6207 // `myotherlib` is added to `myapex` as symlink
6208 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6209 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6210 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6211 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006212 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 +08006213}
6214
Jooyung Han643adc42020-02-27 13:50:06 +09006215func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006216 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006217 apex {
6218 name: "myapex",
6219 key: "myapex.key",
6220 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006221 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006222 }
6223
6224 apex_key {
6225 name: "myapex.key",
6226 public_key: "testkey.avbpubkey",
6227 private_key: "testkey.pem",
6228 }
6229
6230 cc_library {
6231 name: "mylib",
6232 srcs: ["mylib.cpp"],
6233 shared_libs: ["mylib2"],
6234 system_shared_libs: [],
6235 stl: "none",
6236 apex_available: [ "myapex" ],
6237 }
6238
6239 cc_library {
6240 name: "mylib2",
6241 srcs: ["mylib.cpp"],
6242 system_shared_libs: [],
6243 stl: "none",
6244 apex_available: [ "myapex" ],
6245 }
6246 `)
6247
6248 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6249 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6250 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6251 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6252 "lib64/mylib.so",
6253 "lib64/mylib2.so",
6254 })
6255}
6256
Jooyung Han49f67012020-04-17 13:43:10 +09006257func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006258 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006259 apex {
6260 name: "myapex",
6261 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006262 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006263 }
6264 apex_key {
6265 name: "myapex.key",
6266 public_key: "testkey.avbpubkey",
6267 private_key: "testkey.pem",
6268 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006269 `,
6270 android.FixtureModifyConfig(func(config android.Config) {
6271 delete(config.Targets, android.Android)
6272 config.AndroidCommonTarget = android.Target{}
6273 }),
6274 )
Jooyung Han49f67012020-04-17 13:43:10 +09006275
6276 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6277 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6278 }
6279}
6280
Jiyong Parkbd159612020-02-28 15:22:21 +09006281func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006282 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006283 apex {
6284 name: "myapex",
6285 key: "myapex.key",
6286 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006287 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006288 }
6289
6290 apex_key {
6291 name: "myapex.key",
6292 public_key: "testkey.avbpubkey",
6293 private_key: "testkey.pem",
6294 }
6295
6296 android_app {
6297 name: "AppFoo",
6298 srcs: ["foo/bar/MyClass.java"],
6299 sdk_version: "none",
6300 system_modules: "none",
6301 apex_available: [ "myapex" ],
6302 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006303 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006304
Colin Crosscf371cc2020-11-13 11:48:42 -08006305 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006306 content := bundleConfigRule.Args["content"]
6307
6308 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006309 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 +09006310}
6311
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006312func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006313 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006314 apex {
6315 name: "myapex",
6316 key: "myapex.key",
6317 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006318 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006319 }
6320
6321 apex_key {
6322 name: "myapex.key",
6323 public_key: "testkey.avbpubkey",
6324 private_key: "testkey.pem",
6325 }
6326
6327 android_app_set {
6328 name: "AppSet",
6329 set: "AppSet.apks",
6330 }`)
6331 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006332 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006333 content := bundleConfigRule.Args["content"]
6334 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6335 s := mod.Rule("apexRule").Args["copy_commands"]
6336 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6337 if len(copyCmds) != 3 {
6338 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6339 }
6340 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6341 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6342 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6343}
6344
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006345func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006346 ctx := testApex(t, "", android.FixtureModifyMockFS(func(fs android.MockFS) {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006347 bp := `
6348 apex_set {
6349 name: "myapex",
6350 filename: "foo_v2.apex",
6351 sanitized: {
6352 none: { set: "myapex.apks", },
6353 hwaddress: { set: "myapex.hwasan.apks", },
6354 },
6355 }`
6356 fs["Android.bp"] = []byte(bp)
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006357 }),
6358 prepareForTestWithSantitizeHwaddress,
6359 )
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006360
6361 m := ctx.ModuleForTests("myapex", "android_common")
6362 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6363
6364 actual := extractedApex.Inputs
6365 if len(actual) != 1 {
6366 t.Errorf("expected a single input")
6367 }
6368
6369 expected := "myapex.hwasan.apks"
6370 if actual[0].String() != expected {
6371 t.Errorf("expected %s, got %s", expected, actual[0].String())
6372 }
6373}
6374
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006375func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006376 t.Helper()
6377
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006378 bp := `
6379 java_library {
6380 name: "some-updatable-apex-lib",
6381 srcs: ["a.java"],
6382 sdk_version: "current",
6383 apex_available: [
6384 "some-updatable-apex",
6385 ],
6386 }
6387
6388 java_library {
6389 name: "some-non-updatable-apex-lib",
6390 srcs: ["a.java"],
6391 apex_available: [
6392 "some-non-updatable-apex",
6393 ],
6394 }
6395
6396 java_library {
6397 name: "some-platform-lib",
6398 srcs: ["a.java"],
6399 sdk_version: "current",
6400 installable: true,
6401 }
6402
6403 java_library {
6404 name: "some-art-lib",
6405 srcs: ["a.java"],
6406 sdk_version: "current",
6407 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006408 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006409 ],
6410 hostdex: true,
6411 }
6412
6413 apex {
6414 name: "some-updatable-apex",
6415 key: "some-updatable-apex.key",
6416 java_libs: ["some-updatable-apex-lib"],
6417 updatable: true,
6418 min_sdk_version: "current",
6419 }
6420
6421 apex {
6422 name: "some-non-updatable-apex",
6423 key: "some-non-updatable-apex.key",
6424 java_libs: ["some-non-updatable-apex-lib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006425 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006426 }
6427
6428 apex_key {
6429 name: "some-updatable-apex.key",
6430 }
6431
6432 apex_key {
6433 name: "some-non-updatable-apex.key",
6434 }
6435
6436 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006437 name: "com.android.art.debug",
6438 key: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006439 java_libs: ["some-art-lib"],
6440 updatable: true,
6441 min_sdk_version: "current",
6442 }
6443
6444 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006445 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006446 }
6447
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006448 filegroup {
6449 name: "some-updatable-apex-file_contexts",
6450 srcs: [
6451 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6452 ],
6453 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006454
6455 filegroup {
6456 name: "some-non-updatable-apex-file_contexts",
6457 srcs: [
6458 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6459 ],
6460 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006461 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006462
6463 testDexpreoptWithApexes(t, bp, errmsg, transformDexpreoptConfig)
6464}
6465
Paul Duffin064b70c2020-11-02 17:32:38 +00006466func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006467 t.Helper()
6468
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006469 bp += cc.GatherRequiredDepsForTest(android.Android)
6470 bp += java.GatherRequiredDepsForTest()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006471
6472 fs := map[string][]byte{
6473 "a.java": nil,
6474 "a.jar": nil,
6475 "build/make/target/product/security": nil,
6476 "apex_manifest.json": nil,
6477 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006478 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006479 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6480 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6481 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006482 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006483 }
6484 cc.GatherRequiredFilesForTest(fs)
6485
Paul Duffin39853512021-02-26 11:09:39 +00006486 for k, v := range filesForSdkLibrary {
6487 fs[k] = v
6488 }
Colin Crossae8600b2020-10-29 17:09:13 -07006489 config := android.TestArchConfig(buildDir, nil, bp, fs)
6490
6491 ctx := android.NewTestArchContext(config)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006492 ctx.RegisterModuleType("apex", BundleFactory)
6493 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
Paul Duffin064b70c2020-11-02 17:32:38 +00006494 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006495 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinc988c8e2020-04-29 18:27:14 +01006496 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin37856732021-02-26 14:24:15 +00006497 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffin021f4e52020-07-30 16:04:17 +01006498 android.RegisterPrebuiltMutators(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006499 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006500 java.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinf38931c2021-02-05 16:58:28 +00006501 java.RegisterHiddenApiSingletonComponents(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006502 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6503 ctx.PreDepsMutators(RegisterPreDepsMutators)
6504 ctx.PostDepsMutators(RegisterPostDepsMutators)
6505
Colin Crossae8600b2020-10-29 17:09:13 -07006506 ctx.Register()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006507
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006508 pathCtx := android.PathContextForTesting(config)
6509 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
6510 transformDexpreoptConfig(dexpreoptConfig)
6511 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
6512
Paul Duffinf38931c2021-02-05 16:58:28 +00006513 // Make sure that any changes to these dexpreopt properties are mirrored in the corresponding
Paul Duffin4fd997b2021-02-03 20:06:33 +00006514 // product variables.
Paul Duffinf38931c2021-02-05 16:58:28 +00006515 config.TestProductVariables.BootJars = dexpreoptConfig.BootJars
6516 config.TestProductVariables.UpdatableBootJars = dexpreoptConfig.UpdatableBootJars
6517
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006518 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6519 android.FailIfErrored(t, errs)
6520
6521 _, errs = ctx.PrepareBuildActions(config)
6522 if errmsg == "" {
6523 android.FailIfErrored(t, errs)
6524 } else if len(errs) > 0 {
6525 android.FailIfNoMatchingErrors(t, errmsg, errs)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006526 } else {
6527 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6528 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006529
6530 return ctx
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006531}
6532
Jooyung Han548640b2020-04-27 12:10:30 +09006533func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6534 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6535 apex {
6536 name: "myapex",
6537 key: "myapex.key",
6538 updatable: true,
6539 }
6540
6541 apex_key {
6542 name: "myapex.key",
6543 public_key: "testkey.avbpubkey",
6544 private_key: "testkey.pem",
6545 }
6546 `)
6547}
6548
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006549func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6550 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6551 apex {
6552 name: "myapex",
6553 key: "myapex.key",
6554 }
6555
6556 apex_key {
6557 name: "myapex.key",
6558 public_key: "testkey.avbpubkey",
6559 private_key: "testkey.pem",
6560 }
6561 `)
6562}
6563
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006564func TestNoUpdatableJarsInBootImage(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006565 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006566 var transform func(*dexpreopt.GlobalConfig)
6567
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006568 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
6569 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006570 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006571 }
6572 testNoUpdatableJarsInBootImage(t, "", transform)
6573 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006574
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006575 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffind376f792021-01-26 11:59:35 +00006576 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 +01006577 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006578 config.BootJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006579 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006580 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006581 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006582
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006583 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 -07006584 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 +01006585 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006586 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006587 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006588 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006589 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006590
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006591 t.Run("non-updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006592 err = `module "some-non-updatable-apex-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006593 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006594 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006595 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006596 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006597 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006598
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006599 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 -07006600 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 +01006601 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006602 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006603 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006604 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006605 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006606
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006607 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
6608 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006609 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006610 }
6611 testNoUpdatableJarsInBootImage(t, "", transform)
6612 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006613
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006614 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006615 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006616 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006617 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006618 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006619 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006620 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006621
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006622 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006623 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006624 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006625 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006626 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006627 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006628 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006629
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006630 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006631 err = `module "some-platform-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006632 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006633 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006634 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006635 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006636 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006637
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006638 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
6639 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006640 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006641 }
6642 testNoUpdatableJarsInBootImage(t, "", transform)
6643 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006644
6645}
6646
6647func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
6648 transform := func(config *dexpreopt.GlobalConfig) {
6649 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
6650 }
6651 t.Run("prebuilt no source", func(t *testing.T) {
6652 testDexpreoptWithApexes(t, `
6653 prebuilt_apex {
6654 name: "myapex" ,
6655 arch: {
6656 arm64: {
6657 src: "myapex-arm64.apex",
6658 },
6659 arm: {
6660 src: "myapex-arm.apex",
6661 },
6662 },
6663 exported_java_libs: ["libfoo"],
6664 }
6665
6666 java_import {
6667 name: "libfoo",
6668 jars: ["libfoo.jar"],
6669 }
6670`, "", transform)
6671 })
6672
6673 t.Run("prebuilt no source", func(t *testing.T) {
6674 testDexpreoptWithApexes(t, `
6675 prebuilt_apex {
6676 name: "myapex" ,
6677 arch: {
6678 arm64: {
6679 src: "myapex-arm64.apex",
6680 },
6681 arm: {
6682 src: "myapex-arm.apex",
6683 },
6684 },
6685 exported_java_libs: ["libfoo"],
6686 }
6687
6688 java_import {
6689 name: "libfoo",
6690 jars: ["libfoo.jar"],
6691 }
6692`, "", transform)
6693 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006694}
6695
Andrei Onea115e7e72020-06-05 21:14:03 +01006696func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6697 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006698 bp += `
6699 apex_key {
6700 name: "myapex.key",
6701 public_key: "testkey.avbpubkey",
6702 private_key: "testkey.pem",
6703 }`
6704 fs := map[string][]byte{
6705 "lib1/src/A.java": nil,
6706 "lib2/src/B.java": nil,
6707 "system/sepolicy/apex/myapex-file_contexts": nil,
6708 }
6709
Colin Crossae8600b2020-10-29 17:09:13 -07006710 config := android.TestArchConfig(buildDir, nil, bp, fs)
6711 android.SetTestNeverallowRules(config, rules)
6712 updatableBootJars := make([]string, 0, len(apexBootJars))
6713 for _, apexBootJar := range apexBootJars {
6714 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6715 }
6716 config.TestProductVariables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6717
6718 ctx := android.NewTestArchContext(config)
Andrei Onea115e7e72020-06-05 21:14:03 +01006719 ctx.RegisterModuleType("apex", BundleFactory)
6720 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
6721 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
6722 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006723 java.RegisterRequiredBuildComponentsForTest(ctx)
Andrei Onea115e7e72020-06-05 21:14:03 +01006724 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6725 ctx.PreDepsMutators(RegisterPreDepsMutators)
6726 ctx.PostDepsMutators(RegisterPostDepsMutators)
6727 ctx.PostDepsMutators(android.RegisterNeverallowMutator)
6728
Colin Crossae8600b2020-10-29 17:09:13 -07006729 ctx.Register()
Andrei Onea115e7e72020-06-05 21:14:03 +01006730
6731 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6732 android.FailIfErrored(t, errs)
6733
6734 _, errs = ctx.PrepareBuildActions(config)
6735 if errmsg == "" {
6736 android.FailIfErrored(t, errs)
6737 } else if len(errs) > 0 {
6738 android.FailIfNoMatchingErrors(t, errmsg, errs)
6739 return
6740 } else {
6741 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6742 }
6743}
6744
6745func TestApexPermittedPackagesRules(t *testing.T) {
6746 testcases := []struct {
6747 name string
6748 expectedError string
6749 bp string
6750 bootJars []string
6751 modulesPackages map[string][]string
6752 }{
6753
6754 {
6755 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6756 expectedError: "",
6757 bp: `
6758 java_library {
6759 name: "bcp_lib1",
6760 srcs: ["lib1/src/*.java"],
6761 permitted_packages: ["foo.bar"],
6762 apex_available: ["myapex"],
6763 sdk_version: "none",
6764 system_modules: "none",
6765 }
6766 java_library {
6767 name: "nonbcp_lib2",
6768 srcs: ["lib2/src/*.java"],
6769 apex_available: ["myapex"],
6770 permitted_packages: ["a.b"],
6771 sdk_version: "none",
6772 system_modules: "none",
6773 }
6774 apex {
6775 name: "myapex",
6776 key: "myapex.key",
6777 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006778 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006779 }`,
6780 bootJars: []string{"bcp_lib1"},
6781 modulesPackages: map[string][]string{
6782 "myapex": []string{
6783 "foo.bar",
6784 },
6785 },
6786 },
6787 {
6788 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6789 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.`,
6790 bp: `
6791 java_library {
6792 name: "bcp_lib1",
6793 srcs: ["lib1/src/*.java"],
6794 apex_available: ["myapex"],
6795 permitted_packages: ["foo.bar"],
6796 sdk_version: "none",
6797 system_modules: "none",
6798 }
6799 java_library {
6800 name: "bcp_lib2",
6801 srcs: ["lib2/src/*.java"],
6802 apex_available: ["myapex"],
6803 permitted_packages: ["foo.bar", "bar.baz"],
6804 sdk_version: "none",
6805 system_modules: "none",
6806 }
6807 apex {
6808 name: "myapex",
6809 key: "myapex.key",
6810 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006811 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006812 }
6813 `,
6814 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6815 modulesPackages: map[string][]string{
6816 "myapex": []string{
6817 "foo.bar",
6818 },
6819 },
6820 },
6821 }
6822 for _, tc := range testcases {
6823 t.Run(tc.name, func(t *testing.T) {
6824 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6825 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6826 })
6827 }
6828}
6829
Jiyong Park62304bb2020-04-13 16:19:48 +09006830func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006831 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09006832 apex {
6833 name: "myapex",
6834 key: "myapex.key",
6835 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006836 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09006837 }
6838
6839 apex_key {
6840 name: "myapex.key",
6841 public_key: "testkey.avbpubkey",
6842 private_key: "testkey.pem",
6843 }
6844
6845 cc_library {
6846 name: "mylib",
6847 srcs: ["mylib.cpp"],
6848 system_shared_libs: [],
6849 stl: "none",
6850 stubs: {
6851 versions: ["1"],
6852 },
6853 apex_available: ["myapex"],
6854 }
6855
6856 cc_library {
6857 name: "myprivlib",
6858 srcs: ["mylib.cpp"],
6859 system_shared_libs: [],
6860 stl: "none",
6861 apex_available: ["myapex"],
6862 }
6863
6864
6865 cc_test {
6866 name: "mytest",
6867 gtest: false,
6868 srcs: ["mylib.cpp"],
6869 system_shared_libs: [],
6870 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006871 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006872 test_for: ["myapex"]
6873 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006874
6875 cc_library {
6876 name: "mytestlib",
6877 srcs: ["mylib.cpp"],
6878 system_shared_libs: [],
6879 shared_libs: ["mylib", "myprivlib"],
6880 stl: "none",
6881 test_for: ["myapex"],
6882 }
6883
6884 cc_benchmark {
6885 name: "mybench",
6886 srcs: ["mylib.cpp"],
6887 system_shared_libs: [],
6888 shared_libs: ["mylib", "myprivlib"],
6889 stl: "none",
6890 test_for: ["myapex"],
6891 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006892 `)
6893
6894 // the test 'mytest' is a test for the apex, therefore is linked to the
6895 // actual implementation of mylib instead of its stub.
6896 ldFlags := ctx.ModuleForTests("mytest", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6897 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6898 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park46a512f2020-12-04 18:02:13 +09006899
6900 // The same should be true for cc_library
6901 ldFlags = ctx.ModuleForTests("mytestlib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
6902 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6903 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
6904
6905 // ... and for cc_benchmark
6906 ldFlags = ctx.ModuleForTests("mybench", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6907 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6908 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park62304bb2020-04-13 16:19:48 +09006909}
6910
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006911// TODO(jungjw): Move this to proptools
6912func intPtr(i int) *int {
6913 return &i
6914}
6915
6916func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006917 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006918 apex_set {
6919 name: "myapex",
6920 set: "myapex.apks",
6921 filename: "foo_v2.apex",
6922 overrides: ["foo"],
6923 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006924 `,
6925 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6926 variables.Platform_sdk_version = intPtr(30)
6927 }),
6928 android.FixtureModifyConfig(func(config android.Config) {
6929 config.Targets[android.Android] = []android.Target{
6930 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
6931 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
6932 }
6933 }),
6934 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006935
6936 m := ctx.ModuleForTests("myapex", "android_common")
6937
6938 // Check extract_apks tool parameters.
6939 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6940 actual := extractedApex.Args["abis"]
6941 expected := "ARMEABI_V7A,ARM64_V8A"
6942 if actual != expected {
6943 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6944 }
6945 actual = extractedApex.Args["sdk-version"]
6946 expected = "30"
6947 if actual != expected {
6948 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6949 }
6950
6951 a := m.Module().(*ApexSet)
6952 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07006953 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006954 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
6955 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
6956 }
6957}
6958
Jiyong Park7d95a512020-05-10 15:16:24 +09006959func TestNoStaticLinkingToStubsLib(t *testing.T) {
6960 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
6961 apex {
6962 name: "myapex",
6963 key: "myapex.key",
6964 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006965 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09006966 }
6967
6968 apex_key {
6969 name: "myapex.key",
6970 public_key: "testkey.avbpubkey",
6971 private_key: "testkey.pem",
6972 }
6973
6974 cc_library {
6975 name: "mylib",
6976 srcs: ["mylib.cpp"],
6977 static_libs: ["otherlib"],
6978 system_shared_libs: [],
6979 stl: "none",
6980 apex_available: [ "myapex" ],
6981 }
6982
6983 cc_library {
6984 name: "otherlib",
6985 srcs: ["mylib.cpp"],
6986 system_shared_libs: [],
6987 stl: "none",
6988 stubs: {
6989 versions: ["1", "2", "3"],
6990 },
6991 apex_available: [ "myapex" ],
6992 }
6993 `)
6994}
6995
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006996func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006997 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006998 apex {
6999 name: "myapex",
7000 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007001 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007002 }
7003
7004 apex_key {
7005 name: "myapex.key",
7006 public_key: "testkey.avbpubkey",
7007 private_key: "testkey.pem",
7008 }
7009
7010 prebuilt_apex {
7011 name: "myapex",
7012 prefer: true,
7013 arch: {
7014 arm64: {
7015 src: "myapex-arm64.apex",
7016 },
7017 arm: {
7018 src: "myapex-arm.apex",
7019 },
7020 },
7021 }
7022
7023 apex_set {
7024 name: "myapex_set",
7025 set: "myapex.apks",
7026 filename: "myapex_set.apex",
7027 overrides: ["myapex"],
7028 }
7029 `)
7030
7031 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7032 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7033 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 +09007034 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 +09007035}
7036
Jooyung Han938b5932020-06-20 12:47:47 +09007037func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007038 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007039 apex {
7040 name: "myapex",
7041 key: "myapex.key",
7042 apps: ["app"],
7043 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007044 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007045 }
7046
7047 apex_key {
7048 name: "myapex.key",
7049 public_key: "testkey.avbpubkey",
7050 private_key: "testkey.pem",
7051 }
7052
7053 android_app {
7054 name: "app",
7055 srcs: ["foo/bar/MyClass.java"],
7056 package_name: "foo",
7057 sdk_version: "none",
7058 system_modules: "none",
7059 apex_available: [ "myapex" ],
7060 }
7061 `, withFiles(map[string][]byte{
7062 "sub/Android.bp": []byte(`
7063 override_apex {
7064 name: "override_myapex",
7065 base: "myapex",
7066 apps: ["override_app"],
7067 allowed_files: ":allowed",
7068 }
7069 // Overridable "path" property should be referenced indirectly
7070 filegroup {
7071 name: "allowed",
7072 srcs: ["allowed.txt"],
7073 }
7074 override_android_app {
7075 name: "override_app",
7076 base: "app",
7077 package_name: "bar",
7078 }
7079 `),
7080 }))
7081
7082 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7083 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7084 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7085 }
7086
7087 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7088 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7089 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7090 }
7091}
7092
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007093func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007094 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007095 apex {
7096 name: "myapex",
7097 key: "myapex.key",
7098 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007099 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007100 }
7101
7102 apex_key {
7103 name: "myapex.key",
7104 public_key: "testkey.avbpubkey",
7105 private_key: "testkey.pem",
7106 }
7107
7108 cc_library {
7109 name: "mylib",
7110 srcs: ["mylib.cpp"],
7111 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007112 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007113 },
7114 apex_available: ["myapex"],
7115 }
7116
7117 cc_prebuilt_library_shared {
7118 name: "mylib",
7119 prefer: false,
7120 srcs: ["prebuilt.so"],
7121 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007122 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007123 },
7124 apex_available: ["myapex"],
7125 }
7126 `)
7127}
7128
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007129func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007130 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007131 apex {
7132 name: "myapex",
7133 key: "myapex.key",
7134 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007135 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007136 }
7137 apex_key {
7138 name: "myapex.key",
7139 public_key: "testkey.avbpubkey",
7140 private_key: "testkey.pem",
7141 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007142 `,
7143 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7144 variables.CompressedApex = proptools.BoolPtr(true)
7145 }),
7146 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007147
7148 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7149 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7150
7151 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7152 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7153
7154 // Make sure output of bundle is .capex
7155 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7156 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7157
7158 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007159 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007160 var builder strings.Builder
7161 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7162 androidMk := builder.String()
7163 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7164}
7165
Martin Stjernholm2856c662020-12-02 15:03:42 +00007166func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007167 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007168 apex {
7169 name: "myapex",
7170 key: "myapex.key",
7171 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007172 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007173 }
7174
7175 apex_key {
7176 name: "myapex.key",
7177 public_key: "testkey.avbpubkey",
7178 private_key: "testkey.pem",
7179 }
7180
7181 cc_library {
7182 name: "mylib",
7183 srcs: ["mylib.cpp"],
7184 apex_available: ["myapex"],
7185 shared_libs: ["otherlib"],
7186 system_shared_libs: [],
7187 }
7188
7189 cc_library {
7190 name: "otherlib",
7191 srcs: ["mylib.cpp"],
7192 stubs: {
7193 versions: ["current"],
7194 },
7195 }
7196
7197 cc_prebuilt_library_shared {
7198 name: "otherlib",
7199 prefer: true,
7200 srcs: ["prebuilt.so"],
7201 stubs: {
7202 versions: ["current"],
7203 },
7204 }
7205 `)
7206
7207 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007208 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007209 var builder strings.Builder
7210 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7211 androidMk := builder.String()
7212
7213 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7214 // a thing there.
7215 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7216}
7217
Jiyong Parke3867542020-12-03 17:28:25 +09007218func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007219 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007220 apex {
7221 name: "myapex",
7222 key: "myapex.key",
7223 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007224 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007225 }
7226
7227 apex_key {
7228 name: "myapex.key",
7229 public_key: "testkey.avbpubkey",
7230 private_key: "testkey.pem",
7231 }
7232
7233 cc_library {
7234 name: "mylib",
7235 srcs: ["mylib.cpp"],
7236 system_shared_libs: [],
7237 stl: "none",
7238 apex_available: ["myapex"],
7239 shared_libs: ["mylib2"],
7240 target: {
7241 apex: {
7242 exclude_shared_libs: ["mylib2"],
7243 },
7244 },
7245 }
7246
7247 cc_library {
7248 name: "mylib2",
7249 srcs: ["mylib.cpp"],
7250 system_shared_libs: [],
7251 stl: "none",
7252 }
7253 `)
7254
7255 // Check if mylib is linked to mylib2 for the non-apex target
7256 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7257 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7258
7259 // Make sure that the link doesn't occur for the apex target
7260 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7261 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7262
7263 // It shouldn't appear in the copy cmd as well.
7264 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7265 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7266}
7267
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007268func TestPrebuiltStubLibDep(t *testing.T) {
7269 bpBase := `
7270 apex {
7271 name: "myapex",
7272 key: "myapex.key",
7273 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007274 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007275 }
7276 apex_key {
7277 name: "myapex.key",
7278 public_key: "testkey.avbpubkey",
7279 private_key: "testkey.pem",
7280 }
7281 cc_library {
7282 name: "mylib",
7283 srcs: ["mylib.cpp"],
7284 apex_available: ["myapex"],
7285 shared_libs: ["stublib"],
7286 system_shared_libs: [],
7287 }
7288 apex {
7289 name: "otherapex",
7290 enabled: %s,
7291 key: "myapex.key",
7292 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007293 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007294 }
7295 `
7296
7297 stublibSourceBp := `
7298 cc_library {
7299 name: "stublib",
7300 srcs: ["mylib.cpp"],
7301 apex_available: ["otherapex"],
7302 system_shared_libs: [],
7303 stl: "none",
7304 stubs: {
7305 versions: ["1"],
7306 },
7307 }
7308 `
7309
7310 stublibPrebuiltBp := `
7311 cc_prebuilt_library_shared {
7312 name: "stublib",
7313 srcs: ["prebuilt.so"],
7314 apex_available: ["otherapex"],
7315 stubs: {
7316 versions: ["1"],
7317 },
7318 %s
7319 }
7320 `
7321
7322 tests := []struct {
7323 name string
7324 stublibBp string
7325 usePrebuilt bool
7326 modNames []string // Modules to collect AndroidMkEntries for
7327 otherApexEnabled []string
7328 }{
7329 {
7330 name: "only_source",
7331 stublibBp: stublibSourceBp,
7332 usePrebuilt: false,
7333 modNames: []string{"stublib"},
7334 otherApexEnabled: []string{"true", "false"},
7335 },
7336 {
7337 name: "source_preferred",
7338 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7339 usePrebuilt: false,
7340 modNames: []string{"stublib", "prebuilt_stublib"},
7341 otherApexEnabled: []string{"true", "false"},
7342 },
7343 {
7344 name: "prebuilt_preferred",
7345 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7346 usePrebuilt: true,
7347 modNames: []string{"stublib", "prebuilt_stublib"},
7348 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7349 },
7350 {
7351 name: "only_prebuilt",
7352 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7353 usePrebuilt: true,
7354 modNames: []string{"stublib"},
7355 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7356 },
7357 }
7358
7359 for _, test := range tests {
7360 t.Run(test.name, func(t *testing.T) {
7361 for _, otherApexEnabled := range test.otherApexEnabled {
7362 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007363 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007364
7365 type modAndMkEntries struct {
7366 mod *cc.Module
7367 mkEntries android.AndroidMkEntries
7368 }
7369 entries := []*modAndMkEntries{}
7370
7371 // Gather shared lib modules that are installable
7372 for _, modName := range test.modNames {
7373 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7374 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7375 continue
7376 }
7377 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007378 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007379 continue
7380 }
Colin Crossaa255532020-07-03 13:18:24 -07007381 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007382 if ent.Disabled {
7383 continue
7384 }
7385 entries = append(entries, &modAndMkEntries{
7386 mod: mod,
7387 mkEntries: ent,
7388 })
7389 }
7390 }
7391 }
7392
7393 var entry *modAndMkEntries = nil
7394 for _, ent := range entries {
7395 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7396 if entry != nil {
7397 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7398 } else {
7399 entry = ent
7400 }
7401 }
7402 }
7403
7404 if entry == nil {
7405 t.Errorf("AndroidMk entry for \"stublib\" missing")
7406 } else {
7407 isPrebuilt := entry.mod.Prebuilt() != nil
7408 if isPrebuilt != test.usePrebuilt {
7409 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7410 }
7411 if !entry.mod.IsStubs() {
7412 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7413 }
7414 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7415 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7416 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007417 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
7418 expected := "-D__STUBLIB_API__=1"
7419 if !android.InList(expected, cflags) {
7420 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7421 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007422 }
7423 })
7424 }
7425 })
7426 }
7427}
7428
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007429func TestMain(m *testing.M) {
7430 run := func() int {
7431 setUp()
7432 defer tearDown()
7433
7434 return m.Run()
7435 }
7436
7437 os.Exit(run())
7438}