| 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, | 
|  | 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 |  | 
| 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{ | 
|  | 30 | "top/Blueprints": []byte(` | 
|  | 31 | license { | 
|  | 32 | name: "top_license", | 
|  | 33 | visibility: ["//visibility:private"], | 
|  | 34 | licenses: ["other_license"], | 
|  | 35 | }`), | 
|  | 36 | }, | 
|  | 37 | expectedErrors: []string{ | 
|  | 38 | `top/Blueprints:5:14: unrecognized property "licenses"`, | 
|  | 39 | }, | 
|  | 40 | }, | 
|  | 41 | { | 
|  | 42 | name: "private license", | 
|  | 43 | fs: map[string][]byte{ | 
|  | 44 | "top/Blueprints": []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/Blueprints": []byte(` | 
|  | 57 | rule { | 
|  | 58 | name: "arule", | 
|  | 59 | licenses: ["top_allowed_as_notice"], | 
|  | 60 | }`), | 
|  | 61 | "yetmore/Blueprints": []byte(` | 
|  | 62 | package { | 
|  | 63 | default_applicable_licenses: ["top_allowed_as_notice"], | 
|  | 64 | }`), | 
|  | 65 | }, | 
|  | 66 | expectedErrors: []string{ | 
| Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 67 | `other/Blueprints: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`, | 
| Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 69 | `yetmore/Blueprints: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{ | 
|  | 76 | "top/Blueprints": []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{ | 
| Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 88 | `top/Blueprints: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 | { | 
|  | 93 | name: "license_kind module must exist", | 
|  | 94 | fs: map[string][]byte{ | 
|  | 95 | "top/Blueprints": []byte(` | 
|  | 96 | license { | 
|  | 97 | name: "top_notice_allowed", | 
|  | 98 | license_kinds: ["top_notice"], | 
|  | 99 | visibility: ["//visibility:public"], | 
|  | 100 | }`), | 
|  | 101 | }, | 
|  | 102 | expectedErrors: []string{ | 
|  | 103 | `top/Blueprints:2:5: "top_notice_allowed" depends on undefined module "top_notice"`, | 
|  | 104 | }, | 
|  | 105 | }, | 
|  | 106 | { | 
|  | 107 | name: "public license", | 
|  | 108 | fs: map[string][]byte{ | 
|  | 109 | "top/Blueprints": []byte(` | 
|  | 110 | 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 | }`), | 
|  | 121 | "other/Blueprints": []byte(` | 
|  | 122 | rule { | 
|  | 123 | name: "arule", | 
|  | 124 | licenses: ["top_proprietary"], | 
|  | 125 | }`), | 
|  | 126 | "yetmore/Blueprints": []byte(` | 
|  | 127 | package { | 
|  | 128 | default_applicable_licenses: ["top_proprietary"], | 
|  | 129 | }`), | 
|  | 130 | }, | 
|  | 131 | }, | 
|  | 132 | { | 
|  | 133 | name: "multiple licenses", | 
|  | 134 | fs: map[string][]byte{ | 
|  | 135 | "top/Blueprints": []byte(` | 
|  | 136 | 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 | }`), | 
|  | 165 | "other/Blueprints": []byte(` | 
|  | 166 | rule { | 
|  | 167 | name: "arule", | 
|  | 168 | licenses: ["top_proprietary"], | 
|  | 169 | }`), | 
|  | 170 | "yetmore/Blueprints": []byte(` | 
|  | 171 | package { | 
|  | 172 | default_applicable_licenses: ["top_proprietary"], | 
|  | 173 | }`), | 
|  | 174 | }, | 
|  | 175 | }, | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | func TestLicense(t *testing.T) { | 
|  | 179 | for _, test := range licenseTests { | 
|  | 180 | t.Run(test.name, func(t *testing.T) { | 
| Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 181 | // Customize the common license text fixture factory. | 
| Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 182 | GroupFixturePreparers( | 
|  | 183 | prepareForLicenseTest, | 
| Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 184 | FixtureRegisterWithContext(func(ctx RegistrationContext) { | 
|  | 185 | ctx.RegisterModuleType("rule", newMockRuleModule) | 
|  | 186 | }), | 
|  | 187 | test.fs.AddToFixture(), | 
|  | 188 | ). | 
|  | 189 | ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)). | 
|  | 190 | RunTest(t) | 
| Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 191 | }) | 
|  | 192 | } | 
|  | 193 | } | 
|  | 194 |  | 
| Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame] | 195 | func testLicense(t *testing.T, fs MockFS, expectedErrors []string) { | 
| Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 196 | } | 
|  | 197 |  | 
|  | 198 | type mockRuleModule struct { | 
|  | 199 | ModuleBase | 
|  | 200 | DefaultableModuleBase | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | func newMockRuleModule() Module { | 
|  | 204 | m := &mockRuleModule{} | 
|  | 205 | InitAndroidModule(m) | 
|  | 206 | InitDefaultableModule(m) | 
|  | 207 | return m | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | func (p *mockRuleModule) GenerateAndroidBuildActions(ModuleContext) { | 
|  | 211 | } |