xref: /aosp_15_r20/build/soong/android/license_test.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1package android
2
3import (
4	"testing"
5)
6
7// Common test set up for license tests.
8var prepareForLicenseTest = GroupFixturePreparers(
9	// General preparers in alphabetical order.
10	PrepareForTestWithDefaults,
11	PrepareForTestWithLicenses,
12	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
22var licenseTests = []struct {
23	name           string
24	fs             MockFS
25	expectedErrors []string
26}{
27	{
28		name: "license must not accept licenses property",
29		fs: map[string][]byte{
30			"top/Android.bp": []byte(`
31				license {
32					name: "top_license",
33					visibility: ["//visibility:private"],
34					licenses: ["other_license"],
35				}`),
36		},
37		expectedErrors: []string{
38			`top/Android.bp:5:14: unrecognized property "licenses"`,
39		},
40	},
41	{
42		name: "private license",
43		fs: map[string][]byte{
44			"top/Android.bp": []byte(`
45				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				}`),
56			"other/Android.bp": []byte(`
57				rule {
58					name: "arule",
59					licenses: ["top_allowed_as_notice"],
60				}`),
61			"yetmore/Android.bp": []byte(`
62				package {
63					default_applicable_licenses: ["top_allowed_as_notice"],
64				}`),
65		},
66		expectedErrors: []string{
67			`other/Android.bp:2:5: module "arule": depends on //top:top_allowed_as_notice ` +
68				`which is not visible to this module`,
69			`yetmore/Android.bp:2:5: module "//yetmore": depends on //top:top_allowed_as_notice ` +
70				`which is not visible to this module`,
71		},
72	},
73	{
74		name: "must reference license_kind module",
75		fs: map[string][]byte{
76			"top/Android.bp": []byte(`
77				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{
88			`top/Android.bp:6:5: module "top_proprietary": license_kinds property ` +
89				`"top_by_exception_only" is not a license_kind module`,
90		},
91	},
92	{
93		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	{
123		name: "license_kind module must exist",
124		fs: map[string][]byte{
125			"top/Android.bp": []byte(`
126				license {
127					name: "top_notice_allowed",
128					license_kinds: ["top_notice"],
129					visibility: ["//visibility:public"],
130				}`),
131		},
132		expectedErrors: []string{
133			`top/Android.bp:2:5: "top_notice_allowed" depends on undefined module "top_notice"`,
134		},
135	},
136	{
137		name: "public license",
138		fs: map[string][]byte{
139			"top/Android.bp": []byte(`
140				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				}`),
151			"other/Android.bp": []byte(`
152				rule {
153					name: "arule",
154					licenses: ["top_proprietary"],
155				}`),
156			"yetmore/Android.bp": []byte(`
157				package {
158					default_applicable_licenses: ["top_proprietary"],
159				}`),
160		},
161	},
162	{
163		name: "multiple licenses",
164		fs: map[string][]byte{
165			"top/Android.bp": []byte(`
166				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				}`),
195			"other/Android.bp": []byte(`
196				rule {
197					name: "arule",
198					licenses: ["top_proprietary"],
199				}`),
200			"yetmore/Android.bp": []byte(`
201				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) {
211			// Customize the common license text fixture factory.
212			GroupFixturePreparers(
213				prepareForLicenseTest,
214				FixtureRegisterWithContext(func(ctx RegistrationContext) {
215					ctx.RegisterModuleType("rule", newMockRuleModule)
216				}),
217				test.fs.AddToFixture(),
218			).
219				ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)).
220				RunTest(t)
221		})
222	}
223}
224
225func testLicense(t *testing.T, fs MockFS, expectedErrors []string) {
226}
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}
242