From ae440baf74682b9c111462e189097257b7cd7422 Mon Sep 17 00:00:00 2001 From: Peter Esbensen Date: Tue, 31 Mar 2015 22:38:23 -0700 Subject: [PATCH 1/2] Added unit tests for stringutils GenerateRandomAlphaOnlyString and GenerateRandomAsciiString Signed-off-by: Peter Esbensen --- stringutils/stringutils_test.go | 55 +++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/stringutils/stringutils_test.go b/stringutils/stringutils_test.go index 60b848f..e9da58b 100644 --- a/stringutils/stringutils_test.go +++ b/stringutils/stringutils_test.go @@ -23,3 +23,58 @@ func TestRandomStringUniqueness(t *testing.T) { set[str] = struct{}{} } } + +func testLengthHelper(generator func(int) string, t *testing.T) { + expectedLength := 20 + s := generator(expectedLength) + if 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) { + repeats := 25 + set := make(map[string]struct{}, repeats) + for i := 0; i < repeats; i = i + 1 { + str := generator(64) + 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 isASCII(s string) bool { + for _, c := range s { + if c > 127 { + return false + } + } + return true +} + +func TestGenerateRandomAlphaOnlyStringLength(t *testing.T) { + testLengthHelper(GenerateRandomAlphaOnlyString, t) +} + +func TestGenerateRandomAlphaOnlyStringUniqueness(t *testing.T) { + testUniquenessHelper(GenerateRandomAlphaOnlyString, t) +} + +func TestGenerateRandomAsciiStringLength(t *testing.T) { + testLengthHelper(GenerateRandomAsciiString, t) +} + +func TestGenerateRandomAsciiStringUniqueness(t *testing.T) { + testUniquenessHelper(GenerateRandomAsciiString, t) +} + +func TestGenerateRandomAsciiStringIsAscii(t *testing.T) { + str := GenerateRandomAsciiString(64) + if !isASCII(str) { + t.Fatalf("%s contained non-ascii characters", str) + } +} From 88e878b0132644064a6d733cd9ad70e8c51cd526 Mon Sep 17 00:00:00 2001 From: Peter Esbensen Date: Wed, 1 Apr 2015 07:21:07 -0700 Subject: [PATCH 2/2] Fixes #11721 removed GenerateRandomString Signed-off-by: Peter Esbensen gofmt Signed-off-by: Peter Esbensen --- stringutils/stringutils.go | 13 ------------- stringutils/stringutils_test.go | 26 ++------------------------ 2 files changed, 2 insertions(+), 37 deletions(-) diff --git a/stringutils/stringutils.go b/stringutils/stringutils.go index bcb0ece..f5f07dd 100644 --- a/stringutils/stringutils.go +++ b/stringutils/stringutils.go @@ -1,23 +1,10 @@ package stringutils import ( - "crypto/rand" - "encoding/hex" - "io" mathrand "math/rand" "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 func GenerateRandomAlphaOnlyString(n int) string { // make a really long string diff --git a/stringutils/stringutils_test.go b/stringutils/stringutils_test.go index e9da58b..a5a01b4 100644 --- a/stringutils/stringutils_test.go +++ b/stringutils/stringutils_test.go @@ -2,34 +2,12 @@ package stringutils 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) { expectedLength := 20 s := generator(expectedLength) if 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) { @@ -65,7 +43,7 @@ func TestGenerateRandomAlphaOnlyStringUniqueness(t *testing.T) { } func TestGenerateRandomAsciiStringLength(t *testing.T) { - testLengthHelper(GenerateRandomAsciiString, t) + testLengthHelper(GenerateRandomAsciiString, t) } func TestGenerateRandomAsciiStringUniqueness(t *testing.T) {