forked from btcsuite/btcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine_test.go
428 lines (401 loc) · 10.7 KB
/
engine_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright (c) 2013-2017 The btcsuite developers
// Copyright (c) 2015-2019 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package txscript
import (
"testing"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
)
// TestBadPC sets the pc to a deliberately bad result then confirms that Step
// and Disasm fail correctly.
func TestBadPC(t *testing.T) {
t.Parallel()
tests := []struct {
scriptIdx int
}{
{scriptIdx: 2},
{scriptIdx: 3},
}
// tx with almost empty scripts.
tx := &wire.MsgTx{
Version: 1,
TxIn: []*wire.TxIn{
{
PreviousOutPoint: wire.OutPoint{
Hash: chainhash.Hash([32]byte{
0xc9, 0x97, 0xa5, 0xe5,
0x6e, 0x10, 0x41, 0x02,
0xfa, 0x20, 0x9c, 0x6a,
0x85, 0x2d, 0xd9, 0x06,
0x60, 0xa2, 0x0b, 0x2d,
0x9c, 0x35, 0x24, 0x23,
0xed, 0xce, 0x25, 0x85,
0x7f, 0xcd, 0x37, 0x04,
}),
Index: 0,
},
SignatureScript: mustParseShortForm("NOP"),
Sequence: 4294967295,
},
},
TxOut: []*wire.TxOut{{
Value: 1000000000,
PkScript: nil,
}},
LockTime: 0,
}
pkScript := mustParseShortForm("NOP")
for _, test := range tests {
vm, err := NewEngine(pkScript, tx, 0, 0, nil, nil, -1, nil)
if err != nil {
t.Errorf("Failed to create script: %v", err)
}
// Set to after all scripts.
vm.scriptIdx = test.scriptIdx
// Ensure attempting to step fails.
_, err = vm.Step()
if err == nil {
t.Errorf("Step with invalid pc (%v) succeeds!", test)
continue
}
// Ensure attempting to disassemble the current program counter fails.
_, err = vm.DisasmPC()
if err == nil {
t.Errorf("DisasmPC with invalid pc (%v) succeeds!", test)
}
}
}
// TestCheckErrorCondition tests the execute early test in CheckErrorCondition()
// since most code paths are tested elsewhere.
func TestCheckErrorCondition(t *testing.T) {
t.Parallel()
// tx with almost empty scripts.
tx := &wire.MsgTx{
Version: 1,
TxIn: []*wire.TxIn{{
PreviousOutPoint: wire.OutPoint{
Hash: chainhash.Hash([32]byte{
0xc9, 0x97, 0xa5, 0xe5,
0x6e, 0x10, 0x41, 0x02,
0xfa, 0x20, 0x9c, 0x6a,
0x85, 0x2d, 0xd9, 0x06,
0x60, 0xa2, 0x0b, 0x2d,
0x9c, 0x35, 0x24, 0x23,
0xed, 0xce, 0x25, 0x85,
0x7f, 0xcd, 0x37, 0x04,
}),
Index: 0,
},
SignatureScript: nil,
Sequence: 4294967295,
}},
TxOut: []*wire.TxOut{{
Value: 1000000000,
PkScript: nil,
}},
LockTime: 0,
}
pkScript := mustParseShortForm("NOP NOP NOP NOP NOP NOP NOP NOP NOP" +
" NOP TRUE")
vm, err := NewEngine(pkScript, tx, 0, 0, nil, nil, 0, nil)
if err != nil {
t.Errorf("failed to create script: %v", err)
}
for i := 0; i < len(pkScript)-1; i++ {
done, err := vm.Step()
if err != nil {
t.Fatalf("failed to step %dth time: %v", i, err)
}
if done {
t.Fatalf("finshed early on %dth time", i)
}
err = vm.CheckErrorCondition(false)
if !IsErrorCode(err, ErrScriptUnfinished) {
t.Fatalf("got unexepected error %v on %dth iteration",
err, i)
}
}
done, err := vm.Step()
if err != nil {
t.Fatalf("final step failed %v", err)
}
if !done {
t.Fatalf("final step isn't done!")
}
err = vm.CheckErrorCondition(false)
if err != nil {
t.Errorf("unexpected error %v on final check", err)
}
}
// TestInvalidFlagCombinations ensures the script engine returns the expected
// error when disallowed flag combinations are specified.
func TestInvalidFlagCombinations(t *testing.T) {
t.Parallel()
tests := []ScriptFlags{
ScriptVerifyCleanStack,
}
// tx with almost empty scripts.
tx := &wire.MsgTx{
Version: 1,
TxIn: []*wire.TxIn{
{
PreviousOutPoint: wire.OutPoint{
Hash: chainhash.Hash([32]byte{
0xc9, 0x97, 0xa5, 0xe5,
0x6e, 0x10, 0x41, 0x02,
0xfa, 0x20, 0x9c, 0x6a,
0x85, 0x2d, 0xd9, 0x06,
0x60, 0xa2, 0x0b, 0x2d,
0x9c, 0x35, 0x24, 0x23,
0xed, 0xce, 0x25, 0x85,
0x7f, 0xcd, 0x37, 0x04,
}),
Index: 0,
},
SignatureScript: []uint8{OP_NOP},
Sequence: 4294967295,
},
},
TxOut: []*wire.TxOut{
{
Value: 1000000000,
PkScript: nil,
},
},
LockTime: 0,
}
pkScript := []byte{OP_NOP}
for i, test := range tests {
_, err := NewEngine(pkScript, tx, 0, test, nil, nil, -1, nil)
if !IsErrorCode(err, ErrInvalidFlags) {
t.Fatalf("TestInvalidFlagCombinations #%d unexpected "+
"error: %v", i, err)
}
}
}
// TestCheckPubKeyEncoding ensures the internal checkPubKeyEncoding function
// works as expected.
func TestCheckPubKeyEncoding(t *testing.T) {
t.Parallel()
tests := []struct {
name string
key []byte
isValid bool
}{
{
name: "uncompressed ok",
key: hexToBytes("0411db93e1dcdb8a016b49840f8c53bc1eb68" +
"a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf" +
"9744464f82e160bfa9b8b64f9d4c03f999b8643f656b" +
"412a3"),
isValid: true,
},
{
name: "compressed ok",
key: hexToBytes("02ce0b14fb842b1ba549fdd675c98075f12e9" +
"c510f8ef52bd021a9a1f4809d3b4d"),
isValid: true,
},
{
name: "compressed ok",
key: hexToBytes("032689c7c2dab13309fb143e0e8fe39634252" +
"1887e976690b6b47f5b2a4b7d448e"),
isValid: true,
},
{
name: "hybrid",
key: hexToBytes("0679be667ef9dcbbac55a06295ce870b07029" +
"bfcdb2dce28d959f2815b16f81798483ada7726a3c46" +
"55da4fbfc0e1108a8fd17b448a68554199c47d08ffb1" +
"0d4b8"),
isValid: false,
},
{
name: "empty",
key: nil,
isValid: false,
},
}
vm := Engine{flags: ScriptVerifyStrictEncoding}
for _, test := range tests {
err := vm.checkPubKeyEncoding(test.key)
if err != nil && test.isValid {
t.Errorf("checkSignatureEncoding test '%s' failed "+
"when it should have succeeded: %v", test.name,
err)
} else if err == nil && !test.isValid {
t.Errorf("checkSignatureEncooding test '%s' succeeded "+
"when it should have failed", test.name)
}
}
}
// TestCheckSignatureEncoding ensures the internal checkSignatureEncoding
// function works as expected.
func TestCheckSignatureEncoding(t *testing.T) {
t.Parallel()
tests := []struct {
name string
sig []byte
isValid bool
}{
{
name: "valid signature",
sig: hexToBytes("304402204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: true,
},
{
name: "empty.",
sig: nil,
isValid: false,
},
{
name: "bad magic",
sig: hexToBytes("314402204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "bad 1st int marker magic",
sig: hexToBytes("304403204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "bad 2nd int marker",
sig: hexToBytes("304402204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41032018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "short len",
sig: hexToBytes("304302204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "long len",
sig: hexToBytes("304502204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "long X",
sig: hexToBytes("304402424e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "long Y",
sig: hexToBytes("304402204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022118152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "short Y",
sig: hexToBytes("304402204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41021918152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "trailing crap",
sig: hexToBytes("304402204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d0901"),
isValid: false,
},
{
name: "X == N ",
sig: hexToBytes("30440220fffffffffffffffffffffffffffff" +
"ffebaaedce6af48a03bbfd25e8cd0364141022018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "X == N ",
sig: hexToBytes("30440220fffffffffffffffffffffffffffff" +
"ffebaaedce6af48a03bbfd25e8cd0364142022018152" +
"2ec8eca07de4860a4acdd12909d831cc56cbbac46220" +
"82221a8768d1d09"),
isValid: false,
},
{
name: "Y == N",
sig: hexToBytes("304402204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd410220fffff" +
"ffffffffffffffffffffffffffebaaedce6af48a03bb" +
"fd25e8cd0364141"),
isValid: false,
},
{
name: "Y > N",
sig: hexToBytes("304402204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd410220fffff" +
"ffffffffffffffffffffffffffebaaedce6af48a03bb" +
"fd25e8cd0364142"),
isValid: false,
},
{
name: "0 len X",
sig: hexToBytes("302402000220181522ec8eca07de4860a4acd" +
"d12909d831cc56cbbac4622082221a8768d1d09"),
isValid: false,
},
{
name: "0 len Y",
sig: hexToBytes("302402204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd410200"),
isValid: false,
},
{
name: "extra R padding",
sig: hexToBytes("30450221004e45e16932b8af514961a1d3a1a" +
"25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181" +
"522ec8eca07de4860a4acdd12909d831cc56cbbac462" +
"2082221a8768d1d09"),
isValid: false,
},
{
name: "extra S padding",
sig: hexToBytes("304502204e45e16932b8af514961a1d3a1a25" +
"fdf3f4f7732e9d624c6c61548ab5fb8cd41022100181" +
"522ec8eca07de4860a4acdd12909d831cc56cbbac462" +
"2082221a8768d1d09"),
isValid: false,
},
}
vm := Engine{flags: ScriptVerifyStrictEncoding}
for _, test := range tests {
err := vm.checkSignatureEncoding(test.sig)
if err != nil && test.isValid {
t.Errorf("checkSignatureEncoding test '%s' failed "+
"when it should have succeeded: %v", test.name,
err)
} else if err == nil && !test.isValid {
t.Errorf("checkSignatureEncooding test '%s' succeeded "+
"when it should have failed", test.name)
}
}
}