forked from TrueCloudLab/neoneo-go
random: make use or random package in tests
Also implement Bytes/Fill routines for generating byte slices.
This commit is contained in:
parent
9abda40171
commit
0036b3e52b
8 changed files with 49 additions and 68 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/nspcc-dev/dbft/payload"
|
"github.com/nspcc-dev/dbft/payload"
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/internal/random"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -49,7 +50,7 @@ func getDifferentPayloads(t *testing.T, n int) (payloads []Payload) {
|
||||||
payloads = make([]Payload, n)
|
payloads = make([]Payload, n)
|
||||||
for i := range payloads {
|
for i := range payloads {
|
||||||
var sign [signatureSize]byte
|
var sign [signatureSize]byte
|
||||||
fillRandom(t, sign[:])
|
random.Fill(sign[:])
|
||||||
|
|
||||||
payloads[i].SetValidatorIndex(uint16(i))
|
payloads[i].SetValidatorIndex(uint16(i))
|
||||||
payloads[i].SetType(payload.MessageType(commitType))
|
payloads[i].SetType(payload.MessageType(commitType))
|
||||||
|
|
|
@ -3,12 +3,13 @@ package consensus
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/internal/random"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCommit_Setters(t *testing.T) {
|
func TestCommit_Setters(t *testing.T) {
|
||||||
var sign [signatureSize]byte
|
var sign [signatureSize]byte
|
||||||
fillRandom(t, sign[:])
|
random.Fill(sign[:])
|
||||||
|
|
||||||
var c commit
|
var c commit
|
||||||
c.SetSignature(sign[:])
|
c.SetSignature(sign[:])
|
||||||
|
|
|
@ -2,14 +2,13 @@ package consensus
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
gio "io"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/nspcc-dev/dbft/payload"
|
"github.com/nspcc-dev/dbft/payload"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
"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/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/internal/testserdes"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
|
@ -185,13 +184,13 @@ func randomPayload(t *testing.T, mt messageType) *Payload {
|
||||||
version: 1,
|
version: 1,
|
||||||
validatorIndex: 13,
|
validatorIndex: 13,
|
||||||
height: rand.Uint32(),
|
height: rand.Uint32(),
|
||||||
|
prevHash: random.Uint256(),
|
||||||
timestamp: rand.Uint32(),
|
timestamp: rand.Uint32(),
|
||||||
Witness: transaction.Witness{
|
Witness: transaction.Witness{
|
||||||
InvocationScript: fillRandom(t, make([]byte, 3)),
|
InvocationScript: random.Bytes(3),
|
||||||
VerificationScript: []byte{byte(opcode.PUSH0)},
|
VerificationScript: []byte{byte(opcode.PUSH0)},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
fillRandom(t, p.prevHash[:])
|
|
||||||
|
|
||||||
if mt == changeViewType {
|
if mt == changeViewType {
|
||||||
p.payload.(*changeView).newViewNumber = p.ViewNumber() + 1
|
p.payload.(*changeView).newViewNumber = p.ViewNumber() + 1
|
||||||
|
@ -209,12 +208,10 @@ func randomMessage(t *testing.T, mt messageType) io.Serializable {
|
||||||
case prepareRequestType:
|
case prepareRequestType:
|
||||||
return randomPrepareRequest(t)
|
return randomPrepareRequest(t)
|
||||||
case prepareResponseType:
|
case prepareResponseType:
|
||||||
resp := &prepareResponse{}
|
return &prepareResponse{preparationHash: random.Uint256()}
|
||||||
fillRandom(t, resp.preparationHash[:])
|
|
||||||
return resp
|
|
||||||
case commitType:
|
case commitType:
|
||||||
var c commit
|
var c commit
|
||||||
fillRandom(t, c.signature[:])
|
random.Fill(c.signature[:])
|
||||||
return &c
|
return &c
|
||||||
case recoveryRequestType:
|
case recoveryRequestType:
|
||||||
return &recoveryRequest{timestamp: rand.Uint32()}
|
return &recoveryRequest{timestamp: rand.Uint32()}
|
||||||
|
@ -238,9 +235,9 @@ func randomPrepareRequest(t *testing.T) *prepareRequest {
|
||||||
|
|
||||||
req.transactionHashes[0] = req.minerTx.Hash()
|
req.transactionHashes[0] = req.minerTx.Hash()
|
||||||
for i := 1; i < txCount; i++ {
|
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
|
return req
|
||||||
}
|
}
|
||||||
|
@ -254,7 +251,7 @@ func randomRecoveryMessage(t *testing.T) *recoveryMessage {
|
||||||
preparationPayloads: []*preparationCompact{
|
preparationPayloads: []*preparationCompact{
|
||||||
{
|
{
|
||||||
ValidatorIndex: 1,
|
ValidatorIndex: 1,
|
||||||
InvocationScript: fillRandom(t, make([]byte, 10)),
|
InvocationScript: random.Bytes(10),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
commitPayloads: []*commitCompact{
|
commitPayloads: []*commitCompact{
|
||||||
|
@ -262,13 +259,13 @@ func randomRecoveryMessage(t *testing.T) *recoveryMessage {
|
||||||
ViewNumber: 0,
|
ViewNumber: 0,
|
||||||
ValidatorIndex: 1,
|
ValidatorIndex: 1,
|
||||||
Signature: [64]byte{1, 2, 3},
|
Signature: [64]byte{1, 2, 3},
|
||||||
InvocationScript: fillRandom(t, make([]byte, 20)),
|
InvocationScript: random.Bytes(20),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ViewNumber: 0,
|
ViewNumber: 0,
|
||||||
ValidatorIndex: 2,
|
ValidatorIndex: 2,
|
||||||
Signature: [64]byte{11, 3, 4, 98},
|
Signature: [64]byte{11, 3, 4, 98},
|
||||||
InvocationScript: fillRandom(t, make([]byte, 10)),
|
InvocationScript: random.Bytes(10),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
changeViewPayloads: []*changeViewCompact{
|
changeViewPayloads: []*changeViewCompact{
|
||||||
|
@ -276,7 +273,7 @@ func randomRecoveryMessage(t *testing.T) *recoveryMessage {
|
||||||
Timestamp: rand.Uint32(),
|
Timestamp: rand.Uint32(),
|
||||||
ValidatorIndex: 3,
|
ValidatorIndex: 3,
|
||||||
OriginalViewNumber: 3,
|
OriginalViewNumber: 3,
|
||||||
InvocationScript: fillRandom(t, make([]byte, 4)),
|
InvocationScript: random.Bytes(4),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
prepareRequest: &message{
|
prepareRequest: &message{
|
||||||
|
@ -307,14 +304,6 @@ func TestMessageType_String(t *testing.T) {
|
||||||
require.Equal(t, "UNKNOWN(0xff)", messageType(0xff).String())
|
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 {
|
func newMinerTx(nonce uint32) *transaction.Transaction {
|
||||||
return &transaction.Transaction{
|
return &transaction.Transaction{
|
||||||
Type: transaction.MinerType,
|
Type: transaction.MinerType,
|
||||||
|
|
|
@ -3,6 +3,7 @@ package consensus
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/internal/random"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
@ -19,9 +20,7 @@ func TestPrepareRequest_Setters(t *testing.T) {
|
||||||
p.SetNonce(8765)
|
p.SetNonce(8765)
|
||||||
require.EqualValues(t, 8765, p.Nonce())
|
require.EqualValues(t, 8765, p.Nonce())
|
||||||
|
|
||||||
var hashes [2]util.Uint256
|
hashes := [2]util.Uint256{random.Uint256(), random.Uint256()}
|
||||||
fillRandom(t, hashes[0][:])
|
|
||||||
fillRandom(t, hashes[1][:])
|
|
||||||
|
|
||||||
p.SetTransactionHashes(hashes[:])
|
p.SetTransactionHashes(hashes[:])
|
||||||
require.Equal(t, hashes[:], p.TransactionHashes())
|
require.Equal(t, hashes[:], p.TransactionHashes())
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
package state
|
package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
gio "io"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"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/internal/testserdes"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
|
@ -15,10 +15,10 @@ import (
|
||||||
func TestNEP5TransferLog_Append(t *testing.T) {
|
func TestNEP5TransferLog_Append(t *testing.T) {
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
expected := []*NEP5Transfer{
|
expected := []*NEP5Transfer{
|
||||||
randomTransfer(t, r),
|
randomTransfer(r),
|
||||||
randomTransfer(t, r),
|
randomTransfer(r),
|
||||||
randomTransfer(t, r),
|
randomTransfer(r),
|
||||||
randomTransfer(t, r),
|
randomTransfer(r),
|
||||||
}
|
}
|
||||||
|
|
||||||
lg := new(NEP5TransferLog)
|
lg := new(NEP5TransferLog)
|
||||||
|
@ -62,26 +62,18 @@ func TestNEP5Transfer_DecodeBinary(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNEP5TransferSize(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)
|
size := io.GetVarSize(tr)
|
||||||
require.EqualValues(t, NEP5TransferSize, size)
|
require.EqualValues(t, NEP5TransferSize, size)
|
||||||
}
|
}
|
||||||
|
|
||||||
func randomTransfer(t *testing.T, r *rand.Rand) *NEP5Transfer {
|
func randomTransfer(r *rand.Rand) *NEP5Transfer {
|
||||||
tr := &NEP5Transfer{
|
return &NEP5Transfer{
|
||||||
Amount: int64(r.Uint64()),
|
Amount: int64(r.Uint64()),
|
||||||
Block: r.Uint32(),
|
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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,11 +2,10 @@ package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
gio "io"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"testing"
|
"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/internal/testserdes"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -64,10 +63,7 @@ func TestUnclaimedBalances_ForEach(t *testing.T) {
|
||||||
|
|
||||||
func randomUnclaimed(t *testing.T) *UnclaimedBalance {
|
func randomUnclaimed(t *testing.T) *UnclaimedBalance {
|
||||||
b := new(UnclaimedBalance)
|
b := new(UnclaimedBalance)
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
b.Tx = random.Uint256()
|
||||||
_, err := gio.ReadFull(r, b.Tx[:])
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
b.Index = uint16(rand.Uint32())
|
b.Index = uint16(rand.Uint32())
|
||||||
b.Start = rand.Uint32()
|
b.Start = rand.Uint32()
|
||||||
b.End = rand.Uint32()
|
b.End = rand.Uint32()
|
||||||
|
|
|
@ -18,6 +18,20 @@ func String(n int) string {
|
||||||
return string(b)
|
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).
|
// Int returns a random integer in [min,max).
|
||||||
func Int(min, max int) int {
|
func Int(min, max int) int {
|
||||||
return min + rand.Intn(max-min)
|
return min + rand.Intn(max-min)
|
||||||
|
|
|
@ -2,12 +2,10 @@ package context
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"io"
|
|
||||||
"math/rand"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
"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/internal/testserdes"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
|
@ -46,22 +44,13 @@ func TestContextItem_MarshalJSON(t *testing.T) {
|
||||||
Script: util.Uint160{1, 2, 3},
|
Script: util.Uint160{1, 2, 3},
|
||||||
Parameters: []smartcontract.Parameter{{
|
Parameters: []smartcontract.Parameter{{
|
||||||
Type: smartcontract.SignatureType,
|
Type: smartcontract.SignatureType,
|
||||||
Value: getRandomSlice(t, 64),
|
Value: random.Bytes(64),
|
||||||
}},
|
}},
|
||||||
Signatures: map[string][]byte{
|
Signatures: map[string][]byte{
|
||||||
hex.EncodeToString(priv1.PublicKey().Bytes()): getRandomSlice(t, 64),
|
hex.EncodeToString(priv1.PublicKey().Bytes()): random.Bytes(64),
|
||||||
hex.EncodeToString(priv2.PublicKey().Bytes()): getRandomSlice(t, 64),
|
hex.EncodeToString(priv2.PublicKey().Bytes()): random.Bytes(64),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
testserdes.MarshalUnmarshalJSON(t, expected, new(Item))
|
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
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue