blob: 515cb455f040cbc204178cbb928e130ebc48fe0d [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 Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Jooyung Han31c470b2019-10-18 16:26:59 +090022 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090023 "strings"
24 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090025
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090030 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090031)
32
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070033var buildDir string
34
Jooyung Hand3639552019-08-09 12:57:43 +090035// names returns name list from white space separated string
36func names(s string) (ns []string) {
37 for _, n := range strings.Split(s, " ") {
38 if len(n) > 0 {
39 ns = append(ns, n)
40 }
41 }
42 return
43}
44
Jooyung Han344d5432019-08-23 11:17:39 +090045func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
46 t.Helper()
47 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090048 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
49 if len(errs) > 0 {
50 android.FailIfNoMatchingErrors(t, pattern, errs)
51 return
52 }
53 _, errs = ctx.PrepareBuildActions(config)
54 if len(errs) > 0 {
55 android.FailIfNoMatchingErrors(t, pattern, errs)
56 return
57 }
58
59 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
60}
61
Jooyung Han344d5432019-08-23 11:17:39 +090062func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
63 t.Helper()
64 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090065 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
66 android.FailIfErrored(t, errs)
67 _, errs = ctx.PrepareBuildActions(config)
68 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070069 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090070}
71
Jooyung Han344d5432019-08-23 11:17:39 +090072type testCustomizer func(fs map[string][]byte, config android.Config)
73
74func withFiles(files map[string][]byte) testCustomizer {
75 return func(fs map[string][]byte, config android.Config) {
76 for k, v := range files {
77 fs[k] = v
78 }
79 }
80}
81
82func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
83 return func(fs map[string][]byte, config android.Config) {
84 for k, v := range targets {
85 config.Targets[k] = v
86 }
87 }
88}
89
Jooyung Han31c470b2019-10-18 16:26:59 +090090func withBinder32bit(fs map[string][]byte, config android.Config) {
91 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
92}
93
Jiyong Park7cd10e32020-01-14 09:22:18 +090094func withUnbundledBuild(fs map[string][]byte, config android.Config) {
95 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
96}
97
Jooyung Han344d5432019-08-23 11:17:39 +090098func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +090099 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900100
101 bp = bp + `
102 toolchain_library {
103 name: "libcompiler_rt-extras",
104 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900105 vendor_available: true,
106 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900107 }
108
109 toolchain_library {
110 name: "libatomic",
111 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900112 vendor_available: true,
113 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900114 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900115 }
116
117 toolchain_library {
118 name: "libgcc",
119 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900120 vendor_available: true,
121 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900122 }
123
124 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700125 name: "libgcc_stripped",
126 src: "",
127 vendor_available: true,
128 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900129 native_bridge_supported: true,
Yi Kongacee27c2019-03-29 20:05:14 -0700130 }
131
132 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900133 name: "libclang_rt.builtins-aarch64-android",
134 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900135 vendor_available: true,
136 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900137 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900138 }
139
140 toolchain_library {
141 name: "libclang_rt.builtins-arm-android",
142 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900143 vendor_available: true,
144 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900145 native_bridge_supported: true,
146 }
147
148 toolchain_library {
149 name: "libclang_rt.builtins-x86_64-android",
150 src: "",
151 vendor_available: true,
152 recovery_available: true,
153 native_bridge_supported: true,
154 }
155
156 toolchain_library {
157 name: "libclang_rt.builtins-i686-android",
158 src: "",
159 vendor_available: true,
160 recovery_available: true,
161 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900162 }
163
164 cc_object {
165 name: "crtbegin_so",
166 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900167 vendor_available: true,
168 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900169 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900170 }
171
172 cc_object {
173 name: "crtend_so",
174 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900175 vendor_available: true,
176 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900177 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900178 }
179
Alex Light3d673592019-01-18 14:37:31 -0800180 cc_object {
181 name: "crtbegin_static",
182 stl: "none",
183 }
184
185 cc_object {
186 name: "crtend_android",
187 stl: "none",
188 }
189
Jiyong Parkda6eb592018-12-19 17:12:36 +0900190 llndk_library {
191 name: "libc",
192 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900193 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900194 }
195
196 llndk_library {
197 name: "libm",
198 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900199 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900200 }
201
202 llndk_library {
203 name: "libdl",
204 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900205 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900206 }
Jooyung Han54aca7b2019-11-20 02:26:02 +0900207
208 filegroup {
209 name: "myapex-file_contexts",
210 srcs: [
211 "system/sepolicy/apex/myapex-file_contexts",
212 ],
213 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900214 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800215
Dario Frenicde2a032019-10-27 00:29:22 +0100216 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900217
Jooyung Han344d5432019-08-23 11:17:39 +0900218 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900219 "a.java": nil,
220 "PrebuiltAppFoo.apk": nil,
221 "PrebuiltAppFooPriv.apk": nil,
222 "build/make/target/product/security": nil,
223 "apex_manifest.json": nil,
224 "AndroidManifest.xml": nil,
225 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900226 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900227 "system/sepolicy/apex/otherapex-file_contexts": nil,
228 "system/sepolicy/apex/commonapex-file_contexts": nil,
229 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800230 "mylib.cpp": nil,
231 "mylib_common.cpp": nil,
232 "mytest.cpp": nil,
233 "mytest1.cpp": nil,
234 "mytest2.cpp": nil,
235 "mytest3.cpp": nil,
236 "myprebuilt": nil,
237 "my_include": nil,
238 "foo/bar/MyClass.java": nil,
239 "prebuilt.jar": nil,
240 "vendor/foo/devkeys/test.x509.pem": nil,
241 "vendor/foo/devkeys/test.pk8": nil,
242 "testkey.x509.pem": nil,
243 "testkey.pk8": nil,
244 "testkey.override.x509.pem": nil,
245 "testkey.override.pk8": nil,
246 "vendor/foo/devkeys/testkey.avbpubkey": nil,
247 "vendor/foo/devkeys/testkey.pem": nil,
248 "NOTICE": nil,
249 "custom_notice": nil,
250 "testkey2.avbpubkey": nil,
251 "testkey2.pem": nil,
252 "myapex-arm64.apex": nil,
253 "myapex-arm.apex": nil,
254 "frameworks/base/api/current.txt": nil,
255 "framework/aidl/a.aidl": nil,
256 "build/make/core/proguard.flags": nil,
257 "build/make/core/proguard_basic_keeps.flags": nil,
258 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900259 }
260
261 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800262 // The fs now needs to be populated before creating the config, call handlers twice
263 // for now, once to get any fs changes, and later after the config was created to
264 // set product variables or targets.
265 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
266 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900267 }
268
Colin Cross98be1bb2019-12-13 20:41:13 -0800269 config := android.TestArchConfig(buildDir, nil, bp, fs)
270 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
271 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
272 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
273 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
274 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
275 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
276
277 for _, handler := range handlers {
278 // The fs now needs to be populated before creating the config, call handlers twice
279 // for now, earlier to get any fs changes, and now after the config was created to
280 // set product variables or targets.
281 tempFS := map[string][]byte{}
282 handler(tempFS, config)
283 }
284
285 ctx := android.NewTestArchContext()
286 ctx.RegisterModuleType("apex", BundleFactory)
287 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
288 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
289 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
290 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
291 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
292 ctx.RegisterModuleType("override_apex", overrideApexFactory)
293
Jooyung Hana57af4a2020-01-23 05:36:59 +0000294 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
295 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
296
Paul Duffin77980a82019-12-19 16:01:36 +0000297 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800298 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800299 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
300 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800301 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
302 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800303 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000304 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000305 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000306 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900307 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800308
Colin Cross98be1bb2019-12-13 20:41:13 -0800309 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800310 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800311
312 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900313
Jooyung Han5c998b92019-06-27 11:30:33 +0900314 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900315}
316
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700317func setUp() {
318 var err error
319 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900320 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700321 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900322 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323}
324
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700325func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900326 os.RemoveAll(buildDir)
327}
328
329// ensure that 'result' contains 'expected'
330func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900331 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900332 if !strings.Contains(result, expected) {
333 t.Errorf("%q is not found in %q", expected, result)
334 }
335}
336
337// ensures that 'result' does not contain 'notExpected'
338func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900339 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900340 if strings.Contains(result, notExpected) {
341 t.Errorf("%q is found in %q", notExpected, result)
342 }
343}
344
345func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900346 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900347 if !android.InList(expected, result) {
348 t.Errorf("%q is not found in %v", expected, result)
349 }
350}
351
352func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900353 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900354 if android.InList(notExpected, result) {
355 t.Errorf("%q is found in %v", notExpected, result)
356 }
357}
358
Jooyung Hane1633032019-08-01 17:41:43 +0900359func ensureListEmpty(t *testing.T, result []string) {
360 t.Helper()
361 if len(result) > 0 {
362 t.Errorf("%q is expected to be empty", result)
363 }
364}
365
Jiyong Park25fc6a92018-11-18 18:02:45 +0900366// Minimal test
367func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700368 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900369 apex_defaults {
370 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900371 manifest: ":myapex.manifest",
372 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900373 key: "myapex.key",
374 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800375 multilib: {
376 both: {
377 binaries: ["foo",],
378 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900379 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900380 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900381 }
382
Jiyong Park30ca9372019-02-07 16:27:23 +0900383 apex {
384 name: "myapex",
385 defaults: ["myapex-defaults"],
386 }
387
Jiyong Park25fc6a92018-11-18 18:02:45 +0900388 apex_key {
389 name: "myapex.key",
390 public_key: "testkey.avbpubkey",
391 private_key: "testkey.pem",
392 }
393
Jiyong Park809bb722019-02-13 21:33:49 +0900394 filegroup {
395 name: "myapex.manifest",
396 srcs: ["apex_manifest.json"],
397 }
398
399 filegroup {
400 name: "myapex.androidmanifest",
401 srcs: ["AndroidManifest.xml"],
402 }
403
Jiyong Park25fc6a92018-11-18 18:02:45 +0900404 cc_library {
405 name: "mylib",
406 srcs: ["mylib.cpp"],
407 shared_libs: ["mylib2"],
408 system_shared_libs: [],
409 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000410 // TODO: remove //apex_available:platform
411 apex_available: [
412 "//apex_available:platform",
413 "myapex",
414 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900415 }
416
Alex Light3d673592019-01-18 14:37:31 -0800417 cc_binary {
418 name: "foo",
419 srcs: ["mylib.cpp"],
420 compile_multilib: "both",
421 multilib: {
422 lib32: {
423 suffix: "32",
424 },
425 lib64: {
426 suffix: "64",
427 },
428 },
429 symlinks: ["foo_link_"],
430 symlink_preferred_arch: true,
431 system_shared_libs: [],
432 static_executable: true,
433 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000434 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800435 }
436
Jiyong Park25fc6a92018-11-18 18:02:45 +0900437 cc_library {
438 name: "mylib2",
439 srcs: ["mylib.cpp"],
440 system_shared_libs: [],
441 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900442 notice: "custom_notice",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000443 // TODO: remove //apex_available:platform
444 apex_available: [
445 "//apex_available:platform",
446 "myapex",
447 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900448 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900449
450 java_library {
451 name: "myjar",
452 srcs: ["foo/bar/MyClass.java"],
453 sdk_version: "none",
454 system_modules: "none",
455 compile_dex: true,
456 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900457 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000458 // TODO: remove //apex_available:platform
459 apex_available: [
460 "//apex_available:platform",
461 "myapex",
462 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900463 }
464
465 java_library {
466 name: "myotherjar",
467 srcs: ["foo/bar/MyClass.java"],
468 sdk_version: "none",
469 system_modules: "none",
470 compile_dex: true,
471 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900472
473 java_library {
474 name: "mysharedjar",
475 srcs: ["foo/bar/MyClass.java"],
476 sdk_version: "none",
477 system_modules: "none",
478 compile_dex: true,
479 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900480 `)
481
Sundong Ahnabb64432019-10-22 13:58:29 +0900482 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900483
484 optFlags := apexRule.Args["opt_flags"]
485 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700486 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900487 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900488
Jiyong Park25fc6a92018-11-18 18:02:45 +0900489 copyCmds := apexRule.Args["copy_commands"]
490
491 // Ensure that main rule creates an output
492 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
493
494 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800495 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900496 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900497
498 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800499 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900500 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900501
502 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800503 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
504 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900505 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
506 // .. but not for java libs
507 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900508 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800509
Colin Cross7113d202019-11-20 16:39:12 -0800510 // Ensure that the platform variant ends with _shared or _common
511 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
512 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900513 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
514 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900515 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
516
517 // Ensure that dynamic dependency to java libs are not included
518 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800519
520 // Ensure that all symlinks are present.
521 found_foo_link_64 := false
522 found_foo := false
523 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900524 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800525 if strings.HasSuffix(cmd, "bin/foo") {
526 found_foo = true
527 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
528 found_foo_link_64 = true
529 }
530 }
531 }
532 good := found_foo && found_foo_link_64
533 if !good {
534 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
535 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900536
Sundong Ahnabb64432019-10-22 13:58:29 +0900537 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700538 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700539 if len(noticeInputs) != 2 {
540 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900541 }
542 ensureListContains(t, noticeInputs, "NOTICE")
543 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900544
545 depsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("myapex-deps-info.txt").Args["content"], "\\n")
546 ensureListContains(t, depsInfo, "internal myjar")
547 ensureListContains(t, depsInfo, "internal mylib")
548 ensureListContains(t, depsInfo, "internal mylib2")
549 ensureListContains(t, depsInfo, "internal myotherjar")
Alex Light5098a612018-11-29 17:12:15 -0800550}
551
Jooyung Hanf21c7972019-12-16 22:32:06 +0900552func TestDefaults(t *testing.T) {
553 ctx, _ := testApex(t, `
554 apex_defaults {
555 name: "myapex-defaults",
556 key: "myapex.key",
557 prebuilts: ["myetc"],
558 native_shared_libs: ["mylib"],
559 java_libs: ["myjar"],
560 apps: ["AppFoo"],
561 }
562
563 prebuilt_etc {
564 name: "myetc",
565 src: "myprebuilt",
566 }
567
568 apex {
569 name: "myapex",
570 defaults: ["myapex-defaults"],
571 }
572
573 apex_key {
574 name: "myapex.key",
575 public_key: "testkey.avbpubkey",
576 private_key: "testkey.pem",
577 }
578
579 cc_library {
580 name: "mylib",
581 system_shared_libs: [],
582 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000583 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900584 }
585
586 java_library {
587 name: "myjar",
588 srcs: ["foo/bar/MyClass.java"],
589 sdk_version: "none",
590 system_modules: "none",
591 compile_dex: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000592 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900593 }
594
595 android_app {
596 name: "AppFoo",
597 srcs: ["foo/bar/MyClass.java"],
598 sdk_version: "none",
599 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000600 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900601 }
602 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000603 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900604 "etc/myetc",
605 "javalib/myjar.jar",
606 "lib64/mylib.so",
607 "app/AppFoo/AppFoo.apk",
608 })
609}
610
Jooyung Han01a3ee22019-11-02 02:52:25 +0900611func TestApexManifest(t *testing.T) {
612 ctx, _ := testApex(t, `
613 apex {
614 name: "myapex",
615 key: "myapex.key",
616 }
617
618 apex_key {
619 name: "myapex.key",
620 public_key: "testkey.avbpubkey",
621 private_key: "testkey.pem",
622 }
623 `)
624
625 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900626 args := module.Rule("apexRule").Args
627 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
628 t.Error("manifest should be apex_manifest.pb, but " + manifest)
629 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900630}
631
Alex Light5098a612018-11-29 17:12:15 -0800632func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700633 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800634 apex {
635 name: "myapex",
636 key: "myapex.key",
637 payload_type: "zip",
638 native_shared_libs: ["mylib"],
639 }
640
641 apex_key {
642 name: "myapex.key",
643 public_key: "testkey.avbpubkey",
644 private_key: "testkey.pem",
645 }
646
647 cc_library {
648 name: "mylib",
649 srcs: ["mylib.cpp"],
650 shared_libs: ["mylib2"],
651 system_shared_libs: [],
652 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000653 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800654 }
655
656 cc_library {
657 name: "mylib2",
658 srcs: ["mylib.cpp"],
659 system_shared_libs: [],
660 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000661 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800662 }
663 `)
664
Sundong Ahnabb64432019-10-22 13:58:29 +0900665 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800666 copyCmds := zipApexRule.Args["copy_commands"]
667
668 // Ensure that main rule creates an output
669 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
670
671 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800672 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800673
674 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800675 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800676
677 // Ensure that both direct and indirect deps are copied into apex
678 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
679 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900680}
681
682func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700683 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900684 apex {
685 name: "myapex",
686 key: "myapex.key",
687 native_shared_libs: ["mylib", "mylib3"],
688 }
689
690 apex_key {
691 name: "myapex.key",
692 public_key: "testkey.avbpubkey",
693 private_key: "testkey.pem",
694 }
695
696 cc_library {
697 name: "mylib",
698 srcs: ["mylib.cpp"],
699 shared_libs: ["mylib2", "mylib3"],
700 system_shared_libs: [],
701 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000702 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900703 }
704
705 cc_library {
706 name: "mylib2",
707 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900708 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900709 system_shared_libs: [],
710 stl: "none",
711 stubs: {
712 versions: ["1", "2", "3"],
713 },
714 }
715
716 cc_library {
717 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900718 srcs: ["mylib.cpp"],
719 shared_libs: ["mylib4"],
720 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900721 stl: "none",
722 stubs: {
723 versions: ["10", "11", "12"],
724 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000725 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900726 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900727
728 cc_library {
729 name: "mylib4",
730 srcs: ["mylib.cpp"],
731 system_shared_libs: [],
732 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000733 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900734 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900735 `)
736
Sundong Ahnabb64432019-10-22 13:58:29 +0900737 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900738 copyCmds := apexRule.Args["copy_commands"]
739
740 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800741 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900742
743 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800744 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900745
746 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800747 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900748
Colin Cross7113d202019-11-20 16:39:12 -0800749 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900750
751 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900752 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900753 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900754 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900755
756 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Cross7113d202019-11-20 16:39:12 -0800757 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900758 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800759 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900760
761 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800762 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900763 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900764
765 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900766 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900767
Jooyung Hana57af4a2020-01-23 05:36:59 +0000768 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900769 "lib64/mylib.so",
770 "lib64/mylib3.so",
771 "lib64/mylib4.so",
772 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900773}
774
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900775func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700776 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900777 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900778 name: "myapex2",
779 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900780 native_shared_libs: ["mylib"],
781 }
782
783 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900784 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900785 public_key: "testkey.avbpubkey",
786 private_key: "testkey.pem",
787 }
788
789 cc_library {
790 name: "mylib",
791 srcs: ["mylib.cpp"],
792 shared_libs: ["libfoo#10"],
793 system_shared_libs: [],
794 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000795 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900796 }
797
798 cc_library {
799 name: "libfoo",
800 srcs: ["mylib.cpp"],
801 shared_libs: ["libbar"],
802 system_shared_libs: [],
803 stl: "none",
804 stubs: {
805 versions: ["10", "20", "30"],
806 },
807 }
808
809 cc_library {
810 name: "libbar",
811 srcs: ["mylib.cpp"],
812 system_shared_libs: [],
813 stl: "none",
814 }
815
816 `)
817
Jiyong Park83dc74b2020-01-14 18:38:44 +0900818 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900819 copyCmds := apexRule.Args["copy_commands"]
820
821 // Ensure that direct non-stubs dep is always included
822 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
823
824 // Ensure that indirect stubs dep is not included
825 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
826
827 // Ensure that dependency of stubs is not included
828 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
829
Jiyong Park83dc74b2020-01-14 18:38:44 +0900830 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900831
832 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900833 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900834 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900835 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900836
Jiyong Park3ff16992019-12-27 14:11:47 +0900837 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900838
839 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
840 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900841
842 depsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("myapex2-deps-info.txt").Args["content"], "\\n")
843 ensureListContains(t, depsInfo, "internal mylib")
844 ensureListContains(t, depsInfo, "external libfoo")
845 ensureListNotContains(t, depsInfo, "internal libfoo")
846 ensureListNotContains(t, depsInfo, "external mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900847}
848
Jooyung Hand3639552019-08-09 12:57:43 +0900849func TestApexWithRuntimeLibsDependency(t *testing.T) {
850 /*
851 myapex
852 |
853 v (runtime_libs)
854 mylib ------+------> libfoo [provides stub]
855 |
856 `------> libbar
857 */
858 ctx, _ := testApex(t, `
859 apex {
860 name: "myapex",
861 key: "myapex.key",
862 native_shared_libs: ["mylib"],
863 }
864
865 apex_key {
866 name: "myapex.key",
867 public_key: "testkey.avbpubkey",
868 private_key: "testkey.pem",
869 }
870
871 cc_library {
872 name: "mylib",
873 srcs: ["mylib.cpp"],
874 runtime_libs: ["libfoo", "libbar"],
875 system_shared_libs: [],
876 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000877 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900878 }
879
880 cc_library {
881 name: "libfoo",
882 srcs: ["mylib.cpp"],
883 system_shared_libs: [],
884 stl: "none",
885 stubs: {
886 versions: ["10", "20", "30"],
887 },
888 }
889
890 cc_library {
891 name: "libbar",
892 srcs: ["mylib.cpp"],
893 system_shared_libs: [],
894 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000895 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900896 }
897
898 `)
899
Sundong Ahnabb64432019-10-22 13:58:29 +0900900 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900901 copyCmds := apexRule.Args["copy_commands"]
902
903 // Ensure that direct non-stubs dep is always included
904 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
905
906 // Ensure that indirect stubs dep is not included
907 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
908
909 // Ensure that runtime_libs dep in included
910 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
911
Sundong Ahnabb64432019-10-22 13:58:29 +0900912 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900913 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
914 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900915
916}
917
Jooyung Han9c80bae2019-08-20 17:30:57 +0900918func TestApexDependencyToLLNDK(t *testing.T) {
919 ctx, _ := testApex(t, `
920 apex {
921 name: "myapex",
922 key: "myapex.key",
923 use_vendor: true,
924 native_shared_libs: ["mylib"],
925 }
926
927 apex_key {
928 name: "myapex.key",
929 public_key: "testkey.avbpubkey",
930 private_key: "testkey.pem",
931 }
932
933 cc_library {
934 name: "mylib",
935 srcs: ["mylib.cpp"],
936 vendor_available: true,
937 shared_libs: ["libbar"],
938 system_shared_libs: [],
939 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000940 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900941 }
942
943 cc_library {
944 name: "libbar",
945 srcs: ["mylib.cpp"],
946 system_shared_libs: [],
947 stl: "none",
948 }
949
950 llndk_library {
951 name: "libbar",
952 symbol_file: "",
953 }
Jooyung Handc782442019-11-01 03:14:38 +0900954 `, func(fs map[string][]byte, config android.Config) {
955 setUseVendorWhitelistForTest(config, []string{"myapex"})
956 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900957
Sundong Ahnabb64432019-10-22 13:58:29 +0900958 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900959 copyCmds := apexRule.Args["copy_commands"]
960
961 // Ensure that LLNDK dep is not included
962 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
963
Sundong Ahnabb64432019-10-22 13:58:29 +0900964 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900965 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900966
967 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900968 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900969
970}
971
Jiyong Park25fc6a92018-11-18 18:02:45 +0900972func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700973 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900974 apex {
975 name: "myapex",
976 key: "myapex.key",
977 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
978 }
979
980 apex_key {
981 name: "myapex.key",
982 public_key: "testkey.avbpubkey",
983 private_key: "testkey.pem",
984 }
985
986 cc_library {
987 name: "mylib",
988 srcs: ["mylib.cpp"],
989 shared_libs: ["libdl#27"],
990 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000991 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900992 }
993
994 cc_library_shared {
995 name: "mylib_shared",
996 srcs: ["mylib.cpp"],
997 shared_libs: ["libdl#27"],
998 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000999 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001000 }
1001
1002 cc_library {
1003 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -07001004 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001005 nocrt: true,
1006 system_shared_libs: [],
1007 stl: "none",
1008 stubs: {
1009 versions: ["27", "28", "29"],
1010 },
1011 }
1012
1013 cc_library {
1014 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -07001015 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001016 nocrt: true,
1017 system_shared_libs: [],
1018 stl: "none",
1019 stubs: {
1020 versions: ["27", "28", "29"],
1021 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001022 apex_available: [
1023 "//apex_available:platform",
1024 "myapex"
1025 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001026 }
1027
1028 cc_library {
1029 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -07001030 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001031 nocrt: true,
1032 system_shared_libs: [],
1033 stl: "none",
1034 stubs: {
1035 versions: ["27", "28", "29"],
1036 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001037 apex_available: [
1038 "//apex_available:platform",
1039 "myapex"
1040 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001041 }
Jiyong Parkb0788572018-12-20 22:10:17 +09001042
1043 cc_library {
1044 name: "libBootstrap",
1045 srcs: ["mylib.cpp"],
1046 stl: "none",
1047 bootstrap: true,
1048 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001049 `)
1050
Sundong Ahnabb64432019-10-22 13:58:29 +09001051 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001052 copyCmds := apexRule.Args["copy_commands"]
1053
1054 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001055 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001056 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1057 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001058
1059 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001060 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001061
Colin Cross7113d202019-11-20 16:39:12 -08001062 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1063 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1064 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001065
1066 // For dependency to libc
1067 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001068 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001069 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001070 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001071 // ... Cflags from stub is correctly exported to mylib
1072 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1073 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1074
1075 // For dependency to libm
1076 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001077 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001078 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001079 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001080 // ... and is not compiling with the stub
1081 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1082 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1083
1084 // For dependency to libdl
1085 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001086 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001087 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001088 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1089 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001090 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001091 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001092 // ... Cflags from stub is correctly exported to mylib
1093 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1094 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001095
1096 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001097 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1098 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1099 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1100 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001101}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001102
1103func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001104 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001105 apex {
1106 name: "myapex",
1107 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001108 native_shared_libs: ["mylib"],
1109 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001110 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001111 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001112 }
1113
1114 apex_key {
1115 name: "myapex.key",
1116 public_key: "testkey.avbpubkey",
1117 private_key: "testkey.pem",
1118 }
1119
1120 prebuilt_etc {
1121 name: "myetc",
1122 src: "myprebuilt",
1123 sub_dir: "foo/bar",
1124 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001125
1126 cc_library {
1127 name: "mylib",
1128 srcs: ["mylib.cpp"],
1129 relative_install_path: "foo/bar",
1130 system_shared_libs: [],
1131 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001132 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001133 }
1134
1135 cc_binary {
1136 name: "mybin",
1137 srcs: ["mylib.cpp"],
1138 relative_install_path: "foo/bar",
1139 system_shared_libs: [],
1140 static_executable: true,
1141 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001142 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001143 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001144 `)
1145
Sundong Ahnabb64432019-10-22 13:58:29 +09001146 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001147 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1148
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001149 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001150 ensureListContains(t, dirs, "etc")
1151 ensureListContains(t, dirs, "etc/foo")
1152 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001153 ensureListContains(t, dirs, "lib64")
1154 ensureListContains(t, dirs, "lib64/foo")
1155 ensureListContains(t, dirs, "lib64/foo/bar")
1156 ensureListContains(t, dirs, "lib")
1157 ensureListContains(t, dirs, "lib/foo")
1158 ensureListContains(t, dirs, "lib/foo/bar")
1159
Jiyong Parkbd13e442019-03-15 18:10:35 +09001160 ensureListContains(t, dirs, "bin")
1161 ensureListContains(t, dirs, "bin/foo")
1162 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001163}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001164
1165func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001166 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001167 apex {
1168 name: "myapex",
1169 key: "myapex.key",
1170 native_shared_libs: ["mylib"],
1171 use_vendor: true,
1172 }
1173
1174 apex_key {
1175 name: "myapex.key",
1176 public_key: "testkey.avbpubkey",
1177 private_key: "testkey.pem",
1178 }
1179
1180 cc_library {
1181 name: "mylib",
1182 srcs: ["mylib.cpp"],
1183 shared_libs: ["mylib2"],
1184 system_shared_libs: [],
1185 vendor_available: true,
1186 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001187 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001188 }
1189
1190 cc_library {
1191 name: "mylib2",
1192 srcs: ["mylib.cpp"],
1193 system_shared_libs: [],
1194 vendor_available: true,
1195 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001196 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001197 }
Jooyung Handc782442019-11-01 03:14:38 +09001198 `, func(fs map[string][]byte, config android.Config) {
1199 setUseVendorWhitelistForTest(config, []string{"myapex"})
1200 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001201
1202 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001203 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001204 for _, implicit := range i.Implicits {
1205 inputsList = append(inputsList, implicit.String())
1206 }
1207 }
1208 inputsString := strings.Join(inputsList, " ")
1209
1210 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001211 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1212 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001213
1214 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001215 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1216 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001217}
Jiyong Park16e91a02018-12-20 18:18:08 +09001218
Jooyung Handc782442019-11-01 03:14:38 +09001219func TestUseVendorRestriction(t *testing.T) {
1220 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1221 apex {
1222 name: "myapex",
1223 key: "myapex.key",
1224 use_vendor: true,
1225 }
1226 apex_key {
1227 name: "myapex.key",
1228 public_key: "testkey.avbpubkey",
1229 private_key: "testkey.pem",
1230 }
1231 `, func(fs map[string][]byte, config android.Config) {
1232 setUseVendorWhitelistForTest(config, []string{""})
1233 })
1234 // no error with whitelist
1235 testApex(t, `
1236 apex {
1237 name: "myapex",
1238 key: "myapex.key",
1239 use_vendor: true,
1240 }
1241 apex_key {
1242 name: "myapex.key",
1243 public_key: "testkey.avbpubkey",
1244 private_key: "testkey.pem",
1245 }
1246 `, func(fs map[string][]byte, config android.Config) {
1247 setUseVendorWhitelistForTest(config, []string{"myapex"})
1248 })
1249}
1250
Jooyung Han5c998b92019-06-27 11:30:33 +09001251func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1252 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1253 apex {
1254 name: "myapex",
1255 key: "myapex.key",
1256 native_shared_libs: ["mylib"],
1257 use_vendor: true,
1258 }
1259
1260 apex_key {
1261 name: "myapex.key",
1262 public_key: "testkey.avbpubkey",
1263 private_key: "testkey.pem",
1264 }
1265
1266 cc_library {
1267 name: "mylib",
1268 srcs: ["mylib.cpp"],
1269 system_shared_libs: [],
1270 stl: "none",
1271 }
1272 `)
1273}
1274
Jiyong Park16e91a02018-12-20 18:18:08 +09001275func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001276 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001277 apex {
1278 name: "myapex",
1279 key: "myapex.key",
1280 native_shared_libs: ["mylib"],
1281 }
1282
1283 apex_key {
1284 name: "myapex.key",
1285 public_key: "testkey.avbpubkey",
1286 private_key: "testkey.pem",
1287 }
1288
1289 cc_library {
1290 name: "mylib",
1291 srcs: ["mylib.cpp"],
1292 system_shared_libs: [],
1293 stl: "none",
1294 stubs: {
1295 versions: ["1", "2", "3"],
1296 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001297 apex_available: [
1298 "//apex_available:platform",
1299 "myapex",
1300 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001301 }
1302
1303 cc_binary {
1304 name: "not_in_apex",
1305 srcs: ["mylib.cpp"],
1306 static_libs: ["mylib"],
1307 static_executable: true,
1308 system_shared_libs: [],
1309 stl: "none",
1310 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001311 `)
1312
Colin Cross7113d202019-11-20 16:39:12 -08001313 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001314
1315 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001316 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001317}
Jiyong Park9335a262018-12-24 11:31:58 +09001318
1319func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001320 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001321 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001322 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001323 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001324 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001325 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001326 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001327 }
1328
1329 cc_library {
1330 name: "mylib",
1331 srcs: ["mylib.cpp"],
1332 system_shared_libs: [],
1333 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001334 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001335 }
1336
1337 apex_key {
1338 name: "myapex.key",
1339 public_key: "testkey.avbpubkey",
1340 private_key: "testkey.pem",
1341 }
1342
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001343 android_app_certificate {
1344 name: "myapex.certificate",
1345 certificate: "testkey",
1346 }
1347
1348 android_app_certificate {
1349 name: "myapex.certificate.override",
1350 certificate: "testkey.override",
1351 }
1352
Jiyong Park9335a262018-12-24 11:31:58 +09001353 `)
1354
1355 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001356 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001357
1358 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1359 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1360 "vendor/foo/devkeys/testkey.avbpubkey")
1361 }
1362 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1363 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1364 "vendor/foo/devkeys/testkey.pem")
1365 }
1366
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001367 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001368 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001369 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001370 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001371 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001372 }
1373}
Jiyong Park58e364a2019-01-19 19:24:06 +09001374
Jooyung Hanf121a652019-12-17 14:30:11 +09001375func TestCertificate(t *testing.T) {
1376 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1377 ctx, _ := testApex(t, `
1378 apex {
1379 name: "myapex",
1380 key: "myapex.key",
1381 }
1382 apex_key {
1383 name: "myapex.key",
1384 public_key: "testkey.avbpubkey",
1385 private_key: "testkey.pem",
1386 }`)
1387 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1388 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1389 if actual := rule.Args["certificates"]; actual != expected {
1390 t.Errorf("certificates should be %q, not %q", expected, actual)
1391 }
1392 })
1393 t.Run("override when unspecified", func(t *testing.T) {
1394 ctx, _ := testApex(t, `
1395 apex {
1396 name: "myapex_keytest",
1397 key: "myapex.key",
1398 file_contexts: ":myapex-file_contexts",
1399 }
1400 apex_key {
1401 name: "myapex.key",
1402 public_key: "testkey.avbpubkey",
1403 private_key: "testkey.pem",
1404 }
1405 android_app_certificate {
1406 name: "myapex.certificate.override",
1407 certificate: "testkey.override",
1408 }`)
1409 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1410 expected := "testkey.override.x509.pem testkey.override.pk8"
1411 if actual := rule.Args["certificates"]; actual != expected {
1412 t.Errorf("certificates should be %q, not %q", expected, actual)
1413 }
1414 })
1415 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1416 ctx, _ := testApex(t, `
1417 apex {
1418 name: "myapex",
1419 key: "myapex.key",
1420 certificate: ":myapex.certificate",
1421 }
1422 apex_key {
1423 name: "myapex.key",
1424 public_key: "testkey.avbpubkey",
1425 private_key: "testkey.pem",
1426 }
1427 android_app_certificate {
1428 name: "myapex.certificate",
1429 certificate: "testkey",
1430 }`)
1431 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1432 expected := "testkey.x509.pem testkey.pk8"
1433 if actual := rule.Args["certificates"]; actual != expected {
1434 t.Errorf("certificates should be %q, not %q", expected, actual)
1435 }
1436 })
1437 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1438 ctx, _ := testApex(t, `
1439 apex {
1440 name: "myapex_keytest",
1441 key: "myapex.key",
1442 file_contexts: ":myapex-file_contexts",
1443 certificate: ":myapex.certificate",
1444 }
1445 apex_key {
1446 name: "myapex.key",
1447 public_key: "testkey.avbpubkey",
1448 private_key: "testkey.pem",
1449 }
1450 android_app_certificate {
1451 name: "myapex.certificate.override",
1452 certificate: "testkey.override",
1453 }`)
1454 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1455 expected := "testkey.override.x509.pem testkey.override.pk8"
1456 if actual := rule.Args["certificates"]; actual != expected {
1457 t.Errorf("certificates should be %q, not %q", expected, actual)
1458 }
1459 })
1460 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1461 ctx, _ := testApex(t, `
1462 apex {
1463 name: "myapex",
1464 key: "myapex.key",
1465 certificate: "testkey",
1466 }
1467 apex_key {
1468 name: "myapex.key",
1469 public_key: "testkey.avbpubkey",
1470 private_key: "testkey.pem",
1471 }`)
1472 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1473 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1474 if actual := rule.Args["certificates"]; actual != expected {
1475 t.Errorf("certificates should be %q, not %q", expected, actual)
1476 }
1477 })
1478 t.Run("override when specified as <name>", func(t *testing.T) {
1479 ctx, _ := testApex(t, `
1480 apex {
1481 name: "myapex_keytest",
1482 key: "myapex.key",
1483 file_contexts: ":myapex-file_contexts",
1484 certificate: "testkey",
1485 }
1486 apex_key {
1487 name: "myapex.key",
1488 public_key: "testkey.avbpubkey",
1489 private_key: "testkey.pem",
1490 }
1491 android_app_certificate {
1492 name: "myapex.certificate.override",
1493 certificate: "testkey.override",
1494 }`)
1495 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1496 expected := "testkey.override.x509.pem testkey.override.pk8"
1497 if actual := rule.Args["certificates"]; actual != expected {
1498 t.Errorf("certificates should be %q, not %q", expected, actual)
1499 }
1500 })
1501}
1502
Jiyong Park58e364a2019-01-19 19:24:06 +09001503func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001504 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001505 apex {
1506 name: "myapex",
1507 key: "myapex.key",
1508 native_shared_libs: ["mylib"],
1509 }
1510
1511 apex {
1512 name: "otherapex",
1513 key: "myapex.key",
1514 native_shared_libs: ["mylib"],
1515 }
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: "mylib",
1525 srcs: ["mylib.cpp"],
1526 system_shared_libs: [],
1527 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001528 // TODO: remove //apex_available:platform
1529 apex_available: [
1530 "//apex_available:platform",
1531 "myapex",
1532 "otherapex",
1533 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001534 }
1535 `)
1536
Jooyung Han6b8459b2019-10-30 08:29:25 +09001537 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001538 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001539 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001540 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1541 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001542
Jooyung Han6b8459b2019-10-30 08:29:25 +09001543 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001544 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001545 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001546 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1547 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001548
Jooyung Han6b8459b2019-10-30 08:29:25 +09001549 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001550 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001551 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001552 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1553 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001554}
Jiyong Park7e636d02019-01-28 16:16:54 +09001555
1556func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001557 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001558 apex {
1559 name: "myapex",
1560 key: "myapex.key",
1561 native_shared_libs: ["mylib"],
1562 }
1563
1564 apex_key {
1565 name: "myapex.key",
1566 public_key: "testkey.avbpubkey",
1567 private_key: "testkey.pem",
1568 }
1569
1570 cc_library_headers {
1571 name: "mylib_headers",
1572 export_include_dirs: ["my_include"],
1573 system_shared_libs: [],
1574 stl: "none",
1575 }
1576
1577 cc_library {
1578 name: "mylib",
1579 srcs: ["mylib.cpp"],
1580 system_shared_libs: [],
1581 stl: "none",
1582 header_libs: ["mylib_headers"],
1583 export_header_lib_headers: ["mylib_headers"],
1584 stubs: {
1585 versions: ["1", "2", "3"],
1586 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001587 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001588 }
1589
1590 cc_library {
1591 name: "otherlib",
1592 srcs: ["mylib.cpp"],
1593 system_shared_libs: [],
1594 stl: "none",
1595 shared_libs: ["mylib"],
1596 }
1597 `)
1598
Colin Cross7113d202019-11-20 16:39:12 -08001599 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001600
1601 // Ensure that the include path of the header lib is exported to 'otherlib'
1602 ensureContains(t, cFlags, "-Imy_include")
1603}
Alex Light9670d332019-01-29 18:07:33 -08001604
Jiyong Park7cd10e32020-01-14 09:22:18 +09001605type fileInApex struct {
1606 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001607 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001608 isLink bool
1609}
1610
Jooyung Hana57af4a2020-01-23 05:36:59 +00001611func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001612 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001613 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001614 copyCmds := apexRule.Args["copy_commands"]
1615 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001616 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001617 for _, cmd := range strings.Split(copyCmds, "&&") {
1618 cmd = strings.TrimSpace(cmd)
1619 if cmd == "" {
1620 continue
1621 }
1622 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001623 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001624 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001625 switch terms[0] {
1626 case "mkdir":
1627 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001628 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001629 t.Fatal("copyCmds contains invalid cp command", cmd)
1630 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001631 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001632 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001633 isLink = false
1634 case "ln":
1635 if len(terms) != 3 && len(terms) != 4 {
1636 // ln LINK TARGET or ln -s LINK TARGET
1637 t.Fatal("copyCmds contains invalid ln command", cmd)
1638 }
1639 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001640 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001641 isLink = true
1642 default:
1643 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1644 }
1645 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001646 index := strings.Index(dst, imageApexDir)
1647 if index == -1 {
1648 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1649 }
1650 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001651 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001652 }
1653 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001654 return ret
1655}
1656
Jooyung Hana57af4a2020-01-23 05:36:59 +00001657func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1658 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001659 var failed bool
1660 var surplus []string
1661 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001662 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09001663 for _, expected := range files {
1664 if matched, _ := path.Match(expected, file.path); matched {
1665 filesMatched[expected] = true
1666 return
1667 }
1668 }
1669 surplus = append(surplus, file.path)
1670 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001671
Jooyung Han31c470b2019-10-18 16:26:59 +09001672 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001673 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001674 t.Log("surplus files", surplus)
1675 failed = true
1676 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001677
1678 if len(files) > len(filesMatched) {
1679 var missing []string
1680 for _, expected := range files {
1681 if !filesMatched[expected] {
1682 missing = append(missing, expected)
1683 }
1684 }
1685 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001686 t.Log("missing files", missing)
1687 failed = true
1688 }
1689 if failed {
1690 t.Fail()
1691 }
1692}
1693
Jooyung Han344d5432019-08-23 11:17:39 +09001694func TestVndkApexCurrent(t *testing.T) {
1695 ctx, _ := testApex(t, `
1696 apex_vndk {
1697 name: "myapex",
1698 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001699 }
1700
1701 apex_key {
1702 name: "myapex.key",
1703 public_key: "testkey.avbpubkey",
1704 private_key: "testkey.pem",
1705 }
1706
1707 cc_library {
1708 name: "libvndk",
1709 srcs: ["mylib.cpp"],
1710 vendor_available: true,
1711 vndk: {
1712 enabled: true,
1713 },
1714 system_shared_libs: [],
1715 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001716 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001717 }
1718
1719 cc_library {
1720 name: "libvndksp",
1721 srcs: ["mylib.cpp"],
1722 vendor_available: true,
1723 vndk: {
1724 enabled: true,
1725 support_system_process: true,
1726 },
1727 system_shared_libs: [],
1728 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001729 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001730 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001731 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001732
Jooyung Hana57af4a2020-01-23 05:36:59 +00001733 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001734 "lib/libvndk.so",
1735 "lib/libvndksp.so",
1736 "lib64/libvndk.so",
1737 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001738 "etc/llndk.libraries.VER.txt",
1739 "etc/vndkcore.libraries.VER.txt",
1740 "etc/vndksp.libraries.VER.txt",
1741 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001742 })
Jooyung Han344d5432019-08-23 11:17:39 +09001743}
1744
1745func TestVndkApexWithPrebuilt(t *testing.T) {
1746 ctx, _ := testApex(t, `
1747 apex_vndk {
1748 name: "myapex",
1749 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001750 }
1751
1752 apex_key {
1753 name: "myapex.key",
1754 public_key: "testkey.avbpubkey",
1755 private_key: "testkey.pem",
1756 }
1757
1758 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001759 name: "libvndk",
1760 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001761 vendor_available: true,
1762 vndk: {
1763 enabled: true,
1764 },
1765 system_shared_libs: [],
1766 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001767 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001768 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001769
1770 cc_prebuilt_library_shared {
1771 name: "libvndk.arm",
1772 srcs: ["libvndk.arm.so"],
1773 vendor_available: true,
1774 vndk: {
1775 enabled: true,
1776 },
1777 enabled: false,
1778 arch: {
1779 arm: {
1780 enabled: true,
1781 },
1782 },
1783 system_shared_libs: [],
1784 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001785 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001786 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001787 `+vndkLibrariesTxtFiles("current"),
1788 withFiles(map[string][]byte{
1789 "libvndk.so": nil,
1790 "libvndk.arm.so": nil,
1791 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001792
Jooyung Hana57af4a2020-01-23 05:36:59 +00001793 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001794 "lib/libvndk.so",
1795 "lib/libvndk.arm.so",
1796 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001797 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001798 })
Jooyung Han344d5432019-08-23 11:17:39 +09001799}
1800
Jooyung Han39edb6c2019-11-06 16:53:07 +09001801func vndkLibrariesTxtFiles(vers ...string) (result string) {
1802 for _, v := range vers {
1803 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001804 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001805 result += `
1806 vndk_libraries_txt {
1807 name: "` + txt + `.libraries.txt",
1808 }
1809 `
1810 }
1811 } else {
1812 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1813 result += `
1814 prebuilt_etc {
1815 name: "` + txt + `.libraries.` + v + `.txt",
1816 src: "dummy.txt",
1817 }
1818 `
1819 }
1820 }
1821 }
1822 return
1823}
1824
Jooyung Han344d5432019-08-23 11:17:39 +09001825func TestVndkApexVersion(t *testing.T) {
1826 ctx, _ := testApex(t, `
1827 apex_vndk {
1828 name: "myapex_v27",
1829 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001830 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001831 vndk_version: "27",
1832 }
1833
1834 apex_key {
1835 name: "myapex.key",
1836 public_key: "testkey.avbpubkey",
1837 private_key: "testkey.pem",
1838 }
1839
Jooyung Han31c470b2019-10-18 16:26:59 +09001840 vndk_prebuilt_shared {
1841 name: "libvndk27",
1842 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001843 vendor_available: true,
1844 vndk: {
1845 enabled: true,
1846 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001847 target_arch: "arm64",
1848 arch: {
1849 arm: {
1850 srcs: ["libvndk27_arm.so"],
1851 },
1852 arm64: {
1853 srcs: ["libvndk27_arm64.so"],
1854 },
1855 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001856 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001857 }
1858
1859 vndk_prebuilt_shared {
1860 name: "libvndk27",
1861 version: "27",
1862 vendor_available: true,
1863 vndk: {
1864 enabled: true,
1865 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001866 target_arch: "x86_64",
1867 arch: {
1868 x86: {
1869 srcs: ["libvndk27_x86.so"],
1870 },
1871 x86_64: {
1872 srcs: ["libvndk27_x86_64.so"],
1873 },
1874 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001875 }
1876 `+vndkLibrariesTxtFiles("27"),
1877 withFiles(map[string][]byte{
1878 "libvndk27_arm.so": nil,
1879 "libvndk27_arm64.so": nil,
1880 "libvndk27_x86.so": nil,
1881 "libvndk27_x86_64.so": nil,
1882 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001883
Jooyung Hana57af4a2020-01-23 05:36:59 +00001884 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001885 "lib/libvndk27_arm.so",
1886 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001887 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001888 })
Jooyung Han344d5432019-08-23 11:17:39 +09001889}
1890
1891func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1892 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1893 apex_vndk {
1894 name: "myapex_v27",
1895 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001896 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001897 vndk_version: "27",
1898 }
1899 apex_vndk {
1900 name: "myapex_v27_other",
1901 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001902 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001903 vndk_version: "27",
1904 }
1905
1906 apex_key {
1907 name: "myapex.key",
1908 public_key: "testkey.avbpubkey",
1909 private_key: "testkey.pem",
1910 }
1911
1912 cc_library {
1913 name: "libvndk",
1914 srcs: ["mylib.cpp"],
1915 vendor_available: true,
1916 vndk: {
1917 enabled: true,
1918 },
1919 system_shared_libs: [],
1920 stl: "none",
1921 }
1922
1923 vndk_prebuilt_shared {
1924 name: "libvndk",
1925 version: "27",
1926 vendor_available: true,
1927 vndk: {
1928 enabled: true,
1929 },
1930 srcs: ["libvndk.so"],
1931 }
1932 `, withFiles(map[string][]byte{
1933 "libvndk.so": nil,
1934 }))
1935}
1936
Jooyung Han90eee022019-10-01 20:02:42 +09001937func TestVndkApexNameRule(t *testing.T) {
1938 ctx, _ := testApex(t, `
1939 apex_vndk {
1940 name: "myapex",
1941 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001942 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001943 }
1944 apex_vndk {
1945 name: "myapex_v28",
1946 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001947 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001948 vndk_version: "28",
1949 }
1950 apex_key {
1951 name: "myapex.key",
1952 public_key: "testkey.avbpubkey",
1953 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001954 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001955
1956 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00001957 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001958 actual := proptools.String(bundle.properties.Apex_name)
1959 if !reflect.DeepEqual(actual, expected) {
1960 t.Errorf("Got '%v', expected '%v'", actual, expected)
1961 }
1962 }
1963
1964 assertApexName("com.android.vndk.vVER", "myapex")
1965 assertApexName("com.android.vndk.v28", "myapex_v28")
1966}
1967
Jooyung Han344d5432019-08-23 11:17:39 +09001968func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1969 ctx, _ := testApex(t, `
1970 apex_vndk {
1971 name: "myapex",
1972 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001973 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001974 }
1975
1976 apex_key {
1977 name: "myapex.key",
1978 public_key: "testkey.avbpubkey",
1979 private_key: "testkey.pem",
1980 }
1981
1982 cc_library {
1983 name: "libvndk",
1984 srcs: ["mylib.cpp"],
1985 vendor_available: true,
1986 native_bridge_supported: true,
1987 host_supported: true,
1988 vndk: {
1989 enabled: true,
1990 },
1991 system_shared_libs: [],
1992 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001993 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001994 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001995 `+vndkLibrariesTxtFiles("current"),
1996 withTargets(map[android.OsType][]android.Target{
1997 android.Android: []android.Target{
1998 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1999 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2000 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
2001 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
2002 },
2003 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002004
Jooyung Hana57af4a2020-01-23 05:36:59 +00002005 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002006 "lib/libvndk.so",
2007 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002008 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002009 })
Jooyung Han344d5432019-08-23 11:17:39 +09002010}
2011
2012func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2013 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2014 apex_vndk {
2015 name: "myapex",
2016 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002017 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002018 native_bridge_supported: true,
2019 }
2020
2021 apex_key {
2022 name: "myapex.key",
2023 public_key: "testkey.avbpubkey",
2024 private_key: "testkey.pem",
2025 }
2026
2027 cc_library {
2028 name: "libvndk",
2029 srcs: ["mylib.cpp"],
2030 vendor_available: true,
2031 native_bridge_supported: true,
2032 host_supported: true,
2033 vndk: {
2034 enabled: true,
2035 },
2036 system_shared_libs: [],
2037 stl: "none",
2038 }
2039 `)
2040}
2041
Jooyung Han31c470b2019-10-18 16:26:59 +09002042func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002043 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002044 apex_vndk {
2045 name: "myapex_v27",
2046 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002047 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002048 vndk_version: "27",
2049 }
2050
2051 apex_key {
2052 name: "myapex.key",
2053 public_key: "testkey.avbpubkey",
2054 private_key: "testkey.pem",
2055 }
2056
2057 vndk_prebuilt_shared {
2058 name: "libvndk27",
2059 version: "27",
2060 target_arch: "arm",
2061 vendor_available: true,
2062 vndk: {
2063 enabled: true,
2064 },
2065 arch: {
2066 arm: {
2067 srcs: ["libvndk27.so"],
2068 }
2069 },
2070 }
2071
2072 vndk_prebuilt_shared {
2073 name: "libvndk27",
2074 version: "27",
2075 target_arch: "arm",
2076 binder32bit: true,
2077 vendor_available: true,
2078 vndk: {
2079 enabled: true,
2080 },
2081 arch: {
2082 arm: {
2083 srcs: ["libvndk27binder32.so"],
2084 }
2085 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002086 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002087 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002088 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002089 withFiles(map[string][]byte{
2090 "libvndk27.so": nil,
2091 "libvndk27binder32.so": nil,
2092 }),
2093 withBinder32bit,
2094 withTargets(map[android.OsType][]android.Target{
2095 android.Android: []android.Target{
2096 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2097 },
2098 }),
2099 )
2100
Jooyung Hana57af4a2020-01-23 05:36:59 +00002101 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002102 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002103 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002104 })
2105}
2106
Jooyung Hane1633032019-08-01 17:41:43 +09002107func TestDependenciesInApexManifest(t *testing.T) {
2108 ctx, _ := testApex(t, `
2109 apex {
2110 name: "myapex_nodep",
2111 key: "myapex.key",
2112 native_shared_libs: ["lib_nodep"],
2113 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002114 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002115 }
2116
2117 apex {
2118 name: "myapex_dep",
2119 key: "myapex.key",
2120 native_shared_libs: ["lib_dep"],
2121 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002122 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002123 }
2124
2125 apex {
2126 name: "myapex_provider",
2127 key: "myapex.key",
2128 native_shared_libs: ["libfoo"],
2129 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002130 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002131 }
2132
2133 apex {
2134 name: "myapex_selfcontained",
2135 key: "myapex.key",
2136 native_shared_libs: ["lib_dep", "libfoo"],
2137 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002138 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002139 }
2140
2141 apex_key {
2142 name: "myapex.key",
2143 public_key: "testkey.avbpubkey",
2144 private_key: "testkey.pem",
2145 }
2146
2147 cc_library {
2148 name: "lib_nodep",
2149 srcs: ["mylib.cpp"],
2150 system_shared_libs: [],
2151 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002152 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002153 }
2154
2155 cc_library {
2156 name: "lib_dep",
2157 srcs: ["mylib.cpp"],
2158 shared_libs: ["libfoo"],
2159 system_shared_libs: [],
2160 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002161 apex_available: [
2162 "myapex_dep",
2163 "myapex_provider",
2164 "myapex_selfcontained",
2165 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002166 }
2167
2168 cc_library {
2169 name: "libfoo",
2170 srcs: ["mytest.cpp"],
2171 stubs: {
2172 versions: ["1"],
2173 },
2174 system_shared_libs: [],
2175 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002176 apex_available: [
2177 "myapex_provider",
2178 "myapex_selfcontained",
2179 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002180 }
2181 `)
2182
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002183 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002184 var provideNativeLibs, requireNativeLibs []string
2185
Sundong Ahnabb64432019-10-22 13:58:29 +09002186 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002187 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2188 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002189 ensureListEmpty(t, provideNativeLibs)
2190 ensureListEmpty(t, requireNativeLibs)
2191
Sundong Ahnabb64432019-10-22 13:58:29 +09002192 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002193 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2194 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002195 ensureListEmpty(t, provideNativeLibs)
2196 ensureListContains(t, requireNativeLibs, "libfoo.so")
2197
Sundong Ahnabb64432019-10-22 13:58:29 +09002198 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002199 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2200 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002201 ensureListContains(t, provideNativeLibs, "libfoo.so")
2202 ensureListEmpty(t, requireNativeLibs)
2203
Sundong Ahnabb64432019-10-22 13:58:29 +09002204 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002205 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2206 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002207 ensureListContains(t, provideNativeLibs, "libfoo.so")
2208 ensureListEmpty(t, requireNativeLibs)
2209}
2210
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002211func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002212 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002213 apex {
2214 name: "myapex",
2215 key: "myapex.key",
2216 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002217 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002218 }
2219
2220 apex_key {
2221 name: "myapex.key",
2222 public_key: "testkey.avbpubkey",
2223 private_key: "testkey.pem",
2224 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002225
2226 cc_library {
2227 name: "mylib",
2228 srcs: ["mylib.cpp"],
2229 system_shared_libs: [],
2230 stl: "none",
2231 apex_available: [
2232 "//apex_available:platform",
2233 "myapex",
2234 ],
2235 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002236 `)
2237
Sundong Ahnabb64432019-10-22 13:58:29 +09002238 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002239 apexManifestRule := module.Rule("apexManifestRule")
2240 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2241 apexRule := module.Rule("apexRule")
2242 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002243
2244 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2245 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2246 name := apexBundle.BaseModuleName()
2247 prefix := "TARGET_"
2248 var builder strings.Builder
2249 data.Custom(&builder, name, prefix, "", data)
2250 androidMk := builder.String()
2251 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2252 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002253}
2254
Alex Light0851b882019-02-07 13:20:53 -08002255func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002256 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002257 apex {
2258 name: "myapex",
2259 key: "myapex.key",
2260 native_shared_libs: ["mylib_common"],
2261 }
2262
2263 apex_key {
2264 name: "myapex.key",
2265 public_key: "testkey.avbpubkey",
2266 private_key: "testkey.pem",
2267 }
2268
2269 cc_library {
2270 name: "mylib_common",
2271 srcs: ["mylib.cpp"],
2272 system_shared_libs: [],
2273 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002274 apex_available: [
2275 "//apex_available:platform",
2276 "myapex",
2277 ],
Alex Light0851b882019-02-07 13:20:53 -08002278 }
2279 `)
2280
Sundong Ahnabb64432019-10-22 13:58:29 +09002281 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002282 apexRule := module.Rule("apexRule")
2283 copyCmds := apexRule.Args["copy_commands"]
2284
2285 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2286 t.Log("Apex was a test apex!")
2287 t.Fail()
2288 }
2289 // Ensure that main rule creates an output
2290 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2291
2292 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002293 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002294
2295 // Ensure that both direct and indirect deps are copied into apex
2296 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2297
Colin Cross7113d202019-11-20 16:39:12 -08002298 // Ensure that the platform variant ends with _shared
2299 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002300
2301 if !android.InAnyApex("mylib_common") {
2302 t.Log("Found mylib_common not in any apex!")
2303 t.Fail()
2304 }
2305}
2306
2307func TestTestApex(t *testing.T) {
2308 if android.InAnyApex("mylib_common_test") {
2309 t.Fatal("mylib_common_test must not be used in any other tests since this checks that global state is not updated in an illegal way!")
2310 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002311 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002312 apex_test {
2313 name: "myapex",
2314 key: "myapex.key",
2315 native_shared_libs: ["mylib_common_test"],
2316 }
2317
2318 apex_key {
2319 name: "myapex.key",
2320 public_key: "testkey.avbpubkey",
2321 private_key: "testkey.pem",
2322 }
2323
2324 cc_library {
2325 name: "mylib_common_test",
2326 srcs: ["mylib.cpp"],
2327 system_shared_libs: [],
2328 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002329 // TODO: remove //apex_available:platform
2330 apex_available: [
2331 "//apex_available:platform",
2332 "myapex",
2333 ],
Alex Light0851b882019-02-07 13:20:53 -08002334 }
2335 `)
2336
Sundong Ahnabb64432019-10-22 13:58:29 +09002337 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002338 apexRule := module.Rule("apexRule")
2339 copyCmds := apexRule.Args["copy_commands"]
2340
2341 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2342 t.Log("Apex was not a test apex!")
2343 t.Fail()
2344 }
2345 // Ensure that main rule creates an output
2346 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2347
2348 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002349 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002350
2351 // Ensure that both direct and indirect deps are copied into apex
2352 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2353
Colin Cross7113d202019-11-20 16:39:12 -08002354 // Ensure that the platform variant ends with _shared
2355 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002356
2357 if android.InAnyApex("mylib_common_test") {
2358 t.Log("Found mylib_common_test in some apex!")
2359 t.Fail()
2360 }
2361}
2362
Alex Light9670d332019-01-29 18:07:33 -08002363func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002364 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002365 apex {
2366 name: "myapex",
2367 key: "myapex.key",
2368 multilib: {
2369 first: {
2370 native_shared_libs: ["mylib_common"],
2371 }
2372 },
2373 target: {
2374 android: {
2375 multilib: {
2376 first: {
2377 native_shared_libs: ["mylib"],
2378 }
2379 }
2380 },
2381 host: {
2382 multilib: {
2383 first: {
2384 native_shared_libs: ["mylib2"],
2385 }
2386 }
2387 }
2388 }
2389 }
2390
2391 apex_key {
2392 name: "myapex.key",
2393 public_key: "testkey.avbpubkey",
2394 private_key: "testkey.pem",
2395 }
2396
2397 cc_library {
2398 name: "mylib",
2399 srcs: ["mylib.cpp"],
2400 system_shared_libs: [],
2401 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002402 // TODO: remove //apex_available:platform
2403 apex_available: [
2404 "//apex_available:platform",
2405 "myapex",
2406 ],
Alex Light9670d332019-01-29 18:07:33 -08002407 }
2408
2409 cc_library {
2410 name: "mylib_common",
2411 srcs: ["mylib.cpp"],
2412 system_shared_libs: [],
2413 stl: "none",
2414 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002415 // TODO: remove //apex_available:platform
2416 apex_available: [
2417 "//apex_available:platform",
2418 "myapex",
2419 ],
Alex Light9670d332019-01-29 18:07:33 -08002420 }
2421
2422 cc_library {
2423 name: "mylib2",
2424 srcs: ["mylib.cpp"],
2425 system_shared_libs: [],
2426 stl: "none",
2427 compile_multilib: "first",
2428 }
2429 `)
2430
Sundong Ahnabb64432019-10-22 13:58:29 +09002431 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002432 copyCmds := apexRule.Args["copy_commands"]
2433
2434 // Ensure that main rule creates an output
2435 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2436
2437 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002438 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2439 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2440 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002441
2442 // Ensure that both direct and indirect deps are copied into apex
2443 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2444 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2445 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2446
Colin Cross7113d202019-11-20 16:39:12 -08002447 // Ensure that the platform variant ends with _shared
2448 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2449 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2450 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002451}
Jiyong Park04480cf2019-02-06 00:16:29 +09002452
2453func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002454 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002455 apex {
2456 name: "myapex",
2457 key: "myapex.key",
2458 binaries: ["myscript"],
2459 }
2460
2461 apex_key {
2462 name: "myapex.key",
2463 public_key: "testkey.avbpubkey",
2464 private_key: "testkey.pem",
2465 }
2466
2467 sh_binary {
2468 name: "myscript",
2469 src: "mylib.cpp",
2470 filename: "myscript.sh",
2471 sub_dir: "script",
2472 }
2473 `)
2474
Sundong Ahnabb64432019-10-22 13:58:29 +09002475 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002476 copyCmds := apexRule.Args["copy_commands"]
2477
2478 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2479}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002480
Jooyung Han91df2082019-11-20 01:49:42 +09002481func TestApexInVariousPartition(t *testing.T) {
2482 testcases := []struct {
2483 propName, parition, flattenedPartition string
2484 }{
2485 {"", "system", "system_ext"},
2486 {"product_specific: true", "product", "product"},
2487 {"soc_specific: true", "vendor", "vendor"},
2488 {"proprietary: true", "vendor", "vendor"},
2489 {"vendor: true", "vendor", "vendor"},
2490 {"system_ext_specific: true", "system_ext", "system_ext"},
2491 }
2492 for _, tc := range testcases {
2493 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2494 ctx, _ := testApex(t, `
2495 apex {
2496 name: "myapex",
2497 key: "myapex.key",
2498 `+tc.propName+`
2499 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002500
Jooyung Han91df2082019-11-20 01:49:42 +09002501 apex_key {
2502 name: "myapex.key",
2503 public_key: "testkey.avbpubkey",
2504 private_key: "testkey.pem",
2505 }
2506 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002507
Jooyung Han91df2082019-11-20 01:49:42 +09002508 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2509 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2510 actual := apex.installDir.String()
2511 if actual != expected {
2512 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2513 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002514
Jooyung Han91df2082019-11-20 01:49:42 +09002515 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2516 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2517 actual = flattened.installDir.String()
2518 if actual != expected {
2519 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2520 }
2521 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002522 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002523}
Jiyong Park67882562019-03-21 01:11:21 +09002524
Jooyung Han54aca7b2019-11-20 02:26:02 +09002525func TestFileContexts(t *testing.T) {
2526 ctx, _ := testApex(t, `
2527 apex {
2528 name: "myapex",
2529 key: "myapex.key",
2530 }
2531
2532 apex_key {
2533 name: "myapex.key",
2534 public_key: "testkey.avbpubkey",
2535 private_key: "testkey.pem",
2536 }
2537 `)
2538 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2539 apexRule := module.Rule("apexRule")
2540 actual := apexRule.Args["file_contexts"]
2541 expected := "system/sepolicy/apex/myapex-file_contexts"
2542 if actual != expected {
2543 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2544 }
2545
2546 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2547 apex {
2548 name: "myapex",
2549 key: "myapex.key",
2550 file_contexts: "my_own_file_contexts",
2551 }
2552
2553 apex_key {
2554 name: "myapex.key",
2555 public_key: "testkey.avbpubkey",
2556 private_key: "testkey.pem",
2557 }
2558 `, withFiles(map[string][]byte{
2559 "my_own_file_contexts": nil,
2560 }))
2561
2562 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2563 apex {
2564 name: "myapex",
2565 key: "myapex.key",
2566 product_specific: true,
2567 file_contexts: "product_specific_file_contexts",
2568 }
2569
2570 apex_key {
2571 name: "myapex.key",
2572 public_key: "testkey.avbpubkey",
2573 private_key: "testkey.pem",
2574 }
2575 `)
2576
2577 ctx, _ = testApex(t, `
2578 apex {
2579 name: "myapex",
2580 key: "myapex.key",
2581 product_specific: true,
2582 file_contexts: "product_specific_file_contexts",
2583 }
2584
2585 apex_key {
2586 name: "myapex.key",
2587 public_key: "testkey.avbpubkey",
2588 private_key: "testkey.pem",
2589 }
2590 `, withFiles(map[string][]byte{
2591 "product_specific_file_contexts": nil,
2592 }))
2593 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2594 apexRule = module.Rule("apexRule")
2595 actual = apexRule.Args["file_contexts"]
2596 expected = "product_specific_file_contexts"
2597 if actual != expected {
2598 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2599 }
2600
2601 ctx, _ = testApex(t, `
2602 apex {
2603 name: "myapex",
2604 key: "myapex.key",
2605 product_specific: true,
2606 file_contexts: ":my-file-contexts",
2607 }
2608
2609 apex_key {
2610 name: "myapex.key",
2611 public_key: "testkey.avbpubkey",
2612 private_key: "testkey.pem",
2613 }
2614
2615 filegroup {
2616 name: "my-file-contexts",
2617 srcs: ["product_specific_file_contexts"],
2618 }
2619 `, withFiles(map[string][]byte{
2620 "product_specific_file_contexts": nil,
2621 }))
2622 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2623 apexRule = module.Rule("apexRule")
2624 actual = apexRule.Args["file_contexts"]
2625 expected = "product_specific_file_contexts"
2626 if actual != expected {
2627 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2628 }
2629}
2630
Jiyong Park67882562019-03-21 01:11:21 +09002631func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002632 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002633 apex_key {
2634 name: "myapex.key",
2635 public_key: ":my.avbpubkey",
2636 private_key: ":my.pem",
2637 product_specific: true,
2638 }
2639
2640 filegroup {
2641 name: "my.avbpubkey",
2642 srcs: ["testkey2.avbpubkey"],
2643 }
2644
2645 filegroup {
2646 name: "my.pem",
2647 srcs: ["testkey2.pem"],
2648 }
2649 `)
2650
2651 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2652 expected_pubkey := "testkey2.avbpubkey"
2653 actual_pubkey := apex_key.public_key_file.String()
2654 if actual_pubkey != expected_pubkey {
2655 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2656 }
2657 expected_privkey := "testkey2.pem"
2658 actual_privkey := apex_key.private_key_file.String()
2659 if actual_privkey != expected_privkey {
2660 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2661 }
2662}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002663
2664func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002665 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002666 prebuilt_apex {
2667 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002668 arch: {
2669 arm64: {
2670 src: "myapex-arm64.apex",
2671 },
2672 arm: {
2673 src: "myapex-arm.apex",
2674 },
2675 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002676 }
2677 `)
2678
2679 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2680
Jiyong Parkc95714e2019-03-29 14:23:10 +09002681 expectedInput := "myapex-arm64.apex"
2682 if prebuilt.inputApex.String() != expectedInput {
2683 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2684 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002685}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002686
2687func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002688 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002689 prebuilt_apex {
2690 name: "myapex",
2691 src: "myapex-arm.apex",
2692 filename: "notmyapex.apex",
2693 }
2694 `)
2695
2696 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2697
2698 expected := "notmyapex.apex"
2699 if p.installFilename != expected {
2700 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2701 }
2702}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002703
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002704func TestPrebuiltOverrides(t *testing.T) {
2705 ctx, config := testApex(t, `
2706 prebuilt_apex {
2707 name: "myapex.prebuilt",
2708 src: "myapex-arm.apex",
2709 overrides: [
2710 "myapex",
2711 ],
2712 }
2713 `)
2714
2715 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2716
2717 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002718 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002719 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002720 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002721 }
2722}
2723
Roland Levillain630846d2019-06-26 12:48:34 +01002724func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002725 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002726 apex_test {
2727 name: "myapex",
2728 key: "myapex.key",
2729 tests: [
2730 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002731 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002732 ],
2733 }
2734
2735 apex_key {
2736 name: "myapex.key",
2737 public_key: "testkey.avbpubkey",
2738 private_key: "testkey.pem",
2739 }
2740
2741 cc_test {
2742 name: "mytest",
2743 gtest: false,
2744 srcs: ["mytest.cpp"],
2745 relative_install_path: "test",
2746 system_shared_libs: [],
2747 static_executable: true,
2748 stl: "none",
2749 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002750
2751 cc_test {
2752 name: "mytests",
2753 gtest: false,
2754 srcs: [
2755 "mytest1.cpp",
2756 "mytest2.cpp",
2757 "mytest3.cpp",
2758 ],
2759 test_per_src: true,
2760 relative_install_path: "test",
2761 system_shared_libs: [],
2762 static_executable: true,
2763 stl: "none",
2764 }
Roland Levillain630846d2019-06-26 12:48:34 +01002765 `)
2766
Sundong Ahnabb64432019-10-22 13:58:29 +09002767 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002768 copyCmds := apexRule.Args["copy_commands"]
2769
2770 // Ensure that test dep is copied into apex.
2771 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002772
2773 // Ensure that test deps built with `test_per_src` are copied into apex.
2774 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2775 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2776 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002777
2778 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002779 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002780 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2781 name := apexBundle.BaseModuleName()
2782 prefix := "TARGET_"
2783 var builder strings.Builder
2784 data.Custom(&builder, name, prefix, "", data)
2785 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002786 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2787 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2788 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2789 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002790 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002791 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002792 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002793}
2794
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002795func TestInstallExtraFlattenedApexes(t *testing.T) {
2796 ctx, config := testApex(t, `
2797 apex {
2798 name: "myapex",
2799 key: "myapex.key",
2800 }
2801 apex_key {
2802 name: "myapex.key",
2803 public_key: "testkey.avbpubkey",
2804 private_key: "testkey.pem",
2805 }
2806 `, func(fs map[string][]byte, config android.Config) {
2807 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2808 })
2809 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002810 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002811 mk := android.AndroidMkDataForTest(t, config, "", ab)
2812 var builder strings.Builder
2813 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2814 androidMk := builder.String()
2815 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2816}
2817
Jooyung Han5c998b92019-06-27 11:30:33 +09002818func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002819 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002820 apex {
2821 name: "myapex",
2822 key: "myapex.key",
2823 native_shared_libs: ["mylib"],
2824 uses: ["commonapex"],
2825 }
2826
2827 apex {
2828 name: "commonapex",
2829 key: "myapex.key",
2830 native_shared_libs: ["libcommon"],
2831 provide_cpp_shared_libs: true,
2832 }
2833
2834 apex_key {
2835 name: "myapex.key",
2836 public_key: "testkey.avbpubkey",
2837 private_key: "testkey.pem",
2838 }
2839
2840 cc_library {
2841 name: "mylib",
2842 srcs: ["mylib.cpp"],
2843 shared_libs: ["libcommon"],
2844 system_shared_libs: [],
2845 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002846 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002847 }
2848
2849 cc_library {
2850 name: "libcommon",
2851 srcs: ["mylib_common.cpp"],
2852 system_shared_libs: [],
2853 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002854 // TODO: remove //apex_available:platform
2855 apex_available: [
2856 "//apex_available:platform",
2857 "commonapex",
2858 "myapex",
2859 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002860 }
2861 `)
2862
Sundong Ahnabb64432019-10-22 13:58:29 +09002863 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002864 apexRule1 := module1.Rule("apexRule")
2865 copyCmds1 := apexRule1.Args["copy_commands"]
2866
Sundong Ahnabb64432019-10-22 13:58:29 +09002867 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002868 apexRule2 := module2.Rule("apexRule")
2869 copyCmds2 := apexRule2.Args["copy_commands"]
2870
Colin Cross7113d202019-11-20 16:39:12 -08002871 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2872 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002873 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2874 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2875 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2876}
2877
2878func TestApexUsesFailsIfNotProvided(t *testing.T) {
2879 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2880 apex {
2881 name: "myapex",
2882 key: "myapex.key",
2883 uses: ["commonapex"],
2884 }
2885
2886 apex {
2887 name: "commonapex",
2888 key: "myapex.key",
2889 }
2890
2891 apex_key {
2892 name: "myapex.key",
2893 public_key: "testkey.avbpubkey",
2894 private_key: "testkey.pem",
2895 }
2896 `)
2897 testApexError(t, `uses: "commonapex" is not a provider`, `
2898 apex {
2899 name: "myapex",
2900 key: "myapex.key",
2901 uses: ["commonapex"],
2902 }
2903
2904 cc_library {
2905 name: "commonapex",
2906 system_shared_libs: [],
2907 stl: "none",
2908 }
2909
2910 apex_key {
2911 name: "myapex.key",
2912 public_key: "testkey.avbpubkey",
2913 private_key: "testkey.pem",
2914 }
2915 `)
2916}
2917
2918func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2919 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2920 apex {
2921 name: "myapex",
2922 key: "myapex.key",
2923 use_vendor: true,
2924 uses: ["commonapex"],
2925 }
2926
2927 apex {
2928 name: "commonapex",
2929 key: "myapex.key",
2930 provide_cpp_shared_libs: true,
2931 }
2932
2933 apex_key {
2934 name: "myapex.key",
2935 public_key: "testkey.avbpubkey",
2936 private_key: "testkey.pem",
2937 }
Jooyung Handc782442019-11-01 03:14:38 +09002938 `, func(fs map[string][]byte, config android.Config) {
2939 setUseVendorWhitelistForTest(config, []string{"myapex"})
2940 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002941}
2942
Jooyung Hand48f3c32019-08-23 11:18:57 +09002943func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2944 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2945 apex {
2946 name: "myapex",
2947 key: "myapex.key",
2948 native_shared_libs: ["libfoo"],
2949 }
2950
2951 apex_key {
2952 name: "myapex.key",
2953 public_key: "testkey.avbpubkey",
2954 private_key: "testkey.pem",
2955 }
2956
2957 cc_library {
2958 name: "libfoo",
2959 stl: "none",
2960 system_shared_libs: [],
2961 enabled: false,
2962 }
2963 `)
2964 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2965 apex {
2966 name: "myapex",
2967 key: "myapex.key",
2968 java_libs: ["myjar"],
2969 }
2970
2971 apex_key {
2972 name: "myapex.key",
2973 public_key: "testkey.avbpubkey",
2974 private_key: "testkey.pem",
2975 }
2976
2977 java_library {
2978 name: "myjar",
2979 srcs: ["foo/bar/MyClass.java"],
2980 sdk_version: "none",
2981 system_modules: "none",
2982 compile_dex: true,
2983 enabled: false,
2984 }
2985 `)
2986}
2987
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002988func TestApexWithApps(t *testing.T) {
2989 ctx, _ := testApex(t, `
2990 apex {
2991 name: "myapex",
2992 key: "myapex.key",
2993 apps: [
2994 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002995 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002996 ],
2997 }
2998
2999 apex_key {
3000 name: "myapex.key",
3001 public_key: "testkey.avbpubkey",
3002 private_key: "testkey.pem",
3003 }
3004
3005 android_app {
3006 name: "AppFoo",
3007 srcs: ["foo/bar/MyClass.java"],
3008 sdk_version: "none",
3009 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003010 jni_libs: ["libjni"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003011 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003012 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003013
3014 android_app {
3015 name: "AppFooPriv",
3016 srcs: ["foo/bar/MyClass.java"],
3017 sdk_version: "none",
3018 system_modules: "none",
3019 privileged: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003020 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003021 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003022
3023 cc_library_shared {
3024 name: "libjni",
3025 srcs: ["mylib.cpp"],
3026 stl: "none",
3027 system_shared_libs: [],
3028 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003029 `)
3030
Sundong Ahnabb64432019-10-22 13:58:29 +09003031 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003032 apexRule := module.Rule("apexRule")
3033 copyCmds := apexRule.Args["copy_commands"]
3034
3035 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003036 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003037
3038 // JNI libraries are embedded inside APK
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00003039 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni lib")
Colin Cross7113d202019-11-20 16:39:12 -08003040 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09003041 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
3042 // ... uncompressed
3043 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
3044 t.Errorf("jni lib is not uncompressed for AppFoo")
3045 }
3046 // ... and not directly inside the APEX
3047 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01003048}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003049
Dario Frenicde2a032019-10-27 00:29:22 +01003050func TestApexWithAppImports(t *testing.T) {
3051 ctx, _ := testApex(t, `
3052 apex {
3053 name: "myapex",
3054 key: "myapex.key",
3055 apps: [
3056 "AppFooPrebuilt",
3057 "AppFooPrivPrebuilt",
3058 ],
3059 }
3060
3061 apex_key {
3062 name: "myapex.key",
3063 public_key: "testkey.avbpubkey",
3064 private_key: "testkey.pem",
3065 }
3066
3067 android_app_import {
3068 name: "AppFooPrebuilt",
3069 apk: "PrebuiltAppFoo.apk",
3070 presigned: true,
3071 dex_preopt: {
3072 enabled: false,
3073 },
3074 }
3075
3076 android_app_import {
3077 name: "AppFooPrivPrebuilt",
3078 apk: "PrebuiltAppFooPriv.apk",
3079 privileged: true,
3080 presigned: true,
3081 dex_preopt: {
3082 enabled: false,
3083 },
3084 }
3085 `)
3086
Sundong Ahnabb64432019-10-22 13:58:29 +09003087 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003088 apexRule := module.Rule("apexRule")
3089 copyCmds := apexRule.Args["copy_commands"]
3090
3091 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3092 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003093}
3094
Dario Freni6f3937c2019-12-20 22:58:03 +00003095func TestApexWithTestHelperApp(t *testing.T) {
3096 ctx, _ := testApex(t, `
3097 apex {
3098 name: "myapex",
3099 key: "myapex.key",
3100 apps: [
3101 "TesterHelpAppFoo",
3102 ],
3103 }
3104
3105 apex_key {
3106 name: "myapex.key",
3107 public_key: "testkey.avbpubkey",
3108 private_key: "testkey.pem",
3109 }
3110
3111 android_test_helper_app {
3112 name: "TesterHelpAppFoo",
3113 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003114 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003115 }
3116
3117 `)
3118
3119 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3120 apexRule := module.Rule("apexRule")
3121 copyCmds := apexRule.Args["copy_commands"]
3122
3123 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3124}
3125
Jooyung Han18020ea2019-11-13 10:50:48 +09003126func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3127 // libfoo's apex_available comes from cc_defaults
3128 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
3129 apex {
3130 name: "myapex",
3131 key: "myapex.key",
3132 native_shared_libs: ["libfoo"],
3133 }
3134
3135 apex_key {
3136 name: "myapex.key",
3137 public_key: "testkey.avbpubkey",
3138 private_key: "testkey.pem",
3139 }
3140
3141 apex {
3142 name: "otherapex",
3143 key: "myapex.key",
3144 native_shared_libs: ["libfoo"],
3145 }
3146
3147 cc_defaults {
3148 name: "libfoo-defaults",
3149 apex_available: ["otherapex"],
3150 }
3151
3152 cc_library {
3153 name: "libfoo",
3154 defaults: ["libfoo-defaults"],
3155 stl: "none",
3156 system_shared_libs: [],
3157 }`)
3158}
3159
Jiyong Park127b40b2019-09-30 16:04:35 +09003160func TestApexAvailable(t *testing.T) {
3161 // libfoo is not available to myapex, but only to otherapex
3162 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3163 apex {
3164 name: "myapex",
3165 key: "myapex.key",
3166 native_shared_libs: ["libfoo"],
3167 }
3168
3169 apex_key {
3170 name: "myapex.key",
3171 public_key: "testkey.avbpubkey",
3172 private_key: "testkey.pem",
3173 }
3174
3175 apex {
3176 name: "otherapex",
3177 key: "otherapex.key",
3178 native_shared_libs: ["libfoo"],
3179 }
3180
3181 apex_key {
3182 name: "otherapex.key",
3183 public_key: "testkey.avbpubkey",
3184 private_key: "testkey.pem",
3185 }
3186
3187 cc_library {
3188 name: "libfoo",
3189 stl: "none",
3190 system_shared_libs: [],
3191 apex_available: ["otherapex"],
3192 }`)
3193
3194 // libbar is an indirect dep
3195 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3196 apex {
3197 name: "myapex",
3198 key: "myapex.key",
3199 native_shared_libs: ["libfoo"],
3200 }
3201
3202 apex_key {
3203 name: "myapex.key",
3204 public_key: "testkey.avbpubkey",
3205 private_key: "testkey.pem",
3206 }
3207
3208 apex {
3209 name: "otherapex",
3210 key: "otherapex.key",
3211 native_shared_libs: ["libfoo"],
3212 }
3213
3214 apex_key {
3215 name: "otherapex.key",
3216 public_key: "testkey.avbpubkey",
3217 private_key: "testkey.pem",
3218 }
3219
3220 cc_library {
3221 name: "libfoo",
3222 stl: "none",
3223 shared_libs: ["libbar"],
3224 system_shared_libs: [],
3225 apex_available: ["myapex", "otherapex"],
3226 }
3227
3228 cc_library {
3229 name: "libbar",
3230 stl: "none",
3231 system_shared_libs: [],
3232 apex_available: ["otherapex"],
3233 }`)
3234
3235 testApexError(t, "\"otherapex\" is not a valid module name", `
3236 apex {
3237 name: "myapex",
3238 key: "myapex.key",
3239 native_shared_libs: ["libfoo"],
3240 }
3241
3242 apex_key {
3243 name: "myapex.key",
3244 public_key: "testkey.avbpubkey",
3245 private_key: "testkey.pem",
3246 }
3247
3248 cc_library {
3249 name: "libfoo",
3250 stl: "none",
3251 system_shared_libs: [],
3252 apex_available: ["otherapex"],
3253 }`)
3254
3255 ctx, _ := testApex(t, `
3256 apex {
3257 name: "myapex",
3258 key: "myapex.key",
3259 native_shared_libs: ["libfoo", "libbar"],
3260 }
3261
3262 apex_key {
3263 name: "myapex.key",
3264 public_key: "testkey.avbpubkey",
3265 private_key: "testkey.pem",
3266 }
3267
3268 cc_library {
3269 name: "libfoo",
3270 stl: "none",
3271 system_shared_libs: [],
3272 apex_available: ["myapex"],
3273 }
3274
3275 cc_library {
3276 name: "libbar",
3277 stl: "none",
3278 system_shared_libs: [],
3279 apex_available: ["//apex_available:anyapex"],
3280 }`)
3281
3282 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003283 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3284 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3285 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3286 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003287
3288 ctx, _ = testApex(t, `
3289 apex {
3290 name: "myapex",
3291 key: "myapex.key",
3292 }
3293
3294 apex_key {
3295 name: "myapex.key",
3296 public_key: "testkey.avbpubkey",
3297 private_key: "testkey.pem",
3298 }
3299
3300 cc_library {
3301 name: "libfoo",
3302 stl: "none",
3303 system_shared_libs: [],
3304 apex_available: ["//apex_available:platform"],
3305 }`)
3306
3307 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003308 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3309 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003310
3311 ctx, _ = testApex(t, `
3312 apex {
3313 name: "myapex",
3314 key: "myapex.key",
3315 native_shared_libs: ["libfoo"],
3316 }
3317
3318 apex_key {
3319 name: "myapex.key",
3320 public_key: "testkey.avbpubkey",
3321 private_key: "testkey.pem",
3322 }
3323
3324 cc_library {
3325 name: "libfoo",
3326 stl: "none",
3327 system_shared_libs: [],
3328 apex_available: ["myapex"],
3329 static: {
3330 apex_available: ["//apex_available:platform"],
3331 },
3332 }`)
3333
3334 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08003335 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3336 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003337 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08003338 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3339 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003340}
3341
Jiyong Park5d790c32019-11-15 18:40:32 +09003342func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003343 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003344 apex {
3345 name: "myapex",
3346 key: "myapex.key",
3347 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003348 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003349 }
3350
3351 override_apex {
3352 name: "override_myapex",
3353 base: "myapex",
3354 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003355 overrides: ["unknownapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003356 }
3357
3358 apex_key {
3359 name: "myapex.key",
3360 public_key: "testkey.avbpubkey",
3361 private_key: "testkey.pem",
3362 }
3363
3364 android_app {
3365 name: "app",
3366 srcs: ["foo/bar/MyClass.java"],
3367 package_name: "foo",
3368 sdk_version: "none",
3369 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003370 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003371 }
3372
3373 override_android_app {
3374 name: "override_app",
3375 base: "app",
3376 package_name: "bar",
3377 }
3378 `)
3379
Jiyong Park317645e2019-12-05 13:20:58 +09003380 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3381 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3382 if originalVariant.GetOverriddenBy() != "" {
3383 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3384 }
3385 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3386 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3387 }
3388
Jiyong Park5d790c32019-11-15 18:40:32 +09003389 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3390 apexRule := module.Rule("apexRule")
3391 copyCmds := apexRule.Args["copy_commands"]
3392
3393 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3394 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003395
3396 apexBundle := module.Module().(*apexBundle)
3397 name := apexBundle.Name()
3398 if name != "override_myapex" {
3399 t.Errorf("name should be \"override_myapex\", but was %q", name)
3400 }
3401
3402 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3403 var builder strings.Builder
3404 data.Custom(&builder, name, "TARGET_", "", data)
3405 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003406 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003407 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3408 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003409 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003410 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003411 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003412 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3413 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003414}
3415
Jooyung Han214bf372019-11-12 13:03:50 +09003416func TestLegacyAndroid10Support(t *testing.T) {
3417 ctx, _ := testApex(t, `
3418 apex {
3419 name: "myapex",
3420 key: "myapex.key",
3421 legacy_android10_support: true,
3422 }
3423
3424 apex_key {
3425 name: "myapex.key",
3426 public_key: "testkey.avbpubkey",
3427 private_key: "testkey.pem",
3428 }
3429 `)
3430
3431 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3432 args := module.Rule("apexRule").Args
3433 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003434 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Jooyung Han214bf372019-11-12 13:03:50 +09003435}
3436
Jooyung Han58f26ab2019-12-18 15:34:32 +09003437func TestJavaSDKLibrary(t *testing.T) {
3438 ctx, _ := testApex(t, `
3439 apex {
3440 name: "myapex",
3441 key: "myapex.key",
3442 java_libs: ["foo"],
3443 }
3444
3445 apex_key {
3446 name: "myapex.key",
3447 public_key: "testkey.avbpubkey",
3448 private_key: "testkey.pem",
3449 }
3450
3451 java_sdk_library {
3452 name: "foo",
3453 srcs: ["a.java"],
3454 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003455 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003456 }
3457 `, withFiles(map[string][]byte{
3458 "api/current.txt": nil,
3459 "api/removed.txt": nil,
3460 "api/system-current.txt": nil,
3461 "api/system-removed.txt": nil,
3462 "api/test-current.txt": nil,
3463 "api/test-removed.txt": nil,
3464 }))
3465
3466 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003467 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003468 "javalib/foo.jar",
3469 "etc/permissions/foo.xml",
3470 })
3471 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jooyung Han624058e2019-12-24 18:38:06 +09003472 xml := ctx.ModuleForTests("foo", "android_common_myapex").Output("foo.xml")
3473 ensureContains(t, xml.Args["content"], `<library name="foo" file="/apex/myapex/javalib/foo.jar"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003474}
3475
Jiyong Park479321d2019-12-16 11:47:12 +09003476func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3477 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3478 apex {
3479 name: "myapex",
3480 key: "myapex.key",
3481 java_libs: ["myjar"],
3482 }
3483
3484 apex_key {
3485 name: "myapex.key",
3486 public_key: "testkey.avbpubkey",
3487 private_key: "testkey.pem",
3488 }
3489
3490 java_library {
3491 name: "myjar",
3492 srcs: ["foo/bar/MyClass.java"],
3493 sdk_version: "none",
3494 system_modules: "none",
3495 }
3496 `)
3497}
3498
Jiyong Park7afd1072019-12-30 16:56:33 +09003499func TestCarryRequiredModuleNames(t *testing.T) {
3500 ctx, config := testApex(t, `
3501 apex {
3502 name: "myapex",
3503 key: "myapex.key",
3504 native_shared_libs: ["mylib"],
3505 }
3506
3507 apex_key {
3508 name: "myapex.key",
3509 public_key: "testkey.avbpubkey",
3510 private_key: "testkey.pem",
3511 }
3512
3513 cc_library {
3514 name: "mylib",
3515 srcs: ["mylib.cpp"],
3516 system_shared_libs: [],
3517 stl: "none",
3518 required: ["a", "b"],
3519 host_required: ["c", "d"],
3520 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003521 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003522 }
3523 `)
3524
3525 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3526 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3527 name := apexBundle.BaseModuleName()
3528 prefix := "TARGET_"
3529 var builder strings.Builder
3530 data.Custom(&builder, name, prefix, "", data)
3531 androidMk := builder.String()
3532 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3533 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3534 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3535}
3536
Jiyong Park7cd10e32020-01-14 09:22:18 +09003537func TestSymlinksFromApexToSystem(t *testing.T) {
3538 bp := `
3539 apex {
3540 name: "myapex",
3541 key: "myapex.key",
3542 native_shared_libs: ["mylib"],
3543 java_libs: ["myjar"],
3544 }
3545
3546 apex_key {
3547 name: "myapex.key",
3548 public_key: "testkey.avbpubkey",
3549 private_key: "testkey.pem",
3550 }
3551
3552 cc_library {
3553 name: "mylib",
3554 srcs: ["mylib.cpp"],
3555 shared_libs: ["myotherlib"],
3556 system_shared_libs: [],
3557 stl: "none",
3558 apex_available: [
3559 "myapex",
3560 "//apex_available:platform",
3561 ],
3562 }
3563
3564 cc_library {
3565 name: "myotherlib",
3566 srcs: ["mylib.cpp"],
3567 system_shared_libs: [],
3568 stl: "none",
3569 apex_available: [
3570 "myapex",
3571 "//apex_available:platform",
3572 ],
3573 }
3574
3575 java_library {
3576 name: "myjar",
3577 srcs: ["foo/bar/MyClass.java"],
3578 sdk_version: "none",
3579 system_modules: "none",
3580 libs: ["myotherjar"],
3581 compile_dex: true,
3582 apex_available: [
3583 "myapex",
3584 "//apex_available:platform",
3585 ],
3586 }
3587
3588 java_library {
3589 name: "myotherjar",
3590 srcs: ["foo/bar/MyClass.java"],
3591 sdk_version: "none",
3592 system_modules: "none",
3593 apex_available: [
3594 "myapex",
3595 "//apex_available:platform",
3596 ],
3597 }
3598 `
3599
3600 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3601 for _, f := range files {
3602 if f.path == file {
3603 if f.isLink {
3604 t.Errorf("%q is not a real file", file)
3605 }
3606 return
3607 }
3608 }
3609 t.Errorf("%q is not found", file)
3610 }
3611
3612 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3613 for _, f := range files {
3614 if f.path == file {
3615 if !f.isLink {
3616 t.Errorf("%q is not a symlink", file)
3617 }
3618 return
3619 }
3620 }
3621 t.Errorf("%q is not found", file)
3622 }
3623
3624 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003625 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003626 ensureRealfileExists(t, files, "javalib/myjar.jar")
3627 ensureRealfileExists(t, files, "lib64/mylib.so")
3628 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3629
3630 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003631 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003632 ensureRealfileExists(t, files, "javalib/myjar.jar")
3633 ensureRealfileExists(t, files, "lib64/mylib.so")
3634 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
3635}
3636
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003637func TestMain(m *testing.M) {
3638 run := func() int {
3639 setUp()
3640 defer tearDown()
3641
3642 return m.Run()
3643 }
3644
3645 os.Exit(run())
3646}