fixes 11721 replace stringutils.GenerateRandomString with stringid.GenerateRandomID
| ... | ... |
@@ -11,7 +11,7 @@ import ( |
| 11 | 11 |
"time" |
| 12 | 12 |
|
| 13 | 13 |
"github.com/docker/docker/pkg/ioutils" |
| 14 |
- "github.com/docker/docker/pkg/stringutils" |
|
| 14 |
+ "github.com/docker/docker/pkg/stringid" |
|
| 15 | 15 |
) |
| 16 | 16 |
|
| 17 | 17 |
// Installer is a standard interface for objects which can "install" themselves |
| ... | ... |
@@ -78,7 +78,7 @@ func (eng *Engine) RegisterCatchall(catchall Handler) {
|
| 78 | 78 |
func New() *Engine {
|
| 79 | 79 |
eng := &Engine{
|
| 80 | 80 |
handlers: make(map[string]Handler), |
| 81 |
- id: stringutils.GenerateRandomString(), |
|
| 81 |
+ id: stringid.GenerateRandomID(), |
|
| 82 | 82 |
Stdout: os.Stdout, |
| 83 | 83 |
Stderr: os.Stderr, |
| 84 | 84 |
Stdin: os.Stdin, |
| ... | ... |
@@ -1,23 +1,10 @@ |
| 1 | 1 |
package stringutils |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
- "crypto/rand" |
|
| 5 |
- "encoding/hex" |
|
| 6 |
- "io" |
|
| 7 | 4 |
mathrand "math/rand" |
| 8 | 5 |
"time" |
| 9 | 6 |
) |
| 10 | 7 |
|
| 11 |
-// Generate 32 chars random string |
|
| 12 |
-func GenerateRandomString() string {
|
|
| 13 |
- id := make([]byte, 32) |
|
| 14 |
- |
|
| 15 |
- if _, err := io.ReadFull(rand.Reader, id); err != nil {
|
|
| 16 |
- panic(err) // This shouldn't happen |
|
| 17 |
- } |
|
| 18 |
- return hex.EncodeToString(id) |
|
| 19 |
-} |
|
| 20 |
- |
|
| 21 | 8 |
// Generate alpha only random stirng with length n |
| 22 | 9 |
func GenerateRandomAlphaOnlyString(n int) string {
|
| 23 | 10 |
// make a really long string |
| ... | ... |
@@ -2,18 +2,19 @@ package stringutils |
| 2 | 2 |
|
| 3 | 3 |
import "testing" |
| 4 | 4 |
|
| 5 |
-func TestRandomString(t *testing.T) {
|
|
| 6 |
- str := GenerateRandomString() |
|
| 7 |
- if len(str) != 64 {
|
|
| 8 |
- t.Fatalf("Id returned is incorrect: %s", str)
|
|
| 5 |
+func testLengthHelper(generator func(int) string, t *testing.T) {
|
|
| 6 |
+ expectedLength := 20 |
|
| 7 |
+ s := generator(expectedLength) |
|
| 8 |
+ if len(s) != expectedLength {
|
|
| 9 |
+ t.Fatalf("Length of %s was %d but expected length %d", s, len(s), expectedLength)
|
|
| 9 | 10 |
} |
| 10 | 11 |
} |
| 11 | 12 |
|
| 12 |
-func TestRandomStringUniqueness(t *testing.T) {
|
|
| 13 |
+func testUniquenessHelper(generator func(int) string, t *testing.T) {
|
|
| 13 | 14 |
repeats := 25 |
| 14 | 15 |
set := make(map[string]struct{}, repeats)
|
| 15 | 16 |
for i := 0; i < repeats; i = i + 1 {
|
| 16 |
- str := GenerateRandomString() |
|
| 17 |
+ str := generator(64) |
|
| 17 | 18 |
if len(str) != 64 {
|
| 18 | 19 |
t.Fatalf("Id returned is incorrect: %s", str)
|
| 19 | 20 |
} |
| ... | ... |
@@ -23,3 +24,35 @@ func TestRandomStringUniqueness(t *testing.T) {
|
| 23 | 23 |
set[str] = struct{}{}
|
| 24 | 24 |
} |
| 25 | 25 |
} |
| 26 |
+ |
|
| 27 |
+func isASCII(s string) bool {
|
|
| 28 |
+ for _, c := range s {
|
|
| 29 |
+ if c > 127 {
|
|
| 30 |
+ return false |
|
| 31 |
+ } |
|
| 32 |
+ } |
|
| 33 |
+ return true |
|
| 34 |
+} |
|
| 35 |
+ |
|
| 36 |
+func TestGenerateRandomAlphaOnlyStringLength(t *testing.T) {
|
|
| 37 |
+ testLengthHelper(GenerateRandomAlphaOnlyString, t) |
|
| 38 |
+} |
|
| 39 |
+ |
|
| 40 |
+func TestGenerateRandomAlphaOnlyStringUniqueness(t *testing.T) {
|
|
| 41 |
+ testUniquenessHelper(GenerateRandomAlphaOnlyString, t) |
|
| 42 |
+} |
|
| 43 |
+ |
|
| 44 |
+func TestGenerateRandomAsciiStringLength(t *testing.T) {
|
|
| 45 |
+ testLengthHelper(GenerateRandomAsciiString, t) |
|
| 46 |
+} |
|
| 47 |
+ |
|
| 48 |
+func TestGenerateRandomAsciiStringUniqueness(t *testing.T) {
|
|
| 49 |
+ testUniquenessHelper(GenerateRandomAsciiString, t) |
|
| 50 |
+} |
|
| 51 |
+ |
|
| 52 |
+func TestGenerateRandomAsciiStringIsAscii(t *testing.T) {
|
|
| 53 |
+ str := GenerateRandomAsciiString(64) |
|
| 54 |
+ if !isASCII(str) {
|
|
| 55 |
+ t.Fatalf("%s contained non-ascii characters", str)
|
|
| 56 |
+ } |
|
| 57 |
+} |
| ... | ... |
@@ -24,7 +24,7 @@ import ( |
| 24 | 24 |
"github.com/docker/docker/pkg/fileutils" |
| 25 | 25 |
"github.com/docker/docker/pkg/ioutils" |
| 26 | 26 |
"github.com/docker/docker/pkg/jsonmessage" |
| 27 |
- "github.com/docker/docker/pkg/stringutils" |
|
| 27 |
+ "github.com/docker/docker/pkg/stringid" |
|
| 28 | 28 |
) |
| 29 | 29 |
|
| 30 | 30 |
type KeyValuePair struct {
|
| ... | ... |
@@ -313,7 +313,7 @@ var globalTestID string |
| 313 | 313 |
// new directory. |
| 314 | 314 |
func TestDirectory(templateDir string) (dir string, err error) {
|
| 315 | 315 |
if globalTestID == "" {
|
| 316 |
- globalTestID = stringutils.GenerateRandomString()[:4] |
|
| 316 |
+ globalTestID = stringid.GenerateRandomID()[:4] |
|
| 317 | 317 |
} |
| 318 | 318 |
prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, GetCallerName(2))
|
| 319 | 319 |
if prefix == "" {
|