Skip to content

Commit

Permalink
handle bug with username gen, add test, handle tags bug
Browse files Browse the repository at this point in the history
  • Loading branch information
frodopwns committed Apr 3, 2020
1 parent d7fdd8c commit dae21f4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
19 changes: 19 additions & 0 deletions pkg/helpers/helpers_strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

package helpers

import (
"testing"
"unicode"
)

// TestGenerateRandomUsername tests whether the username generated will ever start with a non letter
func TestGenerateRandomUsername(t *testing.T) {
for i := 0; i < 1000; i++ {
u := GenerateRandomUsername(10)
if !unicode.IsLetter(rune(u[0])) {
t.Errorf("index 0 of username '%s' is not a letter '%s'", u, string(u[0]))
}
}
}
2 changes: 1 addition & 1 deletion pkg/helpers/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func LabelsToTags(in map[string]string) map[string]*string {
newK := k
value := v
if strings.ContainsAny(k, "<>%/?\\") {
newK = ReplaceAny(k, []string{"<", ">", "%", "/", "\\\\", "?"})
newK = ReplaceAny(k, []string{"<", ">", "%", "/", "\\\\", "\\?"})
}
out[newK] = &value
}
Expand Down
18 changes: 7 additions & 11 deletions pkg/helpers/stringhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import (
)

const (
passwordLength = 16
passwordChars = lowerAlphaChars + upperAlphaChars + numberChars + specialChars
usernameChars = lowerAlphaChars + upperAlphaChars + numberChars
passwordLength = 16

lowerAlphaChars = "abcdefghijklmnopqrstuvwxyz"
upperAlphaChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numberChars = "0123456789"
specialChars = "!@#$%^&*"
passwordChars = lowerAlphaChars + upperAlphaChars + numberChars + specialChars
usernameChars = lowerAlphaChars + upperAlphaChars + numberChars
allCaseAlpha = lowerAlphaChars + upperAlphaChars
)

var seededRand = NewSeeded()
Expand Down Expand Up @@ -96,19 +98,13 @@ func GenerateRandomUsername(n int) string {
b := make([]byte, n)

// ensure first char is alpha
b[0] = lowerAlphaChars[seededRand.Intn(len(lowerAlphaChars))]
b[0] = allCaseAlpha[seededRand.Intn(len(allCaseAlpha))]

for i := 1; i < n; i++ {
b[i] = usernameChars[seededRand.Intn(len(usernameChars))]
}
// For good measure, shuffle the elements of the entire []byte so that
// the 0 character isn't predicatably lowercase, etc...
for i := range b {
j := seededRand.Intn(len(b))
b[i], b[j] = b[j], b[i]
}
return string(b)

return string(b)
}

// GenerateRandomPassword - helper function to generate random password for sql server
Expand Down

0 comments on commit dae21f4

Please sign in to comment.