diff options
author | Nick Kledzik <kledzik@apple.com> | 2012-06-20 00:28:54 +0000 |
---|---|---|
committer | Nick Kledzik <kledzik@apple.com> | 2012-06-20 00:28:54 +0000 |
commit | 18497e924206a00e102f385eee4a3e99f866d473 (patch) | |
tree | 384423341acdc1ffd69b2c932fb6c4e456f72834 /llvm/unittests/Support | |
parent | 2212f807c7774aa168e9242300b420beb1915650 (diff) | |
download | bcm5719-llvm-18497e924206a00e102f385eee4a3e99f866d473.tar.gz bcm5719-llvm-18497e924206a00e102f385eee4a3e99f866d473.zip |
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.
llvm-svn: 158770
Diffstat (limited to 'llvm/unittests/Support')
-rw-r--r-- | llvm/unittests/Support/Path.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp index 358dad0f838..766c990fcde 100644 --- a/llvm/unittests/Support/Path.cpp +++ b/llvm/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 |