aboutsummaryrefslogtreecommitdiff
path: root/android/license_test.go
blob: 89e7f06e8e3ffb7ea60a5b81dc08635140b8cb12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package android

import (
	"testing"
)

// Common test set up for license tests.
var prepareForLicenseTest = GroupFixturePreparers(
	// General preparers in alphabetical order.
	PrepareForTestWithDefaults,
	PrepareForTestWithLicenses,
	PrepareForTestWithOverrides,
	PrepareForTestWithPackageModule,
	PrepareForTestWithPrebuilts,
	PrepareForTestWithVisibility,

	// Additional test specific stuff
	prepareForTestWithFakePrebuiltModules,
	FixtureMergeEnv(map[string]string{"ANDROID_REQUIRE_LICENSES": "1"}),
)

var licenseTests = []struct {
	name           string
	fs             MockFS
	expectedErrors []string
}{
	{
		name: "license must not accept licenses property",
		fs: map[string][]byte{
			"top/Android.bp": []byte(`
				license {
					name: "top_license",
					visibility: ["//visibility:private"],
					licenses: ["other_license"],
				}`),
		},
		expectedErrors: []string{
			`top/Android.bp:5:14: unrecognized property "licenses"`,
		},
	},
	{
		name: "private license",
		fs: map[string][]byte{
			"top/Android.bp": []byte(`
				license_kind {
					name: "top_notice",
					conditions: ["notice"],
					visibility: ["//visibility:private"],
				}

				license {
					name: "top_allowed_as_notice",
					license_kinds: ["top_notice"],
					visibility: ["//visibility:private"],
				}`),
			"other/Android.bp": []byte(`
				rule {
					name: "arule",
					licenses: ["top_allowed_as_notice"],
				}`),
			"yetmore/Android.bp": []byte(`
				package {
					default_applicable_licenses: ["top_allowed_as_notice"],
				}`),
		},
		expectedErrors: []string{
			`other/Android.bp:2:5: module "arule": depends on //top:top_allowed_as_notice ` +
				`which is not visible to this module`,
			`yetmore/Android.bp:2:5: module "//yetmore": depends on //top:top_allowed_as_notice ` +
				`which is not visible to this module`,
		},
	},
	{
		name: "must reference license_kind module",
		fs: map[string][]byte{
			"top/Android.bp": []byte(`
				rule {
					name: "top_by_exception_only",
				}

				license {
					name: "top_proprietary",
					license_kinds: ["top_by_exception_only"],
					visibility: ["//visibility:public"],
				}`),
		},
		expectedErrors: []string{
			`top/Android.bp:6:5: module "top_proprietary": license_kinds property ` +
				`"top_by_exception_only" is not a license_kind module`,
		},
	},
	{
		name: "must not duplicate license_kind",
		fs: map[string][]byte{
			"top/Android.bp": []byte(`
				license_kind {
					name: "top_by_exception_only",
					conditions: ["by_exception_only"],
					visibility: ["//visibility:private"],
				}

				license_kind {
					name: "top_by_exception_only_2",
					conditions: ["by_exception_only"],
					visibility: ["//visibility:private"],
				}

				license {
					name: "top_proprietary",
					license_kinds: [
						"top_by_exception_only",
						"top_by_exception_only_2",
						"top_by_exception_only"
					],
					visibility: ["//visibility:public"],
				}`),
		},
		expectedErrors: []string{
			`top/Android.bp:14:5: module "top_proprietary": Duplicated license kind: "top_by_exception_only"`,
		},
	},
	{
		name: "license_kind module must exist",
		fs: map[string][]byte{
			"top/Android.bp": []byte(`
				license {
					name: "top_notice_allowed",
					license_kinds: ["top_notice"],
					visibility: ["//visibility:public"],
				}`),
		},
		expectedErrors: []string{
			`top/Android.bp:2:5: "top_notice_allowed" depends on undefined module "top_notice"`,
		},
	},
	{
		name: "public license",
		fs: map[string][]byte{
			"top/Android.bp": []byte(`
				license_kind {
					name: "top_by_exception_only",
					conditions: ["by_exception_only"],
					visibility: ["//visibility:private"],
				}

				license {
					name: "top_proprietary",
					license_kinds: ["top_by_exception_only"],
					visibility: ["//visibility:public"],
				}`),
			"other/Android.bp": []byte(`
				rule {
					name: "arule",
					licenses: ["top_proprietary"],
				}`),
			"yetmore/Android.bp": []byte(`
				package {
					default_applicable_licenses: ["top_proprietary"],
				}`),
		},
	},
	{
		name: "multiple licenses",
		fs: map[string][]byte{
			"top/Android.bp": []byte(`
				package {
					default_applicable_licenses: ["top_proprietary"],
				}

				license_kind {
					name: "top_notice",
					conditions: ["notice"],
				}

				license_kind {
					name: "top_by_exception_only",
					conditions: ["by_exception_only"],
					visibility: ["//visibility:public"],
				}

				license {
					name: "top_allowed_as_notice",
					license_kinds: ["top_notice"],
				}

				license {
					name: "top_proprietary",
					license_kinds: ["top_by_exception_only"],
					visibility: ["//visibility:public"],
				}
				rule {
					name: "myrule",
					licenses: ["top_allowed_as_notice", "top_proprietary"]
				}`),
			"other/Android.bp": []byte(`
				rule {
					name: "arule",
					licenses: ["top_proprietary"],
				}`),
			"yetmore/Android.bp": []byte(`
				package {
					default_applicable_licenses: ["top_proprietary"],
				}`),
		},
	},
}

func TestLicense(t *testing.T) {
	for _, test := range licenseTests {
		t.Run(test.name, func(t *testing.T) {
			// Customize the common license text fixture factory.
			GroupFixturePreparers(
				prepareForLicenseTest,
				FixtureRegisterWithContext(func(ctx RegistrationContext) {
					ctx.RegisterModuleType("rule", newMockRuleModule)
				}),
				test.fs.AddToFixture(),
			).
				ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)).
				RunTest(t)
		})
	}
}

func testLicense(t *testing.T, fs MockFS, expectedErrors []string) {
}

type mockRuleModule struct {
	ModuleBase
	DefaultableModuleBase
}

func newMockRuleModule() Module {
	m := &mockRuleModule{}
	InitAndroidModule(m)
	InitDefaultableModule(m)
	return m
}

func (p *mockRuleModule) GenerateAndroidBuildActions(ModuleContext) {
}