OSDN Git Service

auth_verification_test (#1970)
[bytom/bytom.git] / protocol / vm / types_test.go
1 package vm
2
3 import (
4         "bytes"
5         "math/big"
6         "testing"
7
8         "github.com/holiman/uint256"
9 )
10
11 func TestBoolBytes(t *testing.T) {
12         got := BoolBytes(true)
13         want := []byte{1}
14         if !bytes.Equal(got, want) {
15                 t.Errorf("BoolBytes(t) = %x want %x", got, want)
16         }
17
18         got = BoolBytes(false)
19         want = []byte{}
20         if !bytes.Equal(got, want) {
21                 t.Errorf("BoolBytes(f) = %x want %x", got, want)
22         }
23 }
24
25 func TestAsBool(t *testing.T) {
26         cases := []struct {
27                 data []byte
28                 want bool
29         }{
30                 {[]byte{0, 0, 0, 0}, false},
31                 {[]byte{0}, false},
32                 {[]byte{}, false},
33                 {[]byte{1}, true},
34                 {[]byte{1, 1, 1, 1}, true},
35                 {[]byte{0, 0, 0, 1}, true},
36                 {[]byte{1, 0, 0, 0}, true},
37                 {[]byte{2}, true},
38         }
39
40         for _, c := range cases {
41                 got := AsBool(c.data)
42
43                 if got != c.want {
44                         t.Errorf("AsBool(%x) = %v want %v", c.data, got, c.want)
45                 }
46         }
47 }
48
49 func TestInt64(t *testing.T) {
50         cases := []struct {
51                 num  int64
52                 data []byte
53         }{
54                 {0, []byte{}},
55                 {1, []byte{0x01}},
56                 {255, []byte{0xff}},
57                 {256, []byte{0x00, 0x01}},
58                 {1 << 16, []byte{0x00, 0x00, 0x01}},
59                 {-1, []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
60                 {-2, []byte{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
61         }
62
63         for _, c := range cases {
64                 gotData := Int64Bytes(c.num)
65
66                 if !bytes.Equal(gotData, c.data) {
67                         t.Errorf("Int64Bytes(%d) = %x want %x", c.num, gotData, c.data)
68                 }
69
70                 gotNum, _ := AsInt64(c.data)
71
72                 if gotNum != c.num {
73                         t.Errorf("AsInt64(%x) = %d want %d", c.data, gotNum, c.num)
74                 }
75         }
76
77         data := []byte{1, 1, 1, 1, 1, 1, 1, 1, 1}
78         _, err := AsInt64(data)
79         want := ErrBadValue
80         if err != want {
81                 t.Errorf("AsInt64(%x) = %v want %v", data, err, want)
82         }
83 }
84
85 func TestBigIntBytes(t *testing.T) {
86         tests := []struct {
87                 input []byte
88                 num   *big.Int
89         }{
90                 {num: new(big.Int), input: []byte{}},
91                 {num: new(big.Int).SetInt64(0), input: []byte{}},
92                 {num: new(big.Int).SetInt64(1), input: []byte{0x01}},
93                 {num: new(big.Int).SetInt64(255), input: []byte{0xff}},
94                 {num: new(big.Int).SetInt64(256), input: []byte{0x00, 0x01}},
95                 {num: new(big.Int).SetInt64(46657), input: []byte{0x41, 0xb6}},
96                 {num: new(big.Int).SetInt64(1 << 32), input: []byte{0x00, 0x00, 0x00, 0x00, 0x01}},
97                 {
98                         num:   new(big.Int).Exp(new(big.Int).SetInt64(10), new(big.Int).SetInt64(32), nil),
99                         input: []byte{0x00, 0x00, 0x00, 0x00, 0x81, 0xef, 0xac, 0x85, 0x5b, 0x41, 0x6d, 0x2d, 0xee, 0x04},
100                 },
101                 {num: new(big.Int).SetInt64(-1), input: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
102                         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
103                         0xff, 0xff, 0xff, 0xff}},
104                 {num: new(big.Int).SetInt64(-256), input: []byte{0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
105                         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
106                         0xff, 0xff, 0xff, 0xff}},
107         }
108         for _, test := range tests {
109                 fromBig, b := uint256.FromBig(test.num)
110                 if b {
111                         t.Errorf("FromBig overflow")
112                 }
113
114                 gotData := BigIntBytes(fromBig)
115                 if !bytes.Equal(gotData, test.input) {
116                         t.Errorf("BigIntBytes(%s) = %x want %x", test.num.String(), gotData, test.input)
117                 }
118         }
119 }
120
121 func TestAsBigInt(t *testing.T) {
122         tests := []struct {
123                 input     []byte
124                 num       *big.Int
125                 wantError bool
126         }{
127                 {num: new(big.Int), input: []byte{}},
128                 {num: new(big.Int), input: []byte{0x00}},
129                 {num: new(big.Int).SetInt64(0), input: []byte{0x00}},
130                 {num: new(big.Int).SetInt64(1), input: []byte{0x01}},
131                 {num: new(big.Int).SetInt64(255), input: []byte{0xff}},
132                 {num: new(big.Int).SetInt64(256), input: []byte{0x00, 0x01}},
133                 {num: new(big.Int).SetInt64(46657), input: []byte{0x41, 0xb6}},
134                 {num: new(big.Int).SetInt64(1 << 32), input: []byte{0x00, 0x00, 0x00, 0x00, 0x01}},
135                 {
136                         num:   new(big.Int).Exp(new(big.Int).SetInt64(10), new(big.Int).SetInt64(32), nil),
137                         input: []byte{0x00, 0x00, 0x00, 0x00, 0x81, 0xef, 0xac, 0x85, 0x5b, 0x41, 0x6d, 0x2d, 0xee, 0x04},
138                 },
139                 {num: new(big.Int).SetInt64(-1), input: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, wantError: true},
140                 {num: new(big.Int).SetInt64(-256), input: []byte{0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00}, wantError: true},
141                 {input: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, wantError: true},
142         }
143         for _, test := range tests {
144                 data, err := AsBigInt(test.input)
145                 if err != nil {
146                         if test.wantError {
147                                 continue
148                         }
149                         t.Errorf("AsBigInt(%q) --> err %s", test.input, err.Error())
150                 }
151
152                 fromBig, b := uint256.FromBig(test.num)
153                 if b {
154                         t.Errorf("FromBig overflow")
155                 }
156
157                 if data != nil && !data.Eq(fromBig) {
158                         t.Errorf("AsBigInt(%s) = %x want %x", test.num.String(), data, test.input)
159                 }
160         }
161 }