OSDN Git Service

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