blob: 89e7f06e8e3ffb7ea60a5b81dc08635140b8cb12 [file] [log] [blame]
Bob Badour37af0462021-01-07 03:34:31 +00001package android
2
3import (
4 "testing"
5)
6
Paul Duffin8bd28652021-03-03 00:42:36 +00007// Common test set up for license tests.
Paul Duffin30ac3e72021-03-20 00:36:14 +00008var prepareForLicenseTest = GroupFixturePreparers(
Paul Duffin8bd28652021-03-03 00:42:36 +00009 // General preparers in alphabetical order.
10 PrepareForTestWithDefaults,
Paul Duffine96108d2021-05-06 16:39:27 +010011 PrepareForTestWithLicenses,
Paul Duffin8bd28652021-03-03 00:42:36 +000012 PrepareForTestWithOverrides,
13 PrepareForTestWithPackageModule,
14 PrepareForTestWithPrebuilts,
15 PrepareForTestWithVisibility,
16
17 // Additional test specific stuff
18 prepareForTestWithFakePrebuiltModules,
19 FixtureMergeEnv(map[string]string{"ANDROID_REQUIRE_LICENSES": "1"}),
20)
21
Bob Badour37af0462021-01-07 03:34:31 +000022var licenseTests = []struct {
23 name string
Paul Duffin8bd28652021-03-03 00:42:36 +000024 fs MockFS
Bob Badour37af0462021-01-07 03:34:31 +000025 expectedErrors []string
26}{
27 {
28 name: "license must not accept licenses property",
29 fs: map[string][]byte{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020030 "top/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +000031 license {
32 name: "top_license",
33 visibility: ["//visibility:private"],
34 licenses: ["other_license"],
35 }`),
36 },
37 expectedErrors: []string{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020038 `top/Android.bp:5:14: unrecognized property "licenses"`,
Bob Badour37af0462021-01-07 03:34:31 +000039 },
40 },
41 {
42 name: "private license",
43 fs: map[string][]byte{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020044 "top/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +000045 license_kind {
46 name: "top_notice",
47 conditions: ["notice"],
48 visibility: ["//visibility:private"],
49 }
50
51 license {
52 name: "top_allowed_as_notice",
53 license_kinds: ["top_notice"],
54 visibility: ["//visibility:private"],
55 }`),
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020056 "other/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +000057 rule {
58 name: "arule",
59 licenses: ["top_allowed_as_notice"],
60 }`),
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020061 "yetmore/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +000062 package {
63 default_applicable_licenses: ["top_allowed_as_notice"],
64 }`),
65 },
66 expectedErrors: []string{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020067 `other/Android.bp:2:5: module "arule": depends on //top:top_allowed_as_notice ` +
Bob Badour37af0462021-01-07 03:34:31 +000068 `which is not visible to this module`,
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020069 `yetmore/Android.bp:2:5: module "//yetmore": depends on //top:top_allowed_as_notice ` +
Bob Badour37af0462021-01-07 03:34:31 +000070 `which is not visible to this module`,
71 },
72 },
73 {
74 name: "must reference license_kind module",
75 fs: map[string][]byte{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020076 "top/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +000077 rule {
78 name: "top_by_exception_only",
79 }
80
81 license {
82 name: "top_proprietary",
83 license_kinds: ["top_by_exception_only"],
84 visibility: ["//visibility:public"],
85 }`),
86 },
87 expectedErrors: []string{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020088 `top/Android.bp:6:5: module "top_proprietary": license_kinds property ` +
Bob Badour37af0462021-01-07 03:34:31 +000089 `"top_by_exception_only" is not a license_kind module`,
90 },
91 },
92 {
Cole Faust2ced8c82022-11-22 16:51:21 -080093 name: "must not duplicate license_kind",
94 fs: map[string][]byte{
95 "top/Android.bp": []byte(`
96 license_kind {
97 name: "top_by_exception_only",
98 conditions: ["by_exception_only"],
99 visibility: ["//visibility:private"],
100 }
101
102 license_kind {
103 name: "top_by_exception_only_2",
104 conditions: ["by_exception_only"],
105 visibility: ["//visibility:private"],
106 }
107
108 license {
109 name: "top_proprietary",
110 license_kinds: [
111 "top_by_exception_only",
112 "top_by_exception_only_2",
113 "top_by_exception_only"
114 ],
115 visibility: ["//visibility:public"],
116 }`),
117 },
118 expectedErrors: []string{
119 `top/Android.bp:14:5: module "top_proprietary": Duplicated license kind: "top_by_exception_only"`,
120 },
121 },
122 {
Bob Badour37af0462021-01-07 03:34:31 +0000123 name: "license_kind module must exist",
124 fs: map[string][]byte{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +0200125 "top/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +0000126 license {
127 name: "top_notice_allowed",
128 license_kinds: ["top_notice"],
129 visibility: ["//visibility:public"],
130 }`),
131 },
132 expectedErrors: []string{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +0200133 `top/Android.bp:2:5: "top_notice_allowed" depends on undefined module "top_notice"`,
Bob Badour37af0462021-01-07 03:34:31 +0000134 },
135 },
136 {
137 name: "public license",
138 fs: map[string][]byte{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +0200139 "top/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +0000140 license_kind {
141 name: "top_by_exception_only",
142 conditions: ["by_exception_only"],
143 visibility: ["//visibility:private"],
144 }
145
146 license {
147 name: "top_proprietary",
148 license_kinds: ["top_by_exception_only"],
149 visibility: ["//visibility:public"],
150 }`),
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +0200151 "other/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +0000152 rule {
153 name: "arule",
154 licenses: ["top_proprietary"],
155 }`),
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +0200156 "yetmore/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +0000157 package {
158 default_applicable_licenses: ["top_proprietary"],
159 }`),
160 },
161 },
162 {
163 name: "multiple licenses",
164 fs: map[string][]byte{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +0200165 "top/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +0000166 package {
167 default_applicable_licenses: ["top_proprietary"],
168 }
169
170 license_kind {
171 name: "top_notice",
172 conditions: ["notice"],
173 }
174
175 license_kind {
176 name: "top_by_exception_only",
177 conditions: ["by_exception_only"],
178 visibility: ["//visibility:public"],
179 }
180
181 license {
182 name: "top_allowed_as_notice",
183 license_kinds: ["top_notice"],
184 }
185
186 license {
187 name: "top_proprietary",
188 license_kinds: ["top_by_exception_only"],
189 visibility: ["//visibility:public"],
190 }
191 rule {
192 name: "myrule",
193 licenses: ["top_allowed_as_notice", "top_proprietary"]
194 }`),
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +0200195 "other/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +0000196 rule {
197 name: "arule",
198 licenses: ["top_proprietary"],
199 }`),
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +0200200 "yetmore/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +0000201 package {
202 default_applicable_licenses: ["top_proprietary"],
203 }`),
204 },
205 },
206}
207
208func TestLicense(t *testing.T) {
209 for _, test := range licenseTests {
210 t.Run(test.name, func(t *testing.T) {
Paul Duffin8bd28652021-03-03 00:42:36 +0000211 // Customize the common license text fixture factory.
Paul Duffin30ac3e72021-03-20 00:36:14 +0000212 GroupFixturePreparers(
213 prepareForLicenseTest,
Paul Duffin8bd28652021-03-03 00:42:36 +0000214 FixtureRegisterWithContext(func(ctx RegistrationContext) {
215 ctx.RegisterModuleType("rule", newMockRuleModule)
216 }),
217 test.fs.AddToFixture(),
218 ).
219 ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)).
220 RunTest(t)
Bob Badour37af0462021-01-07 03:34:31 +0000221 })
222 }
223}
224
Paul Duffin8bd28652021-03-03 00:42:36 +0000225func testLicense(t *testing.T, fs MockFS, expectedErrors []string) {
Bob Badour37af0462021-01-07 03:34:31 +0000226}
227
228type mockRuleModule struct {
229 ModuleBase
230 DefaultableModuleBase
231}
232
233func newMockRuleModule() Module {
234 m := &mockRuleModule{}
235 InitAndroidModule(m)
236 InitDefaultableModule(m)
237 return m
238}
239
240func (p *mockRuleModule) GenerateAndroidBuildActions(ModuleContext) {
241}