blob: 7222cd74174ca97258e954b1a40c8c78ad688b89 [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 {
93 name: "license_kind module must exist",
94 fs: map[string][]byte{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020095 "top/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +000096 license {
97 name: "top_notice_allowed",
98 license_kinds: ["top_notice"],
99 visibility: ["//visibility:public"],
100 }`),
101 },
102 expectedErrors: []string{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +0200103 `top/Android.bp:2:5: "top_notice_allowed" depends on undefined module "top_notice"`,
Bob Badour37af0462021-01-07 03:34:31 +0000104 },
105 },
106 {
107 name: "public license",
108 fs: map[string][]byte{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +0200109 "top/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +0000110 license_kind {
111 name: "top_by_exception_only",
112 conditions: ["by_exception_only"],
113 visibility: ["//visibility:private"],
114 }
115
116 license {
117 name: "top_proprietary",
118 license_kinds: ["top_by_exception_only"],
119 visibility: ["//visibility:public"],
120 }`),
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +0200121 "other/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +0000122 rule {
123 name: "arule",
124 licenses: ["top_proprietary"],
125 }`),
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +0200126 "yetmore/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +0000127 package {
128 default_applicable_licenses: ["top_proprietary"],
129 }`),
130 },
131 },
132 {
133 name: "multiple licenses",
134 fs: map[string][]byte{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +0200135 "top/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +0000136 package {
137 default_applicable_licenses: ["top_proprietary"],
138 }
139
140 license_kind {
141 name: "top_notice",
142 conditions: ["notice"],
143 }
144
145 license_kind {
146 name: "top_by_exception_only",
147 conditions: ["by_exception_only"],
148 visibility: ["//visibility:public"],
149 }
150
151 license {
152 name: "top_allowed_as_notice",
153 license_kinds: ["top_notice"],
154 }
155
156 license {
157 name: "top_proprietary",
158 license_kinds: ["top_by_exception_only"],
159 visibility: ["//visibility:public"],
160 }
161 rule {
162 name: "myrule",
163 licenses: ["top_allowed_as_notice", "top_proprietary"]
164 }`),
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +0200165 "other/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +0000166 rule {
167 name: "arule",
168 licenses: ["top_proprietary"],
169 }`),
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +0200170 "yetmore/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +0000171 package {
172 default_applicable_licenses: ["top_proprietary"],
173 }`),
174 },
175 },
176}
177
178func TestLicense(t *testing.T) {
179 for _, test := range licenseTests {
180 t.Run(test.name, func(t *testing.T) {
Paul Duffin8bd28652021-03-03 00:42:36 +0000181 // Customize the common license text fixture factory.
Paul Duffin30ac3e72021-03-20 00:36:14 +0000182 GroupFixturePreparers(
183 prepareForLicenseTest,
Paul Duffin8bd28652021-03-03 00:42:36 +0000184 FixtureRegisterWithContext(func(ctx RegistrationContext) {
185 ctx.RegisterModuleType("rule", newMockRuleModule)
186 }),
187 test.fs.AddToFixture(),
188 ).
189 ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)).
190 RunTest(t)
Bob Badour37af0462021-01-07 03:34:31 +0000191 })
192 }
193}
194
Paul Duffin8bd28652021-03-03 00:42:36 +0000195func testLicense(t *testing.T, fs MockFS, expectedErrors []string) {
Bob Badour37af0462021-01-07 03:34:31 +0000196}
197
198type mockRuleModule struct {
199 ModuleBase
200 DefaultableModuleBase
201}
202
203func newMockRuleModule() Module {
204 m := &mockRuleModule{}
205 InitAndroidModule(m)
206 InitDefaultableModule(m)
207 return m
208}
209
210func (p *mockRuleModule) GenerateAndroidBuildActions(ModuleContext) {
211}