Branch data Line data Source code
# 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>
# 6 : : #include <test/util/setup_common.h>
# 7 : : #include <util/system.h>
# 8 : : #include <util/getuniquepath.h>
# 9 : :
# 10 : : #include <boost/test/unit_test.hpp>
# 11 : :
# 12 : : #include <fstream>
# 13 : : #include <ios>
# 14 : : #include <string>
# 15 : :
# 16 : : BOOST_FIXTURE_TEST_SUITE(fs_tests, BasicTestingSetup)
# 17 : :
# 18 : : BOOST_AUTO_TEST_CASE(fsbridge_pathtostring)
# 19 : 2 : {
# 20 : 2 : std::string u8_str = "fs_tests_₿_🏃";
# 21 : 2 : BOOST_CHECK_EQUAL(fs::PathToString(fs::PathFromString(u8_str)), u8_str);
# 22 : 2 : BOOST_CHECK_EQUAL(fs::u8path(u8_str).u8string(), u8_str);
# 23 : 2 : BOOST_CHECK_EQUAL(fs::PathFromString(u8_str).u8string(), u8_str);
# 24 : 2 : BOOST_CHECK_EQUAL(fs::PathToString(fs::u8path(u8_str)), u8_str);
# 25 : 2 : #ifndef WIN32
# 26 : : // On non-windows systems, verify that arbitrary byte strings containing
# 27 : : // invalid UTF-8 can be round tripped successfully with PathToString and
# 28 : : // PathFromString. On non-windows systems, paths are just byte strings so
# 29 : : // these functions do not do any encoding. On windows, paths are Unicode,
# 30 : : // and these functions do encoding and decoding, so the behavior of this
# 31 : : // test would be undefined.
# 32 : 2 : std::string invalid_u8_str = "\xf0";
# 33 : 2 : BOOST_CHECK_EQUAL(invalid_u8_str.size(), 1);
# 34 : 2 : BOOST_CHECK_EQUAL(fs::PathToString(fs::PathFromString(invalid_u8_str)), invalid_u8_str);
# 35 : 2 : #endif
# 36 : 2 : }
# 37 : :
# 38 : : BOOST_AUTO_TEST_CASE(fsbridge_stem)
# 39 : 2 : {
# 40 : 2 : std::string test_filename = "fs_tests_₿_🏃.dat";
# 41 : 2 : std::string expected_stem = "fs_tests_₿_🏃";
# 42 : 2 : BOOST_CHECK_EQUAL(fs::PathToString(fs::PathFromString(test_filename).stem()), expected_stem);
# 43 : 2 : }
# 44 : :
# 45 : : BOOST_AUTO_TEST_CASE(fsbridge_fstream)
# 46 : 2 : {
# 47 : 2 : fs::path tmpfolder = m_args.GetDataDirBase();
# 48 : : // tmpfile1 should be the same as tmpfile2
# 49 : 2 : fs::path tmpfile1 = tmpfolder / fs::u8path("fs_tests_₿_🏃");
# 50 : 2 : fs::path tmpfile2 = tmpfolder / fs::u8path("fs_tests_₿_🏃");
# 51 : 2 : {
# 52 : 2 : std::ofstream file{tmpfile1};
# 53 : 2 : file << "bitcoin";
# 54 : 2 : }
# 55 : 2 : {
# 56 : 2 : std::ifstream file{tmpfile2};
# 57 : 2 : std::string input_buffer;
# 58 : 2 : file >> input_buffer;
# 59 : 2 : BOOST_CHECK_EQUAL(input_buffer, "bitcoin");
# 60 : 2 : }
# 61 : 2 : {
# 62 : 2 : std::ifstream file{tmpfile1, std::ios_base::in | std::ios_base::ate};
# 63 : 2 : std::string input_buffer;
# 64 : 2 : file >> input_buffer;
# 65 : 2 : BOOST_CHECK_EQUAL(input_buffer, "");
# 66 : 2 : }
# 67 : 2 : {
# 68 : 2 : std::ofstream file{tmpfile2, std::ios_base::out | std::ios_base::app};
# 69 : 2 : file << "tests";
# 70 : 2 : }
# 71 : 2 : {
# 72 : 2 : std::ifstream file{tmpfile1};
# 73 : 2 : std::string input_buffer;
# 74 : 2 : file >> input_buffer;
# 75 : 2 : BOOST_CHECK_EQUAL(input_buffer, "bitcointests");
# 76 : 2 : }
# 77 : 2 : {
# 78 : 2 : std::ofstream file{tmpfile2, std::ios_base::out | std::ios_base::trunc};
# 79 : 2 : file << "bitcoin";
# 80 : 2 : }
# 81 : 2 : {
# 82 : 2 : std::ifstream file{tmpfile1};
# 83 : 2 : std::string input_buffer;
# 84 : 2 : file >> input_buffer;
# 85 : 2 : BOOST_CHECK_EQUAL(input_buffer, "bitcoin");
# 86 : 2 : }
# 87 : 2 : {
# 88 : : // Join an absolute path and a relative path.
# 89 : 2 : fs::path p = fsbridge::AbsPathJoin(tmpfolder, fs::u8path("fs_tests_₿_🏃"));
# 90 : 2 : BOOST_CHECK(p.is_absolute());
# 91 : 2 : BOOST_CHECK_EQUAL(tmpfile1, p);
# 92 : 2 : }
# 93 : 2 : {
# 94 : : // Join two absolute paths.
# 95 : 2 : fs::path p = fsbridge::AbsPathJoin(tmpfile1, tmpfile2);
# 96 : 2 : BOOST_CHECK(p.is_absolute());
# 97 : 2 : BOOST_CHECK_EQUAL(tmpfile2, p);
# 98 : 2 : }
# 99 : 2 : {
# 100 : : // Ensure joining with empty paths does not add trailing path components.
# 101 : 2 : BOOST_CHECK_EQUAL(tmpfile1, fsbridge::AbsPathJoin(tmpfile1, ""));
# 102 : 2 : BOOST_CHECK_EQUAL(tmpfile1, fsbridge::AbsPathJoin(tmpfile1, {}));
# 103 : 2 : }
# 104 : 2 : {
# 105 : 2 : fs::path p1 = GetUniquePath(tmpfolder);
# 106 : 2 : fs::path p2 = GetUniquePath(tmpfolder);
# 107 : 2 : fs::path p3 = GetUniquePath(tmpfolder);
# 108 : :
# 109 : : // Ensure that the parent path is always the same.
# 110 : 2 : BOOST_CHECK_EQUAL(tmpfolder, p1.parent_path());
# 111 : 2 : BOOST_CHECK_EQUAL(tmpfolder, p2.parent_path());
# 112 : 2 : BOOST_CHECK_EQUAL(tmpfolder, p3.parent_path());
# 113 : :
# 114 : : // Ensure that generated paths are actually different.
# 115 : 2 : BOOST_CHECK(p1 != p2);
# 116 : 2 : BOOST_CHECK(p2 != p3);
# 117 : 2 : BOOST_CHECK(p1 != p3);
# 118 : 2 : }
# 119 : 2 : }
# 120 : :
# 121 : : BOOST_AUTO_TEST_CASE(rename)
# 122 : 2 : {
# 123 : 2 : const fs::path tmpfolder{m_args.GetDataDirBase()};
# 124 : :
# 125 : 2 : const fs::path path1{GetUniquePath(tmpfolder)};
# 126 : 2 : const fs::path path2{GetUniquePath(tmpfolder)};
# 127 : :
# 128 : 2 : const std::string path1_contents{"1111"};
# 129 : 2 : const std::string path2_contents{"2222"};
# 130 : :
# 131 : 2 : {
# 132 : 2 : std::ofstream file{path1};
# 133 : 2 : file << path1_contents;
# 134 : 2 : }
# 135 : :
# 136 : 2 : {
# 137 : 2 : std::ofstream file{path2};
# 138 : 2 : file << path2_contents;
# 139 : 2 : }
# 140 : :
# 141 : : // Rename path1 -> path2.
# 142 : 2 : BOOST_CHECK(RenameOver(path1, path2));
# 143 : :
# 144 : 2 : BOOST_CHECK(!fs::exists(path1));
# 145 : :
# 146 : 2 : {
# 147 : 2 : std::ifstream file{path2};
# 148 : 2 : std::string contents;
# 149 : 2 : file >> contents;
# 150 : 2 : BOOST_CHECK_EQUAL(contents, path1_contents);
# 151 : 2 : }
# 152 : 2 : fs::remove(path2);
# 153 : 2 : }
# 154 : :
# 155 : : #ifndef __MINGW64__ // no symlinks on mingw
# 156 : : BOOST_AUTO_TEST_CASE(create_directories)
# 157 : 2 : {
# 158 : : // Test fs::create_directories workaround.
# 159 : 2 : const fs::path tmpfolder{m_args.GetDataDirBase()};
# 160 : :
# 161 : 2 : const fs::path dir{GetUniquePath(tmpfolder)};
# 162 : 2 : fs::create_directory(dir);
# 163 : 2 : BOOST_CHECK(fs::exists(dir));
# 164 : 2 : BOOST_CHECK(fs::is_directory(dir));
# 165 : 2 : BOOST_CHECK(!fs::create_directories(dir));
# 166 : :
# 167 : 2 : const fs::path symlink{GetUniquePath(tmpfolder)};
# 168 : 2 : fs::create_directory_symlink(dir, symlink);
# 169 : 2 : BOOST_CHECK(fs::exists(symlink));
# 170 : 2 : BOOST_CHECK(fs::is_symlink(symlink));
# 171 : 2 : BOOST_CHECK(fs::is_directory(symlink));
# 172 : 2 : BOOST_CHECK(!fs::create_directories(symlink));
# 173 : :
# 174 : 2 : fs::remove(symlink);
# 175 : 2 : fs::remove(dir);
# 176 : 2 : }
# 177 : : #endif // __MINGW64__
# 178 : :
# 179 : : BOOST_AUTO_TEST_SUITE_END()
|