blob: 6d62746ecbeed83967553e6d5851b50412728437 [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",
79 }
Jooyung Han9706cbc2021-04-15 22:43:48 +090080 `)
81
82 // produces "myfilesystem.img"
83 result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img")
Jiyong Park06c4cdc2024-02-16 15:35:03 +090084
85 fs := result.ModuleForTests("myfilesystem", "android_common").Module().(*filesystem)
Jiyong Parkeec7c382024-02-16 16:10:13 +090086 expected := []string{
87 "bin/foo",
88 "lib/libbar.so",
89 "lib64/libbar.so",
90 "etc/bpf/bpf.o",
91 }
Jiyong Park06c4cdc2024-02-16 15:35:03 +090092 for _, e := range expected {
93 android.AssertStringListContains(t, "missing entry", fs.entries, e)
94 }
Jooyung Han9706cbc2021-04-15 22:43:48 +090095}
Jiyong Parkfa616132021-04-20 11:36:40 +090096
Cole Faust4a2a7c92024-03-12 12:44:40 -070097func TestIncludeMakeBuiltFiles(t *testing.T) {
98 result := fixture.RunTestWithBp(t, `
99 android_filesystem {
100 name: "myfilesystem",
101 include_make_built_files: "system",
102 }
103 `)
104
105 output := result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img")
106
107 stampFile := filepath.Join(result.Config.OutDir(), "target/product/test_device/obj/PACKAGING/system_intermediates/staging_dir.stamp")
108 fileListFile := filepath.Join(result.Config.OutDir(), "target/product/test_device/obj/PACKAGING/system_intermediates/file_list.txt")
109 android.AssertStringListContains(t, "deps of filesystem must include the staging dir stamp file", output.Implicits.Strings(), stampFile)
110 android.AssertStringListContains(t, "deps of filesystem must include the staging dir file list", output.Implicits.Strings(), fileListFile)
111}
112
Jiyong Parkfa616132021-04-20 11:36:40 +0900113func TestFileSystemFillsLinkerConfigWithStubLibs(t *testing.T) {
114 result := fixture.RunTestWithBp(t, `
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900115 android_system_image {
Jiyong Parkfa616132021-04-20 11:36:40 +0900116 name: "myfilesystem",
117 deps: [
118 "libfoo",
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900119 "libbar",
Jiyong Parkfa616132021-04-20 11:36:40 +0900120 ],
121 linker_config_src: "linker.config.json",
122 }
123
124 cc_library {
125 name: "libfoo",
126 stubs: {
127 symbol_file: "libfoo.map.txt",
128 },
129 }
130
131 cc_library {
132 name: "libbar",
133 }
134 `)
135
136 module := result.ModuleForTests("myfilesystem", "android_common")
137 output := module.Output("system/etc/linker.config.pb")
138
139 android.AssertStringDoesContain(t, "linker.config.pb should have libfoo",
140 output.RuleParams.Command, "libfoo.so")
141 android.AssertStringDoesNotContain(t, "linker.config.pb should not have libbar",
142 output.RuleParams.Command, "libbar.so")
143}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900144
145func registerComponent(ctx android.RegistrationContext) {
146 ctx.RegisterModuleType("component", componentFactory)
147}
148
149func componentFactory() android.Module {
150 m := &component{}
151 m.AddProperties(&m.properties)
152 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
153 return m
154}
155
156type component struct {
157 android.ModuleBase
158 properties struct {
159 Install_copy_in_data []string
160 }
161}
162
163func (c *component) GenerateAndroidBuildActions(ctx android.ModuleContext) {
164 output := android.PathForModuleOut(ctx, c.Name())
165 dir := android.PathForModuleInstall(ctx, "components")
166 ctx.InstallFile(dir, c.Name(), output)
167
168 dataDir := android.PathForModuleInPartitionInstall(ctx, "data", "components")
169 for _, d := range c.properties.Install_copy_in_data {
170 ctx.InstallFile(dataDir, d, output)
171 }
172}
173
174func TestFileSystemGathersItemsOnlyInSystemPartition(t *testing.T) {
175 f := android.GroupFixturePreparers(fixture, android.FixtureRegisterWithContext(registerComponent))
176 result := f.RunTestWithBp(t, `
177 android_system_image {
178 name: "myfilesystem",
179 multilib: {
180 common: {
181 deps: ["foo"],
182 },
183 },
184 linker_config_src: "linker.config.json",
185 }
186 component {
187 name: "foo",
188 install_copy_in_data: ["bar"],
189 }
190 `)
191
192 module := result.ModuleForTests("myfilesystem", "android_common").Module().(*systemImage)
193 android.AssertDeepEquals(t, "entries should have foo only", []string{"components/foo"}, module.entries)
194}
Jiyong Parkbc485482022-11-15 22:31:49 +0900195
Alice Wang000e3a32023-01-03 16:11:20 +0000196func TestAvbGenVbmetaImage(t *testing.T) {
197 result := fixture.RunTestWithBp(t, `
198 avb_gen_vbmeta_image {
199 name: "input_hashdesc",
200 src: "input.img",
201 partition_name: "input_partition_name",
202 salt: "2222",
203 }`)
204 cmd := result.ModuleForTests("input_hashdesc", "android_arm64_armv8-a").Rule("avbGenVbmetaImage").RuleParams.Command
205 android.AssertStringDoesContain(t, "Can't find correct --partition_name argument",
206 cmd, "--partition_name input_partition_name")
207 android.AssertStringDoesContain(t, "Can't find --do_not_append_vbmeta_image",
208 cmd, "--do_not_append_vbmeta_image")
209 android.AssertStringDoesContain(t, "Can't find --output_vbmeta_image",
210 cmd, "--output_vbmeta_image ")
211 android.AssertStringDoesContain(t, "Can't find --salt argument",
212 cmd, "--salt 2222")
213}
214
Jiyong Parkbc485482022-11-15 22:31:49 +0900215func TestAvbAddHashFooter(t *testing.T) {
216 result := fixture.RunTestWithBp(t, `
Alice Wang000e3a32023-01-03 16:11:20 +0000217 avb_gen_vbmeta_image {
218 name: "input_hashdesc",
219 src: "input.img",
220 partition_name: "input",
221 salt: "2222",
222 }
223
Jiyong Parkbc485482022-11-15 22:31:49 +0900224 avb_add_hash_footer {
225 name: "myfooter",
226 src: "input.img",
227 filename: "output.img",
228 partition_name: "mypartition",
229 private_key: "mykey",
230 salt: "1111",
231 props: [
232 {
233 name: "prop1",
234 value: "value1",
235 },
236 {
237 name: "prop2",
238 file: "value_file",
239 },
240 ],
Alice Wang000e3a32023-01-03 16:11:20 +0000241 include_descriptors_from_images: ["input_hashdesc"],
Jiyong Parkbc485482022-11-15 22:31:49 +0900242 }
243 `)
244 cmd := result.ModuleForTests("myfooter", "android_arm64_armv8-a").Rule("avbAddHashFooter").RuleParams.Command
245 android.AssertStringDoesContain(t, "Can't find correct --partition_name argument",
246 cmd, "--partition_name mypartition")
247 android.AssertStringDoesContain(t, "Can't find correct --key argument",
248 cmd, "--key mykey")
249 android.AssertStringDoesContain(t, "Can't find --salt argument",
250 cmd, "--salt 1111")
251 android.AssertStringDoesContain(t, "Can't find --prop argument",
252 cmd, "--prop 'prop1:value1'")
253 android.AssertStringDoesContain(t, "Can't find --prop_from_file argument",
254 cmd, "--prop_from_file 'prop2:value_file'")
Alice Wang000e3a32023-01-03 16:11:20 +0000255 android.AssertStringDoesContain(t, "Can't find --include_descriptors_from_image",
256 cmd, "--include_descriptors_from_image ")
Jiyong Parkbc485482022-11-15 22:31:49 +0900257}
Jooyung Han54f78052023-02-20 18:17:47 +0900258
259func TestFileSystemShouldInstallCoreVariantIfTargetBuildAppsIsSet(t *testing.T) {
260 context := android.GroupFixturePreparers(
261 fixture,
262 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
263 variables.Unbundled_build_apps = []string{"bar"}
264 }),
265 )
266 result := context.RunTestWithBp(t, `
267 android_system_image {
268 name: "myfilesystem",
269 deps: [
270 "libfoo",
271 ],
272 linker_config_src: "linker.config.json",
273 }
274
275 cc_library {
276 name: "libfoo",
277 shared_libs: [
278 "libbar",
279 ],
280 stl: "none",
281 }
282
283 cc_library {
284 name: "libbar",
285 sdk_version: "9",
286 stl: "none",
287 }
288 `)
289
Cole Faust3b806d32024-03-11 15:15:03 -0700290 inputs := result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img").Implicits
Jooyung Han54f78052023-02-20 18:17:47 +0900291 android.AssertStringListContains(t, "filesystem should have libbar even for unbundled build",
292 inputs.Strings(),
293 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
294}
Jooyung Hane6067592023-03-16 13:11:17 +0900295
296func TestFileSystemWithCoverageVariants(t *testing.T) {
297 context := android.GroupFixturePreparers(
298 fixture,
299 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
300 variables.GcovCoverage = proptools.BoolPtr(true)
301 variables.Native_coverage = proptools.BoolPtr(true)
302 }),
303 )
304
305 result := context.RunTestWithBp(t, `
306 prebuilt_etc {
307 name: "prebuilt",
308 src: ":myfilesystem",
309 }
310
311 android_system_image {
312 name: "myfilesystem",
313 deps: [
314 "libfoo",
315 ],
316 linker_config_src: "linker.config.json",
317 }
318
319 cc_library {
320 name: "libfoo",
321 shared_libs: [
322 "libbar",
323 ],
324 stl: "none",
325 }
326
327 cc_library {
328 name: "libbar",
329 stl: "none",
330 }
331 `)
332
333 filesystem := result.ModuleForTests("myfilesystem", "android_common_cov")
Cole Faust3b806d32024-03-11 15:15:03 -0700334 inputs := filesystem.Output("myfilesystem.img").Implicits
Jooyung Hane6067592023-03-16 13:11:17 +0900335 android.AssertStringListContains(t, "filesystem should have libfoo(cov)",
336 inputs.Strings(),
337 "out/soong/.intermediates/libfoo/android_arm64_armv8-a_shared_cov/libfoo.so")
338 android.AssertStringListContains(t, "filesystem should have libbar(cov)",
339 inputs.Strings(),
340 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared_cov/libbar.so")
341
342 filesystemOutput := filesystem.Output("myfilesystem.img").Output
343 prebuiltInput := result.ModuleForTests("prebuilt", "android_arm64_armv8-a").Rule("Cp").Input
344 if filesystemOutput != prebuiltInput {
345 t.Error("prebuilt should use cov variant of filesystem")
346 }
347}