TestLibCoreMappedFile.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  3. * Copyright (c) 2023, kleines Filmröllchen <filmroellchen@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Format.h>
  8. #include <AK/MaybeOwned.h>
  9. #include <AK/String.h>
  10. #include <LibCore/File.h>
  11. #include <LibCore/MappedFile.h>
  12. #include <LibTest/TestCase.h>
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. TEST_CASE(mapped_file_open)
  16. {
  17. // Fill the file with a little content so we can write to it.
  18. constexpr auto text = "Here's some text to be mmapped."sv;
  19. {
  20. auto maybe_file = Core::File::open("/tmp/file-open-test.txt"sv, Core::File::OpenMode::Write);
  21. if (maybe_file.is_error()) {
  22. warnln("Failed to open the file: {}", strerror(maybe_file.error().code()));
  23. VERIFY_NOT_REACHED();
  24. }
  25. TRY_OR_FAIL(maybe_file.value()->write_until_depleted(text.bytes()));
  26. }
  27. auto maybe_file = Core::MappedFile::map("/tmp/file-open-test.txt"sv, Core::MappedFile::Mode::ReadWrite);
  28. if (maybe_file.is_error()) {
  29. warnln("Failed to open the file: {}", strerror(maybe_file.error().code()));
  30. VERIFY_NOT_REACHED();
  31. }
  32. // Testing out some basic file properties.
  33. auto file = maybe_file.release_value();
  34. EXPECT(file->is_open());
  35. EXPECT(!file->is_eof());
  36. auto size = TRY_OR_FAIL(file->size());
  37. EXPECT_EQ(size, text.length());
  38. }
  39. constexpr auto expected_buffer_contents = "&lt;small&gt;(Please consider translating this message for the benefit of your fellow Wikimedians. Please also consider translating"sv;
  40. TEST_CASE(mapped_file_read_bytes)
  41. {
  42. auto file = TRY_OR_FAIL(Core::MappedFile::map("/usr/Tests/LibCore/long_lines.txt"sv, Core::MappedFile::Mode::ReadOnly));
  43. auto buffer = TRY_OR_FAIL(ByteBuffer::create_uninitialized(131));
  44. auto result = file->read_some(buffer);
  45. EXPECT_EQ(TRY_OR_FAIL(result).size(), 131ul);
  46. StringView buffer_contents { buffer.bytes() };
  47. EXPECT_EQ(buffer_contents, expected_buffer_contents);
  48. }
  49. constexpr auto expected_seek_contents1 = "|Lleer esti mens"sv;
  50. constexpr auto expected_seek_contents2 = "s of advanced ad"sv;
  51. constexpr auto expected_seek_contents3 = "levels of advanc"sv;
  52. TEST_CASE(mapped_file_seeking_around)
  53. {
  54. auto file = TRY_OR_FAIL(Core::MappedFile::map("/usr/Tests/LibCore/long_lines.txt"sv, Core::MappedFile::Mode::ReadOnly));
  55. EXPECT_EQ(file->size().release_value(), 8702ul);
  56. auto buffer = TRY_OR_FAIL(ByteBuffer::create_uninitialized(16));
  57. StringView buffer_contents { buffer.bytes() };
  58. TRY_OR_FAIL(file->seek(500, SeekMode::SetPosition));
  59. EXPECT_EQ(file->tell().release_value(), 500ul);
  60. TRY_OR_FAIL(file->read_until_filled(buffer));
  61. EXPECT_EQ(buffer_contents, expected_seek_contents1);
  62. TRY_OR_FAIL(file->seek(234, SeekMode::FromCurrentPosition));
  63. EXPECT_EQ(file->tell().release_value(), 750ul);
  64. TRY_OR_FAIL(file->read_until_filled(buffer));
  65. EXPECT_EQ(buffer_contents, expected_seek_contents2);
  66. TRY_OR_FAIL(file->seek(-105, SeekMode::FromEndPosition));
  67. EXPECT_EQ(file->tell().release_value(), 8597ul);
  68. TRY_OR_FAIL(file->read_until_filled(buffer));
  69. EXPECT_EQ(buffer_contents, expected_seek_contents3);
  70. }
  71. BENCHMARK_CASE(file_tell)
  72. {
  73. auto file = TRY_OR_FAIL(Core::MappedFile::map("/usr/Tests/LibCore/10kb.txt"sv, Core::MappedFile::Mode::ReadOnly));
  74. auto expected_file_offset = 0u;
  75. auto ten_byte_buffer = TRY_OR_FAIL(ByteBuffer::create_uninitialized(1));
  76. for (auto i = 0u; i < 4000; ++i) {
  77. TRY_OR_FAIL(file->read_until_filled(ten_byte_buffer));
  78. expected_file_offset += 1u;
  79. EXPECT_EQ(expected_file_offset, TRY_OR_FAIL(file->tell()));
  80. }
  81. for (auto i = 0u; i < 4000; ++i) {
  82. auto seek_file_offset = TRY_OR_FAIL(file->seek(-1, SeekMode::FromCurrentPosition));
  83. expected_file_offset -= 1;
  84. EXPECT_EQ(seek_file_offset, TRY_OR_FAIL(file->tell()));
  85. EXPECT_EQ(expected_file_offset, TRY_OR_FAIL(file->tell()));
  86. }
  87. }
  88. TEST_CASE(mapped_file_adopt_fd)
  89. {
  90. int rc = ::open("/usr/Tests/LibCore/long_lines.txt", O_RDONLY);
  91. EXPECT(rc >= 0);
  92. auto file = TRY_OR_FAIL(Core::MappedFile::map_from_fd_and_close(rc, "/usr/Tests/LibCore/long_lines.txt"sv));
  93. EXPECT_EQ(file->size().release_value(), 8702ul);
  94. auto buffer = TRY_OR_FAIL(ByteBuffer::create_uninitialized(16));
  95. StringView buffer_contents { buffer.bytes() };
  96. TRY_OR_FAIL(file->seek(500, SeekMode::SetPosition));
  97. EXPECT_EQ(file->tell().release_value(), 500ul);
  98. TRY_OR_FAIL(file->read_until_filled(buffer));
  99. EXPECT_EQ(buffer_contents, expected_seek_contents1);
  100. // A single seek & read test should be fine for now.
  101. }
  102. TEST_CASE(mapped_file_adopt_invalid_fd)
  103. {
  104. auto maybe_file = Core::MappedFile::map_from_fd_and_close(-1, "/usr/Tests/LibCore/long_lines.txt"sv);
  105. EXPECT(maybe_file.is_error());
  106. EXPECT_EQ(maybe_file.error().code(), EBADF);
  107. }
  108. TEST_CASE(mapped_file_tell_and_seek)
  109. {
  110. auto mapped_file = Core::MappedFile::map("/usr/Tests/LibCore/small.txt"sv).release_value();
  111. // Initial state.
  112. {
  113. auto current_offset = mapped_file->tell().release_value();
  114. EXPECT_EQ(current_offset, 0ul);
  115. }
  116. // Read a character.
  117. {
  118. auto character = mapped_file->read_value<char>().release_value();
  119. EXPECT_EQ(character, 'W');
  120. auto current_offset = mapped_file->tell().release_value();
  121. EXPECT_EQ(current_offset, 1ul);
  122. }
  123. // Read one more character.
  124. {
  125. auto character = mapped_file->read_value<char>().release_value();
  126. EXPECT_EQ(character, 'e');
  127. auto current_offset = mapped_file->tell().release_value();
  128. EXPECT_EQ(current_offset, 2ul);
  129. }
  130. // Seek seven characters forward.
  131. {
  132. auto current_offset = mapped_file->seek(7, SeekMode::FromCurrentPosition).release_value();
  133. EXPECT_EQ(current_offset, 9ul);
  134. }
  135. // Read a character again.
  136. {
  137. auto character = mapped_file->read_value<char>().release_value();
  138. EXPECT_EQ(character, 'o');
  139. auto current_offset = mapped_file->tell().release_value();
  140. EXPECT_EQ(current_offset, 10ul);
  141. }
  142. // Seek five characters backwards.
  143. {
  144. auto current_offset = mapped_file->seek(-5, SeekMode::FromCurrentPosition).release_value();
  145. EXPECT_EQ(current_offset, 5ul);
  146. }
  147. // Read a character.
  148. {
  149. auto character = mapped_file->read_value<char>().release_value();
  150. EXPECT_EQ(character, 'h');
  151. auto current_offset = mapped_file->tell().release_value();
  152. EXPECT_EQ(current_offset, 6ul);
  153. }
  154. // Seek back to the beginning.
  155. {
  156. auto current_offset = mapped_file->seek(0, SeekMode::SetPosition).release_value();
  157. EXPECT_EQ(current_offset, 0ul);
  158. }
  159. // Read the first character. This should prime the buffer if it hasn't happened already.
  160. {
  161. auto character = mapped_file->read_value<char>().release_value();
  162. EXPECT_EQ(character, 'W');
  163. auto current_offset = mapped_file->tell().release_value();
  164. EXPECT_EQ(current_offset, 1ul);
  165. }
  166. // Seek beyond the buffer size, which should invalidate the buffer.
  167. {
  168. auto current_offset = mapped_file->seek(12, SeekMode::SetPosition).release_value();
  169. EXPECT_EQ(current_offset, 12ul);
  170. }
  171. // Ensure that we still read the correct contents from the new offset with a (presumably) freshly filled buffer.
  172. {
  173. auto character = mapped_file->read_value<char>().release_value();
  174. EXPECT_EQ(character, 'r');
  175. auto current_offset = mapped_file->tell().release_value();
  176. EXPECT_EQ(current_offset, 13ul);
  177. }
  178. }