Bitcoin Core  22.99.0
P2P Digital Currency
fs_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2019 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_fstream)
15 {
16  fs::path tmpfolder = m_args.GetDataDirBase();
17  // tmpfile1 should be the same as tmpfile2
18  fs::path tmpfile1 = tmpfolder / "fs_tests_₿_🏃";
19  fs::path tmpfile2 = tmpfolder / "fs_tests_₿_🏃";
20  {
21  fsbridge::ofstream file(tmpfile1);
22  file << "bitcoin";
23  }
24  {
25  fsbridge::ifstream file(tmpfile2);
26  std::string input_buffer;
27  file >> input_buffer;
28  BOOST_CHECK_EQUAL(input_buffer, "bitcoin");
29  }
30  {
31  fsbridge::ifstream file(tmpfile1, std::ios_base::in | std::ios_base::ate);
32  std::string input_buffer;
33  file >> input_buffer;
34  BOOST_CHECK_EQUAL(input_buffer, "");
35  }
36  {
37  fsbridge::ofstream file(tmpfile2, std::ios_base::out | std::ios_base::app);
38  file << "tests";
39  }
40  {
41  fsbridge::ifstream file(tmpfile1);
42  std::string input_buffer;
43  file >> input_buffer;
44  BOOST_CHECK_EQUAL(input_buffer, "bitcointests");
45  }
46  {
47  fsbridge::ofstream file(tmpfile2, std::ios_base::out | std::ios_base::trunc);
48  file << "bitcoin";
49  }
50  {
51  fsbridge::ifstream file(tmpfile1);
52  std::string input_buffer;
53  file >> input_buffer;
54  BOOST_CHECK_EQUAL(input_buffer, "bitcoin");
55  }
56  {
57  // Join an absolute path and a relative path.
58  fs::path p = fsbridge::AbsPathJoin(tmpfolder, "fs_tests_₿_🏃");
59  BOOST_CHECK(p.is_absolute());
60  BOOST_CHECK_EQUAL(tmpfile1, p);
61  }
62  {
63  // Join two absolute paths.
64  fs::path p = fsbridge::AbsPathJoin(tmpfile1, tmpfile2);
65  BOOST_CHECK(p.is_absolute());
66  BOOST_CHECK_EQUAL(tmpfile2, p);
67  }
68  {
69  // Ensure joining with empty paths does not add trailing path components.
70  BOOST_CHECK_EQUAL(tmpfile1, fsbridge::AbsPathJoin(tmpfile1, ""));
71  BOOST_CHECK_EQUAL(tmpfile1, fsbridge::AbsPathJoin(tmpfile1, {}));
72  }
73  {
74  fs::path p1 = GetUniquePath(tmpfolder);
75  fs::path p2 = GetUniquePath(tmpfolder);
76  fs::path p3 = GetUniquePath(tmpfolder);
77 
78  // Ensure that the parent path is always the same.
79  BOOST_CHECK_EQUAL(tmpfolder, p1.parent_path());
80  BOOST_CHECK_EQUAL(tmpfolder, p2.parent_path());
81  BOOST_CHECK_EQUAL(tmpfolder, p3.parent_path());
82 
83  // Ensure that generated paths are actually different.
84  BOOST_CHECK(p1 != p2);
85  BOOST_CHECK(p2 != p3);
86  BOOST_CHECK(p1 != p3);
87  }
88 }
89 
fsbridge::ifstream
fs::ifstream ifstream
Definition: fs.h:101
fs.h
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(fsbridge_fstream)
Definition: fs_tests.cpp:14
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:34
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:102
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:76
system.h
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