2020-06-11 23:44:39 +00:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
2020-07-02 09:31:12 +00:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2020-06-11 23:44:39 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/tunnelstore"
|
|
|
|
"github.com/google/uuid"
|
2020-11-20 22:22:32 +00:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2020-06-15 18:33:41 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-06-11 23:44:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_fmtConnections(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
connections []tunnelstore.Connection
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
args: args{
|
|
|
|
connections: []tunnelstore.Connection{},
|
|
|
|
},
|
|
|
|
want: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "trivial",
|
|
|
|
args: args{
|
|
|
|
connections: []tunnelstore.Connection{
|
|
|
|
{
|
|
|
|
ColoName: "DFW",
|
|
|
|
ID: uuid.MustParse("ea550130-57fd-4463-aab1-752822231ddd"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "1xDFW",
|
|
|
|
},
|
2020-08-05 20:43:56 +00:00
|
|
|
{
|
|
|
|
name: "with a pending reconnect",
|
|
|
|
args: args{
|
|
|
|
connections: []tunnelstore.Connection{
|
|
|
|
{
|
|
|
|
ColoName: "DFW",
|
|
|
|
ID: uuid.MustParse("ea550130-57fd-4463-aab1-752822231ddd"),
|
|
|
|
IsPendingReconnect: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "",
|
|
|
|
},
|
2020-06-11 23:44:39 +00:00
|
|
|
{
|
|
|
|
name: "many colos",
|
|
|
|
args: args{
|
|
|
|
connections: []tunnelstore.Connection{
|
|
|
|
{
|
|
|
|
ColoName: "YRV",
|
|
|
|
ID: uuid.MustParse("ea550130-57fd-4463-aab1-752822231ddd"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ColoName: "DFW",
|
|
|
|
ID: uuid.MustParse("c13c0b3b-0fbf-453c-8169-a1990fced6d0"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ColoName: "ATL",
|
|
|
|
ID: uuid.MustParse("70c90639-e386-4e8d-9a4e-7f046d70e63f"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ColoName: "DFW",
|
|
|
|
ID: uuid.MustParse("30ad6251-0305-4635-a670-d3994f474981"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "1xATL, 2xDFW, 1xYRV",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-08-05 20:43:56 +00:00
|
|
|
if got := fmtConnections(tt.args.connections, false); got != tt.want {
|
2020-06-11 23:44:39 +00:00
|
|
|
t.Errorf("fmtConnections() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-06-15 18:33:41 +00:00
|
|
|
|
|
|
|
func TestTunnelfilePath(t *testing.T) {
|
2020-07-06 08:01:48 +00:00
|
|
|
tunnelID, err := uuid.Parse("f48d8918-bc23-4647-9d48-082c5b76de65")
|
|
|
|
assert.NoError(t, err)
|
2020-07-02 09:31:12 +00:00
|
|
|
originCertDir := filepath.Dir("~/.cloudflared/cert.pem")
|
2020-07-06 08:01:48 +00:00
|
|
|
actual, err := tunnelFilePath(tunnelID, originCertDir)
|
2020-06-15 18:33:41 +00:00
|
|
|
assert.NoError(t, err)
|
2020-07-02 09:31:12 +00:00
|
|
|
homeDir, err := homedir.Dir()
|
|
|
|
assert.NoError(t, err)
|
2020-07-06 08:01:48 +00:00
|
|
|
expected := fmt.Sprintf("%s/.cloudflared/%v.json", homeDir, tunnelID)
|
2020-06-15 18:33:41 +00:00
|
|
|
assert.Equal(t, expected, actual)
|
|
|
|
}
|
2020-09-14 21:41:02 +00:00
|
|
|
|
|
|
|
func TestValidateName(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{name: "", want: false},
|
|
|
|
{name: "-", want: false},
|
|
|
|
{name: ".", want: false},
|
|
|
|
{name: "a b", want: false},
|
|
|
|
{name: "a+b", want: false},
|
|
|
|
{name: "-ab", want: false},
|
|
|
|
|
|
|
|
{name: "ab", want: true},
|
|
|
|
{name: "ab-c", want: true},
|
|
|
|
{name: "abc.def", want: true},
|
|
|
|
{name: "_ab_c.-d-ef", want: true},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2020-11-20 22:22:32 +00:00
|
|
|
if got := validateName(tt.name, false); got != tt.want {
|
2020-09-14 21:41:02 +00:00
|
|
|
t.Errorf("validateName() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
}
|
2020-11-20 22:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_validateHostname(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
s string
|
|
|
|
allowWildcardSubdomain bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Normal",
|
|
|
|
args: args{
|
|
|
|
s: "example.com",
|
|
|
|
allowWildcardSubdomain: true,
|
|
|
|
},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wildcard subdomain for TUN-358",
|
|
|
|
args: args{
|
|
|
|
s: "*.ehrig.io",
|
|
|
|
allowWildcardSubdomain: true,
|
|
|
|
},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Misplaced wildcard",
|
|
|
|
args: args{
|
|
|
|
s: "subdomain.*.ehrig.io",
|
|
|
|
allowWildcardSubdomain: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid domain",
|
|
|
|
args: args{
|
|
|
|
s: "..",
|
|
|
|
allowWildcardSubdomain: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid domain",
|
|
|
|
args: args{
|
|
|
|
s: "..",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := validateHostname(tt.args.s, tt.args.allowWildcardSubdomain); got != tt.want {
|
|
|
|
t.Errorf("validateHostname() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|