Bitcoin Core  22.99.0
P2P Digital Currency
script_parse_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <core_io.h>
6 #include <script/script.h>
7 #include <util/strencodings.h>
9 
10 #include <boost/test/unit_test.hpp>
11 
12 BOOST_AUTO_TEST_SUITE(script_parse_tests)
13 BOOST_AUTO_TEST_CASE(parse_script)
14 {
15  const std::vector<std::pair<std::string,std::string>> IN_OUT{
16  // {IN: script string , OUT: hex string }
17  {"", ""},
18  {"0", "00"},
19  {"1", "51"},
20  {"2", "52"},
21  {"3", "53"},
22  {"4", "54"},
23  {"5", "55"},
24  {"6", "56"},
25  {"7", "57"},
26  {"8", "58"},
27  {"9", "59"},
28  {"10", "5a"},
29  {"11", "5b"},
30  {"12", "5c"},
31  {"13", "5d"},
32  {"14", "5e"},
33  {"15", "5f"},
34  {"16", "60"},
35  {"17", "0111"},
36  {"-9", "0189"},
37  {"0x17", "17"},
38  {"'17'", "023137"},
39  {"ELSE", "67"},
40  {"NOP10", "b9"},
41  };
42  std::string all_in;
43  std::string all_out;
44  for (const auto& [in, out] : IN_OUT) {
46  all_in += " " + in + " ";
47  all_out += out;
48  }
49  BOOST_CHECK_EQUAL(HexStr(ParseScript(all_in)), all_out);
50 
51  BOOST_CHECK_EXCEPTION(ParseScript("11111111111111111111"), std::runtime_error, HasReason("script parse error: decimal numeric value only allowed in the range -0xFFFFFFFF...0xFFFFFFFF"));
52  BOOST_CHECK_EXCEPTION(ParseScript("11111111111"), std::runtime_error, HasReason("script parse error: decimal numeric value only allowed in the range -0xFFFFFFFF...0xFFFFFFFF"));
53  BOOST_CHECK_EXCEPTION(ParseScript("OP_CHECKSIGADD"), std::runtime_error, HasReason("script parse error: unknown opcode"));
54 }
HasReason
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: setup_common.h:217
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
setup_common.h
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(parse_script)
Definition: script_parse_tests.cpp:13
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
core_io.h
strencodings.h
ParseScript
CScript ParseScript(const std::string &s)
Definition: core_read.cpp:55
script.h
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:516
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18