Skip to content

Commit

Permalink
resolved the gosec issue failing unit tests (redhat-developer#3358)
Browse files Browse the repository at this point in the history
* resolved the gosec issue failing unit tests

* resolved typo in the comment

* DRYed the code
  • Loading branch information
Girish Ramnani authored and cdrage committed Jun 17, 2020
1 parent 690842c commit 007800b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
10 changes: 7 additions & 3 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"archive/zip"
"bufio"
"context"
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"math/rand"
"math/big"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -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)
}
Expand Down
13 changes: 2 additions & 11 deletions tests/helper/helper_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"os"
"os/exec"
"path/filepath"
Expand All @@ -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
Expand Down

0 comments on commit 007800b

Please sign in to comment.