summaryrefslogtreecommitdiff
path: root/Tests/LibCore
diff options
context:
space:
mode:
authorsin-ack <sin-ack@users.noreply.github.com>2021-12-29 22:31:45 +0000
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2022-01-13 15:16:12 +0330
commitdbd25916a3029a4b87b658986e11a39803b15b64 (patch)
tree6147a973f5ae6ac208154cfb4a69401e6b5eebe7 /Tests/LibCore
parenteb389db92c4e7e09e3ce529aa173df80d75bab58 (diff)
downloadserenity-dbd25916a3029a4b87b658986e11a39803b15b64.zip
LibCore+Userland+Tests: Convert Stream APIs to construct on heap
As per previous discussion, it was decided that the Stream classes should be constructed on the heap. While I don't personally agree with this change, it does have the benefit of avoiding Function object reconstructions due to the lambda passed to Notifier pointing to a stale object reference. This also has the benefit of not having to "box" objects for virtual usage, as the objects come pre-boxed. However, it means that we now hit the heap everytime we construct a TCPSocket for instance, which might not be desirable.
Diffstat (limited to 'Tests/LibCore')
-rw-r--r--Tests/LibCore/TestLibCoreStream.cpp126
1 files changed, 63 insertions, 63 deletions
diff --git a/Tests/LibCore/TestLibCoreStream.cpp b/Tests/LibCore/TestLibCoreStream.cpp
index bef25c9176..7204b79d48 100644
--- a/Tests/LibCore/TestLibCoreStream.cpp
+++ b/Tests/LibCore/TestLibCoreStream.cpp
@@ -31,12 +31,12 @@ TEST_CASE(file_open)
// Testing out some basic file properties.
auto file = maybe_file.release_value();
- EXPECT(file.is_open());
- EXPECT(!file.is_readable());
- EXPECT(file.is_writable());
- EXPECT(!file.is_eof());
+ EXPECT(file->is_open());
+ EXPECT(!file->is_readable());
+ EXPECT(file->is_writable());
+ EXPECT(!file->is_eof());
- auto maybe_size = file.size();
+ auto maybe_size = file->size();
EXPECT(!maybe_size.is_error());
EXPECT_EQ(maybe_size.value(), 0);
}
@@ -48,7 +48,7 @@ TEST_CASE(file_write_bytes)
constexpr auto some_words = "These are some words"sv;
ReadonlyBytes buffer { some_words.characters_without_null_termination(), some_words.length() };
- auto result = file.write(buffer);
+ auto result = file->write(buffer);
EXPECT(!result.is_error());
}
@@ -64,7 +64,7 @@ TEST_CASE(file_read_bytes)
EXPECT(maybe_buffer.has_value());
auto buffer = maybe_buffer.release_value();
- auto result = file.read(buffer);
+ auto result = file->read(buffer);
EXPECT(!result.is_error());
EXPECT_EQ(result.value(), 131ul);
@@ -82,7 +82,7 @@ TEST_CASE(file_seeking_around)
EXPECT(!maybe_file.is_error());
auto file = maybe_file.release_value();
- EXPECT_EQ(file.size().release_value(), 8702);
+ EXPECT_EQ(file->size().release_value(), 8702);
auto maybe_buffer = ByteBuffer::create_uninitialized(16);
EXPECT(maybe_buffer.has_value());
@@ -90,19 +90,19 @@ TEST_CASE(file_seeking_around)
StringView buffer_contents { buffer.bytes() };
- EXPECT(!file.seek(500, Core::Stream::SeekMode::SetPosition).is_error());
- EXPECT_EQ(file.tell().release_value(), 500);
- EXPECT(file.read_or_error(buffer));
+ EXPECT(!file->seek(500, Core::Stream::SeekMode::SetPosition).is_error());
+ EXPECT_EQ(file->tell().release_value(), 500);
+ EXPECT(file->read_or_error(buffer));
EXPECT_EQ(buffer_contents, expected_seek_contents1);
- EXPECT(!file.seek(234, Core::Stream::SeekMode::FromCurrentPosition).is_error());
- EXPECT_EQ(file.tell().release_value(), 750);
- EXPECT(file.read_or_error(buffer));
+ EXPECT(!file->seek(234, Core::Stream::SeekMode::FromCurrentPosition).is_error());
+ EXPECT_EQ(file->tell().release_value(), 750);
+ EXPECT(file->read_or_error(buffer));
EXPECT_EQ(buffer_contents, expected_seek_contents2);
- EXPECT(!file.seek(-105, Core::Stream::SeekMode::FromEndPosition).is_error());
- EXPECT_EQ(file.tell().release_value(), 8597);
- EXPECT(file.read_or_error(buffer));
+ EXPECT(!file->seek(-105, Core::Stream::SeekMode::FromEndPosition).is_error());
+ EXPECT_EQ(file->tell().release_value(), 8597);
+ EXPECT(file->read_or_error(buffer));
EXPECT_EQ(buffer_contents, expected_seek_contents3);
}
@@ -115,7 +115,7 @@ TEST_CASE(file_adopt_fd)
EXPECT(!maybe_file.is_error());
auto file = maybe_file.release_value();
- EXPECT_EQ(file.size().release_value(), 8702);
+ EXPECT_EQ(file->size().release_value(), 8702);
auto maybe_buffer = ByteBuffer::create_uninitialized(16);
EXPECT(maybe_buffer.has_value());
@@ -123,9 +123,9 @@ TEST_CASE(file_adopt_fd)
StringView buffer_contents { buffer.bytes() };
- EXPECT(!file.seek(500, Core::Stream::SeekMode::SetPosition).is_error());
- EXPECT_EQ(file.tell().release_value(), 500);
- EXPECT(file.read_or_error(buffer));
+ EXPECT(!file->seek(500, Core::Stream::SeekMode::SetPosition).is_error());
+ EXPECT_EQ(file->tell().release_value(), 500);
+ EXPECT(file->read_or_error(buffer));
EXPECT_EQ(buffer_contents, expected_seek_contents1);
// A single seek & read test should be fine for now.
@@ -158,20 +158,20 @@ TEST_CASE(tcp_socket_read)
EXPECT(!maybe_client_socket.is_error());
auto client_socket = maybe_client_socket.release_value();
- EXPECT(client_socket.is_open());
+ EXPECT(client_socket->is_open());
auto maybe_server_socket = tcp_server->accept();
EXPECT(!maybe_server_socket.is_error());
auto server_socket = maybe_server_socket.release_value();
- EXPECT(!server_socket.write({ sent_data.characters_without_null_termination(), sent_data.length() }).is_error());
- server_socket.close();
+ EXPECT(!server_socket->write({ sent_data.characters_without_null_termination(), sent_data.length() }).is_error());
+ server_socket->close();
- EXPECT(client_socket.can_read_without_blocking(100).release_value());
- EXPECT_EQ(client_socket.pending_bytes().release_value(), sent_data.length());
+ EXPECT(client_socket->can_read_without_blocking(100).release_value());
+ EXPECT_EQ(client_socket->pending_bytes().release_value(), sent_data.length());
auto maybe_receive_buffer = ByteBuffer::create_uninitialized(64);
auto receive_buffer = maybe_receive_buffer.release_value();
- auto maybe_nread = client_socket.read(receive_buffer);
+ auto maybe_nread = client_socket->read(receive_buffer);
EXPECT(!maybe_nread.is_error());
auto nread = maybe_nread.release_value();
@@ -196,13 +196,13 @@ TEST_CASE(tcp_socket_write)
auto maybe_server_socket = tcp_server->accept();
EXPECT(!maybe_server_socket.is_error());
auto server_socket = maybe_server_socket.release_value();
- EXPECT(!server_socket.set_blocking(true).is_error());
+ EXPECT(!server_socket->set_blocking(true).is_error());
- EXPECT(client_socket.write_or_error({ sent_data.characters_without_null_termination(), sent_data.length() }));
- client_socket.close();
+ EXPECT(client_socket->write_or_error({ sent_data.characters_without_null_termination(), sent_data.length() }));
+ client_socket->close();
auto receive_buffer = ByteBuffer::create_uninitialized(64).release_value();
- auto maybe_nread = server_socket.read(receive_buffer);
+ auto maybe_nread = server_socket->read(receive_buffer);
EXPECT(!maybe_nread.is_error());
auto nread = maybe_nread.release_value();
@@ -224,21 +224,21 @@ TEST_CASE(tcp_socket_eof)
EXPECT(!maybe_client_socket.is_error());
auto client_socket = maybe_client_socket.release_value();
- EXPECT(client_socket.is_open());
+ EXPECT(client_socket->is_open());
auto server_socket = tcp_server->accept().release_value();
- server_socket.close();
+ server_socket->close();
// NOTE: This may seem unintuitive, but poll will mark a fd which has
// reached EOF (i.e. in the case of the other side disconnecting) as
// POLLIN.
- EXPECT(client_socket.can_read_without_blocking(100).release_value());
- EXPECT_EQ(client_socket.pending_bytes().release_value(), 0ul);
+ EXPECT(client_socket->can_read_without_blocking(100).release_value());
+ EXPECT_EQ(client_socket->pending_bytes().release_value(), 0ul);
auto maybe_receive_buffer = ByteBuffer::create_uninitialized(1);
auto receive_buffer = maybe_receive_buffer.release_value();
- EXPECT_EQ(client_socket.read(receive_buffer).release_value(), 0ul);
- EXPECT(client_socket.is_eof());
+ EXPECT_EQ(client_socket->read(receive_buffer).release_value(), 0ul);
+ EXPECT(client_socket->is_eof());
}
// UDPSocket tests
@@ -258,8 +258,8 @@ TEST_CASE(udp_socket_read_write)
EXPECT(!maybe_client_socket.is_error());
auto client_socket = maybe_client_socket.release_value();
- EXPECT(client_socket.is_open());
- EXPECT(client_socket.write_or_error({ sent_data.characters_without_null_termination(), sent_data.length() }));
+ EXPECT(client_socket->is_open());
+ EXPECT(client_socket->write_or_error({ sent_data.characters_without_null_termination(), sent_data.length() }));
// FIXME: UDPServer::receive sadly doesn't give us a way to block on it,
// currently.
@@ -274,16 +274,16 @@ TEST_CASE(udp_socket_read_write)
EXPECT(!udp_server->send({ udp_reply_data.characters_without_null_termination(), udp_reply_data.length() }, client_address).is_error());
- EXPECT(client_socket.can_read_without_blocking(100).release_value());
- EXPECT_EQ(client_socket.pending_bytes().release_value(), udp_reply_data.length());
+ EXPECT(client_socket->can_read_without_blocking(100).release_value());
+ EXPECT_EQ(client_socket->pending_bytes().release_value(), udp_reply_data.length());
// Testing that supplying a smaller buffer than required causes a failure.
auto small_buffer = ByteBuffer::create_uninitialized(8).release_value();
- EXPECT_EQ(client_socket.read(small_buffer).error().code(), EMSGSIZE);
+ EXPECT_EQ(client_socket->read(small_buffer).error().code(), EMSGSIZE);
auto maybe_client_receive_buffer = ByteBuffer::create_uninitialized(64);
auto client_receive_buffer = maybe_client_receive_buffer.release_value();
- auto maybe_nread = client_socket.read(client_receive_buffer);
+ auto maybe_nread = client_socket->read(client_receive_buffer);
EXPECT(!maybe_nread.is_error());
auto nread = maybe_nread.release_value();
@@ -317,14 +317,14 @@ TEST_CASE(local_socket_read)
EXPECT(!maybe_client_socket.is_error());
auto client_socket = maybe_client_socket.release_value();
- EXPECT(client_socket.is_open());
+ EXPECT(client_socket->is_open());
- EXPECT(client_socket.can_read_without_blocking(100).release_value());
- EXPECT_EQ(client_socket.pending_bytes().release_value(), sent_data.length());
+ EXPECT(client_socket->can_read_without_blocking(100).release_value());
+ EXPECT_EQ(client_socket->pending_bytes().release_value(), sent_data.length());
auto maybe_receive_buffer = ByteBuffer::create_uninitialized(64);
auto receive_buffer = maybe_receive_buffer.release_value();
- auto maybe_nread = client_socket.read(receive_buffer);
+ auto maybe_nread = client_socket->read(receive_buffer);
EXPECT(!maybe_nread.is_error());
auto nread = maybe_nread.release_value();
@@ -367,8 +367,8 @@ TEST_CASE(local_socket_write)
EXPECT(!maybe_client_socket.is_error());
auto client_socket = maybe_client_socket.release_value();
- EXPECT(client_socket.write_or_error({ sent_data.characters_without_null_termination(), sent_data.length() }));
- client_socket.close();
+ EXPECT(client_socket->write_or_error({ sent_data.characters_without_null_termination(), sent_data.length() }));
+ client_socket->close();
return 0;
},
@@ -389,15 +389,15 @@ TEST_CASE(buffered_long_file_read)
auto file = maybe_buffered_file.release_value();
auto buffer = ByteBuffer::create_uninitialized(4096).release_value();
- EXPECT(!file.seek(255, Core::Stream::SeekMode::SetPosition).is_error());
- EXPECT(file.can_read_line().release_value());
- auto maybe_nread = file.read_line(buffer);
+ EXPECT(!file->seek(255, Core::Stream::SeekMode::SetPosition).is_error());
+ EXPECT(file->can_read_line().release_value());
+ auto maybe_nread = file->read_line(buffer);
EXPECT(!maybe_nread.is_error());
EXPECT_EQ(maybe_nread.value(), 4095ul); // 4095 bytes on the third line
// Testing that buffering with seeking works properly
- EXPECT(!file.seek(365, Core::Stream::SeekMode::SetPosition).is_error());
- auto maybe_after_seek_nread = file.read_line(buffer);
+ EXPECT(!file->seek(365, Core::Stream::SeekMode::SetPosition).is_error());
+ auto maybe_after_seek_nread = file->read_line(buffer);
EXPECT(!maybe_after_seek_nread.is_error());
EXPECT_EQ(maybe_after_seek_nread.value(), 3985ul); // 4095 - 110
}
@@ -420,14 +420,14 @@ TEST_CASE(buffered_small_file_read)
// 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);
+ 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());
+ 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;
@@ -450,23 +450,23 @@ TEST_CASE(buffered_tcp_socket_read)
EXPECT(!maybe_buffered_socket.is_error());
auto client_socket = maybe_buffered_socket.release_value();
- EXPECT(client_socket.is_open());
+ EXPECT(client_socket->is_open());
auto maybe_server_socket = tcp_server->accept();
EXPECT(!maybe_server_socket.is_error());
auto server_socket = maybe_server_socket.release_value();
- EXPECT(!server_socket.write({ buffered_sent_data.characters_without_null_termination(), sent_data.length() }).is_error());
+ EXPECT(!server_socket->write({ buffered_sent_data.characters_without_null_termination(), sent_data.length() }).is_error());
- EXPECT(client_socket.can_read_without_blocking(100).release_value());
+ EXPECT(client_socket->can_read_without_blocking(100).release_value());
auto receive_buffer = ByteBuffer::create_uninitialized(64).release_value();
- auto maybe_first_nread = client_socket.read_line(receive_buffer);
+ auto maybe_first_nread = client_socket->read_line(receive_buffer);
EXPECT(!maybe_first_nread.is_error());
StringView first_received_line { receive_buffer.data(), maybe_first_nread.value() };
EXPECT_EQ(first_received_line, first_line);
- auto maybe_second_nread = client_socket.read_line(receive_buffer);
+ auto maybe_second_nread = client_socket->read_line(receive_buffer);
EXPECT(!maybe_second_nread.is_error());
StringView second_received_line { receive_buffer.data(), maybe_second_nread.value() };
EXPECT_EQ(second_received_line, second_line);