Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 1 | package android |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/google/blueprint" |
| 7 | ) |
| 8 | |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 9 | var prepareForTestWithLicenses = GroupFixturePreparers( |
| 10 | FixtureRegisterWithContext(RegisterLicenseKindBuildComponents), |
| 11 | FixtureRegisterWithContext(RegisterLicenseBuildComponents), |
| 12 | FixtureRegisterWithContext(registerLicenseMutators), |
| 13 | ) |
| 14 | |
| 15 | func registerLicenseMutators(ctx RegistrationContext) { |
| 16 | ctx.PreArchMutators(RegisterLicensesPackageMapper) |
| 17 | ctx.PreArchMutators(RegisterLicensesPropertyGatherer) |
| 18 | ctx.PostDepsMutators(RegisterLicensesDependencyChecker) |
| 19 | } |
| 20 | |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 21 | var licensesTests = []struct { |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 22 | name string |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 23 | fs MockFS |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 24 | expectedErrors []string |
| 25 | effectiveLicenses map[string][]string |
| 26 | effectiveInheritedLicenses map[string][]string |
| 27 | effectivePackage map[string]string |
| 28 | effectiveNotices map[string][]string |
| 29 | effectiveKinds map[string][]string |
| 30 | effectiveConditions map[string][]string |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 31 | }{ |
| 32 | { |
| 33 | name: "invalid module type without licenses property", |
| 34 | fs: map[string][]byte{ |
| 35 | "top/Blueprints": []byte(` |
| 36 | mock_bad_module { |
| 37 | name: "libexample", |
| 38 | }`), |
| 39 | }, |
| 40 | expectedErrors: []string{`module type "mock_bad_module" must have an applicable licenses property`}, |
| 41 | }, |
| 42 | { |
| 43 | name: "license must exist", |
| 44 | fs: map[string][]byte{ |
| 45 | "top/Blueprints": []byte(` |
| 46 | mock_library { |
| 47 | name: "libexample", |
| 48 | licenses: ["notice"], |
| 49 | }`), |
| 50 | }, |
| 51 | expectedErrors: []string{`"libexample" depends on undefined module "notice"`}, |
| 52 | }, |
| 53 | { |
| 54 | name: "all good", |
| 55 | fs: map[string][]byte{ |
| 56 | "top/Blueprints": []byte(` |
| 57 | license_kind { |
| 58 | name: "notice", |
| 59 | conditions: ["shownotice"], |
| 60 | } |
| 61 | |
| 62 | license { |
| 63 | name: "top_Apache2", |
| 64 | license_kinds: ["notice"], |
| 65 | package_name: "topDog", |
| 66 | license_text: ["LICENSE", "NOTICE"], |
| 67 | } |
| 68 | |
| 69 | mock_library { |
| 70 | name: "libexample1", |
| 71 | licenses: ["top_Apache2"], |
| 72 | }`), |
| 73 | "top/nested/Blueprints": []byte(` |
| 74 | mock_library { |
| 75 | name: "libnested", |
| 76 | licenses: ["top_Apache2"], |
| 77 | }`), |
| 78 | "other/Blueprints": []byte(` |
| 79 | mock_library { |
| 80 | name: "libother", |
| 81 | licenses: ["top_Apache2"], |
| 82 | }`), |
| 83 | }, |
| 84 | effectiveLicenses: map[string][]string{ |
| 85 | "libexample1": []string{"top_Apache2"}, |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 86 | "libnested": []string{"top_Apache2"}, |
| 87 | "libother": []string{"top_Apache2"}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 88 | }, |
| 89 | effectiveKinds: map[string][]string{ |
| 90 | "libexample1": []string{"notice"}, |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 91 | "libnested": []string{"notice"}, |
| 92 | "libother": []string{"notice"}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 93 | }, |
| 94 | effectivePackage: map[string]string{ |
| 95 | "libexample1": "topDog", |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 96 | "libnested": "topDog", |
| 97 | "libother": "topDog", |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 98 | }, |
| 99 | effectiveConditions: map[string][]string{ |
| 100 | "libexample1": []string{"shownotice"}, |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 101 | "libnested": []string{"shownotice"}, |
| 102 | "libother": []string{"shownotice"}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 103 | }, |
| 104 | effectiveNotices: map[string][]string{ |
| 105 | "libexample1": []string{"top/LICENSE", "top/NOTICE"}, |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 106 | "libnested": []string{"top/LICENSE", "top/NOTICE"}, |
| 107 | "libother": []string{"top/LICENSE", "top/NOTICE"}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 108 | }, |
| 109 | }, |
| 110 | |
| 111 | // Defaults propagation tests |
| 112 | { |
| 113 | // Check that licenses is the union of the defaults modules. |
| 114 | name: "defaults union, basic", |
| 115 | fs: map[string][]byte{ |
| 116 | "top/Blueprints": []byte(` |
| 117 | license_kind { |
| 118 | name: "top_notice", |
| 119 | conditions: ["notice"], |
| 120 | } |
| 121 | |
| 122 | license { |
| 123 | name: "top_other", |
| 124 | license_kinds: ["top_notice"], |
| 125 | } |
| 126 | |
| 127 | mock_defaults { |
| 128 | name: "libexample_defaults", |
| 129 | licenses: ["top_other"], |
| 130 | } |
| 131 | mock_library { |
| 132 | name: "libexample", |
| 133 | licenses: ["nested_other"], |
| 134 | defaults: ["libexample_defaults"], |
| 135 | } |
| 136 | mock_library { |
| 137 | name: "libsamepackage", |
| 138 | deps: ["libexample"], |
| 139 | }`), |
| 140 | "top/nested/Blueprints": []byte(` |
| 141 | license_kind { |
| 142 | name: "nested_notice", |
| 143 | conditions: ["notice"], |
| 144 | } |
| 145 | |
| 146 | license { |
| 147 | name: "nested_other", |
| 148 | license_kinds: ["nested_notice"], |
| 149 | } |
| 150 | |
| 151 | mock_library { |
| 152 | name: "libnested", |
| 153 | deps: ["libexample"], |
| 154 | }`), |
| 155 | "other/Blueprints": []byte(` |
| 156 | mock_library { |
| 157 | name: "libother", |
| 158 | deps: ["libexample"], |
| 159 | }`), |
| 160 | }, |
| 161 | effectiveLicenses: map[string][]string{ |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 162 | "libexample": []string{"nested_other", "top_other"}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 163 | "libsamepackage": []string{}, |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 164 | "libnested": []string{}, |
| 165 | "libother": []string{}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 166 | }, |
| 167 | effectiveInheritedLicenses: map[string][]string{ |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 168 | "libexample": []string{"nested_other", "top_other"}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 169 | "libsamepackage": []string{"nested_other", "top_other"}, |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 170 | "libnested": []string{"nested_other", "top_other"}, |
| 171 | "libother": []string{"nested_other", "top_other"}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 172 | }, |
| 173 | effectiveKinds: map[string][]string{ |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 174 | "libexample": []string{"nested_notice", "top_notice"}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 175 | "libsamepackage": []string{}, |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 176 | "libnested": []string{}, |
| 177 | "libother": []string{}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 178 | }, |
| 179 | effectiveConditions: map[string][]string{ |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 180 | "libexample": []string{"notice"}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 181 | "libsamepackage": []string{}, |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 182 | "libnested": []string{}, |
| 183 | "libother": []string{}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 184 | }, |
| 185 | }, |
| 186 | { |
| 187 | name: "defaults union, multiple defaults", |
| 188 | fs: map[string][]byte{ |
| 189 | "top/Blueprints": []byte(` |
| 190 | license { |
| 191 | name: "top", |
| 192 | } |
| 193 | mock_defaults { |
| 194 | name: "libexample_defaults_1", |
| 195 | licenses: ["other"], |
| 196 | } |
| 197 | mock_defaults { |
| 198 | name: "libexample_defaults_2", |
| 199 | licenses: ["top_nested"], |
| 200 | } |
| 201 | mock_library { |
| 202 | name: "libexample", |
| 203 | defaults: ["libexample_defaults_1", "libexample_defaults_2"], |
| 204 | } |
| 205 | mock_library { |
| 206 | name: "libsamepackage", |
| 207 | deps: ["libexample"], |
| 208 | }`), |
| 209 | "top/nested/Blueprints": []byte(` |
| 210 | license { |
| 211 | name: "top_nested", |
| 212 | license_text: ["LICENSE.txt"], |
| 213 | } |
| 214 | mock_library { |
| 215 | name: "libnested", |
| 216 | deps: ["libexample"], |
| 217 | }`), |
| 218 | "other/Blueprints": []byte(` |
| 219 | license { |
| 220 | name: "other", |
| 221 | } |
| 222 | mock_library { |
| 223 | name: "libother", |
| 224 | deps: ["libexample"], |
| 225 | }`), |
| 226 | "outsider/Blueprints": []byte(` |
| 227 | mock_library { |
| 228 | name: "liboutsider", |
| 229 | deps: ["libexample"], |
| 230 | }`), |
| 231 | }, |
| 232 | effectiveLicenses: map[string][]string{ |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 233 | "libexample": []string{"other", "top_nested"}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 234 | "libsamepackage": []string{}, |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 235 | "libnested": []string{}, |
| 236 | "libother": []string{}, |
| 237 | "liboutsider": []string{}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 238 | }, |
| 239 | effectiveInheritedLicenses: map[string][]string{ |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 240 | "libexample": []string{"other", "top_nested"}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 241 | "libsamepackage": []string{"other", "top_nested"}, |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 242 | "libnested": []string{"other", "top_nested"}, |
| 243 | "libother": []string{"other", "top_nested"}, |
| 244 | "liboutsider": []string{"other", "top_nested"}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 245 | }, |
| 246 | effectiveKinds: map[string][]string{ |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 247 | "libexample": []string{}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 248 | "libsamepackage": []string{}, |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 249 | "libnested": []string{}, |
| 250 | "libother": []string{}, |
| 251 | "liboutsider": []string{}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 252 | }, |
| 253 | effectiveNotices: map[string][]string{ |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 254 | "libexample": []string{"top/nested/LICENSE.txt"}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 255 | "libsamepackage": []string{}, |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 256 | "libnested": []string{}, |
| 257 | "libother": []string{}, |
| 258 | "liboutsider": []string{}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 259 | }, |
| 260 | }, |
| 261 | |
| 262 | // Defaults module's defaults_licenses tests |
| 263 | { |
| 264 | name: "defaults_licenses invalid", |
| 265 | fs: map[string][]byte{ |
| 266 | "top/Blueprints": []byte(` |
| 267 | mock_defaults { |
| 268 | name: "top_defaults", |
| 269 | licenses: ["notice"], |
| 270 | }`), |
| 271 | }, |
| 272 | expectedErrors: []string{`"top_defaults" depends on undefined module "notice"`}, |
| 273 | }, |
| 274 | { |
| 275 | name: "defaults_licenses overrides package default", |
| 276 | fs: map[string][]byte{ |
| 277 | "top/Blueprints": []byte(` |
| 278 | package { |
| 279 | default_applicable_licenses: ["by_exception_only"], |
| 280 | } |
| 281 | license { |
| 282 | name: "by_exception_only", |
| 283 | } |
| 284 | license { |
| 285 | name: "notice", |
| 286 | } |
| 287 | mock_defaults { |
| 288 | name: "top_defaults", |
| 289 | licenses: ["notice"], |
| 290 | } |
| 291 | mock_library { |
| 292 | name: "libexample", |
| 293 | } |
| 294 | mock_library { |
| 295 | name: "libdefaults", |
| 296 | defaults: ["top_defaults"], |
| 297 | }`), |
| 298 | }, |
| 299 | effectiveLicenses: map[string][]string{ |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 300 | "libexample": []string{"by_exception_only"}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 301 | "libdefaults": []string{"notice"}, |
| 302 | }, |
| 303 | effectiveInheritedLicenses: map[string][]string{ |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 304 | "libexample": []string{"by_exception_only"}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 305 | "libdefaults": []string{"notice"}, |
| 306 | }, |
| 307 | }, |
| 308 | |
| 309 | // Package default_applicable_licenses tests |
| 310 | { |
| 311 | name: "package default_applicable_licenses must exist", |
| 312 | fs: map[string][]byte{ |
| 313 | "top/Blueprints": []byte(` |
| 314 | package { |
| 315 | default_applicable_licenses: ["notice"], |
| 316 | }`), |
| 317 | }, |
| 318 | expectedErrors: []string{`"//top" depends on undefined module "notice"`}, |
| 319 | }, |
| 320 | { |
| 321 | // This test relies on the default licenses being legacy_public. |
| 322 | name: "package default_applicable_licenses property used when no licenses specified", |
| 323 | fs: map[string][]byte{ |
| 324 | "top/Blueprints": []byte(` |
| 325 | package { |
| 326 | default_applicable_licenses: ["top_notice"], |
| 327 | } |
| 328 | |
| 329 | license { |
| 330 | name: "top_notice", |
| 331 | } |
| 332 | mock_library { |
| 333 | name: "libexample", |
| 334 | }`), |
| 335 | "outsider/Blueprints": []byte(` |
| 336 | mock_library { |
| 337 | name: "liboutsider", |
| 338 | deps: ["libexample"], |
| 339 | }`), |
| 340 | }, |
| 341 | effectiveLicenses: map[string][]string{ |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 342 | "libexample": []string{"top_notice"}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 343 | "liboutsider": []string{}, |
| 344 | }, |
| 345 | effectiveInheritedLicenses: map[string][]string{ |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 346 | "libexample": []string{"top_notice"}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 347 | "liboutsider": []string{"top_notice"}, |
| 348 | }, |
| 349 | }, |
| 350 | { |
| 351 | name: "package default_applicable_licenses not inherited to subpackages", |
| 352 | fs: map[string][]byte{ |
| 353 | "top/Blueprints": []byte(` |
| 354 | package { |
| 355 | default_applicable_licenses: ["top_notice"], |
| 356 | } |
| 357 | license { |
| 358 | name: "top_notice", |
| 359 | } |
| 360 | mock_library { |
| 361 | name: "libexample", |
| 362 | }`), |
| 363 | "top/nested/Blueprints": []byte(` |
| 364 | package { |
| 365 | default_applicable_licenses: ["outsider"], |
| 366 | } |
| 367 | |
| 368 | mock_library { |
| 369 | name: "libnested", |
| 370 | }`), |
| 371 | "top/other/Blueprints": []byte(` |
| 372 | mock_library { |
| 373 | name: "libother", |
| 374 | }`), |
| 375 | "outsider/Blueprints": []byte(` |
| 376 | license { |
| 377 | name: "outsider", |
| 378 | } |
| 379 | mock_library { |
| 380 | name: "liboutsider", |
| 381 | deps: ["libexample", "libother", "libnested"], |
| 382 | }`), |
| 383 | }, |
| 384 | effectiveLicenses: map[string][]string{ |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 385 | "libexample": []string{"top_notice"}, |
| 386 | "libnested": []string{"outsider"}, |
| 387 | "libother": []string{}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 388 | "liboutsider": []string{}, |
| 389 | }, |
| 390 | effectiveInheritedLicenses: map[string][]string{ |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 391 | "libexample": []string{"top_notice"}, |
| 392 | "libnested": []string{"outsider"}, |
| 393 | "libother": []string{}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 394 | "liboutsider": []string{"top_notice", "outsider"}, |
| 395 | }, |
| 396 | }, |
| 397 | { |
| 398 | name: "verify that prebuilt dependencies are included", |
| 399 | fs: map[string][]byte{ |
| 400 | "prebuilts/Blueprints": []byte(` |
| 401 | license { |
| 402 | name: "prebuilt" |
| 403 | } |
| 404 | prebuilt { |
| 405 | name: "module", |
| 406 | licenses: ["prebuilt"], |
| 407 | }`), |
| 408 | "top/sources/source_file": nil, |
| 409 | "top/sources/Blueprints": []byte(` |
| 410 | license { |
| 411 | name: "top_sources" |
| 412 | } |
| 413 | source { |
| 414 | name: "module", |
| 415 | licenses: ["top_sources"], |
| 416 | }`), |
| 417 | "top/other/source_file": nil, |
| 418 | "top/other/Blueprints": []byte(` |
| 419 | source { |
| 420 | name: "other", |
| 421 | deps: [":module"], |
| 422 | }`), |
| 423 | }, |
| 424 | effectiveLicenses: map[string][]string{ |
| 425 | "other": []string{}, |
| 426 | }, |
| 427 | effectiveInheritedLicenses: map[string][]string{ |
| 428 | "other": []string{"prebuilt", "top_sources"}, |
| 429 | }, |
| 430 | }, |
| 431 | { |
| 432 | name: "verify that prebuilt dependencies are ignored for licenses reasons (preferred)", |
| 433 | fs: map[string][]byte{ |
| 434 | "prebuilts/Blueprints": []byte(` |
| 435 | license { |
| 436 | name: "prebuilt" |
| 437 | } |
| 438 | prebuilt { |
| 439 | name: "module", |
| 440 | licenses: ["prebuilt"], |
| 441 | prefer: true, |
| 442 | }`), |
| 443 | "top/sources/source_file": nil, |
| 444 | "top/sources/Blueprints": []byte(` |
| 445 | license { |
| 446 | name: "top_sources" |
| 447 | } |
| 448 | source { |
| 449 | name: "module", |
| 450 | licenses: ["top_sources"], |
| 451 | }`), |
| 452 | "top/other/source_file": nil, |
| 453 | "top/other/Blueprints": []byte(` |
| 454 | source { |
| 455 | name: "other", |
| 456 | deps: [":module"], |
| 457 | }`), |
| 458 | }, |
| 459 | effectiveLicenses: map[string][]string{ |
| 460 | "other": []string{}, |
| 461 | }, |
| 462 | effectiveInheritedLicenses: map[string][]string{ |
| 463 | "module": []string{"prebuilt", "top_sources"}, |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 464 | "other": []string{"prebuilt", "top_sources"}, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 465 | }, |
| 466 | }, |
| 467 | } |
| 468 | |
| 469 | func TestLicenses(t *testing.T) { |
| 470 | for _, test := range licensesTests { |
| 471 | t.Run(test.name, func(t *testing.T) { |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 472 | // Customize the common license text fixture factory. |
| 473 | result := licenseTestFixtureFactory.Extend( |
| 474 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 475 | ctx.RegisterModuleType("mock_bad_module", newMockLicensesBadModule) |
| 476 | ctx.RegisterModuleType("mock_library", newMockLicensesLibraryModule) |
| 477 | ctx.RegisterModuleType("mock_defaults", defaultsLicensesFactory) |
| 478 | }), |
| 479 | test.fs.AddToFixture(), |
| 480 | ). |
| 481 | ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)). |
| 482 | RunTest(t) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 483 | |
| 484 | if test.effectiveLicenses != nil { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 485 | checkEffectiveLicenses(t, result, test.effectiveLicenses) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | if test.effectivePackage != nil { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 489 | checkEffectivePackage(t, result, test.effectivePackage) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | if test.effectiveNotices != nil { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 493 | checkEffectiveNotices(t, result, test.effectiveNotices) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | if test.effectiveKinds != nil { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 497 | checkEffectiveKinds(t, result, test.effectiveKinds) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | if test.effectiveConditions != nil { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 501 | checkEffectiveConditions(t, result, test.effectiveConditions) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | if test.effectiveInheritedLicenses != nil { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 505 | checkEffectiveInheritedLicenses(t, result, test.effectiveInheritedLicenses) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 506 | } |
| 507 | }) |
| 508 | } |
| 509 | } |
| 510 | |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 511 | func checkEffectiveLicenses(t *testing.T, result *TestResult, effectiveLicenses map[string][]string) { |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 512 | actualLicenses := make(map[string][]string) |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 513 | result.Context.Context.VisitAllModules(func(m blueprint.Module) { |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 514 | if _, ok := m.(*licenseModule); ok { |
| 515 | return |
| 516 | } |
| 517 | if _, ok := m.(*licenseKindModule); ok { |
| 518 | return |
| 519 | } |
| 520 | if _, ok := m.(*packageModule); ok { |
| 521 | return |
| 522 | } |
| 523 | module, ok := m.(Module) |
| 524 | if !ok { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 525 | t.Errorf("%q not a module", m.Name()) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 526 | return |
| 527 | } |
| 528 | base := module.base() |
| 529 | if base == nil { |
| 530 | return |
| 531 | } |
| 532 | actualLicenses[m.Name()] = base.commonProperties.Effective_licenses |
| 533 | }) |
| 534 | |
| 535 | for moduleName, expectedLicenses := range effectiveLicenses { |
| 536 | licenses, ok := actualLicenses[moduleName] |
| 537 | if !ok { |
| 538 | licenses = []string{} |
| 539 | } |
| 540 | if !compareUnorderedStringArrays(expectedLicenses, licenses) { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 541 | t.Errorf("effective licenses mismatch for module %q: expected %q, found %q", moduleName, expectedLicenses, licenses) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 546 | func checkEffectiveInheritedLicenses(t *testing.T, result *TestResult, effectiveInheritedLicenses map[string][]string) { |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 547 | actualLicenses := make(map[string][]string) |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 548 | result.Context.Context.VisitAllModules(func(m blueprint.Module) { |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 549 | if _, ok := m.(*licenseModule); ok { |
| 550 | return |
| 551 | } |
| 552 | if _, ok := m.(*licenseKindModule); ok { |
| 553 | return |
| 554 | } |
| 555 | if _, ok := m.(*packageModule); ok { |
| 556 | return |
| 557 | } |
| 558 | module, ok := m.(Module) |
| 559 | if !ok { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 560 | t.Errorf("%q not a module", m.Name()) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 561 | return |
| 562 | } |
| 563 | base := module.base() |
| 564 | if base == nil { |
| 565 | return |
| 566 | } |
| 567 | inherited := make(map[string]bool) |
| 568 | for _, l := range base.commonProperties.Effective_licenses { |
| 569 | inherited[l] = true |
| 570 | } |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 571 | result.Context.Context.VisitDepsDepthFirst(m, func(c blueprint.Module) { |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 572 | if _, ok := c.(*licenseModule); ok { |
| 573 | return |
| 574 | } |
| 575 | if _, ok := c.(*licenseKindModule); ok { |
| 576 | return |
| 577 | } |
| 578 | if _, ok := c.(*packageModule); ok { |
| 579 | return |
| 580 | } |
| 581 | cmodule, ok := c.(Module) |
| 582 | if !ok { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 583 | t.Errorf("%q not a module", c.Name()) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 584 | return |
| 585 | } |
| 586 | cbase := cmodule.base() |
| 587 | if cbase == nil { |
| 588 | return |
| 589 | } |
| 590 | for _, l := range cbase.commonProperties.Effective_licenses { |
| 591 | inherited[l] = true |
| 592 | } |
| 593 | }) |
| 594 | actualLicenses[m.Name()] = []string{} |
| 595 | for l := range inherited { |
| 596 | actualLicenses[m.Name()] = append(actualLicenses[m.Name()], l) |
| 597 | } |
| 598 | }) |
| 599 | |
| 600 | for moduleName, expectedInheritedLicenses := range effectiveInheritedLicenses { |
| 601 | licenses, ok := actualLicenses[moduleName] |
| 602 | if !ok { |
| 603 | licenses = []string{} |
| 604 | } |
| 605 | if !compareUnorderedStringArrays(expectedInheritedLicenses, licenses) { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 606 | t.Errorf("effective inherited licenses mismatch for module %q: expected %q, found %q", moduleName, expectedInheritedLicenses, licenses) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 611 | func checkEffectivePackage(t *testing.T, result *TestResult, effectivePackage map[string]string) { |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 612 | actualPackage := make(map[string]string) |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 613 | result.Context.Context.VisitAllModules(func(m blueprint.Module) { |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 614 | if _, ok := m.(*licenseModule); ok { |
| 615 | return |
| 616 | } |
| 617 | if _, ok := m.(*licenseKindModule); ok { |
| 618 | return |
| 619 | } |
| 620 | if _, ok := m.(*packageModule); ok { |
| 621 | return |
| 622 | } |
| 623 | module, ok := m.(Module) |
| 624 | if !ok { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 625 | t.Errorf("%q not a module", m.Name()) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 626 | return |
| 627 | } |
| 628 | base := module.base() |
| 629 | if base == nil { |
| 630 | return |
| 631 | } |
| 632 | |
| 633 | if base.commonProperties.Effective_package_name == nil { |
| 634 | actualPackage[m.Name()] = "" |
| 635 | } else { |
| 636 | actualPackage[m.Name()] = *base.commonProperties.Effective_package_name |
| 637 | } |
| 638 | }) |
| 639 | |
| 640 | for moduleName, expectedPackage := range effectivePackage { |
| 641 | packageName, ok := actualPackage[moduleName] |
| 642 | if !ok { |
| 643 | packageName = "" |
| 644 | } |
| 645 | if expectedPackage != packageName { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 646 | t.Errorf("effective package mismatch for module %q: expected %q, found %q", moduleName, expectedPackage, packageName) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 651 | func checkEffectiveNotices(t *testing.T, result *TestResult, effectiveNotices map[string][]string) { |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 652 | actualNotices := make(map[string][]string) |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 653 | result.Context.Context.VisitAllModules(func(m blueprint.Module) { |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 654 | if _, ok := m.(*licenseModule); ok { |
| 655 | return |
| 656 | } |
| 657 | if _, ok := m.(*licenseKindModule); ok { |
| 658 | return |
| 659 | } |
| 660 | if _, ok := m.(*packageModule); ok { |
| 661 | return |
| 662 | } |
| 663 | module, ok := m.(Module) |
| 664 | if !ok { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 665 | t.Errorf("%q not a module", m.Name()) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 666 | return |
| 667 | } |
| 668 | base := module.base() |
| 669 | if base == nil { |
| 670 | return |
| 671 | } |
| 672 | actualNotices[m.Name()] = base.commonProperties.Effective_license_text |
| 673 | }) |
| 674 | |
| 675 | for moduleName, expectedNotices := range effectiveNotices { |
| 676 | notices, ok := actualNotices[moduleName] |
| 677 | if !ok { |
| 678 | notices = []string{} |
| 679 | } |
| 680 | if !compareUnorderedStringArrays(expectedNotices, notices) { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 681 | t.Errorf("effective notice files mismatch for module %q: expected %q, found %q", moduleName, expectedNotices, notices) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 686 | func checkEffectiveKinds(t *testing.T, result *TestResult, effectiveKinds map[string][]string) { |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 687 | actualKinds := make(map[string][]string) |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 688 | result.Context.Context.VisitAllModules(func(m blueprint.Module) { |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 689 | if _, ok := m.(*licenseModule); ok { |
| 690 | return |
| 691 | } |
| 692 | if _, ok := m.(*licenseKindModule); ok { |
| 693 | return |
| 694 | } |
| 695 | if _, ok := m.(*packageModule); ok { |
| 696 | return |
| 697 | } |
| 698 | module, ok := m.(Module) |
| 699 | if !ok { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 700 | t.Errorf("%q not a module", m.Name()) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 701 | return |
| 702 | } |
| 703 | base := module.base() |
| 704 | if base == nil { |
| 705 | return |
| 706 | } |
| 707 | actualKinds[m.Name()] = base.commonProperties.Effective_license_kinds |
| 708 | }) |
| 709 | |
| 710 | for moduleName, expectedKinds := range effectiveKinds { |
| 711 | kinds, ok := actualKinds[moduleName] |
| 712 | if !ok { |
| 713 | kinds = []string{} |
| 714 | } |
| 715 | if !compareUnorderedStringArrays(expectedKinds, kinds) { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 716 | t.Errorf("effective license kinds mismatch for module %q: expected %q, found %q", moduleName, expectedKinds, kinds) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 721 | func checkEffectiveConditions(t *testing.T, result *TestResult, effectiveConditions map[string][]string) { |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 722 | actualConditions := make(map[string][]string) |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 723 | result.Context.Context.VisitAllModules(func(m blueprint.Module) { |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 724 | if _, ok := m.(*licenseModule); ok { |
| 725 | return |
| 726 | } |
| 727 | if _, ok := m.(*licenseKindModule); ok { |
| 728 | return |
| 729 | } |
| 730 | if _, ok := m.(*packageModule); ok { |
| 731 | return |
| 732 | } |
| 733 | module, ok := m.(Module) |
| 734 | if !ok { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 735 | t.Errorf("%q not a module", m.Name()) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 736 | return |
| 737 | } |
| 738 | base := module.base() |
| 739 | if base == nil { |
| 740 | return |
| 741 | } |
| 742 | actualConditions[m.Name()] = base.commonProperties.Effective_license_conditions |
| 743 | }) |
| 744 | |
| 745 | for moduleName, expectedConditions := range effectiveConditions { |
| 746 | conditions, ok := actualConditions[moduleName] |
| 747 | if !ok { |
| 748 | conditions = []string{} |
| 749 | } |
| 750 | if !compareUnorderedStringArrays(expectedConditions, conditions) { |
Paul Duffin | 3d0ddff | 2021-03-12 12:20:59 +0000 | [diff] [blame^] | 751 | t.Errorf("effective license conditions mismatch for module %q: expected %q, found %q", moduleName, expectedConditions, conditions) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | func compareUnorderedStringArrays(expected, actual []string) bool { |
| 757 | if len(expected) != len(actual) { |
| 758 | return false |
| 759 | } |
| 760 | s := make(map[string]int) |
| 761 | for _, v := range expected { |
| 762 | s[v] += 1 |
| 763 | } |
| 764 | for _, v := range actual { |
| 765 | c, ok := s[v] |
| 766 | if !ok { |
| 767 | return false |
| 768 | } |
| 769 | if c < 1 { |
| 770 | return false |
| 771 | } |
| 772 | s[v] -= 1 |
| 773 | } |
| 774 | return true |
| 775 | } |
| 776 | |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 777 | type mockLicensesBadProperties struct { |
| 778 | Visibility []string |
| 779 | } |
| 780 | |
| 781 | type mockLicensesBadModule struct { |
| 782 | ModuleBase |
| 783 | DefaultableModuleBase |
| 784 | properties mockLicensesBadProperties |
| 785 | } |
| 786 | |
| 787 | func newMockLicensesBadModule() Module { |
| 788 | m := &mockLicensesBadModule{} |
| 789 | |
| 790 | base := m.base() |
| 791 | m.AddProperties(&base.nameProperties, &m.properties) |
| 792 | |
| 793 | base.generalProperties = m.GetProperties() |
| 794 | base.customizableProperties = m.GetProperties() |
| 795 | |
| 796 | // The default_visibility property needs to be checked and parsed by the visibility module during |
| 797 | // its checking and parsing phases so make it the primary visibility property. |
| 798 | setPrimaryVisibilityProperty(m, "visibility", &m.properties.Visibility) |
| 799 | |
| 800 | initAndroidModuleBase(m) |
| 801 | InitDefaultableModule(m) |
| 802 | |
| 803 | return m |
| 804 | } |
| 805 | |
| 806 | func (m *mockLicensesBadModule) GenerateAndroidBuildActions(ModuleContext) { |
| 807 | } |
| 808 | |
| 809 | type mockLicensesLibraryProperties struct { |
| 810 | Deps []string |
| 811 | } |
| 812 | |
| 813 | type mockLicensesLibraryModule struct { |
| 814 | ModuleBase |
| 815 | DefaultableModuleBase |
| 816 | properties mockLicensesLibraryProperties |
| 817 | } |
| 818 | |
| 819 | func newMockLicensesLibraryModule() Module { |
| 820 | m := &mockLicensesLibraryModule{} |
| 821 | m.AddProperties(&m.properties) |
| 822 | InitAndroidArchModule(m, HostAndDeviceSupported, MultilibCommon) |
| 823 | InitDefaultableModule(m) |
| 824 | return m |
| 825 | } |
| 826 | |
| 827 | type dependencyLicensesTag struct { |
| 828 | blueprint.BaseDependencyTag |
| 829 | name string |
| 830 | } |
| 831 | |
| 832 | func (j *mockLicensesLibraryModule) DepsMutator(ctx BottomUpMutatorContext) { |
| 833 | ctx.AddVariationDependencies(nil, dependencyLicensesTag{name: "mockdeps"}, j.properties.Deps...) |
| 834 | } |
| 835 | |
| 836 | func (p *mockLicensesLibraryModule) GenerateAndroidBuildActions(ModuleContext) { |
| 837 | } |
| 838 | |
| 839 | type mockLicensesDefaults struct { |
| 840 | ModuleBase |
| 841 | DefaultsModuleBase |
| 842 | } |
| 843 | |
| 844 | func defaultsLicensesFactory() Module { |
| 845 | m := &mockLicensesDefaults{} |
| 846 | InitDefaultsModule(m) |
| 847 | return m |
| 848 | } |