random: seed math/rand in one place with crypto strong seed #4783

This shouldn't be read as encouraging the use of math/rand instead of
crypto/rand in security sensitive contexts, rather as a safer default
if that does happen by accident.
This commit is contained in:
Nick Craig-Wood 2020-11-18 14:02:53 +00:00
parent 7985df3768
commit f0905499e3
4 changed files with 35 additions and 5 deletions

View file

@ -1,6 +1,7 @@
package random
import (
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
@ -48,3 +49,16 @@ func TestPasswordDuplicates(t *testing.T) {
seen[s] = true
}
}
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
}
}