forked from btcsuite/btcscript
/
scriptbuilder_test.go
249 lines (235 loc) · 10.7 KB
/
scriptbuilder_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
// Copyright (c) 2013-2014 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcscript_test
import (
"bytes"
"github.com/conformal/btcscript"
"testing"
)
// TestScriptBuilderAddOp tests that pushing opcodes to a script via the
// ScriptBuilder API works as expected.
func TestScriptBuilderAddOp(t *testing.T) {
tests := []struct {
name string
opcodes []byte
expected []byte
}{
{
name: "push OP_0",
opcodes: []byte{btcscript.OP_0},
expected: []byte{btcscript.OP_0},
},
{
name: "push OP_1 OP_2",
opcodes: []byte{btcscript.OP_1, btcscript.OP_2},
expected: []byte{btcscript.OP_1, btcscript.OP_2},
},
{
name: "push OP_HASH160 OP_EQUAL",
opcodes: []byte{btcscript.OP_HASH160, btcscript.OP_EQUAL},
expected: []byte{btcscript.OP_HASH160, btcscript.OP_EQUAL},
},
}
builder := btcscript.NewScriptBuilder()
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
builder.Reset()
for _, opcode := range test.opcodes {
builder.AddOp(opcode)
}
result := builder.Script()
if !bytes.Equal(result, test.expected) {
t.Errorf("ScriptBuilder.AddOp #%d (%s) wrong result\n"+
"got: %x\nwant: %x", i, test.name, result,
test.expected)
continue
}
}
}
// TestScriptBuilderAddInt64 tests that pushing signed integers to a script via
// the ScriptBuilder API works as expected.
func TestScriptBuilderAddInt64(t *testing.T) {
tests := []struct {
name string
val int64
expected []byte
}{
{name: "push -1", val: -1, expected: []byte{btcscript.OP_1NEGATE}},
{name: "push small int 0", val: 0, expected: []byte{btcscript.OP_0}},
{name: "push small int 1", val: 1, expected: []byte{btcscript.OP_1}},
{name: "push small int 2", val: 2, expected: []byte{btcscript.OP_2}},
{name: "push small int 3", val: 3, expected: []byte{btcscript.OP_3}},
{name: "push small int 4", val: 4, expected: []byte{btcscript.OP_4}},
{name: "push small int 5", val: 5, expected: []byte{btcscript.OP_5}},
{name: "push small int 6", val: 6, expected: []byte{btcscript.OP_6}},
{name: "push small int 7", val: 7, expected: []byte{btcscript.OP_7}},
{name: "push small int 8", val: 8, expected: []byte{btcscript.OP_8}},
{name: "push small int 9", val: 9, expected: []byte{btcscript.OP_9}},
{name: "push small int 10", val: 10, expected: []byte{btcscript.OP_10}},
{name: "push small int 11", val: 11, expected: []byte{btcscript.OP_11}},
{name: "push small int 12", val: 12, expected: []byte{btcscript.OP_12}},
{name: "push small int 13", val: 13, expected: []byte{btcscript.OP_13}},
{name: "push small int 14", val: 14, expected: []byte{btcscript.OP_14}},
{name: "push small int 15", val: 15, expected: []byte{btcscript.OP_15}},
{name: "push small int 16", val: 16, expected: []byte{btcscript.OP_16}},
{name: "push 17", val: 17, expected: []byte{btcscript.OP_DATA_1, 0x11}},
{name: "push 65", val: 65, expected: []byte{btcscript.OP_DATA_1, 0x41}},
{name: "push 127", val: 127, expected: []byte{btcscript.OP_DATA_1, 0x7f}},
{name: "push 128", val: 128, expected: []byte{btcscript.OP_DATA_2, 0x80, 0}},
{name: "push 255", val: 255, expected: []byte{btcscript.OP_DATA_2, 0xff, 0}},
{name: "push 256", val: 256, expected: []byte{btcscript.OP_DATA_2, 0, 0x01}},
{name: "push 32767", val: 32767, expected: []byte{btcscript.OP_DATA_2, 0xff, 0x7f}},
{name: "push 32768", val: 32768, expected: []byte{btcscript.OP_DATA_3, 0, 0x80, 0}},
{name: "push -2", val: -2, expected: []byte{btcscript.OP_DATA_1, 0x82}},
{name: "push -3", val: -3, expected: []byte{btcscript.OP_DATA_1, 0x83}},
{name: "push -4", val: -4, expected: []byte{btcscript.OP_DATA_1, 0x84}},
{name: "push -5", val: -5, expected: []byte{btcscript.OP_DATA_1, 0x85}},
{name: "push -17", val: -17, expected: []byte{btcscript.OP_DATA_1, 0x91}},
{name: "push -65", val: -65, expected: []byte{btcscript.OP_DATA_1, 0xc1}},
{name: "push -127", val: -127, expected: []byte{btcscript.OP_DATA_1, 0xff}},
{name: "push -128", val: -128, expected: []byte{btcscript.OP_DATA_2, 0x80, 0x80}},
{name: "push -255", val: -255, expected: []byte{btcscript.OP_DATA_2, 0xff, 0x80}},
{name: "push -256", val: -256, expected: []byte{btcscript.OP_DATA_2, 0x00, 0x81}},
{name: "push -32767", val: -32767, expected: []byte{btcscript.OP_DATA_2, 0xff, 0xff}},
{name: "push -32768", val: -32768, expected: []byte{btcscript.OP_DATA_3, 0x00, 0x80, 0x80}},
}
builder := btcscript.NewScriptBuilder()
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
builder.Reset().AddInt64(test.val)
result := builder.Script()
if !bytes.Equal(result, test.expected) {
t.Errorf("ScriptBuilder.AddInt64 #%d (%s) wrong result\n"+
"got: %x\nwant: %x", i, test.name, result,
test.expected)
continue
}
}
}
// TestScriptBuilderAddUint64 tests that pushing unsigned integers to a script
// via the ScriptBuilder API works as expected.
func TestScriptBuilderAddUint64(t *testing.T) {
tests := []struct {
name string
val uint64
expected []byte
}{
{name: "push small int 0", val: 0, expected: []byte{btcscript.OP_0}},
{name: "push small int 1", val: 1, expected: []byte{btcscript.OP_1}},
{name: "push small int 2", val: 2, expected: []byte{btcscript.OP_2}},
{name: "push small int 3", val: 3, expected: []byte{btcscript.OP_3}},
{name: "push small int 4", val: 4, expected: []byte{btcscript.OP_4}},
{name: "push small int 5", val: 5, expected: []byte{btcscript.OP_5}},
{name: "push small int 6", val: 6, expected: []byte{btcscript.OP_6}},
{name: "push small int 7", val: 7, expected: []byte{btcscript.OP_7}},
{name: "push small int 8", val: 8, expected: []byte{btcscript.OP_8}},
{name: "push small int 9", val: 9, expected: []byte{btcscript.OP_9}},
{name: "push small int 10", val: 10, expected: []byte{btcscript.OP_10}},
{name: "push small int 11", val: 11, expected: []byte{btcscript.OP_11}},
{name: "push small int 12", val: 12, expected: []byte{btcscript.OP_12}},
{name: "push small int 13", val: 13, expected: []byte{btcscript.OP_13}},
{name: "push small int 14", val: 14, expected: []byte{btcscript.OP_14}},
{name: "push small int 15", val: 15, expected: []byte{btcscript.OP_15}},
{name: "push small int 16", val: 16, expected: []byte{btcscript.OP_16}},
{name: "push 17", val: 17, expected: []byte{btcscript.OP_DATA_1, 0x11}},
{name: "push 65", val: 65, expected: []byte{btcscript.OP_DATA_1, 0x41}},
{name: "push 127", val: 127, expected: []byte{btcscript.OP_DATA_1, 0x7f}},
{name: "push 128", val: 128, expected: []byte{btcscript.OP_DATA_2, 0x80, 0}},
{name: "push 255", val: 255, expected: []byte{btcscript.OP_DATA_2, 0xff, 0}},
{name: "push 256", val: 256, expected: []byte{btcscript.OP_DATA_2, 0, 0x01}},
{name: "push 32767", val: 32767, expected: []byte{btcscript.OP_DATA_2, 0xff, 0x7f}},
{name: "push 32768", val: 32768, expected: []byte{btcscript.OP_DATA_3, 0, 0x80, 0}},
}
builder := btcscript.NewScriptBuilder()
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
builder.Reset().AddUint64(test.val)
result := builder.Script()
if !bytes.Equal(result, test.expected) {
t.Errorf("ScriptBuilder.AddUint64 #%d (%s) wrong result\n"+
"got: %x\nwant: %x", i, test.name, result,
test.expected)
continue
}
}
}
// TestScriptBuilderAddData tests that pushing data to a script via the
// ScriptBuilder API works as expected.
func TestScriptBuilderAddData(t *testing.T) {
tests := []struct {
name string
data []byte
expected []byte
}{
// Start off with the small ints to ensure canonical encoding.
{name: "push small int 0", data: []byte{0}, expected: []byte{btcscript.OP_0}},
{name: "push small int 1", data: []byte{1}, expected: []byte{btcscript.OP_1}},
{name: "push small int 2", data: []byte{2}, expected: []byte{btcscript.OP_2}},
{name: "push small int 3", data: []byte{3}, expected: []byte{btcscript.OP_3}},
{name: "push small int 4", data: []byte{4}, expected: []byte{btcscript.OP_4}},
{name: "push small int 5", data: []byte{5}, expected: []byte{btcscript.OP_5}},
{name: "push small int 6", data: []byte{6}, expected: []byte{btcscript.OP_6}},
{name: "push small int 7", data: []byte{7}, expected: []byte{btcscript.OP_7}},
{name: "push small int 8", data: []byte{8}, expected: []byte{btcscript.OP_8}},
{name: "push small int 9", data: []byte{9}, expected: []byte{btcscript.OP_9}},
{name: "push small int 10", data: []byte{10}, expected: []byte{btcscript.OP_10}},
{name: "push small int 11", data: []byte{11}, expected: []byte{btcscript.OP_11}},
{name: "push small int 12", data: []byte{12}, expected: []byte{btcscript.OP_12}},
{name: "push small int 13", data: []byte{13}, expected: []byte{btcscript.OP_13}},
{name: "push small int 14", data: []byte{14}, expected: []byte{btcscript.OP_14}},
{name: "push small int 15", data: []byte{15}, expected: []byte{btcscript.OP_15}},
{name: "push small int 16", data: []byte{16}, expected: []byte{btcscript.OP_16}},
// 1-byte data push opcodes.
{
name: "push data len 17",
data: bytes.Repeat([]byte{0x49}, 17),
expected: append([]byte{btcscript.OP_DATA_17}, bytes.Repeat([]byte{0x49}, 17)...),
},
{
name: "push data len 75",
data: bytes.Repeat([]byte{0x49}, 75),
expected: append([]byte{btcscript.OP_DATA_75}, bytes.Repeat([]byte{0x49}, 75)...),
},
// 2-byte data push via OP_PUSHDATA_1.
{
name: "push data len 76",
data: bytes.Repeat([]byte{0x49}, 76),
expected: append([]byte{btcscript.OP_PUSHDATA1, 76}, bytes.Repeat([]byte{0x49}, 76)...),
},
{
name: "push data len 255",
data: bytes.Repeat([]byte{0x49}, 255),
expected: append([]byte{btcscript.OP_PUSHDATA1, 255}, bytes.Repeat([]byte{0x49}, 255)...),
},
// 3-byte data push via OP_PUSHDATA_2.
{
name: "push data len 256",
data: bytes.Repeat([]byte{0x49}, 256),
expected: append([]byte{btcscript.OP_PUSHDATA2, 0, 1}, bytes.Repeat([]byte{0x49}, 256)...),
},
{
name: "push data len 32767",
data: bytes.Repeat([]byte{0x49}, 32767),
expected: append([]byte{btcscript.OP_PUSHDATA2, 255, 127}, bytes.Repeat([]byte{0x49}, 32767)...),
},
// 5-byte data push via OP_PUSHDATA_4.
{
name: "push data len 65536",
data: bytes.Repeat([]byte{0x49}, 65536),
expected: append([]byte{btcscript.OP_PUSHDATA4, 0, 0, 1, 0}, bytes.Repeat([]byte{0x49}, 65536)...),
},
}
builder := btcscript.NewScriptBuilder()
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
builder.Reset().AddData(test.data)
result := builder.Script()
if !bytes.Equal(result, test.expected) {
t.Errorf("ScriptBuilder.AddData #%d (%s) wrong result\n"+
"got: %x\nwant: %x", i, test.name, result,
test.expected)
continue
}
}
}