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