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