blob: c4481051788b72d6337a059ac3feee3711085a2d [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"
19 "testing"
20
21 "android/soong/android"
Jiyong Park06c4cdc2024-02-16 15:35:03 +090022 "android/soong/bpf"
Jiyong Parkfa616132021-04-20 11:36:40 +090023 "android/soong/cc"
Jooyung Hane6067592023-03-16 13:11:17 +090024 "android/soong/etc"
25
26 "github.com/google/blueprint/proptools"
Jooyung Han9706cbc2021-04-15 22:43:48 +090027)
28
29func TestMain(m *testing.M) {
30 os.Exit(m.Run())
31}
32
33var fixture = android.GroupFixturePreparers(
34 android.PrepareForIntegrationTestWithAndroid,
Jiyong Park06c4cdc2024-02-16 15:35:03 +090035 bpf.PrepareForTestWithBpf,
Jooyung Hane6067592023-03-16 13:11:17 +090036 etc.PrepareForTestWithPrebuiltEtc,
Jiyong Parkfa616132021-04-20 11:36:40 +090037 cc.PrepareForIntegrationTestWithCc,
Jooyung Han9706cbc2021-04-15 22:43:48 +090038 PrepareForTestWithFilesystemBuildComponents,
39)
40
41func TestFileSystemDeps(t *testing.T) {
42 result := fixture.RunTestWithBp(t, `
43 android_filesystem {
44 name: "myfilesystem",
Jiyong Park06c4cdc2024-02-16 15:35:03 +090045 multilib: {
46 common: {
47 deps: [
48 "bpf.o",
49 ],
50 },
Jiyong Parkeec7c382024-02-16 16:10:13 +090051 lib32: {
52 deps: [
53 "foo",
54 "libbar",
55 ],
56 },
57 lib64: {
58 deps: [
59 "libbar",
60 ],
61 },
Jiyong Park06c4cdc2024-02-16 15:35:03 +090062 },
Jiyong Parkeec7c382024-02-16 16:10:13 +090063 compile_multilib: "both",
Jiyong Park06c4cdc2024-02-16 15:35:03 +090064 }
65
66 bpf {
67 name: "bpf.o",
68 srcs: ["bpf.c"],
Jooyung Han9706cbc2021-04-15 22:43:48 +090069 }
Jiyong Parkeec7c382024-02-16 16:10:13 +090070
71 cc_binary {
72 name: "foo",
73 compile_multilib: "prefer32",
74 }
75
76 cc_library {
77 name: "libbar",
78 }
Jooyung Han9706cbc2021-04-15 22:43:48 +090079 `)
80
81 // produces "myfilesystem.img"
82 result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img")
Jiyong Park06c4cdc2024-02-16 15:35:03 +090083
84 fs := result.ModuleForTests("myfilesystem", "android_common").Module().(*filesystem)
Jiyong Parkeec7c382024-02-16 16:10:13 +090085 expected := []string{
86 "bin/foo",
87 "lib/libbar.so",
88 "lib64/libbar.so",
89 "etc/bpf/bpf.o",
90 }
Jiyong Park06c4cdc2024-02-16 15:35:03 +090091 for _, e := range expected {
92 android.AssertStringListContains(t, "missing entry", fs.entries, e)
93 }
Jooyung Han9706cbc2021-04-15 22:43:48 +090094}
Jiyong Parkfa616132021-04-20 11:36:40 +090095
96func TestFileSystemFillsLinkerConfigWithStubLibs(t *testing.T) {
97 result := fixture.RunTestWithBp(t, `
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090098 android_system_image {
Jiyong Parkfa616132021-04-20 11:36:40 +090099 name: "myfilesystem",
100 deps: [
101 "libfoo",
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900102 "libbar",
Jiyong Parkfa616132021-04-20 11:36:40 +0900103 ],
104 linker_config_src: "linker.config.json",
105 }
106
107 cc_library {
108 name: "libfoo",
109 stubs: {
110 symbol_file: "libfoo.map.txt",
111 },
112 }
113
114 cc_library {
115 name: "libbar",
116 }
117 `)
118
119 module := result.ModuleForTests("myfilesystem", "android_common")
120 output := module.Output("system/etc/linker.config.pb")
121
122 android.AssertStringDoesContain(t, "linker.config.pb should have libfoo",
123 output.RuleParams.Command, "libfoo.so")
124 android.AssertStringDoesNotContain(t, "linker.config.pb should not have libbar",
125 output.RuleParams.Command, "libbar.so")
126}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900127
128func registerComponent(ctx android.RegistrationContext) {
129 ctx.RegisterModuleType("component", componentFactory)
130}
131
132func componentFactory() android.Module {
133 m := &component{}
134 m.AddProperties(&m.properties)
135 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
136 return m
137}
138
139type component struct {
140 android.ModuleBase
141 properties struct {
142 Install_copy_in_data []string
143 }
144}
145
146func (c *component) GenerateAndroidBuildActions(ctx android.ModuleContext) {
147 output := android.PathForModuleOut(ctx, c.Name())
148 dir := android.PathForModuleInstall(ctx, "components")
149 ctx.InstallFile(dir, c.Name(), output)
150
151 dataDir := android.PathForModuleInPartitionInstall(ctx, "data", "components")
152 for _, d := range c.properties.Install_copy_in_data {
153 ctx.InstallFile(dataDir, d, output)
154 }
155}
156
157func TestFileSystemGathersItemsOnlyInSystemPartition(t *testing.T) {
158 f := android.GroupFixturePreparers(fixture, android.FixtureRegisterWithContext(registerComponent))
159 result := f.RunTestWithBp(t, `
160 android_system_image {
161 name: "myfilesystem",
162 multilib: {
163 common: {
164 deps: ["foo"],
165 },
166 },
167 linker_config_src: "linker.config.json",
168 }
169 component {
170 name: "foo",
171 install_copy_in_data: ["bar"],
172 }
173 `)
174
175 module := result.ModuleForTests("myfilesystem", "android_common").Module().(*systemImage)
176 android.AssertDeepEquals(t, "entries should have foo only", []string{"components/foo"}, module.entries)
177}
Jiyong Parkbc485482022-11-15 22:31:49 +0900178
Alice Wang000e3a32023-01-03 16:11:20 +0000179func TestAvbGenVbmetaImage(t *testing.T) {
180 result := fixture.RunTestWithBp(t, `
181 avb_gen_vbmeta_image {
182 name: "input_hashdesc",
183 src: "input.img",
184 partition_name: "input_partition_name",
185 salt: "2222",
186 }`)
187 cmd := result.ModuleForTests("input_hashdesc", "android_arm64_armv8-a").Rule("avbGenVbmetaImage").RuleParams.Command
188 android.AssertStringDoesContain(t, "Can't find correct --partition_name argument",
189 cmd, "--partition_name input_partition_name")
190 android.AssertStringDoesContain(t, "Can't find --do_not_append_vbmeta_image",
191 cmd, "--do_not_append_vbmeta_image")
192 android.AssertStringDoesContain(t, "Can't find --output_vbmeta_image",
193 cmd, "--output_vbmeta_image ")
194 android.AssertStringDoesContain(t, "Can't find --salt argument",
195 cmd, "--salt 2222")
196}
197
Jiyong Parkbc485482022-11-15 22:31:49 +0900198func TestAvbAddHashFooter(t *testing.T) {
199 result := fixture.RunTestWithBp(t, `
Alice Wang000e3a32023-01-03 16:11:20 +0000200 avb_gen_vbmeta_image {
201 name: "input_hashdesc",
202 src: "input.img",
203 partition_name: "input",
204 salt: "2222",
205 }
206
Jiyong Parkbc485482022-11-15 22:31:49 +0900207 avb_add_hash_footer {
208 name: "myfooter",
209 src: "input.img",
210 filename: "output.img",
211 partition_name: "mypartition",
212 private_key: "mykey",
213 salt: "1111",
214 props: [
215 {
216 name: "prop1",
217 value: "value1",
218 },
219 {
220 name: "prop2",
221 file: "value_file",
222 },
223 ],
Alice Wang000e3a32023-01-03 16:11:20 +0000224 include_descriptors_from_images: ["input_hashdesc"],
Jiyong Parkbc485482022-11-15 22:31:49 +0900225 }
226 `)
227 cmd := result.ModuleForTests("myfooter", "android_arm64_armv8-a").Rule("avbAddHashFooter").RuleParams.Command
228 android.AssertStringDoesContain(t, "Can't find correct --partition_name argument",
229 cmd, "--partition_name mypartition")
230 android.AssertStringDoesContain(t, "Can't find correct --key argument",
231 cmd, "--key mykey")
232 android.AssertStringDoesContain(t, "Can't find --salt argument",
233 cmd, "--salt 1111")
234 android.AssertStringDoesContain(t, "Can't find --prop argument",
235 cmd, "--prop 'prop1:value1'")
236 android.AssertStringDoesContain(t, "Can't find --prop_from_file argument",
237 cmd, "--prop_from_file 'prop2:value_file'")
Alice Wang000e3a32023-01-03 16:11:20 +0000238 android.AssertStringDoesContain(t, "Can't find --include_descriptors_from_image",
239 cmd, "--include_descriptors_from_image ")
Jiyong Parkbc485482022-11-15 22:31:49 +0900240}
Jooyung Han54f78052023-02-20 18:17:47 +0900241
242func TestFileSystemShouldInstallCoreVariantIfTargetBuildAppsIsSet(t *testing.T) {
243 context := android.GroupFixturePreparers(
244 fixture,
245 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
246 variables.Unbundled_build_apps = []string{"bar"}
247 }),
248 )
249 result := context.RunTestWithBp(t, `
250 android_system_image {
251 name: "myfilesystem",
252 deps: [
253 "libfoo",
254 ],
255 linker_config_src: "linker.config.json",
256 }
257
258 cc_library {
259 name: "libfoo",
260 shared_libs: [
261 "libbar",
262 ],
263 stl: "none",
264 }
265
266 cc_library {
267 name: "libbar",
268 sdk_version: "9",
269 stl: "none",
270 }
271 `)
272
273 inputs := result.ModuleForTests("myfilesystem", "android_common").Output("deps.zip").Implicits
274 android.AssertStringListContains(t, "filesystem should have libbar even for unbundled build",
275 inputs.Strings(),
276 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
277}
Jooyung Hane6067592023-03-16 13:11:17 +0900278
279func TestFileSystemWithCoverageVariants(t *testing.T) {
280 context := android.GroupFixturePreparers(
281 fixture,
282 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
283 variables.GcovCoverage = proptools.BoolPtr(true)
284 variables.Native_coverage = proptools.BoolPtr(true)
285 }),
286 )
287
288 result := context.RunTestWithBp(t, `
289 prebuilt_etc {
290 name: "prebuilt",
291 src: ":myfilesystem",
292 }
293
294 android_system_image {
295 name: "myfilesystem",
296 deps: [
297 "libfoo",
298 ],
299 linker_config_src: "linker.config.json",
300 }
301
302 cc_library {
303 name: "libfoo",
304 shared_libs: [
305 "libbar",
306 ],
307 stl: "none",
308 }
309
310 cc_library {
311 name: "libbar",
312 stl: "none",
313 }
314 `)
315
316 filesystem := result.ModuleForTests("myfilesystem", "android_common_cov")
317 inputs := filesystem.Output("deps.zip").Implicits
318 android.AssertStringListContains(t, "filesystem should have libfoo(cov)",
319 inputs.Strings(),
320 "out/soong/.intermediates/libfoo/android_arm64_armv8-a_shared_cov/libfoo.so")
321 android.AssertStringListContains(t, "filesystem should have libbar(cov)",
322 inputs.Strings(),
323 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared_cov/libbar.so")
324
325 filesystemOutput := filesystem.Output("myfilesystem.img").Output
326 prebuiltInput := result.ModuleForTests("prebuilt", "android_arm64_armv8-a").Rule("Cp").Input
327 if filesystemOutput != prebuiltInput {
328 t.Error("prebuilt should use cov variant of filesystem")
329 }
330}