Repository navigation
[Repo Assist] test(strutil): add RandomHexWithFallback and simplify jqschema.go #4729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,3 +89,43 @@ func TestRandomHex_Uniqueness(t *testing.T) { | |
| seen[id] = true | ||
| } | ||
| } | ||
|
|
||
| // TestRandomHexWithFallback_ReturnsValidHex verifies the normal (non-fallback) path returns | ||
| // a valid hex-encoded string of the correct length. | ||
| func TestRandomHexWithFallback_ReturnsValidHex(t *testing.T) { | ||
| result := RandomHexWithFallback(16) | ||
| require.NotEmpty(t, result, "result should not be empty") | ||
| // On a healthy system crypto/rand always succeeds, so we expect a 32-char hex string. | ||
| assert.Len(t, result, 32, "16 bytes should produce 32 hex chars") | ||
| _, err := hex.DecodeString(result) | ||
| assert.NoError(t, err, "result should be valid hex") | ||
| } | ||
|
Comment on lines
+93
to
+102
|
||
|
|
||
| // TestRandomHexWithFallback_ZeroBytes verifies zero bytes produces an empty string. | ||
| func TestRandomHexWithFallback_ZeroBytes(t *testing.T) { | ||
| result := RandomHexWithFallback(0) | ||
| assert.Empty(t, result, "zero bytes should produce empty string") | ||
| } | ||
|
|
||
| // TestRandomHexWithFallback_Uniqueness verifies the function produces unique values. | ||
| func TestRandomHexWithFallback_Uniqueness(t *testing.T) { | ||
| seen := make(map[string]bool) | ||
| for i := 0; i < 100; i++ { | ||
| id := RandomHexWithFallback(16) | ||
| assert.NotEmpty(t, id) | ||
| assert.False(t, seen[id], "RandomHexWithFallback should produce unique values") | ||
| seen[id] = true | ||
| } | ||
| } | ||
|
|
||
| // TestRandomHexWithFallback_NegativeFallsBack verifies that a negative size triggers the | ||
| // fallback path (since RandomHex returns an error for negative n) and that the fallback | ||
| // still produces a valid hex-encoded string. | ||
| func TestRandomHexWithFallback_NegativeFallsBack(t *testing.T) { | ||
| result := RandomHexWithFallback(-1) | ||
| // The fallback produces a valid 32-character hex string (pid + nanoseconds encoded). | ||
| assert.NotEmpty(t, result, "fallback should produce a non-empty string") | ||
| _, err := hex.DecodeString(result) | ||
| assert.NoError(t, err, "fallback should produce valid hex") | ||
| assert.Len(t, result, 32, "fallback should produce 32 hex chars (16 bytes)") | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Despite the name/comment, this function does not always return hex: on fallback it returns a
fallback-...string which will failhex.DecodeStringand break callers that assume hex semantics (length2*n, lowercase hex, etc.). Consider either making the fallback also hex-encoded (e.g., hash/encode the fallback bytes), or changing the API to surface whether fallback occurred (or return an error) so callers can decide how to handle non-hex output.This issue also appears in the following locations of the same file: