blob: 18409001e00af83c953a17e9a8120278f30a187e [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)
Colin Cross98be1bb2019-12-13 20:41:13 -0800306 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000307 java.RegisterJavaBuildComponents(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800308 ctx.RegisterModuleType("java_system_modules", java.SystemModulesFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000309 java.RegisterAppBuildComponents(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800310
311 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
312 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
313 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
314 })
315 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
316 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
317 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
318 ctx.BottomUp("test_per_src", cc.TestPerSrcMutator).Parallel()
319 ctx.BottomUp("version", cc.VersionMutator).Parallel()
320 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
321 })
322 ctx.PreDepsMutators(RegisterPreDepsMutators)
323 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
324 ctx.PostDepsMutators(RegisterPostDepsMutators)
325 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
326 ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel()
327 ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel()
328 })
329
330 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900331
Jooyung Han5c998b92019-06-27 11:30:33 +0900332 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900333}
334
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700335func setUp() {
336 var err error
337 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900338 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700339 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900340 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900341}
342
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700343func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900344 os.RemoveAll(buildDir)
345}
346
347// ensure that 'result' contains 'expected'
348func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900349 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900350 if !strings.Contains(result, expected) {
351 t.Errorf("%q is not found in %q", expected, result)
352 }
353}
354
355// ensures that 'result' does not contain 'notExpected'
356func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900357 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900358 if strings.Contains(result, notExpected) {
359 t.Errorf("%q is found in %q", notExpected, result)
360 }
361}
362
363func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900364 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900365 if !android.InList(expected, result) {
366 t.Errorf("%q is not found in %v", expected, result)
367 }
368}
369
370func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900371 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900372 if android.InList(notExpected, result) {
373 t.Errorf("%q is found in %v", notExpected, result)
374 }
375}
376
Jooyung Hane1633032019-08-01 17:41:43 +0900377func ensureListEmpty(t *testing.T, result []string) {
378 t.Helper()
379 if len(result) > 0 {
380 t.Errorf("%q is expected to be empty", result)
381 }
382}
383
Jiyong Park25fc6a92018-11-18 18:02:45 +0900384// Minimal test
385func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700386 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900387 apex_defaults {
388 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900389 manifest: ":myapex.manifest",
390 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900391 key: "myapex.key",
392 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800393 multilib: {
394 both: {
395 binaries: ["foo",],
396 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900397 },
Jiyong Park9e6c2422019-08-09 20:39:45 +0900398 java_libs: ["myjar", "myprebuiltjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900399 }
400
Jiyong Park30ca9372019-02-07 16:27:23 +0900401 apex {
402 name: "myapex",
403 defaults: ["myapex-defaults"],
404 }
405
Jiyong Park25fc6a92018-11-18 18:02:45 +0900406 apex_key {
407 name: "myapex.key",
408 public_key: "testkey.avbpubkey",
409 private_key: "testkey.pem",
410 }
411
Jiyong Park809bb722019-02-13 21:33:49 +0900412 filegroup {
413 name: "myapex.manifest",
414 srcs: ["apex_manifest.json"],
415 }
416
417 filegroup {
418 name: "myapex.androidmanifest",
419 srcs: ["AndroidManifest.xml"],
420 }
421
Jiyong Park25fc6a92018-11-18 18:02:45 +0900422 cc_library {
423 name: "mylib",
424 srcs: ["mylib.cpp"],
425 shared_libs: ["mylib2"],
426 system_shared_libs: [],
427 stl: "none",
428 }
429
Alex Light3d673592019-01-18 14:37:31 -0800430 cc_binary {
431 name: "foo",
432 srcs: ["mylib.cpp"],
433 compile_multilib: "both",
434 multilib: {
435 lib32: {
436 suffix: "32",
437 },
438 lib64: {
439 suffix: "64",
440 },
441 },
442 symlinks: ["foo_link_"],
443 symlink_preferred_arch: true,
444 system_shared_libs: [],
445 static_executable: true,
446 stl: "none",
447 }
448
Jiyong Park25fc6a92018-11-18 18:02:45 +0900449 cc_library {
450 name: "mylib2",
451 srcs: ["mylib.cpp"],
452 system_shared_libs: [],
453 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900454 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900455 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900456
457 java_library {
458 name: "myjar",
459 srcs: ["foo/bar/MyClass.java"],
460 sdk_version: "none",
461 system_modules: "none",
462 compile_dex: true,
463 static_libs: ["myotherjar"],
464 }
465
466 java_library {
467 name: "myotherjar",
468 srcs: ["foo/bar/MyClass.java"],
469 sdk_version: "none",
470 system_modules: "none",
471 compile_dex: true,
472 }
Jiyong Park9e6c2422019-08-09 20:39:45 +0900473
474 java_import {
475 name: "myprebuiltjar",
476 jars: ["prebuilt.jar"],
477 installable: true,
478 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900479 `)
480
Sundong Ahnabb64432019-10-22 13:58:29 +0900481 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900482
483 optFlags := apexRule.Args["opt_flags"]
484 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700485 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900486 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900487
Jiyong Park25fc6a92018-11-18 18:02:45 +0900488 copyCmds := apexRule.Args["copy_commands"]
489
490 // Ensure that main rule creates an output
491 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
492
493 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800494 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900495 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900496 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900497
498 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800499 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900500 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900501
502 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800503 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
504 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900505 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900506 ensureContains(t, copyCmds, "image.apex/javalib/myprebuiltjar.jar")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900507 // .. but not for java libs
508 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800509
Colin Cross7113d202019-11-20 16:39:12 -0800510 // Ensure that the platform variant ends with _shared or _common
511 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
512 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900513 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
514 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900515 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common")
Alex Light3d673592019-01-18 14:37:31 -0800516
517 // Ensure that all symlinks are present.
518 found_foo_link_64 := false
519 found_foo := false
520 for _, cmd := range strings.Split(copyCmds, " && ") {
521 if strings.HasPrefix(cmd, "ln -s foo64") {
522 if strings.HasSuffix(cmd, "bin/foo") {
523 found_foo = true
524 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
525 found_foo_link_64 = true
526 }
527 }
528 }
529 good := found_foo && found_foo_link_64
530 if !good {
531 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
532 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900533
Sundong Ahnabb64432019-10-22 13:58:29 +0900534 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700535 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700536 if len(noticeInputs) != 2 {
537 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900538 }
539 ensureListContains(t, noticeInputs, "NOTICE")
540 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800541}
542
Jooyung Hanf21c7972019-12-16 22:32:06 +0900543func TestDefaults(t *testing.T) {
544 ctx, _ := testApex(t, `
545 apex_defaults {
546 name: "myapex-defaults",
547 key: "myapex.key",
548 prebuilts: ["myetc"],
549 native_shared_libs: ["mylib"],
550 java_libs: ["myjar"],
551 apps: ["AppFoo"],
552 }
553
554 prebuilt_etc {
555 name: "myetc",
556 src: "myprebuilt",
557 }
558
559 apex {
560 name: "myapex",
561 defaults: ["myapex-defaults"],
562 }
563
564 apex_key {
565 name: "myapex.key",
566 public_key: "testkey.avbpubkey",
567 private_key: "testkey.pem",
568 }
569
570 cc_library {
571 name: "mylib",
572 system_shared_libs: [],
573 stl: "none",
574 }
575
576 java_library {
577 name: "myjar",
578 srcs: ["foo/bar/MyClass.java"],
579 sdk_version: "none",
580 system_modules: "none",
581 compile_dex: true,
582 }
583
584 android_app {
585 name: "AppFoo",
586 srcs: ["foo/bar/MyClass.java"],
587 sdk_version: "none",
588 system_modules: "none",
589 }
590 `)
591 ensureExactContents(t, ctx, "myapex", []string{
592 "etc/myetc",
593 "javalib/myjar.jar",
594 "lib64/mylib.so",
595 "app/AppFoo/AppFoo.apk",
596 })
597}
598
Jooyung Han01a3ee22019-11-02 02:52:25 +0900599func TestApexManifest(t *testing.T) {
600 ctx, _ := testApex(t, `
601 apex {
602 name: "myapex",
603 key: "myapex.key",
604 }
605
606 apex_key {
607 name: "myapex.key",
608 public_key: "testkey.avbpubkey",
609 private_key: "testkey.pem",
610 }
611 `)
612
613 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900614 args := module.Rule("apexRule").Args
615 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
616 t.Error("manifest should be apex_manifest.pb, but " + manifest)
617 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900618}
619
Alex Light5098a612018-11-29 17:12:15 -0800620func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700621 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800622 apex {
623 name: "myapex",
624 key: "myapex.key",
625 payload_type: "zip",
626 native_shared_libs: ["mylib"],
627 }
628
629 apex_key {
630 name: "myapex.key",
631 public_key: "testkey.avbpubkey",
632 private_key: "testkey.pem",
633 }
634
635 cc_library {
636 name: "mylib",
637 srcs: ["mylib.cpp"],
638 shared_libs: ["mylib2"],
639 system_shared_libs: [],
640 stl: "none",
641 }
642
643 cc_library {
644 name: "mylib2",
645 srcs: ["mylib.cpp"],
646 system_shared_libs: [],
647 stl: "none",
648 }
649 `)
650
Sundong Ahnabb64432019-10-22 13:58:29 +0900651 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800652 copyCmds := zipApexRule.Args["copy_commands"]
653
654 // Ensure that main rule creates an output
655 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
656
657 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800658 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800659
660 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800661 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800662
663 // Ensure that both direct and indirect deps are copied into apex
664 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
665 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900666}
667
668func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700669 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900670 apex {
671 name: "myapex",
672 key: "myapex.key",
673 native_shared_libs: ["mylib", "mylib3"],
674 }
675
676 apex_key {
677 name: "myapex.key",
678 public_key: "testkey.avbpubkey",
679 private_key: "testkey.pem",
680 }
681
682 cc_library {
683 name: "mylib",
684 srcs: ["mylib.cpp"],
685 shared_libs: ["mylib2", "mylib3"],
686 system_shared_libs: [],
687 stl: "none",
688 }
689
690 cc_library {
691 name: "mylib2",
692 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900693 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900694 system_shared_libs: [],
695 stl: "none",
696 stubs: {
697 versions: ["1", "2", "3"],
698 },
699 }
700
701 cc_library {
702 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900703 srcs: ["mylib.cpp"],
704 shared_libs: ["mylib4"],
705 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900706 stl: "none",
707 stubs: {
708 versions: ["10", "11", "12"],
709 },
710 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900711
712 cc_library {
713 name: "mylib4",
714 srcs: ["mylib.cpp"],
715 system_shared_libs: [],
716 stl: "none",
717 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900718 `)
719
Sundong Ahnabb64432019-10-22 13:58:29 +0900720 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900721 copyCmds := apexRule.Args["copy_commands"]
722
723 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800724 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900725
726 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800727 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900728
729 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800730 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900731
Colin Cross7113d202019-11-20 16:39:12 -0800732 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900733
734 // Ensure that mylib is linking with the latest version of stubs for mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800735 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900736 // ... and not linking to the non-stub (impl) variant of mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800737 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900738
739 // 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 -0800740 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900741 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800742 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900743
744 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800745 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900746 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900747
748 // Ensure that genstub is invoked with --apex
Colin Cross7113d202019-11-20 16:39:12 -0800749 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3_myapex").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900750
751 ensureExactContents(t, ctx, "myapex", []string{
752 "lib64/mylib.so",
753 "lib64/mylib3.so",
754 "lib64/mylib4.so",
755 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900756}
757
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900758func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700759 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900760 apex {
761 name: "myapex",
762 key: "myapex.key",
763 native_shared_libs: ["mylib"],
764 }
765
766 apex_key {
767 name: "myapex.key",
768 public_key: "testkey.avbpubkey",
769 private_key: "testkey.pem",
770 }
771
772 cc_library {
773 name: "mylib",
774 srcs: ["mylib.cpp"],
775 shared_libs: ["libfoo#10"],
776 system_shared_libs: [],
777 stl: "none",
778 }
779
780 cc_library {
781 name: "libfoo",
782 srcs: ["mylib.cpp"],
783 shared_libs: ["libbar"],
784 system_shared_libs: [],
785 stl: "none",
786 stubs: {
787 versions: ["10", "20", "30"],
788 },
789 }
790
791 cc_library {
792 name: "libbar",
793 srcs: ["mylib.cpp"],
794 system_shared_libs: [],
795 stl: "none",
796 }
797
798 `)
799
Sundong Ahnabb64432019-10-22 13:58:29 +0900800 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900801 copyCmds := apexRule.Args["copy_commands"]
802
803 // Ensure that direct non-stubs dep is always included
804 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
805
806 // Ensure that indirect stubs dep is not included
807 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
808
809 // Ensure that dependency of stubs is not included
810 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
811
Colin Cross7113d202019-11-20 16:39:12 -0800812 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900813
814 // Ensure that mylib is linking with version 10 of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800815 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900816 // ... and not linking to the non-stub (impl) variant of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800817 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900818
Colin Cross7113d202019-11-20 16:39:12 -0800819 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900820
821 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
822 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
823}
824
Jooyung Hand3639552019-08-09 12:57:43 +0900825func TestApexWithRuntimeLibsDependency(t *testing.T) {
826 /*
827 myapex
828 |
829 v (runtime_libs)
830 mylib ------+------> libfoo [provides stub]
831 |
832 `------> libbar
833 */
834 ctx, _ := testApex(t, `
835 apex {
836 name: "myapex",
837 key: "myapex.key",
838 native_shared_libs: ["mylib"],
839 }
840
841 apex_key {
842 name: "myapex.key",
843 public_key: "testkey.avbpubkey",
844 private_key: "testkey.pem",
845 }
846
847 cc_library {
848 name: "mylib",
849 srcs: ["mylib.cpp"],
850 runtime_libs: ["libfoo", "libbar"],
851 system_shared_libs: [],
852 stl: "none",
853 }
854
855 cc_library {
856 name: "libfoo",
857 srcs: ["mylib.cpp"],
858 system_shared_libs: [],
859 stl: "none",
860 stubs: {
861 versions: ["10", "20", "30"],
862 },
863 }
864
865 cc_library {
866 name: "libbar",
867 srcs: ["mylib.cpp"],
868 system_shared_libs: [],
869 stl: "none",
870 }
871
872 `)
873
Sundong Ahnabb64432019-10-22 13:58:29 +0900874 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900875 copyCmds := apexRule.Args["copy_commands"]
876
877 // Ensure that direct non-stubs dep is always included
878 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
879
880 // Ensure that indirect stubs dep is not included
881 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
882
883 // Ensure that runtime_libs dep in included
884 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
885
Sundong Ahnabb64432019-10-22 13:58:29 +0900886 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900887 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
888 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900889
890}
891
Jooyung Han9c80bae2019-08-20 17:30:57 +0900892func TestApexDependencyToLLNDK(t *testing.T) {
893 ctx, _ := testApex(t, `
894 apex {
895 name: "myapex",
896 key: "myapex.key",
897 use_vendor: true,
898 native_shared_libs: ["mylib"],
899 }
900
901 apex_key {
902 name: "myapex.key",
903 public_key: "testkey.avbpubkey",
904 private_key: "testkey.pem",
905 }
906
907 cc_library {
908 name: "mylib",
909 srcs: ["mylib.cpp"],
910 vendor_available: true,
911 shared_libs: ["libbar"],
912 system_shared_libs: [],
913 stl: "none",
914 }
915
916 cc_library {
917 name: "libbar",
918 srcs: ["mylib.cpp"],
919 system_shared_libs: [],
920 stl: "none",
921 }
922
923 llndk_library {
924 name: "libbar",
925 symbol_file: "",
926 }
Jooyung Handc782442019-11-01 03:14:38 +0900927 `, func(fs map[string][]byte, config android.Config) {
928 setUseVendorWhitelistForTest(config, []string{"myapex"})
929 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900930
Sundong Ahnabb64432019-10-22 13:58:29 +0900931 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900932 copyCmds := apexRule.Args["copy_commands"]
933
934 // Ensure that LLNDK dep is not included
935 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
936
Sundong Ahnabb64432019-10-22 13:58:29 +0900937 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900938 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900939
940 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900941 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900942
943}
944
Jiyong Park25fc6a92018-11-18 18:02:45 +0900945func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700946 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900947 apex {
948 name: "myapex",
949 key: "myapex.key",
950 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
951 }
952
953 apex_key {
954 name: "myapex.key",
955 public_key: "testkey.avbpubkey",
956 private_key: "testkey.pem",
957 }
958
959 cc_library {
960 name: "mylib",
961 srcs: ["mylib.cpp"],
962 shared_libs: ["libdl#27"],
963 stl: "none",
964 }
965
966 cc_library_shared {
967 name: "mylib_shared",
968 srcs: ["mylib.cpp"],
969 shared_libs: ["libdl#27"],
970 stl: "none",
971 }
972
973 cc_library {
974 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700975 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900976 nocrt: true,
977 system_shared_libs: [],
978 stl: "none",
979 stubs: {
980 versions: ["27", "28", "29"],
981 },
982 }
983
984 cc_library {
985 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700986 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900987 nocrt: true,
988 system_shared_libs: [],
989 stl: "none",
990 stubs: {
991 versions: ["27", "28", "29"],
992 },
993 }
994
995 cc_library {
996 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700997 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900998 nocrt: true,
999 system_shared_libs: [],
1000 stl: "none",
1001 stubs: {
1002 versions: ["27", "28", "29"],
1003 },
1004 }
Jiyong Parkb0788572018-12-20 22:10:17 +09001005
1006 cc_library {
1007 name: "libBootstrap",
1008 srcs: ["mylib.cpp"],
1009 stl: "none",
1010 bootstrap: true,
1011 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001012 `)
1013
Sundong Ahnabb64432019-10-22 13:58:29 +09001014 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001015 copyCmds := apexRule.Args["copy_commands"]
1016
1017 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001018 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001019 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1020 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001021
1022 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001023 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001024
Colin Cross7113d202019-11-20 16:39:12 -08001025 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1026 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1027 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001028
1029 // For dependency to libc
1030 // Ensure that mylib is linking with the latest version of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001031 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001032 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001033 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001034 // ... Cflags from stub is correctly exported to mylib
1035 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1036 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1037
1038 // For dependency to libm
1039 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001040 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001041 // ... and not linking to the stub variant
Colin Cross7113d202019-11-20 16:39:12 -08001042 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001043 // ... and is not compiling with the stub
1044 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1045 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1046
1047 // For dependency to libdl
1048 // Ensure that mylib is linking with the specified version of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001049 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001050 // ... and not linking to the other versions of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001051 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28_myapex/libdl.so")
1052 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001053 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001054 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001055 // ... Cflags from stub is correctly exported to mylib
1056 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1057 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001058
1059 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001060 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1061 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1062 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1063 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001064}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001065
1066func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001067 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001068 apex {
1069 name: "myapex",
1070 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001071 native_shared_libs: ["mylib"],
1072 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001073 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001074 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001075 }
1076
1077 apex_key {
1078 name: "myapex.key",
1079 public_key: "testkey.avbpubkey",
1080 private_key: "testkey.pem",
1081 }
1082
1083 prebuilt_etc {
1084 name: "myetc",
1085 src: "myprebuilt",
1086 sub_dir: "foo/bar",
1087 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001088
1089 cc_library {
1090 name: "mylib",
1091 srcs: ["mylib.cpp"],
1092 relative_install_path: "foo/bar",
1093 system_shared_libs: [],
1094 stl: "none",
1095 }
1096
1097 cc_binary {
1098 name: "mybin",
1099 srcs: ["mylib.cpp"],
1100 relative_install_path: "foo/bar",
1101 system_shared_libs: [],
1102 static_executable: true,
1103 stl: "none",
1104 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001105 `)
1106
Sundong Ahnabb64432019-10-22 13:58:29 +09001107 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001108 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1109
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001110 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001111 ensureListContains(t, dirs, "etc")
1112 ensureListContains(t, dirs, "etc/foo")
1113 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001114 ensureListContains(t, dirs, "lib64")
1115 ensureListContains(t, dirs, "lib64/foo")
1116 ensureListContains(t, dirs, "lib64/foo/bar")
1117 ensureListContains(t, dirs, "lib")
1118 ensureListContains(t, dirs, "lib/foo")
1119 ensureListContains(t, dirs, "lib/foo/bar")
1120
Jiyong Parkbd13e442019-03-15 18:10:35 +09001121 ensureListContains(t, dirs, "bin")
1122 ensureListContains(t, dirs, "bin/foo")
1123 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001124}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001125
1126func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001127 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001128 apex {
1129 name: "myapex",
1130 key: "myapex.key",
1131 native_shared_libs: ["mylib"],
1132 use_vendor: true,
1133 }
1134
1135 apex_key {
1136 name: "myapex.key",
1137 public_key: "testkey.avbpubkey",
1138 private_key: "testkey.pem",
1139 }
1140
1141 cc_library {
1142 name: "mylib",
1143 srcs: ["mylib.cpp"],
1144 shared_libs: ["mylib2"],
1145 system_shared_libs: [],
1146 vendor_available: true,
1147 stl: "none",
1148 }
1149
1150 cc_library {
1151 name: "mylib2",
1152 srcs: ["mylib.cpp"],
1153 system_shared_libs: [],
1154 vendor_available: true,
1155 stl: "none",
1156 }
Jooyung Handc782442019-11-01 03:14:38 +09001157 `, func(fs map[string][]byte, config android.Config) {
1158 setUseVendorWhitelistForTest(config, []string{"myapex"})
1159 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001160
1161 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001162 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001163 for _, implicit := range i.Implicits {
1164 inputsList = append(inputsList, implicit.String())
1165 }
1166 }
1167 inputsString := strings.Join(inputsList, " ")
1168
1169 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001170 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1171 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001172
1173 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001174 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1175 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001176}
Jiyong Park16e91a02018-12-20 18:18:08 +09001177
Jooyung Handc782442019-11-01 03:14:38 +09001178func TestUseVendorRestriction(t *testing.T) {
1179 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1180 apex {
1181 name: "myapex",
1182 key: "myapex.key",
1183 use_vendor: true,
1184 }
1185 apex_key {
1186 name: "myapex.key",
1187 public_key: "testkey.avbpubkey",
1188 private_key: "testkey.pem",
1189 }
1190 `, func(fs map[string][]byte, config android.Config) {
1191 setUseVendorWhitelistForTest(config, []string{""})
1192 })
1193 // no error with whitelist
1194 testApex(t, `
1195 apex {
1196 name: "myapex",
1197 key: "myapex.key",
1198 use_vendor: true,
1199 }
1200 apex_key {
1201 name: "myapex.key",
1202 public_key: "testkey.avbpubkey",
1203 private_key: "testkey.pem",
1204 }
1205 `, func(fs map[string][]byte, config android.Config) {
1206 setUseVendorWhitelistForTest(config, []string{"myapex"})
1207 })
1208}
1209
Jooyung Han5c998b92019-06-27 11:30:33 +09001210func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1211 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1212 apex {
1213 name: "myapex",
1214 key: "myapex.key",
1215 native_shared_libs: ["mylib"],
1216 use_vendor: true,
1217 }
1218
1219 apex_key {
1220 name: "myapex.key",
1221 public_key: "testkey.avbpubkey",
1222 private_key: "testkey.pem",
1223 }
1224
1225 cc_library {
1226 name: "mylib",
1227 srcs: ["mylib.cpp"],
1228 system_shared_libs: [],
1229 stl: "none",
1230 }
1231 `)
1232}
1233
Jiyong Park16e91a02018-12-20 18:18:08 +09001234func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001235 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001236 apex {
1237 name: "myapex",
1238 key: "myapex.key",
1239 native_shared_libs: ["mylib"],
1240 }
1241
1242 apex_key {
1243 name: "myapex.key",
1244 public_key: "testkey.avbpubkey",
1245 private_key: "testkey.pem",
1246 }
1247
1248 cc_library {
1249 name: "mylib",
1250 srcs: ["mylib.cpp"],
1251 system_shared_libs: [],
1252 stl: "none",
1253 stubs: {
1254 versions: ["1", "2", "3"],
1255 },
1256 }
1257
1258 cc_binary {
1259 name: "not_in_apex",
1260 srcs: ["mylib.cpp"],
1261 static_libs: ["mylib"],
1262 static_executable: true,
1263 system_shared_libs: [],
1264 stl: "none",
1265 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001266 `)
1267
Colin Cross7113d202019-11-20 16:39:12 -08001268 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001269
1270 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001271 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001272}
Jiyong Park9335a262018-12-24 11:31:58 +09001273
1274func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001275 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001276 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001277 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001278 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001279 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001280 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001281 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001282 }
1283
1284 cc_library {
1285 name: "mylib",
1286 srcs: ["mylib.cpp"],
1287 system_shared_libs: [],
1288 stl: "none",
1289 }
1290
1291 apex_key {
1292 name: "myapex.key",
1293 public_key: "testkey.avbpubkey",
1294 private_key: "testkey.pem",
1295 }
1296
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001297 android_app_certificate {
1298 name: "myapex.certificate",
1299 certificate: "testkey",
1300 }
1301
1302 android_app_certificate {
1303 name: "myapex.certificate.override",
1304 certificate: "testkey.override",
1305 }
1306
Jiyong Park9335a262018-12-24 11:31:58 +09001307 `)
1308
1309 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001310 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001311
1312 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1313 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1314 "vendor/foo/devkeys/testkey.avbpubkey")
1315 }
1316 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1317 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1318 "vendor/foo/devkeys/testkey.pem")
1319 }
1320
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001321 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001322 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001323 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001324 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001325 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001326 }
1327}
Jiyong Park58e364a2019-01-19 19:24:06 +09001328
Jooyung Hanf121a652019-12-17 14:30:11 +09001329func TestCertificate(t *testing.T) {
1330 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1331 ctx, _ := testApex(t, `
1332 apex {
1333 name: "myapex",
1334 key: "myapex.key",
1335 }
1336 apex_key {
1337 name: "myapex.key",
1338 public_key: "testkey.avbpubkey",
1339 private_key: "testkey.pem",
1340 }`)
1341 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1342 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1343 if actual := rule.Args["certificates"]; actual != expected {
1344 t.Errorf("certificates should be %q, not %q", expected, actual)
1345 }
1346 })
1347 t.Run("override when unspecified", func(t *testing.T) {
1348 ctx, _ := testApex(t, `
1349 apex {
1350 name: "myapex_keytest",
1351 key: "myapex.key",
1352 file_contexts: ":myapex-file_contexts",
1353 }
1354 apex_key {
1355 name: "myapex.key",
1356 public_key: "testkey.avbpubkey",
1357 private_key: "testkey.pem",
1358 }
1359 android_app_certificate {
1360 name: "myapex.certificate.override",
1361 certificate: "testkey.override",
1362 }`)
1363 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1364 expected := "testkey.override.x509.pem testkey.override.pk8"
1365 if actual := rule.Args["certificates"]; actual != expected {
1366 t.Errorf("certificates should be %q, not %q", expected, actual)
1367 }
1368 })
1369 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1370 ctx, _ := testApex(t, `
1371 apex {
1372 name: "myapex",
1373 key: "myapex.key",
1374 certificate: ":myapex.certificate",
1375 }
1376 apex_key {
1377 name: "myapex.key",
1378 public_key: "testkey.avbpubkey",
1379 private_key: "testkey.pem",
1380 }
1381 android_app_certificate {
1382 name: "myapex.certificate",
1383 certificate: "testkey",
1384 }`)
1385 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1386 expected := "testkey.x509.pem testkey.pk8"
1387 if actual := rule.Args["certificates"]; actual != expected {
1388 t.Errorf("certificates should be %q, not %q", expected, actual)
1389 }
1390 })
1391 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1392 ctx, _ := testApex(t, `
1393 apex {
1394 name: "myapex_keytest",
1395 key: "myapex.key",
1396 file_contexts: ":myapex-file_contexts",
1397 certificate: ":myapex.certificate",
1398 }
1399 apex_key {
1400 name: "myapex.key",
1401 public_key: "testkey.avbpubkey",
1402 private_key: "testkey.pem",
1403 }
1404 android_app_certificate {
1405 name: "myapex.certificate.override",
1406 certificate: "testkey.override",
1407 }`)
1408 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1409 expected := "testkey.override.x509.pem testkey.override.pk8"
1410 if actual := rule.Args["certificates"]; actual != expected {
1411 t.Errorf("certificates should be %q, not %q", expected, actual)
1412 }
1413 })
1414 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1415 ctx, _ := testApex(t, `
1416 apex {
1417 name: "myapex",
1418 key: "myapex.key",
1419 certificate: "testkey",
1420 }
1421 apex_key {
1422 name: "myapex.key",
1423 public_key: "testkey.avbpubkey",
1424 private_key: "testkey.pem",
1425 }`)
1426 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1427 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1428 if actual := rule.Args["certificates"]; actual != expected {
1429 t.Errorf("certificates should be %q, not %q", expected, actual)
1430 }
1431 })
1432 t.Run("override when specified as <name>", func(t *testing.T) {
1433 ctx, _ := testApex(t, `
1434 apex {
1435 name: "myapex_keytest",
1436 key: "myapex.key",
1437 file_contexts: ":myapex-file_contexts",
1438 certificate: "testkey",
1439 }
1440 apex_key {
1441 name: "myapex.key",
1442 public_key: "testkey.avbpubkey",
1443 private_key: "testkey.pem",
1444 }
1445 android_app_certificate {
1446 name: "myapex.certificate.override",
1447 certificate: "testkey.override",
1448 }`)
1449 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1450 expected := "testkey.override.x509.pem testkey.override.pk8"
1451 if actual := rule.Args["certificates"]; actual != expected {
1452 t.Errorf("certificates should be %q, not %q", expected, actual)
1453 }
1454 })
1455}
1456
Jiyong Park58e364a2019-01-19 19:24:06 +09001457func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001458 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001459 apex {
1460 name: "myapex",
1461 key: "myapex.key",
1462 native_shared_libs: ["mylib"],
1463 }
1464
1465 apex {
1466 name: "otherapex",
1467 key: "myapex.key",
1468 native_shared_libs: ["mylib"],
1469 }
1470
1471 apex_key {
1472 name: "myapex.key",
1473 public_key: "testkey.avbpubkey",
1474 private_key: "testkey.pem",
1475 }
1476
1477 cc_library {
1478 name: "mylib",
1479 srcs: ["mylib.cpp"],
1480 system_shared_libs: [],
1481 stl: "none",
1482 }
1483 `)
1484
Jooyung Han6b8459b2019-10-30 08:29:25 +09001485 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001486 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001487 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001488 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1489 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001490
Jooyung Han6b8459b2019-10-30 08:29:25 +09001491 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001492 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001493 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001494 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1495 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001496
Jooyung Han6b8459b2019-10-30 08:29:25 +09001497 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001498 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001499 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001500 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1501 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001502}
Jiyong Park7e636d02019-01-28 16:16:54 +09001503
1504func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001505 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001506 apex {
1507 name: "myapex",
1508 key: "myapex.key",
1509 native_shared_libs: ["mylib"],
1510 }
1511
1512 apex_key {
1513 name: "myapex.key",
1514 public_key: "testkey.avbpubkey",
1515 private_key: "testkey.pem",
1516 }
1517
1518 cc_library_headers {
1519 name: "mylib_headers",
1520 export_include_dirs: ["my_include"],
1521 system_shared_libs: [],
1522 stl: "none",
1523 }
1524
1525 cc_library {
1526 name: "mylib",
1527 srcs: ["mylib.cpp"],
1528 system_shared_libs: [],
1529 stl: "none",
1530 header_libs: ["mylib_headers"],
1531 export_header_lib_headers: ["mylib_headers"],
1532 stubs: {
1533 versions: ["1", "2", "3"],
1534 },
1535 }
1536
1537 cc_library {
1538 name: "otherlib",
1539 srcs: ["mylib.cpp"],
1540 system_shared_libs: [],
1541 stl: "none",
1542 shared_libs: ["mylib"],
1543 }
1544 `)
1545
Colin Cross7113d202019-11-20 16:39:12 -08001546 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001547
1548 // Ensure that the include path of the header lib is exported to 'otherlib'
1549 ensureContains(t, cFlags, "-Imy_include")
1550}
Alex Light9670d332019-01-29 18:07:33 -08001551
Jooyung Han31c470b2019-10-18 16:26:59 +09001552func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1553 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001554 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001555 copyCmds := apexRule.Args["copy_commands"]
1556 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001557 var failed bool
1558 var surplus []string
1559 filesMatched := make(map[string]bool)
1560 addContent := func(content string) {
1561 for _, expected := range files {
1562 if matched, _ := path.Match(expected, content); matched {
1563 filesMatched[expected] = true
1564 return
1565 }
1566 }
1567 surplus = append(surplus, content)
1568 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001569 for _, cmd := range strings.Split(copyCmds, "&&") {
1570 cmd = strings.TrimSpace(cmd)
1571 if cmd == "" {
1572 continue
1573 }
1574 terms := strings.Split(cmd, " ")
1575 switch terms[0] {
1576 case "mkdir":
1577 case "cp":
1578 if len(terms) != 3 {
1579 t.Fatal("copyCmds contains invalid cp command", cmd)
1580 }
1581 dst := terms[2]
1582 index := strings.Index(dst, imageApexDir)
1583 if index == -1 {
1584 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1585 }
1586 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001587 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001588 default:
1589 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1590 }
1591 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001592
Jooyung Han31c470b2019-10-18 16:26:59 +09001593 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001594 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001595 t.Log("surplus files", surplus)
1596 failed = true
1597 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001598
1599 if len(files) > len(filesMatched) {
1600 var missing []string
1601 for _, expected := range files {
1602 if !filesMatched[expected] {
1603 missing = append(missing, expected)
1604 }
1605 }
1606 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001607 t.Log("missing files", missing)
1608 failed = true
1609 }
1610 if failed {
1611 t.Fail()
1612 }
1613}
1614
Jooyung Han344d5432019-08-23 11:17:39 +09001615func TestVndkApexCurrent(t *testing.T) {
1616 ctx, _ := testApex(t, `
1617 apex_vndk {
1618 name: "myapex",
1619 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001620 }
1621
1622 apex_key {
1623 name: "myapex.key",
1624 public_key: "testkey.avbpubkey",
1625 private_key: "testkey.pem",
1626 }
1627
1628 cc_library {
1629 name: "libvndk",
1630 srcs: ["mylib.cpp"],
1631 vendor_available: true,
1632 vndk: {
1633 enabled: true,
1634 },
1635 system_shared_libs: [],
1636 stl: "none",
1637 }
1638
1639 cc_library {
1640 name: "libvndksp",
1641 srcs: ["mylib.cpp"],
1642 vendor_available: true,
1643 vndk: {
1644 enabled: true,
1645 support_system_process: true,
1646 },
1647 system_shared_libs: [],
1648 stl: "none",
1649 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001650 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001651
Jooyung Han31c470b2019-10-18 16:26:59 +09001652 ensureExactContents(t, ctx, "myapex", []string{
1653 "lib/libvndk.so",
1654 "lib/libvndksp.so",
1655 "lib64/libvndk.so",
1656 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001657 "etc/llndk.libraries.VER.txt",
1658 "etc/vndkcore.libraries.VER.txt",
1659 "etc/vndksp.libraries.VER.txt",
1660 "etc/vndkprivate.libraries.VER.txt",
1661 "etc/vndkcorevariant.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001662 })
Jooyung Han344d5432019-08-23 11:17:39 +09001663}
1664
1665func TestVndkApexWithPrebuilt(t *testing.T) {
1666 ctx, _ := testApex(t, `
1667 apex_vndk {
1668 name: "myapex",
1669 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001670 }
1671
1672 apex_key {
1673 name: "myapex.key",
1674 public_key: "testkey.avbpubkey",
1675 private_key: "testkey.pem",
1676 }
1677
1678 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001679 name: "libvndk",
1680 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001681 vendor_available: true,
1682 vndk: {
1683 enabled: true,
1684 },
1685 system_shared_libs: [],
1686 stl: "none",
1687 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001688
1689 cc_prebuilt_library_shared {
1690 name: "libvndk.arm",
1691 srcs: ["libvndk.arm.so"],
1692 vendor_available: true,
1693 vndk: {
1694 enabled: true,
1695 },
1696 enabled: false,
1697 arch: {
1698 arm: {
1699 enabled: true,
1700 },
1701 },
1702 system_shared_libs: [],
1703 stl: "none",
1704 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001705 `+vndkLibrariesTxtFiles("current"),
1706 withFiles(map[string][]byte{
1707 "libvndk.so": nil,
1708 "libvndk.arm.so": nil,
1709 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001710
Jooyung Han31c470b2019-10-18 16:26:59 +09001711 ensureExactContents(t, ctx, "myapex", []string{
1712 "lib/libvndk.so",
1713 "lib/libvndk.arm.so",
1714 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001715 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001716 })
Jooyung Han344d5432019-08-23 11:17:39 +09001717}
1718
Jooyung Han39edb6c2019-11-06 16:53:07 +09001719func vndkLibrariesTxtFiles(vers ...string) (result string) {
1720 for _, v := range vers {
1721 if v == "current" {
1722 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkcorevariant"} {
1723 result += `
1724 vndk_libraries_txt {
1725 name: "` + txt + `.libraries.txt",
1726 }
1727 `
1728 }
1729 } else {
1730 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1731 result += `
1732 prebuilt_etc {
1733 name: "` + txt + `.libraries.` + v + `.txt",
1734 src: "dummy.txt",
1735 }
1736 `
1737 }
1738 }
1739 }
1740 return
1741}
1742
Jooyung Han344d5432019-08-23 11:17:39 +09001743func TestVndkApexVersion(t *testing.T) {
1744 ctx, _ := testApex(t, `
1745 apex_vndk {
1746 name: "myapex_v27",
1747 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001748 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001749 vndk_version: "27",
1750 }
1751
1752 apex_key {
1753 name: "myapex.key",
1754 public_key: "testkey.avbpubkey",
1755 private_key: "testkey.pem",
1756 }
1757
Jooyung Han31c470b2019-10-18 16:26:59 +09001758 vndk_prebuilt_shared {
1759 name: "libvndk27",
1760 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001761 vendor_available: true,
1762 vndk: {
1763 enabled: true,
1764 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001765 target_arch: "arm64",
1766 arch: {
1767 arm: {
1768 srcs: ["libvndk27_arm.so"],
1769 },
1770 arm64: {
1771 srcs: ["libvndk27_arm64.so"],
1772 },
1773 },
Jooyung Han344d5432019-08-23 11:17:39 +09001774 }
1775
1776 vndk_prebuilt_shared {
1777 name: "libvndk27",
1778 version: "27",
1779 vendor_available: true,
1780 vndk: {
1781 enabled: true,
1782 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001783 target_arch: "x86_64",
1784 arch: {
1785 x86: {
1786 srcs: ["libvndk27_x86.so"],
1787 },
1788 x86_64: {
1789 srcs: ["libvndk27_x86_64.so"],
1790 },
1791 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001792 }
1793 `+vndkLibrariesTxtFiles("27"),
1794 withFiles(map[string][]byte{
1795 "libvndk27_arm.so": nil,
1796 "libvndk27_arm64.so": nil,
1797 "libvndk27_x86.so": nil,
1798 "libvndk27_x86_64.so": nil,
1799 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001800
Jooyung Han31c470b2019-10-18 16:26:59 +09001801 ensureExactContents(t, ctx, "myapex_v27", []string{
1802 "lib/libvndk27_arm.so",
1803 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001804 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001805 })
Jooyung Han344d5432019-08-23 11:17:39 +09001806}
1807
1808func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1809 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1810 apex_vndk {
1811 name: "myapex_v27",
1812 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001813 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001814 vndk_version: "27",
1815 }
1816 apex_vndk {
1817 name: "myapex_v27_other",
1818 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001819 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001820 vndk_version: "27",
1821 }
1822
1823 apex_key {
1824 name: "myapex.key",
1825 public_key: "testkey.avbpubkey",
1826 private_key: "testkey.pem",
1827 }
1828
1829 cc_library {
1830 name: "libvndk",
1831 srcs: ["mylib.cpp"],
1832 vendor_available: true,
1833 vndk: {
1834 enabled: true,
1835 },
1836 system_shared_libs: [],
1837 stl: "none",
1838 }
1839
1840 vndk_prebuilt_shared {
1841 name: "libvndk",
1842 version: "27",
1843 vendor_available: true,
1844 vndk: {
1845 enabled: true,
1846 },
1847 srcs: ["libvndk.so"],
1848 }
1849 `, withFiles(map[string][]byte{
1850 "libvndk.so": nil,
1851 }))
1852}
1853
Jooyung Han90eee022019-10-01 20:02:42 +09001854func TestVndkApexNameRule(t *testing.T) {
1855 ctx, _ := testApex(t, `
1856 apex_vndk {
1857 name: "myapex",
1858 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001859 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001860 }
1861 apex_vndk {
1862 name: "myapex_v28",
1863 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001864 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001865 vndk_version: "28",
1866 }
1867 apex_key {
1868 name: "myapex.key",
1869 public_key: "testkey.avbpubkey",
1870 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001871 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001872
1873 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001874 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001875 actual := proptools.String(bundle.properties.Apex_name)
1876 if !reflect.DeepEqual(actual, expected) {
1877 t.Errorf("Got '%v', expected '%v'", actual, expected)
1878 }
1879 }
1880
1881 assertApexName("com.android.vndk.vVER", "myapex")
1882 assertApexName("com.android.vndk.v28", "myapex_v28")
1883}
1884
Jooyung Han344d5432019-08-23 11:17:39 +09001885func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1886 ctx, _ := testApex(t, `
1887 apex_vndk {
1888 name: "myapex",
1889 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001890 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001891 }
1892
1893 apex_key {
1894 name: "myapex.key",
1895 public_key: "testkey.avbpubkey",
1896 private_key: "testkey.pem",
1897 }
1898
1899 cc_library {
1900 name: "libvndk",
1901 srcs: ["mylib.cpp"],
1902 vendor_available: true,
1903 native_bridge_supported: true,
1904 host_supported: true,
1905 vndk: {
1906 enabled: true,
1907 },
1908 system_shared_libs: [],
1909 stl: "none",
1910 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001911 `+vndkLibrariesTxtFiles("current"),
1912 withTargets(map[android.OsType][]android.Target{
1913 android.Android: []android.Target{
1914 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1915 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1916 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1917 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1918 },
1919 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001920
Jooyung Han31c470b2019-10-18 16:26:59 +09001921 ensureExactContents(t, ctx, "myapex", []string{
1922 "lib/libvndk.so",
1923 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001924 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001925 })
Jooyung Han344d5432019-08-23 11:17:39 +09001926}
1927
1928func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1929 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1930 apex_vndk {
1931 name: "myapex",
1932 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001933 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001934 native_bridge_supported: true,
1935 }
1936
1937 apex_key {
1938 name: "myapex.key",
1939 public_key: "testkey.avbpubkey",
1940 private_key: "testkey.pem",
1941 }
1942
1943 cc_library {
1944 name: "libvndk",
1945 srcs: ["mylib.cpp"],
1946 vendor_available: true,
1947 native_bridge_supported: true,
1948 host_supported: true,
1949 vndk: {
1950 enabled: true,
1951 },
1952 system_shared_libs: [],
1953 stl: "none",
1954 }
1955 `)
1956}
1957
Jooyung Han31c470b2019-10-18 16:26:59 +09001958func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001959 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001960 apex_vndk {
1961 name: "myapex_v27",
1962 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001963 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001964 vndk_version: "27",
1965 }
1966
1967 apex_key {
1968 name: "myapex.key",
1969 public_key: "testkey.avbpubkey",
1970 private_key: "testkey.pem",
1971 }
1972
1973 vndk_prebuilt_shared {
1974 name: "libvndk27",
1975 version: "27",
1976 target_arch: "arm",
1977 vendor_available: true,
1978 vndk: {
1979 enabled: true,
1980 },
1981 arch: {
1982 arm: {
1983 srcs: ["libvndk27.so"],
1984 }
1985 },
1986 }
1987
1988 vndk_prebuilt_shared {
1989 name: "libvndk27",
1990 version: "27",
1991 target_arch: "arm",
1992 binder32bit: true,
1993 vendor_available: true,
1994 vndk: {
1995 enabled: true,
1996 },
1997 arch: {
1998 arm: {
1999 srcs: ["libvndk27binder32.so"],
2000 }
2001 },
2002 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002003 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002004 withFiles(map[string][]byte{
2005 "libvndk27.so": nil,
2006 "libvndk27binder32.so": nil,
2007 }),
2008 withBinder32bit,
2009 withTargets(map[android.OsType][]android.Target{
2010 android.Android: []android.Target{
2011 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2012 },
2013 }),
2014 )
2015
2016 ensureExactContents(t, ctx, "myapex_v27", []string{
2017 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002018 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002019 })
2020}
2021
Jooyung Hane1633032019-08-01 17:41:43 +09002022func TestDependenciesInApexManifest(t *testing.T) {
2023 ctx, _ := testApex(t, `
2024 apex {
2025 name: "myapex_nodep",
2026 key: "myapex.key",
2027 native_shared_libs: ["lib_nodep"],
2028 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002029 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002030 }
2031
2032 apex {
2033 name: "myapex_dep",
2034 key: "myapex.key",
2035 native_shared_libs: ["lib_dep"],
2036 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002037 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002038 }
2039
2040 apex {
2041 name: "myapex_provider",
2042 key: "myapex.key",
2043 native_shared_libs: ["libfoo"],
2044 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002045 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002046 }
2047
2048 apex {
2049 name: "myapex_selfcontained",
2050 key: "myapex.key",
2051 native_shared_libs: ["lib_dep", "libfoo"],
2052 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002053 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002054 }
2055
2056 apex_key {
2057 name: "myapex.key",
2058 public_key: "testkey.avbpubkey",
2059 private_key: "testkey.pem",
2060 }
2061
2062 cc_library {
2063 name: "lib_nodep",
2064 srcs: ["mylib.cpp"],
2065 system_shared_libs: [],
2066 stl: "none",
2067 }
2068
2069 cc_library {
2070 name: "lib_dep",
2071 srcs: ["mylib.cpp"],
2072 shared_libs: ["libfoo"],
2073 system_shared_libs: [],
2074 stl: "none",
2075 }
2076
2077 cc_library {
2078 name: "libfoo",
2079 srcs: ["mytest.cpp"],
2080 stubs: {
2081 versions: ["1"],
2082 },
2083 system_shared_libs: [],
2084 stl: "none",
2085 }
2086 `)
2087
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002088 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002089 var provideNativeLibs, requireNativeLibs []string
2090
Sundong Ahnabb64432019-10-22 13:58:29 +09002091 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002092 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2093 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002094 ensureListEmpty(t, provideNativeLibs)
2095 ensureListEmpty(t, requireNativeLibs)
2096
Sundong Ahnabb64432019-10-22 13:58:29 +09002097 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002098 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2099 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002100 ensureListEmpty(t, provideNativeLibs)
2101 ensureListContains(t, requireNativeLibs, "libfoo.so")
2102
Sundong Ahnabb64432019-10-22 13:58:29 +09002103 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002104 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2105 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002106 ensureListContains(t, provideNativeLibs, "libfoo.so")
2107 ensureListEmpty(t, requireNativeLibs)
2108
Sundong Ahnabb64432019-10-22 13:58:29 +09002109 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002110 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2111 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002112 ensureListContains(t, provideNativeLibs, "libfoo.so")
2113 ensureListEmpty(t, requireNativeLibs)
2114}
2115
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002116func TestApexName(t *testing.T) {
2117 ctx, _ := testApex(t, `
2118 apex {
2119 name: "myapex",
2120 key: "myapex.key",
2121 apex_name: "com.android.myapex",
2122 }
2123
2124 apex_key {
2125 name: "myapex.key",
2126 public_key: "testkey.avbpubkey",
2127 private_key: "testkey.pem",
2128 }
2129 `)
2130
Sundong Ahnabb64432019-10-22 13:58:29 +09002131 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002132 apexManifestRule := module.Rule("apexManifestRule")
2133 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2134 apexRule := module.Rule("apexRule")
2135 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
2136}
2137
Alex Light0851b882019-02-07 13:20:53 -08002138func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002139 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002140 apex {
2141 name: "myapex",
2142 key: "myapex.key",
2143 native_shared_libs: ["mylib_common"],
2144 }
2145
2146 apex_key {
2147 name: "myapex.key",
2148 public_key: "testkey.avbpubkey",
2149 private_key: "testkey.pem",
2150 }
2151
2152 cc_library {
2153 name: "mylib_common",
2154 srcs: ["mylib.cpp"],
2155 system_shared_libs: [],
2156 stl: "none",
2157 }
2158 `)
2159
Sundong Ahnabb64432019-10-22 13:58:29 +09002160 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002161 apexRule := module.Rule("apexRule")
2162 copyCmds := apexRule.Args["copy_commands"]
2163
2164 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2165 t.Log("Apex was a test apex!")
2166 t.Fail()
2167 }
2168 // Ensure that main rule creates an output
2169 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2170
2171 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002172 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002173
2174 // Ensure that both direct and indirect deps are copied into apex
2175 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2176
Colin Cross7113d202019-11-20 16:39:12 -08002177 // Ensure that the platform variant ends with _shared
2178 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002179
2180 if !android.InAnyApex("mylib_common") {
2181 t.Log("Found mylib_common not in any apex!")
2182 t.Fail()
2183 }
2184}
2185
2186func TestTestApex(t *testing.T) {
2187 if android.InAnyApex("mylib_common_test") {
2188 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!")
2189 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002190 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002191 apex_test {
2192 name: "myapex",
2193 key: "myapex.key",
2194 native_shared_libs: ["mylib_common_test"],
2195 }
2196
2197 apex_key {
2198 name: "myapex.key",
2199 public_key: "testkey.avbpubkey",
2200 private_key: "testkey.pem",
2201 }
2202
2203 cc_library {
2204 name: "mylib_common_test",
2205 srcs: ["mylib.cpp"],
2206 system_shared_libs: [],
2207 stl: "none",
2208 }
2209 `)
2210
Sundong Ahnabb64432019-10-22 13:58:29 +09002211 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002212 apexRule := module.Rule("apexRule")
2213 copyCmds := apexRule.Args["copy_commands"]
2214
2215 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2216 t.Log("Apex was not a test apex!")
2217 t.Fail()
2218 }
2219 // Ensure that main rule creates an output
2220 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2221
2222 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002223 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002224
2225 // Ensure that both direct and indirect deps are copied into apex
2226 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2227
Colin Cross7113d202019-11-20 16:39:12 -08002228 // Ensure that the platform variant ends with _shared
2229 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002230
2231 if android.InAnyApex("mylib_common_test") {
2232 t.Log("Found mylib_common_test in some apex!")
2233 t.Fail()
2234 }
2235}
2236
Alex Light9670d332019-01-29 18:07:33 -08002237func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002238 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002239 apex {
2240 name: "myapex",
2241 key: "myapex.key",
2242 multilib: {
2243 first: {
2244 native_shared_libs: ["mylib_common"],
2245 }
2246 },
2247 target: {
2248 android: {
2249 multilib: {
2250 first: {
2251 native_shared_libs: ["mylib"],
2252 }
2253 }
2254 },
2255 host: {
2256 multilib: {
2257 first: {
2258 native_shared_libs: ["mylib2"],
2259 }
2260 }
2261 }
2262 }
2263 }
2264
2265 apex_key {
2266 name: "myapex.key",
2267 public_key: "testkey.avbpubkey",
2268 private_key: "testkey.pem",
2269 }
2270
2271 cc_library {
2272 name: "mylib",
2273 srcs: ["mylib.cpp"],
2274 system_shared_libs: [],
2275 stl: "none",
2276 }
2277
2278 cc_library {
2279 name: "mylib_common",
2280 srcs: ["mylib.cpp"],
2281 system_shared_libs: [],
2282 stl: "none",
2283 compile_multilib: "first",
2284 }
2285
2286 cc_library {
2287 name: "mylib2",
2288 srcs: ["mylib.cpp"],
2289 system_shared_libs: [],
2290 stl: "none",
2291 compile_multilib: "first",
2292 }
2293 `)
2294
Sundong Ahnabb64432019-10-22 13:58:29 +09002295 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002296 copyCmds := apexRule.Args["copy_commands"]
2297
2298 // Ensure that main rule creates an output
2299 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2300
2301 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002302 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2303 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2304 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002305
2306 // Ensure that both direct and indirect deps are copied into apex
2307 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2308 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2309 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2310
Colin Cross7113d202019-11-20 16:39:12 -08002311 // Ensure that the platform variant ends with _shared
2312 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2313 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2314 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002315}
Jiyong Park04480cf2019-02-06 00:16:29 +09002316
2317func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002318 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002319 apex {
2320 name: "myapex",
2321 key: "myapex.key",
2322 binaries: ["myscript"],
2323 }
2324
2325 apex_key {
2326 name: "myapex.key",
2327 public_key: "testkey.avbpubkey",
2328 private_key: "testkey.pem",
2329 }
2330
2331 sh_binary {
2332 name: "myscript",
2333 src: "mylib.cpp",
2334 filename: "myscript.sh",
2335 sub_dir: "script",
2336 }
2337 `)
2338
Sundong Ahnabb64432019-10-22 13:58:29 +09002339 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002340 copyCmds := apexRule.Args["copy_commands"]
2341
2342 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2343}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002344
Jooyung Han91df2082019-11-20 01:49:42 +09002345func TestApexInVariousPartition(t *testing.T) {
2346 testcases := []struct {
2347 propName, parition, flattenedPartition string
2348 }{
2349 {"", "system", "system_ext"},
2350 {"product_specific: true", "product", "product"},
2351 {"soc_specific: true", "vendor", "vendor"},
2352 {"proprietary: true", "vendor", "vendor"},
2353 {"vendor: true", "vendor", "vendor"},
2354 {"system_ext_specific: true", "system_ext", "system_ext"},
2355 }
2356 for _, tc := range testcases {
2357 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2358 ctx, _ := testApex(t, `
2359 apex {
2360 name: "myapex",
2361 key: "myapex.key",
2362 `+tc.propName+`
2363 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002364
Jooyung Han91df2082019-11-20 01:49:42 +09002365 apex_key {
2366 name: "myapex.key",
2367 public_key: "testkey.avbpubkey",
2368 private_key: "testkey.pem",
2369 }
2370 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002371
Jooyung Han91df2082019-11-20 01:49:42 +09002372 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2373 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2374 actual := apex.installDir.String()
2375 if actual != expected {
2376 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2377 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002378
Jooyung Han91df2082019-11-20 01:49:42 +09002379 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2380 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2381 actual = flattened.installDir.String()
2382 if actual != expected {
2383 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2384 }
2385 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002386 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002387}
Jiyong Park67882562019-03-21 01:11:21 +09002388
Jooyung Han54aca7b2019-11-20 02:26:02 +09002389func TestFileContexts(t *testing.T) {
2390 ctx, _ := testApex(t, `
2391 apex {
2392 name: "myapex",
2393 key: "myapex.key",
2394 }
2395
2396 apex_key {
2397 name: "myapex.key",
2398 public_key: "testkey.avbpubkey",
2399 private_key: "testkey.pem",
2400 }
2401 `)
2402 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2403 apexRule := module.Rule("apexRule")
2404 actual := apexRule.Args["file_contexts"]
2405 expected := "system/sepolicy/apex/myapex-file_contexts"
2406 if actual != expected {
2407 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2408 }
2409
2410 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2411 apex {
2412 name: "myapex",
2413 key: "myapex.key",
2414 file_contexts: "my_own_file_contexts",
2415 }
2416
2417 apex_key {
2418 name: "myapex.key",
2419 public_key: "testkey.avbpubkey",
2420 private_key: "testkey.pem",
2421 }
2422 `, withFiles(map[string][]byte{
2423 "my_own_file_contexts": nil,
2424 }))
2425
2426 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2427 apex {
2428 name: "myapex",
2429 key: "myapex.key",
2430 product_specific: true,
2431 file_contexts: "product_specific_file_contexts",
2432 }
2433
2434 apex_key {
2435 name: "myapex.key",
2436 public_key: "testkey.avbpubkey",
2437 private_key: "testkey.pem",
2438 }
2439 `)
2440
2441 ctx, _ = testApex(t, `
2442 apex {
2443 name: "myapex",
2444 key: "myapex.key",
2445 product_specific: true,
2446 file_contexts: "product_specific_file_contexts",
2447 }
2448
2449 apex_key {
2450 name: "myapex.key",
2451 public_key: "testkey.avbpubkey",
2452 private_key: "testkey.pem",
2453 }
2454 `, withFiles(map[string][]byte{
2455 "product_specific_file_contexts": nil,
2456 }))
2457 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2458 apexRule = module.Rule("apexRule")
2459 actual = apexRule.Args["file_contexts"]
2460 expected = "product_specific_file_contexts"
2461 if actual != expected {
2462 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2463 }
2464
2465 ctx, _ = testApex(t, `
2466 apex {
2467 name: "myapex",
2468 key: "myapex.key",
2469 product_specific: true,
2470 file_contexts: ":my-file-contexts",
2471 }
2472
2473 apex_key {
2474 name: "myapex.key",
2475 public_key: "testkey.avbpubkey",
2476 private_key: "testkey.pem",
2477 }
2478
2479 filegroup {
2480 name: "my-file-contexts",
2481 srcs: ["product_specific_file_contexts"],
2482 }
2483 `, withFiles(map[string][]byte{
2484 "product_specific_file_contexts": nil,
2485 }))
2486 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2487 apexRule = module.Rule("apexRule")
2488 actual = apexRule.Args["file_contexts"]
2489 expected = "product_specific_file_contexts"
2490 if actual != expected {
2491 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2492 }
2493}
2494
Jiyong Park67882562019-03-21 01:11:21 +09002495func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002496 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002497 apex_key {
2498 name: "myapex.key",
2499 public_key: ":my.avbpubkey",
2500 private_key: ":my.pem",
2501 product_specific: true,
2502 }
2503
2504 filegroup {
2505 name: "my.avbpubkey",
2506 srcs: ["testkey2.avbpubkey"],
2507 }
2508
2509 filegroup {
2510 name: "my.pem",
2511 srcs: ["testkey2.pem"],
2512 }
2513 `)
2514
2515 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2516 expected_pubkey := "testkey2.avbpubkey"
2517 actual_pubkey := apex_key.public_key_file.String()
2518 if actual_pubkey != expected_pubkey {
2519 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2520 }
2521 expected_privkey := "testkey2.pem"
2522 actual_privkey := apex_key.private_key_file.String()
2523 if actual_privkey != expected_privkey {
2524 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2525 }
2526}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002527
2528func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002529 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002530 prebuilt_apex {
2531 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002532 arch: {
2533 arm64: {
2534 src: "myapex-arm64.apex",
2535 },
2536 arm: {
2537 src: "myapex-arm.apex",
2538 },
2539 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002540 }
2541 `)
2542
2543 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2544
Jiyong Parkc95714e2019-03-29 14:23:10 +09002545 expectedInput := "myapex-arm64.apex"
2546 if prebuilt.inputApex.String() != expectedInput {
2547 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2548 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002549}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002550
2551func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002552 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002553 prebuilt_apex {
2554 name: "myapex",
2555 src: "myapex-arm.apex",
2556 filename: "notmyapex.apex",
2557 }
2558 `)
2559
2560 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2561
2562 expected := "notmyapex.apex"
2563 if p.installFilename != expected {
2564 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2565 }
2566}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002567
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002568func TestPrebuiltOverrides(t *testing.T) {
2569 ctx, config := testApex(t, `
2570 prebuilt_apex {
2571 name: "myapex.prebuilt",
2572 src: "myapex-arm.apex",
2573 overrides: [
2574 "myapex",
2575 ],
2576 }
2577 `)
2578
2579 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2580
2581 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002582 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002583 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002584 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002585 }
2586}
2587
Roland Levillain630846d2019-06-26 12:48:34 +01002588func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002589 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002590 apex_test {
2591 name: "myapex",
2592 key: "myapex.key",
2593 tests: [
2594 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002595 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002596 ],
2597 }
2598
2599 apex_key {
2600 name: "myapex.key",
2601 public_key: "testkey.avbpubkey",
2602 private_key: "testkey.pem",
2603 }
2604
2605 cc_test {
2606 name: "mytest",
2607 gtest: false,
2608 srcs: ["mytest.cpp"],
2609 relative_install_path: "test",
2610 system_shared_libs: [],
2611 static_executable: true,
2612 stl: "none",
2613 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002614
2615 cc_test {
2616 name: "mytests",
2617 gtest: false,
2618 srcs: [
2619 "mytest1.cpp",
2620 "mytest2.cpp",
2621 "mytest3.cpp",
2622 ],
2623 test_per_src: true,
2624 relative_install_path: "test",
2625 system_shared_libs: [],
2626 static_executable: true,
2627 stl: "none",
2628 }
Roland Levillain630846d2019-06-26 12:48:34 +01002629 `)
2630
Sundong Ahnabb64432019-10-22 13:58:29 +09002631 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002632 copyCmds := apexRule.Args["copy_commands"]
2633
2634 // Ensure that test dep is copied into apex.
2635 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002636
2637 // Ensure that test deps built with `test_per_src` are copied into apex.
2638 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2639 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2640 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002641
2642 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002643 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002644 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2645 name := apexBundle.BaseModuleName()
2646 prefix := "TARGET_"
2647 var builder strings.Builder
2648 data.Custom(&builder, name, prefix, "", data)
2649 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002650 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2651 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2652 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2653 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002654 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002655 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002656 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002657}
2658
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002659func TestInstallExtraFlattenedApexes(t *testing.T) {
2660 ctx, config := testApex(t, `
2661 apex {
2662 name: "myapex",
2663 key: "myapex.key",
2664 }
2665 apex_key {
2666 name: "myapex.key",
2667 public_key: "testkey.avbpubkey",
2668 private_key: "testkey.pem",
2669 }
2670 `, func(fs map[string][]byte, config android.Config) {
2671 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2672 })
2673 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2674 ensureListContains(t, ab.externalDeps, "myapex.flattened")
2675 mk := android.AndroidMkDataForTest(t, config, "", ab)
2676 var builder strings.Builder
2677 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2678 androidMk := builder.String()
2679 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2680}
2681
Jooyung Han5c998b92019-06-27 11:30:33 +09002682func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002683 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002684 apex {
2685 name: "myapex",
2686 key: "myapex.key",
2687 native_shared_libs: ["mylib"],
2688 uses: ["commonapex"],
2689 }
2690
2691 apex {
2692 name: "commonapex",
2693 key: "myapex.key",
2694 native_shared_libs: ["libcommon"],
2695 provide_cpp_shared_libs: true,
2696 }
2697
2698 apex_key {
2699 name: "myapex.key",
2700 public_key: "testkey.avbpubkey",
2701 private_key: "testkey.pem",
2702 }
2703
2704 cc_library {
2705 name: "mylib",
2706 srcs: ["mylib.cpp"],
2707 shared_libs: ["libcommon"],
2708 system_shared_libs: [],
2709 stl: "none",
2710 }
2711
2712 cc_library {
2713 name: "libcommon",
2714 srcs: ["mylib_common.cpp"],
2715 system_shared_libs: [],
2716 stl: "none",
2717 }
2718 `)
2719
Sundong Ahnabb64432019-10-22 13:58:29 +09002720 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002721 apexRule1 := module1.Rule("apexRule")
2722 copyCmds1 := apexRule1.Args["copy_commands"]
2723
Sundong Ahnabb64432019-10-22 13:58:29 +09002724 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002725 apexRule2 := module2.Rule("apexRule")
2726 copyCmds2 := apexRule2.Args["copy_commands"]
2727
Colin Cross7113d202019-11-20 16:39:12 -08002728 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2729 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002730 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2731 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2732 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2733}
2734
2735func TestApexUsesFailsIfNotProvided(t *testing.T) {
2736 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2737 apex {
2738 name: "myapex",
2739 key: "myapex.key",
2740 uses: ["commonapex"],
2741 }
2742
2743 apex {
2744 name: "commonapex",
2745 key: "myapex.key",
2746 }
2747
2748 apex_key {
2749 name: "myapex.key",
2750 public_key: "testkey.avbpubkey",
2751 private_key: "testkey.pem",
2752 }
2753 `)
2754 testApexError(t, `uses: "commonapex" is not a provider`, `
2755 apex {
2756 name: "myapex",
2757 key: "myapex.key",
2758 uses: ["commonapex"],
2759 }
2760
2761 cc_library {
2762 name: "commonapex",
2763 system_shared_libs: [],
2764 stl: "none",
2765 }
2766
2767 apex_key {
2768 name: "myapex.key",
2769 public_key: "testkey.avbpubkey",
2770 private_key: "testkey.pem",
2771 }
2772 `)
2773}
2774
2775func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2776 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2777 apex {
2778 name: "myapex",
2779 key: "myapex.key",
2780 use_vendor: true,
2781 uses: ["commonapex"],
2782 }
2783
2784 apex {
2785 name: "commonapex",
2786 key: "myapex.key",
2787 provide_cpp_shared_libs: true,
2788 }
2789
2790 apex_key {
2791 name: "myapex.key",
2792 public_key: "testkey.avbpubkey",
2793 private_key: "testkey.pem",
2794 }
Jooyung Handc782442019-11-01 03:14:38 +09002795 `, func(fs map[string][]byte, config android.Config) {
2796 setUseVendorWhitelistForTest(config, []string{"myapex"})
2797 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002798}
2799
Jooyung Hand48f3c32019-08-23 11:18:57 +09002800func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2801 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2802 apex {
2803 name: "myapex",
2804 key: "myapex.key",
2805 native_shared_libs: ["libfoo"],
2806 }
2807
2808 apex_key {
2809 name: "myapex.key",
2810 public_key: "testkey.avbpubkey",
2811 private_key: "testkey.pem",
2812 }
2813
2814 cc_library {
2815 name: "libfoo",
2816 stl: "none",
2817 system_shared_libs: [],
2818 enabled: false,
2819 }
2820 `)
2821 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2822 apex {
2823 name: "myapex",
2824 key: "myapex.key",
2825 java_libs: ["myjar"],
2826 }
2827
2828 apex_key {
2829 name: "myapex.key",
2830 public_key: "testkey.avbpubkey",
2831 private_key: "testkey.pem",
2832 }
2833
2834 java_library {
2835 name: "myjar",
2836 srcs: ["foo/bar/MyClass.java"],
2837 sdk_version: "none",
2838 system_modules: "none",
2839 compile_dex: true,
2840 enabled: false,
2841 }
2842 `)
2843}
2844
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002845func TestApexWithApps(t *testing.T) {
2846 ctx, _ := testApex(t, `
2847 apex {
2848 name: "myapex",
2849 key: "myapex.key",
2850 apps: [
2851 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002852 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002853 ],
2854 }
2855
2856 apex_key {
2857 name: "myapex.key",
2858 public_key: "testkey.avbpubkey",
2859 private_key: "testkey.pem",
2860 }
2861
2862 android_app {
2863 name: "AppFoo",
2864 srcs: ["foo/bar/MyClass.java"],
2865 sdk_version: "none",
2866 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002867 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002868 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002869
2870 android_app {
2871 name: "AppFooPriv",
2872 srcs: ["foo/bar/MyClass.java"],
2873 sdk_version: "none",
2874 system_modules: "none",
2875 privileged: true,
2876 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002877
2878 cc_library_shared {
2879 name: "libjni",
2880 srcs: ["mylib.cpp"],
2881 stl: "none",
2882 system_shared_libs: [],
2883 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002884 `)
2885
Sundong Ahnabb64432019-10-22 13:58:29 +09002886 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002887 apexRule := module.Rule("apexRule")
2888 copyCmds := apexRule.Args["copy_commands"]
2889
2890 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002891 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002892
2893 // JNI libraries are embedded inside APK
2894 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
Colin Cross7113d202019-11-20 16:39:12 -08002895 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002896 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2897 // ... uncompressed
2898 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2899 t.Errorf("jni lib is not uncompressed for AppFoo")
2900 }
2901 // ... and not directly inside the APEX
2902 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002903}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002904
Dario Frenicde2a032019-10-27 00:29:22 +01002905func TestApexWithAppImports(t *testing.T) {
2906 ctx, _ := testApex(t, `
2907 apex {
2908 name: "myapex",
2909 key: "myapex.key",
2910 apps: [
2911 "AppFooPrebuilt",
2912 "AppFooPrivPrebuilt",
2913 ],
2914 }
2915
2916 apex_key {
2917 name: "myapex.key",
2918 public_key: "testkey.avbpubkey",
2919 private_key: "testkey.pem",
2920 }
2921
2922 android_app_import {
2923 name: "AppFooPrebuilt",
2924 apk: "PrebuiltAppFoo.apk",
2925 presigned: true,
2926 dex_preopt: {
2927 enabled: false,
2928 },
2929 }
2930
2931 android_app_import {
2932 name: "AppFooPrivPrebuilt",
2933 apk: "PrebuiltAppFooPriv.apk",
2934 privileged: true,
2935 presigned: true,
2936 dex_preopt: {
2937 enabled: false,
2938 },
2939 }
2940 `)
2941
Sundong Ahnabb64432019-10-22 13:58:29 +09002942 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002943 apexRule := module.Rule("apexRule")
2944 copyCmds := apexRule.Args["copy_commands"]
2945
2946 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2947 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002948}
2949
Jooyung Han18020ea2019-11-13 10:50:48 +09002950func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2951 // libfoo's apex_available comes from cc_defaults
2952 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2953 apex {
2954 name: "myapex",
2955 key: "myapex.key",
2956 native_shared_libs: ["libfoo"],
2957 }
2958
2959 apex_key {
2960 name: "myapex.key",
2961 public_key: "testkey.avbpubkey",
2962 private_key: "testkey.pem",
2963 }
2964
2965 apex {
2966 name: "otherapex",
2967 key: "myapex.key",
2968 native_shared_libs: ["libfoo"],
2969 }
2970
2971 cc_defaults {
2972 name: "libfoo-defaults",
2973 apex_available: ["otherapex"],
2974 }
2975
2976 cc_library {
2977 name: "libfoo",
2978 defaults: ["libfoo-defaults"],
2979 stl: "none",
2980 system_shared_libs: [],
2981 }`)
2982}
2983
Jiyong Park127b40b2019-09-30 16:04:35 +09002984func TestApexAvailable(t *testing.T) {
2985 // libfoo is not available to myapex, but only to otherapex
2986 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2987 apex {
2988 name: "myapex",
2989 key: "myapex.key",
2990 native_shared_libs: ["libfoo"],
2991 }
2992
2993 apex_key {
2994 name: "myapex.key",
2995 public_key: "testkey.avbpubkey",
2996 private_key: "testkey.pem",
2997 }
2998
2999 apex {
3000 name: "otherapex",
3001 key: "otherapex.key",
3002 native_shared_libs: ["libfoo"],
3003 }
3004
3005 apex_key {
3006 name: "otherapex.key",
3007 public_key: "testkey.avbpubkey",
3008 private_key: "testkey.pem",
3009 }
3010
3011 cc_library {
3012 name: "libfoo",
3013 stl: "none",
3014 system_shared_libs: [],
3015 apex_available: ["otherapex"],
3016 }`)
3017
3018 // libbar is an indirect dep
3019 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3020 apex {
3021 name: "myapex",
3022 key: "myapex.key",
3023 native_shared_libs: ["libfoo"],
3024 }
3025
3026 apex_key {
3027 name: "myapex.key",
3028 public_key: "testkey.avbpubkey",
3029 private_key: "testkey.pem",
3030 }
3031
3032 apex {
3033 name: "otherapex",
3034 key: "otherapex.key",
3035 native_shared_libs: ["libfoo"],
3036 }
3037
3038 apex_key {
3039 name: "otherapex.key",
3040 public_key: "testkey.avbpubkey",
3041 private_key: "testkey.pem",
3042 }
3043
3044 cc_library {
3045 name: "libfoo",
3046 stl: "none",
3047 shared_libs: ["libbar"],
3048 system_shared_libs: [],
3049 apex_available: ["myapex", "otherapex"],
3050 }
3051
3052 cc_library {
3053 name: "libbar",
3054 stl: "none",
3055 system_shared_libs: [],
3056 apex_available: ["otherapex"],
3057 }`)
3058
3059 testApexError(t, "\"otherapex\" is not a valid module name", `
3060 apex {
3061 name: "myapex",
3062 key: "myapex.key",
3063 native_shared_libs: ["libfoo"],
3064 }
3065
3066 apex_key {
3067 name: "myapex.key",
3068 public_key: "testkey.avbpubkey",
3069 private_key: "testkey.pem",
3070 }
3071
3072 cc_library {
3073 name: "libfoo",
3074 stl: "none",
3075 system_shared_libs: [],
3076 apex_available: ["otherapex"],
3077 }`)
3078
3079 ctx, _ := testApex(t, `
3080 apex {
3081 name: "myapex",
3082 key: "myapex.key",
3083 native_shared_libs: ["libfoo", "libbar"],
3084 }
3085
3086 apex_key {
3087 name: "myapex.key",
3088 public_key: "testkey.avbpubkey",
3089 private_key: "testkey.pem",
3090 }
3091
3092 cc_library {
3093 name: "libfoo",
3094 stl: "none",
3095 system_shared_libs: [],
3096 apex_available: ["myapex"],
3097 }
3098
3099 cc_library {
3100 name: "libbar",
3101 stl: "none",
3102 system_shared_libs: [],
3103 apex_available: ["//apex_available:anyapex"],
3104 }`)
3105
3106 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003107 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3108 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3109 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3110 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003111
3112 ctx, _ = testApex(t, `
3113 apex {
3114 name: "myapex",
3115 key: "myapex.key",
3116 }
3117
3118 apex_key {
3119 name: "myapex.key",
3120 public_key: "testkey.avbpubkey",
3121 private_key: "testkey.pem",
3122 }
3123
3124 cc_library {
3125 name: "libfoo",
3126 stl: "none",
3127 system_shared_libs: [],
3128 apex_available: ["//apex_available:platform"],
3129 }`)
3130
3131 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003132 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3133 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003134
3135 ctx, _ = testApex(t, `
3136 apex {
3137 name: "myapex",
3138 key: "myapex.key",
3139 native_shared_libs: ["libfoo"],
3140 }
3141
3142 apex_key {
3143 name: "myapex.key",
3144 public_key: "testkey.avbpubkey",
3145 private_key: "testkey.pem",
3146 }
3147
3148 cc_library {
3149 name: "libfoo",
3150 stl: "none",
3151 system_shared_libs: [],
3152 apex_available: ["myapex"],
3153 static: {
3154 apex_available: ["//apex_available:platform"],
3155 },
3156 }`)
3157
3158 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08003159 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3160 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003161 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08003162 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3163 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003164}
3165
Jiyong Park5d790c32019-11-15 18:40:32 +09003166func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003167 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003168 apex {
3169 name: "myapex",
3170 key: "myapex.key",
3171 apps: ["app"],
3172 }
3173
3174 override_apex {
3175 name: "override_myapex",
3176 base: "myapex",
3177 apps: ["override_app"],
3178 }
3179
3180 apex_key {
3181 name: "myapex.key",
3182 public_key: "testkey.avbpubkey",
3183 private_key: "testkey.pem",
3184 }
3185
3186 android_app {
3187 name: "app",
3188 srcs: ["foo/bar/MyClass.java"],
3189 package_name: "foo",
3190 sdk_version: "none",
3191 system_modules: "none",
3192 }
3193
3194 override_android_app {
3195 name: "override_app",
3196 base: "app",
3197 package_name: "bar",
3198 }
3199 `)
3200
Jiyong Park317645e2019-12-05 13:20:58 +09003201 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3202 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3203 if originalVariant.GetOverriddenBy() != "" {
3204 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3205 }
3206 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3207 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3208 }
3209
Jiyong Park5d790c32019-11-15 18:40:32 +09003210 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3211 apexRule := module.Rule("apexRule")
3212 copyCmds := apexRule.Args["copy_commands"]
3213
3214 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3215 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003216
3217 apexBundle := module.Module().(*apexBundle)
3218 name := apexBundle.Name()
3219 if name != "override_myapex" {
3220 t.Errorf("name should be \"override_myapex\", but was %q", name)
3221 }
3222
3223 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3224 var builder strings.Builder
3225 data.Custom(&builder, name, "TARGET_", "", data)
3226 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003227 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003228 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3229 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
3230 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003231 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003232 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3233 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003234}
3235
Jooyung Han214bf372019-11-12 13:03:50 +09003236func TestLegacyAndroid10Support(t *testing.T) {
3237 ctx, _ := testApex(t, `
3238 apex {
3239 name: "myapex",
3240 key: "myapex.key",
3241 legacy_android10_support: true,
3242 }
3243
3244 apex_key {
3245 name: "myapex.key",
3246 public_key: "testkey.avbpubkey",
3247 private_key: "testkey.pem",
3248 }
3249 `)
3250
3251 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3252 args := module.Rule("apexRule").Args
3253 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
3254}
3255
Jiyong Park479321d2019-12-16 11:47:12 +09003256func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3257 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3258 apex {
3259 name: "myapex",
3260 key: "myapex.key",
3261 java_libs: ["myjar"],
3262 }
3263
3264 apex_key {
3265 name: "myapex.key",
3266 public_key: "testkey.avbpubkey",
3267 private_key: "testkey.pem",
3268 }
3269
3270 java_library {
3271 name: "myjar",
3272 srcs: ["foo/bar/MyClass.java"],
3273 sdk_version: "none",
3274 system_modules: "none",
3275 }
3276 `)
3277}
3278
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003279func TestMain(m *testing.M) {
3280 run := func() int {
3281 setUp()
3282 defer tearDown()
3283
3284 return m.Run()
3285 }
3286
3287 os.Exit(run())
3288}