blob: 41a59a71331783c153fc0dc6a22a08d8bbd07e31 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Jooyung Han31c470b2019-10-18 16:26:59 +090022 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090023 "strings"
24 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090025
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090030 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090031)
32
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070033var buildDir string
34
Jooyung Hand3639552019-08-09 12:57:43 +090035// names returns name list from white space separated string
36func names(s string) (ns []string) {
37 for _, n := range strings.Split(s, " ") {
38 if len(n) > 0 {
39 ns = append(ns, n)
40 }
41 }
42 return
43}
44
Jooyung Han344d5432019-08-23 11:17:39 +090045func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
46 t.Helper()
47 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090048 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
49 if len(errs) > 0 {
50 android.FailIfNoMatchingErrors(t, pattern, errs)
51 return
52 }
53 _, errs = ctx.PrepareBuildActions(config)
54 if len(errs) > 0 {
55 android.FailIfNoMatchingErrors(t, pattern, errs)
56 return
57 }
58
59 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
60}
61
Jooyung Han344d5432019-08-23 11:17:39 +090062func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
63 t.Helper()
64 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090065 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
66 android.FailIfErrored(t, errs)
67 _, errs = ctx.PrepareBuildActions(config)
68 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070069 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090070}
71
Jooyung Han344d5432019-08-23 11:17:39 +090072type testCustomizer func(fs map[string][]byte, config android.Config)
73
74func withFiles(files map[string][]byte) testCustomizer {
75 return func(fs map[string][]byte, config android.Config) {
76 for k, v := range files {
77 fs[k] = v
78 }
79 }
80}
81
82func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
83 return func(fs map[string][]byte, config android.Config) {
84 for k, v := range targets {
85 config.Targets[k] = v
86 }
87 }
88}
89
Jooyung Han31c470b2019-10-18 16:26:59 +090090func withBinder32bit(fs map[string][]byte, config android.Config) {
91 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
92}
93
Jooyung Han344d5432019-08-23 11:17:39 +090094func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +090095 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +090096
97 bp = bp + `
98 toolchain_library {
99 name: "libcompiler_rt-extras",
100 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900101 vendor_available: true,
102 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900103 }
104
105 toolchain_library {
106 name: "libatomic",
107 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900108 vendor_available: true,
109 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900110 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900111 }
112
113 toolchain_library {
114 name: "libgcc",
115 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900116 vendor_available: true,
117 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900118 }
119
120 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700121 name: "libgcc_stripped",
122 src: "",
123 vendor_available: true,
124 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900125 native_bridge_supported: true,
Yi Kongacee27c2019-03-29 20:05:14 -0700126 }
127
128 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900129 name: "libclang_rt.builtins-aarch64-android",
130 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900131 vendor_available: true,
132 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900133 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900134 }
135
136 toolchain_library {
137 name: "libclang_rt.builtins-arm-android",
138 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900139 vendor_available: true,
140 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900141 native_bridge_supported: true,
142 }
143
144 toolchain_library {
145 name: "libclang_rt.builtins-x86_64-android",
146 src: "",
147 vendor_available: true,
148 recovery_available: true,
149 native_bridge_supported: true,
150 }
151
152 toolchain_library {
153 name: "libclang_rt.builtins-i686-android",
154 src: "",
155 vendor_available: true,
156 recovery_available: true,
157 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900158 }
159
160 cc_object {
161 name: "crtbegin_so",
162 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900163 vendor_available: true,
164 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900165 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900166 }
167
168 cc_object {
169 name: "crtend_so",
170 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900171 vendor_available: true,
172 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900173 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900174 }
175
Alex Light3d673592019-01-18 14:37:31 -0800176 cc_object {
177 name: "crtbegin_static",
178 stl: "none",
179 }
180
181 cc_object {
182 name: "crtend_android",
183 stl: "none",
184 }
185
Jiyong Parkda6eb592018-12-19 17:12:36 +0900186 llndk_library {
187 name: "libc",
188 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900189 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900190 }
191
192 llndk_library {
193 name: "libm",
194 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900195 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900196 }
197
198 llndk_library {
199 name: "libdl",
200 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900201 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900202 }
Jooyung Han54aca7b2019-11-20 02:26:02 +0900203
204 filegroup {
205 name: "myapex-file_contexts",
206 srcs: [
207 "system/sepolicy/apex/myapex-file_contexts",
208 ],
209 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900210 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800211
Dario Frenicde2a032019-10-27 00:29:22 +0100212 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900213
Jooyung Han344d5432019-08-23 11:17:39 +0900214 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900215 "a.java": nil,
216 "PrebuiltAppFoo.apk": nil,
217 "PrebuiltAppFooPriv.apk": nil,
218 "build/make/target/product/security": nil,
219 "apex_manifest.json": nil,
220 "AndroidManifest.xml": nil,
221 "system/sepolicy/apex/myapex-file_contexts": nil,
222 "system/sepolicy/apex/otherapex-file_contexts": nil,
223 "system/sepolicy/apex/commonapex-file_contexts": nil,
224 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800225 "mylib.cpp": nil,
226 "mylib_common.cpp": nil,
227 "mytest.cpp": nil,
228 "mytest1.cpp": nil,
229 "mytest2.cpp": nil,
230 "mytest3.cpp": nil,
231 "myprebuilt": nil,
232 "my_include": nil,
233 "foo/bar/MyClass.java": nil,
234 "prebuilt.jar": nil,
235 "vendor/foo/devkeys/test.x509.pem": nil,
236 "vendor/foo/devkeys/test.pk8": nil,
237 "testkey.x509.pem": nil,
238 "testkey.pk8": nil,
239 "testkey.override.x509.pem": nil,
240 "testkey.override.pk8": nil,
241 "vendor/foo/devkeys/testkey.avbpubkey": nil,
242 "vendor/foo/devkeys/testkey.pem": nil,
243 "NOTICE": nil,
244 "custom_notice": nil,
245 "testkey2.avbpubkey": nil,
246 "testkey2.pem": nil,
247 "myapex-arm64.apex": nil,
248 "myapex-arm.apex": nil,
249 "frameworks/base/api/current.txt": nil,
250 "framework/aidl/a.aidl": nil,
251 "build/make/core/proguard.flags": nil,
252 "build/make/core/proguard_basic_keeps.flags": nil,
253 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900254 }
255
256 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800257 // The fs now needs to be populated before creating the config, call handlers twice
258 // for now, once to get any fs changes, and later after the config was created to
259 // set product variables or targets.
260 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
261 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900262 }
263
Colin Cross98be1bb2019-12-13 20:41:13 -0800264 config := android.TestArchConfig(buildDir, nil, bp, fs)
265 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
266 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
267 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
268 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
269 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
270 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
271
272 for _, handler := range handlers {
273 // The fs now needs to be populated before creating the config, call handlers twice
274 // for now, earlier to get any fs changes, and now after the config was created to
275 // set product variables or targets.
276 tempFS := map[string][]byte{}
277 handler(tempFS, config)
278 }
279
280 ctx := android.NewTestArchContext()
281 ctx.RegisterModuleType("apex", BundleFactory)
282 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
283 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
284 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
285 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
286 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
287 ctx.RegisterModuleType("override_apex", overrideApexFactory)
288
Colin Cross98be1bb2019-12-13 20:41:13 -0800289 ctx.RegisterModuleType("cc_library_shared", cc.LibrarySharedFactory)
290 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
Paul Duffin77980a82019-12-19 16:01:36 +0000291 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800292 ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800293 ctx.RegisterModuleType("cc_defaults", func() android.Module {
294 return cc.DefaultsFactory()
295 })
296 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800297 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
298 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800299 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
300 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800301 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000302 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000303 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000304 java.RegisterAppBuildComponents(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800305
306 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800307 ctx.PreDepsMutators(RegisterPreDepsMutators)
308 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
309 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800310
311 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900312
Jooyung Han5c998b92019-06-27 11:30:33 +0900313 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900314}
315
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700316func setUp() {
317 var err error
318 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900319 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700320 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900321 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900322}
323
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700324func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900325 os.RemoveAll(buildDir)
326}
327
328// ensure that 'result' contains 'expected'
329func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900330 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900331 if !strings.Contains(result, expected) {
332 t.Errorf("%q is not found in %q", expected, result)
333 }
334}
335
336// ensures that 'result' does not contain 'notExpected'
337func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900338 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900339 if strings.Contains(result, notExpected) {
340 t.Errorf("%q is found in %q", notExpected, result)
341 }
342}
343
344func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900345 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900346 if !android.InList(expected, result) {
347 t.Errorf("%q is not found in %v", expected, result)
348 }
349}
350
351func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900352 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353 if android.InList(notExpected, result) {
354 t.Errorf("%q is found in %v", notExpected, result)
355 }
356}
357
Jooyung Hane1633032019-08-01 17:41:43 +0900358func ensureListEmpty(t *testing.T, result []string) {
359 t.Helper()
360 if len(result) > 0 {
361 t.Errorf("%q is expected to be empty", result)
362 }
363}
364
Jiyong Park25fc6a92018-11-18 18:02:45 +0900365// Minimal test
366func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700367 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900368 apex_defaults {
369 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900370 manifest: ":myapex.manifest",
371 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900372 key: "myapex.key",
373 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800374 multilib: {
375 both: {
376 binaries: ["foo",],
377 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900378 },
Jiyong Park9e6c2422019-08-09 20:39:45 +0900379 java_libs: ["myjar", "myprebuiltjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900380 }
381
Jiyong Park30ca9372019-02-07 16:27:23 +0900382 apex {
383 name: "myapex",
384 defaults: ["myapex-defaults"],
385 }
386
Jiyong Park25fc6a92018-11-18 18:02:45 +0900387 apex_key {
388 name: "myapex.key",
389 public_key: "testkey.avbpubkey",
390 private_key: "testkey.pem",
391 }
392
Jiyong Park809bb722019-02-13 21:33:49 +0900393 filegroup {
394 name: "myapex.manifest",
395 srcs: ["apex_manifest.json"],
396 }
397
398 filegroup {
399 name: "myapex.androidmanifest",
400 srcs: ["AndroidManifest.xml"],
401 }
402
Jiyong Park25fc6a92018-11-18 18:02:45 +0900403 cc_library {
404 name: "mylib",
405 srcs: ["mylib.cpp"],
406 shared_libs: ["mylib2"],
407 system_shared_libs: [],
408 stl: "none",
409 }
410
Alex Light3d673592019-01-18 14:37:31 -0800411 cc_binary {
412 name: "foo",
413 srcs: ["mylib.cpp"],
414 compile_multilib: "both",
415 multilib: {
416 lib32: {
417 suffix: "32",
418 },
419 lib64: {
420 suffix: "64",
421 },
422 },
423 symlinks: ["foo_link_"],
424 symlink_preferred_arch: true,
425 system_shared_libs: [],
426 static_executable: true,
427 stl: "none",
428 }
429
Jiyong Park25fc6a92018-11-18 18:02:45 +0900430 cc_library {
431 name: "mylib2",
432 srcs: ["mylib.cpp"],
433 system_shared_libs: [],
434 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900435 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900436 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900437
438 java_library {
439 name: "myjar",
440 srcs: ["foo/bar/MyClass.java"],
441 sdk_version: "none",
442 system_modules: "none",
443 compile_dex: true,
444 static_libs: ["myotherjar"],
445 }
446
447 java_library {
448 name: "myotherjar",
449 srcs: ["foo/bar/MyClass.java"],
450 sdk_version: "none",
451 system_modules: "none",
452 compile_dex: true,
453 }
Jiyong Park9e6c2422019-08-09 20:39:45 +0900454
455 java_import {
456 name: "myprebuiltjar",
457 jars: ["prebuilt.jar"],
458 installable: true,
459 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900460 `)
461
Sundong Ahnabb64432019-10-22 13:58:29 +0900462 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900463
464 optFlags := apexRule.Args["opt_flags"]
465 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700466 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900467 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900468
Jiyong Park25fc6a92018-11-18 18:02:45 +0900469 copyCmds := apexRule.Args["copy_commands"]
470
471 // Ensure that main rule creates an output
472 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
473
474 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800475 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900476 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900477 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900478
479 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800480 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900481 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900482
483 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800484 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
485 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900486 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900487 ensureContains(t, copyCmds, "image.apex/javalib/myprebuiltjar.jar")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900488 // .. but not for java libs
489 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800490
Colin Cross7113d202019-11-20 16:39:12 -0800491 // Ensure that the platform variant ends with _shared or _common
492 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
493 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900494 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
495 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900496 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common")
Alex Light3d673592019-01-18 14:37:31 -0800497
498 // Ensure that all symlinks are present.
499 found_foo_link_64 := false
500 found_foo := false
501 for _, cmd := range strings.Split(copyCmds, " && ") {
502 if strings.HasPrefix(cmd, "ln -s foo64") {
503 if strings.HasSuffix(cmd, "bin/foo") {
504 found_foo = true
505 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
506 found_foo_link_64 = true
507 }
508 }
509 }
510 good := found_foo && found_foo_link_64
511 if !good {
512 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
513 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900514
Sundong Ahnabb64432019-10-22 13:58:29 +0900515 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700516 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700517 if len(noticeInputs) != 2 {
518 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900519 }
520 ensureListContains(t, noticeInputs, "NOTICE")
521 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800522}
523
Jooyung Hanf21c7972019-12-16 22:32:06 +0900524func TestDefaults(t *testing.T) {
525 ctx, _ := testApex(t, `
526 apex_defaults {
527 name: "myapex-defaults",
528 key: "myapex.key",
529 prebuilts: ["myetc"],
530 native_shared_libs: ["mylib"],
531 java_libs: ["myjar"],
532 apps: ["AppFoo"],
533 }
534
535 prebuilt_etc {
536 name: "myetc",
537 src: "myprebuilt",
538 }
539
540 apex {
541 name: "myapex",
542 defaults: ["myapex-defaults"],
543 }
544
545 apex_key {
546 name: "myapex.key",
547 public_key: "testkey.avbpubkey",
548 private_key: "testkey.pem",
549 }
550
551 cc_library {
552 name: "mylib",
553 system_shared_libs: [],
554 stl: "none",
555 }
556
557 java_library {
558 name: "myjar",
559 srcs: ["foo/bar/MyClass.java"],
560 sdk_version: "none",
561 system_modules: "none",
562 compile_dex: true,
563 }
564
565 android_app {
566 name: "AppFoo",
567 srcs: ["foo/bar/MyClass.java"],
568 sdk_version: "none",
569 system_modules: "none",
570 }
571 `)
572 ensureExactContents(t, ctx, "myapex", []string{
573 "etc/myetc",
574 "javalib/myjar.jar",
575 "lib64/mylib.so",
576 "app/AppFoo/AppFoo.apk",
577 })
578}
579
Jooyung Han01a3ee22019-11-02 02:52:25 +0900580func TestApexManifest(t *testing.T) {
581 ctx, _ := testApex(t, `
582 apex {
583 name: "myapex",
584 key: "myapex.key",
585 }
586
587 apex_key {
588 name: "myapex.key",
589 public_key: "testkey.avbpubkey",
590 private_key: "testkey.pem",
591 }
592 `)
593
594 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900595 args := module.Rule("apexRule").Args
596 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
597 t.Error("manifest should be apex_manifest.pb, but " + manifest)
598 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900599}
600
Alex Light5098a612018-11-29 17:12:15 -0800601func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700602 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800603 apex {
604 name: "myapex",
605 key: "myapex.key",
606 payload_type: "zip",
607 native_shared_libs: ["mylib"],
608 }
609
610 apex_key {
611 name: "myapex.key",
612 public_key: "testkey.avbpubkey",
613 private_key: "testkey.pem",
614 }
615
616 cc_library {
617 name: "mylib",
618 srcs: ["mylib.cpp"],
619 shared_libs: ["mylib2"],
620 system_shared_libs: [],
621 stl: "none",
622 }
623
624 cc_library {
625 name: "mylib2",
626 srcs: ["mylib.cpp"],
627 system_shared_libs: [],
628 stl: "none",
629 }
630 `)
631
Sundong Ahnabb64432019-10-22 13:58:29 +0900632 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800633 copyCmds := zipApexRule.Args["copy_commands"]
634
635 // Ensure that main rule creates an output
636 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
637
638 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800639 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800640
641 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800642 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800643
644 // Ensure that both direct and indirect deps are copied into apex
645 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
646 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900647}
648
649func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700650 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900651 apex {
652 name: "myapex",
653 key: "myapex.key",
654 native_shared_libs: ["mylib", "mylib3"],
655 }
656
657 apex_key {
658 name: "myapex.key",
659 public_key: "testkey.avbpubkey",
660 private_key: "testkey.pem",
661 }
662
663 cc_library {
664 name: "mylib",
665 srcs: ["mylib.cpp"],
666 shared_libs: ["mylib2", "mylib3"],
667 system_shared_libs: [],
668 stl: "none",
669 }
670
671 cc_library {
672 name: "mylib2",
673 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900674 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900675 system_shared_libs: [],
676 stl: "none",
677 stubs: {
678 versions: ["1", "2", "3"],
679 },
680 }
681
682 cc_library {
683 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900684 srcs: ["mylib.cpp"],
685 shared_libs: ["mylib4"],
686 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900687 stl: "none",
688 stubs: {
689 versions: ["10", "11", "12"],
690 },
691 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900692
693 cc_library {
694 name: "mylib4",
695 srcs: ["mylib.cpp"],
696 system_shared_libs: [],
697 stl: "none",
698 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900699 `)
700
Sundong Ahnabb64432019-10-22 13:58:29 +0900701 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900702 copyCmds := apexRule.Args["copy_commands"]
703
704 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800705 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900706
707 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800708 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900709
710 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800711 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900712
Colin Cross7113d202019-11-20 16:39:12 -0800713 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900714
715 // Ensure that mylib is linking with the latest version of stubs for mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800716 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900717 // ... and not linking to the non-stub (impl) variant of mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800718 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900719
720 // 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 -0800721 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900722 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800723 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900724
725 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800726 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900727 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900728
729 // Ensure that genstub is invoked with --apex
Colin Cross7113d202019-11-20 16:39:12 -0800730 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3_myapex").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900731
732 ensureExactContents(t, ctx, "myapex", []string{
733 "lib64/mylib.so",
734 "lib64/mylib3.so",
735 "lib64/mylib4.so",
736 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900737}
738
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900739func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700740 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900741 apex {
742 name: "myapex",
743 key: "myapex.key",
744 native_shared_libs: ["mylib"],
745 }
746
747 apex_key {
748 name: "myapex.key",
749 public_key: "testkey.avbpubkey",
750 private_key: "testkey.pem",
751 }
752
753 cc_library {
754 name: "mylib",
755 srcs: ["mylib.cpp"],
756 shared_libs: ["libfoo#10"],
757 system_shared_libs: [],
758 stl: "none",
759 }
760
761 cc_library {
762 name: "libfoo",
763 srcs: ["mylib.cpp"],
764 shared_libs: ["libbar"],
765 system_shared_libs: [],
766 stl: "none",
767 stubs: {
768 versions: ["10", "20", "30"],
769 },
770 }
771
772 cc_library {
773 name: "libbar",
774 srcs: ["mylib.cpp"],
775 system_shared_libs: [],
776 stl: "none",
777 }
778
779 `)
780
Sundong Ahnabb64432019-10-22 13:58:29 +0900781 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900782 copyCmds := apexRule.Args["copy_commands"]
783
784 // Ensure that direct non-stubs dep is always included
785 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
786
787 // Ensure that indirect stubs dep is not included
788 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
789
790 // Ensure that dependency of stubs is not included
791 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
792
Colin Cross7113d202019-11-20 16:39:12 -0800793 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900794
795 // Ensure that mylib is linking with version 10 of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800796 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900797 // ... and not linking to the non-stub (impl) variant of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800798 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900799
Colin Cross7113d202019-11-20 16:39:12 -0800800 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900801
802 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
803 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
804}
805
Jooyung Hand3639552019-08-09 12:57:43 +0900806func TestApexWithRuntimeLibsDependency(t *testing.T) {
807 /*
808 myapex
809 |
810 v (runtime_libs)
811 mylib ------+------> libfoo [provides stub]
812 |
813 `------> libbar
814 */
815 ctx, _ := testApex(t, `
816 apex {
817 name: "myapex",
818 key: "myapex.key",
819 native_shared_libs: ["mylib"],
820 }
821
822 apex_key {
823 name: "myapex.key",
824 public_key: "testkey.avbpubkey",
825 private_key: "testkey.pem",
826 }
827
828 cc_library {
829 name: "mylib",
830 srcs: ["mylib.cpp"],
831 runtime_libs: ["libfoo", "libbar"],
832 system_shared_libs: [],
833 stl: "none",
834 }
835
836 cc_library {
837 name: "libfoo",
838 srcs: ["mylib.cpp"],
839 system_shared_libs: [],
840 stl: "none",
841 stubs: {
842 versions: ["10", "20", "30"],
843 },
844 }
845
846 cc_library {
847 name: "libbar",
848 srcs: ["mylib.cpp"],
849 system_shared_libs: [],
850 stl: "none",
851 }
852
853 `)
854
Sundong Ahnabb64432019-10-22 13:58:29 +0900855 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900856 copyCmds := apexRule.Args["copy_commands"]
857
858 // Ensure that direct non-stubs dep is always included
859 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
860
861 // Ensure that indirect stubs dep is not included
862 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
863
864 // Ensure that runtime_libs dep in included
865 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
866
Sundong Ahnabb64432019-10-22 13:58:29 +0900867 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900868 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
869 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900870
871}
872
Jooyung Han9c80bae2019-08-20 17:30:57 +0900873func TestApexDependencyToLLNDK(t *testing.T) {
874 ctx, _ := testApex(t, `
875 apex {
876 name: "myapex",
877 key: "myapex.key",
878 use_vendor: true,
879 native_shared_libs: ["mylib"],
880 }
881
882 apex_key {
883 name: "myapex.key",
884 public_key: "testkey.avbpubkey",
885 private_key: "testkey.pem",
886 }
887
888 cc_library {
889 name: "mylib",
890 srcs: ["mylib.cpp"],
891 vendor_available: true,
892 shared_libs: ["libbar"],
893 system_shared_libs: [],
894 stl: "none",
895 }
896
897 cc_library {
898 name: "libbar",
899 srcs: ["mylib.cpp"],
900 system_shared_libs: [],
901 stl: "none",
902 }
903
904 llndk_library {
905 name: "libbar",
906 symbol_file: "",
907 }
Jooyung Handc782442019-11-01 03:14:38 +0900908 `, func(fs map[string][]byte, config android.Config) {
909 setUseVendorWhitelistForTest(config, []string{"myapex"})
910 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900911
Sundong Ahnabb64432019-10-22 13:58:29 +0900912 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900913 copyCmds := apexRule.Args["copy_commands"]
914
915 // Ensure that LLNDK dep is not included
916 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
917
Sundong Ahnabb64432019-10-22 13:58:29 +0900918 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900919 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900920
921 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900922 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900923
924}
925
Jiyong Park25fc6a92018-11-18 18:02:45 +0900926func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700927 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900928 apex {
929 name: "myapex",
930 key: "myapex.key",
931 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
932 }
933
934 apex_key {
935 name: "myapex.key",
936 public_key: "testkey.avbpubkey",
937 private_key: "testkey.pem",
938 }
939
940 cc_library {
941 name: "mylib",
942 srcs: ["mylib.cpp"],
943 shared_libs: ["libdl#27"],
944 stl: "none",
945 }
946
947 cc_library_shared {
948 name: "mylib_shared",
949 srcs: ["mylib.cpp"],
950 shared_libs: ["libdl#27"],
951 stl: "none",
952 }
953
954 cc_library {
955 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700956 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900957 nocrt: true,
958 system_shared_libs: [],
959 stl: "none",
960 stubs: {
961 versions: ["27", "28", "29"],
962 },
963 }
964
965 cc_library {
966 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700967 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900968 nocrt: true,
969 system_shared_libs: [],
970 stl: "none",
971 stubs: {
972 versions: ["27", "28", "29"],
973 },
974 }
975
976 cc_library {
977 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700978 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900979 nocrt: true,
980 system_shared_libs: [],
981 stl: "none",
982 stubs: {
983 versions: ["27", "28", "29"],
984 },
985 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900986
987 cc_library {
988 name: "libBootstrap",
989 srcs: ["mylib.cpp"],
990 stl: "none",
991 bootstrap: true,
992 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900993 `)
994
Sundong Ahnabb64432019-10-22 13:58:29 +0900995 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900996 copyCmds := apexRule.Args["copy_commands"]
997
998 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800999 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001000 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1001 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001002
1003 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001004 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001005
Colin Cross7113d202019-11-20 16:39:12 -08001006 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1007 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1008 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001009
1010 // For dependency to libc
1011 // Ensure that mylib is linking with the latest version of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001012 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001013 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001014 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001015 // ... Cflags from stub is correctly exported to mylib
1016 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1017 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1018
1019 // For dependency to libm
1020 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001021 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001022 // ... and not linking to the stub variant
Colin Cross7113d202019-11-20 16:39:12 -08001023 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001024 // ... and is not compiling with the stub
1025 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1026 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1027
1028 // For dependency to libdl
1029 // Ensure that mylib is linking with the specified version of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001030 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001031 // ... and not linking to the other versions of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001032 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28_myapex/libdl.so")
1033 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001034 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001035 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001036 // ... Cflags from stub is correctly exported to mylib
1037 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1038 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001039
1040 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001041 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1042 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1043 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1044 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001045}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001046
1047func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001048 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001049 apex {
1050 name: "myapex",
1051 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001052 native_shared_libs: ["mylib"],
1053 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001054 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001055 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001056 }
1057
1058 apex_key {
1059 name: "myapex.key",
1060 public_key: "testkey.avbpubkey",
1061 private_key: "testkey.pem",
1062 }
1063
1064 prebuilt_etc {
1065 name: "myetc",
1066 src: "myprebuilt",
1067 sub_dir: "foo/bar",
1068 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001069
1070 cc_library {
1071 name: "mylib",
1072 srcs: ["mylib.cpp"],
1073 relative_install_path: "foo/bar",
1074 system_shared_libs: [],
1075 stl: "none",
1076 }
1077
1078 cc_binary {
1079 name: "mybin",
1080 srcs: ["mylib.cpp"],
1081 relative_install_path: "foo/bar",
1082 system_shared_libs: [],
1083 static_executable: true,
1084 stl: "none",
1085 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001086 `)
1087
Sundong Ahnabb64432019-10-22 13:58:29 +09001088 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001089 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1090
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001091 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001092 ensureListContains(t, dirs, "etc")
1093 ensureListContains(t, dirs, "etc/foo")
1094 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001095 ensureListContains(t, dirs, "lib64")
1096 ensureListContains(t, dirs, "lib64/foo")
1097 ensureListContains(t, dirs, "lib64/foo/bar")
1098 ensureListContains(t, dirs, "lib")
1099 ensureListContains(t, dirs, "lib/foo")
1100 ensureListContains(t, dirs, "lib/foo/bar")
1101
Jiyong Parkbd13e442019-03-15 18:10:35 +09001102 ensureListContains(t, dirs, "bin")
1103 ensureListContains(t, dirs, "bin/foo")
1104 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001105}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001106
1107func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001108 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001109 apex {
1110 name: "myapex",
1111 key: "myapex.key",
1112 native_shared_libs: ["mylib"],
1113 use_vendor: true,
1114 }
1115
1116 apex_key {
1117 name: "myapex.key",
1118 public_key: "testkey.avbpubkey",
1119 private_key: "testkey.pem",
1120 }
1121
1122 cc_library {
1123 name: "mylib",
1124 srcs: ["mylib.cpp"],
1125 shared_libs: ["mylib2"],
1126 system_shared_libs: [],
1127 vendor_available: true,
1128 stl: "none",
1129 }
1130
1131 cc_library {
1132 name: "mylib2",
1133 srcs: ["mylib.cpp"],
1134 system_shared_libs: [],
1135 vendor_available: true,
1136 stl: "none",
1137 }
Jooyung Handc782442019-11-01 03:14:38 +09001138 `, func(fs map[string][]byte, config android.Config) {
1139 setUseVendorWhitelistForTest(config, []string{"myapex"})
1140 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001141
1142 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001143 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001144 for _, implicit := range i.Implicits {
1145 inputsList = append(inputsList, implicit.String())
1146 }
1147 }
1148 inputsString := strings.Join(inputsList, " ")
1149
1150 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001151 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1152 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001153
1154 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001155 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1156 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001157}
Jiyong Park16e91a02018-12-20 18:18:08 +09001158
Jooyung Handc782442019-11-01 03:14:38 +09001159func TestUseVendorRestriction(t *testing.T) {
1160 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1161 apex {
1162 name: "myapex",
1163 key: "myapex.key",
1164 use_vendor: true,
1165 }
1166 apex_key {
1167 name: "myapex.key",
1168 public_key: "testkey.avbpubkey",
1169 private_key: "testkey.pem",
1170 }
1171 `, func(fs map[string][]byte, config android.Config) {
1172 setUseVendorWhitelistForTest(config, []string{""})
1173 })
1174 // no error with whitelist
1175 testApex(t, `
1176 apex {
1177 name: "myapex",
1178 key: "myapex.key",
1179 use_vendor: true,
1180 }
1181 apex_key {
1182 name: "myapex.key",
1183 public_key: "testkey.avbpubkey",
1184 private_key: "testkey.pem",
1185 }
1186 `, func(fs map[string][]byte, config android.Config) {
1187 setUseVendorWhitelistForTest(config, []string{"myapex"})
1188 })
1189}
1190
Jooyung Han5c998b92019-06-27 11:30:33 +09001191func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1192 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1193 apex {
1194 name: "myapex",
1195 key: "myapex.key",
1196 native_shared_libs: ["mylib"],
1197 use_vendor: true,
1198 }
1199
1200 apex_key {
1201 name: "myapex.key",
1202 public_key: "testkey.avbpubkey",
1203 private_key: "testkey.pem",
1204 }
1205
1206 cc_library {
1207 name: "mylib",
1208 srcs: ["mylib.cpp"],
1209 system_shared_libs: [],
1210 stl: "none",
1211 }
1212 `)
1213}
1214
Jiyong Park16e91a02018-12-20 18:18:08 +09001215func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001216 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001217 apex {
1218 name: "myapex",
1219 key: "myapex.key",
1220 native_shared_libs: ["mylib"],
1221 }
1222
1223 apex_key {
1224 name: "myapex.key",
1225 public_key: "testkey.avbpubkey",
1226 private_key: "testkey.pem",
1227 }
1228
1229 cc_library {
1230 name: "mylib",
1231 srcs: ["mylib.cpp"],
1232 system_shared_libs: [],
1233 stl: "none",
1234 stubs: {
1235 versions: ["1", "2", "3"],
1236 },
1237 }
1238
1239 cc_binary {
1240 name: "not_in_apex",
1241 srcs: ["mylib.cpp"],
1242 static_libs: ["mylib"],
1243 static_executable: true,
1244 system_shared_libs: [],
1245 stl: "none",
1246 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001247 `)
1248
Colin Cross7113d202019-11-20 16:39:12 -08001249 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001250
1251 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001252 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001253}
Jiyong Park9335a262018-12-24 11:31:58 +09001254
1255func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001256 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001257 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001258 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001259 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001260 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001261 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001262 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001263 }
1264
1265 cc_library {
1266 name: "mylib",
1267 srcs: ["mylib.cpp"],
1268 system_shared_libs: [],
1269 stl: "none",
1270 }
1271
1272 apex_key {
1273 name: "myapex.key",
1274 public_key: "testkey.avbpubkey",
1275 private_key: "testkey.pem",
1276 }
1277
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001278 android_app_certificate {
1279 name: "myapex.certificate",
1280 certificate: "testkey",
1281 }
1282
1283 android_app_certificate {
1284 name: "myapex.certificate.override",
1285 certificate: "testkey.override",
1286 }
1287
Jiyong Park9335a262018-12-24 11:31:58 +09001288 `)
1289
1290 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001291 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001292
1293 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1294 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1295 "vendor/foo/devkeys/testkey.avbpubkey")
1296 }
1297 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1298 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1299 "vendor/foo/devkeys/testkey.pem")
1300 }
1301
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001302 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001303 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001304 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001305 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001306 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001307 }
1308}
Jiyong Park58e364a2019-01-19 19:24:06 +09001309
Jooyung Hanf121a652019-12-17 14:30:11 +09001310func TestCertificate(t *testing.T) {
1311 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1312 ctx, _ := testApex(t, `
1313 apex {
1314 name: "myapex",
1315 key: "myapex.key",
1316 }
1317 apex_key {
1318 name: "myapex.key",
1319 public_key: "testkey.avbpubkey",
1320 private_key: "testkey.pem",
1321 }`)
1322 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1323 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1324 if actual := rule.Args["certificates"]; actual != expected {
1325 t.Errorf("certificates should be %q, not %q", expected, actual)
1326 }
1327 })
1328 t.Run("override when unspecified", func(t *testing.T) {
1329 ctx, _ := testApex(t, `
1330 apex {
1331 name: "myapex_keytest",
1332 key: "myapex.key",
1333 file_contexts: ":myapex-file_contexts",
1334 }
1335 apex_key {
1336 name: "myapex.key",
1337 public_key: "testkey.avbpubkey",
1338 private_key: "testkey.pem",
1339 }
1340 android_app_certificate {
1341 name: "myapex.certificate.override",
1342 certificate: "testkey.override",
1343 }`)
1344 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1345 expected := "testkey.override.x509.pem testkey.override.pk8"
1346 if actual := rule.Args["certificates"]; actual != expected {
1347 t.Errorf("certificates should be %q, not %q", expected, actual)
1348 }
1349 })
1350 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1351 ctx, _ := testApex(t, `
1352 apex {
1353 name: "myapex",
1354 key: "myapex.key",
1355 certificate: ":myapex.certificate",
1356 }
1357 apex_key {
1358 name: "myapex.key",
1359 public_key: "testkey.avbpubkey",
1360 private_key: "testkey.pem",
1361 }
1362 android_app_certificate {
1363 name: "myapex.certificate",
1364 certificate: "testkey",
1365 }`)
1366 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1367 expected := "testkey.x509.pem testkey.pk8"
1368 if actual := rule.Args["certificates"]; actual != expected {
1369 t.Errorf("certificates should be %q, not %q", expected, actual)
1370 }
1371 })
1372 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1373 ctx, _ := testApex(t, `
1374 apex {
1375 name: "myapex_keytest",
1376 key: "myapex.key",
1377 file_contexts: ":myapex-file_contexts",
1378 certificate: ":myapex.certificate",
1379 }
1380 apex_key {
1381 name: "myapex.key",
1382 public_key: "testkey.avbpubkey",
1383 private_key: "testkey.pem",
1384 }
1385 android_app_certificate {
1386 name: "myapex.certificate.override",
1387 certificate: "testkey.override",
1388 }`)
1389 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1390 expected := "testkey.override.x509.pem testkey.override.pk8"
1391 if actual := rule.Args["certificates"]; actual != expected {
1392 t.Errorf("certificates should be %q, not %q", expected, actual)
1393 }
1394 })
1395 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1396 ctx, _ := testApex(t, `
1397 apex {
1398 name: "myapex",
1399 key: "myapex.key",
1400 certificate: "testkey",
1401 }
1402 apex_key {
1403 name: "myapex.key",
1404 public_key: "testkey.avbpubkey",
1405 private_key: "testkey.pem",
1406 }`)
1407 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1408 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1409 if actual := rule.Args["certificates"]; actual != expected {
1410 t.Errorf("certificates should be %q, not %q", expected, actual)
1411 }
1412 })
1413 t.Run("override when specified as <name>", func(t *testing.T) {
1414 ctx, _ := testApex(t, `
1415 apex {
1416 name: "myapex_keytest",
1417 key: "myapex.key",
1418 file_contexts: ":myapex-file_contexts",
1419 certificate: "testkey",
1420 }
1421 apex_key {
1422 name: "myapex.key",
1423 public_key: "testkey.avbpubkey",
1424 private_key: "testkey.pem",
1425 }
1426 android_app_certificate {
1427 name: "myapex.certificate.override",
1428 certificate: "testkey.override",
1429 }`)
1430 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1431 expected := "testkey.override.x509.pem testkey.override.pk8"
1432 if actual := rule.Args["certificates"]; actual != expected {
1433 t.Errorf("certificates should be %q, not %q", expected, actual)
1434 }
1435 })
1436}
1437
Jiyong Park58e364a2019-01-19 19:24:06 +09001438func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001439 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001440 apex {
1441 name: "myapex",
1442 key: "myapex.key",
1443 native_shared_libs: ["mylib"],
1444 }
1445
1446 apex {
1447 name: "otherapex",
1448 key: "myapex.key",
1449 native_shared_libs: ["mylib"],
1450 }
1451
1452 apex_key {
1453 name: "myapex.key",
1454 public_key: "testkey.avbpubkey",
1455 private_key: "testkey.pem",
1456 }
1457
1458 cc_library {
1459 name: "mylib",
1460 srcs: ["mylib.cpp"],
1461 system_shared_libs: [],
1462 stl: "none",
1463 }
1464 `)
1465
Jooyung Han6b8459b2019-10-30 08:29:25 +09001466 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001467 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001468 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001469 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1470 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001471
Jooyung Han6b8459b2019-10-30 08:29:25 +09001472 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001473 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001474 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001475 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1476 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001477
Jooyung Han6b8459b2019-10-30 08:29:25 +09001478 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001479 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001480 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001481 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1482 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001483}
Jiyong Park7e636d02019-01-28 16:16:54 +09001484
1485func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001486 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001487 apex {
1488 name: "myapex",
1489 key: "myapex.key",
1490 native_shared_libs: ["mylib"],
1491 }
1492
1493 apex_key {
1494 name: "myapex.key",
1495 public_key: "testkey.avbpubkey",
1496 private_key: "testkey.pem",
1497 }
1498
1499 cc_library_headers {
1500 name: "mylib_headers",
1501 export_include_dirs: ["my_include"],
1502 system_shared_libs: [],
1503 stl: "none",
1504 }
1505
1506 cc_library {
1507 name: "mylib",
1508 srcs: ["mylib.cpp"],
1509 system_shared_libs: [],
1510 stl: "none",
1511 header_libs: ["mylib_headers"],
1512 export_header_lib_headers: ["mylib_headers"],
1513 stubs: {
1514 versions: ["1", "2", "3"],
1515 },
1516 }
1517
1518 cc_library {
1519 name: "otherlib",
1520 srcs: ["mylib.cpp"],
1521 system_shared_libs: [],
1522 stl: "none",
1523 shared_libs: ["mylib"],
1524 }
1525 `)
1526
Colin Cross7113d202019-11-20 16:39:12 -08001527 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001528
1529 // Ensure that the include path of the header lib is exported to 'otherlib'
1530 ensureContains(t, cFlags, "-Imy_include")
1531}
Alex Light9670d332019-01-29 18:07:33 -08001532
Jooyung Han31c470b2019-10-18 16:26:59 +09001533func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1534 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001535 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001536 copyCmds := apexRule.Args["copy_commands"]
1537 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001538 var failed bool
1539 var surplus []string
1540 filesMatched := make(map[string]bool)
1541 addContent := func(content string) {
1542 for _, expected := range files {
1543 if matched, _ := path.Match(expected, content); matched {
1544 filesMatched[expected] = true
1545 return
1546 }
1547 }
1548 surplus = append(surplus, content)
1549 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001550 for _, cmd := range strings.Split(copyCmds, "&&") {
1551 cmd = strings.TrimSpace(cmd)
1552 if cmd == "" {
1553 continue
1554 }
1555 terms := strings.Split(cmd, " ")
1556 switch terms[0] {
1557 case "mkdir":
1558 case "cp":
1559 if len(terms) != 3 {
1560 t.Fatal("copyCmds contains invalid cp command", cmd)
1561 }
1562 dst := terms[2]
1563 index := strings.Index(dst, imageApexDir)
1564 if index == -1 {
1565 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1566 }
1567 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001568 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001569 default:
1570 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1571 }
1572 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001573
Jooyung Han31c470b2019-10-18 16:26:59 +09001574 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001575 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001576 t.Log("surplus files", surplus)
1577 failed = true
1578 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001579
1580 if len(files) > len(filesMatched) {
1581 var missing []string
1582 for _, expected := range files {
1583 if !filesMatched[expected] {
1584 missing = append(missing, expected)
1585 }
1586 }
1587 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001588 t.Log("missing files", missing)
1589 failed = true
1590 }
1591 if failed {
1592 t.Fail()
1593 }
1594}
1595
Jooyung Han344d5432019-08-23 11:17:39 +09001596func TestVndkApexCurrent(t *testing.T) {
1597 ctx, _ := testApex(t, `
1598 apex_vndk {
1599 name: "myapex",
1600 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001601 }
1602
1603 apex_key {
1604 name: "myapex.key",
1605 public_key: "testkey.avbpubkey",
1606 private_key: "testkey.pem",
1607 }
1608
1609 cc_library {
1610 name: "libvndk",
1611 srcs: ["mylib.cpp"],
1612 vendor_available: true,
1613 vndk: {
1614 enabled: true,
1615 },
1616 system_shared_libs: [],
1617 stl: "none",
1618 }
1619
1620 cc_library {
1621 name: "libvndksp",
1622 srcs: ["mylib.cpp"],
1623 vendor_available: true,
1624 vndk: {
1625 enabled: true,
1626 support_system_process: true,
1627 },
1628 system_shared_libs: [],
1629 stl: "none",
1630 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001631 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001632
Jooyung Han31c470b2019-10-18 16:26:59 +09001633 ensureExactContents(t, ctx, "myapex", []string{
1634 "lib/libvndk.so",
1635 "lib/libvndksp.so",
1636 "lib64/libvndk.so",
1637 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001638 "etc/llndk.libraries.VER.txt",
1639 "etc/vndkcore.libraries.VER.txt",
1640 "etc/vndksp.libraries.VER.txt",
1641 "etc/vndkprivate.libraries.VER.txt",
1642 "etc/vndkcorevariant.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001643 })
Jooyung Han344d5432019-08-23 11:17:39 +09001644}
1645
1646func TestVndkApexWithPrebuilt(t *testing.T) {
1647 ctx, _ := testApex(t, `
1648 apex_vndk {
1649 name: "myapex",
1650 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001651 }
1652
1653 apex_key {
1654 name: "myapex.key",
1655 public_key: "testkey.avbpubkey",
1656 private_key: "testkey.pem",
1657 }
1658
1659 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001660 name: "libvndk",
1661 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001662 vendor_available: true,
1663 vndk: {
1664 enabled: true,
1665 },
1666 system_shared_libs: [],
1667 stl: "none",
1668 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001669
1670 cc_prebuilt_library_shared {
1671 name: "libvndk.arm",
1672 srcs: ["libvndk.arm.so"],
1673 vendor_available: true,
1674 vndk: {
1675 enabled: true,
1676 },
1677 enabled: false,
1678 arch: {
1679 arm: {
1680 enabled: true,
1681 },
1682 },
1683 system_shared_libs: [],
1684 stl: "none",
1685 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001686 `+vndkLibrariesTxtFiles("current"),
1687 withFiles(map[string][]byte{
1688 "libvndk.so": nil,
1689 "libvndk.arm.so": nil,
1690 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001691
Jooyung Han31c470b2019-10-18 16:26:59 +09001692 ensureExactContents(t, ctx, "myapex", []string{
1693 "lib/libvndk.so",
1694 "lib/libvndk.arm.so",
1695 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001696 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001697 })
Jooyung Han344d5432019-08-23 11:17:39 +09001698}
1699
Jooyung Han39edb6c2019-11-06 16:53:07 +09001700func vndkLibrariesTxtFiles(vers ...string) (result string) {
1701 for _, v := range vers {
1702 if v == "current" {
1703 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkcorevariant"} {
1704 result += `
1705 vndk_libraries_txt {
1706 name: "` + txt + `.libraries.txt",
1707 }
1708 `
1709 }
1710 } else {
1711 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1712 result += `
1713 prebuilt_etc {
1714 name: "` + txt + `.libraries.` + v + `.txt",
1715 src: "dummy.txt",
1716 }
1717 `
1718 }
1719 }
1720 }
1721 return
1722}
1723
Jooyung Han344d5432019-08-23 11:17:39 +09001724func TestVndkApexVersion(t *testing.T) {
1725 ctx, _ := testApex(t, `
1726 apex_vndk {
1727 name: "myapex_v27",
1728 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001729 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001730 vndk_version: "27",
1731 }
1732
1733 apex_key {
1734 name: "myapex.key",
1735 public_key: "testkey.avbpubkey",
1736 private_key: "testkey.pem",
1737 }
1738
Jooyung Han31c470b2019-10-18 16:26:59 +09001739 vndk_prebuilt_shared {
1740 name: "libvndk27",
1741 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001742 vendor_available: true,
1743 vndk: {
1744 enabled: true,
1745 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001746 target_arch: "arm64",
1747 arch: {
1748 arm: {
1749 srcs: ["libvndk27_arm.so"],
1750 },
1751 arm64: {
1752 srcs: ["libvndk27_arm64.so"],
1753 },
1754 },
Jooyung Han344d5432019-08-23 11:17:39 +09001755 }
1756
1757 vndk_prebuilt_shared {
1758 name: "libvndk27",
1759 version: "27",
1760 vendor_available: true,
1761 vndk: {
1762 enabled: true,
1763 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001764 target_arch: "x86_64",
1765 arch: {
1766 x86: {
1767 srcs: ["libvndk27_x86.so"],
1768 },
1769 x86_64: {
1770 srcs: ["libvndk27_x86_64.so"],
1771 },
1772 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001773 }
1774 `+vndkLibrariesTxtFiles("27"),
1775 withFiles(map[string][]byte{
1776 "libvndk27_arm.so": nil,
1777 "libvndk27_arm64.so": nil,
1778 "libvndk27_x86.so": nil,
1779 "libvndk27_x86_64.so": nil,
1780 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001781
Jooyung Han31c470b2019-10-18 16:26:59 +09001782 ensureExactContents(t, ctx, "myapex_v27", []string{
1783 "lib/libvndk27_arm.so",
1784 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001785 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001786 })
Jooyung Han344d5432019-08-23 11:17:39 +09001787}
1788
1789func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1790 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1791 apex_vndk {
1792 name: "myapex_v27",
1793 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001794 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001795 vndk_version: "27",
1796 }
1797 apex_vndk {
1798 name: "myapex_v27_other",
1799 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001800 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001801 vndk_version: "27",
1802 }
1803
1804 apex_key {
1805 name: "myapex.key",
1806 public_key: "testkey.avbpubkey",
1807 private_key: "testkey.pem",
1808 }
1809
1810 cc_library {
1811 name: "libvndk",
1812 srcs: ["mylib.cpp"],
1813 vendor_available: true,
1814 vndk: {
1815 enabled: true,
1816 },
1817 system_shared_libs: [],
1818 stl: "none",
1819 }
1820
1821 vndk_prebuilt_shared {
1822 name: "libvndk",
1823 version: "27",
1824 vendor_available: true,
1825 vndk: {
1826 enabled: true,
1827 },
1828 srcs: ["libvndk.so"],
1829 }
1830 `, withFiles(map[string][]byte{
1831 "libvndk.so": nil,
1832 }))
1833}
1834
Jooyung Han90eee022019-10-01 20:02:42 +09001835func TestVndkApexNameRule(t *testing.T) {
1836 ctx, _ := testApex(t, `
1837 apex_vndk {
1838 name: "myapex",
1839 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001840 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001841 }
1842 apex_vndk {
1843 name: "myapex_v28",
1844 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001845 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001846 vndk_version: "28",
1847 }
1848 apex_key {
1849 name: "myapex.key",
1850 public_key: "testkey.avbpubkey",
1851 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001852 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001853
1854 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001855 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001856 actual := proptools.String(bundle.properties.Apex_name)
1857 if !reflect.DeepEqual(actual, expected) {
1858 t.Errorf("Got '%v', expected '%v'", actual, expected)
1859 }
1860 }
1861
1862 assertApexName("com.android.vndk.vVER", "myapex")
1863 assertApexName("com.android.vndk.v28", "myapex_v28")
1864}
1865
Jooyung Han344d5432019-08-23 11:17:39 +09001866func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1867 ctx, _ := testApex(t, `
1868 apex_vndk {
1869 name: "myapex",
1870 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001871 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001872 }
1873
1874 apex_key {
1875 name: "myapex.key",
1876 public_key: "testkey.avbpubkey",
1877 private_key: "testkey.pem",
1878 }
1879
1880 cc_library {
1881 name: "libvndk",
1882 srcs: ["mylib.cpp"],
1883 vendor_available: true,
1884 native_bridge_supported: true,
1885 host_supported: true,
1886 vndk: {
1887 enabled: true,
1888 },
1889 system_shared_libs: [],
1890 stl: "none",
1891 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001892 `+vndkLibrariesTxtFiles("current"),
1893 withTargets(map[android.OsType][]android.Target{
1894 android.Android: []android.Target{
1895 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1896 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1897 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1898 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1899 },
1900 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001901
Jooyung Han31c470b2019-10-18 16:26:59 +09001902 ensureExactContents(t, ctx, "myapex", []string{
1903 "lib/libvndk.so",
1904 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001905 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001906 })
Jooyung Han344d5432019-08-23 11:17:39 +09001907}
1908
1909func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1910 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1911 apex_vndk {
1912 name: "myapex",
1913 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001914 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001915 native_bridge_supported: true,
1916 }
1917
1918 apex_key {
1919 name: "myapex.key",
1920 public_key: "testkey.avbpubkey",
1921 private_key: "testkey.pem",
1922 }
1923
1924 cc_library {
1925 name: "libvndk",
1926 srcs: ["mylib.cpp"],
1927 vendor_available: true,
1928 native_bridge_supported: true,
1929 host_supported: true,
1930 vndk: {
1931 enabled: true,
1932 },
1933 system_shared_libs: [],
1934 stl: "none",
1935 }
1936 `)
1937}
1938
Jooyung Han31c470b2019-10-18 16:26:59 +09001939func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001940 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001941 apex_vndk {
1942 name: "myapex_v27",
1943 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001944 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001945 vndk_version: "27",
1946 }
1947
1948 apex_key {
1949 name: "myapex.key",
1950 public_key: "testkey.avbpubkey",
1951 private_key: "testkey.pem",
1952 }
1953
1954 vndk_prebuilt_shared {
1955 name: "libvndk27",
1956 version: "27",
1957 target_arch: "arm",
1958 vendor_available: true,
1959 vndk: {
1960 enabled: true,
1961 },
1962 arch: {
1963 arm: {
1964 srcs: ["libvndk27.so"],
1965 }
1966 },
1967 }
1968
1969 vndk_prebuilt_shared {
1970 name: "libvndk27",
1971 version: "27",
1972 target_arch: "arm",
1973 binder32bit: true,
1974 vendor_available: true,
1975 vndk: {
1976 enabled: true,
1977 },
1978 arch: {
1979 arm: {
1980 srcs: ["libvndk27binder32.so"],
1981 }
1982 },
1983 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001984 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001985 withFiles(map[string][]byte{
1986 "libvndk27.so": nil,
1987 "libvndk27binder32.so": nil,
1988 }),
1989 withBinder32bit,
1990 withTargets(map[android.OsType][]android.Target{
1991 android.Android: []android.Target{
1992 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1993 },
1994 }),
1995 )
1996
1997 ensureExactContents(t, ctx, "myapex_v27", []string{
1998 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001999 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002000 })
2001}
2002
Jooyung Hane1633032019-08-01 17:41:43 +09002003func TestDependenciesInApexManifest(t *testing.T) {
2004 ctx, _ := testApex(t, `
2005 apex {
2006 name: "myapex_nodep",
2007 key: "myapex.key",
2008 native_shared_libs: ["lib_nodep"],
2009 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002010 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002011 }
2012
2013 apex {
2014 name: "myapex_dep",
2015 key: "myapex.key",
2016 native_shared_libs: ["lib_dep"],
2017 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002018 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002019 }
2020
2021 apex {
2022 name: "myapex_provider",
2023 key: "myapex.key",
2024 native_shared_libs: ["libfoo"],
2025 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002026 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002027 }
2028
2029 apex {
2030 name: "myapex_selfcontained",
2031 key: "myapex.key",
2032 native_shared_libs: ["lib_dep", "libfoo"],
2033 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002034 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002035 }
2036
2037 apex_key {
2038 name: "myapex.key",
2039 public_key: "testkey.avbpubkey",
2040 private_key: "testkey.pem",
2041 }
2042
2043 cc_library {
2044 name: "lib_nodep",
2045 srcs: ["mylib.cpp"],
2046 system_shared_libs: [],
2047 stl: "none",
2048 }
2049
2050 cc_library {
2051 name: "lib_dep",
2052 srcs: ["mylib.cpp"],
2053 shared_libs: ["libfoo"],
2054 system_shared_libs: [],
2055 stl: "none",
2056 }
2057
2058 cc_library {
2059 name: "libfoo",
2060 srcs: ["mytest.cpp"],
2061 stubs: {
2062 versions: ["1"],
2063 },
2064 system_shared_libs: [],
2065 stl: "none",
2066 }
2067 `)
2068
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002069 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002070 var provideNativeLibs, requireNativeLibs []string
2071
Sundong Ahnabb64432019-10-22 13:58:29 +09002072 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002073 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2074 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002075 ensureListEmpty(t, provideNativeLibs)
2076 ensureListEmpty(t, requireNativeLibs)
2077
Sundong Ahnabb64432019-10-22 13:58:29 +09002078 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002079 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2080 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002081 ensureListEmpty(t, provideNativeLibs)
2082 ensureListContains(t, requireNativeLibs, "libfoo.so")
2083
Sundong Ahnabb64432019-10-22 13:58:29 +09002084 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002085 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2086 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002087 ensureListContains(t, provideNativeLibs, "libfoo.so")
2088 ensureListEmpty(t, requireNativeLibs)
2089
Sundong Ahnabb64432019-10-22 13:58:29 +09002090 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002091 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2092 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002093 ensureListContains(t, provideNativeLibs, "libfoo.so")
2094 ensureListEmpty(t, requireNativeLibs)
2095}
2096
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002097func TestApexName(t *testing.T) {
2098 ctx, _ := testApex(t, `
2099 apex {
2100 name: "myapex",
2101 key: "myapex.key",
2102 apex_name: "com.android.myapex",
2103 }
2104
2105 apex_key {
2106 name: "myapex.key",
2107 public_key: "testkey.avbpubkey",
2108 private_key: "testkey.pem",
2109 }
2110 `)
2111
Sundong Ahnabb64432019-10-22 13:58:29 +09002112 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002113 apexManifestRule := module.Rule("apexManifestRule")
2114 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2115 apexRule := module.Rule("apexRule")
2116 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
2117}
2118
Alex Light0851b882019-02-07 13:20:53 -08002119func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002120 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002121 apex {
2122 name: "myapex",
2123 key: "myapex.key",
2124 native_shared_libs: ["mylib_common"],
2125 }
2126
2127 apex_key {
2128 name: "myapex.key",
2129 public_key: "testkey.avbpubkey",
2130 private_key: "testkey.pem",
2131 }
2132
2133 cc_library {
2134 name: "mylib_common",
2135 srcs: ["mylib.cpp"],
2136 system_shared_libs: [],
2137 stl: "none",
2138 }
2139 `)
2140
Sundong Ahnabb64432019-10-22 13:58:29 +09002141 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002142 apexRule := module.Rule("apexRule")
2143 copyCmds := apexRule.Args["copy_commands"]
2144
2145 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2146 t.Log("Apex was a test apex!")
2147 t.Fail()
2148 }
2149 // Ensure that main rule creates an output
2150 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2151
2152 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002153 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002154
2155 // Ensure that both direct and indirect deps are copied into apex
2156 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2157
Colin Cross7113d202019-11-20 16:39:12 -08002158 // Ensure that the platform variant ends with _shared
2159 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002160
2161 if !android.InAnyApex("mylib_common") {
2162 t.Log("Found mylib_common not in any apex!")
2163 t.Fail()
2164 }
2165}
2166
2167func TestTestApex(t *testing.T) {
2168 if android.InAnyApex("mylib_common_test") {
2169 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!")
2170 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002171 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002172 apex_test {
2173 name: "myapex",
2174 key: "myapex.key",
2175 native_shared_libs: ["mylib_common_test"],
2176 }
2177
2178 apex_key {
2179 name: "myapex.key",
2180 public_key: "testkey.avbpubkey",
2181 private_key: "testkey.pem",
2182 }
2183
2184 cc_library {
2185 name: "mylib_common_test",
2186 srcs: ["mylib.cpp"],
2187 system_shared_libs: [],
2188 stl: "none",
2189 }
2190 `)
2191
Sundong Ahnabb64432019-10-22 13:58:29 +09002192 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002193 apexRule := module.Rule("apexRule")
2194 copyCmds := apexRule.Args["copy_commands"]
2195
2196 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2197 t.Log("Apex was not a test apex!")
2198 t.Fail()
2199 }
2200 // Ensure that main rule creates an output
2201 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2202
2203 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002204 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002205
2206 // Ensure that both direct and indirect deps are copied into apex
2207 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2208
Colin Cross7113d202019-11-20 16:39:12 -08002209 // Ensure that the platform variant ends with _shared
2210 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002211
2212 if android.InAnyApex("mylib_common_test") {
2213 t.Log("Found mylib_common_test in some apex!")
2214 t.Fail()
2215 }
2216}
2217
Alex Light9670d332019-01-29 18:07:33 -08002218func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002219 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002220 apex {
2221 name: "myapex",
2222 key: "myapex.key",
2223 multilib: {
2224 first: {
2225 native_shared_libs: ["mylib_common"],
2226 }
2227 },
2228 target: {
2229 android: {
2230 multilib: {
2231 first: {
2232 native_shared_libs: ["mylib"],
2233 }
2234 }
2235 },
2236 host: {
2237 multilib: {
2238 first: {
2239 native_shared_libs: ["mylib2"],
2240 }
2241 }
2242 }
2243 }
2244 }
2245
2246 apex_key {
2247 name: "myapex.key",
2248 public_key: "testkey.avbpubkey",
2249 private_key: "testkey.pem",
2250 }
2251
2252 cc_library {
2253 name: "mylib",
2254 srcs: ["mylib.cpp"],
2255 system_shared_libs: [],
2256 stl: "none",
2257 }
2258
2259 cc_library {
2260 name: "mylib_common",
2261 srcs: ["mylib.cpp"],
2262 system_shared_libs: [],
2263 stl: "none",
2264 compile_multilib: "first",
2265 }
2266
2267 cc_library {
2268 name: "mylib2",
2269 srcs: ["mylib.cpp"],
2270 system_shared_libs: [],
2271 stl: "none",
2272 compile_multilib: "first",
2273 }
2274 `)
2275
Sundong Ahnabb64432019-10-22 13:58:29 +09002276 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002277 copyCmds := apexRule.Args["copy_commands"]
2278
2279 // Ensure that main rule creates an output
2280 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2281
2282 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002283 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2284 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2285 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002286
2287 // Ensure that both direct and indirect deps are copied into apex
2288 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2289 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2290 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2291
Colin Cross7113d202019-11-20 16:39:12 -08002292 // Ensure that the platform variant ends with _shared
2293 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2294 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2295 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002296}
Jiyong Park04480cf2019-02-06 00:16:29 +09002297
2298func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002299 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002300 apex {
2301 name: "myapex",
2302 key: "myapex.key",
2303 binaries: ["myscript"],
2304 }
2305
2306 apex_key {
2307 name: "myapex.key",
2308 public_key: "testkey.avbpubkey",
2309 private_key: "testkey.pem",
2310 }
2311
2312 sh_binary {
2313 name: "myscript",
2314 src: "mylib.cpp",
2315 filename: "myscript.sh",
2316 sub_dir: "script",
2317 }
2318 `)
2319
Sundong Ahnabb64432019-10-22 13:58:29 +09002320 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002321 copyCmds := apexRule.Args["copy_commands"]
2322
2323 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2324}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002325
Jooyung Han91df2082019-11-20 01:49:42 +09002326func TestApexInVariousPartition(t *testing.T) {
2327 testcases := []struct {
2328 propName, parition, flattenedPartition string
2329 }{
2330 {"", "system", "system_ext"},
2331 {"product_specific: true", "product", "product"},
2332 {"soc_specific: true", "vendor", "vendor"},
2333 {"proprietary: true", "vendor", "vendor"},
2334 {"vendor: true", "vendor", "vendor"},
2335 {"system_ext_specific: true", "system_ext", "system_ext"},
2336 }
2337 for _, tc := range testcases {
2338 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2339 ctx, _ := testApex(t, `
2340 apex {
2341 name: "myapex",
2342 key: "myapex.key",
2343 `+tc.propName+`
2344 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002345
Jooyung Han91df2082019-11-20 01:49:42 +09002346 apex_key {
2347 name: "myapex.key",
2348 public_key: "testkey.avbpubkey",
2349 private_key: "testkey.pem",
2350 }
2351 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002352
Jooyung Han91df2082019-11-20 01:49:42 +09002353 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2354 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2355 actual := apex.installDir.String()
2356 if actual != expected {
2357 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2358 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002359
Jooyung Han91df2082019-11-20 01:49:42 +09002360 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2361 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2362 actual = flattened.installDir.String()
2363 if actual != expected {
2364 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2365 }
2366 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002367 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002368}
Jiyong Park67882562019-03-21 01:11:21 +09002369
Jooyung Han54aca7b2019-11-20 02:26:02 +09002370func TestFileContexts(t *testing.T) {
2371 ctx, _ := testApex(t, `
2372 apex {
2373 name: "myapex",
2374 key: "myapex.key",
2375 }
2376
2377 apex_key {
2378 name: "myapex.key",
2379 public_key: "testkey.avbpubkey",
2380 private_key: "testkey.pem",
2381 }
2382 `)
2383 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2384 apexRule := module.Rule("apexRule")
2385 actual := apexRule.Args["file_contexts"]
2386 expected := "system/sepolicy/apex/myapex-file_contexts"
2387 if actual != expected {
2388 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2389 }
2390
2391 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2392 apex {
2393 name: "myapex",
2394 key: "myapex.key",
2395 file_contexts: "my_own_file_contexts",
2396 }
2397
2398 apex_key {
2399 name: "myapex.key",
2400 public_key: "testkey.avbpubkey",
2401 private_key: "testkey.pem",
2402 }
2403 `, withFiles(map[string][]byte{
2404 "my_own_file_contexts": nil,
2405 }))
2406
2407 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2408 apex {
2409 name: "myapex",
2410 key: "myapex.key",
2411 product_specific: true,
2412 file_contexts: "product_specific_file_contexts",
2413 }
2414
2415 apex_key {
2416 name: "myapex.key",
2417 public_key: "testkey.avbpubkey",
2418 private_key: "testkey.pem",
2419 }
2420 `)
2421
2422 ctx, _ = testApex(t, `
2423 apex {
2424 name: "myapex",
2425 key: "myapex.key",
2426 product_specific: true,
2427 file_contexts: "product_specific_file_contexts",
2428 }
2429
2430 apex_key {
2431 name: "myapex.key",
2432 public_key: "testkey.avbpubkey",
2433 private_key: "testkey.pem",
2434 }
2435 `, withFiles(map[string][]byte{
2436 "product_specific_file_contexts": nil,
2437 }))
2438 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2439 apexRule = module.Rule("apexRule")
2440 actual = apexRule.Args["file_contexts"]
2441 expected = "product_specific_file_contexts"
2442 if actual != expected {
2443 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2444 }
2445
2446 ctx, _ = testApex(t, `
2447 apex {
2448 name: "myapex",
2449 key: "myapex.key",
2450 product_specific: true,
2451 file_contexts: ":my-file-contexts",
2452 }
2453
2454 apex_key {
2455 name: "myapex.key",
2456 public_key: "testkey.avbpubkey",
2457 private_key: "testkey.pem",
2458 }
2459
2460 filegroup {
2461 name: "my-file-contexts",
2462 srcs: ["product_specific_file_contexts"],
2463 }
2464 `, withFiles(map[string][]byte{
2465 "product_specific_file_contexts": nil,
2466 }))
2467 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2468 apexRule = module.Rule("apexRule")
2469 actual = apexRule.Args["file_contexts"]
2470 expected = "product_specific_file_contexts"
2471 if actual != expected {
2472 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2473 }
2474}
2475
Jiyong Park67882562019-03-21 01:11:21 +09002476func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002477 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002478 apex_key {
2479 name: "myapex.key",
2480 public_key: ":my.avbpubkey",
2481 private_key: ":my.pem",
2482 product_specific: true,
2483 }
2484
2485 filegroup {
2486 name: "my.avbpubkey",
2487 srcs: ["testkey2.avbpubkey"],
2488 }
2489
2490 filegroup {
2491 name: "my.pem",
2492 srcs: ["testkey2.pem"],
2493 }
2494 `)
2495
2496 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2497 expected_pubkey := "testkey2.avbpubkey"
2498 actual_pubkey := apex_key.public_key_file.String()
2499 if actual_pubkey != expected_pubkey {
2500 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2501 }
2502 expected_privkey := "testkey2.pem"
2503 actual_privkey := apex_key.private_key_file.String()
2504 if actual_privkey != expected_privkey {
2505 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2506 }
2507}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002508
2509func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002510 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002511 prebuilt_apex {
2512 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002513 arch: {
2514 arm64: {
2515 src: "myapex-arm64.apex",
2516 },
2517 arm: {
2518 src: "myapex-arm.apex",
2519 },
2520 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002521 }
2522 `)
2523
2524 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2525
Jiyong Parkc95714e2019-03-29 14:23:10 +09002526 expectedInput := "myapex-arm64.apex"
2527 if prebuilt.inputApex.String() != expectedInput {
2528 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2529 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002530}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002531
2532func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002533 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002534 prebuilt_apex {
2535 name: "myapex",
2536 src: "myapex-arm.apex",
2537 filename: "notmyapex.apex",
2538 }
2539 `)
2540
2541 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2542
2543 expected := "notmyapex.apex"
2544 if p.installFilename != expected {
2545 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2546 }
2547}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002548
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002549func TestPrebuiltOverrides(t *testing.T) {
2550 ctx, config := testApex(t, `
2551 prebuilt_apex {
2552 name: "myapex.prebuilt",
2553 src: "myapex-arm.apex",
2554 overrides: [
2555 "myapex",
2556 ],
2557 }
2558 `)
2559
2560 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2561
2562 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002563 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002564 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002565 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002566 }
2567}
2568
Roland Levillain630846d2019-06-26 12:48:34 +01002569func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002570 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002571 apex_test {
2572 name: "myapex",
2573 key: "myapex.key",
2574 tests: [
2575 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002576 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002577 ],
2578 }
2579
2580 apex_key {
2581 name: "myapex.key",
2582 public_key: "testkey.avbpubkey",
2583 private_key: "testkey.pem",
2584 }
2585
2586 cc_test {
2587 name: "mytest",
2588 gtest: false,
2589 srcs: ["mytest.cpp"],
2590 relative_install_path: "test",
2591 system_shared_libs: [],
2592 static_executable: true,
2593 stl: "none",
2594 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002595
2596 cc_test {
2597 name: "mytests",
2598 gtest: false,
2599 srcs: [
2600 "mytest1.cpp",
2601 "mytest2.cpp",
2602 "mytest3.cpp",
2603 ],
2604 test_per_src: true,
2605 relative_install_path: "test",
2606 system_shared_libs: [],
2607 static_executable: true,
2608 stl: "none",
2609 }
Roland Levillain630846d2019-06-26 12:48:34 +01002610 `)
2611
Sundong Ahnabb64432019-10-22 13:58:29 +09002612 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002613 copyCmds := apexRule.Args["copy_commands"]
2614
2615 // Ensure that test dep is copied into apex.
2616 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002617
2618 // Ensure that test deps built with `test_per_src` are copied into apex.
2619 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2620 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2621 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002622
2623 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002624 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002625 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2626 name := apexBundle.BaseModuleName()
2627 prefix := "TARGET_"
2628 var builder strings.Builder
2629 data.Custom(&builder, name, prefix, "", data)
2630 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002631 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2632 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2633 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2634 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002635 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002636 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002637 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002638}
2639
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002640func TestInstallExtraFlattenedApexes(t *testing.T) {
2641 ctx, config := testApex(t, `
2642 apex {
2643 name: "myapex",
2644 key: "myapex.key",
2645 }
2646 apex_key {
2647 name: "myapex.key",
2648 public_key: "testkey.avbpubkey",
2649 private_key: "testkey.pem",
2650 }
2651 `, func(fs map[string][]byte, config android.Config) {
2652 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2653 })
2654 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2655 ensureListContains(t, ab.externalDeps, "myapex.flattened")
2656 mk := android.AndroidMkDataForTest(t, config, "", ab)
2657 var builder strings.Builder
2658 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2659 androidMk := builder.String()
2660 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2661}
2662
Jooyung Han5c998b92019-06-27 11:30:33 +09002663func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002664 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002665 apex {
2666 name: "myapex",
2667 key: "myapex.key",
2668 native_shared_libs: ["mylib"],
2669 uses: ["commonapex"],
2670 }
2671
2672 apex {
2673 name: "commonapex",
2674 key: "myapex.key",
2675 native_shared_libs: ["libcommon"],
2676 provide_cpp_shared_libs: true,
2677 }
2678
2679 apex_key {
2680 name: "myapex.key",
2681 public_key: "testkey.avbpubkey",
2682 private_key: "testkey.pem",
2683 }
2684
2685 cc_library {
2686 name: "mylib",
2687 srcs: ["mylib.cpp"],
2688 shared_libs: ["libcommon"],
2689 system_shared_libs: [],
2690 stl: "none",
2691 }
2692
2693 cc_library {
2694 name: "libcommon",
2695 srcs: ["mylib_common.cpp"],
2696 system_shared_libs: [],
2697 stl: "none",
2698 }
2699 `)
2700
Sundong Ahnabb64432019-10-22 13:58:29 +09002701 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002702 apexRule1 := module1.Rule("apexRule")
2703 copyCmds1 := apexRule1.Args["copy_commands"]
2704
Sundong Ahnabb64432019-10-22 13:58:29 +09002705 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002706 apexRule2 := module2.Rule("apexRule")
2707 copyCmds2 := apexRule2.Args["copy_commands"]
2708
Colin Cross7113d202019-11-20 16:39:12 -08002709 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2710 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002711 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2712 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2713 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2714}
2715
2716func TestApexUsesFailsIfNotProvided(t *testing.T) {
2717 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2718 apex {
2719 name: "myapex",
2720 key: "myapex.key",
2721 uses: ["commonapex"],
2722 }
2723
2724 apex {
2725 name: "commonapex",
2726 key: "myapex.key",
2727 }
2728
2729 apex_key {
2730 name: "myapex.key",
2731 public_key: "testkey.avbpubkey",
2732 private_key: "testkey.pem",
2733 }
2734 `)
2735 testApexError(t, `uses: "commonapex" is not a provider`, `
2736 apex {
2737 name: "myapex",
2738 key: "myapex.key",
2739 uses: ["commonapex"],
2740 }
2741
2742 cc_library {
2743 name: "commonapex",
2744 system_shared_libs: [],
2745 stl: "none",
2746 }
2747
2748 apex_key {
2749 name: "myapex.key",
2750 public_key: "testkey.avbpubkey",
2751 private_key: "testkey.pem",
2752 }
2753 `)
2754}
2755
2756func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2757 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2758 apex {
2759 name: "myapex",
2760 key: "myapex.key",
2761 use_vendor: true,
2762 uses: ["commonapex"],
2763 }
2764
2765 apex {
2766 name: "commonapex",
2767 key: "myapex.key",
2768 provide_cpp_shared_libs: true,
2769 }
2770
2771 apex_key {
2772 name: "myapex.key",
2773 public_key: "testkey.avbpubkey",
2774 private_key: "testkey.pem",
2775 }
Jooyung Handc782442019-11-01 03:14:38 +09002776 `, func(fs map[string][]byte, config android.Config) {
2777 setUseVendorWhitelistForTest(config, []string{"myapex"})
2778 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002779}
2780
Jooyung Hand48f3c32019-08-23 11:18:57 +09002781func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2782 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2783 apex {
2784 name: "myapex",
2785 key: "myapex.key",
2786 native_shared_libs: ["libfoo"],
2787 }
2788
2789 apex_key {
2790 name: "myapex.key",
2791 public_key: "testkey.avbpubkey",
2792 private_key: "testkey.pem",
2793 }
2794
2795 cc_library {
2796 name: "libfoo",
2797 stl: "none",
2798 system_shared_libs: [],
2799 enabled: false,
2800 }
2801 `)
2802 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2803 apex {
2804 name: "myapex",
2805 key: "myapex.key",
2806 java_libs: ["myjar"],
2807 }
2808
2809 apex_key {
2810 name: "myapex.key",
2811 public_key: "testkey.avbpubkey",
2812 private_key: "testkey.pem",
2813 }
2814
2815 java_library {
2816 name: "myjar",
2817 srcs: ["foo/bar/MyClass.java"],
2818 sdk_version: "none",
2819 system_modules: "none",
2820 compile_dex: true,
2821 enabled: false,
2822 }
2823 `)
2824}
2825
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002826func TestApexWithApps(t *testing.T) {
2827 ctx, _ := testApex(t, `
2828 apex {
2829 name: "myapex",
2830 key: "myapex.key",
2831 apps: [
2832 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002833 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002834 ],
2835 }
2836
2837 apex_key {
2838 name: "myapex.key",
2839 public_key: "testkey.avbpubkey",
2840 private_key: "testkey.pem",
2841 }
2842
2843 android_app {
2844 name: "AppFoo",
2845 srcs: ["foo/bar/MyClass.java"],
2846 sdk_version: "none",
2847 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002848 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002849 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002850
2851 android_app {
2852 name: "AppFooPriv",
2853 srcs: ["foo/bar/MyClass.java"],
2854 sdk_version: "none",
2855 system_modules: "none",
2856 privileged: true,
2857 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002858
2859 cc_library_shared {
2860 name: "libjni",
2861 srcs: ["mylib.cpp"],
2862 stl: "none",
2863 system_shared_libs: [],
2864 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002865 `)
2866
Sundong Ahnabb64432019-10-22 13:58:29 +09002867 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002868 apexRule := module.Rule("apexRule")
2869 copyCmds := apexRule.Args["copy_commands"]
2870
2871 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002872 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002873
2874 // JNI libraries are embedded inside APK
2875 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
Colin Cross7113d202019-11-20 16:39:12 -08002876 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002877 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2878 // ... uncompressed
2879 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2880 t.Errorf("jni lib is not uncompressed for AppFoo")
2881 }
2882 // ... and not directly inside the APEX
2883 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002884}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002885
Dario Frenicde2a032019-10-27 00:29:22 +01002886func TestApexWithAppImports(t *testing.T) {
2887 ctx, _ := testApex(t, `
2888 apex {
2889 name: "myapex",
2890 key: "myapex.key",
2891 apps: [
2892 "AppFooPrebuilt",
2893 "AppFooPrivPrebuilt",
2894 ],
2895 }
2896
2897 apex_key {
2898 name: "myapex.key",
2899 public_key: "testkey.avbpubkey",
2900 private_key: "testkey.pem",
2901 }
2902
2903 android_app_import {
2904 name: "AppFooPrebuilt",
2905 apk: "PrebuiltAppFoo.apk",
2906 presigned: true,
2907 dex_preopt: {
2908 enabled: false,
2909 },
2910 }
2911
2912 android_app_import {
2913 name: "AppFooPrivPrebuilt",
2914 apk: "PrebuiltAppFooPriv.apk",
2915 privileged: true,
2916 presigned: true,
2917 dex_preopt: {
2918 enabled: false,
2919 },
2920 }
2921 `)
2922
Sundong Ahnabb64432019-10-22 13:58:29 +09002923 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002924 apexRule := module.Rule("apexRule")
2925 copyCmds := apexRule.Args["copy_commands"]
2926
2927 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2928 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002929}
2930
Jooyung Han18020ea2019-11-13 10:50:48 +09002931func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2932 // libfoo's apex_available comes from cc_defaults
2933 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2934 apex {
2935 name: "myapex",
2936 key: "myapex.key",
2937 native_shared_libs: ["libfoo"],
2938 }
2939
2940 apex_key {
2941 name: "myapex.key",
2942 public_key: "testkey.avbpubkey",
2943 private_key: "testkey.pem",
2944 }
2945
2946 apex {
2947 name: "otherapex",
2948 key: "myapex.key",
2949 native_shared_libs: ["libfoo"],
2950 }
2951
2952 cc_defaults {
2953 name: "libfoo-defaults",
2954 apex_available: ["otherapex"],
2955 }
2956
2957 cc_library {
2958 name: "libfoo",
2959 defaults: ["libfoo-defaults"],
2960 stl: "none",
2961 system_shared_libs: [],
2962 }`)
2963}
2964
Jiyong Park127b40b2019-09-30 16:04:35 +09002965func TestApexAvailable(t *testing.T) {
2966 // libfoo is not available to myapex, but only to otherapex
2967 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2968 apex {
2969 name: "myapex",
2970 key: "myapex.key",
2971 native_shared_libs: ["libfoo"],
2972 }
2973
2974 apex_key {
2975 name: "myapex.key",
2976 public_key: "testkey.avbpubkey",
2977 private_key: "testkey.pem",
2978 }
2979
2980 apex {
2981 name: "otherapex",
2982 key: "otherapex.key",
2983 native_shared_libs: ["libfoo"],
2984 }
2985
2986 apex_key {
2987 name: "otherapex.key",
2988 public_key: "testkey.avbpubkey",
2989 private_key: "testkey.pem",
2990 }
2991
2992 cc_library {
2993 name: "libfoo",
2994 stl: "none",
2995 system_shared_libs: [],
2996 apex_available: ["otherapex"],
2997 }`)
2998
2999 // libbar is an indirect dep
3000 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3001 apex {
3002 name: "myapex",
3003 key: "myapex.key",
3004 native_shared_libs: ["libfoo"],
3005 }
3006
3007 apex_key {
3008 name: "myapex.key",
3009 public_key: "testkey.avbpubkey",
3010 private_key: "testkey.pem",
3011 }
3012
3013 apex {
3014 name: "otherapex",
3015 key: "otherapex.key",
3016 native_shared_libs: ["libfoo"],
3017 }
3018
3019 apex_key {
3020 name: "otherapex.key",
3021 public_key: "testkey.avbpubkey",
3022 private_key: "testkey.pem",
3023 }
3024
3025 cc_library {
3026 name: "libfoo",
3027 stl: "none",
3028 shared_libs: ["libbar"],
3029 system_shared_libs: [],
3030 apex_available: ["myapex", "otherapex"],
3031 }
3032
3033 cc_library {
3034 name: "libbar",
3035 stl: "none",
3036 system_shared_libs: [],
3037 apex_available: ["otherapex"],
3038 }`)
3039
3040 testApexError(t, "\"otherapex\" is not a valid module name", `
3041 apex {
3042 name: "myapex",
3043 key: "myapex.key",
3044 native_shared_libs: ["libfoo"],
3045 }
3046
3047 apex_key {
3048 name: "myapex.key",
3049 public_key: "testkey.avbpubkey",
3050 private_key: "testkey.pem",
3051 }
3052
3053 cc_library {
3054 name: "libfoo",
3055 stl: "none",
3056 system_shared_libs: [],
3057 apex_available: ["otherapex"],
3058 }`)
3059
3060 ctx, _ := testApex(t, `
3061 apex {
3062 name: "myapex",
3063 key: "myapex.key",
3064 native_shared_libs: ["libfoo", "libbar"],
3065 }
3066
3067 apex_key {
3068 name: "myapex.key",
3069 public_key: "testkey.avbpubkey",
3070 private_key: "testkey.pem",
3071 }
3072
3073 cc_library {
3074 name: "libfoo",
3075 stl: "none",
3076 system_shared_libs: [],
3077 apex_available: ["myapex"],
3078 }
3079
3080 cc_library {
3081 name: "libbar",
3082 stl: "none",
3083 system_shared_libs: [],
3084 apex_available: ["//apex_available:anyapex"],
3085 }`)
3086
3087 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003088 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3089 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3090 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3091 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003092
3093 ctx, _ = testApex(t, `
3094 apex {
3095 name: "myapex",
3096 key: "myapex.key",
3097 }
3098
3099 apex_key {
3100 name: "myapex.key",
3101 public_key: "testkey.avbpubkey",
3102 private_key: "testkey.pem",
3103 }
3104
3105 cc_library {
3106 name: "libfoo",
3107 stl: "none",
3108 system_shared_libs: [],
3109 apex_available: ["//apex_available:platform"],
3110 }`)
3111
3112 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003113 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3114 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003115
3116 ctx, _ = testApex(t, `
3117 apex {
3118 name: "myapex",
3119 key: "myapex.key",
3120 native_shared_libs: ["libfoo"],
3121 }
3122
3123 apex_key {
3124 name: "myapex.key",
3125 public_key: "testkey.avbpubkey",
3126 private_key: "testkey.pem",
3127 }
3128
3129 cc_library {
3130 name: "libfoo",
3131 stl: "none",
3132 system_shared_libs: [],
3133 apex_available: ["myapex"],
3134 static: {
3135 apex_available: ["//apex_available:platform"],
3136 },
3137 }`)
3138
3139 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08003140 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3141 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003142 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08003143 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3144 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003145}
3146
Jiyong Park5d790c32019-11-15 18:40:32 +09003147func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003148 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003149 apex {
3150 name: "myapex",
3151 key: "myapex.key",
3152 apps: ["app"],
3153 }
3154
3155 override_apex {
3156 name: "override_myapex",
3157 base: "myapex",
3158 apps: ["override_app"],
3159 }
3160
3161 apex_key {
3162 name: "myapex.key",
3163 public_key: "testkey.avbpubkey",
3164 private_key: "testkey.pem",
3165 }
3166
3167 android_app {
3168 name: "app",
3169 srcs: ["foo/bar/MyClass.java"],
3170 package_name: "foo",
3171 sdk_version: "none",
3172 system_modules: "none",
3173 }
3174
3175 override_android_app {
3176 name: "override_app",
3177 base: "app",
3178 package_name: "bar",
3179 }
3180 `)
3181
Jiyong Park317645e2019-12-05 13:20:58 +09003182 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3183 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3184 if originalVariant.GetOverriddenBy() != "" {
3185 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3186 }
3187 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3188 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3189 }
3190
Jiyong Park5d790c32019-11-15 18:40:32 +09003191 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3192 apexRule := module.Rule("apexRule")
3193 copyCmds := apexRule.Args["copy_commands"]
3194
3195 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3196 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003197
3198 apexBundle := module.Module().(*apexBundle)
3199 name := apexBundle.Name()
3200 if name != "override_myapex" {
3201 t.Errorf("name should be \"override_myapex\", but was %q", name)
3202 }
3203
3204 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3205 var builder strings.Builder
3206 data.Custom(&builder, name, "TARGET_", "", data)
3207 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003208 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003209 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3210 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
3211 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003212 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003213 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3214 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003215}
3216
Jooyung Han214bf372019-11-12 13:03:50 +09003217func TestLegacyAndroid10Support(t *testing.T) {
3218 ctx, _ := testApex(t, `
3219 apex {
3220 name: "myapex",
3221 key: "myapex.key",
3222 legacy_android10_support: true,
3223 }
3224
3225 apex_key {
3226 name: "myapex.key",
3227 public_key: "testkey.avbpubkey",
3228 private_key: "testkey.pem",
3229 }
3230 `)
3231
3232 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3233 args := module.Rule("apexRule").Args
3234 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
3235}
3236
Jiyong Park479321d2019-12-16 11:47:12 +09003237func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3238 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3239 apex {
3240 name: "myapex",
3241 key: "myapex.key",
3242 java_libs: ["myjar"],
3243 }
3244
3245 apex_key {
3246 name: "myapex.key",
3247 public_key: "testkey.avbpubkey",
3248 private_key: "testkey.pem",
3249 }
3250
3251 java_library {
3252 name: "myjar",
3253 srcs: ["foo/bar/MyClass.java"],
3254 sdk_version: "none",
3255 system_modules: "none",
3256 }
3257 `)
3258}
3259
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003260func TestMain(m *testing.M) {
3261 run := func() int {
3262 setUp()
3263 defer tearDown()
3264
3265 return m.Run()
3266 }
3267
3268 os.Exit(run())
3269}