more tests

This commit is contained in:
Erik Frojdh 2020-02-17 17:24:36 +01:00
parent d5df63ce49
commit f08006db46
2 changed files with 42 additions and 22 deletions

View File

@ -45,7 +45,8 @@ class UdpRxSocket {
const std::string portname = std::to_string(port);
if (getaddrinfo(hostname, portname.c_str(), &hints, &res)) {
throw RuntimeError("Failed at getaddrinfo with " + std::string(hostname));
throw RuntimeError("Failed at getaddrinfo with " +
std::string(hostname));
}
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (fd == -1) {
@ -83,23 +84,23 @@ class UdpRxSocket {
: UdpRxSocket(port_number, ps, InterfaceNameToIp(eth).str().c_str(),
buf_size) {}
const char *LastPacket() const { return buff; }
~UdpRxSocket() {
delete[] buff;
Shutdown();
}
// Receive one packet to the internal buffer of the socket class, preferred
// method?
bool ReceivePacket() {
auto bytes_received = recvfrom(fd, buff, packet_size, 0, nullptr, nullptr);
const char *LastPacket() const noexcept { return buff; }
constexpr ssize_t getPacketSize() const noexcept { return packet_size; }
bool ReceivePacket() noexcept {
auto bytes_received =
recvfrom(fd, buff, packet_size, 0, nullptr, nullptr);
return bytes_received == packet_size;
}
// Receive to an external buffer, do we need it?
bool ReceivePacket(char *dst) {
auto bytes_received = recvfrom(fd, buff, packet_size, 0, nullptr, nullptr);
bool ReceivePacket(char *dst) noexcept {
auto bytes_received =
recvfrom(fd, buff, packet_size, 0, nullptr, nullptr);
return bytes_received == packet_size;
}
@ -107,7 +108,8 @@ class UdpRxSocket {
// refactoring of the receiver
ssize_t ReceiveDataOnly(char *dst) {
auto r = recvfrom(fd, dst, packet_size, 0, nullptr, nullptr);
constexpr ssize_t eiger_header_packet = 40; //only detector that has this
constexpr ssize_t eiger_header_packet =
40; // only detector that has this
if (r == eiger_header_packet) {
FILE_LOG(logWARNING) << "Got header pkg";
r = recvfrom(fd, dst, packet_size, 0, nullptr, nullptr);
@ -115,10 +117,6 @@ class UdpRxSocket {
return r;
}
ssize_t getPacketSize() const{
return packet_size;
}
ssize_t getBufferSize() const {
uint64_t ret_size = 0;
socklen_t optlen = sizeof(uint64_t);

View File

@ -5,8 +5,7 @@
#include <thread>
#include <vector>
TEST_CASE("Receive a packet on localhost") {
constexpr int port = 50001;
int open_socket(int port) {
const char *host = nullptr; // localhost
// Create a socket for sending
@ -28,17 +27,30 @@ TEST_CASE("Receive a packet on localhost") {
throw sls::RuntimeError("Failed to create UDP RX socket");
}
if (connect(fd, res->ai_addr, res->ai_addrlen)){
throw sls::RuntimeError("Failed to connect socket");
}
freeaddrinfo(res);
return fd;
}
TEST_CASE("Receive a packet on localhost") {
constexpr int port = 50001;
std::vector<int> data_to_send{4, 5, 3, 2, 5, 7, 2, 3};
ssize_t packet_size =
sizeof(decltype(data_to_send)::value_type) * data_to_send.size();
sls::UdpRxSocket udpsock{port, packet_size, host};
sls::UdpRxSocket udpsock{port, packet_size};
int n = sendto(fd, data_to_send.data(), packet_size, 0, res->ai_addr,
res->ai_addrlen);
int fd = open_socket(port);
// int n = sendto(fd, data_to_send.data(), packet_size, 0, res->ai_addr,
// res->ai_addrlen);
auto n = write(fd, data_to_send.data(), packet_size);
CHECK(n == packet_size);
CHECK(udpsock.ReceivePacket());
close(fd);
// Copy data from buffer and compare values
std::vector<int> data_received(data_to_send.size());
memcpy(data_received.data(), udpsock.LastPacket(), udpsock.getPacketSize());
@ -65,3 +77,13 @@ TEST_CASE("Shutdown socket without hanging") {
CHECK(r == false); // since we didn't get the packet
}
TEST_CASE("Too small packet"){
constexpr int port = 50001;
sls::UdpRxSocket s(port, 2*sizeof(uint32_t));
auto fd = open_socket(port);
uint32_t val = 10;
write(fd, &val, sizeof(val));
CHECK(s.ReceivePacket() == false);
close(fd);
}