Bitcoin Core  22.99.0
P2P Digital Currency
fs_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 //
5 #include <fs.h>
7 #include <util/system.h>
8 #include <util/getuniquepath.h>
9 
10 #include <boost/test/unit_test.hpp>
11 
13 
14 BOOST_AUTO_TEST_CASE(fsbridge_pathtostring)
15 {
16  std::string u8_str = "fs_tests_₿_🏃";
18  BOOST_CHECK_EQUAL(fs::u8path(u8_str).u8string(), u8_str);
19  BOOST_CHECK_EQUAL(fs::PathFromString(u8_str).u8string(), u8_str);
21 #ifndef WIN32
22  // On non-windows systems, verify that arbitrary byte strings containing
23  // invalid UTF-8 can be round tripped successfully with PathToString and
24  // PathFromString. On non-windows systems, paths are just byte strings so
25  // these functions do not do any encoding. On windows, paths are Unicode,
26  // and these functions do encoding and decoding, so the behavior of this
27  // test would be undefined.
28  std::string invalid_u8_str = "\xf0";
29  BOOST_CHECK_EQUAL(invalid_u8_str.size(), 1);
30  BOOST_CHECK_EQUAL(fs::PathToString(fs::PathFromString(invalid_u8_str)), invalid_u8_str);
31 #endif
32 }
33 
34 BOOST_AUTO_TEST_CASE(fsbridge_stem)
35 {
36  std::string test_filename = "fs_tests_₿_🏃.dat";
37  std::string expected_stem = "fs_tests_₿_🏃";
38  BOOST_CHECK_EQUAL(fs::PathToString(fs::PathFromString(test_filename).stem()), expected_stem);
39 }
40 
41 BOOST_AUTO_TEST_CASE(fsbridge_fstream)
42 {
43  fs::path tmpfolder = m_args.GetDataDirBase();
44  // tmpfile1 should be the same as tmpfile2
45  fs::path tmpfile1 = tmpfolder / "fs_tests_₿_🏃";
46  fs::path tmpfile2 = tmpfolder / "fs_tests_₿_🏃";
47  {
48  fsbridge::ofstream file(tmpfile1);
49  file << "bitcoin";
50  }
51  {
52  fsbridge::ifstream file(tmpfile2);
53  std::string input_buffer;
54  file >> input_buffer;
55  BOOST_CHECK_EQUAL(input_buffer, "bitcoin");
56  }
57  {
58  fsbridge::ifstream file(tmpfile1, std::ios_base::in | std::ios_base::ate);
59  std::string input_buffer;
60  file >> input_buffer;
61  BOOST_CHECK_EQUAL(input_buffer, "");
62  }
63  {
64  fsbridge::ofstream file(tmpfile2, std::ios_base::out | std::ios_base::app);
65  file << "tests";
66  }
67  {
68  fsbridge::ifstream file(tmpfile1);
69  std::string input_buffer;
70  file >> input_buffer;
71  BOOST_CHECK_EQUAL(input_buffer, "bitcointests");
72  }
73  {
74  fsbridge::ofstream file(tmpfile2, std::ios_base::out | std::ios_base::trunc);
75  file << "bitcoin";
76  }
77  {
78  fsbridge::ifstream file(tmpfile1);
79  std::string input_buffer;
80  file >> input_buffer;
81  BOOST_CHECK_EQUAL(input_buffer, "bitcoin");
82  }
83  {
84  // Join an absolute path and a relative path.
85  fs::path p = fsbridge::AbsPathJoin(tmpfolder, "fs_tests_₿_🏃");
86  BOOST_CHECK(p.is_absolute());
87  BOOST_CHECK_EQUAL(tmpfile1, p);
88  }
89  {
90  // Join two absolute paths.
91  fs::path p = fsbridge::AbsPathJoin(tmpfile1, tmpfile2);
92  BOOST_CHECK(p.is_absolute());
93  BOOST_CHECK_EQUAL(tmpfile2, p);
94  }
95  {
96  // Ensure joining with empty paths does not add trailing path components.
97  BOOST_CHECK_EQUAL(tmpfile1, fsbridge::AbsPathJoin(tmpfile1, ""));
98  BOOST_CHECK_EQUAL(tmpfile1, fsbridge::AbsPathJoin(tmpfile1, {}));
99  }
100  {
101  fs::path p1 = GetUniquePath(tmpfolder);
102  fs::path p2 = GetUniquePath(tmpfolder);
103  fs::path p3 = GetUniquePath(tmpfolder);
104 
105  // Ensure that the parent path is always the same.
106  BOOST_CHECK_EQUAL(tmpfolder, p1.parent_path());
107  BOOST_CHECK_EQUAL(tmpfolder, p2.parent_path());
108  BOOST_CHECK_EQUAL(tmpfolder, p3.parent_path());
109 
110  // Ensure that generated paths are actually different.
111  BOOST_CHECK(p1 != p2);
112  BOOST_CHECK(p2 != p3);
113  BOOST_CHECK(p1 != p3);
114  }
115 }
116 
fsbridge::ifstream
fs::ifstream ifstream
Definition: fs.h:234
fs.h
setup_common.h
fsbridge::AbsPathJoin
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
Definition: fs.cpp:35
fs::PathToString
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:120
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
fsbridge::ofstream
fs::ofstream ofstream
Definition: fs.h:235
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(fsbridge_pathtostring)
Definition: fs_tests.cpp:14
fs::path
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
Definition: fs.h:33
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:83
fs::PathFromString
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition: fs.h:143
system.h
fs::u8path
static path u8path(const std::string &string)
Definition: fs.h:63
getuniquepath.h
GetUniquePath
fs::path GetUniquePath(const fs::path &base)
Helper function for getting a unique path.
Definition: getuniquepath.cpp:9
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18