blob: b2d891dbe0863f44466256231acebc8c530876ad [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
289 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
290 ctx.RegisterModuleType("cc_library_shared", cc.LibrarySharedFactory)
291 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
292 ctx.RegisterModuleType("cc_prebuilt_library_shared", cc.PrebuiltSharedLibraryFactory)
293 ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory)
294 ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
295 ctx.RegisterModuleType("cc_object", cc.ObjectFactory)
296 ctx.RegisterModuleType("cc_defaults", func() android.Module {
297 return cc.DefaultsFactory()
298 })
299 ctx.RegisterModuleType("cc_test", cc.TestFactory)
300 ctx.RegisterModuleType("llndk_library", cc.LlndkLibraryFactory)
301 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
302 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
303 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
304 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
305 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
306 ctx.RegisterModuleType("android_app_certificate", java.AndroidAppCertificateFactory)
307 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
308 ctx.RegisterModuleType("java_library", java.LibraryFactory)
309 ctx.RegisterModuleType("java_import", java.ImportFactory)
310 ctx.RegisterModuleType("java_system_modules", java.SystemModulesFactory)
311 ctx.RegisterModuleType("android_app", java.AndroidAppFactory)
312 ctx.RegisterModuleType("android_app_import", java.AndroidAppImportFactory)
313 ctx.RegisterModuleType("override_android_app", java.OverrideAndroidAppModuleFactory)
314
315 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
316 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
317 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
318 })
319 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
320 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
321 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
322 ctx.BottomUp("test_per_src", cc.TestPerSrcMutator).Parallel()
323 ctx.BottomUp("version", cc.VersionMutator).Parallel()
324 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
325 })
326 ctx.PreDepsMutators(RegisterPreDepsMutators)
327 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
328 ctx.PostDepsMutators(RegisterPostDepsMutators)
329 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
330 ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel()
331 ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel()
332 })
333
334 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900335
Jooyung Han5c998b92019-06-27 11:30:33 +0900336 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900337}
338
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700339func setUp() {
340 var err error
341 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900342 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700343 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900344 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900345}
346
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700347func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900348 os.RemoveAll(buildDir)
349}
350
351// ensure that 'result' contains 'expected'
352func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900353 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900354 if !strings.Contains(result, expected) {
355 t.Errorf("%q is not found in %q", expected, result)
356 }
357}
358
359// ensures that 'result' does not contain 'notExpected'
360func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900361 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900362 if strings.Contains(result, notExpected) {
363 t.Errorf("%q is found in %q", notExpected, result)
364 }
365}
366
367func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900368 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900369 if !android.InList(expected, result) {
370 t.Errorf("%q is not found in %v", expected, result)
371 }
372}
373
374func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900375 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900376 if android.InList(notExpected, result) {
377 t.Errorf("%q is found in %v", notExpected, result)
378 }
379}
380
Jooyung Hane1633032019-08-01 17:41:43 +0900381func ensureListEmpty(t *testing.T, result []string) {
382 t.Helper()
383 if len(result) > 0 {
384 t.Errorf("%q is expected to be empty", result)
385 }
386}
387
Jiyong Park25fc6a92018-11-18 18:02:45 +0900388// Minimal test
389func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700390 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900391 apex_defaults {
392 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900393 manifest: ":myapex.manifest",
394 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900395 key: "myapex.key",
396 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800397 multilib: {
398 both: {
399 binaries: ["foo",],
400 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900401 },
Jiyong Park9e6c2422019-08-09 20:39:45 +0900402 java_libs: ["myjar", "myprebuiltjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900403 }
404
Jiyong Park30ca9372019-02-07 16:27:23 +0900405 apex {
406 name: "myapex",
407 defaults: ["myapex-defaults"],
408 }
409
Jiyong Park25fc6a92018-11-18 18:02:45 +0900410 apex_key {
411 name: "myapex.key",
412 public_key: "testkey.avbpubkey",
413 private_key: "testkey.pem",
414 }
415
Jiyong Park809bb722019-02-13 21:33:49 +0900416 filegroup {
417 name: "myapex.manifest",
418 srcs: ["apex_manifest.json"],
419 }
420
421 filegroup {
422 name: "myapex.androidmanifest",
423 srcs: ["AndroidManifest.xml"],
424 }
425
Jiyong Park25fc6a92018-11-18 18:02:45 +0900426 cc_library {
427 name: "mylib",
428 srcs: ["mylib.cpp"],
429 shared_libs: ["mylib2"],
430 system_shared_libs: [],
431 stl: "none",
432 }
433
Alex Light3d673592019-01-18 14:37:31 -0800434 cc_binary {
435 name: "foo",
436 srcs: ["mylib.cpp"],
437 compile_multilib: "both",
438 multilib: {
439 lib32: {
440 suffix: "32",
441 },
442 lib64: {
443 suffix: "64",
444 },
445 },
446 symlinks: ["foo_link_"],
447 symlink_preferred_arch: true,
448 system_shared_libs: [],
449 static_executable: true,
450 stl: "none",
451 }
452
Jiyong Park25fc6a92018-11-18 18:02:45 +0900453 cc_library {
454 name: "mylib2",
455 srcs: ["mylib.cpp"],
456 system_shared_libs: [],
457 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900458 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900459 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900460
461 java_library {
462 name: "myjar",
463 srcs: ["foo/bar/MyClass.java"],
464 sdk_version: "none",
465 system_modules: "none",
466 compile_dex: true,
467 static_libs: ["myotherjar"],
468 }
469
470 java_library {
471 name: "myotherjar",
472 srcs: ["foo/bar/MyClass.java"],
473 sdk_version: "none",
474 system_modules: "none",
475 compile_dex: true,
476 }
Jiyong Park9e6c2422019-08-09 20:39:45 +0900477
478 java_import {
479 name: "myprebuiltjar",
480 jars: ["prebuilt.jar"],
481 installable: true,
482 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900483 `)
484
Sundong Ahnabb64432019-10-22 13:58:29 +0900485 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900486
487 optFlags := apexRule.Args["opt_flags"]
488 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700489 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900490 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900491
Jiyong Park25fc6a92018-11-18 18:02:45 +0900492 copyCmds := apexRule.Args["copy_commands"]
493
494 // Ensure that main rule creates an output
495 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
496
497 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800498 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900499 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900500 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900501
502 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800503 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900504 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900505
506 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800507 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
508 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900509 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900510 ensureContains(t, copyCmds, "image.apex/javalib/myprebuiltjar.jar")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900511 // .. but not for java libs
512 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800513
Colin Cross7113d202019-11-20 16:39:12 -0800514 // Ensure that the platform variant ends with _shared or _common
515 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
516 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900517 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
518 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900519 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common")
Alex Light3d673592019-01-18 14:37:31 -0800520
521 // Ensure that all symlinks are present.
522 found_foo_link_64 := false
523 found_foo := false
524 for _, cmd := range strings.Split(copyCmds, " && ") {
525 if strings.HasPrefix(cmd, "ln -s foo64") {
526 if strings.HasSuffix(cmd, "bin/foo") {
527 found_foo = true
528 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
529 found_foo_link_64 = true
530 }
531 }
532 }
533 good := found_foo && found_foo_link_64
534 if !good {
535 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
536 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900537
Sundong Ahnabb64432019-10-22 13:58:29 +0900538 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700539 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700540 if len(noticeInputs) != 2 {
541 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900542 }
543 ensureListContains(t, noticeInputs, "NOTICE")
544 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800545}
546
Jooyung Hanf21c7972019-12-16 22:32:06 +0900547func TestDefaults(t *testing.T) {
548 ctx, _ := testApex(t, `
549 apex_defaults {
550 name: "myapex-defaults",
551 key: "myapex.key",
552 prebuilts: ["myetc"],
553 native_shared_libs: ["mylib"],
554 java_libs: ["myjar"],
555 apps: ["AppFoo"],
556 }
557
558 prebuilt_etc {
559 name: "myetc",
560 src: "myprebuilt",
561 }
562
563 apex {
564 name: "myapex",
565 defaults: ["myapex-defaults"],
566 }
567
568 apex_key {
569 name: "myapex.key",
570 public_key: "testkey.avbpubkey",
571 private_key: "testkey.pem",
572 }
573
574 cc_library {
575 name: "mylib",
576 system_shared_libs: [],
577 stl: "none",
578 }
579
580 java_library {
581 name: "myjar",
582 srcs: ["foo/bar/MyClass.java"],
583 sdk_version: "none",
584 system_modules: "none",
585 compile_dex: true,
586 }
587
588 android_app {
589 name: "AppFoo",
590 srcs: ["foo/bar/MyClass.java"],
591 sdk_version: "none",
592 system_modules: "none",
593 }
594 `)
595 ensureExactContents(t, ctx, "myapex", []string{
596 "etc/myetc",
597 "javalib/myjar.jar",
598 "lib64/mylib.so",
599 "app/AppFoo/AppFoo.apk",
600 })
601}
602
Jooyung Han01a3ee22019-11-02 02:52:25 +0900603func TestApexManifest(t *testing.T) {
604 ctx, _ := testApex(t, `
605 apex {
606 name: "myapex",
607 key: "myapex.key",
608 }
609
610 apex_key {
611 name: "myapex.key",
612 public_key: "testkey.avbpubkey",
613 private_key: "testkey.pem",
614 }
615 `)
616
617 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900618 args := module.Rule("apexRule").Args
619 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
620 t.Error("manifest should be apex_manifest.pb, but " + manifest)
621 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900622}
623
Alex Light5098a612018-11-29 17:12:15 -0800624func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700625 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800626 apex {
627 name: "myapex",
628 key: "myapex.key",
629 payload_type: "zip",
630 native_shared_libs: ["mylib"],
631 }
632
633 apex_key {
634 name: "myapex.key",
635 public_key: "testkey.avbpubkey",
636 private_key: "testkey.pem",
637 }
638
639 cc_library {
640 name: "mylib",
641 srcs: ["mylib.cpp"],
642 shared_libs: ["mylib2"],
643 system_shared_libs: [],
644 stl: "none",
645 }
646
647 cc_library {
648 name: "mylib2",
649 srcs: ["mylib.cpp"],
650 system_shared_libs: [],
651 stl: "none",
652 }
653 `)
654
Sundong Ahnabb64432019-10-22 13:58:29 +0900655 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800656 copyCmds := zipApexRule.Args["copy_commands"]
657
658 // Ensure that main rule creates an output
659 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
660
661 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800662 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800663
664 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800665 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800666
667 // Ensure that both direct and indirect deps are copied into apex
668 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
669 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900670}
671
672func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700673 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900674 apex {
675 name: "myapex",
676 key: "myapex.key",
677 native_shared_libs: ["mylib", "mylib3"],
678 }
679
680 apex_key {
681 name: "myapex.key",
682 public_key: "testkey.avbpubkey",
683 private_key: "testkey.pem",
684 }
685
686 cc_library {
687 name: "mylib",
688 srcs: ["mylib.cpp"],
689 shared_libs: ["mylib2", "mylib3"],
690 system_shared_libs: [],
691 stl: "none",
692 }
693
694 cc_library {
695 name: "mylib2",
696 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900697 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900698 system_shared_libs: [],
699 stl: "none",
700 stubs: {
701 versions: ["1", "2", "3"],
702 },
703 }
704
705 cc_library {
706 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900707 srcs: ["mylib.cpp"],
708 shared_libs: ["mylib4"],
709 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900710 stl: "none",
711 stubs: {
712 versions: ["10", "11", "12"],
713 },
714 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900715
716 cc_library {
717 name: "mylib4",
718 srcs: ["mylib.cpp"],
719 system_shared_libs: [],
720 stl: "none",
721 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900722 `)
723
Sundong Ahnabb64432019-10-22 13:58:29 +0900724 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900725 copyCmds := apexRule.Args["copy_commands"]
726
727 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800728 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900729
730 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800731 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900732
733 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800734 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900735
Colin Cross7113d202019-11-20 16:39:12 -0800736 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900737
738 // Ensure that mylib is linking with the latest version of stubs for mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800739 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900740 // ... and not linking to the non-stub (impl) variant of mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800741 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900742
743 // 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 -0800744 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900745 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800746 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900747
748 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800749 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900750 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900751
752 // Ensure that genstub is invoked with --apex
Colin Cross7113d202019-11-20 16:39:12 -0800753 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3_myapex").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900754
755 ensureExactContents(t, ctx, "myapex", []string{
756 "lib64/mylib.so",
757 "lib64/mylib3.so",
758 "lib64/mylib4.so",
759 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900760}
761
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900762func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700763 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900764 apex {
765 name: "myapex",
766 key: "myapex.key",
767 native_shared_libs: ["mylib"],
768 }
769
770 apex_key {
771 name: "myapex.key",
772 public_key: "testkey.avbpubkey",
773 private_key: "testkey.pem",
774 }
775
776 cc_library {
777 name: "mylib",
778 srcs: ["mylib.cpp"],
779 shared_libs: ["libfoo#10"],
780 system_shared_libs: [],
781 stl: "none",
782 }
783
784 cc_library {
785 name: "libfoo",
786 srcs: ["mylib.cpp"],
787 shared_libs: ["libbar"],
788 system_shared_libs: [],
789 stl: "none",
790 stubs: {
791 versions: ["10", "20", "30"],
792 },
793 }
794
795 cc_library {
796 name: "libbar",
797 srcs: ["mylib.cpp"],
798 system_shared_libs: [],
799 stl: "none",
800 }
801
802 `)
803
Sundong Ahnabb64432019-10-22 13:58:29 +0900804 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900805 copyCmds := apexRule.Args["copy_commands"]
806
807 // Ensure that direct non-stubs dep is always included
808 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
809
810 // Ensure that indirect stubs dep is not included
811 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
812
813 // Ensure that dependency of stubs is not included
814 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
815
Colin Cross7113d202019-11-20 16:39:12 -0800816 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900817
818 // Ensure that mylib is linking with version 10 of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800819 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900820 // ... and not linking to the non-stub (impl) variant of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800821 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900822
Colin Cross7113d202019-11-20 16:39:12 -0800823 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900824
825 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
826 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
827}
828
Jooyung Hand3639552019-08-09 12:57:43 +0900829func TestApexWithRuntimeLibsDependency(t *testing.T) {
830 /*
831 myapex
832 |
833 v (runtime_libs)
834 mylib ------+------> libfoo [provides stub]
835 |
836 `------> libbar
837 */
838 ctx, _ := testApex(t, `
839 apex {
840 name: "myapex",
841 key: "myapex.key",
842 native_shared_libs: ["mylib"],
843 }
844
845 apex_key {
846 name: "myapex.key",
847 public_key: "testkey.avbpubkey",
848 private_key: "testkey.pem",
849 }
850
851 cc_library {
852 name: "mylib",
853 srcs: ["mylib.cpp"],
854 runtime_libs: ["libfoo", "libbar"],
855 system_shared_libs: [],
856 stl: "none",
857 }
858
859 cc_library {
860 name: "libfoo",
861 srcs: ["mylib.cpp"],
862 system_shared_libs: [],
863 stl: "none",
864 stubs: {
865 versions: ["10", "20", "30"],
866 },
867 }
868
869 cc_library {
870 name: "libbar",
871 srcs: ["mylib.cpp"],
872 system_shared_libs: [],
873 stl: "none",
874 }
875
876 `)
877
Sundong Ahnabb64432019-10-22 13:58:29 +0900878 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900879 copyCmds := apexRule.Args["copy_commands"]
880
881 // Ensure that direct non-stubs dep is always included
882 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
883
884 // Ensure that indirect stubs dep is not included
885 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
886
887 // Ensure that runtime_libs dep in included
888 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
889
Sundong Ahnabb64432019-10-22 13:58:29 +0900890 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900891 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
892 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900893
894}
895
Jooyung Han9c80bae2019-08-20 17:30:57 +0900896func TestApexDependencyToLLNDK(t *testing.T) {
897 ctx, _ := testApex(t, `
898 apex {
899 name: "myapex",
900 key: "myapex.key",
901 use_vendor: true,
902 native_shared_libs: ["mylib"],
903 }
904
905 apex_key {
906 name: "myapex.key",
907 public_key: "testkey.avbpubkey",
908 private_key: "testkey.pem",
909 }
910
911 cc_library {
912 name: "mylib",
913 srcs: ["mylib.cpp"],
914 vendor_available: true,
915 shared_libs: ["libbar"],
916 system_shared_libs: [],
917 stl: "none",
918 }
919
920 cc_library {
921 name: "libbar",
922 srcs: ["mylib.cpp"],
923 system_shared_libs: [],
924 stl: "none",
925 }
926
927 llndk_library {
928 name: "libbar",
929 symbol_file: "",
930 }
Jooyung Handc782442019-11-01 03:14:38 +0900931 `, func(fs map[string][]byte, config android.Config) {
932 setUseVendorWhitelistForTest(config, []string{"myapex"})
933 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900934
Sundong Ahnabb64432019-10-22 13:58:29 +0900935 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900936 copyCmds := apexRule.Args["copy_commands"]
937
938 // Ensure that LLNDK dep is not included
939 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
940
Sundong Ahnabb64432019-10-22 13:58:29 +0900941 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900942 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900943
944 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900945 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900946
947}
948
Jiyong Park25fc6a92018-11-18 18:02:45 +0900949func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700950 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900951 apex {
952 name: "myapex",
953 key: "myapex.key",
954 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
955 }
956
957 apex_key {
958 name: "myapex.key",
959 public_key: "testkey.avbpubkey",
960 private_key: "testkey.pem",
961 }
962
963 cc_library {
964 name: "mylib",
965 srcs: ["mylib.cpp"],
966 shared_libs: ["libdl#27"],
967 stl: "none",
968 }
969
970 cc_library_shared {
971 name: "mylib_shared",
972 srcs: ["mylib.cpp"],
973 shared_libs: ["libdl#27"],
974 stl: "none",
975 }
976
977 cc_library {
978 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700979 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900980 nocrt: true,
981 system_shared_libs: [],
982 stl: "none",
983 stubs: {
984 versions: ["27", "28", "29"],
985 },
986 }
987
988 cc_library {
989 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700990 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900991 nocrt: true,
992 system_shared_libs: [],
993 stl: "none",
994 stubs: {
995 versions: ["27", "28", "29"],
996 },
997 }
998
999 cc_library {
1000 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -07001001 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001002 nocrt: true,
1003 system_shared_libs: [],
1004 stl: "none",
1005 stubs: {
1006 versions: ["27", "28", "29"],
1007 },
1008 }
Jiyong Parkb0788572018-12-20 22:10:17 +09001009
1010 cc_library {
1011 name: "libBootstrap",
1012 srcs: ["mylib.cpp"],
1013 stl: "none",
1014 bootstrap: true,
1015 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001016 `)
1017
Sundong Ahnabb64432019-10-22 13:58:29 +09001018 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001019 copyCmds := apexRule.Args["copy_commands"]
1020
1021 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001022 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001023 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1024 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001025
1026 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001027 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001028
Colin Cross7113d202019-11-20 16:39:12 -08001029 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1030 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1031 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001032
1033 // For dependency to libc
1034 // Ensure that mylib is linking with the latest version of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001035 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001036 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001037 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001038 // ... Cflags from stub is correctly exported to mylib
1039 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1040 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1041
1042 // For dependency to libm
1043 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001044 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001045 // ... and not linking to the stub variant
Colin Cross7113d202019-11-20 16:39:12 -08001046 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001047 // ... and is not compiling with the stub
1048 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1049 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1050
1051 // For dependency to libdl
1052 // Ensure that mylib is linking with the specified version of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001053 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001054 // ... and not linking to the other versions of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001055 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28_myapex/libdl.so")
1056 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001057 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001058 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001059 // ... Cflags from stub is correctly exported to mylib
1060 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1061 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001062
1063 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001064 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1065 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1066 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1067 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001068}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001069
1070func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001071 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001072 apex {
1073 name: "myapex",
1074 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001075 native_shared_libs: ["mylib"],
1076 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001077 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001078 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001079 }
1080
1081 apex_key {
1082 name: "myapex.key",
1083 public_key: "testkey.avbpubkey",
1084 private_key: "testkey.pem",
1085 }
1086
1087 prebuilt_etc {
1088 name: "myetc",
1089 src: "myprebuilt",
1090 sub_dir: "foo/bar",
1091 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001092
1093 cc_library {
1094 name: "mylib",
1095 srcs: ["mylib.cpp"],
1096 relative_install_path: "foo/bar",
1097 system_shared_libs: [],
1098 stl: "none",
1099 }
1100
1101 cc_binary {
1102 name: "mybin",
1103 srcs: ["mylib.cpp"],
1104 relative_install_path: "foo/bar",
1105 system_shared_libs: [],
1106 static_executable: true,
1107 stl: "none",
1108 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001109 `)
1110
Sundong Ahnabb64432019-10-22 13:58:29 +09001111 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001112 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1113
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001114 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001115 ensureListContains(t, dirs, "etc")
1116 ensureListContains(t, dirs, "etc/foo")
1117 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001118 ensureListContains(t, dirs, "lib64")
1119 ensureListContains(t, dirs, "lib64/foo")
1120 ensureListContains(t, dirs, "lib64/foo/bar")
1121 ensureListContains(t, dirs, "lib")
1122 ensureListContains(t, dirs, "lib/foo")
1123 ensureListContains(t, dirs, "lib/foo/bar")
1124
Jiyong Parkbd13e442019-03-15 18:10:35 +09001125 ensureListContains(t, dirs, "bin")
1126 ensureListContains(t, dirs, "bin/foo")
1127 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001128}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001129
1130func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001131 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001132 apex {
1133 name: "myapex",
1134 key: "myapex.key",
1135 native_shared_libs: ["mylib"],
1136 use_vendor: true,
1137 }
1138
1139 apex_key {
1140 name: "myapex.key",
1141 public_key: "testkey.avbpubkey",
1142 private_key: "testkey.pem",
1143 }
1144
1145 cc_library {
1146 name: "mylib",
1147 srcs: ["mylib.cpp"],
1148 shared_libs: ["mylib2"],
1149 system_shared_libs: [],
1150 vendor_available: true,
1151 stl: "none",
1152 }
1153
1154 cc_library {
1155 name: "mylib2",
1156 srcs: ["mylib.cpp"],
1157 system_shared_libs: [],
1158 vendor_available: true,
1159 stl: "none",
1160 }
Jooyung Handc782442019-11-01 03:14:38 +09001161 `, func(fs map[string][]byte, config android.Config) {
1162 setUseVendorWhitelistForTest(config, []string{"myapex"})
1163 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001164
1165 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001166 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001167 for _, implicit := range i.Implicits {
1168 inputsList = append(inputsList, implicit.String())
1169 }
1170 }
1171 inputsString := strings.Join(inputsList, " ")
1172
1173 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001174 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1175 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001176
1177 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001178 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1179 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001180}
Jiyong Park16e91a02018-12-20 18:18:08 +09001181
Jooyung Handc782442019-11-01 03:14:38 +09001182func TestUseVendorRestriction(t *testing.T) {
1183 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1184 apex {
1185 name: "myapex",
1186 key: "myapex.key",
1187 use_vendor: true,
1188 }
1189 apex_key {
1190 name: "myapex.key",
1191 public_key: "testkey.avbpubkey",
1192 private_key: "testkey.pem",
1193 }
1194 `, func(fs map[string][]byte, config android.Config) {
1195 setUseVendorWhitelistForTest(config, []string{""})
1196 })
1197 // no error with whitelist
1198 testApex(t, `
1199 apex {
1200 name: "myapex",
1201 key: "myapex.key",
1202 use_vendor: true,
1203 }
1204 apex_key {
1205 name: "myapex.key",
1206 public_key: "testkey.avbpubkey",
1207 private_key: "testkey.pem",
1208 }
1209 `, func(fs map[string][]byte, config android.Config) {
1210 setUseVendorWhitelistForTest(config, []string{"myapex"})
1211 })
1212}
1213
Jooyung Han5c998b92019-06-27 11:30:33 +09001214func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1215 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1216 apex {
1217 name: "myapex",
1218 key: "myapex.key",
1219 native_shared_libs: ["mylib"],
1220 use_vendor: true,
1221 }
1222
1223 apex_key {
1224 name: "myapex.key",
1225 public_key: "testkey.avbpubkey",
1226 private_key: "testkey.pem",
1227 }
1228
1229 cc_library {
1230 name: "mylib",
1231 srcs: ["mylib.cpp"],
1232 system_shared_libs: [],
1233 stl: "none",
1234 }
1235 `)
1236}
1237
Jiyong Park16e91a02018-12-20 18:18:08 +09001238func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001239 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001240 apex {
1241 name: "myapex",
1242 key: "myapex.key",
1243 native_shared_libs: ["mylib"],
1244 }
1245
1246 apex_key {
1247 name: "myapex.key",
1248 public_key: "testkey.avbpubkey",
1249 private_key: "testkey.pem",
1250 }
1251
1252 cc_library {
1253 name: "mylib",
1254 srcs: ["mylib.cpp"],
1255 system_shared_libs: [],
1256 stl: "none",
1257 stubs: {
1258 versions: ["1", "2", "3"],
1259 },
1260 }
1261
1262 cc_binary {
1263 name: "not_in_apex",
1264 srcs: ["mylib.cpp"],
1265 static_libs: ["mylib"],
1266 static_executable: true,
1267 system_shared_libs: [],
1268 stl: "none",
1269 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001270 `)
1271
Colin Cross7113d202019-11-20 16:39:12 -08001272 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001273
1274 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001275 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001276}
Jiyong Park9335a262018-12-24 11:31:58 +09001277
1278func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001279 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001280 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001281 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001282 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001283 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001284 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001285 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001286 }
1287
1288 cc_library {
1289 name: "mylib",
1290 srcs: ["mylib.cpp"],
1291 system_shared_libs: [],
1292 stl: "none",
1293 }
1294
1295 apex_key {
1296 name: "myapex.key",
1297 public_key: "testkey.avbpubkey",
1298 private_key: "testkey.pem",
1299 }
1300
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001301 android_app_certificate {
1302 name: "myapex.certificate",
1303 certificate: "testkey",
1304 }
1305
1306 android_app_certificate {
1307 name: "myapex.certificate.override",
1308 certificate: "testkey.override",
1309 }
1310
Jiyong Park9335a262018-12-24 11:31:58 +09001311 `)
1312
1313 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001314 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001315
1316 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1317 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1318 "vendor/foo/devkeys/testkey.avbpubkey")
1319 }
1320 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1321 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1322 "vendor/foo/devkeys/testkey.pem")
1323 }
1324
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001325 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001326 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001327 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001328 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001329 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001330 }
1331}
Jiyong Park58e364a2019-01-19 19:24:06 +09001332
Jooyung Hanf121a652019-12-17 14:30:11 +09001333func TestCertificate(t *testing.T) {
1334 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1335 ctx, _ := testApex(t, `
1336 apex {
1337 name: "myapex",
1338 key: "myapex.key",
1339 }
1340 apex_key {
1341 name: "myapex.key",
1342 public_key: "testkey.avbpubkey",
1343 private_key: "testkey.pem",
1344 }`)
1345 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1346 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1347 if actual := rule.Args["certificates"]; actual != expected {
1348 t.Errorf("certificates should be %q, not %q", expected, actual)
1349 }
1350 })
1351 t.Run("override when unspecified", func(t *testing.T) {
1352 ctx, _ := testApex(t, `
1353 apex {
1354 name: "myapex_keytest",
1355 key: "myapex.key",
1356 file_contexts: ":myapex-file_contexts",
1357 }
1358 apex_key {
1359 name: "myapex.key",
1360 public_key: "testkey.avbpubkey",
1361 private_key: "testkey.pem",
1362 }
1363 android_app_certificate {
1364 name: "myapex.certificate.override",
1365 certificate: "testkey.override",
1366 }`)
1367 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1368 expected := "testkey.override.x509.pem testkey.override.pk8"
1369 if actual := rule.Args["certificates"]; actual != expected {
1370 t.Errorf("certificates should be %q, not %q", expected, actual)
1371 }
1372 })
1373 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1374 ctx, _ := testApex(t, `
1375 apex {
1376 name: "myapex",
1377 key: "myapex.key",
1378 certificate: ":myapex.certificate",
1379 }
1380 apex_key {
1381 name: "myapex.key",
1382 public_key: "testkey.avbpubkey",
1383 private_key: "testkey.pem",
1384 }
1385 android_app_certificate {
1386 name: "myapex.certificate",
1387 certificate: "testkey",
1388 }`)
1389 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1390 expected := "testkey.x509.pem testkey.pk8"
1391 if actual := rule.Args["certificates"]; actual != expected {
1392 t.Errorf("certificates should be %q, not %q", expected, actual)
1393 }
1394 })
1395 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1396 ctx, _ := testApex(t, `
1397 apex {
1398 name: "myapex_keytest",
1399 key: "myapex.key",
1400 file_contexts: ":myapex-file_contexts",
1401 certificate: ":myapex.certificate",
1402 }
1403 apex_key {
1404 name: "myapex.key",
1405 public_key: "testkey.avbpubkey",
1406 private_key: "testkey.pem",
1407 }
1408 android_app_certificate {
1409 name: "myapex.certificate.override",
1410 certificate: "testkey.override",
1411 }`)
1412 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1413 expected := "testkey.override.x509.pem testkey.override.pk8"
1414 if actual := rule.Args["certificates"]; actual != expected {
1415 t.Errorf("certificates should be %q, not %q", expected, actual)
1416 }
1417 })
1418 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1419 ctx, _ := testApex(t, `
1420 apex {
1421 name: "myapex",
1422 key: "myapex.key",
1423 certificate: "testkey",
1424 }
1425 apex_key {
1426 name: "myapex.key",
1427 public_key: "testkey.avbpubkey",
1428 private_key: "testkey.pem",
1429 }`)
1430 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1431 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1432 if actual := rule.Args["certificates"]; actual != expected {
1433 t.Errorf("certificates should be %q, not %q", expected, actual)
1434 }
1435 })
1436 t.Run("override when specified as <name>", func(t *testing.T) {
1437 ctx, _ := testApex(t, `
1438 apex {
1439 name: "myapex_keytest",
1440 key: "myapex.key",
1441 file_contexts: ":myapex-file_contexts",
1442 certificate: "testkey",
1443 }
1444 apex_key {
1445 name: "myapex.key",
1446 public_key: "testkey.avbpubkey",
1447 private_key: "testkey.pem",
1448 }
1449 android_app_certificate {
1450 name: "myapex.certificate.override",
1451 certificate: "testkey.override",
1452 }`)
1453 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1454 expected := "testkey.override.x509.pem testkey.override.pk8"
1455 if actual := rule.Args["certificates"]; actual != expected {
1456 t.Errorf("certificates should be %q, not %q", expected, actual)
1457 }
1458 })
1459}
1460
Jiyong Park58e364a2019-01-19 19:24:06 +09001461func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001462 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001463 apex {
1464 name: "myapex",
1465 key: "myapex.key",
1466 native_shared_libs: ["mylib"],
1467 }
1468
1469 apex {
1470 name: "otherapex",
1471 key: "myapex.key",
1472 native_shared_libs: ["mylib"],
1473 }
1474
1475 apex_key {
1476 name: "myapex.key",
1477 public_key: "testkey.avbpubkey",
1478 private_key: "testkey.pem",
1479 }
1480
1481 cc_library {
1482 name: "mylib",
1483 srcs: ["mylib.cpp"],
1484 system_shared_libs: [],
1485 stl: "none",
1486 }
1487 `)
1488
Jooyung Han6b8459b2019-10-30 08:29:25 +09001489 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001490 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001491 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001492 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1493 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001494
Jooyung Han6b8459b2019-10-30 08:29:25 +09001495 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001496 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001497 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001498 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1499 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001500
Jooyung Han6b8459b2019-10-30 08:29:25 +09001501 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001502 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001503 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001504 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1505 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001506}
Jiyong Park7e636d02019-01-28 16:16:54 +09001507
1508func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001509 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001510 apex {
1511 name: "myapex",
1512 key: "myapex.key",
1513 native_shared_libs: ["mylib"],
1514 }
1515
1516 apex_key {
1517 name: "myapex.key",
1518 public_key: "testkey.avbpubkey",
1519 private_key: "testkey.pem",
1520 }
1521
1522 cc_library_headers {
1523 name: "mylib_headers",
1524 export_include_dirs: ["my_include"],
1525 system_shared_libs: [],
1526 stl: "none",
1527 }
1528
1529 cc_library {
1530 name: "mylib",
1531 srcs: ["mylib.cpp"],
1532 system_shared_libs: [],
1533 stl: "none",
1534 header_libs: ["mylib_headers"],
1535 export_header_lib_headers: ["mylib_headers"],
1536 stubs: {
1537 versions: ["1", "2", "3"],
1538 },
1539 }
1540
1541 cc_library {
1542 name: "otherlib",
1543 srcs: ["mylib.cpp"],
1544 system_shared_libs: [],
1545 stl: "none",
1546 shared_libs: ["mylib"],
1547 }
1548 `)
1549
Colin Cross7113d202019-11-20 16:39:12 -08001550 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001551
1552 // Ensure that the include path of the header lib is exported to 'otherlib'
1553 ensureContains(t, cFlags, "-Imy_include")
1554}
Alex Light9670d332019-01-29 18:07:33 -08001555
Jooyung Han31c470b2019-10-18 16:26:59 +09001556func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1557 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001558 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001559 copyCmds := apexRule.Args["copy_commands"]
1560 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001561 var failed bool
1562 var surplus []string
1563 filesMatched := make(map[string]bool)
1564 addContent := func(content string) {
1565 for _, expected := range files {
1566 if matched, _ := path.Match(expected, content); matched {
1567 filesMatched[expected] = true
1568 return
1569 }
1570 }
1571 surplus = append(surplus, content)
1572 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001573 for _, cmd := range strings.Split(copyCmds, "&&") {
1574 cmd = strings.TrimSpace(cmd)
1575 if cmd == "" {
1576 continue
1577 }
1578 terms := strings.Split(cmd, " ")
1579 switch terms[0] {
1580 case "mkdir":
1581 case "cp":
1582 if len(terms) != 3 {
1583 t.Fatal("copyCmds contains invalid cp command", cmd)
1584 }
1585 dst := terms[2]
1586 index := strings.Index(dst, imageApexDir)
1587 if index == -1 {
1588 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1589 }
1590 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001591 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001592 default:
1593 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1594 }
1595 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001596
Jooyung Han31c470b2019-10-18 16:26:59 +09001597 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001598 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001599 t.Log("surplus files", surplus)
1600 failed = true
1601 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001602
1603 if len(files) > len(filesMatched) {
1604 var missing []string
1605 for _, expected := range files {
1606 if !filesMatched[expected] {
1607 missing = append(missing, expected)
1608 }
1609 }
1610 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001611 t.Log("missing files", missing)
1612 failed = true
1613 }
1614 if failed {
1615 t.Fail()
1616 }
1617}
1618
Jooyung Han344d5432019-08-23 11:17:39 +09001619func TestVndkApexCurrent(t *testing.T) {
1620 ctx, _ := testApex(t, `
1621 apex_vndk {
1622 name: "myapex",
1623 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001624 }
1625
1626 apex_key {
1627 name: "myapex.key",
1628 public_key: "testkey.avbpubkey",
1629 private_key: "testkey.pem",
1630 }
1631
1632 cc_library {
1633 name: "libvndk",
1634 srcs: ["mylib.cpp"],
1635 vendor_available: true,
1636 vndk: {
1637 enabled: true,
1638 },
1639 system_shared_libs: [],
1640 stl: "none",
1641 }
1642
1643 cc_library {
1644 name: "libvndksp",
1645 srcs: ["mylib.cpp"],
1646 vendor_available: true,
1647 vndk: {
1648 enabled: true,
1649 support_system_process: true,
1650 },
1651 system_shared_libs: [],
1652 stl: "none",
1653 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001654 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001655
Jooyung Han31c470b2019-10-18 16:26:59 +09001656 ensureExactContents(t, ctx, "myapex", []string{
1657 "lib/libvndk.so",
1658 "lib/libvndksp.so",
1659 "lib64/libvndk.so",
1660 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001661 "etc/llndk.libraries.VER.txt",
1662 "etc/vndkcore.libraries.VER.txt",
1663 "etc/vndksp.libraries.VER.txt",
1664 "etc/vndkprivate.libraries.VER.txt",
1665 "etc/vndkcorevariant.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001666 })
Jooyung Han344d5432019-08-23 11:17:39 +09001667}
1668
1669func TestVndkApexWithPrebuilt(t *testing.T) {
1670 ctx, _ := testApex(t, `
1671 apex_vndk {
1672 name: "myapex",
1673 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001674 }
1675
1676 apex_key {
1677 name: "myapex.key",
1678 public_key: "testkey.avbpubkey",
1679 private_key: "testkey.pem",
1680 }
1681
1682 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001683 name: "libvndk",
1684 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001685 vendor_available: true,
1686 vndk: {
1687 enabled: true,
1688 },
1689 system_shared_libs: [],
1690 stl: "none",
1691 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001692
1693 cc_prebuilt_library_shared {
1694 name: "libvndk.arm",
1695 srcs: ["libvndk.arm.so"],
1696 vendor_available: true,
1697 vndk: {
1698 enabled: true,
1699 },
1700 enabled: false,
1701 arch: {
1702 arm: {
1703 enabled: true,
1704 },
1705 },
1706 system_shared_libs: [],
1707 stl: "none",
1708 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001709 `+vndkLibrariesTxtFiles("current"),
1710 withFiles(map[string][]byte{
1711 "libvndk.so": nil,
1712 "libvndk.arm.so": nil,
1713 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001714
Jooyung Han31c470b2019-10-18 16:26:59 +09001715 ensureExactContents(t, ctx, "myapex", []string{
1716 "lib/libvndk.so",
1717 "lib/libvndk.arm.so",
1718 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001719 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001720 })
Jooyung Han344d5432019-08-23 11:17:39 +09001721}
1722
Jooyung Han39edb6c2019-11-06 16:53:07 +09001723func vndkLibrariesTxtFiles(vers ...string) (result string) {
1724 for _, v := range vers {
1725 if v == "current" {
1726 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkcorevariant"} {
1727 result += `
1728 vndk_libraries_txt {
1729 name: "` + txt + `.libraries.txt",
1730 }
1731 `
1732 }
1733 } else {
1734 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1735 result += `
1736 prebuilt_etc {
1737 name: "` + txt + `.libraries.` + v + `.txt",
1738 src: "dummy.txt",
1739 }
1740 `
1741 }
1742 }
1743 }
1744 return
1745}
1746
Jooyung Han344d5432019-08-23 11:17:39 +09001747func TestVndkApexVersion(t *testing.T) {
1748 ctx, _ := testApex(t, `
1749 apex_vndk {
1750 name: "myapex_v27",
1751 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001752 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001753 vndk_version: "27",
1754 }
1755
1756 apex_key {
1757 name: "myapex.key",
1758 public_key: "testkey.avbpubkey",
1759 private_key: "testkey.pem",
1760 }
1761
Jooyung Han31c470b2019-10-18 16:26:59 +09001762 vndk_prebuilt_shared {
1763 name: "libvndk27",
1764 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001765 vendor_available: true,
1766 vndk: {
1767 enabled: true,
1768 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001769 target_arch: "arm64",
1770 arch: {
1771 arm: {
1772 srcs: ["libvndk27_arm.so"],
1773 },
1774 arm64: {
1775 srcs: ["libvndk27_arm64.so"],
1776 },
1777 },
Jooyung Han344d5432019-08-23 11:17:39 +09001778 }
1779
1780 vndk_prebuilt_shared {
1781 name: "libvndk27",
1782 version: "27",
1783 vendor_available: true,
1784 vndk: {
1785 enabled: true,
1786 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001787 target_arch: "x86_64",
1788 arch: {
1789 x86: {
1790 srcs: ["libvndk27_x86.so"],
1791 },
1792 x86_64: {
1793 srcs: ["libvndk27_x86_64.so"],
1794 },
1795 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001796 }
1797 `+vndkLibrariesTxtFiles("27"),
1798 withFiles(map[string][]byte{
1799 "libvndk27_arm.so": nil,
1800 "libvndk27_arm64.so": nil,
1801 "libvndk27_x86.so": nil,
1802 "libvndk27_x86_64.so": nil,
1803 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001804
Jooyung Han31c470b2019-10-18 16:26:59 +09001805 ensureExactContents(t, ctx, "myapex_v27", []string{
1806 "lib/libvndk27_arm.so",
1807 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001808 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001809 })
Jooyung Han344d5432019-08-23 11:17:39 +09001810}
1811
1812func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1813 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1814 apex_vndk {
1815 name: "myapex_v27",
1816 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001817 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001818 vndk_version: "27",
1819 }
1820 apex_vndk {
1821 name: "myapex_v27_other",
1822 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001823 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001824 vndk_version: "27",
1825 }
1826
1827 apex_key {
1828 name: "myapex.key",
1829 public_key: "testkey.avbpubkey",
1830 private_key: "testkey.pem",
1831 }
1832
1833 cc_library {
1834 name: "libvndk",
1835 srcs: ["mylib.cpp"],
1836 vendor_available: true,
1837 vndk: {
1838 enabled: true,
1839 },
1840 system_shared_libs: [],
1841 stl: "none",
1842 }
1843
1844 vndk_prebuilt_shared {
1845 name: "libvndk",
1846 version: "27",
1847 vendor_available: true,
1848 vndk: {
1849 enabled: true,
1850 },
1851 srcs: ["libvndk.so"],
1852 }
1853 `, withFiles(map[string][]byte{
1854 "libvndk.so": nil,
1855 }))
1856}
1857
Jooyung Han90eee022019-10-01 20:02:42 +09001858func TestVndkApexNameRule(t *testing.T) {
1859 ctx, _ := testApex(t, `
1860 apex_vndk {
1861 name: "myapex",
1862 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001863 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001864 }
1865 apex_vndk {
1866 name: "myapex_v28",
1867 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001868 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001869 vndk_version: "28",
1870 }
1871 apex_key {
1872 name: "myapex.key",
1873 public_key: "testkey.avbpubkey",
1874 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001875 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001876
1877 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001878 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001879 actual := proptools.String(bundle.properties.Apex_name)
1880 if !reflect.DeepEqual(actual, expected) {
1881 t.Errorf("Got '%v', expected '%v'", actual, expected)
1882 }
1883 }
1884
1885 assertApexName("com.android.vndk.vVER", "myapex")
1886 assertApexName("com.android.vndk.v28", "myapex_v28")
1887}
1888
Jooyung Han344d5432019-08-23 11:17:39 +09001889func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1890 ctx, _ := testApex(t, `
1891 apex_vndk {
1892 name: "myapex",
1893 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001894 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001895 }
1896
1897 apex_key {
1898 name: "myapex.key",
1899 public_key: "testkey.avbpubkey",
1900 private_key: "testkey.pem",
1901 }
1902
1903 cc_library {
1904 name: "libvndk",
1905 srcs: ["mylib.cpp"],
1906 vendor_available: true,
1907 native_bridge_supported: true,
1908 host_supported: true,
1909 vndk: {
1910 enabled: true,
1911 },
1912 system_shared_libs: [],
1913 stl: "none",
1914 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001915 `+vndkLibrariesTxtFiles("current"),
1916 withTargets(map[android.OsType][]android.Target{
1917 android.Android: []android.Target{
1918 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1919 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1920 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1921 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1922 },
1923 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001924
Jooyung Han31c470b2019-10-18 16:26:59 +09001925 ensureExactContents(t, ctx, "myapex", []string{
1926 "lib/libvndk.so",
1927 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001928 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001929 })
Jooyung Han344d5432019-08-23 11:17:39 +09001930}
1931
1932func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1933 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1934 apex_vndk {
1935 name: "myapex",
1936 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001937 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001938 native_bridge_supported: true,
1939 }
1940
1941 apex_key {
1942 name: "myapex.key",
1943 public_key: "testkey.avbpubkey",
1944 private_key: "testkey.pem",
1945 }
1946
1947 cc_library {
1948 name: "libvndk",
1949 srcs: ["mylib.cpp"],
1950 vendor_available: true,
1951 native_bridge_supported: true,
1952 host_supported: true,
1953 vndk: {
1954 enabled: true,
1955 },
1956 system_shared_libs: [],
1957 stl: "none",
1958 }
1959 `)
1960}
1961
Jooyung Han31c470b2019-10-18 16:26:59 +09001962func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001963 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001964 apex_vndk {
1965 name: "myapex_v27",
1966 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001967 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001968 vndk_version: "27",
1969 }
1970
1971 apex_key {
1972 name: "myapex.key",
1973 public_key: "testkey.avbpubkey",
1974 private_key: "testkey.pem",
1975 }
1976
1977 vndk_prebuilt_shared {
1978 name: "libvndk27",
1979 version: "27",
1980 target_arch: "arm",
1981 vendor_available: true,
1982 vndk: {
1983 enabled: true,
1984 },
1985 arch: {
1986 arm: {
1987 srcs: ["libvndk27.so"],
1988 }
1989 },
1990 }
1991
1992 vndk_prebuilt_shared {
1993 name: "libvndk27",
1994 version: "27",
1995 target_arch: "arm",
1996 binder32bit: true,
1997 vendor_available: true,
1998 vndk: {
1999 enabled: true,
2000 },
2001 arch: {
2002 arm: {
2003 srcs: ["libvndk27binder32.so"],
2004 }
2005 },
2006 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002007 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002008 withFiles(map[string][]byte{
2009 "libvndk27.so": nil,
2010 "libvndk27binder32.so": nil,
2011 }),
2012 withBinder32bit,
2013 withTargets(map[android.OsType][]android.Target{
2014 android.Android: []android.Target{
2015 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2016 },
2017 }),
2018 )
2019
2020 ensureExactContents(t, ctx, "myapex_v27", []string{
2021 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002022 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002023 })
2024}
2025
Jooyung Hane1633032019-08-01 17:41:43 +09002026func TestDependenciesInApexManifest(t *testing.T) {
2027 ctx, _ := testApex(t, `
2028 apex {
2029 name: "myapex_nodep",
2030 key: "myapex.key",
2031 native_shared_libs: ["lib_nodep"],
2032 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002033 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002034 }
2035
2036 apex {
2037 name: "myapex_dep",
2038 key: "myapex.key",
2039 native_shared_libs: ["lib_dep"],
2040 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002041 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002042 }
2043
2044 apex {
2045 name: "myapex_provider",
2046 key: "myapex.key",
2047 native_shared_libs: ["libfoo"],
2048 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002049 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002050 }
2051
2052 apex {
2053 name: "myapex_selfcontained",
2054 key: "myapex.key",
2055 native_shared_libs: ["lib_dep", "libfoo"],
2056 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002057 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002058 }
2059
2060 apex_key {
2061 name: "myapex.key",
2062 public_key: "testkey.avbpubkey",
2063 private_key: "testkey.pem",
2064 }
2065
2066 cc_library {
2067 name: "lib_nodep",
2068 srcs: ["mylib.cpp"],
2069 system_shared_libs: [],
2070 stl: "none",
2071 }
2072
2073 cc_library {
2074 name: "lib_dep",
2075 srcs: ["mylib.cpp"],
2076 shared_libs: ["libfoo"],
2077 system_shared_libs: [],
2078 stl: "none",
2079 }
2080
2081 cc_library {
2082 name: "libfoo",
2083 srcs: ["mytest.cpp"],
2084 stubs: {
2085 versions: ["1"],
2086 },
2087 system_shared_libs: [],
2088 stl: "none",
2089 }
2090 `)
2091
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002092 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002093 var provideNativeLibs, requireNativeLibs []string
2094
Sundong Ahnabb64432019-10-22 13:58:29 +09002095 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002096 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2097 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002098 ensureListEmpty(t, provideNativeLibs)
2099 ensureListEmpty(t, requireNativeLibs)
2100
Sundong Ahnabb64432019-10-22 13:58:29 +09002101 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002102 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2103 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002104 ensureListEmpty(t, provideNativeLibs)
2105 ensureListContains(t, requireNativeLibs, "libfoo.so")
2106
Sundong Ahnabb64432019-10-22 13:58:29 +09002107 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002108 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2109 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002110 ensureListContains(t, provideNativeLibs, "libfoo.so")
2111 ensureListEmpty(t, requireNativeLibs)
2112
Sundong Ahnabb64432019-10-22 13:58:29 +09002113 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002114 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2115 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002116 ensureListContains(t, provideNativeLibs, "libfoo.so")
2117 ensureListEmpty(t, requireNativeLibs)
2118}
2119
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002120func TestApexName(t *testing.T) {
2121 ctx, _ := testApex(t, `
2122 apex {
2123 name: "myapex",
2124 key: "myapex.key",
2125 apex_name: "com.android.myapex",
2126 }
2127
2128 apex_key {
2129 name: "myapex.key",
2130 public_key: "testkey.avbpubkey",
2131 private_key: "testkey.pem",
2132 }
2133 `)
2134
Sundong Ahnabb64432019-10-22 13:58:29 +09002135 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002136 apexManifestRule := module.Rule("apexManifestRule")
2137 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2138 apexRule := module.Rule("apexRule")
2139 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
2140}
2141
Alex Light0851b882019-02-07 13:20:53 -08002142func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002143 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002144 apex {
2145 name: "myapex",
2146 key: "myapex.key",
2147 native_shared_libs: ["mylib_common"],
2148 }
2149
2150 apex_key {
2151 name: "myapex.key",
2152 public_key: "testkey.avbpubkey",
2153 private_key: "testkey.pem",
2154 }
2155
2156 cc_library {
2157 name: "mylib_common",
2158 srcs: ["mylib.cpp"],
2159 system_shared_libs: [],
2160 stl: "none",
2161 }
2162 `)
2163
Sundong Ahnabb64432019-10-22 13:58:29 +09002164 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002165 apexRule := module.Rule("apexRule")
2166 copyCmds := apexRule.Args["copy_commands"]
2167
2168 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2169 t.Log("Apex was a test apex!")
2170 t.Fail()
2171 }
2172 // Ensure that main rule creates an output
2173 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2174
2175 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002176 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002177
2178 // Ensure that both direct and indirect deps are copied into apex
2179 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2180
Colin Cross7113d202019-11-20 16:39:12 -08002181 // Ensure that the platform variant ends with _shared
2182 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002183
2184 if !android.InAnyApex("mylib_common") {
2185 t.Log("Found mylib_common not in any apex!")
2186 t.Fail()
2187 }
2188}
2189
2190func TestTestApex(t *testing.T) {
2191 if android.InAnyApex("mylib_common_test") {
2192 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!")
2193 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002194 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002195 apex_test {
2196 name: "myapex",
2197 key: "myapex.key",
2198 native_shared_libs: ["mylib_common_test"],
2199 }
2200
2201 apex_key {
2202 name: "myapex.key",
2203 public_key: "testkey.avbpubkey",
2204 private_key: "testkey.pem",
2205 }
2206
2207 cc_library {
2208 name: "mylib_common_test",
2209 srcs: ["mylib.cpp"],
2210 system_shared_libs: [],
2211 stl: "none",
2212 }
2213 `)
2214
Sundong Ahnabb64432019-10-22 13:58:29 +09002215 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002216 apexRule := module.Rule("apexRule")
2217 copyCmds := apexRule.Args["copy_commands"]
2218
2219 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2220 t.Log("Apex was not a test apex!")
2221 t.Fail()
2222 }
2223 // Ensure that main rule creates an output
2224 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2225
2226 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002227 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002228
2229 // Ensure that both direct and indirect deps are copied into apex
2230 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2231
Colin Cross7113d202019-11-20 16:39:12 -08002232 // Ensure that the platform variant ends with _shared
2233 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002234
2235 if android.InAnyApex("mylib_common_test") {
2236 t.Log("Found mylib_common_test in some apex!")
2237 t.Fail()
2238 }
2239}
2240
Alex Light9670d332019-01-29 18:07:33 -08002241func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002242 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002243 apex {
2244 name: "myapex",
2245 key: "myapex.key",
2246 multilib: {
2247 first: {
2248 native_shared_libs: ["mylib_common"],
2249 }
2250 },
2251 target: {
2252 android: {
2253 multilib: {
2254 first: {
2255 native_shared_libs: ["mylib"],
2256 }
2257 }
2258 },
2259 host: {
2260 multilib: {
2261 first: {
2262 native_shared_libs: ["mylib2"],
2263 }
2264 }
2265 }
2266 }
2267 }
2268
2269 apex_key {
2270 name: "myapex.key",
2271 public_key: "testkey.avbpubkey",
2272 private_key: "testkey.pem",
2273 }
2274
2275 cc_library {
2276 name: "mylib",
2277 srcs: ["mylib.cpp"],
2278 system_shared_libs: [],
2279 stl: "none",
2280 }
2281
2282 cc_library {
2283 name: "mylib_common",
2284 srcs: ["mylib.cpp"],
2285 system_shared_libs: [],
2286 stl: "none",
2287 compile_multilib: "first",
2288 }
2289
2290 cc_library {
2291 name: "mylib2",
2292 srcs: ["mylib.cpp"],
2293 system_shared_libs: [],
2294 stl: "none",
2295 compile_multilib: "first",
2296 }
2297 `)
2298
Sundong Ahnabb64432019-10-22 13:58:29 +09002299 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002300 copyCmds := apexRule.Args["copy_commands"]
2301
2302 // Ensure that main rule creates an output
2303 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2304
2305 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002306 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2307 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2308 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002309
2310 // Ensure that both direct and indirect deps are copied into apex
2311 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2312 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2313 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2314
Colin Cross7113d202019-11-20 16:39:12 -08002315 // Ensure that the platform variant ends with _shared
2316 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2317 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2318 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002319}
Jiyong Park04480cf2019-02-06 00:16:29 +09002320
2321func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002322 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002323 apex {
2324 name: "myapex",
2325 key: "myapex.key",
2326 binaries: ["myscript"],
2327 }
2328
2329 apex_key {
2330 name: "myapex.key",
2331 public_key: "testkey.avbpubkey",
2332 private_key: "testkey.pem",
2333 }
2334
2335 sh_binary {
2336 name: "myscript",
2337 src: "mylib.cpp",
2338 filename: "myscript.sh",
2339 sub_dir: "script",
2340 }
2341 `)
2342
Sundong Ahnabb64432019-10-22 13:58:29 +09002343 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002344 copyCmds := apexRule.Args["copy_commands"]
2345
2346 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2347}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002348
Jooyung Han91df2082019-11-20 01:49:42 +09002349func TestApexInVariousPartition(t *testing.T) {
2350 testcases := []struct {
2351 propName, parition, flattenedPartition string
2352 }{
2353 {"", "system", "system_ext"},
2354 {"product_specific: true", "product", "product"},
2355 {"soc_specific: true", "vendor", "vendor"},
2356 {"proprietary: true", "vendor", "vendor"},
2357 {"vendor: true", "vendor", "vendor"},
2358 {"system_ext_specific: true", "system_ext", "system_ext"},
2359 }
2360 for _, tc := range testcases {
2361 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2362 ctx, _ := testApex(t, `
2363 apex {
2364 name: "myapex",
2365 key: "myapex.key",
2366 `+tc.propName+`
2367 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002368
Jooyung Han91df2082019-11-20 01:49:42 +09002369 apex_key {
2370 name: "myapex.key",
2371 public_key: "testkey.avbpubkey",
2372 private_key: "testkey.pem",
2373 }
2374 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002375
Jooyung Han91df2082019-11-20 01:49:42 +09002376 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2377 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2378 actual := apex.installDir.String()
2379 if actual != expected {
2380 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2381 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002382
Jooyung Han91df2082019-11-20 01:49:42 +09002383 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2384 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2385 actual = flattened.installDir.String()
2386 if actual != expected {
2387 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2388 }
2389 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002390 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002391}
Jiyong Park67882562019-03-21 01:11:21 +09002392
Jooyung Han54aca7b2019-11-20 02:26:02 +09002393func TestFileContexts(t *testing.T) {
2394 ctx, _ := testApex(t, `
2395 apex {
2396 name: "myapex",
2397 key: "myapex.key",
2398 }
2399
2400 apex_key {
2401 name: "myapex.key",
2402 public_key: "testkey.avbpubkey",
2403 private_key: "testkey.pem",
2404 }
2405 `)
2406 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2407 apexRule := module.Rule("apexRule")
2408 actual := apexRule.Args["file_contexts"]
2409 expected := "system/sepolicy/apex/myapex-file_contexts"
2410 if actual != expected {
2411 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2412 }
2413
2414 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2415 apex {
2416 name: "myapex",
2417 key: "myapex.key",
2418 file_contexts: "my_own_file_contexts",
2419 }
2420
2421 apex_key {
2422 name: "myapex.key",
2423 public_key: "testkey.avbpubkey",
2424 private_key: "testkey.pem",
2425 }
2426 `, withFiles(map[string][]byte{
2427 "my_own_file_contexts": nil,
2428 }))
2429
2430 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2431 apex {
2432 name: "myapex",
2433 key: "myapex.key",
2434 product_specific: true,
2435 file_contexts: "product_specific_file_contexts",
2436 }
2437
2438 apex_key {
2439 name: "myapex.key",
2440 public_key: "testkey.avbpubkey",
2441 private_key: "testkey.pem",
2442 }
2443 `)
2444
2445 ctx, _ = testApex(t, `
2446 apex {
2447 name: "myapex",
2448 key: "myapex.key",
2449 product_specific: true,
2450 file_contexts: "product_specific_file_contexts",
2451 }
2452
2453 apex_key {
2454 name: "myapex.key",
2455 public_key: "testkey.avbpubkey",
2456 private_key: "testkey.pem",
2457 }
2458 `, withFiles(map[string][]byte{
2459 "product_specific_file_contexts": nil,
2460 }))
2461 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2462 apexRule = module.Rule("apexRule")
2463 actual = apexRule.Args["file_contexts"]
2464 expected = "product_specific_file_contexts"
2465 if actual != expected {
2466 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2467 }
2468
2469 ctx, _ = testApex(t, `
2470 apex {
2471 name: "myapex",
2472 key: "myapex.key",
2473 product_specific: true,
2474 file_contexts: ":my-file-contexts",
2475 }
2476
2477 apex_key {
2478 name: "myapex.key",
2479 public_key: "testkey.avbpubkey",
2480 private_key: "testkey.pem",
2481 }
2482
2483 filegroup {
2484 name: "my-file-contexts",
2485 srcs: ["product_specific_file_contexts"],
2486 }
2487 `, withFiles(map[string][]byte{
2488 "product_specific_file_contexts": nil,
2489 }))
2490 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2491 apexRule = module.Rule("apexRule")
2492 actual = apexRule.Args["file_contexts"]
2493 expected = "product_specific_file_contexts"
2494 if actual != expected {
2495 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2496 }
2497}
2498
Jiyong Park67882562019-03-21 01:11:21 +09002499func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002500 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002501 apex_key {
2502 name: "myapex.key",
2503 public_key: ":my.avbpubkey",
2504 private_key: ":my.pem",
2505 product_specific: true,
2506 }
2507
2508 filegroup {
2509 name: "my.avbpubkey",
2510 srcs: ["testkey2.avbpubkey"],
2511 }
2512
2513 filegroup {
2514 name: "my.pem",
2515 srcs: ["testkey2.pem"],
2516 }
2517 `)
2518
2519 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2520 expected_pubkey := "testkey2.avbpubkey"
2521 actual_pubkey := apex_key.public_key_file.String()
2522 if actual_pubkey != expected_pubkey {
2523 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2524 }
2525 expected_privkey := "testkey2.pem"
2526 actual_privkey := apex_key.private_key_file.String()
2527 if actual_privkey != expected_privkey {
2528 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2529 }
2530}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002531
2532func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002533 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002534 prebuilt_apex {
2535 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002536 arch: {
2537 arm64: {
2538 src: "myapex-arm64.apex",
2539 },
2540 arm: {
2541 src: "myapex-arm.apex",
2542 },
2543 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002544 }
2545 `)
2546
2547 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2548
Jiyong Parkc95714e2019-03-29 14:23:10 +09002549 expectedInput := "myapex-arm64.apex"
2550 if prebuilt.inputApex.String() != expectedInput {
2551 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2552 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002553}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002554
2555func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002556 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002557 prebuilt_apex {
2558 name: "myapex",
2559 src: "myapex-arm.apex",
2560 filename: "notmyapex.apex",
2561 }
2562 `)
2563
2564 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2565
2566 expected := "notmyapex.apex"
2567 if p.installFilename != expected {
2568 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2569 }
2570}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002571
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002572func TestPrebuiltOverrides(t *testing.T) {
2573 ctx, config := testApex(t, `
2574 prebuilt_apex {
2575 name: "myapex.prebuilt",
2576 src: "myapex-arm.apex",
2577 overrides: [
2578 "myapex",
2579 ],
2580 }
2581 `)
2582
2583 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2584
2585 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002586 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002587 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002588 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002589 }
2590}
2591
Roland Levillain630846d2019-06-26 12:48:34 +01002592func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002593 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002594 apex_test {
2595 name: "myapex",
2596 key: "myapex.key",
2597 tests: [
2598 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002599 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002600 ],
2601 }
2602
2603 apex_key {
2604 name: "myapex.key",
2605 public_key: "testkey.avbpubkey",
2606 private_key: "testkey.pem",
2607 }
2608
2609 cc_test {
2610 name: "mytest",
2611 gtest: false,
2612 srcs: ["mytest.cpp"],
2613 relative_install_path: "test",
2614 system_shared_libs: [],
2615 static_executable: true,
2616 stl: "none",
2617 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002618
2619 cc_test {
2620 name: "mytests",
2621 gtest: false,
2622 srcs: [
2623 "mytest1.cpp",
2624 "mytest2.cpp",
2625 "mytest3.cpp",
2626 ],
2627 test_per_src: true,
2628 relative_install_path: "test",
2629 system_shared_libs: [],
2630 static_executable: true,
2631 stl: "none",
2632 }
Roland Levillain630846d2019-06-26 12:48:34 +01002633 `)
2634
Sundong Ahnabb64432019-10-22 13:58:29 +09002635 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002636 copyCmds := apexRule.Args["copy_commands"]
2637
2638 // Ensure that test dep is copied into apex.
2639 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002640
2641 // Ensure that test deps built with `test_per_src` are copied into apex.
2642 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2643 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2644 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002645
2646 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002647 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002648 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2649 name := apexBundle.BaseModuleName()
2650 prefix := "TARGET_"
2651 var builder strings.Builder
2652 data.Custom(&builder, name, prefix, "", data)
2653 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002654 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2655 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2656 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2657 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002658 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002659 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002660 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002661}
2662
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002663func TestInstallExtraFlattenedApexes(t *testing.T) {
2664 ctx, config := testApex(t, `
2665 apex {
2666 name: "myapex",
2667 key: "myapex.key",
2668 }
2669 apex_key {
2670 name: "myapex.key",
2671 public_key: "testkey.avbpubkey",
2672 private_key: "testkey.pem",
2673 }
2674 `, func(fs map[string][]byte, config android.Config) {
2675 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2676 })
2677 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2678 ensureListContains(t, ab.externalDeps, "myapex.flattened")
2679 mk := android.AndroidMkDataForTest(t, config, "", ab)
2680 var builder strings.Builder
2681 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2682 androidMk := builder.String()
2683 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2684}
2685
Jooyung Han5c998b92019-06-27 11:30:33 +09002686func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002687 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002688 apex {
2689 name: "myapex",
2690 key: "myapex.key",
2691 native_shared_libs: ["mylib"],
2692 uses: ["commonapex"],
2693 }
2694
2695 apex {
2696 name: "commonapex",
2697 key: "myapex.key",
2698 native_shared_libs: ["libcommon"],
2699 provide_cpp_shared_libs: true,
2700 }
2701
2702 apex_key {
2703 name: "myapex.key",
2704 public_key: "testkey.avbpubkey",
2705 private_key: "testkey.pem",
2706 }
2707
2708 cc_library {
2709 name: "mylib",
2710 srcs: ["mylib.cpp"],
2711 shared_libs: ["libcommon"],
2712 system_shared_libs: [],
2713 stl: "none",
2714 }
2715
2716 cc_library {
2717 name: "libcommon",
2718 srcs: ["mylib_common.cpp"],
2719 system_shared_libs: [],
2720 stl: "none",
2721 }
2722 `)
2723
Sundong Ahnabb64432019-10-22 13:58:29 +09002724 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002725 apexRule1 := module1.Rule("apexRule")
2726 copyCmds1 := apexRule1.Args["copy_commands"]
2727
Sundong Ahnabb64432019-10-22 13:58:29 +09002728 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002729 apexRule2 := module2.Rule("apexRule")
2730 copyCmds2 := apexRule2.Args["copy_commands"]
2731
Colin Cross7113d202019-11-20 16:39:12 -08002732 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2733 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002734 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2735 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2736 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2737}
2738
2739func TestApexUsesFailsIfNotProvided(t *testing.T) {
2740 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2741 apex {
2742 name: "myapex",
2743 key: "myapex.key",
2744 uses: ["commonapex"],
2745 }
2746
2747 apex {
2748 name: "commonapex",
2749 key: "myapex.key",
2750 }
2751
2752 apex_key {
2753 name: "myapex.key",
2754 public_key: "testkey.avbpubkey",
2755 private_key: "testkey.pem",
2756 }
2757 `)
2758 testApexError(t, `uses: "commonapex" is not a provider`, `
2759 apex {
2760 name: "myapex",
2761 key: "myapex.key",
2762 uses: ["commonapex"],
2763 }
2764
2765 cc_library {
2766 name: "commonapex",
2767 system_shared_libs: [],
2768 stl: "none",
2769 }
2770
2771 apex_key {
2772 name: "myapex.key",
2773 public_key: "testkey.avbpubkey",
2774 private_key: "testkey.pem",
2775 }
2776 `)
2777}
2778
2779func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2780 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2781 apex {
2782 name: "myapex",
2783 key: "myapex.key",
2784 use_vendor: true,
2785 uses: ["commonapex"],
2786 }
2787
2788 apex {
2789 name: "commonapex",
2790 key: "myapex.key",
2791 provide_cpp_shared_libs: true,
2792 }
2793
2794 apex_key {
2795 name: "myapex.key",
2796 public_key: "testkey.avbpubkey",
2797 private_key: "testkey.pem",
2798 }
Jooyung Handc782442019-11-01 03:14:38 +09002799 `, func(fs map[string][]byte, config android.Config) {
2800 setUseVendorWhitelistForTest(config, []string{"myapex"})
2801 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002802}
2803
Jooyung Hand48f3c32019-08-23 11:18:57 +09002804func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2805 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2806 apex {
2807 name: "myapex",
2808 key: "myapex.key",
2809 native_shared_libs: ["libfoo"],
2810 }
2811
2812 apex_key {
2813 name: "myapex.key",
2814 public_key: "testkey.avbpubkey",
2815 private_key: "testkey.pem",
2816 }
2817
2818 cc_library {
2819 name: "libfoo",
2820 stl: "none",
2821 system_shared_libs: [],
2822 enabled: false,
2823 }
2824 `)
2825 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2826 apex {
2827 name: "myapex",
2828 key: "myapex.key",
2829 java_libs: ["myjar"],
2830 }
2831
2832 apex_key {
2833 name: "myapex.key",
2834 public_key: "testkey.avbpubkey",
2835 private_key: "testkey.pem",
2836 }
2837
2838 java_library {
2839 name: "myjar",
2840 srcs: ["foo/bar/MyClass.java"],
2841 sdk_version: "none",
2842 system_modules: "none",
2843 compile_dex: true,
2844 enabled: false,
2845 }
2846 `)
2847}
2848
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002849func TestApexWithApps(t *testing.T) {
2850 ctx, _ := testApex(t, `
2851 apex {
2852 name: "myapex",
2853 key: "myapex.key",
2854 apps: [
2855 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002856 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002857 ],
2858 }
2859
2860 apex_key {
2861 name: "myapex.key",
2862 public_key: "testkey.avbpubkey",
2863 private_key: "testkey.pem",
2864 }
2865
2866 android_app {
2867 name: "AppFoo",
2868 srcs: ["foo/bar/MyClass.java"],
2869 sdk_version: "none",
2870 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002871 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002872 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002873
2874 android_app {
2875 name: "AppFooPriv",
2876 srcs: ["foo/bar/MyClass.java"],
2877 sdk_version: "none",
2878 system_modules: "none",
2879 privileged: true,
2880 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002881
2882 cc_library_shared {
2883 name: "libjni",
2884 srcs: ["mylib.cpp"],
2885 stl: "none",
2886 system_shared_libs: [],
2887 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002888 `)
2889
Sundong Ahnabb64432019-10-22 13:58:29 +09002890 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002891 apexRule := module.Rule("apexRule")
2892 copyCmds := apexRule.Args["copy_commands"]
2893
2894 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002895 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002896
2897 // JNI libraries are embedded inside APK
2898 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
Colin Cross7113d202019-11-20 16:39:12 -08002899 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002900 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2901 // ... uncompressed
2902 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2903 t.Errorf("jni lib is not uncompressed for AppFoo")
2904 }
2905 // ... and not directly inside the APEX
2906 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002907}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002908
Dario Frenicde2a032019-10-27 00:29:22 +01002909func TestApexWithAppImports(t *testing.T) {
2910 ctx, _ := testApex(t, `
2911 apex {
2912 name: "myapex",
2913 key: "myapex.key",
2914 apps: [
2915 "AppFooPrebuilt",
2916 "AppFooPrivPrebuilt",
2917 ],
2918 }
2919
2920 apex_key {
2921 name: "myapex.key",
2922 public_key: "testkey.avbpubkey",
2923 private_key: "testkey.pem",
2924 }
2925
2926 android_app_import {
2927 name: "AppFooPrebuilt",
2928 apk: "PrebuiltAppFoo.apk",
2929 presigned: true,
2930 dex_preopt: {
2931 enabled: false,
2932 },
2933 }
2934
2935 android_app_import {
2936 name: "AppFooPrivPrebuilt",
2937 apk: "PrebuiltAppFooPriv.apk",
2938 privileged: true,
2939 presigned: true,
2940 dex_preopt: {
2941 enabled: false,
2942 },
2943 }
2944 `)
2945
Sundong Ahnabb64432019-10-22 13:58:29 +09002946 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002947 apexRule := module.Rule("apexRule")
2948 copyCmds := apexRule.Args["copy_commands"]
2949
2950 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2951 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002952}
2953
Jooyung Han18020ea2019-11-13 10:50:48 +09002954func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2955 // libfoo's apex_available comes from cc_defaults
2956 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2957 apex {
2958 name: "myapex",
2959 key: "myapex.key",
2960 native_shared_libs: ["libfoo"],
2961 }
2962
2963 apex_key {
2964 name: "myapex.key",
2965 public_key: "testkey.avbpubkey",
2966 private_key: "testkey.pem",
2967 }
2968
2969 apex {
2970 name: "otherapex",
2971 key: "myapex.key",
2972 native_shared_libs: ["libfoo"],
2973 }
2974
2975 cc_defaults {
2976 name: "libfoo-defaults",
2977 apex_available: ["otherapex"],
2978 }
2979
2980 cc_library {
2981 name: "libfoo",
2982 defaults: ["libfoo-defaults"],
2983 stl: "none",
2984 system_shared_libs: [],
2985 }`)
2986}
2987
Jiyong Park127b40b2019-09-30 16:04:35 +09002988func TestApexAvailable(t *testing.T) {
2989 // libfoo is not available to myapex, but only to otherapex
2990 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2991 apex {
2992 name: "myapex",
2993 key: "myapex.key",
2994 native_shared_libs: ["libfoo"],
2995 }
2996
2997 apex_key {
2998 name: "myapex.key",
2999 public_key: "testkey.avbpubkey",
3000 private_key: "testkey.pem",
3001 }
3002
3003 apex {
3004 name: "otherapex",
3005 key: "otherapex.key",
3006 native_shared_libs: ["libfoo"],
3007 }
3008
3009 apex_key {
3010 name: "otherapex.key",
3011 public_key: "testkey.avbpubkey",
3012 private_key: "testkey.pem",
3013 }
3014
3015 cc_library {
3016 name: "libfoo",
3017 stl: "none",
3018 system_shared_libs: [],
3019 apex_available: ["otherapex"],
3020 }`)
3021
3022 // libbar is an indirect dep
3023 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3024 apex {
3025 name: "myapex",
3026 key: "myapex.key",
3027 native_shared_libs: ["libfoo"],
3028 }
3029
3030 apex_key {
3031 name: "myapex.key",
3032 public_key: "testkey.avbpubkey",
3033 private_key: "testkey.pem",
3034 }
3035
3036 apex {
3037 name: "otherapex",
3038 key: "otherapex.key",
3039 native_shared_libs: ["libfoo"],
3040 }
3041
3042 apex_key {
3043 name: "otherapex.key",
3044 public_key: "testkey.avbpubkey",
3045 private_key: "testkey.pem",
3046 }
3047
3048 cc_library {
3049 name: "libfoo",
3050 stl: "none",
3051 shared_libs: ["libbar"],
3052 system_shared_libs: [],
3053 apex_available: ["myapex", "otherapex"],
3054 }
3055
3056 cc_library {
3057 name: "libbar",
3058 stl: "none",
3059 system_shared_libs: [],
3060 apex_available: ["otherapex"],
3061 }`)
3062
3063 testApexError(t, "\"otherapex\" is not a valid module name", `
3064 apex {
3065 name: "myapex",
3066 key: "myapex.key",
3067 native_shared_libs: ["libfoo"],
3068 }
3069
3070 apex_key {
3071 name: "myapex.key",
3072 public_key: "testkey.avbpubkey",
3073 private_key: "testkey.pem",
3074 }
3075
3076 cc_library {
3077 name: "libfoo",
3078 stl: "none",
3079 system_shared_libs: [],
3080 apex_available: ["otherapex"],
3081 }`)
3082
3083 ctx, _ := testApex(t, `
3084 apex {
3085 name: "myapex",
3086 key: "myapex.key",
3087 native_shared_libs: ["libfoo", "libbar"],
3088 }
3089
3090 apex_key {
3091 name: "myapex.key",
3092 public_key: "testkey.avbpubkey",
3093 private_key: "testkey.pem",
3094 }
3095
3096 cc_library {
3097 name: "libfoo",
3098 stl: "none",
3099 system_shared_libs: [],
3100 apex_available: ["myapex"],
3101 }
3102
3103 cc_library {
3104 name: "libbar",
3105 stl: "none",
3106 system_shared_libs: [],
3107 apex_available: ["//apex_available:anyapex"],
3108 }`)
3109
3110 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003111 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3112 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3113 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3114 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003115
3116 ctx, _ = testApex(t, `
3117 apex {
3118 name: "myapex",
3119 key: "myapex.key",
3120 }
3121
3122 apex_key {
3123 name: "myapex.key",
3124 public_key: "testkey.avbpubkey",
3125 private_key: "testkey.pem",
3126 }
3127
3128 cc_library {
3129 name: "libfoo",
3130 stl: "none",
3131 system_shared_libs: [],
3132 apex_available: ["//apex_available:platform"],
3133 }`)
3134
3135 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003136 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3137 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003138
3139 ctx, _ = testApex(t, `
3140 apex {
3141 name: "myapex",
3142 key: "myapex.key",
3143 native_shared_libs: ["libfoo"],
3144 }
3145
3146 apex_key {
3147 name: "myapex.key",
3148 public_key: "testkey.avbpubkey",
3149 private_key: "testkey.pem",
3150 }
3151
3152 cc_library {
3153 name: "libfoo",
3154 stl: "none",
3155 system_shared_libs: [],
3156 apex_available: ["myapex"],
3157 static: {
3158 apex_available: ["//apex_available:platform"],
3159 },
3160 }`)
3161
3162 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08003163 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3164 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003165 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08003166 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3167 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003168}
3169
Jiyong Park5d790c32019-11-15 18:40:32 +09003170func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003171 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003172 apex {
3173 name: "myapex",
3174 key: "myapex.key",
3175 apps: ["app"],
3176 }
3177
3178 override_apex {
3179 name: "override_myapex",
3180 base: "myapex",
3181 apps: ["override_app"],
3182 }
3183
3184 apex_key {
3185 name: "myapex.key",
3186 public_key: "testkey.avbpubkey",
3187 private_key: "testkey.pem",
3188 }
3189
3190 android_app {
3191 name: "app",
3192 srcs: ["foo/bar/MyClass.java"],
3193 package_name: "foo",
3194 sdk_version: "none",
3195 system_modules: "none",
3196 }
3197
3198 override_android_app {
3199 name: "override_app",
3200 base: "app",
3201 package_name: "bar",
3202 }
3203 `)
3204
Jiyong Park317645e2019-12-05 13:20:58 +09003205 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3206 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3207 if originalVariant.GetOverriddenBy() != "" {
3208 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3209 }
3210 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3211 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3212 }
3213
Jiyong Park5d790c32019-11-15 18:40:32 +09003214 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3215 apexRule := module.Rule("apexRule")
3216 copyCmds := apexRule.Args["copy_commands"]
3217
3218 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3219 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003220
3221 apexBundle := module.Module().(*apexBundle)
3222 name := apexBundle.Name()
3223 if name != "override_myapex" {
3224 t.Errorf("name should be \"override_myapex\", but was %q", name)
3225 }
3226
3227 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3228 var builder strings.Builder
3229 data.Custom(&builder, name, "TARGET_", "", data)
3230 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003231 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003232 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3233 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
3234 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003235 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003236 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3237 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003238}
3239
Jooyung Han214bf372019-11-12 13:03:50 +09003240func TestLegacyAndroid10Support(t *testing.T) {
3241 ctx, _ := testApex(t, `
3242 apex {
3243 name: "myapex",
3244 key: "myapex.key",
3245 legacy_android10_support: true,
3246 }
3247
3248 apex_key {
3249 name: "myapex.key",
3250 public_key: "testkey.avbpubkey",
3251 private_key: "testkey.pem",
3252 }
3253 `)
3254
3255 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3256 args := module.Rule("apexRule").Args
3257 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
3258}
3259
Jiyong Park479321d2019-12-16 11:47:12 +09003260func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3261 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3262 apex {
3263 name: "myapex",
3264 key: "myapex.key",
3265 java_libs: ["myjar"],
3266 }
3267
3268 apex_key {
3269 name: "myapex.key",
3270 public_key: "testkey.avbpubkey",
3271 private_key: "testkey.pem",
3272 }
3273
3274 java_library {
3275 name: "myjar",
3276 srcs: ["foo/bar/MyClass.java"],
3277 sdk_version: "none",
3278 system_modules: "none",
3279 }
3280 `)
3281}
3282
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003283func TestMain(m *testing.M) {
3284 run := func() int {
3285 setUp()
3286 defer tearDown()
3287
3288 return m.Run()
3289 }
3290
3291 os.Exit(run())
3292}