Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 1 | package android |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | ) |
| 6 | |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 7 | // Common test set up for license tests. |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 8 | var prepareForLicenseTest = GroupFixturePreparers( |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 9 | // General preparers in alphabetical order. |
| 10 | PrepareForTestWithDefaults, |
Paul Duffin | e96108d | 2021-05-06 16:39:27 +0100 | [diff] [blame] | 11 | PrepareForTestWithLicenses, |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 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 | |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 22 | var licenseTests = []struct { |
| 23 | name string |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 24 | fs MockFS |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 25 | expectedErrors []string |
| 26 | }{ |
| 27 | { |
| 28 | name: "license must not accept licenses property", |
| 29 | fs: map[string][]byte{ |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 30 | "top/Android.bp": []byte(` |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 31 | license { |
| 32 | name: "top_license", |
| 33 | visibility: ["//visibility:private"], |
| 34 | licenses: ["other_license"], |
| 35 | }`), |
| 36 | }, |
| 37 | expectedErrors: []string{ |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 38 | `top/Android.bp:5:14: unrecognized property "licenses"`, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 39 | }, |
| 40 | }, |
| 41 | { |
| 42 | name: "private license", |
| 43 | fs: map[string][]byte{ |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 44 | "top/Android.bp": []byte(` |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 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 | }`), |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 56 | "other/Android.bp": []byte(` |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 57 | rule { |
| 58 | name: "arule", |
| 59 | licenses: ["top_allowed_as_notice"], |
| 60 | }`), |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 61 | "yetmore/Android.bp": []byte(` |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 62 | package { |
| 63 | default_applicable_licenses: ["top_allowed_as_notice"], |
| 64 | }`), |
| 65 | }, |
| 66 | expectedErrors: []string{ |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 67 | `other/Android.bp:2:5: module "arule": depends on //top:top_allowed_as_notice ` + |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 68 | `which is not visible to this module`, |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 69 | `yetmore/Android.bp:2:5: module "//yetmore": depends on //top:top_allowed_as_notice ` + |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 70 | `which is not visible to this module`, |
| 71 | }, |
| 72 | }, |
| 73 | { |
| 74 | name: "must reference license_kind module", |
| 75 | fs: map[string][]byte{ |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 76 | "top/Android.bp": []byte(` |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 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{ |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 88 | `top/Android.bp:6:5: module "top_proprietary": license_kinds property ` + |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 89 | `"top_by_exception_only" is not a license_kind module`, |
| 90 | }, |
| 91 | }, |
| 92 | { |
Cole Faust | 2ced8c8 | 2022-11-22 16:51:21 -0800 | [diff] [blame] | 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 | { |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 123 | name: "license_kind module must exist", |
| 124 | fs: map[string][]byte{ |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 125 | "top/Android.bp": []byte(` |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 126 | license { |
| 127 | name: "top_notice_allowed", |
| 128 | license_kinds: ["top_notice"], |
| 129 | visibility: ["//visibility:public"], |
| 130 | }`), |
| 131 | }, |
| 132 | expectedErrors: []string{ |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 133 | `top/Android.bp:2:5: "top_notice_allowed" depends on undefined module "top_notice"`, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 134 | }, |
| 135 | }, |
| 136 | { |
| 137 | name: "public license", |
| 138 | fs: map[string][]byte{ |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 139 | "top/Android.bp": []byte(` |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 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 | }`), |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 151 | "other/Android.bp": []byte(` |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 152 | rule { |
| 153 | name: "arule", |
| 154 | licenses: ["top_proprietary"], |
| 155 | }`), |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 156 | "yetmore/Android.bp": []byte(` |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 157 | package { |
| 158 | default_applicable_licenses: ["top_proprietary"], |
| 159 | }`), |
| 160 | }, |
| 161 | }, |
| 162 | { |
| 163 | name: "multiple licenses", |
| 164 | fs: map[string][]byte{ |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 165 | "top/Android.bp": []byte(` |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 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 | }`), |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 195 | "other/Android.bp": []byte(` |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 196 | rule { |
| 197 | name: "arule", |
| 198 | licenses: ["top_proprietary"], |
| 199 | }`), |
Lukacs T. Berki | b838b0a | 2021-09-02 11:46:24 +0200 | [diff] [blame] | 200 | "yetmore/Android.bp": []byte(` |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 201 | package { |
| 202 | default_applicable_licenses: ["top_proprietary"], |
| 203 | }`), |
| 204 | }, |
| 205 | }, |
| 206 | } |
| 207 | |
| 208 | func TestLicense(t *testing.T) { |
| 209 | for _, test := range licenseTests { |
| 210 | t.Run(test.name, func(t *testing.T) { |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 211 | // Customize the common license text fixture factory. |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 212 | GroupFixturePreparers( |
| 213 | prepareForLicenseTest, |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 214 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 215 | ctx.RegisterModuleType("rule", newMockRuleModule) |
| 216 | }), |
| 217 | test.fs.AddToFixture(), |
| 218 | ). |
| 219 | ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)). |
| 220 | RunTest(t) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 221 | }) |
| 222 | } |
| 223 | } |
| 224 | |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 225 | func testLicense(t *testing.T, fs MockFS, expectedErrors []string) { |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | type mockRuleModule struct { |
| 229 | ModuleBase |
| 230 | DefaultableModuleBase |
| 231 | } |
| 232 | |
| 233 | func newMockRuleModule() Module { |
| 234 | m := &mockRuleModule{} |
| 235 | InitAndroidModule(m) |
| 236 | InitDefaultableModule(m) |
| 237 | return m |
| 238 | } |
| 239 | |
| 240 | func (p *mockRuleModule) GenerateAndroidBuildActions(ModuleContext) { |
| 241 | } |