From 0036b3e52b93b0f30aad9b1742107821d6633e93 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 27 Mar 2020 10:14:40 +0300 Subject: [PATCH] random: make use or random package in tests Also implement Bytes/Fill routines for generating byte slices. --- pkg/consensus/cache_test.go | 3 ++- pkg/consensus/commit_test.go | 3 ++- pkg/consensus/payload_test.go | 33 +++++++++----------------- pkg/consensus/prepare_request_test.go | 5 ++-- pkg/core/state/nep5_test.go | 32 ++++++++++--------------- pkg/core/state/unclaimed_test.go | 8 ++----- pkg/internal/random/random_util.go | 14 +++++++++++ pkg/smartcontract/context/item_test.go | 19 ++++----------- 8 files changed, 49 insertions(+), 68 deletions(-) diff --git a/pkg/consensus/cache_test.go b/pkg/consensus/cache_test.go index 9e1529bfd..700b706eb 100644 --- a/pkg/consensus/cache_test.go +++ b/pkg/consensus/cache_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/nspcc-dev/dbft/payload" + "github.com/nspcc-dev/neo-go/pkg/internal/random" "github.com/stretchr/testify/require" ) @@ -49,7 +50,7 @@ func getDifferentPayloads(t *testing.T, n int) (payloads []Payload) { payloads = make([]Payload, n) for i := range payloads { var sign [signatureSize]byte - fillRandom(t, sign[:]) + random.Fill(sign[:]) payloads[i].SetValidatorIndex(uint16(i)) payloads[i].SetType(payload.MessageType(commitType)) diff --git a/pkg/consensus/commit_test.go b/pkg/consensus/commit_test.go index 0cefb4e2a..b68e2b6d3 100644 --- a/pkg/consensus/commit_test.go +++ b/pkg/consensus/commit_test.go @@ -3,12 +3,13 @@ package consensus import ( "testing" + "github.com/nspcc-dev/neo-go/pkg/internal/random" "github.com/stretchr/testify/require" ) func TestCommit_Setters(t *testing.T) { var sign [signatureSize]byte - fillRandom(t, sign[:]) + random.Fill(sign[:]) var c commit c.SetSignature(sign[:]) diff --git a/pkg/consensus/payload_test.go b/pkg/consensus/payload_test.go index 00510cba2..70ca25fb4 100644 --- a/pkg/consensus/payload_test.go +++ b/pkg/consensus/payload_test.go @@ -2,14 +2,13 @@ package consensus import ( "encoding/hex" - gio "io" "math/rand" "testing" - "time" "github.com/nspcc-dev/dbft/payload" "github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" + "github.com/nspcc-dev/neo-go/pkg/internal/random" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes" "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/util" @@ -185,13 +184,13 @@ func randomPayload(t *testing.T, mt messageType) *Payload { version: 1, validatorIndex: 13, height: rand.Uint32(), + prevHash: random.Uint256(), timestamp: rand.Uint32(), Witness: transaction.Witness{ - InvocationScript: fillRandom(t, make([]byte, 3)), + InvocationScript: random.Bytes(3), VerificationScript: []byte{byte(opcode.PUSH0)}, }, } - fillRandom(t, p.prevHash[:]) if mt == changeViewType { p.payload.(*changeView).newViewNumber = p.ViewNumber() + 1 @@ -209,12 +208,10 @@ func randomMessage(t *testing.T, mt messageType) io.Serializable { case prepareRequestType: return randomPrepareRequest(t) case prepareResponseType: - resp := &prepareResponse{} - fillRandom(t, resp.preparationHash[:]) - return resp + return &prepareResponse{preparationHash: random.Uint256()} case commitType: var c commit - fillRandom(t, c.signature[:]) + random.Fill(c.signature[:]) return &c case recoveryRequestType: return &recoveryRequest{timestamp: rand.Uint32()} @@ -238,9 +235,9 @@ func randomPrepareRequest(t *testing.T) *prepareRequest { req.transactionHashes[0] = req.minerTx.Hash() for i := 1; i < txCount; i++ { - fillRandom(t, req.transactionHashes[i][:]) + req.transactionHashes[i] = random.Uint256() } - fillRandom(t, req.nextConsensus[:]) + req.nextConsensus = random.Uint160() return req } @@ -254,7 +251,7 @@ func randomRecoveryMessage(t *testing.T) *recoveryMessage { preparationPayloads: []*preparationCompact{ { ValidatorIndex: 1, - InvocationScript: fillRandom(t, make([]byte, 10)), + InvocationScript: random.Bytes(10), }, }, commitPayloads: []*commitCompact{ @@ -262,13 +259,13 @@ func randomRecoveryMessage(t *testing.T) *recoveryMessage { ViewNumber: 0, ValidatorIndex: 1, Signature: [64]byte{1, 2, 3}, - InvocationScript: fillRandom(t, make([]byte, 20)), + InvocationScript: random.Bytes(20), }, { ViewNumber: 0, ValidatorIndex: 2, Signature: [64]byte{11, 3, 4, 98}, - InvocationScript: fillRandom(t, make([]byte, 10)), + InvocationScript: random.Bytes(10), }, }, changeViewPayloads: []*changeViewCompact{ @@ -276,7 +273,7 @@ func randomRecoveryMessage(t *testing.T) *recoveryMessage { Timestamp: rand.Uint32(), ValidatorIndex: 3, OriginalViewNumber: 3, - InvocationScript: fillRandom(t, make([]byte, 4)), + InvocationScript: random.Bytes(4), }, }, prepareRequest: &message{ @@ -307,14 +304,6 @@ func TestMessageType_String(t *testing.T) { require.Equal(t, "UNKNOWN(0xff)", messageType(0xff).String()) } -func fillRandom(t *testing.T, buf []byte) []byte { - r := rand.New(rand.NewSource(time.Now().Unix())) - _, err := gio.ReadFull(r, buf) - require.NoError(t, err) - - return buf -} - func newMinerTx(nonce uint32) *transaction.Transaction { return &transaction.Transaction{ Type: transaction.MinerType, diff --git a/pkg/consensus/prepare_request_test.go b/pkg/consensus/prepare_request_test.go index 25d34bf68..d7a3473b9 100644 --- a/pkg/consensus/prepare_request_test.go +++ b/pkg/consensus/prepare_request_test.go @@ -3,6 +3,7 @@ package consensus import ( "testing" + "github.com/nspcc-dev/neo-go/pkg/internal/random" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/stretchr/testify/require" ) @@ -19,9 +20,7 @@ func TestPrepareRequest_Setters(t *testing.T) { p.SetNonce(8765) require.EqualValues(t, 8765, p.Nonce()) - var hashes [2]util.Uint256 - fillRandom(t, hashes[0][:]) - fillRandom(t, hashes[1][:]) + hashes := [2]util.Uint256{random.Uint256(), random.Uint256()} p.SetTransactionHashes(hashes[:]) require.Equal(t, hashes[:], p.TransactionHashes()) diff --git a/pkg/core/state/nep5_test.go b/pkg/core/state/nep5_test.go index a2f3d8495..543d05819 100644 --- a/pkg/core/state/nep5_test.go +++ b/pkg/core/state/nep5_test.go @@ -1,11 +1,11 @@ package state import ( - gio "io" "math/rand" "testing" "time" + "github.com/nspcc-dev/neo-go/pkg/internal/random" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes" "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/util" @@ -15,10 +15,10 @@ import ( func TestNEP5TransferLog_Append(t *testing.T) { r := rand.New(rand.NewSource(time.Now().UnixNano())) expected := []*NEP5Transfer{ - randomTransfer(t, r), - randomTransfer(t, r), - randomTransfer(t, r), - randomTransfer(t, r), + randomTransfer(r), + randomTransfer(r), + randomTransfer(r), + randomTransfer(r), } lg := new(NEP5TransferLog) @@ -62,26 +62,18 @@ func TestNEP5Transfer_DecodeBinary(t *testing.T) { } func TestNEP5TransferSize(t *testing.T) { - tr := randomTransfer(t, rand.New(rand.NewSource(0))) + tr := randomTransfer(rand.New(rand.NewSource(0))) size := io.GetVarSize(tr) require.EqualValues(t, NEP5TransferSize, size) } -func randomTransfer(t *testing.T, r *rand.Rand) *NEP5Transfer { - tr := &NEP5Transfer{ +func randomTransfer(r *rand.Rand) *NEP5Transfer { + return &NEP5Transfer{ Amount: int64(r.Uint64()), Block: r.Uint32(), + Asset: random.Uint160(), + From: random.Uint160(), + To: random.Uint160(), + Tx: random.Uint256(), } - - var err error - _, err = gio.ReadFull(r, tr.Asset[:]) - require.NoError(t, err) - _, err = gio.ReadFull(r, tr.From[:]) - require.NoError(t, err) - _, err = gio.ReadFull(r, tr.To[:]) - require.NoError(t, err) - _, err = gio.ReadFull(r, tr.Tx[:]) - require.NoError(t, err) - - return tr } diff --git a/pkg/core/state/unclaimed_test.go b/pkg/core/state/unclaimed_test.go index 47ec02d73..5972688a6 100644 --- a/pkg/core/state/unclaimed_test.go +++ b/pkg/core/state/unclaimed_test.go @@ -2,11 +2,10 @@ package state import ( "encoding/binary" - gio "io" "math/rand" "testing" - "time" + "github.com/nspcc-dev/neo-go/pkg/internal/random" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/stretchr/testify/require" @@ -64,10 +63,7 @@ func TestUnclaimedBalances_ForEach(t *testing.T) { func randomUnclaimed(t *testing.T) *UnclaimedBalance { b := new(UnclaimedBalance) - r := rand.New(rand.NewSource(time.Now().UnixNano())) - _, err := gio.ReadFull(r, b.Tx[:]) - require.NoError(t, err) - + b.Tx = random.Uint256() b.Index = uint16(rand.Uint32()) b.Start = rand.Uint32() b.End = rand.Uint32() diff --git a/pkg/internal/random/random_util.go b/pkg/internal/random/random_util.go index de602966c..529f6947f 100644 --- a/pkg/internal/random/random_util.go +++ b/pkg/internal/random/random_util.go @@ -18,6 +18,20 @@ func String(n int) string { return string(b) } +// Bytes returns a random byte slice of specified length. +func Bytes(n int) []byte { + b := make([]byte, n) + Fill(b) + return b +} + +// Fill fills buffer with random bytes. +func Fill(buf []byte) { + r := rand.New(rand.NewSource(time.Now().UnixNano())) + // Rand reader returns no errors + r.Read(buf) +} + // Int returns a random integer in [min,max). func Int(min, max int) int { return min + rand.Intn(max-min) diff --git a/pkg/smartcontract/context/item_test.go b/pkg/smartcontract/context/item_test.go index 334427641..c466ed156 100644 --- a/pkg/smartcontract/context/item_test.go +++ b/pkg/smartcontract/context/item_test.go @@ -2,12 +2,10 @@ package context import ( "encoding/hex" - "io" - "math/rand" "testing" - "time" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" + "github.com/nspcc-dev/neo-go/pkg/internal/random" "github.com/nspcc-dev/neo-go/pkg/internal/testserdes" "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/util" @@ -46,22 +44,13 @@ func TestContextItem_MarshalJSON(t *testing.T) { Script: util.Uint160{1, 2, 3}, Parameters: []smartcontract.Parameter{{ Type: smartcontract.SignatureType, - Value: getRandomSlice(t, 64), + Value: random.Bytes(64), }}, Signatures: map[string][]byte{ - hex.EncodeToString(priv1.PublicKey().Bytes()): getRandomSlice(t, 64), - hex.EncodeToString(priv2.PublicKey().Bytes()): getRandomSlice(t, 64), + hex.EncodeToString(priv1.PublicKey().Bytes()): random.Bytes(64), + hex.EncodeToString(priv2.PublicKey().Bytes()): random.Bytes(64), }, } testserdes.MarshalUnmarshalJSON(t, expected, new(Item)) } - -func getRandomSlice(t *testing.T, n int) []byte { - src := rand.NewSource(time.Now().UnixNano()) - r := rand.New(src) - data := make([]byte, n) - _, err := io.ReadFull(r, data) - require.NoError(t, err) - return data -}