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