OSDN Git Service

Added blockchain struct.
[bytom/bytom.git] / protocol / vm / ops_test.go
1 package vm
2
3 import (
4         "fmt"
5         "testing"
6
7         "chain/errors"
8         "chain/math/checked"
9         "chain/testutil"
10 )
11
12 func TestParseOp(t *testing.T) {
13         cases := []struct {
14                 prog    []byte
15                 pc      uint32
16                 want    Instruction
17                 wantErr error
18         }{{
19                 prog: []byte{byte(OP_ADD)},
20                 want: Instruction{Op: OP_ADD, Len: 1},
21         }, {
22                 prog: []byte{byte(OP_16)},
23                 want: Instruction{Op: OP_16, Data: []byte{16}, Len: 1},
24         }, {
25                 prog: []byte{byte(OP_DATA_5), 1, 1, 1, 1, 1},
26                 want: Instruction{Op: OP_DATA_5, Data: []byte{1, 1, 1, 1, 1}, Len: 6},
27         }, {
28                 prog: []byte{byte(OP_DATA_5), 1, 1, 1, 1, 1, 255},
29                 want: Instruction{Op: OP_DATA_5, Data: []byte{1, 1, 1, 1, 1}, Len: 6},
30         }, {
31                 prog: []byte{byte(OP_PUSHDATA1), 1, 1},
32                 want: Instruction{Op: OP_PUSHDATA1, Data: []byte{1}, Len: 3},
33         }, {
34                 prog: []byte{byte(OP_PUSHDATA1), 1, 1, 255},
35                 want: Instruction{Op: OP_PUSHDATA1, Data: []byte{1}, Len: 3},
36         }, {
37                 prog: []byte{byte(OP_PUSHDATA2), 1, 0, 1},
38                 want: Instruction{Op: OP_PUSHDATA2, Data: []byte{1}, Len: 4},
39         }, {
40                 prog: []byte{byte(OP_PUSHDATA2), 1, 0, 1, 255},
41                 want: Instruction{Op: OP_PUSHDATA2, Data: []byte{1}, Len: 4},
42         }, {
43                 prog: []byte{byte(OP_PUSHDATA4), 1, 0, 0, 0, 1},
44                 want: Instruction{Op: OP_PUSHDATA4, Data: []byte{1}, Len: 6},
45         }, {
46                 prog: []byte{byte(OP_PUSHDATA4), 1, 0, 0, 0, 1, 255},
47                 want: Instruction{Op: OP_PUSHDATA4, Data: []byte{1}, Len: 6},
48         }, {
49                 prog:    []byte{},
50                 wantErr: ErrShortProgram,
51         }, {
52                 prog:    []byte{byte(OP_0)},
53                 pc:      1,
54                 wantErr: ErrShortProgram,
55         }, {
56                 prog:    []byte{byte(OP_DATA_1)},
57                 wantErr: ErrShortProgram,
58         }, {
59                 prog:    []byte{byte(OP_PUSHDATA1)},
60                 wantErr: ErrShortProgram,
61         }, {
62                 prog:    []byte{byte(OP_PUSHDATA1), 1},
63                 wantErr: ErrShortProgram,
64         }, {
65                 prog:    []byte{byte(OP_PUSHDATA2)},
66                 wantErr: ErrShortProgram,
67         }, {
68                 prog:    []byte{byte(OP_PUSHDATA2), 1, 0},
69                 wantErr: ErrShortProgram,
70         }, {
71                 prog:    []byte{byte(OP_PUSHDATA4)},
72                 wantErr: ErrShortProgram,
73         }, {
74                 prog:    []byte{byte(OP_PUSHDATA4), 1, 0, 0, 0},
75                 wantErr: ErrShortProgram,
76         }, {
77                 pc:      71,
78                 prog:    []byte{0x6d, 0x6b, 0xaa, 0x20, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x20, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x0, 0x0, 0x4e, 0xff, 0xff, 0xff, 0xff, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30},
79                 wantErr: checked.ErrOverflow,
80         }}
81
82         for _, c := range cases {
83                 t.Run(fmt.Sprintf("%d: %x", c.pc, c.prog), func(t *testing.T) {
84                         got, gotErr := ParseOp(c.prog, c.pc)
85
86                         if errors.Root(gotErr) != c.wantErr {
87                                 t.Errorf("ParseOp(%x, %d) error = %v want %v", c.prog, c.pc, gotErr, c.wantErr)
88                         }
89
90                         if c.wantErr != nil {
91                                 return
92                         }
93
94                         if !testutil.DeepEqual(got, c.want) {
95                                 t.Errorf("ParseOp(%x, %d) = %+v want %+v", c.prog, c.pc, got, c.want)
96                         }
97                 })
98         }
99 }
100
101 func TestParseProgram(t *testing.T) {
102         cases := []struct {
103                 prog    []byte
104                 want    []Instruction
105                 wantErr error
106         }{
107                 {
108                         prog: []byte{byte(OP_2), byte(OP_3), byte(OP_ADD), byte(OP_5), byte(OP_NUMEQUAL)},
109                         want: []Instruction{
110                                 {Op: OP_2, Data: []byte{0x02}, Len: 1},
111                                 {Op: OP_3, Data: []byte{0x03}, Len: 1},
112                                 {Op: OP_ADD, Len: 1},
113                                 {Op: OP_5, Data: []byte{0x05}, Len: 1},
114                                 {Op: OP_NUMEQUAL, Len: 1},
115                         },
116                 },
117                 {
118                         prog: []byte{255},
119                         want: []Instruction{
120                                 {Op: 255, Len: 1},
121                         },
122                 },
123         }
124
125         for _, c := range cases {
126                 got, gotErr := ParseProgram(c.prog)
127
128                 if errors.Root(gotErr) != c.wantErr {
129                         t.Errorf("ParseProgram(%x) error = %v want %v", c.prog, gotErr, c.wantErr)
130                 }
131
132                 if c.wantErr != nil {
133                         continue
134                 }
135
136                 if !testutil.DeepEqual(got, c.want) {
137                         t.Errorf("ParseProgram(%x) = %+v want %+v", c.prog, got, c.want)
138                 }
139         }
140 }