|
18 | 18 | #include <util/moneystr.h> |
19 | 19 | #include <util/overflow.h> |
20 | 20 | #include <util/ranges_set.h> |
| 21 | +#include <util/readwritefile.h> |
21 | 22 | #include <util/spanparsing.h> |
22 | 23 | #include <util/strencodings.h> |
23 | 24 | #include <util/string.h> |
24 | 25 | #include <util/time.h> |
25 | 26 | #include <util/vector.h> |
26 | 27 |
|
27 | 28 | #include <array> |
| 29 | +#include <fstream> |
28 | 30 | #include <deque> |
29 | 31 | #include <optional> |
30 | 32 | #include <random> |
@@ -2747,6 +2749,52 @@ BOOST_AUTO_TEST_CASE(util_ParseByteUnits) |
2747 | 2749 | BOOST_CHECK(!ParseByteUnits("1x", noop)); |
2748 | 2750 | } |
2749 | 2751 |
|
| 2752 | +BOOST_AUTO_TEST_CASE(util_ReadBinaryFile) |
| 2753 | +{ |
| 2754 | + fs::path tmpfolder = m_args.GetDataDirBase(); |
| 2755 | + fs::path tmpfile = tmpfolder / "read_binary.dat"; |
| 2756 | + std::string expected_text; |
| 2757 | + for (int i = 0; i < 30; i++) { |
| 2758 | + expected_text += "0123456789"; |
| 2759 | + } |
| 2760 | + { |
| 2761 | + std::ofstream file{tmpfile}; |
| 2762 | + file << expected_text; |
| 2763 | + } |
| 2764 | + { |
| 2765 | + // read all contents in file |
| 2766 | + auto [valid, text] = ReadBinaryFile(tmpfile); |
| 2767 | + BOOST_CHECK(valid); |
| 2768 | + BOOST_CHECK_EQUAL(text, expected_text); |
| 2769 | + } |
| 2770 | + { |
| 2771 | + // read half contents in file |
| 2772 | + auto [valid, text] = ReadBinaryFile(tmpfile, expected_text.size() / 2); |
| 2773 | + BOOST_CHECK(valid); |
| 2774 | + BOOST_CHECK_EQUAL(text, expected_text.substr(0, expected_text.size() / 2)); |
| 2775 | + } |
| 2776 | + { |
| 2777 | + // read from non-existent file |
| 2778 | + fs::path invalid_file = tmpfolder / "invalid_binary.dat"; |
| 2779 | + auto [valid, text] = ReadBinaryFile(invalid_file); |
| 2780 | + BOOST_CHECK(!valid); |
| 2781 | + BOOST_CHECK(text.empty()); |
| 2782 | + } |
| 2783 | +} |
| 2784 | + |
| 2785 | +BOOST_AUTO_TEST_CASE(util_WriteBinaryFile) |
| 2786 | +{ |
| 2787 | + fs::path tmpfolder = m_args.GetDataDirBase(); |
| 2788 | + fs::path tmpfile = tmpfolder / "write_binary.dat"; |
| 2789 | + std::string expected_text = "bitcoin"; |
| 2790 | + auto valid = WriteBinaryFile(tmpfile, expected_text); |
| 2791 | + std::string actual_text; |
| 2792 | + std::ifstream file{tmpfile}; |
| 2793 | + file >> actual_text; |
| 2794 | + BOOST_CHECK(valid); |
| 2795 | + BOOST_CHECK_EQUAL(actual_text, expected_text); |
| 2796 | +} |
| 2797 | + |
2750 | 2798 | BOOST_AUTO_TEST_CASE(clearshrink_test) |
2751 | 2799 | { |
2752 | 2800 | { |
|
0 commit comments