Skip to content

Commit

Permalink
Fixed more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikołaj Małecki committed Feb 27, 2024
1 parent 0bee6d8 commit 74592c1
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 33 deletions.
16 changes: 12 additions & 4 deletions test/test_file_transmission.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <ctime>
#include <random>
#include <vector>
#include <atomic>

//#pragma comment (lib, "ws2_32.lib")

Expand Down Expand Up @@ -94,7 +95,7 @@ TEST(Transmission, FileUpload)

// Start listener-receiver thread

bool thread_exit = false;
std::atomic<bool> thread_exit { false };

auto client = std::thread([&]
{
Expand All @@ -117,12 +118,17 @@ TEST(Transmission, FileUpload)
for (;;)
{
int n = srt_recv(accepted_sock, buf.data(), 1456);
ASSERT_NE(n, SRT_ERROR);
EXPECT_NE(n, SRT_ERROR) << srt_getlasterror_str();
if (n == 0)
{
std::cerr << "Received 0 bytes, breaking.\n";
break;
}
else if (n == -1)
{
std::cerr << "READ FAILED, breaking anyway\n";
break;
}

// Write to file any amount of data received
copyfile.write(buf.data(), n);
Expand Down Expand Up @@ -183,8 +189,10 @@ TEST(Transmission, FileUpload)

std::cout << "Comparing files\n";
// Compare files
tarfile.seekg(0, std::ios::end);
ifile.seekg(0, std::ios::beg);
tarfile.seekg(0, std::ios::beg);

ifile.close();
ifile.open("file.source", std::ios::in | std::ios::binary);

for (size_t i = 0; i < tar_size; ++i)
{
Expand Down
92 changes: 64 additions & 28 deletions test/test_reuseaddr.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <thread>
#include <future>
#include <sstream>
#ifndef _WIN32
#include <ifaddrs.h>
#endif
Expand Down Expand Up @@ -103,11 +104,45 @@ static std::string GetLocalIP(int af = AF_UNSPEC)

class ReuseAddr : public srt::Test
{
int m_client_pollid = SRT_ERROR;
int m_server_pollid = SRT_ERROR;


protected:

std::string showEpollContents(const char* label, int* array, int length)
{
std::ostringstream out;
out << label << ":[";
if (length)
{
// Now is at least 1
out << "@" << array[0];

for (int i = 1; i < length; ++i)
out << " @" << array[i];
}
out << "]";
return out.str();
}

struct UniquePollid
{
int pollid = SRT_ERROR;
UniquePollid()
{
pollid = srt_epoll_create();
}

~UniquePollid()
{
srt_epoll_release(pollid);
}

operator int() const
{
return pollid;
}
};

void clientSocket(SRTSOCKET client_sock, std::string ip, int port, bool expect_success)
{
using namespace std;
Expand All @@ -132,8 +167,11 @@ class ReuseAddr : public srt::Test
EXPECT_NE(srt_setsockflag(client_sock, SRTO_SENDER, &yes, sizeof yes), SRT_ERROR);
EXPECT_NE(srt_setsockflag(client_sock, SRTO_TSBPDMODE, &yes, sizeof yes), SRT_ERROR);

UniquePollid client_pollid;
ASSERT_NE(int(client_pollid), SRT_ERROR);

int epoll_out = SRT_EPOLL_OUT;
srt_epoll_add_usock(m_client_pollid, client_sock, &epoll_out);
srt_epoll_add_usock(client_pollid, client_sock, &epoll_out);

sockaddr_any sa = srt::CreateAddr(ip, port, family);

Expand Down Expand Up @@ -164,14 +202,14 @@ class ReuseAddr : public srt::Test

cout << "[T/C] Waiting for connection readiness...\n";

EXPECT_NE(srt_epoll_wait(m_client_pollid, read, &rlen,
EXPECT_NE(srt_epoll_wait(client_pollid, read, &rlen,
write, &wlen,
-1, // -1 is set for debuging purpose.
// in case of production we need to set appropriate value
0, 0, 0, 0), SRT_ERROR);
0, 0, 0, 0), SRT_ERROR) << srt_getlasterror_str();

EXPECT_EQ(rlen, 0) << "[T/C] read-ready"; // get exactly one write event without reads
EXPECT_EQ(wlen, 1) << "[T/C] write-ready"; // get exactly one write event without reads
EXPECT_EQ(rlen, 0) << showEpollContents("[T/C] R", read, rlen); // get exactly one write event without reads
EXPECT_EQ(wlen, 1) << showEpollContents("[T/C] W", write, wlen); // get exactly one write event without reads
EXPECT_EQ(write[0], client_sock); // for our client socket

char buffer[1316] = {1, 2, 3, 4};
Expand All @@ -194,7 +232,7 @@ class ReuseAddr : public srt::Test
cout << "[T/C] Client exit\n";
}

SRTSOCKET prepareSocket()
SRTSOCKET prepareServerSocket()
{
SRTSOCKET bindsock = srt_create_socket();
EXPECT_NE(bindsock, SRT_ERROR);
Expand Down Expand Up @@ -251,7 +289,7 @@ class ReuseAddr : public srt::Test
{
std::cout << "[T/S] serverSocket: creating listener socket\n";

SRTSOCKET bindsock = prepareSocket();
SRTSOCKET bindsock = prepareServerSocket();

if (!bindListener(bindsock, ip, port, expect_success))
return SRT_INVALID_SOCK;
Expand All @@ -263,7 +301,7 @@ class ReuseAddr : public srt::Test
{
std::cout << "[T/S] serverSocket: creating binder socket\n";

SRTSOCKET bindsock = prepareSocket();
SRTSOCKET bindsock = prepareServerSocket();

if (!bindSocket(bindsock, ip, port, expect_success))
{
Expand Down Expand Up @@ -291,18 +329,18 @@ class ReuseAddr : public srt::Test
int wlen = 2;
SRTSOCKET write[2];

std::cout << "[T/S] Wait 10s for acceptance on @" << bindsock << " ...\n";
std::cout << "[T/S] Wait 10s on E" << m_server_pollid << " for acceptance on @" << bindsock << " ...\n";

EXPECT_NE(srt_epoll_wait(m_server_pollid,
read, &rlen,
write, &wlen,
10000, // -1 is set for debuging purpose.
// in case of production we need to set appropriate value
0, 0, 0, 0), SRT_ERROR );
0, 0, 0, 0), SRT_ERROR) << srt_getlasterror_str();


EXPECT_EQ(rlen, 1); // get exactly one read event without writes
EXPECT_EQ(wlen, 0); // get exactly one read event without writes
EXPECT_EQ(rlen, 1) << showEpollContents("[T/S] R", read, rlen); // get exactly one read event without writes
EXPECT_EQ(wlen, 0) << showEpollContents("[T/S] W", write, wlen); // get exactly one read event without writes
ASSERT_EQ(read[0], bindsock); // read event is for bind socket
}

Expand Down Expand Up @@ -336,11 +374,11 @@ class ReuseAddr : public srt::Test
write, &wlen,
-1, // -1 is set for debuging purpose.
// in case of production we need to set appropriate value
0, 0, 0, 0), SRT_ERROR );
0, 0, 0, 0), SRT_ERROR) << srt_getlasterror_str();


EXPECT_EQ(rlen, 1) << "[T/S] read-ready"; // get exactly one read event without writes
EXPECT_EQ(wlen, 0) << "[T/S] write-ready"; // get exactly one read event without writes
EXPECT_EQ(rlen, 1) << showEpollContents("[T/S] R", read, rlen); // get exactly one read event without writes
EXPECT_EQ(wlen, 0) << showEpollContents("[T/S] W", write, wlen); // get exactly one read event without writes
EXPECT_EQ(read[0], accepted_sock.ref()); // read event is for bind socket
}

Expand All @@ -356,8 +394,9 @@ class ReuseAddr : public srt::Test
// client_sock closed through UniqueSocket.
// cannot close client_sock after srt_sendmsg because of issue in api.c:2346

std::cout << "[T/S] joining client async (should close client socket)...\n";
std::cout << "[T/S] joining client async \n";
launched.get();
std::cout << "[T/S] closing client socket\n";
}

static void shutdownListener(SRTSOCKET bindsock)
Expand Down Expand Up @@ -399,17 +438,14 @@ class ReuseAddr : public srt::Test

void setup()
{
m_client_pollid = srt_epoll_create();
ASSERT_NE(m_client_pollid, SRT_ERROR);

m_server_pollid = srt_epoll_create();
ASSERT_NE(m_server_pollid, SRT_ERROR);
}

void teardown()
{
(void)srt_epoll_release(m_client_pollid);
(void)srt_epoll_release(m_server_pollid);
m_server_pollid = SRT_ERROR;
}
};

Expand Down Expand Up @@ -537,7 +573,7 @@ TEST_F(ReuseAddr, Wildcard6)
// This must be obligatory set before binding a socket to "::"
int strict_ipv6 = 1;

SRTSOCKET bindsock_1 = prepareSocket();
SRTSOCKET bindsock_1 = prepareServerSocket();
srt_setsockflag(bindsock_1, SRTO_IPV6ONLY, &strict_ipv6, sizeof strict_ipv6);
bindListener(bindsock_1, "::", 5000, true);

Expand All @@ -561,7 +597,7 @@ TEST_F(ReuseAddr, Wildcard6)

strict_ipv6 = 0;

bindsock_1 = prepareSocket();
bindsock_1 = prepareServerSocket();
srt_setsockflag(bindsock_1, SRTO_IPV6ONLY, &strict_ipv6, sizeof strict_ipv6);
bindListener(bindsock_1, "::", 5000, true);

Expand Down Expand Up @@ -595,8 +631,8 @@ TEST_F(ReuseAddr, ProtocolVersion6)
SRTSOCKET bindsock_1 = createListener("0.0.0.0", 5000, true);

// We need a small interception in this one.
// createListener = prepareSocket | bindListener
SRTSOCKET bindsock_2 = prepareSocket();
// createListener = prepareServerSocket | bindListener
SRTSOCKET bindsock_2 = prepareServerSocket();
{
int yes = 1;

Expand Down Expand Up @@ -624,8 +660,8 @@ TEST_F(ReuseAddr, ProtocolVersionFaux6)
SRTSOCKET bindsock_1 = createListener("0.0.0.0", 5000, true);

// We need a small interception in this one.
// createListener = prepareSocket | bindListener
SRTSOCKET bindsock_2 = prepareSocket();
// createListener = prepareServerSocket | bindListener
SRTSOCKET bindsock_2 = prepareServerSocket();
{
int no = 0;

Expand Down
3 changes: 2 additions & 1 deletion test/test_sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,8 @@ TEST(SyncEvent, WaitForNotifyAll)
/*****************************************************************************/
void* dummythread(void* param)
{
*(bool*)(param) = true;
auto& thread_finished = *(srt::sync::atomic<bool>*)param;
thread_finished = true;
return nullptr;
}

Expand Down

0 comments on commit 74592c1

Please sign in to comment.