mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-14 22:07:12 +02:00
tests for socket
This commit is contained in:
@ -1,13 +1,86 @@
|
||||
#include "catch.hpp"
|
||||
#include "MySocketTCP.h"
|
||||
#include "catch.hpp"
|
||||
// #include "multiSlsDetector.h"
|
||||
#include "logger.h"
|
||||
#include <iostream>
|
||||
TEST_CASE("Construct") {
|
||||
#include <vector>
|
||||
|
||||
#define VERBOSE
|
||||
|
||||
// MySocketTCP(const char* const host_ip_or_name, unsigned short int const port_number): genericSocket(host_ip_or_name, port_number,TCP){setPacketSize(TCP_PACKET_SIZE);}; // sender (client): where to? ip
|
||||
unsigned short int const port_number = 1966;
|
||||
auto receiver = MySocketTCP(port_number);
|
||||
REQUIRE(false);
|
||||
|
||||
TEST_CASE("Sending and receiving data with two sockets") {
|
||||
|
||||
const int port_number{1966}; //TODO! Avoid hardcoded port number!!!
|
||||
auto sender = MySocketTCP("localhost", port_number);
|
||||
auto receiver = MySocketTCP(port_number);
|
||||
|
||||
auto s = sender.Connect();
|
||||
auto r = receiver.Connect();
|
||||
|
||||
REQUIRE(s > 0);
|
||||
REQUIRE(r > 0);
|
||||
REQUIRE(sender.getPortNumber() == port_number);
|
||||
REQUIRE(receiver.getPortNumber() == port_number);
|
||||
|
||||
std::vector<char> message_to_send{'H', 'e', 'l', 'l', 'o'};
|
||||
std::vector<char> received_message(message_to_send.size());
|
||||
|
||||
auto sent = sender.SendDataOnly(message_to_send.data(), message_to_send.size());
|
||||
auto received = receiver.ReceiveDataOnly(received_message.data(), message_to_send.size());
|
||||
REQUIRE(sent == message_to_send.size());
|
||||
REQUIRE(received == received_message.size());
|
||||
REQUIRE(sent == received);
|
||||
REQUIRE(message_to_send == received_message);
|
||||
|
||||
receiver.CloseServerTCPSocketDescriptor();
|
||||
receiver.Disconnect();
|
||||
sender.Disconnect();
|
||||
|
||||
REQUIRE(receiver.getsocketDescriptor() == -1);
|
||||
REQUIRE(receiver.getFileDes() == -1);
|
||||
REQUIRE(sender.getFileDes() == -1);
|
||||
}
|
||||
|
||||
TEST_CASE("Open two sockets on the same port fails and throws") {
|
||||
const int port_number{1966};
|
||||
auto server = MySocketTCP(port_number);
|
||||
CHECK_THROWS(MySocketTCP(port_number));
|
||||
}
|
||||
|
||||
// TEST_CASE("Conversions"){
|
||||
|
||||
// std::cout << "name " << MySocketTCP::nameToMac("enp10s0u1u3u3") << '\n';
|
||||
|
||||
// }
|
||||
|
||||
TEST_CASE("Have two clients connect to the same server") {
|
||||
const int port_number{1966};
|
||||
|
||||
auto server = MySocketTCP(port_number);
|
||||
|
||||
auto client1 = MySocketTCP("localhost", port_number);
|
||||
auto client2 = MySocketTCP("localhost", port_number);
|
||||
client1.SetTimeOut(1);
|
||||
client2.SetTimeOut(1);
|
||||
server.SetTimeOut(1);
|
||||
auto fd1 = client1.Connect();
|
||||
|
||||
auto fd2 = client2.Connect();
|
||||
server.Connect();
|
||||
|
||||
REQUIRE(fd1 > 0);
|
||||
REQUIRE(fd2 > 0);
|
||||
|
||||
std::cout << "fd1 " << fd1 << '\n';
|
||||
std::cout << "fd2 " << fd2 << '\n';
|
||||
|
||||
std::vector<char> message_to_send{'H', 'e', 'l', 'l', 'o'};
|
||||
std::vector<char> received_message(message_to_send.size());
|
||||
|
||||
client1.SendDataOnly(message_to_send.data(), message_to_send.size());
|
||||
auto n1 = server.ReceiveDataOnly(received_message.data(), received_message.size());
|
||||
std::cout << "n1 " << n1 << '\n';
|
||||
|
||||
client2.SendDataOnly(message_to_send.data(), message_to_send.size());
|
||||
auto n2 = server.ReceiveDataOnly(received_message.data(), received_message.size());
|
||||
std::cout << "n2 " << n2 << '\n';
|
||||
}
|
Reference in New Issue
Block a user