This repository was archived by the owner on Sep 14, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathscript_segwit_tests.cpp
More file actions
219 lines (178 loc) · 6.87 KB
/
Copy pathscript_segwit_tests.cpp
File metadata and controls
219 lines (178 loc) · 6.87 KB
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
// Copyright (c) 2012-2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <script/names.h>
#include <script/script.h>
#include <test/util/setup_common.h>
#include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(script_segwit_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Valid)
{
uint256 dummy;
CScript p2wsh;
p2wsh << OP_0 << ToByteVector(dummy);
BOOST_CHECK(p2wsh.IsPayToWitnessScriptHash(false));
std::vector<unsigned char> bytes = {OP_0, 32};
bytes.insert(bytes.end(), 32, 0);
BOOST_CHECK(CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash(false));
}
BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_NotOp0)
{
uint256 dummy;
CScript notp2wsh;
notp2wsh << OP_1 << ToByteVector(dummy);
BOOST_CHECK(!notp2wsh.IsPayToWitnessScriptHash (false));
}
BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_Size)
{
uint160 dummy;
CScript notp2wsh;
notp2wsh << OP_0 << ToByteVector(dummy);
BOOST_CHECK(!notp2wsh.IsPayToWitnessScriptHash (false));
}
BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_Nop)
{
uint256 dummy;
CScript notp2wsh;
notp2wsh << OP_0 << OP_NOP << ToByteVector(dummy);
BOOST_CHECK(!notp2wsh.IsPayToWitnessScriptHash (false));
}
BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_EmptyScript)
{
CScript notp2wsh;
BOOST_CHECK(!notp2wsh.IsPayToWitnessScriptHash (false));
}
BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_Invalid_Pushdata)
{
// A script is not P2WSH if OP_PUSHDATA is used to push the hash.
std::vector<unsigned char> bytes = {OP_0, OP_PUSHDATA1, 32};
bytes.insert(bytes.end(), 32, 0);
BOOST_CHECK(!CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash(false));
bytes = {OP_0, OP_PUSHDATA2, 32, 0};
bytes.insert(bytes.end(), 32, 0);
BOOST_CHECK(!CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash(false));
bytes = {OP_0, OP_PUSHDATA4, 32, 0, 0, 0};
bytes.insert(bytes.end(), 32, 0);
BOOST_CHECK(!CScript(bytes.begin(), bytes.end()).IsPayToWitnessScriptHash(false));
}
BOOST_AUTO_TEST_CASE(IsPayToWitnessScriptHash_NamePrefix)
{
uint256 dummy;
CScript p2wsh;
p2wsh << OP_0 << ToByteVector(dummy);
BOOST_CHECK(p2wsh.IsPayToWitnessScriptHash(true));
BOOST_CHECK(p2wsh.IsPayToWitnessScriptHash(false));
const valtype name(10, 'a');
const valtype value(20, 'b');
const CScript nameP2WSH = CNameScript::buildNameUpdate(p2wsh, name, value);
BOOST_CHECK(nameP2WSH.IsPayToWitnessScriptHash(true));
BOOST_CHECK(!nameP2WSH.IsPayToWitnessScriptHash(false));
}
namespace {
bool IsExpectedWitnessProgram(const bool allowNames, const CScript& script, const int expectedVersion, const std::vector<unsigned char>& expectedProgram)
{
int actualVersion;
std::vector<unsigned char> actualProgram;
if (!script.IsWitnessProgram(allowNames, actualVersion, actualProgram)) {
return false;
}
BOOST_CHECK_EQUAL(actualVersion, expectedVersion);
BOOST_CHECK(actualProgram == expectedProgram);
return true;
}
bool IsNoWitnessProgram(const bool allowNames, const CScript& script)
{
int dummyVersion;
std::vector<unsigned char> dummyProgram;
return !script.IsWitnessProgram(allowNames, dummyVersion, dummyProgram);
}
} // anonymous namespace
BOOST_AUTO_TEST_CASE(IsWitnessProgram_Valid)
{
std::vector<unsigned char> program = {42, 18};
CScript wit;
wit << OP_0 << program;
BOOST_CHECK(IsExpectedWitnessProgram(false, wit, 0, program));
wit.clear();
program.resize(40);
wit << OP_16 << program;
BOOST_CHECK(IsExpectedWitnessProgram(false, wit, 16, program));
program.resize(32);
std::vector<unsigned char> bytes = {OP_5, static_cast<unsigned char>(program.size())};
bytes.insert(bytes.end(), program.begin(), program.end());
BOOST_CHECK(IsExpectedWitnessProgram(false, CScript(bytes.begin(), bytes.end()), 5, program));
}
BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_Version)
{
std::vector<unsigned char> program(10);
CScript nowit;
nowit << OP_1NEGATE << program;
BOOST_CHECK(IsNoWitnessProgram(false, nowit));
}
BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_Size)
{
std::vector<unsigned char> program(1);
CScript nowit;
nowit << OP_0 << program;
BOOST_CHECK(IsNoWitnessProgram(false, nowit));
nowit.clear();
program.resize(41);
nowit << OP_0 << program;
BOOST_CHECK(IsNoWitnessProgram(false, nowit));
}
BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_Nop)
{
std::vector<unsigned char> program(10);
CScript nowit;
nowit << OP_0 << OP_NOP << program;
BOOST_CHECK(IsNoWitnessProgram(false, nowit));
}
BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_EmptyScript)
{
CScript nowit;
BOOST_CHECK(IsNoWitnessProgram(false, nowit));
}
BOOST_AUTO_TEST_CASE(IsWitnessProgram_Invalid_Pushdata)
{
// A script is no witness program if OP_PUSHDATA is used to push the hash.
std::vector<unsigned char> bytes = {OP_0, OP_PUSHDATA1, 32};
bytes.insert(bytes.end(), 32, 0);
BOOST_CHECK(IsNoWitnessProgram(false, CScript(bytes.begin(), bytes.end())));
bytes = {OP_0, OP_PUSHDATA2, 32, 0};
bytes.insert(bytes.end(), 32, 0);
BOOST_CHECK(IsNoWitnessProgram(false, CScript(bytes.begin(), bytes.end())));
bytes = {OP_0, OP_PUSHDATA4, 32, 0, 0, 0};
bytes.insert(bytes.end(), 32, 0);
BOOST_CHECK(IsNoWitnessProgram(false, CScript(bytes.begin(), bytes.end())));
}
BOOST_AUTO_TEST_CASE(IsWitnessProgram_WithNamePrefix)
{
const std::vector<unsigned char> program(20, 42);
CScript wit;
wit << OP_0 << program;
BOOST_CHECK(IsExpectedWitnessProgram(true, wit, 0, program));
BOOST_CHECK(IsExpectedWitnessProgram(false, wit, 0, program));
const valtype name(10, 'a');
const valtype value(20, 'b');
const CScript nameWit = CNameScript::buildNameUpdate(wit, name, value);
BOOST_CHECK(IsExpectedWitnessProgram(true, nameWit, 0, program));
BOOST_CHECK(!IsExpectedWitnessProgram(false, nameWit, 0, program));
}
BOOST_AUTO_TEST_CASE(IsWitnessProgram_NamePrefixNotMisinterpreted)
{
/* Name prefixes themselves start with OP_1 to OP_3, which is also
a valid start for a witness program. Make sure that they are not
misinterpreted as witness programs. */
const valtype name(10, 'a');
const valtype value(20, 'b');
const valtype rand(15, 'x');
const CScript nameNew = CNameScript::buildNameNew(CScript(), name, rand);
const CScript nameFirst = CNameScript::buildNameFirstupdate(CScript(), name, value, rand);
const CScript nameUpdate = CNameScript::buildNameUpdate(CScript(), name, value);
for (const auto& scr : {nameNew, nameFirst, nameUpdate})
{
BOOST_CHECK(IsNoWitnessProgram(true, scr));
BOOST_CHECK(IsNoWitnessProgram(false, scr));
}
}
BOOST_AUTO_TEST_SUITE_END()