OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, Google Inc. |
| 2 // All rights reserved. |
| 3 // |
| 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are |
| 6 // met: |
| 7 // |
| 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above |
| 11 // copyright notice, this list of conditions and the following disclaimer |
| 12 // in the documentation and/or other materials provided with the |
| 13 // distribution. |
| 14 // * Neither the name of Google Inc. nor the names of its |
| 15 // contributors may be used to endorse or promote products derived from |
| 16 // this software without specific prior written permission. |
| 17 // |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 // memory_mapped_file_unittest.cc: |
| 31 // Unit tests for google_breakpad::MemoryMappedFile. |
| 32 |
| 33 #include <fcntl.h> |
| 34 #include <string.h> |
| 35 #include <unistd.h> |
| 36 |
| 37 #include <string> |
| 38 |
| 39 #include "breakpad_googletest_includes.h" |
| 40 #include "common/linux/eintr_wrapper.h" |
| 41 #include "common/linux/memory_mapped_file.h" |
| 42 #include "common/tests/auto_tempdir.h" |
| 43 |
| 44 using google_breakpad::AutoTempDir; |
| 45 using google_breakpad::MemoryMappedFile; |
| 46 using std::string; |
| 47 |
| 48 namespace { |
| 49 |
| 50 bool WriteFile(const string& path, const void* buffer, size_t buffer_size) { |
| 51 int fd = |
| 52 HANDLE_EINTR(open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, S_IRWXU)); |
| 53 if (fd == -1) { |
| 54 perror("open"); |
| 55 return false; |
| 56 } |
| 57 |
| 58 bool ok = true; |
| 59 if (buffer && buffer_size > 0) { |
| 60 if (HANDLE_EINTR(write(fd, buffer, buffer_size) != buffer_size)) { |
| 61 perror("write"); |
| 62 ok = false; |
| 63 } |
| 64 } |
| 65 close(fd); |
| 66 return ok; |
| 67 } |
| 68 |
| 69 class MemoryMappedFileTest : public testing::Test { |
| 70 protected: |
| 71 void ExpectNoMappedData(const MemoryMappedFile& mapped_file) { |
| 72 EXPECT_TRUE(mapped_file.content().IsEmpty()); |
| 73 EXPECT_TRUE(mapped_file.data() == NULL); |
| 74 EXPECT_EQ(0, mapped_file.size()); |
| 75 } |
| 76 }; |
| 77 |
| 78 } // namespace |
| 79 |
| 80 TEST_F(MemoryMappedFileTest, DefaultConstructor) { |
| 81 MemoryMappedFile mapped_file; |
| 82 ExpectNoMappedData(mapped_file); |
| 83 } |
| 84 |
| 85 TEST_F(MemoryMappedFileTest, UnmapWithoutMap) { |
| 86 MemoryMappedFile mapped_file; |
| 87 mapped_file.Unmap(); |
| 88 } |
| 89 |
| 90 TEST_F(MemoryMappedFileTest, MapNonexistentFile) { |
| 91 { |
| 92 MemoryMappedFile mapped_file("nonexistent-file"); |
| 93 ExpectNoMappedData(mapped_file); |
| 94 } |
| 95 { |
| 96 MemoryMappedFile mapped_file; |
| 97 EXPECT_FALSE(mapped_file.Map("nonexistent-file")); |
| 98 ExpectNoMappedData(mapped_file); |
| 99 } |
| 100 } |
| 101 |
| 102 TEST_F(MemoryMappedFileTest, MapEmptyFile) { |
| 103 AutoTempDir temp_dir; |
| 104 string test_file = temp_dir.path() + "/empty_file"; |
| 105 ASSERT_TRUE(WriteFile(test_file, NULL, 0)); |
| 106 |
| 107 { |
| 108 MemoryMappedFile mapped_file(test_file.c_str()); |
| 109 ExpectNoMappedData(mapped_file); |
| 110 } |
| 111 { |
| 112 MemoryMappedFile mapped_file; |
| 113 EXPECT_TRUE(mapped_file.Map(test_file.c_str())); |
| 114 ExpectNoMappedData(mapped_file); |
| 115 } |
| 116 } |
| 117 |
| 118 TEST_F(MemoryMappedFileTest, MapNonEmptyFile) { |
| 119 char data[256]; |
| 120 for (size_t i = 0; i < sizeof(data); ++i) { |
| 121 data[i] = i; |
| 122 } |
| 123 size_t data_size = sizeof(data); |
| 124 |
| 125 AutoTempDir temp_dir; |
| 126 string test_file = temp_dir.path() + "/test_file"; |
| 127 ASSERT_TRUE(WriteFile(test_file, data, data_size)); |
| 128 |
| 129 { |
| 130 MemoryMappedFile mapped_file(test_file.c_str()); |
| 131 EXPECT_FALSE(mapped_file.content().IsEmpty()); |
| 132 EXPECT_TRUE(mapped_file.data() != NULL); |
| 133 EXPECT_EQ(data_size, mapped_file.size()); |
| 134 EXPECT_EQ(0, memcmp(data, mapped_file.data(), data_size)); |
| 135 } |
| 136 { |
| 137 MemoryMappedFile mapped_file; |
| 138 EXPECT_TRUE(mapped_file.Map(test_file.c_str())); |
| 139 EXPECT_FALSE(mapped_file.content().IsEmpty()); |
| 140 EXPECT_TRUE(mapped_file.data() != NULL); |
| 141 EXPECT_EQ(data_size, mapped_file.size()); |
| 142 EXPECT_EQ(0, memcmp(data, mapped_file.data(), data_size)); |
| 143 } |
| 144 } |
OLD | NEW |