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" |
| 19 | "testing" |
| 20 | |
| 21 | "android/soong/android" |
Jiyong Park | 06c4cdc | 2024-02-16 15:35:03 +0900 | [diff] [blame^] | 22 | "android/soong/bpf" |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 23 | "android/soong/cc" |
Jooyung Han | e606759 | 2023-03-16 13:11:17 +0900 | [diff] [blame] | 24 | "android/soong/etc" |
| 25 | |
| 26 | "github.com/google/blueprint/proptools" |
Jooyung Han | 9706cbc | 2021-04-15 22:43:48 +0900 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | func TestMain(m *testing.M) { |
| 30 | os.Exit(m.Run()) |
| 31 | } |
| 32 | |
| 33 | var fixture = android.GroupFixturePreparers( |
| 34 | android.PrepareForIntegrationTestWithAndroid, |
Jiyong Park | 06c4cdc | 2024-02-16 15:35:03 +0900 | [diff] [blame^] | 35 | bpf.PrepareForTestWithBpf, |
Jooyung Han | e606759 | 2023-03-16 13:11:17 +0900 | [diff] [blame] | 36 | etc.PrepareForTestWithPrebuiltEtc, |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 37 | cc.PrepareForIntegrationTestWithCc, |
Jooyung Han | 9706cbc | 2021-04-15 22:43:48 +0900 | [diff] [blame] | 38 | PrepareForTestWithFilesystemBuildComponents, |
| 39 | ) |
| 40 | |
| 41 | func TestFileSystemDeps(t *testing.T) { |
| 42 | result := fixture.RunTestWithBp(t, ` |
| 43 | android_filesystem { |
| 44 | name: "myfilesystem", |
Jiyong Park | 06c4cdc | 2024-02-16 15:35:03 +0900 | [diff] [blame^] | 45 | multilib: { |
| 46 | common: { |
| 47 | deps: [ |
| 48 | "bpf.o", |
| 49 | ], |
| 50 | }, |
| 51 | }, |
| 52 | } |
| 53 | |
| 54 | bpf { |
| 55 | name: "bpf.o", |
| 56 | srcs: ["bpf.c"], |
Jooyung Han | 9706cbc | 2021-04-15 22:43:48 +0900 | [diff] [blame] | 57 | } |
| 58 | `) |
| 59 | |
| 60 | // produces "myfilesystem.img" |
| 61 | result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img") |
Jiyong Park | 06c4cdc | 2024-02-16 15:35:03 +0900 | [diff] [blame^] | 62 | |
| 63 | fs := result.ModuleForTests("myfilesystem", "android_common").Module().(*filesystem) |
| 64 | expected := []string{"etc/bpf/bpf.o"} |
| 65 | for _, e := range expected { |
| 66 | android.AssertStringListContains(t, "missing entry", fs.entries, e) |
| 67 | } |
Jooyung Han | 9706cbc | 2021-04-15 22:43:48 +0900 | [diff] [blame] | 68 | } |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 69 | |
| 70 | func TestFileSystemFillsLinkerConfigWithStubLibs(t *testing.T) { |
| 71 | result := fixture.RunTestWithBp(t, ` |
Jooyung Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 72 | android_system_image { |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 73 | name: "myfilesystem", |
| 74 | deps: [ |
| 75 | "libfoo", |
Jooyung Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 76 | "libbar", |
Jiyong Park | fa61613 | 2021-04-20 11:36:40 +0900 | [diff] [blame] | 77 | ], |
| 78 | linker_config_src: "linker.config.json", |
| 79 | } |
| 80 | |
| 81 | cc_library { |
| 82 | name: "libfoo", |
| 83 | stubs: { |
| 84 | symbol_file: "libfoo.map.txt", |
| 85 | }, |
| 86 | } |
| 87 | |
| 88 | cc_library { |
| 89 | name: "libbar", |
| 90 | } |
| 91 | `) |
| 92 | |
| 93 | module := result.ModuleForTests("myfilesystem", "android_common") |
| 94 | output := module.Output("system/etc/linker.config.pb") |
| 95 | |
| 96 | android.AssertStringDoesContain(t, "linker.config.pb should have libfoo", |
| 97 | output.RuleParams.Command, "libfoo.so") |
| 98 | android.AssertStringDoesNotContain(t, "linker.config.pb should not have libbar", |
| 99 | output.RuleParams.Command, "libbar.so") |
| 100 | } |
Jooyung Han | 0fbbc2b | 2022-03-25 12:35:46 +0900 | [diff] [blame] | 101 | |
| 102 | func registerComponent(ctx android.RegistrationContext) { |
| 103 | ctx.RegisterModuleType("component", componentFactory) |
| 104 | } |
| 105 | |
| 106 | func componentFactory() android.Module { |
| 107 | m := &component{} |
| 108 | m.AddProperties(&m.properties) |
| 109 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 110 | return m |
| 111 | } |
| 112 | |
| 113 | type component struct { |
| 114 | android.ModuleBase |
| 115 | properties struct { |
| 116 | Install_copy_in_data []string |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func (c *component) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 121 | output := android.PathForModuleOut(ctx, c.Name()) |
| 122 | dir := android.PathForModuleInstall(ctx, "components") |
| 123 | ctx.InstallFile(dir, c.Name(), output) |
| 124 | |
| 125 | dataDir := android.PathForModuleInPartitionInstall(ctx, "data", "components") |
| 126 | for _, d := range c.properties.Install_copy_in_data { |
| 127 | ctx.InstallFile(dataDir, d, output) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func TestFileSystemGathersItemsOnlyInSystemPartition(t *testing.T) { |
| 132 | f := android.GroupFixturePreparers(fixture, android.FixtureRegisterWithContext(registerComponent)) |
| 133 | result := f.RunTestWithBp(t, ` |
| 134 | android_system_image { |
| 135 | name: "myfilesystem", |
| 136 | multilib: { |
| 137 | common: { |
| 138 | deps: ["foo"], |
| 139 | }, |
| 140 | }, |
| 141 | linker_config_src: "linker.config.json", |
| 142 | } |
| 143 | component { |
| 144 | name: "foo", |
| 145 | install_copy_in_data: ["bar"], |
| 146 | } |
| 147 | `) |
| 148 | |
| 149 | module := result.ModuleForTests("myfilesystem", "android_common").Module().(*systemImage) |
| 150 | android.AssertDeepEquals(t, "entries should have foo only", []string{"components/foo"}, module.entries) |
| 151 | } |
Jiyong Park | bc48548 | 2022-11-15 22:31:49 +0900 | [diff] [blame] | 152 | |
Alice Wang | 000e3a3 | 2023-01-03 16:11:20 +0000 | [diff] [blame] | 153 | func TestAvbGenVbmetaImage(t *testing.T) { |
| 154 | result := fixture.RunTestWithBp(t, ` |
| 155 | avb_gen_vbmeta_image { |
| 156 | name: "input_hashdesc", |
| 157 | src: "input.img", |
| 158 | partition_name: "input_partition_name", |
| 159 | salt: "2222", |
| 160 | }`) |
| 161 | cmd := result.ModuleForTests("input_hashdesc", "android_arm64_armv8-a").Rule("avbGenVbmetaImage").RuleParams.Command |
| 162 | android.AssertStringDoesContain(t, "Can't find correct --partition_name argument", |
| 163 | cmd, "--partition_name input_partition_name") |
| 164 | android.AssertStringDoesContain(t, "Can't find --do_not_append_vbmeta_image", |
| 165 | cmd, "--do_not_append_vbmeta_image") |
| 166 | android.AssertStringDoesContain(t, "Can't find --output_vbmeta_image", |
| 167 | cmd, "--output_vbmeta_image ") |
| 168 | android.AssertStringDoesContain(t, "Can't find --salt argument", |
| 169 | cmd, "--salt 2222") |
| 170 | } |
| 171 | |
Jiyong Park | bc48548 | 2022-11-15 22:31:49 +0900 | [diff] [blame] | 172 | func TestAvbAddHashFooter(t *testing.T) { |
| 173 | result := fixture.RunTestWithBp(t, ` |
Alice Wang | 000e3a3 | 2023-01-03 16:11:20 +0000 | [diff] [blame] | 174 | avb_gen_vbmeta_image { |
| 175 | name: "input_hashdesc", |
| 176 | src: "input.img", |
| 177 | partition_name: "input", |
| 178 | salt: "2222", |
| 179 | } |
| 180 | |
Jiyong Park | bc48548 | 2022-11-15 22:31:49 +0900 | [diff] [blame] | 181 | avb_add_hash_footer { |
| 182 | name: "myfooter", |
| 183 | src: "input.img", |
| 184 | filename: "output.img", |
| 185 | partition_name: "mypartition", |
| 186 | private_key: "mykey", |
| 187 | salt: "1111", |
| 188 | props: [ |
| 189 | { |
| 190 | name: "prop1", |
| 191 | value: "value1", |
| 192 | }, |
| 193 | { |
| 194 | name: "prop2", |
| 195 | file: "value_file", |
| 196 | }, |
| 197 | ], |
Alice Wang | 000e3a3 | 2023-01-03 16:11:20 +0000 | [diff] [blame] | 198 | include_descriptors_from_images: ["input_hashdesc"], |
Jiyong Park | bc48548 | 2022-11-15 22:31:49 +0900 | [diff] [blame] | 199 | } |
| 200 | `) |
| 201 | cmd := result.ModuleForTests("myfooter", "android_arm64_armv8-a").Rule("avbAddHashFooter").RuleParams.Command |
| 202 | android.AssertStringDoesContain(t, "Can't find correct --partition_name argument", |
| 203 | cmd, "--partition_name mypartition") |
| 204 | android.AssertStringDoesContain(t, "Can't find correct --key argument", |
| 205 | cmd, "--key mykey") |
| 206 | android.AssertStringDoesContain(t, "Can't find --salt argument", |
| 207 | cmd, "--salt 1111") |
| 208 | android.AssertStringDoesContain(t, "Can't find --prop argument", |
| 209 | cmd, "--prop 'prop1:value1'") |
| 210 | android.AssertStringDoesContain(t, "Can't find --prop_from_file argument", |
| 211 | cmd, "--prop_from_file 'prop2:value_file'") |
Alice Wang | 000e3a3 | 2023-01-03 16:11:20 +0000 | [diff] [blame] | 212 | android.AssertStringDoesContain(t, "Can't find --include_descriptors_from_image", |
| 213 | cmd, "--include_descriptors_from_image ") |
Jiyong Park | bc48548 | 2022-11-15 22:31:49 +0900 | [diff] [blame] | 214 | } |
Jooyung Han | 54f7805 | 2023-02-20 18:17:47 +0900 | [diff] [blame] | 215 | |
| 216 | func TestFileSystemShouldInstallCoreVariantIfTargetBuildAppsIsSet(t *testing.T) { |
| 217 | context := android.GroupFixturePreparers( |
| 218 | fixture, |
| 219 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 220 | variables.Unbundled_build_apps = []string{"bar"} |
| 221 | }), |
| 222 | ) |
| 223 | result := context.RunTestWithBp(t, ` |
| 224 | android_system_image { |
| 225 | name: "myfilesystem", |
| 226 | deps: [ |
| 227 | "libfoo", |
| 228 | ], |
| 229 | linker_config_src: "linker.config.json", |
| 230 | } |
| 231 | |
| 232 | cc_library { |
| 233 | name: "libfoo", |
| 234 | shared_libs: [ |
| 235 | "libbar", |
| 236 | ], |
| 237 | stl: "none", |
| 238 | } |
| 239 | |
| 240 | cc_library { |
| 241 | name: "libbar", |
| 242 | sdk_version: "9", |
| 243 | stl: "none", |
| 244 | } |
| 245 | `) |
| 246 | |
| 247 | inputs := result.ModuleForTests("myfilesystem", "android_common").Output("deps.zip").Implicits |
| 248 | android.AssertStringListContains(t, "filesystem should have libbar even for unbundled build", |
| 249 | inputs.Strings(), |
| 250 | "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so") |
| 251 | } |
Jooyung Han | e606759 | 2023-03-16 13:11:17 +0900 | [diff] [blame] | 252 | |
| 253 | func TestFileSystemWithCoverageVariants(t *testing.T) { |
| 254 | context := android.GroupFixturePreparers( |
| 255 | fixture, |
| 256 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 257 | variables.GcovCoverage = proptools.BoolPtr(true) |
| 258 | variables.Native_coverage = proptools.BoolPtr(true) |
| 259 | }), |
| 260 | ) |
| 261 | |
| 262 | result := context.RunTestWithBp(t, ` |
| 263 | prebuilt_etc { |
| 264 | name: "prebuilt", |
| 265 | src: ":myfilesystem", |
| 266 | } |
| 267 | |
| 268 | android_system_image { |
| 269 | name: "myfilesystem", |
| 270 | deps: [ |
| 271 | "libfoo", |
| 272 | ], |
| 273 | linker_config_src: "linker.config.json", |
| 274 | } |
| 275 | |
| 276 | cc_library { |
| 277 | name: "libfoo", |
| 278 | shared_libs: [ |
| 279 | "libbar", |
| 280 | ], |
| 281 | stl: "none", |
| 282 | } |
| 283 | |
| 284 | cc_library { |
| 285 | name: "libbar", |
| 286 | stl: "none", |
| 287 | } |
| 288 | `) |
| 289 | |
| 290 | filesystem := result.ModuleForTests("myfilesystem", "android_common_cov") |
| 291 | inputs := filesystem.Output("deps.zip").Implicits |
| 292 | android.AssertStringListContains(t, "filesystem should have libfoo(cov)", |
| 293 | inputs.Strings(), |
| 294 | "out/soong/.intermediates/libfoo/android_arm64_armv8-a_shared_cov/libfoo.so") |
| 295 | android.AssertStringListContains(t, "filesystem should have libbar(cov)", |
| 296 | inputs.Strings(), |
| 297 | "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared_cov/libbar.so") |
| 298 | |
| 299 | filesystemOutput := filesystem.Output("myfilesystem.img").Output |
| 300 | prebuiltInput := result.ModuleForTests("prebuilt", "android_arm64_armv8-a").Rule("Cp").Input |
| 301 | if filesystemOutput != prebuiltInput { |
| 302 | t.Error("prebuilt should use cov variant of filesystem") |
| 303 | } |
| 304 | } |