Skip to content

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

Merged
Harsh4902 merged 3 commits into
microcks:masterfrom
richochetclementine1315:fixes-and-unittest
Aug 19, 2026
Merged

fix: handle StringFromCharset against empty charset and add Unit test for rand package#494
Harsh4902 merged 3 commits into
microcks:masterfrom
richochetclementine1315:fixes-and-unittest

Conversation

@richochetclementine1315

Copy link
Copy Markdown
Contributor

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 thread pkg/util/rand/rand_test.go Outdated
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 thread pkg/util/rand/rand_test.go Outdated
@richochetclementine1315

richochetclementine1315 commented Jul 26, 2026

Copy link
Copy Markdown
Contributor Author

@Vaishnav88sk @lbroudoux @yada @Harsh4902 requesting to review and suggest changes if needed

Signed-off-by: Mrinmoy Matilal <mrinmoymatilal1315@gmail.com>
@Vaishnav88sk

Copy link
Copy Markdown

LGTM @Harsh4902

@Caesarsage

Copy link
Copy Markdown
Contributor

+1 — verified the guards cover both reported panics: the empty-charset case, and via the n<0 check the String(-1) case from #359 (String delegates to StringFromCharset). One nit: gofmt the new guard lines (if n<0{). When this merges, #359, #493 and #360 can all close against it — #360 predates this with the same idea, so a credit mention there would be a kind touch.

@Harsh4902 Harsh4902 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

GTG

@Harsh4902
Harsh4902 merged commit 0dd8f55 into microcks:master Aug 19, 2026
5 checks passed
@Harsh4902 Harsh4902 added this to the Next milestone Aug 19, 2026
@github-actions

Copy link
Copy Markdown

🎉 @richochetclementine1315

You are now a Microcks community contributor! 💖

Thanks and congrats 🚀 on merging your first pull request! We are delighted and very proud of you! 👏

📢 If you're using Microcks in your organization, please add your company name to this list. 🙏 It really helps the project to gain momentum and credibility. It's a small contribution back to the project with a big impact.

If you need to know why and how to add yourself to the list, please read the blog post "Join the Microcks Adopters list and Empower the vibrant open source Community 🙌"

Kudos and please keep going, we need you 🙌

@richochetclementine1315

Copy link
Copy Markdown
Contributor Author

Thankyou Everyone!!...Looking forward to making more meaningful and quality contributions at MicrocksCLI...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

5 participants