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
This commit is contained in:
Chongyi Zheng 2024-04-27 12:50:35 -04:00 committed by GitHub
parent 8de2992ffb
commit 7b8e418da1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 10 additions and 16 deletions

View File

@ -5,8 +5,8 @@ package user_test
import ( import (
"context" "context"
"crypto/rand"
"fmt" "fmt"
"math/rand"
"strings" "strings"
"testing" "testing"
"time" "time"

View File

@ -4,9 +4,8 @@
package pwn package pwn
import ( import (
"math/rand" "math/rand/v2"
"net/http" "net/http"
"os"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -18,11 +17,6 @@ var client = New(WithHTTP(&http.Client{
Timeout: time.Second * 2, Timeout: time.Second * 2,
})) }))
func TestMain(m *testing.M) {
rand.Seed(time.Now().Unix())
os.Exit(m.Run())
}
func TestPassword(t *testing.T) { func TestPassword(t *testing.T) {
// Check input error // Check input error
_, err := client.CheckPassword("", false) _, err := client.CheckPassword("", false)
@ -81,24 +75,24 @@ func testPassword() string {
// Set special character // Set special character
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
random := rand.Intn(len(specialCharSet)) random := rand.IntN(len(specialCharSet))
password.WriteString(string(specialCharSet[random])) password.WriteString(string(specialCharSet[random]))
} }
// Set numeric // Set numeric
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
random := rand.Intn(len(numberSet)) random := rand.IntN(len(numberSet))
password.WriteString(string(numberSet[random])) password.WriteString(string(numberSet[random]))
} }
// Set uppercase // Set uppercase
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
random := rand.Intn(len(upperCharSet)) random := rand.IntN(len(upperCharSet))
password.WriteString(string(upperCharSet[random])) password.WriteString(string(upperCharSet[random]))
} }
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
random := rand.Intn(len(allCharSet)) random := rand.IntN(len(allCharSet))
password.WriteString(string(allCharSet[random])) password.WriteString(string(allCharSet[random]))
} }
inRune := []rune(password.String()) inRune := []rune(password.String())

View File

@ -4,7 +4,7 @@
package integration package integration
import ( import (
"math/rand" "math/rand/v2"
"net/http" "net/http"
"net/url" "net/url"
"testing" "testing"
@ -18,7 +18,7 @@ import (
func StringWithCharset(length int, charset string) string { func StringWithCharset(length int, charset string) string {
b := make([]byte, length) b := make([]byte, length)
for i := range b { for i := range b {
b[i] = charset[rand.Intn(len(charset))] b[i] = charset[rand.IntN(len(charset))]
} }
return string(b) return string(b)
} }
@ -37,7 +37,7 @@ func BenchmarkRepoBranchCommit(b *testing.B) {
b.ResetTimer() b.ResetTimer()
b.Run("CreateBranch", func(b *testing.B) { b.Run("CreateBranch", func(b *testing.B) {
b.StopTimer() b.StopTimer()
branchName := StringWithCharset(5+rand.Intn(10), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") branchName := StringWithCharset(5+rand.IntN(10), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b.StartTimer() b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
b.Run("new_"+branchName, func(b *testing.B) { b.Run("new_"+branchName, func(b *testing.B) {

View File

@ -5,9 +5,9 @@ package integration
import ( import (
"bytes" "bytes"
"crypto/rand"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"math/rand"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"