diff options
author | Chongyi Zheng <git@zcy.dev> | 2024-04-27 18:50:35 +0200 |
---|---|---|
committer | Earl Warren <contact@earl-warren.org> | 2024-04-28 15:39:02 +0200 |
commit | 781789e779f6ab8a85db4d5a252f706485ef7824 (patch) | |
tree | cd314a1ee8d8243b00b26d7e96f36503de76f3f2 | |
parent | Rename migration package name for 1.22-rc1 (#30730) (diff) | |
download | forgejo-781789e779f6ab8a85db4d5a252f706485ef7824.tar.xz forgejo-781789e779f6ab8a85db4d5a252f706485ef7824.zip |
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
(cherry picked from commit 7b8e418da1e082786b844562a05864ec1177ce97)
-rw-r--r-- | models/user/user_test.go | 2 | ||||
-rw-r--r-- | modules/auth/password/pwn/pwn_test.go | 16 | ||||
-rw-r--r-- | tests/integration/benchmarks_test.go | 6 | ||||
-rw-r--r-- | tests/integration/git_test.go | 2 |
4 files changed, 10 insertions, 16 deletions
diff --git a/models/user/user_test.go b/models/user/user_test.go index 571b8b2a93..4bf8c71369 100644 --- a/models/user/user_test.go +++ b/models/user/user_test.go @@ -5,8 +5,8 @@ package user_test import ( "context" + "crypto/rand" "fmt" - "math/rand" "strings" "testing" "time" diff --git a/modules/auth/password/pwn/pwn_test.go b/modules/auth/password/pwn/pwn_test.go index f9deadc8d7..a2a6b3a174 100644 --- a/modules/auth/password/pwn/pwn_test.go +++ b/modules/auth/password/pwn/pwn_test.go @@ -4,9 +4,8 @@ package pwn import ( - "math/rand" + "math/rand/v2" "net/http" - "os" "strings" "testing" "time" @@ -18,11 +17,6 @@ var client = New(WithHTTP(&http.Client{ Timeout: time.Second * 2, })) -func TestMain(m *testing.M) { - rand.Seed(time.Now().Unix()) - os.Exit(m.Run()) -} - func TestPassword(t *testing.T) { // Check input error _, err := client.CheckPassword("", false) @@ -81,24 +75,24 @@ func testPassword() string { // Set special character for i := 0; i < 5; i++ { - random := rand.Intn(len(specialCharSet)) + random := rand.IntN(len(specialCharSet)) password.WriteString(string(specialCharSet[random])) } // Set numeric for i := 0; i < 5; i++ { - random := rand.Intn(len(numberSet)) + random := rand.IntN(len(numberSet)) password.WriteString(string(numberSet[random])) } // Set uppercase for i := 0; i < 5; i++ { - random := rand.Intn(len(upperCharSet)) + random := rand.IntN(len(upperCharSet)) password.WriteString(string(upperCharSet[random])) } for i := 0; i < 5; i++ { - random := rand.Intn(len(allCharSet)) + random := rand.IntN(len(allCharSet)) password.WriteString(string(allCharSet[random])) } inRune := []rune(password.String()) diff --git a/tests/integration/benchmarks_test.go b/tests/integration/benchmarks_test.go index 7a882fe836..62da761d2d 100644 --- a/tests/integration/benchmarks_test.go +++ b/tests/integration/benchmarks_test.go @@ -4,7 +4,7 @@ package integration import ( - "math/rand" + "math/rand/v2" "net/http" "net/url" "testing" @@ -18,7 +18,7 @@ import ( func StringWithCharset(length int, charset string) string { b := make([]byte, length) for i := range b { - b[i] = charset[rand.Intn(len(charset))] + b[i] = charset[rand.IntN(len(charset))] } return string(b) } @@ -37,7 +37,7 @@ func BenchmarkRepoBranchCommit(b *testing.B) { b.ResetTimer() b.Run("CreateBranch", func(b *testing.B) { b.StopTimer() - branchName := StringWithCharset(5+rand.Intn(10), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") + branchName := StringWithCharset(5+rand.IntN(10), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") b.StartTimer() for i := 0; i < b.N; i++ { b.Run("new_"+branchName, func(b *testing.B) { diff --git a/tests/integration/git_test.go b/tests/integration/git_test.go index f833366568..6ee3be2df2 100644 --- a/tests/integration/git_test.go +++ b/tests/integration/git_test.go @@ -5,9 +5,9 @@ package integration import ( "bytes" + "crypto/rand" "encoding/hex" "fmt" - "math/rand" "net/http" "net/url" "os" |