| 232 | } |
| 233 | |
| 234 | func TestLocalIPMatcher(t *testing.T) { |
| 235 | ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()}) |
| 236 | defer cancel() |
| 237 | |
| 238 | for i, tc := range []struct { |
| 239 | ranges []string |
| 240 | input string |
| 241 | expect bool |
| 242 | }{ |
| 243 | { |
| 244 | ranges: []string{"127.0.0.1"}, |
| 245 | input: "127.0.0.1:12345", |
| 246 | expect: true, |
| 247 | }, |
| 248 | { |
| 249 | ranges: []string{"127.0.0.1"}, |
| 250 | input: "127.0.0.2:12345", |
| 251 | expect: false, |
| 252 | }, |
| 253 | { |
| 254 | ranges: []string{"127.0.0.1/16"}, |
| 255 | input: "127.0.1.23:12345", |
| 256 | expect: true, |
| 257 | }, |
| 258 | { |
| 259 | ranges: []string{"127.0.0.1", "192.168.1.105"}, |
| 260 | input: "192.168.1.105:12345", |
| 261 | expect: true, |
| 262 | }, |
| 263 | { |
| 264 | input: "127.0.0.1:12345", |
| 265 | expect: true, |
| 266 | }, |
| 267 | { |
| 268 | ranges: []string{"127.0.0.1"}, |
| 269 | input: "127.0.0.1:12345", |
| 270 | expect: true, |
| 271 | }, |
| 272 | { |
| 273 | ranges: []string{"127.0.0.2"}, |
| 274 | input: "127.0.0.3:12345", |
| 275 | expect: false, |
| 276 | }, |
| 277 | { |
| 278 | ranges: []string{"127.0.0.2"}, |
| 279 | input: "127.0.0.2", |
| 280 | expect: true, |
| 281 | }, |
| 282 | { |
| 283 | ranges: []string{"127.0.0.2"}, |
| 284 | input: "127.0.0.300", |
| 285 | expect: false, |
| 286 | }, |
| 287 | } { |
| 288 | matcher := MatchLocalIP{Ranges: tc.ranges} |
| 289 | err := matcher.Provision(ctx) |
| 290 | if err != nil { |
| 291 | t.Fatalf("Test %d: Provision failed: %v", i, err) |