blob: 913dc884294e0bb10829d0e06f6894cabbe9ccfb [file] [log] [blame]
Bob Badour37af0462021-01-07 03:34:31 +00001package android
2
3import (
4 "testing"
5
6 "github.com/google/blueprint"
7)
8
Paul Duffin8bd28652021-03-03 00:42:36 +00009var prepareForTestWithLicenses = GroupFixturePreparers(
10 FixtureRegisterWithContext(RegisterLicenseKindBuildComponents),
11 FixtureRegisterWithContext(RegisterLicenseBuildComponents),
12 FixtureRegisterWithContext(registerLicenseMutators),
13)
14
15func registerLicenseMutators(ctx RegistrationContext) {
16 ctx.PreArchMutators(RegisterLicensesPackageMapper)
17 ctx.PreArchMutators(RegisterLicensesPropertyGatherer)
18 ctx.PostDepsMutators(RegisterLicensesDependencyChecker)
19}
20
Bob Badour37af0462021-01-07 03:34:31 +000021var licensesTests = []struct {
Paul Duffin3c298a32021-03-04 17:44:03 +000022 name string
Paul Duffin8bd28652021-03-03 00:42:36 +000023 fs MockFS
Paul Duffin3c298a32021-03-04 17:44:03 +000024 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 Badour37af0462021-01-07 03:34:31 +000031}{
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 Duffin3c298a32021-03-04 17:44:03 +000086 "libnested": []string{"top_Apache2"},
87 "libother": []string{"top_Apache2"},
Bob Badour37af0462021-01-07 03:34:31 +000088 },
89 effectiveKinds: map[string][]string{
90 "libexample1": []string{"notice"},
Paul Duffin3c298a32021-03-04 17:44:03 +000091 "libnested": []string{"notice"},
92 "libother": []string{"notice"},
Bob Badour37af0462021-01-07 03:34:31 +000093 },
94 effectivePackage: map[string]string{
95 "libexample1": "topDog",
Paul Duffin3c298a32021-03-04 17:44:03 +000096 "libnested": "topDog",
97 "libother": "topDog",
Bob Badour37af0462021-01-07 03:34:31 +000098 },
99 effectiveConditions: map[string][]string{
100 "libexample1": []string{"shownotice"},
Paul Duffin3c298a32021-03-04 17:44:03 +0000101 "libnested": []string{"shownotice"},
102 "libother": []string{"shownotice"},
Bob Badour37af0462021-01-07 03:34:31 +0000103 },
104 effectiveNotices: map[string][]string{
105 "libexample1": []string{"top/LICENSE", "top/NOTICE"},
Paul Duffin3c298a32021-03-04 17:44:03 +0000106 "libnested": []string{"top/LICENSE", "top/NOTICE"},
107 "libother": []string{"top/LICENSE", "top/NOTICE"},
Bob Badour37af0462021-01-07 03:34:31 +0000108 },
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 Duffin3c298a32021-03-04 17:44:03 +0000162 "libexample": []string{"nested_other", "top_other"},
Bob Badour37af0462021-01-07 03:34:31 +0000163 "libsamepackage": []string{},
Paul Duffin3c298a32021-03-04 17:44:03 +0000164 "libnested": []string{},
165 "libother": []string{},
Bob Badour37af0462021-01-07 03:34:31 +0000166 },
167 effectiveInheritedLicenses: map[string][]string{
Paul Duffin3c298a32021-03-04 17:44:03 +0000168 "libexample": []string{"nested_other", "top_other"},
Bob Badour37af0462021-01-07 03:34:31 +0000169 "libsamepackage": []string{"nested_other", "top_other"},
Paul Duffin3c298a32021-03-04 17:44:03 +0000170 "libnested": []string{"nested_other", "top_other"},
171 "libother": []string{"nested_other", "top_other"},
Bob Badour37af0462021-01-07 03:34:31 +0000172 },
173 effectiveKinds: map[string][]string{
Paul Duffin3c298a32021-03-04 17:44:03 +0000174 "libexample": []string{"nested_notice", "top_notice"},
Bob Badour37af0462021-01-07 03:34:31 +0000175 "libsamepackage": []string{},
Paul Duffin3c298a32021-03-04 17:44:03 +0000176 "libnested": []string{},
177 "libother": []string{},
Bob Badour37af0462021-01-07 03:34:31 +0000178 },
179 effectiveConditions: map[string][]string{
Paul Duffin3c298a32021-03-04 17:44:03 +0000180 "libexample": []string{"notice"},
Bob Badour37af0462021-01-07 03:34:31 +0000181 "libsamepackage": []string{},
Paul Duffin3c298a32021-03-04 17:44:03 +0000182 "libnested": []string{},
183 "libother": []string{},
Bob Badour37af0462021-01-07 03:34:31 +0000184 },
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 Duffin3c298a32021-03-04 17:44:03 +0000233 "libexample": []string{"other", "top_nested"},
Bob Badour37af0462021-01-07 03:34:31 +0000234 "libsamepackage": []string{},
Paul Duffin3c298a32021-03-04 17:44:03 +0000235 "libnested": []string{},
236 "libother": []string{},
237 "liboutsider": []string{},
Bob Badour37af0462021-01-07 03:34:31 +0000238 },
239 effectiveInheritedLicenses: map[string][]string{
Paul Duffin3c298a32021-03-04 17:44:03 +0000240 "libexample": []string{"other", "top_nested"},
Bob Badour37af0462021-01-07 03:34:31 +0000241 "libsamepackage": []string{"other", "top_nested"},
Paul Duffin3c298a32021-03-04 17:44:03 +0000242 "libnested": []string{"other", "top_nested"},
243 "libother": []string{"other", "top_nested"},
244 "liboutsider": []string{"other", "top_nested"},
Bob Badour37af0462021-01-07 03:34:31 +0000245 },
246 effectiveKinds: map[string][]string{
Paul Duffin3c298a32021-03-04 17:44:03 +0000247 "libexample": []string{},
Bob Badour37af0462021-01-07 03:34:31 +0000248 "libsamepackage": []string{},
Paul Duffin3c298a32021-03-04 17:44:03 +0000249 "libnested": []string{},
250 "libother": []string{},
251 "liboutsider": []string{},
Bob Badour37af0462021-01-07 03:34:31 +0000252 },
253 effectiveNotices: map[string][]string{
Paul Duffin3c298a32021-03-04 17:44:03 +0000254 "libexample": []string{"top/nested/LICENSE.txt"},
Bob Badour37af0462021-01-07 03:34:31 +0000255 "libsamepackage": []string{},
Paul Duffin3c298a32021-03-04 17:44:03 +0000256 "libnested": []string{},
257 "libother": []string{},
258 "liboutsider": []string{},
Bob Badour37af0462021-01-07 03:34:31 +0000259 },
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 Duffin3c298a32021-03-04 17:44:03 +0000300 "libexample": []string{"by_exception_only"},
Bob Badour37af0462021-01-07 03:34:31 +0000301 "libdefaults": []string{"notice"},
302 },
303 effectiveInheritedLicenses: map[string][]string{
Paul Duffin3c298a32021-03-04 17:44:03 +0000304 "libexample": []string{"by_exception_only"},
Bob Badour37af0462021-01-07 03:34:31 +0000305 "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 Duffin3c298a32021-03-04 17:44:03 +0000342 "libexample": []string{"top_notice"},
Bob Badour37af0462021-01-07 03:34:31 +0000343 "liboutsider": []string{},
344 },
345 effectiveInheritedLicenses: map[string][]string{
Paul Duffin3c298a32021-03-04 17:44:03 +0000346 "libexample": []string{"top_notice"},
Bob Badour37af0462021-01-07 03:34:31 +0000347 "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 Duffin3c298a32021-03-04 17:44:03 +0000385 "libexample": []string{"top_notice"},
386 "libnested": []string{"outsider"},
387 "libother": []string{},
Bob Badour37af0462021-01-07 03:34:31 +0000388 "liboutsider": []string{},
389 },
390 effectiveInheritedLicenses: map[string][]string{
Paul Duffin3c298a32021-03-04 17:44:03 +0000391 "libexample": []string{"top_notice"},
392 "libnested": []string{"outsider"},
393 "libother": []string{},
Bob Badour37af0462021-01-07 03:34:31 +0000394 "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 Duffin3c298a32021-03-04 17:44:03 +0000464 "other": []string{"prebuilt", "top_sources"},
Bob Badour37af0462021-01-07 03:34:31 +0000465 },
466 },
467}
468
469func TestLicenses(t *testing.T) {
470 for _, test := range licensesTests {
471 t.Run(test.name, func(t *testing.T) {
Paul Duffin8bd28652021-03-03 00:42:36 +0000472 // Customize the common license text fixture factory.
Paul Duffin30ac3e72021-03-20 00:36:14 +0000473 result := GroupFixturePreparers(
474 prepareForLicenseTest,
Paul Duffin8bd28652021-03-03 00:42:36 +0000475 FixtureRegisterWithContext(func(ctx RegistrationContext) {
476 ctx.RegisterModuleType("mock_bad_module", newMockLicensesBadModule)
477 ctx.RegisterModuleType("mock_library", newMockLicensesLibraryModule)
478 ctx.RegisterModuleType("mock_defaults", defaultsLicensesFactory)
479 }),
480 test.fs.AddToFixture(),
481 ).
482 ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)).
483 RunTest(t)
Bob Badour37af0462021-01-07 03:34:31 +0000484
485 if test.effectiveLicenses != nil {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000486 checkEffectiveLicenses(t, result, test.effectiveLicenses)
Bob Badour37af0462021-01-07 03:34:31 +0000487 }
488
489 if test.effectivePackage != nil {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000490 checkEffectivePackage(t, result, test.effectivePackage)
Bob Badour37af0462021-01-07 03:34:31 +0000491 }
492
493 if test.effectiveNotices != nil {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000494 checkEffectiveNotices(t, result, test.effectiveNotices)
Bob Badour37af0462021-01-07 03:34:31 +0000495 }
496
497 if test.effectiveKinds != nil {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000498 checkEffectiveKinds(t, result, test.effectiveKinds)
Bob Badour37af0462021-01-07 03:34:31 +0000499 }
500
501 if test.effectiveConditions != nil {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000502 checkEffectiveConditions(t, result, test.effectiveConditions)
Bob Badour37af0462021-01-07 03:34:31 +0000503 }
504
505 if test.effectiveInheritedLicenses != nil {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000506 checkEffectiveInheritedLicenses(t, result, test.effectiveInheritedLicenses)
Bob Badour37af0462021-01-07 03:34:31 +0000507 }
508 })
509 }
510}
511
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000512func checkEffectiveLicenses(t *testing.T, result *TestResult, effectiveLicenses map[string][]string) {
Bob Badour37af0462021-01-07 03:34:31 +0000513 actualLicenses := make(map[string][]string)
Paul Duffin8bd28652021-03-03 00:42:36 +0000514 result.Context.Context.VisitAllModules(func(m blueprint.Module) {
Bob Badour37af0462021-01-07 03:34:31 +0000515 if _, ok := m.(*licenseModule); ok {
516 return
517 }
518 if _, ok := m.(*licenseKindModule); ok {
519 return
520 }
521 if _, ok := m.(*packageModule); ok {
522 return
523 }
524 module, ok := m.(Module)
525 if !ok {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000526 t.Errorf("%q not a module", m.Name())
Bob Badour37af0462021-01-07 03:34:31 +0000527 return
528 }
529 base := module.base()
530 if base == nil {
531 return
532 }
533 actualLicenses[m.Name()] = base.commonProperties.Effective_licenses
534 })
535
536 for moduleName, expectedLicenses := range effectiveLicenses {
537 licenses, ok := actualLicenses[moduleName]
538 if !ok {
539 licenses = []string{}
540 }
541 if !compareUnorderedStringArrays(expectedLicenses, licenses) {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000542 t.Errorf("effective licenses mismatch for module %q: expected %q, found %q", moduleName, expectedLicenses, licenses)
Bob Badour37af0462021-01-07 03:34:31 +0000543 }
544 }
545}
546
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000547func checkEffectiveInheritedLicenses(t *testing.T, result *TestResult, effectiveInheritedLicenses map[string][]string) {
Bob Badour37af0462021-01-07 03:34:31 +0000548 actualLicenses := make(map[string][]string)
Paul Duffin8bd28652021-03-03 00:42:36 +0000549 result.Context.Context.VisitAllModules(func(m blueprint.Module) {
Bob Badour37af0462021-01-07 03:34:31 +0000550 if _, ok := m.(*licenseModule); ok {
551 return
552 }
553 if _, ok := m.(*licenseKindModule); ok {
554 return
555 }
556 if _, ok := m.(*packageModule); ok {
557 return
558 }
559 module, ok := m.(Module)
560 if !ok {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000561 t.Errorf("%q not a module", m.Name())
Bob Badour37af0462021-01-07 03:34:31 +0000562 return
563 }
564 base := module.base()
565 if base == nil {
566 return
567 }
568 inherited := make(map[string]bool)
569 for _, l := range base.commonProperties.Effective_licenses {
570 inherited[l] = true
571 }
Paul Duffin8bd28652021-03-03 00:42:36 +0000572 result.Context.Context.VisitDepsDepthFirst(m, func(c blueprint.Module) {
Bob Badour37af0462021-01-07 03:34:31 +0000573 if _, ok := c.(*licenseModule); ok {
574 return
575 }
576 if _, ok := c.(*licenseKindModule); ok {
577 return
578 }
579 if _, ok := c.(*packageModule); ok {
580 return
581 }
582 cmodule, ok := c.(Module)
583 if !ok {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000584 t.Errorf("%q not a module", c.Name())
Bob Badour37af0462021-01-07 03:34:31 +0000585 return
586 }
587 cbase := cmodule.base()
588 if cbase == nil {
589 return
590 }
591 for _, l := range cbase.commonProperties.Effective_licenses {
592 inherited[l] = true
593 }
594 })
595 actualLicenses[m.Name()] = []string{}
596 for l := range inherited {
597 actualLicenses[m.Name()] = append(actualLicenses[m.Name()], l)
598 }
599 })
600
601 for moduleName, expectedInheritedLicenses := range effectiveInheritedLicenses {
602 licenses, ok := actualLicenses[moduleName]
603 if !ok {
604 licenses = []string{}
605 }
606 if !compareUnorderedStringArrays(expectedInheritedLicenses, licenses) {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000607 t.Errorf("effective inherited licenses mismatch for module %q: expected %q, found %q", moduleName, expectedInheritedLicenses, licenses)
Bob Badour37af0462021-01-07 03:34:31 +0000608 }
609 }
610}
611
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000612func checkEffectivePackage(t *testing.T, result *TestResult, effectivePackage map[string]string) {
Bob Badour37af0462021-01-07 03:34:31 +0000613 actualPackage := make(map[string]string)
Paul Duffin8bd28652021-03-03 00:42:36 +0000614 result.Context.Context.VisitAllModules(func(m blueprint.Module) {
Bob Badour37af0462021-01-07 03:34:31 +0000615 if _, ok := m.(*licenseModule); ok {
616 return
617 }
618 if _, ok := m.(*licenseKindModule); ok {
619 return
620 }
621 if _, ok := m.(*packageModule); ok {
622 return
623 }
624 module, ok := m.(Module)
625 if !ok {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000626 t.Errorf("%q not a module", m.Name())
Bob Badour37af0462021-01-07 03:34:31 +0000627 return
628 }
629 base := module.base()
630 if base == nil {
631 return
632 }
633
634 if base.commonProperties.Effective_package_name == nil {
635 actualPackage[m.Name()] = ""
636 } else {
637 actualPackage[m.Name()] = *base.commonProperties.Effective_package_name
638 }
639 })
640
641 for moduleName, expectedPackage := range effectivePackage {
642 packageName, ok := actualPackage[moduleName]
643 if !ok {
644 packageName = ""
645 }
646 if expectedPackage != packageName {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000647 t.Errorf("effective package mismatch for module %q: expected %q, found %q", moduleName, expectedPackage, packageName)
Bob Badour37af0462021-01-07 03:34:31 +0000648 }
649 }
650}
651
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000652func checkEffectiveNotices(t *testing.T, result *TestResult, effectiveNotices map[string][]string) {
Bob Badour37af0462021-01-07 03:34:31 +0000653 actualNotices := make(map[string][]string)
Paul Duffin8bd28652021-03-03 00:42:36 +0000654 result.Context.Context.VisitAllModules(func(m blueprint.Module) {
Bob Badour37af0462021-01-07 03:34:31 +0000655 if _, ok := m.(*licenseModule); ok {
656 return
657 }
658 if _, ok := m.(*licenseKindModule); ok {
659 return
660 }
661 if _, ok := m.(*packageModule); ok {
662 return
663 }
664 module, ok := m.(Module)
665 if !ok {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000666 t.Errorf("%q not a module", m.Name())
Bob Badour37af0462021-01-07 03:34:31 +0000667 return
668 }
669 base := module.base()
670 if base == nil {
671 return
672 }
673 actualNotices[m.Name()] = base.commonProperties.Effective_license_text
674 })
675
676 for moduleName, expectedNotices := range effectiveNotices {
677 notices, ok := actualNotices[moduleName]
678 if !ok {
679 notices = []string{}
680 }
681 if !compareUnorderedStringArrays(expectedNotices, notices) {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000682 t.Errorf("effective notice files mismatch for module %q: expected %q, found %q", moduleName, expectedNotices, notices)
Bob Badour37af0462021-01-07 03:34:31 +0000683 }
684 }
685}
686
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000687func checkEffectiveKinds(t *testing.T, result *TestResult, effectiveKinds map[string][]string) {
Bob Badour37af0462021-01-07 03:34:31 +0000688 actualKinds := make(map[string][]string)
Paul Duffin8bd28652021-03-03 00:42:36 +0000689 result.Context.Context.VisitAllModules(func(m blueprint.Module) {
Bob Badour37af0462021-01-07 03:34:31 +0000690 if _, ok := m.(*licenseModule); ok {
691 return
692 }
693 if _, ok := m.(*licenseKindModule); ok {
694 return
695 }
696 if _, ok := m.(*packageModule); ok {
697 return
698 }
699 module, ok := m.(Module)
700 if !ok {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000701 t.Errorf("%q not a module", m.Name())
Bob Badour37af0462021-01-07 03:34:31 +0000702 return
703 }
704 base := module.base()
705 if base == nil {
706 return
707 }
708 actualKinds[m.Name()] = base.commonProperties.Effective_license_kinds
709 })
710
711 for moduleName, expectedKinds := range effectiveKinds {
712 kinds, ok := actualKinds[moduleName]
713 if !ok {
714 kinds = []string{}
715 }
716 if !compareUnorderedStringArrays(expectedKinds, kinds) {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000717 t.Errorf("effective license kinds mismatch for module %q: expected %q, found %q", moduleName, expectedKinds, kinds)
Bob Badour37af0462021-01-07 03:34:31 +0000718 }
719 }
720}
721
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000722func checkEffectiveConditions(t *testing.T, result *TestResult, effectiveConditions map[string][]string) {
Bob Badour37af0462021-01-07 03:34:31 +0000723 actualConditions := make(map[string][]string)
Paul Duffin8bd28652021-03-03 00:42:36 +0000724 result.Context.Context.VisitAllModules(func(m blueprint.Module) {
Bob Badour37af0462021-01-07 03:34:31 +0000725 if _, ok := m.(*licenseModule); ok {
726 return
727 }
728 if _, ok := m.(*licenseKindModule); ok {
729 return
730 }
731 if _, ok := m.(*packageModule); ok {
732 return
733 }
734 module, ok := m.(Module)
735 if !ok {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000736 t.Errorf("%q not a module", m.Name())
Bob Badour37af0462021-01-07 03:34:31 +0000737 return
738 }
739 base := module.base()
740 if base == nil {
741 return
742 }
743 actualConditions[m.Name()] = base.commonProperties.Effective_license_conditions
744 })
745
746 for moduleName, expectedConditions := range effectiveConditions {
747 conditions, ok := actualConditions[moduleName]
748 if !ok {
749 conditions = []string{}
750 }
751 if !compareUnorderedStringArrays(expectedConditions, conditions) {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000752 t.Errorf("effective license conditions mismatch for module %q: expected %q, found %q", moduleName, expectedConditions, conditions)
Bob Badour37af0462021-01-07 03:34:31 +0000753 }
754 }
755}
756
757func compareUnorderedStringArrays(expected, actual []string) bool {
758 if len(expected) != len(actual) {
759 return false
760 }
761 s := make(map[string]int)
762 for _, v := range expected {
763 s[v] += 1
764 }
765 for _, v := range actual {
766 c, ok := s[v]
767 if !ok {
768 return false
769 }
770 if c < 1 {
771 return false
772 }
773 s[v] -= 1
774 }
775 return true
776}
777
Bob Badour37af0462021-01-07 03:34:31 +0000778type mockLicensesBadProperties struct {
779 Visibility []string
780}
781
782type mockLicensesBadModule struct {
783 ModuleBase
784 DefaultableModuleBase
785 properties mockLicensesBadProperties
786}
787
788func newMockLicensesBadModule() Module {
789 m := &mockLicensesBadModule{}
790
791 base := m.base()
792 m.AddProperties(&base.nameProperties, &m.properties)
793
794 base.generalProperties = m.GetProperties()
795 base.customizableProperties = m.GetProperties()
796
797 // The default_visibility property needs to be checked and parsed by the visibility module during
798 // its checking and parsing phases so make it the primary visibility property.
799 setPrimaryVisibilityProperty(m, "visibility", &m.properties.Visibility)
800
801 initAndroidModuleBase(m)
802 InitDefaultableModule(m)
803
804 return m
805}
806
807func (m *mockLicensesBadModule) GenerateAndroidBuildActions(ModuleContext) {
808}
809
810type mockLicensesLibraryProperties struct {
811 Deps []string
812}
813
814type mockLicensesLibraryModule struct {
815 ModuleBase
816 DefaultableModuleBase
817 properties mockLicensesLibraryProperties
818}
819
820func newMockLicensesLibraryModule() Module {
821 m := &mockLicensesLibraryModule{}
822 m.AddProperties(&m.properties)
823 InitAndroidArchModule(m, HostAndDeviceSupported, MultilibCommon)
824 InitDefaultableModule(m)
825 return m
826}
827
828type dependencyLicensesTag struct {
829 blueprint.BaseDependencyTag
830 name string
831}
832
833func (j *mockLicensesLibraryModule) DepsMutator(ctx BottomUpMutatorContext) {
834 ctx.AddVariationDependencies(nil, dependencyLicensesTag{name: "mockdeps"}, j.properties.Deps...)
835}
836
837func (p *mockLicensesLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
838}
839
840type mockLicensesDefaults struct {
841 ModuleBase
842 DefaultsModuleBase
843}
844
845func defaultsLicensesFactory() Module {
846 m := &mockLicensesDefaults{}
847 InitDefaultsModule(m)
848 return m
849}