github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/protocol/validation/vmcontext_test.go (about)

     1  package validation
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/bytom/bytom/errors"
     8  	"github.com/bytom/bytom/protocol/bc"
     9  	"github.com/bytom/bytom/protocol/bc/types"
    10  	"github.com/bytom/bytom/protocol/vm"
    11  )
    12  
    13  func TestCheckOutput(t *testing.T) {
    14  	tx := types.NewTx(types.TxData{
    15  		Inputs: []*types.TxInput{
    16  			types.NewSpendInput(nil, bc.Hash{}, bc.NewAssetID([32]byte{1}), 5, 1, []byte("spendprog")),
    17  			types.NewIssuanceInput(nil, 6, []byte("issueprog"), nil, nil),
    18  		},
    19  		Outputs: []*types.TxOutput{
    20  			types.NewTxOutput(bc.NewAssetID([32]byte{3}), 8, []byte("wrongprog")),
    21  			types.NewTxOutput(bc.NewAssetID([32]byte{3}), 8, []byte("controlprog")),
    22  			types.NewTxOutput(bc.NewAssetID([32]byte{2}), 8, []byte("controlprog")),
    23  			types.NewTxOutput(bc.NewAssetID([32]byte{2}), 7, []byte("controlprog")),
    24  			types.NewTxOutput(bc.NewAssetID([32]byte{2}), 7, []byte("controlprog")),
    25  		},
    26  	})
    27  
    28  	txCtx := &entryContext{
    29  		entry:   tx.Tx.Entries[tx.Tx.InputIDs[0]],
    30  		entries: tx.Tx.Entries,
    31  	}
    32  
    33  	cases := []struct {
    34  		// args to CheckOutput
    35  		index     uint64
    36  		amount    uint64
    37  		assetID   []byte
    38  		vmVersion uint64
    39  		code      []byte
    40  
    41  		wantErr error
    42  		wantOk  bool
    43  	}{
    44  		{
    45  			index:     4,
    46  			amount:    7,
    47  			assetID:   append([]byte{2}, make([]byte, 31)...),
    48  			vmVersion: 1,
    49  			code:      []byte("controlprog"),
    50  			wantOk:    true,
    51  		},
    52  		{
    53  			index:     3,
    54  			amount:    7,
    55  			assetID:   append([]byte{2}, make([]byte, 31)...),
    56  			vmVersion: 1,
    57  			code:      []byte("controlprog"),
    58  			wantOk:    true,
    59  		},
    60  		{
    61  			index:     0,
    62  			amount:    1,
    63  			assetID:   append([]byte{9}, make([]byte, 31)...),
    64  			vmVersion: 1,
    65  			code:      []byte("missingprog"),
    66  			wantOk:    false,
    67  		},
    68  		{
    69  			index:     5,
    70  			amount:    7,
    71  			assetID:   append([]byte{2}, make([]byte, 31)...),
    72  			vmVersion: 1,
    73  			code:      []byte("controlprog"),
    74  			wantErr:   vm.ErrBadValue,
    75  		},
    76  	}
    77  
    78  	for i, test := range cases {
    79  		t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
    80  			gotOk, err := txCtx.checkOutput(test.index, test.amount, test.assetID, test.vmVersion, test.code, false)
    81  			if g := errors.Root(err); g != test.wantErr {
    82  				t.Errorf("checkOutput(%v, %v, %x, %v, %x) err = %v, want %v", test.index, test.amount, test.assetID, test.vmVersion, test.code, g, test.wantErr)
    83  				return
    84  			}
    85  			if gotOk != test.wantOk {
    86  				t.Errorf("checkOutput(%v, %v, %x, %v, %x) ok = %t, want %v", test.index, test.amount, test.assetID, test.vmVersion, test.code, gotOk, test.wantOk)
    87  			}
    88  
    89  		})
    90  	}
    91  }