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 file(*this, "test_file", "test.txt", "This is temporary text that should get overwritten");
21
22 error_code ec;
23 auto const path = file.file();
24
25 writeFileContents(ec, path, expectedContents);
26 BEAST_EXPECT(!ec);
27
28 {
29 // Test with no max
30 auto const good = getFileContents(ec, path);
31 BEAST_EXPECT(!ec);
32 BEAST_EXPECT(good == expectedContents);
33 }
34
35 {
36 // Test with large max
37 auto const good = getFileContents(ec, path, kilobytes(1));
38 BEAST_EXPECT(!ec);
39 BEAST_EXPECT(good == expectedContents);
40 }
41
42 {
43 // Test with small max
44 auto const bad = getFileContents(ec, path, 16);
45 BEAST_EXPECT(ec && ec.value() == boost::system::errc::file_too_large);
46 BEAST_EXPECT(bad.empty());
47 }
48 }
49
50 void
51 run() override
52 {
54 }
55};
56
57BEAST_DEFINE_TESTSUITE(FileUtilities, basics, xrpl);
58
59} // namespace xrpl
A testsuite class.
Definition suite.h:52
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:6
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