2020-05-15 10:24:31 -07:00
|
|
|
package host
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testCase struct {
|
|
|
|
msg string
|
|
|
|
id string
|
|
|
|
expPass bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultIdentifierValidator(t *testing.T) {
|
|
|
|
testCases := []testCase{
|
|
|
|
{"valid lowercase", "lowercaseid", true},
|
|
|
|
{"valid id special chars", "._+-#[]<>._+-#[]<>", true},
|
|
|
|
{"valid id lower and special chars", "lower._+-#[]<>", true},
|
|
|
|
{"numeric id", "1234567890", true},
|
|
|
|
{"uppercase id", "NOTLOWERCASE", true},
|
|
|
|
{"numeric id", "1234567890", true},
|
|
|
|
{"blank id", " ", false},
|
|
|
|
{"id length out of range", "1", false},
|
|
|
|
{"path-like id", "lower/case/id", false},
|
|
|
|
{"invalid id", "(clientid)", false},
|
|
|
|
{"empty string", "", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
|
|
|
|
err := ClientIdentifierValidator(tc.id)
|
|
|
|
err1 := ConnectionIdentifierValidator(tc.id)
|
|
|
|
err2 := ChannelIdentifierValidator(tc.id)
|
|
|
|
err3 := PortIdentifierValidator(tc.id)
|
|
|
|
if tc.expPass {
|
|
|
|
require.NoError(t, err, tc.msg)
|
|
|
|
require.NoError(t, err1, tc.msg)
|
|
|
|
require.NoError(t, err2, tc.msg)
|
|
|
|
require.NoError(t, err3, tc.msg)
|
|
|
|
} else {
|
|
|
|
require.Error(t, err, tc.msg)
|
|
|
|
require.Error(t, err1, tc.msg)
|
|
|
|
require.Error(t, err2, tc.msg)
|
|
|
|
require.Error(t, err3, tc.msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPathValidator(t *testing.T) {
|
|
|
|
testCases := []testCase{
|
2020-05-15 11:58:43 -07:00
|
|
|
{"valid lowercase", "p/lowercaseid", true},
|
|
|
|
{"numeric path", "p/239123", true},
|
|
|
|
{"valid id special chars", "p/._+-#[]<>._+-#[]<>", true},
|
2020-05-15 10:24:31 -07:00
|
|
|
{"valid id lower and special chars", "lower/._+-#[]<>", true},
|
2020-05-15 11:58:43 -07:00
|
|
|
{"id length out of range", "p/l", true},
|
|
|
|
{"uppercase id", "p/NOTLOWERCASE", true},
|
2020-05-15 10:24:31 -07:00
|
|
|
{"invalid path", "lowercaseid", false},
|
2020-05-15 11:58:43 -07:00
|
|
|
{"blank id", "p/ ", false},
|
|
|
|
{"id length out of range", "p/123456789012345678901", false},
|
|
|
|
{"invalid id", "p/(clientid)", false},
|
2020-05-15 10:24:31 -07:00
|
|
|
{"empty string", "", false},
|
|
|
|
{"separators only", "////", false},
|
|
|
|
{"just separator", "/", false},
|
2020-05-15 11:58:43 -07:00
|
|
|
{"begins with separator", "/id", false},
|
|
|
|
{"blank before separator", " /id", false},
|
|
|
|
{"ends with separator", "id/", false},
|
|
|
|
{"blank after separator", "id/ ", false},
|
|
|
|
{"blanks with separator", " / ", false},
|
2020-05-15 10:24:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
err := PathValidator(tc.id)
|
|
|
|
if tc.expPass {
|
|
|
|
seps := strings.Count(tc.id, "/")
|
|
|
|
require.Equal(t, 1, seps)
|
|
|
|
require.NoError(t, err, tc.msg)
|
|
|
|
} else {
|
|
|
|
require.Error(t, err, tc.msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCustomPathValidator(t *testing.T) {
|
|
|
|
validateFn := NewPathValidator(func(path string) error {
|
|
|
|
if !strings.HasPrefix(path, "id_") {
|
|
|
|
return fmt.Errorf("identifier %s must start with 'id_", path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
testCases := []testCase{
|
2020-05-15 11:58:43 -07:00
|
|
|
{"valid custom path", "id_client/id_one", true},
|
2020-05-15 10:24:31 -07:00
|
|
|
{"invalid path", "client", false},
|
2020-05-15 11:58:43 -07:00
|
|
|
{"invalid custom path", "id_one/client", false},
|
|
|
|
{"invalid identifier", "id_client/id_123456789012345678901", false},
|
2020-05-15 10:24:31 -07:00
|
|
|
{"separators only", "////", false},
|
|
|
|
{"just separator", "/", false},
|
2020-05-15 11:58:43 -07:00
|
|
|
{"ends with separator", "id_client/id_one/", false},
|
|
|
|
{"beings with separator", "/id_client/id_one", false},
|
2020-05-15 10:24:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
err := validateFn(tc.id)
|
|
|
|
if tc.expPass {
|
|
|
|
require.NoError(t, err, tc.msg)
|
|
|
|
} else {
|
|
|
|
require.Error(t, err, tc.msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|