From 9abda40171824151277cc7ea438f73b48568c904 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 26 Mar 2020 17:43:24 +0300 Subject: [PATCH] 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 --- pkg/consensus/payload_test.go | 45 +++++------------ pkg/core/block/block_test.go | 30 ++++-------- pkg/core/block/header_test.go | 12 ++--- pkg/core/block/helper_test.go | 6 +-- pkg/core/helper_test.go | 17 +++---- pkg/core/state/account_test.go | 21 ++------ pkg/core/state/asset_test.go | 11 +---- pkg/core/state/contract_test.go | 11 ++--- pkg/core/state/nep5_test.go | 16 ++---- pkg/core/state/notification_event_test.go | 23 ++------- pkg/core/state/storage_item_test.go | 14 +----- pkg/core/state/unclaimed_test.go | 9 ++-- pkg/core/state/unspent_coin_test.go | 11 +---- pkg/core/state/validator_test.go | 11 +---- pkg/core/transaction/contract_test.go | 10 ++-- pkg/core/transaction/enrollment_test.go | 10 ++-- pkg/core/transaction/helper_test.go | 6 +-- pkg/core/transaction/invocation_test.go | 30 +++--------- pkg/core/transaction/miner_test.go | 11 ++--- pkg/core/transaction/register_test.go | 36 ++++---------- pkg/core/transaction/state_test.go | 10 ++-- pkg/core/transaction/transaction_test.go | 59 ++++++----------------- pkg/crypto/keys/publickey_test.go | 23 ++------- pkg/internal/testserdes/testing.go | 44 +++++++++++++++++ pkg/network/payload/address_test.go | 25 ++-------- pkg/network/payload/getblocks_test.go | 25 ++-------- pkg/network/payload/headers_test.go | 28 ++++------- pkg/network/payload/inventory_test.go | 20 ++------ pkg/network/payload/ping_test.go | 10 +--- pkg/network/payload/version_test.go | 14 ++---- pkg/smartcontract/context/context_test.go | 9 +--- pkg/smartcontract/context/item_test.go | 13 ++--- pkg/smartcontract/parameter_test.go | 21 ++------ pkg/util/fixed8_test.go | 11 +---- pkg/util/uint160_test.go | 8 +-- pkg/util/uint256_test.go | 17 ++----- 36 files changed, 201 insertions(+), 476 deletions(-) create mode 100644 pkg/internal/testserdes/testing.go diff --git a/pkg/consensus/payload_test.go b/pkg/consensus/payload_test.go index 9c0c40162..00510cba2 100644 --- a/pkg/consensus/payload_test.go +++ b/pkg/consensus/payload_test.go @@ -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) diff --git a/pkg/core/block/block_test.go b/pkg/core/block/block_test.go index 326f2adc1..b8a7d1a8c 100644 --- a/pkg/core/block/block_test.go +++ b/pkg/core/block/block_test.go @@ -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)) diff --git a/pkg/core/block/header_test.go b/pkg/core/block/header_test.go index c7901c287..55b20b24b 100644 --- a/pkg/core/block/header_test.go +++ b/pkg/core/block/header_test.go @@ -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") diff --git a/pkg/core/block/helper_test.go b/pkg/core/block/helper_test.go index 92c8c88b0..cede76763 100644 --- a/pkg/core/block/helper_test.go +++ b/pkg/core/block/helper_test.go @@ -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 } diff --git a/pkg/core/helper_test.go b/pkg/core/helper_test.go index 763ffb6eb..5ceec3826 100644 --- a/pkg/core/helper_test.go +++ b/pkg/core/helper_test.go @@ -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)) } } diff --git a/pkg/core/state/account_test.go b/pkg/core/state/account_test.go index 24d0ac457..307abec8f 100644 --- a/pkg/core/state/account_test.go +++ b/pkg/core/state/account_test.go @@ -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) { diff --git a/pkg/core/state/asset_test.go b/pkg/core/state/asset_test.go index 75226c16e..e3cbf2950 100644 --- a/pkg/core/state/asset_test.go +++ b/pkg/core/state/asset_test.go @@ -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) { diff --git a/pkg/core/state/contract_test.go b/pkg/core/state/contract_test.go index 902e8439e..9101dfab1 100644 --- a/pkg/core/state/contract_test.go +++ b/pkg/core/state/contract_test.go @@ -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()) } diff --git a/pkg/core/state/nep5_test.go b/pkg/core/state/nep5_test.go index 36c20f7b5..a2f3d8495 100644 --- a/pkg/core/state/nep5_test.go +++ b/pkg/core/state/nep5_test.go @@ -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) -} diff --git a/pkg/core/state/notification_event_test.go b/pkg/core/state/notification_event_test.go index 3deb48845..1e17e40e9 100644 --- a/pkg/core/state/notification_event_test.go +++ b/pkg/core/state/notification_event_test.go @@ -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)) } diff --git a/pkg/core/state/storage_item_test.go b/pkg/core/state/storage_item_test.go index cbf850cbc..1df6165df 100644 --- a/pkg/core/state/storage_item_test.go +++ b/pkg/core/state/storage_item_test.go @@ -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)) } diff --git a/pkg/core/state/unclaimed_test.go b/pkg/core/state/unclaimed_test.go index 8c5b2b81d..47ec02d73 100644 --- a/pkg/core/state/unclaimed_test.go +++ b/pkg/core/state/unclaimed_test.go @@ -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:])) diff --git a/pkg/core/state/unspent_coin_test.go b/pkg/core/state/unspent_coin_test.go index 2c9a9274d..51e778ab7 100644 --- a/pkg/core/state/unspent_coin_test.go +++ b/pkg/core/state/unspent_coin_test.go @@ -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)) } diff --git a/pkg/core/state/validator_test.go b/pkg/core/state/validator_test.go index 9d104773d..1136aa189 100644 --- a/pkg/core/state/validator_test.go +++ b/pkg/core/state/validator_test.go @@ -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) { diff --git a/pkg/core/transaction/contract_test.go b/pkg/core/transaction/contract_test.go index dbdd21130..04215b55c 100644 --- a/pkg/core/transaction/contract_test.go +++ b/pkg/core/transaction/contract_test.go @@ -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)) } diff --git a/pkg/core/transaction/enrollment_test.go b/pkg/core/transaction/enrollment_test.go index 8e1186d92..ca8483333 100644 --- a/pkg/core/transaction/enrollment_test.go +++ b/pkg/core/transaction/enrollment_test.go @@ -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)) } diff --git a/pkg/core/transaction/helper_test.go b/pkg/core/transaction/helper_test.go index b5563f54e..32f2dd743 100644 --- a/pkg/core/transaction/helper_test.go +++ b/pkg/core/transaction/helper_test.go @@ -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 } diff --git a/pkg/core/transaction/invocation_test.go b/pkg/core/transaction/invocation_test.go index a5e46f464..babe7d275 100644 --- a/pkg/core/transaction/invocation_test.go +++ b/pkg/core/transaction/invocation_test.go @@ -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) } diff --git a/pkg/core/transaction/miner_test.go b/pkg/core/transaction/miner_test.go index 82cf54458..c1a354983 100644 --- a/pkg/core/transaction/miner_test.go +++ b/pkg/core/transaction/miner_test.go @@ -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)) } diff --git a/pkg/core/transaction/register_test.go b/pkg/core/transaction/register_test.go index 199614194..d98818d58 100644 --- a/pkg/core/transaction/register_test.go +++ b/pkg/core/transaction/register_test.go @@ -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)) } diff --git a/pkg/core/transaction/state_test.go b/pkg/core/transaction/state_test.go index 568b37bca..0c8e48031 100644 --- a/pkg/core/transaction/state_test.go +++ b/pkg/core/transaction/state_test.go @@ -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)) } diff --git a/pkg/core/transaction/transaction_test.go b/pkg/core/transaction/transaction_test.go index b98d07ab0..34c3090db 100644 --- a/pkg/core/transaction/transaction_test.go +++ b/pkg/core/transaction/transaction_test.go @@ -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)) } diff --git a/pkg/crypto/keys/publickey_test.go b/pkg/crypto/keys/publickey_test.go index e99f8d347..fda5c3b18 100644 --- a/pkg/crypto/keys/publickey_test.go +++ b/pkg/crypto/keys/publickey_test.go @@ -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))) } } diff --git a/pkg/internal/testserdes/testing.go b/pkg/internal/testserdes/testing.go new file mode 100644 index 000000000..3883f461c --- /dev/null +++ b/pkg/internal/testserdes/testing.go @@ -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 +} diff --git a/pkg/network/payload/address_test.go b/pkg/network/payload/address_test.go index 446678280..30a79d02f 100644 --- a/pkg/network/payload/address_test.go +++ b/pkg/network/payload/address_test.go @@ -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)) } diff --git a/pkg/network/payload/getblocks_test.go b/pkg/network/payload/getblocks_test.go index 0ab49bbd4..baa3f730e 100644 --- a/pkg/network/payload/getblocks_test.go +++ b/pkg/network/payload/getblocks_test.go @@ -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)) } diff --git a/pkg/network/payload/headers_test.go b/pkg/network/payload/headers_test.go index b4e5056e6..729b258be 100644 --- a/pkg/network/payload/headers_test.go +++ b/pkg/network/payload/headers_test.go @@ -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)) } diff --git a/pkg/network/payload/inventory_test.go b/pkg/network/payload/inventory_test.go index 41a17e541..1fd685c40 100644 --- a/pkg/network/payload/inventory_test.go +++ b/pkg/network/payload/inventory_test.go @@ -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)) } diff --git a/pkg/network/payload/ping_test.go b/pkg/network/payload/ping_test.go index 6c71ab451..82b28ed67 100644 --- a/pkg/network/payload/ping_test.go +++ b/pkg/network/payload/ping_test.go @@ -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) diff --git a/pkg/network/payload/version_test.go b/pkg/network/payload/version_test.go index 21fd497a0..1f44a6d7b 100644 --- a/pkg/network/payload/version_test.go +++ b/pkg/network/payload/version_test.go @@ -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)) diff --git a/pkg/smartcontract/context/context_test.go b/pkg/smartcontract/context/context_test.go index f366fdc10..1d6f01a37 100644 --- a/pkg/smartcontract/context/context_test.go +++ b/pkg/smartcontract/context/context_test.go @@ -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) { diff --git a/pkg/smartcontract/context/item_test.go b/pkg/smartcontract/context/item_test.go index fd7fd8be1..334427641 100644 --- a/pkg/smartcontract/context/item_test.go +++ b/pkg/smartcontract/context/item_test.go @@ -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 { diff --git a/pkg/smartcontract/parameter_test.go b/pkg/smartcontract/parameter_test.go index a1715ee13..4980dfd51 100644 --- a/pkg/smartcontract/parameter_test.go +++ b/pkg/smartcontract/parameter_test.go @@ -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)) }) } diff --git a/pkg/util/fixed8_test.go b/pkg/util/fixed8_test.go index aff92644b..639ff837a 100644 --- a/pkg/util/fixed8_test.go +++ b/pkg/util/fixed8_test.go @@ -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)) } diff --git a/pkg/util/uint160_test.go b/pkg/util/uint160_test.go index 5f40da0f3..c72b58633 100644 --- a/pkg/util/uint160_test.go +++ b/pkg/util/uint160_test.go @@ -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`))) } diff --git a/pkg/util/uint256_test.go b/pkg/util/uint256_test.go index 4f12fd658..fbe995243 100644 --- a/pkg/util/uint256_test.go +++ b/pkg/util/uint256_test.go @@ -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) }