rippled
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 <ripple/basics/ByteUtilities.h>
21 #include <ripple/basics/FileUtilities.h>
22 #include <ripple/beast/unit_test.h>
23 #include <test/unit_test/FileDirGuard.h>
24 
25 namespace ripple {
26 
27 class FileUtilities_test : public beast::unit_test::suite
28 {
29 public:
30  void
32  {
33  using namespace ripple::test::detail;
34  using namespace boost::system;
35 
36  constexpr const char* expectedContents =
37  "This file is very short. That's all we need.";
38 
39  FileDirGuard file(
40  *this,
41  "test_file",
42  "test.txt",
43  "This is temporary text that should get overwritten");
44 
45  error_code ec;
46  auto const path = file.file();
47 
48  writeFileContents(ec, path, expectedContents);
49  BEAST_EXPECT(!ec);
50 
51  {
52  // Test with no max
53  auto const good = getFileContents(ec, path);
54  BEAST_EXPECT(!ec);
55  BEAST_EXPECT(good == expectedContents);
56  }
57 
58  {
59  // Test with large max
60  auto const good = getFileContents(ec, path, kilobytes(1));
61  BEAST_EXPECT(!ec);
62  BEAST_EXPECT(good == expectedContents);
63  }
64 
65  {
66  // Test with small max
67  auto const bad = getFileContents(ec, path, 16);
68  BEAST_EXPECT(
69  ec && ec.value() == boost::system::errc::file_too_large);
70  BEAST_EXPECT(bad.empty());
71  }
72  }
73 
74  void
75  run() override
76  {
78  }
79 };
80 
81 BEAST_DEFINE_TESTSUITE(FileUtilities, ripple_basics, ripple);
82 
83 } // namespace ripple
ripple::getFileContents
std::string getFileContents(boost::system::error_code &ec, boost::filesystem::path const &sourcePath, std::optional< std::size_t > maxSize=std::nullopt)
Definition: FileUtilities.cpp:25
ripple::FileUtilities_test::run
void run() override
Definition: FileUtilities_test.cpp:75
ripple::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, ripple)
ripple::kilobytes
constexpr auto kilobytes(T value) noexcept
Definition: ByteUtilities.h:27
ripple::test::detail
Definition: Path_test.cpp:46
ripple::test::detail::FileDirGuard::file
path const & file() const
Definition: FileDirGuard.h:164
ripple::writeFileContents
void writeFileContents(boost::system::error_code &ec, boost::filesystem::path const &destPath, std::string const &contents)
Definition: FileUtilities.cpp:66
ripple::FileUtilities_test
Definition: FileUtilities_test.cpp:27
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::detail::FileDirGuard
Write a file in a directory and remove when done.
Definition: FileDirGuard.h:107
ripple::FileUtilities_test::testGetFileContents
void testGetFileContents()
Definition: FileUtilities_test.cpp:31