Skip to content

Commit

Permalink
resolved the gosec issue failing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Girish Ramnani committed Jun 16, 2020
1 parent fa9f7b9 commit 97c392d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 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 happend
n, _ := rand.Int(rand.Reader, big.NewInt(int64(len(letterRunes))))
b[i] = letterRunes[n.Int64()]
}
return string(b)
}
Expand Down
12 changes: 6 additions & 6 deletions tests/helper/helper_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package helper

import (
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
"math/rand"
"math/big"
"os"
"os/exec"
"path/filepath"
Expand All @@ -16,16 +17,15 @@ import (
"github.com/onsi/gomega/gexec"
)

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))]
// 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(letterBytes))))
b[i] = letterBytes[n.Int64()]
}
return string(b)
}
Expand Down

0 comments on commit 97c392d

Please sign in to comment.