|
17 | 17 | #include <util/message.h> // For MessageSign(), MessageVerify(), MESSAGE_MAGIC
|
18 | 18 | #include <util/moneystr.h>
|
19 | 19 | #include <util/overflow.h>
|
| 20 | +#include <util/readwritefile.h> |
20 | 21 | #include <util/spanparsing.h>
|
21 | 22 | #include <util/strencodings.h>
|
22 | 23 | #include <util/string.h>
|
23 | 24 | #include <util/time.h>
|
24 | 25 | #include <util/vector.h>
|
25 | 26 |
|
26 | 27 | #include <array>
|
27 |
| -#include <optional> |
| 28 | +#include <fstream> |
28 | 29 | #include <limits>
|
29 | 30 | #include <map>
|
| 31 | +#include <optional> |
30 | 32 | #include <stdint.h>
|
31 | 33 | #include <string.h>
|
32 | 34 | #include <thread>
|
@@ -2592,4 +2594,49 @@ BOOST_AUTO_TEST_CASE(util_ParseByteUnits)
|
2592 | 2594 | BOOST_CHECK(!ParseByteUnits("1x", noop));
|
2593 | 2595 | }
|
2594 | 2596 |
|
| 2597 | +BOOST_AUTO_TEST_CASE(util_ReadBinaryFile) |
| 2598 | +{ |
| 2599 | + fs::path tmpfolder = m_args.GetDataDirBase(); |
| 2600 | + fs::path tmpfile = tmpfolder / "read_binary.dat"; |
| 2601 | + std::string expected_text; |
| 2602 | + for (int i = 0; i < 30; i++) { |
| 2603 | + expected_text += "0123456789"; |
| 2604 | + } |
| 2605 | + { |
| 2606 | + std::ofstream file{tmpfile}; |
| 2607 | + file << expected_text; |
| 2608 | + } |
| 2609 | + { |
| 2610 | + // read all contents in file |
| 2611 | + auto [valid, text] = ReadBinaryFile(tmpfile); |
| 2612 | + BOOST_CHECK(valid); |
| 2613 | + BOOST_CHECK_EQUAL(text, expected_text); |
| 2614 | + } |
| 2615 | + { |
| 2616 | + // read half contents in file |
| 2617 | + auto [valid, text] = ReadBinaryFile(tmpfile, expected_text.size() / 2); |
| 2618 | + BOOST_CHECK(valid); |
| 2619 | + BOOST_CHECK_EQUAL(text, expected_text.substr(0, expected_text.size() / 2)); |
| 2620 | + } |
| 2621 | + { |
| 2622 | + // read from non-existent file |
| 2623 | + fs::path invalid_file = tmpfolder / "invalid_binary.dat"; |
| 2624 | + auto [valid, text] = ReadBinaryFile(invalid_file); |
| 2625 | + BOOST_CHECK(!valid); |
| 2626 | + BOOST_CHECK(text.empty()); |
| 2627 | + } |
| 2628 | +} |
| 2629 | + |
| 2630 | +BOOST_AUTO_TEST_CASE(util_WriteBinaryFile) |
| 2631 | +{ |
| 2632 | + fs::path tmpfolder = m_args.GetDataDirBase(); |
| 2633 | + fs::path tmpfile = tmpfolder / "write_binary.dat"; |
| 2634 | + std::string expected_text = "bitcoin"; |
| 2635 | + auto valid = WriteBinaryFile(tmpfile, expected_text); |
| 2636 | + std::string actual_text; |
| 2637 | + std::ifstream file{tmpfile}; |
| 2638 | + file >> actual_text; |
| 2639 | + BOOST_CHECK(valid); |
| 2640 | + BOOST_CHECK_EQUAL(actual_text, expected_text); |
| 2641 | +} |
2595 | 2642 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments