6 "github.com/vapor/testutil"
9 func TestCheckSig(t *testing.T) {
16 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
20 // This one has a wrong-length signature
21 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc2 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
25 // This one has a wrong-length message
26 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
30 // This one has a wrong-length pubkey
31 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584 CHECKSIG",
35 // This one has a wrong byte in the signature
36 "0x00ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
40 // This one has a wrong byte in the message
41 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0002030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
45 // This one has a wrong byte in the pubkey
46 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0x00ca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 CHECKSIG",
50 "0x010203 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0x040506 1 1 CHECKMULTISIG",
54 "0x010203 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 0x040506 1 1 CHECKMULTISIG",
58 "0x26ced30b1942b89ef5332a9f22f1a61e5a6a3f8a5bc33b2fc58b1daf78c81bf1d5c8add19cea050adeb37da3a7bf8f813c6a6922b42934a6441fa6bb1c7fc208 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 0xdbca6fb13badb7cfdf76510070ffad15b85f9934224a9e11202f5e8f86b584a6 1 1 CHECKMULTISIG",
63 for i, c := range cases {
64 prog, err := Assemble(c.prog)
66 t.Fatalf("case %d: %s", i, err)
68 vm := &virtualMachine{
75 t.Errorf("case %d: expected error, got ok result", i)
79 t.Errorf("case %d: expected ok result, got error %s", i, err)
81 } else if !vm.falseResult() {
82 t.Errorf("case %d: expected false VM result, got error %s", i, err)
87 func TestCryptoOps(t *testing.T) {
89 ops[OP_SM3] = opInfo{OP_SM3, "SM3", opSm3}
91 type testStruct struct {
93 startVM *virtualMachine
95 wantVM *virtualMachine
97 cases := []testStruct{{
99 startVM: &virtualMachine{
101 dataStack: [][]byte{{1}},
103 wantVM: &virtualMachine{
105 dataStack: [][]byte{{
106 75, 245, 18, 47, 52, 69, 84, 197, 59, 222, 46, 187, 140, 210, 183, 227,
107 209, 96, 10, 214, 49, 195, 133, 165, 215, 204, 226, 60, 119, 133, 69, 154,
112 startVM: &virtualMachine{
114 dataStack: [][]byte{make([]byte, 65)},
116 wantVM: &virtualMachine{
118 dataStack: [][]byte{{
119 152, 206, 66, 222, 239, 81, 212, 2, 105, 213, 66, 245, 49, 75, 239, 44,
120 116, 104, 212, 1, 173, 93, 133, 22, 139, 250, 180, 192, 16, 143, 117, 247,
125 startVM: &virtualMachine{
127 dataStack: [][]byte{{1}},
129 wantVM: &virtualMachine{
131 dataStack: [][]byte{{
132 39, 103, 241, 92, 138, 242, 242, 199, 34, 93, 82, 115, 253, 214, 131, 237,
133 199, 20, 17, 10, 152, 125, 16, 84, 105, 124, 52, 138, 237, 78, 108, 199,
138 startVM: &virtualMachine{
140 dataStack: [][]byte{make([]byte, 65)},
142 wantVM: &virtualMachine{
144 dataStack: [][]byte{{
145 65, 106, 167, 181, 192, 224, 101, 48, 102, 167, 198, 77, 189, 208, 0, 157,
146 190, 132, 56, 97, 81, 254, 3, 159, 217, 66, 250, 162, 219, 97, 114, 235,
151 startVM: &virtualMachine{
153 dataStack: [][]byte{{1}},
155 wantVM: &virtualMachine{
157 dataStack: [][]byte{{
158 121, 155, 113, 154, 192, 49, 252, 137, 198, 216, 146, 90, 72, 125, 173, 7,
159 48, 143, 131, 123, 122, 183, 187, 199, 206, 189, 58, 65, 24, 253, 47, 56,
164 startVM: &virtualMachine{
166 dataStack: [][]byte{make([]byte, 65)},
168 wantVM: &virtualMachine{
170 dataStack: [][]byte{{
171 177, 247, 110, 45, 29, 65, 214, 241, 187, 59, 9, 192, 155, 130, 25, 218,
172 251, 173, 112, 13, 242, 72, 34, 32, 200, 146, 190, 65, 68, 90, 34, 255,
177 startVM: &virtualMachine{
180 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
181 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
182 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
183 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
186 wantVM: &virtualMachine{
189 dataStack: [][]byte{{1}},
193 startVM: &virtualMachine{
196 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
197 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
198 mustDecodeHex("badda7a7a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
199 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
202 wantVM: &virtualMachine{
205 dataStack: [][]byte{{}},
209 startVM: &virtualMachine{
212 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
213 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
214 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
215 mustDecodeHex("bad220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
218 wantVM: &virtualMachine{
221 dataStack: [][]byte{{}},
225 startVM: &virtualMachine{
228 mustDecodeHex("badabdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
229 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
230 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
231 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
234 wantVM: &virtualMachine{
237 dataStack: [][]byte{{}},
241 startVM: &virtualMachine{
244 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851" +
245 "fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
246 mustDecodeHex("badbad"),
247 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
250 wantErr: ErrBadValue,
253 startVM: &virtualMachine{
256 wantErr: ErrRunLimitExceeded,
258 op: OP_CHECKMULTISIG,
259 startVM: &virtualMachine{
262 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
263 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
264 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
269 wantVM: &virtualMachine{
272 dataStack: [][]byte{{1}},
275 op: OP_CHECKMULTISIG,
276 startVM: &virtualMachine{
279 mustDecodeHex("badabdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
280 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
281 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
286 wantVM: &virtualMachine{
289 dataStack: [][]byte{{}},
292 op: OP_CHECKMULTISIG,
293 startVM: &virtualMachine{
295 dataStack: [][]byte{},
297 wantErr: ErrDataStackUnderflow,
299 op: OP_CHECKMULTISIG,
300 startVM: &virtualMachine{
307 wantErr: ErrDataStackUnderflow,
309 op: OP_CHECKMULTISIG,
310 startVM: &virtualMachine{
313 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
318 wantErr: ErrDataStackUnderflow,
320 op: OP_CHECKMULTISIG,
321 startVM: &virtualMachine{
324 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
325 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
330 wantErr: ErrDataStackUnderflow,
332 op: OP_CHECKMULTISIG,
333 startVM: &virtualMachine{
336 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
337 mustDecodeHex("badbad"),
338 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
343 wantErr: ErrBadValue,
345 op: OP_CHECKMULTISIG,
346 startVM: &virtualMachine{
349 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
350 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
351 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
356 wantErr: ErrBadValue,
358 op: OP_CHECKMULTISIG,
359 startVM: &virtualMachine{
362 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
363 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
364 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
369 wantErr: ErrBadValue,
371 op: OP_CHECKMULTISIG,
372 startVM: &virtualMachine{
375 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
376 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
377 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
382 wantErr: ErrBadValue,
384 op: OP_CHECKMULTISIG,
385 startVM: &virtualMachine{
388 mustDecodeHex("af5abdf4bbb34f4a089efc298234f84fd909def662a8df03b4d7d40372728851fbd3bf59920af5a7c361a4851967714271d1727e3be417a60053c30969d8860c"),
389 mustDecodeHex("916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9"),
390 mustDecodeHex("ab3220d065dc875c6a5b4ecc39809b5f24eb0a605e9eef5190457edbf1e3b866"),
395 wantErr: ErrRunLimitExceeded,
398 startVM: &virtualMachine{
401 TxSigHash: func() []byte {
403 0x2f, 0x00, 0x3c, 0xdd, 0x64, 0x42, 0x7b, 0x5e,
404 0xed, 0xd6, 0xcc, 0xb5, 0x85, 0x47, 0x02, 0x0b,
405 0x02, 0xde, 0xf2, 0x2d, 0xc5, 0x99, 0x7e, 0x9d,
406 0xa9, 0xac, 0x40, 0x49, 0xc3, 0x4a, 0x58, 0xd8,
411 wantVM: &virtualMachine{
413 dataStack: [][]byte{{
414 47, 0, 60, 221, 100, 66, 123, 94,
415 237, 214, 204, 181, 133, 71, 2, 11,
416 2, 222, 242, 45, 197, 153, 126, 157,
417 169, 172, 64, 73, 195, 74, 88, 216,
422 startVM: &virtualMachine{
426 wantErr: ErrRunLimitExceeded,
429 hashOps := []Op{OP_SHA256, OP_SHA3, OP_SM3}
430 for _, op := range hashOps {
431 cases = append(cases, testStruct{
433 startVM: &virtualMachine{
435 dataStack: [][]byte{{1}},
437 wantErr: ErrRunLimitExceeded,
441 for i, c := range cases {
444 err := ops[c.op].fn(c.startVM)
447 if err != c.wantErr {
448 t.Errorf("case %d, op %s: got err = %v want %v", i, ops[c.op].name, err, c.wantErr)
451 if c.wantErr != nil {
455 // Hack: the context objects will otherwise compare unequal
456 // sometimes (because of the function pointer within?) and we
458 c.wantVM.context = gotVM.context
460 if !testutil.DeepEqual(gotVM, c.wantVM) {
461 t.Errorf("case %d, op %s: unexpected vm result\n\tgot: %+v\n\twant: %+v\n", i, ops[c.op].name, gotVM, c.wantVM)