2019-04-11 09:53:04 -04:00
|
|
|
// Copyright (c) 2011-2019 The Bitcoin Core developers
|
2018-09-07 10:12:24 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
//
|
|
|
|
#include <fs.h>
|
2019-11-05 17:18:59 -03:00
|
|
|
#include <test/util/setup_common.h>
|
2019-06-24 11:22:28 -04:00
|
|
|
#include <util/system.h>
|
2021-02-01 09:35:28 -03:00
|
|
|
#include <util/getuniquepath.h>
|
2018-09-07 10:12:24 -03:00
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(fs_tests, BasicTestingSetup)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(fsbridge_fstream)
|
|
|
|
{
|
2019-06-19 17:52:35 -04:00
|
|
|
fs::path tmpfolder = GetDataDir();
|
2018-09-07 10:12:24 -03:00
|
|
|
// tmpfile1 should be the same as tmpfile2
|
|
|
|
fs::path tmpfile1 = tmpfolder / "fs_tests_₿_🏃";
|
2019-10-09 11:09:43 -03:00
|
|
|
fs::path tmpfile2 = tmpfolder / "fs_tests_₿_🏃";
|
2018-09-07 10:12:24 -03:00
|
|
|
{
|
|
|
|
fsbridge::ofstream file(tmpfile1);
|
|
|
|
file << "bitcoin";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
fsbridge::ifstream file(tmpfile2);
|
|
|
|
std::string input_buffer;
|
|
|
|
file >> input_buffer;
|
|
|
|
BOOST_CHECK_EQUAL(input_buffer, "bitcoin");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
fsbridge::ifstream file(tmpfile1, std::ios_base::in | std::ios_base::ate);
|
|
|
|
std::string input_buffer;
|
|
|
|
file >> input_buffer;
|
|
|
|
BOOST_CHECK_EQUAL(input_buffer, "");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
fsbridge::ofstream file(tmpfile2, std::ios_base::out | std::ios_base::app);
|
|
|
|
file << "tests";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
fsbridge::ifstream file(tmpfile1);
|
|
|
|
std::string input_buffer;
|
|
|
|
file >> input_buffer;
|
|
|
|
BOOST_CHECK_EQUAL(input_buffer, "bitcointests");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
fsbridge::ofstream file(tmpfile2, std::ios_base::out | std::ios_base::trunc);
|
|
|
|
file << "bitcoin";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
fsbridge::ifstream file(tmpfile1);
|
|
|
|
std::string input_buffer;
|
|
|
|
file >> input_buffer;
|
|
|
|
BOOST_CHECK_EQUAL(input_buffer, "bitcoin");
|
|
|
|
}
|
2021-01-13 11:44:40 -03:00
|
|
|
{
|
|
|
|
// Join an absolute path and a relative path.
|
|
|
|
fs::path p = fsbridge::AbsPathJoin(tmpfolder, "fs_tests_₿_🏃");
|
|
|
|
BOOST_CHECK(p.is_absolute());
|
|
|
|
BOOST_CHECK_EQUAL(tmpfile1, p);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// Join two absolute paths.
|
|
|
|
fs::path p = fsbridge::AbsPathJoin(tmpfile1, tmpfile2);
|
|
|
|
BOOST_CHECK(p.is_absolute());
|
|
|
|
BOOST_CHECK_EQUAL(tmpfile2, p);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// Ensure joining with empty paths does not add trailing path components.
|
|
|
|
BOOST_CHECK_EQUAL(tmpfile1, fsbridge::AbsPathJoin(tmpfile1, ""));
|
|
|
|
BOOST_CHECK_EQUAL(tmpfile1, fsbridge::AbsPathJoin(tmpfile1, {}));
|
|
|
|
}
|
2021-02-01 09:35:28 -03:00
|
|
|
{
|
|
|
|
fs::path p1 = GetUniquePath(tmpfolder);
|
|
|
|
fs::path p2 = GetUniquePath(tmpfolder);
|
|
|
|
fs::path p3 = GetUniquePath(tmpfolder);
|
|
|
|
|
|
|
|
// Ensure that the parent path is always the same.
|
|
|
|
BOOST_CHECK_EQUAL(tmpfolder, p1.parent_path());
|
|
|
|
BOOST_CHECK_EQUAL(tmpfolder, p2.parent_path());
|
|
|
|
BOOST_CHECK_EQUAL(tmpfolder, p3.parent_path());
|
|
|
|
|
|
|
|
// Ensure that generated paths are actually different.
|
|
|
|
BOOST_CHECK(p1 != p2);
|
|
|
|
BOOST_CHECK(p2 != p3);
|
|
|
|
BOOST_CHECK(p1 != p3);
|
|
|
|
}
|
2018-09-07 10:12:24 -03:00
|
|
|
}
|
|
|
|
|
2021-02-01 09:35:28 -03:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|