netmaker/logic/util_test.go
Matthew R Kasun 6d6ba3aa7d random string
update algorithm
consolidate MakeRandomString, RandomString & GenerateCrytoString into
one function
2023-05-02 13:28:00 -04:00

32 lines
925 B
Go

package logic
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRandomString(t *testing.T) {
for testCase := 0; testCase < 100; testCase++ {
for size := 2; size < 2058; size++ {
if length := len(RandomString(size)); length != size {
t.Fatalf("expected random string of size %d, got %d instead", size, length)
}
}
}
}
func TestMakeRandomStringValid(t *testing.T) {
lengthStr := RandomString(10)
assert.Equal(t, len(lengthStr), 10)
validMqID := RandomString(23)
assert.False(t, strings.Contains(validMqID, "#"))
assert.False(t, strings.Contains(validMqID, "!"))
assert.False(t, strings.Contains(validMqID, "\""))
assert.False(t, strings.Contains(validMqID, "\\"))
assert.False(t, strings.Contains(validMqID, "+"))
assert.False(t, strings.Contains(validMqID, "-"))
assert.False(t, strings.Contains(validMqID, "{"))
assert.False(t, strings.Contains(validMqID, "}"))
}