diff --git a/CMakeLists.txt b/CMakeLists.txt index 23b1bc4c4..98b390d52 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -602,6 +602,7 @@ if(WITH_TESTS) compat_test dwarfs_test entry_test + file_access_test incompressible_categorizer_test metadata_requirements_test pcm_sample_transformer_test diff --git a/test/file_access_test.cpp b/test/file_access_test.cpp new file mode 100644 index 000000000..e131077f2 --- /dev/null +++ b/test/file_access_test.cpp @@ -0,0 +1,120 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/** + * \author Marcus Holland-Moritz (github@mhxnet.de) + * \copyright Copyright (c) Marcus Holland-Moritz + * + * This file is part of dwarfs. + * + * dwarfs 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. + * + * dwarfs 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 dwarfs. If not, see . + */ + +#include + +#include +#include + +#include + +#include "dwarfs/file_access.h" +#include "dwarfs/file_access_generic.h" +#include "test_helpers.h" + +using namespace dwarfs; +namespace fs = std::filesystem; + +TEST(file_access_generic_test, basic) { + folly::test::TemporaryDirectory tempdir("dwarfs"); + auto td = fs::path(tempdir.path().string()); + + auto text_file = td / "test.txt"; + auto binary_file = td / "test.bin"; + + auto fa = create_file_access_generic(); + + { + auto text_os = fa->open_output(text_file); + text_os->os() << "line1" << std::endl; + text_os->os() << "line2" << std::endl; + text_os->close(); + } + + auto binary_data = test::create_random_string(4096); + + { + auto binary_os = fa->open_output_binary(binary_file); + binary_os->os().write(binary_data.data(), binary_data.size()); + binary_os->close(); + } + + EXPECT_TRUE(fa->exists(text_file)); + EXPECT_TRUE(fa->exists(binary_file)); + + EXPECT_TRUE(std::filesystem::exists(text_file)); + EXPECT_TRUE(std::filesystem::exists(binary_file)); + + EXPECT_FALSE(fa->exists(td / "nonexistent")); + + auto binary_size = std::filesystem::file_size(binary_file); + + { + auto text_is = fa->open_input(text_file); + std::string line; + std::getline(text_is->is(), line); + EXPECT_EQ(line, "line1"); + std::getline(text_is->is(), line); + EXPECT_EQ(line, "line2"); + text_is->close(); + } + + { + auto binary_is = fa->open_input_binary(binary_file); + std::string data; + data.resize(binary_size); + binary_is->is().read(data.data(), data.size()); + EXPECT_EQ(data, binary_data); + binary_is->close(); + } +} + +TEST(file_access_generic_test, error_handling) { + folly::test::TemporaryDirectory tempdir("dwarfs"); + auto td = fs::path(tempdir.path().string()); + + auto nonexistent_file = td / "nonexistent"; + auto file_in_subdir = td / "subdir" / "test.txt"; + + auto fa = create_file_access_generic(); + + { + auto matcher = testing::Throws(testing::Property( + &std::system_error::code, + testing::Property(&std::error_code::value, + testing::Eq(static_cast( + std::errc::no_such_file_or_directory))))); + + EXPECT_THAT([&] { fa->open_input(nonexistent_file); }, matcher); + EXPECT_THAT([&] { fa->open_input_binary(nonexistent_file); }, matcher); + } + + { + auto matcher = testing::Throws(testing::Property( + &std::system_error::code, + testing::Property(&std::error_code::value, + testing::Eq(static_cast( + std::errc::no_such_file_or_directory))))); + + EXPECT_THAT([&] { fa->open_output(file_in_subdir); }, matcher); + EXPECT_THAT([&] { fa->open_output_binary(file_in_subdir); }, matcher); + } +}