@@ -2,17 +2,26 @@ package goreplay
22
33import (
44 "bytes"
5+ cryptoRand "crypto/rand"
56 "errors"
67 "fmt"
78 "io/ioutil"
8- cryptoRand "crypto/rand"
99 "math/big"
1010 "os"
1111 "sync"
1212 "testing"
1313 "time"
1414)
1515
16+ // generateSecureRandomID generates a cryptographically secure random ID for use in tests
17+ func generateSecureRandomID (t * testing.T ) int64 {
18+ randomBigInt , err := cryptoRand .Int (cryptoRand .Reader , big .NewInt (1 << 62 ))
19+ if err != nil {
20+ t .Fatalf ("Failed to generate secure random number: %v" , err )
21+ }
22+ return randomBigInt .Int64 ()
23+ }
24+
1625func TestInputFileWithGET (t * testing.T ) {
1726 input := NewTestInput ()
1827 rg := NewRequestGenerator ([]PluginReader {input }, func () { input .EmitGET () }, 1 )
@@ -89,12 +98,7 @@ func TestInputFileWithGETAndPOST(t *testing.T) {
8998}
9099
91100func TestInputFileMultipleFilesWithRequestsOnly (t * testing.T ) {
92- // Use crypto/rand instead of math/rand for secure random number generation
93- randomBigInt , err := cryptoRand .Int (cryptoRand .Reader , big .NewInt (1 << 62 ))
94- if err != nil {
95- t .Fatalf ("Failed to generate secure random number: %v" , err )
96- }
97- rnd := randomBigInt .Int64 ()
101+ rnd := generateSecureRandomID (t )
98102
99103 file1 , _ := os .OpenFile (fmt .Sprintf ("/tmp/%d_0" , rnd ), os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0660 )
100104 file1 .Write ([]byte ("1 1 1\n test1" ))
@@ -124,12 +128,7 @@ func TestInputFileMultipleFilesWithRequestsOnly(t *testing.T) {
124128}
125129
126130func TestInputFileRequestsWithLatency (t * testing.T ) {
127- // Use crypto/rand instead of math/rand for secure random number generation
128- randomBigInt , err := cryptoRand .Int (cryptoRand .Reader , big .NewInt (1 << 62 ))
129- if err != nil {
130- t .Fatalf ("Failed to generate secure random number: %v" , err )
131- }
132- rnd := randomBigInt .Int64 ()
131+ rnd := generateSecureRandomID (t )
133132
134133 file , _ := os .OpenFile (fmt .Sprintf ("/tmp/%d" , rnd ), os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0660 )
135134 defer file .Close ()
@@ -157,12 +156,7 @@ func TestInputFileRequestsWithLatency(t *testing.T) {
157156}
158157
159158func TestInputFileMultipleFilesWithRequestsAndResponses (t * testing.T ) {
160- // Use crypto/rand instead of math/rand for secure random number generation
161- randomBigInt , err := cryptoRand .Int (cryptoRand .Reader , big .NewInt (1 << 62 ))
162- if err != nil {
163- t .Fatalf ("Failed to generate secure random number: %v" , err )
164- }
165- rnd := randomBigInt .Int64 ()
159+ rnd := generateSecureRandomID (t )
166160
167161 file1 , _ := os .OpenFile (fmt .Sprintf ("/tmp/%d_0" , rnd ), os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0660 )
168162 file1 .Write ([]byte ("1 1 1\n request1" ))
@@ -205,12 +199,7 @@ func TestInputFileMultipleFilesWithRequestsAndResponses(t *testing.T) {
205199}
206200
207201func TestInputFileLoop (t * testing.T ) {
208- // Use crypto/rand instead of math/rand for secure random number generation
209- randomBigInt , err := cryptoRand .Int (cryptoRand .Reader , big .NewInt (1 << 62 ))
210- if err != nil {
211- t .Fatalf ("Failed to generate secure random number: %v" , err )
212- }
213- rnd := randomBigInt .Int64 ()
202+ rnd := generateSecureRandomID (t )
214203
215204 file , _ := os .OpenFile (fmt .Sprintf ("/tmp/%d" , rnd ), os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0660 )
216205 file .Write ([]byte ("1 1 1\n test1" ))
@@ -231,12 +220,7 @@ func TestInputFileLoop(t *testing.T) {
231220}
232221
233222func TestInputFileCompressed (t * testing.T ) {
234- // Use crypto/rand instead of math/rand for secure random number generation
235- randomBigInt , err := cryptoRand .Int (cryptoRand .Reader , big .NewInt (1 << 62 ))
236- if err != nil {
237- t .Fatalf ("Failed to generate secure random number: %v" , err )
238- }
239- rnd := randomBigInt .Int64 ()
223+ rnd := generateSecureRandomID (t )
240224
241225 output := NewFileOutput (fmt .Sprintf ("/tmp/%d_0.gz" , rnd ), & FileOutputConfig {FlushInterval : time .Minute , Append : true })
242226 for i := 0 ; i < 1000 ; i ++ {
@@ -262,12 +246,7 @@ func TestInputFileCompressed(t *testing.T) {
262246}
263247
264248func TestInputFileWatchForNewFiles (t * testing.T ) {
265- // Use crypto/rand instead of math/rand for secure random number generation
266- randomBigInt , err := cryptoRand .Int (cryptoRand .Reader , big .NewInt (1 << 62 ))
267- if err != nil {
268- t .Fatalf ("Failed to generate secure random number: %v" , err )
269- }
270- rnd := randomBigInt .Int64 ()
249+ rnd := generateSecureRandomID (t )
271250 basePath := fmt .Sprintf ("/tmp/%d" , rnd )
272251
273252 // Create first file
0 commit comments