refactor: update math/rand import to v2 and adjust usage

This commit is contained in:
Tommaso Gragnato 2025-02-11 20:22:21 +01:00
parent 6f0cfb1141
commit 16273d6c37
No known key found for this signature in database
GPG Key ID: E4345A2BE53026FC
9 changed files with 26 additions and 26 deletions

View File

@ -3,7 +3,7 @@ package mainline
import (
"crypto/rand"
"encoding/binary"
mrand "math/rand"
mrand "math/rand/v2"
"net"
"reflect"
"strconv"
@ -343,7 +343,7 @@ func randomNodeID() []byte {
_, err := rand.Read(nodeID)
if err != nil {
for i := 0; i < 20; i++ {
nodeID[i] = byte(mrand.Intn(256))
nodeID[i] = byte(mrand.IntN(256))
}
}
return nodeID

View File

@ -1,7 +1,7 @@
package mainline
import (
"math/rand"
"math/rand/v2"
"net"
"strconv"
"testing"
@ -37,7 +37,7 @@ func TestUint16BE(t *testing.T) {
func TestBasicIndexingService(t *testing.T) {
t.Parallel()
randomPort := rand.Intn(64511) + 1024
randomPort := rand.IntN(64511) + 1024
tests := []struct {
name string
laddr string

View File

@ -4,7 +4,7 @@ import (
"bytes"
"crypto/rand"
"crypto/sha1"
mrand "math/rand"
mrand "math/rand/v2"
"net"
"sync"
"time"
@ -342,10 +342,10 @@ func (p *Protocol) updateTokenSecret() {
defer p.tokenLock.Unlock()
_, err := rand.Read(p.tokenSecret)
if err != nil {
len := mrand.Intn(256)
len := mrand.IntN(256)
p.tokenSecret = make([]byte, len)
for i := 0; i < len; i++ {
p.tokenSecret[i] = byte(mrand.Intn(256))
p.tokenSecret[i] = byte(mrand.IntN(256))
}
}
}

View File

@ -1,7 +1,7 @@
package mainline
import (
"math/rand"
"math/rand/v2"
"net"
"strconv"
"strings"
@ -105,7 +105,7 @@ func TestTransport_WriteMessages(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
transport := NewTransport(
net.JoinHostPort("::1", strconv.Itoa(rand.Intn(64511)+1024)),
net.JoinHostPort("::1", strconv.Itoa(rand.IntN(64511)+1024)),
func(m *Message, u *net.UDPAddr) {},
1000,
)

View File

@ -1,7 +1,7 @@
package dht
import (
"math/rand"
"math/rand/v2"
"net"
"reflect"
"strconv"
@ -35,9 +35,9 @@ func (tr *TestResult) PeerAddrs() []net.TCPAddr {
func TestChannelOutput(t *testing.T) {
t.Parallel()
address := ManagerAddress + ":" + strconv.Itoa(rand.Intn(64511)+1024)
address := ManagerAddress + ":" + strconv.Itoa(rand.IntN(64511)+1024)
manager := NewManager([]string{address}, MaxNeighbours, []string{"dht.tgragnato.it"}, []net.IPNet{})
peerPort := rand.Intn(64511) + 1024
peerPort := rand.IntN(64511) + 1024
result := &TestResult{
infoHash: [20]byte{255},
@ -61,7 +61,7 @@ func TestChannelOutput(t *testing.T) {
func TestOnIndexingResult(t *testing.T) {
t.Parallel()
address := ManagerAddress + ":" + strconv.Itoa(rand.Intn(64511)+1024)
address := ManagerAddress + ":" + strconv.Itoa(rand.IntN(64511)+1024)
manager := NewManager([]string{address}, MaxNeighbours, []string{"dht.tgragnato.it"}, []net.IPNet{})
result := mainline.IndexingResult{}

View File

@ -13,7 +13,7 @@ import (
"io"
"math"
"math/big"
mrand "math/rand"
mrand "math/rand/v2"
)
// The MIT License (MIT)
@ -382,7 +382,7 @@ func privateKey() *big.Int {
_, err := rand.Read(b)
if err != nil {
for i := 0; i < 20; i++ {
b[i] = byte(mrand.Intn(256))
b[i] = byte(mrand.IntN(256))
}
}
var n big.Int
@ -456,7 +456,7 @@ func padRandom() ([]byte, error) {
_, err = rand.Read(b)
if err != nil {
for i := 0; i < len(b); i++ {
b[i] = byte(mrand.Intn(256))
b[i] = byte(mrand.IntN(256))
}
}
return b, nil

View File

@ -6,7 +6,7 @@ import (
"crypto/sha1"
"encoding/binary"
"errors"
mrand "math/rand"
mrand "math/rand/v2"
"time"
"tgragnato.it/magnetico/bencode"
@ -137,7 +137,7 @@ func randomDigit() byte {
b := make([]byte, 1)
_, err := rand.Read(b)
if err != nil {
b[0] = byte(mrand.Intn(256))
b[0] = byte(mrand.IntN(256))
}
return (b[0] % 10) + '0'
}

View File

@ -5,7 +5,7 @@ import (
"crypto/rand"
"crypto/sha1"
"math"
mrand "math/rand"
mrand "math/rand/v2"
"reflect"
"testing"
"time"
@ -17,7 +17,7 @@ import (
func TestTotalSize(t *testing.T) {
t.Parallel()
positiveRand := mrand.Int63n(math.MaxInt64)
positiveRand := mrand.Int64N(math.MaxInt64)
tests := []struct {
name string
@ -35,7 +35,7 @@ func TestTotalSize(t *testing.T) {
name: "Negative size",
files: []persistence.File{
{
Size: -mrand.Int63n(math.MaxInt64),
Size: -mrand.Int64N(math.MaxInt64),
Path: "",
},
},
@ -272,7 +272,7 @@ func TestUnmarshalMetainfo(t *testing.T) {
metadataBytes := make([]byte, 100)
if _, err := rand.Read(metadataBytes); err != nil {
for i := 0; i < 100; i++ {
metadataBytes[i] = byte(mrand.Intn(256))
metadataBytes[i] = byte(mrand.IntN(256))
}
}

View File

@ -5,7 +5,7 @@ import (
"crypto/rand"
"crypto/sha1"
"fmt"
mrand "math/rand"
mrand "math/rand/v2"
"reflect"
"testing"
"text/template"
@ -69,7 +69,7 @@ func TestDoesTorrentExist(t *testing.T) {
_, err = rand.Read(random[:])
if err != nil {
for i := 0; i < 100; i++ {
random[i] = byte(mrand.Intn(256))
random[i] = byte(mrand.IntN(256))
}
}
infohash := sha1.Sum(random[:])
@ -374,7 +374,7 @@ func TestPostgresDatabase_GetTorrent(t *testing.T) {
_, err = rand.Read(random[:])
if err != nil {
for i := 0; i < 100; i++ {
random[i] = byte(mrand.Intn(256))
random[i] = byte(mrand.IntN(256))
}
}
infohash := sha1.Sum(random[:])
@ -447,7 +447,7 @@ func TestPostgresDatabase_GetFiles(t *testing.T) {
_, err = rand.Read(random[:])
if err != nil {
for i := 0; i < 100; i++ {
random[i] = byte(mrand.Intn(256))
random[i] = byte(mrand.IntN(256))
}
}
infohash := sha1.Sum(random[:])