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 ripple {
8
10{
11public:
12 void
14 {
15 using namespace ripple::detail;
16 using namespace boost::system;
17
18 constexpr char const* expectedContents =
19 "This file is very short. That's all we need.";
20
21 FileDirGuard file(
22 *this,
23 "test_file",
24 "test.txt",
25 "This is temporary text that should get overwritten");
26
27 error_code ec;
28 auto const path = file.file();
29
30 writeFileContents(ec, path, expectedContents);
31 BEAST_EXPECT(!ec);
32
33 {
34 // Test with no max
35 auto const good = getFileContents(ec, path);
36 BEAST_EXPECT(!ec);
37 BEAST_EXPECT(good == expectedContents);
38 }
39
40 {
41 // Test with large max
42 auto const good = getFileContents(ec, path, kilobytes(1));
43 BEAST_EXPECT(!ec);
44 BEAST_EXPECT(good == expectedContents);
45 }
46
47 {
48 // Test with small max
49 auto const bad = getFileContents(ec, path, 16);
50 BEAST_EXPECT(
51 ec && ec.value() == boost::system::errc::file_too_large);
52 BEAST_EXPECT(bad.empty());
53 }
54 }
55
56 void
57 run() override
58 {
60 }
61};
62
63BEAST_DEFINE_TESTSUITE(FileUtilities, basics, ripple);
64
65} // namespace ripple
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
constexpr auto kilobytes(T value) noexcept
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)