github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/pkg/auth/wildcard/match_test.go (about) 1 package wildcard_test 2 3 /* 4 * MinIO Cloud Storage, (C) 2015, 2016 MinIO, Inc. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 import ( 20 "testing" 21 22 "github.com/treeverse/lakefs/pkg/auth/wildcard" 23 ) 24 25 // TestMatch - Tests validate the logic of wild card matching. 26 // `Match` supports '*' and '?' wildcards. 27 // Sample usage: In resource matching for bucket policy validation. 28 func TestMatch(t *testing.T) { 29 tests := []struct { 30 pattern string 31 text string 32 matched bool 33 }{ 34 { 35 pattern: "*", 36 text: "s3:GetObject", 37 matched: true, 38 }, 39 { 40 pattern: "", 41 text: "s3:GetObject", 42 matched: false, 43 }, 44 { 45 pattern: "", 46 text: "", 47 matched: true, 48 }, 49 { 50 pattern: "s3:*", 51 text: "s3:ListMultipartUploadParts", 52 matched: true, 53 }, 54 { 55 pattern: "s3:ListBucketMultipartUploads", 56 text: "s3:ListBucket", 57 matched: false, 58 }, 59 { 60 pattern: "s3:ListBucket", 61 text: "s3:ListBucket", 62 matched: true, 63 }, 64 { 65 pattern: "s3:ListBucketMultipartUploads", 66 text: "s3:ListBucketMultipartUploads", 67 matched: true, 68 }, 69 { 70 pattern: "my-bucket/oo*", 71 text: "my-bucket/oo", 72 matched: true, 73 }, 74 { 75 pattern: "my-bucket/In*", 76 text: "my-bucket/India/Karnataka/", 77 matched: true, 78 }, 79 { 80 pattern: "my-bucket/In*", 81 text: "my-bucket/Karnataka/India/", 82 matched: false, 83 }, 84 { 85 pattern: "my-bucket/In*/Ka*/Ban", 86 text: "my-bucket/India/Karnataka/Ban", 87 matched: true, 88 }, 89 { 90 pattern: "my-bucket/In*/Ka*/Ban", 91 text: "my-bucket/India/Karnataka/Ban/Ban/Ban/Ban/Ban", 92 matched: true, 93 }, 94 { 95 pattern: "my-bucket/In*/Ka*/Ban", 96 text: "my-bucket/India/Karnataka/Area1/Area2/Area3/Ban", 97 matched: true, 98 }, 99 { 100 pattern: "my-bucket/In*/Ka*/Ban", 101 text: "my-bucket/India/State1/State2/Karnataka/Area1/Area2/Area3/Ban", 102 matched: true, 103 }, 104 { 105 pattern: "my-bucket/In*/Ka*/Ban", 106 text: "my-bucket/India/Karnataka/Bangalore", 107 matched: false, 108 }, 109 { 110 pattern: "my-bucket/In*/Ka*/Ban*", 111 text: "my-bucket/India/Karnataka/Bangalore", 112 matched: true, 113 }, 114 { 115 pattern: "my-bucket/*", 116 text: "my-bucket/India", 117 matched: true, 118 }, 119 { 120 pattern: "my-bucket/oo*", 121 text: "my-bucket/odo", 122 matched: false, 123 }, 124 { 125 pattern: "my-bucket?/abc*", 126 text: "mybucket/abc", 127 matched: false, 128 }, 129 { 130 pattern: "my-bucket?/abc*", 131 text: "my-bucket1/abc", 132 matched: true, 133 }, 134 { 135 pattern: "my-?-bucket/abc*", 136 text: "my--bucket/abc", 137 matched: false, 138 }, 139 { 140 pattern: "my-?-bucket/abc*", 141 text: "my-1-bucket/abc", 142 matched: true, 143 }, 144 { 145 pattern: "my-?-bucket/abc*", 146 text: "my-k-bucket/abc", 147 matched: true, 148 }, 149 { 150 pattern: "my??bucket/abc*", 151 text: "mybucket/abc", 152 matched: false, 153 }, 154 { 155 pattern: "my??bucket/abc*", 156 text: "my4abucket/abc", 157 matched: true, 158 }, 159 { 160 pattern: "my-bucket?abc*", 161 text: "my-bucket/abc", 162 matched: true, 163 }, 164 { 165 pattern: "my-bucket/abc?efg", 166 text: "my-bucket/abcdefg", 167 matched: true, 168 }, 169 { 170 pattern: "my-bucket/abc?efg", 171 text: "my-bucket/abc/efg", 172 matched: true, 173 }, 174 { 175 pattern: "my-bucket/abc????", 176 text: "my-bucket/abc", 177 matched: false, 178 }, 179 { 180 pattern: "my-bucket/abc????", 181 text: "my-bucket/abcde", 182 matched: false, 183 }, 184 { 185 pattern: "my-bucket/abc????", 186 text: "my-bucket/abcdefg", 187 matched: true, 188 }, 189 { 190 pattern: "my-bucket/abc?", 191 text: "my-bucket/abc", 192 matched: false, 193 }, 194 { 195 pattern: "my-bucket/abc?", 196 text: "my-bucket/abcd", 197 matched: true, 198 }, 199 { 200 pattern: "my-bucket/abc?", 201 text: "my-bucket/abcde", 202 matched: false, 203 }, 204 { 205 pattern: "my-bucket/mnop*?", 206 text: "my-bucket/mnop", 207 matched: false, 208 }, 209 { 210 pattern: "my-bucket/mnop*?", 211 text: "my-bucket/mnopqrst/mnopqr", 212 matched: true, 213 }, 214 { 215 pattern: "my-bucket/mnop*?", 216 text: "my-bucket/mnopqrst/mnopqrs", 217 matched: true, 218 }, 219 { 220 pattern: "my-bucket/mnop*?", 221 text: "my-bucket/mnop", 222 matched: false, 223 }, 224 { 225 pattern: "my-bucket/mnop*?", 226 text: "my-bucket/mnopq", 227 matched: true, 228 }, 229 { 230 pattern: "my-bucket/mnop*?", 231 text: "my-bucket/mnopqr", 232 matched: true, 233 }, 234 { 235 pattern: "my-bucket/mnop*?and", 236 text: "my-bucket/mnopqand", 237 matched: true, 238 }, 239 { 240 pattern: "my-bucket/mnop*?and", 241 text: "my-bucket/mnopand", 242 matched: false, 243 }, 244 { 245 pattern: "my-bucket/mnop*?and", 246 text: "my-bucket/mnopqand", 247 matched: true, 248 }, 249 { 250 pattern: "my-bucket/mnop*?", 251 text: "my-bucket/mn", 252 matched: false, 253 }, 254 { 255 pattern: "my-bucket/mnop*?", 256 text: "my-bucket/mnopqrst/mnopqrs", 257 matched: true, 258 }, 259 { 260 pattern: "my-bucket/mnop*??", 261 text: "my-bucket/mnopqrst", 262 matched: true, 263 }, 264 { 265 pattern: "my-bucket/mnop*qrst", 266 text: "my-bucket/mnopabcdegqrst", 267 matched: true, 268 }, 269 { 270 pattern: "my-bucket/mnop*?and", 271 text: "my-bucket/mnopqand", 272 matched: true, 273 }, 274 { 275 pattern: "my-bucket/mnop*?and", 276 text: "my-bucket/mnopand", 277 matched: false, 278 }, 279 { 280 pattern: "my-bucket/mnop*?and?", 281 text: "my-bucket/mnopqanda", 282 matched: true, 283 }, 284 { 285 pattern: "my-bucket/mnop*?and", 286 text: "my-bucket/mnopqanda", 287 matched: false, 288 }, 289 { 290 pattern: "my-?-bucket/abc*", 291 text: "my-bucket/mnopqanda", 292 matched: false, 293 }, 294 } 295 for i, tt := range tests { 296 actualResult := wildcard.Match(tt.pattern, tt.text) 297 if tt.matched != actualResult { 298 t.Errorf("Match('%s', '%s') [%d] expected=%t, got=%t", 299 tt.pattern, tt.text, i+1, tt.matched, actualResult) 300 } 301 } 302 }