rippled
Loading...
Searching...
No Matches
FileUtilities_test.cpp
1#include <test/unit_test/FileDirGuard.h>
2
3#include <xrpl/basics/ByteUtilities.h>
4#include <xrpl/basics/FileUtilities.h>
5#include <xrpl/beast/unit_test.h>
6
7namespace xrpl {
8
10{
11public:
12 void
14 {
15 using namespace xrpl::detail;
16 using namespace boost::system;
17
18 constexpr char const* expectedContents = "This file is very short. That's all we need.";
19
20 FileDirGuard const file(
21 *this, "test_file", "test.txt", "This is temporary text that should get overwritten");
22
23 error_code ec;
24 auto const path = file.file();
25
26 writeFileContents(ec, path, expectedContents);
27 BEAST_EXPECT(!ec);
28
29 {
30 // Test with no max
31 auto const good = getFileContents(ec, path);
32 BEAST_EXPECT(!ec);
33 BEAST_EXPECT(good == expectedContents);
34 }
35
36 {
37 // Test with large max
38 auto const good = getFileContents(ec, path, kilobytes(1));
39 BEAST_EXPECT(!ec);
40 BEAST_EXPECT(good == expectedContents);
41 }
42
43 {
44 // Test with small max
45 auto const bad = getFileContents(ec, path, 16);
46 BEAST_EXPECT(ec && ec.value() == boost::system::errc::file_too_large);
47 BEAST_EXPECT(bad.empty());
48 }
49 }
50
51 void
52 run() override
53 {
55 }
56};
57
58BEAST_DEFINE_TESTSUITE(FileUtilities, basics, xrpl);
59
60} // namespace xrpl
A testsuite class.
Definition suite.h:51
void run() override
Runs the suite.
Write a file in a directory and remove when done.
path const & file() const
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string getFileContents(boost::system::error_code &ec, boost::filesystem::path const &sourcePath, std::optional< std::size_t > maxSize=std::nullopt)
void writeFileContents(boost::system::error_code &ec, boost::filesystem::path const &destPath, std::string const &contents)
constexpr auto kilobytes(T value) noexcept