| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 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 caddytls |
| 16 | |
| 17 | import ( |
| 18 | "context" |
| 19 | "crypto/tls" |
| 20 | "net" |
| 21 | "testing" |
| 22 | |
| 23 | "github.com/caddyserver/caddy/v2" |
| 24 | ) |
| 25 | |
| 26 | func TestServerNameMatcher(t *testing.T) { |
| 27 | for i, tc := range []struct { |
| 28 | names []string |
| 29 | input string |
| 30 | expect bool |
| 31 | }{ |
| 32 | { |
| 33 | names: []string{"example.com"}, |
| 34 | input: "example.com", |
| 35 | expect: true, |
| 36 | }, |
| 37 | { |
| 38 | names: []string{"example.com"}, |
| 39 | input: "foo.com", |
| 40 | expect: false, |
| 41 | }, |
| 42 | { |
| 43 | names: []string{"example.com"}, |
| 44 | input: "", |
| 45 | expect: false, |
| 46 | }, |
| 47 | { |
| 48 | names: []string{}, |
| 49 | input: "", |
| 50 | expect: false, |
| 51 | }, |
| 52 | { |
| 53 | names: []string{"foo", "example.com"}, |
| 54 | input: "example.com", |
| 55 | expect: true, |
| 56 | }, |
| 57 | { |
| 58 | names: []string{"foo", "example.com"}, |
| 59 | input: "sub.example.com", |
| 60 | expect: false, |
| 61 | }, |
| 62 | { |
| 63 | names: []string{"foo", "example.com"}, |
| 64 | input: "foo.com", |
| 65 | expect: false, |
| 66 | }, |
| 67 | { |
| 68 | names: []string{"*.example.com"}, |
| 69 | input: "example.com", |
| 70 | expect: false, |
| 71 | }, |
| 72 | { |
| 73 | names: []string{"*.example.com"}, |
| 74 | input: "sub.example.com", |
| 75 | expect: true, |
| 76 | }, |
| 77 | { |
| 78 | names: []string{"*.example.com", "*.sub.example.com"}, |
| 79 | input: "sub2.sub.example.com", |
| 80 | expect: true, |
| 81 | }, |
| 82 | { |
| 83 | names: []string{"つ.localhost"}, |
| 84 | input: "xn--k9j.localhost", |
| 85 | expect: true, |
| 86 | }, |
| 87 | { |
| 88 | names: []string{"つ.Localhost"}, |
| 89 | input: "XN--K9J.LOCALHOST", |
| 90 | expect: true, |
| 91 | }, |
| 92 | { |
| 93 | names: []string{"*.つ.localhost"}, |
| 94 | input: "sub.xn--k9j.localhost", |
| 95 | expect: true, |
| 96 | }, |
| 97 | { |
| 98 | names: []string{"*.つ.Localhost"}, |
| 99 | input: "Sub.XN--K9J.LOCALHOST", |
| 100 | expect: true, |
| 101 | }, |
| 102 | } { |
| 103 | chi := &tls.ClientHelloInfo{ServerName: tc.input} |
| 104 | actual := MatchServerName(tc.names).Match(chi) |
| 105 | if actual != tc.expect { |
| 106 | t.Errorf("Test %d: Expected %t but got %t (input=%s match=%v)", |
| 107 | i, tc.expect, actual, tc.input, tc.names) |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func TestServerNameREMatcher(t *testing.T) { |
| 113 | for i, tc := range []struct { |
| 114 | pattern string |
| 115 | input string |
| 116 | expect bool |
| 117 | }{ |
| 118 | { |
| 119 | pattern: "^example\\.(com|net)$", |
| 120 | input: "example.com", |
| 121 | expect: true, |
| 122 | }, |
| 123 | { |
| 124 | pattern: "^example\\.(com|net)$", |
| 125 | input: "foo.com", |
| 126 | expect: false, |
| 127 | }, |
| 128 | { |
| 129 | pattern: "^example\\.(com|net)$", |
| 130 | input: "", |
| 131 | expect: false, |
| 132 | }, |
| 133 | { |
| 134 | pattern: "", |
| 135 | input: "", |
| 136 | expect: true, |
| 137 | }, |
| 138 | { |
| 139 | pattern: "^example\\.(com|net)$", |
| 140 | input: "foo.example.com", |
| 141 | expect: false, |
| 142 | }, |
| 143 | } { |
| 144 | chi := &tls.ClientHelloInfo{ServerName: tc.input} |
| 145 | mre := MatchServerNameRE{MatchRegexp{Pattern: tc.pattern}} |
| 146 | ctx, _ := caddy.NewContext(caddy.Context{Context: context.Background()}) |
| 147 | if mre.Provision(ctx) != nil { |
| 148 | t.Errorf("Test %d: Failed to provision a regexp matcher (pattern=%v)", i, tc.pattern) |
| 149 | } |
| 150 | actual := mre.Match(chi) |
| 151 | if actual != tc.expect { |
| 152 | t.Errorf("Test %d: Expected %t but got %t (input=%s match=%v)", |
| 153 | i, tc.expect, actual, tc.input, tc.pattern) |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func TestRemoteIPMatcher(t *testing.T) { |
| 159 | ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()}) |
| 160 | defer cancel() |
| 161 | |
| 162 | for i, tc := range []struct { |
| 163 | ranges []string |
| 164 | notRanges []string |
| 165 | input string |
| 166 | expect bool |
| 167 | }{ |
| 168 | { |
| 169 | ranges: []string{"127.0.0.1"}, |
| 170 | input: "127.0.0.1:12345", |
| 171 | expect: true, |
| 172 | }, |
| 173 | { |
| 174 | ranges: []string{"127.0.0.1"}, |
| 175 | input: "127.0.0.2:12345", |
| 176 | expect: false, |
| 177 | }, |
| 178 | { |
| 179 | ranges: []string{"127.0.0.1/16"}, |
| 180 | input: "127.0.1.23:12345", |
| 181 | expect: true, |
| 182 | }, |
| 183 | { |
| 184 | ranges: []string{"127.0.0.1", "192.168.1.105"}, |
| 185 | input: "192.168.1.105:12345", |
| 186 | expect: true, |
| 187 | }, |
| 188 | { |
| 189 | notRanges: []string{"127.0.0.1"}, |
| 190 | input: "127.0.0.1:12345", |
| 191 | expect: false, |
| 192 | }, |
| 193 | { |
| 194 | notRanges: []string{"127.0.0.2"}, |
| 195 | input: "127.0.0.1:12345", |
| 196 | expect: true, |
| 197 | }, |
| 198 | { |
| 199 | ranges: []string{"127.0.0.1"}, |
| 200 | notRanges: []string{"127.0.0.2"}, |
| 201 | input: "127.0.0.1:12345", |
| 202 | expect: true, |
| 203 | }, |
| 204 | { |
| 205 | ranges: []string{"127.0.0.2"}, |
| 206 | notRanges: []string{"127.0.0.2"}, |
| 207 | input: "127.0.0.2:12345", |
| 208 | expect: false, |
| 209 | }, |
| 210 | { |
| 211 | ranges: []string{"127.0.0.2"}, |
| 212 | notRanges: []string{"127.0.0.2"}, |
| 213 | input: "127.0.0.3:12345", |
| 214 | expect: false, |
| 215 | }, |
| 216 | } { |
| 217 | matcher := MatchRemoteIP{Ranges: tc.ranges, NotRanges: tc.notRanges} |
| 218 | err := matcher.Provision(ctx) |
| 219 | if err != nil { |
| 220 | t.Fatalf("Test %d: Provision failed: %v", i, err) |
| 221 | } |
| 222 | |
| 223 | addr := testAddr(tc.input) |
| 224 | chi := &tls.ClientHelloInfo{Conn: testConn{addr: addr}} |
| 225 | |
| 226 | actual := matcher.Match(chi) |
| 227 | if actual != tc.expect { |
| 228 | t.Errorf("Test %d: Expected %t but got %t (input=%s ranges=%v notRanges=%v)", |
| 229 | i, tc.expect, actual, tc.input, tc.ranges, tc.notRanges) |
| 230 | } |
| 231 | } |
| 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) |
| 292 | } |
| 293 | |
| 294 | addr := testAddr(tc.input) |
| 295 | chi := &tls.ClientHelloInfo{Conn: testConn{addr: addr}} |
| 296 | |
| 297 | actual := matcher.Match(chi) |
| 298 | if actual != tc.expect { |
| 299 | t.Errorf("Test %d: Expected %t but got %t (input=%s ranges=%v)", |
| 300 | i, tc.expect, actual, tc.input, tc.ranges) |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | type testConn struct { |
| 306 | *net.TCPConn |
| 307 | addr testAddr |
| 308 | } |
| 309 | |
| 310 | func (tc testConn) RemoteAddr() net.Addr { return tc.addr } |
| 311 | func (tc testConn) LocalAddr() net.Addr { return tc.addr } |
| 312 | |
| 313 | type testAddr string |
| 314 | |
| 315 | func (testAddr) Network() string { return "tcp" } |
| 316 | func (ta testAddr) String() string { return string(ta) } |