From 007800b7f1db30ca096ecba47d20d5a2f71fe94d Mon Sep 17 00:00:00 2001 From: Girish Ramnani Date: Tue, 16 Jun 2020 19:42:43 +0530 Subject: [PATCH] resolved the gosec issue failing unit tests (#3358) * resolved the gosec issue failing unit tests * resolved typo in the comment * DRYed the code --- pkg/util/util.go | 10 +++++++--- tests/helper/helper_generic.go | 13 ++----------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/pkg/util/util.go b/pkg/util/util.go index cf5611cbd5e..98e32e0d021 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -4,10 +4,11 @@ import ( "archive/zip" "bufio" "context" + "crypto/rand" "fmt" "io" "io/ioutil" - "math/rand" + "math/big" "net" "net/http" "net/url" @@ -81,10 +82,13 @@ func ConvertLabelsToSelector(labels map[string]string) string { // GenerateRandomString generates a random string of lower case characters of // the given size func GenerateRandomString(n int) string { - rand.Seed(time.Now().UnixNano()) b := make([]rune, n) + for i := range b { - b[i] = letterRunes[rand.Intn(len(letterRunes))] + // this error is ignored because it fails only when the 2nd arg of Int() is less then 0 + // which wont happen + n, _ := rand.Int(rand.Reader, big.NewInt(int64(len(letterRunes)))) + b[i] = letterRunes[n.Int64()] } return string(b) } diff --git a/tests/helper/helper_generic.go b/tests/helper/helper_generic.go index ee677a3c75a..a724446841b 100644 --- a/tests/helper/helper_generic.go +++ b/tests/helper/helper_generic.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - "math/rand" "os" "os/exec" "path/filepath" @@ -14,20 +13,12 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/onsi/gomega/gexec" + "github.com/openshift/odo/pkg/util" ) -func init() { - rand.Seed(time.Now().UTC().UnixNano()) -} - // RandString returns a random string of given length func RandString(n int) string { - const letterBytes = "abcdefghijklmnopqrstuvwxyz" - b := make([]byte, n) - for i := range b { - b[i] = letterBytes[rand.Intn(len(letterBytes))] - } - return string(b) + return util.GenerateRandomString(n) } // WaitForCmdOut runs a command until it gets