random: make use or random package in tests

Also implement Bytes/Fill routines for generating byte slices.
This commit is contained in:
Evgenii Stratonikov 2020-03-27 10:14:40 +03:00
parent 9abda40171
commit 0036b3e52b
8 changed files with 49 additions and 68 deletions

View file

@ -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))

View file

@ -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[:])

View file

@ -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,

View file

@ -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())

View file

@ -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
}

View file

@ -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()

View file

@ -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)

View file

@ -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
}