blob: 969be63780c8a45a812e187e337a587a4f1f3aaf [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)
308 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
309 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
310 })
Colin Cross98be1bb2019-12-13 20:41:13 -0800311 ctx.PreDepsMutators(RegisterPreDepsMutators)
312 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
313 ctx.PostDepsMutators(RegisterPostDepsMutators)
314 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
315 ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel()
316 ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel()
317 })
318
319 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900320
Jooyung Han5c998b92019-06-27 11:30:33 +0900321 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900322}
323
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700324func setUp() {
325 var err error
326 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900327 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700328 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900329 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330}
331
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700332func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900333 os.RemoveAll(buildDir)
334}
335
336// ensure that 'result' contains 'expected'
337func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900338 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900339 if !strings.Contains(result, expected) {
340 t.Errorf("%q is not found in %q", expected, result)
341 }
342}
343
344// ensures that 'result' does not contain 'notExpected'
345func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900346 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900347 if strings.Contains(result, notExpected) {
348 t.Errorf("%q is found in %q", notExpected, result)
349 }
350}
351
352func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900353 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900354 if !android.InList(expected, result) {
355 t.Errorf("%q is not found in %v", expected, result)
356 }
357}
358
359func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900360 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900361 if android.InList(notExpected, result) {
362 t.Errorf("%q is found in %v", notExpected, result)
363 }
364}
365
Jooyung Hane1633032019-08-01 17:41:43 +0900366func ensureListEmpty(t *testing.T, result []string) {
367 t.Helper()
368 if len(result) > 0 {
369 t.Errorf("%q is expected to be empty", result)
370 }
371}
372
Jiyong Park25fc6a92018-11-18 18:02:45 +0900373// Minimal test
374func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700375 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900376 apex_defaults {
377 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900378 manifest: ":myapex.manifest",
379 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900380 key: "myapex.key",
381 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800382 multilib: {
383 both: {
384 binaries: ["foo",],
385 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900386 },
Jiyong Park9e6c2422019-08-09 20:39:45 +0900387 java_libs: ["myjar", "myprebuiltjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900388 }
389
Jiyong Park30ca9372019-02-07 16:27:23 +0900390 apex {
391 name: "myapex",
392 defaults: ["myapex-defaults"],
393 }
394
Jiyong Park25fc6a92018-11-18 18:02:45 +0900395 apex_key {
396 name: "myapex.key",
397 public_key: "testkey.avbpubkey",
398 private_key: "testkey.pem",
399 }
400
Jiyong Park809bb722019-02-13 21:33:49 +0900401 filegroup {
402 name: "myapex.manifest",
403 srcs: ["apex_manifest.json"],
404 }
405
406 filegroup {
407 name: "myapex.androidmanifest",
408 srcs: ["AndroidManifest.xml"],
409 }
410
Jiyong Park25fc6a92018-11-18 18:02:45 +0900411 cc_library {
412 name: "mylib",
413 srcs: ["mylib.cpp"],
414 shared_libs: ["mylib2"],
415 system_shared_libs: [],
416 stl: "none",
417 }
418
Alex Light3d673592019-01-18 14:37:31 -0800419 cc_binary {
420 name: "foo",
421 srcs: ["mylib.cpp"],
422 compile_multilib: "both",
423 multilib: {
424 lib32: {
425 suffix: "32",
426 },
427 lib64: {
428 suffix: "64",
429 },
430 },
431 symlinks: ["foo_link_"],
432 symlink_preferred_arch: true,
433 system_shared_libs: [],
434 static_executable: true,
435 stl: "none",
436 }
437
Jiyong Park25fc6a92018-11-18 18:02:45 +0900438 cc_library {
439 name: "mylib2",
440 srcs: ["mylib.cpp"],
441 system_shared_libs: [],
442 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900443 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900444 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900445
446 java_library {
447 name: "myjar",
448 srcs: ["foo/bar/MyClass.java"],
449 sdk_version: "none",
450 system_modules: "none",
451 compile_dex: true,
452 static_libs: ["myotherjar"],
453 }
454
455 java_library {
456 name: "myotherjar",
457 srcs: ["foo/bar/MyClass.java"],
458 sdk_version: "none",
459 system_modules: "none",
460 compile_dex: true,
461 }
Jiyong Park9e6c2422019-08-09 20:39:45 +0900462
463 java_import {
464 name: "myprebuiltjar",
465 jars: ["prebuilt.jar"],
466 installable: true,
467 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900468 `)
469
Sundong Ahnabb64432019-10-22 13:58:29 +0900470 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900471
472 optFlags := apexRule.Args["opt_flags"]
473 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700474 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900475 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900476
Jiyong Park25fc6a92018-11-18 18:02:45 +0900477 copyCmds := apexRule.Args["copy_commands"]
478
479 // Ensure that main rule creates an output
480 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
481
482 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800483 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900484 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900485 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900486
487 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800488 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900489 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900490
491 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800492 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
493 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900494 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900495 ensureContains(t, copyCmds, "image.apex/javalib/myprebuiltjar.jar")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900496 // .. but not for java libs
497 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800498
Colin Cross7113d202019-11-20 16:39:12 -0800499 // Ensure that the platform variant ends with _shared or _common
500 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
501 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900502 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
503 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900504 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common")
Alex Light3d673592019-01-18 14:37:31 -0800505
506 // Ensure that all symlinks are present.
507 found_foo_link_64 := false
508 found_foo := false
509 for _, cmd := range strings.Split(copyCmds, " && ") {
510 if strings.HasPrefix(cmd, "ln -s foo64") {
511 if strings.HasSuffix(cmd, "bin/foo") {
512 found_foo = true
513 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
514 found_foo_link_64 = true
515 }
516 }
517 }
518 good := found_foo && found_foo_link_64
519 if !good {
520 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
521 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900522
Sundong Ahnabb64432019-10-22 13:58:29 +0900523 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700524 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700525 if len(noticeInputs) != 2 {
526 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900527 }
528 ensureListContains(t, noticeInputs, "NOTICE")
529 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800530}
531
Jooyung Hanf21c7972019-12-16 22:32:06 +0900532func TestDefaults(t *testing.T) {
533 ctx, _ := testApex(t, `
534 apex_defaults {
535 name: "myapex-defaults",
536 key: "myapex.key",
537 prebuilts: ["myetc"],
538 native_shared_libs: ["mylib"],
539 java_libs: ["myjar"],
540 apps: ["AppFoo"],
541 }
542
543 prebuilt_etc {
544 name: "myetc",
545 src: "myprebuilt",
546 }
547
548 apex {
549 name: "myapex",
550 defaults: ["myapex-defaults"],
551 }
552
553 apex_key {
554 name: "myapex.key",
555 public_key: "testkey.avbpubkey",
556 private_key: "testkey.pem",
557 }
558
559 cc_library {
560 name: "mylib",
561 system_shared_libs: [],
562 stl: "none",
563 }
564
565 java_library {
566 name: "myjar",
567 srcs: ["foo/bar/MyClass.java"],
568 sdk_version: "none",
569 system_modules: "none",
570 compile_dex: true,
571 }
572
573 android_app {
574 name: "AppFoo",
575 srcs: ["foo/bar/MyClass.java"],
576 sdk_version: "none",
577 system_modules: "none",
578 }
579 `)
580 ensureExactContents(t, ctx, "myapex", []string{
581 "etc/myetc",
582 "javalib/myjar.jar",
583 "lib64/mylib.so",
584 "app/AppFoo/AppFoo.apk",
585 })
586}
587
Jooyung Han01a3ee22019-11-02 02:52:25 +0900588func TestApexManifest(t *testing.T) {
589 ctx, _ := testApex(t, `
590 apex {
591 name: "myapex",
592 key: "myapex.key",
593 }
594
595 apex_key {
596 name: "myapex.key",
597 public_key: "testkey.avbpubkey",
598 private_key: "testkey.pem",
599 }
600 `)
601
602 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900603 args := module.Rule("apexRule").Args
604 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
605 t.Error("manifest should be apex_manifest.pb, but " + manifest)
606 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900607}
608
Alex Light5098a612018-11-29 17:12:15 -0800609func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700610 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800611 apex {
612 name: "myapex",
613 key: "myapex.key",
614 payload_type: "zip",
615 native_shared_libs: ["mylib"],
616 }
617
618 apex_key {
619 name: "myapex.key",
620 public_key: "testkey.avbpubkey",
621 private_key: "testkey.pem",
622 }
623
624 cc_library {
625 name: "mylib",
626 srcs: ["mylib.cpp"],
627 shared_libs: ["mylib2"],
628 system_shared_libs: [],
629 stl: "none",
630 }
631
632 cc_library {
633 name: "mylib2",
634 srcs: ["mylib.cpp"],
635 system_shared_libs: [],
636 stl: "none",
637 }
638 `)
639
Sundong Ahnabb64432019-10-22 13:58:29 +0900640 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800641 copyCmds := zipApexRule.Args["copy_commands"]
642
643 // Ensure that main rule creates an output
644 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
645
646 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800647 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800648
649 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800650 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800651
652 // Ensure that both direct and indirect deps are copied into apex
653 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
654 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900655}
656
657func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700658 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900659 apex {
660 name: "myapex",
661 key: "myapex.key",
662 native_shared_libs: ["mylib", "mylib3"],
663 }
664
665 apex_key {
666 name: "myapex.key",
667 public_key: "testkey.avbpubkey",
668 private_key: "testkey.pem",
669 }
670
671 cc_library {
672 name: "mylib",
673 srcs: ["mylib.cpp"],
674 shared_libs: ["mylib2", "mylib3"],
675 system_shared_libs: [],
676 stl: "none",
677 }
678
679 cc_library {
680 name: "mylib2",
681 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900682 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900683 system_shared_libs: [],
684 stl: "none",
685 stubs: {
686 versions: ["1", "2", "3"],
687 },
688 }
689
690 cc_library {
691 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900692 srcs: ["mylib.cpp"],
693 shared_libs: ["mylib4"],
694 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900695 stl: "none",
696 stubs: {
697 versions: ["10", "11", "12"],
698 },
699 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900700
701 cc_library {
702 name: "mylib4",
703 srcs: ["mylib.cpp"],
704 system_shared_libs: [],
705 stl: "none",
706 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900707 `)
708
Sundong Ahnabb64432019-10-22 13:58:29 +0900709 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900710 copyCmds := apexRule.Args["copy_commands"]
711
712 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800713 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900714
715 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800716 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900717
718 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800719 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900720
Colin Cross7113d202019-11-20 16:39:12 -0800721 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900722
723 // Ensure that mylib is linking with the latest version of stubs for mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800724 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900725 // ... and not linking to the non-stub (impl) variant of mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800726 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900727
728 // 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 -0800729 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900730 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800731 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900732
733 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800734 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900735 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900736
737 // Ensure that genstub is invoked with --apex
Colin Cross7113d202019-11-20 16:39:12 -0800738 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3_myapex").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900739
740 ensureExactContents(t, ctx, "myapex", []string{
741 "lib64/mylib.so",
742 "lib64/mylib3.so",
743 "lib64/mylib4.so",
744 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900745}
746
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900747func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700748 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900749 apex {
750 name: "myapex",
751 key: "myapex.key",
752 native_shared_libs: ["mylib"],
753 }
754
755 apex_key {
756 name: "myapex.key",
757 public_key: "testkey.avbpubkey",
758 private_key: "testkey.pem",
759 }
760
761 cc_library {
762 name: "mylib",
763 srcs: ["mylib.cpp"],
764 shared_libs: ["libfoo#10"],
765 system_shared_libs: [],
766 stl: "none",
767 }
768
769 cc_library {
770 name: "libfoo",
771 srcs: ["mylib.cpp"],
772 shared_libs: ["libbar"],
773 system_shared_libs: [],
774 stl: "none",
775 stubs: {
776 versions: ["10", "20", "30"],
777 },
778 }
779
780 cc_library {
781 name: "libbar",
782 srcs: ["mylib.cpp"],
783 system_shared_libs: [],
784 stl: "none",
785 }
786
787 `)
788
Sundong Ahnabb64432019-10-22 13:58:29 +0900789 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900790 copyCmds := apexRule.Args["copy_commands"]
791
792 // Ensure that direct non-stubs dep is always included
793 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
794
795 // Ensure that indirect stubs dep is not included
796 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
797
798 // Ensure that dependency of stubs is not included
799 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
800
Colin Cross7113d202019-11-20 16:39:12 -0800801 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900802
803 // Ensure that mylib is linking with version 10 of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800804 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900805 // ... and not linking to the non-stub (impl) variant of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800806 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900807
Colin Cross7113d202019-11-20 16:39:12 -0800808 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900809
810 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
811 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
812}
813
Jooyung Hand3639552019-08-09 12:57:43 +0900814func TestApexWithRuntimeLibsDependency(t *testing.T) {
815 /*
816 myapex
817 |
818 v (runtime_libs)
819 mylib ------+------> libfoo [provides stub]
820 |
821 `------> libbar
822 */
823 ctx, _ := testApex(t, `
824 apex {
825 name: "myapex",
826 key: "myapex.key",
827 native_shared_libs: ["mylib"],
828 }
829
830 apex_key {
831 name: "myapex.key",
832 public_key: "testkey.avbpubkey",
833 private_key: "testkey.pem",
834 }
835
836 cc_library {
837 name: "mylib",
838 srcs: ["mylib.cpp"],
839 runtime_libs: ["libfoo", "libbar"],
840 system_shared_libs: [],
841 stl: "none",
842 }
843
844 cc_library {
845 name: "libfoo",
846 srcs: ["mylib.cpp"],
847 system_shared_libs: [],
848 stl: "none",
849 stubs: {
850 versions: ["10", "20", "30"],
851 },
852 }
853
854 cc_library {
855 name: "libbar",
856 srcs: ["mylib.cpp"],
857 system_shared_libs: [],
858 stl: "none",
859 }
860
861 `)
862
Sundong Ahnabb64432019-10-22 13:58:29 +0900863 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900864 copyCmds := apexRule.Args["copy_commands"]
865
866 // Ensure that direct non-stubs dep is always included
867 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
868
869 // Ensure that indirect stubs dep is not included
870 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
871
872 // Ensure that runtime_libs dep in included
873 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
874
Sundong Ahnabb64432019-10-22 13:58:29 +0900875 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900876 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
877 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900878
879}
880
Jooyung Han9c80bae2019-08-20 17:30:57 +0900881func TestApexDependencyToLLNDK(t *testing.T) {
882 ctx, _ := testApex(t, `
883 apex {
884 name: "myapex",
885 key: "myapex.key",
886 use_vendor: true,
887 native_shared_libs: ["mylib"],
888 }
889
890 apex_key {
891 name: "myapex.key",
892 public_key: "testkey.avbpubkey",
893 private_key: "testkey.pem",
894 }
895
896 cc_library {
897 name: "mylib",
898 srcs: ["mylib.cpp"],
899 vendor_available: true,
900 shared_libs: ["libbar"],
901 system_shared_libs: [],
902 stl: "none",
903 }
904
905 cc_library {
906 name: "libbar",
907 srcs: ["mylib.cpp"],
908 system_shared_libs: [],
909 stl: "none",
910 }
911
912 llndk_library {
913 name: "libbar",
914 symbol_file: "",
915 }
Jooyung Handc782442019-11-01 03:14:38 +0900916 `, func(fs map[string][]byte, config android.Config) {
917 setUseVendorWhitelistForTest(config, []string{"myapex"})
918 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900919
Sundong Ahnabb64432019-10-22 13:58:29 +0900920 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900921 copyCmds := apexRule.Args["copy_commands"]
922
923 // Ensure that LLNDK dep is not included
924 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
925
Sundong Ahnabb64432019-10-22 13:58:29 +0900926 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900927 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900928
929 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900930 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900931
932}
933
Jiyong Park25fc6a92018-11-18 18:02:45 +0900934func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700935 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900936 apex {
937 name: "myapex",
938 key: "myapex.key",
939 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
940 }
941
942 apex_key {
943 name: "myapex.key",
944 public_key: "testkey.avbpubkey",
945 private_key: "testkey.pem",
946 }
947
948 cc_library {
949 name: "mylib",
950 srcs: ["mylib.cpp"],
951 shared_libs: ["libdl#27"],
952 stl: "none",
953 }
954
955 cc_library_shared {
956 name: "mylib_shared",
957 srcs: ["mylib.cpp"],
958 shared_libs: ["libdl#27"],
959 stl: "none",
960 }
961
962 cc_library {
963 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700964 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900965 nocrt: true,
966 system_shared_libs: [],
967 stl: "none",
968 stubs: {
969 versions: ["27", "28", "29"],
970 },
971 }
972
973 cc_library {
974 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700975 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900976 nocrt: true,
977 system_shared_libs: [],
978 stl: "none",
979 stubs: {
980 versions: ["27", "28", "29"],
981 },
982 }
983
984 cc_library {
985 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700986 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900987 nocrt: true,
988 system_shared_libs: [],
989 stl: "none",
990 stubs: {
991 versions: ["27", "28", "29"],
992 },
993 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900994
995 cc_library {
996 name: "libBootstrap",
997 srcs: ["mylib.cpp"],
998 stl: "none",
999 bootstrap: true,
1000 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001001 `)
1002
Sundong Ahnabb64432019-10-22 13:58:29 +09001003 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001004 copyCmds := apexRule.Args["copy_commands"]
1005
1006 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001007 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001008 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1009 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001010
1011 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001012 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001013
Colin Cross7113d202019-11-20 16:39:12 -08001014 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1015 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1016 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001017
1018 // For dependency to libc
1019 // Ensure that mylib is linking with the latest version of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001020 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001021 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001022 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001023 // ... Cflags from stub is correctly exported to mylib
1024 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1025 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1026
1027 // For dependency to libm
1028 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001029 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001030 // ... and not linking to the stub variant
Colin Cross7113d202019-11-20 16:39:12 -08001031 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001032 // ... and is not compiling with the stub
1033 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1034 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1035
1036 // For dependency to libdl
1037 // Ensure that mylib is linking with the specified version of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001038 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001039 // ... and not linking to the other versions of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001040 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28_myapex/libdl.so")
1041 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001042 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001043 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001044 // ... Cflags from stub is correctly exported to mylib
1045 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1046 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001047
1048 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001049 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1050 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1051 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1052 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001053}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001054
1055func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001056 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001057 apex {
1058 name: "myapex",
1059 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001060 native_shared_libs: ["mylib"],
1061 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001062 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001063 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001064 }
1065
1066 apex_key {
1067 name: "myapex.key",
1068 public_key: "testkey.avbpubkey",
1069 private_key: "testkey.pem",
1070 }
1071
1072 prebuilt_etc {
1073 name: "myetc",
1074 src: "myprebuilt",
1075 sub_dir: "foo/bar",
1076 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001077
1078 cc_library {
1079 name: "mylib",
1080 srcs: ["mylib.cpp"],
1081 relative_install_path: "foo/bar",
1082 system_shared_libs: [],
1083 stl: "none",
1084 }
1085
1086 cc_binary {
1087 name: "mybin",
1088 srcs: ["mylib.cpp"],
1089 relative_install_path: "foo/bar",
1090 system_shared_libs: [],
1091 static_executable: true,
1092 stl: "none",
1093 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001094 `)
1095
Sundong Ahnabb64432019-10-22 13:58:29 +09001096 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001097 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1098
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001099 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001100 ensureListContains(t, dirs, "etc")
1101 ensureListContains(t, dirs, "etc/foo")
1102 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001103 ensureListContains(t, dirs, "lib64")
1104 ensureListContains(t, dirs, "lib64/foo")
1105 ensureListContains(t, dirs, "lib64/foo/bar")
1106 ensureListContains(t, dirs, "lib")
1107 ensureListContains(t, dirs, "lib/foo")
1108 ensureListContains(t, dirs, "lib/foo/bar")
1109
Jiyong Parkbd13e442019-03-15 18:10:35 +09001110 ensureListContains(t, dirs, "bin")
1111 ensureListContains(t, dirs, "bin/foo")
1112 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001113}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001114
1115func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001116 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001117 apex {
1118 name: "myapex",
1119 key: "myapex.key",
1120 native_shared_libs: ["mylib"],
1121 use_vendor: true,
1122 }
1123
1124 apex_key {
1125 name: "myapex.key",
1126 public_key: "testkey.avbpubkey",
1127 private_key: "testkey.pem",
1128 }
1129
1130 cc_library {
1131 name: "mylib",
1132 srcs: ["mylib.cpp"],
1133 shared_libs: ["mylib2"],
1134 system_shared_libs: [],
1135 vendor_available: true,
1136 stl: "none",
1137 }
1138
1139 cc_library {
1140 name: "mylib2",
1141 srcs: ["mylib.cpp"],
1142 system_shared_libs: [],
1143 vendor_available: true,
1144 stl: "none",
1145 }
Jooyung Handc782442019-11-01 03:14:38 +09001146 `, func(fs map[string][]byte, config android.Config) {
1147 setUseVendorWhitelistForTest(config, []string{"myapex"})
1148 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001149
1150 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001151 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001152 for _, implicit := range i.Implicits {
1153 inputsList = append(inputsList, implicit.String())
1154 }
1155 }
1156 inputsString := strings.Join(inputsList, " ")
1157
1158 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001159 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1160 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001161
1162 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001163 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1164 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001165}
Jiyong Park16e91a02018-12-20 18:18:08 +09001166
Jooyung Handc782442019-11-01 03:14:38 +09001167func TestUseVendorRestriction(t *testing.T) {
1168 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1169 apex {
1170 name: "myapex",
1171 key: "myapex.key",
1172 use_vendor: true,
1173 }
1174 apex_key {
1175 name: "myapex.key",
1176 public_key: "testkey.avbpubkey",
1177 private_key: "testkey.pem",
1178 }
1179 `, func(fs map[string][]byte, config android.Config) {
1180 setUseVendorWhitelistForTest(config, []string{""})
1181 })
1182 // no error with whitelist
1183 testApex(t, `
1184 apex {
1185 name: "myapex",
1186 key: "myapex.key",
1187 use_vendor: true,
1188 }
1189 apex_key {
1190 name: "myapex.key",
1191 public_key: "testkey.avbpubkey",
1192 private_key: "testkey.pem",
1193 }
1194 `, func(fs map[string][]byte, config android.Config) {
1195 setUseVendorWhitelistForTest(config, []string{"myapex"})
1196 })
1197}
1198
Jooyung Han5c998b92019-06-27 11:30:33 +09001199func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1200 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1201 apex {
1202 name: "myapex",
1203 key: "myapex.key",
1204 native_shared_libs: ["mylib"],
1205 use_vendor: true,
1206 }
1207
1208 apex_key {
1209 name: "myapex.key",
1210 public_key: "testkey.avbpubkey",
1211 private_key: "testkey.pem",
1212 }
1213
1214 cc_library {
1215 name: "mylib",
1216 srcs: ["mylib.cpp"],
1217 system_shared_libs: [],
1218 stl: "none",
1219 }
1220 `)
1221}
1222
Jiyong Park16e91a02018-12-20 18:18:08 +09001223func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001224 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001225 apex {
1226 name: "myapex",
1227 key: "myapex.key",
1228 native_shared_libs: ["mylib"],
1229 }
1230
1231 apex_key {
1232 name: "myapex.key",
1233 public_key: "testkey.avbpubkey",
1234 private_key: "testkey.pem",
1235 }
1236
1237 cc_library {
1238 name: "mylib",
1239 srcs: ["mylib.cpp"],
1240 system_shared_libs: [],
1241 stl: "none",
1242 stubs: {
1243 versions: ["1", "2", "3"],
1244 },
1245 }
1246
1247 cc_binary {
1248 name: "not_in_apex",
1249 srcs: ["mylib.cpp"],
1250 static_libs: ["mylib"],
1251 static_executable: true,
1252 system_shared_libs: [],
1253 stl: "none",
1254 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001255 `)
1256
Colin Cross7113d202019-11-20 16:39:12 -08001257 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001258
1259 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001260 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001261}
Jiyong Park9335a262018-12-24 11:31:58 +09001262
1263func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001264 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001265 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001266 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001267 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001268 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001269 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001270 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001271 }
1272
1273 cc_library {
1274 name: "mylib",
1275 srcs: ["mylib.cpp"],
1276 system_shared_libs: [],
1277 stl: "none",
1278 }
1279
1280 apex_key {
1281 name: "myapex.key",
1282 public_key: "testkey.avbpubkey",
1283 private_key: "testkey.pem",
1284 }
1285
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001286 android_app_certificate {
1287 name: "myapex.certificate",
1288 certificate: "testkey",
1289 }
1290
1291 android_app_certificate {
1292 name: "myapex.certificate.override",
1293 certificate: "testkey.override",
1294 }
1295
Jiyong Park9335a262018-12-24 11:31:58 +09001296 `)
1297
1298 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001299 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001300
1301 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1302 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1303 "vendor/foo/devkeys/testkey.avbpubkey")
1304 }
1305 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1306 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1307 "vendor/foo/devkeys/testkey.pem")
1308 }
1309
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001310 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001311 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001312 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001313 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001314 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001315 }
1316}
Jiyong Park58e364a2019-01-19 19:24:06 +09001317
Jooyung Hanf121a652019-12-17 14:30:11 +09001318func TestCertificate(t *testing.T) {
1319 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1320 ctx, _ := testApex(t, `
1321 apex {
1322 name: "myapex",
1323 key: "myapex.key",
1324 }
1325 apex_key {
1326 name: "myapex.key",
1327 public_key: "testkey.avbpubkey",
1328 private_key: "testkey.pem",
1329 }`)
1330 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1331 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1332 if actual := rule.Args["certificates"]; actual != expected {
1333 t.Errorf("certificates should be %q, not %q", expected, actual)
1334 }
1335 })
1336 t.Run("override when unspecified", func(t *testing.T) {
1337 ctx, _ := testApex(t, `
1338 apex {
1339 name: "myapex_keytest",
1340 key: "myapex.key",
1341 file_contexts: ":myapex-file_contexts",
1342 }
1343 apex_key {
1344 name: "myapex.key",
1345 public_key: "testkey.avbpubkey",
1346 private_key: "testkey.pem",
1347 }
1348 android_app_certificate {
1349 name: "myapex.certificate.override",
1350 certificate: "testkey.override",
1351 }`)
1352 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1353 expected := "testkey.override.x509.pem testkey.override.pk8"
1354 if actual := rule.Args["certificates"]; actual != expected {
1355 t.Errorf("certificates should be %q, not %q", expected, actual)
1356 }
1357 })
1358 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1359 ctx, _ := testApex(t, `
1360 apex {
1361 name: "myapex",
1362 key: "myapex.key",
1363 certificate: ":myapex.certificate",
1364 }
1365 apex_key {
1366 name: "myapex.key",
1367 public_key: "testkey.avbpubkey",
1368 private_key: "testkey.pem",
1369 }
1370 android_app_certificate {
1371 name: "myapex.certificate",
1372 certificate: "testkey",
1373 }`)
1374 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1375 expected := "testkey.x509.pem testkey.pk8"
1376 if actual := rule.Args["certificates"]; actual != expected {
1377 t.Errorf("certificates should be %q, not %q", expected, actual)
1378 }
1379 })
1380 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1381 ctx, _ := testApex(t, `
1382 apex {
1383 name: "myapex_keytest",
1384 key: "myapex.key",
1385 file_contexts: ":myapex-file_contexts",
1386 certificate: ":myapex.certificate",
1387 }
1388 apex_key {
1389 name: "myapex.key",
1390 public_key: "testkey.avbpubkey",
1391 private_key: "testkey.pem",
1392 }
1393 android_app_certificate {
1394 name: "myapex.certificate.override",
1395 certificate: "testkey.override",
1396 }`)
1397 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1398 expected := "testkey.override.x509.pem testkey.override.pk8"
1399 if actual := rule.Args["certificates"]; actual != expected {
1400 t.Errorf("certificates should be %q, not %q", expected, actual)
1401 }
1402 })
1403 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1404 ctx, _ := testApex(t, `
1405 apex {
1406 name: "myapex",
1407 key: "myapex.key",
1408 certificate: "testkey",
1409 }
1410 apex_key {
1411 name: "myapex.key",
1412 public_key: "testkey.avbpubkey",
1413 private_key: "testkey.pem",
1414 }`)
1415 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1416 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1417 if actual := rule.Args["certificates"]; actual != expected {
1418 t.Errorf("certificates should be %q, not %q", expected, actual)
1419 }
1420 })
1421 t.Run("override when specified as <name>", func(t *testing.T) {
1422 ctx, _ := testApex(t, `
1423 apex {
1424 name: "myapex_keytest",
1425 key: "myapex.key",
1426 file_contexts: ":myapex-file_contexts",
1427 certificate: "testkey",
1428 }
1429 apex_key {
1430 name: "myapex.key",
1431 public_key: "testkey.avbpubkey",
1432 private_key: "testkey.pem",
1433 }
1434 android_app_certificate {
1435 name: "myapex.certificate.override",
1436 certificate: "testkey.override",
1437 }`)
1438 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1439 expected := "testkey.override.x509.pem testkey.override.pk8"
1440 if actual := rule.Args["certificates"]; actual != expected {
1441 t.Errorf("certificates should be %q, not %q", expected, actual)
1442 }
1443 })
1444}
1445
Jiyong Park58e364a2019-01-19 19:24:06 +09001446func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001447 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001448 apex {
1449 name: "myapex",
1450 key: "myapex.key",
1451 native_shared_libs: ["mylib"],
1452 }
1453
1454 apex {
1455 name: "otherapex",
1456 key: "myapex.key",
1457 native_shared_libs: ["mylib"],
1458 }
1459
1460 apex_key {
1461 name: "myapex.key",
1462 public_key: "testkey.avbpubkey",
1463 private_key: "testkey.pem",
1464 }
1465
1466 cc_library {
1467 name: "mylib",
1468 srcs: ["mylib.cpp"],
1469 system_shared_libs: [],
1470 stl: "none",
1471 }
1472 `)
1473
Jooyung Han6b8459b2019-10-30 08:29:25 +09001474 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001475 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001476 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001477 ensureNotContains(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_myapex").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 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1484 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001485
Jooyung Han6b8459b2019-10-30 08:29:25 +09001486 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001487 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001488 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001489 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1490 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001491}
Jiyong Park7e636d02019-01-28 16:16:54 +09001492
1493func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001494 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001495 apex {
1496 name: "myapex",
1497 key: "myapex.key",
1498 native_shared_libs: ["mylib"],
1499 }
1500
1501 apex_key {
1502 name: "myapex.key",
1503 public_key: "testkey.avbpubkey",
1504 private_key: "testkey.pem",
1505 }
1506
1507 cc_library_headers {
1508 name: "mylib_headers",
1509 export_include_dirs: ["my_include"],
1510 system_shared_libs: [],
1511 stl: "none",
1512 }
1513
1514 cc_library {
1515 name: "mylib",
1516 srcs: ["mylib.cpp"],
1517 system_shared_libs: [],
1518 stl: "none",
1519 header_libs: ["mylib_headers"],
1520 export_header_lib_headers: ["mylib_headers"],
1521 stubs: {
1522 versions: ["1", "2", "3"],
1523 },
1524 }
1525
1526 cc_library {
1527 name: "otherlib",
1528 srcs: ["mylib.cpp"],
1529 system_shared_libs: [],
1530 stl: "none",
1531 shared_libs: ["mylib"],
1532 }
1533 `)
1534
Colin Cross7113d202019-11-20 16:39:12 -08001535 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001536
1537 // Ensure that the include path of the header lib is exported to 'otherlib'
1538 ensureContains(t, cFlags, "-Imy_include")
1539}
Alex Light9670d332019-01-29 18:07:33 -08001540
Jooyung Han31c470b2019-10-18 16:26:59 +09001541func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1542 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001543 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001544 copyCmds := apexRule.Args["copy_commands"]
1545 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001546 var failed bool
1547 var surplus []string
1548 filesMatched := make(map[string]bool)
1549 addContent := func(content string) {
1550 for _, expected := range files {
1551 if matched, _ := path.Match(expected, content); matched {
1552 filesMatched[expected] = true
1553 return
1554 }
1555 }
1556 surplus = append(surplus, content)
1557 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001558 for _, cmd := range strings.Split(copyCmds, "&&") {
1559 cmd = strings.TrimSpace(cmd)
1560 if cmd == "" {
1561 continue
1562 }
1563 terms := strings.Split(cmd, " ")
1564 switch terms[0] {
1565 case "mkdir":
1566 case "cp":
1567 if len(terms) != 3 {
1568 t.Fatal("copyCmds contains invalid cp command", cmd)
1569 }
1570 dst := terms[2]
1571 index := strings.Index(dst, imageApexDir)
1572 if index == -1 {
1573 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1574 }
1575 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001576 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001577 default:
1578 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1579 }
1580 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001581
Jooyung Han31c470b2019-10-18 16:26:59 +09001582 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001583 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001584 t.Log("surplus files", surplus)
1585 failed = true
1586 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001587
1588 if len(files) > len(filesMatched) {
1589 var missing []string
1590 for _, expected := range files {
1591 if !filesMatched[expected] {
1592 missing = append(missing, expected)
1593 }
1594 }
1595 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001596 t.Log("missing files", missing)
1597 failed = true
1598 }
1599 if failed {
1600 t.Fail()
1601 }
1602}
1603
Jooyung Han344d5432019-08-23 11:17:39 +09001604func TestVndkApexCurrent(t *testing.T) {
1605 ctx, _ := testApex(t, `
1606 apex_vndk {
1607 name: "myapex",
1608 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001609 }
1610
1611 apex_key {
1612 name: "myapex.key",
1613 public_key: "testkey.avbpubkey",
1614 private_key: "testkey.pem",
1615 }
1616
1617 cc_library {
1618 name: "libvndk",
1619 srcs: ["mylib.cpp"],
1620 vendor_available: true,
1621 vndk: {
1622 enabled: true,
1623 },
1624 system_shared_libs: [],
1625 stl: "none",
1626 }
1627
1628 cc_library {
1629 name: "libvndksp",
1630 srcs: ["mylib.cpp"],
1631 vendor_available: true,
1632 vndk: {
1633 enabled: true,
1634 support_system_process: true,
1635 },
1636 system_shared_libs: [],
1637 stl: "none",
1638 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001639 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001640
Jooyung Han31c470b2019-10-18 16:26:59 +09001641 ensureExactContents(t, ctx, "myapex", []string{
1642 "lib/libvndk.so",
1643 "lib/libvndksp.so",
1644 "lib64/libvndk.so",
1645 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001646 "etc/llndk.libraries.VER.txt",
1647 "etc/vndkcore.libraries.VER.txt",
1648 "etc/vndksp.libraries.VER.txt",
1649 "etc/vndkprivate.libraries.VER.txt",
1650 "etc/vndkcorevariant.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001651 })
Jooyung Han344d5432019-08-23 11:17:39 +09001652}
1653
1654func TestVndkApexWithPrebuilt(t *testing.T) {
1655 ctx, _ := testApex(t, `
1656 apex_vndk {
1657 name: "myapex",
1658 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001659 }
1660
1661 apex_key {
1662 name: "myapex.key",
1663 public_key: "testkey.avbpubkey",
1664 private_key: "testkey.pem",
1665 }
1666
1667 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001668 name: "libvndk",
1669 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001670 vendor_available: true,
1671 vndk: {
1672 enabled: true,
1673 },
1674 system_shared_libs: [],
1675 stl: "none",
1676 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001677
1678 cc_prebuilt_library_shared {
1679 name: "libvndk.arm",
1680 srcs: ["libvndk.arm.so"],
1681 vendor_available: true,
1682 vndk: {
1683 enabled: true,
1684 },
1685 enabled: false,
1686 arch: {
1687 arm: {
1688 enabled: true,
1689 },
1690 },
1691 system_shared_libs: [],
1692 stl: "none",
1693 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001694 `+vndkLibrariesTxtFiles("current"),
1695 withFiles(map[string][]byte{
1696 "libvndk.so": nil,
1697 "libvndk.arm.so": nil,
1698 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001699
Jooyung Han31c470b2019-10-18 16:26:59 +09001700 ensureExactContents(t, ctx, "myapex", []string{
1701 "lib/libvndk.so",
1702 "lib/libvndk.arm.so",
1703 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001704 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001705 })
Jooyung Han344d5432019-08-23 11:17:39 +09001706}
1707
Jooyung Han39edb6c2019-11-06 16:53:07 +09001708func vndkLibrariesTxtFiles(vers ...string) (result string) {
1709 for _, v := range vers {
1710 if v == "current" {
1711 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkcorevariant"} {
1712 result += `
1713 vndk_libraries_txt {
1714 name: "` + txt + `.libraries.txt",
1715 }
1716 `
1717 }
1718 } else {
1719 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1720 result += `
1721 prebuilt_etc {
1722 name: "` + txt + `.libraries.` + v + `.txt",
1723 src: "dummy.txt",
1724 }
1725 `
1726 }
1727 }
1728 }
1729 return
1730}
1731
Jooyung Han344d5432019-08-23 11:17:39 +09001732func TestVndkApexVersion(t *testing.T) {
1733 ctx, _ := testApex(t, `
1734 apex_vndk {
1735 name: "myapex_v27",
1736 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001737 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001738 vndk_version: "27",
1739 }
1740
1741 apex_key {
1742 name: "myapex.key",
1743 public_key: "testkey.avbpubkey",
1744 private_key: "testkey.pem",
1745 }
1746
Jooyung Han31c470b2019-10-18 16:26:59 +09001747 vndk_prebuilt_shared {
1748 name: "libvndk27",
1749 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001750 vendor_available: true,
1751 vndk: {
1752 enabled: true,
1753 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001754 target_arch: "arm64",
1755 arch: {
1756 arm: {
1757 srcs: ["libvndk27_arm.so"],
1758 },
1759 arm64: {
1760 srcs: ["libvndk27_arm64.so"],
1761 },
1762 },
Jooyung Han344d5432019-08-23 11:17:39 +09001763 }
1764
1765 vndk_prebuilt_shared {
1766 name: "libvndk27",
1767 version: "27",
1768 vendor_available: true,
1769 vndk: {
1770 enabled: true,
1771 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001772 target_arch: "x86_64",
1773 arch: {
1774 x86: {
1775 srcs: ["libvndk27_x86.so"],
1776 },
1777 x86_64: {
1778 srcs: ["libvndk27_x86_64.so"],
1779 },
1780 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001781 }
1782 `+vndkLibrariesTxtFiles("27"),
1783 withFiles(map[string][]byte{
1784 "libvndk27_arm.so": nil,
1785 "libvndk27_arm64.so": nil,
1786 "libvndk27_x86.so": nil,
1787 "libvndk27_x86_64.so": nil,
1788 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001789
Jooyung Han31c470b2019-10-18 16:26:59 +09001790 ensureExactContents(t, ctx, "myapex_v27", []string{
1791 "lib/libvndk27_arm.so",
1792 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001793 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001794 })
Jooyung Han344d5432019-08-23 11:17:39 +09001795}
1796
1797func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1798 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1799 apex_vndk {
1800 name: "myapex_v27",
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 apex_vndk {
1806 name: "myapex_v27_other",
1807 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001808 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001809 vndk_version: "27",
1810 }
1811
1812 apex_key {
1813 name: "myapex.key",
1814 public_key: "testkey.avbpubkey",
1815 private_key: "testkey.pem",
1816 }
1817
1818 cc_library {
1819 name: "libvndk",
1820 srcs: ["mylib.cpp"],
1821 vendor_available: true,
1822 vndk: {
1823 enabled: true,
1824 },
1825 system_shared_libs: [],
1826 stl: "none",
1827 }
1828
1829 vndk_prebuilt_shared {
1830 name: "libvndk",
1831 version: "27",
1832 vendor_available: true,
1833 vndk: {
1834 enabled: true,
1835 },
1836 srcs: ["libvndk.so"],
1837 }
1838 `, withFiles(map[string][]byte{
1839 "libvndk.so": nil,
1840 }))
1841}
1842
Jooyung Han90eee022019-10-01 20:02:42 +09001843func TestVndkApexNameRule(t *testing.T) {
1844 ctx, _ := testApex(t, `
1845 apex_vndk {
1846 name: "myapex",
1847 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001848 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001849 }
1850 apex_vndk {
1851 name: "myapex_v28",
1852 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001853 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001854 vndk_version: "28",
1855 }
1856 apex_key {
1857 name: "myapex.key",
1858 public_key: "testkey.avbpubkey",
1859 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001860 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001861
1862 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001863 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001864 actual := proptools.String(bundle.properties.Apex_name)
1865 if !reflect.DeepEqual(actual, expected) {
1866 t.Errorf("Got '%v', expected '%v'", actual, expected)
1867 }
1868 }
1869
1870 assertApexName("com.android.vndk.vVER", "myapex")
1871 assertApexName("com.android.vndk.v28", "myapex_v28")
1872}
1873
Jooyung Han344d5432019-08-23 11:17:39 +09001874func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1875 ctx, _ := testApex(t, `
1876 apex_vndk {
1877 name: "myapex",
1878 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001879 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001880 }
1881
1882 apex_key {
1883 name: "myapex.key",
1884 public_key: "testkey.avbpubkey",
1885 private_key: "testkey.pem",
1886 }
1887
1888 cc_library {
1889 name: "libvndk",
1890 srcs: ["mylib.cpp"],
1891 vendor_available: true,
1892 native_bridge_supported: true,
1893 host_supported: true,
1894 vndk: {
1895 enabled: true,
1896 },
1897 system_shared_libs: [],
1898 stl: "none",
1899 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001900 `+vndkLibrariesTxtFiles("current"),
1901 withTargets(map[android.OsType][]android.Target{
1902 android.Android: []android.Target{
1903 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1904 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1905 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1906 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1907 },
1908 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001909
Jooyung Han31c470b2019-10-18 16:26:59 +09001910 ensureExactContents(t, ctx, "myapex", []string{
1911 "lib/libvndk.so",
1912 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001913 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001914 })
Jooyung Han344d5432019-08-23 11:17:39 +09001915}
1916
1917func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1918 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1919 apex_vndk {
1920 name: "myapex",
1921 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001922 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001923 native_bridge_supported: true,
1924 }
1925
1926 apex_key {
1927 name: "myapex.key",
1928 public_key: "testkey.avbpubkey",
1929 private_key: "testkey.pem",
1930 }
1931
1932 cc_library {
1933 name: "libvndk",
1934 srcs: ["mylib.cpp"],
1935 vendor_available: true,
1936 native_bridge_supported: true,
1937 host_supported: true,
1938 vndk: {
1939 enabled: true,
1940 },
1941 system_shared_libs: [],
1942 stl: "none",
1943 }
1944 `)
1945}
1946
Jooyung Han31c470b2019-10-18 16:26:59 +09001947func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001948 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001949 apex_vndk {
1950 name: "myapex_v27",
1951 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001952 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001953 vndk_version: "27",
1954 }
1955
1956 apex_key {
1957 name: "myapex.key",
1958 public_key: "testkey.avbpubkey",
1959 private_key: "testkey.pem",
1960 }
1961
1962 vndk_prebuilt_shared {
1963 name: "libvndk27",
1964 version: "27",
1965 target_arch: "arm",
1966 vendor_available: true,
1967 vndk: {
1968 enabled: true,
1969 },
1970 arch: {
1971 arm: {
1972 srcs: ["libvndk27.so"],
1973 }
1974 },
1975 }
1976
1977 vndk_prebuilt_shared {
1978 name: "libvndk27",
1979 version: "27",
1980 target_arch: "arm",
1981 binder32bit: true,
1982 vendor_available: true,
1983 vndk: {
1984 enabled: true,
1985 },
1986 arch: {
1987 arm: {
1988 srcs: ["libvndk27binder32.so"],
1989 }
1990 },
1991 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001992 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001993 withFiles(map[string][]byte{
1994 "libvndk27.so": nil,
1995 "libvndk27binder32.so": nil,
1996 }),
1997 withBinder32bit,
1998 withTargets(map[android.OsType][]android.Target{
1999 android.Android: []android.Target{
2000 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2001 },
2002 }),
2003 )
2004
2005 ensureExactContents(t, ctx, "myapex_v27", []string{
2006 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002007 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002008 })
2009}
2010
Jooyung Hane1633032019-08-01 17:41:43 +09002011func TestDependenciesInApexManifest(t *testing.T) {
2012 ctx, _ := testApex(t, `
2013 apex {
2014 name: "myapex_nodep",
2015 key: "myapex.key",
2016 native_shared_libs: ["lib_nodep"],
2017 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002018 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002019 }
2020
2021 apex {
2022 name: "myapex_dep",
2023 key: "myapex.key",
2024 native_shared_libs: ["lib_dep"],
2025 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002026 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002027 }
2028
2029 apex {
2030 name: "myapex_provider",
2031 key: "myapex.key",
2032 native_shared_libs: ["libfoo"],
2033 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002034 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002035 }
2036
2037 apex {
2038 name: "myapex_selfcontained",
2039 key: "myapex.key",
2040 native_shared_libs: ["lib_dep", "libfoo"],
2041 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002042 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002043 }
2044
2045 apex_key {
2046 name: "myapex.key",
2047 public_key: "testkey.avbpubkey",
2048 private_key: "testkey.pem",
2049 }
2050
2051 cc_library {
2052 name: "lib_nodep",
2053 srcs: ["mylib.cpp"],
2054 system_shared_libs: [],
2055 stl: "none",
2056 }
2057
2058 cc_library {
2059 name: "lib_dep",
2060 srcs: ["mylib.cpp"],
2061 shared_libs: ["libfoo"],
2062 system_shared_libs: [],
2063 stl: "none",
2064 }
2065
2066 cc_library {
2067 name: "libfoo",
2068 srcs: ["mytest.cpp"],
2069 stubs: {
2070 versions: ["1"],
2071 },
2072 system_shared_libs: [],
2073 stl: "none",
2074 }
2075 `)
2076
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002077 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002078 var provideNativeLibs, requireNativeLibs []string
2079
Sundong Ahnabb64432019-10-22 13:58:29 +09002080 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_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 ensureListEmpty(t, requireNativeLibs)
2085
Sundong Ahnabb64432019-10-22 13:58:29 +09002086 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_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 ensureListEmpty(t, provideNativeLibs)
2090 ensureListContains(t, requireNativeLibs, "libfoo.so")
2091
Sundong Ahnabb64432019-10-22 13:58:29 +09002092 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_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
Sundong Ahnabb64432019-10-22 13:58:29 +09002098 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002099 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2100 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002101 ensureListContains(t, provideNativeLibs, "libfoo.so")
2102 ensureListEmpty(t, requireNativeLibs)
2103}
2104
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002105func TestApexName(t *testing.T) {
2106 ctx, _ := testApex(t, `
2107 apex {
2108 name: "myapex",
2109 key: "myapex.key",
2110 apex_name: "com.android.myapex",
2111 }
2112
2113 apex_key {
2114 name: "myapex.key",
2115 public_key: "testkey.avbpubkey",
2116 private_key: "testkey.pem",
2117 }
2118 `)
2119
Sundong Ahnabb64432019-10-22 13:58:29 +09002120 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002121 apexManifestRule := module.Rule("apexManifestRule")
2122 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2123 apexRule := module.Rule("apexRule")
2124 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
2125}
2126
Alex Light0851b882019-02-07 13:20:53 -08002127func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002128 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002129 apex {
2130 name: "myapex",
2131 key: "myapex.key",
2132 native_shared_libs: ["mylib_common"],
2133 }
2134
2135 apex_key {
2136 name: "myapex.key",
2137 public_key: "testkey.avbpubkey",
2138 private_key: "testkey.pem",
2139 }
2140
2141 cc_library {
2142 name: "mylib_common",
2143 srcs: ["mylib.cpp"],
2144 system_shared_libs: [],
2145 stl: "none",
2146 }
2147 `)
2148
Sundong Ahnabb64432019-10-22 13:58:29 +09002149 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002150 apexRule := module.Rule("apexRule")
2151 copyCmds := apexRule.Args["copy_commands"]
2152
2153 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2154 t.Log("Apex was a test apex!")
2155 t.Fail()
2156 }
2157 // Ensure that main rule creates an output
2158 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2159
2160 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002161 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002162
2163 // Ensure that both direct and indirect deps are copied into apex
2164 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2165
Colin Cross7113d202019-11-20 16:39:12 -08002166 // Ensure that the platform variant ends with _shared
2167 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002168
2169 if !android.InAnyApex("mylib_common") {
2170 t.Log("Found mylib_common not in any apex!")
2171 t.Fail()
2172 }
2173}
2174
2175func TestTestApex(t *testing.T) {
2176 if android.InAnyApex("mylib_common_test") {
2177 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!")
2178 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002179 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002180 apex_test {
2181 name: "myapex",
2182 key: "myapex.key",
2183 native_shared_libs: ["mylib_common_test"],
2184 }
2185
2186 apex_key {
2187 name: "myapex.key",
2188 public_key: "testkey.avbpubkey",
2189 private_key: "testkey.pem",
2190 }
2191
2192 cc_library {
2193 name: "mylib_common_test",
2194 srcs: ["mylib.cpp"],
2195 system_shared_libs: [],
2196 stl: "none",
2197 }
2198 `)
2199
Sundong Ahnabb64432019-10-22 13:58:29 +09002200 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002201 apexRule := module.Rule("apexRule")
2202 copyCmds := apexRule.Args["copy_commands"]
2203
2204 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2205 t.Log("Apex was not a test apex!")
2206 t.Fail()
2207 }
2208 // Ensure that main rule creates an output
2209 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2210
2211 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002212 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002213
2214 // Ensure that both direct and indirect deps are copied into apex
2215 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2216
Colin Cross7113d202019-11-20 16:39:12 -08002217 // Ensure that the platform variant ends with _shared
2218 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002219
2220 if android.InAnyApex("mylib_common_test") {
2221 t.Log("Found mylib_common_test in some apex!")
2222 t.Fail()
2223 }
2224}
2225
Alex Light9670d332019-01-29 18:07:33 -08002226func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002227 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002228 apex {
2229 name: "myapex",
2230 key: "myapex.key",
2231 multilib: {
2232 first: {
2233 native_shared_libs: ["mylib_common"],
2234 }
2235 },
2236 target: {
2237 android: {
2238 multilib: {
2239 first: {
2240 native_shared_libs: ["mylib"],
2241 }
2242 }
2243 },
2244 host: {
2245 multilib: {
2246 first: {
2247 native_shared_libs: ["mylib2"],
2248 }
2249 }
2250 }
2251 }
2252 }
2253
2254 apex_key {
2255 name: "myapex.key",
2256 public_key: "testkey.avbpubkey",
2257 private_key: "testkey.pem",
2258 }
2259
2260 cc_library {
2261 name: "mylib",
2262 srcs: ["mylib.cpp"],
2263 system_shared_libs: [],
2264 stl: "none",
2265 }
2266
2267 cc_library {
2268 name: "mylib_common",
2269 srcs: ["mylib.cpp"],
2270 system_shared_libs: [],
2271 stl: "none",
2272 compile_multilib: "first",
2273 }
2274
2275 cc_library {
2276 name: "mylib2",
2277 srcs: ["mylib.cpp"],
2278 system_shared_libs: [],
2279 stl: "none",
2280 compile_multilib: "first",
2281 }
2282 `)
2283
Sundong Ahnabb64432019-10-22 13:58:29 +09002284 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002285 copyCmds := apexRule.Args["copy_commands"]
2286
2287 // Ensure that main rule creates an output
2288 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2289
2290 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002291 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2292 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2293 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002294
2295 // Ensure that both direct and indirect deps are copied into apex
2296 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2297 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2298 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2299
Colin Cross7113d202019-11-20 16:39:12 -08002300 // Ensure that the platform variant ends with _shared
2301 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2302 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2303 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002304}
Jiyong Park04480cf2019-02-06 00:16:29 +09002305
2306func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002307 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002308 apex {
2309 name: "myapex",
2310 key: "myapex.key",
2311 binaries: ["myscript"],
2312 }
2313
2314 apex_key {
2315 name: "myapex.key",
2316 public_key: "testkey.avbpubkey",
2317 private_key: "testkey.pem",
2318 }
2319
2320 sh_binary {
2321 name: "myscript",
2322 src: "mylib.cpp",
2323 filename: "myscript.sh",
2324 sub_dir: "script",
2325 }
2326 `)
2327
Sundong Ahnabb64432019-10-22 13:58:29 +09002328 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002329 copyCmds := apexRule.Args["copy_commands"]
2330
2331 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2332}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002333
Jooyung Han91df2082019-11-20 01:49:42 +09002334func TestApexInVariousPartition(t *testing.T) {
2335 testcases := []struct {
2336 propName, parition, flattenedPartition string
2337 }{
2338 {"", "system", "system_ext"},
2339 {"product_specific: true", "product", "product"},
2340 {"soc_specific: true", "vendor", "vendor"},
2341 {"proprietary: true", "vendor", "vendor"},
2342 {"vendor: true", "vendor", "vendor"},
2343 {"system_ext_specific: true", "system_ext", "system_ext"},
2344 }
2345 for _, tc := range testcases {
2346 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2347 ctx, _ := testApex(t, `
2348 apex {
2349 name: "myapex",
2350 key: "myapex.key",
2351 `+tc.propName+`
2352 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002353
Jooyung Han91df2082019-11-20 01:49:42 +09002354 apex_key {
2355 name: "myapex.key",
2356 public_key: "testkey.avbpubkey",
2357 private_key: "testkey.pem",
2358 }
2359 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002360
Jooyung Han91df2082019-11-20 01:49:42 +09002361 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2362 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2363 actual := apex.installDir.String()
2364 if actual != expected {
2365 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2366 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002367
Jooyung Han91df2082019-11-20 01:49:42 +09002368 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2369 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2370 actual = flattened.installDir.String()
2371 if actual != expected {
2372 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2373 }
2374 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002375 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002376}
Jiyong Park67882562019-03-21 01:11:21 +09002377
Jooyung Han54aca7b2019-11-20 02:26:02 +09002378func TestFileContexts(t *testing.T) {
2379 ctx, _ := testApex(t, `
2380 apex {
2381 name: "myapex",
2382 key: "myapex.key",
2383 }
2384
2385 apex_key {
2386 name: "myapex.key",
2387 public_key: "testkey.avbpubkey",
2388 private_key: "testkey.pem",
2389 }
2390 `)
2391 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2392 apexRule := module.Rule("apexRule")
2393 actual := apexRule.Args["file_contexts"]
2394 expected := "system/sepolicy/apex/myapex-file_contexts"
2395 if actual != expected {
2396 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2397 }
2398
2399 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2400 apex {
2401 name: "myapex",
2402 key: "myapex.key",
2403 file_contexts: "my_own_file_contexts",
2404 }
2405
2406 apex_key {
2407 name: "myapex.key",
2408 public_key: "testkey.avbpubkey",
2409 private_key: "testkey.pem",
2410 }
2411 `, withFiles(map[string][]byte{
2412 "my_own_file_contexts": nil,
2413 }))
2414
2415 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2416 apex {
2417 name: "myapex",
2418 key: "myapex.key",
2419 product_specific: true,
2420 file_contexts: "product_specific_file_contexts",
2421 }
2422
2423 apex_key {
2424 name: "myapex.key",
2425 public_key: "testkey.avbpubkey",
2426 private_key: "testkey.pem",
2427 }
2428 `)
2429
2430 ctx, _ = testApex(t, `
2431 apex {
2432 name: "myapex",
2433 key: "myapex.key",
2434 product_specific: true,
2435 file_contexts: "product_specific_file_contexts",
2436 }
2437
2438 apex_key {
2439 name: "myapex.key",
2440 public_key: "testkey.avbpubkey",
2441 private_key: "testkey.pem",
2442 }
2443 `, withFiles(map[string][]byte{
2444 "product_specific_file_contexts": nil,
2445 }))
2446 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2447 apexRule = module.Rule("apexRule")
2448 actual = apexRule.Args["file_contexts"]
2449 expected = "product_specific_file_contexts"
2450 if actual != expected {
2451 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2452 }
2453
2454 ctx, _ = testApex(t, `
2455 apex {
2456 name: "myapex",
2457 key: "myapex.key",
2458 product_specific: true,
2459 file_contexts: ":my-file-contexts",
2460 }
2461
2462 apex_key {
2463 name: "myapex.key",
2464 public_key: "testkey.avbpubkey",
2465 private_key: "testkey.pem",
2466 }
2467
2468 filegroup {
2469 name: "my-file-contexts",
2470 srcs: ["product_specific_file_contexts"],
2471 }
2472 `, withFiles(map[string][]byte{
2473 "product_specific_file_contexts": nil,
2474 }))
2475 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2476 apexRule = module.Rule("apexRule")
2477 actual = apexRule.Args["file_contexts"]
2478 expected = "product_specific_file_contexts"
2479 if actual != expected {
2480 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2481 }
2482}
2483
Jiyong Park67882562019-03-21 01:11:21 +09002484func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002485 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002486 apex_key {
2487 name: "myapex.key",
2488 public_key: ":my.avbpubkey",
2489 private_key: ":my.pem",
2490 product_specific: true,
2491 }
2492
2493 filegroup {
2494 name: "my.avbpubkey",
2495 srcs: ["testkey2.avbpubkey"],
2496 }
2497
2498 filegroup {
2499 name: "my.pem",
2500 srcs: ["testkey2.pem"],
2501 }
2502 `)
2503
2504 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2505 expected_pubkey := "testkey2.avbpubkey"
2506 actual_pubkey := apex_key.public_key_file.String()
2507 if actual_pubkey != expected_pubkey {
2508 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2509 }
2510 expected_privkey := "testkey2.pem"
2511 actual_privkey := apex_key.private_key_file.String()
2512 if actual_privkey != expected_privkey {
2513 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2514 }
2515}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002516
2517func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002518 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002519 prebuilt_apex {
2520 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002521 arch: {
2522 arm64: {
2523 src: "myapex-arm64.apex",
2524 },
2525 arm: {
2526 src: "myapex-arm.apex",
2527 },
2528 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002529 }
2530 `)
2531
2532 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2533
Jiyong Parkc95714e2019-03-29 14:23:10 +09002534 expectedInput := "myapex-arm64.apex"
2535 if prebuilt.inputApex.String() != expectedInput {
2536 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2537 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002538}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002539
2540func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002541 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002542 prebuilt_apex {
2543 name: "myapex",
2544 src: "myapex-arm.apex",
2545 filename: "notmyapex.apex",
2546 }
2547 `)
2548
2549 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2550
2551 expected := "notmyapex.apex"
2552 if p.installFilename != expected {
2553 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2554 }
2555}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002556
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002557func TestPrebuiltOverrides(t *testing.T) {
2558 ctx, config := testApex(t, `
2559 prebuilt_apex {
2560 name: "myapex.prebuilt",
2561 src: "myapex-arm.apex",
2562 overrides: [
2563 "myapex",
2564 ],
2565 }
2566 `)
2567
2568 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2569
2570 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002571 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002572 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002573 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002574 }
2575}
2576
Roland Levillain630846d2019-06-26 12:48:34 +01002577func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002578 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002579 apex_test {
2580 name: "myapex",
2581 key: "myapex.key",
2582 tests: [
2583 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002584 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002585 ],
2586 }
2587
2588 apex_key {
2589 name: "myapex.key",
2590 public_key: "testkey.avbpubkey",
2591 private_key: "testkey.pem",
2592 }
2593
2594 cc_test {
2595 name: "mytest",
2596 gtest: false,
2597 srcs: ["mytest.cpp"],
2598 relative_install_path: "test",
2599 system_shared_libs: [],
2600 static_executable: true,
2601 stl: "none",
2602 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002603
2604 cc_test {
2605 name: "mytests",
2606 gtest: false,
2607 srcs: [
2608 "mytest1.cpp",
2609 "mytest2.cpp",
2610 "mytest3.cpp",
2611 ],
2612 test_per_src: true,
2613 relative_install_path: "test",
2614 system_shared_libs: [],
2615 static_executable: true,
2616 stl: "none",
2617 }
Roland Levillain630846d2019-06-26 12:48:34 +01002618 `)
2619
Sundong Ahnabb64432019-10-22 13:58:29 +09002620 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002621 copyCmds := apexRule.Args["copy_commands"]
2622
2623 // Ensure that test dep is copied into apex.
2624 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002625
2626 // Ensure that test deps built with `test_per_src` are copied into apex.
2627 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2628 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2629 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002630
2631 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002632 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002633 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2634 name := apexBundle.BaseModuleName()
2635 prefix := "TARGET_"
2636 var builder strings.Builder
2637 data.Custom(&builder, name, prefix, "", data)
2638 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002639 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2640 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2641 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2642 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002643 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002644 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002645 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002646}
2647
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002648func TestInstallExtraFlattenedApexes(t *testing.T) {
2649 ctx, config := testApex(t, `
2650 apex {
2651 name: "myapex",
2652 key: "myapex.key",
2653 }
2654 apex_key {
2655 name: "myapex.key",
2656 public_key: "testkey.avbpubkey",
2657 private_key: "testkey.pem",
2658 }
2659 `, func(fs map[string][]byte, config android.Config) {
2660 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2661 })
2662 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2663 ensureListContains(t, ab.externalDeps, "myapex.flattened")
2664 mk := android.AndroidMkDataForTest(t, config, "", ab)
2665 var builder strings.Builder
2666 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2667 androidMk := builder.String()
2668 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2669}
2670
Jooyung Han5c998b92019-06-27 11:30:33 +09002671func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002672 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002673 apex {
2674 name: "myapex",
2675 key: "myapex.key",
2676 native_shared_libs: ["mylib"],
2677 uses: ["commonapex"],
2678 }
2679
2680 apex {
2681 name: "commonapex",
2682 key: "myapex.key",
2683 native_shared_libs: ["libcommon"],
2684 provide_cpp_shared_libs: true,
2685 }
2686
2687 apex_key {
2688 name: "myapex.key",
2689 public_key: "testkey.avbpubkey",
2690 private_key: "testkey.pem",
2691 }
2692
2693 cc_library {
2694 name: "mylib",
2695 srcs: ["mylib.cpp"],
2696 shared_libs: ["libcommon"],
2697 system_shared_libs: [],
2698 stl: "none",
2699 }
2700
2701 cc_library {
2702 name: "libcommon",
2703 srcs: ["mylib_common.cpp"],
2704 system_shared_libs: [],
2705 stl: "none",
2706 }
2707 `)
2708
Sundong Ahnabb64432019-10-22 13:58:29 +09002709 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002710 apexRule1 := module1.Rule("apexRule")
2711 copyCmds1 := apexRule1.Args["copy_commands"]
2712
Sundong Ahnabb64432019-10-22 13:58:29 +09002713 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002714 apexRule2 := module2.Rule("apexRule")
2715 copyCmds2 := apexRule2.Args["copy_commands"]
2716
Colin Cross7113d202019-11-20 16:39:12 -08002717 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2718 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002719 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2720 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2721 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2722}
2723
2724func TestApexUsesFailsIfNotProvided(t *testing.T) {
2725 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2726 apex {
2727 name: "myapex",
2728 key: "myapex.key",
2729 uses: ["commonapex"],
2730 }
2731
2732 apex {
2733 name: "commonapex",
2734 key: "myapex.key",
2735 }
2736
2737 apex_key {
2738 name: "myapex.key",
2739 public_key: "testkey.avbpubkey",
2740 private_key: "testkey.pem",
2741 }
2742 `)
2743 testApexError(t, `uses: "commonapex" is not a provider`, `
2744 apex {
2745 name: "myapex",
2746 key: "myapex.key",
2747 uses: ["commonapex"],
2748 }
2749
2750 cc_library {
2751 name: "commonapex",
2752 system_shared_libs: [],
2753 stl: "none",
2754 }
2755
2756 apex_key {
2757 name: "myapex.key",
2758 public_key: "testkey.avbpubkey",
2759 private_key: "testkey.pem",
2760 }
2761 `)
2762}
2763
2764func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2765 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2766 apex {
2767 name: "myapex",
2768 key: "myapex.key",
2769 use_vendor: true,
2770 uses: ["commonapex"],
2771 }
2772
2773 apex {
2774 name: "commonapex",
2775 key: "myapex.key",
2776 provide_cpp_shared_libs: true,
2777 }
2778
2779 apex_key {
2780 name: "myapex.key",
2781 public_key: "testkey.avbpubkey",
2782 private_key: "testkey.pem",
2783 }
Jooyung Handc782442019-11-01 03:14:38 +09002784 `, func(fs map[string][]byte, config android.Config) {
2785 setUseVendorWhitelistForTest(config, []string{"myapex"})
2786 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002787}
2788
Jooyung Hand48f3c32019-08-23 11:18:57 +09002789func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2790 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2791 apex {
2792 name: "myapex",
2793 key: "myapex.key",
2794 native_shared_libs: ["libfoo"],
2795 }
2796
2797 apex_key {
2798 name: "myapex.key",
2799 public_key: "testkey.avbpubkey",
2800 private_key: "testkey.pem",
2801 }
2802
2803 cc_library {
2804 name: "libfoo",
2805 stl: "none",
2806 system_shared_libs: [],
2807 enabled: false,
2808 }
2809 `)
2810 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2811 apex {
2812 name: "myapex",
2813 key: "myapex.key",
2814 java_libs: ["myjar"],
2815 }
2816
2817 apex_key {
2818 name: "myapex.key",
2819 public_key: "testkey.avbpubkey",
2820 private_key: "testkey.pem",
2821 }
2822
2823 java_library {
2824 name: "myjar",
2825 srcs: ["foo/bar/MyClass.java"],
2826 sdk_version: "none",
2827 system_modules: "none",
2828 compile_dex: true,
2829 enabled: false,
2830 }
2831 `)
2832}
2833
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002834func TestApexWithApps(t *testing.T) {
2835 ctx, _ := testApex(t, `
2836 apex {
2837 name: "myapex",
2838 key: "myapex.key",
2839 apps: [
2840 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002841 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002842 ],
2843 }
2844
2845 apex_key {
2846 name: "myapex.key",
2847 public_key: "testkey.avbpubkey",
2848 private_key: "testkey.pem",
2849 }
2850
2851 android_app {
2852 name: "AppFoo",
2853 srcs: ["foo/bar/MyClass.java"],
2854 sdk_version: "none",
2855 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002856 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002857 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002858
2859 android_app {
2860 name: "AppFooPriv",
2861 srcs: ["foo/bar/MyClass.java"],
2862 sdk_version: "none",
2863 system_modules: "none",
2864 privileged: true,
2865 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002866
2867 cc_library_shared {
2868 name: "libjni",
2869 srcs: ["mylib.cpp"],
2870 stl: "none",
2871 system_shared_libs: [],
2872 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002873 `)
2874
Sundong Ahnabb64432019-10-22 13:58:29 +09002875 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002876 apexRule := module.Rule("apexRule")
2877 copyCmds := apexRule.Args["copy_commands"]
2878
2879 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002880 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002881
2882 // JNI libraries are embedded inside APK
2883 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
Colin Cross7113d202019-11-20 16:39:12 -08002884 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002885 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2886 // ... uncompressed
2887 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2888 t.Errorf("jni lib is not uncompressed for AppFoo")
2889 }
2890 // ... and not directly inside the APEX
2891 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002892}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002893
Dario Frenicde2a032019-10-27 00:29:22 +01002894func TestApexWithAppImports(t *testing.T) {
2895 ctx, _ := testApex(t, `
2896 apex {
2897 name: "myapex",
2898 key: "myapex.key",
2899 apps: [
2900 "AppFooPrebuilt",
2901 "AppFooPrivPrebuilt",
2902 ],
2903 }
2904
2905 apex_key {
2906 name: "myapex.key",
2907 public_key: "testkey.avbpubkey",
2908 private_key: "testkey.pem",
2909 }
2910
2911 android_app_import {
2912 name: "AppFooPrebuilt",
2913 apk: "PrebuiltAppFoo.apk",
2914 presigned: true,
2915 dex_preopt: {
2916 enabled: false,
2917 },
2918 }
2919
2920 android_app_import {
2921 name: "AppFooPrivPrebuilt",
2922 apk: "PrebuiltAppFooPriv.apk",
2923 privileged: true,
2924 presigned: true,
2925 dex_preopt: {
2926 enabled: false,
2927 },
2928 }
2929 `)
2930
Sundong Ahnabb64432019-10-22 13:58:29 +09002931 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002932 apexRule := module.Rule("apexRule")
2933 copyCmds := apexRule.Args["copy_commands"]
2934
2935 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2936 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002937}
2938
Jooyung Han18020ea2019-11-13 10:50:48 +09002939func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2940 // libfoo's apex_available comes from cc_defaults
2941 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2942 apex {
2943 name: "myapex",
2944 key: "myapex.key",
2945 native_shared_libs: ["libfoo"],
2946 }
2947
2948 apex_key {
2949 name: "myapex.key",
2950 public_key: "testkey.avbpubkey",
2951 private_key: "testkey.pem",
2952 }
2953
2954 apex {
2955 name: "otherapex",
2956 key: "myapex.key",
2957 native_shared_libs: ["libfoo"],
2958 }
2959
2960 cc_defaults {
2961 name: "libfoo-defaults",
2962 apex_available: ["otherapex"],
2963 }
2964
2965 cc_library {
2966 name: "libfoo",
2967 defaults: ["libfoo-defaults"],
2968 stl: "none",
2969 system_shared_libs: [],
2970 }`)
2971}
2972
Jiyong Park127b40b2019-09-30 16:04:35 +09002973func TestApexAvailable(t *testing.T) {
2974 // libfoo is not available to myapex, but only to otherapex
2975 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2976 apex {
2977 name: "myapex",
2978 key: "myapex.key",
2979 native_shared_libs: ["libfoo"],
2980 }
2981
2982 apex_key {
2983 name: "myapex.key",
2984 public_key: "testkey.avbpubkey",
2985 private_key: "testkey.pem",
2986 }
2987
2988 apex {
2989 name: "otherapex",
2990 key: "otherapex.key",
2991 native_shared_libs: ["libfoo"],
2992 }
2993
2994 apex_key {
2995 name: "otherapex.key",
2996 public_key: "testkey.avbpubkey",
2997 private_key: "testkey.pem",
2998 }
2999
3000 cc_library {
3001 name: "libfoo",
3002 stl: "none",
3003 system_shared_libs: [],
3004 apex_available: ["otherapex"],
3005 }`)
3006
3007 // libbar is an indirect dep
3008 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3009 apex {
3010 name: "myapex",
3011 key: "myapex.key",
3012 native_shared_libs: ["libfoo"],
3013 }
3014
3015 apex_key {
3016 name: "myapex.key",
3017 public_key: "testkey.avbpubkey",
3018 private_key: "testkey.pem",
3019 }
3020
3021 apex {
3022 name: "otherapex",
3023 key: "otherapex.key",
3024 native_shared_libs: ["libfoo"],
3025 }
3026
3027 apex_key {
3028 name: "otherapex.key",
3029 public_key: "testkey.avbpubkey",
3030 private_key: "testkey.pem",
3031 }
3032
3033 cc_library {
3034 name: "libfoo",
3035 stl: "none",
3036 shared_libs: ["libbar"],
3037 system_shared_libs: [],
3038 apex_available: ["myapex", "otherapex"],
3039 }
3040
3041 cc_library {
3042 name: "libbar",
3043 stl: "none",
3044 system_shared_libs: [],
3045 apex_available: ["otherapex"],
3046 }`)
3047
3048 testApexError(t, "\"otherapex\" is not a valid module name", `
3049 apex {
3050 name: "myapex",
3051 key: "myapex.key",
3052 native_shared_libs: ["libfoo"],
3053 }
3054
3055 apex_key {
3056 name: "myapex.key",
3057 public_key: "testkey.avbpubkey",
3058 private_key: "testkey.pem",
3059 }
3060
3061 cc_library {
3062 name: "libfoo",
3063 stl: "none",
3064 system_shared_libs: [],
3065 apex_available: ["otherapex"],
3066 }`)
3067
3068 ctx, _ := testApex(t, `
3069 apex {
3070 name: "myapex",
3071 key: "myapex.key",
3072 native_shared_libs: ["libfoo", "libbar"],
3073 }
3074
3075 apex_key {
3076 name: "myapex.key",
3077 public_key: "testkey.avbpubkey",
3078 private_key: "testkey.pem",
3079 }
3080
3081 cc_library {
3082 name: "libfoo",
3083 stl: "none",
3084 system_shared_libs: [],
3085 apex_available: ["myapex"],
3086 }
3087
3088 cc_library {
3089 name: "libbar",
3090 stl: "none",
3091 system_shared_libs: [],
3092 apex_available: ["//apex_available:anyapex"],
3093 }`)
3094
3095 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003096 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3097 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3098 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3099 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003100
3101 ctx, _ = testApex(t, `
3102 apex {
3103 name: "myapex",
3104 key: "myapex.key",
3105 }
3106
3107 apex_key {
3108 name: "myapex.key",
3109 public_key: "testkey.avbpubkey",
3110 private_key: "testkey.pem",
3111 }
3112
3113 cc_library {
3114 name: "libfoo",
3115 stl: "none",
3116 system_shared_libs: [],
3117 apex_available: ["//apex_available:platform"],
3118 }`)
3119
3120 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003121 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3122 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003123
3124 ctx, _ = testApex(t, `
3125 apex {
3126 name: "myapex",
3127 key: "myapex.key",
3128 native_shared_libs: ["libfoo"],
3129 }
3130
3131 apex_key {
3132 name: "myapex.key",
3133 public_key: "testkey.avbpubkey",
3134 private_key: "testkey.pem",
3135 }
3136
3137 cc_library {
3138 name: "libfoo",
3139 stl: "none",
3140 system_shared_libs: [],
3141 apex_available: ["myapex"],
3142 static: {
3143 apex_available: ["//apex_available:platform"],
3144 },
3145 }`)
3146
3147 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08003148 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3149 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003150 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08003151 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3152 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003153}
3154
Jiyong Park5d790c32019-11-15 18:40:32 +09003155func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003156 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003157 apex {
3158 name: "myapex",
3159 key: "myapex.key",
3160 apps: ["app"],
3161 }
3162
3163 override_apex {
3164 name: "override_myapex",
3165 base: "myapex",
3166 apps: ["override_app"],
3167 }
3168
3169 apex_key {
3170 name: "myapex.key",
3171 public_key: "testkey.avbpubkey",
3172 private_key: "testkey.pem",
3173 }
3174
3175 android_app {
3176 name: "app",
3177 srcs: ["foo/bar/MyClass.java"],
3178 package_name: "foo",
3179 sdk_version: "none",
3180 system_modules: "none",
3181 }
3182
3183 override_android_app {
3184 name: "override_app",
3185 base: "app",
3186 package_name: "bar",
3187 }
3188 `)
3189
Jiyong Park317645e2019-12-05 13:20:58 +09003190 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3191 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3192 if originalVariant.GetOverriddenBy() != "" {
3193 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3194 }
3195 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3196 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3197 }
3198
Jiyong Park5d790c32019-11-15 18:40:32 +09003199 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3200 apexRule := module.Rule("apexRule")
3201 copyCmds := apexRule.Args["copy_commands"]
3202
3203 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3204 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003205
3206 apexBundle := module.Module().(*apexBundle)
3207 name := apexBundle.Name()
3208 if name != "override_myapex" {
3209 t.Errorf("name should be \"override_myapex\", but was %q", name)
3210 }
3211
3212 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3213 var builder strings.Builder
3214 data.Custom(&builder, name, "TARGET_", "", data)
3215 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003216 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003217 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3218 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
3219 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003220 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003221 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3222 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003223}
3224
Jooyung Han214bf372019-11-12 13:03:50 +09003225func TestLegacyAndroid10Support(t *testing.T) {
3226 ctx, _ := testApex(t, `
3227 apex {
3228 name: "myapex",
3229 key: "myapex.key",
3230 legacy_android10_support: true,
3231 }
3232
3233 apex_key {
3234 name: "myapex.key",
3235 public_key: "testkey.avbpubkey",
3236 private_key: "testkey.pem",
3237 }
3238 `)
3239
3240 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3241 args := module.Rule("apexRule").Args
3242 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
3243}
3244
Jiyong Park479321d2019-12-16 11:47:12 +09003245func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3246 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3247 apex {
3248 name: "myapex",
3249 key: "myapex.key",
3250 java_libs: ["myjar"],
3251 }
3252
3253 apex_key {
3254 name: "myapex.key",
3255 public_key: "testkey.avbpubkey",
3256 private_key: "testkey.pem",
3257 }
3258
3259 java_library {
3260 name: "myjar",
3261 srcs: ["foo/bar/MyClass.java"],
3262 sdk_version: "none",
3263 system_modules: "none",
3264 }
3265 `)
3266}
3267
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003268func TestMain(m *testing.M) {
3269 run := func() int {
3270 setUp()
3271 defer tearDown()
3272
3273 return m.Run()
3274 }
3275
3276 os.Exit(run())
3277}