Debian Codegraph @

modules/caddytls/matchers_test.go

raw symbols SCIP
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 caddytlsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/
16
17 import (
18 "contextscip-go gomod github.com/golang/go/src go1.25.1 context/"
19 "crypto/tlsscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/"
20 "netscip-go gomod github.com/golang/go/src go1.25.1 net/"
21 "testingscip-go gomod github.com/golang/go/src go1.25.1 testing/"
22
23 "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/"
24 )
25
26 func TestServerNameMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/TestServerNameMatcher().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 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 := &tlsscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/.ClientHelloInfoscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/ClientHelloInfo#{ServerNamescip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/ClientHelloInfo#ServerName.: tc.input}
104 actual := MatchServerNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/MatchServerName#(tc.names).Matchscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/MatchServerName#Match().(chi)
105 if actual != tc.expect {
106 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#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 TestServerNameREMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/TestServerNameREMatcher().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 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 := &tlsscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/.ClientHelloInfoscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/ClientHelloInfo#{ServerNamescip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/ClientHelloInfo#ServerName.: tc.input}
145 mre := MatchServerNameREscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/MatchServerNameRE#{MatchRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/MatchRegexp#{Patternscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/MatchRegexp#Pattern.: tc.pattern}}
146 ctx, _ := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.NewContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/NewContext().(caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#{Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#Context.: contextscip-go gomod github.com/golang/go/src go1.25.1 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.1 context/Background().()})
147 if mre.Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/MatchRegexp#Provision().(ctx) != nil {
148 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Failed to provision a regexp matcher (pattern=%v)", i, tc.pattern)
149 }
150 actual := mre.Matchscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/MatchServerNameRE#Match().(chi)
151 if actual != tc.expect {
152 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#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 TestRemoteIPMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/TestRemoteIPMatcher().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) {
159 ctx, cancel := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.NewContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/NewContext().(caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#{Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#Context.: contextscip-go gomod github.com/golang/go/src go1.25.1 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.1 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 := MatchRemoteIPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/MatchRemoteIP#{Rangesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/MatchRemoteIP#Ranges.: tc.ranges, NotRangesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/MatchRemoteIP#NotRanges.: tc.notRanges}
218 err := matcher.Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/MatchRemoteIP#Provision().(ctx)
219 if err != nil {
220 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Test %d: Provision failed: %v", i, err)
221 }
222
223 addr := testAddrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testAddr#(tc.input)
224 chi := &tlsscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/.ClientHelloInfoscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/ClientHelloInfo#{Connscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/ClientHelloInfo#Conn.: testConnscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testConn#{addrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testConn#addr.: addr}}
225
226 actual := matcher.Matchscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/MatchRemoteIP#Match().(chi)
227 if actual != tc.expect {
228 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#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 TestLocalIPMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/TestLocalIPMatcher().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) {
235 ctx, cancel := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.NewContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/NewContext().(caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#{Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#Context.: contextscip-go gomod github.com/golang/go/src go1.25.1 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.1 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 := MatchLocalIPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/MatchLocalIP#{Rangesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/MatchLocalIP#Ranges.: tc.ranges}
289 err := matcher.Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/MatchLocalIP#Provision().(ctx)
290 if err != nil {
291 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Test %d: Provision failed: %v", i, err)
292 }
293
294 addr := testAddrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testAddr#(tc.input)
295 chi := &tlsscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/.ClientHelloInfoscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/ClientHelloInfo#{Connscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/ClientHelloInfo#Conn.: testConnscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testConn#{addrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testConn#addr.: addr}}
296
297 actual := matcher.Matchscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/MatchLocalIP#Match().(chi)
298 if actual != tc.expect {
299 t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#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 testConnscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testConn# struct {
306 *netscip-go gomod github.com/golang/go/src go1.25.1 net/.TCPConnscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testConn#TCPConn.
307 addrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testConn#addr. testAddrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testAddr#
308 }
309
310 func (tc testConnscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testConn#) RemoteAddrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testConn#RemoteAddr().() netscip-go gomod github.com/golang/go/src go1.25.1 net/.Addrscip-go gomod github.com/golang/go/src go1.25.1 net/Addr# { return tc.addrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testConn#addr. }
311 func (tc testConnscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testConn#) LocalAddrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testConn#LocalAddr().() netscip-go gomod github.com/golang/go/src go1.25.1 net/.Addrscip-go gomod github.com/golang/go/src go1.25.1 net/Addr# { return tc.addrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testConn#addr. }
312
313 type testAddrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testAddr# string
314
315 func (testAddrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testAddr#) Networkscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testAddr#Network().() string { return "tcp" }
316 func (ta testAddrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testAddr#) Stringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddytls`/testAddr#String().() string { return string(ta) }