fix: handle StringFromCharset against empty charset and add Unit test for rand package#494
Conversation
…r rand.go Signed-off-by: Mrinmoy Matilal <mrinmoymatilal1315@gmail.com>
|
Welcome to the Microcks community! 💖 Thanks and congrats 🎉 for opening your first pull request here! Be sure to follow the pull request template or please update it accordingly. Hope you have a great time there! |
There was a problem hiding this comment.
Pull request overview
This PR fixes a panic in pkg/util/rand.StringFromCharset when called with an empty charset (by returning an error instead) and introduces the first unit tests for the pkg/util/rand package to cover key behaviors and edge cases.
Changes:
- Add an input guard to prevent
crypto/rand.Intfrom panicking on empty charset whenn > 0. - Add table-driven tests for
String/StringFromCharset, including the previously panicking empty-charset case.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| pkg/util/rand/rand.go | Adds empty-charset validation to prevent a panic and return a descriptive error. |
| pkg/util/rand/rand_test.go | Introduces initial unit test coverage for string generation and charset constraints. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if n > 0 && len(charset) == 0 { | ||
| return "", fmt.Errorf("charset must not be empty when n > 0") | ||
| } |
| func TestStringProducesDifferentValues(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| const length = 32 | ||
|
|
||
| first, err := String(length) | ||
| if err != nil { | ||
| t.Fatalf("first String(%d) returned error: %v", length, err) | ||
| } | ||
|
|
||
| second, err := String(length) | ||
| if err != nil { | ||
| t.Fatalf("second String(%d) returned error: %v", length, err) | ||
| } | ||
|
|
||
| if first == second { | ||
| t.Fatalf("expected two generated strings to differ, both were %q", first) | ||
| } | ||
| } |
Signed-off-by: Mrinmoy Matilal <mrinmoymatilal1315@gmail.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
pkg/util/rand/rand_test.go:108
StringFromCharsetnow has an explicit negative-length error path. Add unit tests for negativen(bothStringFromCharsetandString) so this behavior doesn’t regress back to panicking onmake([]byte, n).
func TestStringFromCharsetEmptyCharset(t *testing.T) {
t.Parallel()
got, err := StringFromCharset(5, "")
if err == nil {
| if n<0{ | ||
| return "", fmt.Errorf("n must be greater than 0") | ||
| } | ||
| if n > 0 && len(charset) == 0 { | ||
| return "", fmt.Errorf("charset must not be empty when n > 0") | ||
| } |
| func TestStringRepeatedCalls(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
|
|
||
| const ( | ||
| length = 32 | ||
| attempts=5 | ||
| ) | ||
|
|
||
| for i := 0; i < attempts; i++ { | ||
| got, err := String(length) | ||
| if err != nil { | ||
| t.Fatalf("String(%d) returned error on attempt %d: %v", length, i, err) | ||
| } | ||
| if len(got) != length { | ||
| t.Fatalf("String(%d) length on attempt %d = %d, want %d", length, i, len(got), length) | ||
| } | ||
| } | ||
| } |
What
Fixes a panic in
StringFromCharset(n, charset)— it panicked withcrypto/rand: argument to Int is <= 0when called with an empty charset andn > 0, instead of returning an error the way its other failure paths already do. Also adds the first unit test coverage forpkg/util/rand, which previously had none.Why
Closes #493
Changes
pkg/util/rand/rand.go: guard against an empty charset before callingcrypto/rand.Int, returning a descriptive error instead of panicking.pkg/util/rand/rand_test.go: table-driven tests forStringandStringFromCharset, covering:Testing
go test ./pkg/util/rand/... -v— all cases pass, including the previously-panicking empty-charset case.go test ./...— full suite passes.Notes
No behavior change for any existing valid input —
String()and existing callers ofStringFromCharset()with a non-empty charset are unaffected. This only changes behavior for the previously-panicking empty-charset edge case, and adds coverage that didn't exist before.