From 0b5c8b9cdc35cadc115edfb3e39c02fc894f0e60 Mon Sep 17 00:00:00 2001 From: Stephen Hassard Date: Sun, 30 Dec 2018 05:09:42 -0800 Subject: Fix RNG seeding and add a test. (#51) * Fix seeding the RNG. * Add test for RandomString. --- pkg/helpers/utils.go | 4 ++-- pkg/helpers/utils_test.go | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 pkg/helpers/utils_test.go diff --git a/pkg/helpers/utils.go b/pkg/helpers/utils.go index 1a3f411..35de2c3 100644 --- a/pkg/helpers/utils.go +++ b/pkg/helpers/utils.go @@ -7,7 +7,7 @@ import ( ) var characterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") -var src = rand.NewSource(time.Now().UnixNano()) +var r = rand.New(rand.NewSource(time.Now().UnixNano())) // TrimTrailingNewline removes the trailing new line form a string. this method // is used mostly on outputs of a command @@ -30,7 +30,7 @@ func Min(x, y int) int { func RandomString(n int) string { b := make([]rune, n) for i := range b { - b[i] = characterRunes[rand.Intn(len(characterRunes))] + b[i] = characterRunes[r.Intn(len(characterRunes))] } return string(b) } diff --git a/pkg/helpers/utils_test.go b/pkg/helpers/utils_test.go new file mode 100644 index 0000000..4b48d3c --- /dev/null +++ b/pkg/helpers/utils_test.go @@ -0,0 +1,10 @@ +package helpers + +import "testing" + +func TestRandomString(t *testing.T) { + var rand_string = RandomString(8) + if len(rand_string) != 8 { + t.Errorf("The length of the string should be equal.") + } +} -- cgit v1.2.3