64 lines
2.0 KiB
Go
64 lines
2.0 KiB
Go
package randomutil_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
random "git.maronato.dev/maronato/goshort/internal/util/random"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestGenerateRandomBytes(t *testing.T) {
|
|
r1, err := random.GenerateRandomBytes(10)
|
|
if err != nil {
|
|
t.Errorf("Error testing GenerateRandomBytes: %v", err)
|
|
}
|
|
|
|
assert.Len(t, r1, 10, "GenerateRandomBytes should return a slice with the length passed as argument")
|
|
|
|
r2, _ := random.GenerateRandomBytes(10)
|
|
|
|
assert.NotEqual(t, r1, r2, "GenerateRandomBytes should return different results for different calls")
|
|
}
|
|
|
|
func TestGenerateFromCharset(t *testing.T) {
|
|
r1 := random.GenerateFromCharset("abc", 10)
|
|
|
|
assert.Len(t, r1, 10, "GenerateFromCharset should return a string with the length passed as argument")
|
|
|
|
for _, char := range r1 {
|
|
assert.Contains(t, "abc", string(char), "GenerateFromCharset should return a string with the characters passed as argument")
|
|
}
|
|
|
|
r2 := random.GenerateFromCharset("abc", 10)
|
|
|
|
assert.NotEqual(t, r1, r2, "GenerateFromCharset should return different results for different calls")
|
|
}
|
|
|
|
func TestGenerateSecureToken(t *testing.T) {
|
|
r1 := random.GenerateSecureToken(10)
|
|
|
|
assert.Len(t, r1, 10, "GenerateSecureToken should return a string with the length passed as argument")
|
|
|
|
for _, char := range r1 {
|
|
assert.Contains(t, random.SecureTokenCharset, string(char), "GenerateSecureToken should return a token with the characters from SecureTokenCharset")
|
|
}
|
|
|
|
r2 := random.GenerateSecureToken(10)
|
|
|
|
assert.NotEqual(t, r1, r2, "GenerateSecureToken should return different results for different calls")
|
|
}
|
|
|
|
func TestGenerateRandomShort(t *testing.T) {
|
|
r1 := random.GenerateRandomShort(10)
|
|
|
|
assert.Len(t, r1, 10, "GenerateRandomShort should return a string with the length passed as argument")
|
|
|
|
for _, char := range r1 {
|
|
assert.Contains(t, random.ShortCharset, string(char), "GenerateRandomShort should return a short with the characters from ShortCharset")
|
|
}
|
|
|
|
r2 := random.GenerateRandomShort(10)
|
|
|
|
assert.NotEqual(t, r1, r2, "GenerateRandomShort should return different results for different calls")
|
|
}
|