Skip to content

Commit 7b8e418

Browse files
authored
Replace deprecated math/rand functions (#30733)
Suggested by logs in #30729 - Remove `math/rand.Seed` `rand.Seed is deprecated: As of Go 1.20 there is no reason to call Seed with a random value.` - Replace `math/rand.Read` `rand.Read is deprecated: For almost all use cases, [crypto/rand.Read] is more appropriate.` - Replace `math/rand` with `math/rand/v2`, which is available since Go 1.22
1 parent 8de2992 commit 7b8e418

File tree

4 files changed

+10
-16
lines changed

4 files changed

+10
-16
lines changed

models/user/user_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ package user_test
55

66
import (
77
"context"
8+
"crypto/rand"
89
"fmt"
9-
"math/rand"
1010
"strings"
1111
"testing"
1212
"time"

modules/auth/password/pwn/pwn_test.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
package pwn
55

66
import (
7-
"math/rand"
7+
"math/rand/v2"
88
"net/http"
9-
"os"
109
"strings"
1110
"testing"
1211
"time"
@@ -18,11 +17,6 @@ var client = New(WithHTTP(&http.Client{
1817
Timeout: time.Second * 2,
1918
}))
2019

21-
func TestMain(m *testing.M) {
22-
rand.Seed(time.Now().Unix())
23-
os.Exit(m.Run())
24-
}
25-
2620
func TestPassword(t *testing.T) {
2721
// Check input error
2822
_, err := client.CheckPassword("", false)
@@ -81,24 +75,24 @@ func testPassword() string {
8175

8276
// Set special character
8377
for i := 0; i < 5; i++ {
84-
random := rand.Intn(len(specialCharSet))
78+
random := rand.IntN(len(specialCharSet))
8579
password.WriteString(string(specialCharSet[random]))
8680
}
8781

8882
// Set numeric
8983
for i := 0; i < 5; i++ {
90-
random := rand.Intn(len(numberSet))
84+
random := rand.IntN(len(numberSet))
9185
password.WriteString(string(numberSet[random]))
9286
}
9387

9488
// Set uppercase
9589
for i := 0; i < 5; i++ {
96-
random := rand.Intn(len(upperCharSet))
90+
random := rand.IntN(len(upperCharSet))
9791
password.WriteString(string(upperCharSet[random]))
9892
}
9993

10094
for i := 0; i < 5; i++ {
101-
random := rand.Intn(len(allCharSet))
95+
random := rand.IntN(len(allCharSet))
10296
password.WriteString(string(allCharSet[random]))
10397
}
10498
inRune := []rune(password.String())

tests/integration/benchmarks_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
package integration
55

66
import (
7-
"math/rand"
7+
"math/rand/v2"
88
"net/http"
99
"net/url"
1010
"testing"
@@ -18,7 +18,7 @@ import (
1818
func StringWithCharset(length int, charset string) string {
1919
b := make([]byte, length)
2020
for i := range b {
21-
b[i] = charset[rand.Intn(len(charset))]
21+
b[i] = charset[rand.IntN(len(charset))]
2222
}
2323
return string(b)
2424
}
@@ -37,7 +37,7 @@ func BenchmarkRepoBranchCommit(b *testing.B) {
3737
b.ResetTimer()
3838
b.Run("CreateBranch", func(b *testing.B) {
3939
b.StopTimer()
40-
branchName := StringWithCharset(5+rand.Intn(10), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
40+
branchName := StringWithCharset(5+rand.IntN(10), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
4141
b.StartTimer()
4242
for i := 0; i < b.N; i++ {
4343
b.Run("new_"+branchName, func(b *testing.B) {

tests/integration/git_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ package integration
55

66
import (
77
"bytes"
8+
"crypto/rand"
89
"encoding/hex"
910
"fmt"
10-
"math/rand"
1111
"net/http"
1212
"net/url"
1313
"os"

0 commit comments

Comments
 (0)