blob: b37674ff18e699dad6f5628f08f2fbd133cf041e [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",
406 }
407
Alex Light3d673592019-01-18 14:37:31 -0800408 cc_binary {
409 name: "foo",
410 srcs: ["mylib.cpp"],
411 compile_multilib: "both",
412 multilib: {
413 lib32: {
414 suffix: "32",
415 },
416 lib64: {
417 suffix: "64",
418 },
419 },
420 symlinks: ["foo_link_"],
421 symlink_preferred_arch: true,
422 system_shared_libs: [],
423 static_executable: true,
424 stl: "none",
425 }
426
Jiyong Park25fc6a92018-11-18 18:02:45 +0900427 cc_library {
428 name: "mylib2",
429 srcs: ["mylib.cpp"],
430 system_shared_libs: [],
431 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900432 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900433 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900434
435 java_library {
436 name: "myjar",
437 srcs: ["foo/bar/MyClass.java"],
438 sdk_version: "none",
439 system_modules: "none",
440 compile_dex: true,
441 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900442 libs: ["mysharedjar"],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900443 }
444
445 java_library {
446 name: "myotherjar",
447 srcs: ["foo/bar/MyClass.java"],
448 sdk_version: "none",
449 system_modules: "none",
450 compile_dex: true,
451 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900452
453 java_library {
454 name: "mysharedjar",
455 srcs: ["foo/bar/MyClass.java"],
456 sdk_version: "none",
457 system_modules: "none",
458 compile_dex: true,
459 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900460 `)
461
Sundong Ahnabb64432019-10-22 13:58:29 +0900462 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900463
464 optFlags := apexRule.Args["opt_flags"]
465 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700466 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900467 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900468
Jiyong Park25fc6a92018-11-18 18:02:45 +0900469 copyCmds := apexRule.Args["copy_commands"]
470
471 // Ensure that main rule creates an output
472 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
473
474 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800475 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900476 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900477
478 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800479 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900480 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900481
482 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800483 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
484 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900485 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
486 // .. but not for java libs
487 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900488 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800489
Colin Cross7113d202019-11-20 16:39:12 -0800490 // Ensure that the platform variant ends with _shared or _common
491 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
492 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900493 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
494 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900495 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
496
497 // Ensure that dynamic dependency to java libs are not included
498 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800499
500 // Ensure that all symlinks are present.
501 found_foo_link_64 := false
502 found_foo := false
503 for _, cmd := range strings.Split(copyCmds, " && ") {
504 if strings.HasPrefix(cmd, "ln -s foo64") {
505 if strings.HasSuffix(cmd, "bin/foo") {
506 found_foo = true
507 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
508 found_foo_link_64 = true
509 }
510 }
511 }
512 good := found_foo && found_foo_link_64
513 if !good {
514 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
515 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900516
Sundong Ahnabb64432019-10-22 13:58:29 +0900517 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700518 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700519 if len(noticeInputs) != 2 {
520 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900521 }
522 ensureListContains(t, noticeInputs, "NOTICE")
523 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900524
525 depsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("myapex-deps-info.txt").Args["content"], "\\n")
526 ensureListContains(t, depsInfo, "internal myjar")
527 ensureListContains(t, depsInfo, "internal mylib")
528 ensureListContains(t, depsInfo, "internal mylib2")
529 ensureListContains(t, depsInfo, "internal myotherjar")
Alex Light5098a612018-11-29 17:12:15 -0800530}
531
Jooyung Hanf21c7972019-12-16 22:32:06 +0900532func TestDefaults(t *testing.T) {
533 ctx, _ := testApex(t, `
534 apex_defaults {
535 name: "myapex-defaults",
536 key: "myapex.key",
537 prebuilts: ["myetc"],
538 native_shared_libs: ["mylib"],
539 java_libs: ["myjar"],
540 apps: ["AppFoo"],
541 }
542
543 prebuilt_etc {
544 name: "myetc",
545 src: "myprebuilt",
546 }
547
548 apex {
549 name: "myapex",
550 defaults: ["myapex-defaults"],
551 }
552
553 apex_key {
554 name: "myapex.key",
555 public_key: "testkey.avbpubkey",
556 private_key: "testkey.pem",
557 }
558
559 cc_library {
560 name: "mylib",
561 system_shared_libs: [],
562 stl: "none",
563 }
564
565 java_library {
566 name: "myjar",
567 srcs: ["foo/bar/MyClass.java"],
568 sdk_version: "none",
569 system_modules: "none",
570 compile_dex: true,
571 }
572
573 android_app {
574 name: "AppFoo",
575 srcs: ["foo/bar/MyClass.java"],
576 sdk_version: "none",
577 system_modules: "none",
578 }
579 `)
580 ensureExactContents(t, ctx, "myapex", []string{
581 "etc/myetc",
582 "javalib/myjar.jar",
583 "lib64/mylib.so",
584 "app/AppFoo/AppFoo.apk",
585 })
586}
587
Jooyung Han01a3ee22019-11-02 02:52:25 +0900588func TestApexManifest(t *testing.T) {
589 ctx, _ := testApex(t, `
590 apex {
591 name: "myapex",
592 key: "myapex.key",
593 }
594
595 apex_key {
596 name: "myapex.key",
597 public_key: "testkey.avbpubkey",
598 private_key: "testkey.pem",
599 }
600 `)
601
602 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900603 args := module.Rule("apexRule").Args
604 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
605 t.Error("manifest should be apex_manifest.pb, but " + manifest)
606 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900607}
608
Alex Light5098a612018-11-29 17:12:15 -0800609func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700610 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800611 apex {
612 name: "myapex",
613 key: "myapex.key",
614 payload_type: "zip",
615 native_shared_libs: ["mylib"],
616 }
617
618 apex_key {
619 name: "myapex.key",
620 public_key: "testkey.avbpubkey",
621 private_key: "testkey.pem",
622 }
623
624 cc_library {
625 name: "mylib",
626 srcs: ["mylib.cpp"],
627 shared_libs: ["mylib2"],
628 system_shared_libs: [],
629 stl: "none",
630 }
631
632 cc_library {
633 name: "mylib2",
634 srcs: ["mylib.cpp"],
635 system_shared_libs: [],
636 stl: "none",
637 }
638 `)
639
Sundong Ahnabb64432019-10-22 13:58:29 +0900640 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800641 copyCmds := zipApexRule.Args["copy_commands"]
642
643 // Ensure that main rule creates an output
644 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
645
646 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800647 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800648
649 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800650 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800651
652 // Ensure that both direct and indirect deps are copied into apex
653 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
654 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900655}
656
657func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700658 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900659 apex {
660 name: "myapex",
661 key: "myapex.key",
662 native_shared_libs: ["mylib", "mylib3"],
663 }
664
665 apex_key {
666 name: "myapex.key",
667 public_key: "testkey.avbpubkey",
668 private_key: "testkey.pem",
669 }
670
671 cc_library {
672 name: "mylib",
673 srcs: ["mylib.cpp"],
674 shared_libs: ["mylib2", "mylib3"],
675 system_shared_libs: [],
676 stl: "none",
677 }
678
679 cc_library {
680 name: "mylib2",
681 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900682 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900683 system_shared_libs: [],
684 stl: "none",
685 stubs: {
686 versions: ["1", "2", "3"],
687 },
688 }
689
690 cc_library {
691 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900692 srcs: ["mylib.cpp"],
693 shared_libs: ["mylib4"],
694 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900695 stl: "none",
696 stubs: {
697 versions: ["10", "11", "12"],
698 },
699 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900700
701 cc_library {
702 name: "mylib4",
703 srcs: ["mylib.cpp"],
704 system_shared_libs: [],
705 stl: "none",
706 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900707 `)
708
Sundong Ahnabb64432019-10-22 13:58:29 +0900709 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900710 copyCmds := apexRule.Args["copy_commands"]
711
712 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800713 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900714
715 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800716 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900717
718 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800719 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900720
Colin Cross7113d202019-11-20 16:39:12 -0800721 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900722
723 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900724 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900725 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900726 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900727
728 // 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 -0800729 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900730 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800731 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900732
733 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800734 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900735 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900736
737 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900738 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900739
740 ensureExactContents(t, ctx, "myapex", []string{
741 "lib64/mylib.so",
742 "lib64/mylib3.so",
743 "lib64/mylib4.so",
744 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900745}
746
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900747func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700748 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900749 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900750 name: "myapex2",
751 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900752 native_shared_libs: ["mylib"],
753 }
754
755 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900756 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900757 public_key: "testkey.avbpubkey",
758 private_key: "testkey.pem",
759 }
760
761 cc_library {
762 name: "mylib",
763 srcs: ["mylib.cpp"],
764 shared_libs: ["libfoo#10"],
765 system_shared_libs: [],
766 stl: "none",
767 }
768
769 cc_library {
770 name: "libfoo",
771 srcs: ["mylib.cpp"],
772 shared_libs: ["libbar"],
773 system_shared_libs: [],
774 stl: "none",
775 stubs: {
776 versions: ["10", "20", "30"],
777 },
778 }
779
780 cc_library {
781 name: "libbar",
782 srcs: ["mylib.cpp"],
783 system_shared_libs: [],
784 stl: "none",
785 }
786
787 `)
788
Jiyong Park83dc74b2020-01-14 18:38:44 +0900789 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900790 copyCmds := apexRule.Args["copy_commands"]
791
792 // Ensure that direct non-stubs dep is always included
793 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
794
795 // Ensure that indirect stubs dep is not included
796 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
797
798 // Ensure that dependency of stubs is not included
799 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
800
Jiyong Park83dc74b2020-01-14 18:38:44 +0900801 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900802
803 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900804 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900805 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900806 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900807
Jiyong Park3ff16992019-12-27 14:11:47 +0900808 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900809
810 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
811 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900812
813 depsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("myapex2-deps-info.txt").Args["content"], "\\n")
814 ensureListContains(t, depsInfo, "internal mylib")
815 ensureListContains(t, depsInfo, "external libfoo")
816 ensureListNotContains(t, depsInfo, "internal libfoo")
817 ensureListNotContains(t, depsInfo, "external mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900818}
819
Jooyung Hand3639552019-08-09 12:57:43 +0900820func TestApexWithRuntimeLibsDependency(t *testing.T) {
821 /*
822 myapex
823 |
824 v (runtime_libs)
825 mylib ------+------> libfoo [provides stub]
826 |
827 `------> libbar
828 */
829 ctx, _ := testApex(t, `
830 apex {
831 name: "myapex",
832 key: "myapex.key",
833 native_shared_libs: ["mylib"],
834 }
835
836 apex_key {
837 name: "myapex.key",
838 public_key: "testkey.avbpubkey",
839 private_key: "testkey.pem",
840 }
841
842 cc_library {
843 name: "mylib",
844 srcs: ["mylib.cpp"],
845 runtime_libs: ["libfoo", "libbar"],
846 system_shared_libs: [],
847 stl: "none",
848 }
849
850 cc_library {
851 name: "libfoo",
852 srcs: ["mylib.cpp"],
853 system_shared_libs: [],
854 stl: "none",
855 stubs: {
856 versions: ["10", "20", "30"],
857 },
858 }
859
860 cc_library {
861 name: "libbar",
862 srcs: ["mylib.cpp"],
863 system_shared_libs: [],
864 stl: "none",
865 }
866
867 `)
868
Sundong Ahnabb64432019-10-22 13:58:29 +0900869 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900870 copyCmds := apexRule.Args["copy_commands"]
871
872 // Ensure that direct non-stubs dep is always included
873 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
874
875 // Ensure that indirect stubs dep is not included
876 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
877
878 // Ensure that runtime_libs dep in included
879 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
880
Sundong Ahnabb64432019-10-22 13:58:29 +0900881 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900882 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
883 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900884
885}
886
Jooyung Han9c80bae2019-08-20 17:30:57 +0900887func TestApexDependencyToLLNDK(t *testing.T) {
888 ctx, _ := testApex(t, `
889 apex {
890 name: "myapex",
891 key: "myapex.key",
892 use_vendor: true,
893 native_shared_libs: ["mylib"],
894 }
895
896 apex_key {
897 name: "myapex.key",
898 public_key: "testkey.avbpubkey",
899 private_key: "testkey.pem",
900 }
901
902 cc_library {
903 name: "mylib",
904 srcs: ["mylib.cpp"],
905 vendor_available: true,
906 shared_libs: ["libbar"],
907 system_shared_libs: [],
908 stl: "none",
909 }
910
911 cc_library {
912 name: "libbar",
913 srcs: ["mylib.cpp"],
914 system_shared_libs: [],
915 stl: "none",
916 }
917
918 llndk_library {
919 name: "libbar",
920 symbol_file: "",
921 }
Jooyung Handc782442019-11-01 03:14:38 +0900922 `, func(fs map[string][]byte, config android.Config) {
923 setUseVendorWhitelistForTest(config, []string{"myapex"})
924 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900925
Sundong Ahnabb64432019-10-22 13:58:29 +0900926 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900927 copyCmds := apexRule.Args["copy_commands"]
928
929 // Ensure that LLNDK dep is not included
930 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
931
Sundong Ahnabb64432019-10-22 13:58:29 +0900932 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900933 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900934
935 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900936 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900937
938}
939
Jiyong Park25fc6a92018-11-18 18:02:45 +0900940func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700941 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900942 apex {
943 name: "myapex",
944 key: "myapex.key",
945 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
946 }
947
948 apex_key {
949 name: "myapex.key",
950 public_key: "testkey.avbpubkey",
951 private_key: "testkey.pem",
952 }
953
954 cc_library {
955 name: "mylib",
956 srcs: ["mylib.cpp"],
957 shared_libs: ["libdl#27"],
958 stl: "none",
959 }
960
961 cc_library_shared {
962 name: "mylib_shared",
963 srcs: ["mylib.cpp"],
964 shared_libs: ["libdl#27"],
965 stl: "none",
966 }
967
968 cc_library {
969 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700970 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900971 nocrt: true,
972 system_shared_libs: [],
973 stl: "none",
974 stubs: {
975 versions: ["27", "28", "29"],
976 },
977 }
978
979 cc_library {
980 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700981 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900982 nocrt: true,
983 system_shared_libs: [],
984 stl: "none",
985 stubs: {
986 versions: ["27", "28", "29"],
987 },
988 }
989
990 cc_library {
991 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700992 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900993 nocrt: true,
994 system_shared_libs: [],
995 stl: "none",
996 stubs: {
997 versions: ["27", "28", "29"],
998 },
999 }
Jiyong Parkb0788572018-12-20 22:10:17 +09001000
1001 cc_library {
1002 name: "libBootstrap",
1003 srcs: ["mylib.cpp"],
1004 stl: "none",
1005 bootstrap: true,
1006 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001007 `)
1008
Sundong Ahnabb64432019-10-22 13:58:29 +09001009 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001010 copyCmds := apexRule.Args["copy_commands"]
1011
1012 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001013 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001014 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1015 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001016
1017 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001018 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001019
Colin Cross7113d202019-11-20 16:39:12 -08001020 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1021 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1022 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001023
1024 // For dependency to libc
1025 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001026 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001027 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001028 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001029 // ... Cflags from stub is correctly exported to mylib
1030 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1031 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1032
1033 // For dependency to libm
1034 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001035 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001036 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001037 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001038 // ... and is not compiling with the stub
1039 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1040 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1041
1042 // For dependency to libdl
1043 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001044 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001045 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001046 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1047 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001048 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001049 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001050 // ... Cflags from stub is correctly exported to mylib
1051 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1052 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001053
1054 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001055 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1056 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1057 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1058 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001059}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001060
1061func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001062 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001063 apex {
1064 name: "myapex",
1065 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001066 native_shared_libs: ["mylib"],
1067 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001068 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001069 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001070 }
1071
1072 apex_key {
1073 name: "myapex.key",
1074 public_key: "testkey.avbpubkey",
1075 private_key: "testkey.pem",
1076 }
1077
1078 prebuilt_etc {
1079 name: "myetc",
1080 src: "myprebuilt",
1081 sub_dir: "foo/bar",
1082 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001083
1084 cc_library {
1085 name: "mylib",
1086 srcs: ["mylib.cpp"],
1087 relative_install_path: "foo/bar",
1088 system_shared_libs: [],
1089 stl: "none",
1090 }
1091
1092 cc_binary {
1093 name: "mybin",
1094 srcs: ["mylib.cpp"],
1095 relative_install_path: "foo/bar",
1096 system_shared_libs: [],
1097 static_executable: true,
1098 stl: "none",
1099 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001100 `)
1101
Sundong Ahnabb64432019-10-22 13:58:29 +09001102 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001103 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1104
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001105 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001106 ensureListContains(t, dirs, "etc")
1107 ensureListContains(t, dirs, "etc/foo")
1108 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001109 ensureListContains(t, dirs, "lib64")
1110 ensureListContains(t, dirs, "lib64/foo")
1111 ensureListContains(t, dirs, "lib64/foo/bar")
1112 ensureListContains(t, dirs, "lib")
1113 ensureListContains(t, dirs, "lib/foo")
1114 ensureListContains(t, dirs, "lib/foo/bar")
1115
Jiyong Parkbd13e442019-03-15 18:10:35 +09001116 ensureListContains(t, dirs, "bin")
1117 ensureListContains(t, dirs, "bin/foo")
1118 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001119}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001120
1121func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001122 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001123 apex {
1124 name: "myapex",
1125 key: "myapex.key",
1126 native_shared_libs: ["mylib"],
1127 use_vendor: true,
1128 }
1129
1130 apex_key {
1131 name: "myapex.key",
1132 public_key: "testkey.avbpubkey",
1133 private_key: "testkey.pem",
1134 }
1135
1136 cc_library {
1137 name: "mylib",
1138 srcs: ["mylib.cpp"],
1139 shared_libs: ["mylib2"],
1140 system_shared_libs: [],
1141 vendor_available: true,
1142 stl: "none",
1143 }
1144
1145 cc_library {
1146 name: "mylib2",
1147 srcs: ["mylib.cpp"],
1148 system_shared_libs: [],
1149 vendor_available: true,
1150 stl: "none",
1151 }
Jooyung Handc782442019-11-01 03:14:38 +09001152 `, func(fs map[string][]byte, config android.Config) {
1153 setUseVendorWhitelistForTest(config, []string{"myapex"})
1154 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001155
1156 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001157 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001158 for _, implicit := range i.Implicits {
1159 inputsList = append(inputsList, implicit.String())
1160 }
1161 }
1162 inputsString := strings.Join(inputsList, " ")
1163
1164 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001165 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1166 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001167
1168 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001169 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1170 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001171}
Jiyong Park16e91a02018-12-20 18:18:08 +09001172
Jooyung Handc782442019-11-01 03:14:38 +09001173func TestUseVendorRestriction(t *testing.T) {
1174 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1175 apex {
1176 name: "myapex",
1177 key: "myapex.key",
1178 use_vendor: true,
1179 }
1180 apex_key {
1181 name: "myapex.key",
1182 public_key: "testkey.avbpubkey",
1183 private_key: "testkey.pem",
1184 }
1185 `, func(fs map[string][]byte, config android.Config) {
1186 setUseVendorWhitelistForTest(config, []string{""})
1187 })
1188 // no error with whitelist
1189 testApex(t, `
1190 apex {
1191 name: "myapex",
1192 key: "myapex.key",
1193 use_vendor: true,
1194 }
1195 apex_key {
1196 name: "myapex.key",
1197 public_key: "testkey.avbpubkey",
1198 private_key: "testkey.pem",
1199 }
1200 `, func(fs map[string][]byte, config android.Config) {
1201 setUseVendorWhitelistForTest(config, []string{"myapex"})
1202 })
1203}
1204
Jooyung Han5c998b92019-06-27 11:30:33 +09001205func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1206 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1207 apex {
1208 name: "myapex",
1209 key: "myapex.key",
1210 native_shared_libs: ["mylib"],
1211 use_vendor: true,
1212 }
1213
1214 apex_key {
1215 name: "myapex.key",
1216 public_key: "testkey.avbpubkey",
1217 private_key: "testkey.pem",
1218 }
1219
1220 cc_library {
1221 name: "mylib",
1222 srcs: ["mylib.cpp"],
1223 system_shared_libs: [],
1224 stl: "none",
1225 }
1226 `)
1227}
1228
Jiyong Park16e91a02018-12-20 18:18:08 +09001229func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001230 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001231 apex {
1232 name: "myapex",
1233 key: "myapex.key",
1234 native_shared_libs: ["mylib"],
1235 }
1236
1237 apex_key {
1238 name: "myapex.key",
1239 public_key: "testkey.avbpubkey",
1240 private_key: "testkey.pem",
1241 }
1242
1243 cc_library {
1244 name: "mylib",
1245 srcs: ["mylib.cpp"],
1246 system_shared_libs: [],
1247 stl: "none",
1248 stubs: {
1249 versions: ["1", "2", "3"],
1250 },
1251 }
1252
1253 cc_binary {
1254 name: "not_in_apex",
1255 srcs: ["mylib.cpp"],
1256 static_libs: ["mylib"],
1257 static_executable: true,
1258 system_shared_libs: [],
1259 stl: "none",
1260 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001261 `)
1262
Colin Cross7113d202019-11-20 16:39:12 -08001263 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001264
1265 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001266 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001267}
Jiyong Park9335a262018-12-24 11:31:58 +09001268
1269func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001270 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001271 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001272 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001273 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001274 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001275 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001276 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001277 }
1278
1279 cc_library {
1280 name: "mylib",
1281 srcs: ["mylib.cpp"],
1282 system_shared_libs: [],
1283 stl: "none",
1284 }
1285
1286 apex_key {
1287 name: "myapex.key",
1288 public_key: "testkey.avbpubkey",
1289 private_key: "testkey.pem",
1290 }
1291
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001292 android_app_certificate {
1293 name: "myapex.certificate",
1294 certificate: "testkey",
1295 }
1296
1297 android_app_certificate {
1298 name: "myapex.certificate.override",
1299 certificate: "testkey.override",
1300 }
1301
Jiyong Park9335a262018-12-24 11:31:58 +09001302 `)
1303
1304 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001305 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001306
1307 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1308 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1309 "vendor/foo/devkeys/testkey.avbpubkey")
1310 }
1311 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1312 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1313 "vendor/foo/devkeys/testkey.pem")
1314 }
1315
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001316 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001317 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001318 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001319 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001320 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001321 }
1322}
Jiyong Park58e364a2019-01-19 19:24:06 +09001323
Jooyung Hanf121a652019-12-17 14:30:11 +09001324func TestCertificate(t *testing.T) {
1325 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1326 ctx, _ := testApex(t, `
1327 apex {
1328 name: "myapex",
1329 key: "myapex.key",
1330 }
1331 apex_key {
1332 name: "myapex.key",
1333 public_key: "testkey.avbpubkey",
1334 private_key: "testkey.pem",
1335 }`)
1336 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1337 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1338 if actual := rule.Args["certificates"]; actual != expected {
1339 t.Errorf("certificates should be %q, not %q", expected, actual)
1340 }
1341 })
1342 t.Run("override when unspecified", func(t *testing.T) {
1343 ctx, _ := testApex(t, `
1344 apex {
1345 name: "myapex_keytest",
1346 key: "myapex.key",
1347 file_contexts: ":myapex-file_contexts",
1348 }
1349 apex_key {
1350 name: "myapex.key",
1351 public_key: "testkey.avbpubkey",
1352 private_key: "testkey.pem",
1353 }
1354 android_app_certificate {
1355 name: "myapex.certificate.override",
1356 certificate: "testkey.override",
1357 }`)
1358 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1359 expected := "testkey.override.x509.pem testkey.override.pk8"
1360 if actual := rule.Args["certificates"]; actual != expected {
1361 t.Errorf("certificates should be %q, not %q", expected, actual)
1362 }
1363 })
1364 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1365 ctx, _ := testApex(t, `
1366 apex {
1367 name: "myapex",
1368 key: "myapex.key",
1369 certificate: ":myapex.certificate",
1370 }
1371 apex_key {
1372 name: "myapex.key",
1373 public_key: "testkey.avbpubkey",
1374 private_key: "testkey.pem",
1375 }
1376 android_app_certificate {
1377 name: "myapex.certificate",
1378 certificate: "testkey",
1379 }`)
1380 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1381 expected := "testkey.x509.pem testkey.pk8"
1382 if actual := rule.Args["certificates"]; actual != expected {
1383 t.Errorf("certificates should be %q, not %q", expected, actual)
1384 }
1385 })
1386 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1387 ctx, _ := testApex(t, `
1388 apex {
1389 name: "myapex_keytest",
1390 key: "myapex.key",
1391 file_contexts: ":myapex-file_contexts",
1392 certificate: ":myapex.certificate",
1393 }
1394 apex_key {
1395 name: "myapex.key",
1396 public_key: "testkey.avbpubkey",
1397 private_key: "testkey.pem",
1398 }
1399 android_app_certificate {
1400 name: "myapex.certificate.override",
1401 certificate: "testkey.override",
1402 }`)
1403 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1404 expected := "testkey.override.x509.pem testkey.override.pk8"
1405 if actual := rule.Args["certificates"]; actual != expected {
1406 t.Errorf("certificates should be %q, not %q", expected, actual)
1407 }
1408 })
1409 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1410 ctx, _ := testApex(t, `
1411 apex {
1412 name: "myapex",
1413 key: "myapex.key",
1414 certificate: "testkey",
1415 }
1416 apex_key {
1417 name: "myapex.key",
1418 public_key: "testkey.avbpubkey",
1419 private_key: "testkey.pem",
1420 }`)
1421 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1422 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1423 if actual := rule.Args["certificates"]; actual != expected {
1424 t.Errorf("certificates should be %q, not %q", expected, actual)
1425 }
1426 })
1427 t.Run("override when specified as <name>", func(t *testing.T) {
1428 ctx, _ := testApex(t, `
1429 apex {
1430 name: "myapex_keytest",
1431 key: "myapex.key",
1432 file_contexts: ":myapex-file_contexts",
1433 certificate: "testkey",
1434 }
1435 apex_key {
1436 name: "myapex.key",
1437 public_key: "testkey.avbpubkey",
1438 private_key: "testkey.pem",
1439 }
1440 android_app_certificate {
1441 name: "myapex.certificate.override",
1442 certificate: "testkey.override",
1443 }`)
1444 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1445 expected := "testkey.override.x509.pem testkey.override.pk8"
1446 if actual := rule.Args["certificates"]; actual != expected {
1447 t.Errorf("certificates should be %q, not %q", expected, actual)
1448 }
1449 })
1450}
1451
Jiyong Park58e364a2019-01-19 19:24:06 +09001452func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001453 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001454 apex {
1455 name: "myapex",
1456 key: "myapex.key",
1457 native_shared_libs: ["mylib"],
1458 }
1459
1460 apex {
1461 name: "otherapex",
1462 key: "myapex.key",
1463 native_shared_libs: ["mylib"],
1464 }
1465
1466 apex_key {
1467 name: "myapex.key",
1468 public_key: "testkey.avbpubkey",
1469 private_key: "testkey.pem",
1470 }
1471
1472 cc_library {
1473 name: "mylib",
1474 srcs: ["mylib.cpp"],
1475 system_shared_libs: [],
1476 stl: "none",
1477 }
1478 `)
1479
Jooyung Han6b8459b2019-10-30 08:29:25 +09001480 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001481 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001482 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001483 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1484 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001485
Jooyung Han6b8459b2019-10-30 08:29:25 +09001486 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001487 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001488 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001489 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1490 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001491
Jooyung Han6b8459b2019-10-30 08:29:25 +09001492 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001493 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001494 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001495 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1496 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001497}
Jiyong Park7e636d02019-01-28 16:16:54 +09001498
1499func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001500 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001501 apex {
1502 name: "myapex",
1503 key: "myapex.key",
1504 native_shared_libs: ["mylib"],
1505 }
1506
1507 apex_key {
1508 name: "myapex.key",
1509 public_key: "testkey.avbpubkey",
1510 private_key: "testkey.pem",
1511 }
1512
1513 cc_library_headers {
1514 name: "mylib_headers",
1515 export_include_dirs: ["my_include"],
1516 system_shared_libs: [],
1517 stl: "none",
1518 }
1519
1520 cc_library {
1521 name: "mylib",
1522 srcs: ["mylib.cpp"],
1523 system_shared_libs: [],
1524 stl: "none",
1525 header_libs: ["mylib_headers"],
1526 export_header_lib_headers: ["mylib_headers"],
1527 stubs: {
1528 versions: ["1", "2", "3"],
1529 },
1530 }
1531
1532 cc_library {
1533 name: "otherlib",
1534 srcs: ["mylib.cpp"],
1535 system_shared_libs: [],
1536 stl: "none",
1537 shared_libs: ["mylib"],
1538 }
1539 `)
1540
Colin Cross7113d202019-11-20 16:39:12 -08001541 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001542
1543 // Ensure that the include path of the header lib is exported to 'otherlib'
1544 ensureContains(t, cFlags, "-Imy_include")
1545}
Alex Light9670d332019-01-29 18:07:33 -08001546
Jooyung Han31c470b2019-10-18 16:26:59 +09001547func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1548 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001549 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001550 copyCmds := apexRule.Args["copy_commands"]
1551 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001552 var failed bool
1553 var surplus []string
1554 filesMatched := make(map[string]bool)
1555 addContent := func(content string) {
1556 for _, expected := range files {
1557 if matched, _ := path.Match(expected, content); matched {
1558 filesMatched[expected] = true
1559 return
1560 }
1561 }
1562 surplus = append(surplus, content)
1563 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001564 for _, cmd := range strings.Split(copyCmds, "&&") {
1565 cmd = strings.TrimSpace(cmd)
1566 if cmd == "" {
1567 continue
1568 }
1569 terms := strings.Split(cmd, " ")
1570 switch terms[0] {
1571 case "mkdir":
1572 case "cp":
1573 if len(terms) != 3 {
1574 t.Fatal("copyCmds contains invalid cp command", cmd)
1575 }
1576 dst := terms[2]
1577 index := strings.Index(dst, imageApexDir)
1578 if index == -1 {
1579 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1580 }
1581 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001582 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001583 default:
1584 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1585 }
1586 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001587
Jooyung Han31c470b2019-10-18 16:26:59 +09001588 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001589 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001590 t.Log("surplus files", surplus)
1591 failed = true
1592 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001593
1594 if len(files) > len(filesMatched) {
1595 var missing []string
1596 for _, expected := range files {
1597 if !filesMatched[expected] {
1598 missing = append(missing, expected)
1599 }
1600 }
1601 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001602 t.Log("missing files", missing)
1603 failed = true
1604 }
1605 if failed {
1606 t.Fail()
1607 }
1608}
1609
Jooyung Han344d5432019-08-23 11:17:39 +09001610func TestVndkApexCurrent(t *testing.T) {
1611 ctx, _ := testApex(t, `
1612 apex_vndk {
1613 name: "myapex",
1614 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001615 }
1616
1617 apex_key {
1618 name: "myapex.key",
1619 public_key: "testkey.avbpubkey",
1620 private_key: "testkey.pem",
1621 }
1622
1623 cc_library {
1624 name: "libvndk",
1625 srcs: ["mylib.cpp"],
1626 vendor_available: true,
1627 vndk: {
1628 enabled: true,
1629 },
1630 system_shared_libs: [],
1631 stl: "none",
1632 }
1633
1634 cc_library {
1635 name: "libvndksp",
1636 srcs: ["mylib.cpp"],
1637 vendor_available: true,
1638 vndk: {
1639 enabled: true,
1640 support_system_process: true,
1641 },
1642 system_shared_libs: [],
1643 stl: "none",
1644 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001645 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001646
Jooyung Han31c470b2019-10-18 16:26:59 +09001647 ensureExactContents(t, ctx, "myapex", []string{
1648 "lib/libvndk.so",
1649 "lib/libvndksp.so",
1650 "lib64/libvndk.so",
1651 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001652 "etc/llndk.libraries.VER.txt",
1653 "etc/vndkcore.libraries.VER.txt",
1654 "etc/vndksp.libraries.VER.txt",
1655 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001656 })
Jooyung Han344d5432019-08-23 11:17:39 +09001657}
1658
1659func TestVndkApexWithPrebuilt(t *testing.T) {
1660 ctx, _ := testApex(t, `
1661 apex_vndk {
1662 name: "myapex",
1663 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001664 }
1665
1666 apex_key {
1667 name: "myapex.key",
1668 public_key: "testkey.avbpubkey",
1669 private_key: "testkey.pem",
1670 }
1671
1672 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001673 name: "libvndk",
1674 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001675 vendor_available: true,
1676 vndk: {
1677 enabled: true,
1678 },
1679 system_shared_libs: [],
1680 stl: "none",
1681 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001682
1683 cc_prebuilt_library_shared {
1684 name: "libvndk.arm",
1685 srcs: ["libvndk.arm.so"],
1686 vendor_available: true,
1687 vndk: {
1688 enabled: true,
1689 },
1690 enabled: false,
1691 arch: {
1692 arm: {
1693 enabled: true,
1694 },
1695 },
1696 system_shared_libs: [],
1697 stl: "none",
1698 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001699 `+vndkLibrariesTxtFiles("current"),
1700 withFiles(map[string][]byte{
1701 "libvndk.so": nil,
1702 "libvndk.arm.so": nil,
1703 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001704
Jooyung Han31c470b2019-10-18 16:26:59 +09001705 ensureExactContents(t, ctx, "myapex", []string{
1706 "lib/libvndk.so",
1707 "lib/libvndk.arm.so",
1708 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001709 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001710 })
Jooyung Han344d5432019-08-23 11:17:39 +09001711}
1712
Jooyung Han39edb6c2019-11-06 16:53:07 +09001713func vndkLibrariesTxtFiles(vers ...string) (result string) {
1714 for _, v := range vers {
1715 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001716 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001717 result += `
1718 vndk_libraries_txt {
1719 name: "` + txt + `.libraries.txt",
1720 }
1721 `
1722 }
1723 } else {
1724 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1725 result += `
1726 prebuilt_etc {
1727 name: "` + txt + `.libraries.` + v + `.txt",
1728 src: "dummy.txt",
1729 }
1730 `
1731 }
1732 }
1733 }
1734 return
1735}
1736
Jooyung Han344d5432019-08-23 11:17:39 +09001737func TestVndkApexVersion(t *testing.T) {
1738 ctx, _ := testApex(t, `
1739 apex_vndk {
1740 name: "myapex_v27",
1741 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001742 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001743 vndk_version: "27",
1744 }
1745
1746 apex_key {
1747 name: "myapex.key",
1748 public_key: "testkey.avbpubkey",
1749 private_key: "testkey.pem",
1750 }
1751
Jooyung Han31c470b2019-10-18 16:26:59 +09001752 vndk_prebuilt_shared {
1753 name: "libvndk27",
1754 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001755 vendor_available: true,
1756 vndk: {
1757 enabled: true,
1758 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001759 target_arch: "arm64",
1760 arch: {
1761 arm: {
1762 srcs: ["libvndk27_arm.so"],
1763 },
1764 arm64: {
1765 srcs: ["libvndk27_arm64.so"],
1766 },
1767 },
Jooyung Han344d5432019-08-23 11:17:39 +09001768 }
1769
1770 vndk_prebuilt_shared {
1771 name: "libvndk27",
1772 version: "27",
1773 vendor_available: true,
1774 vndk: {
1775 enabled: true,
1776 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001777 target_arch: "x86_64",
1778 arch: {
1779 x86: {
1780 srcs: ["libvndk27_x86.so"],
1781 },
1782 x86_64: {
1783 srcs: ["libvndk27_x86_64.so"],
1784 },
1785 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001786 }
1787 `+vndkLibrariesTxtFiles("27"),
1788 withFiles(map[string][]byte{
1789 "libvndk27_arm.so": nil,
1790 "libvndk27_arm64.so": nil,
1791 "libvndk27_x86.so": nil,
1792 "libvndk27_x86_64.so": nil,
1793 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001794
Jooyung Han31c470b2019-10-18 16:26:59 +09001795 ensureExactContents(t, ctx, "myapex_v27", []string{
1796 "lib/libvndk27_arm.so",
1797 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001798 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001799 })
Jooyung Han344d5432019-08-23 11:17:39 +09001800}
1801
1802func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1803 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1804 apex_vndk {
1805 name: "myapex_v27",
1806 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001807 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001808 vndk_version: "27",
1809 }
1810 apex_vndk {
1811 name: "myapex_v27_other",
1812 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001813 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001814 vndk_version: "27",
1815 }
1816
1817 apex_key {
1818 name: "myapex.key",
1819 public_key: "testkey.avbpubkey",
1820 private_key: "testkey.pem",
1821 }
1822
1823 cc_library {
1824 name: "libvndk",
1825 srcs: ["mylib.cpp"],
1826 vendor_available: true,
1827 vndk: {
1828 enabled: true,
1829 },
1830 system_shared_libs: [],
1831 stl: "none",
1832 }
1833
1834 vndk_prebuilt_shared {
1835 name: "libvndk",
1836 version: "27",
1837 vendor_available: true,
1838 vndk: {
1839 enabled: true,
1840 },
1841 srcs: ["libvndk.so"],
1842 }
1843 `, withFiles(map[string][]byte{
1844 "libvndk.so": nil,
1845 }))
1846}
1847
Jooyung Han90eee022019-10-01 20:02:42 +09001848func TestVndkApexNameRule(t *testing.T) {
1849 ctx, _ := testApex(t, `
1850 apex_vndk {
1851 name: "myapex",
1852 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001853 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001854 }
1855 apex_vndk {
1856 name: "myapex_v28",
1857 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001858 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001859 vndk_version: "28",
1860 }
1861 apex_key {
1862 name: "myapex.key",
1863 public_key: "testkey.avbpubkey",
1864 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001865 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001866
1867 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001868 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001869 actual := proptools.String(bundle.properties.Apex_name)
1870 if !reflect.DeepEqual(actual, expected) {
1871 t.Errorf("Got '%v', expected '%v'", actual, expected)
1872 }
1873 }
1874
1875 assertApexName("com.android.vndk.vVER", "myapex")
1876 assertApexName("com.android.vndk.v28", "myapex_v28")
1877}
1878
Jooyung Han344d5432019-08-23 11:17:39 +09001879func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1880 ctx, _ := testApex(t, `
1881 apex_vndk {
1882 name: "myapex",
1883 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001884 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001885 }
1886
1887 apex_key {
1888 name: "myapex.key",
1889 public_key: "testkey.avbpubkey",
1890 private_key: "testkey.pem",
1891 }
1892
1893 cc_library {
1894 name: "libvndk",
1895 srcs: ["mylib.cpp"],
1896 vendor_available: true,
1897 native_bridge_supported: true,
1898 host_supported: true,
1899 vndk: {
1900 enabled: true,
1901 },
1902 system_shared_libs: [],
1903 stl: "none",
1904 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001905 `+vndkLibrariesTxtFiles("current"),
1906 withTargets(map[android.OsType][]android.Target{
1907 android.Android: []android.Target{
1908 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1909 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1910 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1911 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1912 },
1913 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001914
Jooyung Han31c470b2019-10-18 16:26:59 +09001915 ensureExactContents(t, ctx, "myapex", []string{
1916 "lib/libvndk.so",
1917 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001918 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001919 })
Jooyung Han344d5432019-08-23 11:17:39 +09001920}
1921
1922func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1923 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1924 apex_vndk {
1925 name: "myapex",
1926 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001927 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001928 native_bridge_supported: true,
1929 }
1930
1931 apex_key {
1932 name: "myapex.key",
1933 public_key: "testkey.avbpubkey",
1934 private_key: "testkey.pem",
1935 }
1936
1937 cc_library {
1938 name: "libvndk",
1939 srcs: ["mylib.cpp"],
1940 vendor_available: true,
1941 native_bridge_supported: true,
1942 host_supported: true,
1943 vndk: {
1944 enabled: true,
1945 },
1946 system_shared_libs: [],
1947 stl: "none",
1948 }
1949 `)
1950}
1951
Jooyung Han31c470b2019-10-18 16:26:59 +09001952func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001953 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001954 apex_vndk {
1955 name: "myapex_v27",
1956 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001957 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001958 vndk_version: "27",
1959 }
1960
1961 apex_key {
1962 name: "myapex.key",
1963 public_key: "testkey.avbpubkey",
1964 private_key: "testkey.pem",
1965 }
1966
1967 vndk_prebuilt_shared {
1968 name: "libvndk27",
1969 version: "27",
1970 target_arch: "arm",
1971 vendor_available: true,
1972 vndk: {
1973 enabled: true,
1974 },
1975 arch: {
1976 arm: {
1977 srcs: ["libvndk27.so"],
1978 }
1979 },
1980 }
1981
1982 vndk_prebuilt_shared {
1983 name: "libvndk27",
1984 version: "27",
1985 target_arch: "arm",
1986 binder32bit: true,
1987 vendor_available: true,
1988 vndk: {
1989 enabled: true,
1990 },
1991 arch: {
1992 arm: {
1993 srcs: ["libvndk27binder32.so"],
1994 }
1995 },
1996 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001997 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001998 withFiles(map[string][]byte{
1999 "libvndk27.so": nil,
2000 "libvndk27binder32.so": nil,
2001 }),
2002 withBinder32bit,
2003 withTargets(map[android.OsType][]android.Target{
2004 android.Android: []android.Target{
2005 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2006 },
2007 }),
2008 )
2009
2010 ensureExactContents(t, ctx, "myapex_v27", []string{
2011 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002012 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002013 })
2014}
2015
Jooyung Hane1633032019-08-01 17:41:43 +09002016func TestDependenciesInApexManifest(t *testing.T) {
2017 ctx, _ := testApex(t, `
2018 apex {
2019 name: "myapex_nodep",
2020 key: "myapex.key",
2021 native_shared_libs: ["lib_nodep"],
2022 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002023 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002024 }
2025
2026 apex {
2027 name: "myapex_dep",
2028 key: "myapex.key",
2029 native_shared_libs: ["lib_dep"],
2030 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002031 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002032 }
2033
2034 apex {
2035 name: "myapex_provider",
2036 key: "myapex.key",
2037 native_shared_libs: ["libfoo"],
2038 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002039 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002040 }
2041
2042 apex {
2043 name: "myapex_selfcontained",
2044 key: "myapex.key",
2045 native_shared_libs: ["lib_dep", "libfoo"],
2046 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002047 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002048 }
2049
2050 apex_key {
2051 name: "myapex.key",
2052 public_key: "testkey.avbpubkey",
2053 private_key: "testkey.pem",
2054 }
2055
2056 cc_library {
2057 name: "lib_nodep",
2058 srcs: ["mylib.cpp"],
2059 system_shared_libs: [],
2060 stl: "none",
2061 }
2062
2063 cc_library {
2064 name: "lib_dep",
2065 srcs: ["mylib.cpp"],
2066 shared_libs: ["libfoo"],
2067 system_shared_libs: [],
2068 stl: "none",
2069 }
2070
2071 cc_library {
2072 name: "libfoo",
2073 srcs: ["mytest.cpp"],
2074 stubs: {
2075 versions: ["1"],
2076 },
2077 system_shared_libs: [],
2078 stl: "none",
2079 }
2080 `)
2081
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002082 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002083 var provideNativeLibs, requireNativeLibs []string
2084
Sundong Ahnabb64432019-10-22 13:58:29 +09002085 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002086 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2087 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002088 ensureListEmpty(t, provideNativeLibs)
2089 ensureListEmpty(t, requireNativeLibs)
2090
Sundong Ahnabb64432019-10-22 13:58:29 +09002091 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002092 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2093 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002094 ensureListEmpty(t, provideNativeLibs)
2095 ensureListContains(t, requireNativeLibs, "libfoo.so")
2096
Sundong Ahnabb64432019-10-22 13:58:29 +09002097 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002098 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2099 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002100 ensureListContains(t, provideNativeLibs, "libfoo.so")
2101 ensureListEmpty(t, requireNativeLibs)
2102
Sundong Ahnabb64432019-10-22 13:58:29 +09002103 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002104 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2105 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002106 ensureListContains(t, provideNativeLibs, "libfoo.so")
2107 ensureListEmpty(t, requireNativeLibs)
2108}
2109
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002110func TestApexName(t *testing.T) {
2111 ctx, _ := testApex(t, `
2112 apex {
2113 name: "myapex",
2114 key: "myapex.key",
2115 apex_name: "com.android.myapex",
2116 }
2117
2118 apex_key {
2119 name: "myapex.key",
2120 public_key: "testkey.avbpubkey",
2121 private_key: "testkey.pem",
2122 }
2123 `)
2124
Sundong Ahnabb64432019-10-22 13:58:29 +09002125 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002126 apexManifestRule := module.Rule("apexManifestRule")
2127 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2128 apexRule := module.Rule("apexRule")
2129 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
2130}
2131
Alex Light0851b882019-02-07 13:20:53 -08002132func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002133 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002134 apex {
2135 name: "myapex",
2136 key: "myapex.key",
2137 native_shared_libs: ["mylib_common"],
2138 }
2139
2140 apex_key {
2141 name: "myapex.key",
2142 public_key: "testkey.avbpubkey",
2143 private_key: "testkey.pem",
2144 }
2145
2146 cc_library {
2147 name: "mylib_common",
2148 srcs: ["mylib.cpp"],
2149 system_shared_libs: [],
2150 stl: "none",
2151 }
2152 `)
2153
Sundong Ahnabb64432019-10-22 13:58:29 +09002154 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002155 apexRule := module.Rule("apexRule")
2156 copyCmds := apexRule.Args["copy_commands"]
2157
2158 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2159 t.Log("Apex was a test apex!")
2160 t.Fail()
2161 }
2162 // Ensure that main rule creates an output
2163 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2164
2165 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002166 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002167
2168 // Ensure that both direct and indirect deps are copied into apex
2169 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2170
Colin Cross7113d202019-11-20 16:39:12 -08002171 // Ensure that the platform variant ends with _shared
2172 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002173
2174 if !android.InAnyApex("mylib_common") {
2175 t.Log("Found mylib_common not in any apex!")
2176 t.Fail()
2177 }
2178}
2179
2180func TestTestApex(t *testing.T) {
2181 if android.InAnyApex("mylib_common_test") {
2182 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!")
2183 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002184 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002185 apex_test {
2186 name: "myapex",
2187 key: "myapex.key",
2188 native_shared_libs: ["mylib_common_test"],
2189 }
2190
2191 apex_key {
2192 name: "myapex.key",
2193 public_key: "testkey.avbpubkey",
2194 private_key: "testkey.pem",
2195 }
2196
2197 cc_library {
2198 name: "mylib_common_test",
2199 srcs: ["mylib.cpp"],
2200 system_shared_libs: [],
2201 stl: "none",
2202 }
2203 `)
2204
Sundong Ahnabb64432019-10-22 13:58:29 +09002205 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002206 apexRule := module.Rule("apexRule")
2207 copyCmds := apexRule.Args["copy_commands"]
2208
2209 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2210 t.Log("Apex was not a test apex!")
2211 t.Fail()
2212 }
2213 // Ensure that main rule creates an output
2214 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2215
2216 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002217 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002218
2219 // Ensure that both direct and indirect deps are copied into apex
2220 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2221
Colin Cross7113d202019-11-20 16:39:12 -08002222 // Ensure that the platform variant ends with _shared
2223 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002224
2225 if android.InAnyApex("mylib_common_test") {
2226 t.Log("Found mylib_common_test in some apex!")
2227 t.Fail()
2228 }
2229}
2230
Alex Light9670d332019-01-29 18:07:33 -08002231func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002232 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002233 apex {
2234 name: "myapex",
2235 key: "myapex.key",
2236 multilib: {
2237 first: {
2238 native_shared_libs: ["mylib_common"],
2239 }
2240 },
2241 target: {
2242 android: {
2243 multilib: {
2244 first: {
2245 native_shared_libs: ["mylib"],
2246 }
2247 }
2248 },
2249 host: {
2250 multilib: {
2251 first: {
2252 native_shared_libs: ["mylib2"],
2253 }
2254 }
2255 }
2256 }
2257 }
2258
2259 apex_key {
2260 name: "myapex.key",
2261 public_key: "testkey.avbpubkey",
2262 private_key: "testkey.pem",
2263 }
2264
2265 cc_library {
2266 name: "mylib",
2267 srcs: ["mylib.cpp"],
2268 system_shared_libs: [],
2269 stl: "none",
2270 }
2271
2272 cc_library {
2273 name: "mylib_common",
2274 srcs: ["mylib.cpp"],
2275 system_shared_libs: [],
2276 stl: "none",
2277 compile_multilib: "first",
2278 }
2279
2280 cc_library {
2281 name: "mylib2",
2282 srcs: ["mylib.cpp"],
2283 system_shared_libs: [],
2284 stl: "none",
2285 compile_multilib: "first",
2286 }
2287 `)
2288
Sundong Ahnabb64432019-10-22 13:58:29 +09002289 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002290 copyCmds := apexRule.Args["copy_commands"]
2291
2292 // Ensure that main rule creates an output
2293 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2294
2295 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002296 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2297 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2298 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002299
2300 // Ensure that both direct and indirect deps are copied into apex
2301 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2302 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2303 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2304
Colin Cross7113d202019-11-20 16:39:12 -08002305 // Ensure that the platform variant ends with _shared
2306 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2307 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2308 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002309}
Jiyong Park04480cf2019-02-06 00:16:29 +09002310
2311func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002312 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002313 apex {
2314 name: "myapex",
2315 key: "myapex.key",
2316 binaries: ["myscript"],
2317 }
2318
2319 apex_key {
2320 name: "myapex.key",
2321 public_key: "testkey.avbpubkey",
2322 private_key: "testkey.pem",
2323 }
2324
2325 sh_binary {
2326 name: "myscript",
2327 src: "mylib.cpp",
2328 filename: "myscript.sh",
2329 sub_dir: "script",
2330 }
2331 `)
2332
Sundong Ahnabb64432019-10-22 13:58:29 +09002333 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002334 copyCmds := apexRule.Args["copy_commands"]
2335
2336 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2337}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002338
Jooyung Han91df2082019-11-20 01:49:42 +09002339func TestApexInVariousPartition(t *testing.T) {
2340 testcases := []struct {
2341 propName, parition, flattenedPartition string
2342 }{
2343 {"", "system", "system_ext"},
2344 {"product_specific: true", "product", "product"},
2345 {"soc_specific: true", "vendor", "vendor"},
2346 {"proprietary: true", "vendor", "vendor"},
2347 {"vendor: true", "vendor", "vendor"},
2348 {"system_ext_specific: true", "system_ext", "system_ext"},
2349 }
2350 for _, tc := range testcases {
2351 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2352 ctx, _ := testApex(t, `
2353 apex {
2354 name: "myapex",
2355 key: "myapex.key",
2356 `+tc.propName+`
2357 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002358
Jooyung Han91df2082019-11-20 01:49:42 +09002359 apex_key {
2360 name: "myapex.key",
2361 public_key: "testkey.avbpubkey",
2362 private_key: "testkey.pem",
2363 }
2364 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002365
Jooyung Han91df2082019-11-20 01:49:42 +09002366 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2367 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2368 actual := apex.installDir.String()
2369 if actual != expected {
2370 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2371 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002372
Jooyung Han91df2082019-11-20 01:49:42 +09002373 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2374 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2375 actual = flattened.installDir.String()
2376 if actual != expected {
2377 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2378 }
2379 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002380 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002381}
Jiyong Park67882562019-03-21 01:11:21 +09002382
Jooyung Han54aca7b2019-11-20 02:26:02 +09002383func TestFileContexts(t *testing.T) {
2384 ctx, _ := testApex(t, `
2385 apex {
2386 name: "myapex",
2387 key: "myapex.key",
2388 }
2389
2390 apex_key {
2391 name: "myapex.key",
2392 public_key: "testkey.avbpubkey",
2393 private_key: "testkey.pem",
2394 }
2395 `)
2396 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2397 apexRule := module.Rule("apexRule")
2398 actual := apexRule.Args["file_contexts"]
2399 expected := "system/sepolicy/apex/myapex-file_contexts"
2400 if actual != expected {
2401 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2402 }
2403
2404 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2405 apex {
2406 name: "myapex",
2407 key: "myapex.key",
2408 file_contexts: "my_own_file_contexts",
2409 }
2410
2411 apex_key {
2412 name: "myapex.key",
2413 public_key: "testkey.avbpubkey",
2414 private_key: "testkey.pem",
2415 }
2416 `, withFiles(map[string][]byte{
2417 "my_own_file_contexts": nil,
2418 }))
2419
2420 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2421 apex {
2422 name: "myapex",
2423 key: "myapex.key",
2424 product_specific: true,
2425 file_contexts: "product_specific_file_contexts",
2426 }
2427
2428 apex_key {
2429 name: "myapex.key",
2430 public_key: "testkey.avbpubkey",
2431 private_key: "testkey.pem",
2432 }
2433 `)
2434
2435 ctx, _ = testApex(t, `
2436 apex {
2437 name: "myapex",
2438 key: "myapex.key",
2439 product_specific: true,
2440 file_contexts: "product_specific_file_contexts",
2441 }
2442
2443 apex_key {
2444 name: "myapex.key",
2445 public_key: "testkey.avbpubkey",
2446 private_key: "testkey.pem",
2447 }
2448 `, withFiles(map[string][]byte{
2449 "product_specific_file_contexts": nil,
2450 }))
2451 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2452 apexRule = module.Rule("apexRule")
2453 actual = apexRule.Args["file_contexts"]
2454 expected = "product_specific_file_contexts"
2455 if actual != expected {
2456 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2457 }
2458
2459 ctx, _ = testApex(t, `
2460 apex {
2461 name: "myapex",
2462 key: "myapex.key",
2463 product_specific: true,
2464 file_contexts: ":my-file-contexts",
2465 }
2466
2467 apex_key {
2468 name: "myapex.key",
2469 public_key: "testkey.avbpubkey",
2470 private_key: "testkey.pem",
2471 }
2472
2473 filegroup {
2474 name: "my-file-contexts",
2475 srcs: ["product_specific_file_contexts"],
2476 }
2477 `, withFiles(map[string][]byte{
2478 "product_specific_file_contexts": nil,
2479 }))
2480 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2481 apexRule = module.Rule("apexRule")
2482 actual = apexRule.Args["file_contexts"]
2483 expected = "product_specific_file_contexts"
2484 if actual != expected {
2485 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2486 }
2487}
2488
Jiyong Park67882562019-03-21 01:11:21 +09002489func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002490 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002491 apex_key {
2492 name: "myapex.key",
2493 public_key: ":my.avbpubkey",
2494 private_key: ":my.pem",
2495 product_specific: true,
2496 }
2497
2498 filegroup {
2499 name: "my.avbpubkey",
2500 srcs: ["testkey2.avbpubkey"],
2501 }
2502
2503 filegroup {
2504 name: "my.pem",
2505 srcs: ["testkey2.pem"],
2506 }
2507 `)
2508
2509 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2510 expected_pubkey := "testkey2.avbpubkey"
2511 actual_pubkey := apex_key.public_key_file.String()
2512 if actual_pubkey != expected_pubkey {
2513 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2514 }
2515 expected_privkey := "testkey2.pem"
2516 actual_privkey := apex_key.private_key_file.String()
2517 if actual_privkey != expected_privkey {
2518 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2519 }
2520}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002521
2522func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002523 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002524 prebuilt_apex {
2525 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002526 arch: {
2527 arm64: {
2528 src: "myapex-arm64.apex",
2529 },
2530 arm: {
2531 src: "myapex-arm.apex",
2532 },
2533 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002534 }
2535 `)
2536
2537 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2538
Jiyong Parkc95714e2019-03-29 14:23:10 +09002539 expectedInput := "myapex-arm64.apex"
2540 if prebuilt.inputApex.String() != expectedInput {
2541 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2542 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002543}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002544
2545func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002546 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002547 prebuilt_apex {
2548 name: "myapex",
2549 src: "myapex-arm.apex",
2550 filename: "notmyapex.apex",
2551 }
2552 `)
2553
2554 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2555
2556 expected := "notmyapex.apex"
2557 if p.installFilename != expected {
2558 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2559 }
2560}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002561
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002562func TestPrebuiltOverrides(t *testing.T) {
2563 ctx, config := testApex(t, `
2564 prebuilt_apex {
2565 name: "myapex.prebuilt",
2566 src: "myapex-arm.apex",
2567 overrides: [
2568 "myapex",
2569 ],
2570 }
2571 `)
2572
2573 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2574
2575 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002576 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002577 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002578 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002579 }
2580}
2581
Roland Levillain630846d2019-06-26 12:48:34 +01002582func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002583 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002584 apex_test {
2585 name: "myapex",
2586 key: "myapex.key",
2587 tests: [
2588 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002589 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002590 ],
2591 }
2592
2593 apex_key {
2594 name: "myapex.key",
2595 public_key: "testkey.avbpubkey",
2596 private_key: "testkey.pem",
2597 }
2598
2599 cc_test {
2600 name: "mytest",
2601 gtest: false,
2602 srcs: ["mytest.cpp"],
2603 relative_install_path: "test",
2604 system_shared_libs: [],
2605 static_executable: true,
2606 stl: "none",
2607 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002608
2609 cc_test {
2610 name: "mytests",
2611 gtest: false,
2612 srcs: [
2613 "mytest1.cpp",
2614 "mytest2.cpp",
2615 "mytest3.cpp",
2616 ],
2617 test_per_src: true,
2618 relative_install_path: "test",
2619 system_shared_libs: [],
2620 static_executable: true,
2621 stl: "none",
2622 }
Roland Levillain630846d2019-06-26 12:48:34 +01002623 `)
2624
Sundong Ahnabb64432019-10-22 13:58:29 +09002625 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002626 copyCmds := apexRule.Args["copy_commands"]
2627
2628 // Ensure that test dep is copied into apex.
2629 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002630
2631 // Ensure that test deps built with `test_per_src` are copied into apex.
2632 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2633 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2634 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002635
2636 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002637 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002638 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2639 name := apexBundle.BaseModuleName()
2640 prefix := "TARGET_"
2641 var builder strings.Builder
2642 data.Custom(&builder, name, prefix, "", data)
2643 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002644 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2645 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2646 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2647 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002648 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002649 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002650 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002651}
2652
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002653func TestInstallExtraFlattenedApexes(t *testing.T) {
2654 ctx, config := testApex(t, `
2655 apex {
2656 name: "myapex",
2657 key: "myapex.key",
2658 }
2659 apex_key {
2660 name: "myapex.key",
2661 public_key: "testkey.avbpubkey",
2662 private_key: "testkey.pem",
2663 }
2664 `, func(fs map[string][]byte, config android.Config) {
2665 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2666 })
2667 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002668 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002669 mk := android.AndroidMkDataForTest(t, config, "", ab)
2670 var builder strings.Builder
2671 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2672 androidMk := builder.String()
2673 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2674}
2675
Jooyung Han5c998b92019-06-27 11:30:33 +09002676func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002677 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002678 apex {
2679 name: "myapex",
2680 key: "myapex.key",
2681 native_shared_libs: ["mylib"],
2682 uses: ["commonapex"],
2683 }
2684
2685 apex {
2686 name: "commonapex",
2687 key: "myapex.key",
2688 native_shared_libs: ["libcommon"],
2689 provide_cpp_shared_libs: true,
2690 }
2691
2692 apex_key {
2693 name: "myapex.key",
2694 public_key: "testkey.avbpubkey",
2695 private_key: "testkey.pem",
2696 }
2697
2698 cc_library {
2699 name: "mylib",
2700 srcs: ["mylib.cpp"],
2701 shared_libs: ["libcommon"],
2702 system_shared_libs: [],
2703 stl: "none",
2704 }
2705
2706 cc_library {
2707 name: "libcommon",
2708 srcs: ["mylib_common.cpp"],
2709 system_shared_libs: [],
2710 stl: "none",
2711 }
2712 `)
2713
Sundong Ahnabb64432019-10-22 13:58:29 +09002714 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002715 apexRule1 := module1.Rule("apexRule")
2716 copyCmds1 := apexRule1.Args["copy_commands"]
2717
Sundong Ahnabb64432019-10-22 13:58:29 +09002718 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002719 apexRule2 := module2.Rule("apexRule")
2720 copyCmds2 := apexRule2.Args["copy_commands"]
2721
Colin Cross7113d202019-11-20 16:39:12 -08002722 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2723 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002724 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2725 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2726 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2727}
2728
2729func TestApexUsesFailsIfNotProvided(t *testing.T) {
2730 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2731 apex {
2732 name: "myapex",
2733 key: "myapex.key",
2734 uses: ["commonapex"],
2735 }
2736
2737 apex {
2738 name: "commonapex",
2739 key: "myapex.key",
2740 }
2741
2742 apex_key {
2743 name: "myapex.key",
2744 public_key: "testkey.avbpubkey",
2745 private_key: "testkey.pem",
2746 }
2747 `)
2748 testApexError(t, `uses: "commonapex" is not a provider`, `
2749 apex {
2750 name: "myapex",
2751 key: "myapex.key",
2752 uses: ["commonapex"],
2753 }
2754
2755 cc_library {
2756 name: "commonapex",
2757 system_shared_libs: [],
2758 stl: "none",
2759 }
2760
2761 apex_key {
2762 name: "myapex.key",
2763 public_key: "testkey.avbpubkey",
2764 private_key: "testkey.pem",
2765 }
2766 `)
2767}
2768
2769func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2770 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2771 apex {
2772 name: "myapex",
2773 key: "myapex.key",
2774 use_vendor: true,
2775 uses: ["commonapex"],
2776 }
2777
2778 apex {
2779 name: "commonapex",
2780 key: "myapex.key",
2781 provide_cpp_shared_libs: true,
2782 }
2783
2784 apex_key {
2785 name: "myapex.key",
2786 public_key: "testkey.avbpubkey",
2787 private_key: "testkey.pem",
2788 }
Jooyung Handc782442019-11-01 03:14:38 +09002789 `, func(fs map[string][]byte, config android.Config) {
2790 setUseVendorWhitelistForTest(config, []string{"myapex"})
2791 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002792}
2793
Jooyung Hand48f3c32019-08-23 11:18:57 +09002794func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2795 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2796 apex {
2797 name: "myapex",
2798 key: "myapex.key",
2799 native_shared_libs: ["libfoo"],
2800 }
2801
2802 apex_key {
2803 name: "myapex.key",
2804 public_key: "testkey.avbpubkey",
2805 private_key: "testkey.pem",
2806 }
2807
2808 cc_library {
2809 name: "libfoo",
2810 stl: "none",
2811 system_shared_libs: [],
2812 enabled: false,
2813 }
2814 `)
2815 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2816 apex {
2817 name: "myapex",
2818 key: "myapex.key",
2819 java_libs: ["myjar"],
2820 }
2821
2822 apex_key {
2823 name: "myapex.key",
2824 public_key: "testkey.avbpubkey",
2825 private_key: "testkey.pem",
2826 }
2827
2828 java_library {
2829 name: "myjar",
2830 srcs: ["foo/bar/MyClass.java"],
2831 sdk_version: "none",
2832 system_modules: "none",
2833 compile_dex: true,
2834 enabled: false,
2835 }
2836 `)
2837}
2838
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002839func TestApexWithApps(t *testing.T) {
2840 ctx, _ := testApex(t, `
2841 apex {
2842 name: "myapex",
2843 key: "myapex.key",
2844 apps: [
2845 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002846 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002847 ],
2848 }
2849
2850 apex_key {
2851 name: "myapex.key",
2852 public_key: "testkey.avbpubkey",
2853 private_key: "testkey.pem",
2854 }
2855
2856 android_app {
2857 name: "AppFoo",
2858 srcs: ["foo/bar/MyClass.java"],
2859 sdk_version: "none",
2860 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002861 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002862 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002863
2864 android_app {
2865 name: "AppFooPriv",
2866 srcs: ["foo/bar/MyClass.java"],
2867 sdk_version: "none",
2868 system_modules: "none",
2869 privileged: true,
2870 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002871
2872 cc_library_shared {
2873 name: "libjni",
2874 srcs: ["mylib.cpp"],
2875 stl: "none",
2876 system_shared_libs: [],
2877 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002878 `)
2879
Sundong Ahnabb64432019-10-22 13:58:29 +09002880 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002881 apexRule := module.Rule("apexRule")
2882 copyCmds := apexRule.Args["copy_commands"]
2883
2884 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002885 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002886
2887 // JNI libraries are embedded inside APK
2888 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
Colin Cross7113d202019-11-20 16:39:12 -08002889 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002890 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2891 // ... uncompressed
2892 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2893 t.Errorf("jni lib is not uncompressed for AppFoo")
2894 }
2895 // ... and not directly inside the APEX
2896 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002897}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002898
Dario Frenicde2a032019-10-27 00:29:22 +01002899func TestApexWithAppImports(t *testing.T) {
2900 ctx, _ := testApex(t, `
2901 apex {
2902 name: "myapex",
2903 key: "myapex.key",
2904 apps: [
2905 "AppFooPrebuilt",
2906 "AppFooPrivPrebuilt",
2907 ],
2908 }
2909
2910 apex_key {
2911 name: "myapex.key",
2912 public_key: "testkey.avbpubkey",
2913 private_key: "testkey.pem",
2914 }
2915
2916 android_app_import {
2917 name: "AppFooPrebuilt",
2918 apk: "PrebuiltAppFoo.apk",
2919 presigned: true,
2920 dex_preopt: {
2921 enabled: false,
2922 },
2923 }
2924
2925 android_app_import {
2926 name: "AppFooPrivPrebuilt",
2927 apk: "PrebuiltAppFooPriv.apk",
2928 privileged: true,
2929 presigned: true,
2930 dex_preopt: {
2931 enabled: false,
2932 },
2933 }
2934 `)
2935
Sundong Ahnabb64432019-10-22 13:58:29 +09002936 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002937 apexRule := module.Rule("apexRule")
2938 copyCmds := apexRule.Args["copy_commands"]
2939
2940 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2941 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002942}
2943
Dario Freni6f3937c2019-12-20 22:58:03 +00002944func TestApexWithTestHelperApp(t *testing.T) {
2945 ctx, _ := testApex(t, `
2946 apex {
2947 name: "myapex",
2948 key: "myapex.key",
2949 apps: [
2950 "TesterHelpAppFoo",
2951 ],
2952 }
2953
2954 apex_key {
2955 name: "myapex.key",
2956 public_key: "testkey.avbpubkey",
2957 private_key: "testkey.pem",
2958 }
2959
2960 android_test_helper_app {
2961 name: "TesterHelpAppFoo",
2962 srcs: ["foo/bar/MyClass.java"],
2963 }
2964
2965 `)
2966
2967 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2968 apexRule := module.Rule("apexRule")
2969 copyCmds := apexRule.Args["copy_commands"]
2970
2971 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
2972}
2973
Jooyung Han18020ea2019-11-13 10:50:48 +09002974func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2975 // libfoo's apex_available comes from cc_defaults
2976 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2977 apex {
2978 name: "myapex",
2979 key: "myapex.key",
2980 native_shared_libs: ["libfoo"],
2981 }
2982
2983 apex_key {
2984 name: "myapex.key",
2985 public_key: "testkey.avbpubkey",
2986 private_key: "testkey.pem",
2987 }
2988
2989 apex {
2990 name: "otherapex",
2991 key: "myapex.key",
2992 native_shared_libs: ["libfoo"],
2993 }
2994
2995 cc_defaults {
2996 name: "libfoo-defaults",
2997 apex_available: ["otherapex"],
2998 }
2999
3000 cc_library {
3001 name: "libfoo",
3002 defaults: ["libfoo-defaults"],
3003 stl: "none",
3004 system_shared_libs: [],
3005 }`)
3006}
3007
Jiyong Park127b40b2019-09-30 16:04:35 +09003008func TestApexAvailable(t *testing.T) {
3009 // libfoo is not available to myapex, but only to otherapex
3010 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3011 apex {
3012 name: "myapex",
3013 key: "myapex.key",
3014 native_shared_libs: ["libfoo"],
3015 }
3016
3017 apex_key {
3018 name: "myapex.key",
3019 public_key: "testkey.avbpubkey",
3020 private_key: "testkey.pem",
3021 }
3022
3023 apex {
3024 name: "otherapex",
3025 key: "otherapex.key",
3026 native_shared_libs: ["libfoo"],
3027 }
3028
3029 apex_key {
3030 name: "otherapex.key",
3031 public_key: "testkey.avbpubkey",
3032 private_key: "testkey.pem",
3033 }
3034
3035 cc_library {
3036 name: "libfoo",
3037 stl: "none",
3038 system_shared_libs: [],
3039 apex_available: ["otherapex"],
3040 }`)
3041
3042 // libbar is an indirect dep
3043 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3044 apex {
3045 name: "myapex",
3046 key: "myapex.key",
3047 native_shared_libs: ["libfoo"],
3048 }
3049
3050 apex_key {
3051 name: "myapex.key",
3052 public_key: "testkey.avbpubkey",
3053 private_key: "testkey.pem",
3054 }
3055
3056 apex {
3057 name: "otherapex",
3058 key: "otherapex.key",
3059 native_shared_libs: ["libfoo"],
3060 }
3061
3062 apex_key {
3063 name: "otherapex.key",
3064 public_key: "testkey.avbpubkey",
3065 private_key: "testkey.pem",
3066 }
3067
3068 cc_library {
3069 name: "libfoo",
3070 stl: "none",
3071 shared_libs: ["libbar"],
3072 system_shared_libs: [],
3073 apex_available: ["myapex", "otherapex"],
3074 }
3075
3076 cc_library {
3077 name: "libbar",
3078 stl: "none",
3079 system_shared_libs: [],
3080 apex_available: ["otherapex"],
3081 }`)
3082
3083 testApexError(t, "\"otherapex\" is not a valid module name", `
3084 apex {
3085 name: "myapex",
3086 key: "myapex.key",
3087 native_shared_libs: ["libfoo"],
3088 }
3089
3090 apex_key {
3091 name: "myapex.key",
3092 public_key: "testkey.avbpubkey",
3093 private_key: "testkey.pem",
3094 }
3095
3096 cc_library {
3097 name: "libfoo",
3098 stl: "none",
3099 system_shared_libs: [],
3100 apex_available: ["otherapex"],
3101 }`)
3102
3103 ctx, _ := testApex(t, `
3104 apex {
3105 name: "myapex",
3106 key: "myapex.key",
3107 native_shared_libs: ["libfoo", "libbar"],
3108 }
3109
3110 apex_key {
3111 name: "myapex.key",
3112 public_key: "testkey.avbpubkey",
3113 private_key: "testkey.pem",
3114 }
3115
3116 cc_library {
3117 name: "libfoo",
3118 stl: "none",
3119 system_shared_libs: [],
3120 apex_available: ["myapex"],
3121 }
3122
3123 cc_library {
3124 name: "libbar",
3125 stl: "none",
3126 system_shared_libs: [],
3127 apex_available: ["//apex_available:anyapex"],
3128 }`)
3129
3130 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003131 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3132 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3133 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3134 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003135
3136 ctx, _ = testApex(t, `
3137 apex {
3138 name: "myapex",
3139 key: "myapex.key",
3140 }
3141
3142 apex_key {
3143 name: "myapex.key",
3144 public_key: "testkey.avbpubkey",
3145 private_key: "testkey.pem",
3146 }
3147
3148 cc_library {
3149 name: "libfoo",
3150 stl: "none",
3151 system_shared_libs: [],
3152 apex_available: ["//apex_available:platform"],
3153 }`)
3154
3155 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003156 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3157 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003158
3159 ctx, _ = testApex(t, `
3160 apex {
3161 name: "myapex",
3162 key: "myapex.key",
3163 native_shared_libs: ["libfoo"],
3164 }
3165
3166 apex_key {
3167 name: "myapex.key",
3168 public_key: "testkey.avbpubkey",
3169 private_key: "testkey.pem",
3170 }
3171
3172 cc_library {
3173 name: "libfoo",
3174 stl: "none",
3175 system_shared_libs: [],
3176 apex_available: ["myapex"],
3177 static: {
3178 apex_available: ["//apex_available:platform"],
3179 },
3180 }`)
3181
3182 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08003183 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3184 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003185 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08003186 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3187 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003188}
3189
Jiyong Park5d790c32019-11-15 18:40:32 +09003190func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003191 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003192 apex {
3193 name: "myapex",
3194 key: "myapex.key",
3195 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003196 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003197 }
3198
3199 override_apex {
3200 name: "override_myapex",
3201 base: "myapex",
3202 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003203 overrides: ["unknownapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003204 }
3205
3206 apex_key {
3207 name: "myapex.key",
3208 public_key: "testkey.avbpubkey",
3209 private_key: "testkey.pem",
3210 }
3211
3212 android_app {
3213 name: "app",
3214 srcs: ["foo/bar/MyClass.java"],
3215 package_name: "foo",
3216 sdk_version: "none",
3217 system_modules: "none",
3218 }
3219
3220 override_android_app {
3221 name: "override_app",
3222 base: "app",
3223 package_name: "bar",
3224 }
3225 `)
3226
Jiyong Park317645e2019-12-05 13:20:58 +09003227 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3228 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3229 if originalVariant.GetOverriddenBy() != "" {
3230 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3231 }
3232 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3233 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3234 }
3235
Jiyong Park5d790c32019-11-15 18:40:32 +09003236 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3237 apexRule := module.Rule("apexRule")
3238 copyCmds := apexRule.Args["copy_commands"]
3239
3240 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3241 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003242
3243 apexBundle := module.Module().(*apexBundle)
3244 name := apexBundle.Name()
3245 if name != "override_myapex" {
3246 t.Errorf("name should be \"override_myapex\", but was %q", name)
3247 }
3248
3249 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3250 var builder strings.Builder
3251 data.Custom(&builder, name, "TARGET_", "", data)
3252 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003253 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003254 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3255 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003256 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003257 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003258 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003259 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3260 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003261}
3262
Jooyung Han214bf372019-11-12 13:03:50 +09003263func TestLegacyAndroid10Support(t *testing.T) {
3264 ctx, _ := testApex(t, `
3265 apex {
3266 name: "myapex",
3267 key: "myapex.key",
3268 legacy_android10_support: true,
3269 }
3270
3271 apex_key {
3272 name: "myapex.key",
3273 public_key: "testkey.avbpubkey",
3274 private_key: "testkey.pem",
3275 }
3276 `)
3277
3278 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3279 args := module.Rule("apexRule").Args
3280 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
3281}
3282
Jooyung Han58f26ab2019-12-18 15:34:32 +09003283func TestJavaSDKLibrary(t *testing.T) {
3284 ctx, _ := testApex(t, `
3285 apex {
3286 name: "myapex",
3287 key: "myapex.key",
3288 java_libs: ["foo"],
3289 }
3290
3291 apex_key {
3292 name: "myapex.key",
3293 public_key: "testkey.avbpubkey",
3294 private_key: "testkey.pem",
3295 }
3296
3297 java_sdk_library {
3298 name: "foo",
3299 srcs: ["a.java"],
3300 api_packages: ["foo"],
3301 }
3302 `, withFiles(map[string][]byte{
3303 "api/current.txt": nil,
3304 "api/removed.txt": nil,
3305 "api/system-current.txt": nil,
3306 "api/system-removed.txt": nil,
3307 "api/test-current.txt": nil,
3308 "api/test-removed.txt": nil,
3309 }))
3310
3311 // java_sdk_library installs both impl jar and permission XML
3312 ensureExactContents(t, ctx, "myapex", []string{
3313 "javalib/foo.jar",
3314 "etc/permissions/foo.xml",
3315 })
3316 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jooyung Han624058e2019-12-24 18:38:06 +09003317 xml := ctx.ModuleForTests("foo", "android_common_myapex").Output("foo.xml")
3318 ensureContains(t, xml.Args["content"], `<library name="foo" file="/apex/myapex/javalib/foo.jar"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003319}
3320
Jiyong Park479321d2019-12-16 11:47:12 +09003321func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3322 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3323 apex {
3324 name: "myapex",
3325 key: "myapex.key",
3326 java_libs: ["myjar"],
3327 }
3328
3329 apex_key {
3330 name: "myapex.key",
3331 public_key: "testkey.avbpubkey",
3332 private_key: "testkey.pem",
3333 }
3334
3335 java_library {
3336 name: "myjar",
3337 srcs: ["foo/bar/MyClass.java"],
3338 sdk_version: "none",
3339 system_modules: "none",
3340 }
3341 `)
3342}
3343
Jiyong Park7afd1072019-12-30 16:56:33 +09003344func TestCarryRequiredModuleNames(t *testing.T) {
3345 ctx, config := testApex(t, `
3346 apex {
3347 name: "myapex",
3348 key: "myapex.key",
3349 native_shared_libs: ["mylib"],
3350 }
3351
3352 apex_key {
3353 name: "myapex.key",
3354 public_key: "testkey.avbpubkey",
3355 private_key: "testkey.pem",
3356 }
3357
3358 cc_library {
3359 name: "mylib",
3360 srcs: ["mylib.cpp"],
3361 system_shared_libs: [],
3362 stl: "none",
3363 required: ["a", "b"],
3364 host_required: ["c", "d"],
3365 target_required: ["e", "f"],
3366 }
3367 `)
3368
3369 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3370 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3371 name := apexBundle.BaseModuleName()
3372 prefix := "TARGET_"
3373 var builder strings.Builder
3374 data.Custom(&builder, name, prefix, "", data)
3375 androidMk := builder.String()
3376 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3377 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3378 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3379}
3380
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003381func TestMain(m *testing.M) {
3382 run := func() int {
3383 setUp()
3384 defer tearDown()
3385
3386 return m.Run()
3387 }
3388
3389 os.Exit(run())
3390}