blob: a581932bdc96f4757ef11bfc27820547eff342e4 [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.
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 Badour37af0462021-01-07 03:34:31 +0000483
484 if test.effectiveLicenses != nil {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000485 checkEffectiveLicenses(t, result, test.effectiveLicenses)
Bob Badour37af0462021-01-07 03:34:31 +0000486 }
487
488 if test.effectivePackage != nil {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000489 checkEffectivePackage(t, result, test.effectivePackage)
Bob Badour37af0462021-01-07 03:34:31 +0000490 }
491
492 if test.effectiveNotices != nil {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000493 checkEffectiveNotices(t, result, test.effectiveNotices)
Bob Badour37af0462021-01-07 03:34:31 +0000494 }
495
496 if test.effectiveKinds != nil {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000497 checkEffectiveKinds(t, result, test.effectiveKinds)
Bob Badour37af0462021-01-07 03:34:31 +0000498 }
499
500 if test.effectiveConditions != nil {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000501 checkEffectiveConditions(t, result, test.effectiveConditions)
Bob Badour37af0462021-01-07 03:34:31 +0000502 }
503
504 if test.effectiveInheritedLicenses != nil {
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000505 checkEffectiveInheritedLicenses(t, result, test.effectiveInheritedLicenses)
Bob Badour37af0462021-01-07 03:34:31 +0000506 }
507 })
508 }
509}
510
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000511func checkEffectiveLicenses(t *testing.T, result *TestResult, effectiveLicenses map[string][]string) {
Bob Badour37af0462021-01-07 03:34:31 +0000512 actualLicenses := make(map[string][]string)
Paul Duffin8bd28652021-03-03 00:42:36 +0000513 result.Context.Context.VisitAllModules(func(m blueprint.Module) {
Bob Badour37af0462021-01-07 03:34:31 +0000514 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 Duffin3d0ddff2021-03-12 12:20:59 +0000525 t.Errorf("%q not a module", m.Name())
Bob Badour37af0462021-01-07 03:34:31 +0000526 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 Duffin3d0ddff2021-03-12 12:20:59 +0000541 t.Errorf("effective licenses mismatch for module %q: expected %q, found %q", moduleName, expectedLicenses, licenses)
Bob Badour37af0462021-01-07 03:34:31 +0000542 }
543 }
544}
545
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000546func checkEffectiveInheritedLicenses(t *testing.T, result *TestResult, effectiveInheritedLicenses map[string][]string) {
Bob Badour37af0462021-01-07 03:34:31 +0000547 actualLicenses := make(map[string][]string)
Paul Duffin8bd28652021-03-03 00:42:36 +0000548 result.Context.Context.VisitAllModules(func(m blueprint.Module) {
Bob Badour37af0462021-01-07 03:34:31 +0000549 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 Duffin3d0ddff2021-03-12 12:20:59 +0000560 t.Errorf("%q not a module", m.Name())
Bob Badour37af0462021-01-07 03:34:31 +0000561 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 Duffin8bd28652021-03-03 00:42:36 +0000571 result.Context.Context.VisitDepsDepthFirst(m, func(c blueprint.Module) {
Bob Badour37af0462021-01-07 03:34:31 +0000572 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 Duffin3d0ddff2021-03-12 12:20:59 +0000583 t.Errorf("%q not a module", c.Name())
Bob Badour37af0462021-01-07 03:34:31 +0000584 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 Duffin3d0ddff2021-03-12 12:20:59 +0000606 t.Errorf("effective inherited licenses mismatch for module %q: expected %q, found %q", moduleName, expectedInheritedLicenses, licenses)
Bob Badour37af0462021-01-07 03:34:31 +0000607 }
608 }
609}
610
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000611func checkEffectivePackage(t *testing.T, result *TestResult, effectivePackage map[string]string) {
Bob Badour37af0462021-01-07 03:34:31 +0000612 actualPackage := make(map[string]string)
Paul Duffin8bd28652021-03-03 00:42:36 +0000613 result.Context.Context.VisitAllModules(func(m blueprint.Module) {
Bob Badour37af0462021-01-07 03:34:31 +0000614 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 Duffin3d0ddff2021-03-12 12:20:59 +0000625 t.Errorf("%q not a module", m.Name())
Bob Badour37af0462021-01-07 03:34:31 +0000626 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 Duffin3d0ddff2021-03-12 12:20:59 +0000646 t.Errorf("effective package mismatch for module %q: expected %q, found %q", moduleName, expectedPackage, packageName)
Bob Badour37af0462021-01-07 03:34:31 +0000647 }
648 }
649}
650
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000651func checkEffectiveNotices(t *testing.T, result *TestResult, effectiveNotices map[string][]string) {
Bob Badour37af0462021-01-07 03:34:31 +0000652 actualNotices := make(map[string][]string)
Paul Duffin8bd28652021-03-03 00:42:36 +0000653 result.Context.Context.VisitAllModules(func(m blueprint.Module) {
Bob Badour37af0462021-01-07 03:34:31 +0000654 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 Duffin3d0ddff2021-03-12 12:20:59 +0000665 t.Errorf("%q not a module", m.Name())
Bob Badour37af0462021-01-07 03:34:31 +0000666 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 Duffin3d0ddff2021-03-12 12:20:59 +0000681 t.Errorf("effective notice files mismatch for module %q: expected %q, found %q", moduleName, expectedNotices, notices)
Bob Badour37af0462021-01-07 03:34:31 +0000682 }
683 }
684}
685
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000686func checkEffectiveKinds(t *testing.T, result *TestResult, effectiveKinds map[string][]string) {
Bob Badour37af0462021-01-07 03:34:31 +0000687 actualKinds := make(map[string][]string)
Paul Duffin8bd28652021-03-03 00:42:36 +0000688 result.Context.Context.VisitAllModules(func(m blueprint.Module) {
Bob Badour37af0462021-01-07 03:34:31 +0000689 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 Duffin3d0ddff2021-03-12 12:20:59 +0000700 t.Errorf("%q not a module", m.Name())
Bob Badour37af0462021-01-07 03:34:31 +0000701 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 Duffin3d0ddff2021-03-12 12:20:59 +0000716 t.Errorf("effective license kinds mismatch for module %q: expected %q, found %q", moduleName, expectedKinds, kinds)
Bob Badour37af0462021-01-07 03:34:31 +0000717 }
718 }
719}
720
Paul Duffin3d0ddff2021-03-12 12:20:59 +0000721func checkEffectiveConditions(t *testing.T, result *TestResult, effectiveConditions map[string][]string) {
Bob Badour37af0462021-01-07 03:34:31 +0000722 actualConditions := make(map[string][]string)
Paul Duffin8bd28652021-03-03 00:42:36 +0000723 result.Context.Context.VisitAllModules(func(m blueprint.Module) {
Bob Badour37af0462021-01-07 03:34:31 +0000724 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 Duffin3d0ddff2021-03-12 12:20:59 +0000735 t.Errorf("%q not a module", m.Name())
Bob Badour37af0462021-01-07 03:34:31 +0000736 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 Duffin3d0ddff2021-03-12 12:20:59 +0000751 t.Errorf("effective license conditions mismatch for module %q: expected %q, found %q", moduleName, expectedConditions, conditions)
Bob Badour37af0462021-01-07 03:34:31 +0000752 }
753 }
754}
755
756func 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 Badour37af0462021-01-07 03:34:31 +0000777type mockLicensesBadProperties struct {
778 Visibility []string
779}
780
781type mockLicensesBadModule struct {
782 ModuleBase
783 DefaultableModuleBase
784 properties mockLicensesBadProperties
785}
786
787func 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
806func (m *mockLicensesBadModule) GenerateAndroidBuildActions(ModuleContext) {
807}
808
809type mockLicensesLibraryProperties struct {
810 Deps []string
811}
812
813type mockLicensesLibraryModule struct {
814 ModuleBase
815 DefaultableModuleBase
816 properties mockLicensesLibraryProperties
817}
818
819func newMockLicensesLibraryModule() Module {
820 m := &mockLicensesLibraryModule{}
821 m.AddProperties(&m.properties)
822 InitAndroidArchModule(m, HostAndDeviceSupported, MultilibCommon)
823 InitDefaultableModule(m)
824 return m
825}
826
827type dependencyLicensesTag struct {
828 blueprint.BaseDependencyTag
829 name string
830}
831
832func (j *mockLicensesLibraryModule) DepsMutator(ctx BottomUpMutatorContext) {
833 ctx.AddVariationDependencies(nil, dependencyLicensesTag{name: "mockdeps"}, j.properties.Deps...)
834}
835
836func (p *mockLicensesLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
837}
838
839type mockLicensesDefaults struct {
840 ModuleBase
841 DefaultsModuleBase
842}
843
844func defaultsLicensesFactory() Module {
845 m := &mockLicensesDefaults{}
846 InitDefaultsModule(m)
847 return m
848}