2019-06-30 17:07:58 -05:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-07-08 17:46:38 -05:00
|
|
|
package caddy
|
2019-03-26 16:45:51 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-09-05 14:14:39 -05:00
|
|
|
func TestSplitNetworkAddress(t *testing.T) {
|
2019-05-07 11:15:46 -05:00
|
|
|
for i, tc := range []struct {
|
|
|
|
input string
|
|
|
|
expectNetwork string
|
|
|
|
expectHost string
|
|
|
|
expectPort string
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: "",
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "foo",
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "foo:1234",
|
|
|
|
expectHost: "foo",
|
|
|
|
expectPort: "1234",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "foo:1234-5678",
|
|
|
|
expectHost: "foo",
|
|
|
|
expectPort: "1234-5678",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "udp/foo:1234",
|
|
|
|
expectNetwork: "udp",
|
|
|
|
expectHost: "foo",
|
|
|
|
expectPort: "1234",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "tcp6/foo:1234-5678",
|
|
|
|
expectNetwork: "tcp6",
|
|
|
|
expectHost: "foo",
|
|
|
|
expectPort: "1234-5678",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "udp/",
|
|
|
|
expectNetwork: "udp",
|
|
|
|
expectErr: true,
|
|
|
|
},
|
2019-07-08 17:46:38 -05:00
|
|
|
{
|
|
|
|
input: "unix//foo/bar",
|
|
|
|
expectNetwork: "unix",
|
|
|
|
expectHost: "/foo/bar",
|
|
|
|
},
|
2019-09-05 14:14:39 -05:00
|
|
|
{
|
|
|
|
input: "unixgram//foo/bar",
|
|
|
|
expectNetwork: "unixgram",
|
|
|
|
expectHost: "/foo/bar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "unixpacket//foo/bar",
|
|
|
|
expectNetwork: "unixpacket",
|
|
|
|
expectHost: "/foo/bar",
|
|
|
|
},
|
2019-05-07 11:15:46 -05:00
|
|
|
} {
|
2019-09-05 14:14:39 -05:00
|
|
|
actualNetwork, actualHost, actualPort, err := SplitNetworkAddress(tc.input)
|
2019-05-07 11:15:46 -05:00
|
|
|
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 actualNetwork != tc.expectNetwork {
|
|
|
|
t.Errorf("Test %d: Expected network '%s' but got '%s'", i, tc.expectNetwork, actualNetwork)
|
|
|
|
}
|
|
|
|
if actualHost != tc.expectHost {
|
|
|
|
t.Errorf("Test %d: Expected host '%s' but got '%s'", i, tc.expectHost, actualHost)
|
|
|
|
}
|
|
|
|
if actualPort != tc.expectPort {
|
|
|
|
t.Errorf("Test %d: Expected port '%s' but got '%s'", i, tc.expectPort, actualPort)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 14:14:39 -05:00
|
|
|
func TestJoinNetworkAddress(t *testing.T) {
|
2019-05-07 11:15:46 -05:00
|
|
|
for i, tc := range []struct {
|
|
|
|
network, host, port string
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
network: "", host: "", port: "",
|
|
|
|
expect: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
network: "tcp", host: "", port: "",
|
|
|
|
expect: "tcp/",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
network: "", host: "foo", port: "",
|
|
|
|
expect: "foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
network: "", host: "", port: "1234",
|
|
|
|
expect: ":1234",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
network: "", host: "", port: "1234-5678",
|
|
|
|
expect: ":1234-5678",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
network: "", host: "foo", port: "1234",
|
|
|
|
expect: "foo:1234",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
network: "udp", host: "foo", port: "1234",
|
|
|
|
expect: "udp/foo:1234",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
network: "udp", host: "", port: "1234",
|
|
|
|
expect: "udp/:1234",
|
|
|
|
},
|
2019-07-08 17:46:38 -05:00
|
|
|
{
|
|
|
|
network: "unix", host: "/foo/bar", port: "",
|
|
|
|
expect: "unix//foo/bar",
|
|
|
|
},
|
2019-10-11 15:25:39 -05:00
|
|
|
{
|
|
|
|
network: "", host: "::1", port: "1234",
|
|
|
|
expect: "[::1]:1234",
|
|
|
|
},
|
2019-05-07 11:15:46 -05:00
|
|
|
} {
|
2019-09-05 14:14:39 -05:00
|
|
|
actual := JoinNetworkAddress(tc.network, tc.host, tc.port)
|
2019-05-07 11:15:46 -05:00
|
|
|
if actual != tc.expect {
|
|
|
|
t.Errorf("Test %d: Expected '%s' but got '%s'", i, tc.expect, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 14:14:39 -05:00
|
|
|
func TestParseNetworkAddress(t *testing.T) {
|
2019-03-26 16:45:51 -05:00
|
|
|
for i, tc := range []struct {
|
2019-05-07 11:15:46 -05:00
|
|
|
input string
|
|
|
|
expectNetwork string
|
|
|
|
expectAddrs []string
|
|
|
|
expectErr bool
|
2019-03-26 16:45:51 -05:00
|
|
|
}{
|
|
|
|
{
|
2019-05-07 11:15:46 -05:00
|
|
|
input: "",
|
|
|
|
expectNetwork: "tcp",
|
|
|
|
expectErr: true,
|
2019-03-26 16:45:51 -05:00
|
|
|
},
|
|
|
|
{
|
2019-05-07 11:15:46 -05:00
|
|
|
input: ":",
|
|
|
|
expectNetwork: "tcp",
|
|
|
|
expectErr: true,
|
2019-03-26 16:45:51 -05:00
|
|
|
},
|
|
|
|
{
|
2019-05-07 11:15:46 -05:00
|
|
|
input: ":1234",
|
|
|
|
expectNetwork: "tcp",
|
|
|
|
expectAddrs: []string{":1234"},
|
2019-03-26 16:45:51 -05:00
|
|
|
},
|
|
|
|
{
|
2019-05-07 11:15:46 -05:00
|
|
|
input: "tcp/:1234",
|
|
|
|
expectNetwork: "tcp",
|
|
|
|
expectAddrs: []string{":1234"},
|
2019-03-26 16:45:51 -05:00
|
|
|
},
|
|
|
|
{
|
2019-05-07 11:15:46 -05:00
|
|
|
input: "tcp6/:1234",
|
|
|
|
expectNetwork: "tcp6",
|
|
|
|
expectAddrs: []string{":1234"},
|
2019-03-26 16:45:51 -05:00
|
|
|
},
|
|
|
|
{
|
2019-05-07 11:15:46 -05:00
|
|
|
input: "tcp4/localhost:1234",
|
|
|
|
expectNetwork: "tcp4",
|
|
|
|
expectAddrs: []string{"localhost:1234"},
|
2019-03-26 16:45:51 -05:00
|
|
|
},
|
|
|
|
{
|
2019-07-08 17:46:38 -05:00
|
|
|
input: "unix//foo/bar",
|
2019-05-07 11:15:46 -05:00
|
|
|
expectNetwork: "unix",
|
2019-07-08 17:46:38 -05:00
|
|
|
expectAddrs: []string{"/foo/bar"},
|
2019-03-26 16:45:51 -05:00
|
|
|
},
|
|
|
|
{
|
2019-05-07 11:15:46 -05:00
|
|
|
input: "localhost:1234-1234",
|
|
|
|
expectNetwork: "tcp",
|
|
|
|
expectAddrs: []string{"localhost:1234"},
|
2019-03-26 16:45:51 -05:00
|
|
|
},
|
|
|
|
{
|
2019-05-07 11:15:46 -05:00
|
|
|
input: "localhost:2-1",
|
|
|
|
expectNetwork: "tcp",
|
|
|
|
expectErr: true,
|
2019-03-26 16:45:51 -05:00
|
|
|
},
|
|
|
|
{
|
2019-05-07 11:15:46 -05:00
|
|
|
input: "localhost:0",
|
|
|
|
expectNetwork: "tcp",
|
|
|
|
expectAddrs: []string{"localhost:0"},
|
2019-03-26 16:45:51 -05:00
|
|
|
},
|
|
|
|
} {
|
2019-09-05 14:14:39 -05:00
|
|
|
actualNetwork, actualAddrs, err := ParseNetworkAddress(tc.input)
|
2019-03-26 16:45:51 -05:00
|
|
|
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)
|
|
|
|
}
|
2019-05-07 11:15:46 -05:00
|
|
|
if actualNetwork != tc.expectNetwork {
|
|
|
|
t.Errorf("Test %d: Expected network '%s' but got '%s'", i, tc.expectNetwork, actualNetwork)
|
2019-03-26 16:45:51 -05:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tc.expectAddrs, actualAddrs) {
|
|
|
|
t.Errorf("Test %d: Expected addresses %v but got %v", i, tc.expectAddrs, actualAddrs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|