summaryrefslogtreecommitdiff
path: root/Tests/LibCore
diff options
context:
space:
mode:
authorDaniel Bertalan <dani@danielbertalan.dev>2021-12-31 21:36:37 +0100
committerAndreas Kling <kling@serenityos.org>2022-01-01 14:44:02 +0100
commitad57289307ee83cd79bdb6f0a7d9314188e829be (patch)
tree0fa24417882a4f6310c35b4ff31e37188f40c390 /Tests/LibCore
parent7fdf4004de79486d475b270f08b64ea08867d3a4 (diff)
downloadserenity-ad57289307ee83cd79bdb6f0a7d9314188e829be.zip
Tests/LibCore: Add regression test for the read_until_any_of OOB read
Diffstat (limited to 'Tests/LibCore')
-rw-r--r--Tests/LibCore/CMakeLists.txt2
-rw-r--r--Tests/LibCore/TestLibCoreStream.cpp30
-rw-r--r--Tests/LibCore/small.txt4
3 files changed, 34 insertions, 2 deletions
diff --git a/Tests/LibCore/CMakeLists.txt b/Tests/LibCore/CMakeLists.txt
index 1e3f4b5097..c83c229265 100644
--- a/Tests/LibCore/CMakeLists.txt
+++ b/Tests/LibCore/CMakeLists.txt
@@ -13,4 +13,4 @@ endforeach()
# NOTE: Required because of the LocalServer tests
target_link_libraries(TestLibCoreStream LibThreading)
-install(FILES long_lines.txt 10kb.txt DESTINATION usr/Tests/LibCore)
+install(FILES long_lines.txt 10kb.txt small.txt DESTINATION usr/Tests/LibCore)
diff --git a/Tests/LibCore/TestLibCoreStream.cpp b/Tests/LibCore/TestLibCoreStream.cpp
index 2f1276b49c..bef25c9176 100644
--- a/Tests/LibCore/TestLibCoreStream.cpp
+++ b/Tests/LibCore/TestLibCoreStream.cpp
@@ -380,7 +380,7 @@ TEST_CASE(local_socket_write)
// Buffered stream tests
-TEST_CASE(buffered_file_read)
+TEST_CASE(buffered_long_file_read)
{
auto maybe_file = Core::Stream::File::open("/usr/Tests/LibCore/long_lines.txt", Core::Stream::OpenMode::Read);
EXPECT(!maybe_file.is_error());
@@ -402,6 +402,34 @@ TEST_CASE(buffered_file_read)
EXPECT_EQ(maybe_after_seek_nread.value(), 3985ul); // 4095 - 110
}
+TEST_CASE(buffered_small_file_read)
+{
+ auto maybe_file = Core::Stream::File::open("/usr/Tests/LibCore/small.txt", Core::Stream::OpenMode::Read);
+ EXPECT(!maybe_file.is_error());
+ auto maybe_buffered_file = Core::Stream::BufferedFile::create(maybe_file.release_value());
+ EXPECT(!maybe_buffered_file.is_error());
+ auto file = maybe_buffered_file.release_value();
+
+ static constexpr StringView expected_lines[] {
+ "Well"sv,
+ "hello"sv,
+ "friends!"sv,
+ ":^)"sv
+ };
+
+ // Testing that we don't read out of bounds when the entire file fits into the buffer
+ auto buffer = ByteBuffer::create_uninitialized(4096).release_value();
+ for (auto const& line : expected_lines) {
+ VERIFY(file.can_read_line().release_value());
+ auto maybe_nread = file.read_line(buffer);
+ EXPECT(!maybe_nread.is_error());
+ EXPECT_EQ(maybe_nread.value(), line.length());
+ EXPECT_EQ(StringView(buffer.span().trim(maybe_nread.value())), line);
+ }
+ EXPECT(!file.can_read_line().is_error());
+ EXPECT(!file.can_read_line().value());
+}
+
constexpr auto buffered_sent_data = "Well hello friends!\n:^)\nThis shouldn't be present. :^("sv;
constexpr auto first_line = "Well hello friends!"sv;
constexpr auto second_line = ":^)"sv;
diff --git a/Tests/LibCore/small.txt b/Tests/LibCore/small.txt
new file mode 100644
index 0000000000..de8d8a3470
--- /dev/null
+++ b/Tests/LibCore/small.txt
@@ -0,0 +1,4 @@
+Well
+hello
+friends!
+:^)