From f84d23817e16e98fd6a3e03d6a9dd3d75362951c Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Sat, 10 Apr 2021 18:16:03 +0200 Subject: [PATCH] tests/comms: Add tests for TcpInetClient/Server --- tests/zm_comms.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tests/zm_comms.cpp b/tests/zm_comms.cpp index 622ecde72..7a777f515 100644 --- a/tests/zm_comms.cpp +++ b/tests/zm_comms.cpp @@ -245,3 +245,72 @@ TEST_CASE("ZM::UdpUnixSocket send/recv") { REQUIRE(rcv == msg); } } + +TEST_CASE("ZM::TcpInetClient basics") { + ZM::TcpInetClient client; + REQUIRE(client.isClosed() == true); + REQUIRE(client.isOpen() == false); + REQUIRE(client.isConnected() == false); + REQUIRE(client.isDisconnected() == false); + + REQUIRE(client.connect("127.0.0.1", 1234) == false); + REQUIRE(client.isClosed() == true); + REQUIRE(client.isOpen() == false); + REQUIRE(client.isConnected() == false); + REQUIRE(client.isDisconnected() == false); +} + +TEST_CASE("ZM::TcpInetServer basics") { + ZM::TcpInetServer server; + REQUIRE(server.isClosed() == true); + REQUIRE(server.isOpen() == false); + REQUIRE(server.isConnected() == false); + REQUIRE(server.isDisconnected() == false); + + REQUIRE(server.bind(1234) == true); + REQUIRE(server.isOpen() == true); + REQUIRE(server.isClosed() == false); + REQUIRE(server.isConnected() == false); + REQUIRE(server.isDisconnected() == true); + REQUIRE(server.isListening() == false); + + REQUIRE(server.listen() == true); + REQUIRE(server.isListening() == true); + + SECTION("close") { + REQUIRE(server.close() == true); + REQUIRE(server.isClosed() == true); + REQUIRE(server.isOpen() == false); + REQUIRE(server.isConnected() == false); + REQUIRE(server.isDisconnected() == false); + } +} + +TEST_CASE("ZM::TcpInetClient/Server send/recv") { + ZM::TcpInetServer server; + ZM::TcpInetClient client; + + std::array msg = {'a', 'b', 'c'}; + std::array rcv{}; + + SECTION("send/recv on unbound socket") { + REQUIRE(client.send(msg.data(), msg.size()) == -1); + REQUIRE(server.recv(rcv.data(), rcv.size()) == -1); + } + + SECTION("send/recv") { + REQUIRE(server.bind(1234) == true); + REQUIRE(server.isOpen() == true); + REQUIRE(server.listen() == true); + + REQUIRE(client.connect("127.0.0.1", 1234) == true); + REQUIRE(client.isConnected() == true); + + REQUIRE(server.accept() == true); + + REQUIRE(client.send(msg.data(), msg.size()) == msg.size()); + REQUIRE(server.recv(rcv.data(), rcv.size()) == msg.size()); + + REQUIRE(rcv == msg); + } +}