blob: 6e547eb5fd4580d2893ec6e1737764d11b25a090 [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
Colin Cross98be1bb2019-12-13 20:41:13 -0800289 ctx.RegisterModuleType("cc_library_shared", cc.LibrarySharedFactory)
290 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
Paul Duffin77980a82019-12-19 16:01:36 +0000291 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800292 ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800293 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800294 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
295 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800296 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
297 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800298 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000299 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000300 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000301 java.RegisterAppBuildComponents(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800302
303 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800304 ctx.PreDepsMutators(RegisterPreDepsMutators)
305 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
306 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800307
308 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900309
Jooyung Han5c998b92019-06-27 11:30:33 +0900310 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900311}
312
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700313func setUp() {
314 var err error
315 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900316 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700317 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900318 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900319}
320
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700321func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900322 os.RemoveAll(buildDir)
323}
324
325// ensure that 'result' contains 'expected'
326func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900327 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900328 if !strings.Contains(result, expected) {
329 t.Errorf("%q is not found in %q", expected, result)
330 }
331}
332
333// ensures that 'result' does not contain 'notExpected'
334func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900335 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900336 if strings.Contains(result, notExpected) {
337 t.Errorf("%q is found in %q", notExpected, result)
338 }
339}
340
341func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900342 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900343 if !android.InList(expected, result) {
344 t.Errorf("%q is not found in %v", expected, result)
345 }
346}
347
348func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900349 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900350 if android.InList(notExpected, result) {
351 t.Errorf("%q is found in %v", notExpected, result)
352 }
353}
354
Jooyung Hane1633032019-08-01 17:41:43 +0900355func ensureListEmpty(t *testing.T, result []string) {
356 t.Helper()
357 if len(result) > 0 {
358 t.Errorf("%q is expected to be empty", result)
359 }
360}
361
Jiyong Park25fc6a92018-11-18 18:02:45 +0900362// Minimal test
363func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700364 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900365 apex_defaults {
366 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900367 manifest: ":myapex.manifest",
368 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900369 key: "myapex.key",
370 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800371 multilib: {
372 both: {
373 binaries: ["foo",],
374 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900375 },
Jiyong Park9e6c2422019-08-09 20:39:45 +0900376 java_libs: ["myjar", "myprebuiltjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900377 }
378
Jiyong Park30ca9372019-02-07 16:27:23 +0900379 apex {
380 name: "myapex",
381 defaults: ["myapex-defaults"],
382 }
383
Jiyong Park25fc6a92018-11-18 18:02:45 +0900384 apex_key {
385 name: "myapex.key",
386 public_key: "testkey.avbpubkey",
387 private_key: "testkey.pem",
388 }
389
Jiyong Park809bb722019-02-13 21:33:49 +0900390 filegroup {
391 name: "myapex.manifest",
392 srcs: ["apex_manifest.json"],
393 }
394
395 filegroup {
396 name: "myapex.androidmanifest",
397 srcs: ["AndroidManifest.xml"],
398 }
399
Jiyong Park25fc6a92018-11-18 18:02:45 +0900400 cc_library {
401 name: "mylib",
402 srcs: ["mylib.cpp"],
403 shared_libs: ["mylib2"],
404 system_shared_libs: [],
405 stl: "none",
406 }
407
Alex Light3d673592019-01-18 14:37:31 -0800408 cc_binary {
409 name: "foo",
410 srcs: ["mylib.cpp"],
411 compile_multilib: "both",
412 multilib: {
413 lib32: {
414 suffix: "32",
415 },
416 lib64: {
417 suffix: "64",
418 },
419 },
420 symlinks: ["foo_link_"],
421 symlink_preferred_arch: true,
422 system_shared_libs: [],
423 static_executable: true,
424 stl: "none",
425 }
426
Jiyong Park25fc6a92018-11-18 18:02:45 +0900427 cc_library {
428 name: "mylib2",
429 srcs: ["mylib.cpp"],
430 system_shared_libs: [],
431 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900432 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900433 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900434
435 java_library {
436 name: "myjar",
437 srcs: ["foo/bar/MyClass.java"],
438 sdk_version: "none",
439 system_modules: "none",
440 compile_dex: true,
441 static_libs: ["myotherjar"],
442 }
443
444 java_library {
445 name: "myotherjar",
446 srcs: ["foo/bar/MyClass.java"],
447 sdk_version: "none",
448 system_modules: "none",
449 compile_dex: true,
450 }
Jiyong Park9e6c2422019-08-09 20:39:45 +0900451
452 java_import {
453 name: "myprebuiltjar",
454 jars: ["prebuilt.jar"],
455 installable: true,
456 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900457 `)
458
Sundong Ahnabb64432019-10-22 13:58:29 +0900459 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900460
461 optFlags := apexRule.Args["opt_flags"]
462 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700463 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900464 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900465
Jiyong Park25fc6a92018-11-18 18:02:45 +0900466 copyCmds := apexRule.Args["copy_commands"]
467
468 // Ensure that main rule creates an output
469 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
470
471 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800472 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900473 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900474 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900475
476 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800477 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900478 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900479
480 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800481 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
482 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900483 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900484 ensureContains(t, copyCmds, "image.apex/javalib/myprebuiltjar.jar")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900485 // .. but not for java libs
486 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800487
Colin Cross7113d202019-11-20 16:39:12 -0800488 // Ensure that the platform variant ends with _shared or _common
489 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
490 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900491 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
492 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900493 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common")
Alex Light3d673592019-01-18 14:37:31 -0800494
495 // Ensure that all symlinks are present.
496 found_foo_link_64 := false
497 found_foo := false
498 for _, cmd := range strings.Split(copyCmds, " && ") {
499 if strings.HasPrefix(cmd, "ln -s foo64") {
500 if strings.HasSuffix(cmd, "bin/foo") {
501 found_foo = true
502 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
503 found_foo_link_64 = true
504 }
505 }
506 }
507 good := found_foo && found_foo_link_64
508 if !good {
509 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
510 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900511
Sundong Ahnabb64432019-10-22 13:58:29 +0900512 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700513 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700514 if len(noticeInputs) != 2 {
515 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900516 }
517 ensureListContains(t, noticeInputs, "NOTICE")
518 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800519}
520
Jooyung Hanf21c7972019-12-16 22:32:06 +0900521func TestDefaults(t *testing.T) {
522 ctx, _ := testApex(t, `
523 apex_defaults {
524 name: "myapex-defaults",
525 key: "myapex.key",
526 prebuilts: ["myetc"],
527 native_shared_libs: ["mylib"],
528 java_libs: ["myjar"],
529 apps: ["AppFoo"],
530 }
531
532 prebuilt_etc {
533 name: "myetc",
534 src: "myprebuilt",
535 }
536
537 apex {
538 name: "myapex",
539 defaults: ["myapex-defaults"],
540 }
541
542 apex_key {
543 name: "myapex.key",
544 public_key: "testkey.avbpubkey",
545 private_key: "testkey.pem",
546 }
547
548 cc_library {
549 name: "mylib",
550 system_shared_libs: [],
551 stl: "none",
552 }
553
554 java_library {
555 name: "myjar",
556 srcs: ["foo/bar/MyClass.java"],
557 sdk_version: "none",
558 system_modules: "none",
559 compile_dex: true,
560 }
561
562 android_app {
563 name: "AppFoo",
564 srcs: ["foo/bar/MyClass.java"],
565 sdk_version: "none",
566 system_modules: "none",
567 }
568 `)
569 ensureExactContents(t, ctx, "myapex", []string{
570 "etc/myetc",
571 "javalib/myjar.jar",
572 "lib64/mylib.so",
573 "app/AppFoo/AppFoo.apk",
574 })
575}
576
Jooyung Han01a3ee22019-11-02 02:52:25 +0900577func TestApexManifest(t *testing.T) {
578 ctx, _ := testApex(t, `
579 apex {
580 name: "myapex",
581 key: "myapex.key",
582 }
583
584 apex_key {
585 name: "myapex.key",
586 public_key: "testkey.avbpubkey",
587 private_key: "testkey.pem",
588 }
589 `)
590
591 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900592 args := module.Rule("apexRule").Args
593 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
594 t.Error("manifest should be apex_manifest.pb, but " + manifest)
595 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900596}
597
Alex Light5098a612018-11-29 17:12:15 -0800598func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700599 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800600 apex {
601 name: "myapex",
602 key: "myapex.key",
603 payload_type: "zip",
604 native_shared_libs: ["mylib"],
605 }
606
607 apex_key {
608 name: "myapex.key",
609 public_key: "testkey.avbpubkey",
610 private_key: "testkey.pem",
611 }
612
613 cc_library {
614 name: "mylib",
615 srcs: ["mylib.cpp"],
616 shared_libs: ["mylib2"],
617 system_shared_libs: [],
618 stl: "none",
619 }
620
621 cc_library {
622 name: "mylib2",
623 srcs: ["mylib.cpp"],
624 system_shared_libs: [],
625 stl: "none",
626 }
627 `)
628
Sundong Ahnabb64432019-10-22 13:58:29 +0900629 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800630 copyCmds := zipApexRule.Args["copy_commands"]
631
632 // Ensure that main rule creates an output
633 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
634
635 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800636 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800637
638 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800639 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800640
641 // Ensure that both direct and indirect deps are copied into apex
642 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
643 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900644}
645
646func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700647 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900648 apex {
649 name: "myapex",
650 key: "myapex.key",
651 native_shared_libs: ["mylib", "mylib3"],
652 }
653
654 apex_key {
655 name: "myapex.key",
656 public_key: "testkey.avbpubkey",
657 private_key: "testkey.pem",
658 }
659
660 cc_library {
661 name: "mylib",
662 srcs: ["mylib.cpp"],
663 shared_libs: ["mylib2", "mylib3"],
664 system_shared_libs: [],
665 stl: "none",
666 }
667
668 cc_library {
669 name: "mylib2",
670 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900671 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900672 system_shared_libs: [],
673 stl: "none",
674 stubs: {
675 versions: ["1", "2", "3"],
676 },
677 }
678
679 cc_library {
680 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900681 srcs: ["mylib.cpp"],
682 shared_libs: ["mylib4"],
683 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900684 stl: "none",
685 stubs: {
686 versions: ["10", "11", "12"],
687 },
688 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900689
690 cc_library {
691 name: "mylib4",
692 srcs: ["mylib.cpp"],
693 system_shared_libs: [],
694 stl: "none",
695 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900696 `)
697
Sundong Ahnabb64432019-10-22 13:58:29 +0900698 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900699 copyCmds := apexRule.Args["copy_commands"]
700
701 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800702 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900703
704 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800705 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900706
707 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800708 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900709
Colin Cross7113d202019-11-20 16:39:12 -0800710 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900711
712 // Ensure that mylib is linking with the latest version of stubs for mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800713 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900714 // ... and not linking to the non-stub (impl) variant of mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800715 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900716
717 // 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 -0800718 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900719 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800720 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900721
722 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800723 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900724 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900725
726 // Ensure that genstub is invoked with --apex
Colin Cross7113d202019-11-20 16:39:12 -0800727 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3_myapex").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900728
729 ensureExactContents(t, ctx, "myapex", []string{
730 "lib64/mylib.so",
731 "lib64/mylib3.so",
732 "lib64/mylib4.so",
733 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900734}
735
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900736func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700737 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900738 apex {
739 name: "myapex",
740 key: "myapex.key",
741 native_shared_libs: ["mylib"],
742 }
743
744 apex_key {
745 name: "myapex.key",
746 public_key: "testkey.avbpubkey",
747 private_key: "testkey.pem",
748 }
749
750 cc_library {
751 name: "mylib",
752 srcs: ["mylib.cpp"],
753 shared_libs: ["libfoo#10"],
754 system_shared_libs: [],
755 stl: "none",
756 }
757
758 cc_library {
759 name: "libfoo",
760 srcs: ["mylib.cpp"],
761 shared_libs: ["libbar"],
762 system_shared_libs: [],
763 stl: "none",
764 stubs: {
765 versions: ["10", "20", "30"],
766 },
767 }
768
769 cc_library {
770 name: "libbar",
771 srcs: ["mylib.cpp"],
772 system_shared_libs: [],
773 stl: "none",
774 }
775
776 `)
777
Sundong Ahnabb64432019-10-22 13:58:29 +0900778 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900779 copyCmds := apexRule.Args["copy_commands"]
780
781 // Ensure that direct non-stubs dep is always included
782 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
783
784 // Ensure that indirect stubs dep is not included
785 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
786
787 // Ensure that dependency of stubs is not included
788 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
789
Colin Cross7113d202019-11-20 16:39:12 -0800790 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900791
792 // Ensure that mylib is linking with version 10 of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800793 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900794 // ... and not linking to the non-stub (impl) variant of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800795 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900796
Colin Cross7113d202019-11-20 16:39:12 -0800797 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900798
799 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
800 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
801}
802
Jooyung Hand3639552019-08-09 12:57:43 +0900803func TestApexWithRuntimeLibsDependency(t *testing.T) {
804 /*
805 myapex
806 |
807 v (runtime_libs)
808 mylib ------+------> libfoo [provides stub]
809 |
810 `------> libbar
811 */
812 ctx, _ := testApex(t, `
813 apex {
814 name: "myapex",
815 key: "myapex.key",
816 native_shared_libs: ["mylib"],
817 }
818
819 apex_key {
820 name: "myapex.key",
821 public_key: "testkey.avbpubkey",
822 private_key: "testkey.pem",
823 }
824
825 cc_library {
826 name: "mylib",
827 srcs: ["mylib.cpp"],
828 runtime_libs: ["libfoo", "libbar"],
829 system_shared_libs: [],
830 stl: "none",
831 }
832
833 cc_library {
834 name: "libfoo",
835 srcs: ["mylib.cpp"],
836 system_shared_libs: [],
837 stl: "none",
838 stubs: {
839 versions: ["10", "20", "30"],
840 },
841 }
842
843 cc_library {
844 name: "libbar",
845 srcs: ["mylib.cpp"],
846 system_shared_libs: [],
847 stl: "none",
848 }
849
850 `)
851
Sundong Ahnabb64432019-10-22 13:58:29 +0900852 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900853 copyCmds := apexRule.Args["copy_commands"]
854
855 // Ensure that direct non-stubs dep is always included
856 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
857
858 // Ensure that indirect stubs dep is not included
859 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
860
861 // Ensure that runtime_libs dep in included
862 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
863
Sundong Ahnabb64432019-10-22 13:58:29 +0900864 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900865 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
866 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900867
868}
869
Jooyung Han9c80bae2019-08-20 17:30:57 +0900870func TestApexDependencyToLLNDK(t *testing.T) {
871 ctx, _ := testApex(t, `
872 apex {
873 name: "myapex",
874 key: "myapex.key",
875 use_vendor: true,
876 native_shared_libs: ["mylib"],
877 }
878
879 apex_key {
880 name: "myapex.key",
881 public_key: "testkey.avbpubkey",
882 private_key: "testkey.pem",
883 }
884
885 cc_library {
886 name: "mylib",
887 srcs: ["mylib.cpp"],
888 vendor_available: true,
889 shared_libs: ["libbar"],
890 system_shared_libs: [],
891 stl: "none",
892 }
893
894 cc_library {
895 name: "libbar",
896 srcs: ["mylib.cpp"],
897 system_shared_libs: [],
898 stl: "none",
899 }
900
901 llndk_library {
902 name: "libbar",
903 symbol_file: "",
904 }
Jooyung Handc782442019-11-01 03:14:38 +0900905 `, func(fs map[string][]byte, config android.Config) {
906 setUseVendorWhitelistForTest(config, []string{"myapex"})
907 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900908
Sundong Ahnabb64432019-10-22 13:58:29 +0900909 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900910 copyCmds := apexRule.Args["copy_commands"]
911
912 // Ensure that LLNDK dep is not included
913 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
914
Sundong Ahnabb64432019-10-22 13:58:29 +0900915 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900916 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900917
918 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900919 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900920
921}
922
Jiyong Park25fc6a92018-11-18 18:02:45 +0900923func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700924 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900925 apex {
926 name: "myapex",
927 key: "myapex.key",
928 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
929 }
930
931 apex_key {
932 name: "myapex.key",
933 public_key: "testkey.avbpubkey",
934 private_key: "testkey.pem",
935 }
936
937 cc_library {
938 name: "mylib",
939 srcs: ["mylib.cpp"],
940 shared_libs: ["libdl#27"],
941 stl: "none",
942 }
943
944 cc_library_shared {
945 name: "mylib_shared",
946 srcs: ["mylib.cpp"],
947 shared_libs: ["libdl#27"],
948 stl: "none",
949 }
950
951 cc_library {
952 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700953 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900954 nocrt: true,
955 system_shared_libs: [],
956 stl: "none",
957 stubs: {
958 versions: ["27", "28", "29"],
959 },
960 }
961
962 cc_library {
963 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700964 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900965 nocrt: true,
966 system_shared_libs: [],
967 stl: "none",
968 stubs: {
969 versions: ["27", "28", "29"],
970 },
971 }
972
973 cc_library {
974 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700975 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900976 nocrt: true,
977 system_shared_libs: [],
978 stl: "none",
979 stubs: {
980 versions: ["27", "28", "29"],
981 },
982 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900983
984 cc_library {
985 name: "libBootstrap",
986 srcs: ["mylib.cpp"],
987 stl: "none",
988 bootstrap: true,
989 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900990 `)
991
Sundong Ahnabb64432019-10-22 13:58:29 +0900992 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900993 copyCmds := apexRule.Args["copy_commands"]
994
995 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800996 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900997 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
998 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900999
1000 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001001 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001002
Colin Cross7113d202019-11-20 16:39:12 -08001003 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1004 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1005 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001006
1007 // For dependency to libc
1008 // Ensure that mylib is linking with the latest version of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001009 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001010 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001011 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001012 // ... Cflags from stub is correctly exported to mylib
1013 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1014 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1015
1016 // For dependency to libm
1017 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001018 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001019 // ... and not linking to the stub variant
Colin Cross7113d202019-11-20 16:39:12 -08001020 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001021 // ... and is not compiling with the stub
1022 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1023 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1024
1025 // For dependency to libdl
1026 // Ensure that mylib is linking with the specified version of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001027 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001028 // ... and not linking to the other versions of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001029 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28_myapex/libdl.so")
1030 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29_myapex/libdl.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, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001033 // ... Cflags from stub is correctly exported to mylib
1034 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1035 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001036
1037 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001038 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1039 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1040 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1041 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001042}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001043
1044func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001045 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001046 apex {
1047 name: "myapex",
1048 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001049 native_shared_libs: ["mylib"],
1050 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001051 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001052 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001053 }
1054
1055 apex_key {
1056 name: "myapex.key",
1057 public_key: "testkey.avbpubkey",
1058 private_key: "testkey.pem",
1059 }
1060
1061 prebuilt_etc {
1062 name: "myetc",
1063 src: "myprebuilt",
1064 sub_dir: "foo/bar",
1065 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001066
1067 cc_library {
1068 name: "mylib",
1069 srcs: ["mylib.cpp"],
1070 relative_install_path: "foo/bar",
1071 system_shared_libs: [],
1072 stl: "none",
1073 }
1074
1075 cc_binary {
1076 name: "mybin",
1077 srcs: ["mylib.cpp"],
1078 relative_install_path: "foo/bar",
1079 system_shared_libs: [],
1080 static_executable: true,
1081 stl: "none",
1082 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001083 `)
1084
Sundong Ahnabb64432019-10-22 13:58:29 +09001085 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001086 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1087
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001088 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001089 ensureListContains(t, dirs, "etc")
1090 ensureListContains(t, dirs, "etc/foo")
1091 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001092 ensureListContains(t, dirs, "lib64")
1093 ensureListContains(t, dirs, "lib64/foo")
1094 ensureListContains(t, dirs, "lib64/foo/bar")
1095 ensureListContains(t, dirs, "lib")
1096 ensureListContains(t, dirs, "lib/foo")
1097 ensureListContains(t, dirs, "lib/foo/bar")
1098
Jiyong Parkbd13e442019-03-15 18:10:35 +09001099 ensureListContains(t, dirs, "bin")
1100 ensureListContains(t, dirs, "bin/foo")
1101 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001102}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001103
1104func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001105 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001106 apex {
1107 name: "myapex",
1108 key: "myapex.key",
1109 native_shared_libs: ["mylib"],
1110 use_vendor: true,
1111 }
1112
1113 apex_key {
1114 name: "myapex.key",
1115 public_key: "testkey.avbpubkey",
1116 private_key: "testkey.pem",
1117 }
1118
1119 cc_library {
1120 name: "mylib",
1121 srcs: ["mylib.cpp"],
1122 shared_libs: ["mylib2"],
1123 system_shared_libs: [],
1124 vendor_available: true,
1125 stl: "none",
1126 }
1127
1128 cc_library {
1129 name: "mylib2",
1130 srcs: ["mylib.cpp"],
1131 system_shared_libs: [],
1132 vendor_available: true,
1133 stl: "none",
1134 }
Jooyung Handc782442019-11-01 03:14:38 +09001135 `, func(fs map[string][]byte, config android.Config) {
1136 setUseVendorWhitelistForTest(config, []string{"myapex"})
1137 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001138
1139 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001140 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001141 for _, implicit := range i.Implicits {
1142 inputsList = append(inputsList, implicit.String())
1143 }
1144 }
1145 inputsString := strings.Join(inputsList, " ")
1146
1147 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001148 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1149 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001150
1151 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001152 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1153 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001154}
Jiyong Park16e91a02018-12-20 18:18:08 +09001155
Jooyung Handc782442019-11-01 03:14:38 +09001156func TestUseVendorRestriction(t *testing.T) {
1157 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1158 apex {
1159 name: "myapex",
1160 key: "myapex.key",
1161 use_vendor: true,
1162 }
1163 apex_key {
1164 name: "myapex.key",
1165 public_key: "testkey.avbpubkey",
1166 private_key: "testkey.pem",
1167 }
1168 `, func(fs map[string][]byte, config android.Config) {
1169 setUseVendorWhitelistForTest(config, []string{""})
1170 })
1171 // no error with whitelist
1172 testApex(t, `
1173 apex {
1174 name: "myapex",
1175 key: "myapex.key",
1176 use_vendor: true,
1177 }
1178 apex_key {
1179 name: "myapex.key",
1180 public_key: "testkey.avbpubkey",
1181 private_key: "testkey.pem",
1182 }
1183 `, func(fs map[string][]byte, config android.Config) {
1184 setUseVendorWhitelistForTest(config, []string{"myapex"})
1185 })
1186}
1187
Jooyung Han5c998b92019-06-27 11:30:33 +09001188func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1189 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1190 apex {
1191 name: "myapex",
1192 key: "myapex.key",
1193 native_shared_libs: ["mylib"],
1194 use_vendor: true,
1195 }
1196
1197 apex_key {
1198 name: "myapex.key",
1199 public_key: "testkey.avbpubkey",
1200 private_key: "testkey.pem",
1201 }
1202
1203 cc_library {
1204 name: "mylib",
1205 srcs: ["mylib.cpp"],
1206 system_shared_libs: [],
1207 stl: "none",
1208 }
1209 `)
1210}
1211
Jiyong Park16e91a02018-12-20 18:18:08 +09001212func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001213 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001214 apex {
1215 name: "myapex",
1216 key: "myapex.key",
1217 native_shared_libs: ["mylib"],
1218 }
1219
1220 apex_key {
1221 name: "myapex.key",
1222 public_key: "testkey.avbpubkey",
1223 private_key: "testkey.pem",
1224 }
1225
1226 cc_library {
1227 name: "mylib",
1228 srcs: ["mylib.cpp"],
1229 system_shared_libs: [],
1230 stl: "none",
1231 stubs: {
1232 versions: ["1", "2", "3"],
1233 },
1234 }
1235
1236 cc_binary {
1237 name: "not_in_apex",
1238 srcs: ["mylib.cpp"],
1239 static_libs: ["mylib"],
1240 static_executable: true,
1241 system_shared_libs: [],
1242 stl: "none",
1243 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001244 `)
1245
Colin Cross7113d202019-11-20 16:39:12 -08001246 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001247
1248 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001249 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001250}
Jiyong Park9335a262018-12-24 11:31:58 +09001251
1252func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001253 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001254 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001255 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001256 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001257 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001258 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001259 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001260 }
1261
1262 cc_library {
1263 name: "mylib",
1264 srcs: ["mylib.cpp"],
1265 system_shared_libs: [],
1266 stl: "none",
1267 }
1268
1269 apex_key {
1270 name: "myapex.key",
1271 public_key: "testkey.avbpubkey",
1272 private_key: "testkey.pem",
1273 }
1274
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001275 android_app_certificate {
1276 name: "myapex.certificate",
1277 certificate: "testkey",
1278 }
1279
1280 android_app_certificate {
1281 name: "myapex.certificate.override",
1282 certificate: "testkey.override",
1283 }
1284
Jiyong Park9335a262018-12-24 11:31:58 +09001285 `)
1286
1287 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001288 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001289
1290 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1291 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1292 "vendor/foo/devkeys/testkey.avbpubkey")
1293 }
1294 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1295 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1296 "vendor/foo/devkeys/testkey.pem")
1297 }
1298
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001299 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001300 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001301 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001302 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001303 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001304 }
1305}
Jiyong Park58e364a2019-01-19 19:24:06 +09001306
Jooyung Hanf121a652019-12-17 14:30:11 +09001307func TestCertificate(t *testing.T) {
1308 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1309 ctx, _ := testApex(t, `
1310 apex {
1311 name: "myapex",
1312 key: "myapex.key",
1313 }
1314 apex_key {
1315 name: "myapex.key",
1316 public_key: "testkey.avbpubkey",
1317 private_key: "testkey.pem",
1318 }`)
1319 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1320 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1321 if actual := rule.Args["certificates"]; actual != expected {
1322 t.Errorf("certificates should be %q, not %q", expected, actual)
1323 }
1324 })
1325 t.Run("override when unspecified", func(t *testing.T) {
1326 ctx, _ := testApex(t, `
1327 apex {
1328 name: "myapex_keytest",
1329 key: "myapex.key",
1330 file_contexts: ":myapex-file_contexts",
1331 }
1332 apex_key {
1333 name: "myapex.key",
1334 public_key: "testkey.avbpubkey",
1335 private_key: "testkey.pem",
1336 }
1337 android_app_certificate {
1338 name: "myapex.certificate.override",
1339 certificate: "testkey.override",
1340 }`)
1341 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1342 expected := "testkey.override.x509.pem testkey.override.pk8"
1343 if actual := rule.Args["certificates"]; actual != expected {
1344 t.Errorf("certificates should be %q, not %q", expected, actual)
1345 }
1346 })
1347 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1348 ctx, _ := testApex(t, `
1349 apex {
1350 name: "myapex",
1351 key: "myapex.key",
1352 certificate: ":myapex.certificate",
1353 }
1354 apex_key {
1355 name: "myapex.key",
1356 public_key: "testkey.avbpubkey",
1357 private_key: "testkey.pem",
1358 }
1359 android_app_certificate {
1360 name: "myapex.certificate",
1361 certificate: "testkey",
1362 }`)
1363 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1364 expected := "testkey.x509.pem testkey.pk8"
1365 if actual := rule.Args["certificates"]; actual != expected {
1366 t.Errorf("certificates should be %q, not %q", expected, actual)
1367 }
1368 })
1369 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1370 ctx, _ := testApex(t, `
1371 apex {
1372 name: "myapex_keytest",
1373 key: "myapex.key",
1374 file_contexts: ":myapex-file_contexts",
1375 certificate: ":myapex.certificate",
1376 }
1377 apex_key {
1378 name: "myapex.key",
1379 public_key: "testkey.avbpubkey",
1380 private_key: "testkey.pem",
1381 }
1382 android_app_certificate {
1383 name: "myapex.certificate.override",
1384 certificate: "testkey.override",
1385 }`)
1386 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1387 expected := "testkey.override.x509.pem testkey.override.pk8"
1388 if actual := rule.Args["certificates"]; actual != expected {
1389 t.Errorf("certificates should be %q, not %q", expected, actual)
1390 }
1391 })
1392 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1393 ctx, _ := testApex(t, `
1394 apex {
1395 name: "myapex",
1396 key: "myapex.key",
1397 certificate: "testkey",
1398 }
1399 apex_key {
1400 name: "myapex.key",
1401 public_key: "testkey.avbpubkey",
1402 private_key: "testkey.pem",
1403 }`)
1404 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1405 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1406 if actual := rule.Args["certificates"]; actual != expected {
1407 t.Errorf("certificates should be %q, not %q", expected, actual)
1408 }
1409 })
1410 t.Run("override when specified as <name>", func(t *testing.T) {
1411 ctx, _ := testApex(t, `
1412 apex {
1413 name: "myapex_keytest",
1414 key: "myapex.key",
1415 file_contexts: ":myapex-file_contexts",
1416 certificate: "testkey",
1417 }
1418 apex_key {
1419 name: "myapex.key",
1420 public_key: "testkey.avbpubkey",
1421 private_key: "testkey.pem",
1422 }
1423 android_app_certificate {
1424 name: "myapex.certificate.override",
1425 certificate: "testkey.override",
1426 }`)
1427 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1428 expected := "testkey.override.x509.pem testkey.override.pk8"
1429 if actual := rule.Args["certificates"]; actual != expected {
1430 t.Errorf("certificates should be %q, not %q", expected, actual)
1431 }
1432 })
1433}
1434
Jiyong Park58e364a2019-01-19 19:24:06 +09001435func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001436 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001437 apex {
1438 name: "myapex",
1439 key: "myapex.key",
1440 native_shared_libs: ["mylib"],
1441 }
1442
1443 apex {
1444 name: "otherapex",
1445 key: "myapex.key",
1446 native_shared_libs: ["mylib"],
1447 }
1448
1449 apex_key {
1450 name: "myapex.key",
1451 public_key: "testkey.avbpubkey",
1452 private_key: "testkey.pem",
1453 }
1454
1455 cc_library {
1456 name: "mylib",
1457 srcs: ["mylib.cpp"],
1458 system_shared_libs: [],
1459 stl: "none",
1460 }
1461 `)
1462
Jooyung Han6b8459b2019-10-30 08:29:25 +09001463 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001464 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001465 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001466 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1467 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001468
Jooyung Han6b8459b2019-10-30 08:29:25 +09001469 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001470 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001471 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001472 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1473 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001474
Jooyung Han6b8459b2019-10-30 08:29:25 +09001475 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001476 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001477 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001478 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1479 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001480}
Jiyong Park7e636d02019-01-28 16:16:54 +09001481
1482func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001483 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001484 apex {
1485 name: "myapex",
1486 key: "myapex.key",
1487 native_shared_libs: ["mylib"],
1488 }
1489
1490 apex_key {
1491 name: "myapex.key",
1492 public_key: "testkey.avbpubkey",
1493 private_key: "testkey.pem",
1494 }
1495
1496 cc_library_headers {
1497 name: "mylib_headers",
1498 export_include_dirs: ["my_include"],
1499 system_shared_libs: [],
1500 stl: "none",
1501 }
1502
1503 cc_library {
1504 name: "mylib",
1505 srcs: ["mylib.cpp"],
1506 system_shared_libs: [],
1507 stl: "none",
1508 header_libs: ["mylib_headers"],
1509 export_header_lib_headers: ["mylib_headers"],
1510 stubs: {
1511 versions: ["1", "2", "3"],
1512 },
1513 }
1514
1515 cc_library {
1516 name: "otherlib",
1517 srcs: ["mylib.cpp"],
1518 system_shared_libs: [],
1519 stl: "none",
1520 shared_libs: ["mylib"],
1521 }
1522 `)
1523
Colin Cross7113d202019-11-20 16:39:12 -08001524 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001525
1526 // Ensure that the include path of the header lib is exported to 'otherlib'
1527 ensureContains(t, cFlags, "-Imy_include")
1528}
Alex Light9670d332019-01-29 18:07:33 -08001529
Jooyung Han31c470b2019-10-18 16:26:59 +09001530func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1531 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001532 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001533 copyCmds := apexRule.Args["copy_commands"]
1534 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001535 var failed bool
1536 var surplus []string
1537 filesMatched := make(map[string]bool)
1538 addContent := func(content string) {
1539 for _, expected := range files {
1540 if matched, _ := path.Match(expected, content); matched {
1541 filesMatched[expected] = true
1542 return
1543 }
1544 }
1545 surplus = append(surplus, content)
1546 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001547 for _, cmd := range strings.Split(copyCmds, "&&") {
1548 cmd = strings.TrimSpace(cmd)
1549 if cmd == "" {
1550 continue
1551 }
1552 terms := strings.Split(cmd, " ")
1553 switch terms[0] {
1554 case "mkdir":
1555 case "cp":
1556 if len(terms) != 3 {
1557 t.Fatal("copyCmds contains invalid cp command", cmd)
1558 }
1559 dst := terms[2]
1560 index := strings.Index(dst, imageApexDir)
1561 if index == -1 {
1562 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1563 }
1564 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001565 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001566 default:
1567 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1568 }
1569 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001570
Jooyung Han31c470b2019-10-18 16:26:59 +09001571 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001572 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001573 t.Log("surplus files", surplus)
1574 failed = true
1575 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001576
1577 if len(files) > len(filesMatched) {
1578 var missing []string
1579 for _, expected := range files {
1580 if !filesMatched[expected] {
1581 missing = append(missing, expected)
1582 }
1583 }
1584 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001585 t.Log("missing files", missing)
1586 failed = true
1587 }
1588 if failed {
1589 t.Fail()
1590 }
1591}
1592
Jooyung Han344d5432019-08-23 11:17:39 +09001593func TestVndkApexCurrent(t *testing.T) {
1594 ctx, _ := testApex(t, `
1595 apex_vndk {
1596 name: "myapex",
1597 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001598 }
1599
1600 apex_key {
1601 name: "myapex.key",
1602 public_key: "testkey.avbpubkey",
1603 private_key: "testkey.pem",
1604 }
1605
1606 cc_library {
1607 name: "libvndk",
1608 srcs: ["mylib.cpp"],
1609 vendor_available: true,
1610 vndk: {
1611 enabled: true,
1612 },
1613 system_shared_libs: [],
1614 stl: "none",
1615 }
1616
1617 cc_library {
1618 name: "libvndksp",
1619 srcs: ["mylib.cpp"],
1620 vendor_available: true,
1621 vndk: {
1622 enabled: true,
1623 support_system_process: true,
1624 },
1625 system_shared_libs: [],
1626 stl: "none",
1627 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001628 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001629
Jooyung Han31c470b2019-10-18 16:26:59 +09001630 ensureExactContents(t, ctx, "myapex", []string{
1631 "lib/libvndk.so",
1632 "lib/libvndksp.so",
1633 "lib64/libvndk.so",
1634 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001635 "etc/llndk.libraries.VER.txt",
1636 "etc/vndkcore.libraries.VER.txt",
1637 "etc/vndksp.libraries.VER.txt",
1638 "etc/vndkprivate.libraries.VER.txt",
1639 "etc/vndkcorevariant.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001640 })
Jooyung Han344d5432019-08-23 11:17:39 +09001641}
1642
1643func TestVndkApexWithPrebuilt(t *testing.T) {
1644 ctx, _ := testApex(t, `
1645 apex_vndk {
1646 name: "myapex",
1647 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001648 }
1649
1650 apex_key {
1651 name: "myapex.key",
1652 public_key: "testkey.avbpubkey",
1653 private_key: "testkey.pem",
1654 }
1655
1656 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001657 name: "libvndk",
1658 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001659 vendor_available: true,
1660 vndk: {
1661 enabled: true,
1662 },
1663 system_shared_libs: [],
1664 stl: "none",
1665 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001666
1667 cc_prebuilt_library_shared {
1668 name: "libvndk.arm",
1669 srcs: ["libvndk.arm.so"],
1670 vendor_available: true,
1671 vndk: {
1672 enabled: true,
1673 },
1674 enabled: false,
1675 arch: {
1676 arm: {
1677 enabled: true,
1678 },
1679 },
1680 system_shared_libs: [],
1681 stl: "none",
1682 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001683 `+vndkLibrariesTxtFiles("current"),
1684 withFiles(map[string][]byte{
1685 "libvndk.so": nil,
1686 "libvndk.arm.so": nil,
1687 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001688
Jooyung Han31c470b2019-10-18 16:26:59 +09001689 ensureExactContents(t, ctx, "myapex", []string{
1690 "lib/libvndk.so",
1691 "lib/libvndk.arm.so",
1692 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001693 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001694 })
Jooyung Han344d5432019-08-23 11:17:39 +09001695}
1696
Jooyung Han39edb6c2019-11-06 16:53:07 +09001697func vndkLibrariesTxtFiles(vers ...string) (result string) {
1698 for _, v := range vers {
1699 if v == "current" {
1700 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkcorevariant"} {
1701 result += `
1702 vndk_libraries_txt {
1703 name: "` + txt + `.libraries.txt",
1704 }
1705 `
1706 }
1707 } else {
1708 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1709 result += `
1710 prebuilt_etc {
1711 name: "` + txt + `.libraries.` + v + `.txt",
1712 src: "dummy.txt",
1713 }
1714 `
1715 }
1716 }
1717 }
1718 return
1719}
1720
Jooyung Han344d5432019-08-23 11:17:39 +09001721func TestVndkApexVersion(t *testing.T) {
1722 ctx, _ := testApex(t, `
1723 apex_vndk {
1724 name: "myapex_v27",
1725 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001726 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001727 vndk_version: "27",
1728 }
1729
1730 apex_key {
1731 name: "myapex.key",
1732 public_key: "testkey.avbpubkey",
1733 private_key: "testkey.pem",
1734 }
1735
Jooyung Han31c470b2019-10-18 16:26:59 +09001736 vndk_prebuilt_shared {
1737 name: "libvndk27",
1738 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001739 vendor_available: true,
1740 vndk: {
1741 enabled: true,
1742 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001743 target_arch: "arm64",
1744 arch: {
1745 arm: {
1746 srcs: ["libvndk27_arm.so"],
1747 },
1748 arm64: {
1749 srcs: ["libvndk27_arm64.so"],
1750 },
1751 },
Jooyung Han344d5432019-08-23 11:17:39 +09001752 }
1753
1754 vndk_prebuilt_shared {
1755 name: "libvndk27",
1756 version: "27",
1757 vendor_available: true,
1758 vndk: {
1759 enabled: true,
1760 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001761 target_arch: "x86_64",
1762 arch: {
1763 x86: {
1764 srcs: ["libvndk27_x86.so"],
1765 },
1766 x86_64: {
1767 srcs: ["libvndk27_x86_64.so"],
1768 },
1769 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001770 }
1771 `+vndkLibrariesTxtFiles("27"),
1772 withFiles(map[string][]byte{
1773 "libvndk27_arm.so": nil,
1774 "libvndk27_arm64.so": nil,
1775 "libvndk27_x86.so": nil,
1776 "libvndk27_x86_64.so": nil,
1777 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001778
Jooyung Han31c470b2019-10-18 16:26:59 +09001779 ensureExactContents(t, ctx, "myapex_v27", []string{
1780 "lib/libvndk27_arm.so",
1781 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001782 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001783 })
Jooyung Han344d5432019-08-23 11:17:39 +09001784}
1785
1786func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1787 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1788 apex_vndk {
1789 name: "myapex_v27",
1790 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001791 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001792 vndk_version: "27",
1793 }
1794 apex_vndk {
1795 name: "myapex_v27_other",
1796 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001797 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001798 vndk_version: "27",
1799 }
1800
1801 apex_key {
1802 name: "myapex.key",
1803 public_key: "testkey.avbpubkey",
1804 private_key: "testkey.pem",
1805 }
1806
1807 cc_library {
1808 name: "libvndk",
1809 srcs: ["mylib.cpp"],
1810 vendor_available: true,
1811 vndk: {
1812 enabled: true,
1813 },
1814 system_shared_libs: [],
1815 stl: "none",
1816 }
1817
1818 vndk_prebuilt_shared {
1819 name: "libvndk",
1820 version: "27",
1821 vendor_available: true,
1822 vndk: {
1823 enabled: true,
1824 },
1825 srcs: ["libvndk.so"],
1826 }
1827 `, withFiles(map[string][]byte{
1828 "libvndk.so": nil,
1829 }))
1830}
1831
Jooyung Han90eee022019-10-01 20:02:42 +09001832func TestVndkApexNameRule(t *testing.T) {
1833 ctx, _ := testApex(t, `
1834 apex_vndk {
1835 name: "myapex",
1836 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001837 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001838 }
1839 apex_vndk {
1840 name: "myapex_v28",
1841 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001842 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001843 vndk_version: "28",
1844 }
1845 apex_key {
1846 name: "myapex.key",
1847 public_key: "testkey.avbpubkey",
1848 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001849 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001850
1851 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001852 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001853 actual := proptools.String(bundle.properties.Apex_name)
1854 if !reflect.DeepEqual(actual, expected) {
1855 t.Errorf("Got '%v', expected '%v'", actual, expected)
1856 }
1857 }
1858
1859 assertApexName("com.android.vndk.vVER", "myapex")
1860 assertApexName("com.android.vndk.v28", "myapex_v28")
1861}
1862
Jooyung Han344d5432019-08-23 11:17:39 +09001863func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1864 ctx, _ := testApex(t, `
1865 apex_vndk {
1866 name: "myapex",
1867 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001868 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001869 }
1870
1871 apex_key {
1872 name: "myapex.key",
1873 public_key: "testkey.avbpubkey",
1874 private_key: "testkey.pem",
1875 }
1876
1877 cc_library {
1878 name: "libvndk",
1879 srcs: ["mylib.cpp"],
1880 vendor_available: true,
1881 native_bridge_supported: true,
1882 host_supported: true,
1883 vndk: {
1884 enabled: true,
1885 },
1886 system_shared_libs: [],
1887 stl: "none",
1888 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001889 `+vndkLibrariesTxtFiles("current"),
1890 withTargets(map[android.OsType][]android.Target{
1891 android.Android: []android.Target{
1892 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1893 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1894 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1895 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1896 },
1897 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001898
Jooyung Han31c470b2019-10-18 16:26:59 +09001899 ensureExactContents(t, ctx, "myapex", []string{
1900 "lib/libvndk.so",
1901 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001902 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001903 })
Jooyung Han344d5432019-08-23 11:17:39 +09001904}
1905
1906func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1907 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1908 apex_vndk {
1909 name: "myapex",
1910 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001911 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001912 native_bridge_supported: true,
1913 }
1914
1915 apex_key {
1916 name: "myapex.key",
1917 public_key: "testkey.avbpubkey",
1918 private_key: "testkey.pem",
1919 }
1920
1921 cc_library {
1922 name: "libvndk",
1923 srcs: ["mylib.cpp"],
1924 vendor_available: true,
1925 native_bridge_supported: true,
1926 host_supported: true,
1927 vndk: {
1928 enabled: true,
1929 },
1930 system_shared_libs: [],
1931 stl: "none",
1932 }
1933 `)
1934}
1935
Jooyung Han31c470b2019-10-18 16:26:59 +09001936func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001937 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001938 apex_vndk {
1939 name: "myapex_v27",
1940 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001941 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001942 vndk_version: "27",
1943 }
1944
1945 apex_key {
1946 name: "myapex.key",
1947 public_key: "testkey.avbpubkey",
1948 private_key: "testkey.pem",
1949 }
1950
1951 vndk_prebuilt_shared {
1952 name: "libvndk27",
1953 version: "27",
1954 target_arch: "arm",
1955 vendor_available: true,
1956 vndk: {
1957 enabled: true,
1958 },
1959 arch: {
1960 arm: {
1961 srcs: ["libvndk27.so"],
1962 }
1963 },
1964 }
1965
1966 vndk_prebuilt_shared {
1967 name: "libvndk27",
1968 version: "27",
1969 target_arch: "arm",
1970 binder32bit: true,
1971 vendor_available: true,
1972 vndk: {
1973 enabled: true,
1974 },
1975 arch: {
1976 arm: {
1977 srcs: ["libvndk27binder32.so"],
1978 }
1979 },
1980 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001981 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001982 withFiles(map[string][]byte{
1983 "libvndk27.so": nil,
1984 "libvndk27binder32.so": nil,
1985 }),
1986 withBinder32bit,
1987 withTargets(map[android.OsType][]android.Target{
1988 android.Android: []android.Target{
1989 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1990 },
1991 }),
1992 )
1993
1994 ensureExactContents(t, ctx, "myapex_v27", []string{
1995 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001996 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001997 })
1998}
1999
Jooyung Hane1633032019-08-01 17:41:43 +09002000func TestDependenciesInApexManifest(t *testing.T) {
2001 ctx, _ := testApex(t, `
2002 apex {
2003 name: "myapex_nodep",
2004 key: "myapex.key",
2005 native_shared_libs: ["lib_nodep"],
2006 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002007 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002008 }
2009
2010 apex {
2011 name: "myapex_dep",
2012 key: "myapex.key",
2013 native_shared_libs: ["lib_dep"],
2014 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002015 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002016 }
2017
2018 apex {
2019 name: "myapex_provider",
2020 key: "myapex.key",
2021 native_shared_libs: ["libfoo"],
2022 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002023 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002024 }
2025
2026 apex {
2027 name: "myapex_selfcontained",
2028 key: "myapex.key",
2029 native_shared_libs: ["lib_dep", "libfoo"],
2030 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002031 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002032 }
2033
2034 apex_key {
2035 name: "myapex.key",
2036 public_key: "testkey.avbpubkey",
2037 private_key: "testkey.pem",
2038 }
2039
2040 cc_library {
2041 name: "lib_nodep",
2042 srcs: ["mylib.cpp"],
2043 system_shared_libs: [],
2044 stl: "none",
2045 }
2046
2047 cc_library {
2048 name: "lib_dep",
2049 srcs: ["mylib.cpp"],
2050 shared_libs: ["libfoo"],
2051 system_shared_libs: [],
2052 stl: "none",
2053 }
2054
2055 cc_library {
2056 name: "libfoo",
2057 srcs: ["mytest.cpp"],
2058 stubs: {
2059 versions: ["1"],
2060 },
2061 system_shared_libs: [],
2062 stl: "none",
2063 }
2064 `)
2065
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002066 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002067 var provideNativeLibs, requireNativeLibs []string
2068
Sundong Ahnabb64432019-10-22 13:58:29 +09002069 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002070 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2071 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002072 ensureListEmpty(t, provideNativeLibs)
2073 ensureListEmpty(t, requireNativeLibs)
2074
Sundong Ahnabb64432019-10-22 13:58:29 +09002075 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002076 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2077 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002078 ensureListEmpty(t, provideNativeLibs)
2079 ensureListContains(t, requireNativeLibs, "libfoo.so")
2080
Sundong Ahnabb64432019-10-22 13:58:29 +09002081 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002082 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2083 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002084 ensureListContains(t, provideNativeLibs, "libfoo.so")
2085 ensureListEmpty(t, requireNativeLibs)
2086
Sundong Ahnabb64432019-10-22 13:58:29 +09002087 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002088 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2089 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002090 ensureListContains(t, provideNativeLibs, "libfoo.so")
2091 ensureListEmpty(t, requireNativeLibs)
2092}
2093
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002094func TestApexName(t *testing.T) {
2095 ctx, _ := testApex(t, `
2096 apex {
2097 name: "myapex",
2098 key: "myapex.key",
2099 apex_name: "com.android.myapex",
2100 }
2101
2102 apex_key {
2103 name: "myapex.key",
2104 public_key: "testkey.avbpubkey",
2105 private_key: "testkey.pem",
2106 }
2107 `)
2108
Sundong Ahnabb64432019-10-22 13:58:29 +09002109 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002110 apexManifestRule := module.Rule("apexManifestRule")
2111 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2112 apexRule := module.Rule("apexRule")
2113 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
2114}
2115
Alex Light0851b882019-02-07 13:20:53 -08002116func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002117 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002118 apex {
2119 name: "myapex",
2120 key: "myapex.key",
2121 native_shared_libs: ["mylib_common"],
2122 }
2123
2124 apex_key {
2125 name: "myapex.key",
2126 public_key: "testkey.avbpubkey",
2127 private_key: "testkey.pem",
2128 }
2129
2130 cc_library {
2131 name: "mylib_common",
2132 srcs: ["mylib.cpp"],
2133 system_shared_libs: [],
2134 stl: "none",
2135 }
2136 `)
2137
Sundong Ahnabb64432019-10-22 13:58:29 +09002138 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002139 apexRule := module.Rule("apexRule")
2140 copyCmds := apexRule.Args["copy_commands"]
2141
2142 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2143 t.Log("Apex was a test apex!")
2144 t.Fail()
2145 }
2146 // Ensure that main rule creates an output
2147 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2148
2149 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002150 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002151
2152 // Ensure that both direct and indirect deps are copied into apex
2153 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2154
Colin Cross7113d202019-11-20 16:39:12 -08002155 // Ensure that the platform variant ends with _shared
2156 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002157
2158 if !android.InAnyApex("mylib_common") {
2159 t.Log("Found mylib_common not in any apex!")
2160 t.Fail()
2161 }
2162}
2163
2164func TestTestApex(t *testing.T) {
2165 if android.InAnyApex("mylib_common_test") {
2166 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!")
2167 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002168 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002169 apex_test {
2170 name: "myapex",
2171 key: "myapex.key",
2172 native_shared_libs: ["mylib_common_test"],
2173 }
2174
2175 apex_key {
2176 name: "myapex.key",
2177 public_key: "testkey.avbpubkey",
2178 private_key: "testkey.pem",
2179 }
2180
2181 cc_library {
2182 name: "mylib_common_test",
2183 srcs: ["mylib.cpp"],
2184 system_shared_libs: [],
2185 stl: "none",
2186 }
2187 `)
2188
Sundong Ahnabb64432019-10-22 13:58:29 +09002189 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002190 apexRule := module.Rule("apexRule")
2191 copyCmds := apexRule.Args["copy_commands"]
2192
2193 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2194 t.Log("Apex was not a test apex!")
2195 t.Fail()
2196 }
2197 // Ensure that main rule creates an output
2198 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2199
2200 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002201 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002202
2203 // Ensure that both direct and indirect deps are copied into apex
2204 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2205
Colin Cross7113d202019-11-20 16:39:12 -08002206 // Ensure that the platform variant ends with _shared
2207 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002208
2209 if android.InAnyApex("mylib_common_test") {
2210 t.Log("Found mylib_common_test in some apex!")
2211 t.Fail()
2212 }
2213}
2214
Alex Light9670d332019-01-29 18:07:33 -08002215func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002216 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002217 apex {
2218 name: "myapex",
2219 key: "myapex.key",
2220 multilib: {
2221 first: {
2222 native_shared_libs: ["mylib_common"],
2223 }
2224 },
2225 target: {
2226 android: {
2227 multilib: {
2228 first: {
2229 native_shared_libs: ["mylib"],
2230 }
2231 }
2232 },
2233 host: {
2234 multilib: {
2235 first: {
2236 native_shared_libs: ["mylib2"],
2237 }
2238 }
2239 }
2240 }
2241 }
2242
2243 apex_key {
2244 name: "myapex.key",
2245 public_key: "testkey.avbpubkey",
2246 private_key: "testkey.pem",
2247 }
2248
2249 cc_library {
2250 name: "mylib",
2251 srcs: ["mylib.cpp"],
2252 system_shared_libs: [],
2253 stl: "none",
2254 }
2255
2256 cc_library {
2257 name: "mylib_common",
2258 srcs: ["mylib.cpp"],
2259 system_shared_libs: [],
2260 stl: "none",
2261 compile_multilib: "first",
2262 }
2263
2264 cc_library {
2265 name: "mylib2",
2266 srcs: ["mylib.cpp"],
2267 system_shared_libs: [],
2268 stl: "none",
2269 compile_multilib: "first",
2270 }
2271 `)
2272
Sundong Ahnabb64432019-10-22 13:58:29 +09002273 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002274 copyCmds := apexRule.Args["copy_commands"]
2275
2276 // Ensure that main rule creates an output
2277 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2278
2279 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002280 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2281 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2282 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002283
2284 // Ensure that both direct and indirect deps are copied into apex
2285 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2286 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2287 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2288
Colin Cross7113d202019-11-20 16:39:12 -08002289 // Ensure that the platform variant ends with _shared
2290 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2291 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2292 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002293}
Jiyong Park04480cf2019-02-06 00:16:29 +09002294
2295func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002296 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002297 apex {
2298 name: "myapex",
2299 key: "myapex.key",
2300 binaries: ["myscript"],
2301 }
2302
2303 apex_key {
2304 name: "myapex.key",
2305 public_key: "testkey.avbpubkey",
2306 private_key: "testkey.pem",
2307 }
2308
2309 sh_binary {
2310 name: "myscript",
2311 src: "mylib.cpp",
2312 filename: "myscript.sh",
2313 sub_dir: "script",
2314 }
2315 `)
2316
Sundong Ahnabb64432019-10-22 13:58:29 +09002317 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002318 copyCmds := apexRule.Args["copy_commands"]
2319
2320 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2321}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002322
Jooyung Han91df2082019-11-20 01:49:42 +09002323func TestApexInVariousPartition(t *testing.T) {
2324 testcases := []struct {
2325 propName, parition, flattenedPartition string
2326 }{
2327 {"", "system", "system_ext"},
2328 {"product_specific: true", "product", "product"},
2329 {"soc_specific: true", "vendor", "vendor"},
2330 {"proprietary: true", "vendor", "vendor"},
2331 {"vendor: true", "vendor", "vendor"},
2332 {"system_ext_specific: true", "system_ext", "system_ext"},
2333 }
2334 for _, tc := range testcases {
2335 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2336 ctx, _ := testApex(t, `
2337 apex {
2338 name: "myapex",
2339 key: "myapex.key",
2340 `+tc.propName+`
2341 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002342
Jooyung Han91df2082019-11-20 01:49:42 +09002343 apex_key {
2344 name: "myapex.key",
2345 public_key: "testkey.avbpubkey",
2346 private_key: "testkey.pem",
2347 }
2348 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002349
Jooyung Han91df2082019-11-20 01:49:42 +09002350 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2351 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2352 actual := apex.installDir.String()
2353 if actual != expected {
2354 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2355 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002356
Jooyung Han91df2082019-11-20 01:49:42 +09002357 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2358 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2359 actual = flattened.installDir.String()
2360 if actual != expected {
2361 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2362 }
2363 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002364 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002365}
Jiyong Park67882562019-03-21 01:11:21 +09002366
Jooyung Han54aca7b2019-11-20 02:26:02 +09002367func TestFileContexts(t *testing.T) {
2368 ctx, _ := testApex(t, `
2369 apex {
2370 name: "myapex",
2371 key: "myapex.key",
2372 }
2373
2374 apex_key {
2375 name: "myapex.key",
2376 public_key: "testkey.avbpubkey",
2377 private_key: "testkey.pem",
2378 }
2379 `)
2380 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2381 apexRule := module.Rule("apexRule")
2382 actual := apexRule.Args["file_contexts"]
2383 expected := "system/sepolicy/apex/myapex-file_contexts"
2384 if actual != expected {
2385 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2386 }
2387
2388 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2389 apex {
2390 name: "myapex",
2391 key: "myapex.key",
2392 file_contexts: "my_own_file_contexts",
2393 }
2394
2395 apex_key {
2396 name: "myapex.key",
2397 public_key: "testkey.avbpubkey",
2398 private_key: "testkey.pem",
2399 }
2400 `, withFiles(map[string][]byte{
2401 "my_own_file_contexts": nil,
2402 }))
2403
2404 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2405 apex {
2406 name: "myapex",
2407 key: "myapex.key",
2408 product_specific: true,
2409 file_contexts: "product_specific_file_contexts",
2410 }
2411
2412 apex_key {
2413 name: "myapex.key",
2414 public_key: "testkey.avbpubkey",
2415 private_key: "testkey.pem",
2416 }
2417 `)
2418
2419 ctx, _ = testApex(t, `
2420 apex {
2421 name: "myapex",
2422 key: "myapex.key",
2423 product_specific: true,
2424 file_contexts: "product_specific_file_contexts",
2425 }
2426
2427 apex_key {
2428 name: "myapex.key",
2429 public_key: "testkey.avbpubkey",
2430 private_key: "testkey.pem",
2431 }
2432 `, withFiles(map[string][]byte{
2433 "product_specific_file_contexts": nil,
2434 }))
2435 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2436 apexRule = module.Rule("apexRule")
2437 actual = apexRule.Args["file_contexts"]
2438 expected = "product_specific_file_contexts"
2439 if actual != expected {
2440 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2441 }
2442
2443 ctx, _ = testApex(t, `
2444 apex {
2445 name: "myapex",
2446 key: "myapex.key",
2447 product_specific: true,
2448 file_contexts: ":my-file-contexts",
2449 }
2450
2451 apex_key {
2452 name: "myapex.key",
2453 public_key: "testkey.avbpubkey",
2454 private_key: "testkey.pem",
2455 }
2456
2457 filegroup {
2458 name: "my-file-contexts",
2459 srcs: ["product_specific_file_contexts"],
2460 }
2461 `, withFiles(map[string][]byte{
2462 "product_specific_file_contexts": nil,
2463 }))
2464 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2465 apexRule = module.Rule("apexRule")
2466 actual = apexRule.Args["file_contexts"]
2467 expected = "product_specific_file_contexts"
2468 if actual != expected {
2469 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2470 }
2471}
2472
Jiyong Park67882562019-03-21 01:11:21 +09002473func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002474 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002475 apex_key {
2476 name: "myapex.key",
2477 public_key: ":my.avbpubkey",
2478 private_key: ":my.pem",
2479 product_specific: true,
2480 }
2481
2482 filegroup {
2483 name: "my.avbpubkey",
2484 srcs: ["testkey2.avbpubkey"],
2485 }
2486
2487 filegroup {
2488 name: "my.pem",
2489 srcs: ["testkey2.pem"],
2490 }
2491 `)
2492
2493 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2494 expected_pubkey := "testkey2.avbpubkey"
2495 actual_pubkey := apex_key.public_key_file.String()
2496 if actual_pubkey != expected_pubkey {
2497 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2498 }
2499 expected_privkey := "testkey2.pem"
2500 actual_privkey := apex_key.private_key_file.String()
2501 if actual_privkey != expected_privkey {
2502 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2503 }
2504}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002505
2506func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002507 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002508 prebuilt_apex {
2509 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002510 arch: {
2511 arm64: {
2512 src: "myapex-arm64.apex",
2513 },
2514 arm: {
2515 src: "myapex-arm.apex",
2516 },
2517 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002518 }
2519 `)
2520
2521 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2522
Jiyong Parkc95714e2019-03-29 14:23:10 +09002523 expectedInput := "myapex-arm64.apex"
2524 if prebuilt.inputApex.String() != expectedInput {
2525 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2526 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002527}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002528
2529func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002530 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002531 prebuilt_apex {
2532 name: "myapex",
2533 src: "myapex-arm.apex",
2534 filename: "notmyapex.apex",
2535 }
2536 `)
2537
2538 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2539
2540 expected := "notmyapex.apex"
2541 if p.installFilename != expected {
2542 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2543 }
2544}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002545
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002546func TestPrebuiltOverrides(t *testing.T) {
2547 ctx, config := testApex(t, `
2548 prebuilt_apex {
2549 name: "myapex.prebuilt",
2550 src: "myapex-arm.apex",
2551 overrides: [
2552 "myapex",
2553 ],
2554 }
2555 `)
2556
2557 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2558
2559 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002560 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002561 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002562 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002563 }
2564}
2565
Roland Levillain630846d2019-06-26 12:48:34 +01002566func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002567 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002568 apex_test {
2569 name: "myapex",
2570 key: "myapex.key",
2571 tests: [
2572 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002573 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002574 ],
2575 }
2576
2577 apex_key {
2578 name: "myapex.key",
2579 public_key: "testkey.avbpubkey",
2580 private_key: "testkey.pem",
2581 }
2582
2583 cc_test {
2584 name: "mytest",
2585 gtest: false,
2586 srcs: ["mytest.cpp"],
2587 relative_install_path: "test",
2588 system_shared_libs: [],
2589 static_executable: true,
2590 stl: "none",
2591 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002592
2593 cc_test {
2594 name: "mytests",
2595 gtest: false,
2596 srcs: [
2597 "mytest1.cpp",
2598 "mytest2.cpp",
2599 "mytest3.cpp",
2600 ],
2601 test_per_src: true,
2602 relative_install_path: "test",
2603 system_shared_libs: [],
2604 static_executable: true,
2605 stl: "none",
2606 }
Roland Levillain630846d2019-06-26 12:48:34 +01002607 `)
2608
Sundong Ahnabb64432019-10-22 13:58:29 +09002609 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002610 copyCmds := apexRule.Args["copy_commands"]
2611
2612 // Ensure that test dep is copied into apex.
2613 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002614
2615 // Ensure that test deps built with `test_per_src` are copied into apex.
2616 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2617 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2618 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002619
2620 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002621 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002622 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2623 name := apexBundle.BaseModuleName()
2624 prefix := "TARGET_"
2625 var builder strings.Builder
2626 data.Custom(&builder, name, prefix, "", data)
2627 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002628 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2629 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2630 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2631 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002632 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002633 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002634 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002635}
2636
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002637func TestInstallExtraFlattenedApexes(t *testing.T) {
2638 ctx, config := testApex(t, `
2639 apex {
2640 name: "myapex",
2641 key: "myapex.key",
2642 }
2643 apex_key {
2644 name: "myapex.key",
2645 public_key: "testkey.avbpubkey",
2646 private_key: "testkey.pem",
2647 }
2648 `, func(fs map[string][]byte, config android.Config) {
2649 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2650 })
2651 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2652 ensureListContains(t, ab.externalDeps, "myapex.flattened")
2653 mk := android.AndroidMkDataForTest(t, config, "", ab)
2654 var builder strings.Builder
2655 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2656 androidMk := builder.String()
2657 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2658}
2659
Jooyung Han5c998b92019-06-27 11:30:33 +09002660func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002661 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002662 apex {
2663 name: "myapex",
2664 key: "myapex.key",
2665 native_shared_libs: ["mylib"],
2666 uses: ["commonapex"],
2667 }
2668
2669 apex {
2670 name: "commonapex",
2671 key: "myapex.key",
2672 native_shared_libs: ["libcommon"],
2673 provide_cpp_shared_libs: true,
2674 }
2675
2676 apex_key {
2677 name: "myapex.key",
2678 public_key: "testkey.avbpubkey",
2679 private_key: "testkey.pem",
2680 }
2681
2682 cc_library {
2683 name: "mylib",
2684 srcs: ["mylib.cpp"],
2685 shared_libs: ["libcommon"],
2686 system_shared_libs: [],
2687 stl: "none",
2688 }
2689
2690 cc_library {
2691 name: "libcommon",
2692 srcs: ["mylib_common.cpp"],
2693 system_shared_libs: [],
2694 stl: "none",
2695 }
2696 `)
2697
Sundong Ahnabb64432019-10-22 13:58:29 +09002698 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002699 apexRule1 := module1.Rule("apexRule")
2700 copyCmds1 := apexRule1.Args["copy_commands"]
2701
Sundong Ahnabb64432019-10-22 13:58:29 +09002702 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002703 apexRule2 := module2.Rule("apexRule")
2704 copyCmds2 := apexRule2.Args["copy_commands"]
2705
Colin Cross7113d202019-11-20 16:39:12 -08002706 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2707 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002708 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2709 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2710 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2711}
2712
2713func TestApexUsesFailsIfNotProvided(t *testing.T) {
2714 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2715 apex {
2716 name: "myapex",
2717 key: "myapex.key",
2718 uses: ["commonapex"],
2719 }
2720
2721 apex {
2722 name: "commonapex",
2723 key: "myapex.key",
2724 }
2725
2726 apex_key {
2727 name: "myapex.key",
2728 public_key: "testkey.avbpubkey",
2729 private_key: "testkey.pem",
2730 }
2731 `)
2732 testApexError(t, `uses: "commonapex" is not a provider`, `
2733 apex {
2734 name: "myapex",
2735 key: "myapex.key",
2736 uses: ["commonapex"],
2737 }
2738
2739 cc_library {
2740 name: "commonapex",
2741 system_shared_libs: [],
2742 stl: "none",
2743 }
2744
2745 apex_key {
2746 name: "myapex.key",
2747 public_key: "testkey.avbpubkey",
2748 private_key: "testkey.pem",
2749 }
2750 `)
2751}
2752
2753func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2754 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2755 apex {
2756 name: "myapex",
2757 key: "myapex.key",
2758 use_vendor: true,
2759 uses: ["commonapex"],
2760 }
2761
2762 apex {
2763 name: "commonapex",
2764 key: "myapex.key",
2765 provide_cpp_shared_libs: true,
2766 }
2767
2768 apex_key {
2769 name: "myapex.key",
2770 public_key: "testkey.avbpubkey",
2771 private_key: "testkey.pem",
2772 }
Jooyung Handc782442019-11-01 03:14:38 +09002773 `, func(fs map[string][]byte, config android.Config) {
2774 setUseVendorWhitelistForTest(config, []string{"myapex"})
2775 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002776}
2777
Jooyung Hand48f3c32019-08-23 11:18:57 +09002778func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2779 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2780 apex {
2781 name: "myapex",
2782 key: "myapex.key",
2783 native_shared_libs: ["libfoo"],
2784 }
2785
2786 apex_key {
2787 name: "myapex.key",
2788 public_key: "testkey.avbpubkey",
2789 private_key: "testkey.pem",
2790 }
2791
2792 cc_library {
2793 name: "libfoo",
2794 stl: "none",
2795 system_shared_libs: [],
2796 enabled: false,
2797 }
2798 `)
2799 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2800 apex {
2801 name: "myapex",
2802 key: "myapex.key",
2803 java_libs: ["myjar"],
2804 }
2805
2806 apex_key {
2807 name: "myapex.key",
2808 public_key: "testkey.avbpubkey",
2809 private_key: "testkey.pem",
2810 }
2811
2812 java_library {
2813 name: "myjar",
2814 srcs: ["foo/bar/MyClass.java"],
2815 sdk_version: "none",
2816 system_modules: "none",
2817 compile_dex: true,
2818 enabled: false,
2819 }
2820 `)
2821}
2822
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002823func TestApexWithApps(t *testing.T) {
2824 ctx, _ := testApex(t, `
2825 apex {
2826 name: "myapex",
2827 key: "myapex.key",
2828 apps: [
2829 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002830 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002831 ],
2832 }
2833
2834 apex_key {
2835 name: "myapex.key",
2836 public_key: "testkey.avbpubkey",
2837 private_key: "testkey.pem",
2838 }
2839
2840 android_app {
2841 name: "AppFoo",
2842 srcs: ["foo/bar/MyClass.java"],
2843 sdk_version: "none",
2844 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002845 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002846 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002847
2848 android_app {
2849 name: "AppFooPriv",
2850 srcs: ["foo/bar/MyClass.java"],
2851 sdk_version: "none",
2852 system_modules: "none",
2853 privileged: true,
2854 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002855
2856 cc_library_shared {
2857 name: "libjni",
2858 srcs: ["mylib.cpp"],
2859 stl: "none",
2860 system_shared_libs: [],
2861 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002862 `)
2863
Sundong Ahnabb64432019-10-22 13:58:29 +09002864 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002865 apexRule := module.Rule("apexRule")
2866 copyCmds := apexRule.Args["copy_commands"]
2867
2868 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002869 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002870
2871 // JNI libraries are embedded inside APK
2872 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
Colin Cross7113d202019-11-20 16:39:12 -08002873 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002874 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2875 // ... uncompressed
2876 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2877 t.Errorf("jni lib is not uncompressed for AppFoo")
2878 }
2879 // ... and not directly inside the APEX
2880 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002881}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002882
Dario Frenicde2a032019-10-27 00:29:22 +01002883func TestApexWithAppImports(t *testing.T) {
2884 ctx, _ := testApex(t, `
2885 apex {
2886 name: "myapex",
2887 key: "myapex.key",
2888 apps: [
2889 "AppFooPrebuilt",
2890 "AppFooPrivPrebuilt",
2891 ],
2892 }
2893
2894 apex_key {
2895 name: "myapex.key",
2896 public_key: "testkey.avbpubkey",
2897 private_key: "testkey.pem",
2898 }
2899
2900 android_app_import {
2901 name: "AppFooPrebuilt",
2902 apk: "PrebuiltAppFoo.apk",
2903 presigned: true,
2904 dex_preopt: {
2905 enabled: false,
2906 },
2907 }
2908
2909 android_app_import {
2910 name: "AppFooPrivPrebuilt",
2911 apk: "PrebuiltAppFooPriv.apk",
2912 privileged: true,
2913 presigned: true,
2914 dex_preopt: {
2915 enabled: false,
2916 },
2917 }
2918 `)
2919
Sundong Ahnabb64432019-10-22 13:58:29 +09002920 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002921 apexRule := module.Rule("apexRule")
2922 copyCmds := apexRule.Args["copy_commands"]
2923
2924 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2925 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002926}
2927
Jooyung Han18020ea2019-11-13 10:50:48 +09002928func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2929 // libfoo's apex_available comes from cc_defaults
2930 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2931 apex {
2932 name: "myapex",
2933 key: "myapex.key",
2934 native_shared_libs: ["libfoo"],
2935 }
2936
2937 apex_key {
2938 name: "myapex.key",
2939 public_key: "testkey.avbpubkey",
2940 private_key: "testkey.pem",
2941 }
2942
2943 apex {
2944 name: "otherapex",
2945 key: "myapex.key",
2946 native_shared_libs: ["libfoo"],
2947 }
2948
2949 cc_defaults {
2950 name: "libfoo-defaults",
2951 apex_available: ["otherapex"],
2952 }
2953
2954 cc_library {
2955 name: "libfoo",
2956 defaults: ["libfoo-defaults"],
2957 stl: "none",
2958 system_shared_libs: [],
2959 }`)
2960}
2961
Jiyong Park127b40b2019-09-30 16:04:35 +09002962func TestApexAvailable(t *testing.T) {
2963 // libfoo is not available to myapex, but only to otherapex
2964 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2965 apex {
2966 name: "myapex",
2967 key: "myapex.key",
2968 native_shared_libs: ["libfoo"],
2969 }
2970
2971 apex_key {
2972 name: "myapex.key",
2973 public_key: "testkey.avbpubkey",
2974 private_key: "testkey.pem",
2975 }
2976
2977 apex {
2978 name: "otherapex",
2979 key: "otherapex.key",
2980 native_shared_libs: ["libfoo"],
2981 }
2982
2983 apex_key {
2984 name: "otherapex.key",
2985 public_key: "testkey.avbpubkey",
2986 private_key: "testkey.pem",
2987 }
2988
2989 cc_library {
2990 name: "libfoo",
2991 stl: "none",
2992 system_shared_libs: [],
2993 apex_available: ["otherapex"],
2994 }`)
2995
2996 // libbar is an indirect dep
2997 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
2998 apex {
2999 name: "myapex",
3000 key: "myapex.key",
3001 native_shared_libs: ["libfoo"],
3002 }
3003
3004 apex_key {
3005 name: "myapex.key",
3006 public_key: "testkey.avbpubkey",
3007 private_key: "testkey.pem",
3008 }
3009
3010 apex {
3011 name: "otherapex",
3012 key: "otherapex.key",
3013 native_shared_libs: ["libfoo"],
3014 }
3015
3016 apex_key {
3017 name: "otherapex.key",
3018 public_key: "testkey.avbpubkey",
3019 private_key: "testkey.pem",
3020 }
3021
3022 cc_library {
3023 name: "libfoo",
3024 stl: "none",
3025 shared_libs: ["libbar"],
3026 system_shared_libs: [],
3027 apex_available: ["myapex", "otherapex"],
3028 }
3029
3030 cc_library {
3031 name: "libbar",
3032 stl: "none",
3033 system_shared_libs: [],
3034 apex_available: ["otherapex"],
3035 }`)
3036
3037 testApexError(t, "\"otherapex\" is not a valid module name", `
3038 apex {
3039 name: "myapex",
3040 key: "myapex.key",
3041 native_shared_libs: ["libfoo"],
3042 }
3043
3044 apex_key {
3045 name: "myapex.key",
3046 public_key: "testkey.avbpubkey",
3047 private_key: "testkey.pem",
3048 }
3049
3050 cc_library {
3051 name: "libfoo",
3052 stl: "none",
3053 system_shared_libs: [],
3054 apex_available: ["otherapex"],
3055 }`)
3056
3057 ctx, _ := testApex(t, `
3058 apex {
3059 name: "myapex",
3060 key: "myapex.key",
3061 native_shared_libs: ["libfoo", "libbar"],
3062 }
3063
3064 apex_key {
3065 name: "myapex.key",
3066 public_key: "testkey.avbpubkey",
3067 private_key: "testkey.pem",
3068 }
3069
3070 cc_library {
3071 name: "libfoo",
3072 stl: "none",
3073 system_shared_libs: [],
3074 apex_available: ["myapex"],
3075 }
3076
3077 cc_library {
3078 name: "libbar",
3079 stl: "none",
3080 system_shared_libs: [],
3081 apex_available: ["//apex_available:anyapex"],
3082 }`)
3083
3084 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003085 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3086 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3087 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3088 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003089
3090 ctx, _ = testApex(t, `
3091 apex {
3092 name: "myapex",
3093 key: "myapex.key",
3094 }
3095
3096 apex_key {
3097 name: "myapex.key",
3098 public_key: "testkey.avbpubkey",
3099 private_key: "testkey.pem",
3100 }
3101
3102 cc_library {
3103 name: "libfoo",
3104 stl: "none",
3105 system_shared_libs: [],
3106 apex_available: ["//apex_available:platform"],
3107 }`)
3108
3109 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003110 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3111 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003112
3113 ctx, _ = testApex(t, `
3114 apex {
3115 name: "myapex",
3116 key: "myapex.key",
3117 native_shared_libs: ["libfoo"],
3118 }
3119
3120 apex_key {
3121 name: "myapex.key",
3122 public_key: "testkey.avbpubkey",
3123 private_key: "testkey.pem",
3124 }
3125
3126 cc_library {
3127 name: "libfoo",
3128 stl: "none",
3129 system_shared_libs: [],
3130 apex_available: ["myapex"],
3131 static: {
3132 apex_available: ["//apex_available:platform"],
3133 },
3134 }`)
3135
3136 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08003137 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3138 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003139 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08003140 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3141 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003142}
3143
Jiyong Park5d790c32019-11-15 18:40:32 +09003144func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003145 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003146 apex {
3147 name: "myapex",
3148 key: "myapex.key",
3149 apps: ["app"],
3150 }
3151
3152 override_apex {
3153 name: "override_myapex",
3154 base: "myapex",
3155 apps: ["override_app"],
3156 }
3157
3158 apex_key {
3159 name: "myapex.key",
3160 public_key: "testkey.avbpubkey",
3161 private_key: "testkey.pem",
3162 }
3163
3164 android_app {
3165 name: "app",
3166 srcs: ["foo/bar/MyClass.java"],
3167 package_name: "foo",
3168 sdk_version: "none",
3169 system_modules: "none",
3170 }
3171
3172 override_android_app {
3173 name: "override_app",
3174 base: "app",
3175 package_name: "bar",
3176 }
3177 `)
3178
Jiyong Park317645e2019-12-05 13:20:58 +09003179 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3180 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3181 if originalVariant.GetOverriddenBy() != "" {
3182 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3183 }
3184 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3185 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3186 }
3187
Jiyong Park5d790c32019-11-15 18:40:32 +09003188 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3189 apexRule := module.Rule("apexRule")
3190 copyCmds := apexRule.Args["copy_commands"]
3191
3192 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3193 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003194
3195 apexBundle := module.Module().(*apexBundle)
3196 name := apexBundle.Name()
3197 if name != "override_myapex" {
3198 t.Errorf("name should be \"override_myapex\", but was %q", name)
3199 }
3200
3201 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3202 var builder strings.Builder
3203 data.Custom(&builder, name, "TARGET_", "", data)
3204 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003205 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003206 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3207 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
3208 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003209 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003210 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3211 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003212}
3213
Jooyung Han214bf372019-11-12 13:03:50 +09003214func TestLegacyAndroid10Support(t *testing.T) {
3215 ctx, _ := testApex(t, `
3216 apex {
3217 name: "myapex",
3218 key: "myapex.key",
3219 legacy_android10_support: true,
3220 }
3221
3222 apex_key {
3223 name: "myapex.key",
3224 public_key: "testkey.avbpubkey",
3225 private_key: "testkey.pem",
3226 }
3227 `)
3228
3229 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3230 args := module.Rule("apexRule").Args
3231 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
3232}
3233
Jiyong Park479321d2019-12-16 11:47:12 +09003234func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3235 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3236 apex {
3237 name: "myapex",
3238 key: "myapex.key",
3239 java_libs: ["myjar"],
3240 }
3241
3242 apex_key {
3243 name: "myapex.key",
3244 public_key: "testkey.avbpubkey",
3245 private_key: "testkey.pem",
3246 }
3247
3248 java_library {
3249 name: "myjar",
3250 srcs: ["foo/bar/MyClass.java"],
3251 sdk_version: "none",
3252 system_modules: "none",
3253 }
3254 `)
3255}
3256
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003257func TestMain(m *testing.M) {
3258 run := func() int {
3259 setUp()
3260 defer tearDown()
3261
3262 return m.Run()
3263 }
3264
3265 os.Exit(run())
3266}