|
7 | 7 |
|
8 | 8 | #include <userver/fs/blocking/temp_directory.hpp> |
9 | 9 | #include <userver/fs/blocking/write.hpp> |
| 10 | +#include <userver/logging/log.hpp> |
| 11 | +#include <userver/utest/log_capture_fixture.hpp> |
10 | 12 |
|
11 | 13 | USERVER_NAMESPACE_BEGIN |
12 | 14 |
|
13 | 15 | namespace { |
| 16 | +using FsBlockingTempFileWithLog = utest::LogCaptureFixture<>; |
14 | 17 |
|
15 | 18 | bool StartsWith(std::string_view hay, std::string_view needle) { return hay.substr(0, needle.size()) == needle; } |
16 | 19 |
|
@@ -75,4 +78,59 @@ TEST(TempFile, CustomPath) { |
75 | 78 | EXPECT_EQ(boost::filesystem::status(root + "/foo").permissions(), boost::filesystem::perms::owner_all); |
76 | 79 | } |
77 | 80 |
|
| 81 | +TEST_F(FsBlockingTempFileWithLog, BlockingTempFileDestructorWithException) { |
| 82 | + std::string parent_dir; |
| 83 | + { |
| 84 | + const auto parent = fs::blocking::TempDirectory::Create(); |
| 85 | + parent_dir = parent.GetPath(); |
| 86 | + |
| 87 | + { |
| 88 | + // Create temp file in temporaty directory |
| 89 | + const auto file = fs::blocking::TempFile::Create(parent_dir, "BlockingTempFileDestructorWithException"); |
| 90 | + |
| 91 | + const auto dir_status = boost::filesystem::status(parent_dir); |
| 92 | + const auto original_perms = dir_status.permissions(); |
| 93 | + |
| 94 | + LOG_DEBUG() << "Original directory permissions: " << static_cast<int>(original_perms); |
| 95 | + |
| 96 | + // Check that the directory has write permissions |
| 97 | + ASSERT_TRUE((original_perms & boost::filesystem::perms::owner_write) != boost::filesystem::perms::no_perms); |
| 98 | + |
| 99 | + // Change permissions for the directory |
| 100 | + boost::filesystem::permissions( |
| 101 | + parent_dir, |
| 102 | + boost::filesystem::perms::owner_read | boost::filesystem::perms::owner_exe | |
| 103 | + boost::filesystem::perms::group_read | boost::filesystem::perms::group_exe | |
| 104 | + boost::filesystem::perms::others_read | boost::filesystem::perms::others_exe |
| 105 | + ); |
| 106 | + LOG_DEBUG() << "Changed directory permissions, write is now forbidden"; |
| 107 | + |
| 108 | + // Check that the file can not be removed |
| 109 | + try { |
| 110 | + boost::filesystem::remove(file.GetPath()); |
| 111 | + FAIL() << "Expected exception when removing file"; |
| 112 | + } catch (const std::exception& ex) { |
| 113 | + LOG_DEBUG() << "Confirmed file cannot be removed"; |
| 114 | + } |
| 115 | + |
| 116 | + // We don't expect that ~TempFile to throw an exception |
| 117 | + } |
| 118 | + |
| 119 | + // Check that log with error from ~TempFile exists |
| 120 | + EXPECT_EQ(GetLogCapture().Filter("fs::blocking::~TempFile failed with exception:").size(), 1); |
| 121 | + |
| 122 | + // Rollback directory permissions |
| 123 | + boost::filesystem::permissions( |
| 124 | + parent_dir, |
| 125 | + boost::filesystem::perms::owner_read | boost::filesystem::perms::owner_write | |
| 126 | + boost::filesystem::perms::owner_exe | boost::filesystem::perms::group_read | |
| 127 | + boost::filesystem::perms::group_write | boost::filesystem::perms::group_exe | |
| 128 | + boost::filesystem::perms::others_read | boost::filesystem::perms::others_write | |
| 129 | + boost::filesystem::perms::others_exe |
| 130 | + ); |
| 131 | + LOG_DEBUG() << "Changed directory permissions, write is now available"; |
| 132 | + } |
| 133 | + ASSERT_FALSE(boost::filesystem::exists(parent_dir)); |
| 134 | +} |
| 135 | + |
78 | 136 | USERVER_NAMESPACE_END |
0 commit comments