summaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
authorNick Kledzik <kledzik@apple.com>2012-06-20 00:28:54 +0000
committerNick Kledzik <kledzik@apple.com>2012-06-20 00:28:54 +0000
commitca077ec5ea41f4537ae58e141781be622d09c786 (patch)
tree2dc2a2955b3f6c9b950d0fc907d99a18a3375b5b /unittests
parent44b2c82871fad5407930cdf0b90c578fa4e60a3a (diff)
downloadllvm-ca077ec5ea41f4537ae58e141781be622d09c786.tar.gz
llvm-ca077ec5ea41f4537ae58e141781be622d09c786.tar.bz2
llvm-ca077ec5ea41f4537ae58e141781be622d09c786.tar.xz
Add permissions(), map_file_pages(), and unmap_file_pages() to llvm::sys::fs and add unit test. Unix is implemented. Windows side needs to be implemented.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@158770 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r--unittests/Support/Path.cpp65
1 files changed, 65 insertions, 0 deletions
diff --git a/unittests/Support/Path.cpp b/unittests/Support/Path.cpp
index 358dad0f83..766c990fcd 100644
--- a/unittests/Support/Path.cpp
+++ b/unittests/Support/Path.cpp
@@ -312,4 +312,69 @@ TEST_F(FileSystemTest, Magic) {
}
}
+
+TEST_F(FileSystemTest, Permissions) {
+ // Create a temp file.
+ int FileDescriptor;
+ SmallString<64> TempPath;
+ ASSERT_NO_ERROR(
+ fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
+
+ // Mark file as read-only
+ const fs::perms AllWrite = fs::owner_write|fs::group_write|fs::others_write;
+ ASSERT_NO_ERROR(fs::permissions(Twine(TempPath), fs::remove_perms|AllWrite));
+
+ // Verify file is read-only
+ fs::file_status Status;
+ ASSERT_NO_ERROR(fs::status(Twine(TempPath), Status));
+ bool AnyWriteBits = (Status.permissions() & AllWrite);
+ EXPECT_FALSE(AnyWriteBits);
+
+ // Mark file as read-write
+ ASSERT_NO_ERROR(fs::permissions(Twine(TempPath), fs::add_perms|AllWrite));
+
+ // Verify file is read-write
+ ASSERT_NO_ERROR(fs::status(Twine(TempPath), Status));
+ AnyWriteBits = (Status.permissions() & AllWrite);
+ EXPECT_TRUE(AnyWriteBits);
+}
+
+TEST_F(FileSystemTest, FileMapping) {
+ // Create a temp file.
+ int FileDescriptor;
+ SmallString<64> TempPath;
+ ASSERT_NO_ERROR(
+ fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
+
+ // Grow temp file to be 4096 bytes
+ ASSERT_NO_ERROR(sys::fs::resize_file(Twine(TempPath), 4096));
+
+ // Map in temp file and add some content
+ void* MappedMemory;
+ ASSERT_NO_ERROR(fs::map_file_pages(Twine(TempPath), 0, 4096,
+ true /*writable*/, MappedMemory));
+ char* Memory = reinterpret_cast<char*>(MappedMemory);
+ strcpy(Memory, "hello there");
+
+ // Unmap temp file
+ ASSERT_NO_ERROR(fs::unmap_file_pages(MappedMemory, 4096));
+ MappedMemory = NULL;
+ Memory = NULL;
+
+ // Map it back in read-only
+ ASSERT_NO_ERROR(fs::map_file_pages(Twine(TempPath), 0, 4096,
+ false /*read-only*/, MappedMemory));
+
+ // Verify content
+ Memory = reinterpret_cast<char*>(MappedMemory);
+ bool SAME = (strcmp(Memory, "hello there") == 0);
+ EXPECT_TRUE(SAME);
+
+ // Unmap temp file
+ ASSERT_NO_ERROR(fs::unmap_file_pages(MappedMemory, 4096));
+ MappedMemory = NULL;
+ Memory = NULL;
+}
+
+
} // anonymous namespace