Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [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 | expectedErrors: []string{ |
| 23 | `top/Blueprints:5:14: unrecognized property "licenses"`, |
| 24 | }, |
| 25 | }, |
| 26 | { |
| 27 | name: "private license", |
| 28 | fs: map[string][]byte{ |
| 29 | "top/Blueprints": []byte(` |
| 30 | license_kind { |
| 31 | name: "top_notice", |
| 32 | conditions: ["notice"], |
| 33 | visibility: ["//visibility:private"], |
| 34 | } |
| 35 | |
| 36 | license { |
| 37 | name: "top_allowed_as_notice", |
| 38 | license_kinds: ["top_notice"], |
| 39 | visibility: ["//visibility:private"], |
| 40 | }`), |
| 41 | "other/Blueprints": []byte(` |
| 42 | rule { |
| 43 | name: "arule", |
| 44 | licenses: ["top_allowed_as_notice"], |
| 45 | }`), |
| 46 | "yetmore/Blueprints": []byte(` |
| 47 | package { |
| 48 | default_applicable_licenses: ["top_allowed_as_notice"], |
| 49 | }`), |
| 50 | }, |
| 51 | expectedErrors: []string{ |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame^] | 52 | `other/Blueprints:2:5: module "arule": depends on //top:top_allowed_as_notice ` + |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 53 | `which is not visible to this module`, |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame^] | 54 | `yetmore/Blueprints:2:5: module "//yetmore": depends on //top:top_allowed_as_notice ` + |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 55 | `which is not visible to this module`, |
| 56 | }, |
| 57 | }, |
| 58 | { |
| 59 | name: "must reference license_kind module", |
| 60 | fs: map[string][]byte{ |
| 61 | "top/Blueprints": []byte(` |
| 62 | rule { |
| 63 | name: "top_by_exception_only", |
| 64 | } |
| 65 | |
| 66 | license { |
| 67 | name: "top_proprietary", |
| 68 | license_kinds: ["top_by_exception_only"], |
| 69 | visibility: ["//visibility:public"], |
| 70 | }`), |
| 71 | }, |
| 72 | expectedErrors: []string{ |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame^] | 73 | `top/Blueprints:6:5: module "top_proprietary": license_kinds property ` + |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 74 | `"top_by_exception_only" is not a license_kind module`, |
| 75 | }, |
| 76 | }, |
| 77 | { |
| 78 | name: "license_kind module must exist", |
| 79 | fs: map[string][]byte{ |
| 80 | "top/Blueprints": []byte(` |
| 81 | license { |
| 82 | name: "top_notice_allowed", |
| 83 | license_kinds: ["top_notice"], |
| 84 | visibility: ["//visibility:public"], |
| 85 | }`), |
| 86 | }, |
| 87 | expectedErrors: []string{ |
| 88 | `top/Blueprints:2:5: "top_notice_allowed" depends on undefined module "top_notice"`, |
| 89 | }, |
| 90 | }, |
| 91 | { |
| 92 | name: "public license", |
| 93 | fs: map[string][]byte{ |
| 94 | "top/Blueprints": []byte(` |
| 95 | license_kind { |
| 96 | name: "top_by_exception_only", |
| 97 | conditions: ["by_exception_only"], |
| 98 | visibility: ["//visibility:private"], |
| 99 | } |
| 100 | |
| 101 | license { |
| 102 | name: "top_proprietary", |
| 103 | license_kinds: ["top_by_exception_only"], |
| 104 | visibility: ["//visibility:public"], |
| 105 | }`), |
| 106 | "other/Blueprints": []byte(` |
| 107 | rule { |
| 108 | name: "arule", |
| 109 | licenses: ["top_proprietary"], |
| 110 | }`), |
| 111 | "yetmore/Blueprints": []byte(` |
| 112 | package { |
| 113 | default_applicable_licenses: ["top_proprietary"], |
| 114 | }`), |
| 115 | }, |
| 116 | }, |
| 117 | { |
| 118 | name: "multiple licenses", |
| 119 | fs: map[string][]byte{ |
| 120 | "top/Blueprints": []byte(` |
| 121 | package { |
| 122 | default_applicable_licenses: ["top_proprietary"], |
| 123 | } |
| 124 | |
| 125 | license_kind { |
| 126 | name: "top_notice", |
| 127 | conditions: ["notice"], |
| 128 | } |
| 129 | |
| 130 | license_kind { |
| 131 | name: "top_by_exception_only", |
| 132 | conditions: ["by_exception_only"], |
| 133 | visibility: ["//visibility:public"], |
| 134 | } |
| 135 | |
| 136 | license { |
| 137 | name: "top_allowed_as_notice", |
| 138 | license_kinds: ["top_notice"], |
| 139 | } |
| 140 | |
| 141 | license { |
| 142 | name: "top_proprietary", |
| 143 | license_kinds: ["top_by_exception_only"], |
| 144 | visibility: ["//visibility:public"], |
| 145 | } |
| 146 | rule { |
| 147 | name: "myrule", |
| 148 | licenses: ["top_allowed_as_notice", "top_proprietary"] |
| 149 | }`), |
| 150 | "other/Blueprints": []byte(` |
| 151 | rule { |
| 152 | name: "arule", |
| 153 | licenses: ["top_proprietary"], |
| 154 | }`), |
| 155 | "yetmore/Blueprints": []byte(` |
| 156 | package { |
| 157 | default_applicable_licenses: ["top_proprietary"], |
| 158 | }`), |
| 159 | }, |
| 160 | }, |
| 161 | } |
| 162 | |
| 163 | func TestLicense(t *testing.T) { |
| 164 | for _, test := range licenseTests { |
| 165 | t.Run(test.name, func(t *testing.T) { |
| 166 | _, errs := testLicense(test.fs) |
| 167 | |
| 168 | expectedErrors := test.expectedErrors |
| 169 | if expectedErrors == nil { |
| 170 | FailIfErrored(t, errs) |
| 171 | } else { |
| 172 | for _, expectedError := range expectedErrors { |
| 173 | FailIfNoMatchingErrors(t, expectedError, errs) |
| 174 | } |
| 175 | if len(errs) > len(expectedErrors) { |
| 176 | t.Errorf("additional errors found, expected %d, found %d", len(expectedErrors), len(errs)) |
| 177 | for i, expectedError := range expectedErrors { |
| 178 | t.Errorf("expectedErrors[%d] = %s", i, expectedError) |
| 179 | } |
| 180 | for i, err := range errs { |
| 181 | t.Errorf("errs[%d] = %s", i, err) |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | }) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | func testLicense(fs map[string][]byte) (*TestContext, []error) { |
| 190 | |
| 191 | // Create a new config per test as visibility information is stored in the config. |
| 192 | env := make(map[string]string) |
| 193 | env["ANDROID_REQUIRE_LICENSES"] = "1" |
| 194 | config := TestArchConfig(buildDir, env, "", fs) |
| 195 | |
| 196 | ctx := NewTestArchContext(config) |
| 197 | RegisterPackageBuildComponents(ctx) |
| 198 | registerTestPrebuiltBuildComponents(ctx) |
| 199 | RegisterLicenseKindBuildComponents(ctx) |
| 200 | RegisterLicenseBuildComponents(ctx) |
| 201 | ctx.RegisterModuleType("rule", newMockRuleModule) |
| 202 | ctx.PreArchMutators(RegisterVisibilityRuleChecker) |
| 203 | ctx.PreArchMutators(RegisterLicensesPackageMapper) |
| 204 | ctx.PreArchMutators(RegisterDefaultsPreArchMutators) |
| 205 | ctx.PreArchMutators(RegisterLicensesPropertyGatherer) |
| 206 | ctx.PreArchMutators(RegisterVisibilityRuleGatherer) |
| 207 | ctx.PostDepsMutators(RegisterVisibilityRuleEnforcer) |
| 208 | ctx.PostDepsMutators(RegisterLicensesDependencyChecker) |
| 209 | ctx.Register() |
| 210 | |
| 211 | _, errs := ctx.ParseBlueprintsFiles(".") |
| 212 | if len(errs) > 0 { |
| 213 | return ctx, errs |
| 214 | } |
| 215 | |
| 216 | _, errs = ctx.PrepareBuildActions(config) |
| 217 | return ctx, errs |
| 218 | } |
| 219 | |
| 220 | type mockRuleModule struct { |
| 221 | ModuleBase |
| 222 | DefaultableModuleBase |
| 223 | } |
| 224 | |
| 225 | func newMockRuleModule() Module { |
| 226 | m := &mockRuleModule{} |
| 227 | InitAndroidModule(m) |
| 228 | InitDefaultableModule(m) |
| 229 | return m |
| 230 | } |
| 231 | |
| 232 | func (p *mockRuleModule) GenerateAndroidBuildActions(ModuleContext) { |
| 233 | } |