Bob Badour | dc5be90 | 2021-03-15 20:00:01 -0700 | [diff] [blame] | 1 | package android |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | ) |
| 6 | |
| 7 | var licenseTests = []struct { |
| 8 | name string |
| 9 | fs map[string][]byte |
| 10 | expectedErrors []string |
| 11 | }{ |
| 12 | { |
| 13 | name: "license must not accept licenses property", |
| 14 | fs: map[string][]byte{ |
| 15 | "top/Blueprints": []byte(` |
| 16 | license { |
| 17 | name: "top_license", |
| 18 | visibility: ["//visibility:private"], |
| 19 | licenses: ["other_license"], |
| 20 | |
| 21 | }`), |
| 22 | }, |
| 23 | expectedErrors: []string{ |
| 24 | `top/Blueprints:5:14: unrecognized property "licenses"`, |
| 25 | }, |
| 26 | }, |
| 27 | { |
| 28 | name: "public license", |
| 29 | fs: map[string][]byte{ |
| 30 | "top/Blueprints": []byte(` |
| 31 | license { |
| 32 | name: "top_proprietary", |
| 33 | license_kinds: ["top_by_exception_only"], |
| 34 | visibility: ["//visibility:public"], |
| 35 | }`), |
| 36 | "other/Blueprints": []byte(` |
| 37 | rule { |
| 38 | name: "arule", |
| 39 | licenses: ["top_proprietary"], |
| 40 | |
| 41 | }`), |
| 42 | "yetmore/Blueprints": []byte(` |
| 43 | package { |
| 44 | default_applicable_licenses: ["top_proprietary"], |
| 45 | }`), |
| 46 | }, |
| 47 | }, |
| 48 | { |
| 49 | name: "multiple licenses", |
| 50 | fs: map[string][]byte{ |
| 51 | "top/Blueprints": []byte(` |
| 52 | package { |
| 53 | default_applicable_licenses: ["top_proprietary"], |
| 54 | } |
| 55 | license { |
| 56 | name: "top_allowed_as_notice", |
| 57 | license_kinds: ["top_notice"], |
| 58 | } |
| 59 | license { |
| 60 | name: "top_proprietary", |
| 61 | license_kinds: ["top_by_exception_only"], |
| 62 | visibility: ["//visibility:public"], |
| 63 | } |
| 64 | rule { |
| 65 | name: "myrule", |
| 66 | licenses: ["top_allowed_as_notice", "top_proprietary"] |
| 67 | }`), |
| 68 | "other/Blueprints": []byte(` |
| 69 | rule { |
| 70 | name: "arule", |
| 71 | licenses: ["top_proprietary"], |
| 72 | |
| 73 | }`), |
| 74 | "yetmore/Blueprints": []byte(` |
| 75 | package { |
| 76 | default_applicable_licenses: ["top_proprietary"], |
| 77 | }`), |
| 78 | }, |
| 79 | }, |
| 80 | } |
| 81 | |
| 82 | func TestLicense(t *testing.T) { |
| 83 | for _, test := range licenseTests { |
| 84 | t.Run(test.name, func(t *testing.T) { |
| 85 | _, errs := testLicense(test.fs) |
| 86 | expectedErrors := test.expectedErrors |
| 87 | if expectedErrors == nil { |
| 88 | FailIfErrored(t, errs) |
| 89 | } else { |
| 90 | for _, expectedError := range expectedErrors { |
| 91 | FailIfNoMatchingErrors(t, expectedError, errs) |
| 92 | } |
| 93 | if len(errs) > len(expectedErrors) { |
| 94 | t.Errorf("additional errors found, expected %d, found %d", len(expectedErrors), len(errs)) |
| 95 | for i, expectedError := range expectedErrors { |
| 96 | t.Errorf("expectedErrors[%d] = %s", i, expectedError) |
| 97 | } |
| 98 | for i, err := range errs { |
| 99 | t.Errorf("errs[%d] = %s", i, err) |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | }) |
| 104 | } |
| 105 | } |
| 106 | func testLicense(fs map[string][]byte) (*TestContext, []error) { |
| 107 | // Create a new config per test as visibility information is stored in the config. |
| 108 | env := make(map[string]string) |
| 109 | env["ANDROID_REQUIRE_LICENSES"] = "1" |
| 110 | config := TestArchConfig(buildDir, env, "", fs) |
| 111 | ctx := NewTestArchContext() |
| 112 | RegisterPackageBuildComponents(ctx) |
| 113 | registerTestPrebuiltBuildComponents(ctx) |
| 114 | RegisterLicenseBuildComponents(ctx) |
| 115 | ctx.RegisterModuleType("rule", newMockRuleModule) |
| 116 | ctx.PreArchMutators(RegisterVisibilityRuleChecker) |
| 117 | ctx.PreArchMutators(RegisterDefaultsPreArchMutators) |
| 118 | ctx.PreArchMutators(RegisterVisibilityRuleGatherer) |
| 119 | ctx.PostDepsMutators(RegisterVisibilityRuleEnforcer) |
| 120 | ctx.Register(config) |
| 121 | _, errs := ctx.ParseBlueprintsFiles(".") |
| 122 | if len(errs) > 0 { |
| 123 | return ctx, errs |
| 124 | } |
| 125 | _, errs = ctx.PrepareBuildActions(config) |
| 126 | return ctx, errs |
| 127 | } |
| 128 | |
| 129 | type mockRuleModule struct { |
| 130 | ModuleBase |
| 131 | DefaultableModuleBase |
| 132 | } |
| 133 | |
| 134 | func newMockRuleModule() Module { |
| 135 | m := &mockRuleModule{} |
| 136 | InitAndroidModule(m) |
| 137 | InitDefaultableModule(m) |
| 138 | return m |
| 139 | } |
| 140 | |
| 141 | func (p *mockRuleModule) GenerateAndroidBuildActions(ModuleContext) { |
| 142 | } |