summaryrefslogtreecommitdiff
path: root/listeners_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'listeners_test.go')
-rw-r--r--listeners_test.go281
1 files changed, 260 insertions, 21 deletions
diff --git a/listeners_test.go b/listeners_test.go
index c5aa527..f8a13ca 100644
--- a/listeners_test.go
+++ b/listeners_test.go
@@ -17,6 +17,8 @@ package caddy
import (
"reflect"
"testing"
+
+ "github.com/caddyserver/caddy/v2/internal"
)
func TestSplitNetworkAddress(t *testing.T) {
@@ -175,47 +177,57 @@ func TestJoinNetworkAddress(t *testing.T) {
func TestParseNetworkAddress(t *testing.T) {
for i, tc := range []struct {
- input string
- expectAddr NetworkAddress
- expectErr bool
+ input string
+ defaultNetwork string
+ defaultPort uint
+ expectAddr NetworkAddress
+ expectErr bool
}{
{
input: "",
expectErr: true,
},
{
- input: ":",
+ input: ":",
+ defaultNetwork: "udp",
expectAddr: NetworkAddress{
- Network: "tcp",
+ Network: "udp",
},
},
{
- input: "[::]",
+ input: "[::]",
+ defaultNetwork: "udp",
+ defaultPort: 53,
expectAddr: NetworkAddress{
- Network: "tcp",
- Host: "::",
+ Network: "udp",
+ Host: "::",
+ StartPort: 53,
+ EndPort: 53,
},
},
{
- input: ":1234",
+ input: ":1234",
+ defaultNetwork: "udp",
expectAddr: NetworkAddress{
- Network: "tcp",
+ Network: "udp",
Host: "",
StartPort: 1234,
EndPort: 1234,
},
},
{
- input: "tcp/:1234",
+ input: "udp/:1234",
+ defaultNetwork: "udp",
expectAddr: NetworkAddress{
- Network: "tcp",
+ Network: "udp",
Host: "",
StartPort: 1234,
EndPort: 1234,
},
},
{
- input: "tcp6/:1234",
+ input: "tcp6/:1234",
+ defaultNetwork: "tcp",
expectAddr: NetworkAddress{
Network: "tcp6",
Host: "",
@@ -224,7 +236,8 @@ func TestParseNetworkAddress(t *testing.T) {
},
},
{
- input: "tcp4/localhost:1234",
+ input: "tcp4/localhost:1234",
+ defaultNetwork: "tcp",
expectAddr: NetworkAddress{
Network: "tcp4",
Host: "localhost",
@@ -233,14 +246,16 @@ func TestParseNetworkAddress(t *testing.T) {
},
},
{
- input: "unix//foo/bar",
+ input: "unix//foo/bar",
+ defaultNetwork: "tcp",
expectAddr: NetworkAddress{
Network: "unix",
Host: "/foo/bar",
},
},
{
- input: "localhost:1234-1234",
+ input: "localhost:1234-1234",
+ defaultNetwork: "tcp",
expectAddr: NetworkAddress{
Network: "tcp",
Host: "localhost",
@@ -249,11 +264,13 @@ func TestParseNetworkAddress(t *testing.T) {
},
},
{
- input: "localhost:2-1",
- expectErr: true,
+ input: "localhost:2-1",
+ defaultNetwork: "tcp",
+ expectErr: true,
},
{
- input: "localhost:0",
+ input: "localhost:0",
+ defaultNetwork: "tcp",
expectAddr: NetworkAddress{
Network: "tcp",
Host: "localhost",
@@ -262,11 +279,138 @@ func TestParseNetworkAddress(t *testing.T) {
},
},
{
- input: "localhost:1-999999999999",
+ input: "localhost:1-999999999999",
+ defaultNetwork: "tcp",
+ expectErr: true,
+ },
+ } {
+ actualAddr, err := ParseNetworkAddressWithDefaults(tc.input, tc.defaultNetwork, tc.defaultPort)
+ if tc.expectErr && err == nil {
+ t.Errorf("Test %d: Expected error but got: %v", i, err)
+ }
+ if !tc.expectErr && err != nil {
+ t.Errorf("Test %d: Expected no error but got: %v", i, err)
+ }
+
+ if actualAddr.Network != tc.expectAddr.Network {
+ t.Errorf("Test %d: Expected network '%v' but got '%v'", i, tc.expectAddr, actualAddr)
+ }
+ if !reflect.DeepEqual(tc.expectAddr, actualAddr) {
+ t.Errorf("Test %d: Expected addresses %v but got %v", i, tc.expectAddr, actualAddr)
+ }
+ }
+}
+
+func TestParseNetworkAddressWithDefaults(t *testing.T) {
+ for i, tc := range []struct {
+ input string
+ defaultNetwork string
+ defaultPort uint
+ expectAddr NetworkAddress
+ expectErr bool
+ }{
+ {
+ input: "",
expectErr: true,
},
+ {
+ input: ":",
+ defaultNetwork: "udp",
+ expectAddr: NetworkAddress{
+ Network: "udp",
+ },
+ },
+ {
+ input: "[::]",
+ defaultNetwork: "udp",
+ defaultPort: 53,
+ expectAddr: NetworkAddress{
+ Network: "udp",
+ Host: "::",
+ StartPort: 53,
+ EndPort: 53,
+ },
+ },
+ {
+ input: ":1234",
+ defaultNetwork: "udp",
+ expectAddr: NetworkAddress{
+ Network: "udp",
+ Host: "",
+ StartPort: 1234,
+ EndPort: 1234,
+ },
+ },
+ {
+ input: "udp/:1234",
+ defaultNetwork: "udp",
+ expectAddr: NetworkAddress{
+ Network: "udp",
+ Host: "",
+ StartPort: 1234,
+ EndPort: 1234,
+ },
+ },
+ {
+ input: "tcp6/:1234",
+ defaultNetwork: "tcp",
+ expectAddr: NetworkAddress{
+ Network: "tcp6",
+ Host: "",
+ StartPort: 1234,
+ EndPort: 1234,
+ },
+ },
+ {
+ input: "tcp4/localhost:1234",
+ defaultNetwork: "tcp",
+ expectAddr: NetworkAddress{
+ Network: "tcp4",
+ Host: "localhost",
+ StartPort: 1234,
+ EndPort: 1234,
+ },
+ },
+ {
+ input: "unix//foo/bar",
+ defaultNetwork: "tcp",
+ expectAddr: NetworkAddress{
+ Network: "unix",
+ Host: "/foo/bar",
+ },
+ },
+ {
+ input: "localhost:1234-1234",
+ defaultNetwork: "tcp",
+ expectAddr: NetworkAddress{
+ Network: "tcp",
+ Host: "localhost",
+ StartPort: 1234,
+ EndPort: 1234,
+ },
+ },
+ {
+ input: "localhost:2-1",
+ defaultNetwork: "tcp",
+ expectErr: true,
+ },
+ {
+ input: "localhost:0",
+ defaultNetwork: "tcp",
+ expectAddr: NetworkAddress{
+ Network: "tcp",
+ Host: "localhost",
+ StartPort: 0,
+ EndPort: 0,
+ },
+ },
+ {
+ input: "localhost:1-999999999999",
+ defaultNetwork: "tcp",
+ expectErr: true,
+ },
} {
- actualAddr, err := ParseNetworkAddress(tc.input)
+ actualAddr, err := ParseNetworkAddressWithDefaults(tc.input, tc.defaultNetwork, tc.defaultPort)
if tc.expectErr && err == nil {
t.Errorf("Test %d: Expected error but got: %v", i, err)
}
@@ -413,3 +557,98 @@ func TestExpand(t *testing.T) {
}
}
}
+
+func TestSplitUnixSocketPermissionsBits(t *testing.T) {
+ for i, tc := range []struct {
+ input string
+ expectNetwork string
+ expectPath string
+ expectFileMode string
+ expectErr bool
+ }{
+ {
+ input: "./foo.socket",
+ expectPath: "./foo.socket",
+ expectFileMode: "--w-------",
+ },
+ {
+ input: `.\relative\path.socket`,
+ expectPath: `.\relative\path.socket`,
+ expectFileMode: "--w-------",
+ },
+ {
+ // literal colon in resulting address
+ // and defaulting to 0200 bits
+ input: "./foo.socket:0666",
+ expectPath: "./foo.socket:0666",
+ expectFileMode: "--w-------",
+ },
+ {
+ input: "./foo.socket|0220",
+ expectPath: "./foo.socket",
+ expectFileMode: "--w--w----",
+ },
+ {
+ input: "/var/run/foo|222",
+ expectPath: "/var/run/foo",
+ expectFileMode: "--w--w--w-",
+ },
+ {
+ input: "./foo.socket|0660",
+ expectPath: "./foo.socket",
+ expectFileMode: "-rw-rw----",
+ },
+ {
+ input: "./foo.socket|0666",
+ expectPath: "./foo.socket",
+ expectFileMode: "-rw-rw-rw-",
+ },
+ {
+ input: "/var/run/foo|666",
+ expectPath: "/var/run/foo",
+ expectFileMode: "-rw-rw-rw-",
+ },
+ {
+ input: `c:\absolute\path.socket|220`,
+ expectPath: `c:\absolute\path.socket`,
+ expectFileMode: "--w--w----",
+ },
+ {
+ // symbolic permission representation is not supported for now
+ input: "./foo.socket|u=rw,g=rw,o=rw",
+ expectErr: true,
+ },
+ {
+ // octal (base-8) permission representation has to be between
+ // `0` for no read, no write, no exec (`---`) and
+ // `7` for read (4), write (2), exec (1) (`rwx` => `4+2+1 = 7`)
+ input: "./foo.socket|888",
+ expectErr: true,
+ },
+ {
+ // too many colons in address
+ input: "./foo.socket|123456|0660",
+ expectErr: true,
+ },
+ {
+ // owner is missing write perms
+ input: "./foo.socket|0522",
+ expectErr: true,
+ },
+ } {
+ actualPath, actualFileMode, err := internal.SplitUnixSocketPermissionsBits(tc.input)
+ if tc.expectErr && err == nil {
+ t.Errorf("Test %d: Expected error but got: %v", i, err)
+ }
+ if !tc.expectErr && err != nil {
+ t.Errorf("Test %d: Expected no error but got: %v", i, err)
+ }
+ if actualPath != tc.expectPath {
+ t.Errorf("Test %d: Expected path '%s' but got '%s'", i, tc.expectPath, actualPath)
+ }
+ // fileMode.Perm().String() parses 0 to "----------"
+ if !tc.expectErr && actualFileMode.Perm().String() != tc.expectFileMode {
+ t.Errorf("Test %d: Expected perms '%s' but got '%s'", i, tc.expectFileMode, actualFileMode.Perm().String())
+ }
+ }
+}