| Jooyung Han | 9706cbc | 2021-04-15 22:43:48 +0900 | [diff] [blame] | 1 | // 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 |  | 
|  | 15 | package filesystem | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "os" | 
| Cole Faust | 4a2a7c9 | 2024-03-12 12:44:40 -0700 | [diff] [blame] | 19 | "path/filepath" | 
| Jooyung Han | 9706cbc | 2021-04-15 22:43:48 +0900 | [diff] [blame] | 20 | "testing" | 
|  | 21 |  | 
|  | 22 | "android/soong/android" | 
| Jiyong Park | 06c4cdc | 2024-02-16 15:35:03 +0900 | [diff] [blame] | 23 | "android/soong/bpf" | 
| Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 24 | "android/soong/cc" | 
| Jooyung Han | e606759 | 2023-03-16 13:11:17 +0900 | [diff] [blame] | 25 | "android/soong/etc" | 
|  | 26 |  | 
|  | 27 | "github.com/google/blueprint/proptools" | 
| Jooyung Han | 9706cbc | 2021-04-15 22:43:48 +0900 | [diff] [blame] | 28 | ) | 
|  | 29 |  | 
|  | 30 | func TestMain(m *testing.M) { | 
|  | 31 | os.Exit(m.Run()) | 
|  | 32 | } | 
|  | 33 |  | 
|  | 34 | var fixture = android.GroupFixturePreparers( | 
|  | 35 | android.PrepareForIntegrationTestWithAndroid, | 
| Jiyong Park | 06c4cdc | 2024-02-16 15:35:03 +0900 | [diff] [blame] | 36 | bpf.PrepareForTestWithBpf, | 
| Jooyung Han | e606759 | 2023-03-16 13:11:17 +0900 | [diff] [blame] | 37 | etc.PrepareForTestWithPrebuiltEtc, | 
| Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 38 | cc.PrepareForIntegrationTestWithCc, | 
| Jooyung Han | 9706cbc | 2021-04-15 22:43:48 +0900 | [diff] [blame] | 39 | PrepareForTestWithFilesystemBuildComponents, | 
|  | 40 | ) | 
|  | 41 |  | 
|  | 42 | func TestFileSystemDeps(t *testing.T) { | 
|  | 43 | result := fixture.RunTestWithBp(t, ` | 
|  | 44 | android_filesystem { | 
|  | 45 | name: "myfilesystem", | 
| Jiyong Park | 06c4cdc | 2024-02-16 15:35:03 +0900 | [diff] [blame] | 46 | multilib: { | 
|  | 47 | common: { | 
|  | 48 | deps: [ | 
|  | 49 | "bpf.o", | 
|  | 50 | ], | 
|  | 51 | }, | 
| Jiyong Park | eec7c38 | 2024-02-16 16:10:13 +0900 | [diff] [blame] | 52 | lib32: { | 
|  | 53 | deps: [ | 
|  | 54 | "foo", | 
|  | 55 | "libbar", | 
|  | 56 | ], | 
|  | 57 | }, | 
|  | 58 | lib64: { | 
|  | 59 | deps: [ | 
|  | 60 | "libbar", | 
|  | 61 | ], | 
|  | 62 | }, | 
| Jiyong Park | 06c4cdc | 2024-02-16 15:35:03 +0900 | [diff] [blame] | 63 | }, | 
| Jiyong Park | eec7c38 | 2024-02-16 16:10:13 +0900 | [diff] [blame] | 64 | compile_multilib: "both", | 
| Jiyong Park | 06c4cdc | 2024-02-16 15:35:03 +0900 | [diff] [blame] | 65 | } | 
|  | 66 |  | 
|  | 67 | bpf { | 
|  | 68 | name: "bpf.o", | 
|  | 69 | srcs: ["bpf.c"], | 
| Jooyung Han | 9706cbc | 2021-04-15 22:43:48 +0900 | [diff] [blame] | 70 | } | 
| Jiyong Park | eec7c38 | 2024-02-16 16:10:13 +0900 | [diff] [blame] | 71 |  | 
|  | 72 | cc_binary { | 
|  | 73 | name: "foo", | 
|  | 74 | compile_multilib: "prefer32", | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | cc_library { | 
|  | 78 | name: "libbar", | 
|  | 79 | } | 
| Jooyung Han | 9706cbc | 2021-04-15 22:43:48 +0900 | [diff] [blame] | 80 | `) | 
|  | 81 |  | 
|  | 82 | // produces "myfilesystem.img" | 
|  | 83 | result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img") | 
| Jiyong Park | 06c4cdc | 2024-02-16 15:35:03 +0900 | [diff] [blame] | 84 |  | 
|  | 85 | fs := result.ModuleForTests("myfilesystem", "android_common").Module().(*filesystem) | 
| Jiyong Park | eec7c38 | 2024-02-16 16:10:13 +0900 | [diff] [blame] | 86 | expected := []string{ | 
|  | 87 | "bin/foo", | 
|  | 88 | "lib/libbar.so", | 
|  | 89 | "lib64/libbar.so", | 
|  | 90 | "etc/bpf/bpf.o", | 
|  | 91 | } | 
| Jiyong Park | 06c4cdc | 2024-02-16 15:35:03 +0900 | [diff] [blame] | 92 | for _, e := range expected { | 
|  | 93 | android.AssertStringListContains(t, "missing entry", fs.entries, e) | 
|  | 94 | } | 
| Jooyung Han | 9706cbc | 2021-04-15 22:43:48 +0900 | [diff] [blame] | 95 | } | 
| Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 96 |  | 
| Cole Faust | 4a2a7c9 | 2024-03-12 12:44:40 -0700 | [diff] [blame] | 97 | func 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 Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 113 | func TestFileSystemFillsLinkerConfigWithStubLibs(t *testing.T) { | 
|  | 114 | result := fixture.RunTestWithBp(t, ` | 
| Jooyung Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 115 | android_system_image { | 
| Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 116 | name: "myfilesystem", | 
|  | 117 | deps: [ | 
|  | 118 | "libfoo", | 
| Jooyung Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 119 | "libbar", | 
| Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 120 | ], | 
|  | 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 Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 144 |  | 
|  | 145 | func registerComponent(ctx android.RegistrationContext) { | 
|  | 146 | ctx.RegisterModuleType("component", componentFactory) | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | func componentFactory() android.Module { | 
|  | 150 | m := &component{} | 
|  | 151 | m.AddProperties(&m.properties) | 
|  | 152 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) | 
|  | 153 | return m | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | type component struct { | 
|  | 157 | android.ModuleBase | 
|  | 158 | properties struct { | 
|  | 159 | Install_copy_in_data []string | 
|  | 160 | } | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | func (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 |  | 
|  | 174 | func 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 Park | bc48548 | 2022-11-15 22:31:49 +0900 | [diff] [blame] | 195 |  | 
| Alice Wang | 000e3a3 | 2023-01-03 16:11:20 +0000 | [diff] [blame] | 196 | func 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 Park | bc48548 | 2022-11-15 22:31:49 +0900 | [diff] [blame] | 215 | func TestAvbAddHashFooter(t *testing.T) { | 
|  | 216 | result := fixture.RunTestWithBp(t, ` | 
| Alice Wang | 000e3a3 | 2023-01-03 16:11:20 +0000 | [diff] [blame] | 217 | avb_gen_vbmeta_image { | 
|  | 218 | name: "input_hashdesc", | 
|  | 219 | src: "input.img", | 
|  | 220 | partition_name: "input", | 
|  | 221 | salt: "2222", | 
|  | 222 | } | 
|  | 223 |  | 
| Jiyong Park | bc48548 | 2022-11-15 22:31:49 +0900 | [diff] [blame] | 224 | 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 Wang | 000e3a3 | 2023-01-03 16:11:20 +0000 | [diff] [blame] | 241 | include_descriptors_from_images: ["input_hashdesc"], | 
| Jiyong Park | bc48548 | 2022-11-15 22:31:49 +0900 | [diff] [blame] | 242 | } | 
|  | 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 Wang | 000e3a3 | 2023-01-03 16:11:20 +0000 | [diff] [blame] | 255 | android.AssertStringDoesContain(t, "Can't find --include_descriptors_from_image", | 
|  | 256 | cmd, "--include_descriptors_from_image ") | 
| Jiyong Park | bc48548 | 2022-11-15 22:31:49 +0900 | [diff] [blame] | 257 | } | 
| Jooyung Han | 54f7805 | 2023-02-20 18:17:47 +0900 | [diff] [blame] | 258 |  | 
|  | 259 | func 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 Faust | 3b806d3 | 2024-03-11 15:15:03 -0700 | [diff] [blame] | 290 | inputs := result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img").Implicits | 
| Jooyung Han | 54f7805 | 2023-02-20 18:17:47 +0900 | [diff] [blame] | 291 | 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 Han | e606759 | 2023-03-16 13:11:17 +0900 | [diff] [blame] | 295 |  | 
|  | 296 | func 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 Faust | 3b806d3 | 2024-03-11 15:15:03 -0700 | [diff] [blame] | 334 | inputs := filesystem.Output("myfilesystem.img").Implicits | 
| Jooyung Han | e606759 | 2023-03-16 13:11:17 +0900 | [diff] [blame] | 335 | 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 | } |