blob: 9f68713746f74f49c95691b30d228c64d3db0c40 [file] [log] [blame]
Bob Badour37af0462021-01-07 03:34:31 +00001package android
2
3import (
4 "testing"
5)
6
7var 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 Duffin3c298a32021-03-04 17:44:03 +000052 `other/Blueprints:2:5: module "arule": depends on //top:top_allowed_as_notice ` +
Bob Badour37af0462021-01-07 03:34:31 +000053 `which is not visible to this module`,
Paul Duffin3c298a32021-03-04 17:44:03 +000054 `yetmore/Blueprints:2:5: module "//yetmore": depends on //top:top_allowed_as_notice ` +
Bob Badour37af0462021-01-07 03:34:31 +000055 `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 Duffin3c298a32021-03-04 17:44:03 +000073 `top/Blueprints:6:5: module "top_proprietary": license_kinds property ` +
Bob Badour37af0462021-01-07 03:34:31 +000074 `"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
163func 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
189func 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
220type mockRuleModule struct {
221 ModuleBase
222 DefaultableModuleBase
223}
224
225func newMockRuleModule() Module {
226 m := &mockRuleModule{}
227 InitAndroidModule(m)
228 InitDefaultableModule(m)
229 return m
230}
231
232func (p *mockRuleModule) GenerateAndroidBuildActions(ModuleContext) {
233}