2021-06-25 13:28:54 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
|
|
|
|
|
|
|
/// Unit tests for the CommonIO routines.
|
|
|
|
|
|
|
|
#include <libsolutil/CommonIO.h>
|
2022-05-15 15:45:43 +00:00
|
|
|
#include <libsolutil/TemporaryDirectory.h>
|
2021-06-25 13:28:54 +00:00
|
|
|
|
|
|
|
#include <test/Common.h>
|
|
|
|
#include <test/FilesystemUtils.h>
|
|
|
|
#include <test/libsolidity/util/SoltestErrors.h>
|
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace solidity::test;
|
|
|
|
|
2021-07-21 13:10:51 +00:00
|
|
|
#define TEST_CASE_NAME (boost::unit_test::framework::current_test_case().p_name)
|
|
|
|
|
2021-06-25 13:28:54 +00:00
|
|
|
namespace solidity::util::test
|
|
|
|
{
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(CommonIOTest)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(readFileAsString_regular_file)
|
|
|
|
{
|
2021-07-21 13:10:51 +00:00
|
|
|
TemporaryDirectory tempDir(TEST_CASE_NAME);
|
2021-06-25 13:28:54 +00:00
|
|
|
createFileWithContent(tempDir.path() / "test.txt", "ABC\ndef\n");
|
|
|
|
|
2021-07-13 07:12:04 +00:00
|
|
|
BOOST_TEST(readFileAsString(tempDir.path() / "test.txt") == "ABC\ndef\n");
|
2021-06-25 13:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(readFileAsString_directory)
|
|
|
|
{
|
2021-07-21 13:10:51 +00:00
|
|
|
TemporaryDirectory tempDir(TEST_CASE_NAME);
|
2021-07-22 14:04:45 +00:00
|
|
|
BOOST_CHECK_THROW(readFileAsString(tempDir), NotAFile);
|
2021-06-25 13:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(readFileAsString_symlink)
|
|
|
|
{
|
2021-07-21 13:10:51 +00:00
|
|
|
TemporaryDirectory tempDir(TEST_CASE_NAME);
|
2021-06-25 13:28:54 +00:00
|
|
|
createFileWithContent(tempDir.path() / "test.txt", "ABC\ndef\n");
|
|
|
|
|
2021-07-02 22:02:58 +00:00
|
|
|
if (!createSymlinkIfSupportedByFilesystem("test.txt", tempDir.path() / "symlink.txt", false))
|
2021-06-25 13:28:54 +00:00
|
|
|
return;
|
|
|
|
|
2021-07-13 07:12:04 +00:00
|
|
|
BOOST_TEST(readFileAsString(tempDir.path() / "symlink.txt") == "ABC\ndef\n");
|
2021-06-25 13:28:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-03 17:30:44 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(readUntilEnd_no_ending_newline)
|
|
|
|
{
|
|
|
|
istringstream inputStream("ABC\ndef");
|
|
|
|
BOOST_TEST(readUntilEnd(inputStream) == "ABC\ndef");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(readUntilEnd_with_ending_newline)
|
|
|
|
{
|
|
|
|
istringstream inputStream("ABC\ndef\n");
|
|
|
|
BOOST_TEST(readUntilEnd(inputStream) == "ABC\ndef\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(readUntilEnd_cr_lf_newline)
|
|
|
|
{
|
|
|
|
istringstream inputStream("ABC\r\ndef");
|
|
|
|
BOOST_TEST(readUntilEnd(inputStream) == "ABC\r\ndef");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(readUntilEnd_empty)
|
|
|
|
{
|
|
|
|
istringstream inputStream("");
|
|
|
|
BOOST_TEST(readUntilEnd(inputStream) == "");
|
|
|
|
}
|
|
|
|
|
2021-12-01 11:31:21 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(readBytes_past_end)
|
|
|
|
{
|
|
|
|
istringstream inputStream("abc");
|
|
|
|
BOOST_CHECK_EQUAL(readBytes(inputStream, 0), "");
|
|
|
|
BOOST_CHECK_EQUAL(readBytes(inputStream, 1), "a");
|
|
|
|
BOOST_CHECK_EQUAL(readBytes(inputStream, 20), "bc");
|
|
|
|
BOOST_CHECK_EQUAL(readBytes(inputStream, 20), "");
|
|
|
|
}
|
|
|
|
|
2021-06-25 13:28:54 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
} // namespace solidity::util::test
|