|
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>
|
@@ -2576,4 +2578,49 @@ BOOST_AUTO_TEST_CASE(util_ParseByteUnits)
|
2576 | 2578 | BOOST_CHECK(!ParseByteUnits("1x", noop));
|
2577 | 2579 | }
|
2578 | 2580 |
|
| 2581 | +BOOST_AUTO_TEST_CASE(util_ReadBinaryFile) |
| 2582 | +{ |
| 2583 | + fs::path tmpfolder = m_args.GetDataDirBase(); |
| 2584 | + fs::path tmpfile = tmpfolder / "read_binary.dat"; |
| 2585 | + std::string expected_text; |
| 2586 | + for (int i = 0; i < 30; i++) { |
| 2587 | + expected_text += "0123456789"; |
| 2588 | + } |
| 2589 | + { |
| 2590 | + std::ofstream file{tmpfile}; |
| 2591 | + file << expected_text; |
| 2592 | + } |
| 2593 | + { |
| 2594 | + // read all contents in file |
| 2595 | + auto [valid, text] = ReadBinaryFile(tmpfile); |
| 2596 | + BOOST_CHECK(valid); |
| 2597 | + BOOST_CHECK_EQUAL(text, expected_text); |
| 2598 | + } |
| 2599 | + { |
| 2600 | + // read half contents in file |
| 2601 | + auto [valid, text] = ReadBinaryFile(tmpfile, expected_text.size() / 2); |
| 2602 | + BOOST_CHECK(valid); |
| 2603 | + BOOST_CHECK_EQUAL(text, expected_text.substr(0, expected_text.size() / 2)); |
| 2604 | + } |
| 2605 | + { |
| 2606 | + // read from non-existent file |
| 2607 | + fs::path invalid_file = tmpfolder / "invalid_binary.dat"; |
| 2608 | + auto [valid, text] = ReadBinaryFile(invalid_file); |
| 2609 | + BOOST_CHECK(!valid); |
| 2610 | + BOOST_CHECK(text.empty()); |
| 2611 | + } |
| 2612 | +} |
| 2613 | + |
| 2614 | +BOOST_AUTO_TEST_CASE(util_WriteBinaryFile) |
| 2615 | +{ |
| 2616 | + fs::path tmpfolder = m_args.GetDataDirBase(); |
| 2617 | + fs::path tmpfile = tmpfolder / "write_binary.dat"; |
| 2618 | + std::string expected_text = "bitcoin"; |
| 2619 | + auto valid = WriteBinaryFile(tmpfile, expected_text); |
| 2620 | + std::string actual_text; |
| 2621 | + std::ifstream file{tmpfile}; |
| 2622 | + file >> actual_text; |
| 2623 | + BOOST_CHECK(valid); |
| 2624 | + BOOST_CHECK_EQUAL(actual_text, expected_text); |
| 2625 | +} |
2579 | 2626 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments