From edba35c971fde444ecfa78063810fface72fb853 Mon Sep 17 00:00:00 2001 From: Hamid <7atashfeshan@gmail.com> Date: Mon, 18 Mar 2019 17:46:46 +0330 Subject: [PATCH] dcrutil: Add tests for NewTxDeep. This adds tests for NewTxDeep to make sure new MsgTx is deeply copied. --- dcrutil/tx_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/dcrutil/tx_test.go b/dcrutil/tx_test.go index b2b48240..21f01aea 100644 --- a/dcrutil/tx_test.go +++ b/dcrutil/tx_test.go @@ -121,3 +121,53 @@ func TestTxErrors(t *testing.T) { "got %v, want %v", err, io.EOF) } } + +// TestNewTxDeep tests the API for Tx deep copy. +func TestNewTxDeep(t *testing.T) { + orgTx := Block100000.Transactions[0] + copyTxDeep := NewTxDeep(orgTx) + copyTx := copyTxDeep.MsgTx() + + // Ensure original and copied has equal values to original transaction. + if !reflect.DeepEqual(orgTx, copyTx) { + t.Fatalf("MsgTx is not equal - got %v, want %v", + spew.Sdump(copyTx), spew.Sdump(&orgTx)) + } + + // Ensure original and copied transaction referring to different allocations. + if orgTx == copyTx { + t.Fatal("MsgTx is referring to the same allocation") + } + + // Compare each original and copied input transaction allocations. + for i := 0; i < len(orgTx.TxIn); i++ { + // Ensure input transactions referring to different allocations. + if orgTx.TxIn[i] == copyTx.TxIn[i] { + t.Errorf("TxIn #%d is referring to the same allocation", i) + } + + // Ensure previous transaction output points referring to different + // allocations. + if &orgTx.TxIn[i].PreviousOutPoint == ©Tx.TxIn[i].PreviousOutPoint { + t.Errorf("PreviousOutPoint #%d is referring to the same allocation", i) + } + + // Ensure signature scripts referring to different allocations. + if &orgTx.TxIn[i].SignatureScript[0] == ©Tx.TxIn[i].SignatureScript[0] { + t.Errorf("SignatureScript #%d is referring to the same allocation", i) + } + } + + // Compare each original and copied output transaction allocations. + for i := 0; i < len(orgTx.TxOut); i++ { + // Ensure output transactions referring to different allocations. + if orgTx.TxOut[i] == copyTx.TxOut[i] { + t.Errorf("TxOut #%d is referring to the same allocation", i) + } + + // Ensure PkScripts referring to different allocations. + if &orgTx.TxOut[i].PkScript[0] == ©Tx.TxOut[i].PkScript[0] { + t.Errorf("PkScript #%d is referring to the same allocation", i) + } + } +}