blob: 8c5ca8b6d9eb7d377fe5eb2f4fc789f63d75c22e [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
Jiyong Park7cd10e32020-01-14 09:22:18 +090094func withUnbundledBuild(fs map[string][]byte, config android.Config) {
95 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
96}
97
Jooyung Han344d5432019-08-23 11:17:39 +090098func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +090099 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900100
101 bp = bp + `
Jooyung Han54aca7b2019-11-20 02:26:02 +0900102 filegroup {
103 name: "myapex-file_contexts",
104 srcs: [
105 "system/sepolicy/apex/myapex-file_contexts",
106 ],
107 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900108 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800109
Colin Crossf9aabd72020-02-15 11:29:50 -0800110 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
111
Dario Frenicde2a032019-10-27 00:29:22 +0100112 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900113
Jooyung Han344d5432019-08-23 11:17:39 +0900114 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900115 "a.java": nil,
116 "PrebuiltAppFoo.apk": nil,
117 "PrebuiltAppFooPriv.apk": nil,
118 "build/make/target/product/security": nil,
119 "apex_manifest.json": nil,
120 "AndroidManifest.xml": nil,
121 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park9d677202020-02-19 16:29:35 +0900122 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900123 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900124 "system/sepolicy/apex/otherapex-file_contexts": nil,
125 "system/sepolicy/apex/commonapex-file_contexts": nil,
126 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800127 "mylib.cpp": nil,
128 "mylib_common.cpp": nil,
129 "mytest.cpp": nil,
130 "mytest1.cpp": nil,
131 "mytest2.cpp": nil,
132 "mytest3.cpp": nil,
133 "myprebuilt": nil,
134 "my_include": nil,
135 "foo/bar/MyClass.java": nil,
136 "prebuilt.jar": nil,
137 "vendor/foo/devkeys/test.x509.pem": nil,
138 "vendor/foo/devkeys/test.pk8": nil,
139 "testkey.x509.pem": nil,
140 "testkey.pk8": nil,
141 "testkey.override.x509.pem": nil,
142 "testkey.override.pk8": nil,
143 "vendor/foo/devkeys/testkey.avbpubkey": nil,
144 "vendor/foo/devkeys/testkey.pem": nil,
145 "NOTICE": nil,
146 "custom_notice": nil,
147 "testkey2.avbpubkey": nil,
148 "testkey2.pem": nil,
149 "myapex-arm64.apex": nil,
150 "myapex-arm.apex": nil,
151 "frameworks/base/api/current.txt": nil,
152 "framework/aidl/a.aidl": nil,
153 "build/make/core/proguard.flags": nil,
154 "build/make/core/proguard_basic_keeps.flags": nil,
155 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900156 }
157
Colin Crossf9aabd72020-02-15 11:29:50 -0800158 cc.GatherRequiredFilesForTest(fs)
159
Jooyung Han344d5432019-08-23 11:17:39 +0900160 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800161 // The fs now needs to be populated before creating the config, call handlers twice
162 // for now, once to get any fs changes, and later after the config was created to
163 // set product variables or targets.
164 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
165 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900166 }
167
Colin Cross98be1bb2019-12-13 20:41:13 -0800168 config := android.TestArchConfig(buildDir, nil, bp, fs)
169 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
170 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
171 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
172 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
173 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
174 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
175
176 for _, handler := range handlers {
177 // The fs now needs to be populated before creating the config, call handlers twice
178 // for now, earlier to get any fs changes, and now after the config was created to
179 // set product variables or targets.
180 tempFS := map[string][]byte{}
181 handler(tempFS, config)
182 }
183
184 ctx := android.NewTestArchContext()
185 ctx.RegisterModuleType("apex", BundleFactory)
186 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
187 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
188 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
189 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
190 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
191 ctx.RegisterModuleType("override_apex", overrideApexFactory)
192
Jooyung Hana57af4a2020-01-23 05:36:59 +0000193 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
194 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
195
Paul Duffin77980a82019-12-19 16:01:36 +0000196 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800197 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800198 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
199 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800200 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000201 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800202 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800203 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000204 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000205 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000206 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900207 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800208
Colin Cross98be1bb2019-12-13 20:41:13 -0800209 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800210 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800211
212 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900213
Jooyung Han5c998b92019-06-27 11:30:33 +0900214 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900215}
216
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700217func setUp() {
218 var err error
219 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900220 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700221 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900222 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900223}
224
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700225func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900226 os.RemoveAll(buildDir)
227}
228
Jooyung Han643adc42020-02-27 13:50:06 +0900229// ensure that 'result' equals 'expected'
230func ensureEquals(t *testing.T, result string, expected string) {
231 t.Helper()
232 if result != expected {
233 t.Errorf("%q != %q", expected, result)
234 }
235}
236
Jiyong Park25fc6a92018-11-18 18:02:45 +0900237// ensure that 'result' contains 'expected'
238func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900239 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900240 if !strings.Contains(result, expected) {
241 t.Errorf("%q is not found in %q", expected, result)
242 }
243}
244
245// ensures that 'result' does not contain 'notExpected'
246func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900247 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900248 if strings.Contains(result, notExpected) {
249 t.Errorf("%q is found in %q", notExpected, result)
250 }
251}
252
253func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900254 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900255 if !android.InList(expected, result) {
256 t.Errorf("%q is not found in %v", expected, result)
257 }
258}
259
260func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900261 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900262 if android.InList(notExpected, result) {
263 t.Errorf("%q is found in %v", notExpected, result)
264 }
265}
266
Jooyung Hane1633032019-08-01 17:41:43 +0900267func ensureListEmpty(t *testing.T, result []string) {
268 t.Helper()
269 if len(result) > 0 {
270 t.Errorf("%q is expected to be empty", result)
271 }
272}
273
Jiyong Park25fc6a92018-11-18 18:02:45 +0900274// Minimal test
275func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700276 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900277 apex_defaults {
278 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900279 manifest: ":myapex.manifest",
280 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900281 key: "myapex.key",
282 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800283 multilib: {
284 both: {
285 binaries: ["foo",],
286 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900287 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900288 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900289 }
290
Jiyong Park30ca9372019-02-07 16:27:23 +0900291 apex {
292 name: "myapex",
293 defaults: ["myapex-defaults"],
294 }
295
Jiyong Park25fc6a92018-11-18 18:02:45 +0900296 apex_key {
297 name: "myapex.key",
298 public_key: "testkey.avbpubkey",
299 private_key: "testkey.pem",
300 }
301
Jiyong Park809bb722019-02-13 21:33:49 +0900302 filegroup {
303 name: "myapex.manifest",
304 srcs: ["apex_manifest.json"],
305 }
306
307 filegroup {
308 name: "myapex.androidmanifest",
309 srcs: ["AndroidManifest.xml"],
310 }
311
Jiyong Park25fc6a92018-11-18 18:02:45 +0900312 cc_library {
313 name: "mylib",
314 srcs: ["mylib.cpp"],
315 shared_libs: ["mylib2"],
316 system_shared_libs: [],
317 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000318 // TODO: remove //apex_available:platform
319 apex_available: [
320 "//apex_available:platform",
321 "myapex",
322 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323 }
324
Alex Light3d673592019-01-18 14:37:31 -0800325 cc_binary {
326 name: "foo",
327 srcs: ["mylib.cpp"],
328 compile_multilib: "both",
329 multilib: {
330 lib32: {
331 suffix: "32",
332 },
333 lib64: {
334 suffix: "64",
335 },
336 },
337 symlinks: ["foo_link_"],
338 symlink_preferred_arch: true,
339 system_shared_libs: [],
340 static_executable: true,
341 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000342 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800343 }
344
Jiyong Park25fc6a92018-11-18 18:02:45 +0900345 cc_library {
346 name: "mylib2",
347 srcs: ["mylib.cpp"],
348 system_shared_libs: [],
349 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900350 notice: "custom_notice",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000351 // TODO: remove //apex_available:platform
352 apex_available: [
353 "//apex_available:platform",
354 "myapex",
355 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900356 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900357
358 java_library {
359 name: "myjar",
360 srcs: ["foo/bar/MyClass.java"],
361 sdk_version: "none",
362 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900363 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900364 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000365 // TODO: remove //apex_available:platform
366 apex_available: [
367 "//apex_available:platform",
368 "myapex",
369 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900370 }
371
372 java_library {
373 name: "myotherjar",
374 srcs: ["foo/bar/MyClass.java"],
375 sdk_version: "none",
376 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900377 // TODO: remove //apex_available:platform
378 apex_available: [
379 "//apex_available:platform",
380 "myapex",
381 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900382 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900383
384 java_library {
385 name: "mysharedjar",
386 srcs: ["foo/bar/MyClass.java"],
387 sdk_version: "none",
388 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900389 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900390 `)
391
Sundong Ahnabb64432019-10-22 13:58:29 +0900392 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900393
394 optFlags := apexRule.Args["opt_flags"]
395 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700396 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900397 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900398
Jiyong Park25fc6a92018-11-18 18:02:45 +0900399 copyCmds := apexRule.Args["copy_commands"]
400
401 // Ensure that main rule creates an output
402 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
403
404 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800405 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900406 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900407
408 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800409 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900410 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900411
412 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800413 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
414 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900415 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
416 // .. but not for java libs
417 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900418 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800419
Colin Cross7113d202019-11-20 16:39:12 -0800420 // Ensure that the platform variant ends with _shared or _common
421 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
422 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900423 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
424 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900425 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
426
427 // Ensure that dynamic dependency to java libs are not included
428 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800429
430 // Ensure that all symlinks are present.
431 found_foo_link_64 := false
432 found_foo := false
433 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900434 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800435 if strings.HasSuffix(cmd, "bin/foo") {
436 found_foo = true
437 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
438 found_foo_link_64 = true
439 }
440 }
441 }
442 good := found_foo && found_foo_link_64
443 if !good {
444 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
445 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900446
Sundong Ahnabb64432019-10-22 13:58:29 +0900447 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700448 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700449 if len(noticeInputs) != 2 {
450 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900451 }
452 ensureListContains(t, noticeInputs, "NOTICE")
453 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900454
455 depsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("myapex-deps-info.txt").Args["content"], "\\n")
Jiyong Park678c8812020-02-07 17:25:49 +0900456 ensureListContains(t, depsInfo, "myjar <- myapex")
457 ensureListContains(t, depsInfo, "mylib <- myapex")
458 ensureListContains(t, depsInfo, "mylib2 <- mylib")
459 ensureListContains(t, depsInfo, "myotherjar <- myjar")
460 ensureListContains(t, depsInfo, "mysharedjar (external) <- myjar")
Alex Light5098a612018-11-29 17:12:15 -0800461}
462
Jooyung Hanf21c7972019-12-16 22:32:06 +0900463func TestDefaults(t *testing.T) {
464 ctx, _ := testApex(t, `
465 apex_defaults {
466 name: "myapex-defaults",
467 key: "myapex.key",
468 prebuilts: ["myetc"],
469 native_shared_libs: ["mylib"],
470 java_libs: ["myjar"],
471 apps: ["AppFoo"],
472 }
473
474 prebuilt_etc {
475 name: "myetc",
476 src: "myprebuilt",
477 }
478
479 apex {
480 name: "myapex",
481 defaults: ["myapex-defaults"],
482 }
483
484 apex_key {
485 name: "myapex.key",
486 public_key: "testkey.avbpubkey",
487 private_key: "testkey.pem",
488 }
489
490 cc_library {
491 name: "mylib",
492 system_shared_libs: [],
493 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000494 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900495 }
496
497 java_library {
498 name: "myjar",
499 srcs: ["foo/bar/MyClass.java"],
500 sdk_version: "none",
501 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000502 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900503 }
504
505 android_app {
506 name: "AppFoo",
507 srcs: ["foo/bar/MyClass.java"],
508 sdk_version: "none",
509 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000510 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900511 }
512 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000513 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900514 "etc/myetc",
515 "javalib/myjar.jar",
516 "lib64/mylib.so",
517 "app/AppFoo/AppFoo.apk",
518 })
519}
520
Jooyung Han01a3ee22019-11-02 02:52:25 +0900521func TestApexManifest(t *testing.T) {
522 ctx, _ := testApex(t, `
523 apex {
524 name: "myapex",
525 key: "myapex.key",
526 }
527
528 apex_key {
529 name: "myapex.key",
530 public_key: "testkey.avbpubkey",
531 private_key: "testkey.pem",
532 }
533 `)
534
535 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900536 args := module.Rule("apexRule").Args
537 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
538 t.Error("manifest should be apex_manifest.pb, but " + manifest)
539 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900540}
541
Alex Light5098a612018-11-29 17:12:15 -0800542func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700543 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800544 apex {
545 name: "myapex",
546 key: "myapex.key",
547 payload_type: "zip",
548 native_shared_libs: ["mylib"],
549 }
550
551 apex_key {
552 name: "myapex.key",
553 public_key: "testkey.avbpubkey",
554 private_key: "testkey.pem",
555 }
556
557 cc_library {
558 name: "mylib",
559 srcs: ["mylib.cpp"],
560 shared_libs: ["mylib2"],
561 system_shared_libs: [],
562 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000563 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800564 }
565
566 cc_library {
567 name: "mylib2",
568 srcs: ["mylib.cpp"],
569 system_shared_libs: [],
570 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000571 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800572 }
573 `)
574
Sundong Ahnabb64432019-10-22 13:58:29 +0900575 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800576 copyCmds := zipApexRule.Args["copy_commands"]
577
578 // Ensure that main rule creates an output
579 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
580
581 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800582 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800583
584 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800585 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800586
587 // Ensure that both direct and indirect deps are copied into apex
588 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
589 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900590}
591
592func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700593 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900594 apex {
595 name: "myapex",
596 key: "myapex.key",
597 native_shared_libs: ["mylib", "mylib3"],
598 }
599
600 apex_key {
601 name: "myapex.key",
602 public_key: "testkey.avbpubkey",
603 private_key: "testkey.pem",
604 }
605
606 cc_library {
607 name: "mylib",
608 srcs: ["mylib.cpp"],
609 shared_libs: ["mylib2", "mylib3"],
610 system_shared_libs: [],
611 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000612 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900613 }
614
615 cc_library {
616 name: "mylib2",
617 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900618 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900619 system_shared_libs: [],
620 stl: "none",
621 stubs: {
622 versions: ["1", "2", "3"],
623 },
624 }
625
626 cc_library {
627 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900628 srcs: ["mylib.cpp"],
629 shared_libs: ["mylib4"],
630 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900631 stl: "none",
632 stubs: {
633 versions: ["10", "11", "12"],
634 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000635 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900636 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900637
638 cc_library {
639 name: "mylib4",
640 srcs: ["mylib.cpp"],
641 system_shared_libs: [],
642 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000643 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900644 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900645 `)
646
Sundong Ahnabb64432019-10-22 13:58:29 +0900647 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900648 copyCmds := apexRule.Args["copy_commands"]
649
650 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800651 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900652
653 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800654 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900655
656 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800657 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900658
Colin Cross7113d202019-11-20 16:39:12 -0800659 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900660
661 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900662 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900663 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900664 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900665
666 // 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 -0800667 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900668 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800669 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900670
671 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900672 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900673 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900674
675 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900676 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900677
Jooyung Hana57af4a2020-01-23 05:36:59 +0000678 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900679 "lib64/mylib.so",
680 "lib64/mylib3.so",
681 "lib64/mylib4.so",
682 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900683}
684
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900685func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700686 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900687 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900688 name: "myapex2",
689 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900690 native_shared_libs: ["mylib"],
691 }
692
693 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900694 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900695 public_key: "testkey.avbpubkey",
696 private_key: "testkey.pem",
697 }
698
699 cc_library {
700 name: "mylib",
701 srcs: ["mylib.cpp"],
702 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900703 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900704 system_shared_libs: [],
705 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000706 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900707 }
708
709 cc_library {
710 name: "libfoo",
711 srcs: ["mylib.cpp"],
712 shared_libs: ["libbar"],
713 system_shared_libs: [],
714 stl: "none",
715 stubs: {
716 versions: ["10", "20", "30"],
717 },
718 }
719
720 cc_library {
721 name: "libbar",
722 srcs: ["mylib.cpp"],
723 system_shared_libs: [],
724 stl: "none",
725 }
726
Jiyong Park678c8812020-02-07 17:25:49 +0900727 cc_library_static {
728 name: "libbaz",
729 srcs: ["mylib.cpp"],
730 system_shared_libs: [],
731 stl: "none",
732 apex_available: [ "myapex2" ],
733 }
734
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900735 `)
736
Jiyong Park83dc74b2020-01-14 18:38:44 +0900737 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900738 copyCmds := apexRule.Args["copy_commands"]
739
740 // Ensure that direct non-stubs dep is always included
741 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
742
743 // Ensure that indirect stubs dep is not included
744 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
745
746 // Ensure that dependency of stubs is not included
747 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
748
Jiyong Park83dc74b2020-01-14 18:38:44 +0900749 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900750
751 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900752 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900753 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900754 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900755
Jiyong Park3ff16992019-12-27 14:11:47 +0900756 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900757
758 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
759 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900760
761 depsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("myapex2-deps-info.txt").Args["content"], "\\n")
Jiyong Park678c8812020-02-07 17:25:49 +0900762
763 ensureListContains(t, depsInfo, "mylib <- myapex2")
764 ensureListContains(t, depsInfo, "libbaz <- mylib")
765 ensureListContains(t, depsInfo, "libfoo (external) <- mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900766}
767
Jooyung Hand3639552019-08-09 12:57:43 +0900768func TestApexWithRuntimeLibsDependency(t *testing.T) {
769 /*
770 myapex
771 |
772 v (runtime_libs)
773 mylib ------+------> libfoo [provides stub]
774 |
775 `------> libbar
776 */
777 ctx, _ := testApex(t, `
778 apex {
779 name: "myapex",
780 key: "myapex.key",
781 native_shared_libs: ["mylib"],
782 }
783
784 apex_key {
785 name: "myapex.key",
786 public_key: "testkey.avbpubkey",
787 private_key: "testkey.pem",
788 }
789
790 cc_library {
791 name: "mylib",
792 srcs: ["mylib.cpp"],
793 runtime_libs: ["libfoo", "libbar"],
794 system_shared_libs: [],
795 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000796 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900797 }
798
799 cc_library {
800 name: "libfoo",
801 srcs: ["mylib.cpp"],
802 system_shared_libs: [],
803 stl: "none",
804 stubs: {
805 versions: ["10", "20", "30"],
806 },
Jiyong Park0f80c182020-01-31 02:49:53 +0900807 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900808 }
809
810 cc_library {
811 name: "libbar",
812 srcs: ["mylib.cpp"],
813 system_shared_libs: [],
814 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000815 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900816 }
817
818 `)
819
Sundong Ahnabb64432019-10-22 13:58:29 +0900820 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900821 copyCmds := apexRule.Args["copy_commands"]
822
823 // Ensure that direct non-stubs dep is always included
824 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
825
826 // Ensure that indirect stubs dep is not included
827 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
828
829 // Ensure that runtime_libs dep in included
830 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
831
Sundong Ahnabb64432019-10-22 13:58:29 +0900832 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900833 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
834 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900835
836}
837
Jooyung Han9c80bae2019-08-20 17:30:57 +0900838func TestApexDependencyToLLNDK(t *testing.T) {
839 ctx, _ := testApex(t, `
840 apex {
841 name: "myapex",
842 key: "myapex.key",
843 use_vendor: true,
844 native_shared_libs: ["mylib"],
845 }
846
847 apex_key {
848 name: "myapex.key",
849 public_key: "testkey.avbpubkey",
850 private_key: "testkey.pem",
851 }
852
853 cc_library {
854 name: "mylib",
855 srcs: ["mylib.cpp"],
856 vendor_available: true,
857 shared_libs: ["libbar"],
858 system_shared_libs: [],
859 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000860 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900861 }
862
863 cc_library {
864 name: "libbar",
865 srcs: ["mylib.cpp"],
866 system_shared_libs: [],
867 stl: "none",
868 }
869
870 llndk_library {
871 name: "libbar",
872 symbol_file: "",
873 }
Jooyung Handc782442019-11-01 03:14:38 +0900874 `, func(fs map[string][]byte, config android.Config) {
875 setUseVendorWhitelistForTest(config, []string{"myapex"})
876 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900877
Sundong Ahnabb64432019-10-22 13:58:29 +0900878 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900879 copyCmds := apexRule.Args["copy_commands"]
880
881 // Ensure that LLNDK dep is not included
882 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
883
Sundong Ahnabb64432019-10-22 13:58:29 +0900884 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900885 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900886
887 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900888 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900889
890}
891
Jiyong Park25fc6a92018-11-18 18:02:45 +0900892func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700893 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900894 apex {
895 name: "myapex",
896 key: "myapex.key",
897 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
898 }
899
900 apex_key {
901 name: "myapex.key",
902 public_key: "testkey.avbpubkey",
903 private_key: "testkey.pem",
904 }
905
906 cc_library {
907 name: "mylib",
908 srcs: ["mylib.cpp"],
909 shared_libs: ["libdl#27"],
910 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000911 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900912 }
913
914 cc_library_shared {
915 name: "mylib_shared",
916 srcs: ["mylib.cpp"],
917 shared_libs: ["libdl#27"],
918 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000919 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900920 }
921
922 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +0900923 name: "libBootstrap",
924 srcs: ["mylib.cpp"],
925 stl: "none",
926 bootstrap: true,
927 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900928 `)
929
Sundong Ahnabb64432019-10-22 13:58:29 +0900930 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900931 copyCmds := apexRule.Args["copy_commands"]
932
933 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800934 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900935 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
936 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900937
938 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900939 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900940
Colin Cross7113d202019-11-20 16:39:12 -0800941 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
942 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
943 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900944
945 // For dependency to libc
946 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900947 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900948 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900949 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900950 // ... Cflags from stub is correctly exported to mylib
951 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
952 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
953
954 // For dependency to libm
955 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800956 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900957 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +0900958 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900959 // ... and is not compiling with the stub
960 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
961 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
962
963 // For dependency to libdl
964 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900965 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900966 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +0900967 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
968 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900969 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800970 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900971 // ... Cflags from stub is correctly exported to mylib
972 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
973 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900974
975 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -0800976 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
977 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
978 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
979 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900980}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900981
982func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700983 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +0900984 apex {
985 name: "myapex",
986 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900987 native_shared_libs: ["mylib"],
988 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900989 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900990 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900991 }
992
993 apex_key {
994 name: "myapex.key",
995 public_key: "testkey.avbpubkey",
996 private_key: "testkey.pem",
997 }
998
999 prebuilt_etc {
1000 name: "myetc",
1001 src: "myprebuilt",
1002 sub_dir: "foo/bar",
1003 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001004
1005 cc_library {
1006 name: "mylib",
1007 srcs: ["mylib.cpp"],
1008 relative_install_path: "foo/bar",
1009 system_shared_libs: [],
1010 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001011 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001012 }
1013
1014 cc_binary {
1015 name: "mybin",
1016 srcs: ["mylib.cpp"],
1017 relative_install_path: "foo/bar",
1018 system_shared_libs: [],
1019 static_executable: true,
1020 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001021 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001022 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001023 `)
1024
Sundong Ahnabb64432019-10-22 13:58:29 +09001025 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001026 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1027
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001028 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001029 ensureListContains(t, dirs, "etc")
1030 ensureListContains(t, dirs, "etc/foo")
1031 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001032 ensureListContains(t, dirs, "lib64")
1033 ensureListContains(t, dirs, "lib64/foo")
1034 ensureListContains(t, dirs, "lib64/foo/bar")
1035 ensureListContains(t, dirs, "lib")
1036 ensureListContains(t, dirs, "lib/foo")
1037 ensureListContains(t, dirs, "lib/foo/bar")
1038
Jiyong Parkbd13e442019-03-15 18:10:35 +09001039 ensureListContains(t, dirs, "bin")
1040 ensureListContains(t, dirs, "bin/foo")
1041 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001042}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001043
1044func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001045 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001046 apex {
1047 name: "myapex",
1048 key: "myapex.key",
1049 native_shared_libs: ["mylib"],
1050 use_vendor: true,
1051 }
1052
1053 apex_key {
1054 name: "myapex.key",
1055 public_key: "testkey.avbpubkey",
1056 private_key: "testkey.pem",
1057 }
1058
1059 cc_library {
1060 name: "mylib",
1061 srcs: ["mylib.cpp"],
1062 shared_libs: ["mylib2"],
1063 system_shared_libs: [],
1064 vendor_available: true,
1065 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001066 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001067 }
1068
1069 cc_library {
1070 name: "mylib2",
1071 srcs: ["mylib.cpp"],
1072 system_shared_libs: [],
1073 vendor_available: true,
1074 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001075 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001076 }
Jooyung Handc782442019-11-01 03:14:38 +09001077 `, func(fs map[string][]byte, config android.Config) {
1078 setUseVendorWhitelistForTest(config, []string{"myapex"})
1079 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001080
1081 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001082 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001083 for _, implicit := range i.Implicits {
1084 inputsList = append(inputsList, implicit.String())
1085 }
1086 }
1087 inputsString := strings.Join(inputsList, " ")
1088
1089 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001090 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1091 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001092
1093 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001094 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1095 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001096}
Jiyong Park16e91a02018-12-20 18:18:08 +09001097
Jooyung Handc782442019-11-01 03:14:38 +09001098func TestUseVendorRestriction(t *testing.T) {
1099 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1100 apex {
1101 name: "myapex",
1102 key: "myapex.key",
1103 use_vendor: true,
1104 }
1105 apex_key {
1106 name: "myapex.key",
1107 public_key: "testkey.avbpubkey",
1108 private_key: "testkey.pem",
1109 }
1110 `, func(fs map[string][]byte, config android.Config) {
1111 setUseVendorWhitelistForTest(config, []string{""})
1112 })
1113 // no error with whitelist
1114 testApex(t, `
1115 apex {
1116 name: "myapex",
1117 key: "myapex.key",
1118 use_vendor: true,
1119 }
1120 apex_key {
1121 name: "myapex.key",
1122 public_key: "testkey.avbpubkey",
1123 private_key: "testkey.pem",
1124 }
1125 `, func(fs map[string][]byte, config android.Config) {
1126 setUseVendorWhitelistForTest(config, []string{"myapex"})
1127 })
1128}
1129
Jooyung Han5c998b92019-06-27 11:30:33 +09001130func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1131 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1132 apex {
1133 name: "myapex",
1134 key: "myapex.key",
1135 native_shared_libs: ["mylib"],
1136 use_vendor: true,
1137 }
1138
1139 apex_key {
1140 name: "myapex.key",
1141 public_key: "testkey.avbpubkey",
1142 private_key: "testkey.pem",
1143 }
1144
1145 cc_library {
1146 name: "mylib",
1147 srcs: ["mylib.cpp"],
1148 system_shared_libs: [],
1149 stl: "none",
1150 }
1151 `)
1152}
1153
Jiyong Park16e91a02018-12-20 18:18:08 +09001154func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001155 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001156 apex {
1157 name: "myapex",
1158 key: "myapex.key",
1159 native_shared_libs: ["mylib"],
1160 }
1161
1162 apex_key {
1163 name: "myapex.key",
1164 public_key: "testkey.avbpubkey",
1165 private_key: "testkey.pem",
1166 }
1167
1168 cc_library {
1169 name: "mylib",
1170 srcs: ["mylib.cpp"],
1171 system_shared_libs: [],
1172 stl: "none",
1173 stubs: {
1174 versions: ["1", "2", "3"],
1175 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001176 apex_available: [
1177 "//apex_available:platform",
1178 "myapex",
1179 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001180 }
1181
1182 cc_binary {
1183 name: "not_in_apex",
1184 srcs: ["mylib.cpp"],
1185 static_libs: ["mylib"],
1186 static_executable: true,
1187 system_shared_libs: [],
1188 stl: "none",
1189 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001190 `)
1191
Colin Cross7113d202019-11-20 16:39:12 -08001192 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001193
1194 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001195 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001196}
Jiyong Park9335a262018-12-24 11:31:58 +09001197
1198func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001199 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001200 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001201 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001202 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001203 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001204 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001205 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001206 }
1207
1208 cc_library {
1209 name: "mylib",
1210 srcs: ["mylib.cpp"],
1211 system_shared_libs: [],
1212 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001213 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001214 }
1215
1216 apex_key {
1217 name: "myapex.key",
1218 public_key: "testkey.avbpubkey",
1219 private_key: "testkey.pem",
1220 }
1221
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001222 android_app_certificate {
1223 name: "myapex.certificate",
1224 certificate: "testkey",
1225 }
1226
1227 android_app_certificate {
1228 name: "myapex.certificate.override",
1229 certificate: "testkey.override",
1230 }
1231
Jiyong Park9335a262018-12-24 11:31:58 +09001232 `)
1233
1234 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001235 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001236
1237 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1238 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1239 "vendor/foo/devkeys/testkey.avbpubkey")
1240 }
1241 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1242 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1243 "vendor/foo/devkeys/testkey.pem")
1244 }
1245
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001246 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001247 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001248 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001249 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001250 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001251 }
1252}
Jiyong Park58e364a2019-01-19 19:24:06 +09001253
Jooyung Hanf121a652019-12-17 14:30:11 +09001254func TestCertificate(t *testing.T) {
1255 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1256 ctx, _ := testApex(t, `
1257 apex {
1258 name: "myapex",
1259 key: "myapex.key",
1260 }
1261 apex_key {
1262 name: "myapex.key",
1263 public_key: "testkey.avbpubkey",
1264 private_key: "testkey.pem",
1265 }`)
1266 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1267 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1268 if actual := rule.Args["certificates"]; actual != expected {
1269 t.Errorf("certificates should be %q, not %q", expected, actual)
1270 }
1271 })
1272 t.Run("override when unspecified", func(t *testing.T) {
1273 ctx, _ := testApex(t, `
1274 apex {
1275 name: "myapex_keytest",
1276 key: "myapex.key",
1277 file_contexts: ":myapex-file_contexts",
1278 }
1279 apex_key {
1280 name: "myapex.key",
1281 public_key: "testkey.avbpubkey",
1282 private_key: "testkey.pem",
1283 }
1284 android_app_certificate {
1285 name: "myapex.certificate.override",
1286 certificate: "testkey.override",
1287 }`)
1288 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1289 expected := "testkey.override.x509.pem testkey.override.pk8"
1290 if actual := rule.Args["certificates"]; actual != expected {
1291 t.Errorf("certificates should be %q, not %q", expected, actual)
1292 }
1293 })
1294 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1295 ctx, _ := testApex(t, `
1296 apex {
1297 name: "myapex",
1298 key: "myapex.key",
1299 certificate: ":myapex.certificate",
1300 }
1301 apex_key {
1302 name: "myapex.key",
1303 public_key: "testkey.avbpubkey",
1304 private_key: "testkey.pem",
1305 }
1306 android_app_certificate {
1307 name: "myapex.certificate",
1308 certificate: "testkey",
1309 }`)
1310 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1311 expected := "testkey.x509.pem testkey.pk8"
1312 if actual := rule.Args["certificates"]; actual != expected {
1313 t.Errorf("certificates should be %q, not %q", expected, actual)
1314 }
1315 })
1316 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1317 ctx, _ := testApex(t, `
1318 apex {
1319 name: "myapex_keytest",
1320 key: "myapex.key",
1321 file_contexts: ":myapex-file_contexts",
1322 certificate: ":myapex.certificate",
1323 }
1324 apex_key {
1325 name: "myapex.key",
1326 public_key: "testkey.avbpubkey",
1327 private_key: "testkey.pem",
1328 }
1329 android_app_certificate {
1330 name: "myapex.certificate.override",
1331 certificate: "testkey.override",
1332 }`)
1333 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1334 expected := "testkey.override.x509.pem testkey.override.pk8"
1335 if actual := rule.Args["certificates"]; actual != expected {
1336 t.Errorf("certificates should be %q, not %q", expected, actual)
1337 }
1338 })
1339 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1340 ctx, _ := testApex(t, `
1341 apex {
1342 name: "myapex",
1343 key: "myapex.key",
1344 certificate: "testkey",
1345 }
1346 apex_key {
1347 name: "myapex.key",
1348 public_key: "testkey.avbpubkey",
1349 private_key: "testkey.pem",
1350 }`)
1351 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1352 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1353 if actual := rule.Args["certificates"]; actual != expected {
1354 t.Errorf("certificates should be %q, not %q", expected, actual)
1355 }
1356 })
1357 t.Run("override when specified as <name>", func(t *testing.T) {
1358 ctx, _ := testApex(t, `
1359 apex {
1360 name: "myapex_keytest",
1361 key: "myapex.key",
1362 file_contexts: ":myapex-file_contexts",
1363 certificate: "testkey",
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.override",
1372 certificate: "testkey.override",
1373 }`)
1374 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1375 expected := "testkey.override.x509.pem testkey.override.pk8"
1376 if actual := rule.Args["certificates"]; actual != expected {
1377 t.Errorf("certificates should be %q, not %q", expected, actual)
1378 }
1379 })
1380}
1381
Jiyong Park58e364a2019-01-19 19:24:06 +09001382func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001383 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001384 apex {
1385 name: "myapex",
1386 key: "myapex.key",
1387 native_shared_libs: ["mylib"],
1388 }
1389
1390 apex {
1391 name: "otherapex",
1392 key: "myapex.key",
1393 native_shared_libs: ["mylib"],
1394 }
1395
1396 apex_key {
1397 name: "myapex.key",
1398 public_key: "testkey.avbpubkey",
1399 private_key: "testkey.pem",
1400 }
1401
1402 cc_library {
1403 name: "mylib",
1404 srcs: ["mylib.cpp"],
1405 system_shared_libs: [],
1406 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001407 // TODO: remove //apex_available:platform
1408 apex_available: [
1409 "//apex_available:platform",
1410 "myapex",
1411 "otherapex",
1412 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001413 }
1414 `)
1415
Jooyung Han6b8459b2019-10-30 08:29:25 +09001416 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001417 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001418 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001419 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1420 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001421
Jooyung Han6b8459b2019-10-30 08:29:25 +09001422 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001423 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001424 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001425 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1426 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001427
Jooyung Han6b8459b2019-10-30 08:29:25 +09001428 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001429 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001430 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001431 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1432 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001433}
Jiyong Park7e636d02019-01-28 16:16:54 +09001434
1435func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001436 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001437 apex {
1438 name: "myapex",
1439 key: "myapex.key",
1440 native_shared_libs: ["mylib"],
1441 }
1442
1443 apex_key {
1444 name: "myapex.key",
1445 public_key: "testkey.avbpubkey",
1446 private_key: "testkey.pem",
1447 }
1448
1449 cc_library_headers {
1450 name: "mylib_headers",
1451 export_include_dirs: ["my_include"],
1452 system_shared_libs: [],
1453 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001454 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001455 }
1456
1457 cc_library {
1458 name: "mylib",
1459 srcs: ["mylib.cpp"],
1460 system_shared_libs: [],
1461 stl: "none",
1462 header_libs: ["mylib_headers"],
1463 export_header_lib_headers: ["mylib_headers"],
1464 stubs: {
1465 versions: ["1", "2", "3"],
1466 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001467 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001468 }
1469
1470 cc_library {
1471 name: "otherlib",
1472 srcs: ["mylib.cpp"],
1473 system_shared_libs: [],
1474 stl: "none",
1475 shared_libs: ["mylib"],
1476 }
1477 `)
1478
Colin Cross7113d202019-11-20 16:39:12 -08001479 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001480
1481 // Ensure that the include path of the header lib is exported to 'otherlib'
1482 ensureContains(t, cFlags, "-Imy_include")
1483}
Alex Light9670d332019-01-29 18:07:33 -08001484
Jiyong Park7cd10e32020-01-14 09:22:18 +09001485type fileInApex struct {
1486 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001487 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001488 isLink bool
1489}
1490
Jooyung Hana57af4a2020-01-23 05:36:59 +00001491func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001492 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001493 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001494 copyCmds := apexRule.Args["copy_commands"]
1495 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001496 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001497 for _, cmd := range strings.Split(copyCmds, "&&") {
1498 cmd = strings.TrimSpace(cmd)
1499 if cmd == "" {
1500 continue
1501 }
1502 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001503 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001504 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001505 switch terms[0] {
1506 case "mkdir":
1507 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001508 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001509 t.Fatal("copyCmds contains invalid cp command", cmd)
1510 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001511 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001512 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001513 isLink = false
1514 case "ln":
1515 if len(terms) != 3 && len(terms) != 4 {
1516 // ln LINK TARGET or ln -s LINK TARGET
1517 t.Fatal("copyCmds contains invalid ln command", cmd)
1518 }
1519 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001520 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001521 isLink = true
1522 default:
1523 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1524 }
1525 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001526 index := strings.Index(dst, imageApexDir)
1527 if index == -1 {
1528 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1529 }
1530 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001531 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001532 }
1533 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001534 return ret
1535}
1536
Jooyung Hana57af4a2020-01-23 05:36:59 +00001537func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1538 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001539 var failed bool
1540 var surplus []string
1541 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001542 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09001543 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09001544 for _, expected := range files {
1545 if matched, _ := path.Match(expected, file.path); matched {
1546 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09001547 mactchFound = true
1548 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09001549 }
1550 }
Jooyung Hane6436d72020-02-27 13:31:56 +09001551 if !mactchFound {
1552 surplus = append(surplus, file.path)
1553 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001554 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001555
Jooyung Han31c470b2019-10-18 16:26:59 +09001556 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001557 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001558 t.Log("surplus files", surplus)
1559 failed = true
1560 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001561
1562 if len(files) > len(filesMatched) {
1563 var missing []string
1564 for _, expected := range files {
1565 if !filesMatched[expected] {
1566 missing = append(missing, expected)
1567 }
1568 }
1569 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001570 t.Log("missing files", missing)
1571 failed = true
1572 }
1573 if failed {
1574 t.Fail()
1575 }
1576}
1577
Jooyung Han344d5432019-08-23 11:17:39 +09001578func TestVndkApexCurrent(t *testing.T) {
1579 ctx, _ := testApex(t, `
1580 apex_vndk {
1581 name: "myapex",
1582 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001583 }
1584
1585 apex_key {
1586 name: "myapex.key",
1587 public_key: "testkey.avbpubkey",
1588 private_key: "testkey.pem",
1589 }
1590
1591 cc_library {
1592 name: "libvndk",
1593 srcs: ["mylib.cpp"],
1594 vendor_available: true,
1595 vndk: {
1596 enabled: true,
1597 },
1598 system_shared_libs: [],
1599 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001600 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001601 }
1602
1603 cc_library {
1604 name: "libvndksp",
1605 srcs: ["mylib.cpp"],
1606 vendor_available: true,
1607 vndk: {
1608 enabled: true,
1609 support_system_process: true,
1610 },
1611 system_shared_libs: [],
1612 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001613 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001614 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001615 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001616
Jooyung Hana57af4a2020-01-23 05:36:59 +00001617 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001618 "lib/libvndk.so",
1619 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001620 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09001621 "lib64/libvndk.so",
1622 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001623 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001624 "etc/llndk.libraries.VER.txt",
1625 "etc/vndkcore.libraries.VER.txt",
1626 "etc/vndksp.libraries.VER.txt",
1627 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001628 })
Jooyung Han344d5432019-08-23 11:17:39 +09001629}
1630
1631func TestVndkApexWithPrebuilt(t *testing.T) {
1632 ctx, _ := testApex(t, `
1633 apex_vndk {
1634 name: "myapex",
1635 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001636 }
1637
1638 apex_key {
1639 name: "myapex.key",
1640 public_key: "testkey.avbpubkey",
1641 private_key: "testkey.pem",
1642 }
1643
1644 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001645 name: "libvndk",
1646 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001647 vendor_available: true,
1648 vndk: {
1649 enabled: true,
1650 },
1651 system_shared_libs: [],
1652 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001653 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001654 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001655
1656 cc_prebuilt_library_shared {
1657 name: "libvndk.arm",
1658 srcs: ["libvndk.arm.so"],
1659 vendor_available: true,
1660 vndk: {
1661 enabled: true,
1662 },
1663 enabled: false,
1664 arch: {
1665 arm: {
1666 enabled: true,
1667 },
1668 },
1669 system_shared_libs: [],
1670 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001671 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001672 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001673 `+vndkLibrariesTxtFiles("current"),
1674 withFiles(map[string][]byte{
1675 "libvndk.so": nil,
1676 "libvndk.arm.so": nil,
1677 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001678
Jooyung Hana57af4a2020-01-23 05:36:59 +00001679 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001680 "lib/libvndk.so",
1681 "lib/libvndk.arm.so",
1682 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001683 "lib/libc++.so",
1684 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001685 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001686 })
Jooyung Han344d5432019-08-23 11:17:39 +09001687}
1688
Jooyung Han39edb6c2019-11-06 16:53:07 +09001689func vndkLibrariesTxtFiles(vers ...string) (result string) {
1690 for _, v := range vers {
1691 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001692 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001693 result += `
1694 vndk_libraries_txt {
1695 name: "` + txt + `.libraries.txt",
1696 }
1697 `
1698 }
1699 } else {
1700 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1701 result += `
1702 prebuilt_etc {
1703 name: "` + txt + `.libraries.` + v + `.txt",
1704 src: "dummy.txt",
1705 }
1706 `
1707 }
1708 }
1709 }
1710 return
1711}
1712
Jooyung Han344d5432019-08-23 11:17:39 +09001713func TestVndkApexVersion(t *testing.T) {
1714 ctx, _ := testApex(t, `
1715 apex_vndk {
1716 name: "myapex_v27",
1717 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001718 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001719 vndk_version: "27",
1720 }
1721
1722 apex_key {
1723 name: "myapex.key",
1724 public_key: "testkey.avbpubkey",
1725 private_key: "testkey.pem",
1726 }
1727
Jooyung Han31c470b2019-10-18 16:26:59 +09001728 vndk_prebuilt_shared {
1729 name: "libvndk27",
1730 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001731 vendor_available: true,
1732 vndk: {
1733 enabled: true,
1734 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001735 target_arch: "arm64",
1736 arch: {
1737 arm: {
1738 srcs: ["libvndk27_arm.so"],
1739 },
1740 arm64: {
1741 srcs: ["libvndk27_arm64.so"],
1742 },
1743 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001744 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001745 }
1746
1747 vndk_prebuilt_shared {
1748 name: "libvndk27",
1749 version: "27",
1750 vendor_available: true,
1751 vndk: {
1752 enabled: true,
1753 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001754 target_arch: "x86_64",
1755 arch: {
1756 x86: {
1757 srcs: ["libvndk27_x86.so"],
1758 },
1759 x86_64: {
1760 srcs: ["libvndk27_x86_64.so"],
1761 },
1762 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001763 }
1764 `+vndkLibrariesTxtFiles("27"),
1765 withFiles(map[string][]byte{
1766 "libvndk27_arm.so": nil,
1767 "libvndk27_arm64.so": nil,
1768 "libvndk27_x86.so": nil,
1769 "libvndk27_x86_64.so": nil,
1770 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001771
Jooyung Hana57af4a2020-01-23 05:36:59 +00001772 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001773 "lib/libvndk27_arm.so",
1774 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001775 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001776 })
Jooyung Han344d5432019-08-23 11:17:39 +09001777}
1778
1779func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1780 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1781 apex_vndk {
1782 name: "myapex_v27",
1783 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001784 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001785 vndk_version: "27",
1786 }
1787 apex_vndk {
1788 name: "myapex_v27_other",
1789 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001790 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001791 vndk_version: "27",
1792 }
1793
1794 apex_key {
1795 name: "myapex.key",
1796 public_key: "testkey.avbpubkey",
1797 private_key: "testkey.pem",
1798 }
1799
1800 cc_library {
1801 name: "libvndk",
1802 srcs: ["mylib.cpp"],
1803 vendor_available: true,
1804 vndk: {
1805 enabled: true,
1806 },
1807 system_shared_libs: [],
1808 stl: "none",
1809 }
1810
1811 vndk_prebuilt_shared {
1812 name: "libvndk",
1813 version: "27",
1814 vendor_available: true,
1815 vndk: {
1816 enabled: true,
1817 },
1818 srcs: ["libvndk.so"],
1819 }
1820 `, withFiles(map[string][]byte{
1821 "libvndk.so": nil,
1822 }))
1823}
1824
Jooyung Han90eee022019-10-01 20:02:42 +09001825func TestVndkApexNameRule(t *testing.T) {
1826 ctx, _ := testApex(t, `
1827 apex_vndk {
1828 name: "myapex",
1829 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001830 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001831 }
1832 apex_vndk {
1833 name: "myapex_v28",
1834 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001835 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001836 vndk_version: "28",
1837 }
1838 apex_key {
1839 name: "myapex.key",
1840 public_key: "testkey.avbpubkey",
1841 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001842 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001843
1844 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00001845 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001846 actual := proptools.String(bundle.properties.Apex_name)
1847 if !reflect.DeepEqual(actual, expected) {
1848 t.Errorf("Got '%v', expected '%v'", actual, expected)
1849 }
1850 }
1851
1852 assertApexName("com.android.vndk.vVER", "myapex")
1853 assertApexName("com.android.vndk.v28", "myapex_v28")
1854}
1855
Jooyung Han344d5432019-08-23 11:17:39 +09001856func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1857 ctx, _ := testApex(t, `
1858 apex_vndk {
1859 name: "myapex",
1860 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001861 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001862 }
1863
1864 apex_key {
1865 name: "myapex.key",
1866 public_key: "testkey.avbpubkey",
1867 private_key: "testkey.pem",
1868 }
1869
1870 cc_library {
1871 name: "libvndk",
1872 srcs: ["mylib.cpp"],
1873 vendor_available: true,
1874 native_bridge_supported: true,
1875 host_supported: true,
1876 vndk: {
1877 enabled: true,
1878 },
1879 system_shared_libs: [],
1880 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001881 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001882 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001883 `+vndkLibrariesTxtFiles("current"),
1884 withTargets(map[android.OsType][]android.Target{
1885 android.Android: []android.Target{
1886 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1887 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1888 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1889 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1890 },
1891 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001892
Jooyung Hana57af4a2020-01-23 05:36:59 +00001893 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001894 "lib/libvndk.so",
1895 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09001896 "lib/libc++.so",
1897 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001898 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001899 })
Jooyung Han344d5432019-08-23 11:17:39 +09001900}
1901
1902func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1903 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1904 apex_vndk {
1905 name: "myapex",
1906 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001907 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001908 native_bridge_supported: true,
1909 }
1910
1911 apex_key {
1912 name: "myapex.key",
1913 public_key: "testkey.avbpubkey",
1914 private_key: "testkey.pem",
1915 }
1916
1917 cc_library {
1918 name: "libvndk",
1919 srcs: ["mylib.cpp"],
1920 vendor_available: true,
1921 native_bridge_supported: true,
1922 host_supported: true,
1923 vndk: {
1924 enabled: true,
1925 },
1926 system_shared_libs: [],
1927 stl: "none",
1928 }
1929 `)
1930}
1931
Jooyung Han31c470b2019-10-18 16:26:59 +09001932func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001933 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001934 apex_vndk {
1935 name: "myapex_v27",
1936 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001937 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001938 vndk_version: "27",
1939 }
1940
1941 apex_key {
1942 name: "myapex.key",
1943 public_key: "testkey.avbpubkey",
1944 private_key: "testkey.pem",
1945 }
1946
1947 vndk_prebuilt_shared {
1948 name: "libvndk27",
1949 version: "27",
1950 target_arch: "arm",
1951 vendor_available: true,
1952 vndk: {
1953 enabled: true,
1954 },
1955 arch: {
1956 arm: {
1957 srcs: ["libvndk27.so"],
1958 }
1959 },
1960 }
1961
1962 vndk_prebuilt_shared {
1963 name: "libvndk27",
1964 version: "27",
1965 target_arch: "arm",
1966 binder32bit: true,
1967 vendor_available: true,
1968 vndk: {
1969 enabled: true,
1970 },
1971 arch: {
1972 arm: {
1973 srcs: ["libvndk27binder32.so"],
1974 }
1975 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001976 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001977 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001978 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001979 withFiles(map[string][]byte{
1980 "libvndk27.so": nil,
1981 "libvndk27binder32.so": nil,
1982 }),
1983 withBinder32bit,
1984 withTargets(map[android.OsType][]android.Target{
1985 android.Android: []android.Target{
1986 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1987 },
1988 }),
1989 )
1990
Jooyung Hana57af4a2020-01-23 05:36:59 +00001991 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001992 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001993 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001994 })
1995}
1996
Jooyung Hane1633032019-08-01 17:41:43 +09001997func TestDependenciesInApexManifest(t *testing.T) {
1998 ctx, _ := testApex(t, `
1999 apex {
2000 name: "myapex_nodep",
2001 key: "myapex.key",
2002 native_shared_libs: ["lib_nodep"],
2003 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002004 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002005 }
2006
2007 apex {
2008 name: "myapex_dep",
2009 key: "myapex.key",
2010 native_shared_libs: ["lib_dep"],
2011 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002012 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002013 }
2014
2015 apex {
2016 name: "myapex_provider",
2017 key: "myapex.key",
2018 native_shared_libs: ["libfoo"],
2019 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002020 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002021 }
2022
2023 apex {
2024 name: "myapex_selfcontained",
2025 key: "myapex.key",
2026 native_shared_libs: ["lib_dep", "libfoo"],
2027 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002028 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002029 }
2030
2031 apex_key {
2032 name: "myapex.key",
2033 public_key: "testkey.avbpubkey",
2034 private_key: "testkey.pem",
2035 }
2036
2037 cc_library {
2038 name: "lib_nodep",
2039 srcs: ["mylib.cpp"],
2040 system_shared_libs: [],
2041 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002042 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002043 }
2044
2045 cc_library {
2046 name: "lib_dep",
2047 srcs: ["mylib.cpp"],
2048 shared_libs: ["libfoo"],
2049 system_shared_libs: [],
2050 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002051 apex_available: [
2052 "myapex_dep",
2053 "myapex_provider",
2054 "myapex_selfcontained",
2055 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002056 }
2057
2058 cc_library {
2059 name: "libfoo",
2060 srcs: ["mytest.cpp"],
2061 stubs: {
2062 versions: ["1"],
2063 },
2064 system_shared_libs: [],
2065 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002066 apex_available: [
2067 "myapex_provider",
2068 "myapex_selfcontained",
2069 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002070 }
2071 `)
2072
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002073 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002074 var provideNativeLibs, requireNativeLibs []string
2075
Sundong Ahnabb64432019-10-22 13:58:29 +09002076 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002077 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2078 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002079 ensureListEmpty(t, provideNativeLibs)
2080 ensureListEmpty(t, requireNativeLibs)
2081
Sundong Ahnabb64432019-10-22 13:58:29 +09002082 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002083 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2084 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002085 ensureListEmpty(t, provideNativeLibs)
2086 ensureListContains(t, requireNativeLibs, "libfoo.so")
2087
Sundong Ahnabb64432019-10-22 13:58:29 +09002088 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002089 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2090 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002091 ensureListContains(t, provideNativeLibs, "libfoo.so")
2092 ensureListEmpty(t, requireNativeLibs)
2093
Sundong Ahnabb64432019-10-22 13:58:29 +09002094 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002095 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2096 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002097 ensureListContains(t, provideNativeLibs, "libfoo.so")
2098 ensureListEmpty(t, requireNativeLibs)
2099}
2100
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002101func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002102 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002103 apex {
2104 name: "myapex",
2105 key: "myapex.key",
2106 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002107 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002108 }
2109
2110 apex_key {
2111 name: "myapex.key",
2112 public_key: "testkey.avbpubkey",
2113 private_key: "testkey.pem",
2114 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002115
2116 cc_library {
2117 name: "mylib",
2118 srcs: ["mylib.cpp"],
2119 system_shared_libs: [],
2120 stl: "none",
2121 apex_available: [
2122 "//apex_available:platform",
2123 "myapex",
2124 ],
2125 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002126 `)
2127
Sundong Ahnabb64432019-10-22 13:58:29 +09002128 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002129 apexManifestRule := module.Rule("apexManifestRule")
2130 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2131 apexRule := module.Rule("apexRule")
2132 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002133
2134 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2135 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2136 name := apexBundle.BaseModuleName()
2137 prefix := "TARGET_"
2138 var builder strings.Builder
2139 data.Custom(&builder, name, prefix, "", data)
2140 androidMk := builder.String()
2141 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2142 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002143}
2144
Alex Light0851b882019-02-07 13:20:53 -08002145func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002146 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002147 apex {
2148 name: "myapex",
2149 key: "myapex.key",
2150 native_shared_libs: ["mylib_common"],
2151 }
2152
2153 apex_key {
2154 name: "myapex.key",
2155 public_key: "testkey.avbpubkey",
2156 private_key: "testkey.pem",
2157 }
2158
2159 cc_library {
2160 name: "mylib_common",
2161 srcs: ["mylib.cpp"],
2162 system_shared_libs: [],
2163 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002164 apex_available: [
2165 "//apex_available:platform",
2166 "myapex",
2167 ],
Alex Light0851b882019-02-07 13:20:53 -08002168 }
2169 `)
2170
Sundong Ahnabb64432019-10-22 13:58:29 +09002171 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002172 apexRule := module.Rule("apexRule")
2173 copyCmds := apexRule.Args["copy_commands"]
2174
2175 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2176 t.Log("Apex was a test apex!")
2177 t.Fail()
2178 }
2179 // Ensure that main rule creates an output
2180 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2181
2182 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002183 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002184
2185 // Ensure that both direct and indirect deps are copied into apex
2186 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2187
Colin Cross7113d202019-11-20 16:39:12 -08002188 // Ensure that the platform variant ends with _shared
2189 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002190
2191 if !android.InAnyApex("mylib_common") {
2192 t.Log("Found mylib_common not in any apex!")
2193 t.Fail()
2194 }
2195}
2196
2197func TestTestApex(t *testing.T) {
2198 if android.InAnyApex("mylib_common_test") {
2199 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!")
2200 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002201 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002202 apex_test {
2203 name: "myapex",
2204 key: "myapex.key",
2205 native_shared_libs: ["mylib_common_test"],
2206 }
2207
2208 apex_key {
2209 name: "myapex.key",
2210 public_key: "testkey.avbpubkey",
2211 private_key: "testkey.pem",
2212 }
2213
2214 cc_library {
2215 name: "mylib_common_test",
2216 srcs: ["mylib.cpp"],
2217 system_shared_libs: [],
2218 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002219 // TODO: remove //apex_available:platform
2220 apex_available: [
2221 "//apex_available:platform",
2222 "myapex",
2223 ],
Alex Light0851b882019-02-07 13:20:53 -08002224 }
2225 `)
2226
Sundong Ahnabb64432019-10-22 13:58:29 +09002227 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002228 apexRule := module.Rule("apexRule")
2229 copyCmds := apexRule.Args["copy_commands"]
2230
2231 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2232 t.Log("Apex was not a test apex!")
2233 t.Fail()
2234 }
2235 // Ensure that main rule creates an output
2236 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2237
2238 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002239 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002240
2241 // Ensure that both direct and indirect deps are copied into apex
2242 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2243
Colin Cross7113d202019-11-20 16:39:12 -08002244 // Ensure that the platform variant ends with _shared
2245 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002246}
2247
Alex Light9670d332019-01-29 18:07:33 -08002248func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002249 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002250 apex {
2251 name: "myapex",
2252 key: "myapex.key",
2253 multilib: {
2254 first: {
2255 native_shared_libs: ["mylib_common"],
2256 }
2257 },
2258 target: {
2259 android: {
2260 multilib: {
2261 first: {
2262 native_shared_libs: ["mylib"],
2263 }
2264 }
2265 },
2266 host: {
2267 multilib: {
2268 first: {
2269 native_shared_libs: ["mylib2"],
2270 }
2271 }
2272 }
2273 }
2274 }
2275
2276 apex_key {
2277 name: "myapex.key",
2278 public_key: "testkey.avbpubkey",
2279 private_key: "testkey.pem",
2280 }
2281
2282 cc_library {
2283 name: "mylib",
2284 srcs: ["mylib.cpp"],
2285 system_shared_libs: [],
2286 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002287 // TODO: remove //apex_available:platform
2288 apex_available: [
2289 "//apex_available:platform",
2290 "myapex",
2291 ],
Alex Light9670d332019-01-29 18:07:33 -08002292 }
2293
2294 cc_library {
2295 name: "mylib_common",
2296 srcs: ["mylib.cpp"],
2297 system_shared_libs: [],
2298 stl: "none",
2299 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002300 // TODO: remove //apex_available:platform
2301 apex_available: [
2302 "//apex_available:platform",
2303 "myapex",
2304 ],
Alex Light9670d332019-01-29 18:07:33 -08002305 }
2306
2307 cc_library {
2308 name: "mylib2",
2309 srcs: ["mylib.cpp"],
2310 system_shared_libs: [],
2311 stl: "none",
2312 compile_multilib: "first",
2313 }
2314 `)
2315
Sundong Ahnabb64432019-10-22 13:58:29 +09002316 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002317 copyCmds := apexRule.Args["copy_commands"]
2318
2319 // Ensure that main rule creates an output
2320 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2321
2322 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002323 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2324 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2325 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002326
2327 // Ensure that both direct and indirect deps are copied into apex
2328 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2329 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2330 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2331
Colin Cross7113d202019-11-20 16:39:12 -08002332 // Ensure that the platform variant ends with _shared
2333 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2334 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2335 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002336}
Jiyong Park04480cf2019-02-06 00:16:29 +09002337
2338func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002339 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002340 apex {
2341 name: "myapex",
2342 key: "myapex.key",
2343 binaries: ["myscript"],
2344 }
2345
2346 apex_key {
2347 name: "myapex.key",
2348 public_key: "testkey.avbpubkey",
2349 private_key: "testkey.pem",
2350 }
2351
2352 sh_binary {
2353 name: "myscript",
2354 src: "mylib.cpp",
2355 filename: "myscript.sh",
2356 sub_dir: "script",
2357 }
2358 `)
2359
Sundong Ahnabb64432019-10-22 13:58:29 +09002360 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002361 copyCmds := apexRule.Args["copy_commands"]
2362
2363 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2364}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002365
Jooyung Han91df2082019-11-20 01:49:42 +09002366func TestApexInVariousPartition(t *testing.T) {
2367 testcases := []struct {
2368 propName, parition, flattenedPartition string
2369 }{
2370 {"", "system", "system_ext"},
2371 {"product_specific: true", "product", "product"},
2372 {"soc_specific: true", "vendor", "vendor"},
2373 {"proprietary: true", "vendor", "vendor"},
2374 {"vendor: true", "vendor", "vendor"},
2375 {"system_ext_specific: true", "system_ext", "system_ext"},
2376 }
2377 for _, tc := range testcases {
2378 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2379 ctx, _ := testApex(t, `
2380 apex {
2381 name: "myapex",
2382 key: "myapex.key",
2383 `+tc.propName+`
2384 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002385
Jooyung Han91df2082019-11-20 01:49:42 +09002386 apex_key {
2387 name: "myapex.key",
2388 public_key: "testkey.avbpubkey",
2389 private_key: "testkey.pem",
2390 }
2391 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002392
Jooyung Han91df2082019-11-20 01:49:42 +09002393 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2394 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2395 actual := apex.installDir.String()
2396 if actual != expected {
2397 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2398 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002399
Jooyung Han91df2082019-11-20 01:49:42 +09002400 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2401 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2402 actual = flattened.installDir.String()
2403 if actual != expected {
2404 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2405 }
2406 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002407 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002408}
Jiyong Park67882562019-03-21 01:11:21 +09002409
Jooyung Han54aca7b2019-11-20 02:26:02 +09002410func TestFileContexts(t *testing.T) {
2411 ctx, _ := testApex(t, `
2412 apex {
2413 name: "myapex",
2414 key: "myapex.key",
2415 }
2416
2417 apex_key {
2418 name: "myapex.key",
2419 public_key: "testkey.avbpubkey",
2420 private_key: "testkey.pem",
2421 }
2422 `)
2423 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2424 apexRule := module.Rule("apexRule")
2425 actual := apexRule.Args["file_contexts"]
2426 expected := "system/sepolicy/apex/myapex-file_contexts"
2427 if actual != expected {
2428 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2429 }
2430
2431 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2432 apex {
2433 name: "myapex",
2434 key: "myapex.key",
2435 file_contexts: "my_own_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 "my_own_file_contexts": nil,
2445 }))
2446
2447 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2448 apex {
2449 name: "myapex",
2450 key: "myapex.key",
2451 product_specific: true,
2452 file_contexts: "product_specific_file_contexts",
2453 }
2454
2455 apex_key {
2456 name: "myapex.key",
2457 public_key: "testkey.avbpubkey",
2458 private_key: "testkey.pem",
2459 }
2460 `)
2461
2462 ctx, _ = testApex(t, `
2463 apex {
2464 name: "myapex",
2465 key: "myapex.key",
2466 product_specific: true,
2467 file_contexts: "product_specific_file_contexts",
2468 }
2469
2470 apex_key {
2471 name: "myapex.key",
2472 public_key: "testkey.avbpubkey",
2473 private_key: "testkey.pem",
2474 }
2475 `, withFiles(map[string][]byte{
2476 "product_specific_file_contexts": nil,
2477 }))
2478 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2479 apexRule = module.Rule("apexRule")
2480 actual = apexRule.Args["file_contexts"]
2481 expected = "product_specific_file_contexts"
2482 if actual != expected {
2483 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2484 }
2485
2486 ctx, _ = testApex(t, `
2487 apex {
2488 name: "myapex",
2489 key: "myapex.key",
2490 product_specific: true,
2491 file_contexts: ":my-file-contexts",
2492 }
2493
2494 apex_key {
2495 name: "myapex.key",
2496 public_key: "testkey.avbpubkey",
2497 private_key: "testkey.pem",
2498 }
2499
2500 filegroup {
2501 name: "my-file-contexts",
2502 srcs: ["product_specific_file_contexts"],
2503 }
2504 `, withFiles(map[string][]byte{
2505 "product_specific_file_contexts": nil,
2506 }))
2507 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2508 apexRule = module.Rule("apexRule")
2509 actual = apexRule.Args["file_contexts"]
2510 expected = "product_specific_file_contexts"
2511 if actual != expected {
2512 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2513 }
2514}
2515
Jiyong Park67882562019-03-21 01:11:21 +09002516func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002517 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002518 apex_key {
2519 name: "myapex.key",
2520 public_key: ":my.avbpubkey",
2521 private_key: ":my.pem",
2522 product_specific: true,
2523 }
2524
2525 filegroup {
2526 name: "my.avbpubkey",
2527 srcs: ["testkey2.avbpubkey"],
2528 }
2529
2530 filegroup {
2531 name: "my.pem",
2532 srcs: ["testkey2.pem"],
2533 }
2534 `)
2535
2536 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2537 expected_pubkey := "testkey2.avbpubkey"
2538 actual_pubkey := apex_key.public_key_file.String()
2539 if actual_pubkey != expected_pubkey {
2540 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2541 }
2542 expected_privkey := "testkey2.pem"
2543 actual_privkey := apex_key.private_key_file.String()
2544 if actual_privkey != expected_privkey {
2545 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2546 }
2547}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002548
2549func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002550 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002551 prebuilt_apex {
2552 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002553 arch: {
2554 arm64: {
2555 src: "myapex-arm64.apex",
2556 },
2557 arm: {
2558 src: "myapex-arm.apex",
2559 },
2560 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002561 }
2562 `)
2563
2564 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2565
Jiyong Parkc95714e2019-03-29 14:23:10 +09002566 expectedInput := "myapex-arm64.apex"
2567 if prebuilt.inputApex.String() != expectedInput {
2568 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2569 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002570}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002571
2572func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002573 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002574 prebuilt_apex {
2575 name: "myapex",
2576 src: "myapex-arm.apex",
2577 filename: "notmyapex.apex",
2578 }
2579 `)
2580
2581 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2582
2583 expected := "notmyapex.apex"
2584 if p.installFilename != expected {
2585 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2586 }
2587}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002588
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002589func TestPrebuiltOverrides(t *testing.T) {
2590 ctx, config := testApex(t, `
2591 prebuilt_apex {
2592 name: "myapex.prebuilt",
2593 src: "myapex-arm.apex",
2594 overrides: [
2595 "myapex",
2596 ],
2597 }
2598 `)
2599
2600 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2601
2602 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002603 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002604 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002605 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002606 }
2607}
2608
Roland Levillain630846d2019-06-26 12:48:34 +01002609func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002610 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002611 apex_test {
2612 name: "myapex",
2613 key: "myapex.key",
2614 tests: [
2615 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002616 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002617 ],
2618 }
2619
2620 apex_key {
2621 name: "myapex.key",
2622 public_key: "testkey.avbpubkey",
2623 private_key: "testkey.pem",
2624 }
2625
2626 cc_test {
2627 name: "mytest",
2628 gtest: false,
2629 srcs: ["mytest.cpp"],
2630 relative_install_path: "test",
2631 system_shared_libs: [],
2632 static_executable: true,
2633 stl: "none",
2634 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002635
2636 cc_test {
2637 name: "mytests",
2638 gtest: false,
2639 srcs: [
2640 "mytest1.cpp",
2641 "mytest2.cpp",
2642 "mytest3.cpp",
2643 ],
2644 test_per_src: true,
2645 relative_install_path: "test",
2646 system_shared_libs: [],
2647 static_executable: true,
2648 stl: "none",
2649 }
Roland Levillain630846d2019-06-26 12:48:34 +01002650 `)
2651
Sundong Ahnabb64432019-10-22 13:58:29 +09002652 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002653 copyCmds := apexRule.Args["copy_commands"]
2654
2655 // Ensure that test dep is copied into apex.
2656 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002657
2658 // Ensure that test deps built with `test_per_src` are copied into apex.
2659 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2660 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2661 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002662
2663 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002664 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002665 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2666 name := apexBundle.BaseModuleName()
2667 prefix := "TARGET_"
2668 var builder strings.Builder
2669 data.Custom(&builder, name, prefix, "", data)
2670 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002671 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2672 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2673 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2674 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002675 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002676 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002677 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002678}
2679
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002680func TestInstallExtraFlattenedApexes(t *testing.T) {
2681 ctx, config := testApex(t, `
2682 apex {
2683 name: "myapex",
2684 key: "myapex.key",
2685 }
2686 apex_key {
2687 name: "myapex.key",
2688 public_key: "testkey.avbpubkey",
2689 private_key: "testkey.pem",
2690 }
2691 `, func(fs map[string][]byte, config android.Config) {
2692 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2693 })
2694 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002695 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002696 mk := android.AndroidMkDataForTest(t, config, "", ab)
2697 var builder strings.Builder
2698 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2699 androidMk := builder.String()
2700 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2701}
2702
Jooyung Han5c998b92019-06-27 11:30:33 +09002703func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002704 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002705 apex {
2706 name: "myapex",
2707 key: "myapex.key",
2708 native_shared_libs: ["mylib"],
2709 uses: ["commonapex"],
2710 }
2711
2712 apex {
2713 name: "commonapex",
2714 key: "myapex.key",
2715 native_shared_libs: ["libcommon"],
2716 provide_cpp_shared_libs: true,
2717 }
2718
2719 apex_key {
2720 name: "myapex.key",
2721 public_key: "testkey.avbpubkey",
2722 private_key: "testkey.pem",
2723 }
2724
2725 cc_library {
2726 name: "mylib",
2727 srcs: ["mylib.cpp"],
2728 shared_libs: ["libcommon"],
2729 system_shared_libs: [],
2730 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002731 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002732 }
2733
2734 cc_library {
2735 name: "libcommon",
2736 srcs: ["mylib_common.cpp"],
2737 system_shared_libs: [],
2738 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002739 // TODO: remove //apex_available:platform
2740 apex_available: [
2741 "//apex_available:platform",
2742 "commonapex",
2743 "myapex",
2744 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002745 }
2746 `)
2747
Sundong Ahnabb64432019-10-22 13:58:29 +09002748 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002749 apexRule1 := module1.Rule("apexRule")
2750 copyCmds1 := apexRule1.Args["copy_commands"]
2751
Sundong Ahnabb64432019-10-22 13:58:29 +09002752 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002753 apexRule2 := module2.Rule("apexRule")
2754 copyCmds2 := apexRule2.Args["copy_commands"]
2755
Colin Cross7113d202019-11-20 16:39:12 -08002756 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2757 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002758 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2759 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2760 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2761}
2762
2763func TestApexUsesFailsIfNotProvided(t *testing.T) {
2764 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2765 apex {
2766 name: "myapex",
2767 key: "myapex.key",
2768 uses: ["commonapex"],
2769 }
2770
2771 apex {
2772 name: "commonapex",
2773 key: "myapex.key",
2774 }
2775
2776 apex_key {
2777 name: "myapex.key",
2778 public_key: "testkey.avbpubkey",
2779 private_key: "testkey.pem",
2780 }
2781 `)
2782 testApexError(t, `uses: "commonapex" is not a provider`, `
2783 apex {
2784 name: "myapex",
2785 key: "myapex.key",
2786 uses: ["commonapex"],
2787 }
2788
2789 cc_library {
2790 name: "commonapex",
2791 system_shared_libs: [],
2792 stl: "none",
2793 }
2794
2795 apex_key {
2796 name: "myapex.key",
2797 public_key: "testkey.avbpubkey",
2798 private_key: "testkey.pem",
2799 }
2800 `)
2801}
2802
2803func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2804 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2805 apex {
2806 name: "myapex",
2807 key: "myapex.key",
2808 use_vendor: true,
2809 uses: ["commonapex"],
2810 }
2811
2812 apex {
2813 name: "commonapex",
2814 key: "myapex.key",
2815 provide_cpp_shared_libs: true,
2816 }
2817
2818 apex_key {
2819 name: "myapex.key",
2820 public_key: "testkey.avbpubkey",
2821 private_key: "testkey.pem",
2822 }
Jooyung Handc782442019-11-01 03:14:38 +09002823 `, func(fs map[string][]byte, config android.Config) {
2824 setUseVendorWhitelistForTest(config, []string{"myapex"})
2825 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002826}
2827
Jooyung Hand48f3c32019-08-23 11:18:57 +09002828func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2829 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2830 apex {
2831 name: "myapex",
2832 key: "myapex.key",
2833 native_shared_libs: ["libfoo"],
2834 }
2835
2836 apex_key {
2837 name: "myapex.key",
2838 public_key: "testkey.avbpubkey",
2839 private_key: "testkey.pem",
2840 }
2841
2842 cc_library {
2843 name: "libfoo",
2844 stl: "none",
2845 system_shared_libs: [],
2846 enabled: false,
2847 }
2848 `)
2849 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2850 apex {
2851 name: "myapex",
2852 key: "myapex.key",
2853 java_libs: ["myjar"],
2854 }
2855
2856 apex_key {
2857 name: "myapex.key",
2858 public_key: "testkey.avbpubkey",
2859 private_key: "testkey.pem",
2860 }
2861
2862 java_library {
2863 name: "myjar",
2864 srcs: ["foo/bar/MyClass.java"],
2865 sdk_version: "none",
2866 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09002867 enabled: false,
2868 }
2869 `)
2870}
2871
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002872func TestApexWithApps(t *testing.T) {
2873 ctx, _ := testApex(t, `
2874 apex {
2875 name: "myapex",
2876 key: "myapex.key",
2877 apps: [
2878 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002879 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002880 ],
2881 }
2882
2883 apex_key {
2884 name: "myapex.key",
2885 public_key: "testkey.avbpubkey",
2886 private_key: "testkey.pem",
2887 }
2888
2889 android_app {
2890 name: "AppFoo",
2891 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08002892 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002893 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002894 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08002895 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002896 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002897 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002898
2899 android_app {
2900 name: "AppFooPriv",
2901 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08002902 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09002903 system_modules: "none",
2904 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08002905 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002906 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09002907 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002908
2909 cc_library_shared {
2910 name: "libjni",
2911 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002912 shared_libs: ["libfoo"],
2913 stl: "none",
2914 system_shared_libs: [],
2915 apex_available: [ "myapex" ],
2916 sdk_version: "current",
2917 }
2918
2919 cc_library_shared {
2920 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09002921 stl: "none",
2922 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09002923 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08002924 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09002925 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002926 `)
2927
Sundong Ahnabb64432019-10-22 13:58:29 +09002928 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002929 apexRule := module.Rule("apexRule")
2930 copyCmds := apexRule.Args["copy_commands"]
2931
2932 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002933 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002934
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002935 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni libs")
2936 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09002937 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002938 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002939 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09002940 // JNI libraries including transitive deps are
2941 for _, jni := range []string{"libjni", "libfoo"} {
2942 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
2943 // ... embedded inside APK (jnilibs.zip)
2944 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
2945 // ... and not directly inside the APEX
2946 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
2947 }
Dario Frenicde2a032019-10-27 00:29:22 +01002948}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002949
Dario Frenicde2a032019-10-27 00:29:22 +01002950func TestApexWithAppImports(t *testing.T) {
2951 ctx, _ := testApex(t, `
2952 apex {
2953 name: "myapex",
2954 key: "myapex.key",
2955 apps: [
2956 "AppFooPrebuilt",
2957 "AppFooPrivPrebuilt",
2958 ],
2959 }
2960
2961 apex_key {
2962 name: "myapex.key",
2963 public_key: "testkey.avbpubkey",
2964 private_key: "testkey.pem",
2965 }
2966
2967 android_app_import {
2968 name: "AppFooPrebuilt",
2969 apk: "PrebuiltAppFoo.apk",
2970 presigned: true,
2971 dex_preopt: {
2972 enabled: false,
2973 },
2974 }
2975
2976 android_app_import {
2977 name: "AppFooPrivPrebuilt",
2978 apk: "PrebuiltAppFooPriv.apk",
2979 privileged: true,
2980 presigned: true,
2981 dex_preopt: {
2982 enabled: false,
2983 },
2984 }
2985 `)
2986
Sundong Ahnabb64432019-10-22 13:58:29 +09002987 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002988 apexRule := module.Rule("apexRule")
2989 copyCmds := apexRule.Args["copy_commands"]
2990
2991 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2992 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002993}
2994
Dario Freni6f3937c2019-12-20 22:58:03 +00002995func TestApexWithTestHelperApp(t *testing.T) {
2996 ctx, _ := testApex(t, `
2997 apex {
2998 name: "myapex",
2999 key: "myapex.key",
3000 apps: [
3001 "TesterHelpAppFoo",
3002 ],
3003 }
3004
3005 apex_key {
3006 name: "myapex.key",
3007 public_key: "testkey.avbpubkey",
3008 private_key: "testkey.pem",
3009 }
3010
3011 android_test_helper_app {
3012 name: "TesterHelpAppFoo",
3013 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003014 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003015 }
3016
3017 `)
3018
3019 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3020 apexRule := module.Rule("apexRule")
3021 copyCmds := apexRule.Args["copy_commands"]
3022
3023 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3024}
3025
Jooyung Han18020ea2019-11-13 10:50:48 +09003026func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3027 // libfoo's apex_available comes from cc_defaults
3028 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
3029 apex {
3030 name: "myapex",
3031 key: "myapex.key",
3032 native_shared_libs: ["libfoo"],
3033 }
3034
3035 apex_key {
3036 name: "myapex.key",
3037 public_key: "testkey.avbpubkey",
3038 private_key: "testkey.pem",
3039 }
3040
3041 apex {
3042 name: "otherapex",
3043 key: "myapex.key",
3044 native_shared_libs: ["libfoo"],
3045 }
3046
3047 cc_defaults {
3048 name: "libfoo-defaults",
3049 apex_available: ["otherapex"],
3050 }
3051
3052 cc_library {
3053 name: "libfoo",
3054 defaults: ["libfoo-defaults"],
3055 stl: "none",
3056 system_shared_libs: [],
3057 }`)
3058}
3059
Jiyong Park127b40b2019-09-30 16:04:35 +09003060func TestApexAvailable(t *testing.T) {
3061 // libfoo is not available to myapex, but only to otherapex
3062 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3063 apex {
3064 name: "myapex",
3065 key: "myapex.key",
3066 native_shared_libs: ["libfoo"],
3067 }
3068
3069 apex_key {
3070 name: "myapex.key",
3071 public_key: "testkey.avbpubkey",
3072 private_key: "testkey.pem",
3073 }
3074
3075 apex {
3076 name: "otherapex",
3077 key: "otherapex.key",
3078 native_shared_libs: ["libfoo"],
3079 }
3080
3081 apex_key {
3082 name: "otherapex.key",
3083 public_key: "testkey.avbpubkey",
3084 private_key: "testkey.pem",
3085 }
3086
3087 cc_library {
3088 name: "libfoo",
3089 stl: "none",
3090 system_shared_libs: [],
3091 apex_available: ["otherapex"],
3092 }`)
3093
3094 // libbar is an indirect dep
3095 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3096 apex {
3097 name: "myapex",
3098 key: "myapex.key",
3099 native_shared_libs: ["libfoo"],
3100 }
3101
3102 apex_key {
3103 name: "myapex.key",
3104 public_key: "testkey.avbpubkey",
3105 private_key: "testkey.pem",
3106 }
3107
3108 apex {
3109 name: "otherapex",
3110 key: "otherapex.key",
3111 native_shared_libs: ["libfoo"],
3112 }
3113
3114 apex_key {
3115 name: "otherapex.key",
3116 public_key: "testkey.avbpubkey",
3117 private_key: "testkey.pem",
3118 }
3119
3120 cc_library {
3121 name: "libfoo",
3122 stl: "none",
3123 shared_libs: ["libbar"],
3124 system_shared_libs: [],
3125 apex_available: ["myapex", "otherapex"],
3126 }
3127
3128 cc_library {
3129 name: "libbar",
3130 stl: "none",
3131 system_shared_libs: [],
3132 apex_available: ["otherapex"],
3133 }`)
3134
3135 testApexError(t, "\"otherapex\" is not a valid module name", `
3136 apex {
3137 name: "myapex",
3138 key: "myapex.key",
3139 native_shared_libs: ["libfoo"],
3140 }
3141
3142 apex_key {
3143 name: "myapex.key",
3144 public_key: "testkey.avbpubkey",
3145 private_key: "testkey.pem",
3146 }
3147
3148 cc_library {
3149 name: "libfoo",
3150 stl: "none",
3151 system_shared_libs: [],
3152 apex_available: ["otherapex"],
3153 }`)
3154
3155 ctx, _ := testApex(t, `
3156 apex {
3157 name: "myapex",
3158 key: "myapex.key",
3159 native_shared_libs: ["libfoo", "libbar"],
3160 }
3161
3162 apex_key {
3163 name: "myapex.key",
3164 public_key: "testkey.avbpubkey",
3165 private_key: "testkey.pem",
3166 }
3167
3168 cc_library {
3169 name: "libfoo",
3170 stl: "none",
3171 system_shared_libs: [],
3172 apex_available: ["myapex"],
3173 }
3174
3175 cc_library {
3176 name: "libbar",
3177 stl: "none",
3178 system_shared_libs: [],
3179 apex_available: ["//apex_available:anyapex"],
3180 }`)
3181
3182 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003183 // TODO(jiyong) the checks for the platform variant are removed because we now create
3184 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3185 // the platform variants are not used from other platform modules. When that is done,
3186 // these checks will be replaced by expecting a specific error message that will be
3187 // emitted when the platform variant is used.
3188 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3189 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3190 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3191 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003192
3193 ctx, _ = testApex(t, `
3194 apex {
3195 name: "myapex",
3196 key: "myapex.key",
3197 }
3198
3199 apex_key {
3200 name: "myapex.key",
3201 public_key: "testkey.avbpubkey",
3202 private_key: "testkey.pem",
3203 }
3204
3205 cc_library {
3206 name: "libfoo",
3207 stl: "none",
3208 system_shared_libs: [],
3209 apex_available: ["//apex_available:platform"],
3210 }`)
3211
3212 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003213 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3214 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003215
3216 ctx, _ = testApex(t, `
3217 apex {
3218 name: "myapex",
3219 key: "myapex.key",
3220 native_shared_libs: ["libfoo"],
3221 }
3222
3223 apex_key {
3224 name: "myapex.key",
3225 public_key: "testkey.avbpubkey",
3226 private_key: "testkey.pem",
3227 }
3228
3229 cc_library {
3230 name: "libfoo",
3231 stl: "none",
3232 system_shared_libs: [],
3233 apex_available: ["myapex"],
3234 static: {
3235 apex_available: ["//apex_available:platform"],
3236 },
3237 }`)
3238
3239 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003240 // TODO(jiyong) the checks for the platform variant are removed because we now create
3241 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3242 // the platform variants are not used from other platform modules. When that is done,
3243 // these checks will be replaced by expecting a specific error message that will be
3244 // emitted when the platform variant is used.
3245 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3246 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3247 // // but the static variant is available to both myapex and the platform
3248 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3249 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003250}
3251
Jiyong Park5d790c32019-11-15 18:40:32 +09003252func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003253 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003254 apex {
3255 name: "myapex",
3256 key: "myapex.key",
3257 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003258 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003259 }
3260
3261 override_apex {
3262 name: "override_myapex",
3263 base: "myapex",
3264 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003265 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08003266 logging_parent: "com.foo.bar",
Jiyong Park5d790c32019-11-15 18:40:32 +09003267 }
3268
3269 apex_key {
3270 name: "myapex.key",
3271 public_key: "testkey.avbpubkey",
3272 private_key: "testkey.pem",
3273 }
3274
3275 android_app {
3276 name: "app",
3277 srcs: ["foo/bar/MyClass.java"],
3278 package_name: "foo",
3279 sdk_version: "none",
3280 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003281 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003282 }
3283
3284 override_android_app {
3285 name: "override_app",
3286 base: "app",
3287 package_name: "bar",
3288 }
3289 `)
3290
Jiyong Park317645e2019-12-05 13:20:58 +09003291 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3292 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3293 if originalVariant.GetOverriddenBy() != "" {
3294 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3295 }
3296 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3297 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3298 }
3299
Jiyong Park5d790c32019-11-15 18:40:32 +09003300 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3301 apexRule := module.Rule("apexRule")
3302 copyCmds := apexRule.Args["copy_commands"]
3303
3304 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3305 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003306
3307 apexBundle := module.Module().(*apexBundle)
3308 name := apexBundle.Name()
3309 if name != "override_myapex" {
3310 t.Errorf("name should be \"override_myapex\", but was %q", name)
3311 }
3312
Baligh Uddin004d7172020-02-19 21:29:28 -08003313 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
3314 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
3315 }
3316
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003317 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3318 var builder strings.Builder
3319 data.Custom(&builder, name, "TARGET_", "", data)
3320 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003321 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003322 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3323 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003324 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003325 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003326 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003327 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3328 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003329}
3330
Jooyung Han214bf372019-11-12 13:03:50 +09003331func TestLegacyAndroid10Support(t *testing.T) {
3332 ctx, _ := testApex(t, `
3333 apex {
3334 name: "myapex",
3335 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003336 native_shared_libs: ["mylib"],
Jooyung Han214bf372019-11-12 13:03:50 +09003337 legacy_android10_support: true,
3338 }
3339
3340 apex_key {
3341 name: "myapex.key",
3342 public_key: "testkey.avbpubkey",
3343 private_key: "testkey.pem",
3344 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003345
3346 cc_library {
3347 name: "mylib",
3348 srcs: ["mylib.cpp"],
3349 stl: "libc++",
3350 system_shared_libs: [],
3351 apex_available: [ "myapex" ],
3352 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003353 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09003354
3355 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3356 args := module.Rule("apexRule").Args
3357 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003358 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08003359
3360 // The copies of the libraries in the apex should have one more dependency than
3361 // the ones outside the apex, namely the unwinder. Ideally we should check
3362 // the dependency names directly here but for some reason the names are blank in
3363 // this test.
3364 for _, lib := range []string{"libc++", "mylib"} {
3365 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_myapex").Rule("ld").Implicits
3366 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
3367 if len(apexImplicits) != len(nonApexImplicits)+1 {
3368 t.Errorf("%q missing unwinder dep", lib)
3369 }
3370 }
Jooyung Han214bf372019-11-12 13:03:50 +09003371}
3372
Jooyung Han58f26ab2019-12-18 15:34:32 +09003373func TestJavaSDKLibrary(t *testing.T) {
3374 ctx, _ := testApex(t, `
3375 apex {
3376 name: "myapex",
3377 key: "myapex.key",
3378 java_libs: ["foo"],
3379 }
3380
3381 apex_key {
3382 name: "myapex.key",
3383 public_key: "testkey.avbpubkey",
3384 private_key: "testkey.pem",
3385 }
3386
3387 java_sdk_library {
3388 name: "foo",
3389 srcs: ["a.java"],
3390 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003391 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003392 }
3393 `, withFiles(map[string][]byte{
3394 "api/current.txt": nil,
3395 "api/removed.txt": nil,
3396 "api/system-current.txt": nil,
3397 "api/system-removed.txt": nil,
3398 "api/test-current.txt": nil,
3399 "api/test-removed.txt": nil,
3400 }))
3401
3402 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003403 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003404 "javalib/foo.jar",
3405 "etc/permissions/foo.xml",
3406 })
3407 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09003408 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
3409 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003410}
3411
atrost6e126252020-01-27 17:01:16 +00003412func TestCompatConfig(t *testing.T) {
3413 ctx, _ := testApex(t, `
3414 apex {
3415 name: "myapex",
3416 key: "myapex.key",
3417 prebuilts: ["myjar-platform-compat-config"],
3418 java_libs: ["myjar"],
3419 }
3420
3421 apex_key {
3422 name: "myapex.key",
3423 public_key: "testkey.avbpubkey",
3424 private_key: "testkey.pem",
3425 }
3426
3427 platform_compat_config {
3428 name: "myjar-platform-compat-config",
3429 src: ":myjar",
3430 }
3431
3432 java_library {
3433 name: "myjar",
3434 srcs: ["foo/bar/MyClass.java"],
3435 sdk_version: "none",
3436 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003437 apex_available: [ "myapex" ],
3438 }
3439 `)
3440 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3441 "etc/compatconfig/myjar-platform-compat-config.xml",
3442 "javalib/myjar.jar",
3443 })
3444}
3445
Jiyong Park479321d2019-12-16 11:47:12 +09003446func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3447 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3448 apex {
3449 name: "myapex",
3450 key: "myapex.key",
3451 java_libs: ["myjar"],
3452 }
3453
3454 apex_key {
3455 name: "myapex.key",
3456 public_key: "testkey.avbpubkey",
3457 private_key: "testkey.pem",
3458 }
3459
3460 java_library {
3461 name: "myjar",
3462 srcs: ["foo/bar/MyClass.java"],
3463 sdk_version: "none",
3464 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003465 compile_dex: false,
Jiyong Park479321d2019-12-16 11:47:12 +09003466 }
3467 `)
3468}
3469
Jiyong Park7afd1072019-12-30 16:56:33 +09003470func TestCarryRequiredModuleNames(t *testing.T) {
3471 ctx, config := testApex(t, `
3472 apex {
3473 name: "myapex",
3474 key: "myapex.key",
3475 native_shared_libs: ["mylib"],
3476 }
3477
3478 apex_key {
3479 name: "myapex.key",
3480 public_key: "testkey.avbpubkey",
3481 private_key: "testkey.pem",
3482 }
3483
3484 cc_library {
3485 name: "mylib",
3486 srcs: ["mylib.cpp"],
3487 system_shared_libs: [],
3488 stl: "none",
3489 required: ["a", "b"],
3490 host_required: ["c", "d"],
3491 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003492 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003493 }
3494 `)
3495
3496 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3497 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3498 name := apexBundle.BaseModuleName()
3499 prefix := "TARGET_"
3500 var builder strings.Builder
3501 data.Custom(&builder, name, prefix, "", data)
3502 androidMk := builder.String()
3503 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3504 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3505 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3506}
3507
Jiyong Park7cd10e32020-01-14 09:22:18 +09003508func TestSymlinksFromApexToSystem(t *testing.T) {
3509 bp := `
3510 apex {
3511 name: "myapex",
3512 key: "myapex.key",
3513 native_shared_libs: ["mylib"],
3514 java_libs: ["myjar"],
3515 }
3516
Jiyong Park9d677202020-02-19 16:29:35 +09003517 apex {
3518 name: "myapex.updatable",
3519 key: "myapex.key",
3520 native_shared_libs: ["mylib"],
3521 java_libs: ["myjar"],
3522 updatable: true,
3523 }
3524
Jiyong Park7cd10e32020-01-14 09:22:18 +09003525 apex_key {
3526 name: "myapex.key",
3527 public_key: "testkey.avbpubkey",
3528 private_key: "testkey.pem",
3529 }
3530
3531 cc_library {
3532 name: "mylib",
3533 srcs: ["mylib.cpp"],
3534 shared_libs: ["myotherlib"],
3535 system_shared_libs: [],
3536 stl: "none",
3537 apex_available: [
3538 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003539 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003540 "//apex_available:platform",
3541 ],
3542 }
3543
3544 cc_library {
3545 name: "myotherlib",
3546 srcs: ["mylib.cpp"],
3547 system_shared_libs: [],
3548 stl: "none",
3549 apex_available: [
3550 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003551 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003552 "//apex_available:platform",
3553 ],
3554 }
3555
3556 java_library {
3557 name: "myjar",
3558 srcs: ["foo/bar/MyClass.java"],
3559 sdk_version: "none",
3560 system_modules: "none",
3561 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003562 apex_available: [
3563 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003564 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003565 "//apex_available:platform",
3566 ],
3567 }
3568
3569 java_library {
3570 name: "myotherjar",
3571 srcs: ["foo/bar/MyClass.java"],
3572 sdk_version: "none",
3573 system_modules: "none",
3574 apex_available: [
3575 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003576 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003577 "//apex_available:platform",
3578 ],
3579 }
3580 `
3581
3582 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3583 for _, f := range files {
3584 if f.path == file {
3585 if f.isLink {
3586 t.Errorf("%q is not a real file", file)
3587 }
3588 return
3589 }
3590 }
3591 t.Errorf("%q is not found", file)
3592 }
3593
3594 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3595 for _, f := range files {
3596 if f.path == file {
3597 if !f.isLink {
3598 t.Errorf("%q is not a symlink", file)
3599 }
3600 return
3601 }
3602 }
3603 t.Errorf("%q is not found", file)
3604 }
3605
Jiyong Park9d677202020-02-19 16:29:35 +09003606 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
3607 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09003608 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003609 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003610 ensureRealfileExists(t, files, "javalib/myjar.jar")
3611 ensureRealfileExists(t, files, "lib64/mylib.so")
3612 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3613
Jiyong Park9d677202020-02-19 16:29:35 +09003614 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3615 ensureRealfileExists(t, files, "javalib/myjar.jar")
3616 ensureRealfileExists(t, files, "lib64/mylib.so")
3617 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3618
3619 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09003620 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003621 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003622 ensureRealfileExists(t, files, "javalib/myjar.jar")
3623 ensureRealfileExists(t, files, "lib64/mylib.so")
3624 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09003625
3626 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3627 ensureRealfileExists(t, files, "javalib/myjar.jar")
3628 ensureRealfileExists(t, files, "lib64/mylib.so")
3629 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09003630}
3631
Jooyung Han643adc42020-02-27 13:50:06 +09003632func TestApexWithJniLibs(t *testing.T) {
3633 ctx, _ := testApex(t, `
3634 apex {
3635 name: "myapex",
3636 key: "myapex.key",
3637 jni_libs: ["mylib"],
3638 }
3639
3640 apex_key {
3641 name: "myapex.key",
3642 public_key: "testkey.avbpubkey",
3643 private_key: "testkey.pem",
3644 }
3645
3646 cc_library {
3647 name: "mylib",
3648 srcs: ["mylib.cpp"],
3649 shared_libs: ["mylib2"],
3650 system_shared_libs: [],
3651 stl: "none",
3652 apex_available: [ "myapex" ],
3653 }
3654
3655 cc_library {
3656 name: "mylib2",
3657 srcs: ["mylib.cpp"],
3658 system_shared_libs: [],
3659 stl: "none",
3660 apex_available: [ "myapex" ],
3661 }
3662 `)
3663
3664 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
3665 // Notice mylib2.so (transitive dep) is not added as a jni_lib
3666 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
3667 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3668 "lib64/mylib.so",
3669 "lib64/mylib2.so",
3670 })
3671}
3672
3673func TestApexWithJniLibs_Errors(t *testing.T) {
3674 testApexError(t, `jni_libs: "xxx" is not a cc_library`, `
3675 apex {
3676 name: "myapex",
3677 key: "myapex.key",
3678 jni_libs: ["xxx"],
3679 }
3680
3681 apex_key {
3682 name: "myapex.key",
3683 public_key: "testkey.avbpubkey",
3684 private_key: "testkey.pem",
3685 }
3686
3687 prebuilt_etc {
3688 name: "xxx",
3689 src: "xxx",
3690 }
3691 `, withFiles(map[string][]byte{
3692 "xxx": nil,
3693 }))
3694}
3695
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003696func TestMain(m *testing.M) {
3697 run := func() int {
3698 setUp()
3699 defer tearDown()
3700
3701 return m.Run()
3702 }
3703
3704 os.Exit(run())
3705}