blob: 80decd087734813a78e5239eadaa4eed85c7c52a [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
Jooyung Han344d5432019-08-23 11:17:39 +090094func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +090095 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +090096
97 bp = bp + `
98 toolchain_library {
99 name: "libcompiler_rt-extras",
100 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900101 vendor_available: true,
102 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900103 }
104
105 toolchain_library {
106 name: "libatomic",
107 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900108 vendor_available: true,
109 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900110 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900111 }
112
113 toolchain_library {
114 name: "libgcc",
115 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900116 vendor_available: true,
117 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900118 }
119
120 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700121 name: "libgcc_stripped",
122 src: "",
123 vendor_available: true,
124 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900125 native_bridge_supported: true,
Yi Kongacee27c2019-03-29 20:05:14 -0700126 }
127
128 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900129 name: "libclang_rt.builtins-aarch64-android",
130 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900131 vendor_available: true,
132 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900133 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900134 }
135
136 toolchain_library {
137 name: "libclang_rt.builtins-arm-android",
138 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900139 vendor_available: true,
140 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900141 native_bridge_supported: true,
142 }
143
144 toolchain_library {
145 name: "libclang_rt.builtins-x86_64-android",
146 src: "",
147 vendor_available: true,
148 recovery_available: true,
149 native_bridge_supported: true,
150 }
151
152 toolchain_library {
153 name: "libclang_rt.builtins-i686-android",
154 src: "",
155 vendor_available: true,
156 recovery_available: true,
157 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900158 }
159
160 cc_object {
161 name: "crtbegin_so",
162 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900163 vendor_available: true,
164 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900165 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900166 }
167
168 cc_object {
169 name: "crtend_so",
170 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900171 vendor_available: true,
172 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900173 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900174 }
175
Alex Light3d673592019-01-18 14:37:31 -0800176 cc_object {
177 name: "crtbegin_static",
178 stl: "none",
179 }
180
181 cc_object {
182 name: "crtend_android",
183 stl: "none",
184 }
185
Jiyong Parkda6eb592018-12-19 17:12:36 +0900186 llndk_library {
187 name: "libc",
188 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900189 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900190 }
191
192 llndk_library {
193 name: "libm",
194 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900195 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900196 }
197
198 llndk_library {
199 name: "libdl",
200 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900201 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900202 }
Jooyung Han54aca7b2019-11-20 02:26:02 +0900203
204 filegroup {
205 name: "myapex-file_contexts",
206 srcs: [
207 "system/sepolicy/apex/myapex-file_contexts",
208 ],
209 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900210 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800211
Dario Frenicde2a032019-10-27 00:29:22 +0100212 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900213
Jooyung Han344d5432019-08-23 11:17:39 +0900214 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900215 "a.java": nil,
216 "PrebuiltAppFoo.apk": nil,
217 "PrebuiltAppFooPriv.apk": nil,
218 "build/make/target/product/security": nil,
219 "apex_manifest.json": nil,
220 "AndroidManifest.xml": nil,
221 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900222 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900223 "system/sepolicy/apex/otherapex-file_contexts": nil,
224 "system/sepolicy/apex/commonapex-file_contexts": nil,
225 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800226 "mylib.cpp": nil,
227 "mylib_common.cpp": nil,
228 "mytest.cpp": nil,
229 "mytest1.cpp": nil,
230 "mytest2.cpp": nil,
231 "mytest3.cpp": nil,
232 "myprebuilt": nil,
233 "my_include": nil,
234 "foo/bar/MyClass.java": nil,
235 "prebuilt.jar": nil,
236 "vendor/foo/devkeys/test.x509.pem": nil,
237 "vendor/foo/devkeys/test.pk8": nil,
238 "testkey.x509.pem": nil,
239 "testkey.pk8": nil,
240 "testkey.override.x509.pem": nil,
241 "testkey.override.pk8": nil,
242 "vendor/foo/devkeys/testkey.avbpubkey": nil,
243 "vendor/foo/devkeys/testkey.pem": nil,
244 "NOTICE": nil,
245 "custom_notice": nil,
246 "testkey2.avbpubkey": nil,
247 "testkey2.pem": nil,
248 "myapex-arm64.apex": nil,
249 "myapex-arm.apex": nil,
250 "frameworks/base/api/current.txt": nil,
251 "framework/aidl/a.aidl": nil,
252 "build/make/core/proguard.flags": nil,
253 "build/make/core/proguard_basic_keeps.flags": nil,
254 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900255 }
256
257 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800258 // The fs now needs to be populated before creating the config, call handlers twice
259 // for now, once to get any fs changes, and later after the config was created to
260 // set product variables or targets.
261 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
262 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900263 }
264
Colin Cross98be1bb2019-12-13 20:41:13 -0800265 config := android.TestArchConfig(buildDir, nil, bp, fs)
266 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
267 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
268 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
269 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
270 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
271 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
272
273 for _, handler := range handlers {
274 // The fs now needs to be populated before creating the config, call handlers twice
275 // for now, earlier to get any fs changes, and now after the config was created to
276 // set product variables or targets.
277 tempFS := map[string][]byte{}
278 handler(tempFS, config)
279 }
280
281 ctx := android.NewTestArchContext()
282 ctx.RegisterModuleType("apex", BundleFactory)
283 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
284 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
285 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
286 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
287 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
288 ctx.RegisterModuleType("override_apex", overrideApexFactory)
289
Paul Duffin77980a82019-12-19 16:01:36 +0000290 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800291 ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800292 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800293 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
294 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800295 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
296 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800297 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000298 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000299 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000300 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900301 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800302
303 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800304 ctx.PreDepsMutators(RegisterPreDepsMutators)
305 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
306 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800307
308 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900309
Jooyung Han5c998b92019-06-27 11:30:33 +0900310 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900311}
312
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700313func setUp() {
314 var err error
315 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900316 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700317 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900318 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900319}
320
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700321func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900322 os.RemoveAll(buildDir)
323}
324
325// ensure that 'result' contains 'expected'
326func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900327 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900328 if !strings.Contains(result, expected) {
329 t.Errorf("%q is not found in %q", expected, result)
330 }
331}
332
333// ensures that 'result' does not contain 'notExpected'
334func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900335 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900336 if strings.Contains(result, notExpected) {
337 t.Errorf("%q is found in %q", notExpected, result)
338 }
339}
340
341func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900342 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900343 if !android.InList(expected, result) {
344 t.Errorf("%q is not found in %v", expected, result)
345 }
346}
347
348func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900349 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900350 if android.InList(notExpected, result) {
351 t.Errorf("%q is found in %v", notExpected, result)
352 }
353}
354
Jooyung Hane1633032019-08-01 17:41:43 +0900355func ensureListEmpty(t *testing.T, result []string) {
356 t.Helper()
357 if len(result) > 0 {
358 t.Errorf("%q is expected to be empty", result)
359 }
360}
361
Jiyong Park25fc6a92018-11-18 18:02:45 +0900362// Minimal test
363func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700364 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900365 apex_defaults {
366 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900367 manifest: ":myapex.manifest",
368 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900369 key: "myapex.key",
370 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800371 multilib: {
372 both: {
373 binaries: ["foo",],
374 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900375 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900376 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900377 }
378
Jiyong Park30ca9372019-02-07 16:27:23 +0900379 apex {
380 name: "myapex",
381 defaults: ["myapex-defaults"],
382 }
383
Jiyong Park25fc6a92018-11-18 18:02:45 +0900384 apex_key {
385 name: "myapex.key",
386 public_key: "testkey.avbpubkey",
387 private_key: "testkey.pem",
388 }
389
Jiyong Park809bb722019-02-13 21:33:49 +0900390 filegroup {
391 name: "myapex.manifest",
392 srcs: ["apex_manifest.json"],
393 }
394
395 filegroup {
396 name: "myapex.androidmanifest",
397 srcs: ["AndroidManifest.xml"],
398 }
399
Jiyong Park25fc6a92018-11-18 18:02:45 +0900400 cc_library {
401 name: "mylib",
402 srcs: ["mylib.cpp"],
403 shared_libs: ["mylib2"],
404 system_shared_libs: [],
405 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000406 // TODO: remove //apex_available:platform
407 apex_available: [
408 "//apex_available:platform",
409 "myapex",
410 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900411 }
412
Alex Light3d673592019-01-18 14:37:31 -0800413 cc_binary {
414 name: "foo",
415 srcs: ["mylib.cpp"],
416 compile_multilib: "both",
417 multilib: {
418 lib32: {
419 suffix: "32",
420 },
421 lib64: {
422 suffix: "64",
423 },
424 },
425 symlinks: ["foo_link_"],
426 symlink_preferred_arch: true,
427 system_shared_libs: [],
428 static_executable: true,
429 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000430 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800431 }
432
Jiyong Park25fc6a92018-11-18 18:02:45 +0900433 cc_library {
434 name: "mylib2",
435 srcs: ["mylib.cpp"],
436 system_shared_libs: [],
437 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900438 notice: "custom_notice",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000439 // TODO: remove //apex_available:platform
440 apex_available: [
441 "//apex_available:platform",
442 "myapex",
443 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900444 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900445
446 java_library {
447 name: "myjar",
448 srcs: ["foo/bar/MyClass.java"],
449 sdk_version: "none",
450 system_modules: "none",
451 compile_dex: true,
452 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900453 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000454 // TODO: remove //apex_available:platform
455 apex_available: [
456 "//apex_available:platform",
457 "myapex",
458 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900459 }
460
461 java_library {
462 name: "myotherjar",
463 srcs: ["foo/bar/MyClass.java"],
464 sdk_version: "none",
465 system_modules: "none",
466 compile_dex: true,
467 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900468
469 java_library {
470 name: "mysharedjar",
471 srcs: ["foo/bar/MyClass.java"],
472 sdk_version: "none",
473 system_modules: "none",
474 compile_dex: true,
475 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900476 `)
477
Sundong Ahnabb64432019-10-22 13:58:29 +0900478 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900479
480 optFlags := apexRule.Args["opt_flags"]
481 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700482 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900483 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900484
Jiyong Park25fc6a92018-11-18 18:02:45 +0900485 copyCmds := apexRule.Args["copy_commands"]
486
487 // Ensure that main rule creates an output
488 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
489
490 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800491 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900492 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900493
494 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800495 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900496 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900497
498 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800499 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
500 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900501 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
502 // .. but not for java libs
503 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900504 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800505
Colin Cross7113d202019-11-20 16:39:12 -0800506 // Ensure that the platform variant ends with _shared or _common
507 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
508 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900509 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
510 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900511 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
512
513 // Ensure that dynamic dependency to java libs are not included
514 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800515
516 // Ensure that all symlinks are present.
517 found_foo_link_64 := false
518 found_foo := false
519 for _, cmd := range strings.Split(copyCmds, " && ") {
520 if strings.HasPrefix(cmd, "ln -s foo64") {
521 if strings.HasSuffix(cmd, "bin/foo") {
522 found_foo = true
523 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
524 found_foo_link_64 = true
525 }
526 }
527 }
528 good := found_foo && found_foo_link_64
529 if !good {
530 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
531 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900532
Sundong Ahnabb64432019-10-22 13:58:29 +0900533 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700534 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700535 if len(noticeInputs) != 2 {
536 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900537 }
538 ensureListContains(t, noticeInputs, "NOTICE")
539 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900540
541 depsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("myapex-deps-info.txt").Args["content"], "\\n")
542 ensureListContains(t, depsInfo, "internal myjar")
543 ensureListContains(t, depsInfo, "internal mylib")
544 ensureListContains(t, depsInfo, "internal mylib2")
545 ensureListContains(t, depsInfo, "internal myotherjar")
Alex Light5098a612018-11-29 17:12:15 -0800546}
547
Jooyung Hanf21c7972019-12-16 22:32:06 +0900548func TestDefaults(t *testing.T) {
549 ctx, _ := testApex(t, `
550 apex_defaults {
551 name: "myapex-defaults",
552 key: "myapex.key",
553 prebuilts: ["myetc"],
554 native_shared_libs: ["mylib"],
555 java_libs: ["myjar"],
556 apps: ["AppFoo"],
557 }
558
559 prebuilt_etc {
560 name: "myetc",
561 src: "myprebuilt",
562 }
563
564 apex {
565 name: "myapex",
566 defaults: ["myapex-defaults"],
567 }
568
569 apex_key {
570 name: "myapex.key",
571 public_key: "testkey.avbpubkey",
572 private_key: "testkey.pem",
573 }
574
575 cc_library {
576 name: "mylib",
577 system_shared_libs: [],
578 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000579 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900580 }
581
582 java_library {
583 name: "myjar",
584 srcs: ["foo/bar/MyClass.java"],
585 sdk_version: "none",
586 system_modules: "none",
587 compile_dex: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000588 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900589 }
590
591 android_app {
592 name: "AppFoo",
593 srcs: ["foo/bar/MyClass.java"],
594 sdk_version: "none",
595 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000596 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900597 }
598 `)
599 ensureExactContents(t, ctx, "myapex", []string{
600 "etc/myetc",
601 "javalib/myjar.jar",
602 "lib64/mylib.so",
603 "app/AppFoo/AppFoo.apk",
604 })
605}
606
Jooyung Han01a3ee22019-11-02 02:52:25 +0900607func TestApexManifest(t *testing.T) {
608 ctx, _ := testApex(t, `
609 apex {
610 name: "myapex",
611 key: "myapex.key",
612 }
613
614 apex_key {
615 name: "myapex.key",
616 public_key: "testkey.avbpubkey",
617 private_key: "testkey.pem",
618 }
619 `)
620
621 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900622 args := module.Rule("apexRule").Args
623 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
624 t.Error("manifest should be apex_manifest.pb, but " + manifest)
625 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900626}
627
Alex Light5098a612018-11-29 17:12:15 -0800628func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700629 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800630 apex {
631 name: "myapex",
632 key: "myapex.key",
633 payload_type: "zip",
634 native_shared_libs: ["mylib"],
635 }
636
637 apex_key {
638 name: "myapex.key",
639 public_key: "testkey.avbpubkey",
640 private_key: "testkey.pem",
641 }
642
643 cc_library {
644 name: "mylib",
645 srcs: ["mylib.cpp"],
646 shared_libs: ["mylib2"],
647 system_shared_libs: [],
648 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000649 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800650 }
651
652 cc_library {
653 name: "mylib2",
654 srcs: ["mylib.cpp"],
655 system_shared_libs: [],
656 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000657 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800658 }
659 `)
660
Sundong Ahnabb64432019-10-22 13:58:29 +0900661 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800662 copyCmds := zipApexRule.Args["copy_commands"]
663
664 // Ensure that main rule creates an output
665 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
666
667 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800668 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800669
670 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800671 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800672
673 // Ensure that both direct and indirect deps are copied into apex
674 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
675 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900676}
677
678func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700679 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900680 apex {
681 name: "myapex",
682 key: "myapex.key",
683 native_shared_libs: ["mylib", "mylib3"],
684 }
685
686 apex_key {
687 name: "myapex.key",
688 public_key: "testkey.avbpubkey",
689 private_key: "testkey.pem",
690 }
691
692 cc_library {
693 name: "mylib",
694 srcs: ["mylib.cpp"],
695 shared_libs: ["mylib2", "mylib3"],
696 system_shared_libs: [],
697 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000698 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900699 }
700
701 cc_library {
702 name: "mylib2",
703 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900704 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900705 system_shared_libs: [],
706 stl: "none",
707 stubs: {
708 versions: ["1", "2", "3"],
709 },
710 }
711
712 cc_library {
713 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900714 srcs: ["mylib.cpp"],
715 shared_libs: ["mylib4"],
716 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900717 stl: "none",
718 stubs: {
719 versions: ["10", "11", "12"],
720 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000721 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900722 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900723
724 cc_library {
725 name: "mylib4",
726 srcs: ["mylib.cpp"],
727 system_shared_libs: [],
728 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000729 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900730 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900731 `)
732
Sundong Ahnabb64432019-10-22 13:58:29 +0900733 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900734 copyCmds := apexRule.Args["copy_commands"]
735
736 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800737 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900738
739 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800740 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900741
742 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800743 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900744
Colin Cross7113d202019-11-20 16:39:12 -0800745 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900746
747 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900748 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900749 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900750 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900751
752 // 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 -0800753 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900754 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800755 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900756
757 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800758 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900759 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900760
761 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900762 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900763
764 ensureExactContents(t, ctx, "myapex", []string{
765 "lib64/mylib.so",
766 "lib64/mylib3.so",
767 "lib64/mylib4.so",
768 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900769}
770
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900771func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700772 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900773 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900774 name: "myapex2",
775 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900776 native_shared_libs: ["mylib"],
777 }
778
779 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900780 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900781 public_key: "testkey.avbpubkey",
782 private_key: "testkey.pem",
783 }
784
785 cc_library {
786 name: "mylib",
787 srcs: ["mylib.cpp"],
788 shared_libs: ["libfoo#10"],
789 system_shared_libs: [],
790 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000791 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900792 }
793
794 cc_library {
795 name: "libfoo",
796 srcs: ["mylib.cpp"],
797 shared_libs: ["libbar"],
798 system_shared_libs: [],
799 stl: "none",
800 stubs: {
801 versions: ["10", "20", "30"],
802 },
803 }
804
805 cc_library {
806 name: "libbar",
807 srcs: ["mylib.cpp"],
808 system_shared_libs: [],
809 stl: "none",
810 }
811
812 `)
813
Jiyong Park83dc74b2020-01-14 18:38:44 +0900814 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900815 copyCmds := apexRule.Args["copy_commands"]
816
817 // Ensure that direct non-stubs dep is always included
818 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
819
820 // Ensure that indirect stubs dep is not included
821 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
822
823 // Ensure that dependency of stubs is not included
824 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
825
Jiyong Park83dc74b2020-01-14 18:38:44 +0900826 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900827
828 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900829 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900830 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900831 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900832
Jiyong Park3ff16992019-12-27 14:11:47 +0900833 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900834
835 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
836 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900837
838 depsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("myapex2-deps-info.txt").Args["content"], "\\n")
839 ensureListContains(t, depsInfo, "internal mylib")
840 ensureListContains(t, depsInfo, "external libfoo")
841 ensureListNotContains(t, depsInfo, "internal libfoo")
842 ensureListNotContains(t, depsInfo, "external mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900843}
844
Jooyung Hand3639552019-08-09 12:57:43 +0900845func TestApexWithRuntimeLibsDependency(t *testing.T) {
846 /*
847 myapex
848 |
849 v (runtime_libs)
850 mylib ------+------> libfoo [provides stub]
851 |
852 `------> libbar
853 */
854 ctx, _ := testApex(t, `
855 apex {
856 name: "myapex",
857 key: "myapex.key",
858 native_shared_libs: ["mylib"],
859 }
860
861 apex_key {
862 name: "myapex.key",
863 public_key: "testkey.avbpubkey",
864 private_key: "testkey.pem",
865 }
866
867 cc_library {
868 name: "mylib",
869 srcs: ["mylib.cpp"],
870 runtime_libs: ["libfoo", "libbar"],
871 system_shared_libs: [],
872 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000873 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900874 }
875
876 cc_library {
877 name: "libfoo",
878 srcs: ["mylib.cpp"],
879 system_shared_libs: [],
880 stl: "none",
881 stubs: {
882 versions: ["10", "20", "30"],
883 },
884 }
885
886 cc_library {
887 name: "libbar",
888 srcs: ["mylib.cpp"],
889 system_shared_libs: [],
890 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000891 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900892 }
893
894 `)
895
Sundong Ahnabb64432019-10-22 13:58:29 +0900896 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900897 copyCmds := apexRule.Args["copy_commands"]
898
899 // Ensure that direct non-stubs dep is always included
900 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
901
902 // Ensure that indirect stubs dep is not included
903 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
904
905 // Ensure that runtime_libs dep in included
906 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
907
Sundong Ahnabb64432019-10-22 13:58:29 +0900908 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900909 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
910 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900911
912}
913
Jooyung Han9c80bae2019-08-20 17:30:57 +0900914func TestApexDependencyToLLNDK(t *testing.T) {
915 ctx, _ := testApex(t, `
916 apex {
917 name: "myapex",
918 key: "myapex.key",
919 use_vendor: true,
920 native_shared_libs: ["mylib"],
921 }
922
923 apex_key {
924 name: "myapex.key",
925 public_key: "testkey.avbpubkey",
926 private_key: "testkey.pem",
927 }
928
929 cc_library {
930 name: "mylib",
931 srcs: ["mylib.cpp"],
932 vendor_available: true,
933 shared_libs: ["libbar"],
934 system_shared_libs: [],
935 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000936 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900937 }
938
939 cc_library {
940 name: "libbar",
941 srcs: ["mylib.cpp"],
942 system_shared_libs: [],
943 stl: "none",
944 }
945
946 llndk_library {
947 name: "libbar",
948 symbol_file: "",
949 }
Jooyung Handc782442019-11-01 03:14:38 +0900950 `, func(fs map[string][]byte, config android.Config) {
951 setUseVendorWhitelistForTest(config, []string{"myapex"})
952 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900953
Sundong Ahnabb64432019-10-22 13:58:29 +0900954 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900955 copyCmds := apexRule.Args["copy_commands"]
956
957 // Ensure that LLNDK dep is not included
958 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
959
Sundong Ahnabb64432019-10-22 13:58:29 +0900960 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900961 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900962
963 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900964 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900965
966}
967
Jiyong Park25fc6a92018-11-18 18:02:45 +0900968func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700969 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900970 apex {
971 name: "myapex",
972 key: "myapex.key",
973 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
974 }
975
976 apex_key {
977 name: "myapex.key",
978 public_key: "testkey.avbpubkey",
979 private_key: "testkey.pem",
980 }
981
982 cc_library {
983 name: "mylib",
984 srcs: ["mylib.cpp"],
985 shared_libs: ["libdl#27"],
986 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000987 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900988 }
989
990 cc_library_shared {
991 name: "mylib_shared",
992 srcs: ["mylib.cpp"],
993 shared_libs: ["libdl#27"],
994 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000995 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900996 }
997
998 cc_library {
999 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -07001000 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001001 nocrt: true,
1002 system_shared_libs: [],
1003 stl: "none",
1004 stubs: {
1005 versions: ["27", "28", "29"],
1006 },
1007 }
1008
1009 cc_library {
1010 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -07001011 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001012 nocrt: true,
1013 system_shared_libs: [],
1014 stl: "none",
1015 stubs: {
1016 versions: ["27", "28", "29"],
1017 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001018 apex_available: [
1019 "//apex_available:platform",
1020 "myapex"
1021 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001022 }
1023
1024 cc_library {
1025 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -07001026 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001027 nocrt: true,
1028 system_shared_libs: [],
1029 stl: "none",
1030 stubs: {
1031 versions: ["27", "28", "29"],
1032 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001033 apex_available: [
1034 "//apex_available:platform",
1035 "myapex"
1036 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001037 }
Jiyong Parkb0788572018-12-20 22:10:17 +09001038
1039 cc_library {
1040 name: "libBootstrap",
1041 srcs: ["mylib.cpp"],
1042 stl: "none",
1043 bootstrap: true,
1044 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001045 `)
1046
Sundong Ahnabb64432019-10-22 13:58:29 +09001047 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001048 copyCmds := apexRule.Args["copy_commands"]
1049
1050 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001051 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001052 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1053 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001054
1055 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001056 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001057
Colin Cross7113d202019-11-20 16:39:12 -08001058 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1059 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1060 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001061
1062 // For dependency to libc
1063 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001064 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001065 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001066 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001067 // ... Cflags from stub is correctly exported to mylib
1068 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1069 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1070
1071 // For dependency to libm
1072 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001073 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001074 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001075 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001076 // ... and is not compiling with the stub
1077 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1078 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1079
1080 // For dependency to libdl
1081 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001082 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001083 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001084 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1085 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001086 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001087 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001088 // ... Cflags from stub is correctly exported to mylib
1089 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1090 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001091
1092 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001093 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1094 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1095 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1096 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001097}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001098
1099func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001100 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001101 apex {
1102 name: "myapex",
1103 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001104 native_shared_libs: ["mylib"],
1105 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001106 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001107 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001108 }
1109
1110 apex_key {
1111 name: "myapex.key",
1112 public_key: "testkey.avbpubkey",
1113 private_key: "testkey.pem",
1114 }
1115
1116 prebuilt_etc {
1117 name: "myetc",
1118 src: "myprebuilt",
1119 sub_dir: "foo/bar",
1120 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001121
1122 cc_library {
1123 name: "mylib",
1124 srcs: ["mylib.cpp"],
1125 relative_install_path: "foo/bar",
1126 system_shared_libs: [],
1127 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001128 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001129 }
1130
1131 cc_binary {
1132 name: "mybin",
1133 srcs: ["mylib.cpp"],
1134 relative_install_path: "foo/bar",
1135 system_shared_libs: [],
1136 static_executable: true,
1137 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001138 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001139 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001140 `)
1141
Sundong Ahnabb64432019-10-22 13:58:29 +09001142 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001143 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1144
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001145 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001146 ensureListContains(t, dirs, "etc")
1147 ensureListContains(t, dirs, "etc/foo")
1148 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001149 ensureListContains(t, dirs, "lib64")
1150 ensureListContains(t, dirs, "lib64/foo")
1151 ensureListContains(t, dirs, "lib64/foo/bar")
1152 ensureListContains(t, dirs, "lib")
1153 ensureListContains(t, dirs, "lib/foo")
1154 ensureListContains(t, dirs, "lib/foo/bar")
1155
Jiyong Parkbd13e442019-03-15 18:10:35 +09001156 ensureListContains(t, dirs, "bin")
1157 ensureListContains(t, dirs, "bin/foo")
1158 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001159}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001160
1161func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001162 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001163 apex {
1164 name: "myapex",
1165 key: "myapex.key",
1166 native_shared_libs: ["mylib"],
1167 use_vendor: true,
1168 }
1169
1170 apex_key {
1171 name: "myapex.key",
1172 public_key: "testkey.avbpubkey",
1173 private_key: "testkey.pem",
1174 }
1175
1176 cc_library {
1177 name: "mylib",
1178 srcs: ["mylib.cpp"],
1179 shared_libs: ["mylib2"],
1180 system_shared_libs: [],
1181 vendor_available: true,
1182 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001183 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001184 }
1185
1186 cc_library {
1187 name: "mylib2",
1188 srcs: ["mylib.cpp"],
1189 system_shared_libs: [],
1190 vendor_available: true,
1191 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001192 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001193 }
Jooyung Handc782442019-11-01 03:14:38 +09001194 `, func(fs map[string][]byte, config android.Config) {
1195 setUseVendorWhitelistForTest(config, []string{"myapex"})
1196 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001197
1198 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001199 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001200 for _, implicit := range i.Implicits {
1201 inputsList = append(inputsList, implicit.String())
1202 }
1203 }
1204 inputsString := strings.Join(inputsList, " ")
1205
1206 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001207 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1208 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001209
1210 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001211 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1212 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001213}
Jiyong Park16e91a02018-12-20 18:18:08 +09001214
Jooyung Handc782442019-11-01 03:14:38 +09001215func TestUseVendorRestriction(t *testing.T) {
1216 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1217 apex {
1218 name: "myapex",
1219 key: "myapex.key",
1220 use_vendor: true,
1221 }
1222 apex_key {
1223 name: "myapex.key",
1224 public_key: "testkey.avbpubkey",
1225 private_key: "testkey.pem",
1226 }
1227 `, func(fs map[string][]byte, config android.Config) {
1228 setUseVendorWhitelistForTest(config, []string{""})
1229 })
1230 // no error with whitelist
1231 testApex(t, `
1232 apex {
1233 name: "myapex",
1234 key: "myapex.key",
1235 use_vendor: true,
1236 }
1237 apex_key {
1238 name: "myapex.key",
1239 public_key: "testkey.avbpubkey",
1240 private_key: "testkey.pem",
1241 }
1242 `, func(fs map[string][]byte, config android.Config) {
1243 setUseVendorWhitelistForTest(config, []string{"myapex"})
1244 })
1245}
1246
Jooyung Han5c998b92019-06-27 11:30:33 +09001247func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1248 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1249 apex {
1250 name: "myapex",
1251 key: "myapex.key",
1252 native_shared_libs: ["mylib"],
1253 use_vendor: true,
1254 }
1255
1256 apex_key {
1257 name: "myapex.key",
1258 public_key: "testkey.avbpubkey",
1259 private_key: "testkey.pem",
1260 }
1261
1262 cc_library {
1263 name: "mylib",
1264 srcs: ["mylib.cpp"],
1265 system_shared_libs: [],
1266 stl: "none",
1267 }
1268 `)
1269}
1270
Jiyong Park16e91a02018-12-20 18:18:08 +09001271func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001272 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001273 apex {
1274 name: "myapex",
1275 key: "myapex.key",
1276 native_shared_libs: ["mylib"],
1277 }
1278
1279 apex_key {
1280 name: "myapex.key",
1281 public_key: "testkey.avbpubkey",
1282 private_key: "testkey.pem",
1283 }
1284
1285 cc_library {
1286 name: "mylib",
1287 srcs: ["mylib.cpp"],
1288 system_shared_libs: [],
1289 stl: "none",
1290 stubs: {
1291 versions: ["1", "2", "3"],
1292 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001293 apex_available: [
1294 "//apex_available:platform",
1295 "myapex",
1296 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001297 }
1298
1299 cc_binary {
1300 name: "not_in_apex",
1301 srcs: ["mylib.cpp"],
1302 static_libs: ["mylib"],
1303 static_executable: true,
1304 system_shared_libs: [],
1305 stl: "none",
1306 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001307 `)
1308
Colin Cross7113d202019-11-20 16:39:12 -08001309 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001310
1311 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001312 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001313}
Jiyong Park9335a262018-12-24 11:31:58 +09001314
1315func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001316 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001317 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001318 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001319 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001320 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001321 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001322 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001323 }
1324
1325 cc_library {
1326 name: "mylib",
1327 srcs: ["mylib.cpp"],
1328 system_shared_libs: [],
1329 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001330 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001331 }
1332
1333 apex_key {
1334 name: "myapex.key",
1335 public_key: "testkey.avbpubkey",
1336 private_key: "testkey.pem",
1337 }
1338
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001339 android_app_certificate {
1340 name: "myapex.certificate",
1341 certificate: "testkey",
1342 }
1343
1344 android_app_certificate {
1345 name: "myapex.certificate.override",
1346 certificate: "testkey.override",
1347 }
1348
Jiyong Park9335a262018-12-24 11:31:58 +09001349 `)
1350
1351 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001352 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001353
1354 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1355 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1356 "vendor/foo/devkeys/testkey.avbpubkey")
1357 }
1358 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1359 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1360 "vendor/foo/devkeys/testkey.pem")
1361 }
1362
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001363 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001364 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001365 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001366 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001367 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001368 }
1369}
Jiyong Park58e364a2019-01-19 19:24:06 +09001370
Jooyung Hanf121a652019-12-17 14:30:11 +09001371func TestCertificate(t *testing.T) {
1372 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1373 ctx, _ := testApex(t, `
1374 apex {
1375 name: "myapex",
1376 key: "myapex.key",
1377 }
1378 apex_key {
1379 name: "myapex.key",
1380 public_key: "testkey.avbpubkey",
1381 private_key: "testkey.pem",
1382 }`)
1383 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1384 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1385 if actual := rule.Args["certificates"]; actual != expected {
1386 t.Errorf("certificates should be %q, not %q", expected, actual)
1387 }
1388 })
1389 t.Run("override when unspecified", func(t *testing.T) {
1390 ctx, _ := testApex(t, `
1391 apex {
1392 name: "myapex_keytest",
1393 key: "myapex.key",
1394 file_contexts: ":myapex-file_contexts",
1395 }
1396 apex_key {
1397 name: "myapex.key",
1398 public_key: "testkey.avbpubkey",
1399 private_key: "testkey.pem",
1400 }
1401 android_app_certificate {
1402 name: "myapex.certificate.override",
1403 certificate: "testkey.override",
1404 }`)
1405 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1406 expected := "testkey.override.x509.pem testkey.override.pk8"
1407 if actual := rule.Args["certificates"]; actual != expected {
1408 t.Errorf("certificates should be %q, not %q", expected, actual)
1409 }
1410 })
1411 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1412 ctx, _ := testApex(t, `
1413 apex {
1414 name: "myapex",
1415 key: "myapex.key",
1416 certificate: ":myapex.certificate",
1417 }
1418 apex_key {
1419 name: "myapex.key",
1420 public_key: "testkey.avbpubkey",
1421 private_key: "testkey.pem",
1422 }
1423 android_app_certificate {
1424 name: "myapex.certificate",
1425 certificate: "testkey",
1426 }`)
1427 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1428 expected := "testkey.x509.pem testkey.pk8"
1429 if actual := rule.Args["certificates"]; actual != expected {
1430 t.Errorf("certificates should be %q, not %q", expected, actual)
1431 }
1432 })
1433 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1434 ctx, _ := testApex(t, `
1435 apex {
1436 name: "myapex_keytest",
1437 key: "myapex.key",
1438 file_contexts: ":myapex-file_contexts",
1439 certificate: ":myapex.certificate",
1440 }
1441 apex_key {
1442 name: "myapex.key",
1443 public_key: "testkey.avbpubkey",
1444 private_key: "testkey.pem",
1445 }
1446 android_app_certificate {
1447 name: "myapex.certificate.override",
1448 certificate: "testkey.override",
1449 }`)
1450 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1451 expected := "testkey.override.x509.pem testkey.override.pk8"
1452 if actual := rule.Args["certificates"]; actual != expected {
1453 t.Errorf("certificates should be %q, not %q", expected, actual)
1454 }
1455 })
1456 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1457 ctx, _ := testApex(t, `
1458 apex {
1459 name: "myapex",
1460 key: "myapex.key",
1461 certificate: "testkey",
1462 }
1463 apex_key {
1464 name: "myapex.key",
1465 public_key: "testkey.avbpubkey",
1466 private_key: "testkey.pem",
1467 }`)
1468 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1469 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1470 if actual := rule.Args["certificates"]; actual != expected {
1471 t.Errorf("certificates should be %q, not %q", expected, actual)
1472 }
1473 })
1474 t.Run("override when specified as <name>", func(t *testing.T) {
1475 ctx, _ := testApex(t, `
1476 apex {
1477 name: "myapex_keytest",
1478 key: "myapex.key",
1479 file_contexts: ":myapex-file_contexts",
1480 certificate: "testkey",
1481 }
1482 apex_key {
1483 name: "myapex.key",
1484 public_key: "testkey.avbpubkey",
1485 private_key: "testkey.pem",
1486 }
1487 android_app_certificate {
1488 name: "myapex.certificate.override",
1489 certificate: "testkey.override",
1490 }`)
1491 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1492 expected := "testkey.override.x509.pem testkey.override.pk8"
1493 if actual := rule.Args["certificates"]; actual != expected {
1494 t.Errorf("certificates should be %q, not %q", expected, actual)
1495 }
1496 })
1497}
1498
Jiyong Park58e364a2019-01-19 19:24:06 +09001499func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001500 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001501 apex {
1502 name: "myapex",
1503 key: "myapex.key",
1504 native_shared_libs: ["mylib"],
1505 }
1506
1507 apex {
1508 name: "otherapex",
1509 key: "myapex.key",
1510 native_shared_libs: ["mylib"],
1511 }
1512
1513 apex_key {
1514 name: "myapex.key",
1515 public_key: "testkey.avbpubkey",
1516 private_key: "testkey.pem",
1517 }
1518
1519 cc_library {
1520 name: "mylib",
1521 srcs: ["mylib.cpp"],
1522 system_shared_libs: [],
1523 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001524 // TODO: remove //apex_available:platform
1525 apex_available: [
1526 "//apex_available:platform",
1527 "myapex",
1528 "otherapex",
1529 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001530 }
1531 `)
1532
Jooyung Han6b8459b2019-10-30 08:29:25 +09001533 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001534 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001535 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001536 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1537 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001538
Jooyung Han6b8459b2019-10-30 08:29:25 +09001539 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001540 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001541 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001542 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1543 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001544
Jooyung Han6b8459b2019-10-30 08:29:25 +09001545 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001546 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001547 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001548 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1549 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001550}
Jiyong Park7e636d02019-01-28 16:16:54 +09001551
1552func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001553 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001554 apex {
1555 name: "myapex",
1556 key: "myapex.key",
1557 native_shared_libs: ["mylib"],
1558 }
1559
1560 apex_key {
1561 name: "myapex.key",
1562 public_key: "testkey.avbpubkey",
1563 private_key: "testkey.pem",
1564 }
1565
1566 cc_library_headers {
1567 name: "mylib_headers",
1568 export_include_dirs: ["my_include"],
1569 system_shared_libs: [],
1570 stl: "none",
1571 }
1572
1573 cc_library {
1574 name: "mylib",
1575 srcs: ["mylib.cpp"],
1576 system_shared_libs: [],
1577 stl: "none",
1578 header_libs: ["mylib_headers"],
1579 export_header_lib_headers: ["mylib_headers"],
1580 stubs: {
1581 versions: ["1", "2", "3"],
1582 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001583 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001584 }
1585
1586 cc_library {
1587 name: "otherlib",
1588 srcs: ["mylib.cpp"],
1589 system_shared_libs: [],
1590 stl: "none",
1591 shared_libs: ["mylib"],
1592 }
1593 `)
1594
Colin Cross7113d202019-11-20 16:39:12 -08001595 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001596
1597 // Ensure that the include path of the header lib is exported to 'otherlib'
1598 ensureContains(t, cFlags, "-Imy_include")
1599}
Alex Light9670d332019-01-29 18:07:33 -08001600
Jooyung Han31c470b2019-10-18 16:26:59 +09001601func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1602 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001603 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001604 copyCmds := apexRule.Args["copy_commands"]
1605 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001606 var failed bool
1607 var surplus []string
1608 filesMatched := make(map[string]bool)
1609 addContent := func(content string) {
1610 for _, expected := range files {
1611 if matched, _ := path.Match(expected, content); matched {
1612 filesMatched[expected] = true
1613 return
1614 }
1615 }
1616 surplus = append(surplus, content)
1617 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001618 for _, cmd := range strings.Split(copyCmds, "&&") {
1619 cmd = strings.TrimSpace(cmd)
1620 if cmd == "" {
1621 continue
1622 }
1623 terms := strings.Split(cmd, " ")
1624 switch terms[0] {
1625 case "mkdir":
1626 case "cp":
1627 if len(terms) != 3 {
1628 t.Fatal("copyCmds contains invalid cp command", cmd)
1629 }
1630 dst := terms[2]
1631 index := strings.Index(dst, imageApexDir)
1632 if index == -1 {
1633 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1634 }
1635 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001636 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001637 default:
1638 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1639 }
1640 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001641
Jooyung Han31c470b2019-10-18 16:26:59 +09001642 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001643 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001644 t.Log("surplus files", surplus)
1645 failed = true
1646 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001647
1648 if len(files) > len(filesMatched) {
1649 var missing []string
1650 for _, expected := range files {
1651 if !filesMatched[expected] {
1652 missing = append(missing, expected)
1653 }
1654 }
1655 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001656 t.Log("missing files", missing)
1657 failed = true
1658 }
1659 if failed {
1660 t.Fail()
1661 }
1662}
1663
Jooyung Han344d5432019-08-23 11:17:39 +09001664func TestVndkApexCurrent(t *testing.T) {
1665 ctx, _ := testApex(t, `
1666 apex_vndk {
1667 name: "myapex",
1668 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001669 }
1670
1671 apex_key {
1672 name: "myapex.key",
1673 public_key: "testkey.avbpubkey",
1674 private_key: "testkey.pem",
1675 }
1676
1677 cc_library {
1678 name: "libvndk",
1679 srcs: ["mylib.cpp"],
1680 vendor_available: true,
1681 vndk: {
1682 enabled: true,
1683 },
1684 system_shared_libs: [],
1685 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001686 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001687 }
1688
1689 cc_library {
1690 name: "libvndksp",
1691 srcs: ["mylib.cpp"],
1692 vendor_available: true,
1693 vndk: {
1694 enabled: true,
1695 support_system_process: true,
1696 },
1697 system_shared_libs: [],
1698 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001699 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001700 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001701 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001702
Jooyung Han31c470b2019-10-18 16:26:59 +09001703 ensureExactContents(t, ctx, "myapex", []string{
1704 "lib/libvndk.so",
1705 "lib/libvndksp.so",
1706 "lib64/libvndk.so",
1707 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001708 "etc/llndk.libraries.VER.txt",
1709 "etc/vndkcore.libraries.VER.txt",
1710 "etc/vndksp.libraries.VER.txt",
1711 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001712 })
Jooyung Han344d5432019-08-23 11:17:39 +09001713}
1714
1715func TestVndkApexWithPrebuilt(t *testing.T) {
1716 ctx, _ := testApex(t, `
1717 apex_vndk {
1718 name: "myapex",
1719 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001720 }
1721
1722 apex_key {
1723 name: "myapex.key",
1724 public_key: "testkey.avbpubkey",
1725 private_key: "testkey.pem",
1726 }
1727
1728 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001729 name: "libvndk",
1730 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001731 vendor_available: true,
1732 vndk: {
1733 enabled: true,
1734 },
1735 system_shared_libs: [],
1736 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001737 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001738 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001739
1740 cc_prebuilt_library_shared {
1741 name: "libvndk.arm",
1742 srcs: ["libvndk.arm.so"],
1743 vendor_available: true,
1744 vndk: {
1745 enabled: true,
1746 },
1747 enabled: false,
1748 arch: {
1749 arm: {
1750 enabled: true,
1751 },
1752 },
1753 system_shared_libs: [],
1754 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001755 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001756 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001757 `+vndkLibrariesTxtFiles("current"),
1758 withFiles(map[string][]byte{
1759 "libvndk.so": nil,
1760 "libvndk.arm.so": nil,
1761 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001762
Jooyung Han31c470b2019-10-18 16:26:59 +09001763 ensureExactContents(t, ctx, "myapex", []string{
1764 "lib/libvndk.so",
1765 "lib/libvndk.arm.so",
1766 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001767 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001768 })
Jooyung Han344d5432019-08-23 11:17:39 +09001769}
1770
Jooyung Han39edb6c2019-11-06 16:53:07 +09001771func vndkLibrariesTxtFiles(vers ...string) (result string) {
1772 for _, v := range vers {
1773 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001774 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001775 result += `
1776 vndk_libraries_txt {
1777 name: "` + txt + `.libraries.txt",
1778 }
1779 `
1780 }
1781 } else {
1782 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1783 result += `
1784 prebuilt_etc {
1785 name: "` + txt + `.libraries.` + v + `.txt",
1786 src: "dummy.txt",
1787 }
1788 `
1789 }
1790 }
1791 }
1792 return
1793}
1794
Jooyung Han344d5432019-08-23 11:17:39 +09001795func TestVndkApexVersion(t *testing.T) {
1796 ctx, _ := testApex(t, `
1797 apex_vndk {
1798 name: "myapex_v27",
1799 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001800 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001801 vndk_version: "27",
1802 }
1803
1804 apex_key {
1805 name: "myapex.key",
1806 public_key: "testkey.avbpubkey",
1807 private_key: "testkey.pem",
1808 }
1809
Jooyung Han31c470b2019-10-18 16:26:59 +09001810 vndk_prebuilt_shared {
1811 name: "libvndk27",
1812 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001813 vendor_available: true,
1814 vndk: {
1815 enabled: true,
1816 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001817 target_arch: "arm64",
1818 arch: {
1819 arm: {
1820 srcs: ["libvndk27_arm.so"],
1821 },
1822 arm64: {
1823 srcs: ["libvndk27_arm64.so"],
1824 },
1825 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001826 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001827 }
1828
1829 vndk_prebuilt_shared {
1830 name: "libvndk27",
1831 version: "27",
1832 vendor_available: true,
1833 vndk: {
1834 enabled: true,
1835 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001836 target_arch: "x86_64",
1837 arch: {
1838 x86: {
1839 srcs: ["libvndk27_x86.so"],
1840 },
1841 x86_64: {
1842 srcs: ["libvndk27_x86_64.so"],
1843 },
1844 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001845 }
1846 `+vndkLibrariesTxtFiles("27"),
1847 withFiles(map[string][]byte{
1848 "libvndk27_arm.so": nil,
1849 "libvndk27_arm64.so": nil,
1850 "libvndk27_x86.so": nil,
1851 "libvndk27_x86_64.so": nil,
1852 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001853
Jooyung Han31c470b2019-10-18 16:26:59 +09001854 ensureExactContents(t, ctx, "myapex_v27", []string{
1855 "lib/libvndk27_arm.so",
1856 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001857 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001858 })
Jooyung Han344d5432019-08-23 11:17:39 +09001859}
1860
1861func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1862 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1863 apex_vndk {
1864 name: "myapex_v27",
1865 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001866 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001867 vndk_version: "27",
1868 }
1869 apex_vndk {
1870 name: "myapex_v27_other",
1871 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001872 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001873 vndk_version: "27",
1874 }
1875
1876 apex_key {
1877 name: "myapex.key",
1878 public_key: "testkey.avbpubkey",
1879 private_key: "testkey.pem",
1880 }
1881
1882 cc_library {
1883 name: "libvndk",
1884 srcs: ["mylib.cpp"],
1885 vendor_available: true,
1886 vndk: {
1887 enabled: true,
1888 },
1889 system_shared_libs: [],
1890 stl: "none",
1891 }
1892
1893 vndk_prebuilt_shared {
1894 name: "libvndk",
1895 version: "27",
1896 vendor_available: true,
1897 vndk: {
1898 enabled: true,
1899 },
1900 srcs: ["libvndk.so"],
1901 }
1902 `, withFiles(map[string][]byte{
1903 "libvndk.so": nil,
1904 }))
1905}
1906
Jooyung Han90eee022019-10-01 20:02:42 +09001907func TestVndkApexNameRule(t *testing.T) {
1908 ctx, _ := testApex(t, `
1909 apex_vndk {
1910 name: "myapex",
1911 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001912 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001913 }
1914 apex_vndk {
1915 name: "myapex_v28",
1916 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001917 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001918 vndk_version: "28",
1919 }
1920 apex_key {
1921 name: "myapex.key",
1922 public_key: "testkey.avbpubkey",
1923 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001924 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001925
1926 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001927 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001928 actual := proptools.String(bundle.properties.Apex_name)
1929 if !reflect.DeepEqual(actual, expected) {
1930 t.Errorf("Got '%v', expected '%v'", actual, expected)
1931 }
1932 }
1933
1934 assertApexName("com.android.vndk.vVER", "myapex")
1935 assertApexName("com.android.vndk.v28", "myapex_v28")
1936}
1937
Jooyung Han344d5432019-08-23 11:17:39 +09001938func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1939 ctx, _ := testApex(t, `
1940 apex_vndk {
1941 name: "myapex",
1942 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001943 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001944 }
1945
1946 apex_key {
1947 name: "myapex.key",
1948 public_key: "testkey.avbpubkey",
1949 private_key: "testkey.pem",
1950 }
1951
1952 cc_library {
1953 name: "libvndk",
1954 srcs: ["mylib.cpp"],
1955 vendor_available: true,
1956 native_bridge_supported: true,
1957 host_supported: true,
1958 vndk: {
1959 enabled: true,
1960 },
1961 system_shared_libs: [],
1962 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001963 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001964 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001965 `+vndkLibrariesTxtFiles("current"),
1966 withTargets(map[android.OsType][]android.Target{
1967 android.Android: []android.Target{
1968 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1969 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1970 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1971 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1972 },
1973 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001974
Jooyung Han31c470b2019-10-18 16:26:59 +09001975 ensureExactContents(t, ctx, "myapex", []string{
1976 "lib/libvndk.so",
1977 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001978 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001979 })
Jooyung Han344d5432019-08-23 11:17:39 +09001980}
1981
1982func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1983 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1984 apex_vndk {
1985 name: "myapex",
1986 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001987 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001988 native_bridge_supported: true,
1989 }
1990
1991 apex_key {
1992 name: "myapex.key",
1993 public_key: "testkey.avbpubkey",
1994 private_key: "testkey.pem",
1995 }
1996
1997 cc_library {
1998 name: "libvndk",
1999 srcs: ["mylib.cpp"],
2000 vendor_available: true,
2001 native_bridge_supported: true,
2002 host_supported: true,
2003 vndk: {
2004 enabled: true,
2005 },
2006 system_shared_libs: [],
2007 stl: "none",
2008 }
2009 `)
2010}
2011
Jooyung Han31c470b2019-10-18 16:26:59 +09002012func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002013 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002014 apex_vndk {
2015 name: "myapex_v27",
2016 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002017 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002018 vndk_version: "27",
2019 }
2020
2021 apex_key {
2022 name: "myapex.key",
2023 public_key: "testkey.avbpubkey",
2024 private_key: "testkey.pem",
2025 }
2026
2027 vndk_prebuilt_shared {
2028 name: "libvndk27",
2029 version: "27",
2030 target_arch: "arm",
2031 vendor_available: true,
2032 vndk: {
2033 enabled: true,
2034 },
2035 arch: {
2036 arm: {
2037 srcs: ["libvndk27.so"],
2038 }
2039 },
2040 }
2041
2042 vndk_prebuilt_shared {
2043 name: "libvndk27",
2044 version: "27",
2045 target_arch: "arm",
2046 binder32bit: true,
2047 vendor_available: true,
2048 vndk: {
2049 enabled: true,
2050 },
2051 arch: {
2052 arm: {
2053 srcs: ["libvndk27binder32.so"],
2054 }
2055 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002056 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002057 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002058 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002059 withFiles(map[string][]byte{
2060 "libvndk27.so": nil,
2061 "libvndk27binder32.so": nil,
2062 }),
2063 withBinder32bit,
2064 withTargets(map[android.OsType][]android.Target{
2065 android.Android: []android.Target{
2066 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2067 },
2068 }),
2069 )
2070
2071 ensureExactContents(t, ctx, "myapex_v27", []string{
2072 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002073 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002074 })
2075}
2076
Jooyung Hane1633032019-08-01 17:41:43 +09002077func TestDependenciesInApexManifest(t *testing.T) {
2078 ctx, _ := testApex(t, `
2079 apex {
2080 name: "myapex_nodep",
2081 key: "myapex.key",
2082 native_shared_libs: ["lib_nodep"],
2083 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002084 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002085 }
2086
2087 apex {
2088 name: "myapex_dep",
2089 key: "myapex.key",
2090 native_shared_libs: ["lib_dep"],
2091 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002092 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002093 }
2094
2095 apex {
2096 name: "myapex_provider",
2097 key: "myapex.key",
2098 native_shared_libs: ["libfoo"],
2099 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002100 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002101 }
2102
2103 apex {
2104 name: "myapex_selfcontained",
2105 key: "myapex.key",
2106 native_shared_libs: ["lib_dep", "libfoo"],
2107 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002108 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002109 }
2110
2111 apex_key {
2112 name: "myapex.key",
2113 public_key: "testkey.avbpubkey",
2114 private_key: "testkey.pem",
2115 }
2116
2117 cc_library {
2118 name: "lib_nodep",
2119 srcs: ["mylib.cpp"],
2120 system_shared_libs: [],
2121 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002122 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002123 }
2124
2125 cc_library {
2126 name: "lib_dep",
2127 srcs: ["mylib.cpp"],
2128 shared_libs: ["libfoo"],
2129 system_shared_libs: [],
2130 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002131 apex_available: [
2132 "myapex_dep",
2133 "myapex_provider",
2134 "myapex_selfcontained",
2135 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002136 }
2137
2138 cc_library {
2139 name: "libfoo",
2140 srcs: ["mytest.cpp"],
2141 stubs: {
2142 versions: ["1"],
2143 },
2144 system_shared_libs: [],
2145 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002146 apex_available: [
2147 "myapex_provider",
2148 "myapex_selfcontained",
2149 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002150 }
2151 `)
2152
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002153 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002154 var provideNativeLibs, requireNativeLibs []string
2155
Sundong Ahnabb64432019-10-22 13:58:29 +09002156 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002157 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2158 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002159 ensureListEmpty(t, provideNativeLibs)
2160 ensureListEmpty(t, requireNativeLibs)
2161
Sundong Ahnabb64432019-10-22 13:58:29 +09002162 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002163 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2164 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002165 ensureListEmpty(t, provideNativeLibs)
2166 ensureListContains(t, requireNativeLibs, "libfoo.so")
2167
Sundong Ahnabb64432019-10-22 13:58:29 +09002168 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002169 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2170 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002171 ensureListContains(t, provideNativeLibs, "libfoo.so")
2172 ensureListEmpty(t, requireNativeLibs)
2173
Sundong Ahnabb64432019-10-22 13:58:29 +09002174 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002175 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2176 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002177 ensureListContains(t, provideNativeLibs, "libfoo.so")
2178 ensureListEmpty(t, requireNativeLibs)
2179}
2180
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002181func TestApexName(t *testing.T) {
2182 ctx, _ := testApex(t, `
2183 apex {
2184 name: "myapex",
2185 key: "myapex.key",
2186 apex_name: "com.android.myapex",
2187 }
2188
2189 apex_key {
2190 name: "myapex.key",
2191 public_key: "testkey.avbpubkey",
2192 private_key: "testkey.pem",
2193 }
2194 `)
2195
Sundong Ahnabb64432019-10-22 13:58:29 +09002196 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002197 apexManifestRule := module.Rule("apexManifestRule")
2198 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2199 apexRule := module.Rule("apexRule")
2200 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
2201}
2202
Alex Light0851b882019-02-07 13:20:53 -08002203func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002204 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002205 apex {
2206 name: "myapex",
2207 key: "myapex.key",
2208 native_shared_libs: ["mylib_common"],
2209 }
2210
2211 apex_key {
2212 name: "myapex.key",
2213 public_key: "testkey.avbpubkey",
2214 private_key: "testkey.pem",
2215 }
2216
2217 cc_library {
2218 name: "mylib_common",
2219 srcs: ["mylib.cpp"],
2220 system_shared_libs: [],
2221 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002222 apex_available: [
2223 "//apex_available:platform",
2224 "myapex",
2225 ],
Alex Light0851b882019-02-07 13:20:53 -08002226 }
2227 `)
2228
Sundong Ahnabb64432019-10-22 13:58:29 +09002229 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002230 apexRule := module.Rule("apexRule")
2231 copyCmds := apexRule.Args["copy_commands"]
2232
2233 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2234 t.Log("Apex was a test apex!")
2235 t.Fail()
2236 }
2237 // Ensure that main rule creates an output
2238 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2239
2240 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002241 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002242
2243 // Ensure that both direct and indirect deps are copied into apex
2244 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2245
Colin Cross7113d202019-11-20 16:39:12 -08002246 // Ensure that the platform variant ends with _shared
2247 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002248
2249 if !android.InAnyApex("mylib_common") {
2250 t.Log("Found mylib_common not in any apex!")
2251 t.Fail()
2252 }
2253}
2254
2255func TestTestApex(t *testing.T) {
2256 if android.InAnyApex("mylib_common_test") {
2257 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!")
2258 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002259 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002260 apex_test {
2261 name: "myapex",
2262 key: "myapex.key",
2263 native_shared_libs: ["mylib_common_test"],
2264 }
2265
2266 apex_key {
2267 name: "myapex.key",
2268 public_key: "testkey.avbpubkey",
2269 private_key: "testkey.pem",
2270 }
2271
2272 cc_library {
2273 name: "mylib_common_test",
2274 srcs: ["mylib.cpp"],
2275 system_shared_libs: [],
2276 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002277 // TODO: remove //apex_available:platform
2278 apex_available: [
2279 "//apex_available:platform",
2280 "myapex",
2281 ],
Alex Light0851b882019-02-07 13:20:53 -08002282 }
2283 `)
2284
Sundong Ahnabb64432019-10-22 13:58:29 +09002285 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002286 apexRule := module.Rule("apexRule")
2287 copyCmds := apexRule.Args["copy_commands"]
2288
2289 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2290 t.Log("Apex was not a test apex!")
2291 t.Fail()
2292 }
2293 // Ensure that main rule creates an output
2294 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2295
2296 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002297 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002298
2299 // Ensure that both direct and indirect deps are copied into apex
2300 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2301
Colin Cross7113d202019-11-20 16:39:12 -08002302 // Ensure that the platform variant ends with _shared
2303 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002304
2305 if android.InAnyApex("mylib_common_test") {
2306 t.Log("Found mylib_common_test in some apex!")
2307 t.Fail()
2308 }
2309}
2310
Alex Light9670d332019-01-29 18:07:33 -08002311func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002312 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002313 apex {
2314 name: "myapex",
2315 key: "myapex.key",
2316 multilib: {
2317 first: {
2318 native_shared_libs: ["mylib_common"],
2319 }
2320 },
2321 target: {
2322 android: {
2323 multilib: {
2324 first: {
2325 native_shared_libs: ["mylib"],
2326 }
2327 }
2328 },
2329 host: {
2330 multilib: {
2331 first: {
2332 native_shared_libs: ["mylib2"],
2333 }
2334 }
2335 }
2336 }
2337 }
2338
2339 apex_key {
2340 name: "myapex.key",
2341 public_key: "testkey.avbpubkey",
2342 private_key: "testkey.pem",
2343 }
2344
2345 cc_library {
2346 name: "mylib",
2347 srcs: ["mylib.cpp"],
2348 system_shared_libs: [],
2349 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002350 // TODO: remove //apex_available:platform
2351 apex_available: [
2352 "//apex_available:platform",
2353 "myapex",
2354 ],
Alex Light9670d332019-01-29 18:07:33 -08002355 }
2356
2357 cc_library {
2358 name: "mylib_common",
2359 srcs: ["mylib.cpp"],
2360 system_shared_libs: [],
2361 stl: "none",
2362 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002363 // TODO: remove //apex_available:platform
2364 apex_available: [
2365 "//apex_available:platform",
2366 "myapex",
2367 ],
Alex Light9670d332019-01-29 18:07:33 -08002368 }
2369
2370 cc_library {
2371 name: "mylib2",
2372 srcs: ["mylib.cpp"],
2373 system_shared_libs: [],
2374 stl: "none",
2375 compile_multilib: "first",
2376 }
2377 `)
2378
Sundong Ahnabb64432019-10-22 13:58:29 +09002379 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002380 copyCmds := apexRule.Args["copy_commands"]
2381
2382 // Ensure that main rule creates an output
2383 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2384
2385 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002386 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2387 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2388 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002389
2390 // Ensure that both direct and indirect deps are copied into apex
2391 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2392 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2393 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2394
Colin Cross7113d202019-11-20 16:39:12 -08002395 // Ensure that the platform variant ends with _shared
2396 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2397 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2398 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002399}
Jiyong Park04480cf2019-02-06 00:16:29 +09002400
2401func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002402 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002403 apex {
2404 name: "myapex",
2405 key: "myapex.key",
2406 binaries: ["myscript"],
2407 }
2408
2409 apex_key {
2410 name: "myapex.key",
2411 public_key: "testkey.avbpubkey",
2412 private_key: "testkey.pem",
2413 }
2414
2415 sh_binary {
2416 name: "myscript",
2417 src: "mylib.cpp",
2418 filename: "myscript.sh",
2419 sub_dir: "script",
2420 }
2421 `)
2422
Sundong Ahnabb64432019-10-22 13:58:29 +09002423 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002424 copyCmds := apexRule.Args["copy_commands"]
2425
2426 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2427}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002428
Jooyung Han91df2082019-11-20 01:49:42 +09002429func TestApexInVariousPartition(t *testing.T) {
2430 testcases := []struct {
2431 propName, parition, flattenedPartition string
2432 }{
2433 {"", "system", "system_ext"},
2434 {"product_specific: true", "product", "product"},
2435 {"soc_specific: true", "vendor", "vendor"},
2436 {"proprietary: true", "vendor", "vendor"},
2437 {"vendor: true", "vendor", "vendor"},
2438 {"system_ext_specific: true", "system_ext", "system_ext"},
2439 }
2440 for _, tc := range testcases {
2441 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2442 ctx, _ := testApex(t, `
2443 apex {
2444 name: "myapex",
2445 key: "myapex.key",
2446 `+tc.propName+`
2447 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002448
Jooyung Han91df2082019-11-20 01:49:42 +09002449 apex_key {
2450 name: "myapex.key",
2451 public_key: "testkey.avbpubkey",
2452 private_key: "testkey.pem",
2453 }
2454 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002455
Jooyung Han91df2082019-11-20 01:49:42 +09002456 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2457 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2458 actual := apex.installDir.String()
2459 if actual != expected {
2460 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2461 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002462
Jooyung Han91df2082019-11-20 01:49:42 +09002463 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2464 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2465 actual = flattened.installDir.String()
2466 if actual != expected {
2467 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2468 }
2469 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002470 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002471}
Jiyong Park67882562019-03-21 01:11:21 +09002472
Jooyung Han54aca7b2019-11-20 02:26:02 +09002473func TestFileContexts(t *testing.T) {
2474 ctx, _ := testApex(t, `
2475 apex {
2476 name: "myapex",
2477 key: "myapex.key",
2478 }
2479
2480 apex_key {
2481 name: "myapex.key",
2482 public_key: "testkey.avbpubkey",
2483 private_key: "testkey.pem",
2484 }
2485 `)
2486 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2487 apexRule := module.Rule("apexRule")
2488 actual := apexRule.Args["file_contexts"]
2489 expected := "system/sepolicy/apex/myapex-file_contexts"
2490 if actual != expected {
2491 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2492 }
2493
2494 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2495 apex {
2496 name: "myapex",
2497 key: "myapex.key",
2498 file_contexts: "my_own_file_contexts",
2499 }
2500
2501 apex_key {
2502 name: "myapex.key",
2503 public_key: "testkey.avbpubkey",
2504 private_key: "testkey.pem",
2505 }
2506 `, withFiles(map[string][]byte{
2507 "my_own_file_contexts": nil,
2508 }))
2509
2510 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2511 apex {
2512 name: "myapex",
2513 key: "myapex.key",
2514 product_specific: true,
2515 file_contexts: "product_specific_file_contexts",
2516 }
2517
2518 apex_key {
2519 name: "myapex.key",
2520 public_key: "testkey.avbpubkey",
2521 private_key: "testkey.pem",
2522 }
2523 `)
2524
2525 ctx, _ = testApex(t, `
2526 apex {
2527 name: "myapex",
2528 key: "myapex.key",
2529 product_specific: true,
2530 file_contexts: "product_specific_file_contexts",
2531 }
2532
2533 apex_key {
2534 name: "myapex.key",
2535 public_key: "testkey.avbpubkey",
2536 private_key: "testkey.pem",
2537 }
2538 `, withFiles(map[string][]byte{
2539 "product_specific_file_contexts": nil,
2540 }))
2541 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2542 apexRule = module.Rule("apexRule")
2543 actual = apexRule.Args["file_contexts"]
2544 expected = "product_specific_file_contexts"
2545 if actual != expected {
2546 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2547 }
2548
2549 ctx, _ = testApex(t, `
2550 apex {
2551 name: "myapex",
2552 key: "myapex.key",
2553 product_specific: true,
2554 file_contexts: ":my-file-contexts",
2555 }
2556
2557 apex_key {
2558 name: "myapex.key",
2559 public_key: "testkey.avbpubkey",
2560 private_key: "testkey.pem",
2561 }
2562
2563 filegroup {
2564 name: "my-file-contexts",
2565 srcs: ["product_specific_file_contexts"],
2566 }
2567 `, withFiles(map[string][]byte{
2568 "product_specific_file_contexts": nil,
2569 }))
2570 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2571 apexRule = module.Rule("apexRule")
2572 actual = apexRule.Args["file_contexts"]
2573 expected = "product_specific_file_contexts"
2574 if actual != expected {
2575 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2576 }
2577}
2578
Jiyong Park67882562019-03-21 01:11:21 +09002579func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002580 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002581 apex_key {
2582 name: "myapex.key",
2583 public_key: ":my.avbpubkey",
2584 private_key: ":my.pem",
2585 product_specific: true,
2586 }
2587
2588 filegroup {
2589 name: "my.avbpubkey",
2590 srcs: ["testkey2.avbpubkey"],
2591 }
2592
2593 filegroup {
2594 name: "my.pem",
2595 srcs: ["testkey2.pem"],
2596 }
2597 `)
2598
2599 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2600 expected_pubkey := "testkey2.avbpubkey"
2601 actual_pubkey := apex_key.public_key_file.String()
2602 if actual_pubkey != expected_pubkey {
2603 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2604 }
2605 expected_privkey := "testkey2.pem"
2606 actual_privkey := apex_key.private_key_file.String()
2607 if actual_privkey != expected_privkey {
2608 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2609 }
2610}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002611
2612func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002613 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002614 prebuilt_apex {
2615 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002616 arch: {
2617 arm64: {
2618 src: "myapex-arm64.apex",
2619 },
2620 arm: {
2621 src: "myapex-arm.apex",
2622 },
2623 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002624 }
2625 `)
2626
2627 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2628
Jiyong Parkc95714e2019-03-29 14:23:10 +09002629 expectedInput := "myapex-arm64.apex"
2630 if prebuilt.inputApex.String() != expectedInput {
2631 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2632 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002633}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002634
2635func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002636 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002637 prebuilt_apex {
2638 name: "myapex",
2639 src: "myapex-arm.apex",
2640 filename: "notmyapex.apex",
2641 }
2642 `)
2643
2644 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2645
2646 expected := "notmyapex.apex"
2647 if p.installFilename != expected {
2648 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2649 }
2650}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002651
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002652func TestPrebuiltOverrides(t *testing.T) {
2653 ctx, config := testApex(t, `
2654 prebuilt_apex {
2655 name: "myapex.prebuilt",
2656 src: "myapex-arm.apex",
2657 overrides: [
2658 "myapex",
2659 ],
2660 }
2661 `)
2662
2663 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2664
2665 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002666 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002667 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002668 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002669 }
2670}
2671
Roland Levillain630846d2019-06-26 12:48:34 +01002672func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002673 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002674 apex_test {
2675 name: "myapex",
2676 key: "myapex.key",
2677 tests: [
2678 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002679 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002680 ],
2681 }
2682
2683 apex_key {
2684 name: "myapex.key",
2685 public_key: "testkey.avbpubkey",
2686 private_key: "testkey.pem",
2687 }
2688
2689 cc_test {
2690 name: "mytest",
2691 gtest: false,
2692 srcs: ["mytest.cpp"],
2693 relative_install_path: "test",
2694 system_shared_libs: [],
2695 static_executable: true,
2696 stl: "none",
2697 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002698
2699 cc_test {
2700 name: "mytests",
2701 gtest: false,
2702 srcs: [
2703 "mytest1.cpp",
2704 "mytest2.cpp",
2705 "mytest3.cpp",
2706 ],
2707 test_per_src: true,
2708 relative_install_path: "test",
2709 system_shared_libs: [],
2710 static_executable: true,
2711 stl: "none",
2712 }
Roland Levillain630846d2019-06-26 12:48:34 +01002713 `)
2714
Sundong Ahnabb64432019-10-22 13:58:29 +09002715 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002716 copyCmds := apexRule.Args["copy_commands"]
2717
2718 // Ensure that test dep is copied into apex.
2719 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002720
2721 // Ensure that test deps built with `test_per_src` are copied into apex.
2722 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2723 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2724 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002725
2726 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002727 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002728 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2729 name := apexBundle.BaseModuleName()
2730 prefix := "TARGET_"
2731 var builder strings.Builder
2732 data.Custom(&builder, name, prefix, "", data)
2733 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002734 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2735 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2736 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2737 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002738 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002739 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002740 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002741}
2742
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002743func TestInstallExtraFlattenedApexes(t *testing.T) {
2744 ctx, config := testApex(t, `
2745 apex {
2746 name: "myapex",
2747 key: "myapex.key",
2748 }
2749 apex_key {
2750 name: "myapex.key",
2751 public_key: "testkey.avbpubkey",
2752 private_key: "testkey.pem",
2753 }
2754 `, func(fs map[string][]byte, config android.Config) {
2755 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2756 })
2757 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002758 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002759 mk := android.AndroidMkDataForTest(t, config, "", ab)
2760 var builder strings.Builder
2761 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2762 androidMk := builder.String()
2763 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2764}
2765
Jooyung Han5c998b92019-06-27 11:30:33 +09002766func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002767 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002768 apex {
2769 name: "myapex",
2770 key: "myapex.key",
2771 native_shared_libs: ["mylib"],
2772 uses: ["commonapex"],
2773 }
2774
2775 apex {
2776 name: "commonapex",
2777 key: "myapex.key",
2778 native_shared_libs: ["libcommon"],
2779 provide_cpp_shared_libs: true,
2780 }
2781
2782 apex_key {
2783 name: "myapex.key",
2784 public_key: "testkey.avbpubkey",
2785 private_key: "testkey.pem",
2786 }
2787
2788 cc_library {
2789 name: "mylib",
2790 srcs: ["mylib.cpp"],
2791 shared_libs: ["libcommon"],
2792 system_shared_libs: [],
2793 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002794 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002795 }
2796
2797 cc_library {
2798 name: "libcommon",
2799 srcs: ["mylib_common.cpp"],
2800 system_shared_libs: [],
2801 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002802 // TODO: remove //apex_available:platform
2803 apex_available: [
2804 "//apex_available:platform",
2805 "commonapex",
2806 "myapex",
2807 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002808 }
2809 `)
2810
Sundong Ahnabb64432019-10-22 13:58:29 +09002811 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002812 apexRule1 := module1.Rule("apexRule")
2813 copyCmds1 := apexRule1.Args["copy_commands"]
2814
Sundong Ahnabb64432019-10-22 13:58:29 +09002815 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002816 apexRule2 := module2.Rule("apexRule")
2817 copyCmds2 := apexRule2.Args["copy_commands"]
2818
Colin Cross7113d202019-11-20 16:39:12 -08002819 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2820 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002821 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2822 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2823 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2824}
2825
2826func TestApexUsesFailsIfNotProvided(t *testing.T) {
2827 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2828 apex {
2829 name: "myapex",
2830 key: "myapex.key",
2831 uses: ["commonapex"],
2832 }
2833
2834 apex {
2835 name: "commonapex",
2836 key: "myapex.key",
2837 }
2838
2839 apex_key {
2840 name: "myapex.key",
2841 public_key: "testkey.avbpubkey",
2842 private_key: "testkey.pem",
2843 }
2844 `)
2845 testApexError(t, `uses: "commonapex" is not a provider`, `
2846 apex {
2847 name: "myapex",
2848 key: "myapex.key",
2849 uses: ["commonapex"],
2850 }
2851
2852 cc_library {
2853 name: "commonapex",
2854 system_shared_libs: [],
2855 stl: "none",
2856 }
2857
2858 apex_key {
2859 name: "myapex.key",
2860 public_key: "testkey.avbpubkey",
2861 private_key: "testkey.pem",
2862 }
2863 `)
2864}
2865
2866func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2867 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2868 apex {
2869 name: "myapex",
2870 key: "myapex.key",
2871 use_vendor: true,
2872 uses: ["commonapex"],
2873 }
2874
2875 apex {
2876 name: "commonapex",
2877 key: "myapex.key",
2878 provide_cpp_shared_libs: true,
2879 }
2880
2881 apex_key {
2882 name: "myapex.key",
2883 public_key: "testkey.avbpubkey",
2884 private_key: "testkey.pem",
2885 }
Jooyung Handc782442019-11-01 03:14:38 +09002886 `, func(fs map[string][]byte, config android.Config) {
2887 setUseVendorWhitelistForTest(config, []string{"myapex"})
2888 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002889}
2890
Jooyung Hand48f3c32019-08-23 11:18:57 +09002891func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2892 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2893 apex {
2894 name: "myapex",
2895 key: "myapex.key",
2896 native_shared_libs: ["libfoo"],
2897 }
2898
2899 apex_key {
2900 name: "myapex.key",
2901 public_key: "testkey.avbpubkey",
2902 private_key: "testkey.pem",
2903 }
2904
2905 cc_library {
2906 name: "libfoo",
2907 stl: "none",
2908 system_shared_libs: [],
2909 enabled: false,
2910 }
2911 `)
2912 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2913 apex {
2914 name: "myapex",
2915 key: "myapex.key",
2916 java_libs: ["myjar"],
2917 }
2918
2919 apex_key {
2920 name: "myapex.key",
2921 public_key: "testkey.avbpubkey",
2922 private_key: "testkey.pem",
2923 }
2924
2925 java_library {
2926 name: "myjar",
2927 srcs: ["foo/bar/MyClass.java"],
2928 sdk_version: "none",
2929 system_modules: "none",
2930 compile_dex: true,
2931 enabled: false,
2932 }
2933 `)
2934}
2935
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002936func TestApexWithApps(t *testing.T) {
2937 ctx, _ := testApex(t, `
2938 apex {
2939 name: "myapex",
2940 key: "myapex.key",
2941 apps: [
2942 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002943 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002944 ],
2945 }
2946
2947 apex_key {
2948 name: "myapex.key",
2949 public_key: "testkey.avbpubkey",
2950 private_key: "testkey.pem",
2951 }
2952
2953 android_app {
2954 name: "AppFoo",
2955 srcs: ["foo/bar/MyClass.java"],
2956 sdk_version: "none",
2957 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002958 jni_libs: ["libjni"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002959 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002960 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002961
2962 android_app {
2963 name: "AppFooPriv",
2964 srcs: ["foo/bar/MyClass.java"],
2965 sdk_version: "none",
2966 system_modules: "none",
2967 privileged: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002968 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09002969 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002970
2971 cc_library_shared {
2972 name: "libjni",
2973 srcs: ["mylib.cpp"],
2974 stl: "none",
2975 system_shared_libs: [],
2976 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002977 `)
2978
Sundong Ahnabb64432019-10-22 13:58:29 +09002979 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002980 apexRule := module.Rule("apexRule")
2981 copyCmds := apexRule.Args["copy_commands"]
2982
2983 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002984 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002985
2986 // JNI libraries are embedded inside APK
2987 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
Colin Cross7113d202019-11-20 16:39:12 -08002988 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002989 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2990 // ... uncompressed
2991 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2992 t.Errorf("jni lib is not uncompressed for AppFoo")
2993 }
2994 // ... and not directly inside the APEX
2995 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002996}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002997
Dario Frenicde2a032019-10-27 00:29:22 +01002998func TestApexWithAppImports(t *testing.T) {
2999 ctx, _ := testApex(t, `
3000 apex {
3001 name: "myapex",
3002 key: "myapex.key",
3003 apps: [
3004 "AppFooPrebuilt",
3005 "AppFooPrivPrebuilt",
3006 ],
3007 }
3008
3009 apex_key {
3010 name: "myapex.key",
3011 public_key: "testkey.avbpubkey",
3012 private_key: "testkey.pem",
3013 }
3014
3015 android_app_import {
3016 name: "AppFooPrebuilt",
3017 apk: "PrebuiltAppFoo.apk",
3018 presigned: true,
3019 dex_preopt: {
3020 enabled: false,
3021 },
3022 }
3023
3024 android_app_import {
3025 name: "AppFooPrivPrebuilt",
3026 apk: "PrebuiltAppFooPriv.apk",
3027 privileged: true,
3028 presigned: true,
3029 dex_preopt: {
3030 enabled: false,
3031 },
3032 }
3033 `)
3034
Sundong Ahnabb64432019-10-22 13:58:29 +09003035 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003036 apexRule := module.Rule("apexRule")
3037 copyCmds := apexRule.Args["copy_commands"]
3038
3039 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3040 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003041}
3042
Dario Freni6f3937c2019-12-20 22:58:03 +00003043func TestApexWithTestHelperApp(t *testing.T) {
3044 ctx, _ := testApex(t, `
3045 apex {
3046 name: "myapex",
3047 key: "myapex.key",
3048 apps: [
3049 "TesterHelpAppFoo",
3050 ],
3051 }
3052
3053 apex_key {
3054 name: "myapex.key",
3055 public_key: "testkey.avbpubkey",
3056 private_key: "testkey.pem",
3057 }
3058
3059 android_test_helper_app {
3060 name: "TesterHelpAppFoo",
3061 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003062 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003063 }
3064
3065 `)
3066
3067 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3068 apexRule := module.Rule("apexRule")
3069 copyCmds := apexRule.Args["copy_commands"]
3070
3071 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3072}
3073
Jooyung Han18020ea2019-11-13 10:50:48 +09003074func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3075 // libfoo's apex_available comes from cc_defaults
3076 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
3077 apex {
3078 name: "myapex",
3079 key: "myapex.key",
3080 native_shared_libs: ["libfoo"],
3081 }
3082
3083 apex_key {
3084 name: "myapex.key",
3085 public_key: "testkey.avbpubkey",
3086 private_key: "testkey.pem",
3087 }
3088
3089 apex {
3090 name: "otherapex",
3091 key: "myapex.key",
3092 native_shared_libs: ["libfoo"],
3093 }
3094
3095 cc_defaults {
3096 name: "libfoo-defaults",
3097 apex_available: ["otherapex"],
3098 }
3099
3100 cc_library {
3101 name: "libfoo",
3102 defaults: ["libfoo-defaults"],
3103 stl: "none",
3104 system_shared_libs: [],
3105 }`)
3106}
3107
Jiyong Park127b40b2019-09-30 16:04:35 +09003108func TestApexAvailable(t *testing.T) {
3109 // libfoo is not available to myapex, but only to otherapex
3110 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3111 apex {
3112 name: "myapex",
3113 key: "myapex.key",
3114 native_shared_libs: ["libfoo"],
3115 }
3116
3117 apex_key {
3118 name: "myapex.key",
3119 public_key: "testkey.avbpubkey",
3120 private_key: "testkey.pem",
3121 }
3122
3123 apex {
3124 name: "otherapex",
3125 key: "otherapex.key",
3126 native_shared_libs: ["libfoo"],
3127 }
3128
3129 apex_key {
3130 name: "otherapex.key",
3131 public_key: "testkey.avbpubkey",
3132 private_key: "testkey.pem",
3133 }
3134
3135 cc_library {
3136 name: "libfoo",
3137 stl: "none",
3138 system_shared_libs: [],
3139 apex_available: ["otherapex"],
3140 }`)
3141
3142 // libbar is an indirect dep
3143 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3144 apex {
3145 name: "myapex",
3146 key: "myapex.key",
3147 native_shared_libs: ["libfoo"],
3148 }
3149
3150 apex_key {
3151 name: "myapex.key",
3152 public_key: "testkey.avbpubkey",
3153 private_key: "testkey.pem",
3154 }
3155
3156 apex {
3157 name: "otherapex",
3158 key: "otherapex.key",
3159 native_shared_libs: ["libfoo"],
3160 }
3161
3162 apex_key {
3163 name: "otherapex.key",
3164 public_key: "testkey.avbpubkey",
3165 private_key: "testkey.pem",
3166 }
3167
3168 cc_library {
3169 name: "libfoo",
3170 stl: "none",
3171 shared_libs: ["libbar"],
3172 system_shared_libs: [],
3173 apex_available: ["myapex", "otherapex"],
3174 }
3175
3176 cc_library {
3177 name: "libbar",
3178 stl: "none",
3179 system_shared_libs: [],
3180 apex_available: ["otherapex"],
3181 }`)
3182
3183 testApexError(t, "\"otherapex\" is not a valid module name", `
3184 apex {
3185 name: "myapex",
3186 key: "myapex.key",
3187 native_shared_libs: ["libfoo"],
3188 }
3189
3190 apex_key {
3191 name: "myapex.key",
3192 public_key: "testkey.avbpubkey",
3193 private_key: "testkey.pem",
3194 }
3195
3196 cc_library {
3197 name: "libfoo",
3198 stl: "none",
3199 system_shared_libs: [],
3200 apex_available: ["otherapex"],
3201 }`)
3202
3203 ctx, _ := testApex(t, `
3204 apex {
3205 name: "myapex",
3206 key: "myapex.key",
3207 native_shared_libs: ["libfoo", "libbar"],
3208 }
3209
3210 apex_key {
3211 name: "myapex.key",
3212 public_key: "testkey.avbpubkey",
3213 private_key: "testkey.pem",
3214 }
3215
3216 cc_library {
3217 name: "libfoo",
3218 stl: "none",
3219 system_shared_libs: [],
3220 apex_available: ["myapex"],
3221 }
3222
3223 cc_library {
3224 name: "libbar",
3225 stl: "none",
3226 system_shared_libs: [],
3227 apex_available: ["//apex_available:anyapex"],
3228 }`)
3229
3230 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003231 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3232 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3233 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3234 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003235
3236 ctx, _ = testApex(t, `
3237 apex {
3238 name: "myapex",
3239 key: "myapex.key",
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: ["//apex_available:platform"],
3253 }`)
3254
3255 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003256 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3257 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003258
3259 ctx, _ = testApex(t, `
3260 apex {
3261 name: "myapex",
3262 key: "myapex.key",
3263 native_shared_libs: ["libfoo"],
3264 }
3265
3266 apex_key {
3267 name: "myapex.key",
3268 public_key: "testkey.avbpubkey",
3269 private_key: "testkey.pem",
3270 }
3271
3272 cc_library {
3273 name: "libfoo",
3274 stl: "none",
3275 system_shared_libs: [],
3276 apex_available: ["myapex"],
3277 static: {
3278 apex_available: ["//apex_available:platform"],
3279 },
3280 }`)
3281
3282 // shared variant of libfoo is only available to myapex
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")
Jiyong Parka90ca002019-10-07 15:47:24 +09003285 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08003286 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3287 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003288}
3289
Jiyong Park5d790c32019-11-15 18:40:32 +09003290func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003291 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003292 apex {
3293 name: "myapex",
3294 key: "myapex.key",
3295 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003296 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003297 }
3298
3299 override_apex {
3300 name: "override_myapex",
3301 base: "myapex",
3302 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003303 overrides: ["unknownapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003304 }
3305
3306 apex_key {
3307 name: "myapex.key",
3308 public_key: "testkey.avbpubkey",
3309 private_key: "testkey.pem",
3310 }
3311
3312 android_app {
3313 name: "app",
3314 srcs: ["foo/bar/MyClass.java"],
3315 package_name: "foo",
3316 sdk_version: "none",
3317 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003318 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003319 }
3320
3321 override_android_app {
3322 name: "override_app",
3323 base: "app",
3324 package_name: "bar",
3325 }
3326 `)
3327
Jiyong Park317645e2019-12-05 13:20:58 +09003328 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3329 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3330 if originalVariant.GetOverriddenBy() != "" {
3331 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3332 }
3333 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3334 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3335 }
3336
Jiyong Park5d790c32019-11-15 18:40:32 +09003337 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3338 apexRule := module.Rule("apexRule")
3339 copyCmds := apexRule.Args["copy_commands"]
3340
3341 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3342 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003343
3344 apexBundle := module.Module().(*apexBundle)
3345 name := apexBundle.Name()
3346 if name != "override_myapex" {
3347 t.Errorf("name should be \"override_myapex\", but was %q", name)
3348 }
3349
3350 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3351 var builder strings.Builder
3352 data.Custom(&builder, name, "TARGET_", "", data)
3353 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003354 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003355 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3356 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003357 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003358 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003359 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003360 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3361 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003362}
3363
Jooyung Han214bf372019-11-12 13:03:50 +09003364func TestLegacyAndroid10Support(t *testing.T) {
3365 ctx, _ := testApex(t, `
3366 apex {
3367 name: "myapex",
3368 key: "myapex.key",
3369 legacy_android10_support: true,
3370 }
3371
3372 apex_key {
3373 name: "myapex.key",
3374 public_key: "testkey.avbpubkey",
3375 private_key: "testkey.pem",
3376 }
3377 `)
3378
3379 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3380 args := module.Rule("apexRule").Args
3381 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
3382}
3383
Jooyung Han58f26ab2019-12-18 15:34:32 +09003384func TestJavaSDKLibrary(t *testing.T) {
3385 ctx, _ := testApex(t, `
3386 apex {
3387 name: "myapex",
3388 key: "myapex.key",
3389 java_libs: ["foo"],
3390 }
3391
3392 apex_key {
3393 name: "myapex.key",
3394 public_key: "testkey.avbpubkey",
3395 private_key: "testkey.pem",
3396 }
3397
3398 java_sdk_library {
3399 name: "foo",
3400 srcs: ["a.java"],
3401 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003402 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003403 }
3404 `, withFiles(map[string][]byte{
3405 "api/current.txt": nil,
3406 "api/removed.txt": nil,
3407 "api/system-current.txt": nil,
3408 "api/system-removed.txt": nil,
3409 "api/test-current.txt": nil,
3410 "api/test-removed.txt": nil,
3411 }))
3412
3413 // java_sdk_library installs both impl jar and permission XML
3414 ensureExactContents(t, ctx, "myapex", []string{
3415 "javalib/foo.jar",
3416 "etc/permissions/foo.xml",
3417 })
3418 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jooyung Han624058e2019-12-24 18:38:06 +09003419 xml := ctx.ModuleForTests("foo", "android_common_myapex").Output("foo.xml")
3420 ensureContains(t, xml.Args["content"], `<library name="foo" file="/apex/myapex/javalib/foo.jar"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003421}
3422
Jiyong Park479321d2019-12-16 11:47:12 +09003423func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3424 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3425 apex {
3426 name: "myapex",
3427 key: "myapex.key",
3428 java_libs: ["myjar"],
3429 }
3430
3431 apex_key {
3432 name: "myapex.key",
3433 public_key: "testkey.avbpubkey",
3434 private_key: "testkey.pem",
3435 }
3436
3437 java_library {
3438 name: "myjar",
3439 srcs: ["foo/bar/MyClass.java"],
3440 sdk_version: "none",
3441 system_modules: "none",
3442 }
3443 `)
3444}
3445
Jiyong Park7afd1072019-12-30 16:56:33 +09003446func TestCarryRequiredModuleNames(t *testing.T) {
3447 ctx, config := testApex(t, `
3448 apex {
3449 name: "myapex",
3450 key: "myapex.key",
3451 native_shared_libs: ["mylib"],
3452 }
3453
3454 apex_key {
3455 name: "myapex.key",
3456 public_key: "testkey.avbpubkey",
3457 private_key: "testkey.pem",
3458 }
3459
3460 cc_library {
3461 name: "mylib",
3462 srcs: ["mylib.cpp"],
3463 system_shared_libs: [],
3464 stl: "none",
3465 required: ["a", "b"],
3466 host_required: ["c", "d"],
3467 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003468 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003469 }
3470 `)
3471
3472 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3473 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3474 name := apexBundle.BaseModuleName()
3475 prefix := "TARGET_"
3476 var builder strings.Builder
3477 data.Custom(&builder, name, prefix, "", data)
3478 androidMk := builder.String()
3479 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3480 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3481 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3482}
3483
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003484func TestMain(m *testing.M) {
3485 run := func() int {
3486 setUp()
3487 defer tearDown()
3488
3489 return m.Run()
3490 }
3491
3492 os.Exit(run())
3493}