Tests/LibCore: Add regression test for the read_until_any_of OOB read

This commit is contained in:
Daniel Bertalan 2021-12-31 21:36:37 +01:00 committed by Andreas Kling
parent 7fdf4004de
commit ad57289307
Notes: sideshowbarker 2024-07-17 21:51:18 +09:00
3 changed files with 34 additions and 2 deletions

View file

@ -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)

View file

@ -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;

4
Tests/LibCore/small.txt Normal file
View file

@ -0,0 +1,4 @@
Well
hello
friends!
:^)