Skip to content

fix: handle StringFromCharset against empty charset and add Unit test for rand package#494

Open
richochetclementine1315 wants to merge 2 commits into
microcks:masterfrom
richochetclementine1315:fixes-and-unittest
Open

fix: handle StringFromCharset against empty charset and add Unit test for rand package#494
richochetclementine1315 wants to merge 2 commits into
microcks:masterfrom
richochetclementine1315:fixes-and-unittest

Conversation

@richochetclementine1315

Copy link
Copy Markdown

What

Fixes a panic in StringFromCharset(n, charset) — it panicked with crypto/rand: argument to Int is <= 0 when called with an empty charset and n > 0, instead of returning an error the way its other failure paths already do. Also adds the first unit test coverage for pkg/util/rand, which previously had none.

Why

Closes #493

Changes

  • pkg/util/rand/rand.go: guard against an empty charset before calling crypto/rand.Int, returning a descriptive error instead of panicking.
  • pkg/util/rand/rand_test.go: table-driven tests for String and StringFromCharset, covering:
    • output length matches requested length across zero/single/short/long inputs
    • output characters all belong to the supplied/default charset
    • two calls with identical parameters produce different output
    • single-character charset behavior
    • empty-charset behavior — the case that previously panicked and now correctly returns an error

Testing

  • go test ./pkg/util/rand/... -v — all cases pass, including the previously-panicking empty-charset case.
  • go test ./... — full suite passes.
Screenshot 2026-07-25 222139

Notes

No behavior change for any existing valid input — String() and existing callers of StringFromCharset() 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.

…r rand.go

Signed-off-by: Mrinmoy Matilal <mrinmoymatilal1315@gmail.com>
Copilot AI review requested due to automatic review settings July 25, 2026 17:58
@github-actions

Copy link
Copy Markdown

👋 @richochetclementine1315

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!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.Int from panicking on empty charset when n > 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.

Comment thread pkg/util/rand/rand.go
Comment on lines +18 to +20
if n > 0 && len(charset) == 0 {
return "", fmt.Errorf("charset must not be empty when n > 0")
}
Comment thread pkg/util/rand/rand_test.go Outdated
Comment on lines +41 to +59
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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  • StringFromCharset now has an explicit negative-length error path. Add unit tests for negative n (both StringFromCharset and String) so this behavior doesn’t regress back to panicking on make([]byte, n).
func TestStringFromCharsetEmptyCharset(t *testing.T) {
	t.Parallel()

	got, err := StringFromCharset(5, "")
	if err == nil {

Comment thread pkg/util/rand/rand.go
Comment on lines +18 to +23
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")
}
Comment on lines +41 to +59
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)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StringFromCharset panics on empty charset — pkg/util/rand has no test coverage

2 participants