Branch data Line data Source code
1 : : // Copyright (c) 2011-2022 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 <clientversion.h>
6 : : #include <common/signmessage.h> // For MessageSign(), MessageVerify(), MESSAGE_MAGIC
7 : : #include <hash.h> // For Hash()
8 : : #include <key.h> // For CKey
9 : : #include <script/parsing.h>
10 : : #include <span.h>
11 : : #include <sync.h>
12 : : #include <test/util/random.h>
13 : : #include <test/util/setup_common.h>
14 : : #include <uint256.h>
15 : : #include <util/bitdeque.h>
16 : : #include <util/fs.h>
17 : : #include <util/fs_helpers.h>
18 : : #include <util/moneystr.h>
19 : : #include <util/overflow.h>
20 : : #include <util/readwritefile.h>
21 : : #include <util/strencodings.h>
22 : : #include <util/string.h>
23 : : #include <util/time.h>
24 : : #include <util/vector.h>
25 : :
26 : : #include <array>
27 : : #include <cmath>
28 : : #include <fstream>
29 : : #include <limits>
30 : : #include <map>
31 : : #include <optional>
32 : : #include <stdint.h>
33 : : #include <string.h>
34 : : #include <thread>
35 : : #include <univalue.h>
36 : : #include <utility>
37 : : #include <vector>
38 : :
39 : : #include <sys/types.h>
40 : :
41 : : #ifndef WIN32
42 : : #include <signal.h>
43 : : #include <sys/wait.h>
44 : : #endif
45 : :
46 : : #include <boost/test/unit_test.hpp>
47 : :
48 : : using namespace std::literals;
49 : : using namespace util::hex_literals;
50 : : using util::ConstevalHexDigit;
51 : : using util::Join;
52 : : using util::RemovePrefix;
53 : : using util::RemovePrefixView;
54 : : using util::ReplaceAll;
55 : : using util::Split;
56 : : using util::SplitString;
57 : : using util::TrimString;
58 : : using util::TrimStringView;
59 : :
60 : : static const std::string STRING_WITH_EMBEDDED_NULL_CHAR{"1"s "\0" "1"s};
61 : :
62 : : /* defined in logging.cpp */
63 : : namespace BCLog {
64 : : std::string LogEscapeMessage(std::string_view str);
65 : : }
66 : :
67 : : BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup)
68 : :
69 : : namespace {
70 : : class NoCopyOrMove
71 : : {
72 : : public:
73 : : int i;
74 : 2 : explicit NoCopyOrMove(int i) : i{i} { }
75 : :
76 : : NoCopyOrMove() = delete;
77 : : NoCopyOrMove(const NoCopyOrMove&) = delete;
78 : : NoCopyOrMove(NoCopyOrMove&&) = delete;
79 : : NoCopyOrMove& operator=(const NoCopyOrMove&) = delete;
80 : : NoCopyOrMove& operator=(NoCopyOrMove&&) = delete;
81 : :
82 [ - + ]: 6 : operator bool() const { return i != 0; }
83 : :
84 : 8 : int get_ip1() { return i + 1; }
85 : 4 : bool test()
86 : : {
87 : : // Check that Assume can be used within a lambda and still call methods
88 : 8 : [&]() { Assume(get_ip1()); }();
89 : 4 : return Assume(get_ip1() != 5);
90 : : }
91 : : };
92 : : } // namespace
93 : :
94 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(util_check)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
95 : : {
96 : : // Check that Assert can forward
97 [ + - ]: 2 : const std::unique_ptr<int> p_two = Assert(std::make_unique<int>(2));
98 : : // Check that Assert works on lvalues and rvalues
99 [ + - + - ]: 2 : const int two = *Assert(p_two);
100 [ + - ]: 2 : Assert(two == 2);
101 [ + - ]: 2 : Assert(true);
102 : : // Check that Assume can be used as unary expression
103 [ + - ]: 2 : const bool result{Assume(two == 2)};
104 [ + - ]: 2 : Assert(result);
105 : :
106 : : // Check that Assert doesn't require copy/move
107 : 2 : NoCopyOrMove x{9};
108 [ + - ]: 2 : Assert(x).i += 3;
109 [ + - + - ]: 2 : Assert(x).test();
110 : :
111 : : // Check nested Asserts
112 [ + - + - : 2 : BOOST_CHECK_EQUAL(Assert((Assert(x).test() ? 3 : 0)), 3);
+ - - + +
- + - ]
113 : :
114 : : // Check -Wdangling-gsl does not trigger when copying the int. (It would
115 : : // trigger on "const int&")
116 : 2 : const int nine{*Assert(std::optional<int>{9})};
117 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(9, nine);
118 : 2 : }
119 : :
120 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(util_criticalsection)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
121 : : {
122 : 2 : RecursiveMutex cs;
123 : :
124 : 4 : do {
125 : 2 : LOCK(cs);
126 [ + - ]: 2 : break;
127 : :
128 : : BOOST_ERROR("break was swallowed!");
129 : 2 : } while(0);
130 : :
131 : 2 : do {
132 : 2 : TRY_LOCK(cs, lockTest);
133 [ + - ]: 2 : if (lockTest) {
134 [ + - + - : 4 : BOOST_CHECK(true); // Needed to suppress "Test case [...] did not check any assertions"
+ - ]
135 [ + - ]: 2 : break;
136 : : }
137 : :
138 [ # # # # : 0 : BOOST_ERROR("break was swallowed!");
# # ]
139 : 2 : } while(0);
140 : 2 : }
141 : :
142 : : constexpr char HEX_PARSE_INPUT[] = "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f";
143 : : constexpr uint8_t HEX_PARSE_OUTPUT[] = {
144 : : 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7,
145 : : 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde,
146 : : 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12,
147 : : 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
148 : : 0x5f
149 : : };
150 : : static_assert((sizeof(HEX_PARSE_INPUT) - 1) == 2 * sizeof(HEX_PARSE_OUTPUT));
151 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(parse_hex)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
152 : : {
153 : 2 : std::vector<unsigned char> result;
154 : :
155 : : // Basic test vector
156 [ + - ]: 2 : std::vector<unsigned char> expected(std::begin(HEX_PARSE_OUTPUT), std::end(HEX_PARSE_OUTPUT));
157 : 2 : constexpr std::array<std::byte, 65> hex_literal_array{operator""_hex<util::detail::Hex(HEX_PARSE_INPUT)>()};
158 : 2 : auto hex_literal_span{MakeUCharSpan(hex_literal_array)};
159 [ + - + - : 4 : BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_span.begin(), hex_literal_span.end(), expected.begin(), expected.end());
+ - + - ]
160 : :
161 [ + - ]: 2 : const std::vector<std::byte> hex_literal_vector{operator""_hex_v<util::detail::Hex(HEX_PARSE_INPUT)>()};
162 : 2 : hex_literal_span = MakeUCharSpan(hex_literal_vector);
163 [ + - + - : 4 : BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_span.begin(), hex_literal_span.end(), expected.begin(), expected.end());
+ - + - ]
164 : :
165 : 2 : constexpr std::array<uint8_t, 65> hex_literal_array_uint8{operator""_hex_u8<util::detail::Hex(HEX_PARSE_INPUT)>()};
166 [ + - + - : 4 : BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_array_uint8.begin(), hex_literal_array_uint8.end(), expected.begin(), expected.end());
+ - + - ]
167 : :
168 [ + - ]: 4 : result = operator""_hex_v_u8<util::detail::Hex(HEX_PARSE_INPUT)>();
169 [ + - + - : 4 : BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
+ - + - ]
170 : :
171 [ + - ]: 4 : result = ParseHex(HEX_PARSE_INPUT);
172 [ + - + - : 4 : BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
+ - + - ]
173 : :
174 [ + - ]: 4 : result = TryParseHex<uint8_t>(HEX_PARSE_INPUT).value();
175 [ + - + - : 4 : BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
+ - + - ]
176 : :
177 : : // Spaces between bytes must be supported
178 [ + - ]: 2 : expected = {0x12, 0x34, 0x56, 0x78};
179 [ + - ]: 4 : result = ParseHex("12 34 56 78");
180 [ + - + - : 4 : BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
+ - + - ]
181 [ + - ]: 4 : result = TryParseHex<uint8_t>("12 34 56 78").value();
182 [ + - + - : 4 : BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
+ - + - ]
183 : :
184 : : // Leading space must be supported (used in BerkeleyEnvironment::Salvage)
185 [ + - ]: 2 : expected = {0x89, 0x34, 0x56, 0x78};
186 [ + - ]: 4 : result = ParseHex(" 89 34 56 78");
187 [ + - + - : 4 : BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
+ - + - ]
188 [ + - ]: 4 : result = TryParseHex<uint8_t>(" 89 34 56 78").value();
189 [ + - + - : 4 : BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
+ - + - ]
190 : :
191 : : // Mixed case and spaces are supported
192 [ + - ]: 2 : expected = {0xff, 0xaa};
193 [ + - ]: 4 : result = ParseHex(" Ff aA ");
194 [ + - + - : 4 : BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
+ - + - ]
195 [ + - ]: 4 : result = TryParseHex<uint8_t>(" Ff aA ").value();
196 [ + - + - : 4 : BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
+ - + - ]
197 : :
198 : : // Empty string is supported
199 : 2 : static_assert(""_hex.empty());
200 : 2 : static_assert(""_hex_u8.empty());
201 [ + - + - : 2 : BOOST_CHECK_EQUAL(""_hex_v.size(), 0);
+ - ]
202 [ + - + - : 2 : BOOST_CHECK_EQUAL(""_hex_v_u8.size(), 0);
+ - ]
203 [ + - + - : 2 : BOOST_CHECK_EQUAL(ParseHex("").size(), 0);
+ - ]
204 [ + - + - : 4 : BOOST_CHECK_EQUAL(TryParseHex<uint8_t>("").value().size(), 0);
+ - ]
205 : :
206 : : // Spaces between nibbles is treated as invalid
207 [ + - + - : 2 : BOOST_CHECK_EQUAL(ParseHex("AAF F").size(), 0);
+ - ]
208 [ + - + - : 4 : BOOST_CHECK(!TryParseHex("AAF F").has_value());
+ - + - ]
209 : :
210 : : // Embedded null is treated as invalid
211 [ + - ]: 2 : const std::string with_embedded_null{" 11 "s
212 : : " \0 "
213 : 2 : " 22 "s};
214 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(with_embedded_null.size(), 11);
215 [ + - + - : 2 : BOOST_CHECK_EQUAL(ParseHex(with_embedded_null).size(), 0);
+ - ]
216 [ + - + - : 4 : BOOST_CHECK(!TryParseHex(with_embedded_null).has_value());
+ - + - ]
217 : :
218 : : // Non-hex is treated as invalid
219 [ + - + - : 2 : BOOST_CHECK_EQUAL(ParseHex("1234 invalid 1234").size(), 0);
+ - ]
220 [ + - + - : 4 : BOOST_CHECK(!TryParseHex("1234 invalid 1234").has_value());
+ - + - ]
221 : :
222 : : // Truncated input is treated as invalid
223 [ + - + - : 2 : BOOST_CHECK_EQUAL(ParseHex("12 3").size(), 0);
+ - ]
224 [ + - + - : 4 : BOOST_CHECK(!TryParseHex("12 3").has_value());
+ - ]
225 : 2 : }
226 : :
227 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(consteval_hex_digit)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
228 : : {
229 [ + - ]: 2 : BOOST_CHECK_EQUAL(ConstevalHexDigit('0'), 0);
230 [ + - ]: 2 : BOOST_CHECK_EQUAL(ConstevalHexDigit('9'), 9);
231 [ + - ]: 2 : BOOST_CHECK_EQUAL(ConstevalHexDigit('a'), 0xa);
232 [ + - ]: 2 : BOOST_CHECK_EQUAL(ConstevalHexDigit('f'), 0xf);
233 : 2 : }
234 : :
235 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(util_HexStr)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
236 : : {
237 [ + - ]: 2 : BOOST_CHECK_EQUAL(HexStr(HEX_PARSE_OUTPUT), HEX_PARSE_INPUT);
238 [ + - ]: 2 : BOOST_CHECK_EQUAL(HexStr(Span{HEX_PARSE_OUTPUT}.last(0)), "");
239 [ + - ]: 2 : BOOST_CHECK_EQUAL(HexStr(Span{HEX_PARSE_OUTPUT}.first(0)), "");
240 : :
241 : 2 : {
242 : 2 : constexpr std::string_view out_exp{"04678afdb0"};
243 : 2 : constexpr std::span in_s{HEX_PARSE_OUTPUT, out_exp.size() / 2};
244 : 2 : const Span<const uint8_t> in_u{MakeUCharSpan(in_s)};
245 : 2 : const Span<const std::byte> in_b{MakeByteSpan(in_s)};
246 : :
247 [ + - ]: 2 : BOOST_CHECK_EQUAL(HexStr(in_u), out_exp);
248 [ + - ]: 2 : BOOST_CHECK_EQUAL(HexStr(in_s), out_exp);
249 [ + - ]: 2 : BOOST_CHECK_EQUAL(HexStr(in_b), out_exp);
250 : : }
251 : :
252 : 2 : {
253 : 2 : auto input = std::string();
254 [ + + ]: 514 : for (size_t i=0; i<256; ++i) {
255 [ + - ]: 512 : input.push_back(static_cast<char>(i));
256 : : }
257 : :
258 [ + - ]: 2 : auto hex = HexStr(input);
259 [ + - + - : 4 : BOOST_TEST_REQUIRE(hex.size() == 512);
+ - ]
260 : 2 : static constexpr auto hexmap = std::string_view("0123456789abcdef");
261 [ + + ]: 514 : for (size_t i = 0; i < 256; ++i) {
262 [ + - ]: 512 : auto upper = hexmap.find(hex[i * 2]);
263 [ + - ]: 512 : auto lower = hexmap.find(hex[i * 2 + 1]);
264 [ + - + - : 1024 : BOOST_TEST_REQUIRE(upper != std::string_view::npos);
+ - + - ]
265 [ + - + - : 1024 : BOOST_TEST_REQUIRE(lower != std::string_view::npos);
+ - + - ]
266 [ + - + - : 1024 : BOOST_TEST_REQUIRE(i == upper*16 + lower);
+ - ]
267 : : }
268 : 2 : }
269 : 2 : }
270 : :
271 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(span_write_bytes)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
272 : : {
273 : 2 : std::array mut_arr{uint8_t{0xaa}, uint8_t{0xbb}};
274 : 2 : const auto mut_bytes{MakeWritableByteSpan(mut_arr)};
275 : 2 : mut_bytes[1] = std::byte{0x11};
276 [ + - ]: 2 : BOOST_CHECK_EQUAL(mut_arr.at(0), 0xaa);
277 [ + - ]: 2 : BOOST_CHECK_EQUAL(mut_arr.at(1), 0x11);
278 : 2 : }
279 : :
280 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(util_Join)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
281 : : {
282 : : // Normal version
283 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(Join(std::vector<std::string>{}, ", "), "");
284 [ + - + + : 6 : BOOST_CHECK_EQUAL(Join(std::vector<std::string>{"foo"}, ", "), "foo");
+ - + - +
- + + - -
- - ]
285 [ + - + + : 10 : BOOST_CHECK_EQUAL(Join(std::vector<std::string>{"foo", "bar"}, ", "), "foo, bar");
+ - + - +
- + + - -
- - ]
286 : :
287 : : // Version with unary operator
288 : 8 : const auto op_upper = [](const std::string& s) { return ToUpper(s); };
289 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(Join(std::list<std::string>{}, ", ", op_upper), "");
290 [ + - + + : 6 : BOOST_CHECK_EQUAL(Join(std::list<std::string>{"foo"}, ", ", op_upper), "FOO");
+ - + - +
- + + - -
- - ]
291 [ + - + + : 10 : BOOST_CHECK_EQUAL(Join(std::list<std::string>{"foo", "bar"}, ", ", op_upper), "FOO, BAR");
+ - + - +
- + + - -
- - ]
292 : 2 : }
293 : :
294 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(util_ReplaceAll)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
295 : : {
296 : 2 : const std::string original("A test \"%s\" string '%s'.");
297 : 12 : auto test_replaceall = [&original](const std::string& search, const std::string& substitute, const std::string& expected) {
298 : 10 : auto test = original;
299 [ + - ]: 10 : ReplaceAll(test, search, substitute);
300 [ + - + - ]: 10 : BOOST_CHECK_EQUAL(test, expected);
301 : 12 : };
302 : :
303 [ + - + - : 4 : test_replaceall("", "foo", original);
+ - ]
304 [ + - + - : 4 : test_replaceall(original, "foo", "foo");
+ - ]
305 [ + - + - : 4 : test_replaceall("%s", "foo", "A test \"foo\" string 'foo'.");
+ - + - ]
306 [ + - + - : 4 : test_replaceall("\"", "foo", "A test foo%sfoo string '%s'.");
+ - + - ]
307 [ + - + - : 4 : test_replaceall("'", "foo", "A test \"%s\" string foo%sfoo.");
+ - + - ]
308 : 2 : }
309 : :
310 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(util_TrimString)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
311 : : {
312 [ + - ]: 2 : BOOST_CHECK_EQUAL(TrimString(" foo bar "), "foo bar");
313 [ + - ]: 2 : BOOST_CHECK_EQUAL(TrimStringView("\t \n \n \f\n\r\t\v\tfoo \n \f\n\r\t\v\tbar\t \n \f\n\r\t\v\t\n "), "foo \n \f\n\r\t\v\tbar");
314 [ + - ]: 2 : BOOST_CHECK_EQUAL(TrimString("\t \n foo \n\tbar\t \n "), "foo \n\tbar");
315 [ + - ]: 2 : BOOST_CHECK_EQUAL(TrimStringView("\t \n foo \n\tbar\t \n ", "fobar"), "\t \n foo \n\tbar\t \n ");
316 [ + - ]: 2 : BOOST_CHECK_EQUAL(TrimString("foo bar"), "foo bar");
317 [ + - ]: 2 : BOOST_CHECK_EQUAL(TrimStringView("foo bar", "fobar"), " ");
318 [ + - + - : 2 : BOOST_CHECK_EQUAL(TrimString(std::string("\0 foo \0 ", 8)), std::string("\0 foo \0", 7));
+ - ]
319 [ + - + - : 2 : BOOST_CHECK_EQUAL(TrimStringView(std::string(" foo ", 5)), std::string("foo", 3));
+ - ]
320 [ + - + - : 2 : BOOST_CHECK_EQUAL(TrimString(std::string("\t\t\0\0\n\n", 6)), std::string("\0\0", 2));
+ - ]
321 [ + - + - : 2 : BOOST_CHECK_EQUAL(TrimStringView(std::string("\x05\x04\x03\x02\x01\x00", 6)), std::string("\x05\x04\x03\x02\x01\x00", 6));
+ - ]
322 [ + - + - : 2 : BOOST_CHECK_EQUAL(TrimString(std::string("\x05\x04\x03\x02\x01\x00", 6), std::string("\x05\x04\x03\x02\x01", 5)), std::string("\0", 1));
+ - + - ]
323 [ + - + - : 2 : BOOST_CHECK_EQUAL(TrimStringView(std::string("\x05\x04\x03\x02\x01\x00", 6), std::string("\x05\x04\x03\x02\x01\x00", 6)), "");
+ - ]
324 : 2 : }
325 : :
326 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(util_FormatISO8601DateTime)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
327 : : {
328 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatISO8601DateTime(971890963199), "32767-12-31T23:59:59Z");
329 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatISO8601DateTime(971890876800), "32767-12-31T00:00:00Z");
330 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatISO8601DateTime(1317425777), "2011-09-30T23:36:17Z");
331 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatISO8601DateTime(0), "1970-01-01T00:00:00Z");
332 : 2 : }
333 : :
334 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(util_FormatISO8601Date)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
335 : : {
336 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatISO8601Date(971890963199), "32767-12-31");
337 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatISO8601Date(971890876800), "32767-12-31");
338 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatISO8601Date(1317425777), "2011-09-30");
339 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatISO8601Date(0), "1970-01-01");
340 : 2 : }
341 : :
342 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(util_FormatMoney)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
343 : : {
344 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(0), "0.00");
345 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney((COIN/10000)*123456789), "12345.6789");
346 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(-COIN), "-1.00");
347 : :
348 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(COIN*100000000), "100000000.00");
349 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(COIN*10000000), "10000000.00");
350 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(COIN*1000000), "1000000.00");
351 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(COIN*100000), "100000.00");
352 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(COIN*10000), "10000.00");
353 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(COIN*1000), "1000.00");
354 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(COIN*100), "100.00");
355 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(COIN*10), "10.00");
356 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(COIN), "1.00");
357 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(COIN/10), "0.10");
358 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(COIN/100), "0.01");
359 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(COIN/1000), "0.001");
360 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(COIN/10000), "0.0001");
361 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(COIN/100000), "0.00001");
362 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(COIN/1000000), "0.000001");
363 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(COIN/10000000), "0.0000001");
364 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(COIN/100000000), "0.00000001");
365 : :
366 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::max()), "92233720368.54775807");
367 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::max() - 1), "92233720368.54775806");
368 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::max() - 2), "92233720368.54775805");
369 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::max() - 3), "92233720368.54775804");
370 : : // ...
371 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::min() + 3), "-92233720368.54775805");
372 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::min() + 2), "-92233720368.54775806");
373 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::min() + 1), "-92233720368.54775807");
374 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatMoney(std::numeric_limits<CAmount>::min()), "-92233720368.54775808");
375 : 2 : }
376 : :
377 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(util_ParseMoney)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
378 : : {
379 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("0.0").value(), 0);
380 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney(".").value(), 0);
381 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("0.").value(), 0);
382 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney(".0").value(), 0);
383 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney(".6789").value(), 6789'0000);
384 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("12345.").value(), COIN * 12345);
385 : :
386 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("12345.6789").value(), (COIN/10000)*123456789);
387 : :
388 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("10000000.00").value(), COIN*10000000);
389 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("1000000.00").value(), COIN*1000000);
390 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("100000.00").value(), COIN*100000);
391 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("10000.00").value(), COIN*10000);
392 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("1000.00").value(), COIN*1000);
393 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("100.00").value(), COIN*100);
394 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("10.00").value(), COIN*10);
395 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("1.00").value(), COIN);
396 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("1").value(), COIN);
397 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney(" 1").value(), COIN);
398 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("1 ").value(), COIN);
399 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney(" 1 ").value(), COIN);
400 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("0.1").value(), COIN/10);
401 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("0.01").value(), COIN/100);
402 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("0.001").value(), COIN/1000);
403 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("0.0001").value(), COIN/10000);
404 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("0.00001").value(), COIN/100000);
405 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("0.000001").value(), COIN/1000000);
406 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("0.0000001").value(), COIN/10000000);
407 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("0.00000001").value(), COIN/100000000);
408 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney(" 0.00000001 ").value(), COIN/100000000);
409 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney("0.00000001 ").value(), COIN/100000000);
410 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(ParseMoney(" 0.00000001").value(), COIN/100000000);
411 : :
412 : : // Parsing amount that cannot be represented should fail
413 [ + - + - : 4 : BOOST_CHECK(!ParseMoney("100000000.00"));
+ - ]
414 [ + - + - : 4 : BOOST_CHECK(!ParseMoney("0.000000001"));
+ - ]
415 : :
416 : : // Parsing empty string should fail
417 [ + - + - : 4 : BOOST_CHECK(!ParseMoney(""));
+ - ]
418 [ + - + - : 4 : BOOST_CHECK(!ParseMoney(" "));
+ - ]
419 [ + - + - : 4 : BOOST_CHECK(!ParseMoney(" "));
+ - ]
420 : :
421 : : // Parsing two numbers should fail
422 [ + - + - : 4 : BOOST_CHECK(!ParseMoney(".."));
+ - ]
423 [ + - + - : 4 : BOOST_CHECK(!ParseMoney("0..0"));
+ - ]
424 [ + - + - : 4 : BOOST_CHECK(!ParseMoney("1 2"));
+ - ]
425 [ + - + - : 4 : BOOST_CHECK(!ParseMoney(" 1 2 "));
+ - ]
426 [ + - + - : 4 : BOOST_CHECK(!ParseMoney(" 1.2 3 "));
+ - ]
427 [ + - + - : 4 : BOOST_CHECK(!ParseMoney(" 1 2.3 "));
+ - ]
428 : :
429 : : // Embedded whitespace should fail
430 [ + - + - : 4 : BOOST_CHECK(!ParseMoney(" -1 .2 "));
+ - ]
431 [ + - + - : 4 : BOOST_CHECK(!ParseMoney(" 1 .2 "));
+ - ]
432 [ + - + - : 4 : BOOST_CHECK(!ParseMoney(" +1 .2 "));
+ - ]
433 : :
434 : : // Attempted 63 bit overflow should fail
435 [ + - + - : 4 : BOOST_CHECK(!ParseMoney("92233720368.54775808"));
+ - ]
436 : :
437 : : // Parsing negative amounts must fail
438 [ + - + - : 4 : BOOST_CHECK(!ParseMoney("-1"));
+ - ]
439 : :
440 : : // Parsing strings with embedded NUL characters should fail
441 [ + - + - : 4 : BOOST_CHECK(!ParseMoney("\0-1"s));
+ - ]
442 [ + - + - ]: 4 : BOOST_CHECK(!ParseMoney(STRING_WITH_EMBEDDED_NULL_CHAR));
443 [ + - + - : 4 : BOOST_CHECK(!ParseMoney("1\0"s));
+ - ]
444 : 2 : }
445 : :
446 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(util_IsHex)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
447 : : {
448 [ + - + - ]: 4 : BOOST_CHECK(IsHex("00"));
449 [ + - + - ]: 4 : BOOST_CHECK(IsHex("00112233445566778899aabbccddeeffAABBCCDDEEFF"));
450 [ + - + - ]: 4 : BOOST_CHECK(IsHex("ff"));
451 [ + - + - ]: 4 : BOOST_CHECK(IsHex("FF"));
452 : :
453 [ + - + - ]: 4 : BOOST_CHECK(!IsHex(""));
454 [ + - + - ]: 4 : BOOST_CHECK(!IsHex("0"));
455 [ + - + - ]: 4 : BOOST_CHECK(!IsHex("a"));
456 [ + - + - ]: 4 : BOOST_CHECK(!IsHex("eleven"));
457 [ + - + - ]: 4 : BOOST_CHECK(!IsHex("00xx00"));
458 [ + - + - ]: 4 : BOOST_CHECK(!IsHex("0x0000"));
459 : 2 : }
460 : :
461 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(util_seed_insecure_rand)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
462 : : {
463 : 2 : SeedRandomForTest(SeedRand::ZEROS);
464 [ + + ]: 20 : for (int mod=2;mod<11;mod++)
465 : : {
466 : 18 : int mask = 1;
467 : : // Really rough binomial confidence approximation.
468 : 18 : int err = 30*10000./mod*sqrt((1./mod*(1-1./mod))/10000.);
469 : : //mask is 2^ceil(log2(mod))-1
470 [ + + ]: 50 : while(mask<mod-1)mask=(mask<<1)+1;
471 : :
472 : : int count = 0;
473 : : //How often does it get a zero from the uniform range [0,mod)?
474 [ + + ]: 180018 : for (int i = 0; i < 10000; i++) {
475 : 235594 : uint32_t rval;
476 : 235594 : do{
477 : 235594 : rval=m_rng.rand32()&mask;
478 [ + + ]: 235594 : }while(rval>=(uint32_t)mod);
479 : 180000 : count += rval==0;
480 : : }
481 [ + - ]: 36 : BOOST_CHECK(count<=10000/mod+err);
482 [ + - ]: 36 : BOOST_CHECK(count>=10000/mod-err);
483 : : }
484 : 2 : }
485 : :
486 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(util_TimingResistantEqual)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
487 : : {
488 [ + - + - : 4 : BOOST_CHECK(TimingResistantEqual(std::string(""), std::string("")));
+ - ]
489 [ + - + - : 4 : BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("")));
+ - ]
490 [ + - + - : 4 : BOOST_CHECK(!TimingResistantEqual(std::string(""), std::string("abc")));
+ - ]
491 [ + - + - : 4 : BOOST_CHECK(!TimingResistantEqual(std::string("a"), std::string("aa")));
+ - ]
492 [ + - + - : 4 : BOOST_CHECK(!TimingResistantEqual(std::string("aa"), std::string("a")));
+ - ]
493 [ + - + - : 4 : BOOST_CHECK(TimingResistantEqual(std::string("abc"), std::string("abc")));
+ - ]
494 [ + - + - : 4 : BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("aba")));
+ - ]
495 : 2 : }
496 : :
497 : : /* Test strprintf formatting directives.
498 : : * Put a string before and after to ensure sanity of element sizes on stack. */
499 : : #define B "check_prefix"
500 : : #define E "check_postfix"
501 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(strprintf_numbers)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
502 : : {
503 : 2 : int64_t s64t = -9223372036854775807LL; /* signed 64 bit test value */
504 : 2 : uint64_t u64t = 18446744073709551615ULL; /* unsigned 64 bit test value */
505 [ + - + - ]: 4 : BOOST_CHECK(strprintf("%s %d %s", B, s64t, E) == B" -9223372036854775807 " E);
506 [ + - + - ]: 4 : BOOST_CHECK(strprintf("%s %u %s", B, u64t, E) == B" 18446744073709551615 " E);
507 [ + - + - ]: 4 : BOOST_CHECK(strprintf("%s %x %s", B, u64t, E) == B" ffffffffffffffff " E);
508 : :
509 : 2 : size_t st = 12345678; /* unsigned size_t test value */
510 : 2 : ssize_t sst = -12345678; /* signed size_t test value */
511 [ + - + - ]: 4 : BOOST_CHECK(strprintf("%s %d %s", B, sst, E) == B" -12345678 " E);
512 [ + - + - ]: 4 : BOOST_CHECK(strprintf("%s %u %s", B, st, E) == B" 12345678 " E);
513 [ + - + - ]: 4 : BOOST_CHECK(strprintf("%s %x %s", B, st, E) == B" bc614e " E);
514 : :
515 : 2 : ptrdiff_t pt = 87654321; /* positive ptrdiff_t test value */
516 : 2 : ptrdiff_t spt = -87654321; /* negative ptrdiff_t test value */
517 [ + - + - ]: 4 : BOOST_CHECK(strprintf("%s %d %s", B, spt, E) == B" -87654321 " E);
518 [ + - + - ]: 4 : BOOST_CHECK(strprintf("%s %u %s", B, pt, E) == B" 87654321 " E);
519 [ + - + - ]: 4 : BOOST_CHECK(strprintf("%s %x %s", B, pt, E) == B" 5397fb1 " E);
520 : 2 : }
521 : : #undef B
522 : : #undef E
523 : :
524 : : /* Check for mingw/wine issue #3494
525 : : * Remove this test before time.ctime(0xffffffff) == 'Sun Feb 7 07:28:15 2106'
526 : : */
527 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(gettime)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
528 : : {
529 [ + - + - ]: 4 : BOOST_CHECK((GetTime() & ~0xFFFFFFFFLL) == 0);
530 : 2 : }
531 : :
532 [ + - + - : 24 : BOOST_AUTO_TEST_CASE(util_time_GetTime)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
533 : : {
534 : 2 : SetMockTime(111);
535 : : // Check that mock time does not change after a sleep
536 [ + + ]: 6 : for (const auto& num_sleep : {0ms, 1ms}) {
537 : 4 : UninterruptibleSleep(num_sleep);
538 [ + - ]: 4 : BOOST_CHECK_EQUAL(111, GetTime()); // Deprecated time getter
539 [ + - ]: 4 : BOOST_CHECK_EQUAL(111, Now<NodeSeconds>().time_since_epoch().count());
540 [ + - ]: 4 : BOOST_CHECK_EQUAL(111, TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()));
541 [ + - ]: 4 : BOOST_CHECK_EQUAL(111, TicksSinceEpoch<SecondsDouble>(Now<NodeSeconds>()));
542 [ + - ]: 4 : BOOST_CHECK_EQUAL(111, GetTime<std::chrono::seconds>().count());
543 [ + - ]: 4 : BOOST_CHECK_EQUAL(111000, GetTime<std::chrono::milliseconds>().count());
544 [ + - ]: 4 : BOOST_CHECK_EQUAL(111000, TicksSinceEpoch<std::chrono::milliseconds>(NodeClock::now()));
545 [ + - ]: 4 : BOOST_CHECK_EQUAL(111000000, GetTime<std::chrono::microseconds>().count());
546 : : }
547 : :
548 : 2 : SetMockTime(0);
549 : : // Check that steady time and system time changes after a sleep
550 : 2 : const auto steady_ms_0 = Now<SteadyMilliseconds>();
551 : 2 : const auto steady_0 = std::chrono::steady_clock::now();
552 : 2 : const auto ms_0 = GetTime<std::chrono::milliseconds>();
553 : 2 : const auto us_0 = GetTime<std::chrono::microseconds>();
554 : 2 : UninterruptibleSleep(1ms);
555 [ + - ]: 4 : BOOST_CHECK(steady_ms_0 < Now<SteadyMilliseconds>());
556 [ + - ]: 4 : BOOST_CHECK(steady_0 + 1ms <= std::chrono::steady_clock::now());
557 [ + - ]: 4 : BOOST_CHECK(ms_0 < GetTime<std::chrono::milliseconds>());
558 [ + - ]: 4 : BOOST_CHECK(us_0 < GetTime<std::chrono::microseconds>());
559 : 2 : }
560 : :
561 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(test_IsDigit)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
562 : : {
563 [ + - ]: 2 : BOOST_CHECK_EQUAL(IsDigit('0'), true);
564 [ + - ]: 2 : BOOST_CHECK_EQUAL(IsDigit('1'), true);
565 [ + - ]: 2 : BOOST_CHECK_EQUAL(IsDigit('8'), true);
566 [ + - ]: 2 : BOOST_CHECK_EQUAL(IsDigit('9'), true);
567 : :
568 [ + - ]: 2 : BOOST_CHECK_EQUAL(IsDigit('0' - 1), false);
569 [ + - ]: 2 : BOOST_CHECK_EQUAL(IsDigit('9' + 1), false);
570 [ + - ]: 2 : BOOST_CHECK_EQUAL(IsDigit(0), false);
571 [ + - ]: 2 : BOOST_CHECK_EQUAL(IsDigit(1), false);
572 [ + - ]: 2 : BOOST_CHECK_EQUAL(IsDigit(8), false);
573 [ + - ]: 2 : BOOST_CHECK_EQUAL(IsDigit(9), false);
574 : 2 : }
575 : :
576 : : /* Check for overflow */
577 : : template <typename T>
578 : 4 : static void TestAddMatrixOverflow()
579 : : {
580 : 4 : constexpr T MAXI{std::numeric_limits<T>::max()};
581 [ + - ]: 8 : BOOST_CHECK(!CheckedAdd(T{1}, MAXI));
582 [ + - ]: 8 : BOOST_CHECK(!CheckedAdd(MAXI, MAXI));
583 [ + - ]: 4 : BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(T{1}, MAXI));
584 [ + - ]: 4 : BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(MAXI, MAXI));
585 : :
586 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(0, CheckedAdd(T{0}, T{0}).value());
587 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(MAXI, CheckedAdd(T{0}, MAXI).value());
588 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(MAXI, CheckedAdd(T{1}, MAXI - 1).value());
589 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(MAXI - 1, CheckedAdd(T{1}, MAXI - 2).value());
590 [ + - ]: 4 : BOOST_CHECK_EQUAL(0, SaturatingAdd(T{0}, T{0}));
591 [ + - ]: 4 : BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(T{0}, MAXI));
592 [ + - ]: 4 : BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(T{1}, MAXI - 1));
593 [ + - ]: 4 : BOOST_CHECK_EQUAL(MAXI - 1, SaturatingAdd(T{1}, MAXI - 2));
594 : 4 : }
595 : :
596 : : /* Check for overflow or underflow */
597 : : template <typename T>
598 : 2 : static void TestAddMatrix()
599 : : {
600 : 2 : TestAddMatrixOverflow<T>();
601 : 2 : constexpr T MINI{std::numeric_limits<T>::min()};
602 : 2 : constexpr T MAXI{std::numeric_limits<T>::max()};
603 [ + - ]: 4 : BOOST_CHECK(!CheckedAdd(T{-1}, MINI));
604 [ + - ]: 4 : BOOST_CHECK(!CheckedAdd(MINI, MINI));
605 [ + - ]: 2 : BOOST_CHECK_EQUAL(MINI, SaturatingAdd(T{-1}, MINI));
606 [ + - ]: 2 : BOOST_CHECK_EQUAL(MINI, SaturatingAdd(MINI, MINI));
607 : :
608 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(MINI, CheckedAdd(T{0}, MINI).value());
609 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(MINI, CheckedAdd(T{-1}, MINI + 1).value());
610 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(-1, CheckedAdd(MINI, MAXI).value());
611 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(MINI + 1, CheckedAdd(T{-1}, MINI + 2).value());
612 [ + - ]: 2 : BOOST_CHECK_EQUAL(MINI, SaturatingAdd(T{0}, MINI));
613 [ + - ]: 2 : BOOST_CHECK_EQUAL(MINI, SaturatingAdd(T{-1}, MINI + 1));
614 [ + - ]: 2 : BOOST_CHECK_EQUAL(MINI + 1, SaturatingAdd(T{-1}, MINI + 2));
615 [ + - ]: 2 : BOOST_CHECK_EQUAL(-1, SaturatingAdd(MINI, MAXI));
616 : 2 : }
617 : :
618 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(util_overflow)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
619 : : {
620 : 2 : TestAddMatrixOverflow<unsigned>();
621 : 2 : TestAddMatrix<signed>();
622 : 2 : }
623 : :
624 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(test_ParseInt32)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
625 : : {
626 : 2 : int32_t n;
627 : : // Valid values
628 [ + - + - ]: 4 : BOOST_CHECK(ParseInt32("1234", nullptr));
629 [ + - + - : 4 : BOOST_CHECK(ParseInt32("0", &n) && n == 0);
- + + - ]
630 [ + - + - : 4 : BOOST_CHECK(ParseInt32("1234", &n) && n == 1234);
- + + - ]
631 [ + - + - : 4 : BOOST_CHECK(ParseInt32("01234", &n) && n == 1234); // no octal
- + + - ]
632 [ + - + - : 4 : BOOST_CHECK(ParseInt32("2147483647", &n) && n == 2147483647);
- + + - ]
633 [ + - + - : 4 : BOOST_CHECK(ParseInt32("-2147483648", &n) && n == (-2147483647 - 1)); // (-2147483647 - 1) equals INT_MIN
- + + - ]
634 [ + - + - : 4 : BOOST_CHECK(ParseInt32("-1234", &n) && n == -1234);
- + + - ]
635 [ + - + - : 4 : BOOST_CHECK(ParseInt32("00000000000000001234", &n) && n == 1234);
- + + - ]
636 [ + - + - : 4 : BOOST_CHECK(ParseInt32("-00000000000000001234", &n) && n == -1234);
- + + - ]
637 [ + - + - : 4 : BOOST_CHECK(ParseInt32("00000000000000000000", &n) && n == 0);
- + + - ]
638 [ + - + - : 4 : BOOST_CHECK(ParseInt32("-00000000000000000000", &n) && n == 0);
- + + - ]
639 : : // Invalid values
640 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt32("", &n));
641 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt32(" 1", &n)); // no padding inside
642 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt32("1 ", &n));
643 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt32("++1", &n));
644 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt32("+-1", &n));
645 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt32("-+1", &n));
646 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt32("--1", &n));
647 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt32("1a", &n));
648 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt32("aap", &n));
649 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex
650 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt32(STRING_WITH_EMBEDDED_NULL_CHAR, &n));
651 : : // Overflow and underflow
652 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt32("-2147483649", nullptr));
653 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt32("2147483648", nullptr));
654 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt32("-32482348723847471234", nullptr));
655 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt32("32482348723847471234", nullptr));
656 : 2 : }
657 : :
658 : : template <typename T>
659 : 16 : static void RunToIntegralTests()
660 : : {
661 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>(STRING_WITH_EMBEDDED_NULL_CHAR));
662 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>(" 1"));
663 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>("1 "));
664 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>("1a"));
665 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>("1.1"));
666 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>("1.9"));
667 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>("+01.9"));
668 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>("-"));
669 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>("+"));
670 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>(" -1"));
671 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>("-1 "));
672 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>(" -1 "));
673 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>("+1"));
674 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>(" +1"));
675 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>(" +1 "));
676 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>("+-1"));
677 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>("-+1"));
678 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>("++1"));
679 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>("--1"));
680 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>(""));
681 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>("aap"));
682 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>("0x1"));
683 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>("-32482348723847471234"));
684 [ + - ]: 32 : BOOST_CHECK(!ToIntegral<T>("32482348723847471234"));
685 : 16 : }
686 : :
687 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(test_ToIntegral)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
688 : : {
689 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<int32_t>("1234").value(), 1'234);
690 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<int32_t>("0").value(), 0);
691 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<int32_t>("01234").value(), 1'234);
692 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<int32_t>("00000000000000001234").value(), 1'234);
693 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-00000000000000001234").value(), -1'234);
694 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<int32_t>("00000000000000000000").value(), 0);
695 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-00000000000000000000").value(), 0);
696 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-1234").value(), -1'234);
697 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-1").value(), -1);
698 : :
699 : 2 : RunToIntegralTests<uint64_t>();
700 : 2 : RunToIntegralTests<int64_t>();
701 : 2 : RunToIntegralTests<uint32_t>();
702 : 2 : RunToIntegralTests<int32_t>();
703 : 2 : RunToIntegralTests<uint16_t>();
704 : 2 : RunToIntegralTests<int16_t>();
705 : 2 : RunToIntegralTests<uint8_t>();
706 : 2 : RunToIntegralTests<int8_t>();
707 : :
708 [ + - ]: 4 : BOOST_CHECK(!ToIntegral<int64_t>("-9223372036854775809"));
709 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<int64_t>("-9223372036854775808").value(), -9'223'372'036'854'775'807LL - 1LL);
710 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<int64_t>("9223372036854775807").value(), 9'223'372'036'854'775'807);
711 [ + - ]: 4 : BOOST_CHECK(!ToIntegral<int64_t>("9223372036854775808"));
712 : :
713 [ + - ]: 4 : BOOST_CHECK(!ToIntegral<uint64_t>("-1"));
714 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<uint64_t>("0").value(), 0U);
715 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<uint64_t>("18446744073709551615").value(), 18'446'744'073'709'551'615ULL);
716 [ + - ]: 4 : BOOST_CHECK(!ToIntegral<uint64_t>("18446744073709551616"));
717 : :
718 [ + - ]: 4 : BOOST_CHECK(!ToIntegral<int32_t>("-2147483649"));
719 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-2147483648").value(), -2'147'483'648LL);
720 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<int32_t>("2147483647").value(), 2'147'483'647);
721 [ + - ]: 4 : BOOST_CHECK(!ToIntegral<int32_t>("2147483648"));
722 : :
723 [ + - ]: 4 : BOOST_CHECK(!ToIntegral<uint32_t>("-1"));
724 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<uint32_t>("0").value(), 0U);
725 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<uint32_t>("4294967295").value(), 4'294'967'295U);
726 [ + - ]: 4 : BOOST_CHECK(!ToIntegral<uint32_t>("4294967296"));
727 : :
728 [ + - ]: 4 : BOOST_CHECK(!ToIntegral<int16_t>("-32769"));
729 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<int16_t>("-32768").value(), -32'768);
730 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<int16_t>("32767").value(), 32'767);
731 [ + - ]: 4 : BOOST_CHECK(!ToIntegral<int16_t>("32768"));
732 : :
733 [ + - ]: 4 : BOOST_CHECK(!ToIntegral<uint16_t>("-1"));
734 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<uint16_t>("0").value(), 0U);
735 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<uint16_t>("65535").value(), 65'535U);
736 [ + - ]: 4 : BOOST_CHECK(!ToIntegral<uint16_t>("65536"));
737 : :
738 [ + - ]: 4 : BOOST_CHECK(!ToIntegral<int8_t>("-129"));
739 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<int8_t>("-128").value(), -128);
740 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<int8_t>("127").value(), 127);
741 [ + - ]: 4 : BOOST_CHECK(!ToIntegral<int8_t>("128"));
742 : :
743 [ + - ]: 4 : BOOST_CHECK(!ToIntegral<uint8_t>("-1"));
744 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<uint8_t>("0").value(), 0U);
745 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(ToIntegral<uint8_t>("255").value(), 255U);
746 [ + - ]: 4 : BOOST_CHECK(!ToIntegral<uint8_t>("256"));
747 : 2 : }
748 : :
749 : 16 : int64_t atoi64_legacy(const std::string& str)
750 : : {
751 : 16 : return strtoll(str.c_str(), nullptr, 10);
752 : : }
753 : :
754 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(test_LocaleIndependentAtoi)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
755 : : {
756 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("1234"), 1'234);
757 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("0"), 0);
758 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("01234"), 1'234);
759 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-1234"), -1'234);
760 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(" 1"), 1);
761 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("1 "), 1);
762 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("1a"), 1);
763 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("1.1"), 1);
764 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("1.9"), 1);
765 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("+01.9"), 1);
766 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-1"), -1);
767 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(" -1"), -1);
768 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-1 "), -1);
769 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(" -1 "), -1);
770 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("+1"), 1);
771 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(" +1"), 1);
772 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(" +1 "), 1);
773 : :
774 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("+-1"), 0);
775 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-+1"), 0);
776 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("++1"), 0);
777 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("--1"), 0);
778 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(""), 0);
779 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("aap"), 0);
780 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("0x1"), 0);
781 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-32482348723847471234"), -2'147'483'647 - 1);
782 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("32482348723847471234"), 2'147'483'647);
783 : :
784 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("-9223372036854775809"), -9'223'372'036'854'775'807LL - 1LL);
785 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("-9223372036854775808"), -9'223'372'036'854'775'807LL - 1LL);
786 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("9223372036854775807"), 9'223'372'036'854'775'807);
787 [ + - ]: 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("9223372036854775808"), 9'223'372'036'854'775'807);
788 : :
789 : 2 : std::map<std::string, int64_t> atoi64_test_pairs = {
790 [ + - ]: 2 : {"-9223372036854775809", std::numeric_limits<int64_t>::min()},
791 : 2 : {"-9223372036854775808", -9'223'372'036'854'775'807LL - 1LL},
792 : 2 : {"9223372036854775807", 9'223'372'036'854'775'807},
793 : 2 : {"9223372036854775808", std::numeric_limits<int64_t>::max()},
794 : 2 : {"+-", 0},
795 : 2 : {"0x1", 0},
796 : 2 : {"ox1", 0},
797 : 2 : {"", 0},
798 [ + + - - ]: 18 : };
799 : :
800 [ + + ]: 18 : for (const auto& pair : atoi64_test_pairs) {
801 [ + - + - : 16 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>(pair.first), pair.second);
+ - ]
802 : : }
803 : :
804 : : // Ensure legacy compatibility with previous versions of Bitcoin Core's atoi64
805 [ + + ]: 18 : for (const auto& pair : atoi64_test_pairs) {
806 [ + - + - : 16 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>(pair.first), atoi64_legacy(pair.first));
+ - ]
807 : : }
808 : :
809 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>("-1"), 0U);
+ - ]
810 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>("0"), 0U);
+ - ]
811 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>("18446744073709551615"), 18'446'744'073'709'551'615ULL);
+ - ]
812 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>("18446744073709551616"), 18'446'744'073'709'551'615ULL);
+ - ]
813 : :
814 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-2147483649"), -2'147'483'648LL);
+ - ]
815 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-2147483648"), -2'147'483'648LL);
+ - ]
816 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("2147483647"), 2'147'483'647);
+ - ]
817 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("2147483648"), 2'147'483'647);
+ - ]
818 : :
819 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("-1"), 0U);
+ - ]
820 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("0"), 0U);
+ - ]
821 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("4294967295"), 4'294'967'295U);
+ - ]
822 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("4294967296"), 4'294'967'295U);
+ - ]
823 : :
824 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("-32769"), -32'768);
+ - ]
825 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("-32768"), -32'768);
+ - ]
826 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("32767"), 32'767);
+ - ]
827 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("32768"), 32'767);
+ - ]
828 : :
829 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("-1"), 0U);
+ - ]
830 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("0"), 0U);
+ - ]
831 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("65535"), 65'535U);
+ - ]
832 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("65536"), 65'535U);
+ - ]
833 : :
834 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("-129"), -128);
+ - ]
835 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("-128"), -128);
+ - ]
836 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("127"), 127);
+ - ]
837 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("128"), 127);
+ - ]
838 : :
839 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("-1"), 0U);
+ - ]
840 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("0"), 0U);
+ - ]
841 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("255"), 255U);
+ - ]
842 [ + - + - : 2 : BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("256"), 255U);
+ - ]
843 [ + - + - : 4 : }
+ - + - +
- + - + -
- + - - ]
844 : :
845 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(test_ParseInt64)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
846 : : {
847 : 2 : int64_t n;
848 : : // Valid values
849 [ + - + - ]: 4 : BOOST_CHECK(ParseInt64("1234", nullptr));
850 [ + - + - : 4 : BOOST_CHECK(ParseInt64("0", &n) && n == 0LL);
- + + - ]
851 [ + - + - : 4 : BOOST_CHECK(ParseInt64("1234", &n) && n == 1234LL);
- + + - ]
852 [ + - + - : 4 : BOOST_CHECK(ParseInt64("01234", &n) && n == 1234LL); // no octal
- + + - ]
853 [ + - + - : 4 : BOOST_CHECK(ParseInt64("2147483647", &n) && n == 2147483647LL);
- + + - ]
854 [ + - + - : 4 : BOOST_CHECK(ParseInt64("-2147483648", &n) && n == -2147483648LL);
- + + - ]
855 [ + - + - : 4 : BOOST_CHECK(ParseInt64("9223372036854775807", &n) && n == int64_t{9223372036854775807});
- + + - ]
856 [ + - + - : 4 : BOOST_CHECK(ParseInt64("-9223372036854775808", &n) && n == int64_t{-9223372036854775807-1});
- + + - ]
857 [ + - + - : 4 : BOOST_CHECK(ParseInt64("-1234", &n) && n == -1234LL);
- + + - ]
858 : : // Invalid values
859 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt64("", &n));
860 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt64(" 1", &n)); // no padding inside
861 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt64("1 ", &n));
862 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt64("1a", &n));
863 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt64("aap", &n));
864 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt64("0x1", &n)); // no hex
865 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt64(STRING_WITH_EMBEDDED_NULL_CHAR, &n));
866 : : // Overflow and underflow
867 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt64("-9223372036854775809", nullptr));
868 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt64("9223372036854775808", nullptr));
869 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt64("-32482348723847471234", nullptr));
870 [ + - + - ]: 4 : BOOST_CHECK(!ParseInt64("32482348723847471234", nullptr));
871 : 2 : }
872 : :
873 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(test_ParseUInt8)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
874 : : {
875 : 2 : uint8_t n;
876 : : // Valid values
877 [ + - + - ]: 4 : BOOST_CHECK(ParseUInt8("255", nullptr));
878 [ + - + - : 4 : BOOST_CHECK(ParseUInt8("0", &n) && n == 0);
- + + - ]
879 [ + - + - : 4 : BOOST_CHECK(ParseUInt8("255", &n) && n == 255);
- + + - ]
880 [ + - + - : 4 : BOOST_CHECK(ParseUInt8("0255", &n) && n == 255); // no octal
- + + - ]
881 [ + - + - : 4 : BOOST_CHECK(ParseUInt8("255", &n) && n == static_cast<uint8_t>(255));
- + + - ]
882 [ + - + - : 4 : BOOST_CHECK(ParseUInt8("+255", &n) && n == 255);
- + + - ]
883 [ + - + - : 4 : BOOST_CHECK(ParseUInt8("00000000000000000012", &n) && n == 12);
- + + - ]
884 [ + - + - : 4 : BOOST_CHECK(ParseUInt8("00000000000000000000", &n) && n == 0);
- + + - ]
885 : : // Invalid values
886 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8("-00000000000000000000", &n));
887 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8("", &n));
888 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8(" 1", &n)); // no padding inside
889 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8(" -1", &n));
890 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8("++1", &n));
891 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8("+-1", &n));
892 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8("-+1", &n));
893 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8("--1", &n));
894 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8("-1", &n));
895 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8("1 ", &n));
896 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8("1a", &n));
897 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8("aap", &n));
898 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8("0x1", &n)); // no hex
899 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8(STRING_WITH_EMBEDDED_NULL_CHAR, &n));
900 : : // Overflow and underflow
901 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8("-255", &n));
902 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8("256", &n));
903 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8("-123", &n));
904 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8("-123", nullptr));
905 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt8("256", nullptr));
906 : 2 : }
907 : :
908 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(test_ParseUInt16)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
909 : : {
910 : 2 : uint16_t n;
911 : : // Valid values
912 [ + - + - ]: 4 : BOOST_CHECK(ParseUInt16("1234", nullptr));
913 [ + - + - : 4 : BOOST_CHECK(ParseUInt16("0", &n) && n == 0);
- + + - ]
914 [ + - + - : 4 : BOOST_CHECK(ParseUInt16("1234", &n) && n == 1234);
- + + - ]
915 [ + - + - : 4 : BOOST_CHECK(ParseUInt16("01234", &n) && n == 1234); // no octal
- + + - ]
916 [ + - + - : 4 : BOOST_CHECK(ParseUInt16("65535", &n) && n == static_cast<uint16_t>(65535));
- + + - ]
917 [ + - + - : 4 : BOOST_CHECK(ParseUInt16("+65535", &n) && n == 65535);
- + + - ]
918 [ + - + - : 4 : BOOST_CHECK(ParseUInt16("00000000000000000012", &n) && n == 12);
- + + - ]
919 [ + - + - : 4 : BOOST_CHECK(ParseUInt16("00000000000000000000", &n) && n == 0);
- + + - ]
920 : : // Invalid values
921 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16("-00000000000000000000", &n));
922 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16("", &n));
923 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16(" 1", &n)); // no padding inside
924 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16(" -1", &n));
925 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16("++1", &n));
926 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16("+-1", &n));
927 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16("-+1", &n));
928 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16("--1", &n));
929 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16("-1", &n));
930 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16("1 ", &n));
931 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16("1a", &n));
932 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16("aap", &n));
933 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16("0x1", &n)); // no hex
934 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16(STRING_WITH_EMBEDDED_NULL_CHAR, &n));
935 : : // Overflow and underflow
936 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16("-65535", &n));
937 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16("65536", &n));
938 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16("-123", &n));
939 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16("-123", nullptr));
940 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt16("65536", nullptr));
941 : 2 : }
942 : :
943 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(test_ParseUInt32)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
944 : : {
945 : 2 : uint32_t n;
946 : : // Valid values
947 [ + - + - ]: 4 : BOOST_CHECK(ParseUInt32("1234", nullptr));
948 [ + - + - : 4 : BOOST_CHECK(ParseUInt32("0", &n) && n == 0);
- + + - ]
949 [ + - + - : 4 : BOOST_CHECK(ParseUInt32("1234", &n) && n == 1234);
- + + - ]
950 [ + - + - : 4 : BOOST_CHECK(ParseUInt32("01234", &n) && n == 1234); // no octal
- + + - ]
951 [ + - + - : 4 : BOOST_CHECK(ParseUInt32("2147483647", &n) && n == 2147483647);
- + + - ]
952 [ + - + - : 4 : BOOST_CHECK(ParseUInt32("2147483648", &n) && n == uint32_t{2147483648});
- + + - ]
953 [ + - + - : 4 : BOOST_CHECK(ParseUInt32("4294967295", &n) && n == uint32_t{4294967295});
- + + - ]
954 [ + - + - : 4 : BOOST_CHECK(ParseUInt32("+1234", &n) && n == 1234);
- + + - ]
955 [ + - + - : 4 : BOOST_CHECK(ParseUInt32("00000000000000001234", &n) && n == 1234);
- + + - ]
956 [ + - + - : 4 : BOOST_CHECK(ParseUInt32("00000000000000000000", &n) && n == 0);
- + + - ]
957 : : // Invalid values
958 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32("-00000000000000000000", &n));
959 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32("", &n));
960 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32(" 1", &n)); // no padding inside
961 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32(" -1", &n));
962 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32("++1", &n));
963 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32("+-1", &n));
964 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32("-+1", &n));
965 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32("--1", &n));
966 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32("-1", &n));
967 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32("1 ", &n));
968 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32("1a", &n));
969 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32("aap", &n));
970 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32("0x1", &n)); // no hex
971 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32(STRING_WITH_EMBEDDED_NULL_CHAR, &n));
972 : : // Overflow and underflow
973 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32("-2147483648", &n));
974 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32("4294967296", &n));
975 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32("-1234", &n));
976 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32("-32482348723847471234", nullptr));
977 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt32("32482348723847471234", nullptr));
978 : 2 : }
979 : :
980 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(test_ParseUInt64)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
981 : : {
982 : 2 : uint64_t n;
983 : : // Valid values
984 [ + - + - ]: 4 : BOOST_CHECK(ParseUInt64("1234", nullptr));
985 [ + - + - : 4 : BOOST_CHECK(ParseUInt64("0", &n) && n == 0LL);
- + + - ]
986 [ + - + - : 4 : BOOST_CHECK(ParseUInt64("1234", &n) && n == 1234LL);
- + + - ]
987 [ + - + - : 4 : BOOST_CHECK(ParseUInt64("01234", &n) && n == 1234LL); // no octal
- + + - ]
988 [ + - + - : 4 : BOOST_CHECK(ParseUInt64("2147483647", &n) && n == 2147483647LL);
- + + - ]
989 [ + - + - : 4 : BOOST_CHECK(ParseUInt64("9223372036854775807", &n) && n == 9223372036854775807ULL);
- + + - ]
990 [ + - + - : 4 : BOOST_CHECK(ParseUInt64("9223372036854775808", &n) && n == 9223372036854775808ULL);
- + + - ]
991 [ + - + - : 4 : BOOST_CHECK(ParseUInt64("18446744073709551615", &n) && n == 18446744073709551615ULL);
- + + - ]
992 : : // Invalid values
993 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt64("", &n));
994 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt64(" 1", &n)); // no padding inside
995 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt64(" -1", &n));
996 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt64("1 ", &n));
997 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt64("1a", &n));
998 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt64("aap", &n));
999 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt64("0x1", &n)); // no hex
1000 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt64(STRING_WITH_EMBEDDED_NULL_CHAR, &n));
1001 : : // Overflow and underflow
1002 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt64("-9223372036854775809", nullptr));
1003 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt64("18446744073709551616", nullptr));
1004 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt64("-32482348723847471234", nullptr));
1005 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt64("-2147483648", &n));
1006 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt64("-9223372036854775808", &n));
1007 [ + - + - ]: 4 : BOOST_CHECK(!ParseUInt64("-1234", &n));
1008 : 2 : }
1009 : :
1010 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(test_FormatParagraph)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1011 : : {
1012 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatParagraph("", 79, 0), "");
1013 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatParagraph("test", 79, 0), "test");
1014 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatParagraph(" test", 79, 0), " test");
1015 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatParagraph("test test", 79, 0), "test test");
1016 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatParagraph("test test", 4, 0), "test\ntest");
1017 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatParagraph("testerde test", 4, 0), "testerde\ntest");
1018 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatParagraph("test test", 4, 4), "test\n test");
1019 : :
1020 : : // Make sure we don't indent a fully-new line following a too-long line ending
1021 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatParagraph("test test\nabc", 4, 4), "test\n test\nabc");
1022 : :
1023 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatParagraph("This_is_a_very_long_test_string_without_any_spaces_so_it_should_just_get_returned_as_is_despite_the_length until it gets here", 79), "This_is_a_very_long_test_string_without_any_spaces_so_it_should_just_get_returned_as_is_despite_the_length\nuntil it gets here");
1024 : :
1025 : : // Test wrap length is exact
1026 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatParagraph("a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de f g h i j k l m n o p", 79), "a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de\nf g h i j k l m n o p");
1027 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatParagraph("x\na b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de f g h i j k l m n o p", 79), "x\na b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de\nf g h i j k l m n o p");
1028 : : // Indent should be included in length of lines
1029 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatParagraph("x\na b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e fg h i j k", 79, 4), "x\na b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9 a b c de\n f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e fg\n h i j k");
1030 : :
1031 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatParagraph("This is a very long test string. This is a second sentence in the very long test string.", 79), "This is a very long test string. This is a second sentence in the very long\ntest string.");
1032 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatParagraph("This is a very long test string.\nThis is a second sentence in the very long test string. This is a third sentence in the very long test string.", 79), "This is a very long test string.\nThis is a second sentence in the very long test string. This is a third\nsentence in the very long test string.");
1033 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatParagraph("This is a very long test string.\n\nThis is a second sentence in the very long test string. This is a third sentence in the very long test string.", 79), "This is a very long test string.\n\nThis is a second sentence in the very long test string. This is a third\nsentence in the very long test string.");
1034 [ + - ]: 2 : BOOST_CHECK_EQUAL(FormatParagraph("Testing that normal newlines do not get indented.\nLike here.", 79), "Testing that normal newlines do not get indented.\nLike here.");
1035 : 2 : }
1036 : :
1037 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(test_FormatSubVersion)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1038 : : {
1039 : 2 : std::vector<std::string> comments;
1040 [ + - ]: 2 : comments.emplace_back("comment1");
1041 : 2 : std::vector<std::string> comments2;
1042 [ + - ]: 2 : comments2.emplace_back("comment1");
1043 [ + - + - ]: 4 : comments2.push_back(SanitizeString(std::string("Comment2; .,_?@-; !\"#$%&'()*+/<=>[]\\^`{|}~"), SAFE_CHARS_UA_COMMENT)); // Semicolon is discouraged but not forbidden by BIP-0014
1044 [ + - + - : 2 : BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, std::vector<std::string>()),std::string("/Test:9.99.0/"));
+ - + - +
- ]
1045 [ + - + - : 2 : BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments),std::string("/Test:9.99.0(comment1)/"));
+ - + - +
- ]
1046 [ + - + - : 2 : BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments2),std::string("/Test:9.99.0(comment1; Comment2; .,_?@-; )/"));
+ - + - +
- ]
1047 : 2 : }
1048 : :
1049 [ + - + - : 14 : BOOST_AUTO_TEST_CASE(test_ParseFixedPoint)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1050 : : {
1051 : 2 : int64_t amount = 0;
1052 [ + - + - ]: 4 : BOOST_CHECK(ParseFixedPoint("0", 8, &amount));
1053 [ + - ]: 2 : BOOST_CHECK_EQUAL(amount, 0LL);
1054 [ + - + - ]: 4 : BOOST_CHECK(ParseFixedPoint("1", 8, &amount));
1055 [ + - ]: 2 : BOOST_CHECK_EQUAL(amount, 100000000LL);
1056 [ + - + - ]: 4 : BOOST_CHECK(ParseFixedPoint("0.0", 8, &amount));
1057 [ + - ]: 2 : BOOST_CHECK_EQUAL(amount, 0LL);
1058 [ + - + - ]: 4 : BOOST_CHECK(ParseFixedPoint("-0.1", 8, &amount));
1059 [ + - ]: 2 : BOOST_CHECK_EQUAL(amount, -10000000LL);
1060 [ + - + - ]: 4 : BOOST_CHECK(ParseFixedPoint("1.1", 8, &amount));
1061 [ + - ]: 2 : BOOST_CHECK_EQUAL(amount, 110000000LL);
1062 [ + - + - ]: 4 : BOOST_CHECK(ParseFixedPoint("1.10000000000000000", 8, &amount));
1063 [ + - ]: 2 : BOOST_CHECK_EQUAL(amount, 110000000LL);
1064 [ + - + - ]: 4 : BOOST_CHECK(ParseFixedPoint("1.1e1", 8, &amount));
1065 [ + - ]: 2 : BOOST_CHECK_EQUAL(amount, 1100000000LL);
1066 [ + - + - ]: 4 : BOOST_CHECK(ParseFixedPoint("1.1e-1", 8, &amount));
1067 [ + - ]: 2 : BOOST_CHECK_EQUAL(amount, 11000000LL);
1068 [ + - + - ]: 4 : BOOST_CHECK(ParseFixedPoint("1000", 8, &amount));
1069 [ + - ]: 2 : BOOST_CHECK_EQUAL(amount, 100000000000LL);
1070 [ + - + - ]: 4 : BOOST_CHECK(ParseFixedPoint("-1000", 8, &amount));
1071 [ + - ]: 2 : BOOST_CHECK_EQUAL(amount, -100000000000LL);
1072 [ + - + - ]: 4 : BOOST_CHECK(ParseFixedPoint("0.00000001", 8, &amount));
1073 [ + - ]: 2 : BOOST_CHECK_EQUAL(amount, 1LL);
1074 [ + - + - ]: 4 : BOOST_CHECK(ParseFixedPoint("0.0000000100000000", 8, &amount));
1075 [ + - ]: 2 : BOOST_CHECK_EQUAL(amount, 1LL);
1076 [ + - + - ]: 4 : BOOST_CHECK(ParseFixedPoint("-0.00000001", 8, &amount));
1077 [ + - ]: 2 : BOOST_CHECK_EQUAL(amount, -1LL);
1078 [ + - + - ]: 4 : BOOST_CHECK(ParseFixedPoint("1000000000.00000001", 8, &amount));
1079 [ + - ]: 2 : BOOST_CHECK_EQUAL(amount, 100000000000000001LL);
1080 [ + - + - ]: 4 : BOOST_CHECK(ParseFixedPoint("9999999999.99999999", 8, &amount));
1081 [ + - ]: 2 : BOOST_CHECK_EQUAL(amount, 999999999999999999LL);
1082 [ + - + - ]: 4 : BOOST_CHECK(ParseFixedPoint("-9999999999.99999999", 8, &amount));
1083 [ + - ]: 2 : BOOST_CHECK_EQUAL(amount, -999999999999999999LL);
1084 : :
1085 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("", 8, &amount));
1086 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("-", 8, &amount));
1087 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("a-1000", 8, &amount));
1088 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("-a1000", 8, &amount));
1089 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("-1000a", 8, &amount));
1090 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("-01000", 8, &amount));
1091 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("00.1", 8, &amount));
1092 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint(".1", 8, &amount));
1093 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("--0.1", 8, &amount));
1094 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("0.000000001", 8, &amount));
1095 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("-0.000000001", 8, &amount));
1096 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("0.00000001000000001", 8, &amount));
1097 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("-10000000000.00000000", 8, &amount));
1098 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("10000000000.00000000", 8, &amount));
1099 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("-10000000000.00000001", 8, &amount));
1100 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("10000000000.00000001", 8, &amount));
1101 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("-10000000000.00000009", 8, &amount));
1102 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("10000000000.00000009", 8, &amount));
1103 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("-99999999999.99999999", 8, &amount));
1104 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("99999909999.09999999", 8, &amount));
1105 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("92233720368.54775807", 8, &amount));
1106 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("92233720368.54775808", 8, &amount));
1107 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("-92233720368.54775808", 8, &amount));
1108 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("-92233720368.54775809", 8, &amount));
1109 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("1.1e", 8, &amount));
1110 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("1.1e-", 8, &amount));
1111 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("1.", 8, &amount));
1112 : :
1113 : : // Test with 3 decimal places for fee rates in sat/vB.
1114 [ + - + - ]: 4 : BOOST_CHECK(ParseFixedPoint("0.001", 3, &amount));
1115 [ + - ]: 2 : BOOST_CHECK_EQUAL(amount, CAmount{1});
1116 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("0.0009", 3, &amount));
1117 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("31.00100001", 3, &amount));
1118 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("31.0011", 3, &amount));
1119 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("31.99999999", 3, &amount));
1120 [ + - + - ]: 4 : BOOST_CHECK(!ParseFixedPoint("31.999999999999999999999", 3, &amount));
1121 : 2 : }
1122 : :
1123 : : #ifndef WIN32 // Cannot do this test on WIN32 due to lack of fork()
1124 : : static constexpr char LockCommand = 'L';
1125 : : static constexpr char UnlockCommand = 'U';
1126 : : static constexpr char ExitCommand = 'X';
1127 : : enum : char {
1128 : : ResSuccess = 2, // Start with 2 to avoid accidental collision with common values 0 and 1
1129 : : ResErrorWrite,
1130 : : ResErrorLock,
1131 : : ResUnlockSuccess,
1132 : : };
1133 : :
1134 : 1 : [[noreturn]] static void TestOtherProcess(fs::path dirname, fs::path lockname, int fd)
1135 : : {
1136 : 6 : char ch;
1137 : 6 : while (true) {
1138 : 6 : int rv = read(fd, &ch, 1); // Wait for command
1139 [ - + ]: 6 : assert(rv == 1);
1140 [ + + + - ]: 6 : switch (ch) {
1141 : 4 : case LockCommand:
1142 : 12 : ch = [&] {
1143 [ + + - + ]: 4 : switch (util::LockDirectory(dirname, lockname)) {
1144 : : case util::LockResult::Success: return ResSuccess;
1145 : 1 : case util::LockResult::ErrorWrite: return ResErrorWrite;
1146 : 1 : case util::LockResult::ErrorLock: return ResErrorLock;
1147 : : } // no default case, so the compiler can warn about missing cases
1148 : 0 : assert(false);
1149 : 4 : }();
1150 : 4 : rv = write(fd, &ch, 1);
1151 [ + - ]: 4 : assert(rv == 1);
1152 : : break;
1153 : 1 : case UnlockCommand:
1154 : 1 : ReleaseDirectoryLocks();
1155 : 1 : ch = ResUnlockSuccess; // Always succeeds
1156 : 1 : rv = write(fd, &ch, 1);
1157 [ + - ]: 1 : assert(rv == 1);
1158 : : break;
1159 : 1 : case ExitCommand:
1160 : 1 : close(fd);
1161 : 1 : exit(0);
1162 : 0 : default:
1163 : 0 : assert(0);
1164 : : }
1165 : : }
1166 : : }
1167 : : #endif
1168 : :
1169 [ + - + - : 12 : BOOST_AUTO_TEST_CASE(test_LockDirectory)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1170 : : {
1171 [ + - ]: 4 : fs::path dirname = m_args.GetDataDirBase() / "lock_dir";
1172 [ + - ]: 2 : const fs::path lockname = ".lock";
1173 : : #ifndef WIN32
1174 : : // Revert SIGCHLD to default, otherwise boost.test will catch and fail on
1175 : : // it: there is BOOST_TEST_IGNORE_SIGCHLD but that only works when defined
1176 : : // at build-time of the boost library
1177 : 2 : void (*old_handler)(int) = signal(SIGCHLD, SIG_DFL);
1178 : :
1179 : : // Fork another process for testing before creating the lock, so that we
1180 : : // won't fork while holding the lock (which might be undefined, and is not
1181 : : // relevant as test case as that is avoided with -daemonize).
1182 : 2 : int fd[2];
1183 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(socketpair(AF_UNIX, SOCK_STREAM, 0, fd), 0);
1184 : 2 : pid_t pid = fork();
1185 [ + + ]: 2 : if (!pid) {
1186 [ + - + - : 1 : BOOST_CHECK_EQUAL(close(fd[1]), 0); // Child: close parent end
+ - ]
1187 [ + - + - ]: 1 : TestOtherProcess(dirname, lockname, fd[0]);
1188 : : }
1189 [ + - + - : 1 : BOOST_CHECK_EQUAL(close(fd[0]), 0); // Parent: close child end
+ - ]
1190 : :
1191 : 1 : char ch;
1192 : : // Lock on non-existent directory should fail
1193 [ + - + - : 1 : BOOST_CHECK_EQUAL(write(fd[1], &LockCommand, 1), 1);
+ - ]
1194 [ + - + - : 1 : BOOST_CHECK_EQUAL(read(fd[1], &ch, 1), 1);
+ - ]
1195 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ch, ResErrorWrite);
1196 : : #endif
1197 : : // Lock on non-existent directory should fail
1198 [ + - + - : 1 : BOOST_CHECK_EQUAL(util::LockDirectory(dirname, lockname), util::LockResult::ErrorWrite);
+ - ]
1199 : :
1200 [ + - ]: 1 : fs::create_directories(dirname);
1201 : :
1202 : : // Probing lock on new directory should succeed
1203 [ + - + - : 1 : BOOST_CHECK_EQUAL(util::LockDirectory(dirname, lockname, true), util::LockResult::Success);
+ - ]
1204 : :
1205 : : // Persistent lock on new directory should succeed
1206 [ + - + - : 1 : BOOST_CHECK_EQUAL(util::LockDirectory(dirname, lockname), util::LockResult::Success);
+ - ]
1207 : :
1208 : : // Another lock on the directory from the same thread should succeed
1209 [ + - + - : 1 : BOOST_CHECK_EQUAL(util::LockDirectory(dirname, lockname), util::LockResult::Success);
+ - ]
1210 : :
1211 : : // Another lock on the directory from a different thread within the same process should succeed
1212 : 1 : util::LockResult threadresult;
1213 [ + - ]: 2 : std::thread thr([&] { threadresult = util::LockDirectory(dirname, lockname); });
1214 [ + - ]: 1 : thr.join();
1215 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(threadresult, util::LockResult::Success);
1216 : : #ifndef WIN32
1217 : : // Try to acquire lock in child process while we're holding it, this should fail.
1218 [ + - + - : 1 : BOOST_CHECK_EQUAL(write(fd[1], &LockCommand, 1), 1);
+ - ]
1219 [ + - + - : 1 : BOOST_CHECK_EQUAL(read(fd[1], &ch, 1), 1);
+ - ]
1220 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ch, ResErrorLock);
1221 : :
1222 : : // Give up our lock
1223 [ + - ]: 1 : ReleaseDirectoryLocks();
1224 : : // Probing lock from our side now should succeed, but not hold on to the lock.
1225 [ + - + - : 1 : BOOST_CHECK_EQUAL(util::LockDirectory(dirname, lockname, true), util::LockResult::Success);
+ - ]
1226 : :
1227 : : // Try to acquire the lock in the child process, this should be successful.
1228 [ + - + - : 1 : BOOST_CHECK_EQUAL(write(fd[1], &LockCommand, 1), 1);
+ - ]
1229 [ + - + - : 1 : BOOST_CHECK_EQUAL(read(fd[1], &ch, 1), 1);
+ - ]
1230 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ch, ResSuccess);
1231 : :
1232 : : // When we try to probe the lock now, it should fail.
1233 [ + - + - : 1 : BOOST_CHECK_EQUAL(util::LockDirectory(dirname, lockname, true), util::LockResult::ErrorLock);
+ - ]
1234 : :
1235 : : // Unlock the lock in the child process
1236 [ + - + - : 1 : BOOST_CHECK_EQUAL(write(fd[1], &UnlockCommand, 1), 1);
+ - ]
1237 [ + - + - : 1 : BOOST_CHECK_EQUAL(read(fd[1], &ch, 1), 1);
+ - ]
1238 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ch, ResUnlockSuccess);
1239 : :
1240 : : // When we try to probe the lock now, it should succeed.
1241 [ + - + - : 1 : BOOST_CHECK_EQUAL(util::LockDirectory(dirname, lockname, true), util::LockResult::Success);
+ - ]
1242 : :
1243 : : // Re-lock the lock in the child process, then wait for it to exit, check
1244 : : // successful return. After that, we check that exiting the process
1245 : : // has released the lock as we would expect by probing it.
1246 : 1 : int processstatus;
1247 [ + - + - : 1 : BOOST_CHECK_EQUAL(write(fd[1], &LockCommand, 1), 1);
+ - ]
1248 : : // The following line invokes the ~CNetCleanup dtor without
1249 : : // a paired SetupNetworking call. This is acceptable as long as
1250 : : // ~CNetCleanup is a no-op for non-Windows platforms.
1251 [ + - + - : 1 : BOOST_CHECK_EQUAL(write(fd[1], &ExitCommand, 1), 1);
+ - ]
1252 [ + - + - : 1 : BOOST_CHECK_EQUAL(waitpid(pid, &processstatus, 0), pid);
+ - ]
1253 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(processstatus, 0);
1254 [ + - + - : 1 : BOOST_CHECK_EQUAL(util::LockDirectory(dirname, lockname, true), util::LockResult::Success);
+ - ]
1255 : :
1256 : : // Restore SIGCHLD
1257 : 1 : signal(SIGCHLD, old_handler);
1258 [ + - + - : 1 : BOOST_CHECK_EQUAL(close(fd[1]), 0); // Close our side of the socketpair
+ - ]
1259 : : #endif
1260 : : // Clean up
1261 [ + - ]: 1 : ReleaseDirectoryLocks();
1262 [ + - ]: 1 : fs::remove_all(dirname);
1263 : 3 : }
1264 : :
1265 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(test_ToLower)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1266 : : {
1267 [ + - ]: 1 : BOOST_CHECK_EQUAL(ToLower('@'), '@');
1268 [ + - ]: 1 : BOOST_CHECK_EQUAL(ToLower('A'), 'a');
1269 [ + - ]: 1 : BOOST_CHECK_EQUAL(ToLower('Z'), 'z');
1270 [ + - ]: 1 : BOOST_CHECK_EQUAL(ToLower('['), '[');
1271 [ + - ]: 1 : BOOST_CHECK_EQUAL(ToLower(0), 0);
1272 [ + - ]: 1 : BOOST_CHECK_EQUAL(ToLower('\xff'), '\xff');
1273 : :
1274 [ + - ]: 1 : BOOST_CHECK_EQUAL(ToLower(""), "");
1275 [ + - ]: 1 : BOOST_CHECK_EQUAL(ToLower("#HODL"), "#hodl");
1276 [ + - ]: 1 : BOOST_CHECK_EQUAL(ToLower("\x00\xfe\xff"), "\x00\xfe\xff");
1277 : 1 : }
1278 : :
1279 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(test_ToUpper)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1280 : : {
1281 [ + - ]: 1 : BOOST_CHECK_EQUAL(ToUpper('`'), '`');
1282 [ + - ]: 1 : BOOST_CHECK_EQUAL(ToUpper('a'), 'A');
1283 [ + - ]: 1 : BOOST_CHECK_EQUAL(ToUpper('z'), 'Z');
1284 [ + - ]: 1 : BOOST_CHECK_EQUAL(ToUpper('{'), '{');
1285 [ + - ]: 1 : BOOST_CHECK_EQUAL(ToUpper(0), 0);
1286 [ + - ]: 1 : BOOST_CHECK_EQUAL(ToUpper('\xff'), '\xff');
1287 : :
1288 [ + - ]: 1 : BOOST_CHECK_EQUAL(ToUpper(""), "");
1289 [ + - ]: 1 : BOOST_CHECK_EQUAL(ToUpper("#hodl"), "#HODL");
1290 [ + - ]: 1 : BOOST_CHECK_EQUAL(ToUpper("\x00\xfe\xff"), "\x00\xfe\xff");
1291 : 1 : }
1292 : :
1293 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(test_Capitalize)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1294 : : {
1295 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(Capitalize(""), "");
1296 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(Capitalize("bitcoin"), "Bitcoin");
1297 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(Capitalize("\x00\xfe\xff"), "\x00\xfe\xff");
1298 : 1 : }
1299 : :
1300 : 28 : static std::string SpanToStr(const Span<const char>& span)
1301 : : {
1302 : 28 : return std::string(span.begin(), span.end());
1303 : : }
1304 : :
1305 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(test_script_parsing)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1306 : : {
1307 : 1 : using namespace script;
1308 [ + - ]: 1 : std::string input;
1309 : 1 : Span<const char> sp;
1310 : 1 : bool success;
1311 : :
1312 : : // Const(...): parse a constant, update span to skip it if successful
1313 [ + - ]: 1 : input = "MilkToastHoney";
1314 [ + - ]: 1 : sp = input;
1315 [ + - + - ]: 1 : success = Const("", sp); // empty
1316 [ + - + - : 2 : BOOST_CHECK(success);
+ - ]
1317 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(sp), "MilkToastHoney");
+ - ]
1318 : :
1319 [ + - + - ]: 1 : success = Const("Milk", sp);
1320 [ + - + - : 2 : BOOST_CHECK(success);
+ - ]
1321 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(sp), "ToastHoney");
+ - ]
1322 : :
1323 [ + - + - ]: 1 : success = Const("Bread", sp);
1324 [ + - + - : 2 : BOOST_CHECK(!success);
+ - ]
1325 : :
1326 [ + - + - ]: 1 : success = Const("Toast", sp);
1327 [ + - + - : 2 : BOOST_CHECK(success);
+ - ]
1328 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(sp), "Honey");
+ - ]
1329 : :
1330 [ + - + - ]: 1 : success = Const("Honeybadger", sp);
1331 [ + - + - : 2 : BOOST_CHECK(!success);
+ - ]
1332 : :
1333 [ + - + - ]: 1 : success = Const("Honey", sp);
1334 [ + - + - : 2 : BOOST_CHECK(success);
+ - ]
1335 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(sp), "");
+ - ]
1336 : :
1337 : : // Func(...): parse a function call, update span to argument if successful
1338 [ + - ]: 1 : input = "Foo(Bar(xy,z()))";
1339 [ + - ]: 1 : sp = input;
1340 : :
1341 [ + - + - ]: 1 : success = Func("FooBar", sp);
1342 [ + - + - : 2 : BOOST_CHECK(!success);
+ - ]
1343 : :
1344 [ + - + - ]: 1 : success = Func("Foo(", sp);
1345 [ + - + - : 2 : BOOST_CHECK(!success);
+ - ]
1346 : :
1347 [ + - + - ]: 1 : success = Func("Foo", sp);
1348 [ + - + - : 2 : BOOST_CHECK(success);
+ - ]
1349 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(sp), "Bar(xy,z())");
+ - ]
1350 : :
1351 [ + - + - ]: 1 : success = Func("Bar", sp);
1352 [ + - + - : 2 : BOOST_CHECK(success);
+ - ]
1353 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(sp), "xy,z()");
+ - ]
1354 : :
1355 [ + - + - ]: 1 : success = Func("xy", sp);
1356 [ + - + - : 2 : BOOST_CHECK(!success);
+ - ]
1357 : :
1358 : : // Expr(...): return expression that span begins with, update span to skip it
1359 : 1 : Span<const char> result;
1360 : :
1361 [ + - ]: 1 : input = "(n*(n-1))/2";
1362 [ + - ]: 1 : sp = input;
1363 [ + - ]: 1 : result = Expr(sp);
1364 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(result), "(n*(n-1))/2");
+ - ]
1365 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(sp), "");
+ - ]
1366 : :
1367 [ + - ]: 1 : input = "foo,bar";
1368 [ + - ]: 1 : sp = input;
1369 [ + - ]: 1 : result = Expr(sp);
1370 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(result), "foo");
+ - ]
1371 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(sp), ",bar");
+ - ]
1372 : :
1373 [ + - ]: 1 : input = "(aaaaa,bbbbb()),c";
1374 [ + - ]: 1 : sp = input;
1375 [ + - ]: 1 : result = Expr(sp);
1376 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(result), "(aaaaa,bbbbb())");
+ - ]
1377 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(sp), ",c");
+ - ]
1378 : :
1379 [ + - ]: 1 : input = "xyz)foo";
1380 [ + - ]: 1 : sp = input;
1381 [ + - ]: 1 : result = Expr(sp);
1382 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(result), "xyz");
+ - ]
1383 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(sp), ")foo");
+ - ]
1384 : :
1385 [ + - ]: 1 : input = "((a),(b),(c)),xxx";
1386 [ + - ]: 1 : sp = input;
1387 [ + - ]: 1 : result = Expr(sp);
1388 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(result), "((a),(b),(c))");
+ - ]
1389 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(sp), ",xxx");
+ - ]
1390 : :
1391 : : // Split(...): split a string on every instance of sep, return vector
1392 : 1 : std::vector<Span<const char>> results;
1393 : :
1394 [ + - ]: 1 : input = "xxx";
1395 [ + - ]: 2 : results = Split(input, 'x');
1396 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(results.size(), 4U);
1397 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(results[0]), "");
+ - ]
1398 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(results[1]), "");
+ - ]
1399 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(results[2]), "");
+ - ]
1400 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(results[3]), "");
+ - ]
1401 : :
1402 [ + - ]: 1 : input = "one#two#three";
1403 [ + - ]: 2 : results = Split(input, '-');
1404 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(results.size(), 1U);
1405 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(results[0]), "one#two#three");
+ - ]
1406 : :
1407 [ + - ]: 1 : input = "one#two#three";
1408 [ + - ]: 2 : results = Split(input, '#');
1409 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(results.size(), 3U);
1410 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(results[0]), "one");
+ - ]
1411 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(results[1]), "two");
+ - ]
1412 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(results[2]), "three");
+ - ]
1413 : :
1414 [ + - ]: 1 : input = "*foo*bar*";
1415 [ + - ]: 2 : results = Split(input, '*');
1416 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(results.size(), 4U);
1417 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(results[0]), "");
+ - ]
1418 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(results[1]), "foo");
+ - ]
1419 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(results[2]), "bar");
+ - ]
1420 [ + - + - : 1 : BOOST_CHECK_EQUAL(SpanToStr(results[3]), "");
+ - ]
1421 : 1 : }
1422 : :
1423 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(test_SplitString)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1424 : : {
1425 : : // Empty string.
1426 : 1 : {
1427 : 1 : std::vector<std::string> result = SplitString("", '-');
1428 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result.size(), 1);
1429 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result[0], "");
1430 : 1 : }
1431 : :
1432 : : // Empty items.
1433 : 1 : {
1434 : 1 : std::vector<std::string> result = SplitString("-", '-');
1435 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result.size(), 2);
1436 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result[0], "");
1437 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result[1], "");
1438 : 1 : }
1439 : :
1440 : : // More empty items.
1441 : 1 : {
1442 : 1 : std::vector<std::string> result = SplitString("--", '-');
1443 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result.size(), 3);
1444 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result[0], "");
1445 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result[1], "");
1446 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result[2], "");
1447 : 1 : }
1448 : :
1449 : : // Separator is not present.
1450 : 1 : {
1451 : 1 : std::vector<std::string> result = SplitString("abc", '-');
1452 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result.size(), 1);
1453 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result[0], "abc");
1454 : 1 : }
1455 : :
1456 : : // Basic behavior.
1457 : 1 : {
1458 : 1 : std::vector<std::string> result = SplitString("a-b", '-');
1459 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result.size(), 2);
1460 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result[0], "a");
1461 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result[1], "b");
1462 : 1 : }
1463 : :
1464 : : // Case-sensitivity of the separator.
1465 : 1 : {
1466 : 1 : std::vector<std::string> result = SplitString("AAA", 'a');
1467 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result.size(), 1);
1468 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(result[0], "AAA");
1469 : 1 : }
1470 : :
1471 : : // multiple split characters
1472 : 1 : {
1473 : 1 : using V = std::vector<std::string>;
1474 [ + - + + : 9 : BOOST_TEST(SplitString("a,b.c:d;e", ",;") == V({"a", "b.c:d", "e"}));
+ - + - +
- + - + +
- - - - ]
1475 [ + - + + : 13 : BOOST_TEST(SplitString("a,b.c:d;e", ",;:.") == V({"a", "b", "c", "d", "e"}));
+ - + - +
- + - + +
- - - - ]
1476 [ + - + + : 5 : BOOST_TEST(SplitString("a,b.c:d;e", "") == V({"a,b.c:d;e"}));
+ - + - +
- + - + +
- - - - ]
1477 [ + - + + : 5 : BOOST_TEST(SplitString("aaa", "bcdefg") == V({"aaa"}));
+ - + - +
- + - + +
- - - - ]
1478 [ + - + + : 8 : BOOST_TEST(SplitString("x\0a,b"s, "\0"s) == V({"x", "a,b"}));
+ - + - +
- + - + -
+ + - - -
- ]
1479 [ + - + + : 8 : BOOST_TEST(SplitString("x\0a,b"s, '\0') == V({"x", "a,b"}));
+ - + - +
- + - + +
- - - - ]
1480 [ + - + + : 10 : BOOST_TEST(SplitString("x\0a,b"s, "\0,"s) == V({"x", "a", "b"}));
+ - + - +
- + - + -
+ + - - -
- ]
1481 [ + - + + : 11 : BOOST_TEST(SplitString("abcdefg", "bcd") == V({"a", "", "", "efg"}));
+ - + - +
- + - + +
- - - - ]
1482 : : }
1483 : 1 : }
1484 : :
1485 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(test_LogEscapeMessage)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1486 : : {
1487 : : // ASCII and UTF-8 must pass through unaltered.
1488 [ + - ]: 1 : BOOST_CHECK_EQUAL(BCLog::LogEscapeMessage("Valid log message貓"), "Valid log message貓");
1489 : : // Newlines must pass through unaltered.
1490 [ + - ]: 1 : BOOST_CHECK_EQUAL(BCLog::LogEscapeMessage("Message\n with newlines\n"), "Message\n with newlines\n");
1491 : : // Other control characters are escaped in C syntax.
1492 [ + - ]: 1 : BOOST_CHECK_EQUAL(BCLog::LogEscapeMessage("\x01\x7f Corrupted log message\x0d"), R"(\x01\x7f Corrupted log message\x0d)");
1493 : : // Embedded NULL characters are escaped too.
1494 : 1 : const std::string NUL("O\x00O", 3);
1495 [ + - + - : 1 : BOOST_CHECK_EQUAL(BCLog::LogEscapeMessage(NUL), R"(O\x00O)");
+ - ]
1496 : 1 : }
1497 : :
1498 : : namespace {
1499 : :
1500 : : struct Tracker
1501 : : {
1502 : : //! Points to the original object (possibly itself) we moved/copied from
1503 : : const Tracker* origin;
1504 : : //! How many copies where involved between the original object and this one (moves are not counted)
1505 : : int copies{0};
1506 : :
1507 : 1 : Tracker() noexcept : origin(this) {}
1508 : 10 : Tracker(const Tracker& t) noexcept : origin(t.origin), copies(t.copies + 1) {}
1509 : 13 : Tracker(Tracker&& t) noexcept : origin(t.origin), copies(t.copies) {}
1510 : : Tracker& operator=(const Tracker& t) noexcept
1511 : : {
1512 : : if (this != &t) {
1513 : : origin = t.origin;
1514 : : copies = t.copies + 1;
1515 : : }
1516 : : return *this;
1517 : : }
1518 : : };
1519 : :
1520 : : }
1521 : :
1522 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(test_tracked_vector)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1523 : : {
1524 : 1 : Tracker t1;
1525 : 1 : Tracker t2;
1526 : 1 : Tracker t3;
1527 : :
1528 [ + - ]: 2 : BOOST_CHECK(t1.origin == &t1);
1529 [ + - ]: 2 : BOOST_CHECK(t2.origin == &t2);
1530 [ + - ]: 2 : BOOST_CHECK(t3.origin == &t3);
1531 : :
1532 : 1 : auto v1 = Vector(t1);
1533 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v1.size(), 1U);
1534 [ + - + - : 2 : BOOST_CHECK(v1[0].origin == &t1);
+ - ]
1535 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v1[0].copies, 1);
1536 : :
1537 [ + - ]: 1 : auto v2 = Vector(std::move(t2));
1538 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v2.size(), 1U);
1539 [ + - + - : 2 : BOOST_CHECK(v2[0].origin == &t2); // NOLINT(*-use-after-move)
+ - ]
1540 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v2[0].copies, 0);
1541 : :
1542 [ + - ]: 1 : auto v3 = Vector(t1, std::move(t2));
1543 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v3.size(), 2U);
1544 [ + - + - : 2 : BOOST_CHECK(v3[0].origin == &t1);
+ - ]
1545 [ + - + - : 2 : BOOST_CHECK(v3[1].origin == &t2); // NOLINT(*-use-after-move)
+ - ]
1546 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v3[0].copies, 1);
1547 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v3[1].copies, 0);
1548 : :
1549 [ + - ]: 1 : auto v4 = Vector(std::move(v3[0]), v3[1], std::move(t3));
1550 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v4.size(), 3U);
1551 [ + - + - : 2 : BOOST_CHECK(v4[0].origin == &t1);
+ - ]
1552 [ + - + - : 2 : BOOST_CHECK(v4[1].origin == &t2);
+ - ]
1553 [ + - + - : 2 : BOOST_CHECK(v4[2].origin == &t3); // NOLINT(*-use-after-move)
+ - ]
1554 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v4[0].copies, 1);
1555 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v4[1].copies, 1);
1556 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v4[2].copies, 0);
1557 : :
1558 [ + - + - ]: 1 : auto v5 = Cat(v1, v4);
1559 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v5.size(), 4U);
1560 [ + - + - : 2 : BOOST_CHECK(v5[0].origin == &t1);
+ - ]
1561 [ + - + - : 2 : BOOST_CHECK(v5[1].origin == &t1);
+ - ]
1562 [ + - + - : 2 : BOOST_CHECK(v5[2].origin == &t2);
+ - ]
1563 [ + - + - : 2 : BOOST_CHECK(v5[3].origin == &t3);
+ - ]
1564 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v5[0].copies, 2);
1565 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v5[1].copies, 2);
1566 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v5[2].copies, 2);
1567 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v5[3].copies, 1);
1568 : :
1569 [ + - ]: 1 : auto v6 = Cat(std::move(v1), v3);
1570 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v6.size(), 3U);
1571 [ + - + - : 2 : BOOST_CHECK(v6[0].origin == &t1);
+ - ]
1572 [ + - + - : 2 : BOOST_CHECK(v6[1].origin == &t1);
+ - ]
1573 [ + - + - : 2 : BOOST_CHECK(v6[2].origin == &t2);
+ - ]
1574 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v6[0].copies, 1);
1575 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v6[1].copies, 2);
1576 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v6[2].copies, 1);
1577 : :
1578 [ + - + - ]: 1 : auto v7 = Cat(v2, std::move(v4));
1579 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v7.size(), 4U);
1580 [ + - + - : 2 : BOOST_CHECK(v7[0].origin == &t2);
+ - ]
1581 [ + - + - : 2 : BOOST_CHECK(v7[1].origin == &t1);
+ - ]
1582 [ + - + - : 2 : BOOST_CHECK(v7[2].origin == &t2);
+ - ]
1583 [ + - + - : 2 : BOOST_CHECK(v7[3].origin == &t3);
+ - ]
1584 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v7[0].copies, 1);
1585 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v7[1].copies, 1);
1586 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v7[2].copies, 1);
1587 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v7[3].copies, 0);
1588 : :
1589 [ + - ]: 1 : auto v8 = Cat(std::move(v2), std::move(v3));
1590 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v8.size(), 3U);
1591 [ + - + - : 2 : BOOST_CHECK(v8[0].origin == &t2);
+ - ]
1592 [ + - + - : 2 : BOOST_CHECK(v8[1].origin == &t1);
+ - ]
1593 [ + - + - : 2 : BOOST_CHECK(v8[2].origin == &t2);
+ - ]
1594 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v8[0].copies, 0);
1595 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v8[1].copies, 1);
1596 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v8[2].copies, 0);
1597 : 1 : }
1598 : :
1599 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(message_sign)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1600 : : {
1601 : 1 : const std::array<unsigned char, 32> privkey_bytes = {
1602 : : // just some random data
1603 : : // derived address from this private key: 15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs
1604 : : 0xD9, 0x7F, 0x51, 0x08, 0xF1, 0x1C, 0xDA, 0x6E,
1605 : : 0xEE, 0xBA, 0xAA, 0x42, 0x0F, 0xEF, 0x07, 0x26,
1606 : : 0xB1, 0xF8, 0x98, 0x06, 0x0B, 0x98, 0x48, 0x9F,
1607 : : 0xA3, 0x09, 0x84, 0x63, 0xC0, 0x03, 0x28, 0x66
1608 : : };
1609 : :
1610 : 1 : const std::string message = "Trust no one";
1611 : :
1612 [ + - ]: 1 : const std::string expected_signature =
1613 [ + - ]: 1 : "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=";
1614 : :
1615 : 1 : CKey privkey;
1616 [ + - ]: 1 : std::string generated_signature;
1617 : :
1618 [ + - + - : 2 : BOOST_REQUIRE_MESSAGE(!privkey.IsValid(),
+ - ]
1619 : : "Confirm the private key is invalid");
1620 : :
1621 [ + - + - : 2 : BOOST_CHECK_MESSAGE(!MessageSign(privkey, message, generated_signature),
+ - + - ]
1622 : : "Sign with an invalid private key");
1623 : :
1624 [ + - ]: 1 : privkey.Set(privkey_bytes.begin(), privkey_bytes.end(), true);
1625 : :
1626 [ + - + - : 2 : BOOST_REQUIRE_MESSAGE(privkey.IsValid(),
+ - ]
1627 : : "Confirm the private key is valid");
1628 : :
1629 [ + - + - : 2 : BOOST_CHECK_MESSAGE(MessageSign(privkey, message, generated_signature),
+ - + - ]
1630 : : "Sign with a valid private key");
1631 : :
1632 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(expected_signature, generated_signature);
1633 : 1 : }
1634 : :
1635 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(message_verify)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1636 : : {
1637 [ + - + - : 1 : BOOST_CHECK_EQUAL(
+ - + - ]
1638 : : MessageVerify(
1639 : : "invalid address",
1640 : : "signature should be irrelevant",
1641 : : "message too"),
1642 : : MessageVerificationResult::ERR_INVALID_ADDRESS);
1643 : :
1644 [ + - + - : 1 : BOOST_CHECK_EQUAL(
+ - + - ]
1645 : : MessageVerify(
1646 : : "3B5fQsEXEaV8v6U3ejYc8XaKXAkyQj2MjV",
1647 : : "signature should be irrelevant",
1648 : : "message too"),
1649 : : MessageVerificationResult::ERR_ADDRESS_NO_KEY);
1650 : :
1651 [ + - + - : 1 : BOOST_CHECK_EQUAL(
+ - + - ]
1652 : : MessageVerify(
1653 : : "1KqbBpLy5FARmTPD4VZnDDpYjkUvkr82Pm",
1654 : : "invalid signature, not in base64 encoding",
1655 : : "message should be irrelevant"),
1656 : : MessageVerificationResult::ERR_MALFORMED_SIGNATURE);
1657 : :
1658 [ + - + - : 1 : BOOST_CHECK_EQUAL(
+ - + - ]
1659 : : MessageVerify(
1660 : : "1KqbBpLy5FARmTPD4VZnDDpYjkUvkr82Pm",
1661 : : "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
1662 : : "message should be irrelevant"),
1663 : : MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED);
1664 : :
1665 [ + - + - : 1 : BOOST_CHECK_EQUAL(
+ - + - ]
1666 : : MessageVerify(
1667 : : "15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs",
1668 : : "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=",
1669 : : "I never signed this"),
1670 : : MessageVerificationResult::ERR_NOT_SIGNED);
1671 : :
1672 [ + - + - : 1 : BOOST_CHECK_EQUAL(
+ - + - ]
1673 : : MessageVerify(
1674 : : "15CRxFdyRpGZLW9w8HnHvVduizdL5jKNbs",
1675 : : "IPojfrX2dfPnH26UegfbGQQLrdK844DlHq5157/P6h57WyuS/Qsl+h/WSVGDF4MUi4rWSswW38oimDYfNNUBUOk=",
1676 : : "Trust no one"),
1677 : : MessageVerificationResult::OK);
1678 : :
1679 [ + - + - : 1 : BOOST_CHECK_EQUAL(
+ - + - ]
1680 : : MessageVerify(
1681 : : "11canuhp9X2NocwCq7xNrQYTmUgZAnLK3",
1682 : : "IIcaIENoYW5jZWxsb3Igb24gYnJpbmsgb2Ygc2Vjb25kIGJhaWxvdXQgZm9yIGJhbmtzIAaHRtbCeDZINyavx14=",
1683 : : "Trust me"),
1684 : : MessageVerificationResult::OK);
1685 : 1 : }
1686 : :
1687 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(message_hash)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1688 : : {
1689 : 1 : const std::string unsigned_tx = "...";
1690 [ + - ]: 1 : const std::string prefixed_message =
1691 [ + - ]: 2 : std::string(1, (char)MESSAGE_MAGIC.length()) +
1692 [ + - ]: 2 : MESSAGE_MAGIC +
1693 [ + - + - : 2 : std::string(1, (char)unsigned_tx.length()) +
+ - ]
1694 [ + - ]: 1 : unsigned_tx;
1695 : :
1696 [ + - ]: 1 : const uint256 signature_hash = Hash(unsigned_tx);
1697 [ + - ]: 1 : const uint256 message_hash1 = Hash(prefixed_message);
1698 [ + - ]: 1 : const uint256 message_hash2 = MessageHash(unsigned_tx);
1699 : :
1700 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(message_hash1, message_hash2);
1701 [ + - + - ]: 1 : BOOST_CHECK_NE(message_hash1, signature_hash);
1702 : 1 : }
1703 : :
1704 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(remove_prefix)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1705 : : {
1706 [ + - ]: 1 : BOOST_CHECK_EQUAL(RemovePrefix("./common/system.h", "./"), "common/system.h");
1707 [ + - ]: 1 : BOOST_CHECK_EQUAL(RemovePrefixView("foo", "foo"), "");
1708 [ + - ]: 1 : BOOST_CHECK_EQUAL(RemovePrefix("foo", "fo"), "o");
1709 [ + - ]: 1 : BOOST_CHECK_EQUAL(RemovePrefixView("foo", "f"), "oo");
1710 [ + - ]: 1 : BOOST_CHECK_EQUAL(RemovePrefix("foo", ""), "foo");
1711 [ + - ]: 1 : BOOST_CHECK_EQUAL(RemovePrefixView("fo", "foo"), "fo");
1712 [ + - ]: 1 : BOOST_CHECK_EQUAL(RemovePrefix("f", "foo"), "f");
1713 [ + - ]: 1 : BOOST_CHECK_EQUAL(RemovePrefixView("", "foo"), "");
1714 [ + - ]: 1 : BOOST_CHECK_EQUAL(RemovePrefix("", ""), "");
1715 : 1 : }
1716 : :
1717 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(util_ParseByteUnits)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1718 : : {
1719 : 1 : auto noop = ByteUnit::NOOP;
1720 : :
1721 : : // no multiplier
1722 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseByteUnits("1", noop).value(), 1);
1723 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseByteUnits("0", noop).value(), 0);
1724 : :
1725 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseByteUnits("1k", noop).value(), 1000ULL);
1726 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseByteUnits("1K", noop).value(), 1ULL << 10);
1727 : :
1728 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseByteUnits("2m", noop).value(), 2'000'000ULL);
1729 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseByteUnits("2M", noop).value(), 2ULL << 20);
1730 : :
1731 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseByteUnits("3g", noop).value(), 3'000'000'000ULL);
1732 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseByteUnits("3G", noop).value(), 3ULL << 30);
1733 : :
1734 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseByteUnits("4t", noop).value(), 4'000'000'000'000ULL);
1735 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseByteUnits("4T", noop).value(), 4ULL << 40);
1736 : :
1737 : : // check default multiplier
1738 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseByteUnits("5", ByteUnit::K).value(), 5ULL << 10);
1739 : :
1740 : : // NaN
1741 [ + - + - ]: 2 : BOOST_CHECK(!ParseByteUnits("", noop));
1742 [ + - + - ]: 2 : BOOST_CHECK(!ParseByteUnits("foo", noop));
1743 : :
1744 : : // whitespace
1745 [ + - + - ]: 2 : BOOST_CHECK(!ParseByteUnits("123m ", noop));
1746 [ + - + - ]: 2 : BOOST_CHECK(!ParseByteUnits(" 123m", noop));
1747 : :
1748 : : // no +-
1749 [ + - + - ]: 2 : BOOST_CHECK(!ParseByteUnits("-123m", noop));
1750 [ + - + - ]: 2 : BOOST_CHECK(!ParseByteUnits("+123m", noop));
1751 : :
1752 : : // zero padding
1753 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseByteUnits("020M", noop).value(), 20ULL << 20);
1754 : :
1755 : : // fractions not allowed
1756 [ + - + - ]: 2 : BOOST_CHECK(!ParseByteUnits("0.5T", noop));
1757 : :
1758 : : // overflow
1759 [ + - + - ]: 2 : BOOST_CHECK(!ParseByteUnits("18446744073709551615g", noop));
1760 : :
1761 : : // invalid unit
1762 [ + - + - ]: 2 : BOOST_CHECK(!ParseByteUnits("1x", noop));
1763 : 1 : }
1764 : :
1765 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(util_ReadBinaryFile)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1766 : : {
1767 : 1 : fs::path tmpfolder = m_args.GetDataDirBase();
1768 [ + - + - ]: 2 : fs::path tmpfile = tmpfolder / "read_binary.dat";
1769 : 1 : std::string expected_text;
1770 [ + + ]: 31 : for (int i = 0; i < 30; i++) {
1771 [ + - ]: 30 : expected_text += "0123456789";
1772 : : }
1773 : 1 : {
1774 [ + - ]: 1 : std::ofstream file{tmpfile};
1775 [ + - ]: 1 : file << expected_text;
1776 : 1 : }
1777 : 1 : {
1778 : : // read all contents in file
1779 [ + - + - ]: 1 : auto [valid, text] = ReadBinaryFile(tmpfile);
1780 [ + - + - : 2 : BOOST_CHECK(valid);
+ - ]
1781 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(text, expected_text);
1782 : 0 : }
1783 : 1 : {
1784 : : // read half contents in file
1785 [ + - + - ]: 1 : auto [valid, text] = ReadBinaryFile(tmpfile, expected_text.size() / 2);
1786 [ + - + - : 2 : BOOST_CHECK(valid);
+ - ]
1787 [ + - + - : 1 : BOOST_CHECK_EQUAL(text, expected_text.substr(0, expected_text.size() / 2));
+ - ]
1788 : 0 : }
1789 : 1 : {
1790 : : // read from non-existent file
1791 [ + - + - ]: 2 : fs::path invalid_file = tmpfolder / "invalid_binary.dat";
1792 [ + - ]: 1 : auto [valid, text] = ReadBinaryFile(invalid_file);
1793 [ + - + - : 2 : BOOST_CHECK(!valid);
+ - ]
1794 [ + - + - ]: 2 : BOOST_CHECK(text.empty());
1795 : 2 : }
1796 : 3 : }
1797 : :
1798 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(util_WriteBinaryFile)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1799 : : {
1800 : 1 : fs::path tmpfolder = m_args.GetDataDirBase();
1801 [ + - + - ]: 2 : fs::path tmpfile = tmpfolder / "write_binary.dat";
1802 [ + - ]: 1 : std::string expected_text = "bitcoin";
1803 [ + - ]: 1 : auto valid = WriteBinaryFile(tmpfile, expected_text);
1804 [ + - ]: 1 : std::string actual_text;
1805 [ + - ]: 1 : std::ifstream file{tmpfile};
1806 [ + - ]: 1 : file >> actual_text;
1807 [ + - + - : 2 : BOOST_CHECK(valid);
+ - ]
1808 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(actual_text, expected_text);
1809 : 3 : }
1810 : :
1811 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(clearshrink_test)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1812 : : {
1813 : 1 : {
1814 : 1 : std::vector<uint8_t> v = {1, 2, 3};
1815 : 1 : ClearShrink(v);
1816 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v.size(), 0);
1817 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v.capacity(), 0);
1818 : 0 : }
1819 : :
1820 : 1 : {
1821 : 1 : std::vector<bool> v = {false, true, false, false, true, true};
1822 : 1 : ClearShrink(v);
1823 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v.size(), 0);
1824 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v.capacity(), 0);
1825 : 0 : }
1826 : :
1827 : 1 : {
1828 : 1 : std::deque<int> v = {1, 3, 3, 7};
1829 : 1 : ClearShrink(v);
1830 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v.size(), 0);
1831 : : // std::deque has no capacity() we can observe.
1832 : 1 : }
1833 : 1 : }
1834 : :
1835 : : BOOST_AUTO_TEST_SUITE_END()
|