blob: 74c79e3bbf812a22d55303930ca356b7a2bc88fc [file] [log] [blame]
Jooyung Han9706cbc2021-04-15 22:43:48 +09001// Copyright 2021 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 filesystem
16
17import (
18 "os"
Cole Faust4a2a7c92024-03-12 12:44:40 -070019 "path/filepath"
Jooyung Han9706cbc2021-04-15 22:43:48 +090020 "testing"
21
22 "android/soong/android"
Jiyong Park06c4cdc2024-02-16 15:35:03 +090023 "android/soong/bpf"
Jiyong Parkfa616132021-04-20 11:36:40 +090024 "android/soong/cc"
Jooyung Hane6067592023-03-16 13:11:17 +090025 "android/soong/etc"
26
27 "github.com/google/blueprint/proptools"
Jooyung Han9706cbc2021-04-15 22:43:48 +090028)
29
30func TestMain(m *testing.M) {
31 os.Exit(m.Run())
32}
33
34var fixture = android.GroupFixturePreparers(
35 android.PrepareForIntegrationTestWithAndroid,
Jiyong Park06c4cdc2024-02-16 15:35:03 +090036 bpf.PrepareForTestWithBpf,
Jooyung Hane6067592023-03-16 13:11:17 +090037 etc.PrepareForTestWithPrebuiltEtc,
Jiyong Parkfa616132021-04-20 11:36:40 +090038 cc.PrepareForIntegrationTestWithCc,
Jooyung Han9706cbc2021-04-15 22:43:48 +090039 PrepareForTestWithFilesystemBuildComponents,
40)
41
42func TestFileSystemDeps(t *testing.T) {
43 result := fixture.RunTestWithBp(t, `
44 android_filesystem {
45 name: "myfilesystem",
Jiyong Park06c4cdc2024-02-16 15:35:03 +090046 multilib: {
47 common: {
48 deps: [
49 "bpf.o",
50 ],
51 },
Jiyong Parkeec7c382024-02-16 16:10:13 +090052 lib32: {
53 deps: [
54 "foo",
55 "libbar",
56 ],
57 },
58 lib64: {
59 deps: [
60 "libbar",
61 ],
62 },
Jiyong Park06c4cdc2024-02-16 15:35:03 +090063 },
Jiyong Parkeec7c382024-02-16 16:10:13 +090064 compile_multilib: "both",
Jiyong Park06c4cdc2024-02-16 15:35:03 +090065 }
66
67 bpf {
68 name: "bpf.o",
69 srcs: ["bpf.c"],
Jooyung Han9706cbc2021-04-15 22:43:48 +090070 }
Jiyong Parkeec7c382024-02-16 16:10:13 +090071
72 cc_binary {
73 name: "foo",
74 compile_multilib: "prefer32",
75 }
76
77 cc_library {
78 name: "libbar",
Jiyong Park8bcf3c62024-03-18 18:37:10 +090079 required: ["libbaz"],
80 }
81
82 cc_library {
83 name: "libbaz",
Jiyong Parkeec7c382024-02-16 16:10:13 +090084 }
Jooyung Han9706cbc2021-04-15 22:43:48 +090085 `)
86
87 // produces "myfilesystem.img"
88 result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img")
Jiyong Park06c4cdc2024-02-16 15:35:03 +090089
90 fs := result.ModuleForTests("myfilesystem", "android_common").Module().(*filesystem)
Jiyong Parkeec7c382024-02-16 16:10:13 +090091 expected := []string{
92 "bin/foo",
93 "lib/libbar.so",
94 "lib64/libbar.so",
Jiyong Park8bcf3c62024-03-18 18:37:10 +090095 "lib64/libbaz.so",
Jiyong Parkeec7c382024-02-16 16:10:13 +090096 "etc/bpf/bpf.o",
97 }
Jiyong Park06c4cdc2024-02-16 15:35:03 +090098 for _, e := range expected {
99 android.AssertStringListContains(t, "missing entry", fs.entries, e)
100 }
Jooyung Han9706cbc2021-04-15 22:43:48 +0900101}
Jiyong Parkfa616132021-04-20 11:36:40 +0900102
Cole Faust4a2a7c92024-03-12 12:44:40 -0700103func TestIncludeMakeBuiltFiles(t *testing.T) {
104 result := fixture.RunTestWithBp(t, `
105 android_filesystem {
106 name: "myfilesystem",
107 include_make_built_files: "system",
108 }
109 `)
110
111 output := result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img")
112
113 stampFile := filepath.Join(result.Config.OutDir(), "target/product/test_device/obj/PACKAGING/system_intermediates/staging_dir.stamp")
114 fileListFile := filepath.Join(result.Config.OutDir(), "target/product/test_device/obj/PACKAGING/system_intermediates/file_list.txt")
115 android.AssertStringListContains(t, "deps of filesystem must include the staging dir stamp file", output.Implicits.Strings(), stampFile)
116 android.AssertStringListContains(t, "deps of filesystem must include the staging dir file list", output.Implicits.Strings(), fileListFile)
117}
118
Jiyong Parkfa616132021-04-20 11:36:40 +0900119func TestFileSystemFillsLinkerConfigWithStubLibs(t *testing.T) {
120 result := fixture.RunTestWithBp(t, `
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900121 android_system_image {
Jiyong Parkfa616132021-04-20 11:36:40 +0900122 name: "myfilesystem",
123 deps: [
124 "libfoo",
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900125 "libbar",
Jiyong Parkfa616132021-04-20 11:36:40 +0900126 ],
127 linker_config_src: "linker.config.json",
128 }
129
130 cc_library {
131 name: "libfoo",
132 stubs: {
133 symbol_file: "libfoo.map.txt",
134 },
135 }
136
137 cc_library {
138 name: "libbar",
139 }
140 `)
141
142 module := result.ModuleForTests("myfilesystem", "android_common")
143 output := module.Output("system/etc/linker.config.pb")
144
145 android.AssertStringDoesContain(t, "linker.config.pb should have libfoo",
146 output.RuleParams.Command, "libfoo.so")
147 android.AssertStringDoesNotContain(t, "linker.config.pb should not have libbar",
148 output.RuleParams.Command, "libbar.so")
149}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900150
151func registerComponent(ctx android.RegistrationContext) {
152 ctx.RegisterModuleType("component", componentFactory)
153}
154
155func componentFactory() android.Module {
156 m := &component{}
157 m.AddProperties(&m.properties)
158 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
159 return m
160}
161
162type component struct {
163 android.ModuleBase
164 properties struct {
165 Install_copy_in_data []string
166 }
167}
168
169func (c *component) GenerateAndroidBuildActions(ctx android.ModuleContext) {
170 output := android.PathForModuleOut(ctx, c.Name())
171 dir := android.PathForModuleInstall(ctx, "components")
172 ctx.InstallFile(dir, c.Name(), output)
173
174 dataDir := android.PathForModuleInPartitionInstall(ctx, "data", "components")
175 for _, d := range c.properties.Install_copy_in_data {
176 ctx.InstallFile(dataDir, d, output)
177 }
178}
179
180func TestFileSystemGathersItemsOnlyInSystemPartition(t *testing.T) {
181 f := android.GroupFixturePreparers(fixture, android.FixtureRegisterWithContext(registerComponent))
182 result := f.RunTestWithBp(t, `
183 android_system_image {
184 name: "myfilesystem",
185 multilib: {
186 common: {
187 deps: ["foo"],
188 },
189 },
190 linker_config_src: "linker.config.json",
191 }
192 component {
193 name: "foo",
194 install_copy_in_data: ["bar"],
195 }
196 `)
197
198 module := result.ModuleForTests("myfilesystem", "android_common").Module().(*systemImage)
199 android.AssertDeepEquals(t, "entries should have foo only", []string{"components/foo"}, module.entries)
200}
Jiyong Parkbc485482022-11-15 22:31:49 +0900201
Alice Wang000e3a32023-01-03 16:11:20 +0000202func TestAvbGenVbmetaImage(t *testing.T) {
203 result := fixture.RunTestWithBp(t, `
204 avb_gen_vbmeta_image {
205 name: "input_hashdesc",
206 src: "input.img",
207 partition_name: "input_partition_name",
208 salt: "2222",
209 }`)
210 cmd := result.ModuleForTests("input_hashdesc", "android_arm64_armv8-a").Rule("avbGenVbmetaImage").RuleParams.Command
211 android.AssertStringDoesContain(t, "Can't find correct --partition_name argument",
212 cmd, "--partition_name input_partition_name")
213 android.AssertStringDoesContain(t, "Can't find --do_not_append_vbmeta_image",
214 cmd, "--do_not_append_vbmeta_image")
215 android.AssertStringDoesContain(t, "Can't find --output_vbmeta_image",
216 cmd, "--output_vbmeta_image ")
217 android.AssertStringDoesContain(t, "Can't find --salt argument",
218 cmd, "--salt 2222")
219}
220
Jiyong Parkbc485482022-11-15 22:31:49 +0900221func TestAvbAddHashFooter(t *testing.T) {
222 result := fixture.RunTestWithBp(t, `
Alice Wang000e3a32023-01-03 16:11:20 +0000223 avb_gen_vbmeta_image {
224 name: "input_hashdesc",
225 src: "input.img",
226 partition_name: "input",
227 salt: "2222",
228 }
229
Jiyong Parkbc485482022-11-15 22:31:49 +0900230 avb_add_hash_footer {
231 name: "myfooter",
232 src: "input.img",
233 filename: "output.img",
234 partition_name: "mypartition",
235 private_key: "mykey",
236 salt: "1111",
237 props: [
238 {
239 name: "prop1",
240 value: "value1",
241 },
242 {
243 name: "prop2",
244 file: "value_file",
245 },
246 ],
Alice Wang000e3a32023-01-03 16:11:20 +0000247 include_descriptors_from_images: ["input_hashdesc"],
Jiyong Parkbc485482022-11-15 22:31:49 +0900248 }
249 `)
250 cmd := result.ModuleForTests("myfooter", "android_arm64_armv8-a").Rule("avbAddHashFooter").RuleParams.Command
251 android.AssertStringDoesContain(t, "Can't find correct --partition_name argument",
252 cmd, "--partition_name mypartition")
253 android.AssertStringDoesContain(t, "Can't find correct --key argument",
254 cmd, "--key mykey")
255 android.AssertStringDoesContain(t, "Can't find --salt argument",
256 cmd, "--salt 1111")
257 android.AssertStringDoesContain(t, "Can't find --prop argument",
258 cmd, "--prop 'prop1:value1'")
259 android.AssertStringDoesContain(t, "Can't find --prop_from_file argument",
260 cmd, "--prop_from_file 'prop2:value_file'")
Alice Wang000e3a32023-01-03 16:11:20 +0000261 android.AssertStringDoesContain(t, "Can't find --include_descriptors_from_image",
262 cmd, "--include_descriptors_from_image ")
Jiyong Parkbc485482022-11-15 22:31:49 +0900263}
Jooyung Han54f78052023-02-20 18:17:47 +0900264
265func TestFileSystemShouldInstallCoreVariantIfTargetBuildAppsIsSet(t *testing.T) {
266 context := android.GroupFixturePreparers(
267 fixture,
268 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
269 variables.Unbundled_build_apps = []string{"bar"}
270 }),
271 )
272 result := context.RunTestWithBp(t, `
273 android_system_image {
274 name: "myfilesystem",
275 deps: [
276 "libfoo",
277 ],
278 linker_config_src: "linker.config.json",
279 }
280
281 cc_library {
282 name: "libfoo",
283 shared_libs: [
284 "libbar",
285 ],
286 stl: "none",
287 }
288
289 cc_library {
290 name: "libbar",
291 sdk_version: "9",
292 stl: "none",
293 }
294 `)
295
Cole Faust3b806d32024-03-11 15:15:03 -0700296 inputs := result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img").Implicits
Jooyung Han54f78052023-02-20 18:17:47 +0900297 android.AssertStringListContains(t, "filesystem should have libbar even for unbundled build",
298 inputs.Strings(),
299 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
300}
Jooyung Hane6067592023-03-16 13:11:17 +0900301
302func TestFileSystemWithCoverageVariants(t *testing.T) {
303 context := android.GroupFixturePreparers(
304 fixture,
305 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
306 variables.GcovCoverage = proptools.BoolPtr(true)
307 variables.Native_coverage = proptools.BoolPtr(true)
308 }),
309 )
310
311 result := context.RunTestWithBp(t, `
312 prebuilt_etc {
313 name: "prebuilt",
314 src: ":myfilesystem",
315 }
316
317 android_system_image {
318 name: "myfilesystem",
319 deps: [
320 "libfoo",
321 ],
322 linker_config_src: "linker.config.json",
323 }
324
325 cc_library {
326 name: "libfoo",
327 shared_libs: [
328 "libbar",
329 ],
330 stl: "none",
331 }
332
333 cc_library {
334 name: "libbar",
335 stl: "none",
336 }
337 `)
338
339 filesystem := result.ModuleForTests("myfilesystem", "android_common_cov")
Cole Faust3b806d32024-03-11 15:15:03 -0700340 inputs := filesystem.Output("myfilesystem.img").Implicits
Jooyung Hane6067592023-03-16 13:11:17 +0900341 android.AssertStringListContains(t, "filesystem should have libfoo(cov)",
342 inputs.Strings(),
343 "out/soong/.intermediates/libfoo/android_arm64_armv8-a_shared_cov/libfoo.so")
344 android.AssertStringListContains(t, "filesystem should have libbar(cov)",
345 inputs.Strings(),
346 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared_cov/libbar.so")
347
348 filesystemOutput := filesystem.Output("myfilesystem.img").Output
349 prebuiltInput := result.ModuleForTests("prebuilt", "android_arm64_armv8-a").Rule("Cp").Input
350 if filesystemOutput != prebuiltInput {
351 t.Error("prebuilt should use cov variant of filesystem")
352 }
353}