blob: 97600b6c5aa1f9677fb4c2723a890f007d42d6ed [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"],
441 }
442
443 java_library {
444 name: "myotherjar",
445 srcs: ["foo/bar/MyClass.java"],
446 sdk_version: "none",
447 system_modules: "none",
448 compile_dex: true,
449 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900450 `)
451
Sundong Ahnabb64432019-10-22 13:58:29 +0900452 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900453
454 optFlags := apexRule.Args["opt_flags"]
455 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700456 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900457 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900458
Jiyong Park25fc6a92018-11-18 18:02:45 +0900459 copyCmds := apexRule.Args["copy_commands"]
460
461 // Ensure that main rule creates an output
462 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
463
464 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800465 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900466 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900467
468 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800469 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900470 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900471
472 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800473 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
474 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900475 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
476 // .. but not for java libs
477 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800478
Colin Cross7113d202019-11-20 16:39:12 -0800479 // Ensure that the platform variant ends with _shared or _common
480 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
481 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900482 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
483 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Alex Light3d673592019-01-18 14:37:31 -0800484
485 // Ensure that all symlinks are present.
486 found_foo_link_64 := false
487 found_foo := false
488 for _, cmd := range strings.Split(copyCmds, " && ") {
489 if strings.HasPrefix(cmd, "ln -s foo64") {
490 if strings.HasSuffix(cmd, "bin/foo") {
491 found_foo = true
492 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
493 found_foo_link_64 = true
494 }
495 }
496 }
497 good := found_foo && found_foo_link_64
498 if !good {
499 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
500 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900501
Sundong Ahnabb64432019-10-22 13:58:29 +0900502 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700503 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700504 if len(noticeInputs) != 2 {
505 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900506 }
507 ensureListContains(t, noticeInputs, "NOTICE")
508 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800509}
510
Jooyung Hanf21c7972019-12-16 22:32:06 +0900511func TestDefaults(t *testing.T) {
512 ctx, _ := testApex(t, `
513 apex_defaults {
514 name: "myapex-defaults",
515 key: "myapex.key",
516 prebuilts: ["myetc"],
517 native_shared_libs: ["mylib"],
518 java_libs: ["myjar"],
519 apps: ["AppFoo"],
520 }
521
522 prebuilt_etc {
523 name: "myetc",
524 src: "myprebuilt",
525 }
526
527 apex {
528 name: "myapex",
529 defaults: ["myapex-defaults"],
530 }
531
532 apex_key {
533 name: "myapex.key",
534 public_key: "testkey.avbpubkey",
535 private_key: "testkey.pem",
536 }
537
538 cc_library {
539 name: "mylib",
540 system_shared_libs: [],
541 stl: "none",
542 }
543
544 java_library {
545 name: "myjar",
546 srcs: ["foo/bar/MyClass.java"],
547 sdk_version: "none",
548 system_modules: "none",
549 compile_dex: true,
550 }
551
552 android_app {
553 name: "AppFoo",
554 srcs: ["foo/bar/MyClass.java"],
555 sdk_version: "none",
556 system_modules: "none",
557 }
558 `)
559 ensureExactContents(t, ctx, "myapex", []string{
560 "etc/myetc",
561 "javalib/myjar.jar",
562 "lib64/mylib.so",
563 "app/AppFoo/AppFoo.apk",
564 })
565}
566
Jooyung Han01a3ee22019-11-02 02:52:25 +0900567func TestApexManifest(t *testing.T) {
568 ctx, _ := testApex(t, `
569 apex {
570 name: "myapex",
571 key: "myapex.key",
572 }
573
574 apex_key {
575 name: "myapex.key",
576 public_key: "testkey.avbpubkey",
577 private_key: "testkey.pem",
578 }
579 `)
580
581 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900582 args := module.Rule("apexRule").Args
583 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
584 t.Error("manifest should be apex_manifest.pb, but " + manifest)
585 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900586}
587
Alex Light5098a612018-11-29 17:12:15 -0800588func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700589 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800590 apex {
591 name: "myapex",
592 key: "myapex.key",
593 payload_type: "zip",
594 native_shared_libs: ["mylib"],
595 }
596
597 apex_key {
598 name: "myapex.key",
599 public_key: "testkey.avbpubkey",
600 private_key: "testkey.pem",
601 }
602
603 cc_library {
604 name: "mylib",
605 srcs: ["mylib.cpp"],
606 shared_libs: ["mylib2"],
607 system_shared_libs: [],
608 stl: "none",
609 }
610
611 cc_library {
612 name: "mylib2",
613 srcs: ["mylib.cpp"],
614 system_shared_libs: [],
615 stl: "none",
616 }
617 `)
618
Sundong Ahnabb64432019-10-22 13:58:29 +0900619 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800620 copyCmds := zipApexRule.Args["copy_commands"]
621
622 // Ensure that main rule creates an output
623 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
624
625 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800626 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800627
628 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800629 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800630
631 // Ensure that both direct and indirect deps are copied into apex
632 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
633 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900634}
635
636func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700637 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900638 apex {
639 name: "myapex",
640 key: "myapex.key",
641 native_shared_libs: ["mylib", "mylib3"],
642 }
643
644 apex_key {
645 name: "myapex.key",
646 public_key: "testkey.avbpubkey",
647 private_key: "testkey.pem",
648 }
649
650 cc_library {
651 name: "mylib",
652 srcs: ["mylib.cpp"],
653 shared_libs: ["mylib2", "mylib3"],
654 system_shared_libs: [],
655 stl: "none",
656 }
657
658 cc_library {
659 name: "mylib2",
660 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900661 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900662 system_shared_libs: [],
663 stl: "none",
664 stubs: {
665 versions: ["1", "2", "3"],
666 },
667 }
668
669 cc_library {
670 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900671 srcs: ["mylib.cpp"],
672 shared_libs: ["mylib4"],
673 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900674 stl: "none",
675 stubs: {
676 versions: ["10", "11", "12"],
677 },
678 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900679
680 cc_library {
681 name: "mylib4",
682 srcs: ["mylib.cpp"],
683 system_shared_libs: [],
684 stl: "none",
685 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900686 `)
687
Sundong Ahnabb64432019-10-22 13:58:29 +0900688 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900689 copyCmds := apexRule.Args["copy_commands"]
690
691 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800692 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900693
694 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800695 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900696
697 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800698 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900699
Colin Cross7113d202019-11-20 16:39:12 -0800700 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900701
702 // Ensure that mylib is linking with the latest version of stubs for mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800703 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900704 // ... and not linking to the non-stub (impl) variant of mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800705 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900706
707 // 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 -0800708 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900709 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800710 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900711
712 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800713 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900714 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900715
716 // Ensure that genstub is invoked with --apex
Colin Cross7113d202019-11-20 16:39:12 -0800717 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3_myapex").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900718
719 ensureExactContents(t, ctx, "myapex", []string{
720 "lib64/mylib.so",
721 "lib64/mylib3.so",
722 "lib64/mylib4.so",
723 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900724}
725
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900726func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700727 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900728 apex {
729 name: "myapex",
730 key: "myapex.key",
731 native_shared_libs: ["mylib"],
732 }
733
734 apex_key {
735 name: "myapex.key",
736 public_key: "testkey.avbpubkey",
737 private_key: "testkey.pem",
738 }
739
740 cc_library {
741 name: "mylib",
742 srcs: ["mylib.cpp"],
743 shared_libs: ["libfoo#10"],
744 system_shared_libs: [],
745 stl: "none",
746 }
747
748 cc_library {
749 name: "libfoo",
750 srcs: ["mylib.cpp"],
751 shared_libs: ["libbar"],
752 system_shared_libs: [],
753 stl: "none",
754 stubs: {
755 versions: ["10", "20", "30"],
756 },
757 }
758
759 cc_library {
760 name: "libbar",
761 srcs: ["mylib.cpp"],
762 system_shared_libs: [],
763 stl: "none",
764 }
765
766 `)
767
Sundong Ahnabb64432019-10-22 13:58:29 +0900768 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900769 copyCmds := apexRule.Args["copy_commands"]
770
771 // Ensure that direct non-stubs dep is always included
772 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
773
774 // Ensure that indirect stubs dep is not included
775 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
776
777 // Ensure that dependency of stubs is not included
778 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
779
Colin Cross7113d202019-11-20 16:39:12 -0800780 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900781
782 // Ensure that mylib is linking with version 10 of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800783 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900784 // ... and not linking to the non-stub (impl) variant of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800785 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900786
Colin Cross7113d202019-11-20 16:39:12 -0800787 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900788
789 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
790 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
791}
792
Jooyung Hand3639552019-08-09 12:57:43 +0900793func TestApexWithRuntimeLibsDependency(t *testing.T) {
794 /*
795 myapex
796 |
797 v (runtime_libs)
798 mylib ------+------> libfoo [provides stub]
799 |
800 `------> libbar
801 */
802 ctx, _ := testApex(t, `
803 apex {
804 name: "myapex",
805 key: "myapex.key",
806 native_shared_libs: ["mylib"],
807 }
808
809 apex_key {
810 name: "myapex.key",
811 public_key: "testkey.avbpubkey",
812 private_key: "testkey.pem",
813 }
814
815 cc_library {
816 name: "mylib",
817 srcs: ["mylib.cpp"],
818 runtime_libs: ["libfoo", "libbar"],
819 system_shared_libs: [],
820 stl: "none",
821 }
822
823 cc_library {
824 name: "libfoo",
825 srcs: ["mylib.cpp"],
826 system_shared_libs: [],
827 stl: "none",
828 stubs: {
829 versions: ["10", "20", "30"],
830 },
831 }
832
833 cc_library {
834 name: "libbar",
835 srcs: ["mylib.cpp"],
836 system_shared_libs: [],
837 stl: "none",
838 }
839
840 `)
841
Sundong Ahnabb64432019-10-22 13:58:29 +0900842 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900843 copyCmds := apexRule.Args["copy_commands"]
844
845 // Ensure that direct non-stubs dep is always included
846 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
847
848 // Ensure that indirect stubs dep is not included
849 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
850
851 // Ensure that runtime_libs dep in included
852 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
853
Sundong Ahnabb64432019-10-22 13:58:29 +0900854 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900855 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
856 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900857
858}
859
Jooyung Han9c80bae2019-08-20 17:30:57 +0900860func TestApexDependencyToLLNDK(t *testing.T) {
861 ctx, _ := testApex(t, `
862 apex {
863 name: "myapex",
864 key: "myapex.key",
865 use_vendor: true,
866 native_shared_libs: ["mylib"],
867 }
868
869 apex_key {
870 name: "myapex.key",
871 public_key: "testkey.avbpubkey",
872 private_key: "testkey.pem",
873 }
874
875 cc_library {
876 name: "mylib",
877 srcs: ["mylib.cpp"],
878 vendor_available: true,
879 shared_libs: ["libbar"],
880 system_shared_libs: [],
881 stl: "none",
882 }
883
884 cc_library {
885 name: "libbar",
886 srcs: ["mylib.cpp"],
887 system_shared_libs: [],
888 stl: "none",
889 }
890
891 llndk_library {
892 name: "libbar",
893 symbol_file: "",
894 }
Jooyung Handc782442019-11-01 03:14:38 +0900895 `, func(fs map[string][]byte, config android.Config) {
896 setUseVendorWhitelistForTest(config, []string{"myapex"})
897 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900898
Sundong Ahnabb64432019-10-22 13:58:29 +0900899 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900900 copyCmds := apexRule.Args["copy_commands"]
901
902 // Ensure that LLNDK dep is not included
903 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
904
Sundong Ahnabb64432019-10-22 13:58:29 +0900905 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900906 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900907
908 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900909 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900910
911}
912
Jiyong Park25fc6a92018-11-18 18:02:45 +0900913func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700914 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900915 apex {
916 name: "myapex",
917 key: "myapex.key",
918 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
919 }
920
921 apex_key {
922 name: "myapex.key",
923 public_key: "testkey.avbpubkey",
924 private_key: "testkey.pem",
925 }
926
927 cc_library {
928 name: "mylib",
929 srcs: ["mylib.cpp"],
930 shared_libs: ["libdl#27"],
931 stl: "none",
932 }
933
934 cc_library_shared {
935 name: "mylib_shared",
936 srcs: ["mylib.cpp"],
937 shared_libs: ["libdl#27"],
938 stl: "none",
939 }
940
941 cc_library {
942 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700943 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900944 nocrt: true,
945 system_shared_libs: [],
946 stl: "none",
947 stubs: {
948 versions: ["27", "28", "29"],
949 },
950 }
951
952 cc_library {
953 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700954 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900955 nocrt: true,
956 system_shared_libs: [],
957 stl: "none",
958 stubs: {
959 versions: ["27", "28", "29"],
960 },
961 }
962
963 cc_library {
964 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700965 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900966 nocrt: true,
967 system_shared_libs: [],
968 stl: "none",
969 stubs: {
970 versions: ["27", "28", "29"],
971 },
972 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900973
974 cc_library {
975 name: "libBootstrap",
976 srcs: ["mylib.cpp"],
977 stl: "none",
978 bootstrap: true,
979 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900980 `)
981
Sundong Ahnabb64432019-10-22 13:58:29 +0900982 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900983 copyCmds := apexRule.Args["copy_commands"]
984
985 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800986 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900987 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
988 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900989
990 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900991 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900992
Colin Cross7113d202019-11-20 16:39:12 -0800993 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
994 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
995 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900996
997 // For dependency to libc
998 // Ensure that mylib is linking with the latest version of stubs
Colin Cross7113d202019-11-20 16:39:12 -0800999 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001000 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001001 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001002 // ... Cflags from stub is correctly exported to mylib
1003 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1004 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1005
1006 // For dependency to libm
1007 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001008 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001009 // ... and not linking to the stub variant
Colin Cross7113d202019-11-20 16:39:12 -08001010 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001011 // ... and is not compiling with the stub
1012 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1013 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1014
1015 // For dependency to libdl
1016 // Ensure that mylib is linking with the specified version of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001017 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001018 // ... and not linking to the other versions of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001019 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28_myapex/libdl.so")
1020 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001021 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001022 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001023 // ... Cflags from stub is correctly exported to mylib
1024 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1025 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001026
1027 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001028 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1029 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1030 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1031 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001032}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001033
1034func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001035 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001036 apex {
1037 name: "myapex",
1038 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001039 native_shared_libs: ["mylib"],
1040 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001041 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001042 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001043 }
1044
1045 apex_key {
1046 name: "myapex.key",
1047 public_key: "testkey.avbpubkey",
1048 private_key: "testkey.pem",
1049 }
1050
1051 prebuilt_etc {
1052 name: "myetc",
1053 src: "myprebuilt",
1054 sub_dir: "foo/bar",
1055 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001056
1057 cc_library {
1058 name: "mylib",
1059 srcs: ["mylib.cpp"],
1060 relative_install_path: "foo/bar",
1061 system_shared_libs: [],
1062 stl: "none",
1063 }
1064
1065 cc_binary {
1066 name: "mybin",
1067 srcs: ["mylib.cpp"],
1068 relative_install_path: "foo/bar",
1069 system_shared_libs: [],
1070 static_executable: true,
1071 stl: "none",
1072 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001073 `)
1074
Sundong Ahnabb64432019-10-22 13:58:29 +09001075 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001076 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1077
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001078 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001079 ensureListContains(t, dirs, "etc")
1080 ensureListContains(t, dirs, "etc/foo")
1081 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001082 ensureListContains(t, dirs, "lib64")
1083 ensureListContains(t, dirs, "lib64/foo")
1084 ensureListContains(t, dirs, "lib64/foo/bar")
1085 ensureListContains(t, dirs, "lib")
1086 ensureListContains(t, dirs, "lib/foo")
1087 ensureListContains(t, dirs, "lib/foo/bar")
1088
Jiyong Parkbd13e442019-03-15 18:10:35 +09001089 ensureListContains(t, dirs, "bin")
1090 ensureListContains(t, dirs, "bin/foo")
1091 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001092}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001093
1094func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001095 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001096 apex {
1097 name: "myapex",
1098 key: "myapex.key",
1099 native_shared_libs: ["mylib"],
1100 use_vendor: true,
1101 }
1102
1103 apex_key {
1104 name: "myapex.key",
1105 public_key: "testkey.avbpubkey",
1106 private_key: "testkey.pem",
1107 }
1108
1109 cc_library {
1110 name: "mylib",
1111 srcs: ["mylib.cpp"],
1112 shared_libs: ["mylib2"],
1113 system_shared_libs: [],
1114 vendor_available: true,
1115 stl: "none",
1116 }
1117
1118 cc_library {
1119 name: "mylib2",
1120 srcs: ["mylib.cpp"],
1121 system_shared_libs: [],
1122 vendor_available: true,
1123 stl: "none",
1124 }
Jooyung Handc782442019-11-01 03:14:38 +09001125 `, func(fs map[string][]byte, config android.Config) {
1126 setUseVendorWhitelistForTest(config, []string{"myapex"})
1127 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001128
1129 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001130 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001131 for _, implicit := range i.Implicits {
1132 inputsList = append(inputsList, implicit.String())
1133 }
1134 }
1135 inputsString := strings.Join(inputsList, " ")
1136
1137 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001138 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1139 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001140
1141 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001142 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1143 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001144}
Jiyong Park16e91a02018-12-20 18:18:08 +09001145
Jooyung Handc782442019-11-01 03:14:38 +09001146func TestUseVendorRestriction(t *testing.T) {
1147 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1148 apex {
1149 name: "myapex",
1150 key: "myapex.key",
1151 use_vendor: true,
1152 }
1153 apex_key {
1154 name: "myapex.key",
1155 public_key: "testkey.avbpubkey",
1156 private_key: "testkey.pem",
1157 }
1158 `, func(fs map[string][]byte, config android.Config) {
1159 setUseVendorWhitelistForTest(config, []string{""})
1160 })
1161 // no error with whitelist
1162 testApex(t, `
1163 apex {
1164 name: "myapex",
1165 key: "myapex.key",
1166 use_vendor: true,
1167 }
1168 apex_key {
1169 name: "myapex.key",
1170 public_key: "testkey.avbpubkey",
1171 private_key: "testkey.pem",
1172 }
1173 `, func(fs map[string][]byte, config android.Config) {
1174 setUseVendorWhitelistForTest(config, []string{"myapex"})
1175 })
1176}
1177
Jooyung Han5c998b92019-06-27 11:30:33 +09001178func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1179 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1180 apex {
1181 name: "myapex",
1182 key: "myapex.key",
1183 native_shared_libs: ["mylib"],
1184 use_vendor: true,
1185 }
1186
1187 apex_key {
1188 name: "myapex.key",
1189 public_key: "testkey.avbpubkey",
1190 private_key: "testkey.pem",
1191 }
1192
1193 cc_library {
1194 name: "mylib",
1195 srcs: ["mylib.cpp"],
1196 system_shared_libs: [],
1197 stl: "none",
1198 }
1199 `)
1200}
1201
Jiyong Park16e91a02018-12-20 18:18:08 +09001202func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001203 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001204 apex {
1205 name: "myapex",
1206 key: "myapex.key",
1207 native_shared_libs: ["mylib"],
1208 }
1209
1210 apex_key {
1211 name: "myapex.key",
1212 public_key: "testkey.avbpubkey",
1213 private_key: "testkey.pem",
1214 }
1215
1216 cc_library {
1217 name: "mylib",
1218 srcs: ["mylib.cpp"],
1219 system_shared_libs: [],
1220 stl: "none",
1221 stubs: {
1222 versions: ["1", "2", "3"],
1223 },
1224 }
1225
1226 cc_binary {
1227 name: "not_in_apex",
1228 srcs: ["mylib.cpp"],
1229 static_libs: ["mylib"],
1230 static_executable: true,
1231 system_shared_libs: [],
1232 stl: "none",
1233 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001234 `)
1235
Colin Cross7113d202019-11-20 16:39:12 -08001236 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001237
1238 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001239 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001240}
Jiyong Park9335a262018-12-24 11:31:58 +09001241
1242func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001243 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001244 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001245 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001246 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001247 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001248 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001249 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001250 }
1251
1252 cc_library {
1253 name: "mylib",
1254 srcs: ["mylib.cpp"],
1255 system_shared_libs: [],
1256 stl: "none",
1257 }
1258
1259 apex_key {
1260 name: "myapex.key",
1261 public_key: "testkey.avbpubkey",
1262 private_key: "testkey.pem",
1263 }
1264
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001265 android_app_certificate {
1266 name: "myapex.certificate",
1267 certificate: "testkey",
1268 }
1269
1270 android_app_certificate {
1271 name: "myapex.certificate.override",
1272 certificate: "testkey.override",
1273 }
1274
Jiyong Park9335a262018-12-24 11:31:58 +09001275 `)
1276
1277 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001278 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001279
1280 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1281 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1282 "vendor/foo/devkeys/testkey.avbpubkey")
1283 }
1284 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1285 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1286 "vendor/foo/devkeys/testkey.pem")
1287 }
1288
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001289 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001290 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001291 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001292 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001293 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001294 }
1295}
Jiyong Park58e364a2019-01-19 19:24:06 +09001296
Jooyung Hanf121a652019-12-17 14:30:11 +09001297func TestCertificate(t *testing.T) {
1298 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1299 ctx, _ := testApex(t, `
1300 apex {
1301 name: "myapex",
1302 key: "myapex.key",
1303 }
1304 apex_key {
1305 name: "myapex.key",
1306 public_key: "testkey.avbpubkey",
1307 private_key: "testkey.pem",
1308 }`)
1309 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1310 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1311 if actual := rule.Args["certificates"]; actual != expected {
1312 t.Errorf("certificates should be %q, not %q", expected, actual)
1313 }
1314 })
1315 t.Run("override when unspecified", func(t *testing.T) {
1316 ctx, _ := testApex(t, `
1317 apex {
1318 name: "myapex_keytest",
1319 key: "myapex.key",
1320 file_contexts: ":myapex-file_contexts",
1321 }
1322 apex_key {
1323 name: "myapex.key",
1324 public_key: "testkey.avbpubkey",
1325 private_key: "testkey.pem",
1326 }
1327 android_app_certificate {
1328 name: "myapex.certificate.override",
1329 certificate: "testkey.override",
1330 }`)
1331 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1332 expected := "testkey.override.x509.pem testkey.override.pk8"
1333 if actual := rule.Args["certificates"]; actual != expected {
1334 t.Errorf("certificates should be %q, not %q", expected, actual)
1335 }
1336 })
1337 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1338 ctx, _ := testApex(t, `
1339 apex {
1340 name: "myapex",
1341 key: "myapex.key",
1342 certificate: ":myapex.certificate",
1343 }
1344 apex_key {
1345 name: "myapex.key",
1346 public_key: "testkey.avbpubkey",
1347 private_key: "testkey.pem",
1348 }
1349 android_app_certificate {
1350 name: "myapex.certificate",
1351 certificate: "testkey",
1352 }`)
1353 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1354 expected := "testkey.x509.pem testkey.pk8"
1355 if actual := rule.Args["certificates"]; actual != expected {
1356 t.Errorf("certificates should be %q, not %q", expected, actual)
1357 }
1358 })
1359 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1360 ctx, _ := testApex(t, `
1361 apex {
1362 name: "myapex_keytest",
1363 key: "myapex.key",
1364 file_contexts: ":myapex-file_contexts",
1365 certificate: ":myapex.certificate",
1366 }
1367 apex_key {
1368 name: "myapex.key",
1369 public_key: "testkey.avbpubkey",
1370 private_key: "testkey.pem",
1371 }
1372 android_app_certificate {
1373 name: "myapex.certificate.override",
1374 certificate: "testkey.override",
1375 }`)
1376 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1377 expected := "testkey.override.x509.pem testkey.override.pk8"
1378 if actual := rule.Args["certificates"]; actual != expected {
1379 t.Errorf("certificates should be %q, not %q", expected, actual)
1380 }
1381 })
1382 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1383 ctx, _ := testApex(t, `
1384 apex {
1385 name: "myapex",
1386 key: "myapex.key",
1387 certificate: "testkey",
1388 }
1389 apex_key {
1390 name: "myapex.key",
1391 public_key: "testkey.avbpubkey",
1392 private_key: "testkey.pem",
1393 }`)
1394 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1395 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1396 if actual := rule.Args["certificates"]; actual != expected {
1397 t.Errorf("certificates should be %q, not %q", expected, actual)
1398 }
1399 })
1400 t.Run("override when specified as <name>", func(t *testing.T) {
1401 ctx, _ := testApex(t, `
1402 apex {
1403 name: "myapex_keytest",
1404 key: "myapex.key",
1405 file_contexts: ":myapex-file_contexts",
1406 certificate: "testkey",
1407 }
1408 apex_key {
1409 name: "myapex.key",
1410 public_key: "testkey.avbpubkey",
1411 private_key: "testkey.pem",
1412 }
1413 android_app_certificate {
1414 name: "myapex.certificate.override",
1415 certificate: "testkey.override",
1416 }`)
1417 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1418 expected := "testkey.override.x509.pem testkey.override.pk8"
1419 if actual := rule.Args["certificates"]; actual != expected {
1420 t.Errorf("certificates should be %q, not %q", expected, actual)
1421 }
1422 })
1423}
1424
Jiyong Park58e364a2019-01-19 19:24:06 +09001425func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001426 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001427 apex {
1428 name: "myapex",
1429 key: "myapex.key",
1430 native_shared_libs: ["mylib"],
1431 }
1432
1433 apex {
1434 name: "otherapex",
1435 key: "myapex.key",
1436 native_shared_libs: ["mylib"],
1437 }
1438
1439 apex_key {
1440 name: "myapex.key",
1441 public_key: "testkey.avbpubkey",
1442 private_key: "testkey.pem",
1443 }
1444
1445 cc_library {
1446 name: "mylib",
1447 srcs: ["mylib.cpp"],
1448 system_shared_libs: [],
1449 stl: "none",
1450 }
1451 `)
1452
Jooyung Han6b8459b2019-10-30 08:29:25 +09001453 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001454 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001455 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001456 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1457 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001458
Jooyung Han6b8459b2019-10-30 08:29:25 +09001459 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001460 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001461 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001462 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1463 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001464
Jooyung Han6b8459b2019-10-30 08:29:25 +09001465 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001466 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001467 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001468 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1469 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001470}
Jiyong Park7e636d02019-01-28 16:16:54 +09001471
1472func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001473 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001474 apex {
1475 name: "myapex",
1476 key: "myapex.key",
1477 native_shared_libs: ["mylib"],
1478 }
1479
1480 apex_key {
1481 name: "myapex.key",
1482 public_key: "testkey.avbpubkey",
1483 private_key: "testkey.pem",
1484 }
1485
1486 cc_library_headers {
1487 name: "mylib_headers",
1488 export_include_dirs: ["my_include"],
1489 system_shared_libs: [],
1490 stl: "none",
1491 }
1492
1493 cc_library {
1494 name: "mylib",
1495 srcs: ["mylib.cpp"],
1496 system_shared_libs: [],
1497 stl: "none",
1498 header_libs: ["mylib_headers"],
1499 export_header_lib_headers: ["mylib_headers"],
1500 stubs: {
1501 versions: ["1", "2", "3"],
1502 },
1503 }
1504
1505 cc_library {
1506 name: "otherlib",
1507 srcs: ["mylib.cpp"],
1508 system_shared_libs: [],
1509 stl: "none",
1510 shared_libs: ["mylib"],
1511 }
1512 `)
1513
Colin Cross7113d202019-11-20 16:39:12 -08001514 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001515
1516 // Ensure that the include path of the header lib is exported to 'otherlib'
1517 ensureContains(t, cFlags, "-Imy_include")
1518}
Alex Light9670d332019-01-29 18:07:33 -08001519
Jooyung Han31c470b2019-10-18 16:26:59 +09001520func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1521 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001522 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001523 copyCmds := apexRule.Args["copy_commands"]
1524 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001525 var failed bool
1526 var surplus []string
1527 filesMatched := make(map[string]bool)
1528 addContent := func(content string) {
1529 for _, expected := range files {
1530 if matched, _ := path.Match(expected, content); matched {
1531 filesMatched[expected] = true
1532 return
1533 }
1534 }
1535 surplus = append(surplus, content)
1536 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001537 for _, cmd := range strings.Split(copyCmds, "&&") {
1538 cmd = strings.TrimSpace(cmd)
1539 if cmd == "" {
1540 continue
1541 }
1542 terms := strings.Split(cmd, " ")
1543 switch terms[0] {
1544 case "mkdir":
1545 case "cp":
1546 if len(terms) != 3 {
1547 t.Fatal("copyCmds contains invalid cp command", cmd)
1548 }
1549 dst := terms[2]
1550 index := strings.Index(dst, imageApexDir)
1551 if index == -1 {
1552 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1553 }
1554 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001555 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001556 default:
1557 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1558 }
1559 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001560
Jooyung Han31c470b2019-10-18 16:26:59 +09001561 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001562 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001563 t.Log("surplus files", surplus)
1564 failed = true
1565 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001566
1567 if len(files) > len(filesMatched) {
1568 var missing []string
1569 for _, expected := range files {
1570 if !filesMatched[expected] {
1571 missing = append(missing, expected)
1572 }
1573 }
1574 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001575 t.Log("missing files", missing)
1576 failed = true
1577 }
1578 if failed {
1579 t.Fail()
1580 }
1581}
1582
Jooyung Han344d5432019-08-23 11:17:39 +09001583func TestVndkApexCurrent(t *testing.T) {
1584 ctx, _ := testApex(t, `
1585 apex_vndk {
1586 name: "myapex",
1587 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001588 }
1589
1590 apex_key {
1591 name: "myapex.key",
1592 public_key: "testkey.avbpubkey",
1593 private_key: "testkey.pem",
1594 }
1595
1596 cc_library {
1597 name: "libvndk",
1598 srcs: ["mylib.cpp"],
1599 vendor_available: true,
1600 vndk: {
1601 enabled: true,
1602 },
1603 system_shared_libs: [],
1604 stl: "none",
1605 }
1606
1607 cc_library {
1608 name: "libvndksp",
1609 srcs: ["mylib.cpp"],
1610 vendor_available: true,
1611 vndk: {
1612 enabled: true,
1613 support_system_process: true,
1614 },
1615 system_shared_libs: [],
1616 stl: "none",
1617 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001618 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001619
Jooyung Han31c470b2019-10-18 16:26:59 +09001620 ensureExactContents(t, ctx, "myapex", []string{
1621 "lib/libvndk.so",
1622 "lib/libvndksp.so",
1623 "lib64/libvndk.so",
1624 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001625 "etc/llndk.libraries.VER.txt",
1626 "etc/vndkcore.libraries.VER.txt",
1627 "etc/vndksp.libraries.VER.txt",
1628 "etc/vndkprivate.libraries.VER.txt",
1629 "etc/vndkcorevariant.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001630 })
Jooyung Han344d5432019-08-23 11:17:39 +09001631}
1632
1633func TestVndkApexWithPrebuilt(t *testing.T) {
1634 ctx, _ := testApex(t, `
1635 apex_vndk {
1636 name: "myapex",
1637 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001638 }
1639
1640 apex_key {
1641 name: "myapex.key",
1642 public_key: "testkey.avbpubkey",
1643 private_key: "testkey.pem",
1644 }
1645
1646 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001647 name: "libvndk",
1648 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001649 vendor_available: true,
1650 vndk: {
1651 enabled: true,
1652 },
1653 system_shared_libs: [],
1654 stl: "none",
1655 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001656
1657 cc_prebuilt_library_shared {
1658 name: "libvndk.arm",
1659 srcs: ["libvndk.arm.so"],
1660 vendor_available: true,
1661 vndk: {
1662 enabled: true,
1663 },
1664 enabled: false,
1665 arch: {
1666 arm: {
1667 enabled: true,
1668 },
1669 },
1670 system_shared_libs: [],
1671 stl: "none",
1672 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001673 `+vndkLibrariesTxtFiles("current"),
1674 withFiles(map[string][]byte{
1675 "libvndk.so": nil,
1676 "libvndk.arm.so": nil,
1677 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001678
Jooyung Han31c470b2019-10-18 16:26:59 +09001679 ensureExactContents(t, ctx, "myapex", []string{
1680 "lib/libvndk.so",
1681 "lib/libvndk.arm.so",
1682 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001683 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001684 })
Jooyung Han344d5432019-08-23 11:17:39 +09001685}
1686
Jooyung Han39edb6c2019-11-06 16:53:07 +09001687func vndkLibrariesTxtFiles(vers ...string) (result string) {
1688 for _, v := range vers {
1689 if v == "current" {
1690 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkcorevariant"} {
1691 result += `
1692 vndk_libraries_txt {
1693 name: "` + txt + `.libraries.txt",
1694 }
1695 `
1696 }
1697 } else {
1698 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1699 result += `
1700 prebuilt_etc {
1701 name: "` + txt + `.libraries.` + v + `.txt",
1702 src: "dummy.txt",
1703 }
1704 `
1705 }
1706 }
1707 }
1708 return
1709}
1710
Jooyung Han344d5432019-08-23 11:17:39 +09001711func TestVndkApexVersion(t *testing.T) {
1712 ctx, _ := testApex(t, `
1713 apex_vndk {
1714 name: "myapex_v27",
1715 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001716 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001717 vndk_version: "27",
1718 }
1719
1720 apex_key {
1721 name: "myapex.key",
1722 public_key: "testkey.avbpubkey",
1723 private_key: "testkey.pem",
1724 }
1725
Jooyung Han31c470b2019-10-18 16:26:59 +09001726 vndk_prebuilt_shared {
1727 name: "libvndk27",
1728 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001729 vendor_available: true,
1730 vndk: {
1731 enabled: true,
1732 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001733 target_arch: "arm64",
1734 arch: {
1735 arm: {
1736 srcs: ["libvndk27_arm.so"],
1737 },
1738 arm64: {
1739 srcs: ["libvndk27_arm64.so"],
1740 },
1741 },
Jooyung Han344d5432019-08-23 11:17:39 +09001742 }
1743
1744 vndk_prebuilt_shared {
1745 name: "libvndk27",
1746 version: "27",
1747 vendor_available: true,
1748 vndk: {
1749 enabled: true,
1750 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001751 target_arch: "x86_64",
1752 arch: {
1753 x86: {
1754 srcs: ["libvndk27_x86.so"],
1755 },
1756 x86_64: {
1757 srcs: ["libvndk27_x86_64.so"],
1758 },
1759 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001760 }
1761 `+vndkLibrariesTxtFiles("27"),
1762 withFiles(map[string][]byte{
1763 "libvndk27_arm.so": nil,
1764 "libvndk27_arm64.so": nil,
1765 "libvndk27_x86.so": nil,
1766 "libvndk27_x86_64.so": nil,
1767 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001768
Jooyung Han31c470b2019-10-18 16:26:59 +09001769 ensureExactContents(t, ctx, "myapex_v27", []string{
1770 "lib/libvndk27_arm.so",
1771 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001772 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001773 })
Jooyung Han344d5432019-08-23 11:17:39 +09001774}
1775
1776func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1777 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1778 apex_vndk {
1779 name: "myapex_v27",
1780 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001781 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001782 vndk_version: "27",
1783 }
1784 apex_vndk {
1785 name: "myapex_v27_other",
1786 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001787 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001788 vndk_version: "27",
1789 }
1790
1791 apex_key {
1792 name: "myapex.key",
1793 public_key: "testkey.avbpubkey",
1794 private_key: "testkey.pem",
1795 }
1796
1797 cc_library {
1798 name: "libvndk",
1799 srcs: ["mylib.cpp"],
1800 vendor_available: true,
1801 vndk: {
1802 enabled: true,
1803 },
1804 system_shared_libs: [],
1805 stl: "none",
1806 }
1807
1808 vndk_prebuilt_shared {
1809 name: "libvndk",
1810 version: "27",
1811 vendor_available: true,
1812 vndk: {
1813 enabled: true,
1814 },
1815 srcs: ["libvndk.so"],
1816 }
1817 `, withFiles(map[string][]byte{
1818 "libvndk.so": nil,
1819 }))
1820}
1821
Jooyung Han90eee022019-10-01 20:02:42 +09001822func TestVndkApexNameRule(t *testing.T) {
1823 ctx, _ := testApex(t, `
1824 apex_vndk {
1825 name: "myapex",
1826 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001827 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001828 }
1829 apex_vndk {
1830 name: "myapex_v28",
1831 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001832 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001833 vndk_version: "28",
1834 }
1835 apex_key {
1836 name: "myapex.key",
1837 public_key: "testkey.avbpubkey",
1838 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001839 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001840
1841 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001842 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001843 actual := proptools.String(bundle.properties.Apex_name)
1844 if !reflect.DeepEqual(actual, expected) {
1845 t.Errorf("Got '%v', expected '%v'", actual, expected)
1846 }
1847 }
1848
1849 assertApexName("com.android.vndk.vVER", "myapex")
1850 assertApexName("com.android.vndk.v28", "myapex_v28")
1851}
1852
Jooyung Han344d5432019-08-23 11:17:39 +09001853func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1854 ctx, _ := testApex(t, `
1855 apex_vndk {
1856 name: "myapex",
1857 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001858 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001859 }
1860
1861 apex_key {
1862 name: "myapex.key",
1863 public_key: "testkey.avbpubkey",
1864 private_key: "testkey.pem",
1865 }
1866
1867 cc_library {
1868 name: "libvndk",
1869 srcs: ["mylib.cpp"],
1870 vendor_available: true,
1871 native_bridge_supported: true,
1872 host_supported: true,
1873 vndk: {
1874 enabled: true,
1875 },
1876 system_shared_libs: [],
1877 stl: "none",
1878 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001879 `+vndkLibrariesTxtFiles("current"),
1880 withTargets(map[android.OsType][]android.Target{
1881 android.Android: []android.Target{
1882 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1883 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1884 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1885 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1886 },
1887 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001888
Jooyung Han31c470b2019-10-18 16:26:59 +09001889 ensureExactContents(t, ctx, "myapex", []string{
1890 "lib/libvndk.so",
1891 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001892 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001893 })
Jooyung Han344d5432019-08-23 11:17:39 +09001894}
1895
1896func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1897 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1898 apex_vndk {
1899 name: "myapex",
1900 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001901 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001902 native_bridge_supported: true,
1903 }
1904
1905 apex_key {
1906 name: "myapex.key",
1907 public_key: "testkey.avbpubkey",
1908 private_key: "testkey.pem",
1909 }
1910
1911 cc_library {
1912 name: "libvndk",
1913 srcs: ["mylib.cpp"],
1914 vendor_available: true,
1915 native_bridge_supported: true,
1916 host_supported: true,
1917 vndk: {
1918 enabled: true,
1919 },
1920 system_shared_libs: [],
1921 stl: "none",
1922 }
1923 `)
1924}
1925
Jooyung Han31c470b2019-10-18 16:26:59 +09001926func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001927 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001928 apex_vndk {
1929 name: "myapex_v27",
1930 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001931 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001932 vndk_version: "27",
1933 }
1934
1935 apex_key {
1936 name: "myapex.key",
1937 public_key: "testkey.avbpubkey",
1938 private_key: "testkey.pem",
1939 }
1940
1941 vndk_prebuilt_shared {
1942 name: "libvndk27",
1943 version: "27",
1944 target_arch: "arm",
1945 vendor_available: true,
1946 vndk: {
1947 enabled: true,
1948 },
1949 arch: {
1950 arm: {
1951 srcs: ["libvndk27.so"],
1952 }
1953 },
1954 }
1955
1956 vndk_prebuilt_shared {
1957 name: "libvndk27",
1958 version: "27",
1959 target_arch: "arm",
1960 binder32bit: true,
1961 vendor_available: true,
1962 vndk: {
1963 enabled: true,
1964 },
1965 arch: {
1966 arm: {
1967 srcs: ["libvndk27binder32.so"],
1968 }
1969 },
1970 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001971 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001972 withFiles(map[string][]byte{
1973 "libvndk27.so": nil,
1974 "libvndk27binder32.so": nil,
1975 }),
1976 withBinder32bit,
1977 withTargets(map[android.OsType][]android.Target{
1978 android.Android: []android.Target{
1979 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1980 },
1981 }),
1982 )
1983
1984 ensureExactContents(t, ctx, "myapex_v27", []string{
1985 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001986 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001987 })
1988}
1989
Jooyung Hane1633032019-08-01 17:41:43 +09001990func TestDependenciesInApexManifest(t *testing.T) {
1991 ctx, _ := testApex(t, `
1992 apex {
1993 name: "myapex_nodep",
1994 key: "myapex.key",
1995 native_shared_libs: ["lib_nodep"],
1996 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001997 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001998 }
1999
2000 apex {
2001 name: "myapex_dep",
2002 key: "myapex.key",
2003 native_shared_libs: ["lib_dep"],
2004 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002005 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002006 }
2007
2008 apex {
2009 name: "myapex_provider",
2010 key: "myapex.key",
2011 native_shared_libs: ["libfoo"],
2012 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002013 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002014 }
2015
2016 apex {
2017 name: "myapex_selfcontained",
2018 key: "myapex.key",
2019 native_shared_libs: ["lib_dep", "libfoo"],
2020 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002021 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002022 }
2023
2024 apex_key {
2025 name: "myapex.key",
2026 public_key: "testkey.avbpubkey",
2027 private_key: "testkey.pem",
2028 }
2029
2030 cc_library {
2031 name: "lib_nodep",
2032 srcs: ["mylib.cpp"],
2033 system_shared_libs: [],
2034 stl: "none",
2035 }
2036
2037 cc_library {
2038 name: "lib_dep",
2039 srcs: ["mylib.cpp"],
2040 shared_libs: ["libfoo"],
2041 system_shared_libs: [],
2042 stl: "none",
2043 }
2044
2045 cc_library {
2046 name: "libfoo",
2047 srcs: ["mytest.cpp"],
2048 stubs: {
2049 versions: ["1"],
2050 },
2051 system_shared_libs: [],
2052 stl: "none",
2053 }
2054 `)
2055
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002056 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002057 var provideNativeLibs, requireNativeLibs []string
2058
Sundong Ahnabb64432019-10-22 13:58:29 +09002059 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002060 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2061 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002062 ensureListEmpty(t, provideNativeLibs)
2063 ensureListEmpty(t, requireNativeLibs)
2064
Sundong Ahnabb64432019-10-22 13:58:29 +09002065 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002066 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2067 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002068 ensureListEmpty(t, provideNativeLibs)
2069 ensureListContains(t, requireNativeLibs, "libfoo.so")
2070
Sundong Ahnabb64432019-10-22 13:58:29 +09002071 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002072 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2073 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002074 ensureListContains(t, provideNativeLibs, "libfoo.so")
2075 ensureListEmpty(t, requireNativeLibs)
2076
Sundong Ahnabb64432019-10-22 13:58:29 +09002077 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002078 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2079 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002080 ensureListContains(t, provideNativeLibs, "libfoo.so")
2081 ensureListEmpty(t, requireNativeLibs)
2082}
2083
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002084func TestApexName(t *testing.T) {
2085 ctx, _ := testApex(t, `
2086 apex {
2087 name: "myapex",
2088 key: "myapex.key",
2089 apex_name: "com.android.myapex",
2090 }
2091
2092 apex_key {
2093 name: "myapex.key",
2094 public_key: "testkey.avbpubkey",
2095 private_key: "testkey.pem",
2096 }
2097 `)
2098
Sundong Ahnabb64432019-10-22 13:58:29 +09002099 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002100 apexManifestRule := module.Rule("apexManifestRule")
2101 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2102 apexRule := module.Rule("apexRule")
2103 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
2104}
2105
Alex Light0851b882019-02-07 13:20:53 -08002106func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002107 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002108 apex {
2109 name: "myapex",
2110 key: "myapex.key",
2111 native_shared_libs: ["mylib_common"],
2112 }
2113
2114 apex_key {
2115 name: "myapex.key",
2116 public_key: "testkey.avbpubkey",
2117 private_key: "testkey.pem",
2118 }
2119
2120 cc_library {
2121 name: "mylib_common",
2122 srcs: ["mylib.cpp"],
2123 system_shared_libs: [],
2124 stl: "none",
2125 }
2126 `)
2127
Sundong Ahnabb64432019-10-22 13:58:29 +09002128 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002129 apexRule := module.Rule("apexRule")
2130 copyCmds := apexRule.Args["copy_commands"]
2131
2132 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2133 t.Log("Apex was a test apex!")
2134 t.Fail()
2135 }
2136 // Ensure that main rule creates an output
2137 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2138
2139 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002140 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002141
2142 // Ensure that both direct and indirect deps are copied into apex
2143 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2144
Colin Cross7113d202019-11-20 16:39:12 -08002145 // Ensure that the platform variant ends with _shared
2146 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002147
2148 if !android.InAnyApex("mylib_common") {
2149 t.Log("Found mylib_common not in any apex!")
2150 t.Fail()
2151 }
2152}
2153
2154func TestTestApex(t *testing.T) {
2155 if android.InAnyApex("mylib_common_test") {
2156 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!")
2157 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002158 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002159 apex_test {
2160 name: "myapex",
2161 key: "myapex.key",
2162 native_shared_libs: ["mylib_common_test"],
2163 }
2164
2165 apex_key {
2166 name: "myapex.key",
2167 public_key: "testkey.avbpubkey",
2168 private_key: "testkey.pem",
2169 }
2170
2171 cc_library {
2172 name: "mylib_common_test",
2173 srcs: ["mylib.cpp"],
2174 system_shared_libs: [],
2175 stl: "none",
2176 }
2177 `)
2178
Sundong Ahnabb64432019-10-22 13:58:29 +09002179 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002180 apexRule := module.Rule("apexRule")
2181 copyCmds := apexRule.Args["copy_commands"]
2182
2183 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2184 t.Log("Apex was not a test apex!")
2185 t.Fail()
2186 }
2187 // Ensure that main rule creates an output
2188 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2189
2190 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002191 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002192
2193 // Ensure that both direct and indirect deps are copied into apex
2194 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2195
Colin Cross7113d202019-11-20 16:39:12 -08002196 // Ensure that the platform variant ends with _shared
2197 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002198
2199 if android.InAnyApex("mylib_common_test") {
2200 t.Log("Found mylib_common_test in some apex!")
2201 t.Fail()
2202 }
2203}
2204
Alex Light9670d332019-01-29 18:07:33 -08002205func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002206 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002207 apex {
2208 name: "myapex",
2209 key: "myapex.key",
2210 multilib: {
2211 first: {
2212 native_shared_libs: ["mylib_common"],
2213 }
2214 },
2215 target: {
2216 android: {
2217 multilib: {
2218 first: {
2219 native_shared_libs: ["mylib"],
2220 }
2221 }
2222 },
2223 host: {
2224 multilib: {
2225 first: {
2226 native_shared_libs: ["mylib2"],
2227 }
2228 }
2229 }
2230 }
2231 }
2232
2233 apex_key {
2234 name: "myapex.key",
2235 public_key: "testkey.avbpubkey",
2236 private_key: "testkey.pem",
2237 }
2238
2239 cc_library {
2240 name: "mylib",
2241 srcs: ["mylib.cpp"],
2242 system_shared_libs: [],
2243 stl: "none",
2244 }
2245
2246 cc_library {
2247 name: "mylib_common",
2248 srcs: ["mylib.cpp"],
2249 system_shared_libs: [],
2250 stl: "none",
2251 compile_multilib: "first",
2252 }
2253
2254 cc_library {
2255 name: "mylib2",
2256 srcs: ["mylib.cpp"],
2257 system_shared_libs: [],
2258 stl: "none",
2259 compile_multilib: "first",
2260 }
2261 `)
2262
Sundong Ahnabb64432019-10-22 13:58:29 +09002263 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002264 copyCmds := apexRule.Args["copy_commands"]
2265
2266 // Ensure that main rule creates an output
2267 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2268
2269 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002270 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2271 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2272 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002273
2274 // Ensure that both direct and indirect deps are copied into apex
2275 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2276 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2277 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2278
Colin Cross7113d202019-11-20 16:39:12 -08002279 // Ensure that the platform variant ends with _shared
2280 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2281 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2282 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002283}
Jiyong Park04480cf2019-02-06 00:16:29 +09002284
2285func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002286 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002287 apex {
2288 name: "myapex",
2289 key: "myapex.key",
2290 binaries: ["myscript"],
2291 }
2292
2293 apex_key {
2294 name: "myapex.key",
2295 public_key: "testkey.avbpubkey",
2296 private_key: "testkey.pem",
2297 }
2298
2299 sh_binary {
2300 name: "myscript",
2301 src: "mylib.cpp",
2302 filename: "myscript.sh",
2303 sub_dir: "script",
2304 }
2305 `)
2306
Sundong Ahnabb64432019-10-22 13:58:29 +09002307 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002308 copyCmds := apexRule.Args["copy_commands"]
2309
2310 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2311}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002312
Jooyung Han91df2082019-11-20 01:49:42 +09002313func TestApexInVariousPartition(t *testing.T) {
2314 testcases := []struct {
2315 propName, parition, flattenedPartition string
2316 }{
2317 {"", "system", "system_ext"},
2318 {"product_specific: true", "product", "product"},
2319 {"soc_specific: true", "vendor", "vendor"},
2320 {"proprietary: true", "vendor", "vendor"},
2321 {"vendor: true", "vendor", "vendor"},
2322 {"system_ext_specific: true", "system_ext", "system_ext"},
2323 }
2324 for _, tc := range testcases {
2325 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2326 ctx, _ := testApex(t, `
2327 apex {
2328 name: "myapex",
2329 key: "myapex.key",
2330 `+tc.propName+`
2331 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002332
Jooyung Han91df2082019-11-20 01:49:42 +09002333 apex_key {
2334 name: "myapex.key",
2335 public_key: "testkey.avbpubkey",
2336 private_key: "testkey.pem",
2337 }
2338 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002339
Jooyung Han91df2082019-11-20 01:49:42 +09002340 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2341 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2342 actual := apex.installDir.String()
2343 if actual != expected {
2344 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2345 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002346
Jooyung Han91df2082019-11-20 01:49:42 +09002347 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2348 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2349 actual = flattened.installDir.String()
2350 if actual != expected {
2351 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2352 }
2353 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002354 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002355}
Jiyong Park67882562019-03-21 01:11:21 +09002356
Jooyung Han54aca7b2019-11-20 02:26:02 +09002357func TestFileContexts(t *testing.T) {
2358 ctx, _ := testApex(t, `
2359 apex {
2360 name: "myapex",
2361 key: "myapex.key",
2362 }
2363
2364 apex_key {
2365 name: "myapex.key",
2366 public_key: "testkey.avbpubkey",
2367 private_key: "testkey.pem",
2368 }
2369 `)
2370 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2371 apexRule := module.Rule("apexRule")
2372 actual := apexRule.Args["file_contexts"]
2373 expected := "system/sepolicy/apex/myapex-file_contexts"
2374 if actual != expected {
2375 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2376 }
2377
2378 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2379 apex {
2380 name: "myapex",
2381 key: "myapex.key",
2382 file_contexts: "my_own_file_contexts",
2383 }
2384
2385 apex_key {
2386 name: "myapex.key",
2387 public_key: "testkey.avbpubkey",
2388 private_key: "testkey.pem",
2389 }
2390 `, withFiles(map[string][]byte{
2391 "my_own_file_contexts": nil,
2392 }))
2393
2394 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2395 apex {
2396 name: "myapex",
2397 key: "myapex.key",
2398 product_specific: true,
2399 file_contexts: "product_specific_file_contexts",
2400 }
2401
2402 apex_key {
2403 name: "myapex.key",
2404 public_key: "testkey.avbpubkey",
2405 private_key: "testkey.pem",
2406 }
2407 `)
2408
2409 ctx, _ = testApex(t, `
2410 apex {
2411 name: "myapex",
2412 key: "myapex.key",
2413 product_specific: true,
2414 file_contexts: "product_specific_file_contexts",
2415 }
2416
2417 apex_key {
2418 name: "myapex.key",
2419 public_key: "testkey.avbpubkey",
2420 private_key: "testkey.pem",
2421 }
2422 `, withFiles(map[string][]byte{
2423 "product_specific_file_contexts": nil,
2424 }))
2425 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2426 apexRule = module.Rule("apexRule")
2427 actual = apexRule.Args["file_contexts"]
2428 expected = "product_specific_file_contexts"
2429 if actual != expected {
2430 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2431 }
2432
2433 ctx, _ = testApex(t, `
2434 apex {
2435 name: "myapex",
2436 key: "myapex.key",
2437 product_specific: true,
2438 file_contexts: ":my-file-contexts",
2439 }
2440
2441 apex_key {
2442 name: "myapex.key",
2443 public_key: "testkey.avbpubkey",
2444 private_key: "testkey.pem",
2445 }
2446
2447 filegroup {
2448 name: "my-file-contexts",
2449 srcs: ["product_specific_file_contexts"],
2450 }
2451 `, withFiles(map[string][]byte{
2452 "product_specific_file_contexts": nil,
2453 }))
2454 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2455 apexRule = module.Rule("apexRule")
2456 actual = apexRule.Args["file_contexts"]
2457 expected = "product_specific_file_contexts"
2458 if actual != expected {
2459 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2460 }
2461}
2462
Jiyong Park67882562019-03-21 01:11:21 +09002463func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002464 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002465 apex_key {
2466 name: "myapex.key",
2467 public_key: ":my.avbpubkey",
2468 private_key: ":my.pem",
2469 product_specific: true,
2470 }
2471
2472 filegroup {
2473 name: "my.avbpubkey",
2474 srcs: ["testkey2.avbpubkey"],
2475 }
2476
2477 filegroup {
2478 name: "my.pem",
2479 srcs: ["testkey2.pem"],
2480 }
2481 `)
2482
2483 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2484 expected_pubkey := "testkey2.avbpubkey"
2485 actual_pubkey := apex_key.public_key_file.String()
2486 if actual_pubkey != expected_pubkey {
2487 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2488 }
2489 expected_privkey := "testkey2.pem"
2490 actual_privkey := apex_key.private_key_file.String()
2491 if actual_privkey != expected_privkey {
2492 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2493 }
2494}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002495
2496func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002497 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002498 prebuilt_apex {
2499 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002500 arch: {
2501 arm64: {
2502 src: "myapex-arm64.apex",
2503 },
2504 arm: {
2505 src: "myapex-arm.apex",
2506 },
2507 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002508 }
2509 `)
2510
2511 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2512
Jiyong Parkc95714e2019-03-29 14:23:10 +09002513 expectedInput := "myapex-arm64.apex"
2514 if prebuilt.inputApex.String() != expectedInput {
2515 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2516 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002517}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002518
2519func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002520 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002521 prebuilt_apex {
2522 name: "myapex",
2523 src: "myapex-arm.apex",
2524 filename: "notmyapex.apex",
2525 }
2526 `)
2527
2528 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2529
2530 expected := "notmyapex.apex"
2531 if p.installFilename != expected {
2532 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2533 }
2534}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002535
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002536func TestPrebuiltOverrides(t *testing.T) {
2537 ctx, config := testApex(t, `
2538 prebuilt_apex {
2539 name: "myapex.prebuilt",
2540 src: "myapex-arm.apex",
2541 overrides: [
2542 "myapex",
2543 ],
2544 }
2545 `)
2546
2547 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2548
2549 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002550 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002551 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002552 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002553 }
2554}
2555
Roland Levillain630846d2019-06-26 12:48:34 +01002556func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002557 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002558 apex_test {
2559 name: "myapex",
2560 key: "myapex.key",
2561 tests: [
2562 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002563 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002564 ],
2565 }
2566
2567 apex_key {
2568 name: "myapex.key",
2569 public_key: "testkey.avbpubkey",
2570 private_key: "testkey.pem",
2571 }
2572
2573 cc_test {
2574 name: "mytest",
2575 gtest: false,
2576 srcs: ["mytest.cpp"],
2577 relative_install_path: "test",
2578 system_shared_libs: [],
2579 static_executable: true,
2580 stl: "none",
2581 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002582
2583 cc_test {
2584 name: "mytests",
2585 gtest: false,
2586 srcs: [
2587 "mytest1.cpp",
2588 "mytest2.cpp",
2589 "mytest3.cpp",
2590 ],
2591 test_per_src: true,
2592 relative_install_path: "test",
2593 system_shared_libs: [],
2594 static_executable: true,
2595 stl: "none",
2596 }
Roland Levillain630846d2019-06-26 12:48:34 +01002597 `)
2598
Sundong Ahnabb64432019-10-22 13:58:29 +09002599 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002600 copyCmds := apexRule.Args["copy_commands"]
2601
2602 // Ensure that test dep is copied into apex.
2603 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002604
2605 // Ensure that test deps built with `test_per_src` are copied into apex.
2606 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2607 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2608 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002609
2610 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002611 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002612 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2613 name := apexBundle.BaseModuleName()
2614 prefix := "TARGET_"
2615 var builder strings.Builder
2616 data.Custom(&builder, name, prefix, "", data)
2617 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002618 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2619 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2620 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2621 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002622 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002623 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002624 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002625}
2626
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002627func TestInstallExtraFlattenedApexes(t *testing.T) {
2628 ctx, config := testApex(t, `
2629 apex {
2630 name: "myapex",
2631 key: "myapex.key",
2632 }
2633 apex_key {
2634 name: "myapex.key",
2635 public_key: "testkey.avbpubkey",
2636 private_key: "testkey.pem",
2637 }
2638 `, func(fs map[string][]byte, config android.Config) {
2639 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2640 })
2641 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2642 ensureListContains(t, ab.externalDeps, "myapex.flattened")
2643 mk := android.AndroidMkDataForTest(t, config, "", ab)
2644 var builder strings.Builder
2645 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2646 androidMk := builder.String()
2647 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2648}
2649
Jooyung Han5c998b92019-06-27 11:30:33 +09002650func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002651 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002652 apex {
2653 name: "myapex",
2654 key: "myapex.key",
2655 native_shared_libs: ["mylib"],
2656 uses: ["commonapex"],
2657 }
2658
2659 apex {
2660 name: "commonapex",
2661 key: "myapex.key",
2662 native_shared_libs: ["libcommon"],
2663 provide_cpp_shared_libs: true,
2664 }
2665
2666 apex_key {
2667 name: "myapex.key",
2668 public_key: "testkey.avbpubkey",
2669 private_key: "testkey.pem",
2670 }
2671
2672 cc_library {
2673 name: "mylib",
2674 srcs: ["mylib.cpp"],
2675 shared_libs: ["libcommon"],
2676 system_shared_libs: [],
2677 stl: "none",
2678 }
2679
2680 cc_library {
2681 name: "libcommon",
2682 srcs: ["mylib_common.cpp"],
2683 system_shared_libs: [],
2684 stl: "none",
2685 }
2686 `)
2687
Sundong Ahnabb64432019-10-22 13:58:29 +09002688 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002689 apexRule1 := module1.Rule("apexRule")
2690 copyCmds1 := apexRule1.Args["copy_commands"]
2691
Sundong Ahnabb64432019-10-22 13:58:29 +09002692 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002693 apexRule2 := module2.Rule("apexRule")
2694 copyCmds2 := apexRule2.Args["copy_commands"]
2695
Colin Cross7113d202019-11-20 16:39:12 -08002696 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2697 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002698 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2699 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2700 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2701}
2702
2703func TestApexUsesFailsIfNotProvided(t *testing.T) {
2704 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2705 apex {
2706 name: "myapex",
2707 key: "myapex.key",
2708 uses: ["commonapex"],
2709 }
2710
2711 apex {
2712 name: "commonapex",
2713 key: "myapex.key",
2714 }
2715
2716 apex_key {
2717 name: "myapex.key",
2718 public_key: "testkey.avbpubkey",
2719 private_key: "testkey.pem",
2720 }
2721 `)
2722 testApexError(t, `uses: "commonapex" is not a provider`, `
2723 apex {
2724 name: "myapex",
2725 key: "myapex.key",
2726 uses: ["commonapex"],
2727 }
2728
2729 cc_library {
2730 name: "commonapex",
2731 system_shared_libs: [],
2732 stl: "none",
2733 }
2734
2735 apex_key {
2736 name: "myapex.key",
2737 public_key: "testkey.avbpubkey",
2738 private_key: "testkey.pem",
2739 }
2740 `)
2741}
2742
2743func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2744 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2745 apex {
2746 name: "myapex",
2747 key: "myapex.key",
2748 use_vendor: true,
2749 uses: ["commonapex"],
2750 }
2751
2752 apex {
2753 name: "commonapex",
2754 key: "myapex.key",
2755 provide_cpp_shared_libs: true,
2756 }
2757
2758 apex_key {
2759 name: "myapex.key",
2760 public_key: "testkey.avbpubkey",
2761 private_key: "testkey.pem",
2762 }
Jooyung Handc782442019-11-01 03:14:38 +09002763 `, func(fs map[string][]byte, config android.Config) {
2764 setUseVendorWhitelistForTest(config, []string{"myapex"})
2765 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002766}
2767
Jooyung Hand48f3c32019-08-23 11:18:57 +09002768func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2769 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2770 apex {
2771 name: "myapex",
2772 key: "myapex.key",
2773 native_shared_libs: ["libfoo"],
2774 }
2775
2776 apex_key {
2777 name: "myapex.key",
2778 public_key: "testkey.avbpubkey",
2779 private_key: "testkey.pem",
2780 }
2781
2782 cc_library {
2783 name: "libfoo",
2784 stl: "none",
2785 system_shared_libs: [],
2786 enabled: false,
2787 }
2788 `)
2789 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2790 apex {
2791 name: "myapex",
2792 key: "myapex.key",
2793 java_libs: ["myjar"],
2794 }
2795
2796 apex_key {
2797 name: "myapex.key",
2798 public_key: "testkey.avbpubkey",
2799 private_key: "testkey.pem",
2800 }
2801
2802 java_library {
2803 name: "myjar",
2804 srcs: ["foo/bar/MyClass.java"],
2805 sdk_version: "none",
2806 system_modules: "none",
2807 compile_dex: true,
2808 enabled: false,
2809 }
2810 `)
2811}
2812
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002813func TestApexWithApps(t *testing.T) {
2814 ctx, _ := testApex(t, `
2815 apex {
2816 name: "myapex",
2817 key: "myapex.key",
2818 apps: [
2819 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002820 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002821 ],
2822 }
2823
2824 apex_key {
2825 name: "myapex.key",
2826 public_key: "testkey.avbpubkey",
2827 private_key: "testkey.pem",
2828 }
2829
2830 android_app {
2831 name: "AppFoo",
2832 srcs: ["foo/bar/MyClass.java"],
2833 sdk_version: "none",
2834 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002835 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002836 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002837
2838 android_app {
2839 name: "AppFooPriv",
2840 srcs: ["foo/bar/MyClass.java"],
2841 sdk_version: "none",
2842 system_modules: "none",
2843 privileged: true,
2844 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002845
2846 cc_library_shared {
2847 name: "libjni",
2848 srcs: ["mylib.cpp"],
2849 stl: "none",
2850 system_shared_libs: [],
2851 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002852 `)
2853
Sundong Ahnabb64432019-10-22 13:58:29 +09002854 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002855 apexRule := module.Rule("apexRule")
2856 copyCmds := apexRule.Args["copy_commands"]
2857
2858 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002859 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002860
2861 // JNI libraries are embedded inside APK
2862 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
Colin Cross7113d202019-11-20 16:39:12 -08002863 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002864 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2865 // ... uncompressed
2866 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2867 t.Errorf("jni lib is not uncompressed for AppFoo")
2868 }
2869 // ... and not directly inside the APEX
2870 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002871}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002872
Dario Frenicde2a032019-10-27 00:29:22 +01002873func TestApexWithAppImports(t *testing.T) {
2874 ctx, _ := testApex(t, `
2875 apex {
2876 name: "myapex",
2877 key: "myapex.key",
2878 apps: [
2879 "AppFooPrebuilt",
2880 "AppFooPrivPrebuilt",
2881 ],
2882 }
2883
2884 apex_key {
2885 name: "myapex.key",
2886 public_key: "testkey.avbpubkey",
2887 private_key: "testkey.pem",
2888 }
2889
2890 android_app_import {
2891 name: "AppFooPrebuilt",
2892 apk: "PrebuiltAppFoo.apk",
2893 presigned: true,
2894 dex_preopt: {
2895 enabled: false,
2896 },
2897 }
2898
2899 android_app_import {
2900 name: "AppFooPrivPrebuilt",
2901 apk: "PrebuiltAppFooPriv.apk",
2902 privileged: true,
2903 presigned: true,
2904 dex_preopt: {
2905 enabled: false,
2906 },
2907 }
2908 `)
2909
Sundong Ahnabb64432019-10-22 13:58:29 +09002910 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002911 apexRule := module.Rule("apexRule")
2912 copyCmds := apexRule.Args["copy_commands"]
2913
2914 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2915 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002916}
2917
Dario Freni6f3937c2019-12-20 22:58:03 +00002918func TestApexWithTestHelperApp(t *testing.T) {
2919 ctx, _ := testApex(t, `
2920 apex {
2921 name: "myapex",
2922 key: "myapex.key",
2923 apps: [
2924 "TesterHelpAppFoo",
2925 ],
2926 }
2927
2928 apex_key {
2929 name: "myapex.key",
2930 public_key: "testkey.avbpubkey",
2931 private_key: "testkey.pem",
2932 }
2933
2934 android_test_helper_app {
2935 name: "TesterHelpAppFoo",
2936 srcs: ["foo/bar/MyClass.java"],
2937 }
2938
2939 `)
2940
2941 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2942 apexRule := module.Rule("apexRule")
2943 copyCmds := apexRule.Args["copy_commands"]
2944
2945 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
2946}
2947
Jooyung Han18020ea2019-11-13 10:50:48 +09002948func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2949 // libfoo's apex_available comes from cc_defaults
2950 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2951 apex {
2952 name: "myapex",
2953 key: "myapex.key",
2954 native_shared_libs: ["libfoo"],
2955 }
2956
2957 apex_key {
2958 name: "myapex.key",
2959 public_key: "testkey.avbpubkey",
2960 private_key: "testkey.pem",
2961 }
2962
2963 apex {
2964 name: "otherapex",
2965 key: "myapex.key",
2966 native_shared_libs: ["libfoo"],
2967 }
2968
2969 cc_defaults {
2970 name: "libfoo-defaults",
2971 apex_available: ["otherapex"],
2972 }
2973
2974 cc_library {
2975 name: "libfoo",
2976 defaults: ["libfoo-defaults"],
2977 stl: "none",
2978 system_shared_libs: [],
2979 }`)
2980}
2981
Jiyong Park127b40b2019-09-30 16:04:35 +09002982func TestApexAvailable(t *testing.T) {
2983 // libfoo is not available to myapex, but only to otherapex
2984 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2985 apex {
2986 name: "myapex",
2987 key: "myapex.key",
2988 native_shared_libs: ["libfoo"],
2989 }
2990
2991 apex_key {
2992 name: "myapex.key",
2993 public_key: "testkey.avbpubkey",
2994 private_key: "testkey.pem",
2995 }
2996
2997 apex {
2998 name: "otherapex",
2999 key: "otherapex.key",
3000 native_shared_libs: ["libfoo"],
3001 }
3002
3003 apex_key {
3004 name: "otherapex.key",
3005 public_key: "testkey.avbpubkey",
3006 private_key: "testkey.pem",
3007 }
3008
3009 cc_library {
3010 name: "libfoo",
3011 stl: "none",
3012 system_shared_libs: [],
3013 apex_available: ["otherapex"],
3014 }`)
3015
3016 // libbar is an indirect dep
3017 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3018 apex {
3019 name: "myapex",
3020 key: "myapex.key",
3021 native_shared_libs: ["libfoo"],
3022 }
3023
3024 apex_key {
3025 name: "myapex.key",
3026 public_key: "testkey.avbpubkey",
3027 private_key: "testkey.pem",
3028 }
3029
3030 apex {
3031 name: "otherapex",
3032 key: "otherapex.key",
3033 native_shared_libs: ["libfoo"],
3034 }
3035
3036 apex_key {
3037 name: "otherapex.key",
3038 public_key: "testkey.avbpubkey",
3039 private_key: "testkey.pem",
3040 }
3041
3042 cc_library {
3043 name: "libfoo",
3044 stl: "none",
3045 shared_libs: ["libbar"],
3046 system_shared_libs: [],
3047 apex_available: ["myapex", "otherapex"],
3048 }
3049
3050 cc_library {
3051 name: "libbar",
3052 stl: "none",
3053 system_shared_libs: [],
3054 apex_available: ["otherapex"],
3055 }`)
3056
3057 testApexError(t, "\"otherapex\" is not a valid module name", `
3058 apex {
3059 name: "myapex",
3060 key: "myapex.key",
3061 native_shared_libs: ["libfoo"],
3062 }
3063
3064 apex_key {
3065 name: "myapex.key",
3066 public_key: "testkey.avbpubkey",
3067 private_key: "testkey.pem",
3068 }
3069
3070 cc_library {
3071 name: "libfoo",
3072 stl: "none",
3073 system_shared_libs: [],
3074 apex_available: ["otherapex"],
3075 }`)
3076
3077 ctx, _ := testApex(t, `
3078 apex {
3079 name: "myapex",
3080 key: "myapex.key",
3081 native_shared_libs: ["libfoo", "libbar"],
3082 }
3083
3084 apex_key {
3085 name: "myapex.key",
3086 public_key: "testkey.avbpubkey",
3087 private_key: "testkey.pem",
3088 }
3089
3090 cc_library {
3091 name: "libfoo",
3092 stl: "none",
3093 system_shared_libs: [],
3094 apex_available: ["myapex"],
3095 }
3096
3097 cc_library {
3098 name: "libbar",
3099 stl: "none",
3100 system_shared_libs: [],
3101 apex_available: ["//apex_available:anyapex"],
3102 }`)
3103
3104 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003105 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3106 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3107 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3108 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003109
3110 ctx, _ = testApex(t, `
3111 apex {
3112 name: "myapex",
3113 key: "myapex.key",
3114 }
3115
3116 apex_key {
3117 name: "myapex.key",
3118 public_key: "testkey.avbpubkey",
3119 private_key: "testkey.pem",
3120 }
3121
3122 cc_library {
3123 name: "libfoo",
3124 stl: "none",
3125 system_shared_libs: [],
3126 apex_available: ["//apex_available:platform"],
3127 }`)
3128
3129 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003130 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3131 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003132
3133 ctx, _ = testApex(t, `
3134 apex {
3135 name: "myapex",
3136 key: "myapex.key",
3137 native_shared_libs: ["libfoo"],
3138 }
3139
3140 apex_key {
3141 name: "myapex.key",
3142 public_key: "testkey.avbpubkey",
3143 private_key: "testkey.pem",
3144 }
3145
3146 cc_library {
3147 name: "libfoo",
3148 stl: "none",
3149 system_shared_libs: [],
3150 apex_available: ["myapex"],
3151 static: {
3152 apex_available: ["//apex_available:platform"],
3153 },
3154 }`)
3155
3156 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08003157 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3158 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003159 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08003160 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3161 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003162}
3163
Jiyong Park5d790c32019-11-15 18:40:32 +09003164func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003165 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003166 apex {
3167 name: "myapex",
3168 key: "myapex.key",
3169 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003170 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003171 }
3172
3173 override_apex {
3174 name: "override_myapex",
3175 base: "myapex",
3176 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003177 overrides: ["unknownapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003178 }
3179
3180 apex_key {
3181 name: "myapex.key",
3182 public_key: "testkey.avbpubkey",
3183 private_key: "testkey.pem",
3184 }
3185
3186 android_app {
3187 name: "app",
3188 srcs: ["foo/bar/MyClass.java"],
3189 package_name: "foo",
3190 sdk_version: "none",
3191 system_modules: "none",
3192 }
3193
3194 override_android_app {
3195 name: "override_app",
3196 base: "app",
3197 package_name: "bar",
3198 }
3199 `)
3200
Jiyong Park317645e2019-12-05 13:20:58 +09003201 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3202 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3203 if originalVariant.GetOverriddenBy() != "" {
3204 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3205 }
3206 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3207 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3208 }
3209
Jiyong Park5d790c32019-11-15 18:40:32 +09003210 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3211 apexRule := module.Rule("apexRule")
3212 copyCmds := apexRule.Args["copy_commands"]
3213
3214 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3215 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003216
3217 apexBundle := module.Module().(*apexBundle)
3218 name := apexBundle.Name()
3219 if name != "override_myapex" {
3220 t.Errorf("name should be \"override_myapex\", but was %q", name)
3221 }
3222
3223 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3224 var builder strings.Builder
3225 data.Custom(&builder, name, "TARGET_", "", data)
3226 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003227 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003228 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3229 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003230 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003231 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003232 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003233 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3234 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003235}
3236
Jooyung Han214bf372019-11-12 13:03:50 +09003237func TestLegacyAndroid10Support(t *testing.T) {
3238 ctx, _ := testApex(t, `
3239 apex {
3240 name: "myapex",
3241 key: "myapex.key",
3242 legacy_android10_support: true,
3243 }
3244
3245 apex_key {
3246 name: "myapex.key",
3247 public_key: "testkey.avbpubkey",
3248 private_key: "testkey.pem",
3249 }
3250 `)
3251
3252 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3253 args := module.Rule("apexRule").Args
3254 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
3255}
3256
Jooyung Han58f26ab2019-12-18 15:34:32 +09003257func TestJavaSDKLibrary(t *testing.T) {
3258 ctx, _ := testApex(t, `
3259 apex {
3260 name: "myapex",
3261 key: "myapex.key",
3262 java_libs: ["foo"],
3263 }
3264
3265 apex_key {
3266 name: "myapex.key",
3267 public_key: "testkey.avbpubkey",
3268 private_key: "testkey.pem",
3269 }
3270
3271 java_sdk_library {
3272 name: "foo",
3273 srcs: ["a.java"],
3274 api_packages: ["foo"],
3275 }
3276 `, withFiles(map[string][]byte{
3277 "api/current.txt": nil,
3278 "api/removed.txt": nil,
3279 "api/system-current.txt": nil,
3280 "api/system-removed.txt": nil,
3281 "api/test-current.txt": nil,
3282 "api/test-removed.txt": nil,
3283 }))
3284
3285 // java_sdk_library installs both impl jar and permission XML
3286 ensureExactContents(t, ctx, "myapex", []string{
3287 "javalib/foo.jar",
3288 "etc/permissions/foo.xml",
3289 })
3290 // Permission XML should point to the activated path of impl jar of java_sdk_library
3291 genXMLCommand := ctx.ModuleForTests("foo", "android_common_myapex").Output("foo.xml").RuleParams.Command
3292 ensureContains(t, genXMLCommand, `<library name="foo" file="/apex/myapex/javalib/foo.jar"`)
3293}
3294
Jiyong Park479321d2019-12-16 11:47:12 +09003295func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3296 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3297 apex {
3298 name: "myapex",
3299 key: "myapex.key",
3300 java_libs: ["myjar"],
3301 }
3302
3303 apex_key {
3304 name: "myapex.key",
3305 public_key: "testkey.avbpubkey",
3306 private_key: "testkey.pem",
3307 }
3308
3309 java_library {
3310 name: "myjar",
3311 srcs: ["foo/bar/MyClass.java"],
3312 sdk_version: "none",
3313 system_modules: "none",
3314 }
3315 `)
3316}
3317
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003318func TestMain(m *testing.M) {
3319 run := func() int {
3320 setUp()
3321 defer tearDown()
3322
3323 return m.Run()
3324 }
3325
3326 os.Exit(run())
3327}