Debian Codegraph @

listeners_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 caddy
16
17 import (
18 "crypto/tls"
19 "reflect"
20 "testing"
21
22 "github.com/caddyserver/caddy/v2/internal"
23 )
24
25 func TestSplitNetworkAddress(t *testing.T) {
26 for i, tc := range []struct {
27 input string
28 expectNetwork string
29 expectHost string
30 expectPort string
31 expectErr bool
32 }{
33 {
34 input: "",
35 expectHost: "",
36 },
37 {
38 input: "foo",
39 expectHost: "foo",
40 },
41 {
42 input: ":", // empty host & empty port
43 },
44 {
45 input: "::",
46 expectHost: "::",
47 },
48 {
49 input: "[::]",
50 expectHost: "::",
51 },
52 {
53 input: ":1234",
54 expectPort: "1234",
55 },
56 {
57 input: "foo:1234",
58 expectHost: "foo",
59 expectPort: "1234",
60 },
61 {
62 input: "foo:1234-5678",
63 expectHost: "foo",
64 expectPort: "1234-5678",
65 },
66 {
67 input: "udp/foo:1234",
68 expectNetwork: "udp",
69 expectHost: "foo",
70 expectPort: "1234",
71 },
72 {
73 input: "tcp6/foo:1234-5678",
74 expectNetwork: "tcp6",
75 expectHost: "foo",
76 expectPort: "1234-5678",
77 },
78 {
79 input: "udp/",
80 expectNetwork: "udp",
81 expectHost: "",
82 },
83 {
84 input: "unix//foo/bar",
85 expectNetwork: "unix",
86 expectHost: "/foo/bar",
87 },
88 {
89 input: "unixgram//foo/bar",
90 expectNetwork: "unixgram",
91 expectHost: "/foo/bar",
92 },
93 {
94 input: "unixpacket//foo/bar",
95 expectNetwork: "unixpacket",
96 expectHost: "/foo/bar",
97 },
98 } {
99 actualNetwork, actualHost, actualPort, err := SplitNetworkAddress(tc.input)
100 if tc.expectErr && err == nil {
101 t.Errorf("Test %d: Expected error but got %v", i, err)
102 }
103 if !tc.expectErr && err != nil {
104 t.Errorf("Test %d: Expected no error but got %v", i, err)
105 }
106 if actualNetwork != tc.expectNetwork {
107 t.Errorf("Test %d: Expected network '%s' but got '%s'", i, tc.expectNetwork, actualNetwork)
108 }
109 if actualHost != tc.expectHost {
110 t.Errorf("Test %d: Expected host '%s' but got '%s'", i, tc.expectHost, actualHost)
111 }
112 if actualPort != tc.expectPort {
113 t.Errorf("Test %d: Expected port '%s' but got '%s'", i, tc.expectPort, actualPort)
114 }
115 }
116 }
117
118 func TestJoinNetworkAddress(t *testing.T) {
119 for i, tc := range []struct {
120 network, host, port string
121 expect string
122 }{
123 {
124 network: "", host: "", port: "",
125 expect: "",
126 },
127 {
128 network: "tcp", host: "", port: "",
129 expect: "tcp/",
130 },
131 {
132 network: "", host: "foo", port: "",
133 expect: "foo",
134 },
135 {
136 network: "", host: "", port: "1234",
137 expect: ":1234",
138 },
139 {
140 network: "", host: "", port: "1234-5678",
141 expect: ":1234-5678",
142 },
143 {
144 network: "", host: "foo", port: "1234",
145 expect: "foo:1234",
146 },
147 {
148 network: "udp", host: "foo", port: "1234",
149 expect: "udp/foo:1234",
150 },
151 {
152 network: "udp", host: "", port: "1234",
153 expect: "udp/:1234",
154 },
155 {
156 network: "unix", host: "/foo/bar", port: "",
157 expect: "unix//foo/bar",
158 },
159 {
160 network: "unix", host: "/foo/bar", port: "0",
161 expect: "unix//foo/bar",
162 },
163 {
164 network: "unix", host: "/foo/bar", port: "1234",
165 expect: "unix//foo/bar",
166 },
167 {
168 network: "", host: "::1", port: "1234",
169 expect: "[::1]:1234",
170 },
171 } {
172 actual := JoinNetworkAddress(tc.network, tc.host, tc.port)
173 if actual != tc.expect {
174 t.Errorf("Test %d: Expected '%s' but got '%s'", i, tc.expect, actual)
175 }
176 }
177 }
178
179 func TestSharedQUICStateGetEncryptedClientHelloKeys(t *testing.T) {
180 hello := &tls.ClientHelloInfo{ServerName: "example.com"}
181 initialKeys := []tls.EncryptedClientHelloKey{{Config: []byte("initial"), PrivateKey: []byte("initial-key")}}
182 updatedKeys := []tls.EncryptedClientHelloKey{{Config: []byte("updated"), PrivateKey: []byte("updated-key")}}
183
184 initialConfig := &tls.Config{
185 GetConfigForClient: func(*tls.ClientHelloInfo) (*tls.Config, error) {
186 return nil, nil
187 },
188 GetEncryptedClientHelloKeys: func(*tls.ClientHelloInfo) ([]tls.EncryptedClientHelloKey, error) {
189 return initialKeys, nil
190 },
191 }
192
193 sqs := newSharedQUICState(initialConfig)
194
195 keys, err := sqs.getEncryptedClientHelloKeys(hello)
196 if err != nil {
197 t.Fatalf("getting initial ECH keys: %v", err)
198 }
199 if !reflect.DeepEqual(keys, initialKeys) {
200 t.Fatalf("unexpected initial ECH keys: got %#v, want %#v", keys, initialKeys)
201 }
202
203 updatedConfig := &tls.Config{
204 GetConfigForClient: func(*tls.ClientHelloInfo) (*tls.Config, error) {
205 return nil, nil
206 },
207 GetEncryptedClientHelloKeys: func(*tls.ClientHelloInfo) ([]tls.EncryptedClientHelloKey, error) {
208 return updatedKeys, nil
209 },
210 }
211
212 _, cancel := sqs.addState(updatedConfig)
213 sqs.rmu.Lock()
214 sqs.activeTlsConf = updatedConfig
215 sqs.rmu.Unlock()
216
217 keys, err = sqs.getEncryptedClientHelloKeys(hello)
218 if err != nil {
219 t.Fatalf("getting updated ECH keys: %v", err)
220 }
221 if !reflect.DeepEqual(keys, updatedKeys) {
222 t.Fatalf("unexpected updated ECH keys: got %#v, want %#v", keys, updatedKeys)
223 }
224
225 cancel(nil)
226
227 keys, err = sqs.getEncryptedClientHelloKeys(hello)
228 if err != nil {
229 t.Fatalf("getting restored ECH keys: %v", err)
230 }
231 if !reflect.DeepEqual(keys, initialKeys) {
232 t.Fatalf("unexpected restored ECH keys: got %#v, want %#v", keys, initialKeys)
233 }
234 }
235
236 func TestParseNetworkAddress(t *testing.T) {
237 for i, tc := range []struct {
238 input string
239 defaultNetwork string
240 defaultPort uint
241 expectAddr NetworkAddress
242 expectErr bool
243 }{
244 {
245 input: "",
246 expectAddr: NetworkAddress{},
247 },
248 {
249 input: ":",
250 defaultNetwork: "udp",
251 expectAddr: NetworkAddress{
252 Network: "udp",
253 },
254 },
255 {
256 input: "[::]",
257 defaultNetwork: "udp",
258 defaultPort: 53,
259 expectAddr: NetworkAddress{
260 Network: "udp",
261 Host: "::",
262 StartPort: 53,
263 EndPort: 53,
264 },
265 },
266 {
267 input: ":1234",
268 defaultNetwork: "udp",
269 expectAddr: NetworkAddress{
270 Network: "udp",
271 Host: "",
272 StartPort: 1234,
273 EndPort: 1234,
274 },
275 },
276 {
277 input: "udp/:1234",
278 defaultNetwork: "udp",
279 expectAddr: NetworkAddress{
280 Network: "udp",
281 Host: "",
282 StartPort: 1234,
283 EndPort: 1234,
284 },
285 },
286 {
287 input: "tcp6/:1234",
288 defaultNetwork: "tcp",
289 expectAddr: NetworkAddress{
290 Network: "tcp6",
291 Host: "",
292 StartPort: 1234,
293 EndPort: 1234,
294 },
295 },
296 {
297 input: "tcp4/localhost:1234",
298 defaultNetwork: "tcp",
299 expectAddr: NetworkAddress{
300 Network: "tcp4",
301 Host: "localhost",
302 StartPort: 1234,
303 EndPort: 1234,
304 },
305 },
306 {
307 input: "unix//foo/bar",
308 defaultNetwork: "tcp",
309 expectAddr: NetworkAddress{
310 Network: "unix",
311 Host: "/foo/bar",
312 },
313 },
314 {
315 input: "localhost:1234-1234",
316 defaultNetwork: "tcp",
317 expectAddr: NetworkAddress{
318 Network: "tcp",
319 Host: "localhost",
320 StartPort: 1234,
321 EndPort: 1234,
322 },
323 },
324 {
325 input: "localhost:2-1",
326 defaultNetwork: "tcp",
327 expectErr: true,
328 },
329 {
330 input: "localhost:0",
331 defaultNetwork: "tcp",
332 expectAddr: NetworkAddress{
333 Network: "tcp",
334 Host: "localhost",
335 StartPort: 0,
336 EndPort: 0,
337 },
338 },
339 {
340 input: "localhost:1-999999999999",
341 defaultNetwork: "tcp",
342 expectErr: true,
343 },
344 } {
345 actualAddr, err := ParseNetworkAddressWithDefaults(tc.input, tc.defaultNetwork, tc.defaultPort)
346 if tc.expectErr && err == nil {
347 t.Errorf("Test %d: Expected error but got: %v", i, err)
348 }
349 if !tc.expectErr && err != nil {
350 t.Errorf("Test %d: Expected no error but got: %v", i, err)
351 }
352
353 if actualAddr.Network != tc.expectAddr.Network {
354 t.Errorf("Test %d: Expected network '%v' but got '%v'", i, tc.expectAddr, actualAddr)
355 }
356 if !reflect.DeepEqual(tc.expectAddr, actualAddr) {
357 t.Errorf("Test %d: Expected addresses %v but got %v", i, tc.expectAddr, actualAddr)
358 }
359 }
360 }
361
362 func TestParseNetworkAddressWithDefaults(t *testing.T) {
363 for i, tc := range []struct {
364 input string
365 defaultNetwork string
366 defaultPort uint
367 expectAddr NetworkAddress
368 expectErr bool
369 }{
370 {
371 input: "",
372 expectAddr: NetworkAddress{},
373 },
374 {
375 input: ":",
376 defaultNetwork: "udp",
377 expectAddr: NetworkAddress{
378 Network: "udp",
379 },
380 },
381 {
382 input: "[::]",
383 defaultNetwork: "udp",
384 defaultPort: 53,
385 expectAddr: NetworkAddress{
386 Network: "udp",
387 Host: "::",
388 StartPort: 53,
389 EndPort: 53,
390 },
391 },
392 {
393 input: ":1234",
394 defaultNetwork: "udp",
395 expectAddr: NetworkAddress{
396 Network: "udp",
397 Host: "",
398 StartPort: 1234,
399 EndPort: 1234,
400 },
401 },
402 {
403 input: "udp/:1234",
404 defaultNetwork: "udp",
405 expectAddr: NetworkAddress{
406 Network: "udp",
407 Host: "",
408 StartPort: 1234,
409 EndPort: 1234,
410 },
411 },
412 {
413 input: "tcp6/:1234",
414 defaultNetwork: "tcp",
415 expectAddr: NetworkAddress{
416 Network: "tcp6",
417 Host: "",
418 StartPort: 1234,
419 EndPort: 1234,
420 },
421 },
422 {
423 input: "tcp4/localhost:1234",
424 defaultNetwork: "tcp",
425 expectAddr: NetworkAddress{
426 Network: "tcp4",
427 Host: "localhost",
428 StartPort: 1234,
429 EndPort: 1234,
430 },
431 },
432 {
433 input: "unix//foo/bar",
434 defaultNetwork: "tcp",
435 expectAddr: NetworkAddress{
436 Network: "unix",
437 Host: "/foo/bar",
438 },
439 },
440 {
441 input: "localhost:1234-1234",
442 defaultNetwork: "tcp",
443 expectAddr: NetworkAddress{
444 Network: "tcp",
445 Host: "localhost",
446 StartPort: 1234,
447 EndPort: 1234,
448 },
449 },
450 {
451 input: "localhost:2-1",
452 defaultNetwork: "tcp",
453 expectErr: true,
454 },
455 {
456 input: "localhost:0",
457 defaultNetwork: "tcp",
458 expectAddr: NetworkAddress{
459 Network: "tcp",
460 Host: "localhost",
461 StartPort: 0,
462 EndPort: 0,
463 },
464 },
465 {
466 input: "localhost:1-999999999999",
467 defaultNetwork: "tcp",
468 expectErr: true,
469 },
470 } {
471 actualAddr, err := ParseNetworkAddressWithDefaults(tc.input, tc.defaultNetwork, tc.defaultPort)
472 if tc.expectErr && err == nil {
473 t.Errorf("Test %d: Expected error but got: %v", i, err)
474 }
475 if !tc.expectErr && err != nil {
476 t.Errorf("Test %d: Expected no error but got: %v", i, err)
477 }
478
479 if actualAddr.Network != tc.expectAddr.Network {
480 t.Errorf("Test %d: Expected network '%v' but got '%v'", i, tc.expectAddr, actualAddr)
481 }
482 if !reflect.DeepEqual(tc.expectAddr, actualAddr) {
483 t.Errorf("Test %d: Expected addresses %v but got %v", i, tc.expectAddr, actualAddr)
484 }
485 }
486 }
487
488 func TestJoinHostPort(t *testing.T) {
489 for i, tc := range []struct {
490 pa NetworkAddress
491 offset uint
492 expect string
493 }{
494 {
495 pa: NetworkAddress{
496 Network: "tcp",
497 Host: "localhost",
498 StartPort: 1234,
499 EndPort: 1234,
500 },
501 expect: "localhost:1234",
502 },
503 {
504 pa: NetworkAddress{
505 Network: "tcp",
506 Host: "localhost",
507 StartPort: 1234,
508 EndPort: 1235,
509 },
510 expect: "localhost:1234",
511 },
512 {
513 pa: NetworkAddress{
514 Network: "tcp",
515 Host: "localhost",
516 StartPort: 1234,
517 EndPort: 1235,
518 },
519 offset: 1,
520 expect: "localhost:1235",
521 },
522 {
523 pa: NetworkAddress{
524 Network: "unix",
525 Host: "/run/php/php7.3-fpm.sock",
526 },
527 expect: "/run/php/php7.3-fpm.sock",
528 },
529 } {
530 actual := tc.pa.JoinHostPort(tc.offset)
531 if actual != tc.expect {
532 t.Errorf("Test %d: Expected '%s' but got '%s'", i, tc.expect, actual)
533 }
534 }
535 }
536
537 func TestExpand(t *testing.T) {
538 for i, tc := range []struct {
539 input NetworkAddress
540 expect []NetworkAddress
541 }{
542 {
543 input: NetworkAddress{
544 Network: "tcp",
545 Host: "localhost",
546 StartPort: 2000,
547 EndPort: 2000,
548 },
549 expect: []NetworkAddress{
550 {
551 Network: "tcp",
552 Host: "localhost",
553 StartPort: 2000,
554 EndPort: 2000,
555 },
556 },
557 },
558 {
559 input: NetworkAddress{
560 Network: "tcp",
561 Host: "localhost",
562 StartPort: 2000,
563 EndPort: 2002,
564 },
565 expect: []NetworkAddress{
566 {
567 Network: "tcp",
568 Host: "localhost",
569 StartPort: 2000,
570 EndPort: 2000,
571 },
572 {
573 Network: "tcp",
574 Host: "localhost",
575 StartPort: 2001,
576 EndPort: 2001,
577 },
578 {
579 Network: "tcp",
580 Host: "localhost",
581 StartPort: 2002,
582 EndPort: 2002,
583 },
584 },
585 },
586 {
587 input: NetworkAddress{
588 Network: "tcp",
589 Host: "localhost",
590 StartPort: 2000,
591 EndPort: 1999,
592 },
593 expect: []NetworkAddress{},
594 },
595 {
596 input: NetworkAddress{
597 Network: "unix",
598 Host: "/foo/bar",
599 StartPort: 0,
600 EndPort: 0,
601 },
602 expect: []NetworkAddress{
603 {
604 Network: "unix",
605 Host: "/foo/bar",
606 StartPort: 0,
607 EndPort: 0,
608 },
609 },
610 },
611 } {
612 actual := tc.input.Expand()
613 if !reflect.DeepEqual(actual, tc.expect) {
614 t.Errorf("Test %d: Expected %+v but got %+v", i, tc.expect, actual)
615 }
616 }
617 }
618
619 func TestSplitUnixSocketPermissionsBits(t *testing.T) {
620 for i, tc := range []struct {
621 input string
622 expectNetwork string
623 expectPath string
624 expectFileMode string
625 expectErr bool
626 }{
627 {
628 input: "./foo.socket",
629 expectPath: "./foo.socket",
630 expectFileMode: "--w-------",
631 },
632 {
633 input: `.\relative\path.socket`,
634 expectPath: `.\relative\path.socket`,
635 expectFileMode: "--w-------",
636 },
637 {
638 // literal colon in resulting address
639 // and defaulting to 0200 bits
640 input: "./foo.socket:0666",
641 expectPath: "./foo.socket:0666",
642 expectFileMode: "--w-------",
643 },
644 {
645 input: "./foo.socket|0220",
646 expectPath: "./foo.socket",
647 expectFileMode: "--w--w----",
648 },
649 {
650 input: "/var/run/foo|222",
651 expectPath: "/var/run/foo",
652 expectFileMode: "--w--w--w-",
653 },
654 {
655 input: "./foo.socket|0660",
656 expectPath: "./foo.socket",
657 expectFileMode: "-rw-rw----",
658 },
659 {
660 input: "./foo.socket|0666",
661 expectPath: "./foo.socket",
662 expectFileMode: "-rw-rw-rw-",
663 },
664 {
665 input: "/var/run/foo|666",
666 expectPath: "/var/run/foo",
667 expectFileMode: "-rw-rw-rw-",
668 },
669 {
670 input: `c:\absolute\path.socket|220`,
671 expectPath: `c:\absolute\path.socket`,
672 expectFileMode: "--w--w----",
673 },
674 {
675 // symbolic permission representation is not supported for now
676 input: "./foo.socket|u=rw,g=rw,o=rw",
677 expectErr: true,
678 },
679 {
680 // octal (base-8) permission representation has to be between
681 // `0` for no read, no write, no exec (`---`) and
682 // `7` for read (4), write (2), exec (1) (`rwx` => `4+2+1 = 7`)
683 input: "./foo.socket|888",
684 expectErr: true,
685 },
686 {
687 // too many colons in address
688 input: "./foo.socket|123456|0660",
689 expectErr: true,
690 },
691 {
692 // owner is missing write perms
693 input: "./foo.socket|0522",
694 expectErr: true,
695 },
696 } {
697 actualPath, actualFileMode, err := internal.SplitUnixSocketPermissionsBits(tc.input)
698 if tc.expectErr && err == nil {
699 t.Errorf("Test %d: Expected error but got: %v", i, err)
700 }
701 if !tc.expectErr && err != nil {
702 t.Errorf("Test %d: Expected no error but got: %v", i, err)
703 }
704 if actualPath != tc.expectPath {
705 t.Errorf("Test %d: Expected path '%s' but got '%s'", i, tc.expectPath, actualPath)
706 }
707 // fileMode.Perm().String() parses 0 to "----------"
708 if !tc.expectErr && actualFileMode.Perm().String() != tc.expectFileMode {
709 t.Errorf("Test %d: Expected perms '%s' but got '%s'", i, tc.expectFileMode, actualFileMode.Perm().String())
710 }
711 }
712 }