forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve sashabaranov#487: Enhance tests and fix linting issues
- Add randomized fields to Engine structs in tests - Implement cryptographically secure random utility functions - Address golangci-lint warnings
- Loading branch information
Showing
2 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package test | ||
|
||
import ( | ||
"crypto/rand" | ||
"math/big" | ||
) | ||
|
||
var ( | ||
strLen = 10 | ||
//nolint:gomnd // this avoids the golangci-lint "magic number" warning | ||
n = big.NewInt(2) | ||
) | ||
|
||
// RandomString generates a cryptographically secure random string of a fixed | ||
// length. The string is composed of alphanumeric characters and is generated | ||
// using the crypto/rand library. The length of the string is determined by the | ||
// constant strLen. | ||
func RandomString() string { | ||
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") | ||
s := make([]rune, strLen) | ||
max := big.NewInt(int64(len(letters))) | ||
for i := range s { | ||
randomInt, _ := rand.Int(rand.Reader, max) | ||
s[i] = letters[randomInt.Int64()] | ||
} | ||
return string(s) | ||
} | ||
|
||
// RandomBool generates a cryptographically secure random boolean value. It | ||
// uses the crypto/rand library to generate a random integer (either 0 or 1), | ||
// and returns true if the integer is 1, and false otherwise. | ||
func RandomBool() bool { | ||
randomInt, _ := rand.Int(rand.Reader, n) | ||
return randomInt.Int64() == 1 | ||
} |