Skip to content

Commit

Permalink
[BUGFIX] Fix RandString typo bug. (Vonage#242)
Browse files Browse the repository at this point in the history
* Bump version

* Fix RandString typo bug

* Rename constant

---------

Co-authored-by: nicolaasuni-vonage <nicola.asuni@vonage.com>
  • Loading branch information
github-actions[bot] and nicolaasuni-vonage authored May 3, 2024
1 parent 134fa91 commit 4a197fb
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.90.0
1.90.1
2 changes: 1 addition & 1 deletion examples/service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.22.2
replace github.com/Vonage/gosrvlib => ../..

require (
github.com/Vonage/gosrvlib v1.90.0
github.com/Vonage/gosrvlib v1.90.1
github.com/golang/mock v1.6.0
github.com/jstemmer/go-junit-report v1.0.0
github.com/prometheus/client_golang v1.19.0
Expand Down
2 changes: 1 addition & 1 deletion pkg/random/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Option func(c *Rnd)
func WithByteToCharMap(cm []byte) Option {
switch d := len(cm); {
case d == 0:
cm = []byte(chrMap)
cm = []byte(chrMapDefault)
case d > chrMapMaxLen:
cm = cm[:chrMapMaxLen]
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/random/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestWithByteToCharMap(t *testing.T) {
t.Parallel()

want := []byte(chrMap)
want := []byte(chrMapDefault)
c := &Rnd{}

WithByteToCharMap(want)(c)
Expand Down
18 changes: 10 additions & 8 deletions pkg/random/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
)

const (
chrDigits = "0123456789"
chrUppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
chrLowercase = "abcdefghijklmnopqrstuvwxyz"
chrSymbols = "!#$%&()*+,-./:;<=>?@[]^_{|}~" // (exclude "\"'\\`")
chrMap = chrDigits + chrUppercase + chrLowercase + chrSymbols
chrMapMaxLen = 256
chrDigits = "0123456789"
chrUppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
chrLowercase = "abcdefghijklmnopqrstuvwxyz"
chrSymbols = "!#$%&()*+,-./:;<=>?@[]^_{|}~" // (exclude "\"'\\`")
chrMapDefault = chrDigits + chrUppercase + chrLowercase + chrSymbols
chrMapMaxLen = 256
)

// Rnd defines then random number generator.
Expand All @@ -36,7 +36,7 @@ func New(r io.Reader, opts ...Option) *Rnd {

rnd := &Rnd{
reader: r,
chrMap: []byte(chrMap),
chrMap: []byte(chrMapDefault),
}

for _, applyOpt := range opts {
Expand Down Expand Up @@ -89,8 +89,10 @@ func (r *Rnd) RandString(n int) (string, error) {
return "", err
}

cmlen := len(r.chrMap)

for i, v := range b {
b[i] = r.chrMap[(int(v)*len(chrMap))>>8]
b[i] = r.chrMap[(int(v)*cmlen)>>8]
}

return string(b), nil
Expand Down
7 changes: 7 additions & 0 deletions pkg/random/random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ func TestRandString(t *testing.T) {
require.NoError(t, err)
require.Len(t, s, 17)

rc := New(nil, WithByteToCharMap([]byte(chrDigits+chrLowercase)))

sc, err := rc.RandString(16)

require.NoError(t, err)
require.Len(t, sc, 16)

re := New(iotest.ErrReader(errors.New("test-randstring-error")))

s, err = re.RandString(32)
Expand Down

0 comments on commit 4a197fb

Please sign in to comment.