| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "path/filepath" |
| 19 | "reflect" |
| Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 20 | "regexp" |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 21 | "strconv" |
| 22 | "strings" |
| 23 | |
| 24 | "github.com/google/blueprint/proptools" |
| 25 | ) |
| 26 | |
| 27 | // "neverallow" rules for the build system. |
| 28 | // |
| 29 | // This allows things which aren't related to the build system and are enforced |
| 30 | // for sanity, in progress code refactors, or policy to be expressed in a |
| 31 | // straightforward away disjoint from implementations and tests which should |
| 32 | // work regardless of these restrictions. |
| 33 | // |
| 34 | // A module is disallowed if all of the following are true: |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 35 | // - it is in one of the "In" paths |
| 36 | // - it is not in one of the "NotIn" paths |
| 37 | // - it has all "With" properties matched |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 38 | // - - values are matched in their entirety |
| 39 | // - - nil is interpreted as an empty string |
| 40 | // - - nested properties are separated with a '.' |
| 41 | // - - if the property is a list, any of the values in the list being matches |
| 42 | // counts as a match |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 43 | // - it has none of the "Without" properties matched (same rules as above) |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 44 | |
| Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 45 | func registerNeverallowMutator(ctx RegisterMutatorsContext) { |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 46 | ctx.BottomUp("neverallow", neverallowMutator).Parallel() |
| 47 | } |
| 48 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 49 | var neverallows = []Rule{} |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 50 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 51 | func init() { |
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 52 | AddNeverAllowRules(createIncludeDirsRules()...) |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 53 | AddNeverAllowRules(createTrebleRules()...) |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 54 | AddNeverAllowRules(createJavaDeviceForHostRules()...) |
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 55 | AddNeverAllowRules(createCcSdkVariantRules()...) |
| David Srbecky | e033cba | 2020-05-20 22:20:28 +0100 | [diff] [blame] | 56 | AddNeverAllowRules(createUncompressDexRules()...) |
| Yifan Hong | 696ed4d | 2020-07-27 12:59:58 -0700 | [diff] [blame] | 57 | AddNeverAllowRules(createMakefileGoalRules()...) |
| Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 58 | } |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 59 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 60 | // Add a NeverAllow rule to the set of rules to apply. |
| 61 | func AddNeverAllowRules(rules ...Rule) { |
| 62 | neverallows = append(neverallows, rules...) |
| 63 | } |
| 64 | |
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 65 | func createIncludeDirsRules() []Rule { |
| Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 66 | notInIncludeDir := []string{ |
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 67 | "art", |
| Orion Hodson | 6341f01 | 2019-11-06 13:39:46 +0000 | [diff] [blame] | 68 | "art/libnativebridge", |
| 69 | "art/libnativeloader", |
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 70 | "libcore", |
| 71 | "libnativehelper", |
| 72 | "external/apache-harmony", |
| 73 | "external/apache-xml", |
| 74 | "external/boringssl", |
| 75 | "external/bouncycastle", |
| 76 | "external/conscrypt", |
| 77 | "external/icu", |
| 78 | "external/okhttp", |
| 79 | "external/vixl", |
| 80 | "external/wycheproof", |
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 81 | } |
| Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 82 | noUseIncludeDir := []string{ |
| Steven Moreland | f36a3ac | 2021-04-27 18:03:14 +0000 | [diff] [blame] | 83 | "frameworks/av/apex", |
| 84 | "frameworks/av/tools", |
| 85 | "frameworks/native/cmds", |
| 86 | "system/apex", |
| 87 | "system/bpf", |
| 88 | "system/gatekeeper", |
| 89 | "system/hwservicemanager", |
| 90 | "system/libbase", |
| Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 91 | "system/libfmq", |
| Steven Moreland | f36a3ac | 2021-04-27 18:03:14 +0000 | [diff] [blame] | 92 | "system/libvintf", |
| Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 93 | } |
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 94 | |
| Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 95 | rules := make([]Rule, 0, len(notInIncludeDir)+len(noUseIncludeDir)) |
| 96 | |
| 97 | for _, path := range notInIncludeDir { |
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 98 | rule := |
| 99 | NeverAllow(). |
| 100 | WithMatcher("include_dirs", StartsWith(path+"/")). |
| 101 | Because("include_dirs is deprecated, all usages of '" + path + "' have been migrated" + |
| 102 | " to use alternate mechanisms and so can no longer be used.") |
| 103 | |
| 104 | rules = append(rules, rule) |
| 105 | } |
| 106 | |
| Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 107 | for _, path := range noUseIncludeDir { |
| 108 | rule := NeverAllow().In(path+"/").WithMatcher("include_dirs", isSetMatcherInstance). |
| 109 | Because("include_dirs is deprecated, all usages of them in '" + path + "' have been migrated" + |
| 110 | " to use alternate mechanisms and so can no longer be used.") |
| 111 | rules = append(rules, rule) |
| 112 | } |
| 113 | |
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 114 | return rules |
| 115 | } |
| 116 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 117 | func createTrebleRules() []Rule { |
| 118 | return []Rule{ |
| 119 | NeverAllow(). |
| 120 | In("vendor", "device"). |
| 121 | With("vndk.enabled", "true"). |
| 122 | Without("vendor", "true"). |
| Justin Yun | 0ecf0b2 | 2020-02-28 15:07:59 +0900 | [diff] [blame] | 123 | Without("product_specific", "true"). |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 124 | Because("the VNDK can never contain a library that is device dependent."), |
| 125 | NeverAllow(). |
| 126 | With("vndk.enabled", "true"). |
| 127 | Without("vendor", "true"). |
| 128 | Without("owner", ""). |
| 129 | Because("a VNDK module can never have an owner."), |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 130 | |
| Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 131 | // TODO(b/67974785): always enforce the manifest |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 132 | NeverAllow(). |
| Steven Moreland | 51ce4f6 | 2020-02-10 17:21:32 -0800 | [diff] [blame] | 133 | Without("name", "libhidlbase-combined-impl"). |
| 134 | Without("name", "libhidlbase"). |
| 135 | Without("name", "libhidlbase_pgo"). |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 136 | With("product_variables.enforce_vintf_manifest.cflags", "*"). |
| 137 | Because("manifest enforcement should be independent of ."), |
| Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 138 | |
| 139 | // TODO(b/67975799): vendor code should always use /vendor/bin/sh |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 140 | NeverAllow(). |
| 141 | Without("name", "libc_bionic_ndk"). |
| 142 | With("product_variables.treble_linker_namespaces.cflags", "*"). |
| 143 | Because("nothing should care if linker namespaces are enabled or not"), |
| Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 144 | |
| 145 | // Example: |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 146 | // *NeverAllow().with("Srcs", "main.cpp")) |
| Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 150 | func createJavaDeviceForHostRules() []Rule { |
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 151 | javaDeviceForHostProjectsAllowedList := []string{ |
| Colin Cross | b5191a5 | 2019-04-11 14:07:38 -0700 | [diff] [blame] | 152 | "external/guava", |
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 153 | "external/robolectric-shadows", |
| 154 | "framework/layoutlib", |
| 155 | } |
| 156 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 157 | return []Rule{ |
| 158 | NeverAllow(). |
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 159 | NotIn(javaDeviceForHostProjectsAllowedList...). |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 160 | ModuleType("java_device_for_host", "java_host_for_device"). |
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 161 | Because("java_device_for_host can only be used in allowed projects"), |
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 165 | func createCcSdkVariantRules() []Rule { |
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 166 | sdkVersionOnlyAllowedList := []string{ |
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 167 | // derive_sdk_prefer32 has stem: "derive_sdk" which conflicts with the derive_sdk. |
| 168 | // This sometimes works because the APEX modules that contain derive_sdk and |
| 169 | // derive_sdk_prefer32 suppress the platform installation rules, but fails when |
| 170 | // the APEX modules contain the SDK variant and the platform variant still exists. |
| Anton Hansson | 4b8e64b | 2020-05-27 18:25:23 +0100 | [diff] [blame] | 171 | "packages/modules/SdkExtensions/derive_sdk", |
| Dan Albert | e2054a9 | 2020-04-20 14:46:47 -0700 | [diff] [blame] | 172 | // These are for apps and shouldn't be used by non-SDK variant modules. |
| 173 | "prebuilts/ndk", |
| 174 | "tools/test/graphicsbenchmark/apps/sample_app", |
| 175 | "tools/test/graphicsbenchmark/functional_tests/java", |
| Dan Albert | 5557605 | 2020-04-20 14:46:47 -0700 | [diff] [blame] | 176 | "vendor/xts/gts-tests/hostsidetests/gamedevicecert/apps/javatests", |
| Chang Li | 9a7158b | 2021-06-18 14:04:50 +0000 | [diff] [blame] | 177 | "external/libtextclassifier/native", |
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 180 | platformVariantPropertiesAllowedList := []string{ |
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 181 | // android_native_app_glue and libRSSupport use native_window.h but target old |
| 182 | // sdk versions (minimum and 9 respectively) where libnativewindow didn't exist, |
| 183 | // so they can't add libnativewindow to shared_libs to get the header directory |
| 184 | // for the platform variant. Allow them to use the platform variant |
| 185 | // property to set shared_libs. |
| 186 | "prebuilts/ndk", |
| 187 | "frameworks/rs", |
| 188 | } |
| 189 | |
| 190 | return []Rule{ |
| 191 | NeverAllow(). |
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 192 | NotIn(sdkVersionOnlyAllowedList...). |
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 193 | WithMatcher("sdk_variant_only", isSetMatcherInstance). |
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 194 | Because("sdk_variant_only can only be used in allowed projects"), |
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 195 | NeverAllow(). |
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 196 | NotIn(platformVariantPropertiesAllowedList...). |
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 197 | WithMatcher("platform.shared_libs", isSetMatcherInstance). |
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 198 | Because("platform variant properties can only be used in allowed projects"), |
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| David Srbecky | e033cba | 2020-05-20 22:20:28 +0100 | [diff] [blame] | 202 | func createUncompressDexRules() []Rule { |
| 203 | return []Rule{ |
| 204 | NeverAllow(). |
| 205 | NotIn("art"). |
| 206 | WithMatcher("uncompress_dex", isSetMatcherInstance). |
| 207 | Because("uncompress_dex is only allowed for certain jars for test in art."), |
| 208 | } |
| 209 | } |
| 210 | |
| Yifan Hong | 696ed4d | 2020-07-27 12:59:58 -0700 | [diff] [blame] | 211 | func createMakefileGoalRules() []Rule { |
| 212 | return []Rule{ |
| 213 | NeverAllow(). |
| 214 | ModuleType("makefile_goal"). |
| 215 | WithoutMatcher("product_out_path", Regexp("^boot[0-9a-zA-Z.-]*[.]img$")). |
| 216 | Because("Only boot images may be imported as a makefile goal."), |
| 217 | } |
| 218 | } |
| 219 | |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 220 | func neverallowMutator(ctx BottomUpMutatorContext) { |
| 221 | m, ok := ctx.Module().(Module) |
| 222 | if !ok { |
| 223 | return |
| 224 | } |
| 225 | |
| 226 | dir := ctx.ModuleDir() + "/" |
| 227 | properties := m.GetProperties() |
| 228 | |
| Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 229 | osClass := ctx.Module().Target().Os.Class |
| 230 | |
| Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 231 | for _, r := range neverallowRules(ctx.Config()) { |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 232 | n := r.(*rule) |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 233 | if !n.appliesToPath(dir) { |
| 234 | continue |
| 235 | } |
| 236 | |
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 237 | if !n.appliesToModuleType(ctx.ModuleType()) { |
| 238 | continue |
| 239 | } |
| 240 | |
| Remi NGUYEN VAN | efb49af | 2021-12-10 00:17:57 +0000 | [diff] [blame] | 241 | if !n.appliesToProperties(ctx, properties) { |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 242 | continue |
| 243 | } |
| 244 | |
| Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 245 | if !n.appliesToOsClass(osClass) { |
| 246 | continue |
| 247 | } |
| 248 | |
| Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 249 | if !n.appliesToDirectDeps(ctx) { |
| 250 | continue |
| 251 | } |
| 252 | |
| Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 253 | if !n.appliesToBootclasspathJar(ctx) { |
| 254 | continue |
| 255 | } |
| 256 | |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 257 | ctx.ModuleErrorf("violates " + n.String()) |
| 258 | } |
| 259 | } |
| 260 | |
| Remi NGUYEN VAN | efb49af | 2021-12-10 00:17:57 +0000 | [diff] [blame] | 261 | type ValueMatcherContext interface { |
| 262 | Config() Config |
| 263 | } |
| 264 | |
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 265 | type ValueMatcher interface { |
| Remi NGUYEN VAN | efb49af | 2021-12-10 00:17:57 +0000 | [diff] [blame] | 266 | Test(ValueMatcherContext, string) bool |
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 267 | String() string |
| 268 | } |
| 269 | |
| 270 | type equalMatcher struct { |
| 271 | expected string |
| 272 | } |
| 273 | |
| Remi NGUYEN VAN | efb49af | 2021-12-10 00:17:57 +0000 | [diff] [blame] | 274 | func (m *equalMatcher) Test(ctx ValueMatcherContext, value string) bool { |
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 275 | return m.expected == value |
| 276 | } |
| 277 | |
| 278 | func (m *equalMatcher) String() string { |
| 279 | return "=" + m.expected |
| 280 | } |
| 281 | |
| 282 | type anyMatcher struct { |
| 283 | } |
| 284 | |
| Remi NGUYEN VAN | efb49af | 2021-12-10 00:17:57 +0000 | [diff] [blame] | 285 | func (m *anyMatcher) Test(ctx ValueMatcherContext, value string) bool { |
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 286 | return true |
| 287 | } |
| 288 | |
| 289 | func (m *anyMatcher) String() string { |
| 290 | return "=*" |
| 291 | } |
| 292 | |
| 293 | var anyMatcherInstance = &anyMatcher{} |
| 294 | |
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 295 | type startsWithMatcher struct { |
| 296 | prefix string |
| 297 | } |
| 298 | |
| Remi NGUYEN VAN | efb49af | 2021-12-10 00:17:57 +0000 | [diff] [blame] | 299 | func (m *startsWithMatcher) Test(ctx ValueMatcherContext, value string) bool { |
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 300 | return strings.HasPrefix(value, m.prefix) |
| 301 | } |
| 302 | |
| 303 | func (m *startsWithMatcher) String() string { |
| 304 | return ".starts-with(" + m.prefix + ")" |
| 305 | } |
| 306 | |
| Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 307 | type regexMatcher struct { |
| 308 | re *regexp.Regexp |
| 309 | } |
| 310 | |
| Remi NGUYEN VAN | efb49af | 2021-12-10 00:17:57 +0000 | [diff] [blame] | 311 | func (m *regexMatcher) Test(ctx ValueMatcherContext, value string) bool { |
| Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 312 | return m.re.MatchString(value) |
| 313 | } |
| 314 | |
| 315 | func (m *regexMatcher) String() string { |
| 316 | return ".regexp(" + m.re.String() + ")" |
| 317 | } |
| 318 | |
| Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 319 | type notInListMatcher struct { |
| 320 | allowed []string |
| 321 | } |
| 322 | |
| Remi NGUYEN VAN | efb49af | 2021-12-10 00:17:57 +0000 | [diff] [blame] | 323 | func (m *notInListMatcher) Test(ctx ValueMatcherContext, value string) bool { |
| Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 324 | return !InList(value, m.allowed) |
| 325 | } |
| 326 | |
| 327 | func (m *notInListMatcher) String() string { |
| 328 | return ".not-in-list(" + strings.Join(m.allowed, ",") + ")" |
| 329 | } |
| 330 | |
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 331 | type isSetMatcher struct{} |
| 332 | |
| Remi NGUYEN VAN | efb49af | 2021-12-10 00:17:57 +0000 | [diff] [blame] | 333 | func (m *isSetMatcher) Test(ctx ValueMatcherContext, value string) bool { |
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 334 | return value != "" |
| 335 | } |
| 336 | |
| 337 | func (m *isSetMatcher) String() string { |
| 338 | return ".is-set" |
| 339 | } |
| 340 | |
| 341 | var isSetMatcherInstance = &isSetMatcher{} |
| 342 | |
| Remi NGUYEN VAN | efb49af | 2021-12-10 00:17:57 +0000 | [diff] [blame] | 343 | type sdkVersionMatcher struct { |
| 344 | condition func(ctx ValueMatcherContext, spec SdkSpec) bool |
| 345 | description string |
| 346 | } |
| 347 | |
| 348 | func (m *sdkVersionMatcher) Test(ctx ValueMatcherContext, value string) bool { |
| 349 | return m.condition(ctx, SdkSpecFromWithConfig(ctx.Config(), value)) |
| 350 | } |
| 351 | |
| 352 | func (m *sdkVersionMatcher) String() string { |
| 353 | return ".sdk-version(" + m.description + ")" |
| 354 | } |
| 355 | |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 356 | type ruleProperty struct { |
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 357 | fields []string // e.x.: Vndk.Enabled |
| 358 | matcher ValueMatcher |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 361 | // A NeverAllow rule. |
| 362 | type Rule interface { |
| 363 | In(path ...string) Rule |
| 364 | |
| 365 | NotIn(path ...string) Rule |
| 366 | |
| Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 367 | InDirectDeps(deps ...string) Rule |
| 368 | |
| Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 369 | WithOsClass(osClasses ...OsClass) Rule |
| 370 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 371 | ModuleType(types ...string) Rule |
| 372 | |
| 373 | NotModuleType(types ...string) Rule |
| 374 | |
| Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 375 | BootclasspathJar() Rule |
| 376 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 377 | With(properties, value string) Rule |
| 378 | |
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 379 | WithMatcher(properties string, matcher ValueMatcher) Rule |
| 380 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 381 | Without(properties, value string) Rule |
| 382 | |
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 383 | WithoutMatcher(properties string, matcher ValueMatcher) Rule |
| 384 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 385 | Because(reason string) Rule |
| 386 | } |
| 387 | |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 388 | type rule struct { |
| 389 | // User string for why this is a thing. |
| 390 | reason string |
| 391 | |
| 392 | paths []string |
| 393 | unlessPaths []string |
| 394 | |
| Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 395 | directDeps map[string]bool |
| 396 | |
| Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 397 | osClasses []OsClass |
| 398 | |
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 399 | moduleTypes []string |
| 400 | unlessModuleTypes []string |
| 401 | |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 402 | props []ruleProperty |
| 403 | unlessProps []ruleProperty |
| Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 404 | |
| 405 | onlyBootclasspathJar bool |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 408 | // Create a new NeverAllow rule. |
| 409 | func NeverAllow() Rule { |
| Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 410 | return &rule{directDeps: make(map[string]bool)} |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 411 | } |
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 412 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 413 | func (r *rule) In(path ...string) Rule { |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 414 | r.paths = append(r.paths, cleanPaths(path)...) |
| 415 | return r |
| 416 | } |
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 417 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 418 | func (r *rule) NotIn(path ...string) Rule { |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 419 | r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...) |
| 420 | return r |
| 421 | } |
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 422 | |
| Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 423 | func (r *rule) InDirectDeps(deps ...string) Rule { |
| 424 | for _, d := range deps { |
| 425 | r.directDeps[d] = true |
| 426 | } |
| 427 | return r |
| 428 | } |
| 429 | |
| Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 430 | func (r *rule) WithOsClass(osClasses ...OsClass) Rule { |
| 431 | r.osClasses = append(r.osClasses, osClasses...) |
| 432 | return r |
| 433 | } |
| 434 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 435 | func (r *rule) ModuleType(types ...string) Rule { |
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 436 | r.moduleTypes = append(r.moduleTypes, types...) |
| 437 | return r |
| 438 | } |
| 439 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 440 | func (r *rule) NotModuleType(types ...string) Rule { |
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 441 | r.unlessModuleTypes = append(r.unlessModuleTypes, types...) |
| 442 | return r |
| 443 | } |
| 444 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 445 | func (r *rule) With(properties, value string) Rule { |
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 446 | return r.WithMatcher(properties, selectMatcher(value)) |
| 447 | } |
| 448 | |
| 449 | func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule { |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 450 | r.props = append(r.props, ruleProperty{ |
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 451 | fields: fieldNamesForProperties(properties), |
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 452 | matcher: matcher, |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 453 | }) |
| 454 | return r |
| 455 | } |
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 456 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 457 | func (r *rule) Without(properties, value string) Rule { |
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 458 | return r.WithoutMatcher(properties, selectMatcher(value)) |
| 459 | } |
| 460 | |
| 461 | func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule { |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 462 | r.unlessProps = append(r.unlessProps, ruleProperty{ |
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 463 | fields: fieldNamesForProperties(properties), |
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 464 | matcher: matcher, |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 465 | }) |
| 466 | return r |
| 467 | } |
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 468 | |
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 469 | func selectMatcher(expected string) ValueMatcher { |
| 470 | if expected == "*" { |
| 471 | return anyMatcherInstance |
| 472 | } |
| 473 | return &equalMatcher{expected: expected} |
| 474 | } |
| 475 | |
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 476 | func (r *rule) Because(reason string) Rule { |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 477 | r.reason = reason |
| 478 | return r |
| 479 | } |
| 480 | |
| Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 481 | func (r *rule) BootclasspathJar() Rule { |
| 482 | r.onlyBootclasspathJar = true |
| 483 | return r |
| 484 | } |
| 485 | |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 486 | func (r *rule) String() string { |
| 487 | s := "neverallow" |
| 488 | for _, v := range r.paths { |
| 489 | s += " dir:" + v + "*" |
| 490 | } |
| 491 | for _, v := range r.unlessPaths { |
| 492 | s += " -dir:" + v + "*" |
| 493 | } |
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 494 | for _, v := range r.moduleTypes { |
| 495 | s += " type:" + v |
| 496 | } |
| 497 | for _, v := range r.unlessModuleTypes { |
| 498 | s += " -type:" + v |
| 499 | } |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 500 | for _, v := range r.props { |
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 501 | s += " " + strings.Join(v.fields, ".") + v.matcher.String() |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 502 | } |
| 503 | for _, v := range r.unlessProps { |
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 504 | s += " -" + strings.Join(v.fields, ".") + v.matcher.String() |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 505 | } |
| Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 506 | for k := range r.directDeps { |
| 507 | s += " deps:" + k |
| 508 | } |
| Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 509 | for _, v := range r.osClasses { |
| 510 | s += " os:" + v.String() |
| 511 | } |
| Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 512 | if r.onlyBootclasspathJar { |
| 513 | s += " inBcp" |
| 514 | } |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 515 | if len(r.reason) != 0 { |
| 516 | s += " which is restricted because " + r.reason |
| 517 | } |
| 518 | return s |
| 519 | } |
| 520 | |
| 521 | func (r *rule) appliesToPath(dir string) bool { |
| Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 522 | includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths) |
| 523 | excludePath := HasAnyPrefix(dir, r.unlessPaths) |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 524 | return includePath && !excludePath |
| 525 | } |
| 526 | |
| Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 527 | func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool { |
| 528 | if len(r.directDeps) == 0 { |
| 529 | return true |
| 530 | } |
| 531 | |
| 532 | matches := false |
| 533 | ctx.VisitDirectDeps(func(m Module) { |
| 534 | if !matches { |
| 535 | name := ctx.OtherModuleName(m) |
| 536 | matches = r.directDeps[name] |
| 537 | } |
| 538 | }) |
| 539 | |
| 540 | return matches |
| 541 | } |
| 542 | |
| Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 543 | func (r *rule) appliesToBootclasspathJar(ctx BottomUpMutatorContext) bool { |
| 544 | if !r.onlyBootclasspathJar { |
| 545 | return true |
| 546 | } |
| 547 | |
| 548 | return InList(ctx.ModuleName(), ctx.Config().BootJars()) |
| 549 | } |
| 550 | |
| Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 551 | func (r *rule) appliesToOsClass(osClass OsClass) bool { |
| 552 | if len(r.osClasses) == 0 { |
| 553 | return true |
| 554 | } |
| 555 | |
| 556 | for _, c := range r.osClasses { |
| 557 | if c == osClass { |
| 558 | return true |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | return false |
| 563 | } |
| 564 | |
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 565 | func (r *rule) appliesToModuleType(moduleType string) bool { |
| 566 | return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes) |
| 567 | } |
| 568 | |
| Remi NGUYEN VAN | efb49af | 2021-12-10 00:17:57 +0000 | [diff] [blame] | 569 | func (r *rule) appliesToProperties(ctx ValueMatcherContext, |
| 570 | properties []interface{}) bool { |
| 571 | includeProps := hasAllProperties(ctx, properties, r.props) |
| 572 | excludeProps := hasAnyProperty(ctx, properties, r.unlessProps) |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 573 | return includeProps && !excludeProps |
| 574 | } |
| 575 | |
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 576 | func StartsWith(prefix string) ValueMatcher { |
| 577 | return &startsWithMatcher{prefix} |
| 578 | } |
| 579 | |
| Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 580 | func Regexp(re string) ValueMatcher { |
| 581 | r, err := regexp.Compile(re) |
| 582 | if err != nil { |
| 583 | panic(err) |
| 584 | } |
| 585 | return ®exMatcher{r} |
| 586 | } |
| 587 | |
| Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 588 | func NotInList(allowed []string) ValueMatcher { |
| 589 | return ¬InListMatcher{allowed} |
| 590 | } |
| 591 | |
| Remi NGUYEN VAN | efb49af | 2021-12-10 00:17:57 +0000 | [diff] [blame] | 592 | func LessThanSdkVersion(sdk string) ValueMatcher { |
| 593 | return &sdkVersionMatcher{ |
| 594 | condition: func(ctx ValueMatcherContext, spec SdkSpec) bool { |
| 595 | return spec.ApiLevel.LessThan( |
| 596 | SdkSpecFromWithConfig(ctx.Config(), sdk).ApiLevel) |
| 597 | }, |
| 598 | description: "lessThan=" + sdk, |
| 599 | } |
| 600 | } |
| 601 | |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 602 | // assorted utils |
| 603 | |
| 604 | func cleanPaths(paths []string) []string { |
| 605 | res := make([]string, len(paths)) |
| 606 | for i, v := range paths { |
| 607 | res[i] = filepath.Clean(v) + "/" |
| 608 | } |
| 609 | return res |
| 610 | } |
| 611 | |
| 612 | func fieldNamesForProperties(propertyNames string) []string { |
| 613 | names := strings.Split(propertyNames, ".") |
| 614 | for i, v := range names { |
| 615 | names[i] = proptools.FieldNameForProperty(v) |
| 616 | } |
| 617 | return names |
| 618 | } |
| 619 | |
| Remi NGUYEN VAN | efb49af | 2021-12-10 00:17:57 +0000 | [diff] [blame] | 620 | func hasAnyProperty(ctx ValueMatcherContext, properties []interface{}, |
| 621 | props []ruleProperty) bool { |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 622 | for _, v := range props { |
| Remi NGUYEN VAN | efb49af | 2021-12-10 00:17:57 +0000 | [diff] [blame] | 623 | if hasProperty(ctx, properties, v) { |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 624 | return true |
| 625 | } |
| 626 | } |
| 627 | return false |
| 628 | } |
| 629 | |
| Remi NGUYEN VAN | efb49af | 2021-12-10 00:17:57 +0000 | [diff] [blame] | 630 | func hasAllProperties(ctx ValueMatcherContext, properties []interface{}, |
| 631 | props []ruleProperty) bool { |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 632 | for _, v := range props { |
| Remi NGUYEN VAN | efb49af | 2021-12-10 00:17:57 +0000 | [diff] [blame] | 633 | if !hasProperty(ctx, properties, v) { |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 634 | return false |
| 635 | } |
| 636 | } |
| 637 | return true |
| 638 | } |
| 639 | |
| Remi NGUYEN VAN | efb49af | 2021-12-10 00:17:57 +0000 | [diff] [blame] | 640 | func hasProperty(ctx ValueMatcherContext, properties []interface{}, |
| 641 | prop ruleProperty) bool { |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 642 | for _, propertyStruct := range properties { |
| 643 | propertiesValue := reflect.ValueOf(propertyStruct).Elem() |
| 644 | for _, v := range prop.fields { |
| 645 | if !propertiesValue.IsValid() { |
| 646 | break |
| 647 | } |
| 648 | propertiesValue = propertiesValue.FieldByName(v) |
| 649 | } |
| 650 | if !propertiesValue.IsValid() { |
| 651 | continue |
| 652 | } |
| 653 | |
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 654 | check := func(value string) bool { |
| Remi NGUYEN VAN | efb49af | 2021-12-10 00:17:57 +0000 | [diff] [blame] | 655 | return prop.matcher.Test(ctx, value) |
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | if matchValue(propertiesValue, check) { |
| 659 | return true |
| 660 | } |
| 661 | } |
| 662 | return false |
| 663 | } |
| 664 | |
| 665 | func matchValue(value reflect.Value, check func(string) bool) bool { |
| 666 | if !value.IsValid() { |
| 667 | return false |
| 668 | } |
| 669 | |
| 670 | if value.Kind() == reflect.Ptr { |
| 671 | if value.IsNil() { |
| 672 | return check("") |
| 673 | } |
| 674 | value = value.Elem() |
| 675 | } |
| 676 | |
| 677 | switch value.Kind() { |
| 678 | case reflect.String: |
| 679 | return check(value.String()) |
| 680 | case reflect.Bool: |
| 681 | return check(strconv.FormatBool(value.Bool())) |
| 682 | case reflect.Int: |
| 683 | return check(strconv.FormatInt(value.Int(), 10)) |
| 684 | case reflect.Slice: |
| 685 | slice, ok := value.Interface().([]string) |
| 686 | if !ok { |
| 687 | panic("Can only handle slice of string") |
| 688 | } |
| 689 | for _, v := range slice { |
| 690 | if check(v) { |
| 691 | return true |
| 692 | } |
| 693 | } |
| 694 | return false |
| 695 | } |
| 696 | |
| 697 | panic("Can't handle type: " + value.Kind().String()) |
| 698 | } |
| Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 699 | |
| 700 | var neverallowRulesKey = NewOnceKey("neverallowRules") |
| 701 | |
| 702 | func neverallowRules(config Config) []Rule { |
| 703 | return config.Once(neverallowRulesKey, func() interface{} { |
| 704 | // No test rules were set by setTestNeverallowRules, use the global rules |
| 705 | return neverallows |
| 706 | }).([]Rule) |
| 707 | } |
| 708 | |
| 709 | // Overrides the default neverallow rules for the supplied config. |
| 710 | // |
| 711 | // For testing only. |
| Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 712 | func setTestNeverallowRules(config Config, testRules []Rule) { |
| Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 713 | config.Once(neverallowRulesKey, func() interface{} { return testRules }) |
| 714 | } |
| Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 715 | |
| 716 | // Prepares for a test by setting neverallow rules and enabling the mutator. |
| 717 | // |
| 718 | // If the supplied rules are nil then the default rules are used. |
| 719 | func PrepareForTestWithNeverallowRules(testRules []Rule) FixturePreparer { |
| 720 | return GroupFixturePreparers( |
| 721 | FixtureModifyConfig(func(config Config) { |
| 722 | if testRules != nil { |
| 723 | setTestNeverallowRules(config, testRules) |
| 724 | } |
| 725 | }), |
| 726 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 727 | ctx.PostDepsMutators(registerNeverallowMutator) |
| 728 | }), |
| 729 | ) |
| 730 | } |