github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/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"), nil), 17 types.NewIssuanceInput(nil, 6, []byte("issueprog"), nil, nil), 18 }, 19 Outputs: []*types.TxOutput{ 20 types.NewOriginalTxOutput(bc.NewAssetID([32]byte{3}), 8, []byte("wrongprog"), [][]byte{[]byte("statedata")}), 21 types.NewOriginalTxOutput(bc.NewAssetID([32]byte{3}), 8, []byte("controlprog"), [][]byte{[]byte("wrongstatedata")}), 22 types.NewOriginalTxOutput(bc.NewAssetID([32]byte{2}), 8, []byte("controlprog"), [][]byte{[]byte("statedata")}), 23 types.NewOriginalTxOutput(bc.NewAssetID([32]byte{2}), 7, []byte("controlprog"), [][]byte{[]byte("statedata")}), 24 types.NewOriginalTxOutput(bc.NewAssetID([32]byte{2}), 7, []byte("controlprog"), [][]byte{[]byte("statedata")}), 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 state [][]byte 41 42 wantErr error 43 wantOk bool 44 }{ 45 { 46 index: 4, 47 amount: 7, 48 assetID: append([]byte{2}, make([]byte, 31)...), 49 vmVersion: 1, 50 code: []byte("controlprog"), 51 state: [][]byte{[]byte("statedata")}, 52 wantOk: true, 53 }, 54 { 55 index: 3, 56 amount: 7, 57 assetID: append([]byte{2}, make([]byte, 31)...), 58 vmVersion: 1, 59 code: []byte("controlprog"), 60 state: [][]byte{[]byte("statedata")}, 61 wantOk: true, 62 }, 63 { 64 index: 0, 65 amount: 1, 66 assetID: append([]byte{9}, make([]byte, 31)...), 67 vmVersion: 1, 68 code: []byte("controlprog"), 69 wantOk: false, 70 }, 71 { 72 index: 1, 73 amount: 8, 74 assetID: append([]byte{3}, make([]byte, 31)...), 75 vmVersion: 1, 76 code: []byte("controlprog"), 77 state: [][]byte{[]byte("missingstatedata")}, 78 wantOk: false, 79 }, 80 { 81 index: 5, 82 amount: 7, 83 assetID: append([]byte{2}, make([]byte, 31)...), 84 vmVersion: 1, 85 code: []byte("controlprog"), 86 wantErr: vm.ErrBadValue, 87 }, 88 } 89 90 for i, test := range cases { 91 t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) { 92 gotOk, err := txCtx.checkOutput(test.index, test.amount, test.assetID, test.vmVersion, test.code, test.state, false) 93 if g := errors.Root(err); g != test.wantErr { 94 t.Errorf("checkOutput(%v, %v, %x, %v, %x, %v) err = %v, want %v", test.index, test.amount, test.assetID, test.vmVersion, test.code, test.state, g, test.wantErr) 95 return 96 } 97 if gotOk != test.wantOk { 98 t.Errorf("checkOutput(%v, %v, %x, %v, %x, %v) ok = %t, want %v", test.index, test.amount, test.assetID, test.vmVersion, test.code, test.state, gotOk, test.wantOk) 99 } 100 101 }) 102 } 103 }