From 53ece336dcab8e778fc37afc7a264f7d168110ff Mon Sep 17 00:00:00 2001 From: Srini Brahmaroutu Date: Wed, 25 Feb 2015 18:27:14 +0000 Subject: [PATCH] moving random.go from utils Closes #10962 Signed-off-by: Srini Brahmaroutu --- engine/engine.go | 4 +-- pkg/common/randomid.go | 9 ++++++ pkg/common/randomid_test.go | 59 +++++++++++++++++++++++++++++++++++++ utils/random.go | 16 ---------- utils/utils.go | 3 +- 5 files changed, 72 insertions(+), 19 deletions(-) create mode 100644 pkg/common/randomid_test.go delete mode 100644 utils/random.go diff --git a/engine/engine.go b/engine/engine.go index 26f9953d66..e8286d89f7 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -10,8 +10,8 @@ import ( "sync" "time" + "github.com/docker/docker/pkg/common" "github.com/docker/docker/pkg/ioutils" - "github.com/docker/docker/utils" ) // Installer is a standard interface for objects which can "install" themselves @@ -77,7 +77,7 @@ func (eng *Engine) RegisterCatchall(catchall Handler) { func New() *Engine { eng := &Engine{ handlers: make(map[string]Handler), - id: utils.RandomString(), + id: common.RandomString(), Stdout: os.Stdout, Stderr: os.Stderr, Stdin: os.Stdin, diff --git a/pkg/common/randomid.go b/pkg/common/randomid.go index 5b89cdf9cd..5c6d5920e8 100644 --- a/pkg/common/randomid.go +++ b/pkg/common/randomid.go @@ -36,3 +36,12 @@ func GenerateRandomID() string { return value } } + +func RandomString() string { + id := make([]byte, 32) + + if _, err := io.ReadFull(rand.Reader, id); err != nil { + panic(err) // This shouldn't happen + } + return hex.EncodeToString(id) +} diff --git a/pkg/common/randomid_test.go b/pkg/common/randomid_test.go new file mode 100644 index 0000000000..1dba41254b --- /dev/null +++ b/pkg/common/randomid_test.go @@ -0,0 +1,59 @@ +package common + +import ( + "testing" +) + +func TestShortenId(t *testing.T) { + id := GenerateRandomID() + truncID := TruncateID(id) + if len(truncID) != 12 { + t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID) + } +} + +func TestShortenIdEmpty(t *testing.T) { + id := "" + truncID := TruncateID(id) + if len(truncID) > len(id) { + t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID) + } +} + +func TestShortenIdInvalid(t *testing.T) { + id := "1234" + truncID := TruncateID(id) + if len(truncID) != len(id) { + t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID) + } +} + +func TestGenerateRandomID(t *testing.T) { + id := GenerateRandomID() + + if len(id) != 64 { + t.Fatalf("Id returned is incorrect: %s", id) + } +} + +func TestRandomString(t *testing.T) { + id := RandomString() + if len(id) != 64 { + t.Fatalf("Id returned is incorrect: %s", id) + } +} + +func TestRandomStringUniqueness(t *testing.T) { + repeats := 25 + set := make(map[string]struct{}, repeats) + for i := 0; i < repeats; i = i + 1 { + id := RandomString() + if len(id) != 64 { + t.Fatalf("Id returned is incorrect: %s", id) + } + if _, ok := set[id]; ok { + t.Fatalf("Random number is repeated") + } + set[id] = struct{}{} + } +} diff --git a/utils/random.go b/utils/random.go deleted file mode 100644 index 907f28eec3..0000000000 --- a/utils/random.go +++ /dev/null @@ -1,16 +0,0 @@ -package utils - -import ( - "crypto/rand" - "encoding/hex" - "io" -) - -func RandomString() string { - id := make([]byte, 32) - - if _, err := io.ReadFull(rand.Reader, id); err != nil { - panic(err) // This shouldn't happen - } - return hex.EncodeToString(id) -} diff --git a/utils/utils.go b/utils/utils.go index a3d59eef70..cc3b499f65 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -21,6 +21,7 @@ import ( log "github.com/Sirupsen/logrus" "github.com/docker/docker/autogen/dockerversion" "github.com/docker/docker/pkg/archive" + "github.com/docker/docker/pkg/common" "github.com/docker/docker/pkg/fileutils" "github.com/docker/docker/pkg/ioutils" ) @@ -311,7 +312,7 @@ var globalTestID string // new directory. func TestDirectory(templateDir string) (dir string, err error) { if globalTestID == "" { - globalTestID = RandomString()[:4] + globalTestID = common.RandomString()[:4] } prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, GetCallerName(2)) if prefix == "" {