From eb31a52b8391b86f1e6b3d6174e4888260370bcd Mon Sep 17 00:00:00 2001 From: heinrich5991 Date: Wed, 20 Dec 2017 16:56:44 +0100 Subject: [PATCH] Add very basic test for Unix sockets --- CMakeLists.txt | 1 + src/base/system.c | 7 ++++++- src/base/system.h | 16 +++++++++++++--- src/engine/server/server.cpp | 6 +++--- src/test/teehistorian.cpp | 1 + src/test/unix.cpp | 12 ++++++++++++ 6 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 src/test/unix.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b498355f2..255535ae7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1053,6 +1053,7 @@ if(GTEST_FOUND OR DOWNLOAD_GTEST) test.cpp test.h thread.cpp + unix.cpp ) set(TESTS_EXTRA src/game/server/teehistorian.cpp diff --git a/src/base/system.c b/src/base/system.c index 12d9d8bc0..c2be9b44c 100644 --- a/src/base/system.c +++ b/src/base/system.c @@ -1826,7 +1826,7 @@ int net_init() } #if defined(CONF_FAMILY_UNIX) -UNIXSOCKET net_unnamed_unix_create() +UNIXSOCKET net_unix_create_unnamed() { return socket(AF_UNIX, SOCK_DGRAM, 0); } @@ -1842,6 +1842,11 @@ void net_unix_set_addr(UNIXSOCKETADDR *addr, const char *path) addr->sun_family = AF_UNIX; str_copy(addr->sun_path, path, sizeof(addr->sun_path)); } + +void net_unix_close(UNIXSOCKET sock) +{ + close(sock); +} #endif int fs_listdir_info(const char *dir, FS_LISTDIR_INFO_CALLBACK cb, int type, void *user) diff --git a/src/base/system.h b/src/base/system.h index 849da455a..841110622 100644 --- a/src/base/system.h +++ b/src/base/system.h @@ -837,13 +837,13 @@ int net_tcp_close(NETSOCKET sock); /* Group: Network Unix Sockets */ /* - Function: net_unnamed_unix_create - Creates an unnamed unix dgram socket. + Function: net_unix_create_unnamed + Creates an unnamed unix datagram socket. Returns: On success it returns a handle to the socket. On failure it returns -1. */ -UNIXSOCKET net_unnamed_unix_create(); +UNIXSOCKET net_unix_create_unnamed(); /* Function: net_unix_send @@ -869,6 +869,16 @@ int net_unix_send(UNIXSOCKET sock, UNIXSOCKETADDR *addr, void *data, int size); path - Path to the (named) unix socket. */ void net_unix_set_addr(UNIXSOCKETADDR *addr, const char *path); + +/* + Function: net_unix_close + Closes a Unix socket. + + Parameters: + sock - Socket to close. +*/ +void net_unix_close(UNIXSOCKET sock); + #endif /* Group: Strings */ diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index 23dbec6c9..c6b9fc930 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -863,7 +863,7 @@ void CServer::SendConnLoggingCommand(CONN_LOGGING_CMD Cmd, const NETADDR* pAddr) mem_copy(&aData[5], pAddr->ip, 16); mem_copy(&aData[21], &pAddr->port, 2); - net_unix_socket_send(m_ConnLoggingSocket, &m_ConnLoggingDestAddr, aData, sizeof(aData)); + net_unix_send(m_ConnLoggingSocket, &m_ConnLoggingDestAddr, aData, sizeof(aData)); } #endif @@ -2652,7 +2652,7 @@ void CServer::ConchainConnLoggingServerChange(IConsole::IResult *pResult, void * // open socket to send new connections if(!pServer->m_ConnLoggingSocketCreated) { - pServer->m_ConnLoggingSocket = net_unnamed_unix_socket_create(); + pServer->m_ConnLoggingSocket = net_unix_create_unnamed(); if(pServer->m_ConnLoggingSocket == -1) { pServer->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "Failed to created socket for communication with the connection logging server."); @@ -2664,7 +2664,7 @@ void CServer::ConchainConnLoggingServerChange(IConsole::IResult *pResult, void * } // set the destination address for the connection logging - net_unix_socket_set_addr(&pServer->m_ConnLoggingDestAddr, pResult->GetString(0)); + net_unix_set_addr(&pServer->m_ConnLoggingDestAddr, pResult->GetString(0)); } } #endif diff --git a/src/test/teehistorian.cpp b/src/test/teehistorian.cpp index 7607f41c3..d11dabe23 100644 --- a/src/test/teehistorian.cpp +++ b/src/test/teehistorian.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include diff --git a/src/test/unix.cpp b/src/test/unix.cpp new file mode 100644 index 000000000..1a6876b3f --- /dev/null +++ b/src/test/unix.cpp @@ -0,0 +1,12 @@ +#include + +#include + +#if defined(CONF_FAMILY_UNIX) +TEST(Unix, Create) +{ + UNIXSOCKET Socket = net_unix_create_unnamed(); + ASSERT_GE(Socket, 0); + net_unix_close(Socket); +} +#endif