Fixes #11721 removed GenerateRandomString
Signed-off-by: Peter Esbensen <pkesbensen@gmail.com> gofmt Signed-off-by: Peter Esbensen <pkesbensen@gmail.com>
This commit is contained in:
parent
ae440baf74
commit
88e878b013
2 changed files with 2 additions and 37 deletions
|
@ -1,23 +1,10 @@
|
||||||
package stringutils
|
package stringutils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
|
||||||
"encoding/hex"
|
|
||||||
"io"
|
|
||||||
mathrand "math/rand"
|
mathrand "math/rand"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Generate 32 chars random string
|
|
||||||
func GenerateRandomString() string {
|
|
||||||
id := make([]byte, 32)
|
|
||||||
|
|
||||||
if _, err := io.ReadFull(rand.Reader, id); err != nil {
|
|
||||||
panic(err) // This shouldn't happen
|
|
||||||
}
|
|
||||||
return hex.EncodeToString(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate alpha only random stirng with length n
|
// Generate alpha only random stirng with length n
|
||||||
func GenerateRandomAlphaOnlyString(n int) string {
|
func GenerateRandomAlphaOnlyString(n int) string {
|
||||||
// make a really long string
|
// make a really long string
|
||||||
|
|
|
@ -2,34 +2,12 @@ package stringutils
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestRandomString(t *testing.T) {
|
|
||||||
str := GenerateRandomString()
|
|
||||||
if len(str) != 64 {
|
|
||||||
t.Fatalf("Id returned is incorrect: %s", str)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRandomStringUniqueness(t *testing.T) {
|
|
||||||
repeats := 25
|
|
||||||
set := make(map[string]struct{}, repeats)
|
|
||||||
for i := 0; i < repeats; i = i + 1 {
|
|
||||||
str := GenerateRandomString()
|
|
||||||
if len(str) != 64 {
|
|
||||||
t.Fatalf("Id returned is incorrect: %s", str)
|
|
||||||
}
|
|
||||||
if _, ok := set[str]; ok {
|
|
||||||
t.Fatalf("Random number is repeated")
|
|
||||||
}
|
|
||||||
set[str] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func testLengthHelper(generator func(int) string, t *testing.T) {
|
func testLengthHelper(generator func(int) string, t *testing.T) {
|
||||||
expectedLength := 20
|
expectedLength := 20
|
||||||
s := generator(expectedLength)
|
s := generator(expectedLength)
|
||||||
if len(s) != expectedLength {
|
if len(s) != expectedLength {
|
||||||
t.Fatalf("Length of %s was %d but expected length %d", s, len(s), expectedLength)
|
t.Fatalf("Length of %s was %d but expected length %d", s, len(s), expectedLength)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testUniquenessHelper(generator func(int) string, t *testing.T) {
|
func testUniquenessHelper(generator func(int) string, t *testing.T) {
|
||||||
|
@ -65,7 +43,7 @@ func TestGenerateRandomAlphaOnlyStringUniqueness(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGenerateRandomAsciiStringLength(t *testing.T) {
|
func TestGenerateRandomAsciiStringLength(t *testing.T) {
|
||||||
testLengthHelper(GenerateRandomAsciiString, t)
|
testLengthHelper(GenerateRandomAsciiString, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGenerateRandomAsciiStringUniqueness(t *testing.T) {
|
func TestGenerateRandomAsciiStringUniqueness(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue