OSDN Git Service

feat: add processIssuing (#152)
[bytom/vapor.git] / vendor / github.com / bytom / protocol / vm / vmutil / script_test.go
1 package vmutil
2
3 import (
4         "encoding/hex"
5         "testing"
6
7         "github.com/bytom/crypto/ed25519"
8         "github.com/bytom/errors"
9 )
10
11 // TestIsUnspendable ensures the IsUnspendable function returns the expected
12 // results.
13 func TestIsUnspendable(t *testing.T) {
14         tests := []struct {
15                 pkScript []byte
16                 expected bool
17         }{
18                 {
19                         // Unspendable
20                         pkScript: []byte{0x6a, 0x04, 0x74, 0x65, 0x73, 0x74},
21                         expected: true,
22                 },
23                 {
24                         // Spendable
25                         pkScript: []byte{0x76, 0xa9, 0x14, 0x29, 0x95, 0xa0,
26                                 0xfe, 0x68, 0x43, 0xfa, 0x9b, 0x95, 0x45,
27                                 0x97, 0xf0, 0xdc, 0xa7, 0xa4, 0x4d, 0xf6,
28                                 0xfa, 0x0b, 0x5c, 0x88, 0xac},
29                         expected: false,
30                 },
31         }
32
33         for i, test := range tests {
34                 res := IsUnspendable(test.pkScript)
35                 if res != test.expected {
36                         t.Errorf("TestIsUnspendable #%d failed: got %v want %v",
37                                 i, res, test.expected)
38                         continue
39                 }
40         }
41 }
42
43 func TestP2SPMultiSigProgram(t *testing.T) {
44         pub1, _ := hex.DecodeString("988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb6")
45         pub2, _ := hex.DecodeString("7192bf4eac0789ee19c88dfa87861cf59e215820f7bdb7be02761d9ed92e6c62")
46         pub3, _ := hex.DecodeString("8bcd251d9f4e03877130b6e6f1d577eda562375f07c3cdfad8f1d541002fd1a3")
47
48         tests := []struct {
49                 pubkeys     []ed25519.PublicKey
50                 nrequired   int
51                 wantProgram string
52                 wantErr     error
53         }{
54                 {
55                         pubkeys:     []ed25519.PublicKey{pub1},
56                         nrequired:   1,
57                         wantProgram: "ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb65151ad",
58                 },
59                 {
60                         pubkeys:     []ed25519.PublicKey{pub1, pub2},
61                         nrequired:   2,
62                         wantProgram: "ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb6207192bf4eac0789ee19c88dfa87861cf59e215820f7bdb7be02761d9ed92e6c625252ad",
63                 },
64                 {
65                         pubkeys:     []ed25519.PublicKey{pub1, pub2, pub3},
66                         nrequired:   2,
67                         wantProgram: "ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb6207192bf4eac0789ee19c88dfa87861cf59e215820f7bdb7be02761d9ed92e6c62208bcd251d9f4e03877130b6e6f1d577eda562375f07c3cdfad8f1d541002fd1a35253ad",
68                 },
69                 {
70                         pubkeys:   []ed25519.PublicKey{pub1},
71                         nrequired: -1,
72                         wantErr:   errors.WithDetail(ErrBadValue, "negative quorum"),
73                 },
74                 {
75                         pubkeys:   []ed25519.PublicKey{pub1},
76                         nrequired: 0,
77                         wantErr:   errors.WithDetail(ErrBadValue, "quorum empty with non-empty pubkey list"),
78                 },
79                 {
80                         pubkeys:   []ed25519.PublicKey{pub1, pub2},
81                         nrequired: 3,
82                         wantErr:   errors.WithDetail(ErrBadValue, "quorum too big"),
83                 },
84         }
85
86         for i, test := range tests {
87                 got, err := P2SPMultiSigProgram(test.pubkeys, test.nrequired)
88                 if err != nil {
89                         if test.wantErr != nil && err.Error() != test.wantErr.Error() {
90                                 t.Errorf("TestP2SPMultiSigProgram #%d failed: got %v want %v", i, err.Error(), test.wantErr.Error())
91                         } else if test.wantErr == nil {
92                                 t.Fatal(err)
93                         }
94                 }
95
96                 if hex.EncodeToString(got) != test.wantProgram {
97                         t.Errorf("TestP2SPMultiSigProgram #%d failed: got %v want %v", i, hex.EncodeToString(got), test.wantProgram)
98                 }
99         }
100 }
101
102 func TestP2SPMultiSigProgramWithHeight(t *testing.T) {
103         pub1, _ := hex.DecodeString("988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb6")
104         pub2, _ := hex.DecodeString("7192bf4eac0789ee19c88dfa87861cf59e215820f7bdb7be02761d9ed92e6c62")
105         pub3, _ := hex.DecodeString("8bcd251d9f4e03877130b6e6f1d577eda562375f07c3cdfad8f1d541002fd1a3")
106
107         tests := []struct {
108                 pubkeys     []ed25519.PublicKey
109                 nrequired   int
110                 height      int64
111                 wantProgram string
112                 wantErr     error
113         }{
114                 {
115                         pubkeys:     []ed25519.PublicKey{pub1},
116                         nrequired:   1,
117                         wantProgram: "ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb65151ad",
118                 },
119                 {
120                         pubkeys:     []ed25519.PublicKey{pub1, pub2},
121                         nrequired:   2,
122                         wantProgram: "ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb6207192bf4eac0789ee19c88dfa87861cf59e215820f7bdb7be02761d9ed92e6c625252ad",
123                 },
124                 {
125                         pubkeys:     []ed25519.PublicKey{pub1, pub2, pub3},
126                         nrequired:   2,
127                         wantProgram: "ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb6207192bf4eac0789ee19c88dfa87861cf59e215820f7bdb7be02761d9ed92e6c62208bcd251d9f4e03877130b6e6f1d577eda562375f07c3cdfad8f1d541002fd1a35253ad",
128                 },
129                 {
130                         pubkeys:   []ed25519.PublicKey{pub1},
131                         nrequired: 1,
132                         height:    -1,
133                         wantErr:   errors.WithDetail(ErrBadValue, "negative blockHeight"),
134                 },
135                 {
136                         pubkeys:     []ed25519.PublicKey{pub1},
137                         nrequired:   1,
138                         height:      0,
139                         wantProgram: "ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb65151ad",
140                 },
141                 {
142                         pubkeys:     []ed25519.PublicKey{pub1},
143                         nrequired:   1,
144                         height:      200,
145                         wantProgram: "01c8cda069ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb65151ad",
146                 },
147                 {
148                         pubkeys:     []ed25519.PublicKey{pub1, pub2},
149                         nrequired:   2,
150                         height:      200,
151                         wantProgram: "01c8cda069ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb6207192bf4eac0789ee19c88dfa87861cf59e215820f7bdb7be02761d9ed92e6c625252ad",
152                 },
153                 {
154                         pubkeys:     []ed25519.PublicKey{pub1, pub2, pub3},
155                         nrequired:   2,
156                         height:      200,
157                         wantProgram: "01c8cda069ae20988650ff921c82d47a953527894f792572ba63197c56e5fe79e5df0c444d6bb6207192bf4eac0789ee19c88dfa87861cf59e215820f7bdb7be02761d9ed92e6c62208bcd251d9f4e03877130b6e6f1d577eda562375f07c3cdfad8f1d541002fd1a35253ad",
158                 },
159                 {
160                         pubkeys:   []ed25519.PublicKey{pub1},
161                         nrequired: -1,
162                         wantErr:   errors.WithDetail(ErrBadValue, "negative quorum"),
163                 },
164                 {
165                         pubkeys:   []ed25519.PublicKey{pub1},
166                         nrequired: 0,
167                         wantErr:   errors.WithDetail(ErrBadValue, "quorum empty with non-empty pubkey list"),
168                 },
169                 {
170                         pubkeys:   []ed25519.PublicKey{pub1, pub2},
171                         nrequired: 3,
172                         wantErr:   errors.WithDetail(ErrBadValue, "quorum too big"),
173                 },
174         }
175
176         for i, test := range tests {
177                 got, err := P2SPMultiSigProgramWithHeight(test.pubkeys, test.nrequired, test.height)
178                 if err != nil {
179                         if test.wantErr != nil && err.Error() != test.wantErr.Error() {
180                                 t.Errorf("TestP2SPMultiSigProgram #%d failed: got %v want %v", i, err.Error(), test.wantErr.Error())
181                         } else if test.wantErr == nil {
182                                 t.Fatal(err)
183                         }
184                 }
185
186                 if hex.EncodeToString(got) != test.wantProgram {
187                         t.Errorf("TestP2SPMultiSigProgram #%d failed: got %v want %v", i, hex.EncodeToString(got), test.wantProgram)
188                 }
189         }
190 }
191
192 func TestGetIssuanceProgramRestrictHeight(t *testing.T) {
193         tests := []struct {
194                 issuanceProgram string
195                 wantHeight      int64
196         }{
197                 {
198                         issuanceProgram: "",
199                         wantHeight:      0,
200                 },
201                 {
202                         issuanceProgram: "ae20ac20f5cdb9ada2ae9836bcfff32126d6b885aa3f73ee111a95d1bf37f3904aca5151ad",
203                         wantHeight:      0,
204                 },
205                 {
206                         issuanceProgram: "01c8cda069ae20f44dd85be89de08b0f894476ccc7b3eebcf0a288c79504fa7e4c8033f5b7338020c86dc682ce3ecac64e165d9b5f8cca9ee05bd0d4df07adbfd11251ad7e88f1685152ad",
207                         wantHeight:      200,
208                 },
209         }
210
211         for i, test := range tests {
212                 program, err := hex.DecodeString(test.issuanceProgram)
213                 if err != nil {
214                         t.Fatal(err)
215                 }
216
217                 gotHeight := GetIssuanceProgramRestrictHeight(program)
218                 if gotHeight != test.wantHeight {
219                         t.Errorf("TestGetIssuanceProgramRestrictHeight #%d failed: got %d want %d", i, gotHeight, test.wantHeight)
220                 }
221         }
222 }