diff options
author | Zeger-Jan van de Weg <git@zjvandeweg.nl> | 2019-06-24 11:28:02 +0300 |
---|---|---|
committer | Zeger-Jan van de Weg <git@zjvandeweg.nl> | 2019-06-24 11:28:02 +0300 |
commit | 6df618362e4019b841023c02bfbf5a8b86bd9f5a (patch) | |
tree | 05ca52196504615303e9f2d39c887fc472424f61 | |
parent | 1c414abc11b4e55fed512a65e484a7b7c6d7f538 (diff) | |
parent | f3e285c404df7dd23ad00922dc46590b33b679d0 (diff) |
Merge branch 'jv-random-hex-helper' into 'master'
Add text.RandomHex helper
See merge request gitlab-org/gitaly!1322
-rw-r--r-- | internal/git/objectpool/fetch_test.go | 7 | ||||
-rw-r--r-- | internal/helper/text/random.go | 17 | ||||
-rw-r--r-- | internal/service/objectpool/alternates.go | 9 | ||||
-rw-r--r-- | internal/testhelper/testhelper.go | 6 |
4 files changed, 24 insertions, 15 deletions
diff --git a/internal/git/objectpool/fetch_test.go b/internal/git/objectpool/fetch_test.go index 5af01b220..6b4e94463 100644 --- a/internal/git/objectpool/fetch_test.go +++ b/internal/git/objectpool/fetch_test.go @@ -1,10 +1,7 @@ package objectpool import ( - "crypto/rand" - "encoding/hex" "fmt" - "io" "io/ioutil" "path/filepath" "strings" @@ -36,10 +33,8 @@ func TestFetchFromOriginDangling(t *testing.T) { // We want to have some objects that are guaranteed to be dangling. Use // random data to make each object unique. - nonceBytes := make([]byte, 4) - _, err = io.ReadFull(rand.Reader, nonceBytes) + nonce, err := text.RandomHex(4) require.NoError(t, err) - nonce := hex.EncodeToString(nonceBytes) baseArgs := []string{"-C", pool.FullPath()} diff --git a/internal/helper/text/random.go b/internal/helper/text/random.go new file mode 100644 index 000000000..5b9f9a714 --- /dev/null +++ b/internal/helper/text/random.go @@ -0,0 +1,17 @@ +package text + +import ( + "crypto/rand" + "encoding/hex" + "io" +) + +// RandomHex returns an n-byte hexademical random string. +func RandomHex(n int) (string, error) { + buf := make([]byte, n) + if _, err := io.ReadFull(rand.Reader, buf); err != nil { + return "", err + } + + return hex.EncodeToString(buf), nil +} diff --git a/internal/service/objectpool/alternates.go b/internal/service/objectpool/alternates.go index 7e952993c..332c7a2e5 100644 --- a/internal/service/objectpool/alternates.go +++ b/internal/service/objectpool/alternates.go @@ -2,10 +2,8 @@ package objectpool import ( "context" - "crypto/rand" "errors" "fmt" - "io" "io/ioutil" "os" "path/filepath" @@ -17,6 +15,7 @@ import ( "gitlab.com/gitlab-org/gitaly-proto/go/gitalypb" "gitlab.com/gitlab-org/gitaly/internal/git" "gitlab.com/gitlab-org/gitaly/internal/helper" + "gitlab.com/gitlab-org/gitaly/internal/helper/text" ) // DisconnectGitAlternates is a slightly dangerous RPC. It optimistically @@ -109,12 +108,12 @@ func disconnectAlternates(ctx context.Context, repo *gitalypb.Repository) error } func newBackupFile(altFile string) (string, error) { - randSuffix := make([]byte, 6) - if _, err := io.ReadFull(rand.Reader, randSuffix); err != nil { + randSuffix, err := text.RandomHex(6) + if err != nil { return "", err } - return fmt.Sprintf("%s.%d.%x", altFile, time.Now().Unix(), randSuffix), nil + return fmt.Sprintf("%s.%d.%s", altFile, time.Now().Unix(), randSuffix), nil } func findObjectFiles(altDir string) ([]string, error) { diff --git a/internal/testhelper/testhelper.go b/internal/testhelper/testhelper.go index 2ff9c0a3f..fcd7db736 100644 --- a/internal/testhelper/testhelper.go +++ b/internal/testhelper/testhelper.go @@ -3,7 +3,6 @@ package testhelper import ( "bytes" "context" - "crypto/rand" "encoding/base64" "encoding/json" "fmt" @@ -482,11 +481,10 @@ func AssertFileNotExists(t *testing.T, path string) { // NewTestObjectPoolName returns a random pool repository name. func NewTestObjectPoolName(t *testing.T) string { - b := make([]byte, 5) - _, err := io.ReadFull(rand.Reader, b) + b, err := text.RandomHex(5) require.NoError(t, err) - return fmt.Sprintf("pool-%x.git", b) + return fmt.Sprintf("pool-%s.git", b) } // CreateLooseRef creates a ref that points to master |