testserdes: implement helpers for encode/decode routines

Frequently one needs to check if struct serializes/deserializes
properly. This commit implements helpers for such cases including:
1. JSON
2. io.Serializable interface
This commit is contained in:
Evgenii Stratonikov 2020-03-26 17:43:24 +03:00
parent d4622768d1
commit 9abda40171
36 changed files with 201 additions and 476 deletions

View file

@ -10,6 +10,7 @@ import (
"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/testserdes"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
@ -78,19 +79,15 @@ func TestConsensusPayload_Hash(t *testing.T) {
data, err := hex.DecodeString(dataHex)
require.NoError(t, err)
r := io.NewBinReaderFromBuf(data)
var p Payload
p.DecodeBinary(r)
require.NoError(t, err)
require.NoError(t, testserdes.DecodeBinary(data, &p))
require.Equal(t, p.Hash().String(), "45859759c8491597804f1922773947e0d37bf54484af82f80cd642f7b063aa56")
}
func TestConsensusPayload_Serializable(t *testing.T) {
for _, mt := range messageTypes {
p := randomPayload(t, mt)
testSerializable(t, p, new(Payload))
testserdes.EncodeDecodeBinary(t, p, new(Payload))
data := p.MarshalUnsigned()
pu := new(Payload)
@ -133,57 +130,49 @@ func TestConsensusPayload_DecodeBinaryInvalid(t *testing.T) {
buf[delimeterIndex] = 1
buf[lenIndex] = 34
buf[typeIndex] = byte(prepareResponseType)
r := io.NewBinReaderFromBuf(buf)
p := new(Payload)
p.DecodeBinary(r)
require.NoError(t, r.Err)
require.NoError(t, testserdes.DecodeBinary(buf, p))
require.Equal(t, expected, p)
// invalid type
buf[typeIndex] = 0xFF
r = io.NewBinReaderFromBuf(buf)
new(Payload).DecodeBinary(r)
require.Error(t, r.Err)
require.Error(t, testserdes.DecodeBinary(buf, new(Payload)))
// invalid format
buf[delimeterIndex] = 0
buf[typeIndex] = byte(prepareResponseType)
r = io.NewBinReaderFromBuf(buf)
new(Payload).DecodeBinary(r)
require.Error(t, r.Err)
require.Error(t, testserdes.DecodeBinary(buf, new(Payload)))
// invalid message length
buf[delimeterIndex] = 1
buf[lenIndex] = 0xFF
buf[typeIndex] = byte(prepareResponseType)
r = io.NewBinReaderFromBuf(buf)
new(Payload).DecodeBinary(r)
require.Error(t, r.Err)
require.Error(t, testserdes.DecodeBinary(buf, new(Payload)))
}
func TestCommit_Serializable(t *testing.T) {
c := randomMessage(t, commitType)
testSerializable(t, c, new(commit))
testserdes.EncodeDecodeBinary(t, c, new(commit))
}
func TestPrepareResponse_Serializable(t *testing.T) {
resp := randomMessage(t, prepareResponseType)
testSerializable(t, resp, new(prepareResponse))
testserdes.EncodeDecodeBinary(t, resp, new(prepareResponse))
}
func TestPrepareRequest_Serializable(t *testing.T) {
req := randomMessage(t, prepareRequestType)
testSerializable(t, req, new(prepareRequest))
testserdes.EncodeDecodeBinary(t, req, new(prepareRequest))
}
func TestRecoveryRequest_Serializable(t *testing.T) {
req := randomMessage(t, recoveryRequestType)
testSerializable(t, req, new(recoveryRequest))
testserdes.EncodeDecodeBinary(t, req, new(recoveryRequest))
}
func TestRecoveryMessage_Serializable(t *testing.T) {
msg := randomMessage(t, recoveryMessageType)
testSerializable(t, msg, new(recoveryMessage))
testserdes.EncodeDecodeBinary(t, msg, new(recoveryMessage))
}
func randomPayload(t *testing.T, mt messageType) *Payload {
@ -318,16 +307,6 @@ func TestMessageType_String(t *testing.T) {
require.Equal(t, "UNKNOWN(0xff)", messageType(0xff).String())
}
func testSerializable(t *testing.T, expected, actual io.Serializable) {
w := io.NewBufBinWriter()
expected.EncodeBinary(w.BinWriter)
r := io.NewBinReaderFromBuf(w.Bytes())
actual.DecodeBinary(r)
require.Equal(t, expected, actual)
}
func fillRandom(t *testing.T, buf []byte) []byte {
r := rand.New(rand.NewSource(time.Now().Unix()))
_, err := gio.ReadFull(r, buf)

View file

@ -7,6 +7,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -22,9 +23,7 @@ func TestDecodeBlock1(t *testing.T) {
require.NoError(t, err)
block := &Block{}
r := io.NewBinReaderFromBuf(b)
block.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.NoError(t, testserdes.DecodeBinary(b, block))
assert.Equal(t, uint32(data["index"].(float64)), block.Index)
assert.Equal(t, uint32(data["version"].(float64)), block.Version)
@ -132,9 +131,7 @@ func TestBinBlockDecodeEncode(t *testing.T) {
b := Block{}
r := io.NewBinReaderFromBuf(rawtxBytes)
b.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.NoError(t, testserdes.DecodeBinary(rawtxBytes, &b))
expected := map[string]bool{ // 18 trans
"009f61f481f47eb7478e887871e4e744669d461b13d68e04250035260171d706": false,
@ -188,12 +185,9 @@ func TestBinBlockDecodeEncode(t *testing.T) {
}
assert.Equal(t, true, val)
buf := io.NewBufBinWriter()
b.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
assert.Equal(t, rawtx, hex.EncodeToString(buf.Bytes()))
data, err := testserdes.EncodeBinary(&b)
assert.NoError(t, err)
assert.Equal(t, rawtx, hex.EncodeToString(data))
}
func TestBlockSizeCalculation(t *testing.T) {
@ -207,10 +201,7 @@ func TestBlockSizeCalculation(t *testing.T) {
rawBlockBytes, _ := hex.DecodeString(rawBlock)
b := Block{}
r := io.NewBinReaderFromBuf(rawBlockBytes)
b.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.NoError(t, testserdes.DecodeBinary(rawBlockBytes, &b))
expected := []struct {
ID string
@ -273,11 +264,8 @@ func TestBlockSizeCalculation(t *testing.T) {
assert.Equal(t, "552102486fd15702c4490a26703112a5cc1d0923fd697a33406bd5a1c00e0013b09a7021024c7b7fb6c310fccf1ba33b082519d82964ea93868d676662d4a59ad548df0e7d2102aaec38470f6aad0042c6e877cfd8087d2676b0f516fddd362801b9bd3936399e2103b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c2103b8d9d5771d8f513aa0869b9cc8d50986403b78c6da36890638c3d46a5adce04a2102ca0e27697b9c248f6f16e085fd0061e26f44da85b58ee835c110caa5ec3ba5542102df48f60e8f3e01c48ff40b9b7f1310d7a8b2a193188befe1c2e3df740e89509357ae", hex.EncodeToString(b.Script.VerificationScript))
assert.Equal(t, "0006d3ff96e269f599eb1b5c5a527c218439e498dcc65b63794591bbcdc0516b", b.Hash().StringLE())
buf := io.NewBufBinWriter()
b.EncodeBinary(buf.BinWriter)
assert.Nil(t, r.Err)
benc := buf.Bytes()
benc, err := testserdes.EncodeBinary(&b)
assert.NoError(t, err)
// test size of the block
assert.Equal(t, 7360, len(benc))
assert.Equal(t, rawBlock, hex.EncodeToString(benc))

View file

@ -6,7 +6,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
)
@ -26,14 +26,10 @@ func TestHeaderEncodeDecode(t *testing.T) {
},
}}
buf := io.NewBufBinWriter()
header.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
_ = header.Hash()
headerDecode := &Header{}
r := io.NewBinReaderFromBuf(buf.Bytes())
headerDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
testserdes.EncodeDecodeBinary(t, &header, headerDecode)
assert.Equal(t, header.Version, headerDecode.Version, "expected both versions to be equal")
assert.Equal(t, header.PrevHash, headerDecode.PrevHash, "expected both prev hashes to be equal")
assert.Equal(t, header.MerkleRoot, headerDecode.MerkleRoot, "expected both merkle roots to be equal")

View file

@ -7,7 +7,7 @@ import (
"io/ioutil"
"testing"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/require"
)
@ -19,9 +19,7 @@ func getDecodedBlock(t *testing.T, i int) *Block {
require.NoError(t, err)
block := &Block{}
r := io.NewBinReaderFromBuf(b)
block.DecodeBinary(r)
require.NoError(t, r.Err)
require.NoError(t, testserdes.DecodeBinary(b, block))
return block
}

View file

@ -15,6 +15,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util"
@ -119,9 +120,7 @@ func getDecodedBlock(t *testing.T, i int) *block.Block {
require.NoError(t, err)
block := &block.Block{}
r := io.NewBinReaderFromBuf(b)
block.DecodeBinary(r)
require.NoError(t, r.Err)
require.NoError(t, testserdes.DecodeBinary(b, block))
return block
}
@ -383,9 +382,8 @@ func TestCreateBasicChain(t *testing.T) {
bh := bc.GetHeaderHash(i)
b, err := bc.GetBlock(bh)
require.NoError(t, err)
buf := io.NewBufBinWriter()
b.EncodeBinary(buf.BinWriter)
bytes := buf.Bytes()
bytes, err := testserdes.EncodeBinary(b)
require.NoError(t, err)
writer.WriteU32LE(uint32(len(bytes)))
writer.WriteBytes(bytes)
require.NoError(t, writer.Err)
@ -397,10 +395,9 @@ func TestCreateBasicChain(t *testing.T) {
var blocks []*block.Block
blocks = append(blocks, bc.newBlock(), bc.newBlock(newMinerTX()))
for _, b := range blocks {
buf := io.NewBufBinWriter()
b.EncodeBinary(buf.BinWriter)
require.NoError(t, buf.Err)
t.Log(hex.EncodeToString(buf.Bytes()))
data, err := testserdes.EncodeBinary(b)
require.NoError(t, err)
t.Log(hex.EncodeToString(data))
}
}

View file

@ -5,7 +5,7 @@ import (
"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/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
)
@ -36,25 +36,10 @@ func TestDecodeEncodeAccountState(t *testing.T) {
IsFrozen: true,
Votes: votes,
Balances: balances,
Unclaimed: UnclaimedBalances{Raw: []byte{}},
}
buf := io.NewBufBinWriter()
a.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
aDecode := &Account{}
r := io.NewBinReaderFromBuf(buf.Bytes())
aDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, a.Version, aDecode.Version)
assert.Equal(t, a.ScriptHash, aDecode.ScriptHash)
assert.Equal(t, a.IsFrozen, aDecode.IsFrozen)
for i, vote := range a.Votes {
assert.Equal(t, vote.X, aDecode.Votes[i].X)
}
assert.Equal(t, a.Balances, aDecode.Balances)
testserdes.EncodeDecodeBinary(t, a, new(Account))
}
func TestAccountStateBalanceValues(t *testing.T) {

View file

@ -6,7 +6,7 @@ import (
"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/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
)
@ -27,14 +27,7 @@ func TestEncodeDecodeAssetState(t *testing.T) {
IsFrozen: false,
}
buf := io.NewBufBinWriter()
asset.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
assetDecode := &Asset{}
r := io.NewBinReaderFromBuf(buf.Bytes())
assetDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, asset, assetDecode)
testserdes.EncodeDecodeBinary(t, asset, new(Asset))
}
func TestAssetState_GetName_NEO(t *testing.T) {

View file

@ -4,7 +4,7 @@ import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/stretchr/testify/assert"
)
@ -25,14 +25,9 @@ func TestEncodeDecodeContractState(t *testing.T) {
}
assert.Equal(t, hash.Hash160(script), contract.ScriptHash())
buf := io.NewBufBinWriter()
contract.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
contractDecoded := &Contract{}
r := io.NewBinReaderFromBuf(buf.Bytes())
contractDecoded.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, contract, contractDecoded)
testserdes.EncodeDecodeBinary(t, contract, contractDecoded)
assert.Equal(t, contract.ScriptHash(), contractDecoded.ScriptHash())
}

View file

@ -6,6 +6,7 @@ import (
"testing"
"time"
"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"
"github.com/stretchr/testify/require"
@ -43,7 +44,7 @@ func TestNEP5Tracker_EncodeBinary(t *testing.T) {
LastUpdatedBlock: rand.Uint32(),
}
testEncodeDecode(t, expected, new(NEP5Tracker))
testserdes.EncodeDecodeBinary(t, expected, new(NEP5Tracker))
}
func TestNEP5Transfer_DecodeBinary(t *testing.T) {
@ -57,7 +58,7 @@ func TestNEP5Transfer_DecodeBinary(t *testing.T) {
Tx: util.Uint256{8, 5, 3},
}
testEncodeDecode(t, expected, new(NEP5Transfer))
testserdes.EncodeDecodeBinary(t, expected, new(NEP5Transfer))
}
func TestNEP5TransferSize(t *testing.T) {
@ -84,14 +85,3 @@ func randomTransfer(t *testing.T, r *rand.Rand) *NEP5Transfer {
return tr
}
func testEncodeDecode(t *testing.T, expected, actual io.Serializable) {
w := io.NewBufBinWriter()
expected.EncodeBinary(w.BinWriter)
require.NoError(t, w.Err)
r := io.NewBinReaderFromBuf(w.Bytes())
actual.DecodeBinary(r)
require.NoError(t, r.Err)
require.Equal(t, expected, actual)
}

View file

@ -4,25 +4,18 @@ import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/internal/random"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/stretchr/testify/assert"
"github.com/nspcc-dev/neo-go/pkg/vm"
)
func TestEncodeDecodeNotificationEvent(t *testing.T) {
event := &NotificationEvent{
ScriptHash: random.Uint160(),
Item: nil,
Item: vm.NewBoolItem(true),
}
buf := io.NewBufBinWriter()
event.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
eventDecoded := &NotificationEvent{}
reader := io.NewBinReaderFromBuf(buf.Bytes())
eventDecoded.DecodeBinary(reader)
assert.Equal(t, event, eventDecoded)
testserdes.EncodeDecodeBinary(t, event, new(NotificationEvent))
}
func TestEncodeDecodeAppExecResult(t *testing.T) {
@ -34,12 +27,6 @@ func TestEncodeDecodeAppExecResult(t *testing.T) {
Stack: []smartcontract.Parameter{},
Events: []NotificationEvent{},
}
buf := io.NewBufBinWriter()
appExecResult.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
appExecResultDecoded := &AppExecResult{}
reader := io.NewBinReaderFromBuf(buf.Bytes())
appExecResultDecoded.DecodeBinary(reader)
assert.Equal(t, appExecResult, appExecResultDecoded)
testserdes.EncodeDecodeBinary(t, appExecResult, new(AppExecResult))
}

View file

@ -3,9 +3,7 @@ package state
import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
)
func TestEncodeDecodeStorageItem(t *testing.T) {
@ -13,14 +11,6 @@ func TestEncodeDecodeStorageItem(t *testing.T) {
Value: []byte{},
IsConst: false,
}
buf := io.NewBufBinWriter()
storageItem.EncodeBinary(buf.BinWriter)
require.NoError(t, buf.Err)
decodedStorageItem := &StorageItem{}
r := io.NewBinReaderFromBuf(buf.Bytes())
decodedStorageItem.DecodeBinary(r)
require.NoError(t, r.Err)
assert.Equal(t, storageItem, decodedStorageItem)
testserdes.EncodeDecodeBinary(t, storageItem, new(StorageItem))
}

View file

@ -7,18 +7,15 @@ import (
"testing"
"time"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/require"
)
func TestUnclaimedBalance_Structure(t *testing.T) {
b := randomUnclaimed(t)
w := io.NewBufBinWriter()
b.EncodeBinary(w.BinWriter)
require.NoError(t, w.Err)
buf := w.Bytes()
buf, err := testserdes.EncodeBinary(b)
require.NoError(t, err)
require.Equal(t, UnclaimedBalanceSize, len(buf))
require.Equal(t, b.Tx.BytesBE(), buf[:util.Uint256Size])
require.Equal(t, b.Index, binary.LittleEndian.Uint16(buf[util.Uint256Size:]))

View file

@ -5,9 +5,8 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/internal/random"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
)
func TestDecodeEncodeUnspentCoin(t *testing.T) {
@ -44,11 +43,5 @@ func TestDecodeEncodeUnspentCoin(t *testing.T) {
},
}
buf := io.NewBufBinWriter()
unspent.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
unspentDecode := &UnspentCoin{}
r := io.NewBinReaderFromBuf(buf.Bytes())
unspentDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
testserdes.EncodeDecodeBinary(t, unspent, new(UnspentCoin))
}

View file

@ -5,7 +5,7 @@ import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/require"
)
@ -16,15 +16,8 @@ func TestValidatorState_DecodeEncodeBinary(t *testing.T) {
Registered: false,
Votes: util.Fixed8(10),
}
buf := io.NewBufBinWriter()
state.EncodeBinary(buf.BinWriter)
require.NoError(t, buf.Err)
decodedState := &Validator{}
reader := io.NewBinReaderFromBuf(buf.Bytes())
decodedState.DecodeBinary(reader)
require.NoError(t, reader.Err)
require.Equal(t, state, decodedState)
testserdes.EncodeDecodeBinary(t, state, new(Validator))
}
func TestRegisteredAndHasVotes_Registered(t *testing.T) {

View file

@ -4,7 +4,7 @@ import (
"encoding/hex"
"testing"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert"
)
@ -30,9 +30,7 @@ func TestEncodeDecodeContract(t *testing.T) {
assert.Equal(t, "bdf6cc3b9af12a7565bda80933a75ee8cef1bc771d0d58effc08e4c8b436da79", tx.Hash().StringLE())
// Encode
buf := io.NewBufBinWriter()
tx.EncodeBinary(buf.BinWriter)
assert.Equal(t, nil, buf.Err)
assert.Equal(t, rawtx, hex.EncodeToString(buf.Bytes()))
data, err := testserdes.EncodeBinary(tx)
assert.NoError(t, err)
assert.Equal(t, rawtx, hex.EncodeToString(data))
}

View file

@ -4,7 +4,7 @@ import (
"encoding/hex"
"testing"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert"
)
@ -16,9 +16,7 @@ func TestEncodeDecodeEnrollment(t *testing.T) {
assert.IsType(t, tx.Data, &EnrollmentTX{})
assert.Equal(t, 0, int(tx.Version))
buf := io.NewBufBinWriter()
tx.EncodeBinary(buf.BinWriter)
assert.Equal(t, nil, buf.Err)
assert.Equal(t, rawtx, hex.EncodeToString(buf.Bytes()))
data, err := testserdes.EncodeBinary(tx)
assert.Equal(t, nil, err)
assert.Equal(t, rawtx, hex.EncodeToString(data))
}

View file

@ -4,7 +4,7 @@ import (
"encoding/hex"
"testing"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert"
)
@ -21,8 +21,6 @@ func decodeTransaction(rawTX string, t *testing.T) *Transaction {
b, err1 := hex.DecodeString(rawTX)
assert.Nil(t, err1)
tx := &Transaction{}
r := io.NewBinReaderFromBuf(b)
tx.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.NoError(t, testserdes.DecodeBinary(b, tx))
return tx
}

View file

@ -4,7 +4,7 @@ import (
"encoding/hex"
"testing"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -16,18 +16,13 @@ func TestInvocationZeroScript(t *testing.T) {
require.NoError(t, err)
inv := &InvocationTX{Version: 1}
r := io.NewBinReaderFromBuf(in)
inv.DecodeBinary(r)
assert.Error(t, r.Err)
assert.Error(t, testserdes.DecodeBinary(in, inv))
// PUSH1 script.
in, err = hex.DecodeString("01510000000000000000")
require.NoError(t, err)
r = io.NewBinReaderFromBuf(in)
inv.DecodeBinary(r)
assert.NoError(t, r.Err)
assert.NoError(t, testserdes.DecodeBinary(in, inv))
}
func TestInvocationNegativeGas(t *testing.T) {
@ -36,18 +31,13 @@ func TestInvocationNegativeGas(t *testing.T) {
require.NoError(t, err)
inv := &InvocationTX{Version: 1}
r := io.NewBinReaderFromBuf(in)
inv.DecodeBinary(r)
assert.Error(t, r.Err)
assert.Error(t, testserdes.DecodeBinary(in, inv))
// Positive GAS.
in, err = hex.DecodeString("01510100000000000000")
require.NoError(t, err)
r = io.NewBinReaderFromBuf(in)
inv.DecodeBinary(r)
assert.NoError(t, r.Err)
assert.NoError(t, testserdes.DecodeBinary(in, inv))
assert.Equal(t, util.Fixed8(1), inv.Gas)
}
@ -56,15 +46,9 @@ func TestInvocationVersionZero(t *testing.T) {
require.NoError(t, err)
inv := &InvocationTX{Version: 1}
r := io.NewBinReaderFromBuf(in)
inv.DecodeBinary(r)
assert.Error(t, r.Err)
assert.Error(t, testserdes.DecodeBinary(in, inv))
inv = &InvocationTX{Version: 0}
r = io.NewBinReaderFromBuf(in)
inv.DecodeBinary(r)
assert.NoError(t, r.Err)
assert.NoError(t, testserdes.DecodeBinary(in, inv))
assert.Equal(t, util.Fixed8(0), inv.Gas)
}

View file

@ -4,7 +4,7 @@ import (
"encoding/hex"
"testing"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert"
)
@ -21,10 +21,7 @@ func TestEncodeDecodeMiner(t *testing.T) {
assert.Equal(t, "a1f219dc6be4c35eca172e65e02d4591045220221b1543f1a4b67b9e9442c264", tx.Hash().StringLE())
// Encode
buf := io.NewBufBinWriter()
tx.EncodeBinary(buf.BinWriter)
assert.Equal(t, nil, buf.Err)
assert.Equal(t, rawtx, hex.EncodeToString(buf.Bytes()))
data, err := testserdes.EncodeBinary(tx)
assert.NoError(t, err)
assert.Equal(t, rawtx, hex.EncodeToString(data))
}

View file

@ -6,7 +6,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -24,21 +24,14 @@ func TestRegisterTX(t *testing.T) {
Precision: 8,
Admin: someuint160,
},
Attributes: []Attribute{},
Inputs: []Input{},
Outputs: []Output{},
Scripts: []Witness{},
}
_ = tx.Hash()
buf := io.NewBufBinWriter()
tx.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
b := buf.Bytes()
txDecode := &Transaction{}
r := io.NewBinReaderFromBuf(b)
txDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
txData := tx.Data.(*RegisterTX)
txDecodeData := txDecode.Data.(*RegisterTX)
assert.Equal(t, txData, txDecodeData)
assert.Equal(t, tx.Hash(), txDecode.Hash())
testserdes.EncodeDecodeBinary(t, tx, new(Transaction))
}
func TestDecodeRegisterTXFromRawString(t *testing.T) {
@ -47,9 +40,7 @@ func TestDecodeRegisterTXFromRawString(t *testing.T) {
require.NoError(t, err)
tx := &Transaction{}
r := io.NewBinReaderFromBuf(b)
tx.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.NoError(t, testserdes.DecodeBinary(b, tx))
assert.Equal(t, RegisterType, tx.Type)
txData := tx.Data.(*RegisterTX)
assert.Equal(t, GoverningToken, txData.AssetType)
@ -60,14 +51,5 @@ func TestDecodeRegisterTXFromRawString(t *testing.T) {
assert.Equal(t, "Abf2qMs1pzQb8kYk9RuxtUb9jtRKJVuBJt", address.Uint160ToString(txData.Admin))
assert.Equal(t, "c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b", tx.Hash().StringLE())
buf := io.NewBufBinWriter()
tx.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
benc := buf.Bytes()
txDecode := &Transaction{}
encreader := io.NewBinReaderFromBuf(benc)
txDecode.DecodeBinary(encreader)
assert.Nil(t, encreader.Err)
assert.Equal(t, tx, txDecode)
testserdes.EncodeDecodeBinary(t, tx, new(Transaction))
}

View file

@ -4,7 +4,7 @@ import (
"encoding/hex"
"testing"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert"
)
@ -30,9 +30,7 @@ func TestEncodeDecodeState(t *testing.T) {
assert.Equal(t, Validator, descriptor.Type)
// Encode
buf := io.NewBufBinWriter()
tx.EncodeBinary(buf.BinWriter)
assert.Equal(t, nil, buf.Err)
assert.Equal(t, rawtx, hex.EncodeToString(buf.Bytes()))
data, err := testserdes.EncodeBinary(tx)
assert.NoError(t, err)
assert.Equal(t, rawtx, hex.EncodeToString(data))
}

View file

@ -2,11 +2,10 @@ package transaction
import (
"encoding/hex"
"encoding/json"
"testing"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/io"
"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"
"github.com/stretchr/testify/assert"
@ -29,19 +28,11 @@ func TestWitnessEncodeDecode(t *testing.T) {
VerificationScript: verif,
}
buf := io.NewBufBinWriter()
wit.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
benc := buf.Bytes()
witDecode := &Witness{}
encreader := io.NewBinReaderFromBuf(benc)
witDecode.DecodeBinary(encreader)
assert.Nil(t, encreader.Err)
testserdes.EncodeDecodeBinary(t, wit, witDecode)
t.Log(len(witDecode.VerificationScript))
t.Log(len(witDecode.InvocationScript))
assert.Equal(t, wit, witDecode)
}
func TestDecodeEncodeClaimTX(t *testing.T) {
@ -62,10 +53,9 @@ func TestDecodeEncodeClaimTX(t *testing.T) {
assert.Equal(t, invoc, hex.EncodeToString(tx.Scripts[0].InvocationScript))
assert.Equal(t, verif, hex.EncodeToString(tx.Scripts[0].VerificationScript))
buf := io.NewBufBinWriter()
tx.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
assert.Equal(t, rawClaimTX, hex.EncodeToString(buf.Bytes()))
data, err := testserdes.EncodeBinary(tx)
assert.NoError(t, err)
assert.Equal(t, rawClaimTX, hex.EncodeToString(data))
hash := "2c6a45547b3898318e400e541628990a07acb00f3b9a15a8e966ae49525304da"
assert.Equal(t, hash, tx.hash.StringLE())
@ -90,11 +80,9 @@ func TestDecodeEncodeInvocationTX(t *testing.T) {
assert.Equal(t, invoc, hex.EncodeToString(tx.Scripts[0].InvocationScript))
assert.Equal(t, verif, hex.EncodeToString(tx.Scripts[0].VerificationScript))
buf := io.NewBufBinWriter()
tx.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
assert.Equal(t, rawInvocationTX, hex.EncodeToString(buf.Bytes()))
data, err := testserdes.EncodeBinary(tx)
assert.NoError(t, err)
assert.Equal(t, rawInvocationTX, hex.EncodeToString(data))
}
func TestNewInvocationTX(t *testing.T) {
@ -104,16 +92,9 @@ func TestNewInvocationTX(t *testing.T) {
assert.Equal(t, InvocationType, tx.Type)
assert.Equal(t, tx.Version, txData.Version)
assert.Equal(t, script, txData.Script)
buf := io.NewBufBinWriter()
// Update hash fields to match tx2 that is gonna autoupdate them on decode.
_ = tx.Hash()
tx.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
var tx2 Transaction
r := io.NewBinReaderFromBuf(buf.Bytes())
tx2.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, *tx, tx2)
testserdes.EncodeDecodeBinary(t, tx, new(Transaction))
}
func TestDecodePublishTX(t *testing.T) {
@ -164,17 +145,14 @@ func TestDecodePublishTX(t *testing.T) {
assert.Equal(t, expectedTX.Type, actualTX.Type)
assert.Equal(t, expectedTX.Version, actualTX.Version)
buf := io.NewBufBinWriter()
actualTX.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
assert.Equal(t, rawPublishTX, hex.EncodeToString(buf.Bytes()))
data, err := testserdes.EncodeBinary(actualTX)
assert.NoError(t, err)
assert.Equal(t, rawPublishTX, hex.EncodeToString(data))
}
func TestEncodingTXWithNoData(t *testing.T) {
buf := io.NewBufBinWriter()
tx := &Transaction{}
tx.EncodeBinary(buf.BinWriter)
require.Error(t, buf.Err)
_, err := testserdes.EncodeBinary(new(Transaction))
require.Error(t, err)
}
func TestMarshalUnmarshalJSON(t *testing.T) {
@ -189,10 +167,5 @@ func TestMarshalUnmarshalJSON(t *testing.T) {
InvocationScript: []byte{5, 3, 1},
VerificationScript: []byte{2, 4, 6},
}}
data, err := json.Marshal(tx)
require.NoError(t, err)
txNew := new(Transaction)
require.NoError(t, json.Unmarshal(data, txNew))
require.Equal(t, tx, txNew)
testserdes.MarshalUnmarshalJSON(t, tx, new(Transaction))
}

View file

@ -7,16 +7,14 @@ import (
"sort"
"testing"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/require"
)
func TestEncodeDecodeInfinity(t *testing.T) {
key := &PublicKey{}
buf := io.NewBufBinWriter()
key.EncodeBinary(buf.BinWriter)
require.NoError(t, buf.Err)
b := buf.Bytes()
b, err := testserdes.EncodeBinary(key)
require.NoError(t, err)
require.Equal(t, 1, len(b))
keyDecode := &PublicKey{}
@ -29,24 +27,13 @@ func TestEncodeDecodePublicKey(t *testing.T) {
k, err := NewPrivateKey()
require.NoError(t, err)
p := k.PublicKey()
buf := io.NewBufBinWriter()
p.EncodeBinary(buf.BinWriter)
require.NoError(t, buf.Err)
b := buf.Bytes()
pDecode := &PublicKey{}
require.NoError(t, pDecode.DecodeBytes(b))
require.Equal(t, p.X, pDecode.X)
testserdes.EncodeDecodeBinary(t, p, new(PublicKey))
}
errCases := [][]byte{{}, {0x02}, {0x04}}
for _, tc := range errCases {
r := io.NewBinReaderFromBuf(tc)
var pDecode PublicKey
pDecode.DecodeBinary(r)
require.Error(t, r.Err)
require.Error(t, testserdes.DecodeBinary(tc, new(PublicKey)))
}
}

View file

@ -0,0 +1,44 @@
package testserdes
import (
"encoding/json"
"testing"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/stretchr/testify/require"
)
// MarshalUnmarshalJSON checks if expected stays the same after
// marshal/unmarshal via JSON.
func MarshalUnmarshalJSON(t *testing.T, expected, actual interface{}) {
data, err := json.Marshal(expected)
require.NoError(t, err)
require.NoError(t, json.Unmarshal(data, actual))
require.Equal(t, expected, actual)
}
// EncodeDecodeBinary checks if expected stays the same after
// serializing/deserializing via io.Serializable methods.
func EncodeDecodeBinary(t *testing.T, expected, actual io.Serializable) {
data, err := EncodeBinary(expected)
require.NoError(t, err)
require.NoError(t, DecodeBinary(data, actual))
require.Equal(t, expected, actual)
}
// EncodeBinary serializes a to a byte slice.
func EncodeBinary(a io.Serializable) ([]byte, error) {
w := io.NewBufBinWriter()
a.EncodeBinary(w.BinWriter)
if w.Err != nil {
return nil, w.Err
}
return w.Bytes(), nil
}
// DecodeBinary deserializes a from a byte slice.
func DecodeBinary(data []byte, a io.Serializable) error {
r := io.NewBinReaderFromBuf(data)
a.DecodeBinary(r)
return r.Err
}

View file

@ -6,7 +6,7 @@ import (
"testing"
"time"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert"
)
@ -15,7 +15,6 @@ func TestEncodeDecodeAddress(t *testing.T) {
e, _ = net.ResolveTCPAddr("tcp", "127.0.0.1:2000")
ts = time.Now()
addr = NewAddressAndTime(e, ts)
buf = io.NewBufBinWriter()
)
assert.Equal(t, ts.UTC().Unix(), int64(addr.Timestamp))
@ -23,16 +22,8 @@ func TestEncodeDecodeAddress(t *testing.T) {
copy(aatip, addr.IP[:])
assert.Equal(t, e.IP, aatip)
assert.Equal(t, e.Port, int(addr.Port))
addr.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
b := buf.Bytes()
r := io.NewBinReaderFromBuf(b)
addrDecode := &AddressAndTime{}
addrDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, addr, addrDecode)
testserdes.EncodeDecodeBinary(t, addr, new(AddressAndTime))
}
func TestEncodeDecodeAddressList(t *testing.T) {
@ -43,15 +34,5 @@ func TestEncodeDecodeAddressList(t *testing.T) {
addrList.Addrs[i] = NewAddressAndTime(e, time.Now())
}
buf := io.NewBufBinWriter()
addrList.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
b := buf.Bytes()
r := io.NewBinReaderFromBuf(b)
addrListDecode := &AddressList{}
addrListDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, addrList, addrListDecode)
testserdes.EncodeDecodeBinary(t, addrList, new(AddressList))
}

View file

@ -4,9 +4,8 @@ import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
)
func TestGetBlockEncodeDecode(t *testing.T) {
@ -18,16 +17,7 @@ func TestGetBlockEncodeDecode(t *testing.T) {
}
p := NewGetBlocks(start, util.Uint256{})
buf := io.NewBufBinWriter()
p.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
b := buf.Bytes()
r := io.NewBinReaderFromBuf(b)
pDecode := &GetBlocks{}
pDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, p, pDecode)
testserdes.EncodeDecodeBinary(t, p, new(GetBlocks))
}
func TestGetBlockEncodeDecodeWithHashStop(t *testing.T) {
@ -41,14 +31,5 @@ func TestGetBlockEncodeDecodeWithHashStop(t *testing.T) {
stop = hash.Sha256([]byte("e"))
)
p := NewGetBlocks(start, stop)
buf := io.NewBufBinWriter()
p.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
b := buf.Bytes()
r := io.NewBinReaderFromBuf(b)
pDecode := &GetBlocks{}
pDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, p, pDecode)
testserdes.EncodeDecodeBinary(t, p, new(GetBlocks))
}

View file

@ -6,7 +6,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert"
)
@ -44,21 +44,18 @@ func newTestHeaders(n int) *Headers {
}
func testHeadersEncodeDecode(t *testing.T, headers *Headers, expected int, limit bool) {
buf := io.NewBufBinWriter()
headers.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
data, err := testserdes.EncodeBinary(headers)
assert.Nil(t, err)
b := buf.Bytes()
r := io.NewBinReaderFromBuf(b)
headersDecode := &Headers{}
headersDecode.DecodeBinary(r)
rErr := testserdes.DecodeBinary(data, headersDecode)
var err error
err = nil
if limit {
err = ErrTooManyHeaders
}
assert.Equal(t, err, r.Err)
assert.Equal(t, err, rErr)
assert.Equal(t, expected, len(headersDecode.Hdrs))
for i := 0; i < len(headersDecode.Hdrs); i++ {
@ -75,10 +72,7 @@ func TestBinEncodeDecode(t *testing.T) {
rawBlockBytes, _ := hex.DecodeString(rawBlockHeaders)
r := io.NewBinReaderFromBuf(rawBlockBytes)
headerMsg.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.NoError(t, testserdes.DecodeBinary(rawBlockBytes, &headerMsg))
assert.Equal(t, 1, len(headerMsg.Hdrs))
header := headerMsg.Hdrs[0]
@ -86,9 +80,7 @@ func TestBinEncodeDecode(t *testing.T) {
assert.Equal(t, "f3c4ec44c07eccbda974f1ee34bc6654ab6d3f22cd89c2e5c593a16d6cc7e6e8", hash.StringLE())
buf := io.NewBufBinWriter()
headerMsg.EncodeBinary(buf.BinWriter)
assert.Equal(t, nil, buf.Err)
assert.Equal(t, hex.EncodeToString(rawBlockBytes), hex.EncodeToString(buf.Bytes()))
data, err := testserdes.EncodeBinary(&headerMsg)
assert.NoError(t, err)
assert.Equal(t, hex.EncodeToString(rawBlockBytes), hex.EncodeToString(data))
}

View file

@ -4,7 +4,7 @@ import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
. "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
)
@ -16,24 +16,14 @@ func TestInventoryEncodeDecode(t *testing.T) {
}
inv := NewInventory(BlockType, hashes)
buf := io.NewBufBinWriter()
inv.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
b := buf.Bytes()
r := io.NewBinReaderFromBuf(b)
invDecode := &Inventory{}
invDecode.DecodeBinary(r)
assert.Nil(t, r.Err)
assert.Equal(t, inv, invDecode)
testserdes.EncodeDecodeBinary(t, inv, new(Inventory))
}
func TestEmptyInv(t *testing.T) {
msgInv := NewInventory(TXType, []Uint256{})
buf := io.NewBufBinWriter()
msgInv.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
assert.Equal(t, []byte{byte(TXType), 0}, buf.Bytes())
data, err := testserdes.EncodeBinary(msgInv)
assert.Nil(t, err)
assert.Equal(t, []byte{byte(TXType), 0}, data)
assert.Equal(t, 0, len(msgInv.Hashes))
}

View file

@ -3,7 +3,7 @@ package payload
import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert"
)
@ -11,14 +11,8 @@ func TestEncodeDecodeBinary(t *testing.T) {
payload := NewPing(uint32(1), uint32(2))
assert.NotEqual(t, 0, payload.Timestamp)
bufBinWriter := io.NewBufBinWriter()
payload.EncodeBinary(bufBinWriter.BinWriter)
assert.Nil(t, bufBinWriter.Err)
binReader := io.NewBinReaderFromBuf(bufBinWriter.Bytes())
decodedPing := &Ping{}
decodedPing.DecodeBinary(binReader)
assert.Nil(t, binReader.Err)
testserdes.EncodeDecodeBinary(t, payload, decodedPing)
assert.Equal(t, uint32(1), decodedPing.LastBlockIndex)
assert.Equal(t, uint32(2), decodedPing.Nonce)

View file

@ -3,7 +3,7 @@ package payload
import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert"
)
@ -15,17 +15,9 @@ func TestVersionEncodeDecode(t *testing.T) {
var relay = true
version := NewVersion(id, port, useragent, height, relay)
buf := io.NewBufBinWriter()
version.EncodeBinary(buf.BinWriter)
assert.Nil(t, buf.Err)
b := buf.Bytes()
assert.Equal(t, io.GetVarSize(version), len(b))
r := io.NewBinReaderFromBuf(b)
versionDecoded := &Version{}
versionDecoded.DecodeBinary(r)
assert.Nil(t, r.Err)
testserdes.EncodeDecodeBinary(t, version, versionDecoded)
assert.Equal(t, versionDecoded.Nonce, id)
assert.Equal(t, versionDecoded.Port, port)
assert.Equal(t, versionDecoded.UserAgent, []byte(useragent))

View file

@ -2,11 +2,11 @@ package context
import (
"encoding/hex"
"encoding/json"
"testing"
"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/testserdes"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm"
@ -137,12 +137,7 @@ func TestParameterContext_MarshalJSON(t *testing.T) {
},
}
data, err = json.Marshal(expected)
require.NoError(t, err)
actual := new(ParameterContext)
require.NoError(t, json.Unmarshal(data, actual))
require.Equal(t, expected, actual)
testserdes.MarshalUnmarshalJSON(t, expected, new(ParameterContext))
}
func getPrivateKeys(t *testing.T, n int) ([]*keys.PrivateKey, []*keys.PublicKey) {

View file

@ -2,17 +2,15 @@ package context
import (
"encoding/hex"
"encoding/json"
"io"
"math/rand"
"testing"
"time"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"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"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -56,12 +54,7 @@ func TestContextItem_MarshalJSON(t *testing.T) {
},
}
data, err := json.Marshal(expected)
require.NoError(t, err)
actual := new(Item)
require.NoError(t, json.Unmarshal(data, actual))
assert.Equal(t, expected, actual)
testserdes.MarshalUnmarshalJSON(t, expected, new(Item))
}
func getRandomSlice(t *testing.T, n int) []byte {

View file

@ -6,7 +6,7 @@ import (
"reflect"
"testing"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -446,25 +446,14 @@ func TestNewParameterFromString(t *testing.T) {
func TestEncodeDecodeBinary(t *testing.T) {
for _, tc := range marshalJSONTestCases {
w := io.NewBufBinWriter()
tc.input.EncodeBinary(w.BinWriter)
require.NoError(t, w.Err)
r := io.NewBinReaderFromBuf(w.Bytes())
var p Parameter
p.DecodeBinary(r)
require.NoError(t, r.Err)
require.Equal(t, tc.input, p)
testserdes.EncodeDecodeBinary(t, &tc.input, new(Parameter))
}
t.Run("unknown", func(t *testing.T) {
p := Parameter{Type: UnknownType}
w := io.NewBufBinWriter()
p.EncodeBinary(w.BinWriter)
require.Error(t, w.Err)
_, err := testserdes.EncodeBinary(&p)
require.Error(t, err)
r := io.NewBinReaderFromBuf([]byte{0xAA})
p.DecodeBinary(r)
require.Error(t, r.Err)
require.Error(t, testserdes.DecodeBinary([]byte{0xAA}, &p))
})
}

View file

@ -7,7 +7,7 @@ import (
"testing"
"github.com/go-yaml/yaml"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -175,12 +175,5 @@ func TestFixed8_Arith(t *testing.T) {
func TestFixed8_Serializable(t *testing.T) {
a := Fixed8(0x0102030405060708)
w := io.NewBufBinWriter()
a.EncodeBinary(w.BinWriter)
require.NoError(t, w.Err)
var b Fixed8
r := io.NewBinReaderFromBuf(w.Bytes())
b.DecodeBinary(r)
require.Equal(t, a, b)
testserdes.EncodeDecodeBinary(t, &a, new(Fixed8))
}

View file

@ -4,6 +4,7 @@ import (
"encoding/hex"
"testing"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -19,12 +20,7 @@ func TestUint160UnmarshalJSON(t *testing.T) {
assert.NoError(t, u1.UnmarshalJSON([]byte(`"`+str+`"`)))
assert.True(t, expected.Equals(u1))
s, err := expected.MarshalJSON()
require.NoError(t, err)
// UnmarshalJSON decodes hex-strings prefixed by 0x
assert.NoError(t, u2.UnmarshalJSON(s))
assert.True(t, expected.Equals(u1))
testserdes.MarshalUnmarshalJSON(t, &expected, &u2)
assert.Error(t, u2.UnmarshalJSON([]byte(`123`)))
}

View file

@ -4,7 +4,7 @@ import (
"encoding/hex"
"testing"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/internal/testserdes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -20,12 +20,7 @@ func TestUint256UnmarshalJSON(t *testing.T) {
require.NoError(t, u1.UnmarshalJSON([]byte(`"`+str+`"`)))
assert.True(t, expected.Equals(u1))
s, err := expected.MarshalJSON()
require.NoError(t, err)
// UnmarshalJSON decodes hex-strings prefixed by 0x
require.NoError(t, u2.UnmarshalJSON(s))
assert.True(t, expected.Equals(u1))
testserdes.MarshalUnmarshalJSON(t, &expected, &u2)
// UnmarshalJSON does not accepts numbers
assert.Error(t, u2.UnmarshalJSON([]byte("123")))
@ -96,12 +91,6 @@ func TestUint256_Serializable(t *testing.T) {
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
}
w := io.NewBufBinWriter()
a.EncodeBinary(w.BinWriter)
require.NoError(t, w.Err)
var b Uint256
r := io.NewBinReaderFromBuf(w.Bytes())
b.DecodeBinary(r)
require.Equal(t, a, b)
testserdes.EncodeDecodeBinary(t, &a, &b)
}