diff --git a/engines_test.go b/engines_test.go index 69c30377f..e3b08247c 100644 --- a/engines_test.go +++ b/engines_test.go @@ -24,19 +24,27 @@ func RandomEngine() Engine { // TestGetEngine Tests the retrieve engine endpoint of the API using the mocked server. func TestGetEngine(t *testing.T) { + test.Seed(42) // Seed the RNG client, server, teardown := setupOpenAITestServer() defer teardown() + + expectedEngine := RandomEngine() // move outside of handler per code review comment server.RegisterHandler("/v1/engines/text-davinci-003", func(w http.ResponseWriter, r *http.Request) { - engine := RandomEngine() - resBytes, _ := json.Marshal(engine) + resBytes, _ := json.Marshal(expectedEngine) fmt.Fprintln(w, string(resBytes)) }) - _, err := client.GetEngine(context.Background(), "text-davinci-003") + actualEngine, err := client.GetEngine(context.Background(), "text-davinci-003") checks.NoError(t, err, "GetEngine error") + + // Compare the two using only one field per code review comment + if actualEngine.ID != expectedEngine.ID { + t.Errorf("Engine ID mismatch: got %s, expected %s", actualEngine.ID, expectedEngine.ID) + } } // TestListEngines Tests the list engines endpoint of the API using the mocked server. func TestListEngines(t *testing.T) { + test.Seed(42) // Seed the RNG client, server, teardown := setupOpenAITestServer() defer teardown() server.RegisterHandler("/v1/engines", func(w http.ResponseWriter, r *http.Request) { diff --git a/internal/test/random.go b/internal/test/random.go index babbc5ad4..04f404d1f 100644 --- a/internal/test/random.go +++ b/internal/test/random.go @@ -1,30 +1,33 @@ 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]) } @@ -32,29 +35,14 @@ func RandomString() 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 }