summaryrefslogtreecommitdiff
path: root/src/mongo/db/storage/storage_engine_lock_file_test.cpp
diff options
context:
space:
mode:
authorAdam Midvidy <amidvidy@gmail.com>2016-02-16 13:04:11 -0500
committerAdam Midvidy <amidvidy@gmail.com>2016-02-16 13:04:11 -0500
commit1c7404095583a712d9e946e2751572c8973f13aa (patch)
treee2f6a4c0580864e755b35800e7da5e8152e63b85 /src/mongo/db/storage/storage_engine_lock_file_test.cpp
parent22c684d6f89c01914c53441abd95d6913831cc96 (diff)
downloadmongo-1c7404095583a712d9e946e2751572c8973f13aa.tar.gz
SERVER-22352 lockFile changes for readOnly mode
Diffstat (limited to 'src/mongo/db/storage/storage_engine_lock_file_test.cpp')
-rw-r--r--src/mongo/db/storage/storage_engine_lock_file_test.cpp99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/mongo/db/storage/storage_engine_lock_file_test.cpp b/src/mongo/db/storage/storage_engine_lock_file_test.cpp
index 9312b7f7c3a..ee77676291a 100644
--- a/src/mongo/db/storage/storage_engine_lock_file_test.cpp
+++ b/src/mongo/db/storage/storage_engine_lock_file_test.cpp
@@ -37,6 +37,12 @@
#include "mongo/unittest/temp_dir.h"
#include "mongo/unittest/unittest.h"
+#ifndef _WIN32
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#endif
+
namespace {
using std::string;
@@ -169,4 +175,97 @@ TEST(StorageEngineLockFileTest, ClearPidAndUnlock) {
ASSERT_EQUALS(0U, boost::filesystem::file_size(lockFile.getFilespec()));
}
+class ScopedReadOnlyDirectory {
+public:
+ ScopedReadOnlyDirectory(const std::string& path) : _path(std::move(path)) {
+ _applyToPathRecursive(_path, makePathReadOnly);
+ }
+
+ ~ScopedReadOnlyDirectory() {
+ _applyToPathRecursive(_path, makePathWritable);
+ }
+
+private:
+ const std::string& _path;
+
+ static void makePathReadOnly(const boost::filesystem::path& path) {
+#ifdef _WIN32
+ ::SetFileAttributes(path.c_str(), FILE_ATTRIBUTE_READONLY);
+#else
+ ::chmod(path.c_str(), 0544);
+#endif
+ }
+
+ static void makePathWritable(const boost::filesystem::path& path) {
+#ifdef _WIN32
+ ::SetFileAttributes(path.c_str(), FILE_ATTRIBUTE_NORMAL);
+#else
+ ::chmod(path.c_str(), 0777);
+#endif
+ }
+
+ template <typename Func>
+ static void _applyToPathRecursive(const boost::filesystem::path& path, Func func) {
+ func(path);
+
+ using rdi = boost::filesystem::recursive_directory_iterator;
+ for (auto iter = rdi{path}; iter != rdi(); ++iter) {
+ func(*iter);
+ }
+ }
+};
+
+#ifndef _WIN32
+
+// Windows has no concept of read only directories - only read only files.
+TEST(StorageEngineLockFileTest, ReadOnlyDirectory) {
+ // If we are running as root, do not run this test as read only permissions will not be
+ // respected.
+ if (::getuid() == 0) {
+ return;
+ }
+
+ TempDir tempDir("StorageEngineLockFileTest_ReadOnlyDirectory");
+
+ // Make tempDir read-only.
+ ScopedReadOnlyDirectory srod(tempDir.path());
+
+ StorageEngineLockFile lockFile(tempDir.path());
+
+ auto openStatus = lockFile.open();
+
+ ASSERT_NOT_OK(openStatus);
+ ASSERT_EQ(openStatus, ErrorCodes::IllegalOperation);
+}
+
+#endif
+
+TEST(StorageEngineLockFileTest, ReadOnlyDirectoryWithLockFile) {
+#ifndef _WIN32
+ // If we are running as root, do not run this test as read only permissions will not be
+ // respected.
+ if (::getuid() == 0) {
+ return;
+ }
+#endif
+
+ TempDir tempDir("StorageEngineLockFileTest_ReadOnlyDirectoryWithLockFile");
+
+
+ StorageEngineLockFile lockFile(tempDir.path());
+ ASSERT_OK(lockFile.open());
+ ASSERT_OK(lockFile.writePid());
+
+ // Make tempDir read-only.
+ ScopedReadOnlyDirectory srod(tempDir.path());
+
+ // Try to create a new lock file.
+ StorageEngineLockFile lockFile2(tempDir.path());
+
+ auto openStatus = lockFile2.open();
+
+ ASSERT_NOT_OK(openStatus);
+ ASSERT_EQ(openStatus, ErrorCodes::IllegalOperation);
+}
+
} // namespace