blob: 035a553f9b48a95984322bc3e5c58d46e220e322 [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)
Paul Duffin59986b22019-12-19 14:38:36 +0000292 cc.RegisterPrebuiltBuildComponents(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800293 ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
294 ctx.RegisterModuleType("cc_object", cc.ObjectFactory)
295 ctx.RegisterModuleType("cc_defaults", func() android.Module {
296 return cc.DefaultsFactory()
297 })
298 ctx.RegisterModuleType("cc_test", cc.TestFactory)
299 ctx.RegisterModuleType("llndk_library", cc.LlndkLibraryFactory)
300 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
301 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
302 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
303 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
304 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800305 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000306 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000307 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000308 java.RegisterAppBuildComponents(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800309
310 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
311 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
312 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
313 })
314 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
315 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
316 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
317 ctx.BottomUp("test_per_src", cc.TestPerSrcMutator).Parallel()
318 ctx.BottomUp("version", cc.VersionMutator).Parallel()
319 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
320 })
321 ctx.PreDepsMutators(RegisterPreDepsMutators)
322 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
323 ctx.PostDepsMutators(RegisterPostDepsMutators)
324 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
325 ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel()
326 ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel()
327 })
328
329 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330
Jooyung Han5c998b92019-06-27 11:30:33 +0900331 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900332}
333
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700334func setUp() {
335 var err error
336 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900337 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700338 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900339 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900340}
341
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700342func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900343 os.RemoveAll(buildDir)
344}
345
346// ensure that 'result' contains 'expected'
347func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900348 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900349 if !strings.Contains(result, expected) {
350 t.Errorf("%q is not found in %q", expected, result)
351 }
352}
353
354// ensures that 'result' does not contain 'notExpected'
355func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900356 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900357 if strings.Contains(result, notExpected) {
358 t.Errorf("%q is found in %q", notExpected, result)
359 }
360}
361
362func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900363 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900364 if !android.InList(expected, result) {
365 t.Errorf("%q is not found in %v", expected, result)
366 }
367}
368
369func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900370 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900371 if android.InList(notExpected, result) {
372 t.Errorf("%q is found in %v", notExpected, result)
373 }
374}
375
Jooyung Hane1633032019-08-01 17:41:43 +0900376func ensureListEmpty(t *testing.T, result []string) {
377 t.Helper()
378 if len(result) > 0 {
379 t.Errorf("%q is expected to be empty", result)
380 }
381}
382
Jiyong Park25fc6a92018-11-18 18:02:45 +0900383// Minimal test
384func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700385 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900386 apex_defaults {
387 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900388 manifest: ":myapex.manifest",
389 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900390 key: "myapex.key",
391 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800392 multilib: {
393 both: {
394 binaries: ["foo",],
395 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900396 },
Jiyong Park9e6c2422019-08-09 20:39:45 +0900397 java_libs: ["myjar", "myprebuiltjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900398 }
399
Jiyong Park30ca9372019-02-07 16:27:23 +0900400 apex {
401 name: "myapex",
402 defaults: ["myapex-defaults"],
403 }
404
Jiyong Park25fc6a92018-11-18 18:02:45 +0900405 apex_key {
406 name: "myapex.key",
407 public_key: "testkey.avbpubkey",
408 private_key: "testkey.pem",
409 }
410
Jiyong Park809bb722019-02-13 21:33:49 +0900411 filegroup {
412 name: "myapex.manifest",
413 srcs: ["apex_manifest.json"],
414 }
415
416 filegroup {
417 name: "myapex.androidmanifest",
418 srcs: ["AndroidManifest.xml"],
419 }
420
Jiyong Park25fc6a92018-11-18 18:02:45 +0900421 cc_library {
422 name: "mylib",
423 srcs: ["mylib.cpp"],
424 shared_libs: ["mylib2"],
425 system_shared_libs: [],
426 stl: "none",
427 }
428
Alex Light3d673592019-01-18 14:37:31 -0800429 cc_binary {
430 name: "foo",
431 srcs: ["mylib.cpp"],
432 compile_multilib: "both",
433 multilib: {
434 lib32: {
435 suffix: "32",
436 },
437 lib64: {
438 suffix: "64",
439 },
440 },
441 symlinks: ["foo_link_"],
442 symlink_preferred_arch: true,
443 system_shared_libs: [],
444 static_executable: true,
445 stl: "none",
446 }
447
Jiyong Park25fc6a92018-11-18 18:02:45 +0900448 cc_library {
449 name: "mylib2",
450 srcs: ["mylib.cpp"],
451 system_shared_libs: [],
452 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900453 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900454 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900455
456 java_library {
457 name: "myjar",
458 srcs: ["foo/bar/MyClass.java"],
459 sdk_version: "none",
460 system_modules: "none",
461 compile_dex: true,
462 static_libs: ["myotherjar"],
463 }
464
465 java_library {
466 name: "myotherjar",
467 srcs: ["foo/bar/MyClass.java"],
468 sdk_version: "none",
469 system_modules: "none",
470 compile_dex: true,
471 }
Jiyong Park9e6c2422019-08-09 20:39:45 +0900472
473 java_import {
474 name: "myprebuiltjar",
475 jars: ["prebuilt.jar"],
476 installable: true,
477 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900478 `)
479
Sundong Ahnabb64432019-10-22 13:58:29 +0900480 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900481
482 optFlags := apexRule.Args["opt_flags"]
483 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700484 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900485 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900486
Jiyong Park25fc6a92018-11-18 18:02:45 +0900487 copyCmds := apexRule.Args["copy_commands"]
488
489 // Ensure that main rule creates an output
490 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
491
492 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800493 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900494 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900495 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900496
497 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800498 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900499 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900500
501 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800502 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
503 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900504 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900505 ensureContains(t, copyCmds, "image.apex/javalib/myprebuiltjar.jar")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900506 // .. but not for java libs
507 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800508
Colin Cross7113d202019-11-20 16:39:12 -0800509 // Ensure that the platform variant ends with _shared or _common
510 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
511 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900512 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
513 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900514 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common")
Alex Light3d673592019-01-18 14:37:31 -0800515
516 // Ensure that all symlinks are present.
517 found_foo_link_64 := false
518 found_foo := false
519 for _, cmd := range strings.Split(copyCmds, " && ") {
520 if strings.HasPrefix(cmd, "ln -s foo64") {
521 if strings.HasSuffix(cmd, "bin/foo") {
522 found_foo = true
523 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
524 found_foo_link_64 = true
525 }
526 }
527 }
528 good := found_foo && found_foo_link_64
529 if !good {
530 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
531 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900532
Sundong Ahnabb64432019-10-22 13:58:29 +0900533 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700534 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700535 if len(noticeInputs) != 2 {
536 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900537 }
538 ensureListContains(t, noticeInputs, "NOTICE")
539 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800540}
541
Jooyung Hanf21c7972019-12-16 22:32:06 +0900542func TestDefaults(t *testing.T) {
543 ctx, _ := testApex(t, `
544 apex_defaults {
545 name: "myapex-defaults",
546 key: "myapex.key",
547 prebuilts: ["myetc"],
548 native_shared_libs: ["mylib"],
549 java_libs: ["myjar"],
550 apps: ["AppFoo"],
551 }
552
553 prebuilt_etc {
554 name: "myetc",
555 src: "myprebuilt",
556 }
557
558 apex {
559 name: "myapex",
560 defaults: ["myapex-defaults"],
561 }
562
563 apex_key {
564 name: "myapex.key",
565 public_key: "testkey.avbpubkey",
566 private_key: "testkey.pem",
567 }
568
569 cc_library {
570 name: "mylib",
571 system_shared_libs: [],
572 stl: "none",
573 }
574
575 java_library {
576 name: "myjar",
577 srcs: ["foo/bar/MyClass.java"],
578 sdk_version: "none",
579 system_modules: "none",
580 compile_dex: true,
581 }
582
583 android_app {
584 name: "AppFoo",
585 srcs: ["foo/bar/MyClass.java"],
586 sdk_version: "none",
587 system_modules: "none",
588 }
589 `)
590 ensureExactContents(t, ctx, "myapex", []string{
591 "etc/myetc",
592 "javalib/myjar.jar",
593 "lib64/mylib.so",
594 "app/AppFoo/AppFoo.apk",
595 })
596}
597
Jooyung Han01a3ee22019-11-02 02:52:25 +0900598func TestApexManifest(t *testing.T) {
599 ctx, _ := testApex(t, `
600 apex {
601 name: "myapex",
602 key: "myapex.key",
603 }
604
605 apex_key {
606 name: "myapex.key",
607 public_key: "testkey.avbpubkey",
608 private_key: "testkey.pem",
609 }
610 `)
611
612 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900613 args := module.Rule("apexRule").Args
614 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
615 t.Error("manifest should be apex_manifest.pb, but " + manifest)
616 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900617}
618
Alex Light5098a612018-11-29 17:12:15 -0800619func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700620 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800621 apex {
622 name: "myapex",
623 key: "myapex.key",
624 payload_type: "zip",
625 native_shared_libs: ["mylib"],
626 }
627
628 apex_key {
629 name: "myapex.key",
630 public_key: "testkey.avbpubkey",
631 private_key: "testkey.pem",
632 }
633
634 cc_library {
635 name: "mylib",
636 srcs: ["mylib.cpp"],
637 shared_libs: ["mylib2"],
638 system_shared_libs: [],
639 stl: "none",
640 }
641
642 cc_library {
643 name: "mylib2",
644 srcs: ["mylib.cpp"],
645 system_shared_libs: [],
646 stl: "none",
647 }
648 `)
649
Sundong Ahnabb64432019-10-22 13:58:29 +0900650 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800651 copyCmds := zipApexRule.Args["copy_commands"]
652
653 // Ensure that main rule creates an output
654 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
655
656 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800657 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800658
659 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800660 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800661
662 // Ensure that both direct and indirect deps are copied into apex
663 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
664 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900665}
666
667func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700668 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900669 apex {
670 name: "myapex",
671 key: "myapex.key",
672 native_shared_libs: ["mylib", "mylib3"],
673 }
674
675 apex_key {
676 name: "myapex.key",
677 public_key: "testkey.avbpubkey",
678 private_key: "testkey.pem",
679 }
680
681 cc_library {
682 name: "mylib",
683 srcs: ["mylib.cpp"],
684 shared_libs: ["mylib2", "mylib3"],
685 system_shared_libs: [],
686 stl: "none",
687 }
688
689 cc_library {
690 name: "mylib2",
691 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900692 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900693 system_shared_libs: [],
694 stl: "none",
695 stubs: {
696 versions: ["1", "2", "3"],
697 },
698 }
699
700 cc_library {
701 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900702 srcs: ["mylib.cpp"],
703 shared_libs: ["mylib4"],
704 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900705 stl: "none",
706 stubs: {
707 versions: ["10", "11", "12"],
708 },
709 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900710
711 cc_library {
712 name: "mylib4",
713 srcs: ["mylib.cpp"],
714 system_shared_libs: [],
715 stl: "none",
716 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900717 `)
718
Sundong Ahnabb64432019-10-22 13:58:29 +0900719 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900720 copyCmds := apexRule.Args["copy_commands"]
721
722 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800723 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900724
725 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800726 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900727
728 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800729 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900730
Colin Cross7113d202019-11-20 16:39:12 -0800731 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900732
733 // Ensure that mylib is linking with the latest version of stubs for mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800734 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900735 // ... and not linking to the non-stub (impl) variant of mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800736 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900737
738 // 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 -0800739 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900740 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800741 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900742
743 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800744 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900745 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900746
747 // Ensure that genstub is invoked with --apex
Colin Cross7113d202019-11-20 16:39:12 -0800748 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3_myapex").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900749
750 ensureExactContents(t, ctx, "myapex", []string{
751 "lib64/mylib.so",
752 "lib64/mylib3.so",
753 "lib64/mylib4.so",
754 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900755}
756
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900757func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700758 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900759 apex {
760 name: "myapex",
761 key: "myapex.key",
762 native_shared_libs: ["mylib"],
763 }
764
765 apex_key {
766 name: "myapex.key",
767 public_key: "testkey.avbpubkey",
768 private_key: "testkey.pem",
769 }
770
771 cc_library {
772 name: "mylib",
773 srcs: ["mylib.cpp"],
774 shared_libs: ["libfoo#10"],
775 system_shared_libs: [],
776 stl: "none",
777 }
778
779 cc_library {
780 name: "libfoo",
781 srcs: ["mylib.cpp"],
782 shared_libs: ["libbar"],
783 system_shared_libs: [],
784 stl: "none",
785 stubs: {
786 versions: ["10", "20", "30"],
787 },
788 }
789
790 cc_library {
791 name: "libbar",
792 srcs: ["mylib.cpp"],
793 system_shared_libs: [],
794 stl: "none",
795 }
796
797 `)
798
Sundong Ahnabb64432019-10-22 13:58:29 +0900799 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900800 copyCmds := apexRule.Args["copy_commands"]
801
802 // Ensure that direct non-stubs dep is always included
803 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
804
805 // Ensure that indirect stubs dep is not included
806 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
807
808 // Ensure that dependency of stubs is not included
809 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
810
Colin Cross7113d202019-11-20 16:39:12 -0800811 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900812
813 // Ensure that mylib is linking with version 10 of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800814 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900815 // ... and not linking to the non-stub (impl) variant of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800816 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900817
Colin Cross7113d202019-11-20 16:39:12 -0800818 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900819
820 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
821 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
822}
823
Jooyung Hand3639552019-08-09 12:57:43 +0900824func TestApexWithRuntimeLibsDependency(t *testing.T) {
825 /*
826 myapex
827 |
828 v (runtime_libs)
829 mylib ------+------> libfoo [provides stub]
830 |
831 `------> libbar
832 */
833 ctx, _ := testApex(t, `
834 apex {
835 name: "myapex",
836 key: "myapex.key",
837 native_shared_libs: ["mylib"],
838 }
839
840 apex_key {
841 name: "myapex.key",
842 public_key: "testkey.avbpubkey",
843 private_key: "testkey.pem",
844 }
845
846 cc_library {
847 name: "mylib",
848 srcs: ["mylib.cpp"],
849 runtime_libs: ["libfoo", "libbar"],
850 system_shared_libs: [],
851 stl: "none",
852 }
853
854 cc_library {
855 name: "libfoo",
856 srcs: ["mylib.cpp"],
857 system_shared_libs: [],
858 stl: "none",
859 stubs: {
860 versions: ["10", "20", "30"],
861 },
862 }
863
864 cc_library {
865 name: "libbar",
866 srcs: ["mylib.cpp"],
867 system_shared_libs: [],
868 stl: "none",
869 }
870
871 `)
872
Sundong Ahnabb64432019-10-22 13:58:29 +0900873 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900874 copyCmds := apexRule.Args["copy_commands"]
875
876 // Ensure that direct non-stubs dep is always included
877 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
878
879 // Ensure that indirect stubs dep is not included
880 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
881
882 // Ensure that runtime_libs dep in included
883 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
884
Sundong Ahnabb64432019-10-22 13:58:29 +0900885 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900886 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
887 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900888
889}
890
Jooyung Han9c80bae2019-08-20 17:30:57 +0900891func TestApexDependencyToLLNDK(t *testing.T) {
892 ctx, _ := testApex(t, `
893 apex {
894 name: "myapex",
895 key: "myapex.key",
896 use_vendor: true,
897 native_shared_libs: ["mylib"],
898 }
899
900 apex_key {
901 name: "myapex.key",
902 public_key: "testkey.avbpubkey",
903 private_key: "testkey.pem",
904 }
905
906 cc_library {
907 name: "mylib",
908 srcs: ["mylib.cpp"],
909 vendor_available: true,
910 shared_libs: ["libbar"],
911 system_shared_libs: [],
912 stl: "none",
913 }
914
915 cc_library {
916 name: "libbar",
917 srcs: ["mylib.cpp"],
918 system_shared_libs: [],
919 stl: "none",
920 }
921
922 llndk_library {
923 name: "libbar",
924 symbol_file: "",
925 }
Jooyung Handc782442019-11-01 03:14:38 +0900926 `, func(fs map[string][]byte, config android.Config) {
927 setUseVendorWhitelistForTest(config, []string{"myapex"})
928 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900929
Sundong Ahnabb64432019-10-22 13:58:29 +0900930 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900931 copyCmds := apexRule.Args["copy_commands"]
932
933 // Ensure that LLNDK dep is not included
934 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
935
Sundong Ahnabb64432019-10-22 13:58:29 +0900936 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900937 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900938
939 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900940 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900941
942}
943
Jiyong Park25fc6a92018-11-18 18:02:45 +0900944func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700945 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900946 apex {
947 name: "myapex",
948 key: "myapex.key",
949 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
950 }
951
952 apex_key {
953 name: "myapex.key",
954 public_key: "testkey.avbpubkey",
955 private_key: "testkey.pem",
956 }
957
958 cc_library {
959 name: "mylib",
960 srcs: ["mylib.cpp"],
961 shared_libs: ["libdl#27"],
962 stl: "none",
963 }
964
965 cc_library_shared {
966 name: "mylib_shared",
967 srcs: ["mylib.cpp"],
968 shared_libs: ["libdl#27"],
969 stl: "none",
970 }
971
972 cc_library {
973 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700974 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900975 nocrt: true,
976 system_shared_libs: [],
977 stl: "none",
978 stubs: {
979 versions: ["27", "28", "29"],
980 },
981 }
982
983 cc_library {
984 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700985 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900986 nocrt: true,
987 system_shared_libs: [],
988 stl: "none",
989 stubs: {
990 versions: ["27", "28", "29"],
991 },
992 }
993
994 cc_library {
995 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700996 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900997 nocrt: true,
998 system_shared_libs: [],
999 stl: "none",
1000 stubs: {
1001 versions: ["27", "28", "29"],
1002 },
1003 }
Jiyong Parkb0788572018-12-20 22:10:17 +09001004
1005 cc_library {
1006 name: "libBootstrap",
1007 srcs: ["mylib.cpp"],
1008 stl: "none",
1009 bootstrap: true,
1010 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001011 `)
1012
Sundong Ahnabb64432019-10-22 13:58:29 +09001013 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001014 copyCmds := apexRule.Args["copy_commands"]
1015
1016 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001017 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001018 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1019 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001020
1021 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001022 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001023
Colin Cross7113d202019-11-20 16:39:12 -08001024 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1025 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1026 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001027
1028 // For dependency to libc
1029 // Ensure that mylib is linking with the latest version of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001030 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001031 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001032 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001033 // ... Cflags from stub is correctly exported to mylib
1034 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1035 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1036
1037 // For dependency to libm
1038 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001039 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001040 // ... and not linking to the stub variant
Colin Cross7113d202019-11-20 16:39:12 -08001041 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001042 // ... and is not compiling with the stub
1043 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1044 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1045
1046 // For dependency to libdl
1047 // Ensure that mylib is linking with the specified version of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001048 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001049 // ... and not linking to the other versions of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001050 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28_myapex/libdl.so")
1051 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001052 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001053 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001054 // ... Cflags from stub is correctly exported to mylib
1055 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1056 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001057
1058 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001059 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1060 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1061 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1062 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001063}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001064
1065func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001066 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001067 apex {
1068 name: "myapex",
1069 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001070 native_shared_libs: ["mylib"],
1071 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001072 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001073 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001074 }
1075
1076 apex_key {
1077 name: "myapex.key",
1078 public_key: "testkey.avbpubkey",
1079 private_key: "testkey.pem",
1080 }
1081
1082 prebuilt_etc {
1083 name: "myetc",
1084 src: "myprebuilt",
1085 sub_dir: "foo/bar",
1086 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001087
1088 cc_library {
1089 name: "mylib",
1090 srcs: ["mylib.cpp"],
1091 relative_install_path: "foo/bar",
1092 system_shared_libs: [],
1093 stl: "none",
1094 }
1095
1096 cc_binary {
1097 name: "mybin",
1098 srcs: ["mylib.cpp"],
1099 relative_install_path: "foo/bar",
1100 system_shared_libs: [],
1101 static_executable: true,
1102 stl: "none",
1103 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001104 `)
1105
Sundong Ahnabb64432019-10-22 13:58:29 +09001106 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001107 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1108
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001109 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001110 ensureListContains(t, dirs, "etc")
1111 ensureListContains(t, dirs, "etc/foo")
1112 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001113 ensureListContains(t, dirs, "lib64")
1114 ensureListContains(t, dirs, "lib64/foo")
1115 ensureListContains(t, dirs, "lib64/foo/bar")
1116 ensureListContains(t, dirs, "lib")
1117 ensureListContains(t, dirs, "lib/foo")
1118 ensureListContains(t, dirs, "lib/foo/bar")
1119
Jiyong Parkbd13e442019-03-15 18:10:35 +09001120 ensureListContains(t, dirs, "bin")
1121 ensureListContains(t, dirs, "bin/foo")
1122 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001123}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001124
1125func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001126 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001127 apex {
1128 name: "myapex",
1129 key: "myapex.key",
1130 native_shared_libs: ["mylib"],
1131 use_vendor: true,
1132 }
1133
1134 apex_key {
1135 name: "myapex.key",
1136 public_key: "testkey.avbpubkey",
1137 private_key: "testkey.pem",
1138 }
1139
1140 cc_library {
1141 name: "mylib",
1142 srcs: ["mylib.cpp"],
1143 shared_libs: ["mylib2"],
1144 system_shared_libs: [],
1145 vendor_available: true,
1146 stl: "none",
1147 }
1148
1149 cc_library {
1150 name: "mylib2",
1151 srcs: ["mylib.cpp"],
1152 system_shared_libs: [],
1153 vendor_available: true,
1154 stl: "none",
1155 }
Jooyung Handc782442019-11-01 03:14:38 +09001156 `, func(fs map[string][]byte, config android.Config) {
1157 setUseVendorWhitelistForTest(config, []string{"myapex"})
1158 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001159
1160 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001161 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001162 for _, implicit := range i.Implicits {
1163 inputsList = append(inputsList, implicit.String())
1164 }
1165 }
1166 inputsString := strings.Join(inputsList, " ")
1167
1168 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001169 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1170 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001171
1172 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001173 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1174 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001175}
Jiyong Park16e91a02018-12-20 18:18:08 +09001176
Jooyung Handc782442019-11-01 03:14:38 +09001177func TestUseVendorRestriction(t *testing.T) {
1178 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1179 apex {
1180 name: "myapex",
1181 key: "myapex.key",
1182 use_vendor: true,
1183 }
1184 apex_key {
1185 name: "myapex.key",
1186 public_key: "testkey.avbpubkey",
1187 private_key: "testkey.pem",
1188 }
1189 `, func(fs map[string][]byte, config android.Config) {
1190 setUseVendorWhitelistForTest(config, []string{""})
1191 })
1192 // no error with whitelist
1193 testApex(t, `
1194 apex {
1195 name: "myapex",
1196 key: "myapex.key",
1197 use_vendor: true,
1198 }
1199 apex_key {
1200 name: "myapex.key",
1201 public_key: "testkey.avbpubkey",
1202 private_key: "testkey.pem",
1203 }
1204 `, func(fs map[string][]byte, config android.Config) {
1205 setUseVendorWhitelistForTest(config, []string{"myapex"})
1206 })
1207}
1208
Jooyung Han5c998b92019-06-27 11:30:33 +09001209func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1210 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1211 apex {
1212 name: "myapex",
1213 key: "myapex.key",
1214 native_shared_libs: ["mylib"],
1215 use_vendor: true,
1216 }
1217
1218 apex_key {
1219 name: "myapex.key",
1220 public_key: "testkey.avbpubkey",
1221 private_key: "testkey.pem",
1222 }
1223
1224 cc_library {
1225 name: "mylib",
1226 srcs: ["mylib.cpp"],
1227 system_shared_libs: [],
1228 stl: "none",
1229 }
1230 `)
1231}
1232
Jiyong Park16e91a02018-12-20 18:18:08 +09001233func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001234 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001235 apex {
1236 name: "myapex",
1237 key: "myapex.key",
1238 native_shared_libs: ["mylib"],
1239 }
1240
1241 apex_key {
1242 name: "myapex.key",
1243 public_key: "testkey.avbpubkey",
1244 private_key: "testkey.pem",
1245 }
1246
1247 cc_library {
1248 name: "mylib",
1249 srcs: ["mylib.cpp"],
1250 system_shared_libs: [],
1251 stl: "none",
1252 stubs: {
1253 versions: ["1", "2", "3"],
1254 },
1255 }
1256
1257 cc_binary {
1258 name: "not_in_apex",
1259 srcs: ["mylib.cpp"],
1260 static_libs: ["mylib"],
1261 static_executable: true,
1262 system_shared_libs: [],
1263 stl: "none",
1264 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001265 `)
1266
Colin Cross7113d202019-11-20 16:39:12 -08001267 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001268
1269 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001270 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001271}
Jiyong Park9335a262018-12-24 11:31:58 +09001272
1273func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001274 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001275 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001276 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001277 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001278 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001279 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001280 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001281 }
1282
1283 cc_library {
1284 name: "mylib",
1285 srcs: ["mylib.cpp"],
1286 system_shared_libs: [],
1287 stl: "none",
1288 }
1289
1290 apex_key {
1291 name: "myapex.key",
1292 public_key: "testkey.avbpubkey",
1293 private_key: "testkey.pem",
1294 }
1295
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001296 android_app_certificate {
1297 name: "myapex.certificate",
1298 certificate: "testkey",
1299 }
1300
1301 android_app_certificate {
1302 name: "myapex.certificate.override",
1303 certificate: "testkey.override",
1304 }
1305
Jiyong Park9335a262018-12-24 11:31:58 +09001306 `)
1307
1308 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001309 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001310
1311 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1312 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1313 "vendor/foo/devkeys/testkey.avbpubkey")
1314 }
1315 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1316 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1317 "vendor/foo/devkeys/testkey.pem")
1318 }
1319
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001320 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001321 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001322 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001323 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001324 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001325 }
1326}
Jiyong Park58e364a2019-01-19 19:24:06 +09001327
Jooyung Hanf121a652019-12-17 14:30:11 +09001328func TestCertificate(t *testing.T) {
1329 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1330 ctx, _ := testApex(t, `
1331 apex {
1332 name: "myapex",
1333 key: "myapex.key",
1334 }
1335 apex_key {
1336 name: "myapex.key",
1337 public_key: "testkey.avbpubkey",
1338 private_key: "testkey.pem",
1339 }`)
1340 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1341 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1342 if actual := rule.Args["certificates"]; actual != expected {
1343 t.Errorf("certificates should be %q, not %q", expected, actual)
1344 }
1345 })
1346 t.Run("override when unspecified", func(t *testing.T) {
1347 ctx, _ := testApex(t, `
1348 apex {
1349 name: "myapex_keytest",
1350 key: "myapex.key",
1351 file_contexts: ":myapex-file_contexts",
1352 }
1353 apex_key {
1354 name: "myapex.key",
1355 public_key: "testkey.avbpubkey",
1356 private_key: "testkey.pem",
1357 }
1358 android_app_certificate {
1359 name: "myapex.certificate.override",
1360 certificate: "testkey.override",
1361 }`)
1362 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1363 expected := "testkey.override.x509.pem testkey.override.pk8"
1364 if actual := rule.Args["certificates"]; actual != expected {
1365 t.Errorf("certificates should be %q, not %q", expected, actual)
1366 }
1367 })
1368 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1369 ctx, _ := testApex(t, `
1370 apex {
1371 name: "myapex",
1372 key: "myapex.key",
1373 certificate: ":myapex.certificate",
1374 }
1375 apex_key {
1376 name: "myapex.key",
1377 public_key: "testkey.avbpubkey",
1378 private_key: "testkey.pem",
1379 }
1380 android_app_certificate {
1381 name: "myapex.certificate",
1382 certificate: "testkey",
1383 }`)
1384 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1385 expected := "testkey.x509.pem testkey.pk8"
1386 if actual := rule.Args["certificates"]; actual != expected {
1387 t.Errorf("certificates should be %q, not %q", expected, actual)
1388 }
1389 })
1390 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1391 ctx, _ := testApex(t, `
1392 apex {
1393 name: "myapex_keytest",
1394 key: "myapex.key",
1395 file_contexts: ":myapex-file_contexts",
1396 certificate: ":myapex.certificate",
1397 }
1398 apex_key {
1399 name: "myapex.key",
1400 public_key: "testkey.avbpubkey",
1401 private_key: "testkey.pem",
1402 }
1403 android_app_certificate {
1404 name: "myapex.certificate.override",
1405 certificate: "testkey.override",
1406 }`)
1407 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1408 expected := "testkey.override.x509.pem testkey.override.pk8"
1409 if actual := rule.Args["certificates"]; actual != expected {
1410 t.Errorf("certificates should be %q, not %q", expected, actual)
1411 }
1412 })
1413 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1414 ctx, _ := testApex(t, `
1415 apex {
1416 name: "myapex",
1417 key: "myapex.key",
1418 certificate: "testkey",
1419 }
1420 apex_key {
1421 name: "myapex.key",
1422 public_key: "testkey.avbpubkey",
1423 private_key: "testkey.pem",
1424 }`)
1425 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1426 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1427 if actual := rule.Args["certificates"]; actual != expected {
1428 t.Errorf("certificates should be %q, not %q", expected, actual)
1429 }
1430 })
1431 t.Run("override when specified as <name>", func(t *testing.T) {
1432 ctx, _ := testApex(t, `
1433 apex {
1434 name: "myapex_keytest",
1435 key: "myapex.key",
1436 file_contexts: ":myapex-file_contexts",
1437 certificate: "testkey",
1438 }
1439 apex_key {
1440 name: "myapex.key",
1441 public_key: "testkey.avbpubkey",
1442 private_key: "testkey.pem",
1443 }
1444 android_app_certificate {
1445 name: "myapex.certificate.override",
1446 certificate: "testkey.override",
1447 }`)
1448 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1449 expected := "testkey.override.x509.pem testkey.override.pk8"
1450 if actual := rule.Args["certificates"]; actual != expected {
1451 t.Errorf("certificates should be %q, not %q", expected, actual)
1452 }
1453 })
1454}
1455
Jiyong Park58e364a2019-01-19 19:24:06 +09001456func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001457 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001458 apex {
1459 name: "myapex",
1460 key: "myapex.key",
1461 native_shared_libs: ["mylib"],
1462 }
1463
1464 apex {
1465 name: "otherapex",
1466 key: "myapex.key",
1467 native_shared_libs: ["mylib"],
1468 }
1469
1470 apex_key {
1471 name: "myapex.key",
1472 public_key: "testkey.avbpubkey",
1473 private_key: "testkey.pem",
1474 }
1475
1476 cc_library {
1477 name: "mylib",
1478 srcs: ["mylib.cpp"],
1479 system_shared_libs: [],
1480 stl: "none",
1481 }
1482 `)
1483
Jooyung Han6b8459b2019-10-30 08:29:25 +09001484 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001485 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001486 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001487 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1488 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001489
Jooyung Han6b8459b2019-10-30 08:29:25 +09001490 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001491 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001492 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001493 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1494 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001495
Jooyung Han6b8459b2019-10-30 08:29:25 +09001496 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001497 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001498 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001499 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1500 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001501}
Jiyong Park7e636d02019-01-28 16:16:54 +09001502
1503func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001504 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001505 apex {
1506 name: "myapex",
1507 key: "myapex.key",
1508 native_shared_libs: ["mylib"],
1509 }
1510
1511 apex_key {
1512 name: "myapex.key",
1513 public_key: "testkey.avbpubkey",
1514 private_key: "testkey.pem",
1515 }
1516
1517 cc_library_headers {
1518 name: "mylib_headers",
1519 export_include_dirs: ["my_include"],
1520 system_shared_libs: [],
1521 stl: "none",
1522 }
1523
1524 cc_library {
1525 name: "mylib",
1526 srcs: ["mylib.cpp"],
1527 system_shared_libs: [],
1528 stl: "none",
1529 header_libs: ["mylib_headers"],
1530 export_header_lib_headers: ["mylib_headers"],
1531 stubs: {
1532 versions: ["1", "2", "3"],
1533 },
1534 }
1535
1536 cc_library {
1537 name: "otherlib",
1538 srcs: ["mylib.cpp"],
1539 system_shared_libs: [],
1540 stl: "none",
1541 shared_libs: ["mylib"],
1542 }
1543 `)
1544
Colin Cross7113d202019-11-20 16:39:12 -08001545 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001546
1547 // Ensure that the include path of the header lib is exported to 'otherlib'
1548 ensureContains(t, cFlags, "-Imy_include")
1549}
Alex Light9670d332019-01-29 18:07:33 -08001550
Jooyung Han31c470b2019-10-18 16:26:59 +09001551func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1552 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001553 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001554 copyCmds := apexRule.Args["copy_commands"]
1555 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001556 var failed bool
1557 var surplus []string
1558 filesMatched := make(map[string]bool)
1559 addContent := func(content string) {
1560 for _, expected := range files {
1561 if matched, _ := path.Match(expected, content); matched {
1562 filesMatched[expected] = true
1563 return
1564 }
1565 }
1566 surplus = append(surplus, content)
1567 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001568 for _, cmd := range strings.Split(copyCmds, "&&") {
1569 cmd = strings.TrimSpace(cmd)
1570 if cmd == "" {
1571 continue
1572 }
1573 terms := strings.Split(cmd, " ")
1574 switch terms[0] {
1575 case "mkdir":
1576 case "cp":
1577 if len(terms) != 3 {
1578 t.Fatal("copyCmds contains invalid cp command", cmd)
1579 }
1580 dst := terms[2]
1581 index := strings.Index(dst, imageApexDir)
1582 if index == -1 {
1583 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1584 }
1585 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001586 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001587 default:
1588 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1589 }
1590 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001591
Jooyung Han31c470b2019-10-18 16:26:59 +09001592 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001593 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001594 t.Log("surplus files", surplus)
1595 failed = true
1596 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001597
1598 if len(files) > len(filesMatched) {
1599 var missing []string
1600 for _, expected := range files {
1601 if !filesMatched[expected] {
1602 missing = append(missing, expected)
1603 }
1604 }
1605 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001606 t.Log("missing files", missing)
1607 failed = true
1608 }
1609 if failed {
1610 t.Fail()
1611 }
1612}
1613
Jooyung Han344d5432019-08-23 11:17:39 +09001614func TestVndkApexCurrent(t *testing.T) {
1615 ctx, _ := testApex(t, `
1616 apex_vndk {
1617 name: "myapex",
1618 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001619 }
1620
1621 apex_key {
1622 name: "myapex.key",
1623 public_key: "testkey.avbpubkey",
1624 private_key: "testkey.pem",
1625 }
1626
1627 cc_library {
1628 name: "libvndk",
1629 srcs: ["mylib.cpp"],
1630 vendor_available: true,
1631 vndk: {
1632 enabled: true,
1633 },
1634 system_shared_libs: [],
1635 stl: "none",
1636 }
1637
1638 cc_library {
1639 name: "libvndksp",
1640 srcs: ["mylib.cpp"],
1641 vendor_available: true,
1642 vndk: {
1643 enabled: true,
1644 support_system_process: true,
1645 },
1646 system_shared_libs: [],
1647 stl: "none",
1648 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001649 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001650
Jooyung Han31c470b2019-10-18 16:26:59 +09001651 ensureExactContents(t, ctx, "myapex", []string{
1652 "lib/libvndk.so",
1653 "lib/libvndksp.so",
1654 "lib64/libvndk.so",
1655 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001656 "etc/llndk.libraries.VER.txt",
1657 "etc/vndkcore.libraries.VER.txt",
1658 "etc/vndksp.libraries.VER.txt",
1659 "etc/vndkprivate.libraries.VER.txt",
1660 "etc/vndkcorevariant.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001661 })
Jooyung Han344d5432019-08-23 11:17:39 +09001662}
1663
1664func TestVndkApexWithPrebuilt(t *testing.T) {
1665 ctx, _ := testApex(t, `
1666 apex_vndk {
1667 name: "myapex",
1668 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001669 }
1670
1671 apex_key {
1672 name: "myapex.key",
1673 public_key: "testkey.avbpubkey",
1674 private_key: "testkey.pem",
1675 }
1676
1677 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001678 name: "libvndk",
1679 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001680 vendor_available: true,
1681 vndk: {
1682 enabled: true,
1683 },
1684 system_shared_libs: [],
1685 stl: "none",
1686 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001687
1688 cc_prebuilt_library_shared {
1689 name: "libvndk.arm",
1690 srcs: ["libvndk.arm.so"],
1691 vendor_available: true,
1692 vndk: {
1693 enabled: true,
1694 },
1695 enabled: false,
1696 arch: {
1697 arm: {
1698 enabled: true,
1699 },
1700 },
1701 system_shared_libs: [],
1702 stl: "none",
1703 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001704 `+vndkLibrariesTxtFiles("current"),
1705 withFiles(map[string][]byte{
1706 "libvndk.so": nil,
1707 "libvndk.arm.so": nil,
1708 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001709
Jooyung Han31c470b2019-10-18 16:26:59 +09001710 ensureExactContents(t, ctx, "myapex", []string{
1711 "lib/libvndk.so",
1712 "lib/libvndk.arm.so",
1713 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001714 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001715 })
Jooyung Han344d5432019-08-23 11:17:39 +09001716}
1717
Jooyung Han39edb6c2019-11-06 16:53:07 +09001718func vndkLibrariesTxtFiles(vers ...string) (result string) {
1719 for _, v := range vers {
1720 if v == "current" {
1721 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkcorevariant"} {
1722 result += `
1723 vndk_libraries_txt {
1724 name: "` + txt + `.libraries.txt",
1725 }
1726 `
1727 }
1728 } else {
1729 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1730 result += `
1731 prebuilt_etc {
1732 name: "` + txt + `.libraries.` + v + `.txt",
1733 src: "dummy.txt",
1734 }
1735 `
1736 }
1737 }
1738 }
1739 return
1740}
1741
Jooyung Han344d5432019-08-23 11:17:39 +09001742func TestVndkApexVersion(t *testing.T) {
1743 ctx, _ := testApex(t, `
1744 apex_vndk {
1745 name: "myapex_v27",
1746 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001747 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001748 vndk_version: "27",
1749 }
1750
1751 apex_key {
1752 name: "myapex.key",
1753 public_key: "testkey.avbpubkey",
1754 private_key: "testkey.pem",
1755 }
1756
Jooyung Han31c470b2019-10-18 16:26:59 +09001757 vndk_prebuilt_shared {
1758 name: "libvndk27",
1759 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001760 vendor_available: true,
1761 vndk: {
1762 enabled: true,
1763 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001764 target_arch: "arm64",
1765 arch: {
1766 arm: {
1767 srcs: ["libvndk27_arm.so"],
1768 },
1769 arm64: {
1770 srcs: ["libvndk27_arm64.so"],
1771 },
1772 },
Jooyung Han344d5432019-08-23 11:17:39 +09001773 }
1774
1775 vndk_prebuilt_shared {
1776 name: "libvndk27",
1777 version: "27",
1778 vendor_available: true,
1779 vndk: {
1780 enabled: true,
1781 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001782 target_arch: "x86_64",
1783 arch: {
1784 x86: {
1785 srcs: ["libvndk27_x86.so"],
1786 },
1787 x86_64: {
1788 srcs: ["libvndk27_x86_64.so"],
1789 },
1790 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001791 }
1792 `+vndkLibrariesTxtFiles("27"),
1793 withFiles(map[string][]byte{
1794 "libvndk27_arm.so": nil,
1795 "libvndk27_arm64.so": nil,
1796 "libvndk27_x86.so": nil,
1797 "libvndk27_x86_64.so": nil,
1798 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001799
Jooyung Han31c470b2019-10-18 16:26:59 +09001800 ensureExactContents(t, ctx, "myapex_v27", []string{
1801 "lib/libvndk27_arm.so",
1802 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001803 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001804 })
Jooyung Han344d5432019-08-23 11:17:39 +09001805}
1806
1807func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1808 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1809 apex_vndk {
1810 name: "myapex_v27",
1811 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001812 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001813 vndk_version: "27",
1814 }
1815 apex_vndk {
1816 name: "myapex_v27_other",
1817 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001818 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001819 vndk_version: "27",
1820 }
1821
1822 apex_key {
1823 name: "myapex.key",
1824 public_key: "testkey.avbpubkey",
1825 private_key: "testkey.pem",
1826 }
1827
1828 cc_library {
1829 name: "libvndk",
1830 srcs: ["mylib.cpp"],
1831 vendor_available: true,
1832 vndk: {
1833 enabled: true,
1834 },
1835 system_shared_libs: [],
1836 stl: "none",
1837 }
1838
1839 vndk_prebuilt_shared {
1840 name: "libvndk",
1841 version: "27",
1842 vendor_available: true,
1843 vndk: {
1844 enabled: true,
1845 },
1846 srcs: ["libvndk.so"],
1847 }
1848 `, withFiles(map[string][]byte{
1849 "libvndk.so": nil,
1850 }))
1851}
1852
Jooyung Han90eee022019-10-01 20:02:42 +09001853func TestVndkApexNameRule(t *testing.T) {
1854 ctx, _ := testApex(t, `
1855 apex_vndk {
1856 name: "myapex",
1857 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001858 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001859 }
1860 apex_vndk {
1861 name: "myapex_v28",
1862 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001863 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001864 vndk_version: "28",
1865 }
1866 apex_key {
1867 name: "myapex.key",
1868 public_key: "testkey.avbpubkey",
1869 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001870 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001871
1872 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001873 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001874 actual := proptools.String(bundle.properties.Apex_name)
1875 if !reflect.DeepEqual(actual, expected) {
1876 t.Errorf("Got '%v', expected '%v'", actual, expected)
1877 }
1878 }
1879
1880 assertApexName("com.android.vndk.vVER", "myapex")
1881 assertApexName("com.android.vndk.v28", "myapex_v28")
1882}
1883
Jooyung Han344d5432019-08-23 11:17:39 +09001884func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1885 ctx, _ := testApex(t, `
1886 apex_vndk {
1887 name: "myapex",
1888 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001889 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001890 }
1891
1892 apex_key {
1893 name: "myapex.key",
1894 public_key: "testkey.avbpubkey",
1895 private_key: "testkey.pem",
1896 }
1897
1898 cc_library {
1899 name: "libvndk",
1900 srcs: ["mylib.cpp"],
1901 vendor_available: true,
1902 native_bridge_supported: true,
1903 host_supported: true,
1904 vndk: {
1905 enabled: true,
1906 },
1907 system_shared_libs: [],
1908 stl: "none",
1909 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001910 `+vndkLibrariesTxtFiles("current"),
1911 withTargets(map[android.OsType][]android.Target{
1912 android.Android: []android.Target{
1913 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1914 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1915 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1916 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1917 },
1918 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001919
Jooyung Han31c470b2019-10-18 16:26:59 +09001920 ensureExactContents(t, ctx, "myapex", []string{
1921 "lib/libvndk.so",
1922 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001923 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001924 })
Jooyung Han344d5432019-08-23 11:17:39 +09001925}
1926
1927func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1928 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1929 apex_vndk {
1930 name: "myapex",
1931 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001932 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001933 native_bridge_supported: true,
1934 }
1935
1936 apex_key {
1937 name: "myapex.key",
1938 public_key: "testkey.avbpubkey",
1939 private_key: "testkey.pem",
1940 }
1941
1942 cc_library {
1943 name: "libvndk",
1944 srcs: ["mylib.cpp"],
1945 vendor_available: true,
1946 native_bridge_supported: true,
1947 host_supported: true,
1948 vndk: {
1949 enabled: true,
1950 },
1951 system_shared_libs: [],
1952 stl: "none",
1953 }
1954 `)
1955}
1956
Jooyung Han31c470b2019-10-18 16:26:59 +09001957func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001958 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001959 apex_vndk {
1960 name: "myapex_v27",
1961 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001962 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001963 vndk_version: "27",
1964 }
1965
1966 apex_key {
1967 name: "myapex.key",
1968 public_key: "testkey.avbpubkey",
1969 private_key: "testkey.pem",
1970 }
1971
1972 vndk_prebuilt_shared {
1973 name: "libvndk27",
1974 version: "27",
1975 target_arch: "arm",
1976 vendor_available: true,
1977 vndk: {
1978 enabled: true,
1979 },
1980 arch: {
1981 arm: {
1982 srcs: ["libvndk27.so"],
1983 }
1984 },
1985 }
1986
1987 vndk_prebuilt_shared {
1988 name: "libvndk27",
1989 version: "27",
1990 target_arch: "arm",
1991 binder32bit: true,
1992 vendor_available: true,
1993 vndk: {
1994 enabled: true,
1995 },
1996 arch: {
1997 arm: {
1998 srcs: ["libvndk27binder32.so"],
1999 }
2000 },
2001 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002002 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002003 withFiles(map[string][]byte{
2004 "libvndk27.so": nil,
2005 "libvndk27binder32.so": nil,
2006 }),
2007 withBinder32bit,
2008 withTargets(map[android.OsType][]android.Target{
2009 android.Android: []android.Target{
2010 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2011 },
2012 }),
2013 )
2014
2015 ensureExactContents(t, ctx, "myapex_v27", []string{
2016 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002017 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002018 })
2019}
2020
Jooyung Hane1633032019-08-01 17:41:43 +09002021func TestDependenciesInApexManifest(t *testing.T) {
2022 ctx, _ := testApex(t, `
2023 apex {
2024 name: "myapex_nodep",
2025 key: "myapex.key",
2026 native_shared_libs: ["lib_nodep"],
2027 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002028 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002029 }
2030
2031 apex {
2032 name: "myapex_dep",
2033 key: "myapex.key",
2034 native_shared_libs: ["lib_dep"],
2035 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002036 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002037 }
2038
2039 apex {
2040 name: "myapex_provider",
2041 key: "myapex.key",
2042 native_shared_libs: ["libfoo"],
2043 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002044 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002045 }
2046
2047 apex {
2048 name: "myapex_selfcontained",
2049 key: "myapex.key",
2050 native_shared_libs: ["lib_dep", "libfoo"],
2051 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002052 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002053 }
2054
2055 apex_key {
2056 name: "myapex.key",
2057 public_key: "testkey.avbpubkey",
2058 private_key: "testkey.pem",
2059 }
2060
2061 cc_library {
2062 name: "lib_nodep",
2063 srcs: ["mylib.cpp"],
2064 system_shared_libs: [],
2065 stl: "none",
2066 }
2067
2068 cc_library {
2069 name: "lib_dep",
2070 srcs: ["mylib.cpp"],
2071 shared_libs: ["libfoo"],
2072 system_shared_libs: [],
2073 stl: "none",
2074 }
2075
2076 cc_library {
2077 name: "libfoo",
2078 srcs: ["mytest.cpp"],
2079 stubs: {
2080 versions: ["1"],
2081 },
2082 system_shared_libs: [],
2083 stl: "none",
2084 }
2085 `)
2086
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002087 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002088 var provideNativeLibs, requireNativeLibs []string
2089
Sundong Ahnabb64432019-10-22 13:58:29 +09002090 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002091 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2092 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002093 ensureListEmpty(t, provideNativeLibs)
2094 ensureListEmpty(t, requireNativeLibs)
2095
Sundong Ahnabb64432019-10-22 13:58:29 +09002096 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002097 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2098 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002099 ensureListEmpty(t, provideNativeLibs)
2100 ensureListContains(t, requireNativeLibs, "libfoo.so")
2101
Sundong Ahnabb64432019-10-22 13:58:29 +09002102 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002103 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2104 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002105 ensureListContains(t, provideNativeLibs, "libfoo.so")
2106 ensureListEmpty(t, requireNativeLibs)
2107
Sundong Ahnabb64432019-10-22 13:58:29 +09002108 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002109 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2110 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002111 ensureListContains(t, provideNativeLibs, "libfoo.so")
2112 ensureListEmpty(t, requireNativeLibs)
2113}
2114
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002115func TestApexName(t *testing.T) {
2116 ctx, _ := testApex(t, `
2117 apex {
2118 name: "myapex",
2119 key: "myapex.key",
2120 apex_name: "com.android.myapex",
2121 }
2122
2123 apex_key {
2124 name: "myapex.key",
2125 public_key: "testkey.avbpubkey",
2126 private_key: "testkey.pem",
2127 }
2128 `)
2129
Sundong Ahnabb64432019-10-22 13:58:29 +09002130 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002131 apexManifestRule := module.Rule("apexManifestRule")
2132 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2133 apexRule := module.Rule("apexRule")
2134 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
2135}
2136
Alex Light0851b882019-02-07 13:20:53 -08002137func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002138 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002139 apex {
2140 name: "myapex",
2141 key: "myapex.key",
2142 native_shared_libs: ["mylib_common"],
2143 }
2144
2145 apex_key {
2146 name: "myapex.key",
2147 public_key: "testkey.avbpubkey",
2148 private_key: "testkey.pem",
2149 }
2150
2151 cc_library {
2152 name: "mylib_common",
2153 srcs: ["mylib.cpp"],
2154 system_shared_libs: [],
2155 stl: "none",
2156 }
2157 `)
2158
Sundong Ahnabb64432019-10-22 13:58:29 +09002159 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002160 apexRule := module.Rule("apexRule")
2161 copyCmds := apexRule.Args["copy_commands"]
2162
2163 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2164 t.Log("Apex was a test apex!")
2165 t.Fail()
2166 }
2167 // Ensure that main rule creates an output
2168 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2169
2170 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002171 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002172
2173 // Ensure that both direct and indirect deps are copied into apex
2174 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2175
Colin Cross7113d202019-11-20 16:39:12 -08002176 // Ensure that the platform variant ends with _shared
2177 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002178
2179 if !android.InAnyApex("mylib_common") {
2180 t.Log("Found mylib_common not in any apex!")
2181 t.Fail()
2182 }
2183}
2184
2185func TestTestApex(t *testing.T) {
2186 if android.InAnyApex("mylib_common_test") {
2187 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!")
2188 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002189 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002190 apex_test {
2191 name: "myapex",
2192 key: "myapex.key",
2193 native_shared_libs: ["mylib_common_test"],
2194 }
2195
2196 apex_key {
2197 name: "myapex.key",
2198 public_key: "testkey.avbpubkey",
2199 private_key: "testkey.pem",
2200 }
2201
2202 cc_library {
2203 name: "mylib_common_test",
2204 srcs: ["mylib.cpp"],
2205 system_shared_libs: [],
2206 stl: "none",
2207 }
2208 `)
2209
Sundong Ahnabb64432019-10-22 13:58:29 +09002210 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002211 apexRule := module.Rule("apexRule")
2212 copyCmds := apexRule.Args["copy_commands"]
2213
2214 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2215 t.Log("Apex was not a test apex!")
2216 t.Fail()
2217 }
2218 // Ensure that main rule creates an output
2219 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2220
2221 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002222 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002223
2224 // Ensure that both direct and indirect deps are copied into apex
2225 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2226
Colin Cross7113d202019-11-20 16:39:12 -08002227 // Ensure that the platform variant ends with _shared
2228 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002229
2230 if android.InAnyApex("mylib_common_test") {
2231 t.Log("Found mylib_common_test in some apex!")
2232 t.Fail()
2233 }
2234}
2235
Alex Light9670d332019-01-29 18:07:33 -08002236func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002237 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002238 apex {
2239 name: "myapex",
2240 key: "myapex.key",
2241 multilib: {
2242 first: {
2243 native_shared_libs: ["mylib_common"],
2244 }
2245 },
2246 target: {
2247 android: {
2248 multilib: {
2249 first: {
2250 native_shared_libs: ["mylib"],
2251 }
2252 }
2253 },
2254 host: {
2255 multilib: {
2256 first: {
2257 native_shared_libs: ["mylib2"],
2258 }
2259 }
2260 }
2261 }
2262 }
2263
2264 apex_key {
2265 name: "myapex.key",
2266 public_key: "testkey.avbpubkey",
2267 private_key: "testkey.pem",
2268 }
2269
2270 cc_library {
2271 name: "mylib",
2272 srcs: ["mylib.cpp"],
2273 system_shared_libs: [],
2274 stl: "none",
2275 }
2276
2277 cc_library {
2278 name: "mylib_common",
2279 srcs: ["mylib.cpp"],
2280 system_shared_libs: [],
2281 stl: "none",
2282 compile_multilib: "first",
2283 }
2284
2285 cc_library {
2286 name: "mylib2",
2287 srcs: ["mylib.cpp"],
2288 system_shared_libs: [],
2289 stl: "none",
2290 compile_multilib: "first",
2291 }
2292 `)
2293
Sundong Ahnabb64432019-10-22 13:58:29 +09002294 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002295 copyCmds := apexRule.Args["copy_commands"]
2296
2297 // Ensure that main rule creates an output
2298 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2299
2300 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002301 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2302 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2303 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002304
2305 // Ensure that both direct and indirect deps are copied into apex
2306 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2307 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2308 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2309
Colin Cross7113d202019-11-20 16:39:12 -08002310 // Ensure that the platform variant ends with _shared
2311 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2312 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2313 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002314}
Jiyong Park04480cf2019-02-06 00:16:29 +09002315
2316func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002317 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002318 apex {
2319 name: "myapex",
2320 key: "myapex.key",
2321 binaries: ["myscript"],
2322 }
2323
2324 apex_key {
2325 name: "myapex.key",
2326 public_key: "testkey.avbpubkey",
2327 private_key: "testkey.pem",
2328 }
2329
2330 sh_binary {
2331 name: "myscript",
2332 src: "mylib.cpp",
2333 filename: "myscript.sh",
2334 sub_dir: "script",
2335 }
2336 `)
2337
Sundong Ahnabb64432019-10-22 13:58:29 +09002338 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002339 copyCmds := apexRule.Args["copy_commands"]
2340
2341 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2342}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002343
Jooyung Han91df2082019-11-20 01:49:42 +09002344func TestApexInVariousPartition(t *testing.T) {
2345 testcases := []struct {
2346 propName, parition, flattenedPartition string
2347 }{
2348 {"", "system", "system_ext"},
2349 {"product_specific: true", "product", "product"},
2350 {"soc_specific: true", "vendor", "vendor"},
2351 {"proprietary: true", "vendor", "vendor"},
2352 {"vendor: true", "vendor", "vendor"},
2353 {"system_ext_specific: true", "system_ext", "system_ext"},
2354 }
2355 for _, tc := range testcases {
2356 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2357 ctx, _ := testApex(t, `
2358 apex {
2359 name: "myapex",
2360 key: "myapex.key",
2361 `+tc.propName+`
2362 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002363
Jooyung Han91df2082019-11-20 01:49:42 +09002364 apex_key {
2365 name: "myapex.key",
2366 public_key: "testkey.avbpubkey",
2367 private_key: "testkey.pem",
2368 }
2369 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002370
Jooyung Han91df2082019-11-20 01:49:42 +09002371 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2372 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2373 actual := apex.installDir.String()
2374 if actual != expected {
2375 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2376 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002377
Jooyung Han91df2082019-11-20 01:49:42 +09002378 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2379 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2380 actual = flattened.installDir.String()
2381 if actual != expected {
2382 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2383 }
2384 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002385 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002386}
Jiyong Park67882562019-03-21 01:11:21 +09002387
Jooyung Han54aca7b2019-11-20 02:26:02 +09002388func TestFileContexts(t *testing.T) {
2389 ctx, _ := testApex(t, `
2390 apex {
2391 name: "myapex",
2392 key: "myapex.key",
2393 }
2394
2395 apex_key {
2396 name: "myapex.key",
2397 public_key: "testkey.avbpubkey",
2398 private_key: "testkey.pem",
2399 }
2400 `)
2401 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2402 apexRule := module.Rule("apexRule")
2403 actual := apexRule.Args["file_contexts"]
2404 expected := "system/sepolicy/apex/myapex-file_contexts"
2405 if actual != expected {
2406 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2407 }
2408
2409 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2410 apex {
2411 name: "myapex",
2412 key: "myapex.key",
2413 file_contexts: "my_own_file_contexts",
2414 }
2415
2416 apex_key {
2417 name: "myapex.key",
2418 public_key: "testkey.avbpubkey",
2419 private_key: "testkey.pem",
2420 }
2421 `, withFiles(map[string][]byte{
2422 "my_own_file_contexts": nil,
2423 }))
2424
2425 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2426 apex {
2427 name: "myapex",
2428 key: "myapex.key",
2429 product_specific: true,
2430 file_contexts: "product_specific_file_contexts",
2431 }
2432
2433 apex_key {
2434 name: "myapex.key",
2435 public_key: "testkey.avbpubkey",
2436 private_key: "testkey.pem",
2437 }
2438 `)
2439
2440 ctx, _ = testApex(t, `
2441 apex {
2442 name: "myapex",
2443 key: "myapex.key",
2444 product_specific: true,
2445 file_contexts: "product_specific_file_contexts",
2446 }
2447
2448 apex_key {
2449 name: "myapex.key",
2450 public_key: "testkey.avbpubkey",
2451 private_key: "testkey.pem",
2452 }
2453 `, withFiles(map[string][]byte{
2454 "product_specific_file_contexts": nil,
2455 }))
2456 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2457 apexRule = module.Rule("apexRule")
2458 actual = apexRule.Args["file_contexts"]
2459 expected = "product_specific_file_contexts"
2460 if actual != expected {
2461 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2462 }
2463
2464 ctx, _ = testApex(t, `
2465 apex {
2466 name: "myapex",
2467 key: "myapex.key",
2468 product_specific: true,
2469 file_contexts: ":my-file-contexts",
2470 }
2471
2472 apex_key {
2473 name: "myapex.key",
2474 public_key: "testkey.avbpubkey",
2475 private_key: "testkey.pem",
2476 }
2477
2478 filegroup {
2479 name: "my-file-contexts",
2480 srcs: ["product_specific_file_contexts"],
2481 }
2482 `, withFiles(map[string][]byte{
2483 "product_specific_file_contexts": nil,
2484 }))
2485 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2486 apexRule = module.Rule("apexRule")
2487 actual = apexRule.Args["file_contexts"]
2488 expected = "product_specific_file_contexts"
2489 if actual != expected {
2490 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2491 }
2492}
2493
Jiyong Park67882562019-03-21 01:11:21 +09002494func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002495 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002496 apex_key {
2497 name: "myapex.key",
2498 public_key: ":my.avbpubkey",
2499 private_key: ":my.pem",
2500 product_specific: true,
2501 }
2502
2503 filegroup {
2504 name: "my.avbpubkey",
2505 srcs: ["testkey2.avbpubkey"],
2506 }
2507
2508 filegroup {
2509 name: "my.pem",
2510 srcs: ["testkey2.pem"],
2511 }
2512 `)
2513
2514 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2515 expected_pubkey := "testkey2.avbpubkey"
2516 actual_pubkey := apex_key.public_key_file.String()
2517 if actual_pubkey != expected_pubkey {
2518 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2519 }
2520 expected_privkey := "testkey2.pem"
2521 actual_privkey := apex_key.private_key_file.String()
2522 if actual_privkey != expected_privkey {
2523 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2524 }
2525}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002526
2527func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002528 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002529 prebuilt_apex {
2530 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002531 arch: {
2532 arm64: {
2533 src: "myapex-arm64.apex",
2534 },
2535 arm: {
2536 src: "myapex-arm.apex",
2537 },
2538 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002539 }
2540 `)
2541
2542 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2543
Jiyong Parkc95714e2019-03-29 14:23:10 +09002544 expectedInput := "myapex-arm64.apex"
2545 if prebuilt.inputApex.String() != expectedInput {
2546 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2547 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002548}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002549
2550func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002551 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002552 prebuilt_apex {
2553 name: "myapex",
2554 src: "myapex-arm.apex",
2555 filename: "notmyapex.apex",
2556 }
2557 `)
2558
2559 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2560
2561 expected := "notmyapex.apex"
2562 if p.installFilename != expected {
2563 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2564 }
2565}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002566
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002567func TestPrebuiltOverrides(t *testing.T) {
2568 ctx, config := testApex(t, `
2569 prebuilt_apex {
2570 name: "myapex.prebuilt",
2571 src: "myapex-arm.apex",
2572 overrides: [
2573 "myapex",
2574 ],
2575 }
2576 `)
2577
2578 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2579
2580 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002581 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002582 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002583 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002584 }
2585}
2586
Roland Levillain630846d2019-06-26 12:48:34 +01002587func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002588 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002589 apex_test {
2590 name: "myapex",
2591 key: "myapex.key",
2592 tests: [
2593 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002594 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002595 ],
2596 }
2597
2598 apex_key {
2599 name: "myapex.key",
2600 public_key: "testkey.avbpubkey",
2601 private_key: "testkey.pem",
2602 }
2603
2604 cc_test {
2605 name: "mytest",
2606 gtest: false,
2607 srcs: ["mytest.cpp"],
2608 relative_install_path: "test",
2609 system_shared_libs: [],
2610 static_executable: true,
2611 stl: "none",
2612 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002613
2614 cc_test {
2615 name: "mytests",
2616 gtest: false,
2617 srcs: [
2618 "mytest1.cpp",
2619 "mytest2.cpp",
2620 "mytest3.cpp",
2621 ],
2622 test_per_src: true,
2623 relative_install_path: "test",
2624 system_shared_libs: [],
2625 static_executable: true,
2626 stl: "none",
2627 }
Roland Levillain630846d2019-06-26 12:48:34 +01002628 `)
2629
Sundong Ahnabb64432019-10-22 13:58:29 +09002630 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002631 copyCmds := apexRule.Args["copy_commands"]
2632
2633 // Ensure that test dep is copied into apex.
2634 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002635
2636 // Ensure that test deps built with `test_per_src` are copied into apex.
2637 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2638 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2639 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002640
2641 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002642 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002643 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2644 name := apexBundle.BaseModuleName()
2645 prefix := "TARGET_"
2646 var builder strings.Builder
2647 data.Custom(&builder, name, prefix, "", data)
2648 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002649 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2650 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2651 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2652 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002653 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002654 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002655 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002656}
2657
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002658func TestInstallExtraFlattenedApexes(t *testing.T) {
2659 ctx, config := testApex(t, `
2660 apex {
2661 name: "myapex",
2662 key: "myapex.key",
2663 }
2664 apex_key {
2665 name: "myapex.key",
2666 public_key: "testkey.avbpubkey",
2667 private_key: "testkey.pem",
2668 }
2669 `, func(fs map[string][]byte, config android.Config) {
2670 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2671 })
2672 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2673 ensureListContains(t, ab.externalDeps, "myapex.flattened")
2674 mk := android.AndroidMkDataForTest(t, config, "", ab)
2675 var builder strings.Builder
2676 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2677 androidMk := builder.String()
2678 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2679}
2680
Jooyung Han5c998b92019-06-27 11:30:33 +09002681func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002682 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002683 apex {
2684 name: "myapex",
2685 key: "myapex.key",
2686 native_shared_libs: ["mylib"],
2687 uses: ["commonapex"],
2688 }
2689
2690 apex {
2691 name: "commonapex",
2692 key: "myapex.key",
2693 native_shared_libs: ["libcommon"],
2694 provide_cpp_shared_libs: true,
2695 }
2696
2697 apex_key {
2698 name: "myapex.key",
2699 public_key: "testkey.avbpubkey",
2700 private_key: "testkey.pem",
2701 }
2702
2703 cc_library {
2704 name: "mylib",
2705 srcs: ["mylib.cpp"],
2706 shared_libs: ["libcommon"],
2707 system_shared_libs: [],
2708 stl: "none",
2709 }
2710
2711 cc_library {
2712 name: "libcommon",
2713 srcs: ["mylib_common.cpp"],
2714 system_shared_libs: [],
2715 stl: "none",
2716 }
2717 `)
2718
Sundong Ahnabb64432019-10-22 13:58:29 +09002719 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002720 apexRule1 := module1.Rule("apexRule")
2721 copyCmds1 := apexRule1.Args["copy_commands"]
2722
Sundong Ahnabb64432019-10-22 13:58:29 +09002723 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002724 apexRule2 := module2.Rule("apexRule")
2725 copyCmds2 := apexRule2.Args["copy_commands"]
2726
Colin Cross7113d202019-11-20 16:39:12 -08002727 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2728 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002729 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2730 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2731 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2732}
2733
2734func TestApexUsesFailsIfNotProvided(t *testing.T) {
2735 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2736 apex {
2737 name: "myapex",
2738 key: "myapex.key",
2739 uses: ["commonapex"],
2740 }
2741
2742 apex {
2743 name: "commonapex",
2744 key: "myapex.key",
2745 }
2746
2747 apex_key {
2748 name: "myapex.key",
2749 public_key: "testkey.avbpubkey",
2750 private_key: "testkey.pem",
2751 }
2752 `)
2753 testApexError(t, `uses: "commonapex" is not a provider`, `
2754 apex {
2755 name: "myapex",
2756 key: "myapex.key",
2757 uses: ["commonapex"],
2758 }
2759
2760 cc_library {
2761 name: "commonapex",
2762 system_shared_libs: [],
2763 stl: "none",
2764 }
2765
2766 apex_key {
2767 name: "myapex.key",
2768 public_key: "testkey.avbpubkey",
2769 private_key: "testkey.pem",
2770 }
2771 `)
2772}
2773
2774func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2775 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2776 apex {
2777 name: "myapex",
2778 key: "myapex.key",
2779 use_vendor: true,
2780 uses: ["commonapex"],
2781 }
2782
2783 apex {
2784 name: "commonapex",
2785 key: "myapex.key",
2786 provide_cpp_shared_libs: true,
2787 }
2788
2789 apex_key {
2790 name: "myapex.key",
2791 public_key: "testkey.avbpubkey",
2792 private_key: "testkey.pem",
2793 }
Jooyung Handc782442019-11-01 03:14:38 +09002794 `, func(fs map[string][]byte, config android.Config) {
2795 setUseVendorWhitelistForTest(config, []string{"myapex"})
2796 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002797}
2798
Jooyung Hand48f3c32019-08-23 11:18:57 +09002799func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2800 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2801 apex {
2802 name: "myapex",
2803 key: "myapex.key",
2804 native_shared_libs: ["libfoo"],
2805 }
2806
2807 apex_key {
2808 name: "myapex.key",
2809 public_key: "testkey.avbpubkey",
2810 private_key: "testkey.pem",
2811 }
2812
2813 cc_library {
2814 name: "libfoo",
2815 stl: "none",
2816 system_shared_libs: [],
2817 enabled: false,
2818 }
2819 `)
2820 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2821 apex {
2822 name: "myapex",
2823 key: "myapex.key",
2824 java_libs: ["myjar"],
2825 }
2826
2827 apex_key {
2828 name: "myapex.key",
2829 public_key: "testkey.avbpubkey",
2830 private_key: "testkey.pem",
2831 }
2832
2833 java_library {
2834 name: "myjar",
2835 srcs: ["foo/bar/MyClass.java"],
2836 sdk_version: "none",
2837 system_modules: "none",
2838 compile_dex: true,
2839 enabled: false,
2840 }
2841 `)
2842}
2843
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002844func TestApexWithApps(t *testing.T) {
2845 ctx, _ := testApex(t, `
2846 apex {
2847 name: "myapex",
2848 key: "myapex.key",
2849 apps: [
2850 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002851 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002852 ],
2853 }
2854
2855 apex_key {
2856 name: "myapex.key",
2857 public_key: "testkey.avbpubkey",
2858 private_key: "testkey.pem",
2859 }
2860
2861 android_app {
2862 name: "AppFoo",
2863 srcs: ["foo/bar/MyClass.java"],
2864 sdk_version: "none",
2865 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002866 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002867 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002868
2869 android_app {
2870 name: "AppFooPriv",
2871 srcs: ["foo/bar/MyClass.java"],
2872 sdk_version: "none",
2873 system_modules: "none",
2874 privileged: true,
2875 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002876
2877 cc_library_shared {
2878 name: "libjni",
2879 srcs: ["mylib.cpp"],
2880 stl: "none",
2881 system_shared_libs: [],
2882 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002883 `)
2884
Sundong Ahnabb64432019-10-22 13:58:29 +09002885 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002886 apexRule := module.Rule("apexRule")
2887 copyCmds := apexRule.Args["copy_commands"]
2888
2889 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002890 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002891
2892 // JNI libraries are embedded inside APK
2893 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
Colin Cross7113d202019-11-20 16:39:12 -08002894 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002895 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2896 // ... uncompressed
2897 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2898 t.Errorf("jni lib is not uncompressed for AppFoo")
2899 }
2900 // ... and not directly inside the APEX
2901 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002902}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002903
Dario Frenicde2a032019-10-27 00:29:22 +01002904func TestApexWithAppImports(t *testing.T) {
2905 ctx, _ := testApex(t, `
2906 apex {
2907 name: "myapex",
2908 key: "myapex.key",
2909 apps: [
2910 "AppFooPrebuilt",
2911 "AppFooPrivPrebuilt",
2912 ],
2913 }
2914
2915 apex_key {
2916 name: "myapex.key",
2917 public_key: "testkey.avbpubkey",
2918 private_key: "testkey.pem",
2919 }
2920
2921 android_app_import {
2922 name: "AppFooPrebuilt",
2923 apk: "PrebuiltAppFoo.apk",
2924 presigned: true,
2925 dex_preopt: {
2926 enabled: false,
2927 },
2928 }
2929
2930 android_app_import {
2931 name: "AppFooPrivPrebuilt",
2932 apk: "PrebuiltAppFooPriv.apk",
2933 privileged: true,
2934 presigned: true,
2935 dex_preopt: {
2936 enabled: false,
2937 },
2938 }
2939 `)
2940
Sundong Ahnabb64432019-10-22 13:58:29 +09002941 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002942 apexRule := module.Rule("apexRule")
2943 copyCmds := apexRule.Args["copy_commands"]
2944
2945 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2946 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002947}
2948
Jooyung Han18020ea2019-11-13 10:50:48 +09002949func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2950 // libfoo's apex_available comes from cc_defaults
2951 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2952 apex {
2953 name: "myapex",
2954 key: "myapex.key",
2955 native_shared_libs: ["libfoo"],
2956 }
2957
2958 apex_key {
2959 name: "myapex.key",
2960 public_key: "testkey.avbpubkey",
2961 private_key: "testkey.pem",
2962 }
2963
2964 apex {
2965 name: "otherapex",
2966 key: "myapex.key",
2967 native_shared_libs: ["libfoo"],
2968 }
2969
2970 cc_defaults {
2971 name: "libfoo-defaults",
2972 apex_available: ["otherapex"],
2973 }
2974
2975 cc_library {
2976 name: "libfoo",
2977 defaults: ["libfoo-defaults"],
2978 stl: "none",
2979 system_shared_libs: [],
2980 }`)
2981}
2982
Jiyong Park127b40b2019-09-30 16:04:35 +09002983func TestApexAvailable(t *testing.T) {
2984 // libfoo is not available to myapex, but only to otherapex
2985 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2986 apex {
2987 name: "myapex",
2988 key: "myapex.key",
2989 native_shared_libs: ["libfoo"],
2990 }
2991
2992 apex_key {
2993 name: "myapex.key",
2994 public_key: "testkey.avbpubkey",
2995 private_key: "testkey.pem",
2996 }
2997
2998 apex {
2999 name: "otherapex",
3000 key: "otherapex.key",
3001 native_shared_libs: ["libfoo"],
3002 }
3003
3004 apex_key {
3005 name: "otherapex.key",
3006 public_key: "testkey.avbpubkey",
3007 private_key: "testkey.pem",
3008 }
3009
3010 cc_library {
3011 name: "libfoo",
3012 stl: "none",
3013 system_shared_libs: [],
3014 apex_available: ["otherapex"],
3015 }`)
3016
3017 // libbar is an indirect dep
3018 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3019 apex {
3020 name: "myapex",
3021 key: "myapex.key",
3022 native_shared_libs: ["libfoo"],
3023 }
3024
3025 apex_key {
3026 name: "myapex.key",
3027 public_key: "testkey.avbpubkey",
3028 private_key: "testkey.pem",
3029 }
3030
3031 apex {
3032 name: "otherapex",
3033 key: "otherapex.key",
3034 native_shared_libs: ["libfoo"],
3035 }
3036
3037 apex_key {
3038 name: "otherapex.key",
3039 public_key: "testkey.avbpubkey",
3040 private_key: "testkey.pem",
3041 }
3042
3043 cc_library {
3044 name: "libfoo",
3045 stl: "none",
3046 shared_libs: ["libbar"],
3047 system_shared_libs: [],
3048 apex_available: ["myapex", "otherapex"],
3049 }
3050
3051 cc_library {
3052 name: "libbar",
3053 stl: "none",
3054 system_shared_libs: [],
3055 apex_available: ["otherapex"],
3056 }`)
3057
3058 testApexError(t, "\"otherapex\" is not a valid module name", `
3059 apex {
3060 name: "myapex",
3061 key: "myapex.key",
3062 native_shared_libs: ["libfoo"],
3063 }
3064
3065 apex_key {
3066 name: "myapex.key",
3067 public_key: "testkey.avbpubkey",
3068 private_key: "testkey.pem",
3069 }
3070
3071 cc_library {
3072 name: "libfoo",
3073 stl: "none",
3074 system_shared_libs: [],
3075 apex_available: ["otherapex"],
3076 }`)
3077
3078 ctx, _ := testApex(t, `
3079 apex {
3080 name: "myapex",
3081 key: "myapex.key",
3082 native_shared_libs: ["libfoo", "libbar"],
3083 }
3084
3085 apex_key {
3086 name: "myapex.key",
3087 public_key: "testkey.avbpubkey",
3088 private_key: "testkey.pem",
3089 }
3090
3091 cc_library {
3092 name: "libfoo",
3093 stl: "none",
3094 system_shared_libs: [],
3095 apex_available: ["myapex"],
3096 }
3097
3098 cc_library {
3099 name: "libbar",
3100 stl: "none",
3101 system_shared_libs: [],
3102 apex_available: ["//apex_available:anyapex"],
3103 }`)
3104
3105 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003106 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3107 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3108 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3109 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003110
3111 ctx, _ = testApex(t, `
3112 apex {
3113 name: "myapex",
3114 key: "myapex.key",
3115 }
3116
3117 apex_key {
3118 name: "myapex.key",
3119 public_key: "testkey.avbpubkey",
3120 private_key: "testkey.pem",
3121 }
3122
3123 cc_library {
3124 name: "libfoo",
3125 stl: "none",
3126 system_shared_libs: [],
3127 apex_available: ["//apex_available:platform"],
3128 }`)
3129
3130 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003131 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3132 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003133
3134 ctx, _ = testApex(t, `
3135 apex {
3136 name: "myapex",
3137 key: "myapex.key",
3138 native_shared_libs: ["libfoo"],
3139 }
3140
3141 apex_key {
3142 name: "myapex.key",
3143 public_key: "testkey.avbpubkey",
3144 private_key: "testkey.pem",
3145 }
3146
3147 cc_library {
3148 name: "libfoo",
3149 stl: "none",
3150 system_shared_libs: [],
3151 apex_available: ["myapex"],
3152 static: {
3153 apex_available: ["//apex_available:platform"],
3154 },
3155 }`)
3156
3157 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08003158 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3159 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003160 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08003161 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3162 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003163}
3164
Jiyong Park5d790c32019-11-15 18:40:32 +09003165func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003166 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003167 apex {
3168 name: "myapex",
3169 key: "myapex.key",
3170 apps: ["app"],
3171 }
3172
3173 override_apex {
3174 name: "override_myapex",
3175 base: "myapex",
3176 apps: ["override_app"],
3177 }
3178
3179 apex_key {
3180 name: "myapex.key",
3181 public_key: "testkey.avbpubkey",
3182 private_key: "testkey.pem",
3183 }
3184
3185 android_app {
3186 name: "app",
3187 srcs: ["foo/bar/MyClass.java"],
3188 package_name: "foo",
3189 sdk_version: "none",
3190 system_modules: "none",
3191 }
3192
3193 override_android_app {
3194 name: "override_app",
3195 base: "app",
3196 package_name: "bar",
3197 }
3198 `)
3199
Jiyong Park317645e2019-12-05 13:20:58 +09003200 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3201 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3202 if originalVariant.GetOverriddenBy() != "" {
3203 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3204 }
3205 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3206 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3207 }
3208
Jiyong Park5d790c32019-11-15 18:40:32 +09003209 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3210 apexRule := module.Rule("apexRule")
3211 copyCmds := apexRule.Args["copy_commands"]
3212
3213 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3214 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003215
3216 apexBundle := module.Module().(*apexBundle)
3217 name := apexBundle.Name()
3218 if name != "override_myapex" {
3219 t.Errorf("name should be \"override_myapex\", but was %q", name)
3220 }
3221
3222 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3223 var builder strings.Builder
3224 data.Custom(&builder, name, "TARGET_", "", data)
3225 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003226 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003227 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3228 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
3229 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003230 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003231 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3232 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003233}
3234
Jooyung Han214bf372019-11-12 13:03:50 +09003235func TestLegacyAndroid10Support(t *testing.T) {
3236 ctx, _ := testApex(t, `
3237 apex {
3238 name: "myapex",
3239 key: "myapex.key",
3240 legacy_android10_support: true,
3241 }
3242
3243 apex_key {
3244 name: "myapex.key",
3245 public_key: "testkey.avbpubkey",
3246 private_key: "testkey.pem",
3247 }
3248 `)
3249
3250 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3251 args := module.Rule("apexRule").Args
3252 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
3253}
3254
Jiyong Park479321d2019-12-16 11:47:12 +09003255func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3256 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3257 apex {
3258 name: "myapex",
3259 key: "myapex.key",
3260 java_libs: ["myjar"],
3261 }
3262
3263 apex_key {
3264 name: "myapex.key",
3265 public_key: "testkey.avbpubkey",
3266 private_key: "testkey.pem",
3267 }
3268
3269 java_library {
3270 name: "myjar",
3271 srcs: ["foo/bar/MyClass.java"],
3272 sdk_version: "none",
3273 system_modules: "none",
3274 }
3275 `)
3276}
3277
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003278func TestMain(m *testing.M) {
3279 run := func() int {
3280 setUp()
3281 defer tearDown()
3282
3283 return m.Run()
3284 }
3285
3286 os.Exit(run())
3287}