Skip to content
/ gitea Public
  • Sponsor go-gitea/gitea

  • Notifications You must be signed in to change notification settings
  • Fork 5.8k

Replace deprecated math/rand functions #30733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion models/user/user_test.go
Original file line number Diff line number Diff line change
@@ -5,8 +5,8 @@ package user_test

import (
"context"
"crypto/rand"
"fmt"
"math/rand"
"strings"
"testing"
"time"
16 changes: 5 additions & 11 deletions modules/auth/password/pwn/pwn_test.go
Original file line number Diff line number Diff line change
@@ -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())
6 changes: 3 additions & 3 deletions tests/integration/benchmarks_test.go
Original file line number Diff line number Diff line change
@@ -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) {
2 changes: 1 addition & 1 deletion tests/integration/git_test.go
Original file line number Diff line number Diff line change
@@ -5,9 +5,9 @@ package integration

import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"math/rand"
"net/http"
"net/url"
"os"