-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve code review comments in PR #491
- Add a seed for the random number generator to ensure that the tests are deterministic. - Move the expected engine outside of the handler - Add logic to compare the ID of the engine object returned by the client with the expected engine object.
- Loading branch information
Showing
2 changed files
with
30 additions
and
34 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 |
---|---|---|
@@ -1,60 +1,48 @@ | ||
package test | ||
|
||
import ( | ||
"crypto/rand" | ||
"log" | ||
"math/rand" | ||
"strings" | ||
"time" | ||
) | ||
|
||
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
const strLen = 10 | ||
const bitLen = 0xFF | ||
|
||
// #nosec G404 | ||
var r = rand.New(rand.NewSource(time.Now().UnixNano())) | ||
|
||
// Seeding func. | ||
// #nosec G404 | ||
func Seed(s int64) { | ||
r = rand.New(rand.NewSource(s)) | ||
} | ||
|
||
// See StackOverflow answer: | ||
// https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go | ||
// RandomString generates a cryptographically secure random string of length | ||
// RandomString generates a random string of length | ||
// strLen. | ||
func RandomString() string { | ||
sb := strings.Builder{} | ||
sb.Grow(strLen) | ||
|
||
for i := 0; i < strLen; i++ { | ||
randomByte := make([]byte, 1) | ||
_, err := rand.Read(randomByte) | ||
if err != nil { | ||
log.Fatalf("Error generating random string: %v", err) | ||
} | ||
randomIndex := randomByte[0] % byte(len(letters)) | ||
randomIndex := r.Intn(len(letters)) | ||
sb.WriteByte(letters[randomIndex]) | ||
} | ||
|
||
return sb.String() | ||
} | ||
|
||
// RandomInt generates a random integer between 0 (inclusive) and 'max' | ||
// (exclusive). We uses the crypto/rand library for generating random | ||
// bytes. It then performs a bitwise AND operation with 0xFF to keep only the | ||
// least significant 8 bits, effectively converting the byte to an integer. The | ||
// resulting integer is then modulo'd with 'max'. | ||
// (exclusive). | ||
func RandomInt(max int) int { | ||
var b [1]byte | ||
_, err := rand.Read(b[:]) | ||
if err != nil { | ||
log.Fatalf("Error generating random int: %v", err) | ||
} | ||
n := int(b[0]&bitLen) % max | ||
return n | ||
return r.Intn(max) | ||
} | ||
|
||
// RandomBool generates a cryptographically secure random boolean value. | ||
// It reads a single byte from the crypto/rand library and uses its least | ||
// significant bit to determine the boolean value. The function returns | ||
// true if the least significant bit is 1, and false otherwise. | ||
// RandomBool generates a random boolean value. | ||
// #nosec G404 | ||
func RandomBool() bool { | ||
var b [1]byte | ||
_, err := rand.Read(b[:]) | ||
if err != nil { | ||
log.Fatalf("Error generating random bool: %v", err) | ||
} | ||
return b[0]&1 == 1 | ||
n := 2 // #gomnd (golangci-lint magic number suppression) | ||
return r.Intn(n) == 1 | ||
} |