blob: d5ea2bc17928fa7b0a295c19ebb8109a15ac0ac7 [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"
Jiyong Park9c540c82024-03-26 10:48:27 +090026 "android/soong/phony"
Jooyung Hane6067592023-03-16 13:11:17 +090027
28 "github.com/google/blueprint/proptools"
Jooyung Han9706cbc2021-04-15 22:43:48 +090029)
30
31func TestMain(m *testing.M) {
32 os.Exit(m.Run())
33}
34
35var fixture = android.GroupFixturePreparers(
36 android.PrepareForIntegrationTestWithAndroid,
Jiyong Park06c4cdc2024-02-16 15:35:03 +090037 bpf.PrepareForTestWithBpf,
Jooyung Hane6067592023-03-16 13:11:17 +090038 etc.PrepareForTestWithPrebuiltEtc,
Jiyong Parkfa616132021-04-20 11:36:40 +090039 cc.PrepareForIntegrationTestWithCc,
Jiyong Park9c540c82024-03-26 10:48:27 +090040 phony.PrepareForTestWithPhony,
Jooyung Han9706cbc2021-04-15 22:43:48 +090041 PrepareForTestWithFilesystemBuildComponents,
42)
43
44func TestFileSystemDeps(t *testing.T) {
45 result := fixture.RunTestWithBp(t, `
46 android_filesystem {
47 name: "myfilesystem",
Jiyong Park06c4cdc2024-02-16 15:35:03 +090048 multilib: {
49 common: {
50 deps: [
51 "bpf.o",
Jiyong Park9c540c82024-03-26 10:48:27 +090052 "phony",
Jiyong Park06c4cdc2024-02-16 15:35:03 +090053 ],
54 },
Jiyong Parkeec7c382024-02-16 16:10:13 +090055 lib32: {
56 deps: [
57 "foo",
58 "libbar",
59 ],
60 },
61 lib64: {
62 deps: [
63 "libbar",
64 ],
65 },
Jiyong Park06c4cdc2024-02-16 15:35:03 +090066 },
Jiyong Parkeec7c382024-02-16 16:10:13 +090067 compile_multilib: "both",
Jiyong Park06c4cdc2024-02-16 15:35:03 +090068 }
69
70 bpf {
71 name: "bpf.o",
72 srcs: ["bpf.c"],
Jooyung Han9706cbc2021-04-15 22:43:48 +090073 }
Jiyong Parkeec7c382024-02-16 16:10:13 +090074
75 cc_binary {
76 name: "foo",
77 compile_multilib: "prefer32",
78 }
79
80 cc_library {
81 name: "libbar",
Jiyong Park8bcf3c62024-03-18 18:37:10 +090082 required: ["libbaz"],
83 }
84
85 cc_library {
86 name: "libbaz",
Jiyong Parkeec7c382024-02-16 16:10:13 +090087 }
Jiyong Park9c540c82024-03-26 10:48:27 +090088
89 phony {
90 name: "phony",
91 required: ["libquz"],
92 }
93
94 cc_library {
95 name: "libquz",
96 }
Jooyung Han9706cbc2021-04-15 22:43:48 +090097 `)
98
99 // produces "myfilesystem.img"
100 result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img")
Jiyong Park06c4cdc2024-02-16 15:35:03 +0900101
102 fs := result.ModuleForTests("myfilesystem", "android_common").Module().(*filesystem)
Jiyong Parkeec7c382024-02-16 16:10:13 +0900103 expected := []string{
104 "bin/foo",
105 "lib/libbar.so",
106 "lib64/libbar.so",
Jiyong Park8bcf3c62024-03-18 18:37:10 +0900107 "lib64/libbaz.so",
Jiyong Park9c540c82024-03-26 10:48:27 +0900108 "lib64/libquz.so",
Jiyong Parkeec7c382024-02-16 16:10:13 +0900109 "etc/bpf/bpf.o",
110 }
Jiyong Park06c4cdc2024-02-16 15:35:03 +0900111 for _, e := range expected {
112 android.AssertStringListContains(t, "missing entry", fs.entries, e)
113 }
Jooyung Han9706cbc2021-04-15 22:43:48 +0900114}
Jiyong Parkfa616132021-04-20 11:36:40 +0900115
Cole Faust4a2a7c92024-03-12 12:44:40 -0700116func TestIncludeMakeBuiltFiles(t *testing.T) {
117 result := fixture.RunTestWithBp(t, `
118 android_filesystem {
119 name: "myfilesystem",
120 include_make_built_files: "system",
121 }
122 `)
123
124 output := result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img")
125
126 stampFile := filepath.Join(result.Config.OutDir(), "target/product/test_device/obj/PACKAGING/system_intermediates/staging_dir.stamp")
127 fileListFile := filepath.Join(result.Config.OutDir(), "target/product/test_device/obj/PACKAGING/system_intermediates/file_list.txt")
128 android.AssertStringListContains(t, "deps of filesystem must include the staging dir stamp file", output.Implicits.Strings(), stampFile)
129 android.AssertStringListContains(t, "deps of filesystem must include the staging dir file list", output.Implicits.Strings(), fileListFile)
130}
131
Jiyong Parkfa616132021-04-20 11:36:40 +0900132func TestFileSystemFillsLinkerConfigWithStubLibs(t *testing.T) {
133 result := fixture.RunTestWithBp(t, `
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900134 android_system_image {
Jiyong Parkfa616132021-04-20 11:36:40 +0900135 name: "myfilesystem",
136 deps: [
137 "libfoo",
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900138 "libbar",
Jiyong Parkfa616132021-04-20 11:36:40 +0900139 ],
140 linker_config_src: "linker.config.json",
141 }
142
143 cc_library {
144 name: "libfoo",
145 stubs: {
146 symbol_file: "libfoo.map.txt",
147 },
148 }
149
150 cc_library {
151 name: "libbar",
152 }
153 `)
154
155 module := result.ModuleForTests("myfilesystem", "android_common")
156 output := module.Output("system/etc/linker.config.pb")
157
158 android.AssertStringDoesContain(t, "linker.config.pb should have libfoo",
159 output.RuleParams.Command, "libfoo.so")
160 android.AssertStringDoesNotContain(t, "linker.config.pb should not have libbar",
161 output.RuleParams.Command, "libbar.so")
162}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900163
164func registerComponent(ctx android.RegistrationContext) {
165 ctx.RegisterModuleType("component", componentFactory)
166}
167
168func componentFactory() android.Module {
169 m := &component{}
170 m.AddProperties(&m.properties)
171 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
172 return m
173}
174
175type component struct {
176 android.ModuleBase
177 properties struct {
178 Install_copy_in_data []string
179 }
180}
181
182func (c *component) GenerateAndroidBuildActions(ctx android.ModuleContext) {
183 output := android.PathForModuleOut(ctx, c.Name())
184 dir := android.PathForModuleInstall(ctx, "components")
185 ctx.InstallFile(dir, c.Name(), output)
186
187 dataDir := android.PathForModuleInPartitionInstall(ctx, "data", "components")
188 for _, d := range c.properties.Install_copy_in_data {
189 ctx.InstallFile(dataDir, d, output)
190 }
191}
192
193func TestFileSystemGathersItemsOnlyInSystemPartition(t *testing.T) {
194 f := android.GroupFixturePreparers(fixture, android.FixtureRegisterWithContext(registerComponent))
195 result := f.RunTestWithBp(t, `
196 android_system_image {
197 name: "myfilesystem",
198 multilib: {
199 common: {
200 deps: ["foo"],
201 },
202 },
203 linker_config_src: "linker.config.json",
204 }
205 component {
206 name: "foo",
207 install_copy_in_data: ["bar"],
208 }
209 `)
210
211 module := result.ModuleForTests("myfilesystem", "android_common").Module().(*systemImage)
212 android.AssertDeepEquals(t, "entries should have foo only", []string{"components/foo"}, module.entries)
213}
Jiyong Parkbc485482022-11-15 22:31:49 +0900214
Alice Wang000e3a32023-01-03 16:11:20 +0000215func TestAvbGenVbmetaImage(t *testing.T) {
216 result := fixture.RunTestWithBp(t, `
217 avb_gen_vbmeta_image {
218 name: "input_hashdesc",
219 src: "input.img",
220 partition_name: "input_partition_name",
221 salt: "2222",
222 }`)
223 cmd := result.ModuleForTests("input_hashdesc", "android_arm64_armv8-a").Rule("avbGenVbmetaImage").RuleParams.Command
224 android.AssertStringDoesContain(t, "Can't find correct --partition_name argument",
225 cmd, "--partition_name input_partition_name")
226 android.AssertStringDoesContain(t, "Can't find --do_not_append_vbmeta_image",
227 cmd, "--do_not_append_vbmeta_image")
228 android.AssertStringDoesContain(t, "Can't find --output_vbmeta_image",
229 cmd, "--output_vbmeta_image ")
230 android.AssertStringDoesContain(t, "Can't find --salt argument",
231 cmd, "--salt 2222")
232}
233
Jiyong Parkbc485482022-11-15 22:31:49 +0900234func TestAvbAddHashFooter(t *testing.T) {
235 result := fixture.RunTestWithBp(t, `
Alice Wang000e3a32023-01-03 16:11:20 +0000236 avb_gen_vbmeta_image {
237 name: "input_hashdesc",
238 src: "input.img",
239 partition_name: "input",
240 salt: "2222",
241 }
242
Jiyong Parkbc485482022-11-15 22:31:49 +0900243 avb_add_hash_footer {
244 name: "myfooter",
245 src: "input.img",
246 filename: "output.img",
247 partition_name: "mypartition",
248 private_key: "mykey",
249 salt: "1111",
250 props: [
251 {
252 name: "prop1",
253 value: "value1",
254 },
255 {
256 name: "prop2",
257 file: "value_file",
258 },
259 ],
Alice Wang000e3a32023-01-03 16:11:20 +0000260 include_descriptors_from_images: ["input_hashdesc"],
Jiyong Parkbc485482022-11-15 22:31:49 +0900261 }
262 `)
263 cmd := result.ModuleForTests("myfooter", "android_arm64_armv8-a").Rule("avbAddHashFooter").RuleParams.Command
264 android.AssertStringDoesContain(t, "Can't find correct --partition_name argument",
265 cmd, "--partition_name mypartition")
266 android.AssertStringDoesContain(t, "Can't find correct --key argument",
267 cmd, "--key mykey")
268 android.AssertStringDoesContain(t, "Can't find --salt argument",
269 cmd, "--salt 1111")
270 android.AssertStringDoesContain(t, "Can't find --prop argument",
271 cmd, "--prop 'prop1:value1'")
272 android.AssertStringDoesContain(t, "Can't find --prop_from_file argument",
273 cmd, "--prop_from_file 'prop2:value_file'")
Alice Wang000e3a32023-01-03 16:11:20 +0000274 android.AssertStringDoesContain(t, "Can't find --include_descriptors_from_image",
275 cmd, "--include_descriptors_from_image ")
Jiyong Parkbc485482022-11-15 22:31:49 +0900276}
Jooyung Han54f78052023-02-20 18:17:47 +0900277
278func TestFileSystemShouldInstallCoreVariantIfTargetBuildAppsIsSet(t *testing.T) {
279 context := android.GroupFixturePreparers(
280 fixture,
281 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
282 variables.Unbundled_build_apps = []string{"bar"}
283 }),
284 )
285 result := context.RunTestWithBp(t, `
286 android_system_image {
287 name: "myfilesystem",
288 deps: [
289 "libfoo",
290 ],
291 linker_config_src: "linker.config.json",
292 }
293
294 cc_library {
295 name: "libfoo",
296 shared_libs: [
297 "libbar",
298 ],
299 stl: "none",
300 }
301
302 cc_library {
303 name: "libbar",
304 sdk_version: "9",
305 stl: "none",
306 }
307 `)
308
Cole Faust3b806d32024-03-11 15:15:03 -0700309 inputs := result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img").Implicits
Jooyung Han54f78052023-02-20 18:17:47 +0900310 android.AssertStringListContains(t, "filesystem should have libbar even for unbundled build",
311 inputs.Strings(),
312 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
313}
Jooyung Hane6067592023-03-16 13:11:17 +0900314
315func TestFileSystemWithCoverageVariants(t *testing.T) {
316 context := android.GroupFixturePreparers(
317 fixture,
318 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
319 variables.GcovCoverage = proptools.BoolPtr(true)
320 variables.Native_coverage = proptools.BoolPtr(true)
321 }),
322 )
323
324 result := context.RunTestWithBp(t, `
325 prebuilt_etc {
326 name: "prebuilt",
327 src: ":myfilesystem",
328 }
329
330 android_system_image {
331 name: "myfilesystem",
332 deps: [
333 "libfoo",
334 ],
335 linker_config_src: "linker.config.json",
336 }
337
338 cc_library {
339 name: "libfoo",
340 shared_libs: [
341 "libbar",
342 ],
343 stl: "none",
344 }
345
346 cc_library {
347 name: "libbar",
348 stl: "none",
349 }
350 `)
351
352 filesystem := result.ModuleForTests("myfilesystem", "android_common_cov")
Cole Faust3b806d32024-03-11 15:15:03 -0700353 inputs := filesystem.Output("myfilesystem.img").Implicits
Jooyung Hane6067592023-03-16 13:11:17 +0900354 android.AssertStringListContains(t, "filesystem should have libfoo(cov)",
355 inputs.Strings(),
356 "out/soong/.intermediates/libfoo/android_arm64_armv8-a_shared_cov/libfoo.so")
357 android.AssertStringListContains(t, "filesystem should have libbar(cov)",
358 inputs.Strings(),
359 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared_cov/libbar.so")
360
361 filesystemOutput := filesystem.Output("myfilesystem.img").Output
362 prebuiltInput := result.ModuleForTests("prebuilt", "android_arm64_armv8-a").Rule("Cp").Input
363 if filesystemOutput != prebuiltInput {
364 t.Error("prebuilt should use cov variant of filesystem")
365 }
366}