2019-08-06 19:44:08 +08:00
|
|
|
package random
|
|
|
|
|
|
|
|
import (
|
2020-11-18 22:02:53 +08:00
|
|
|
"math/rand"
|
2019-08-06 19:44:08 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-08-25 15:39:31 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-06 19:44:08 +08:00
|
|
|
)
|
|
|
|
|
2019-08-25 15:39:31 +08:00
|
|
|
func TestStringLength(t *testing.T) {
|
2019-08-06 19:44:08 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
2019-08-25 15:39:31 +08:00
|
|
|
s := String(i)
|
|
|
|
assert.Equal(t, i, len(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStringDuplicates(t *testing.T) {
|
|
|
|
seen := map[string]bool{}
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
s := String(8)
|
|
|
|
assert.False(t, seen[s])
|
|
|
|
assert.Equal(t, 8, len(s))
|
|
|
|
seen[s] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPasswordLength(t *testing.T) {
|
|
|
|
for i := 0; i <= 128; i++ {
|
|
|
|
s, err := Password(i)
|
|
|
|
require.NoError(t, err)
|
|
|
|
// expected length is number of bytes rounded up
|
|
|
|
expected := i / 8
|
|
|
|
if i%8 != 0 {
|
|
|
|
expected++
|
|
|
|
}
|
|
|
|
// then converted to base 64
|
|
|
|
expected = (expected*8 + 5) / 6
|
|
|
|
assert.Equal(t, expected, len(s), i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPasswordDuplicates(t *testing.T) {
|
|
|
|
seen := map[string]bool{}
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
s, err := Password(64)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, seen[s])
|
|
|
|
seen[s] = true
|
2019-08-06 19:44:08 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-18 22:02:53 +08:00
|
|
|
|
|
|
|
func TestSeed(t *testing.T) {
|
|
|
|
// seed 100 times and check the first random number doesn't repeat
|
|
|
|
// This test could fail with a probability of ~ 10**-15
|
|
|
|
const n = 100
|
|
|
|
var seen = map[int64]bool{}
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
assert.NoError(t, Seed())
|
|
|
|
first := rand.Int63()
|
|
|
|
assert.False(t, seen[first])
|
|
|
|
seen[first] = true
|
|
|
|
}
|
|
|
|
}
|