diff --git a/common/randomid.go b/common/randomid.go index 5b89cdf..5c6d592 100644 --- a/common/randomid.go +++ b/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/common/randomid_test.go b/common/randomid_test.go new file mode 100644 index 0000000..1dba412 --- /dev/null +++ b/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{}{} + } +}