Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 10, 23:45 UTC
| 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 internal |
| 16 | |
| 17 | import ( |
| 18 | "errors" |
| 19 | "net" |
| 20 | "os" |
| 21 | "testing" |
| 22 | |
| 23 | "golang.org/x/sys/unix" |
| 24 | ) |
| 25 | |
| 26 | func TestIsPortBind(t *testing.T) { |
| 27 | for _, tc := range []struct { |
| 28 | network, address string |
| 29 | want bool |
| 30 | }{ |
| 31 | {"tcp", ":80", true}, |
| 32 | {"tcp4", "127.0.0.1:443", true}, |
| 33 | {"udp6", "[::1]:443", true}, |
| 34 | {"tcp", ":1023", true}, |
| 35 | {"tcp", ":1024", true}, |
| 36 | {"tcp", ":8080", true}, |
| 37 | {"tcp", ":65535", true}, |
| 38 | {"tcp", ":65536", false}, |
| 39 | {"tcp", ":0", false}, |
| 40 | {"unix", "/run/caddy.sock", false}, |
| 41 | {"fd", "3", false}, |
| 42 | {"ip4:icmp", "127.0.0.1", false}, |
| 43 | } { |
| 44 | if got := isPortBind(tc.network, tc.address); got != tc.want { |
| 45 | t.Errorf("isPortBind(%q, %q) = %v, want %v", tc.network, tc.address, got, tc.want) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestWithBindCapabilityPassthrough(t *testing.T) { |
| 51 | before, err := shouldRaiseBindCapability() |
| 52 | if err != nil { |
| 53 | t.Fatalf("reading capabilities: %v", err) |
| 54 | } |
| 55 | |
| 56 | // a privileged-port EACCES is retried only if the capability can be raised |
| 57 | retryCalls := 1 |
| 58 | if before { |
| 59 | retryCalls = 2 |
| 60 | } |
| 61 | |
| 62 | // shaped like the error net.ListenConfig.Listen returns |
| 63 | eacces := &net.OpError{Op: "listen", Net: "tcp", Err: os.NewSyscallError("bind", unix.EACCES)} |
| 64 | sentinel := errors.New("sentinel") |
| 65 | for _, tc := range []struct { |
| 66 | network, address string |
| 67 | err error |
| 68 | wantCalls int |
| 69 | }{ |
| 70 | {"tcp", "localhost:80", nil, 1}, |
| 71 | {"tcp", "localhost:80", sentinel, 1}, |
| 72 | {"udp", ":8080", sentinel, 1}, |
| 73 | {"unix", "/tmp/caddy.sock", eacces, 1}, |
| 74 | {"tcp", ":0", eacces, 1}, |
| 75 | {"tcp", "localhost:80", eacces, retryCalls}, |
| 76 | } { |
| 77 | calls := 0 |
| 78 | ln, err := WithBindCapability(tc.network, tc.address, func() (any, error) { |
| 79 | calls++ |
| 80 | return "listener", tc.err |
| 81 | }) |
| 82 | if calls != tc.wantCalls { |
| 83 | t.Errorf("%s/%s/%v: listen called %d times, want %d", tc.network, tc.address, tc.err, calls, tc.wantCalls) |
| 84 | } |
| 85 | if ln != "listener" || !errors.Is(err, tc.err) { |
| 86 | t.Errorf("%s/%s/%v: got (%v, %v), want listen's return values", tc.network, tc.address, tc.err, ln, err) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | after, err := shouldRaiseBindCapability() |
| 91 | if err != nil { |
| 92 | t.Fatalf("reading capabilities: %v", err) |
| 93 | } |
| 94 | if before != after { |
| 95 | t.Errorf("capabilities of calling thread changed") |
| 96 | } |
| 97 | } |