From 49ad985cadd2a86955aae557e5ab43f1a1af5e02 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 8 Sep 2023 17:15:08 +0300 Subject: [PATCH] [#161] *: Do not use math/rand.Read() Signed-off-by: Evgenii Stratonikov --- checksum/example_test.go | 2 +- checksum/test/generate.go | 4 ++-- container/id/id_test.go | 2 +- container/id/test/id.go | 4 ++-- crypto/crypto_test.go | 2 +- eacl/test/benchmark_test.go | 2 +- eacl/validator_test.go | 2 +- netmap/selector_test.go | 7 ++++--- netmap/test/generate.go | 4 ++-- ns/nns_test.go | 2 +- object/id/test/generate.go | 4 ++-- object/search_test.go | 2 +- object/tombstone_test.go | 2 +- session/container_test.go | 5 +++-- user/id_test.go | 2 +- 15 files changed, 24 insertions(+), 22 deletions(-) diff --git a/checksum/example_test.go b/checksum/example_test.go index d98c992..337767a 100644 --- a/checksum/example_test.go +++ b/checksum/example_test.go @@ -2,9 +2,9 @@ package checksum import ( "bytes" + "crypto/rand" "crypto/sha256" "fmt" - "math/rand" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs" ) diff --git a/checksum/test/generate.go b/checksum/test/generate.go index d8c6b0b..06c9059 100644 --- a/checksum/test/generate.go +++ b/checksum/test/generate.go @@ -1,8 +1,8 @@ package checksumtest import ( + "crypto/rand" "crypto/sha256" - "math/rand" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/checksum" ) @@ -11,7 +11,7 @@ import ( func Checksum() checksum.Checksum { var cs [sha256.Size]byte - rand.Read(cs[:]) + _, _ = rand.Read(cs[:]) var x checksum.Checksum diff --git a/container/id/id_test.go b/container/id/id_test.go index ad12295..ded7457 100644 --- a/container/id/id_test.go +++ b/container/id/id_test.go @@ -1,8 +1,8 @@ package cid_test import ( + "crypto/rand" "crypto/sha256" - "math/rand" "testing" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs" diff --git a/container/id/test/id.go b/container/id/test/id.go index 2ebcf09..6790b22 100644 --- a/container/id/test/id.go +++ b/container/id/test/id.go @@ -1,8 +1,8 @@ package cidtest import ( + "crypto/rand" "crypto/sha256" - "math/rand" cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id" ) @@ -11,7 +11,7 @@ import ( func ID() cid.ID { checksum := [sha256.Size]byte{} - rand.Read(checksum[:]) + _, _ = rand.Read(checksum[:]) return IDWithChecksum(checksum) } diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go index 095cdd9..7d1ad52 100644 --- a/crypto/crypto_test.go +++ b/crypto/crypto_test.go @@ -1,7 +1,7 @@ package frostfscrypto_test import ( - "math/rand" + "crypto/rand" "testing" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs" diff --git a/eacl/test/benchmark_test.go b/eacl/test/benchmark_test.go index cc2c3d0..865789b 100644 --- a/eacl/test/benchmark_test.go +++ b/eacl/test/benchmark_test.go @@ -2,7 +2,7 @@ package eacltest import ( "bytes" - "math/rand" + "crypto/rand" "testing" cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test" diff --git a/eacl/validator_test.go b/eacl/validator_test.go index 3b8d9f1..582a6bc 100644 --- a/eacl/validator_test.go +++ b/eacl/validator_test.go @@ -1,7 +1,7 @@ package eacl import ( - "math/rand" + "crypto/rand" "testing" "github.com/stretchr/testify/require" diff --git a/netmap/selector_test.go b/netmap/selector_test.go index ccb5eb2..759e7ee 100644 --- a/netmap/selector_test.go +++ b/netmap/selector_test.go @@ -1,9 +1,10 @@ package netmap import ( + "crypto/rand" "encoding/binary" "fmt" - "math/rand" + mrand "math/rand" "sort" "strconv" "testing" @@ -28,10 +29,10 @@ func BenchmarkHRWSort(b *testing.B) { node.SetPublicKey(key) vectors[i] = nodes{node} - weights[i] = float64(rand.Uint32()%10) / 10.0 + weights[i] = float64(mrand.Uint32()%10) / 10.0 } - pivot := rand.Uint64() + pivot := mrand.Uint64() b.Run("sort by index, no weight", func(b *testing.B) { realNodes := make([]nodes, netmapSize) b.ResetTimer() diff --git a/netmap/test/generate.go b/netmap/test/generate.go index 3f5f26b..19c3514 100644 --- a/netmap/test/generate.go +++ b/netmap/test/generate.go @@ -1,7 +1,7 @@ package netmaptest import ( - "math/rand" + "crypto/rand" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap" ) @@ -70,7 +70,7 @@ func NetworkInfo() (x netmap.NetworkInfo) { // NodeInfo returns random netmap.NodeInfo. func NodeInfo() (x netmap.NodeInfo) { key := make([]byte, 33) - rand.Read(key) + _, _ = rand.Read(key) x.SetPublicKey(key) x.SetNetworkEndpoints("1", "2", "3") diff --git a/ns/nns_test.go b/ns/nns_test.go index 9970b88..669f4af 100644 --- a/ns/nns_test.go +++ b/ns/nns_test.go @@ -1,10 +1,10 @@ package ns import ( + "crypto/rand" "errors" "fmt" "math/big" - "math/rand" "strings" "testing" diff --git a/object/id/test/generate.go b/object/id/test/generate.go index e51405d..0da4119 100644 --- a/object/id/test/generate.go +++ b/object/id/test/generate.go @@ -1,8 +1,8 @@ package oidtest import ( + "crypto/rand" "crypto/sha256" - "math/rand" cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test" oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id" @@ -12,7 +12,7 @@ import ( func ID() oid.ID { checksum := [sha256.Size]byte{} - rand.Read(checksum[:]) + _, _ = rand.Read(checksum[:]) return idWithChecksum(checksum) } diff --git a/object/search_test.go b/object/search_test.go index d0f36de..40df23b 100644 --- a/object/search_test.go +++ b/object/search_test.go @@ -1,8 +1,8 @@ package object_test import ( + "crypto/rand" "crypto/sha256" - "math/rand" "testing" v2object "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object" diff --git a/object/tombstone_test.go b/object/tombstone_test.go index cd74e45..9825133 100644 --- a/object/tombstone_test.go +++ b/object/tombstone_test.go @@ -1,8 +1,8 @@ package object import ( + "crypto/rand" "crypto/sha256" - "math/rand" "testing" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/tombstone" diff --git a/session/container_test.go b/session/container_test.go index a152e31..d3d4662 100644 --- a/session/container_test.go +++ b/session/container_test.go @@ -1,9 +1,10 @@ package session_test import ( + "crypto/rand" "fmt" "math" - "math/rand" + mrand "math/rand" "testing" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs" @@ -396,7 +397,7 @@ func TestContainer_AppliedTo(t *testing.T) { func TestContainer_InvalidAt(t *testing.T) { var x session.Container - nbf := rand.Uint64() + nbf := mrand.Uint64() if nbf == math.MaxUint64 { nbf-- } diff --git a/user/id_test.go b/user/id_test.go index 27d93dc..30e19db 100644 --- a/user/id_test.go +++ b/user/id_test.go @@ -1,7 +1,7 @@ package user_test import ( - "math/rand" + "crypto/rand" "testing" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"