rippled
Loading...
Searching...
No Matches
FileUtilities_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2018 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <test/unit_test/FileDirGuard.h>
21
22#include <xrpl/basics/ByteUtilities.h>
23#include <xrpl/basics/FileUtilities.h>
24#include <xrpl/beast/unit_test.h>
25
26namespace ripple {
27
29{
30public:
31 void
33 {
34 using namespace ripple::test::detail;
35 using namespace boost::system;
36
37 constexpr char const* expectedContents =
38 "This file is very short. That's all we need.";
39
40 FileDirGuard file(
41 *this,
42 "test_file",
43 "test.txt",
44 "This is temporary text that should get overwritten");
45
46 error_code ec;
47 auto const path = file.file();
48
49 writeFileContents(ec, path, expectedContents);
50 BEAST_EXPECT(!ec);
51
52 {
53 // Test with no max
54 auto const good = getFileContents(ec, path);
55 BEAST_EXPECT(!ec);
56 BEAST_EXPECT(good == expectedContents);
57 }
58
59 {
60 // Test with large max
61 auto const good = getFileContents(ec, path, kilobytes(1));
62 BEAST_EXPECT(!ec);
63 BEAST_EXPECT(good == expectedContents);
64 }
65
66 {
67 // Test with small max
68 auto const bad = getFileContents(ec, path, 16);
69 BEAST_EXPECT(
70 ec && ec.value() == boost::system::errc::file_too_large);
71 BEAST_EXPECT(bad.empty());
72 }
73 }
74
75 void
76 run() override
77 {
79 }
80};
81
82BEAST_DEFINE_TESTSUITE(FileUtilities, basics, ripple);
83
84} // namespace ripple
A testsuite class.
Definition: suite.h:55
void run() override
Runs the suite.
Write a file in a directory and remove when done.
Definition: FileDirGuard.h:112
Add a path.
Definition: paths.h:58
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
constexpr auto kilobytes(T value) noexcept
Definition: ByteUtilities.h:27
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)