blob: cc346e99b207b1575073ed99a26456ab9e3adc6a [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,
222 "system/sepolicy/apex/otherapex-file_contexts": nil,
223 "system/sepolicy/apex/commonapex-file_contexts": nil,
224 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800225 "mylib.cpp": nil,
226 "mylib_common.cpp": nil,
227 "mytest.cpp": nil,
228 "mytest1.cpp": nil,
229 "mytest2.cpp": nil,
230 "mytest3.cpp": nil,
231 "myprebuilt": nil,
232 "my_include": nil,
233 "foo/bar/MyClass.java": nil,
234 "prebuilt.jar": nil,
235 "vendor/foo/devkeys/test.x509.pem": nil,
236 "vendor/foo/devkeys/test.pk8": nil,
237 "testkey.x509.pem": nil,
238 "testkey.pk8": nil,
239 "testkey.override.x509.pem": nil,
240 "testkey.override.pk8": nil,
241 "vendor/foo/devkeys/testkey.avbpubkey": nil,
242 "vendor/foo/devkeys/testkey.pem": nil,
243 "NOTICE": nil,
244 "custom_notice": nil,
245 "testkey2.avbpubkey": nil,
246 "testkey2.pem": nil,
247 "myapex-arm64.apex": nil,
248 "myapex-arm.apex": nil,
249 "frameworks/base/api/current.txt": nil,
250 "framework/aidl/a.aidl": nil,
251 "build/make/core/proguard.flags": nil,
252 "build/make/core/proguard_basic_keeps.flags": nil,
253 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900254 }
255
256 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800257 // The fs now needs to be populated before creating the config, call handlers twice
258 // for now, once to get any fs changes, and later after the config was created to
259 // set product variables or targets.
260 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
261 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900262 }
263
Colin Cross98be1bb2019-12-13 20:41:13 -0800264 config := android.TestArchConfig(buildDir, nil, bp, fs)
265 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
266 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
267 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
268 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
269 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
270 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
271
272 for _, handler := range handlers {
273 // The fs now needs to be populated before creating the config, call handlers twice
274 // for now, earlier to get any fs changes, and now after the config was created to
275 // set product variables or targets.
276 tempFS := map[string][]byte{}
277 handler(tempFS, config)
278 }
279
280 ctx := android.NewTestArchContext()
281 ctx.RegisterModuleType("apex", BundleFactory)
282 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
283 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
284 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
285 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
286 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
287 ctx.RegisterModuleType("override_apex", overrideApexFactory)
288
Paul Duffin77980a82019-12-19 16:01:36 +0000289 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800290 ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800291 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800292 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
293 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800294 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
295 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800296 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000297 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000298 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000299 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900300 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800301
302 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800303 ctx.PreDepsMutators(RegisterPreDepsMutators)
304 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
305 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800306
307 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900308
Jooyung Han5c998b92019-06-27 11:30:33 +0900309 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900310}
311
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700312func setUp() {
313 var err error
314 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900315 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700316 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900317 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900318}
319
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700320func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900321 os.RemoveAll(buildDir)
322}
323
324// ensure that 'result' contains 'expected'
325func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900326 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900327 if !strings.Contains(result, expected) {
328 t.Errorf("%q is not found in %q", expected, result)
329 }
330}
331
332// ensures that 'result' does not contain 'notExpected'
333func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900334 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900335 if strings.Contains(result, notExpected) {
336 t.Errorf("%q is found in %q", notExpected, result)
337 }
338}
339
340func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900341 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900342 if !android.InList(expected, result) {
343 t.Errorf("%q is not found in %v", expected, result)
344 }
345}
346
347func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900348 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900349 if android.InList(notExpected, result) {
350 t.Errorf("%q is found in %v", notExpected, result)
351 }
352}
353
Jooyung Hane1633032019-08-01 17:41:43 +0900354func ensureListEmpty(t *testing.T, result []string) {
355 t.Helper()
356 if len(result) > 0 {
357 t.Errorf("%q is expected to be empty", result)
358 }
359}
360
Jiyong Park25fc6a92018-11-18 18:02:45 +0900361// Minimal test
362func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700363 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900364 apex_defaults {
365 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900366 manifest: ":myapex.manifest",
367 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900368 key: "myapex.key",
369 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800370 multilib: {
371 both: {
372 binaries: ["foo",],
373 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900374 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900375 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900376 }
377
Jiyong Park30ca9372019-02-07 16:27:23 +0900378 apex {
379 name: "myapex",
380 defaults: ["myapex-defaults"],
381 }
382
Jiyong Park25fc6a92018-11-18 18:02:45 +0900383 apex_key {
384 name: "myapex.key",
385 public_key: "testkey.avbpubkey",
386 private_key: "testkey.pem",
387 }
388
Jiyong Park809bb722019-02-13 21:33:49 +0900389 filegroup {
390 name: "myapex.manifest",
391 srcs: ["apex_manifest.json"],
392 }
393
394 filegroup {
395 name: "myapex.androidmanifest",
396 srcs: ["AndroidManifest.xml"],
397 }
398
Jiyong Park25fc6a92018-11-18 18:02:45 +0900399 cc_library {
400 name: "mylib",
401 srcs: ["mylib.cpp"],
402 shared_libs: ["mylib2"],
403 system_shared_libs: [],
404 stl: "none",
405 }
406
Alex Light3d673592019-01-18 14:37:31 -0800407 cc_binary {
408 name: "foo",
409 srcs: ["mylib.cpp"],
410 compile_multilib: "both",
411 multilib: {
412 lib32: {
413 suffix: "32",
414 },
415 lib64: {
416 suffix: "64",
417 },
418 },
419 symlinks: ["foo_link_"],
420 symlink_preferred_arch: true,
421 system_shared_libs: [],
422 static_executable: true,
423 stl: "none",
424 }
425
Jiyong Park25fc6a92018-11-18 18:02:45 +0900426 cc_library {
427 name: "mylib2",
428 srcs: ["mylib.cpp"],
429 system_shared_libs: [],
430 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900431 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900432 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900433
434 java_library {
435 name: "myjar",
436 srcs: ["foo/bar/MyClass.java"],
437 sdk_version: "none",
438 system_modules: "none",
439 compile_dex: true,
440 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900441 libs: ["mysharedjar"],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900442 }
443
444 java_library {
445 name: "myotherjar",
446 srcs: ["foo/bar/MyClass.java"],
447 sdk_version: "none",
448 system_modules: "none",
449 compile_dex: true,
450 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900451
452 java_library {
453 name: "mysharedjar",
454 srcs: ["foo/bar/MyClass.java"],
455 sdk_version: "none",
456 system_modules: "none",
457 compile_dex: true,
458 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900459 `)
460
Sundong Ahnabb64432019-10-22 13:58:29 +0900461 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900462
463 optFlags := apexRule.Args["opt_flags"]
464 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700465 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900466 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900467
Jiyong Park25fc6a92018-11-18 18:02:45 +0900468 copyCmds := apexRule.Args["copy_commands"]
469
470 // Ensure that main rule creates an output
471 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
472
473 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800474 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900475 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900476
477 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800478 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900479 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900480
481 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800482 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
483 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900484 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
485 // .. but not for java libs
486 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900487 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800488
Colin Cross7113d202019-11-20 16:39:12 -0800489 // Ensure that the platform variant ends with _shared or _common
490 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
491 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900492 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
493 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900494 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
495
496 // Ensure that dynamic dependency to java libs are not included
497 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800498
499 // Ensure that all symlinks are present.
500 found_foo_link_64 := false
501 found_foo := false
502 for _, cmd := range strings.Split(copyCmds, " && ") {
503 if strings.HasPrefix(cmd, "ln -s foo64") {
504 if strings.HasSuffix(cmd, "bin/foo") {
505 found_foo = true
506 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
507 found_foo_link_64 = true
508 }
509 }
510 }
511 good := found_foo && found_foo_link_64
512 if !good {
513 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
514 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900515
Sundong Ahnabb64432019-10-22 13:58:29 +0900516 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700517 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700518 if len(noticeInputs) != 2 {
519 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900520 }
521 ensureListContains(t, noticeInputs, "NOTICE")
522 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800523}
524
Jooyung Hanf21c7972019-12-16 22:32:06 +0900525func TestDefaults(t *testing.T) {
526 ctx, _ := testApex(t, `
527 apex_defaults {
528 name: "myapex-defaults",
529 key: "myapex.key",
530 prebuilts: ["myetc"],
531 native_shared_libs: ["mylib"],
532 java_libs: ["myjar"],
533 apps: ["AppFoo"],
534 }
535
536 prebuilt_etc {
537 name: "myetc",
538 src: "myprebuilt",
539 }
540
541 apex {
542 name: "myapex",
543 defaults: ["myapex-defaults"],
544 }
545
546 apex_key {
547 name: "myapex.key",
548 public_key: "testkey.avbpubkey",
549 private_key: "testkey.pem",
550 }
551
552 cc_library {
553 name: "mylib",
554 system_shared_libs: [],
555 stl: "none",
556 }
557
558 java_library {
559 name: "myjar",
560 srcs: ["foo/bar/MyClass.java"],
561 sdk_version: "none",
562 system_modules: "none",
563 compile_dex: true,
564 }
565
566 android_app {
567 name: "AppFoo",
568 srcs: ["foo/bar/MyClass.java"],
569 sdk_version: "none",
570 system_modules: "none",
571 }
572 `)
573 ensureExactContents(t, ctx, "myapex", []string{
574 "etc/myetc",
575 "javalib/myjar.jar",
576 "lib64/mylib.so",
577 "app/AppFoo/AppFoo.apk",
578 })
579}
580
Jooyung Han01a3ee22019-11-02 02:52:25 +0900581func TestApexManifest(t *testing.T) {
582 ctx, _ := testApex(t, `
583 apex {
584 name: "myapex",
585 key: "myapex.key",
586 }
587
588 apex_key {
589 name: "myapex.key",
590 public_key: "testkey.avbpubkey",
591 private_key: "testkey.pem",
592 }
593 `)
594
595 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900596 args := module.Rule("apexRule").Args
597 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
598 t.Error("manifest should be apex_manifest.pb, but " + manifest)
599 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900600}
601
Alex Light5098a612018-11-29 17:12:15 -0800602func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700603 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800604 apex {
605 name: "myapex",
606 key: "myapex.key",
607 payload_type: "zip",
608 native_shared_libs: ["mylib"],
609 }
610
611 apex_key {
612 name: "myapex.key",
613 public_key: "testkey.avbpubkey",
614 private_key: "testkey.pem",
615 }
616
617 cc_library {
618 name: "mylib",
619 srcs: ["mylib.cpp"],
620 shared_libs: ["mylib2"],
621 system_shared_libs: [],
622 stl: "none",
623 }
624
625 cc_library {
626 name: "mylib2",
627 srcs: ["mylib.cpp"],
628 system_shared_libs: [],
629 stl: "none",
630 }
631 `)
632
Sundong Ahnabb64432019-10-22 13:58:29 +0900633 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800634 copyCmds := zipApexRule.Args["copy_commands"]
635
636 // Ensure that main rule creates an output
637 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
638
639 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800640 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800641
642 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800643 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800644
645 // Ensure that both direct and indirect deps are copied into apex
646 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
647 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900648}
649
650func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700651 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900652 apex {
653 name: "myapex",
654 key: "myapex.key",
655 native_shared_libs: ["mylib", "mylib3"],
656 }
657
658 apex_key {
659 name: "myapex.key",
660 public_key: "testkey.avbpubkey",
661 private_key: "testkey.pem",
662 }
663
664 cc_library {
665 name: "mylib",
666 srcs: ["mylib.cpp"],
667 shared_libs: ["mylib2", "mylib3"],
668 system_shared_libs: [],
669 stl: "none",
670 }
671
672 cc_library {
673 name: "mylib2",
674 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900675 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900676 system_shared_libs: [],
677 stl: "none",
678 stubs: {
679 versions: ["1", "2", "3"],
680 },
681 }
682
683 cc_library {
684 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900685 srcs: ["mylib.cpp"],
686 shared_libs: ["mylib4"],
687 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900688 stl: "none",
689 stubs: {
690 versions: ["10", "11", "12"],
691 },
692 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900693
694 cc_library {
695 name: "mylib4",
696 srcs: ["mylib.cpp"],
697 system_shared_libs: [],
698 stl: "none",
699 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900700 `)
701
Sundong Ahnabb64432019-10-22 13:58:29 +0900702 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900703 copyCmds := apexRule.Args["copy_commands"]
704
705 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800706 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900707
708 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800709 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900710
711 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800712 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900713
Colin Cross7113d202019-11-20 16:39:12 -0800714 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900715
716 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900717 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900718 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900719 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900720
721 // 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 -0800722 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900723 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800724 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900725
726 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800727 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900728 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900729
730 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900731 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900732
733 ensureExactContents(t, ctx, "myapex", []string{
734 "lib64/mylib.so",
735 "lib64/mylib3.so",
736 "lib64/mylib4.so",
737 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900738}
739
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900740func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700741 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900742 apex {
743 name: "myapex",
744 key: "myapex.key",
745 native_shared_libs: ["mylib"],
746 }
747
748 apex_key {
749 name: "myapex.key",
750 public_key: "testkey.avbpubkey",
751 private_key: "testkey.pem",
752 }
753
754 cc_library {
755 name: "mylib",
756 srcs: ["mylib.cpp"],
757 shared_libs: ["libfoo#10"],
758 system_shared_libs: [],
759 stl: "none",
760 }
761
762 cc_library {
763 name: "libfoo",
764 srcs: ["mylib.cpp"],
765 shared_libs: ["libbar"],
766 system_shared_libs: [],
767 stl: "none",
768 stubs: {
769 versions: ["10", "20", "30"],
770 },
771 }
772
773 cc_library {
774 name: "libbar",
775 srcs: ["mylib.cpp"],
776 system_shared_libs: [],
777 stl: "none",
778 }
779
780 `)
781
Sundong Ahnabb64432019-10-22 13:58:29 +0900782 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900783 copyCmds := apexRule.Args["copy_commands"]
784
785 // Ensure that direct non-stubs dep is always included
786 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
787
788 // Ensure that indirect stubs dep is not included
789 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
790
791 // Ensure that dependency of stubs is not included
792 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
793
Colin Cross7113d202019-11-20 16:39:12 -0800794 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900795
796 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900797 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900798 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900799 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900800
Jiyong Park3ff16992019-12-27 14:11:47 +0900801 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900802
803 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
804 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
805}
806
Jooyung Hand3639552019-08-09 12:57:43 +0900807func TestApexWithRuntimeLibsDependency(t *testing.T) {
808 /*
809 myapex
810 |
811 v (runtime_libs)
812 mylib ------+------> libfoo [provides stub]
813 |
814 `------> libbar
815 */
816 ctx, _ := testApex(t, `
817 apex {
818 name: "myapex",
819 key: "myapex.key",
820 native_shared_libs: ["mylib"],
821 }
822
823 apex_key {
824 name: "myapex.key",
825 public_key: "testkey.avbpubkey",
826 private_key: "testkey.pem",
827 }
828
829 cc_library {
830 name: "mylib",
831 srcs: ["mylib.cpp"],
832 runtime_libs: ["libfoo", "libbar"],
833 system_shared_libs: [],
834 stl: "none",
835 }
836
837 cc_library {
838 name: "libfoo",
839 srcs: ["mylib.cpp"],
840 system_shared_libs: [],
841 stl: "none",
842 stubs: {
843 versions: ["10", "20", "30"],
844 },
845 }
846
847 cc_library {
848 name: "libbar",
849 srcs: ["mylib.cpp"],
850 system_shared_libs: [],
851 stl: "none",
852 }
853
854 `)
855
Sundong Ahnabb64432019-10-22 13:58:29 +0900856 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900857 copyCmds := apexRule.Args["copy_commands"]
858
859 // Ensure that direct non-stubs dep is always included
860 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
861
862 // Ensure that indirect stubs dep is not included
863 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
864
865 // Ensure that runtime_libs dep in included
866 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
867
Sundong Ahnabb64432019-10-22 13:58:29 +0900868 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900869 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
870 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900871
872}
873
Jooyung Han9c80bae2019-08-20 17:30:57 +0900874func TestApexDependencyToLLNDK(t *testing.T) {
875 ctx, _ := testApex(t, `
876 apex {
877 name: "myapex",
878 key: "myapex.key",
879 use_vendor: true,
880 native_shared_libs: ["mylib"],
881 }
882
883 apex_key {
884 name: "myapex.key",
885 public_key: "testkey.avbpubkey",
886 private_key: "testkey.pem",
887 }
888
889 cc_library {
890 name: "mylib",
891 srcs: ["mylib.cpp"],
892 vendor_available: true,
893 shared_libs: ["libbar"],
894 system_shared_libs: [],
895 stl: "none",
896 }
897
898 cc_library {
899 name: "libbar",
900 srcs: ["mylib.cpp"],
901 system_shared_libs: [],
902 stl: "none",
903 }
904
905 llndk_library {
906 name: "libbar",
907 symbol_file: "",
908 }
Jooyung Handc782442019-11-01 03:14:38 +0900909 `, func(fs map[string][]byte, config android.Config) {
910 setUseVendorWhitelistForTest(config, []string{"myapex"})
911 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900912
Sundong Ahnabb64432019-10-22 13:58:29 +0900913 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900914 copyCmds := apexRule.Args["copy_commands"]
915
916 // Ensure that LLNDK dep is not included
917 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
918
Sundong Ahnabb64432019-10-22 13:58:29 +0900919 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900920 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900921
922 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900923 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900924
925}
926
Jiyong Park25fc6a92018-11-18 18:02:45 +0900927func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700928 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900929 apex {
930 name: "myapex",
931 key: "myapex.key",
932 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
933 }
934
935 apex_key {
936 name: "myapex.key",
937 public_key: "testkey.avbpubkey",
938 private_key: "testkey.pem",
939 }
940
941 cc_library {
942 name: "mylib",
943 srcs: ["mylib.cpp"],
944 shared_libs: ["libdl#27"],
945 stl: "none",
946 }
947
948 cc_library_shared {
949 name: "mylib_shared",
950 srcs: ["mylib.cpp"],
951 shared_libs: ["libdl#27"],
952 stl: "none",
953 }
954
955 cc_library {
956 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700957 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900958 nocrt: true,
959 system_shared_libs: [],
960 stl: "none",
961 stubs: {
962 versions: ["27", "28", "29"],
963 },
964 }
965
966 cc_library {
967 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700968 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900969 nocrt: true,
970 system_shared_libs: [],
971 stl: "none",
972 stubs: {
973 versions: ["27", "28", "29"],
974 },
975 }
976
977 cc_library {
978 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700979 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900980 nocrt: true,
981 system_shared_libs: [],
982 stl: "none",
983 stubs: {
984 versions: ["27", "28", "29"],
985 },
986 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900987
988 cc_library {
989 name: "libBootstrap",
990 srcs: ["mylib.cpp"],
991 stl: "none",
992 bootstrap: true,
993 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900994 `)
995
Sundong Ahnabb64432019-10-22 13:58:29 +0900996 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900997 copyCmds := apexRule.Args["copy_commands"]
998
999 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001000 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001001 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1002 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001003
1004 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001005 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001006
Colin Cross7113d202019-11-20 16:39:12 -08001007 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1008 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1009 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001010
1011 // For dependency to libc
1012 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001013 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001014 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001015 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001016 // ... Cflags from stub is correctly exported to mylib
1017 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1018 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1019
1020 // For dependency to libm
1021 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001022 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001023 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001024 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001025 // ... and is not compiling with the stub
1026 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1027 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1028
1029 // For dependency to libdl
1030 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001031 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001032 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001033 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1034 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001035 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001036 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001037 // ... Cflags from stub is correctly exported to mylib
1038 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1039 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001040
1041 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001042 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1043 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1044 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1045 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001046}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001047
1048func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001049 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001050 apex {
1051 name: "myapex",
1052 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001053 native_shared_libs: ["mylib"],
1054 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001055 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001056 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001057 }
1058
1059 apex_key {
1060 name: "myapex.key",
1061 public_key: "testkey.avbpubkey",
1062 private_key: "testkey.pem",
1063 }
1064
1065 prebuilt_etc {
1066 name: "myetc",
1067 src: "myprebuilt",
1068 sub_dir: "foo/bar",
1069 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001070
1071 cc_library {
1072 name: "mylib",
1073 srcs: ["mylib.cpp"],
1074 relative_install_path: "foo/bar",
1075 system_shared_libs: [],
1076 stl: "none",
1077 }
1078
1079 cc_binary {
1080 name: "mybin",
1081 srcs: ["mylib.cpp"],
1082 relative_install_path: "foo/bar",
1083 system_shared_libs: [],
1084 static_executable: true,
1085 stl: "none",
1086 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001087 `)
1088
Sundong Ahnabb64432019-10-22 13:58:29 +09001089 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001090 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1091
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001092 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001093 ensureListContains(t, dirs, "etc")
1094 ensureListContains(t, dirs, "etc/foo")
1095 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001096 ensureListContains(t, dirs, "lib64")
1097 ensureListContains(t, dirs, "lib64/foo")
1098 ensureListContains(t, dirs, "lib64/foo/bar")
1099 ensureListContains(t, dirs, "lib")
1100 ensureListContains(t, dirs, "lib/foo")
1101 ensureListContains(t, dirs, "lib/foo/bar")
1102
Jiyong Parkbd13e442019-03-15 18:10:35 +09001103 ensureListContains(t, dirs, "bin")
1104 ensureListContains(t, dirs, "bin/foo")
1105 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001106}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001107
1108func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001109 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001110 apex {
1111 name: "myapex",
1112 key: "myapex.key",
1113 native_shared_libs: ["mylib"],
1114 use_vendor: true,
1115 }
1116
1117 apex_key {
1118 name: "myapex.key",
1119 public_key: "testkey.avbpubkey",
1120 private_key: "testkey.pem",
1121 }
1122
1123 cc_library {
1124 name: "mylib",
1125 srcs: ["mylib.cpp"],
1126 shared_libs: ["mylib2"],
1127 system_shared_libs: [],
1128 vendor_available: true,
1129 stl: "none",
1130 }
1131
1132 cc_library {
1133 name: "mylib2",
1134 srcs: ["mylib.cpp"],
1135 system_shared_libs: [],
1136 vendor_available: true,
1137 stl: "none",
1138 }
Jooyung Handc782442019-11-01 03:14:38 +09001139 `, func(fs map[string][]byte, config android.Config) {
1140 setUseVendorWhitelistForTest(config, []string{"myapex"})
1141 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001142
1143 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001144 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001145 for _, implicit := range i.Implicits {
1146 inputsList = append(inputsList, implicit.String())
1147 }
1148 }
1149 inputsString := strings.Join(inputsList, " ")
1150
1151 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001152 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1153 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001154
1155 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001156 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1157 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001158}
Jiyong Park16e91a02018-12-20 18:18:08 +09001159
Jooyung Handc782442019-11-01 03:14:38 +09001160func TestUseVendorRestriction(t *testing.T) {
1161 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1162 apex {
1163 name: "myapex",
1164 key: "myapex.key",
1165 use_vendor: true,
1166 }
1167 apex_key {
1168 name: "myapex.key",
1169 public_key: "testkey.avbpubkey",
1170 private_key: "testkey.pem",
1171 }
1172 `, func(fs map[string][]byte, config android.Config) {
1173 setUseVendorWhitelistForTest(config, []string{""})
1174 })
1175 // no error with whitelist
1176 testApex(t, `
1177 apex {
1178 name: "myapex",
1179 key: "myapex.key",
1180 use_vendor: true,
1181 }
1182 apex_key {
1183 name: "myapex.key",
1184 public_key: "testkey.avbpubkey",
1185 private_key: "testkey.pem",
1186 }
1187 `, func(fs map[string][]byte, config android.Config) {
1188 setUseVendorWhitelistForTest(config, []string{"myapex"})
1189 })
1190}
1191
Jooyung Han5c998b92019-06-27 11:30:33 +09001192func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1193 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1194 apex {
1195 name: "myapex",
1196 key: "myapex.key",
1197 native_shared_libs: ["mylib"],
1198 use_vendor: true,
1199 }
1200
1201 apex_key {
1202 name: "myapex.key",
1203 public_key: "testkey.avbpubkey",
1204 private_key: "testkey.pem",
1205 }
1206
1207 cc_library {
1208 name: "mylib",
1209 srcs: ["mylib.cpp"],
1210 system_shared_libs: [],
1211 stl: "none",
1212 }
1213 `)
1214}
1215
Jiyong Park16e91a02018-12-20 18:18:08 +09001216func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001217 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001218 apex {
1219 name: "myapex",
1220 key: "myapex.key",
1221 native_shared_libs: ["mylib"],
1222 }
1223
1224 apex_key {
1225 name: "myapex.key",
1226 public_key: "testkey.avbpubkey",
1227 private_key: "testkey.pem",
1228 }
1229
1230 cc_library {
1231 name: "mylib",
1232 srcs: ["mylib.cpp"],
1233 system_shared_libs: [],
1234 stl: "none",
1235 stubs: {
1236 versions: ["1", "2", "3"],
1237 },
1238 }
1239
1240 cc_binary {
1241 name: "not_in_apex",
1242 srcs: ["mylib.cpp"],
1243 static_libs: ["mylib"],
1244 static_executable: true,
1245 system_shared_libs: [],
1246 stl: "none",
1247 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001248 `)
1249
Colin Cross7113d202019-11-20 16:39:12 -08001250 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001251
1252 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001253 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001254}
Jiyong Park9335a262018-12-24 11:31:58 +09001255
1256func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001257 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001258 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001259 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001260 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001261 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001262 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001263 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001264 }
1265
1266 cc_library {
1267 name: "mylib",
1268 srcs: ["mylib.cpp"],
1269 system_shared_libs: [],
1270 stl: "none",
1271 }
1272
1273 apex_key {
1274 name: "myapex.key",
1275 public_key: "testkey.avbpubkey",
1276 private_key: "testkey.pem",
1277 }
1278
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001279 android_app_certificate {
1280 name: "myapex.certificate",
1281 certificate: "testkey",
1282 }
1283
1284 android_app_certificate {
1285 name: "myapex.certificate.override",
1286 certificate: "testkey.override",
1287 }
1288
Jiyong Park9335a262018-12-24 11:31:58 +09001289 `)
1290
1291 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001292 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001293
1294 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1295 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1296 "vendor/foo/devkeys/testkey.avbpubkey")
1297 }
1298 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1299 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1300 "vendor/foo/devkeys/testkey.pem")
1301 }
1302
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001303 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001304 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001305 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001306 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001307 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001308 }
1309}
Jiyong Park58e364a2019-01-19 19:24:06 +09001310
Jooyung Hanf121a652019-12-17 14:30:11 +09001311func TestCertificate(t *testing.T) {
1312 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1313 ctx, _ := testApex(t, `
1314 apex {
1315 name: "myapex",
1316 key: "myapex.key",
1317 }
1318 apex_key {
1319 name: "myapex.key",
1320 public_key: "testkey.avbpubkey",
1321 private_key: "testkey.pem",
1322 }`)
1323 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1324 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1325 if actual := rule.Args["certificates"]; actual != expected {
1326 t.Errorf("certificates should be %q, not %q", expected, actual)
1327 }
1328 })
1329 t.Run("override when unspecified", func(t *testing.T) {
1330 ctx, _ := testApex(t, `
1331 apex {
1332 name: "myapex_keytest",
1333 key: "myapex.key",
1334 file_contexts: ":myapex-file_contexts",
1335 }
1336 apex_key {
1337 name: "myapex.key",
1338 public_key: "testkey.avbpubkey",
1339 private_key: "testkey.pem",
1340 }
1341 android_app_certificate {
1342 name: "myapex.certificate.override",
1343 certificate: "testkey.override",
1344 }`)
1345 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1346 expected := "testkey.override.x509.pem testkey.override.pk8"
1347 if actual := rule.Args["certificates"]; actual != expected {
1348 t.Errorf("certificates should be %q, not %q", expected, actual)
1349 }
1350 })
1351 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1352 ctx, _ := testApex(t, `
1353 apex {
1354 name: "myapex",
1355 key: "myapex.key",
1356 certificate: ":myapex.certificate",
1357 }
1358 apex_key {
1359 name: "myapex.key",
1360 public_key: "testkey.avbpubkey",
1361 private_key: "testkey.pem",
1362 }
1363 android_app_certificate {
1364 name: "myapex.certificate",
1365 certificate: "testkey",
1366 }`)
1367 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1368 expected := "testkey.x509.pem testkey.pk8"
1369 if actual := rule.Args["certificates"]; actual != expected {
1370 t.Errorf("certificates should be %q, not %q", expected, actual)
1371 }
1372 })
1373 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1374 ctx, _ := testApex(t, `
1375 apex {
1376 name: "myapex_keytest",
1377 key: "myapex.key",
1378 file_contexts: ":myapex-file_contexts",
1379 certificate: ":myapex.certificate",
1380 }
1381 apex_key {
1382 name: "myapex.key",
1383 public_key: "testkey.avbpubkey",
1384 private_key: "testkey.pem",
1385 }
1386 android_app_certificate {
1387 name: "myapex.certificate.override",
1388 certificate: "testkey.override",
1389 }`)
1390 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1391 expected := "testkey.override.x509.pem testkey.override.pk8"
1392 if actual := rule.Args["certificates"]; actual != expected {
1393 t.Errorf("certificates should be %q, not %q", expected, actual)
1394 }
1395 })
1396 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1397 ctx, _ := testApex(t, `
1398 apex {
1399 name: "myapex",
1400 key: "myapex.key",
1401 certificate: "testkey",
1402 }
1403 apex_key {
1404 name: "myapex.key",
1405 public_key: "testkey.avbpubkey",
1406 private_key: "testkey.pem",
1407 }`)
1408 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1409 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1410 if actual := rule.Args["certificates"]; actual != expected {
1411 t.Errorf("certificates should be %q, not %q", expected, actual)
1412 }
1413 })
1414 t.Run("override when specified as <name>", func(t *testing.T) {
1415 ctx, _ := testApex(t, `
1416 apex {
1417 name: "myapex_keytest",
1418 key: "myapex.key",
1419 file_contexts: ":myapex-file_contexts",
1420 certificate: "testkey",
1421 }
1422 apex_key {
1423 name: "myapex.key",
1424 public_key: "testkey.avbpubkey",
1425 private_key: "testkey.pem",
1426 }
1427 android_app_certificate {
1428 name: "myapex.certificate.override",
1429 certificate: "testkey.override",
1430 }`)
1431 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1432 expected := "testkey.override.x509.pem testkey.override.pk8"
1433 if actual := rule.Args["certificates"]; actual != expected {
1434 t.Errorf("certificates should be %q, not %q", expected, actual)
1435 }
1436 })
1437}
1438
Jiyong Park58e364a2019-01-19 19:24:06 +09001439func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001440 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001441 apex {
1442 name: "myapex",
1443 key: "myapex.key",
1444 native_shared_libs: ["mylib"],
1445 }
1446
1447 apex {
1448 name: "otherapex",
1449 key: "myapex.key",
1450 native_shared_libs: ["mylib"],
1451 }
1452
1453 apex_key {
1454 name: "myapex.key",
1455 public_key: "testkey.avbpubkey",
1456 private_key: "testkey.pem",
1457 }
1458
1459 cc_library {
1460 name: "mylib",
1461 srcs: ["mylib.cpp"],
1462 system_shared_libs: [],
1463 stl: "none",
1464 }
1465 `)
1466
Jooyung Han6b8459b2019-10-30 08:29:25 +09001467 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001468 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001469 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001470 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1471 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001472
Jooyung Han6b8459b2019-10-30 08:29:25 +09001473 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001474 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001475 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001476 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1477 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001478
Jooyung Han6b8459b2019-10-30 08:29:25 +09001479 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001480 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001481 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001482 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1483 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001484}
Jiyong Park7e636d02019-01-28 16:16:54 +09001485
1486func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001487 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001488 apex {
1489 name: "myapex",
1490 key: "myapex.key",
1491 native_shared_libs: ["mylib"],
1492 }
1493
1494 apex_key {
1495 name: "myapex.key",
1496 public_key: "testkey.avbpubkey",
1497 private_key: "testkey.pem",
1498 }
1499
1500 cc_library_headers {
1501 name: "mylib_headers",
1502 export_include_dirs: ["my_include"],
1503 system_shared_libs: [],
1504 stl: "none",
1505 }
1506
1507 cc_library {
1508 name: "mylib",
1509 srcs: ["mylib.cpp"],
1510 system_shared_libs: [],
1511 stl: "none",
1512 header_libs: ["mylib_headers"],
1513 export_header_lib_headers: ["mylib_headers"],
1514 stubs: {
1515 versions: ["1", "2", "3"],
1516 },
1517 }
1518
1519 cc_library {
1520 name: "otherlib",
1521 srcs: ["mylib.cpp"],
1522 system_shared_libs: [],
1523 stl: "none",
1524 shared_libs: ["mylib"],
1525 }
1526 `)
1527
Colin Cross7113d202019-11-20 16:39:12 -08001528 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001529
1530 // Ensure that the include path of the header lib is exported to 'otherlib'
1531 ensureContains(t, cFlags, "-Imy_include")
1532}
Alex Light9670d332019-01-29 18:07:33 -08001533
Jooyung Han31c470b2019-10-18 16:26:59 +09001534func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1535 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001536 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001537 copyCmds := apexRule.Args["copy_commands"]
1538 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001539 var failed bool
1540 var surplus []string
1541 filesMatched := make(map[string]bool)
1542 addContent := func(content string) {
1543 for _, expected := range files {
1544 if matched, _ := path.Match(expected, content); matched {
1545 filesMatched[expected] = true
1546 return
1547 }
1548 }
1549 surplus = append(surplus, content)
1550 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001551 for _, cmd := range strings.Split(copyCmds, "&&") {
1552 cmd = strings.TrimSpace(cmd)
1553 if cmd == "" {
1554 continue
1555 }
1556 terms := strings.Split(cmd, " ")
1557 switch terms[0] {
1558 case "mkdir":
1559 case "cp":
1560 if len(terms) != 3 {
1561 t.Fatal("copyCmds contains invalid cp command", cmd)
1562 }
1563 dst := terms[2]
1564 index := strings.Index(dst, imageApexDir)
1565 if index == -1 {
1566 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1567 }
1568 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001569 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001570 default:
1571 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1572 }
1573 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001574
Jooyung Han31c470b2019-10-18 16:26:59 +09001575 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001576 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001577 t.Log("surplus files", surplus)
1578 failed = true
1579 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001580
1581 if len(files) > len(filesMatched) {
1582 var missing []string
1583 for _, expected := range files {
1584 if !filesMatched[expected] {
1585 missing = append(missing, expected)
1586 }
1587 }
1588 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001589 t.Log("missing files", missing)
1590 failed = true
1591 }
1592 if failed {
1593 t.Fail()
1594 }
1595}
1596
Jooyung Han344d5432019-08-23 11:17:39 +09001597func TestVndkApexCurrent(t *testing.T) {
1598 ctx, _ := testApex(t, `
1599 apex_vndk {
1600 name: "myapex",
1601 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001602 }
1603
1604 apex_key {
1605 name: "myapex.key",
1606 public_key: "testkey.avbpubkey",
1607 private_key: "testkey.pem",
1608 }
1609
1610 cc_library {
1611 name: "libvndk",
1612 srcs: ["mylib.cpp"],
1613 vendor_available: true,
1614 vndk: {
1615 enabled: true,
1616 },
1617 system_shared_libs: [],
1618 stl: "none",
1619 }
1620
1621 cc_library {
1622 name: "libvndksp",
1623 srcs: ["mylib.cpp"],
1624 vendor_available: true,
1625 vndk: {
1626 enabled: true,
1627 support_system_process: true,
1628 },
1629 system_shared_libs: [],
1630 stl: "none",
1631 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001632 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001633
Jooyung Han31c470b2019-10-18 16:26:59 +09001634 ensureExactContents(t, ctx, "myapex", []string{
1635 "lib/libvndk.so",
1636 "lib/libvndksp.so",
1637 "lib64/libvndk.so",
1638 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001639 "etc/llndk.libraries.VER.txt",
1640 "etc/vndkcore.libraries.VER.txt",
1641 "etc/vndksp.libraries.VER.txt",
1642 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001643 })
Jooyung Han344d5432019-08-23 11:17:39 +09001644}
1645
1646func TestVndkApexWithPrebuilt(t *testing.T) {
1647 ctx, _ := testApex(t, `
1648 apex_vndk {
1649 name: "myapex",
1650 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001651 }
1652
1653 apex_key {
1654 name: "myapex.key",
1655 public_key: "testkey.avbpubkey",
1656 private_key: "testkey.pem",
1657 }
1658
1659 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001660 name: "libvndk",
1661 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001662 vendor_available: true,
1663 vndk: {
1664 enabled: true,
1665 },
1666 system_shared_libs: [],
1667 stl: "none",
1668 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001669
1670 cc_prebuilt_library_shared {
1671 name: "libvndk.arm",
1672 srcs: ["libvndk.arm.so"],
1673 vendor_available: true,
1674 vndk: {
1675 enabled: true,
1676 },
1677 enabled: false,
1678 arch: {
1679 arm: {
1680 enabled: true,
1681 },
1682 },
1683 system_shared_libs: [],
1684 stl: "none",
1685 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001686 `+vndkLibrariesTxtFiles("current"),
1687 withFiles(map[string][]byte{
1688 "libvndk.so": nil,
1689 "libvndk.arm.so": nil,
1690 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001691
Jooyung Han31c470b2019-10-18 16:26:59 +09001692 ensureExactContents(t, ctx, "myapex", []string{
1693 "lib/libvndk.so",
1694 "lib/libvndk.arm.so",
1695 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001696 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001697 })
Jooyung Han344d5432019-08-23 11:17:39 +09001698}
1699
Jooyung Han39edb6c2019-11-06 16:53:07 +09001700func vndkLibrariesTxtFiles(vers ...string) (result string) {
1701 for _, v := range vers {
1702 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001703 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001704 result += `
1705 vndk_libraries_txt {
1706 name: "` + txt + `.libraries.txt",
1707 }
1708 `
1709 }
1710 } else {
1711 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1712 result += `
1713 prebuilt_etc {
1714 name: "` + txt + `.libraries.` + v + `.txt",
1715 src: "dummy.txt",
1716 }
1717 `
1718 }
1719 }
1720 }
1721 return
1722}
1723
Jooyung Han344d5432019-08-23 11:17:39 +09001724func TestVndkApexVersion(t *testing.T) {
1725 ctx, _ := testApex(t, `
1726 apex_vndk {
1727 name: "myapex_v27",
1728 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001729 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001730 vndk_version: "27",
1731 }
1732
1733 apex_key {
1734 name: "myapex.key",
1735 public_key: "testkey.avbpubkey",
1736 private_key: "testkey.pem",
1737 }
1738
Jooyung Han31c470b2019-10-18 16:26:59 +09001739 vndk_prebuilt_shared {
1740 name: "libvndk27",
1741 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001742 vendor_available: true,
1743 vndk: {
1744 enabled: true,
1745 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001746 target_arch: "arm64",
1747 arch: {
1748 arm: {
1749 srcs: ["libvndk27_arm.so"],
1750 },
1751 arm64: {
1752 srcs: ["libvndk27_arm64.so"],
1753 },
1754 },
Jooyung Han344d5432019-08-23 11:17:39 +09001755 }
1756
1757 vndk_prebuilt_shared {
1758 name: "libvndk27",
1759 version: "27",
1760 vendor_available: true,
1761 vndk: {
1762 enabled: true,
1763 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001764 target_arch: "x86_64",
1765 arch: {
1766 x86: {
1767 srcs: ["libvndk27_x86.so"],
1768 },
1769 x86_64: {
1770 srcs: ["libvndk27_x86_64.so"],
1771 },
1772 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001773 }
1774 `+vndkLibrariesTxtFiles("27"),
1775 withFiles(map[string][]byte{
1776 "libvndk27_arm.so": nil,
1777 "libvndk27_arm64.so": nil,
1778 "libvndk27_x86.so": nil,
1779 "libvndk27_x86_64.so": nil,
1780 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001781
Jooyung Han31c470b2019-10-18 16:26:59 +09001782 ensureExactContents(t, ctx, "myapex_v27", []string{
1783 "lib/libvndk27_arm.so",
1784 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001785 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001786 })
Jooyung Han344d5432019-08-23 11:17:39 +09001787}
1788
1789func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1790 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1791 apex_vndk {
1792 name: "myapex_v27",
1793 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001794 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001795 vndk_version: "27",
1796 }
1797 apex_vndk {
1798 name: "myapex_v27_other",
1799 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001800 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001801 vndk_version: "27",
1802 }
1803
1804 apex_key {
1805 name: "myapex.key",
1806 public_key: "testkey.avbpubkey",
1807 private_key: "testkey.pem",
1808 }
1809
1810 cc_library {
1811 name: "libvndk",
1812 srcs: ["mylib.cpp"],
1813 vendor_available: true,
1814 vndk: {
1815 enabled: true,
1816 },
1817 system_shared_libs: [],
1818 stl: "none",
1819 }
1820
1821 vndk_prebuilt_shared {
1822 name: "libvndk",
1823 version: "27",
1824 vendor_available: true,
1825 vndk: {
1826 enabled: true,
1827 },
1828 srcs: ["libvndk.so"],
1829 }
1830 `, withFiles(map[string][]byte{
1831 "libvndk.so": nil,
1832 }))
1833}
1834
Jooyung Han90eee022019-10-01 20:02:42 +09001835func TestVndkApexNameRule(t *testing.T) {
1836 ctx, _ := testApex(t, `
1837 apex_vndk {
1838 name: "myapex",
1839 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001840 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001841 }
1842 apex_vndk {
1843 name: "myapex_v28",
1844 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001845 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001846 vndk_version: "28",
1847 }
1848 apex_key {
1849 name: "myapex.key",
1850 public_key: "testkey.avbpubkey",
1851 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001852 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001853
1854 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001855 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001856 actual := proptools.String(bundle.properties.Apex_name)
1857 if !reflect.DeepEqual(actual, expected) {
1858 t.Errorf("Got '%v', expected '%v'", actual, expected)
1859 }
1860 }
1861
1862 assertApexName("com.android.vndk.vVER", "myapex")
1863 assertApexName("com.android.vndk.v28", "myapex_v28")
1864}
1865
Jooyung Han344d5432019-08-23 11:17:39 +09001866func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1867 ctx, _ := testApex(t, `
1868 apex_vndk {
1869 name: "myapex",
1870 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001871 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001872 }
1873
1874 apex_key {
1875 name: "myapex.key",
1876 public_key: "testkey.avbpubkey",
1877 private_key: "testkey.pem",
1878 }
1879
1880 cc_library {
1881 name: "libvndk",
1882 srcs: ["mylib.cpp"],
1883 vendor_available: true,
1884 native_bridge_supported: true,
1885 host_supported: true,
1886 vndk: {
1887 enabled: true,
1888 },
1889 system_shared_libs: [],
1890 stl: "none",
1891 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001892 `+vndkLibrariesTxtFiles("current"),
1893 withTargets(map[android.OsType][]android.Target{
1894 android.Android: []android.Target{
1895 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1896 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1897 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1898 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1899 },
1900 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001901
Jooyung Han31c470b2019-10-18 16:26:59 +09001902 ensureExactContents(t, ctx, "myapex", []string{
1903 "lib/libvndk.so",
1904 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001905 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001906 })
Jooyung Han344d5432019-08-23 11:17:39 +09001907}
1908
1909func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1910 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1911 apex_vndk {
1912 name: "myapex",
1913 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001914 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001915 native_bridge_supported: true,
1916 }
1917
1918 apex_key {
1919 name: "myapex.key",
1920 public_key: "testkey.avbpubkey",
1921 private_key: "testkey.pem",
1922 }
1923
1924 cc_library {
1925 name: "libvndk",
1926 srcs: ["mylib.cpp"],
1927 vendor_available: true,
1928 native_bridge_supported: true,
1929 host_supported: true,
1930 vndk: {
1931 enabled: true,
1932 },
1933 system_shared_libs: [],
1934 stl: "none",
1935 }
1936 `)
1937}
1938
Jooyung Han31c470b2019-10-18 16:26:59 +09001939func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001940 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001941 apex_vndk {
1942 name: "myapex_v27",
1943 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001944 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001945 vndk_version: "27",
1946 }
1947
1948 apex_key {
1949 name: "myapex.key",
1950 public_key: "testkey.avbpubkey",
1951 private_key: "testkey.pem",
1952 }
1953
1954 vndk_prebuilt_shared {
1955 name: "libvndk27",
1956 version: "27",
1957 target_arch: "arm",
1958 vendor_available: true,
1959 vndk: {
1960 enabled: true,
1961 },
1962 arch: {
1963 arm: {
1964 srcs: ["libvndk27.so"],
1965 }
1966 },
1967 }
1968
1969 vndk_prebuilt_shared {
1970 name: "libvndk27",
1971 version: "27",
1972 target_arch: "arm",
1973 binder32bit: true,
1974 vendor_available: true,
1975 vndk: {
1976 enabled: true,
1977 },
1978 arch: {
1979 arm: {
1980 srcs: ["libvndk27binder32.so"],
1981 }
1982 },
1983 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001984 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001985 withFiles(map[string][]byte{
1986 "libvndk27.so": nil,
1987 "libvndk27binder32.so": nil,
1988 }),
1989 withBinder32bit,
1990 withTargets(map[android.OsType][]android.Target{
1991 android.Android: []android.Target{
1992 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1993 },
1994 }),
1995 )
1996
1997 ensureExactContents(t, ctx, "myapex_v27", []string{
1998 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001999 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002000 })
2001}
2002
Jooyung Hane1633032019-08-01 17:41:43 +09002003func TestDependenciesInApexManifest(t *testing.T) {
2004 ctx, _ := testApex(t, `
2005 apex {
2006 name: "myapex_nodep",
2007 key: "myapex.key",
2008 native_shared_libs: ["lib_nodep"],
2009 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002010 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002011 }
2012
2013 apex {
2014 name: "myapex_dep",
2015 key: "myapex.key",
2016 native_shared_libs: ["lib_dep"],
2017 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002018 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002019 }
2020
2021 apex {
2022 name: "myapex_provider",
2023 key: "myapex.key",
2024 native_shared_libs: ["libfoo"],
2025 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002026 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002027 }
2028
2029 apex {
2030 name: "myapex_selfcontained",
2031 key: "myapex.key",
2032 native_shared_libs: ["lib_dep", "libfoo"],
2033 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002034 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002035 }
2036
2037 apex_key {
2038 name: "myapex.key",
2039 public_key: "testkey.avbpubkey",
2040 private_key: "testkey.pem",
2041 }
2042
2043 cc_library {
2044 name: "lib_nodep",
2045 srcs: ["mylib.cpp"],
2046 system_shared_libs: [],
2047 stl: "none",
2048 }
2049
2050 cc_library {
2051 name: "lib_dep",
2052 srcs: ["mylib.cpp"],
2053 shared_libs: ["libfoo"],
2054 system_shared_libs: [],
2055 stl: "none",
2056 }
2057
2058 cc_library {
2059 name: "libfoo",
2060 srcs: ["mytest.cpp"],
2061 stubs: {
2062 versions: ["1"],
2063 },
2064 system_shared_libs: [],
2065 stl: "none",
2066 }
2067 `)
2068
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002069 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002070 var provideNativeLibs, requireNativeLibs []string
2071
Sundong Ahnabb64432019-10-22 13:58:29 +09002072 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002073 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2074 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002075 ensureListEmpty(t, provideNativeLibs)
2076 ensureListEmpty(t, requireNativeLibs)
2077
Sundong Ahnabb64432019-10-22 13:58:29 +09002078 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002079 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2080 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002081 ensureListEmpty(t, provideNativeLibs)
2082 ensureListContains(t, requireNativeLibs, "libfoo.so")
2083
Sundong Ahnabb64432019-10-22 13:58:29 +09002084 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002085 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2086 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002087 ensureListContains(t, provideNativeLibs, "libfoo.so")
2088 ensureListEmpty(t, requireNativeLibs)
2089
Sundong Ahnabb64432019-10-22 13:58:29 +09002090 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002091 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2092 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002093 ensureListContains(t, provideNativeLibs, "libfoo.so")
2094 ensureListEmpty(t, requireNativeLibs)
2095}
2096
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002097func TestApexName(t *testing.T) {
2098 ctx, _ := testApex(t, `
2099 apex {
2100 name: "myapex",
2101 key: "myapex.key",
2102 apex_name: "com.android.myapex",
2103 }
2104
2105 apex_key {
2106 name: "myapex.key",
2107 public_key: "testkey.avbpubkey",
2108 private_key: "testkey.pem",
2109 }
2110 `)
2111
Sundong Ahnabb64432019-10-22 13:58:29 +09002112 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002113 apexManifestRule := module.Rule("apexManifestRule")
2114 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2115 apexRule := module.Rule("apexRule")
2116 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
2117}
2118
Alex Light0851b882019-02-07 13:20:53 -08002119func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002120 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002121 apex {
2122 name: "myapex",
2123 key: "myapex.key",
2124 native_shared_libs: ["mylib_common"],
2125 }
2126
2127 apex_key {
2128 name: "myapex.key",
2129 public_key: "testkey.avbpubkey",
2130 private_key: "testkey.pem",
2131 }
2132
2133 cc_library {
2134 name: "mylib_common",
2135 srcs: ["mylib.cpp"],
2136 system_shared_libs: [],
2137 stl: "none",
2138 }
2139 `)
2140
Sundong Ahnabb64432019-10-22 13:58:29 +09002141 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002142 apexRule := module.Rule("apexRule")
2143 copyCmds := apexRule.Args["copy_commands"]
2144
2145 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2146 t.Log("Apex was a test apex!")
2147 t.Fail()
2148 }
2149 // Ensure that main rule creates an output
2150 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2151
2152 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002153 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002154
2155 // Ensure that both direct and indirect deps are copied into apex
2156 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2157
Colin Cross7113d202019-11-20 16:39:12 -08002158 // Ensure that the platform variant ends with _shared
2159 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002160
2161 if !android.InAnyApex("mylib_common") {
2162 t.Log("Found mylib_common not in any apex!")
2163 t.Fail()
2164 }
2165}
2166
2167func TestTestApex(t *testing.T) {
2168 if android.InAnyApex("mylib_common_test") {
2169 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!")
2170 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002171 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002172 apex_test {
2173 name: "myapex",
2174 key: "myapex.key",
2175 native_shared_libs: ["mylib_common_test"],
2176 }
2177
2178 apex_key {
2179 name: "myapex.key",
2180 public_key: "testkey.avbpubkey",
2181 private_key: "testkey.pem",
2182 }
2183
2184 cc_library {
2185 name: "mylib_common_test",
2186 srcs: ["mylib.cpp"],
2187 system_shared_libs: [],
2188 stl: "none",
2189 }
2190 `)
2191
Sundong Ahnabb64432019-10-22 13:58:29 +09002192 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002193 apexRule := module.Rule("apexRule")
2194 copyCmds := apexRule.Args["copy_commands"]
2195
2196 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2197 t.Log("Apex was not a test apex!")
2198 t.Fail()
2199 }
2200 // Ensure that main rule creates an output
2201 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2202
2203 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002204 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002205
2206 // Ensure that both direct and indirect deps are copied into apex
2207 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2208
Colin Cross7113d202019-11-20 16:39:12 -08002209 // Ensure that the platform variant ends with _shared
2210 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002211
2212 if android.InAnyApex("mylib_common_test") {
2213 t.Log("Found mylib_common_test in some apex!")
2214 t.Fail()
2215 }
2216}
2217
Alex Light9670d332019-01-29 18:07:33 -08002218func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002219 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002220 apex {
2221 name: "myapex",
2222 key: "myapex.key",
2223 multilib: {
2224 first: {
2225 native_shared_libs: ["mylib_common"],
2226 }
2227 },
2228 target: {
2229 android: {
2230 multilib: {
2231 first: {
2232 native_shared_libs: ["mylib"],
2233 }
2234 }
2235 },
2236 host: {
2237 multilib: {
2238 first: {
2239 native_shared_libs: ["mylib2"],
2240 }
2241 }
2242 }
2243 }
2244 }
2245
2246 apex_key {
2247 name: "myapex.key",
2248 public_key: "testkey.avbpubkey",
2249 private_key: "testkey.pem",
2250 }
2251
2252 cc_library {
2253 name: "mylib",
2254 srcs: ["mylib.cpp"],
2255 system_shared_libs: [],
2256 stl: "none",
2257 }
2258
2259 cc_library {
2260 name: "mylib_common",
2261 srcs: ["mylib.cpp"],
2262 system_shared_libs: [],
2263 stl: "none",
2264 compile_multilib: "first",
2265 }
2266
2267 cc_library {
2268 name: "mylib2",
2269 srcs: ["mylib.cpp"],
2270 system_shared_libs: [],
2271 stl: "none",
2272 compile_multilib: "first",
2273 }
2274 `)
2275
Sundong Ahnabb64432019-10-22 13:58:29 +09002276 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002277 copyCmds := apexRule.Args["copy_commands"]
2278
2279 // Ensure that main rule creates an output
2280 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2281
2282 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002283 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2284 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2285 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002286
2287 // Ensure that both direct and indirect deps are copied into apex
2288 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2289 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2290 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2291
Colin Cross7113d202019-11-20 16:39:12 -08002292 // Ensure that the platform variant ends with _shared
2293 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2294 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2295 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002296}
Jiyong Park04480cf2019-02-06 00:16:29 +09002297
2298func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002299 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002300 apex {
2301 name: "myapex",
2302 key: "myapex.key",
2303 binaries: ["myscript"],
2304 }
2305
2306 apex_key {
2307 name: "myapex.key",
2308 public_key: "testkey.avbpubkey",
2309 private_key: "testkey.pem",
2310 }
2311
2312 sh_binary {
2313 name: "myscript",
2314 src: "mylib.cpp",
2315 filename: "myscript.sh",
2316 sub_dir: "script",
2317 }
2318 `)
2319
Sundong Ahnabb64432019-10-22 13:58:29 +09002320 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002321 copyCmds := apexRule.Args["copy_commands"]
2322
2323 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2324}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002325
Jooyung Han91df2082019-11-20 01:49:42 +09002326func TestApexInVariousPartition(t *testing.T) {
2327 testcases := []struct {
2328 propName, parition, flattenedPartition string
2329 }{
2330 {"", "system", "system_ext"},
2331 {"product_specific: true", "product", "product"},
2332 {"soc_specific: true", "vendor", "vendor"},
2333 {"proprietary: true", "vendor", "vendor"},
2334 {"vendor: true", "vendor", "vendor"},
2335 {"system_ext_specific: true", "system_ext", "system_ext"},
2336 }
2337 for _, tc := range testcases {
2338 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2339 ctx, _ := testApex(t, `
2340 apex {
2341 name: "myapex",
2342 key: "myapex.key",
2343 `+tc.propName+`
2344 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002345
Jooyung Han91df2082019-11-20 01:49:42 +09002346 apex_key {
2347 name: "myapex.key",
2348 public_key: "testkey.avbpubkey",
2349 private_key: "testkey.pem",
2350 }
2351 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002352
Jooyung Han91df2082019-11-20 01:49:42 +09002353 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2354 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2355 actual := apex.installDir.String()
2356 if actual != expected {
2357 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2358 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002359
Jooyung Han91df2082019-11-20 01:49:42 +09002360 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2361 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2362 actual = flattened.installDir.String()
2363 if actual != expected {
2364 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2365 }
2366 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002367 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002368}
Jiyong Park67882562019-03-21 01:11:21 +09002369
Jooyung Han54aca7b2019-11-20 02:26:02 +09002370func TestFileContexts(t *testing.T) {
2371 ctx, _ := testApex(t, `
2372 apex {
2373 name: "myapex",
2374 key: "myapex.key",
2375 }
2376
2377 apex_key {
2378 name: "myapex.key",
2379 public_key: "testkey.avbpubkey",
2380 private_key: "testkey.pem",
2381 }
2382 `)
2383 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2384 apexRule := module.Rule("apexRule")
2385 actual := apexRule.Args["file_contexts"]
2386 expected := "system/sepolicy/apex/myapex-file_contexts"
2387 if actual != expected {
2388 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2389 }
2390
2391 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2392 apex {
2393 name: "myapex",
2394 key: "myapex.key",
2395 file_contexts: "my_own_file_contexts",
2396 }
2397
2398 apex_key {
2399 name: "myapex.key",
2400 public_key: "testkey.avbpubkey",
2401 private_key: "testkey.pem",
2402 }
2403 `, withFiles(map[string][]byte{
2404 "my_own_file_contexts": nil,
2405 }))
2406
2407 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2408 apex {
2409 name: "myapex",
2410 key: "myapex.key",
2411 product_specific: true,
2412 file_contexts: "product_specific_file_contexts",
2413 }
2414
2415 apex_key {
2416 name: "myapex.key",
2417 public_key: "testkey.avbpubkey",
2418 private_key: "testkey.pem",
2419 }
2420 `)
2421
2422 ctx, _ = testApex(t, `
2423 apex {
2424 name: "myapex",
2425 key: "myapex.key",
2426 product_specific: true,
2427 file_contexts: "product_specific_file_contexts",
2428 }
2429
2430 apex_key {
2431 name: "myapex.key",
2432 public_key: "testkey.avbpubkey",
2433 private_key: "testkey.pem",
2434 }
2435 `, withFiles(map[string][]byte{
2436 "product_specific_file_contexts": nil,
2437 }))
2438 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2439 apexRule = module.Rule("apexRule")
2440 actual = apexRule.Args["file_contexts"]
2441 expected = "product_specific_file_contexts"
2442 if actual != expected {
2443 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2444 }
2445
2446 ctx, _ = testApex(t, `
2447 apex {
2448 name: "myapex",
2449 key: "myapex.key",
2450 product_specific: true,
2451 file_contexts: ":my-file-contexts",
2452 }
2453
2454 apex_key {
2455 name: "myapex.key",
2456 public_key: "testkey.avbpubkey",
2457 private_key: "testkey.pem",
2458 }
2459
2460 filegroup {
2461 name: "my-file-contexts",
2462 srcs: ["product_specific_file_contexts"],
2463 }
2464 `, withFiles(map[string][]byte{
2465 "product_specific_file_contexts": nil,
2466 }))
2467 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2468 apexRule = module.Rule("apexRule")
2469 actual = apexRule.Args["file_contexts"]
2470 expected = "product_specific_file_contexts"
2471 if actual != expected {
2472 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2473 }
2474}
2475
Jiyong Park67882562019-03-21 01:11:21 +09002476func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002477 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002478 apex_key {
2479 name: "myapex.key",
2480 public_key: ":my.avbpubkey",
2481 private_key: ":my.pem",
2482 product_specific: true,
2483 }
2484
2485 filegroup {
2486 name: "my.avbpubkey",
2487 srcs: ["testkey2.avbpubkey"],
2488 }
2489
2490 filegroup {
2491 name: "my.pem",
2492 srcs: ["testkey2.pem"],
2493 }
2494 `)
2495
2496 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2497 expected_pubkey := "testkey2.avbpubkey"
2498 actual_pubkey := apex_key.public_key_file.String()
2499 if actual_pubkey != expected_pubkey {
2500 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2501 }
2502 expected_privkey := "testkey2.pem"
2503 actual_privkey := apex_key.private_key_file.String()
2504 if actual_privkey != expected_privkey {
2505 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2506 }
2507}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002508
2509func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002510 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002511 prebuilt_apex {
2512 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002513 arch: {
2514 arm64: {
2515 src: "myapex-arm64.apex",
2516 },
2517 arm: {
2518 src: "myapex-arm.apex",
2519 },
2520 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002521 }
2522 `)
2523
2524 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2525
Jiyong Parkc95714e2019-03-29 14:23:10 +09002526 expectedInput := "myapex-arm64.apex"
2527 if prebuilt.inputApex.String() != expectedInput {
2528 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2529 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002530}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002531
2532func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002533 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002534 prebuilt_apex {
2535 name: "myapex",
2536 src: "myapex-arm.apex",
2537 filename: "notmyapex.apex",
2538 }
2539 `)
2540
2541 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2542
2543 expected := "notmyapex.apex"
2544 if p.installFilename != expected {
2545 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2546 }
2547}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002548
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002549func TestPrebuiltOverrides(t *testing.T) {
2550 ctx, config := testApex(t, `
2551 prebuilt_apex {
2552 name: "myapex.prebuilt",
2553 src: "myapex-arm.apex",
2554 overrides: [
2555 "myapex",
2556 ],
2557 }
2558 `)
2559
2560 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2561
2562 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002563 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002564 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002565 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002566 }
2567}
2568
Roland Levillain630846d2019-06-26 12:48:34 +01002569func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002570 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002571 apex_test {
2572 name: "myapex",
2573 key: "myapex.key",
2574 tests: [
2575 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002576 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002577 ],
2578 }
2579
2580 apex_key {
2581 name: "myapex.key",
2582 public_key: "testkey.avbpubkey",
2583 private_key: "testkey.pem",
2584 }
2585
2586 cc_test {
2587 name: "mytest",
2588 gtest: false,
2589 srcs: ["mytest.cpp"],
2590 relative_install_path: "test",
2591 system_shared_libs: [],
2592 static_executable: true,
2593 stl: "none",
2594 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002595
2596 cc_test {
2597 name: "mytests",
2598 gtest: false,
2599 srcs: [
2600 "mytest1.cpp",
2601 "mytest2.cpp",
2602 "mytest3.cpp",
2603 ],
2604 test_per_src: true,
2605 relative_install_path: "test",
2606 system_shared_libs: [],
2607 static_executable: true,
2608 stl: "none",
2609 }
Roland Levillain630846d2019-06-26 12:48:34 +01002610 `)
2611
Sundong Ahnabb64432019-10-22 13:58:29 +09002612 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002613 copyCmds := apexRule.Args["copy_commands"]
2614
2615 // Ensure that test dep is copied into apex.
2616 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002617
2618 // Ensure that test deps built with `test_per_src` are copied into apex.
2619 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2620 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2621 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002622
2623 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002624 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002625 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2626 name := apexBundle.BaseModuleName()
2627 prefix := "TARGET_"
2628 var builder strings.Builder
2629 data.Custom(&builder, name, prefix, "", data)
2630 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002631 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2632 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2633 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2634 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002635 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002636 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002637 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002638}
2639
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002640func TestInstallExtraFlattenedApexes(t *testing.T) {
2641 ctx, config := testApex(t, `
2642 apex {
2643 name: "myapex",
2644 key: "myapex.key",
2645 }
2646 apex_key {
2647 name: "myapex.key",
2648 public_key: "testkey.avbpubkey",
2649 private_key: "testkey.pem",
2650 }
2651 `, func(fs map[string][]byte, config android.Config) {
2652 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2653 })
2654 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2655 ensureListContains(t, ab.externalDeps, "myapex.flattened")
2656 mk := android.AndroidMkDataForTest(t, config, "", ab)
2657 var builder strings.Builder
2658 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2659 androidMk := builder.String()
2660 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2661}
2662
Jooyung Han5c998b92019-06-27 11:30:33 +09002663func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002664 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002665 apex {
2666 name: "myapex",
2667 key: "myapex.key",
2668 native_shared_libs: ["mylib"],
2669 uses: ["commonapex"],
2670 }
2671
2672 apex {
2673 name: "commonapex",
2674 key: "myapex.key",
2675 native_shared_libs: ["libcommon"],
2676 provide_cpp_shared_libs: true,
2677 }
2678
2679 apex_key {
2680 name: "myapex.key",
2681 public_key: "testkey.avbpubkey",
2682 private_key: "testkey.pem",
2683 }
2684
2685 cc_library {
2686 name: "mylib",
2687 srcs: ["mylib.cpp"],
2688 shared_libs: ["libcommon"],
2689 system_shared_libs: [],
2690 stl: "none",
2691 }
2692
2693 cc_library {
2694 name: "libcommon",
2695 srcs: ["mylib_common.cpp"],
2696 system_shared_libs: [],
2697 stl: "none",
2698 }
2699 `)
2700
Sundong Ahnabb64432019-10-22 13:58:29 +09002701 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002702 apexRule1 := module1.Rule("apexRule")
2703 copyCmds1 := apexRule1.Args["copy_commands"]
2704
Sundong Ahnabb64432019-10-22 13:58:29 +09002705 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002706 apexRule2 := module2.Rule("apexRule")
2707 copyCmds2 := apexRule2.Args["copy_commands"]
2708
Colin Cross7113d202019-11-20 16:39:12 -08002709 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2710 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002711 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2712 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2713 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2714}
2715
2716func TestApexUsesFailsIfNotProvided(t *testing.T) {
2717 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2718 apex {
2719 name: "myapex",
2720 key: "myapex.key",
2721 uses: ["commonapex"],
2722 }
2723
2724 apex {
2725 name: "commonapex",
2726 key: "myapex.key",
2727 }
2728
2729 apex_key {
2730 name: "myapex.key",
2731 public_key: "testkey.avbpubkey",
2732 private_key: "testkey.pem",
2733 }
2734 `)
2735 testApexError(t, `uses: "commonapex" is not a provider`, `
2736 apex {
2737 name: "myapex",
2738 key: "myapex.key",
2739 uses: ["commonapex"],
2740 }
2741
2742 cc_library {
2743 name: "commonapex",
2744 system_shared_libs: [],
2745 stl: "none",
2746 }
2747
2748 apex_key {
2749 name: "myapex.key",
2750 public_key: "testkey.avbpubkey",
2751 private_key: "testkey.pem",
2752 }
2753 `)
2754}
2755
2756func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2757 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2758 apex {
2759 name: "myapex",
2760 key: "myapex.key",
2761 use_vendor: true,
2762 uses: ["commonapex"],
2763 }
2764
2765 apex {
2766 name: "commonapex",
2767 key: "myapex.key",
2768 provide_cpp_shared_libs: true,
2769 }
2770
2771 apex_key {
2772 name: "myapex.key",
2773 public_key: "testkey.avbpubkey",
2774 private_key: "testkey.pem",
2775 }
Jooyung Handc782442019-11-01 03:14:38 +09002776 `, func(fs map[string][]byte, config android.Config) {
2777 setUseVendorWhitelistForTest(config, []string{"myapex"})
2778 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002779}
2780
Jooyung Hand48f3c32019-08-23 11:18:57 +09002781func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2782 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2783 apex {
2784 name: "myapex",
2785 key: "myapex.key",
2786 native_shared_libs: ["libfoo"],
2787 }
2788
2789 apex_key {
2790 name: "myapex.key",
2791 public_key: "testkey.avbpubkey",
2792 private_key: "testkey.pem",
2793 }
2794
2795 cc_library {
2796 name: "libfoo",
2797 stl: "none",
2798 system_shared_libs: [],
2799 enabled: false,
2800 }
2801 `)
2802 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2803 apex {
2804 name: "myapex",
2805 key: "myapex.key",
2806 java_libs: ["myjar"],
2807 }
2808
2809 apex_key {
2810 name: "myapex.key",
2811 public_key: "testkey.avbpubkey",
2812 private_key: "testkey.pem",
2813 }
2814
2815 java_library {
2816 name: "myjar",
2817 srcs: ["foo/bar/MyClass.java"],
2818 sdk_version: "none",
2819 system_modules: "none",
2820 compile_dex: true,
2821 enabled: false,
2822 }
2823 `)
2824}
2825
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002826func TestApexWithApps(t *testing.T) {
2827 ctx, _ := testApex(t, `
2828 apex {
2829 name: "myapex",
2830 key: "myapex.key",
2831 apps: [
2832 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002833 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002834 ],
2835 }
2836
2837 apex_key {
2838 name: "myapex.key",
2839 public_key: "testkey.avbpubkey",
2840 private_key: "testkey.pem",
2841 }
2842
2843 android_app {
2844 name: "AppFoo",
2845 srcs: ["foo/bar/MyClass.java"],
2846 sdk_version: "none",
2847 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002848 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002849 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002850
2851 android_app {
2852 name: "AppFooPriv",
2853 srcs: ["foo/bar/MyClass.java"],
2854 sdk_version: "none",
2855 system_modules: "none",
2856 privileged: true,
2857 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002858
2859 cc_library_shared {
2860 name: "libjni",
2861 srcs: ["mylib.cpp"],
2862 stl: "none",
2863 system_shared_libs: [],
2864 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002865 `)
2866
Sundong Ahnabb64432019-10-22 13:58:29 +09002867 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002868 apexRule := module.Rule("apexRule")
2869 copyCmds := apexRule.Args["copy_commands"]
2870
2871 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002872 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002873
2874 // JNI libraries are embedded inside APK
2875 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
Colin Cross7113d202019-11-20 16:39:12 -08002876 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002877 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2878 // ... uncompressed
2879 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2880 t.Errorf("jni lib is not uncompressed for AppFoo")
2881 }
2882 // ... and not directly inside the APEX
2883 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002884}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002885
Dario Frenicde2a032019-10-27 00:29:22 +01002886func TestApexWithAppImports(t *testing.T) {
2887 ctx, _ := testApex(t, `
2888 apex {
2889 name: "myapex",
2890 key: "myapex.key",
2891 apps: [
2892 "AppFooPrebuilt",
2893 "AppFooPrivPrebuilt",
2894 ],
2895 }
2896
2897 apex_key {
2898 name: "myapex.key",
2899 public_key: "testkey.avbpubkey",
2900 private_key: "testkey.pem",
2901 }
2902
2903 android_app_import {
2904 name: "AppFooPrebuilt",
2905 apk: "PrebuiltAppFoo.apk",
2906 presigned: true,
2907 dex_preopt: {
2908 enabled: false,
2909 },
2910 }
2911
2912 android_app_import {
2913 name: "AppFooPrivPrebuilt",
2914 apk: "PrebuiltAppFooPriv.apk",
2915 privileged: true,
2916 presigned: true,
2917 dex_preopt: {
2918 enabled: false,
2919 },
2920 }
2921 `)
2922
Sundong Ahnabb64432019-10-22 13:58:29 +09002923 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002924 apexRule := module.Rule("apexRule")
2925 copyCmds := apexRule.Args["copy_commands"]
2926
2927 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2928 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002929}
2930
Dario Freni6f3937c2019-12-20 22:58:03 +00002931func TestApexWithTestHelperApp(t *testing.T) {
2932 ctx, _ := testApex(t, `
2933 apex {
2934 name: "myapex",
2935 key: "myapex.key",
2936 apps: [
2937 "TesterHelpAppFoo",
2938 ],
2939 }
2940
2941 apex_key {
2942 name: "myapex.key",
2943 public_key: "testkey.avbpubkey",
2944 private_key: "testkey.pem",
2945 }
2946
2947 android_test_helper_app {
2948 name: "TesterHelpAppFoo",
2949 srcs: ["foo/bar/MyClass.java"],
2950 }
2951
2952 `)
2953
2954 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2955 apexRule := module.Rule("apexRule")
2956 copyCmds := apexRule.Args["copy_commands"]
2957
2958 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
2959}
2960
Jooyung Han18020ea2019-11-13 10:50:48 +09002961func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2962 // libfoo's apex_available comes from cc_defaults
2963 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2964 apex {
2965 name: "myapex",
2966 key: "myapex.key",
2967 native_shared_libs: ["libfoo"],
2968 }
2969
2970 apex_key {
2971 name: "myapex.key",
2972 public_key: "testkey.avbpubkey",
2973 private_key: "testkey.pem",
2974 }
2975
2976 apex {
2977 name: "otherapex",
2978 key: "myapex.key",
2979 native_shared_libs: ["libfoo"],
2980 }
2981
2982 cc_defaults {
2983 name: "libfoo-defaults",
2984 apex_available: ["otherapex"],
2985 }
2986
2987 cc_library {
2988 name: "libfoo",
2989 defaults: ["libfoo-defaults"],
2990 stl: "none",
2991 system_shared_libs: [],
2992 }`)
2993}
2994
Jiyong Park127b40b2019-09-30 16:04:35 +09002995func TestApexAvailable(t *testing.T) {
2996 // libfoo is not available to myapex, but only to otherapex
2997 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2998 apex {
2999 name: "myapex",
3000 key: "myapex.key",
3001 native_shared_libs: ["libfoo"],
3002 }
3003
3004 apex_key {
3005 name: "myapex.key",
3006 public_key: "testkey.avbpubkey",
3007 private_key: "testkey.pem",
3008 }
3009
3010 apex {
3011 name: "otherapex",
3012 key: "otherapex.key",
3013 native_shared_libs: ["libfoo"],
3014 }
3015
3016 apex_key {
3017 name: "otherapex.key",
3018 public_key: "testkey.avbpubkey",
3019 private_key: "testkey.pem",
3020 }
3021
3022 cc_library {
3023 name: "libfoo",
3024 stl: "none",
3025 system_shared_libs: [],
3026 apex_available: ["otherapex"],
3027 }`)
3028
3029 // libbar is an indirect dep
3030 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3031 apex {
3032 name: "myapex",
3033 key: "myapex.key",
3034 native_shared_libs: ["libfoo"],
3035 }
3036
3037 apex_key {
3038 name: "myapex.key",
3039 public_key: "testkey.avbpubkey",
3040 private_key: "testkey.pem",
3041 }
3042
3043 apex {
3044 name: "otherapex",
3045 key: "otherapex.key",
3046 native_shared_libs: ["libfoo"],
3047 }
3048
3049 apex_key {
3050 name: "otherapex.key",
3051 public_key: "testkey.avbpubkey",
3052 private_key: "testkey.pem",
3053 }
3054
3055 cc_library {
3056 name: "libfoo",
3057 stl: "none",
3058 shared_libs: ["libbar"],
3059 system_shared_libs: [],
3060 apex_available: ["myapex", "otherapex"],
3061 }
3062
3063 cc_library {
3064 name: "libbar",
3065 stl: "none",
3066 system_shared_libs: [],
3067 apex_available: ["otherapex"],
3068 }`)
3069
3070 testApexError(t, "\"otherapex\" is not a valid module name", `
3071 apex {
3072 name: "myapex",
3073 key: "myapex.key",
3074 native_shared_libs: ["libfoo"],
3075 }
3076
3077 apex_key {
3078 name: "myapex.key",
3079 public_key: "testkey.avbpubkey",
3080 private_key: "testkey.pem",
3081 }
3082
3083 cc_library {
3084 name: "libfoo",
3085 stl: "none",
3086 system_shared_libs: [],
3087 apex_available: ["otherapex"],
3088 }`)
3089
3090 ctx, _ := testApex(t, `
3091 apex {
3092 name: "myapex",
3093 key: "myapex.key",
3094 native_shared_libs: ["libfoo", "libbar"],
3095 }
3096
3097 apex_key {
3098 name: "myapex.key",
3099 public_key: "testkey.avbpubkey",
3100 private_key: "testkey.pem",
3101 }
3102
3103 cc_library {
3104 name: "libfoo",
3105 stl: "none",
3106 system_shared_libs: [],
3107 apex_available: ["myapex"],
3108 }
3109
3110 cc_library {
3111 name: "libbar",
3112 stl: "none",
3113 system_shared_libs: [],
3114 apex_available: ["//apex_available:anyapex"],
3115 }`)
3116
3117 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003118 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3119 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3120 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3121 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003122
3123 ctx, _ = testApex(t, `
3124 apex {
3125 name: "myapex",
3126 key: "myapex.key",
3127 }
3128
3129 apex_key {
3130 name: "myapex.key",
3131 public_key: "testkey.avbpubkey",
3132 private_key: "testkey.pem",
3133 }
3134
3135 cc_library {
3136 name: "libfoo",
3137 stl: "none",
3138 system_shared_libs: [],
3139 apex_available: ["//apex_available:platform"],
3140 }`)
3141
3142 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003143 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3144 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003145
3146 ctx, _ = testApex(t, `
3147 apex {
3148 name: "myapex",
3149 key: "myapex.key",
3150 native_shared_libs: ["libfoo"],
3151 }
3152
3153 apex_key {
3154 name: "myapex.key",
3155 public_key: "testkey.avbpubkey",
3156 private_key: "testkey.pem",
3157 }
3158
3159 cc_library {
3160 name: "libfoo",
3161 stl: "none",
3162 system_shared_libs: [],
3163 apex_available: ["myapex"],
3164 static: {
3165 apex_available: ["//apex_available:platform"],
3166 },
3167 }`)
3168
3169 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08003170 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3171 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003172 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08003173 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3174 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003175}
3176
Jiyong Park5d790c32019-11-15 18:40:32 +09003177func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003178 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003179 apex {
3180 name: "myapex",
3181 key: "myapex.key",
3182 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003183 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003184 }
3185
3186 override_apex {
3187 name: "override_myapex",
3188 base: "myapex",
3189 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003190 overrides: ["unknownapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003191 }
3192
3193 apex_key {
3194 name: "myapex.key",
3195 public_key: "testkey.avbpubkey",
3196 private_key: "testkey.pem",
3197 }
3198
3199 android_app {
3200 name: "app",
3201 srcs: ["foo/bar/MyClass.java"],
3202 package_name: "foo",
3203 sdk_version: "none",
3204 system_modules: "none",
3205 }
3206
3207 override_android_app {
3208 name: "override_app",
3209 base: "app",
3210 package_name: "bar",
3211 }
3212 `)
3213
Jiyong Park317645e2019-12-05 13:20:58 +09003214 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3215 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3216 if originalVariant.GetOverriddenBy() != "" {
3217 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3218 }
3219 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3220 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3221 }
3222
Jiyong Park5d790c32019-11-15 18:40:32 +09003223 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3224 apexRule := module.Rule("apexRule")
3225 copyCmds := apexRule.Args["copy_commands"]
3226
3227 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3228 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003229
3230 apexBundle := module.Module().(*apexBundle)
3231 name := apexBundle.Name()
3232 if name != "override_myapex" {
3233 t.Errorf("name should be \"override_myapex\", but was %q", name)
3234 }
3235
3236 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3237 var builder strings.Builder
3238 data.Custom(&builder, name, "TARGET_", "", data)
3239 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003240 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003241 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3242 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003243 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003244 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003245 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003246 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3247 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003248}
3249
Jooyung Han214bf372019-11-12 13:03:50 +09003250func TestLegacyAndroid10Support(t *testing.T) {
3251 ctx, _ := testApex(t, `
3252 apex {
3253 name: "myapex",
3254 key: "myapex.key",
3255 legacy_android10_support: true,
3256 }
3257
3258 apex_key {
3259 name: "myapex.key",
3260 public_key: "testkey.avbpubkey",
3261 private_key: "testkey.pem",
3262 }
3263 `)
3264
3265 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3266 args := module.Rule("apexRule").Args
3267 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
3268}
3269
Jooyung Han58f26ab2019-12-18 15:34:32 +09003270func TestJavaSDKLibrary(t *testing.T) {
3271 ctx, _ := testApex(t, `
3272 apex {
3273 name: "myapex",
3274 key: "myapex.key",
3275 java_libs: ["foo"],
3276 }
3277
3278 apex_key {
3279 name: "myapex.key",
3280 public_key: "testkey.avbpubkey",
3281 private_key: "testkey.pem",
3282 }
3283
3284 java_sdk_library {
3285 name: "foo",
3286 srcs: ["a.java"],
3287 api_packages: ["foo"],
3288 }
3289 `, withFiles(map[string][]byte{
3290 "api/current.txt": nil,
3291 "api/removed.txt": nil,
3292 "api/system-current.txt": nil,
3293 "api/system-removed.txt": nil,
3294 "api/test-current.txt": nil,
3295 "api/test-removed.txt": nil,
3296 }))
3297
3298 // java_sdk_library installs both impl jar and permission XML
3299 ensureExactContents(t, ctx, "myapex", []string{
3300 "javalib/foo.jar",
3301 "etc/permissions/foo.xml",
3302 })
3303 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jooyung Han624058e2019-12-24 18:38:06 +09003304 xml := ctx.ModuleForTests("foo", "android_common_myapex").Output("foo.xml")
3305 ensureContains(t, xml.Args["content"], `<library name="foo" file="/apex/myapex/javalib/foo.jar"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003306}
3307
Jiyong Park479321d2019-12-16 11:47:12 +09003308func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3309 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3310 apex {
3311 name: "myapex",
3312 key: "myapex.key",
3313 java_libs: ["myjar"],
3314 }
3315
3316 apex_key {
3317 name: "myapex.key",
3318 public_key: "testkey.avbpubkey",
3319 private_key: "testkey.pem",
3320 }
3321
3322 java_library {
3323 name: "myjar",
3324 srcs: ["foo/bar/MyClass.java"],
3325 sdk_version: "none",
3326 system_modules: "none",
3327 }
3328 `)
3329}
3330
Jiyong Park7afd1072019-12-30 16:56:33 +09003331func TestCarryRequiredModuleNames(t *testing.T) {
3332 ctx, config := testApex(t, `
3333 apex {
3334 name: "myapex",
3335 key: "myapex.key",
3336 native_shared_libs: ["mylib"],
3337 }
3338
3339 apex_key {
3340 name: "myapex.key",
3341 public_key: "testkey.avbpubkey",
3342 private_key: "testkey.pem",
3343 }
3344
3345 cc_library {
3346 name: "mylib",
3347 srcs: ["mylib.cpp"],
3348 system_shared_libs: [],
3349 stl: "none",
3350 required: ["a", "b"],
3351 host_required: ["c", "d"],
3352 target_required: ["e", "f"],
3353 }
3354 `)
3355
3356 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3357 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3358 name := apexBundle.BaseModuleName()
3359 prefix := "TARGET_"
3360 var builder strings.Builder
3361 data.Custom(&builder, name, prefix, "", data)
3362 androidMk := builder.String()
3363 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3364 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3365 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3366}
3367
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003368func TestMain(m *testing.M) {
3369 run := func() int {
3370 setUp()
3371 defer tearDown()
3372
3373 return m.Run()
3374 }
3375
3376 os.Exit(run())
3377}