blob: 72c4b084534c844a9145dc01a67af2067b54535f [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Jooyung Han31c470b2019-10-18 16:26:59 +090022 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090023 "strings"
24 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090025
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090030 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090031)
32
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070033var buildDir string
34
Jooyung Hand3639552019-08-09 12:57:43 +090035// names returns name list from white space separated string
36func names(s string) (ns []string) {
37 for _, n := range strings.Split(s, " ") {
38 if len(n) > 0 {
39 ns = append(ns, n)
40 }
41 }
42 return
43}
44
Jooyung Han344d5432019-08-23 11:17:39 +090045func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
46 t.Helper()
47 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090048 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
49 if len(errs) > 0 {
50 android.FailIfNoMatchingErrors(t, pattern, errs)
51 return
52 }
53 _, errs = ctx.PrepareBuildActions(config)
54 if len(errs) > 0 {
55 android.FailIfNoMatchingErrors(t, pattern, errs)
56 return
57 }
58
59 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
60}
61
Jooyung Han344d5432019-08-23 11:17:39 +090062func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
63 t.Helper()
64 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090065 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
66 android.FailIfErrored(t, errs)
67 _, errs = ctx.PrepareBuildActions(config)
68 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070069 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090070}
71
Jooyung Han344d5432019-08-23 11:17:39 +090072type testCustomizer func(fs map[string][]byte, config android.Config)
73
74func withFiles(files map[string][]byte) testCustomizer {
75 return func(fs map[string][]byte, config android.Config) {
76 for k, v := range files {
77 fs[k] = v
78 }
79 }
80}
81
82func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
83 return func(fs map[string][]byte, config android.Config) {
84 for k, v := range targets {
85 config.Targets[k] = v
86 }
87 }
88}
89
Jooyung Han31c470b2019-10-18 16:26:59 +090090func withBinder32bit(fs map[string][]byte, config android.Config) {
91 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
92}
93
Jooyung Han344d5432019-08-23 11:17:39 +090094func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +090095 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +090096
97 bp = bp + `
98 toolchain_library {
99 name: "libcompiler_rt-extras",
100 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900101 vendor_available: true,
102 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900103 }
104
105 toolchain_library {
106 name: "libatomic",
107 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900108 vendor_available: true,
109 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900110 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900111 }
112
113 toolchain_library {
114 name: "libgcc",
115 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900116 vendor_available: true,
117 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900118 }
119
120 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700121 name: "libgcc_stripped",
122 src: "",
123 vendor_available: true,
124 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900125 native_bridge_supported: true,
Yi Kongacee27c2019-03-29 20:05:14 -0700126 }
127
128 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900129 name: "libclang_rt.builtins-aarch64-android",
130 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900131 vendor_available: true,
132 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900133 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900134 }
135
136 toolchain_library {
137 name: "libclang_rt.builtins-arm-android",
138 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900139 vendor_available: true,
140 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900141 native_bridge_supported: true,
142 }
143
144 toolchain_library {
145 name: "libclang_rt.builtins-x86_64-android",
146 src: "",
147 vendor_available: true,
148 recovery_available: true,
149 native_bridge_supported: true,
150 }
151
152 toolchain_library {
153 name: "libclang_rt.builtins-i686-android",
154 src: "",
155 vendor_available: true,
156 recovery_available: true,
157 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900158 }
159
160 cc_object {
161 name: "crtbegin_so",
162 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900163 vendor_available: true,
164 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900165 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900166 }
167
168 cc_object {
169 name: "crtend_so",
170 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900171 vendor_available: true,
172 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900173 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900174 }
175
Alex Light3d673592019-01-18 14:37:31 -0800176 cc_object {
177 name: "crtbegin_static",
178 stl: "none",
179 }
180
181 cc_object {
182 name: "crtend_android",
183 stl: "none",
184 }
185
Jiyong Parkda6eb592018-12-19 17:12:36 +0900186 llndk_library {
187 name: "libc",
188 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900189 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900190 }
191
192 llndk_library {
193 name: "libm",
194 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900195 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900196 }
197
198 llndk_library {
199 name: "libdl",
200 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900201 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900202 }
Jooyung Han54aca7b2019-11-20 02:26:02 +0900203
204 filegroup {
205 name: "myapex-file_contexts",
206 srcs: [
207 "system/sepolicy/apex/myapex-file_contexts",
208 ],
209 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900210 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800211
Dario Frenicde2a032019-10-27 00:29:22 +0100212 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900213
Jooyung Han344d5432019-08-23 11:17:39 +0900214 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900215 "a.java": nil,
216 "PrebuiltAppFoo.apk": nil,
217 "PrebuiltAppFooPriv.apk": nil,
218 "build/make/target/product/security": nil,
219 "apex_manifest.json": nil,
220 "AndroidManifest.xml": nil,
221 "system/sepolicy/apex/myapex-file_contexts": nil,
222 "system/sepolicy/apex/otherapex-file_contexts": nil,
223 "system/sepolicy/apex/commonapex-file_contexts": nil,
224 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800225 "mylib.cpp": nil,
226 "mylib_common.cpp": nil,
227 "mytest.cpp": nil,
228 "mytest1.cpp": nil,
229 "mytest2.cpp": nil,
230 "mytest3.cpp": nil,
231 "myprebuilt": nil,
232 "my_include": nil,
233 "foo/bar/MyClass.java": nil,
234 "prebuilt.jar": nil,
235 "vendor/foo/devkeys/test.x509.pem": nil,
236 "vendor/foo/devkeys/test.pk8": nil,
237 "testkey.x509.pem": nil,
238 "testkey.pk8": nil,
239 "testkey.override.x509.pem": nil,
240 "testkey.override.pk8": nil,
241 "vendor/foo/devkeys/testkey.avbpubkey": nil,
242 "vendor/foo/devkeys/testkey.pem": nil,
243 "NOTICE": nil,
244 "custom_notice": nil,
245 "testkey2.avbpubkey": nil,
246 "testkey2.pem": nil,
247 "myapex-arm64.apex": nil,
248 "myapex-arm.apex": nil,
249 "frameworks/base/api/current.txt": nil,
250 "framework/aidl/a.aidl": nil,
251 "build/make/core/proguard.flags": nil,
252 "build/make/core/proguard_basic_keeps.flags": nil,
253 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900254 }
255
256 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800257 // The fs now needs to be populated before creating the config, call handlers twice
258 // for now, once to get any fs changes, and later after the config was created to
259 // set product variables or targets.
260 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
261 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900262 }
263
Colin Cross98be1bb2019-12-13 20:41:13 -0800264 config := android.TestArchConfig(buildDir, nil, bp, fs)
265 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
266 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
267 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
268 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
269 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
270 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
271
272 for _, handler := range handlers {
273 // The fs now needs to be populated before creating the config, call handlers twice
274 // for now, earlier to get any fs changes, and now after the config was created to
275 // set product variables or targets.
276 tempFS := map[string][]byte{}
277 handler(tempFS, config)
278 }
279
280 ctx := android.NewTestArchContext()
281 ctx.RegisterModuleType("apex", BundleFactory)
282 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
283 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
284 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
285 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
286 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
287 ctx.RegisterModuleType("override_apex", overrideApexFactory)
288
Paul Duffin77980a82019-12-19 16:01:36 +0000289 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800290 ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800291 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800292 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
293 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800294 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
295 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800296 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000297 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000298 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000299 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900300 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800301
302 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800303 ctx.PreDepsMutators(RegisterPreDepsMutators)
304 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
305 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800306
307 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900308
Jooyung Han5c998b92019-06-27 11:30:33 +0900309 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900310}
311
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700312func setUp() {
313 var err error
314 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900315 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700316 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900317 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900318}
319
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700320func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900321 os.RemoveAll(buildDir)
322}
323
324// ensure that 'result' contains 'expected'
325func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900326 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900327 if !strings.Contains(result, expected) {
328 t.Errorf("%q is not found in %q", expected, result)
329 }
330}
331
332// ensures that 'result' does not contain 'notExpected'
333func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900334 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900335 if strings.Contains(result, notExpected) {
336 t.Errorf("%q is found in %q", notExpected, result)
337 }
338}
339
340func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900341 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900342 if !android.InList(expected, result) {
343 t.Errorf("%q is not found in %v", expected, result)
344 }
345}
346
347func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900348 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900349 if android.InList(notExpected, result) {
350 t.Errorf("%q is found in %v", notExpected, result)
351 }
352}
353
Jooyung Hane1633032019-08-01 17:41:43 +0900354func ensureListEmpty(t *testing.T, result []string) {
355 t.Helper()
356 if len(result) > 0 {
357 t.Errorf("%q is expected to be empty", result)
358 }
359}
360
Jiyong Park25fc6a92018-11-18 18:02:45 +0900361// Minimal test
362func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700363 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900364 apex_defaults {
365 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900366 manifest: ":myapex.manifest",
367 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900368 key: "myapex.key",
369 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800370 multilib: {
371 both: {
372 binaries: ["foo",],
373 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900374 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900375 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900376 }
377
Jiyong Park30ca9372019-02-07 16:27:23 +0900378 apex {
379 name: "myapex",
380 defaults: ["myapex-defaults"],
381 }
382
Jiyong Park25fc6a92018-11-18 18:02:45 +0900383 apex_key {
384 name: "myapex.key",
385 public_key: "testkey.avbpubkey",
386 private_key: "testkey.pem",
387 }
388
Jiyong Park809bb722019-02-13 21:33:49 +0900389 filegroup {
390 name: "myapex.manifest",
391 srcs: ["apex_manifest.json"],
392 }
393
394 filegroup {
395 name: "myapex.androidmanifest",
396 srcs: ["AndroidManifest.xml"],
397 }
398
Jiyong Park25fc6a92018-11-18 18:02:45 +0900399 cc_library {
400 name: "mylib",
401 srcs: ["mylib.cpp"],
402 shared_libs: ["mylib2"],
403 system_shared_libs: [],
404 stl: "none",
405 }
406
Alex Light3d673592019-01-18 14:37:31 -0800407 cc_binary {
408 name: "foo",
409 srcs: ["mylib.cpp"],
410 compile_multilib: "both",
411 multilib: {
412 lib32: {
413 suffix: "32",
414 },
415 lib64: {
416 suffix: "64",
417 },
418 },
419 symlinks: ["foo_link_"],
420 symlink_preferred_arch: true,
421 system_shared_libs: [],
422 static_executable: true,
423 stl: "none",
424 }
425
Jiyong Park25fc6a92018-11-18 18:02:45 +0900426 cc_library {
427 name: "mylib2",
428 srcs: ["mylib.cpp"],
429 system_shared_libs: [],
430 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900431 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900432 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900433
434 java_library {
435 name: "myjar",
436 srcs: ["foo/bar/MyClass.java"],
437 sdk_version: "none",
438 system_modules: "none",
439 compile_dex: true,
440 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900441 libs: ["mysharedjar"],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900442 }
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 Park3ff16992019-12-27 14:11:47 +0900451
452 java_library {
453 name: "mysharedjar",
454 srcs: ["foo/bar/MyClass.java"],
455 sdk_version: "none",
456 system_modules: "none",
457 compile_dex: true,
458 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900459 `)
460
Sundong Ahnabb64432019-10-22 13:58:29 +0900461 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900462
463 optFlags := apexRule.Args["opt_flags"]
464 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700465 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900466 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900467
Jiyong Park25fc6a92018-11-18 18:02:45 +0900468 copyCmds := apexRule.Args["copy_commands"]
469
470 // Ensure that main rule creates an output
471 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
472
473 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800474 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900475 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900476
477 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800478 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900479 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900480
481 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800482 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
483 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900484 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
485 // .. but not for java libs
486 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900487 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800488
Colin Cross7113d202019-11-20 16:39:12 -0800489 // Ensure that the platform variant ends with _shared or _common
490 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
491 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900492 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
493 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900494 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
495
496 // Ensure that dynamic dependency to java libs are not included
497 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800498
499 // Ensure that all symlinks are present.
500 found_foo_link_64 := false
501 found_foo := false
502 for _, cmd := range strings.Split(copyCmds, " && ") {
503 if strings.HasPrefix(cmd, "ln -s foo64") {
504 if strings.HasSuffix(cmd, "bin/foo") {
505 found_foo = true
506 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
507 found_foo_link_64 = true
508 }
509 }
510 }
511 good := found_foo && found_foo_link_64
512 if !good {
513 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
514 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900515
Sundong Ahnabb64432019-10-22 13:58:29 +0900516 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700517 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700518 if len(noticeInputs) != 2 {
519 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900520 }
521 ensureListContains(t, noticeInputs, "NOTICE")
522 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800523}
524
Jooyung Hanf21c7972019-12-16 22:32:06 +0900525func TestDefaults(t *testing.T) {
526 ctx, _ := testApex(t, `
527 apex_defaults {
528 name: "myapex-defaults",
529 key: "myapex.key",
530 prebuilts: ["myetc"],
531 native_shared_libs: ["mylib"],
532 java_libs: ["myjar"],
533 apps: ["AppFoo"],
534 }
535
536 prebuilt_etc {
537 name: "myetc",
538 src: "myprebuilt",
539 }
540
541 apex {
542 name: "myapex",
543 defaults: ["myapex-defaults"],
544 }
545
546 apex_key {
547 name: "myapex.key",
548 public_key: "testkey.avbpubkey",
549 private_key: "testkey.pem",
550 }
551
552 cc_library {
553 name: "mylib",
554 system_shared_libs: [],
555 stl: "none",
556 }
557
558 java_library {
559 name: "myjar",
560 srcs: ["foo/bar/MyClass.java"],
561 sdk_version: "none",
562 system_modules: "none",
563 compile_dex: true,
564 }
565
566 android_app {
567 name: "AppFoo",
568 srcs: ["foo/bar/MyClass.java"],
569 sdk_version: "none",
570 system_modules: "none",
571 }
572 `)
573 ensureExactContents(t, ctx, "myapex", []string{
574 "etc/myetc",
575 "javalib/myjar.jar",
576 "lib64/mylib.so",
577 "app/AppFoo/AppFoo.apk",
578 })
579}
580
Jooyung Han01a3ee22019-11-02 02:52:25 +0900581func TestApexManifest(t *testing.T) {
582 ctx, _ := testApex(t, `
583 apex {
584 name: "myapex",
585 key: "myapex.key",
586 }
587
588 apex_key {
589 name: "myapex.key",
590 public_key: "testkey.avbpubkey",
591 private_key: "testkey.pem",
592 }
593 `)
594
595 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900596 args := module.Rule("apexRule").Args
597 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
598 t.Error("manifest should be apex_manifest.pb, but " + manifest)
599 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900600}
601
Alex Light5098a612018-11-29 17:12:15 -0800602func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700603 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800604 apex {
605 name: "myapex",
606 key: "myapex.key",
607 payload_type: "zip",
608 native_shared_libs: ["mylib"],
609 }
610
611 apex_key {
612 name: "myapex.key",
613 public_key: "testkey.avbpubkey",
614 private_key: "testkey.pem",
615 }
616
617 cc_library {
618 name: "mylib",
619 srcs: ["mylib.cpp"],
620 shared_libs: ["mylib2"],
621 system_shared_libs: [],
622 stl: "none",
623 }
624
625 cc_library {
626 name: "mylib2",
627 srcs: ["mylib.cpp"],
628 system_shared_libs: [],
629 stl: "none",
630 }
631 `)
632
Sundong Ahnabb64432019-10-22 13:58:29 +0900633 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800634 copyCmds := zipApexRule.Args["copy_commands"]
635
636 // Ensure that main rule creates an output
637 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
638
639 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800640 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800641
642 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800643 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800644
645 // Ensure that both direct and indirect deps are copied into apex
646 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
647 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900648}
649
650func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700651 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900652 apex {
653 name: "myapex",
654 key: "myapex.key",
655 native_shared_libs: ["mylib", "mylib3"],
656 }
657
658 apex_key {
659 name: "myapex.key",
660 public_key: "testkey.avbpubkey",
661 private_key: "testkey.pem",
662 }
663
664 cc_library {
665 name: "mylib",
666 srcs: ["mylib.cpp"],
667 shared_libs: ["mylib2", "mylib3"],
668 system_shared_libs: [],
669 stl: "none",
670 }
671
672 cc_library {
673 name: "mylib2",
674 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900675 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900676 system_shared_libs: [],
677 stl: "none",
678 stubs: {
679 versions: ["1", "2", "3"],
680 },
681 }
682
683 cc_library {
684 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900685 srcs: ["mylib.cpp"],
686 shared_libs: ["mylib4"],
687 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900688 stl: "none",
689 stubs: {
690 versions: ["10", "11", "12"],
691 },
692 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900693
694 cc_library {
695 name: "mylib4",
696 srcs: ["mylib.cpp"],
697 system_shared_libs: [],
698 stl: "none",
699 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900700 `)
701
Sundong Ahnabb64432019-10-22 13:58:29 +0900702 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900703 copyCmds := apexRule.Args["copy_commands"]
704
705 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800706 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900707
708 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800709 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900710
711 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800712 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900713
Colin Cross7113d202019-11-20 16:39:12 -0800714 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900715
716 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900717 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900718 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900719 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900720
721 // 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 -0800722 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900723 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800724 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900725
726 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800727 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900728 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900729
730 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900731 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900732
733 ensureExactContents(t, ctx, "myapex", []string{
734 "lib64/mylib.so",
735 "lib64/mylib3.so",
736 "lib64/mylib4.so",
737 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900738}
739
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900740func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700741 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900742 apex {
743 name: "myapex",
744 key: "myapex.key",
745 native_shared_libs: ["mylib"],
746 }
747
748 apex_key {
749 name: "myapex.key",
750 public_key: "testkey.avbpubkey",
751 private_key: "testkey.pem",
752 }
753
754 cc_library {
755 name: "mylib",
756 srcs: ["mylib.cpp"],
757 shared_libs: ["libfoo#10"],
758 system_shared_libs: [],
759 stl: "none",
760 }
761
762 cc_library {
763 name: "libfoo",
764 srcs: ["mylib.cpp"],
765 shared_libs: ["libbar"],
766 system_shared_libs: [],
767 stl: "none",
768 stubs: {
769 versions: ["10", "20", "30"],
770 },
771 }
772
773 cc_library {
774 name: "libbar",
775 srcs: ["mylib.cpp"],
776 system_shared_libs: [],
777 stl: "none",
778 }
779
780 `)
781
Sundong Ahnabb64432019-10-22 13:58:29 +0900782 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900783 copyCmds := apexRule.Args["copy_commands"]
784
785 // Ensure that direct non-stubs dep is always included
786 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
787
788 // Ensure that indirect stubs dep is not included
789 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
790
791 // Ensure that dependency of stubs is not included
792 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
793
Colin Cross7113d202019-11-20 16:39:12 -0800794 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900795
796 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900797 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900798 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900799 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900800
Jiyong Park3ff16992019-12-27 14:11:47 +0900801 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900802
803 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
804 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
805}
806
Jooyung Hand3639552019-08-09 12:57:43 +0900807func TestApexWithRuntimeLibsDependency(t *testing.T) {
808 /*
809 myapex
810 |
811 v (runtime_libs)
812 mylib ------+------> libfoo [provides stub]
813 |
814 `------> libbar
815 */
816 ctx, _ := testApex(t, `
817 apex {
818 name: "myapex",
819 key: "myapex.key",
820 native_shared_libs: ["mylib"],
821 }
822
823 apex_key {
824 name: "myapex.key",
825 public_key: "testkey.avbpubkey",
826 private_key: "testkey.pem",
827 }
828
829 cc_library {
830 name: "mylib",
831 srcs: ["mylib.cpp"],
832 runtime_libs: ["libfoo", "libbar"],
833 system_shared_libs: [],
834 stl: "none",
835 }
836
837 cc_library {
838 name: "libfoo",
839 srcs: ["mylib.cpp"],
840 system_shared_libs: [],
841 stl: "none",
842 stubs: {
843 versions: ["10", "20", "30"],
844 },
845 }
846
847 cc_library {
848 name: "libbar",
849 srcs: ["mylib.cpp"],
850 system_shared_libs: [],
851 stl: "none",
852 }
853
854 `)
855
Sundong Ahnabb64432019-10-22 13:58:29 +0900856 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900857 copyCmds := apexRule.Args["copy_commands"]
858
859 // Ensure that direct non-stubs dep is always included
860 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
861
862 // Ensure that indirect stubs dep is not included
863 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
864
865 // Ensure that runtime_libs dep in included
866 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
867
Sundong Ahnabb64432019-10-22 13:58:29 +0900868 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900869 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
870 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900871
872}
873
Jooyung Han9c80bae2019-08-20 17:30:57 +0900874func TestApexDependencyToLLNDK(t *testing.T) {
875 ctx, _ := testApex(t, `
876 apex {
877 name: "myapex",
878 key: "myapex.key",
879 use_vendor: true,
880 native_shared_libs: ["mylib"],
881 }
882
883 apex_key {
884 name: "myapex.key",
885 public_key: "testkey.avbpubkey",
886 private_key: "testkey.pem",
887 }
888
889 cc_library {
890 name: "mylib",
891 srcs: ["mylib.cpp"],
892 vendor_available: true,
893 shared_libs: ["libbar"],
894 system_shared_libs: [],
895 stl: "none",
896 }
897
898 cc_library {
899 name: "libbar",
900 srcs: ["mylib.cpp"],
901 system_shared_libs: [],
902 stl: "none",
903 }
904
905 llndk_library {
906 name: "libbar",
907 symbol_file: "",
908 }
Jooyung Handc782442019-11-01 03:14:38 +0900909 `, func(fs map[string][]byte, config android.Config) {
910 setUseVendorWhitelistForTest(config, []string{"myapex"})
911 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900912
Sundong Ahnabb64432019-10-22 13:58:29 +0900913 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900914 copyCmds := apexRule.Args["copy_commands"]
915
916 // Ensure that LLNDK dep is not included
917 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
918
Sundong Ahnabb64432019-10-22 13:58:29 +0900919 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900920 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900921
922 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900923 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900924
925}
926
Jiyong Park25fc6a92018-11-18 18:02:45 +0900927func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700928 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900929 apex {
930 name: "myapex",
931 key: "myapex.key",
932 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
933 }
934
935 apex_key {
936 name: "myapex.key",
937 public_key: "testkey.avbpubkey",
938 private_key: "testkey.pem",
939 }
940
941 cc_library {
942 name: "mylib",
943 srcs: ["mylib.cpp"],
944 shared_libs: ["libdl#27"],
945 stl: "none",
946 }
947
948 cc_library_shared {
949 name: "mylib_shared",
950 srcs: ["mylib.cpp"],
951 shared_libs: ["libdl#27"],
952 stl: "none",
953 }
954
955 cc_library {
956 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700957 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900958 nocrt: true,
959 system_shared_libs: [],
960 stl: "none",
961 stubs: {
962 versions: ["27", "28", "29"],
963 },
964 }
965
966 cc_library {
967 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700968 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900969 nocrt: true,
970 system_shared_libs: [],
971 stl: "none",
972 stubs: {
973 versions: ["27", "28", "29"],
974 },
975 }
976
977 cc_library {
978 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700979 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900980 nocrt: true,
981 system_shared_libs: [],
982 stl: "none",
983 stubs: {
984 versions: ["27", "28", "29"],
985 },
986 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900987
988 cc_library {
989 name: "libBootstrap",
990 srcs: ["mylib.cpp"],
991 stl: "none",
992 bootstrap: true,
993 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900994 `)
995
Sundong Ahnabb64432019-10-22 13:58:29 +0900996 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900997 copyCmds := apexRule.Args["copy_commands"]
998
999 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001000 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001001 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1002 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001003
1004 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001005 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001006
Colin Cross7113d202019-11-20 16:39:12 -08001007 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1008 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1009 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001010
1011 // For dependency to libc
1012 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001013 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001014 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001015 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001016 // ... Cflags from stub is correctly exported to mylib
1017 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1018 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1019
1020 // For dependency to libm
1021 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001022 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001023 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001024 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001025 // ... and is not compiling with the stub
1026 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1027 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1028
1029 // For dependency to libdl
1030 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001031 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001032 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001033 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1034 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001035 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001036 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001037 // ... Cflags from stub is correctly exported to mylib
1038 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1039 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001040
1041 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001042 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1043 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1044 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1045 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001046}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001047
1048func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001049 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001050 apex {
1051 name: "myapex",
1052 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001053 native_shared_libs: ["mylib"],
1054 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001055 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001056 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001057 }
1058
1059 apex_key {
1060 name: "myapex.key",
1061 public_key: "testkey.avbpubkey",
1062 private_key: "testkey.pem",
1063 }
1064
1065 prebuilt_etc {
1066 name: "myetc",
1067 src: "myprebuilt",
1068 sub_dir: "foo/bar",
1069 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001070
1071 cc_library {
1072 name: "mylib",
1073 srcs: ["mylib.cpp"],
1074 relative_install_path: "foo/bar",
1075 system_shared_libs: [],
1076 stl: "none",
1077 }
1078
1079 cc_binary {
1080 name: "mybin",
1081 srcs: ["mylib.cpp"],
1082 relative_install_path: "foo/bar",
1083 system_shared_libs: [],
1084 static_executable: true,
1085 stl: "none",
1086 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001087 `)
1088
Sundong Ahnabb64432019-10-22 13:58:29 +09001089 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001090 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1091
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001092 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001093 ensureListContains(t, dirs, "etc")
1094 ensureListContains(t, dirs, "etc/foo")
1095 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001096 ensureListContains(t, dirs, "lib64")
1097 ensureListContains(t, dirs, "lib64/foo")
1098 ensureListContains(t, dirs, "lib64/foo/bar")
1099 ensureListContains(t, dirs, "lib")
1100 ensureListContains(t, dirs, "lib/foo")
1101 ensureListContains(t, dirs, "lib/foo/bar")
1102
Jiyong Parkbd13e442019-03-15 18:10:35 +09001103 ensureListContains(t, dirs, "bin")
1104 ensureListContains(t, dirs, "bin/foo")
1105 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001106}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001107
1108func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001109 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001110 apex {
1111 name: "myapex",
1112 key: "myapex.key",
1113 native_shared_libs: ["mylib"],
1114 use_vendor: true,
1115 }
1116
1117 apex_key {
1118 name: "myapex.key",
1119 public_key: "testkey.avbpubkey",
1120 private_key: "testkey.pem",
1121 }
1122
1123 cc_library {
1124 name: "mylib",
1125 srcs: ["mylib.cpp"],
1126 shared_libs: ["mylib2"],
1127 system_shared_libs: [],
1128 vendor_available: true,
1129 stl: "none",
1130 }
1131
1132 cc_library {
1133 name: "mylib2",
1134 srcs: ["mylib.cpp"],
1135 system_shared_libs: [],
1136 vendor_available: true,
1137 stl: "none",
1138 }
Jooyung Handc782442019-11-01 03:14:38 +09001139 `, func(fs map[string][]byte, config android.Config) {
1140 setUseVendorWhitelistForTest(config, []string{"myapex"})
1141 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001142
1143 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001144 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001145 for _, implicit := range i.Implicits {
1146 inputsList = append(inputsList, implicit.String())
1147 }
1148 }
1149 inputsString := strings.Join(inputsList, " ")
1150
1151 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001152 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1153 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001154
1155 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001156 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1157 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001158}
Jiyong Park16e91a02018-12-20 18:18:08 +09001159
Jooyung Handc782442019-11-01 03:14:38 +09001160func TestUseVendorRestriction(t *testing.T) {
1161 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1162 apex {
1163 name: "myapex",
1164 key: "myapex.key",
1165 use_vendor: true,
1166 }
1167 apex_key {
1168 name: "myapex.key",
1169 public_key: "testkey.avbpubkey",
1170 private_key: "testkey.pem",
1171 }
1172 `, func(fs map[string][]byte, config android.Config) {
1173 setUseVendorWhitelistForTest(config, []string{""})
1174 })
1175 // no error with whitelist
1176 testApex(t, `
1177 apex {
1178 name: "myapex",
1179 key: "myapex.key",
1180 use_vendor: true,
1181 }
1182 apex_key {
1183 name: "myapex.key",
1184 public_key: "testkey.avbpubkey",
1185 private_key: "testkey.pem",
1186 }
1187 `, func(fs map[string][]byte, config android.Config) {
1188 setUseVendorWhitelistForTest(config, []string{"myapex"})
1189 })
1190}
1191
Jooyung Han5c998b92019-06-27 11:30:33 +09001192func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1193 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1194 apex {
1195 name: "myapex",
1196 key: "myapex.key",
1197 native_shared_libs: ["mylib"],
1198 use_vendor: true,
1199 }
1200
1201 apex_key {
1202 name: "myapex.key",
1203 public_key: "testkey.avbpubkey",
1204 private_key: "testkey.pem",
1205 }
1206
1207 cc_library {
1208 name: "mylib",
1209 srcs: ["mylib.cpp"],
1210 system_shared_libs: [],
1211 stl: "none",
1212 }
1213 `)
1214}
1215
Jiyong Park16e91a02018-12-20 18:18:08 +09001216func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001217 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001218 apex {
1219 name: "myapex",
1220 key: "myapex.key",
1221 native_shared_libs: ["mylib"],
1222 }
1223
1224 apex_key {
1225 name: "myapex.key",
1226 public_key: "testkey.avbpubkey",
1227 private_key: "testkey.pem",
1228 }
1229
1230 cc_library {
1231 name: "mylib",
1232 srcs: ["mylib.cpp"],
1233 system_shared_libs: [],
1234 stl: "none",
1235 stubs: {
1236 versions: ["1", "2", "3"],
1237 },
1238 }
1239
1240 cc_binary {
1241 name: "not_in_apex",
1242 srcs: ["mylib.cpp"],
1243 static_libs: ["mylib"],
1244 static_executable: true,
1245 system_shared_libs: [],
1246 stl: "none",
1247 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001248 `)
1249
Colin Cross7113d202019-11-20 16:39:12 -08001250 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001251
1252 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001253 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001254}
Jiyong Park9335a262018-12-24 11:31:58 +09001255
1256func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001257 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001258 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001259 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001260 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001261 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001262 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001263 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001264 }
1265
1266 cc_library {
1267 name: "mylib",
1268 srcs: ["mylib.cpp"],
1269 system_shared_libs: [],
1270 stl: "none",
1271 }
1272
1273 apex_key {
1274 name: "myapex.key",
1275 public_key: "testkey.avbpubkey",
1276 private_key: "testkey.pem",
1277 }
1278
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001279 android_app_certificate {
1280 name: "myapex.certificate",
1281 certificate: "testkey",
1282 }
1283
1284 android_app_certificate {
1285 name: "myapex.certificate.override",
1286 certificate: "testkey.override",
1287 }
1288
Jiyong Park9335a262018-12-24 11:31:58 +09001289 `)
1290
1291 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001292 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001293
1294 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1295 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1296 "vendor/foo/devkeys/testkey.avbpubkey")
1297 }
1298 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1299 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1300 "vendor/foo/devkeys/testkey.pem")
1301 }
1302
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001303 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001304 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001305 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001306 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001307 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001308 }
1309}
Jiyong Park58e364a2019-01-19 19:24:06 +09001310
Jooyung Hanf121a652019-12-17 14:30:11 +09001311func TestCertificate(t *testing.T) {
1312 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1313 ctx, _ := testApex(t, `
1314 apex {
1315 name: "myapex",
1316 key: "myapex.key",
1317 }
1318 apex_key {
1319 name: "myapex.key",
1320 public_key: "testkey.avbpubkey",
1321 private_key: "testkey.pem",
1322 }`)
1323 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1324 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1325 if actual := rule.Args["certificates"]; actual != expected {
1326 t.Errorf("certificates should be %q, not %q", expected, actual)
1327 }
1328 })
1329 t.Run("override when unspecified", func(t *testing.T) {
1330 ctx, _ := testApex(t, `
1331 apex {
1332 name: "myapex_keytest",
1333 key: "myapex.key",
1334 file_contexts: ":myapex-file_contexts",
1335 }
1336 apex_key {
1337 name: "myapex.key",
1338 public_key: "testkey.avbpubkey",
1339 private_key: "testkey.pem",
1340 }
1341 android_app_certificate {
1342 name: "myapex.certificate.override",
1343 certificate: "testkey.override",
1344 }`)
1345 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1346 expected := "testkey.override.x509.pem testkey.override.pk8"
1347 if actual := rule.Args["certificates"]; actual != expected {
1348 t.Errorf("certificates should be %q, not %q", expected, actual)
1349 }
1350 })
1351 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1352 ctx, _ := testApex(t, `
1353 apex {
1354 name: "myapex",
1355 key: "myapex.key",
1356 certificate: ":myapex.certificate",
1357 }
1358 apex_key {
1359 name: "myapex.key",
1360 public_key: "testkey.avbpubkey",
1361 private_key: "testkey.pem",
1362 }
1363 android_app_certificate {
1364 name: "myapex.certificate",
1365 certificate: "testkey",
1366 }`)
1367 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1368 expected := "testkey.x509.pem testkey.pk8"
1369 if actual := rule.Args["certificates"]; actual != expected {
1370 t.Errorf("certificates should be %q, not %q", expected, actual)
1371 }
1372 })
1373 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1374 ctx, _ := testApex(t, `
1375 apex {
1376 name: "myapex_keytest",
1377 key: "myapex.key",
1378 file_contexts: ":myapex-file_contexts",
1379 certificate: ":myapex.certificate",
1380 }
1381 apex_key {
1382 name: "myapex.key",
1383 public_key: "testkey.avbpubkey",
1384 private_key: "testkey.pem",
1385 }
1386 android_app_certificate {
1387 name: "myapex.certificate.override",
1388 certificate: "testkey.override",
1389 }`)
1390 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1391 expected := "testkey.override.x509.pem testkey.override.pk8"
1392 if actual := rule.Args["certificates"]; actual != expected {
1393 t.Errorf("certificates should be %q, not %q", expected, actual)
1394 }
1395 })
1396 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1397 ctx, _ := testApex(t, `
1398 apex {
1399 name: "myapex",
1400 key: "myapex.key",
1401 certificate: "testkey",
1402 }
1403 apex_key {
1404 name: "myapex.key",
1405 public_key: "testkey.avbpubkey",
1406 private_key: "testkey.pem",
1407 }`)
1408 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1409 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1410 if actual := rule.Args["certificates"]; actual != expected {
1411 t.Errorf("certificates should be %q, not %q", expected, actual)
1412 }
1413 })
1414 t.Run("override when specified as <name>", func(t *testing.T) {
1415 ctx, _ := testApex(t, `
1416 apex {
1417 name: "myapex_keytest",
1418 key: "myapex.key",
1419 file_contexts: ":myapex-file_contexts",
1420 certificate: "testkey",
1421 }
1422 apex_key {
1423 name: "myapex.key",
1424 public_key: "testkey.avbpubkey",
1425 private_key: "testkey.pem",
1426 }
1427 android_app_certificate {
1428 name: "myapex.certificate.override",
1429 certificate: "testkey.override",
1430 }`)
1431 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1432 expected := "testkey.override.x509.pem testkey.override.pk8"
1433 if actual := rule.Args["certificates"]; actual != expected {
1434 t.Errorf("certificates should be %q, not %q", expected, actual)
1435 }
1436 })
1437}
1438
Jiyong Park58e364a2019-01-19 19:24:06 +09001439func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001440 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001441 apex {
1442 name: "myapex",
1443 key: "myapex.key",
1444 native_shared_libs: ["mylib"],
1445 }
1446
1447 apex {
1448 name: "otherapex",
1449 key: "myapex.key",
1450 native_shared_libs: ["mylib"],
1451 }
1452
1453 apex_key {
1454 name: "myapex.key",
1455 public_key: "testkey.avbpubkey",
1456 private_key: "testkey.pem",
1457 }
1458
1459 cc_library {
1460 name: "mylib",
1461 srcs: ["mylib.cpp"],
1462 system_shared_libs: [],
1463 stl: "none",
1464 }
1465 `)
1466
Jooyung Han6b8459b2019-10-30 08:29:25 +09001467 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001468 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001469 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001470 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1471 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001472
Jooyung Han6b8459b2019-10-30 08:29:25 +09001473 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001474 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001475 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001476 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1477 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001478
Jooyung Han6b8459b2019-10-30 08:29:25 +09001479 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001480 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001481 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001482 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1483 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001484}
Jiyong Park7e636d02019-01-28 16:16:54 +09001485
1486func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001487 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001488 apex {
1489 name: "myapex",
1490 key: "myapex.key",
1491 native_shared_libs: ["mylib"],
1492 }
1493
1494 apex_key {
1495 name: "myapex.key",
1496 public_key: "testkey.avbpubkey",
1497 private_key: "testkey.pem",
1498 }
1499
1500 cc_library_headers {
1501 name: "mylib_headers",
1502 export_include_dirs: ["my_include"],
1503 system_shared_libs: [],
1504 stl: "none",
1505 }
1506
1507 cc_library {
1508 name: "mylib",
1509 srcs: ["mylib.cpp"],
1510 system_shared_libs: [],
1511 stl: "none",
1512 header_libs: ["mylib_headers"],
1513 export_header_lib_headers: ["mylib_headers"],
1514 stubs: {
1515 versions: ["1", "2", "3"],
1516 },
1517 }
1518
1519 cc_library {
1520 name: "otherlib",
1521 srcs: ["mylib.cpp"],
1522 system_shared_libs: [],
1523 stl: "none",
1524 shared_libs: ["mylib"],
1525 }
1526 `)
1527
Colin Cross7113d202019-11-20 16:39:12 -08001528 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001529
1530 // Ensure that the include path of the header lib is exported to 'otherlib'
1531 ensureContains(t, cFlags, "-Imy_include")
1532}
Alex Light9670d332019-01-29 18:07:33 -08001533
Jooyung Han31c470b2019-10-18 16:26:59 +09001534func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1535 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001536 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001537 copyCmds := apexRule.Args["copy_commands"]
1538 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001539 var failed bool
1540 var surplus []string
1541 filesMatched := make(map[string]bool)
1542 addContent := func(content string) {
1543 for _, expected := range files {
1544 if matched, _ := path.Match(expected, content); matched {
1545 filesMatched[expected] = true
1546 return
1547 }
1548 }
1549 surplus = append(surplus, content)
1550 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001551 for _, cmd := range strings.Split(copyCmds, "&&") {
1552 cmd = strings.TrimSpace(cmd)
1553 if cmd == "" {
1554 continue
1555 }
1556 terms := strings.Split(cmd, " ")
1557 switch terms[0] {
1558 case "mkdir":
1559 case "cp":
1560 if len(terms) != 3 {
1561 t.Fatal("copyCmds contains invalid cp command", cmd)
1562 }
1563 dst := terms[2]
1564 index := strings.Index(dst, imageApexDir)
1565 if index == -1 {
1566 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1567 }
1568 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001569 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001570 default:
1571 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1572 }
1573 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001574
Jooyung Han31c470b2019-10-18 16:26:59 +09001575 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001576 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001577 t.Log("surplus files", surplus)
1578 failed = true
1579 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001580
1581 if len(files) > len(filesMatched) {
1582 var missing []string
1583 for _, expected := range files {
1584 if !filesMatched[expected] {
1585 missing = append(missing, expected)
1586 }
1587 }
1588 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001589 t.Log("missing files", missing)
1590 failed = true
1591 }
1592 if failed {
1593 t.Fail()
1594 }
1595}
1596
Jooyung Han344d5432019-08-23 11:17:39 +09001597func TestVndkApexCurrent(t *testing.T) {
1598 ctx, _ := testApex(t, `
1599 apex_vndk {
1600 name: "myapex",
1601 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001602 }
1603
1604 apex_key {
1605 name: "myapex.key",
1606 public_key: "testkey.avbpubkey",
1607 private_key: "testkey.pem",
1608 }
1609
1610 cc_library {
1611 name: "libvndk",
1612 srcs: ["mylib.cpp"],
1613 vendor_available: true,
1614 vndk: {
1615 enabled: true,
1616 },
1617 system_shared_libs: [],
1618 stl: "none",
1619 }
1620
1621 cc_library {
1622 name: "libvndksp",
1623 srcs: ["mylib.cpp"],
1624 vendor_available: true,
1625 vndk: {
1626 enabled: true,
1627 support_system_process: true,
1628 },
1629 system_shared_libs: [],
1630 stl: "none",
1631 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001632 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001633
Jooyung Han31c470b2019-10-18 16:26:59 +09001634 ensureExactContents(t, ctx, "myapex", []string{
1635 "lib/libvndk.so",
1636 "lib/libvndksp.so",
1637 "lib64/libvndk.so",
1638 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001639 "etc/llndk.libraries.VER.txt",
1640 "etc/vndkcore.libraries.VER.txt",
1641 "etc/vndksp.libraries.VER.txt",
1642 "etc/vndkprivate.libraries.VER.txt",
1643 "etc/vndkcorevariant.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001644 })
Jooyung Han344d5432019-08-23 11:17:39 +09001645}
1646
1647func TestVndkApexWithPrebuilt(t *testing.T) {
1648 ctx, _ := testApex(t, `
1649 apex_vndk {
1650 name: "myapex",
1651 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001652 }
1653
1654 apex_key {
1655 name: "myapex.key",
1656 public_key: "testkey.avbpubkey",
1657 private_key: "testkey.pem",
1658 }
1659
1660 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001661 name: "libvndk",
1662 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001663 vendor_available: true,
1664 vndk: {
1665 enabled: true,
1666 },
1667 system_shared_libs: [],
1668 stl: "none",
1669 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001670
1671 cc_prebuilt_library_shared {
1672 name: "libvndk.arm",
1673 srcs: ["libvndk.arm.so"],
1674 vendor_available: true,
1675 vndk: {
1676 enabled: true,
1677 },
1678 enabled: false,
1679 arch: {
1680 arm: {
1681 enabled: true,
1682 },
1683 },
1684 system_shared_libs: [],
1685 stl: "none",
1686 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001687 `+vndkLibrariesTxtFiles("current"),
1688 withFiles(map[string][]byte{
1689 "libvndk.so": nil,
1690 "libvndk.arm.so": nil,
1691 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001692
Jooyung Han31c470b2019-10-18 16:26:59 +09001693 ensureExactContents(t, ctx, "myapex", []string{
1694 "lib/libvndk.so",
1695 "lib/libvndk.arm.so",
1696 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001697 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001698 })
Jooyung Han344d5432019-08-23 11:17:39 +09001699}
1700
Jooyung Han39edb6c2019-11-06 16:53:07 +09001701func vndkLibrariesTxtFiles(vers ...string) (result string) {
1702 for _, v := range vers {
1703 if v == "current" {
1704 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkcorevariant"} {
1705 result += `
1706 vndk_libraries_txt {
1707 name: "` + txt + `.libraries.txt",
1708 }
1709 `
1710 }
1711 } else {
1712 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1713 result += `
1714 prebuilt_etc {
1715 name: "` + txt + `.libraries.` + v + `.txt",
1716 src: "dummy.txt",
1717 }
1718 `
1719 }
1720 }
1721 }
1722 return
1723}
1724
Jooyung Han344d5432019-08-23 11:17:39 +09001725func TestVndkApexVersion(t *testing.T) {
1726 ctx, _ := testApex(t, `
1727 apex_vndk {
1728 name: "myapex_v27",
1729 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001730 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001731 vndk_version: "27",
1732 }
1733
1734 apex_key {
1735 name: "myapex.key",
1736 public_key: "testkey.avbpubkey",
1737 private_key: "testkey.pem",
1738 }
1739
Jooyung Han31c470b2019-10-18 16:26:59 +09001740 vndk_prebuilt_shared {
1741 name: "libvndk27",
1742 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001743 vendor_available: true,
1744 vndk: {
1745 enabled: true,
1746 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001747 target_arch: "arm64",
1748 arch: {
1749 arm: {
1750 srcs: ["libvndk27_arm.so"],
1751 },
1752 arm64: {
1753 srcs: ["libvndk27_arm64.so"],
1754 },
1755 },
Jooyung Han344d5432019-08-23 11:17:39 +09001756 }
1757
1758 vndk_prebuilt_shared {
1759 name: "libvndk27",
1760 version: "27",
1761 vendor_available: true,
1762 vndk: {
1763 enabled: true,
1764 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001765 target_arch: "x86_64",
1766 arch: {
1767 x86: {
1768 srcs: ["libvndk27_x86.so"],
1769 },
1770 x86_64: {
1771 srcs: ["libvndk27_x86_64.so"],
1772 },
1773 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001774 }
1775 `+vndkLibrariesTxtFiles("27"),
1776 withFiles(map[string][]byte{
1777 "libvndk27_arm.so": nil,
1778 "libvndk27_arm64.so": nil,
1779 "libvndk27_x86.so": nil,
1780 "libvndk27_x86_64.so": nil,
1781 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001782
Jooyung Han31c470b2019-10-18 16:26:59 +09001783 ensureExactContents(t, ctx, "myapex_v27", []string{
1784 "lib/libvndk27_arm.so",
1785 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001786 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001787 })
Jooyung Han344d5432019-08-23 11:17:39 +09001788}
1789
1790func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1791 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1792 apex_vndk {
1793 name: "myapex_v27",
1794 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001795 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001796 vndk_version: "27",
1797 }
1798 apex_vndk {
1799 name: "myapex_v27_other",
1800 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001801 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001802 vndk_version: "27",
1803 }
1804
1805 apex_key {
1806 name: "myapex.key",
1807 public_key: "testkey.avbpubkey",
1808 private_key: "testkey.pem",
1809 }
1810
1811 cc_library {
1812 name: "libvndk",
1813 srcs: ["mylib.cpp"],
1814 vendor_available: true,
1815 vndk: {
1816 enabled: true,
1817 },
1818 system_shared_libs: [],
1819 stl: "none",
1820 }
1821
1822 vndk_prebuilt_shared {
1823 name: "libvndk",
1824 version: "27",
1825 vendor_available: true,
1826 vndk: {
1827 enabled: true,
1828 },
1829 srcs: ["libvndk.so"],
1830 }
1831 `, withFiles(map[string][]byte{
1832 "libvndk.so": nil,
1833 }))
1834}
1835
Jooyung Han90eee022019-10-01 20:02:42 +09001836func TestVndkApexNameRule(t *testing.T) {
1837 ctx, _ := testApex(t, `
1838 apex_vndk {
1839 name: "myapex",
1840 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001841 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001842 }
1843 apex_vndk {
1844 name: "myapex_v28",
1845 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001846 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001847 vndk_version: "28",
1848 }
1849 apex_key {
1850 name: "myapex.key",
1851 public_key: "testkey.avbpubkey",
1852 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001853 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001854
1855 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001856 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001857 actual := proptools.String(bundle.properties.Apex_name)
1858 if !reflect.DeepEqual(actual, expected) {
1859 t.Errorf("Got '%v', expected '%v'", actual, expected)
1860 }
1861 }
1862
1863 assertApexName("com.android.vndk.vVER", "myapex")
1864 assertApexName("com.android.vndk.v28", "myapex_v28")
1865}
1866
Jooyung Han344d5432019-08-23 11:17:39 +09001867func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1868 ctx, _ := testApex(t, `
1869 apex_vndk {
1870 name: "myapex",
1871 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001872 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001873 }
1874
1875 apex_key {
1876 name: "myapex.key",
1877 public_key: "testkey.avbpubkey",
1878 private_key: "testkey.pem",
1879 }
1880
1881 cc_library {
1882 name: "libvndk",
1883 srcs: ["mylib.cpp"],
1884 vendor_available: true,
1885 native_bridge_supported: true,
1886 host_supported: true,
1887 vndk: {
1888 enabled: true,
1889 },
1890 system_shared_libs: [],
1891 stl: "none",
1892 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001893 `+vndkLibrariesTxtFiles("current"),
1894 withTargets(map[android.OsType][]android.Target{
1895 android.Android: []android.Target{
1896 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1897 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1898 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1899 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1900 },
1901 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001902
Jooyung Han31c470b2019-10-18 16:26:59 +09001903 ensureExactContents(t, ctx, "myapex", []string{
1904 "lib/libvndk.so",
1905 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001906 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001907 })
Jooyung Han344d5432019-08-23 11:17:39 +09001908}
1909
1910func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1911 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1912 apex_vndk {
1913 name: "myapex",
1914 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001915 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001916 native_bridge_supported: true,
1917 }
1918
1919 apex_key {
1920 name: "myapex.key",
1921 public_key: "testkey.avbpubkey",
1922 private_key: "testkey.pem",
1923 }
1924
1925 cc_library {
1926 name: "libvndk",
1927 srcs: ["mylib.cpp"],
1928 vendor_available: true,
1929 native_bridge_supported: true,
1930 host_supported: true,
1931 vndk: {
1932 enabled: true,
1933 },
1934 system_shared_libs: [],
1935 stl: "none",
1936 }
1937 `)
1938}
1939
Jooyung Han31c470b2019-10-18 16:26:59 +09001940func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001941 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001942 apex_vndk {
1943 name: "myapex_v27",
1944 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001945 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001946 vndk_version: "27",
1947 }
1948
1949 apex_key {
1950 name: "myapex.key",
1951 public_key: "testkey.avbpubkey",
1952 private_key: "testkey.pem",
1953 }
1954
1955 vndk_prebuilt_shared {
1956 name: "libvndk27",
1957 version: "27",
1958 target_arch: "arm",
1959 vendor_available: true,
1960 vndk: {
1961 enabled: true,
1962 },
1963 arch: {
1964 arm: {
1965 srcs: ["libvndk27.so"],
1966 }
1967 },
1968 }
1969
1970 vndk_prebuilt_shared {
1971 name: "libvndk27",
1972 version: "27",
1973 target_arch: "arm",
1974 binder32bit: true,
1975 vendor_available: true,
1976 vndk: {
1977 enabled: true,
1978 },
1979 arch: {
1980 arm: {
1981 srcs: ["libvndk27binder32.so"],
1982 }
1983 },
1984 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001985 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001986 withFiles(map[string][]byte{
1987 "libvndk27.so": nil,
1988 "libvndk27binder32.so": nil,
1989 }),
1990 withBinder32bit,
1991 withTargets(map[android.OsType][]android.Target{
1992 android.Android: []android.Target{
1993 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1994 },
1995 }),
1996 )
1997
1998 ensureExactContents(t, ctx, "myapex_v27", []string{
1999 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002000 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002001 })
2002}
2003
Jooyung Hane1633032019-08-01 17:41:43 +09002004func TestDependenciesInApexManifest(t *testing.T) {
2005 ctx, _ := testApex(t, `
2006 apex {
2007 name: "myapex_nodep",
2008 key: "myapex.key",
2009 native_shared_libs: ["lib_nodep"],
2010 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002011 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002012 }
2013
2014 apex {
2015 name: "myapex_dep",
2016 key: "myapex.key",
2017 native_shared_libs: ["lib_dep"],
2018 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002019 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002020 }
2021
2022 apex {
2023 name: "myapex_provider",
2024 key: "myapex.key",
2025 native_shared_libs: ["libfoo"],
2026 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002027 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002028 }
2029
2030 apex {
2031 name: "myapex_selfcontained",
2032 key: "myapex.key",
2033 native_shared_libs: ["lib_dep", "libfoo"],
2034 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002035 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002036 }
2037
2038 apex_key {
2039 name: "myapex.key",
2040 public_key: "testkey.avbpubkey",
2041 private_key: "testkey.pem",
2042 }
2043
2044 cc_library {
2045 name: "lib_nodep",
2046 srcs: ["mylib.cpp"],
2047 system_shared_libs: [],
2048 stl: "none",
2049 }
2050
2051 cc_library {
2052 name: "lib_dep",
2053 srcs: ["mylib.cpp"],
2054 shared_libs: ["libfoo"],
2055 system_shared_libs: [],
2056 stl: "none",
2057 }
2058
2059 cc_library {
2060 name: "libfoo",
2061 srcs: ["mytest.cpp"],
2062 stubs: {
2063 versions: ["1"],
2064 },
2065 system_shared_libs: [],
2066 stl: "none",
2067 }
2068 `)
2069
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002070 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002071 var provideNativeLibs, requireNativeLibs []string
2072
Sundong Ahnabb64432019-10-22 13:58:29 +09002073 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002074 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2075 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002076 ensureListEmpty(t, provideNativeLibs)
2077 ensureListEmpty(t, requireNativeLibs)
2078
Sundong Ahnabb64432019-10-22 13:58:29 +09002079 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002080 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2081 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002082 ensureListEmpty(t, provideNativeLibs)
2083 ensureListContains(t, requireNativeLibs, "libfoo.so")
2084
Sundong Ahnabb64432019-10-22 13:58:29 +09002085 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002086 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2087 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002088 ensureListContains(t, provideNativeLibs, "libfoo.so")
2089 ensureListEmpty(t, requireNativeLibs)
2090
Sundong Ahnabb64432019-10-22 13:58:29 +09002091 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002092 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2093 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002094 ensureListContains(t, provideNativeLibs, "libfoo.so")
2095 ensureListEmpty(t, requireNativeLibs)
2096}
2097
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002098func TestApexName(t *testing.T) {
2099 ctx, _ := testApex(t, `
2100 apex {
2101 name: "myapex",
2102 key: "myapex.key",
2103 apex_name: "com.android.myapex",
2104 }
2105
2106 apex_key {
2107 name: "myapex.key",
2108 public_key: "testkey.avbpubkey",
2109 private_key: "testkey.pem",
2110 }
2111 `)
2112
Sundong Ahnabb64432019-10-22 13:58:29 +09002113 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002114 apexManifestRule := module.Rule("apexManifestRule")
2115 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2116 apexRule := module.Rule("apexRule")
2117 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
2118}
2119
Alex Light0851b882019-02-07 13:20:53 -08002120func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002121 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002122 apex {
2123 name: "myapex",
2124 key: "myapex.key",
2125 native_shared_libs: ["mylib_common"],
2126 }
2127
2128 apex_key {
2129 name: "myapex.key",
2130 public_key: "testkey.avbpubkey",
2131 private_key: "testkey.pem",
2132 }
2133
2134 cc_library {
2135 name: "mylib_common",
2136 srcs: ["mylib.cpp"],
2137 system_shared_libs: [],
2138 stl: "none",
2139 }
2140 `)
2141
Sundong Ahnabb64432019-10-22 13:58:29 +09002142 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002143 apexRule := module.Rule("apexRule")
2144 copyCmds := apexRule.Args["copy_commands"]
2145
2146 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2147 t.Log("Apex was a test apex!")
2148 t.Fail()
2149 }
2150 // Ensure that main rule creates an output
2151 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2152
2153 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002154 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002155
2156 // Ensure that both direct and indirect deps are copied into apex
2157 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2158
Colin Cross7113d202019-11-20 16:39:12 -08002159 // Ensure that the platform variant ends with _shared
2160 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002161
2162 if !android.InAnyApex("mylib_common") {
2163 t.Log("Found mylib_common not in any apex!")
2164 t.Fail()
2165 }
2166}
2167
2168func TestTestApex(t *testing.T) {
2169 if android.InAnyApex("mylib_common_test") {
2170 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!")
2171 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002172 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002173 apex_test {
2174 name: "myapex",
2175 key: "myapex.key",
2176 native_shared_libs: ["mylib_common_test"],
2177 }
2178
2179 apex_key {
2180 name: "myapex.key",
2181 public_key: "testkey.avbpubkey",
2182 private_key: "testkey.pem",
2183 }
2184
2185 cc_library {
2186 name: "mylib_common_test",
2187 srcs: ["mylib.cpp"],
2188 system_shared_libs: [],
2189 stl: "none",
2190 }
2191 `)
2192
Sundong Ahnabb64432019-10-22 13:58:29 +09002193 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002194 apexRule := module.Rule("apexRule")
2195 copyCmds := apexRule.Args["copy_commands"]
2196
2197 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2198 t.Log("Apex was not a test apex!")
2199 t.Fail()
2200 }
2201 // Ensure that main rule creates an output
2202 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2203
2204 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002205 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002206
2207 // Ensure that both direct and indirect deps are copied into apex
2208 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2209
Colin Cross7113d202019-11-20 16:39:12 -08002210 // Ensure that the platform variant ends with _shared
2211 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002212
2213 if android.InAnyApex("mylib_common_test") {
2214 t.Log("Found mylib_common_test in some apex!")
2215 t.Fail()
2216 }
2217}
2218
Alex Light9670d332019-01-29 18:07:33 -08002219func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002220 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002221 apex {
2222 name: "myapex",
2223 key: "myapex.key",
2224 multilib: {
2225 first: {
2226 native_shared_libs: ["mylib_common"],
2227 }
2228 },
2229 target: {
2230 android: {
2231 multilib: {
2232 first: {
2233 native_shared_libs: ["mylib"],
2234 }
2235 }
2236 },
2237 host: {
2238 multilib: {
2239 first: {
2240 native_shared_libs: ["mylib2"],
2241 }
2242 }
2243 }
2244 }
2245 }
2246
2247 apex_key {
2248 name: "myapex.key",
2249 public_key: "testkey.avbpubkey",
2250 private_key: "testkey.pem",
2251 }
2252
2253 cc_library {
2254 name: "mylib",
2255 srcs: ["mylib.cpp"],
2256 system_shared_libs: [],
2257 stl: "none",
2258 }
2259
2260 cc_library {
2261 name: "mylib_common",
2262 srcs: ["mylib.cpp"],
2263 system_shared_libs: [],
2264 stl: "none",
2265 compile_multilib: "first",
2266 }
2267
2268 cc_library {
2269 name: "mylib2",
2270 srcs: ["mylib.cpp"],
2271 system_shared_libs: [],
2272 stl: "none",
2273 compile_multilib: "first",
2274 }
2275 `)
2276
Sundong Ahnabb64432019-10-22 13:58:29 +09002277 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002278 copyCmds := apexRule.Args["copy_commands"]
2279
2280 // Ensure that main rule creates an output
2281 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2282
2283 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002284 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2285 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2286 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002287
2288 // Ensure that both direct and indirect deps are copied into apex
2289 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2290 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2291 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2292
Colin Cross7113d202019-11-20 16:39:12 -08002293 // Ensure that the platform variant ends with _shared
2294 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2295 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2296 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002297}
Jiyong Park04480cf2019-02-06 00:16:29 +09002298
2299func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002300 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002301 apex {
2302 name: "myapex",
2303 key: "myapex.key",
2304 binaries: ["myscript"],
2305 }
2306
2307 apex_key {
2308 name: "myapex.key",
2309 public_key: "testkey.avbpubkey",
2310 private_key: "testkey.pem",
2311 }
2312
2313 sh_binary {
2314 name: "myscript",
2315 src: "mylib.cpp",
2316 filename: "myscript.sh",
2317 sub_dir: "script",
2318 }
2319 `)
2320
Sundong Ahnabb64432019-10-22 13:58:29 +09002321 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002322 copyCmds := apexRule.Args["copy_commands"]
2323
2324 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2325}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002326
Jooyung Han91df2082019-11-20 01:49:42 +09002327func TestApexInVariousPartition(t *testing.T) {
2328 testcases := []struct {
2329 propName, parition, flattenedPartition string
2330 }{
2331 {"", "system", "system_ext"},
2332 {"product_specific: true", "product", "product"},
2333 {"soc_specific: true", "vendor", "vendor"},
2334 {"proprietary: true", "vendor", "vendor"},
2335 {"vendor: true", "vendor", "vendor"},
2336 {"system_ext_specific: true", "system_ext", "system_ext"},
2337 }
2338 for _, tc := range testcases {
2339 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2340 ctx, _ := testApex(t, `
2341 apex {
2342 name: "myapex",
2343 key: "myapex.key",
2344 `+tc.propName+`
2345 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002346
Jooyung Han91df2082019-11-20 01:49:42 +09002347 apex_key {
2348 name: "myapex.key",
2349 public_key: "testkey.avbpubkey",
2350 private_key: "testkey.pem",
2351 }
2352 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002353
Jooyung Han91df2082019-11-20 01:49:42 +09002354 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2355 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2356 actual := apex.installDir.String()
2357 if actual != expected {
2358 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2359 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002360
Jooyung Han91df2082019-11-20 01:49:42 +09002361 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2362 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2363 actual = flattened.installDir.String()
2364 if actual != expected {
2365 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2366 }
2367 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002368 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002369}
Jiyong Park67882562019-03-21 01:11:21 +09002370
Jooyung Han54aca7b2019-11-20 02:26:02 +09002371func TestFileContexts(t *testing.T) {
2372 ctx, _ := testApex(t, `
2373 apex {
2374 name: "myapex",
2375 key: "myapex.key",
2376 }
2377
2378 apex_key {
2379 name: "myapex.key",
2380 public_key: "testkey.avbpubkey",
2381 private_key: "testkey.pem",
2382 }
2383 `)
2384 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2385 apexRule := module.Rule("apexRule")
2386 actual := apexRule.Args["file_contexts"]
2387 expected := "system/sepolicy/apex/myapex-file_contexts"
2388 if actual != expected {
2389 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2390 }
2391
2392 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2393 apex {
2394 name: "myapex",
2395 key: "myapex.key",
2396 file_contexts: "my_own_file_contexts",
2397 }
2398
2399 apex_key {
2400 name: "myapex.key",
2401 public_key: "testkey.avbpubkey",
2402 private_key: "testkey.pem",
2403 }
2404 `, withFiles(map[string][]byte{
2405 "my_own_file_contexts": nil,
2406 }))
2407
2408 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2409 apex {
2410 name: "myapex",
2411 key: "myapex.key",
2412 product_specific: true,
2413 file_contexts: "product_specific_file_contexts",
2414 }
2415
2416 apex_key {
2417 name: "myapex.key",
2418 public_key: "testkey.avbpubkey",
2419 private_key: "testkey.pem",
2420 }
2421 `)
2422
2423 ctx, _ = testApex(t, `
2424 apex {
2425 name: "myapex",
2426 key: "myapex.key",
2427 product_specific: true,
2428 file_contexts: "product_specific_file_contexts",
2429 }
2430
2431 apex_key {
2432 name: "myapex.key",
2433 public_key: "testkey.avbpubkey",
2434 private_key: "testkey.pem",
2435 }
2436 `, withFiles(map[string][]byte{
2437 "product_specific_file_contexts": nil,
2438 }))
2439 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2440 apexRule = module.Rule("apexRule")
2441 actual = apexRule.Args["file_contexts"]
2442 expected = "product_specific_file_contexts"
2443 if actual != expected {
2444 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2445 }
2446
2447 ctx, _ = testApex(t, `
2448 apex {
2449 name: "myapex",
2450 key: "myapex.key",
2451 product_specific: true,
2452 file_contexts: ":my-file-contexts",
2453 }
2454
2455 apex_key {
2456 name: "myapex.key",
2457 public_key: "testkey.avbpubkey",
2458 private_key: "testkey.pem",
2459 }
2460
2461 filegroup {
2462 name: "my-file-contexts",
2463 srcs: ["product_specific_file_contexts"],
2464 }
2465 `, withFiles(map[string][]byte{
2466 "product_specific_file_contexts": nil,
2467 }))
2468 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2469 apexRule = module.Rule("apexRule")
2470 actual = apexRule.Args["file_contexts"]
2471 expected = "product_specific_file_contexts"
2472 if actual != expected {
2473 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2474 }
2475}
2476
Jiyong Park67882562019-03-21 01:11:21 +09002477func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002478 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002479 apex_key {
2480 name: "myapex.key",
2481 public_key: ":my.avbpubkey",
2482 private_key: ":my.pem",
2483 product_specific: true,
2484 }
2485
2486 filegroup {
2487 name: "my.avbpubkey",
2488 srcs: ["testkey2.avbpubkey"],
2489 }
2490
2491 filegroup {
2492 name: "my.pem",
2493 srcs: ["testkey2.pem"],
2494 }
2495 `)
2496
2497 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2498 expected_pubkey := "testkey2.avbpubkey"
2499 actual_pubkey := apex_key.public_key_file.String()
2500 if actual_pubkey != expected_pubkey {
2501 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2502 }
2503 expected_privkey := "testkey2.pem"
2504 actual_privkey := apex_key.private_key_file.String()
2505 if actual_privkey != expected_privkey {
2506 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2507 }
2508}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002509
2510func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002511 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002512 prebuilt_apex {
2513 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002514 arch: {
2515 arm64: {
2516 src: "myapex-arm64.apex",
2517 },
2518 arm: {
2519 src: "myapex-arm.apex",
2520 },
2521 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002522 }
2523 `)
2524
2525 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2526
Jiyong Parkc95714e2019-03-29 14:23:10 +09002527 expectedInput := "myapex-arm64.apex"
2528 if prebuilt.inputApex.String() != expectedInput {
2529 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2530 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002531}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002532
2533func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002534 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002535 prebuilt_apex {
2536 name: "myapex",
2537 src: "myapex-arm.apex",
2538 filename: "notmyapex.apex",
2539 }
2540 `)
2541
2542 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2543
2544 expected := "notmyapex.apex"
2545 if p.installFilename != expected {
2546 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2547 }
2548}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002549
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002550func TestPrebuiltOverrides(t *testing.T) {
2551 ctx, config := testApex(t, `
2552 prebuilt_apex {
2553 name: "myapex.prebuilt",
2554 src: "myapex-arm.apex",
2555 overrides: [
2556 "myapex",
2557 ],
2558 }
2559 `)
2560
2561 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2562
2563 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002564 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002565 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002566 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002567 }
2568}
2569
Roland Levillain630846d2019-06-26 12:48:34 +01002570func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002571 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002572 apex_test {
2573 name: "myapex",
2574 key: "myapex.key",
2575 tests: [
2576 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002577 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002578 ],
2579 }
2580
2581 apex_key {
2582 name: "myapex.key",
2583 public_key: "testkey.avbpubkey",
2584 private_key: "testkey.pem",
2585 }
2586
2587 cc_test {
2588 name: "mytest",
2589 gtest: false,
2590 srcs: ["mytest.cpp"],
2591 relative_install_path: "test",
2592 system_shared_libs: [],
2593 static_executable: true,
2594 stl: "none",
2595 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002596
2597 cc_test {
2598 name: "mytests",
2599 gtest: false,
2600 srcs: [
2601 "mytest1.cpp",
2602 "mytest2.cpp",
2603 "mytest3.cpp",
2604 ],
2605 test_per_src: true,
2606 relative_install_path: "test",
2607 system_shared_libs: [],
2608 static_executable: true,
2609 stl: "none",
2610 }
Roland Levillain630846d2019-06-26 12:48:34 +01002611 `)
2612
Sundong Ahnabb64432019-10-22 13:58:29 +09002613 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002614 copyCmds := apexRule.Args["copy_commands"]
2615
2616 // Ensure that test dep is copied into apex.
2617 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002618
2619 // Ensure that test deps built with `test_per_src` are copied into apex.
2620 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2621 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2622 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002623
2624 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002625 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002626 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2627 name := apexBundle.BaseModuleName()
2628 prefix := "TARGET_"
2629 var builder strings.Builder
2630 data.Custom(&builder, name, prefix, "", data)
2631 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002632 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2633 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2634 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2635 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002636 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002637 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002638 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002639}
2640
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002641func TestInstallExtraFlattenedApexes(t *testing.T) {
2642 ctx, config := testApex(t, `
2643 apex {
2644 name: "myapex",
2645 key: "myapex.key",
2646 }
2647 apex_key {
2648 name: "myapex.key",
2649 public_key: "testkey.avbpubkey",
2650 private_key: "testkey.pem",
2651 }
2652 `, func(fs map[string][]byte, config android.Config) {
2653 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2654 })
2655 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2656 ensureListContains(t, ab.externalDeps, "myapex.flattened")
2657 mk := android.AndroidMkDataForTest(t, config, "", ab)
2658 var builder strings.Builder
2659 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2660 androidMk := builder.String()
2661 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2662}
2663
Jooyung Han5c998b92019-06-27 11:30:33 +09002664func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002665 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002666 apex {
2667 name: "myapex",
2668 key: "myapex.key",
2669 native_shared_libs: ["mylib"],
2670 uses: ["commonapex"],
2671 }
2672
2673 apex {
2674 name: "commonapex",
2675 key: "myapex.key",
2676 native_shared_libs: ["libcommon"],
2677 provide_cpp_shared_libs: true,
2678 }
2679
2680 apex_key {
2681 name: "myapex.key",
2682 public_key: "testkey.avbpubkey",
2683 private_key: "testkey.pem",
2684 }
2685
2686 cc_library {
2687 name: "mylib",
2688 srcs: ["mylib.cpp"],
2689 shared_libs: ["libcommon"],
2690 system_shared_libs: [],
2691 stl: "none",
2692 }
2693
2694 cc_library {
2695 name: "libcommon",
2696 srcs: ["mylib_common.cpp"],
2697 system_shared_libs: [],
2698 stl: "none",
2699 }
2700 `)
2701
Sundong Ahnabb64432019-10-22 13:58:29 +09002702 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002703 apexRule1 := module1.Rule("apexRule")
2704 copyCmds1 := apexRule1.Args["copy_commands"]
2705
Sundong Ahnabb64432019-10-22 13:58:29 +09002706 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002707 apexRule2 := module2.Rule("apexRule")
2708 copyCmds2 := apexRule2.Args["copy_commands"]
2709
Colin Cross7113d202019-11-20 16:39:12 -08002710 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2711 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002712 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2713 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2714 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2715}
2716
2717func TestApexUsesFailsIfNotProvided(t *testing.T) {
2718 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2719 apex {
2720 name: "myapex",
2721 key: "myapex.key",
2722 uses: ["commonapex"],
2723 }
2724
2725 apex {
2726 name: "commonapex",
2727 key: "myapex.key",
2728 }
2729
2730 apex_key {
2731 name: "myapex.key",
2732 public_key: "testkey.avbpubkey",
2733 private_key: "testkey.pem",
2734 }
2735 `)
2736 testApexError(t, `uses: "commonapex" is not a provider`, `
2737 apex {
2738 name: "myapex",
2739 key: "myapex.key",
2740 uses: ["commonapex"],
2741 }
2742
2743 cc_library {
2744 name: "commonapex",
2745 system_shared_libs: [],
2746 stl: "none",
2747 }
2748
2749 apex_key {
2750 name: "myapex.key",
2751 public_key: "testkey.avbpubkey",
2752 private_key: "testkey.pem",
2753 }
2754 `)
2755}
2756
2757func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2758 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2759 apex {
2760 name: "myapex",
2761 key: "myapex.key",
2762 use_vendor: true,
2763 uses: ["commonapex"],
2764 }
2765
2766 apex {
2767 name: "commonapex",
2768 key: "myapex.key",
2769 provide_cpp_shared_libs: true,
2770 }
2771
2772 apex_key {
2773 name: "myapex.key",
2774 public_key: "testkey.avbpubkey",
2775 private_key: "testkey.pem",
2776 }
Jooyung Handc782442019-11-01 03:14:38 +09002777 `, func(fs map[string][]byte, config android.Config) {
2778 setUseVendorWhitelistForTest(config, []string{"myapex"})
2779 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002780}
2781
Jooyung Hand48f3c32019-08-23 11:18:57 +09002782func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2783 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2784 apex {
2785 name: "myapex",
2786 key: "myapex.key",
2787 native_shared_libs: ["libfoo"],
2788 }
2789
2790 apex_key {
2791 name: "myapex.key",
2792 public_key: "testkey.avbpubkey",
2793 private_key: "testkey.pem",
2794 }
2795
2796 cc_library {
2797 name: "libfoo",
2798 stl: "none",
2799 system_shared_libs: [],
2800 enabled: false,
2801 }
2802 `)
2803 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2804 apex {
2805 name: "myapex",
2806 key: "myapex.key",
2807 java_libs: ["myjar"],
2808 }
2809
2810 apex_key {
2811 name: "myapex.key",
2812 public_key: "testkey.avbpubkey",
2813 private_key: "testkey.pem",
2814 }
2815
2816 java_library {
2817 name: "myjar",
2818 srcs: ["foo/bar/MyClass.java"],
2819 sdk_version: "none",
2820 system_modules: "none",
2821 compile_dex: true,
2822 enabled: false,
2823 }
2824 `)
2825}
2826
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002827func TestApexWithApps(t *testing.T) {
2828 ctx, _ := testApex(t, `
2829 apex {
2830 name: "myapex",
2831 key: "myapex.key",
2832 apps: [
2833 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002834 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002835 ],
2836 }
2837
2838 apex_key {
2839 name: "myapex.key",
2840 public_key: "testkey.avbpubkey",
2841 private_key: "testkey.pem",
2842 }
2843
2844 android_app {
2845 name: "AppFoo",
2846 srcs: ["foo/bar/MyClass.java"],
2847 sdk_version: "none",
2848 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002849 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002850 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002851
2852 android_app {
2853 name: "AppFooPriv",
2854 srcs: ["foo/bar/MyClass.java"],
2855 sdk_version: "none",
2856 system_modules: "none",
2857 privileged: true,
2858 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002859
2860 cc_library_shared {
2861 name: "libjni",
2862 srcs: ["mylib.cpp"],
2863 stl: "none",
2864 system_shared_libs: [],
2865 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002866 `)
2867
Sundong Ahnabb64432019-10-22 13:58:29 +09002868 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002869 apexRule := module.Rule("apexRule")
2870 copyCmds := apexRule.Args["copy_commands"]
2871
2872 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002873 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002874
2875 // JNI libraries are embedded inside APK
2876 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
Colin Cross7113d202019-11-20 16:39:12 -08002877 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002878 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2879 // ... uncompressed
2880 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2881 t.Errorf("jni lib is not uncompressed for AppFoo")
2882 }
2883 // ... and not directly inside the APEX
2884 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002885}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002886
Dario Frenicde2a032019-10-27 00:29:22 +01002887func TestApexWithAppImports(t *testing.T) {
2888 ctx, _ := testApex(t, `
2889 apex {
2890 name: "myapex",
2891 key: "myapex.key",
2892 apps: [
2893 "AppFooPrebuilt",
2894 "AppFooPrivPrebuilt",
2895 ],
2896 }
2897
2898 apex_key {
2899 name: "myapex.key",
2900 public_key: "testkey.avbpubkey",
2901 private_key: "testkey.pem",
2902 }
2903
2904 android_app_import {
2905 name: "AppFooPrebuilt",
2906 apk: "PrebuiltAppFoo.apk",
2907 presigned: true,
2908 dex_preopt: {
2909 enabled: false,
2910 },
2911 }
2912
2913 android_app_import {
2914 name: "AppFooPrivPrebuilt",
2915 apk: "PrebuiltAppFooPriv.apk",
2916 privileged: true,
2917 presigned: true,
2918 dex_preopt: {
2919 enabled: false,
2920 },
2921 }
2922 `)
2923
Sundong Ahnabb64432019-10-22 13:58:29 +09002924 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002925 apexRule := module.Rule("apexRule")
2926 copyCmds := apexRule.Args["copy_commands"]
2927
2928 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2929 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002930}
2931
Dario Freni6f3937c2019-12-20 22:58:03 +00002932func TestApexWithTestHelperApp(t *testing.T) {
2933 ctx, _ := testApex(t, `
2934 apex {
2935 name: "myapex",
2936 key: "myapex.key",
2937 apps: [
2938 "TesterHelpAppFoo",
2939 ],
2940 }
2941
2942 apex_key {
2943 name: "myapex.key",
2944 public_key: "testkey.avbpubkey",
2945 private_key: "testkey.pem",
2946 }
2947
2948 android_test_helper_app {
2949 name: "TesterHelpAppFoo",
2950 srcs: ["foo/bar/MyClass.java"],
2951 }
2952
2953 `)
2954
2955 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2956 apexRule := module.Rule("apexRule")
2957 copyCmds := apexRule.Args["copy_commands"]
2958
2959 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
2960}
2961
Jooyung Han18020ea2019-11-13 10:50:48 +09002962func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2963 // libfoo's apex_available comes from cc_defaults
2964 testApexError(t, `"myapex" .*: 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: "myapex.key",
2980 native_shared_libs: ["libfoo"],
2981 }
2982
2983 cc_defaults {
2984 name: "libfoo-defaults",
2985 apex_available: ["otherapex"],
2986 }
2987
2988 cc_library {
2989 name: "libfoo",
2990 defaults: ["libfoo-defaults"],
2991 stl: "none",
2992 system_shared_libs: [],
2993 }`)
2994}
2995
Jiyong Park127b40b2019-09-30 16:04:35 +09002996func TestApexAvailable(t *testing.T) {
2997 // libfoo is not available to myapex, but only to otherapex
2998 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2999 apex {
3000 name: "myapex",
3001 key: "myapex.key",
3002 native_shared_libs: ["libfoo"],
3003 }
3004
3005 apex_key {
3006 name: "myapex.key",
3007 public_key: "testkey.avbpubkey",
3008 private_key: "testkey.pem",
3009 }
3010
3011 apex {
3012 name: "otherapex",
3013 key: "otherapex.key",
3014 native_shared_libs: ["libfoo"],
3015 }
3016
3017 apex_key {
3018 name: "otherapex.key",
3019 public_key: "testkey.avbpubkey",
3020 private_key: "testkey.pem",
3021 }
3022
3023 cc_library {
3024 name: "libfoo",
3025 stl: "none",
3026 system_shared_libs: [],
3027 apex_available: ["otherapex"],
3028 }`)
3029
3030 // libbar is an indirect dep
3031 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3032 apex {
3033 name: "myapex",
3034 key: "myapex.key",
3035 native_shared_libs: ["libfoo"],
3036 }
3037
3038 apex_key {
3039 name: "myapex.key",
3040 public_key: "testkey.avbpubkey",
3041 private_key: "testkey.pem",
3042 }
3043
3044 apex {
3045 name: "otherapex",
3046 key: "otherapex.key",
3047 native_shared_libs: ["libfoo"],
3048 }
3049
3050 apex_key {
3051 name: "otherapex.key",
3052 public_key: "testkey.avbpubkey",
3053 private_key: "testkey.pem",
3054 }
3055
3056 cc_library {
3057 name: "libfoo",
3058 stl: "none",
3059 shared_libs: ["libbar"],
3060 system_shared_libs: [],
3061 apex_available: ["myapex", "otherapex"],
3062 }
3063
3064 cc_library {
3065 name: "libbar",
3066 stl: "none",
3067 system_shared_libs: [],
3068 apex_available: ["otherapex"],
3069 }`)
3070
3071 testApexError(t, "\"otherapex\" is not a valid module name", `
3072 apex {
3073 name: "myapex",
3074 key: "myapex.key",
3075 native_shared_libs: ["libfoo"],
3076 }
3077
3078 apex_key {
3079 name: "myapex.key",
3080 public_key: "testkey.avbpubkey",
3081 private_key: "testkey.pem",
3082 }
3083
3084 cc_library {
3085 name: "libfoo",
3086 stl: "none",
3087 system_shared_libs: [],
3088 apex_available: ["otherapex"],
3089 }`)
3090
3091 ctx, _ := testApex(t, `
3092 apex {
3093 name: "myapex",
3094 key: "myapex.key",
3095 native_shared_libs: ["libfoo", "libbar"],
3096 }
3097
3098 apex_key {
3099 name: "myapex.key",
3100 public_key: "testkey.avbpubkey",
3101 private_key: "testkey.pem",
3102 }
3103
3104 cc_library {
3105 name: "libfoo",
3106 stl: "none",
3107 system_shared_libs: [],
3108 apex_available: ["myapex"],
3109 }
3110
3111 cc_library {
3112 name: "libbar",
3113 stl: "none",
3114 system_shared_libs: [],
3115 apex_available: ["//apex_available:anyapex"],
3116 }`)
3117
3118 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003119 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3120 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3121 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3122 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003123
3124 ctx, _ = testApex(t, `
3125 apex {
3126 name: "myapex",
3127 key: "myapex.key",
3128 }
3129
3130 apex_key {
3131 name: "myapex.key",
3132 public_key: "testkey.avbpubkey",
3133 private_key: "testkey.pem",
3134 }
3135
3136 cc_library {
3137 name: "libfoo",
3138 stl: "none",
3139 system_shared_libs: [],
3140 apex_available: ["//apex_available:platform"],
3141 }`)
3142
3143 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003144 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3145 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003146
3147 ctx, _ = testApex(t, `
3148 apex {
3149 name: "myapex",
3150 key: "myapex.key",
3151 native_shared_libs: ["libfoo"],
3152 }
3153
3154 apex_key {
3155 name: "myapex.key",
3156 public_key: "testkey.avbpubkey",
3157 private_key: "testkey.pem",
3158 }
3159
3160 cc_library {
3161 name: "libfoo",
3162 stl: "none",
3163 system_shared_libs: [],
3164 apex_available: ["myapex"],
3165 static: {
3166 apex_available: ["//apex_available:platform"],
3167 },
3168 }`)
3169
3170 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08003171 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3172 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003173 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08003174 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3175 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003176}
3177
Jiyong Park5d790c32019-11-15 18:40:32 +09003178func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003179 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003180 apex {
3181 name: "myapex",
3182 key: "myapex.key",
3183 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003184 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003185 }
3186
3187 override_apex {
3188 name: "override_myapex",
3189 base: "myapex",
3190 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003191 overrides: ["unknownapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003192 }
3193
3194 apex_key {
3195 name: "myapex.key",
3196 public_key: "testkey.avbpubkey",
3197 private_key: "testkey.pem",
3198 }
3199
3200 android_app {
3201 name: "app",
3202 srcs: ["foo/bar/MyClass.java"],
3203 package_name: "foo",
3204 sdk_version: "none",
3205 system_modules: "none",
3206 }
3207
3208 override_android_app {
3209 name: "override_app",
3210 base: "app",
3211 package_name: "bar",
3212 }
3213 `)
3214
Jiyong Park317645e2019-12-05 13:20:58 +09003215 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3216 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3217 if originalVariant.GetOverriddenBy() != "" {
3218 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3219 }
3220 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3221 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3222 }
3223
Jiyong Park5d790c32019-11-15 18:40:32 +09003224 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3225 apexRule := module.Rule("apexRule")
3226 copyCmds := apexRule.Args["copy_commands"]
3227
3228 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3229 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003230
3231 apexBundle := module.Module().(*apexBundle)
3232 name := apexBundle.Name()
3233 if name != "override_myapex" {
3234 t.Errorf("name should be \"override_myapex\", but was %q", name)
3235 }
3236
3237 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3238 var builder strings.Builder
3239 data.Custom(&builder, name, "TARGET_", "", data)
3240 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003241 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003242 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3243 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003244 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003245 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003246 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003247 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3248 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003249}
3250
Jooyung Han214bf372019-11-12 13:03:50 +09003251func TestLegacyAndroid10Support(t *testing.T) {
3252 ctx, _ := testApex(t, `
3253 apex {
3254 name: "myapex",
3255 key: "myapex.key",
3256 legacy_android10_support: true,
3257 }
3258
3259 apex_key {
3260 name: "myapex.key",
3261 public_key: "testkey.avbpubkey",
3262 private_key: "testkey.pem",
3263 }
3264 `)
3265
3266 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3267 args := module.Rule("apexRule").Args
3268 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
3269}
3270
Jooyung Han58f26ab2019-12-18 15:34:32 +09003271func TestJavaSDKLibrary(t *testing.T) {
3272 ctx, _ := testApex(t, `
3273 apex {
3274 name: "myapex",
3275 key: "myapex.key",
3276 java_libs: ["foo"],
3277 }
3278
3279 apex_key {
3280 name: "myapex.key",
3281 public_key: "testkey.avbpubkey",
3282 private_key: "testkey.pem",
3283 }
3284
3285 java_sdk_library {
3286 name: "foo",
3287 srcs: ["a.java"],
3288 api_packages: ["foo"],
3289 }
3290 `, withFiles(map[string][]byte{
3291 "api/current.txt": nil,
3292 "api/removed.txt": nil,
3293 "api/system-current.txt": nil,
3294 "api/system-removed.txt": nil,
3295 "api/test-current.txt": nil,
3296 "api/test-removed.txt": nil,
3297 }))
3298
3299 // java_sdk_library installs both impl jar and permission XML
3300 ensureExactContents(t, ctx, "myapex", []string{
3301 "javalib/foo.jar",
3302 "etc/permissions/foo.xml",
3303 })
3304 // Permission XML should point to the activated path of impl jar of java_sdk_library
3305 genXMLCommand := ctx.ModuleForTests("foo", "android_common_myapex").Output("foo.xml").RuleParams.Command
3306 ensureContains(t, genXMLCommand, `<library name="foo" file="/apex/myapex/javalib/foo.jar"`)
3307}
3308
Jiyong Park479321d2019-12-16 11:47:12 +09003309func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3310 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3311 apex {
3312 name: "myapex",
3313 key: "myapex.key",
3314 java_libs: ["myjar"],
3315 }
3316
3317 apex_key {
3318 name: "myapex.key",
3319 public_key: "testkey.avbpubkey",
3320 private_key: "testkey.pem",
3321 }
3322
3323 java_library {
3324 name: "myjar",
3325 srcs: ["foo/bar/MyClass.java"],
3326 sdk_version: "none",
3327 system_modules: "none",
3328 }
3329 `)
3330}
3331
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003332func TestMain(m *testing.M) {
3333 run := func() int {
3334 setUp()
3335 defer tearDown()
3336
3337 return m.Run()
3338 }
3339
3340 os.Exit(run())
3341}