blob: de5fdf7ce4583ca5c5303c65f7ddde51a23f27e8 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Jooyung Han31c470b2019-10-18 16:26:59 +090022 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090023 "strings"
24 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090025
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090030 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090031)
32
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070033var buildDir string
34
Jooyung Hand3639552019-08-09 12:57:43 +090035// names returns name list from white space separated string
36func names(s string) (ns []string) {
37 for _, n := range strings.Split(s, " ") {
38 if len(n) > 0 {
39 ns = append(ns, n)
40 }
41 }
42 return
43}
44
Jooyung Han344d5432019-08-23 11:17:39 +090045func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
46 t.Helper()
47 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090048 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
49 if len(errs) > 0 {
50 android.FailIfNoMatchingErrors(t, pattern, errs)
51 return
52 }
53 _, errs = ctx.PrepareBuildActions(config)
54 if len(errs) > 0 {
55 android.FailIfNoMatchingErrors(t, pattern, errs)
56 return
57 }
58
59 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
60}
61
Jooyung Han344d5432019-08-23 11:17:39 +090062func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
63 t.Helper()
64 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090065 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
66 android.FailIfErrored(t, errs)
67 _, errs = ctx.PrepareBuildActions(config)
68 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070069 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090070}
71
Jooyung Han344d5432019-08-23 11:17:39 +090072type testCustomizer func(fs map[string][]byte, config android.Config)
73
74func withFiles(files map[string][]byte) testCustomizer {
75 return func(fs map[string][]byte, config android.Config) {
76 for k, v := range files {
77 fs[k] = v
78 }
79 }
80}
81
82func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
83 return func(fs map[string][]byte, config android.Config) {
84 for k, v := range targets {
85 config.Targets[k] = v
86 }
87 }
88}
89
Jooyung Han31c470b2019-10-18 16:26:59 +090090func withBinder32bit(fs map[string][]byte, config android.Config) {
91 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
92}
93
Jooyung Han344d5432019-08-23 11:17:39 +090094func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +090095 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +090096
97 bp = bp + `
98 toolchain_library {
99 name: "libcompiler_rt-extras",
100 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900101 vendor_available: true,
102 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900103 }
104
105 toolchain_library {
106 name: "libatomic",
107 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900108 vendor_available: true,
109 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900110 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900111 }
112
113 toolchain_library {
114 name: "libgcc",
115 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900116 vendor_available: true,
117 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900118 }
119
120 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700121 name: "libgcc_stripped",
122 src: "",
123 vendor_available: true,
124 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900125 native_bridge_supported: true,
Yi Kongacee27c2019-03-29 20:05:14 -0700126 }
127
128 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900129 name: "libclang_rt.builtins-aarch64-android",
130 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900131 vendor_available: true,
132 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900133 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900134 }
135
136 toolchain_library {
137 name: "libclang_rt.builtins-arm-android",
138 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900139 vendor_available: true,
140 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900141 native_bridge_supported: true,
142 }
143
144 toolchain_library {
145 name: "libclang_rt.builtins-x86_64-android",
146 src: "",
147 vendor_available: true,
148 recovery_available: true,
149 native_bridge_supported: true,
150 }
151
152 toolchain_library {
153 name: "libclang_rt.builtins-i686-android",
154 src: "",
155 vendor_available: true,
156 recovery_available: true,
157 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900158 }
159
160 cc_object {
161 name: "crtbegin_so",
162 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900163 vendor_available: true,
164 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900165 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900166 }
167
168 cc_object {
169 name: "crtend_so",
170 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900171 vendor_available: true,
172 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900173 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900174 }
175
Alex Light3d673592019-01-18 14:37:31 -0800176 cc_object {
177 name: "crtbegin_static",
178 stl: "none",
179 }
180
181 cc_object {
182 name: "crtend_android",
183 stl: "none",
184 }
185
Jiyong Parkda6eb592018-12-19 17:12:36 +0900186 llndk_library {
187 name: "libc",
188 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900189 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900190 }
191
192 llndk_library {
193 name: "libm",
194 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900195 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900196 }
197
198 llndk_library {
199 name: "libdl",
200 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900201 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900202 }
Jooyung Han54aca7b2019-11-20 02:26:02 +0900203
204 filegroup {
205 name: "myapex-file_contexts",
206 srcs: [
207 "system/sepolicy/apex/myapex-file_contexts",
208 ],
209 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900210 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800211
Dario Frenicde2a032019-10-27 00:29:22 +0100212 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900213
Jooyung Han344d5432019-08-23 11:17:39 +0900214 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900215 "a.java": nil,
216 "PrebuiltAppFoo.apk": nil,
217 "PrebuiltAppFooPriv.apk": nil,
218 "build/make/target/product/security": nil,
219 "apex_manifest.json": nil,
220 "AndroidManifest.xml": nil,
221 "system/sepolicy/apex/myapex-file_contexts": nil,
222 "system/sepolicy/apex/otherapex-file_contexts": nil,
223 "system/sepolicy/apex/commonapex-file_contexts": nil,
224 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800225 "mylib.cpp": nil,
226 "mylib_common.cpp": nil,
227 "mytest.cpp": nil,
228 "mytest1.cpp": nil,
229 "mytest2.cpp": nil,
230 "mytest3.cpp": nil,
231 "myprebuilt": nil,
232 "my_include": nil,
233 "foo/bar/MyClass.java": nil,
234 "prebuilt.jar": nil,
235 "vendor/foo/devkeys/test.x509.pem": nil,
236 "vendor/foo/devkeys/test.pk8": nil,
237 "testkey.x509.pem": nil,
238 "testkey.pk8": nil,
239 "testkey.override.x509.pem": nil,
240 "testkey.override.pk8": nil,
241 "vendor/foo/devkeys/testkey.avbpubkey": nil,
242 "vendor/foo/devkeys/testkey.pem": nil,
243 "NOTICE": nil,
244 "custom_notice": nil,
245 "testkey2.avbpubkey": nil,
246 "testkey2.pem": nil,
247 "myapex-arm64.apex": nil,
248 "myapex-arm.apex": nil,
249 "frameworks/base/api/current.txt": nil,
250 "framework/aidl/a.aidl": nil,
251 "build/make/core/proguard.flags": nil,
252 "build/make/core/proguard_basic_keeps.flags": nil,
253 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900254 }
255
256 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800257 // The fs now needs to be populated before creating the config, call handlers twice
258 // for now, once to get any fs changes, and later after the config was created to
259 // set product variables or targets.
260 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
261 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900262 }
263
Colin Cross98be1bb2019-12-13 20:41:13 -0800264 config := android.TestArchConfig(buildDir, nil, bp, fs)
265 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
266 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
267 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
268 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
269 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
270 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
271
272 for _, handler := range handlers {
273 // The fs now needs to be populated before creating the config, call handlers twice
274 // for now, earlier to get any fs changes, and now after the config was created to
275 // set product variables or targets.
276 tempFS := map[string][]byte{}
277 handler(tempFS, config)
278 }
279
280 ctx := android.NewTestArchContext()
281 ctx.RegisterModuleType("apex", BundleFactory)
282 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
283 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
284 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
285 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
286 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
287 ctx.RegisterModuleType("override_apex", overrideApexFactory)
288
Colin Cross98be1bb2019-12-13 20:41:13 -0800289 ctx.RegisterModuleType("cc_library_shared", cc.LibrarySharedFactory)
290 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
Paul Duffin59986b22019-12-19 14:38:36 +0000291 cc.RegisterPrebuiltBuildComponents(ctx)
Paul Duffin77980a82019-12-19 16:01:36 +0000292 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800293 ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800294 ctx.RegisterModuleType("cc_defaults", func() android.Module {
295 return cc.DefaultsFactory()
296 })
297 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800298 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
299 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800300 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
301 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800302 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000303 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000304 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000305 java.RegisterAppBuildComponents(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800306
307 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin0c4979b2019-12-19 15:11:53 +0000308 android.RegisterPrebuiltMutators(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800309 ctx.PreDepsMutators(RegisterPreDepsMutators)
310 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
311 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800312
313 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900314
Jooyung Han5c998b92019-06-27 11:30:33 +0900315 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900316}
317
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700318func setUp() {
319 var err error
320 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900321 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700322 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900324}
325
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700326func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900327 os.RemoveAll(buildDir)
328}
329
330// ensure that 'result' contains 'expected'
331func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900332 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900333 if !strings.Contains(result, expected) {
334 t.Errorf("%q is not found in %q", expected, result)
335 }
336}
337
338// ensures that 'result' does not contain 'notExpected'
339func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900340 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900341 if strings.Contains(result, notExpected) {
342 t.Errorf("%q is found in %q", notExpected, result)
343 }
344}
345
346func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900347 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900348 if !android.InList(expected, result) {
349 t.Errorf("%q is not found in %v", expected, result)
350 }
351}
352
353func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900354 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900355 if android.InList(notExpected, result) {
356 t.Errorf("%q is found in %v", notExpected, result)
357 }
358}
359
Jooyung Hane1633032019-08-01 17:41:43 +0900360func ensureListEmpty(t *testing.T, result []string) {
361 t.Helper()
362 if len(result) > 0 {
363 t.Errorf("%q is expected to be empty", result)
364 }
365}
366
Jiyong Park25fc6a92018-11-18 18:02:45 +0900367// Minimal test
368func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700369 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900370 apex_defaults {
371 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900372 manifest: ":myapex.manifest",
373 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900374 key: "myapex.key",
375 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800376 multilib: {
377 both: {
378 binaries: ["foo",],
379 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900380 },
Jiyong Park9e6c2422019-08-09 20:39:45 +0900381 java_libs: ["myjar", "myprebuiltjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900382 }
383
Jiyong Park30ca9372019-02-07 16:27:23 +0900384 apex {
385 name: "myapex",
386 defaults: ["myapex-defaults"],
387 }
388
Jiyong Park25fc6a92018-11-18 18:02:45 +0900389 apex_key {
390 name: "myapex.key",
391 public_key: "testkey.avbpubkey",
392 private_key: "testkey.pem",
393 }
394
Jiyong Park809bb722019-02-13 21:33:49 +0900395 filegroup {
396 name: "myapex.manifest",
397 srcs: ["apex_manifest.json"],
398 }
399
400 filegroup {
401 name: "myapex.androidmanifest",
402 srcs: ["AndroidManifest.xml"],
403 }
404
Jiyong Park25fc6a92018-11-18 18:02:45 +0900405 cc_library {
406 name: "mylib",
407 srcs: ["mylib.cpp"],
408 shared_libs: ["mylib2"],
409 system_shared_libs: [],
410 stl: "none",
411 }
412
Alex Light3d673592019-01-18 14:37:31 -0800413 cc_binary {
414 name: "foo",
415 srcs: ["mylib.cpp"],
416 compile_multilib: "both",
417 multilib: {
418 lib32: {
419 suffix: "32",
420 },
421 lib64: {
422 suffix: "64",
423 },
424 },
425 symlinks: ["foo_link_"],
426 symlink_preferred_arch: true,
427 system_shared_libs: [],
428 static_executable: true,
429 stl: "none",
430 }
431
Jiyong Park25fc6a92018-11-18 18:02:45 +0900432 cc_library {
433 name: "mylib2",
434 srcs: ["mylib.cpp"],
435 system_shared_libs: [],
436 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900437 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900438 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900439
440 java_library {
441 name: "myjar",
442 srcs: ["foo/bar/MyClass.java"],
443 sdk_version: "none",
444 system_modules: "none",
445 compile_dex: true,
446 static_libs: ["myotherjar"],
447 }
448
449 java_library {
450 name: "myotherjar",
451 srcs: ["foo/bar/MyClass.java"],
452 sdk_version: "none",
453 system_modules: "none",
454 compile_dex: true,
455 }
Jiyong Park9e6c2422019-08-09 20:39:45 +0900456
457 java_import {
458 name: "myprebuiltjar",
459 jars: ["prebuilt.jar"],
460 installable: true,
461 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900462 `)
463
Sundong Ahnabb64432019-10-22 13:58:29 +0900464 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900465
466 optFlags := apexRule.Args["opt_flags"]
467 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700468 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900469 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900470
Jiyong Park25fc6a92018-11-18 18:02:45 +0900471 copyCmds := apexRule.Args["copy_commands"]
472
473 // Ensure that main rule creates an output
474 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
475
476 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800477 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900478 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900479 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900480
481 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800482 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900483 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900484
485 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800486 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
487 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900488 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900489 ensureContains(t, copyCmds, "image.apex/javalib/myprebuiltjar.jar")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900490 // .. but not for java libs
491 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800492
Colin Cross7113d202019-11-20 16:39:12 -0800493 // Ensure that the platform variant ends with _shared or _common
494 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
495 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900496 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
497 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900498 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common")
Alex Light3d673592019-01-18 14:37:31 -0800499
500 // Ensure that all symlinks are present.
501 found_foo_link_64 := false
502 found_foo := false
503 for _, cmd := range strings.Split(copyCmds, " && ") {
504 if strings.HasPrefix(cmd, "ln -s foo64") {
505 if strings.HasSuffix(cmd, "bin/foo") {
506 found_foo = true
507 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
508 found_foo_link_64 = true
509 }
510 }
511 }
512 good := found_foo && found_foo_link_64
513 if !good {
514 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
515 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900516
Sundong Ahnabb64432019-10-22 13:58:29 +0900517 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700518 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700519 if len(noticeInputs) != 2 {
520 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900521 }
522 ensureListContains(t, noticeInputs, "NOTICE")
523 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800524}
525
Jooyung Hanf21c7972019-12-16 22:32:06 +0900526func TestDefaults(t *testing.T) {
527 ctx, _ := testApex(t, `
528 apex_defaults {
529 name: "myapex-defaults",
530 key: "myapex.key",
531 prebuilts: ["myetc"],
532 native_shared_libs: ["mylib"],
533 java_libs: ["myjar"],
534 apps: ["AppFoo"],
535 }
536
537 prebuilt_etc {
538 name: "myetc",
539 src: "myprebuilt",
540 }
541
542 apex {
543 name: "myapex",
544 defaults: ["myapex-defaults"],
545 }
546
547 apex_key {
548 name: "myapex.key",
549 public_key: "testkey.avbpubkey",
550 private_key: "testkey.pem",
551 }
552
553 cc_library {
554 name: "mylib",
555 system_shared_libs: [],
556 stl: "none",
557 }
558
559 java_library {
560 name: "myjar",
561 srcs: ["foo/bar/MyClass.java"],
562 sdk_version: "none",
563 system_modules: "none",
564 compile_dex: true,
565 }
566
567 android_app {
568 name: "AppFoo",
569 srcs: ["foo/bar/MyClass.java"],
570 sdk_version: "none",
571 system_modules: "none",
572 }
573 `)
574 ensureExactContents(t, ctx, "myapex", []string{
575 "etc/myetc",
576 "javalib/myjar.jar",
577 "lib64/mylib.so",
578 "app/AppFoo/AppFoo.apk",
579 })
580}
581
Jooyung Han01a3ee22019-11-02 02:52:25 +0900582func TestApexManifest(t *testing.T) {
583 ctx, _ := testApex(t, `
584 apex {
585 name: "myapex",
586 key: "myapex.key",
587 }
588
589 apex_key {
590 name: "myapex.key",
591 public_key: "testkey.avbpubkey",
592 private_key: "testkey.pem",
593 }
594 `)
595
596 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900597 args := module.Rule("apexRule").Args
598 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
599 t.Error("manifest should be apex_manifest.pb, but " + manifest)
600 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900601}
602
Alex Light5098a612018-11-29 17:12:15 -0800603func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700604 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800605 apex {
606 name: "myapex",
607 key: "myapex.key",
608 payload_type: "zip",
609 native_shared_libs: ["mylib"],
610 }
611
612 apex_key {
613 name: "myapex.key",
614 public_key: "testkey.avbpubkey",
615 private_key: "testkey.pem",
616 }
617
618 cc_library {
619 name: "mylib",
620 srcs: ["mylib.cpp"],
621 shared_libs: ["mylib2"],
622 system_shared_libs: [],
623 stl: "none",
624 }
625
626 cc_library {
627 name: "mylib2",
628 srcs: ["mylib.cpp"],
629 system_shared_libs: [],
630 stl: "none",
631 }
632 `)
633
Sundong Ahnabb64432019-10-22 13:58:29 +0900634 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800635 copyCmds := zipApexRule.Args["copy_commands"]
636
637 // Ensure that main rule creates an output
638 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
639
640 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800641 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800642
643 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800644 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800645
646 // Ensure that both direct and indirect deps are copied into apex
647 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
648 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900649}
650
651func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700652 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900653 apex {
654 name: "myapex",
655 key: "myapex.key",
656 native_shared_libs: ["mylib", "mylib3"],
657 }
658
659 apex_key {
660 name: "myapex.key",
661 public_key: "testkey.avbpubkey",
662 private_key: "testkey.pem",
663 }
664
665 cc_library {
666 name: "mylib",
667 srcs: ["mylib.cpp"],
668 shared_libs: ["mylib2", "mylib3"],
669 system_shared_libs: [],
670 stl: "none",
671 }
672
673 cc_library {
674 name: "mylib2",
675 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900676 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900677 system_shared_libs: [],
678 stl: "none",
679 stubs: {
680 versions: ["1", "2", "3"],
681 },
682 }
683
684 cc_library {
685 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900686 srcs: ["mylib.cpp"],
687 shared_libs: ["mylib4"],
688 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900689 stl: "none",
690 stubs: {
691 versions: ["10", "11", "12"],
692 },
693 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900694
695 cc_library {
696 name: "mylib4",
697 srcs: ["mylib.cpp"],
698 system_shared_libs: [],
699 stl: "none",
700 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900701 `)
702
Sundong Ahnabb64432019-10-22 13:58:29 +0900703 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900704 copyCmds := apexRule.Args["copy_commands"]
705
706 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800707 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900708
709 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800710 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900711
712 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800713 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900714
Colin Cross7113d202019-11-20 16:39:12 -0800715 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900716
717 // Ensure that mylib is linking with the latest version of stubs for mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800718 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900719 // ... and not linking to the non-stub (impl) variant of mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800720 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900721
722 // 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 -0800723 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900724 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800725 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900726
727 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800728 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900729 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900730
731 // Ensure that genstub is invoked with --apex
Colin Cross7113d202019-11-20 16:39:12 -0800732 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3_myapex").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900733
734 ensureExactContents(t, ctx, "myapex", []string{
735 "lib64/mylib.so",
736 "lib64/mylib3.so",
737 "lib64/mylib4.so",
738 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900739}
740
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900741func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700742 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900743 apex {
744 name: "myapex",
745 key: "myapex.key",
746 native_shared_libs: ["mylib"],
747 }
748
749 apex_key {
750 name: "myapex.key",
751 public_key: "testkey.avbpubkey",
752 private_key: "testkey.pem",
753 }
754
755 cc_library {
756 name: "mylib",
757 srcs: ["mylib.cpp"],
758 shared_libs: ["libfoo#10"],
759 system_shared_libs: [],
760 stl: "none",
761 }
762
763 cc_library {
764 name: "libfoo",
765 srcs: ["mylib.cpp"],
766 shared_libs: ["libbar"],
767 system_shared_libs: [],
768 stl: "none",
769 stubs: {
770 versions: ["10", "20", "30"],
771 },
772 }
773
774 cc_library {
775 name: "libbar",
776 srcs: ["mylib.cpp"],
777 system_shared_libs: [],
778 stl: "none",
779 }
780
781 `)
782
Sundong Ahnabb64432019-10-22 13:58:29 +0900783 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900784 copyCmds := apexRule.Args["copy_commands"]
785
786 // Ensure that direct non-stubs dep is always included
787 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
788
789 // Ensure that indirect stubs dep is not included
790 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
791
792 // Ensure that dependency of stubs is not included
793 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
794
Colin Cross7113d202019-11-20 16:39:12 -0800795 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900796
797 // Ensure that mylib is linking with version 10 of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800798 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900799 // ... and not linking to the non-stub (impl) variant of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800800 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900801
Colin Cross7113d202019-11-20 16:39:12 -0800802 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900803
804 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
805 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
806}
807
Jooyung Hand3639552019-08-09 12:57:43 +0900808func TestApexWithRuntimeLibsDependency(t *testing.T) {
809 /*
810 myapex
811 |
812 v (runtime_libs)
813 mylib ------+------> libfoo [provides stub]
814 |
815 `------> libbar
816 */
817 ctx, _ := testApex(t, `
818 apex {
819 name: "myapex",
820 key: "myapex.key",
821 native_shared_libs: ["mylib"],
822 }
823
824 apex_key {
825 name: "myapex.key",
826 public_key: "testkey.avbpubkey",
827 private_key: "testkey.pem",
828 }
829
830 cc_library {
831 name: "mylib",
832 srcs: ["mylib.cpp"],
833 runtime_libs: ["libfoo", "libbar"],
834 system_shared_libs: [],
835 stl: "none",
836 }
837
838 cc_library {
839 name: "libfoo",
840 srcs: ["mylib.cpp"],
841 system_shared_libs: [],
842 stl: "none",
843 stubs: {
844 versions: ["10", "20", "30"],
845 },
846 }
847
848 cc_library {
849 name: "libbar",
850 srcs: ["mylib.cpp"],
851 system_shared_libs: [],
852 stl: "none",
853 }
854
855 `)
856
Sundong Ahnabb64432019-10-22 13:58:29 +0900857 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900858 copyCmds := apexRule.Args["copy_commands"]
859
860 // Ensure that direct non-stubs dep is always included
861 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
862
863 // Ensure that indirect stubs dep is not included
864 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
865
866 // Ensure that runtime_libs dep in included
867 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
868
Sundong Ahnabb64432019-10-22 13:58:29 +0900869 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900870 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
871 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900872
873}
874
Jooyung Han9c80bae2019-08-20 17:30:57 +0900875func TestApexDependencyToLLNDK(t *testing.T) {
876 ctx, _ := testApex(t, `
877 apex {
878 name: "myapex",
879 key: "myapex.key",
880 use_vendor: true,
881 native_shared_libs: ["mylib"],
882 }
883
884 apex_key {
885 name: "myapex.key",
886 public_key: "testkey.avbpubkey",
887 private_key: "testkey.pem",
888 }
889
890 cc_library {
891 name: "mylib",
892 srcs: ["mylib.cpp"],
893 vendor_available: true,
894 shared_libs: ["libbar"],
895 system_shared_libs: [],
896 stl: "none",
897 }
898
899 cc_library {
900 name: "libbar",
901 srcs: ["mylib.cpp"],
902 system_shared_libs: [],
903 stl: "none",
904 }
905
906 llndk_library {
907 name: "libbar",
908 symbol_file: "",
909 }
Jooyung Handc782442019-11-01 03:14:38 +0900910 `, func(fs map[string][]byte, config android.Config) {
911 setUseVendorWhitelistForTest(config, []string{"myapex"})
912 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900913
Sundong Ahnabb64432019-10-22 13:58:29 +0900914 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900915 copyCmds := apexRule.Args["copy_commands"]
916
917 // Ensure that LLNDK dep is not included
918 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
919
Sundong Ahnabb64432019-10-22 13:58:29 +0900920 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900921 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900922
923 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900924 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900925
926}
927
Jiyong Park25fc6a92018-11-18 18:02:45 +0900928func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700929 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900930 apex {
931 name: "myapex",
932 key: "myapex.key",
933 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
934 }
935
936 apex_key {
937 name: "myapex.key",
938 public_key: "testkey.avbpubkey",
939 private_key: "testkey.pem",
940 }
941
942 cc_library {
943 name: "mylib",
944 srcs: ["mylib.cpp"],
945 shared_libs: ["libdl#27"],
946 stl: "none",
947 }
948
949 cc_library_shared {
950 name: "mylib_shared",
951 srcs: ["mylib.cpp"],
952 shared_libs: ["libdl#27"],
953 stl: "none",
954 }
955
956 cc_library {
957 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700958 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900959 nocrt: true,
960 system_shared_libs: [],
961 stl: "none",
962 stubs: {
963 versions: ["27", "28", "29"],
964 },
965 }
966
967 cc_library {
968 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700969 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900970 nocrt: true,
971 system_shared_libs: [],
972 stl: "none",
973 stubs: {
974 versions: ["27", "28", "29"],
975 },
976 }
977
978 cc_library {
979 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700980 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900981 nocrt: true,
982 system_shared_libs: [],
983 stl: "none",
984 stubs: {
985 versions: ["27", "28", "29"],
986 },
987 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900988
989 cc_library {
990 name: "libBootstrap",
991 srcs: ["mylib.cpp"],
992 stl: "none",
993 bootstrap: true,
994 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900995 `)
996
Sundong Ahnabb64432019-10-22 13:58:29 +0900997 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900998 copyCmds := apexRule.Args["copy_commands"]
999
1000 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001001 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001002 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1003 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001004
1005 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001006 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001007
Colin Cross7113d202019-11-20 16:39:12 -08001008 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1009 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1010 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001011
1012 // For dependency to libc
1013 // Ensure that mylib is linking with the latest version of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001014 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001015 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001016 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001017 // ... Cflags from stub is correctly exported to mylib
1018 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1019 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1020
1021 // For dependency to libm
1022 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001023 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001024 // ... and not linking to the stub variant
Colin Cross7113d202019-11-20 16:39:12 -08001025 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001026 // ... and is not compiling with the stub
1027 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1028 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1029
1030 // For dependency to libdl
1031 // Ensure that mylib is linking with the specified version of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001032 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001033 // ... and not linking to the other versions of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001034 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28_myapex/libdl.so")
1035 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001036 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001037 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001038 // ... Cflags from stub is correctly exported to mylib
1039 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1040 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001041
1042 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001043 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1044 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1045 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1046 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001047}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001048
1049func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001050 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001051 apex {
1052 name: "myapex",
1053 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001054 native_shared_libs: ["mylib"],
1055 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001056 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001057 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001058 }
1059
1060 apex_key {
1061 name: "myapex.key",
1062 public_key: "testkey.avbpubkey",
1063 private_key: "testkey.pem",
1064 }
1065
1066 prebuilt_etc {
1067 name: "myetc",
1068 src: "myprebuilt",
1069 sub_dir: "foo/bar",
1070 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001071
1072 cc_library {
1073 name: "mylib",
1074 srcs: ["mylib.cpp"],
1075 relative_install_path: "foo/bar",
1076 system_shared_libs: [],
1077 stl: "none",
1078 }
1079
1080 cc_binary {
1081 name: "mybin",
1082 srcs: ["mylib.cpp"],
1083 relative_install_path: "foo/bar",
1084 system_shared_libs: [],
1085 static_executable: true,
1086 stl: "none",
1087 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001088 `)
1089
Sundong Ahnabb64432019-10-22 13:58:29 +09001090 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001091 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1092
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001093 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001094 ensureListContains(t, dirs, "etc")
1095 ensureListContains(t, dirs, "etc/foo")
1096 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001097 ensureListContains(t, dirs, "lib64")
1098 ensureListContains(t, dirs, "lib64/foo")
1099 ensureListContains(t, dirs, "lib64/foo/bar")
1100 ensureListContains(t, dirs, "lib")
1101 ensureListContains(t, dirs, "lib/foo")
1102 ensureListContains(t, dirs, "lib/foo/bar")
1103
Jiyong Parkbd13e442019-03-15 18:10:35 +09001104 ensureListContains(t, dirs, "bin")
1105 ensureListContains(t, dirs, "bin/foo")
1106 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001107}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001108
1109func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001110 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001111 apex {
1112 name: "myapex",
1113 key: "myapex.key",
1114 native_shared_libs: ["mylib"],
1115 use_vendor: true,
1116 }
1117
1118 apex_key {
1119 name: "myapex.key",
1120 public_key: "testkey.avbpubkey",
1121 private_key: "testkey.pem",
1122 }
1123
1124 cc_library {
1125 name: "mylib",
1126 srcs: ["mylib.cpp"],
1127 shared_libs: ["mylib2"],
1128 system_shared_libs: [],
1129 vendor_available: true,
1130 stl: "none",
1131 }
1132
1133 cc_library {
1134 name: "mylib2",
1135 srcs: ["mylib.cpp"],
1136 system_shared_libs: [],
1137 vendor_available: true,
1138 stl: "none",
1139 }
Jooyung Handc782442019-11-01 03:14:38 +09001140 `, func(fs map[string][]byte, config android.Config) {
1141 setUseVendorWhitelistForTest(config, []string{"myapex"})
1142 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001143
1144 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001145 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001146 for _, implicit := range i.Implicits {
1147 inputsList = append(inputsList, implicit.String())
1148 }
1149 }
1150 inputsString := strings.Join(inputsList, " ")
1151
1152 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001153 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1154 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001155
1156 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001157 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1158 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001159}
Jiyong Park16e91a02018-12-20 18:18:08 +09001160
Jooyung Handc782442019-11-01 03:14:38 +09001161func TestUseVendorRestriction(t *testing.T) {
1162 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1163 apex {
1164 name: "myapex",
1165 key: "myapex.key",
1166 use_vendor: true,
1167 }
1168 apex_key {
1169 name: "myapex.key",
1170 public_key: "testkey.avbpubkey",
1171 private_key: "testkey.pem",
1172 }
1173 `, func(fs map[string][]byte, config android.Config) {
1174 setUseVendorWhitelistForTest(config, []string{""})
1175 })
1176 // no error with whitelist
1177 testApex(t, `
1178 apex {
1179 name: "myapex",
1180 key: "myapex.key",
1181 use_vendor: true,
1182 }
1183 apex_key {
1184 name: "myapex.key",
1185 public_key: "testkey.avbpubkey",
1186 private_key: "testkey.pem",
1187 }
1188 `, func(fs map[string][]byte, config android.Config) {
1189 setUseVendorWhitelistForTest(config, []string{"myapex"})
1190 })
1191}
1192
Jooyung Han5c998b92019-06-27 11:30:33 +09001193func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1194 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1195 apex {
1196 name: "myapex",
1197 key: "myapex.key",
1198 native_shared_libs: ["mylib"],
1199 use_vendor: true,
1200 }
1201
1202 apex_key {
1203 name: "myapex.key",
1204 public_key: "testkey.avbpubkey",
1205 private_key: "testkey.pem",
1206 }
1207
1208 cc_library {
1209 name: "mylib",
1210 srcs: ["mylib.cpp"],
1211 system_shared_libs: [],
1212 stl: "none",
1213 }
1214 `)
1215}
1216
Jiyong Park16e91a02018-12-20 18:18:08 +09001217func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001218 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001219 apex {
1220 name: "myapex",
1221 key: "myapex.key",
1222 native_shared_libs: ["mylib"],
1223 }
1224
1225 apex_key {
1226 name: "myapex.key",
1227 public_key: "testkey.avbpubkey",
1228 private_key: "testkey.pem",
1229 }
1230
1231 cc_library {
1232 name: "mylib",
1233 srcs: ["mylib.cpp"],
1234 system_shared_libs: [],
1235 stl: "none",
1236 stubs: {
1237 versions: ["1", "2", "3"],
1238 },
1239 }
1240
1241 cc_binary {
1242 name: "not_in_apex",
1243 srcs: ["mylib.cpp"],
1244 static_libs: ["mylib"],
1245 static_executable: true,
1246 system_shared_libs: [],
1247 stl: "none",
1248 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001249 `)
1250
Colin Cross7113d202019-11-20 16:39:12 -08001251 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001252
1253 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001254 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001255}
Jiyong Park9335a262018-12-24 11:31:58 +09001256
1257func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001258 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001259 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001260 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001261 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001262 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001263 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001264 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001265 }
1266
1267 cc_library {
1268 name: "mylib",
1269 srcs: ["mylib.cpp"],
1270 system_shared_libs: [],
1271 stl: "none",
1272 }
1273
1274 apex_key {
1275 name: "myapex.key",
1276 public_key: "testkey.avbpubkey",
1277 private_key: "testkey.pem",
1278 }
1279
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001280 android_app_certificate {
1281 name: "myapex.certificate",
1282 certificate: "testkey",
1283 }
1284
1285 android_app_certificate {
1286 name: "myapex.certificate.override",
1287 certificate: "testkey.override",
1288 }
1289
Jiyong Park9335a262018-12-24 11:31:58 +09001290 `)
1291
1292 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001293 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001294
1295 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1296 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1297 "vendor/foo/devkeys/testkey.avbpubkey")
1298 }
1299 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1300 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1301 "vendor/foo/devkeys/testkey.pem")
1302 }
1303
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001304 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001305 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001306 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001307 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001308 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001309 }
1310}
Jiyong Park58e364a2019-01-19 19:24:06 +09001311
Jooyung Hanf121a652019-12-17 14:30:11 +09001312func TestCertificate(t *testing.T) {
1313 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1314 ctx, _ := testApex(t, `
1315 apex {
1316 name: "myapex",
1317 key: "myapex.key",
1318 }
1319 apex_key {
1320 name: "myapex.key",
1321 public_key: "testkey.avbpubkey",
1322 private_key: "testkey.pem",
1323 }`)
1324 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1325 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1326 if actual := rule.Args["certificates"]; actual != expected {
1327 t.Errorf("certificates should be %q, not %q", expected, actual)
1328 }
1329 })
1330 t.Run("override when unspecified", func(t *testing.T) {
1331 ctx, _ := testApex(t, `
1332 apex {
1333 name: "myapex_keytest",
1334 key: "myapex.key",
1335 file_contexts: ":myapex-file_contexts",
1336 }
1337 apex_key {
1338 name: "myapex.key",
1339 public_key: "testkey.avbpubkey",
1340 private_key: "testkey.pem",
1341 }
1342 android_app_certificate {
1343 name: "myapex.certificate.override",
1344 certificate: "testkey.override",
1345 }`)
1346 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1347 expected := "testkey.override.x509.pem testkey.override.pk8"
1348 if actual := rule.Args["certificates"]; actual != expected {
1349 t.Errorf("certificates should be %q, not %q", expected, actual)
1350 }
1351 })
1352 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1353 ctx, _ := testApex(t, `
1354 apex {
1355 name: "myapex",
1356 key: "myapex.key",
1357 certificate: ":myapex.certificate",
1358 }
1359 apex_key {
1360 name: "myapex.key",
1361 public_key: "testkey.avbpubkey",
1362 private_key: "testkey.pem",
1363 }
1364 android_app_certificate {
1365 name: "myapex.certificate",
1366 certificate: "testkey",
1367 }`)
1368 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1369 expected := "testkey.x509.pem testkey.pk8"
1370 if actual := rule.Args["certificates"]; actual != expected {
1371 t.Errorf("certificates should be %q, not %q", expected, actual)
1372 }
1373 })
1374 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1375 ctx, _ := testApex(t, `
1376 apex {
1377 name: "myapex_keytest",
1378 key: "myapex.key",
1379 file_contexts: ":myapex-file_contexts",
1380 certificate: ":myapex.certificate",
1381 }
1382 apex_key {
1383 name: "myapex.key",
1384 public_key: "testkey.avbpubkey",
1385 private_key: "testkey.pem",
1386 }
1387 android_app_certificate {
1388 name: "myapex.certificate.override",
1389 certificate: "testkey.override",
1390 }`)
1391 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1392 expected := "testkey.override.x509.pem testkey.override.pk8"
1393 if actual := rule.Args["certificates"]; actual != expected {
1394 t.Errorf("certificates should be %q, not %q", expected, actual)
1395 }
1396 })
1397 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1398 ctx, _ := testApex(t, `
1399 apex {
1400 name: "myapex",
1401 key: "myapex.key",
1402 certificate: "testkey",
1403 }
1404 apex_key {
1405 name: "myapex.key",
1406 public_key: "testkey.avbpubkey",
1407 private_key: "testkey.pem",
1408 }`)
1409 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1410 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1411 if actual := rule.Args["certificates"]; actual != expected {
1412 t.Errorf("certificates should be %q, not %q", expected, actual)
1413 }
1414 })
1415 t.Run("override when specified as <name>", func(t *testing.T) {
1416 ctx, _ := testApex(t, `
1417 apex {
1418 name: "myapex_keytest",
1419 key: "myapex.key",
1420 file_contexts: ":myapex-file_contexts",
1421 certificate: "testkey",
1422 }
1423 apex_key {
1424 name: "myapex.key",
1425 public_key: "testkey.avbpubkey",
1426 private_key: "testkey.pem",
1427 }
1428 android_app_certificate {
1429 name: "myapex.certificate.override",
1430 certificate: "testkey.override",
1431 }`)
1432 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1433 expected := "testkey.override.x509.pem testkey.override.pk8"
1434 if actual := rule.Args["certificates"]; actual != expected {
1435 t.Errorf("certificates should be %q, not %q", expected, actual)
1436 }
1437 })
1438}
1439
Jiyong Park58e364a2019-01-19 19:24:06 +09001440func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001441 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001442 apex {
1443 name: "myapex",
1444 key: "myapex.key",
1445 native_shared_libs: ["mylib"],
1446 }
1447
1448 apex {
1449 name: "otherapex",
1450 key: "myapex.key",
1451 native_shared_libs: ["mylib"],
1452 }
1453
1454 apex_key {
1455 name: "myapex.key",
1456 public_key: "testkey.avbpubkey",
1457 private_key: "testkey.pem",
1458 }
1459
1460 cc_library {
1461 name: "mylib",
1462 srcs: ["mylib.cpp"],
1463 system_shared_libs: [],
1464 stl: "none",
1465 }
1466 `)
1467
Jooyung Han6b8459b2019-10-30 08:29:25 +09001468 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001469 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001470 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001471 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1472 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001473
Jooyung Han6b8459b2019-10-30 08:29:25 +09001474 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001475 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001476 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001477 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1478 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001479
Jooyung Han6b8459b2019-10-30 08:29:25 +09001480 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001481 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001482 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001483 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1484 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001485}
Jiyong Park7e636d02019-01-28 16:16:54 +09001486
1487func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001488 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001489 apex {
1490 name: "myapex",
1491 key: "myapex.key",
1492 native_shared_libs: ["mylib"],
1493 }
1494
1495 apex_key {
1496 name: "myapex.key",
1497 public_key: "testkey.avbpubkey",
1498 private_key: "testkey.pem",
1499 }
1500
1501 cc_library_headers {
1502 name: "mylib_headers",
1503 export_include_dirs: ["my_include"],
1504 system_shared_libs: [],
1505 stl: "none",
1506 }
1507
1508 cc_library {
1509 name: "mylib",
1510 srcs: ["mylib.cpp"],
1511 system_shared_libs: [],
1512 stl: "none",
1513 header_libs: ["mylib_headers"],
1514 export_header_lib_headers: ["mylib_headers"],
1515 stubs: {
1516 versions: ["1", "2", "3"],
1517 },
1518 }
1519
1520 cc_library {
1521 name: "otherlib",
1522 srcs: ["mylib.cpp"],
1523 system_shared_libs: [],
1524 stl: "none",
1525 shared_libs: ["mylib"],
1526 }
1527 `)
1528
Colin Cross7113d202019-11-20 16:39:12 -08001529 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001530
1531 // Ensure that the include path of the header lib is exported to 'otherlib'
1532 ensureContains(t, cFlags, "-Imy_include")
1533}
Alex Light9670d332019-01-29 18:07:33 -08001534
Jooyung Han31c470b2019-10-18 16:26:59 +09001535func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1536 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001537 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001538 copyCmds := apexRule.Args["copy_commands"]
1539 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001540 var failed bool
1541 var surplus []string
1542 filesMatched := make(map[string]bool)
1543 addContent := func(content string) {
1544 for _, expected := range files {
1545 if matched, _ := path.Match(expected, content); matched {
1546 filesMatched[expected] = true
1547 return
1548 }
1549 }
1550 surplus = append(surplus, content)
1551 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001552 for _, cmd := range strings.Split(copyCmds, "&&") {
1553 cmd = strings.TrimSpace(cmd)
1554 if cmd == "" {
1555 continue
1556 }
1557 terms := strings.Split(cmd, " ")
1558 switch terms[0] {
1559 case "mkdir":
1560 case "cp":
1561 if len(terms) != 3 {
1562 t.Fatal("copyCmds contains invalid cp command", cmd)
1563 }
1564 dst := terms[2]
1565 index := strings.Index(dst, imageApexDir)
1566 if index == -1 {
1567 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1568 }
1569 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001570 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001571 default:
1572 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1573 }
1574 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001575
Jooyung Han31c470b2019-10-18 16:26:59 +09001576 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001577 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001578 t.Log("surplus files", surplus)
1579 failed = true
1580 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001581
1582 if len(files) > len(filesMatched) {
1583 var missing []string
1584 for _, expected := range files {
1585 if !filesMatched[expected] {
1586 missing = append(missing, expected)
1587 }
1588 }
1589 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001590 t.Log("missing files", missing)
1591 failed = true
1592 }
1593 if failed {
1594 t.Fail()
1595 }
1596}
1597
Jooyung Han344d5432019-08-23 11:17:39 +09001598func TestVndkApexCurrent(t *testing.T) {
1599 ctx, _ := testApex(t, `
1600 apex_vndk {
1601 name: "myapex",
1602 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001603 }
1604
1605 apex_key {
1606 name: "myapex.key",
1607 public_key: "testkey.avbpubkey",
1608 private_key: "testkey.pem",
1609 }
1610
1611 cc_library {
1612 name: "libvndk",
1613 srcs: ["mylib.cpp"],
1614 vendor_available: true,
1615 vndk: {
1616 enabled: true,
1617 },
1618 system_shared_libs: [],
1619 stl: "none",
1620 }
1621
1622 cc_library {
1623 name: "libvndksp",
1624 srcs: ["mylib.cpp"],
1625 vendor_available: true,
1626 vndk: {
1627 enabled: true,
1628 support_system_process: true,
1629 },
1630 system_shared_libs: [],
1631 stl: "none",
1632 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001633 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001634
Jooyung Han31c470b2019-10-18 16:26:59 +09001635 ensureExactContents(t, ctx, "myapex", []string{
1636 "lib/libvndk.so",
1637 "lib/libvndksp.so",
1638 "lib64/libvndk.so",
1639 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001640 "etc/llndk.libraries.VER.txt",
1641 "etc/vndkcore.libraries.VER.txt",
1642 "etc/vndksp.libraries.VER.txt",
1643 "etc/vndkprivate.libraries.VER.txt",
1644 "etc/vndkcorevariant.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001645 })
Jooyung Han344d5432019-08-23 11:17:39 +09001646}
1647
1648func TestVndkApexWithPrebuilt(t *testing.T) {
1649 ctx, _ := testApex(t, `
1650 apex_vndk {
1651 name: "myapex",
1652 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001653 }
1654
1655 apex_key {
1656 name: "myapex.key",
1657 public_key: "testkey.avbpubkey",
1658 private_key: "testkey.pem",
1659 }
1660
1661 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001662 name: "libvndk",
1663 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001664 vendor_available: true,
1665 vndk: {
1666 enabled: true,
1667 },
1668 system_shared_libs: [],
1669 stl: "none",
1670 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001671
1672 cc_prebuilt_library_shared {
1673 name: "libvndk.arm",
1674 srcs: ["libvndk.arm.so"],
1675 vendor_available: true,
1676 vndk: {
1677 enabled: true,
1678 },
1679 enabled: false,
1680 arch: {
1681 arm: {
1682 enabled: true,
1683 },
1684 },
1685 system_shared_libs: [],
1686 stl: "none",
1687 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001688 `+vndkLibrariesTxtFiles("current"),
1689 withFiles(map[string][]byte{
1690 "libvndk.so": nil,
1691 "libvndk.arm.so": nil,
1692 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001693
Jooyung Han31c470b2019-10-18 16:26:59 +09001694 ensureExactContents(t, ctx, "myapex", []string{
1695 "lib/libvndk.so",
1696 "lib/libvndk.arm.so",
1697 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001698 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001699 })
Jooyung Han344d5432019-08-23 11:17:39 +09001700}
1701
Jooyung Han39edb6c2019-11-06 16:53:07 +09001702func vndkLibrariesTxtFiles(vers ...string) (result string) {
1703 for _, v := range vers {
1704 if v == "current" {
1705 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkcorevariant"} {
1706 result += `
1707 vndk_libraries_txt {
1708 name: "` + txt + `.libraries.txt",
1709 }
1710 `
1711 }
1712 } else {
1713 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1714 result += `
1715 prebuilt_etc {
1716 name: "` + txt + `.libraries.` + v + `.txt",
1717 src: "dummy.txt",
1718 }
1719 `
1720 }
1721 }
1722 }
1723 return
1724}
1725
Jooyung Han344d5432019-08-23 11:17:39 +09001726func TestVndkApexVersion(t *testing.T) {
1727 ctx, _ := testApex(t, `
1728 apex_vndk {
1729 name: "myapex_v27",
1730 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001731 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001732 vndk_version: "27",
1733 }
1734
1735 apex_key {
1736 name: "myapex.key",
1737 public_key: "testkey.avbpubkey",
1738 private_key: "testkey.pem",
1739 }
1740
Jooyung Han31c470b2019-10-18 16:26:59 +09001741 vndk_prebuilt_shared {
1742 name: "libvndk27",
1743 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001744 vendor_available: true,
1745 vndk: {
1746 enabled: true,
1747 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001748 target_arch: "arm64",
1749 arch: {
1750 arm: {
1751 srcs: ["libvndk27_arm.so"],
1752 },
1753 arm64: {
1754 srcs: ["libvndk27_arm64.so"],
1755 },
1756 },
Jooyung Han344d5432019-08-23 11:17:39 +09001757 }
1758
1759 vndk_prebuilt_shared {
1760 name: "libvndk27",
1761 version: "27",
1762 vendor_available: true,
1763 vndk: {
1764 enabled: true,
1765 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001766 target_arch: "x86_64",
1767 arch: {
1768 x86: {
1769 srcs: ["libvndk27_x86.so"],
1770 },
1771 x86_64: {
1772 srcs: ["libvndk27_x86_64.so"],
1773 },
1774 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001775 }
1776 `+vndkLibrariesTxtFiles("27"),
1777 withFiles(map[string][]byte{
1778 "libvndk27_arm.so": nil,
1779 "libvndk27_arm64.so": nil,
1780 "libvndk27_x86.so": nil,
1781 "libvndk27_x86_64.so": nil,
1782 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001783
Jooyung Han31c470b2019-10-18 16:26:59 +09001784 ensureExactContents(t, ctx, "myapex_v27", []string{
1785 "lib/libvndk27_arm.so",
1786 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001787 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001788 })
Jooyung Han344d5432019-08-23 11:17:39 +09001789}
1790
1791func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1792 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1793 apex_vndk {
1794 name: "myapex_v27",
1795 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001796 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001797 vndk_version: "27",
1798 }
1799 apex_vndk {
1800 name: "myapex_v27_other",
1801 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001802 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001803 vndk_version: "27",
1804 }
1805
1806 apex_key {
1807 name: "myapex.key",
1808 public_key: "testkey.avbpubkey",
1809 private_key: "testkey.pem",
1810 }
1811
1812 cc_library {
1813 name: "libvndk",
1814 srcs: ["mylib.cpp"],
1815 vendor_available: true,
1816 vndk: {
1817 enabled: true,
1818 },
1819 system_shared_libs: [],
1820 stl: "none",
1821 }
1822
1823 vndk_prebuilt_shared {
1824 name: "libvndk",
1825 version: "27",
1826 vendor_available: true,
1827 vndk: {
1828 enabled: true,
1829 },
1830 srcs: ["libvndk.so"],
1831 }
1832 `, withFiles(map[string][]byte{
1833 "libvndk.so": nil,
1834 }))
1835}
1836
Jooyung Han90eee022019-10-01 20:02:42 +09001837func TestVndkApexNameRule(t *testing.T) {
1838 ctx, _ := testApex(t, `
1839 apex_vndk {
1840 name: "myapex",
1841 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001842 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001843 }
1844 apex_vndk {
1845 name: "myapex_v28",
1846 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001847 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001848 vndk_version: "28",
1849 }
1850 apex_key {
1851 name: "myapex.key",
1852 public_key: "testkey.avbpubkey",
1853 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001854 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001855
1856 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001857 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001858 actual := proptools.String(bundle.properties.Apex_name)
1859 if !reflect.DeepEqual(actual, expected) {
1860 t.Errorf("Got '%v', expected '%v'", actual, expected)
1861 }
1862 }
1863
1864 assertApexName("com.android.vndk.vVER", "myapex")
1865 assertApexName("com.android.vndk.v28", "myapex_v28")
1866}
1867
Jooyung Han344d5432019-08-23 11:17:39 +09001868func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1869 ctx, _ := testApex(t, `
1870 apex_vndk {
1871 name: "myapex",
1872 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001873 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001874 }
1875
1876 apex_key {
1877 name: "myapex.key",
1878 public_key: "testkey.avbpubkey",
1879 private_key: "testkey.pem",
1880 }
1881
1882 cc_library {
1883 name: "libvndk",
1884 srcs: ["mylib.cpp"],
1885 vendor_available: true,
1886 native_bridge_supported: true,
1887 host_supported: true,
1888 vndk: {
1889 enabled: true,
1890 },
1891 system_shared_libs: [],
1892 stl: "none",
1893 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001894 `+vndkLibrariesTxtFiles("current"),
1895 withTargets(map[android.OsType][]android.Target{
1896 android.Android: []android.Target{
1897 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1898 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1899 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1900 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1901 },
1902 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001903
Jooyung Han31c470b2019-10-18 16:26:59 +09001904 ensureExactContents(t, ctx, "myapex", []string{
1905 "lib/libvndk.so",
1906 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001907 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001908 })
Jooyung Han344d5432019-08-23 11:17:39 +09001909}
1910
1911func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1912 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1913 apex_vndk {
1914 name: "myapex",
1915 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001916 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001917 native_bridge_supported: true,
1918 }
1919
1920 apex_key {
1921 name: "myapex.key",
1922 public_key: "testkey.avbpubkey",
1923 private_key: "testkey.pem",
1924 }
1925
1926 cc_library {
1927 name: "libvndk",
1928 srcs: ["mylib.cpp"],
1929 vendor_available: true,
1930 native_bridge_supported: true,
1931 host_supported: true,
1932 vndk: {
1933 enabled: true,
1934 },
1935 system_shared_libs: [],
1936 stl: "none",
1937 }
1938 `)
1939}
1940
Jooyung Han31c470b2019-10-18 16:26:59 +09001941func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001942 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001943 apex_vndk {
1944 name: "myapex_v27",
1945 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001946 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001947 vndk_version: "27",
1948 }
1949
1950 apex_key {
1951 name: "myapex.key",
1952 public_key: "testkey.avbpubkey",
1953 private_key: "testkey.pem",
1954 }
1955
1956 vndk_prebuilt_shared {
1957 name: "libvndk27",
1958 version: "27",
1959 target_arch: "arm",
1960 vendor_available: true,
1961 vndk: {
1962 enabled: true,
1963 },
1964 arch: {
1965 arm: {
1966 srcs: ["libvndk27.so"],
1967 }
1968 },
1969 }
1970
1971 vndk_prebuilt_shared {
1972 name: "libvndk27",
1973 version: "27",
1974 target_arch: "arm",
1975 binder32bit: true,
1976 vendor_available: true,
1977 vndk: {
1978 enabled: true,
1979 },
1980 arch: {
1981 arm: {
1982 srcs: ["libvndk27binder32.so"],
1983 }
1984 },
1985 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001986 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001987 withFiles(map[string][]byte{
1988 "libvndk27.so": nil,
1989 "libvndk27binder32.so": nil,
1990 }),
1991 withBinder32bit,
1992 withTargets(map[android.OsType][]android.Target{
1993 android.Android: []android.Target{
1994 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1995 },
1996 }),
1997 )
1998
1999 ensureExactContents(t, ctx, "myapex_v27", []string{
2000 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002001 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002002 })
2003}
2004
Jooyung Hane1633032019-08-01 17:41:43 +09002005func TestDependenciesInApexManifest(t *testing.T) {
2006 ctx, _ := testApex(t, `
2007 apex {
2008 name: "myapex_nodep",
2009 key: "myapex.key",
2010 native_shared_libs: ["lib_nodep"],
2011 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002012 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002013 }
2014
2015 apex {
2016 name: "myapex_dep",
2017 key: "myapex.key",
2018 native_shared_libs: ["lib_dep"],
2019 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002020 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002021 }
2022
2023 apex {
2024 name: "myapex_provider",
2025 key: "myapex.key",
2026 native_shared_libs: ["libfoo"],
2027 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002028 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002029 }
2030
2031 apex {
2032 name: "myapex_selfcontained",
2033 key: "myapex.key",
2034 native_shared_libs: ["lib_dep", "libfoo"],
2035 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002036 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002037 }
2038
2039 apex_key {
2040 name: "myapex.key",
2041 public_key: "testkey.avbpubkey",
2042 private_key: "testkey.pem",
2043 }
2044
2045 cc_library {
2046 name: "lib_nodep",
2047 srcs: ["mylib.cpp"],
2048 system_shared_libs: [],
2049 stl: "none",
2050 }
2051
2052 cc_library {
2053 name: "lib_dep",
2054 srcs: ["mylib.cpp"],
2055 shared_libs: ["libfoo"],
2056 system_shared_libs: [],
2057 stl: "none",
2058 }
2059
2060 cc_library {
2061 name: "libfoo",
2062 srcs: ["mytest.cpp"],
2063 stubs: {
2064 versions: ["1"],
2065 },
2066 system_shared_libs: [],
2067 stl: "none",
2068 }
2069 `)
2070
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002071 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002072 var provideNativeLibs, requireNativeLibs []string
2073
Sundong Ahnabb64432019-10-22 13:58:29 +09002074 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002075 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2076 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002077 ensureListEmpty(t, provideNativeLibs)
2078 ensureListEmpty(t, requireNativeLibs)
2079
Sundong Ahnabb64432019-10-22 13:58:29 +09002080 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002081 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2082 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002083 ensureListEmpty(t, provideNativeLibs)
2084 ensureListContains(t, requireNativeLibs, "libfoo.so")
2085
Sundong Ahnabb64432019-10-22 13:58:29 +09002086 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002087 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2088 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002089 ensureListContains(t, provideNativeLibs, "libfoo.so")
2090 ensureListEmpty(t, requireNativeLibs)
2091
Sundong Ahnabb64432019-10-22 13:58:29 +09002092 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002093 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2094 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002095 ensureListContains(t, provideNativeLibs, "libfoo.so")
2096 ensureListEmpty(t, requireNativeLibs)
2097}
2098
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002099func TestApexName(t *testing.T) {
2100 ctx, _ := testApex(t, `
2101 apex {
2102 name: "myapex",
2103 key: "myapex.key",
2104 apex_name: "com.android.myapex",
2105 }
2106
2107 apex_key {
2108 name: "myapex.key",
2109 public_key: "testkey.avbpubkey",
2110 private_key: "testkey.pem",
2111 }
2112 `)
2113
Sundong Ahnabb64432019-10-22 13:58:29 +09002114 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002115 apexManifestRule := module.Rule("apexManifestRule")
2116 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2117 apexRule := module.Rule("apexRule")
2118 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
2119}
2120
Alex Light0851b882019-02-07 13:20:53 -08002121func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002122 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002123 apex {
2124 name: "myapex",
2125 key: "myapex.key",
2126 native_shared_libs: ["mylib_common"],
2127 }
2128
2129 apex_key {
2130 name: "myapex.key",
2131 public_key: "testkey.avbpubkey",
2132 private_key: "testkey.pem",
2133 }
2134
2135 cc_library {
2136 name: "mylib_common",
2137 srcs: ["mylib.cpp"],
2138 system_shared_libs: [],
2139 stl: "none",
2140 }
2141 `)
2142
Sundong Ahnabb64432019-10-22 13:58:29 +09002143 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002144 apexRule := module.Rule("apexRule")
2145 copyCmds := apexRule.Args["copy_commands"]
2146
2147 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2148 t.Log("Apex was a test apex!")
2149 t.Fail()
2150 }
2151 // Ensure that main rule creates an output
2152 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2153
2154 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002155 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002156
2157 // Ensure that both direct and indirect deps are copied into apex
2158 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2159
Colin Cross7113d202019-11-20 16:39:12 -08002160 // Ensure that the platform variant ends with _shared
2161 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002162
2163 if !android.InAnyApex("mylib_common") {
2164 t.Log("Found mylib_common not in any apex!")
2165 t.Fail()
2166 }
2167}
2168
2169func TestTestApex(t *testing.T) {
2170 if android.InAnyApex("mylib_common_test") {
2171 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!")
2172 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002173 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002174 apex_test {
2175 name: "myapex",
2176 key: "myapex.key",
2177 native_shared_libs: ["mylib_common_test"],
2178 }
2179
2180 apex_key {
2181 name: "myapex.key",
2182 public_key: "testkey.avbpubkey",
2183 private_key: "testkey.pem",
2184 }
2185
2186 cc_library {
2187 name: "mylib_common_test",
2188 srcs: ["mylib.cpp"],
2189 system_shared_libs: [],
2190 stl: "none",
2191 }
2192 `)
2193
Sundong Ahnabb64432019-10-22 13:58:29 +09002194 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002195 apexRule := module.Rule("apexRule")
2196 copyCmds := apexRule.Args["copy_commands"]
2197
2198 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2199 t.Log("Apex was not a test apex!")
2200 t.Fail()
2201 }
2202 // Ensure that main rule creates an output
2203 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2204
2205 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002206 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002207
2208 // Ensure that both direct and indirect deps are copied into apex
2209 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2210
Colin Cross7113d202019-11-20 16:39:12 -08002211 // Ensure that the platform variant ends with _shared
2212 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002213
2214 if android.InAnyApex("mylib_common_test") {
2215 t.Log("Found mylib_common_test in some apex!")
2216 t.Fail()
2217 }
2218}
2219
Alex Light9670d332019-01-29 18:07:33 -08002220func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002221 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002222 apex {
2223 name: "myapex",
2224 key: "myapex.key",
2225 multilib: {
2226 first: {
2227 native_shared_libs: ["mylib_common"],
2228 }
2229 },
2230 target: {
2231 android: {
2232 multilib: {
2233 first: {
2234 native_shared_libs: ["mylib"],
2235 }
2236 }
2237 },
2238 host: {
2239 multilib: {
2240 first: {
2241 native_shared_libs: ["mylib2"],
2242 }
2243 }
2244 }
2245 }
2246 }
2247
2248 apex_key {
2249 name: "myapex.key",
2250 public_key: "testkey.avbpubkey",
2251 private_key: "testkey.pem",
2252 }
2253
2254 cc_library {
2255 name: "mylib",
2256 srcs: ["mylib.cpp"],
2257 system_shared_libs: [],
2258 stl: "none",
2259 }
2260
2261 cc_library {
2262 name: "mylib_common",
2263 srcs: ["mylib.cpp"],
2264 system_shared_libs: [],
2265 stl: "none",
2266 compile_multilib: "first",
2267 }
2268
2269 cc_library {
2270 name: "mylib2",
2271 srcs: ["mylib.cpp"],
2272 system_shared_libs: [],
2273 stl: "none",
2274 compile_multilib: "first",
2275 }
2276 `)
2277
Sundong Ahnabb64432019-10-22 13:58:29 +09002278 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002279 copyCmds := apexRule.Args["copy_commands"]
2280
2281 // Ensure that main rule creates an output
2282 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2283
2284 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002285 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2286 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2287 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002288
2289 // Ensure that both direct and indirect deps are copied into apex
2290 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2291 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2292 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2293
Colin Cross7113d202019-11-20 16:39:12 -08002294 // Ensure that the platform variant ends with _shared
2295 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2296 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2297 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002298}
Jiyong Park04480cf2019-02-06 00:16:29 +09002299
2300func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002301 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002302 apex {
2303 name: "myapex",
2304 key: "myapex.key",
2305 binaries: ["myscript"],
2306 }
2307
2308 apex_key {
2309 name: "myapex.key",
2310 public_key: "testkey.avbpubkey",
2311 private_key: "testkey.pem",
2312 }
2313
2314 sh_binary {
2315 name: "myscript",
2316 src: "mylib.cpp",
2317 filename: "myscript.sh",
2318 sub_dir: "script",
2319 }
2320 `)
2321
Sundong Ahnabb64432019-10-22 13:58:29 +09002322 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002323 copyCmds := apexRule.Args["copy_commands"]
2324
2325 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2326}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002327
Jooyung Han91df2082019-11-20 01:49:42 +09002328func TestApexInVariousPartition(t *testing.T) {
2329 testcases := []struct {
2330 propName, parition, flattenedPartition string
2331 }{
2332 {"", "system", "system_ext"},
2333 {"product_specific: true", "product", "product"},
2334 {"soc_specific: true", "vendor", "vendor"},
2335 {"proprietary: true", "vendor", "vendor"},
2336 {"vendor: true", "vendor", "vendor"},
2337 {"system_ext_specific: true", "system_ext", "system_ext"},
2338 }
2339 for _, tc := range testcases {
2340 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2341 ctx, _ := testApex(t, `
2342 apex {
2343 name: "myapex",
2344 key: "myapex.key",
2345 `+tc.propName+`
2346 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002347
Jooyung Han91df2082019-11-20 01:49:42 +09002348 apex_key {
2349 name: "myapex.key",
2350 public_key: "testkey.avbpubkey",
2351 private_key: "testkey.pem",
2352 }
2353 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002354
Jooyung Han91df2082019-11-20 01:49:42 +09002355 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2356 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2357 actual := apex.installDir.String()
2358 if actual != expected {
2359 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2360 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002361
Jooyung Han91df2082019-11-20 01:49:42 +09002362 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2363 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2364 actual = flattened.installDir.String()
2365 if actual != expected {
2366 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2367 }
2368 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002369 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002370}
Jiyong Park67882562019-03-21 01:11:21 +09002371
Jooyung Han54aca7b2019-11-20 02:26:02 +09002372func TestFileContexts(t *testing.T) {
2373 ctx, _ := testApex(t, `
2374 apex {
2375 name: "myapex",
2376 key: "myapex.key",
2377 }
2378
2379 apex_key {
2380 name: "myapex.key",
2381 public_key: "testkey.avbpubkey",
2382 private_key: "testkey.pem",
2383 }
2384 `)
2385 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2386 apexRule := module.Rule("apexRule")
2387 actual := apexRule.Args["file_contexts"]
2388 expected := "system/sepolicy/apex/myapex-file_contexts"
2389 if actual != expected {
2390 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2391 }
2392
2393 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2394 apex {
2395 name: "myapex",
2396 key: "myapex.key",
2397 file_contexts: "my_own_file_contexts",
2398 }
2399
2400 apex_key {
2401 name: "myapex.key",
2402 public_key: "testkey.avbpubkey",
2403 private_key: "testkey.pem",
2404 }
2405 `, withFiles(map[string][]byte{
2406 "my_own_file_contexts": nil,
2407 }))
2408
2409 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2410 apex {
2411 name: "myapex",
2412 key: "myapex.key",
2413 product_specific: true,
2414 file_contexts: "product_specific_file_contexts",
2415 }
2416
2417 apex_key {
2418 name: "myapex.key",
2419 public_key: "testkey.avbpubkey",
2420 private_key: "testkey.pem",
2421 }
2422 `)
2423
2424 ctx, _ = testApex(t, `
2425 apex {
2426 name: "myapex",
2427 key: "myapex.key",
2428 product_specific: true,
2429 file_contexts: "product_specific_file_contexts",
2430 }
2431
2432 apex_key {
2433 name: "myapex.key",
2434 public_key: "testkey.avbpubkey",
2435 private_key: "testkey.pem",
2436 }
2437 `, withFiles(map[string][]byte{
2438 "product_specific_file_contexts": nil,
2439 }))
2440 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2441 apexRule = module.Rule("apexRule")
2442 actual = apexRule.Args["file_contexts"]
2443 expected = "product_specific_file_contexts"
2444 if actual != expected {
2445 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2446 }
2447
2448 ctx, _ = testApex(t, `
2449 apex {
2450 name: "myapex",
2451 key: "myapex.key",
2452 product_specific: true,
2453 file_contexts: ":my-file-contexts",
2454 }
2455
2456 apex_key {
2457 name: "myapex.key",
2458 public_key: "testkey.avbpubkey",
2459 private_key: "testkey.pem",
2460 }
2461
2462 filegroup {
2463 name: "my-file-contexts",
2464 srcs: ["product_specific_file_contexts"],
2465 }
2466 `, withFiles(map[string][]byte{
2467 "product_specific_file_contexts": nil,
2468 }))
2469 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2470 apexRule = module.Rule("apexRule")
2471 actual = apexRule.Args["file_contexts"]
2472 expected = "product_specific_file_contexts"
2473 if actual != expected {
2474 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2475 }
2476}
2477
Jiyong Park67882562019-03-21 01:11:21 +09002478func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002479 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002480 apex_key {
2481 name: "myapex.key",
2482 public_key: ":my.avbpubkey",
2483 private_key: ":my.pem",
2484 product_specific: true,
2485 }
2486
2487 filegroup {
2488 name: "my.avbpubkey",
2489 srcs: ["testkey2.avbpubkey"],
2490 }
2491
2492 filegroup {
2493 name: "my.pem",
2494 srcs: ["testkey2.pem"],
2495 }
2496 `)
2497
2498 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2499 expected_pubkey := "testkey2.avbpubkey"
2500 actual_pubkey := apex_key.public_key_file.String()
2501 if actual_pubkey != expected_pubkey {
2502 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2503 }
2504 expected_privkey := "testkey2.pem"
2505 actual_privkey := apex_key.private_key_file.String()
2506 if actual_privkey != expected_privkey {
2507 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2508 }
2509}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002510
2511func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002512 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002513 prebuilt_apex {
2514 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002515 arch: {
2516 arm64: {
2517 src: "myapex-arm64.apex",
2518 },
2519 arm: {
2520 src: "myapex-arm.apex",
2521 },
2522 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002523 }
2524 `)
2525
2526 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2527
Jiyong Parkc95714e2019-03-29 14:23:10 +09002528 expectedInput := "myapex-arm64.apex"
2529 if prebuilt.inputApex.String() != expectedInput {
2530 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2531 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002532}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002533
2534func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002535 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002536 prebuilt_apex {
2537 name: "myapex",
2538 src: "myapex-arm.apex",
2539 filename: "notmyapex.apex",
2540 }
2541 `)
2542
2543 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2544
2545 expected := "notmyapex.apex"
2546 if p.installFilename != expected {
2547 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2548 }
2549}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002550
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002551func TestPrebuiltOverrides(t *testing.T) {
2552 ctx, config := testApex(t, `
2553 prebuilt_apex {
2554 name: "myapex.prebuilt",
2555 src: "myapex-arm.apex",
2556 overrides: [
2557 "myapex",
2558 ],
2559 }
2560 `)
2561
2562 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2563
2564 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002565 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002566 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002567 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002568 }
2569}
2570
Roland Levillain630846d2019-06-26 12:48:34 +01002571func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002572 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002573 apex_test {
2574 name: "myapex",
2575 key: "myapex.key",
2576 tests: [
2577 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002578 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002579 ],
2580 }
2581
2582 apex_key {
2583 name: "myapex.key",
2584 public_key: "testkey.avbpubkey",
2585 private_key: "testkey.pem",
2586 }
2587
2588 cc_test {
2589 name: "mytest",
2590 gtest: false,
2591 srcs: ["mytest.cpp"],
2592 relative_install_path: "test",
2593 system_shared_libs: [],
2594 static_executable: true,
2595 stl: "none",
2596 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002597
2598 cc_test {
2599 name: "mytests",
2600 gtest: false,
2601 srcs: [
2602 "mytest1.cpp",
2603 "mytest2.cpp",
2604 "mytest3.cpp",
2605 ],
2606 test_per_src: true,
2607 relative_install_path: "test",
2608 system_shared_libs: [],
2609 static_executable: true,
2610 stl: "none",
2611 }
Roland Levillain630846d2019-06-26 12:48:34 +01002612 `)
2613
Sundong Ahnabb64432019-10-22 13:58:29 +09002614 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002615 copyCmds := apexRule.Args["copy_commands"]
2616
2617 // Ensure that test dep is copied into apex.
2618 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002619
2620 // Ensure that test deps built with `test_per_src` are copied into apex.
2621 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2622 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2623 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002624
2625 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002626 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002627 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2628 name := apexBundle.BaseModuleName()
2629 prefix := "TARGET_"
2630 var builder strings.Builder
2631 data.Custom(&builder, name, prefix, "", data)
2632 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002633 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2634 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2635 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2636 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002637 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002638 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002639 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002640}
2641
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002642func TestInstallExtraFlattenedApexes(t *testing.T) {
2643 ctx, config := testApex(t, `
2644 apex {
2645 name: "myapex",
2646 key: "myapex.key",
2647 }
2648 apex_key {
2649 name: "myapex.key",
2650 public_key: "testkey.avbpubkey",
2651 private_key: "testkey.pem",
2652 }
2653 `, func(fs map[string][]byte, config android.Config) {
2654 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2655 })
2656 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2657 ensureListContains(t, ab.externalDeps, "myapex.flattened")
2658 mk := android.AndroidMkDataForTest(t, config, "", ab)
2659 var builder strings.Builder
2660 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2661 androidMk := builder.String()
2662 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2663}
2664
Jooyung Han5c998b92019-06-27 11:30:33 +09002665func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002666 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002667 apex {
2668 name: "myapex",
2669 key: "myapex.key",
2670 native_shared_libs: ["mylib"],
2671 uses: ["commonapex"],
2672 }
2673
2674 apex {
2675 name: "commonapex",
2676 key: "myapex.key",
2677 native_shared_libs: ["libcommon"],
2678 provide_cpp_shared_libs: true,
2679 }
2680
2681 apex_key {
2682 name: "myapex.key",
2683 public_key: "testkey.avbpubkey",
2684 private_key: "testkey.pem",
2685 }
2686
2687 cc_library {
2688 name: "mylib",
2689 srcs: ["mylib.cpp"],
2690 shared_libs: ["libcommon"],
2691 system_shared_libs: [],
2692 stl: "none",
2693 }
2694
2695 cc_library {
2696 name: "libcommon",
2697 srcs: ["mylib_common.cpp"],
2698 system_shared_libs: [],
2699 stl: "none",
2700 }
2701 `)
2702
Sundong Ahnabb64432019-10-22 13:58:29 +09002703 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002704 apexRule1 := module1.Rule("apexRule")
2705 copyCmds1 := apexRule1.Args["copy_commands"]
2706
Sundong Ahnabb64432019-10-22 13:58:29 +09002707 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002708 apexRule2 := module2.Rule("apexRule")
2709 copyCmds2 := apexRule2.Args["copy_commands"]
2710
Colin Cross7113d202019-11-20 16:39:12 -08002711 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2712 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002713 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2714 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2715 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2716}
2717
2718func TestApexUsesFailsIfNotProvided(t *testing.T) {
2719 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2720 apex {
2721 name: "myapex",
2722 key: "myapex.key",
2723 uses: ["commonapex"],
2724 }
2725
2726 apex {
2727 name: "commonapex",
2728 key: "myapex.key",
2729 }
2730
2731 apex_key {
2732 name: "myapex.key",
2733 public_key: "testkey.avbpubkey",
2734 private_key: "testkey.pem",
2735 }
2736 `)
2737 testApexError(t, `uses: "commonapex" is not a provider`, `
2738 apex {
2739 name: "myapex",
2740 key: "myapex.key",
2741 uses: ["commonapex"],
2742 }
2743
2744 cc_library {
2745 name: "commonapex",
2746 system_shared_libs: [],
2747 stl: "none",
2748 }
2749
2750 apex_key {
2751 name: "myapex.key",
2752 public_key: "testkey.avbpubkey",
2753 private_key: "testkey.pem",
2754 }
2755 `)
2756}
2757
2758func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2759 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2760 apex {
2761 name: "myapex",
2762 key: "myapex.key",
2763 use_vendor: true,
2764 uses: ["commonapex"],
2765 }
2766
2767 apex {
2768 name: "commonapex",
2769 key: "myapex.key",
2770 provide_cpp_shared_libs: true,
2771 }
2772
2773 apex_key {
2774 name: "myapex.key",
2775 public_key: "testkey.avbpubkey",
2776 private_key: "testkey.pem",
2777 }
Jooyung Handc782442019-11-01 03:14:38 +09002778 `, func(fs map[string][]byte, config android.Config) {
2779 setUseVendorWhitelistForTest(config, []string{"myapex"})
2780 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002781}
2782
Jooyung Hand48f3c32019-08-23 11:18:57 +09002783func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2784 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2785 apex {
2786 name: "myapex",
2787 key: "myapex.key",
2788 native_shared_libs: ["libfoo"],
2789 }
2790
2791 apex_key {
2792 name: "myapex.key",
2793 public_key: "testkey.avbpubkey",
2794 private_key: "testkey.pem",
2795 }
2796
2797 cc_library {
2798 name: "libfoo",
2799 stl: "none",
2800 system_shared_libs: [],
2801 enabled: false,
2802 }
2803 `)
2804 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2805 apex {
2806 name: "myapex",
2807 key: "myapex.key",
2808 java_libs: ["myjar"],
2809 }
2810
2811 apex_key {
2812 name: "myapex.key",
2813 public_key: "testkey.avbpubkey",
2814 private_key: "testkey.pem",
2815 }
2816
2817 java_library {
2818 name: "myjar",
2819 srcs: ["foo/bar/MyClass.java"],
2820 sdk_version: "none",
2821 system_modules: "none",
2822 compile_dex: true,
2823 enabled: false,
2824 }
2825 `)
2826}
2827
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002828func TestApexWithApps(t *testing.T) {
2829 ctx, _ := testApex(t, `
2830 apex {
2831 name: "myapex",
2832 key: "myapex.key",
2833 apps: [
2834 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002835 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002836 ],
2837 }
2838
2839 apex_key {
2840 name: "myapex.key",
2841 public_key: "testkey.avbpubkey",
2842 private_key: "testkey.pem",
2843 }
2844
2845 android_app {
2846 name: "AppFoo",
2847 srcs: ["foo/bar/MyClass.java"],
2848 sdk_version: "none",
2849 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002850 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002851 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002852
2853 android_app {
2854 name: "AppFooPriv",
2855 srcs: ["foo/bar/MyClass.java"],
2856 sdk_version: "none",
2857 system_modules: "none",
2858 privileged: true,
2859 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002860
2861 cc_library_shared {
2862 name: "libjni",
2863 srcs: ["mylib.cpp"],
2864 stl: "none",
2865 system_shared_libs: [],
2866 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002867 `)
2868
Sundong Ahnabb64432019-10-22 13:58:29 +09002869 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002870 apexRule := module.Rule("apexRule")
2871 copyCmds := apexRule.Args["copy_commands"]
2872
2873 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002874 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002875
2876 // JNI libraries are embedded inside APK
2877 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
Colin Cross7113d202019-11-20 16:39:12 -08002878 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002879 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2880 // ... uncompressed
2881 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2882 t.Errorf("jni lib is not uncompressed for AppFoo")
2883 }
2884 // ... and not directly inside the APEX
2885 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002886}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002887
Dario Frenicde2a032019-10-27 00:29:22 +01002888func TestApexWithAppImports(t *testing.T) {
2889 ctx, _ := testApex(t, `
2890 apex {
2891 name: "myapex",
2892 key: "myapex.key",
2893 apps: [
2894 "AppFooPrebuilt",
2895 "AppFooPrivPrebuilt",
2896 ],
2897 }
2898
2899 apex_key {
2900 name: "myapex.key",
2901 public_key: "testkey.avbpubkey",
2902 private_key: "testkey.pem",
2903 }
2904
2905 android_app_import {
2906 name: "AppFooPrebuilt",
2907 apk: "PrebuiltAppFoo.apk",
2908 presigned: true,
2909 dex_preopt: {
2910 enabled: false,
2911 },
2912 }
2913
2914 android_app_import {
2915 name: "AppFooPrivPrebuilt",
2916 apk: "PrebuiltAppFooPriv.apk",
2917 privileged: true,
2918 presigned: true,
2919 dex_preopt: {
2920 enabled: false,
2921 },
2922 }
2923 `)
2924
Sundong Ahnabb64432019-10-22 13:58:29 +09002925 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002926 apexRule := module.Rule("apexRule")
2927 copyCmds := apexRule.Args["copy_commands"]
2928
2929 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2930 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002931}
2932
Jooyung Han18020ea2019-11-13 10:50:48 +09002933func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2934 // libfoo's apex_available comes from cc_defaults
2935 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2936 apex {
2937 name: "myapex",
2938 key: "myapex.key",
2939 native_shared_libs: ["libfoo"],
2940 }
2941
2942 apex_key {
2943 name: "myapex.key",
2944 public_key: "testkey.avbpubkey",
2945 private_key: "testkey.pem",
2946 }
2947
2948 apex {
2949 name: "otherapex",
2950 key: "myapex.key",
2951 native_shared_libs: ["libfoo"],
2952 }
2953
2954 cc_defaults {
2955 name: "libfoo-defaults",
2956 apex_available: ["otherapex"],
2957 }
2958
2959 cc_library {
2960 name: "libfoo",
2961 defaults: ["libfoo-defaults"],
2962 stl: "none",
2963 system_shared_libs: [],
2964 }`)
2965}
2966
Jiyong Park127b40b2019-09-30 16:04:35 +09002967func TestApexAvailable(t *testing.T) {
2968 // libfoo is not available to myapex, but only to otherapex
2969 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2970 apex {
2971 name: "myapex",
2972 key: "myapex.key",
2973 native_shared_libs: ["libfoo"],
2974 }
2975
2976 apex_key {
2977 name: "myapex.key",
2978 public_key: "testkey.avbpubkey",
2979 private_key: "testkey.pem",
2980 }
2981
2982 apex {
2983 name: "otherapex",
2984 key: "otherapex.key",
2985 native_shared_libs: ["libfoo"],
2986 }
2987
2988 apex_key {
2989 name: "otherapex.key",
2990 public_key: "testkey.avbpubkey",
2991 private_key: "testkey.pem",
2992 }
2993
2994 cc_library {
2995 name: "libfoo",
2996 stl: "none",
2997 system_shared_libs: [],
2998 apex_available: ["otherapex"],
2999 }`)
3000
3001 // libbar is an indirect dep
3002 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3003 apex {
3004 name: "myapex",
3005 key: "myapex.key",
3006 native_shared_libs: ["libfoo"],
3007 }
3008
3009 apex_key {
3010 name: "myapex.key",
3011 public_key: "testkey.avbpubkey",
3012 private_key: "testkey.pem",
3013 }
3014
3015 apex {
3016 name: "otherapex",
3017 key: "otherapex.key",
3018 native_shared_libs: ["libfoo"],
3019 }
3020
3021 apex_key {
3022 name: "otherapex.key",
3023 public_key: "testkey.avbpubkey",
3024 private_key: "testkey.pem",
3025 }
3026
3027 cc_library {
3028 name: "libfoo",
3029 stl: "none",
3030 shared_libs: ["libbar"],
3031 system_shared_libs: [],
3032 apex_available: ["myapex", "otherapex"],
3033 }
3034
3035 cc_library {
3036 name: "libbar",
3037 stl: "none",
3038 system_shared_libs: [],
3039 apex_available: ["otherapex"],
3040 }`)
3041
3042 testApexError(t, "\"otherapex\" is not a valid module name", `
3043 apex {
3044 name: "myapex",
3045 key: "myapex.key",
3046 native_shared_libs: ["libfoo"],
3047 }
3048
3049 apex_key {
3050 name: "myapex.key",
3051 public_key: "testkey.avbpubkey",
3052 private_key: "testkey.pem",
3053 }
3054
3055 cc_library {
3056 name: "libfoo",
3057 stl: "none",
3058 system_shared_libs: [],
3059 apex_available: ["otherapex"],
3060 }`)
3061
3062 ctx, _ := testApex(t, `
3063 apex {
3064 name: "myapex",
3065 key: "myapex.key",
3066 native_shared_libs: ["libfoo", "libbar"],
3067 }
3068
3069 apex_key {
3070 name: "myapex.key",
3071 public_key: "testkey.avbpubkey",
3072 private_key: "testkey.pem",
3073 }
3074
3075 cc_library {
3076 name: "libfoo",
3077 stl: "none",
3078 system_shared_libs: [],
3079 apex_available: ["myapex"],
3080 }
3081
3082 cc_library {
3083 name: "libbar",
3084 stl: "none",
3085 system_shared_libs: [],
3086 apex_available: ["//apex_available:anyapex"],
3087 }`)
3088
3089 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003090 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3091 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3092 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3093 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003094
3095 ctx, _ = testApex(t, `
3096 apex {
3097 name: "myapex",
3098 key: "myapex.key",
3099 }
3100
3101 apex_key {
3102 name: "myapex.key",
3103 public_key: "testkey.avbpubkey",
3104 private_key: "testkey.pem",
3105 }
3106
3107 cc_library {
3108 name: "libfoo",
3109 stl: "none",
3110 system_shared_libs: [],
3111 apex_available: ["//apex_available:platform"],
3112 }`)
3113
3114 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003115 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3116 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003117
3118 ctx, _ = testApex(t, `
3119 apex {
3120 name: "myapex",
3121 key: "myapex.key",
3122 native_shared_libs: ["libfoo"],
3123 }
3124
3125 apex_key {
3126 name: "myapex.key",
3127 public_key: "testkey.avbpubkey",
3128 private_key: "testkey.pem",
3129 }
3130
3131 cc_library {
3132 name: "libfoo",
3133 stl: "none",
3134 system_shared_libs: [],
3135 apex_available: ["myapex"],
3136 static: {
3137 apex_available: ["//apex_available:platform"],
3138 },
3139 }`)
3140
3141 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08003142 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3143 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003144 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08003145 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3146 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003147}
3148
Jiyong Park5d790c32019-11-15 18:40:32 +09003149func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003150 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003151 apex {
3152 name: "myapex",
3153 key: "myapex.key",
3154 apps: ["app"],
3155 }
3156
3157 override_apex {
3158 name: "override_myapex",
3159 base: "myapex",
3160 apps: ["override_app"],
3161 }
3162
3163 apex_key {
3164 name: "myapex.key",
3165 public_key: "testkey.avbpubkey",
3166 private_key: "testkey.pem",
3167 }
3168
3169 android_app {
3170 name: "app",
3171 srcs: ["foo/bar/MyClass.java"],
3172 package_name: "foo",
3173 sdk_version: "none",
3174 system_modules: "none",
3175 }
3176
3177 override_android_app {
3178 name: "override_app",
3179 base: "app",
3180 package_name: "bar",
3181 }
3182 `)
3183
Jiyong Park317645e2019-12-05 13:20:58 +09003184 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3185 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3186 if originalVariant.GetOverriddenBy() != "" {
3187 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3188 }
3189 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3190 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3191 }
3192
Jiyong Park5d790c32019-11-15 18:40:32 +09003193 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3194 apexRule := module.Rule("apexRule")
3195 copyCmds := apexRule.Args["copy_commands"]
3196
3197 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3198 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003199
3200 apexBundle := module.Module().(*apexBundle)
3201 name := apexBundle.Name()
3202 if name != "override_myapex" {
3203 t.Errorf("name should be \"override_myapex\", but was %q", name)
3204 }
3205
3206 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3207 var builder strings.Builder
3208 data.Custom(&builder, name, "TARGET_", "", data)
3209 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003210 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003211 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3212 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
3213 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003214 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003215 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3216 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003217}
3218
Jooyung Han214bf372019-11-12 13:03:50 +09003219func TestLegacyAndroid10Support(t *testing.T) {
3220 ctx, _ := testApex(t, `
3221 apex {
3222 name: "myapex",
3223 key: "myapex.key",
3224 legacy_android10_support: true,
3225 }
3226
3227 apex_key {
3228 name: "myapex.key",
3229 public_key: "testkey.avbpubkey",
3230 private_key: "testkey.pem",
3231 }
3232 `)
3233
3234 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3235 args := module.Rule("apexRule").Args
3236 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
3237}
3238
Jiyong Park479321d2019-12-16 11:47:12 +09003239func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3240 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3241 apex {
3242 name: "myapex",
3243 key: "myapex.key",
3244 java_libs: ["myjar"],
3245 }
3246
3247 apex_key {
3248 name: "myapex.key",
3249 public_key: "testkey.avbpubkey",
3250 private_key: "testkey.pem",
3251 }
3252
3253 java_library {
3254 name: "myjar",
3255 srcs: ["foo/bar/MyClass.java"],
3256 sdk_version: "none",
3257 system_modules: "none",
3258 }
3259 `)
3260}
3261
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003262func TestMain(m *testing.M) {
3263 run := func() int {
3264 setUp()
3265 defer tearDown()
3266
3267 return m.Run()
3268 }
3269
3270 os.Exit(run())
3271}