Add very basic test for Unix sockets

This commit is contained in:
heinrich5991 2017-12-20 16:56:44 +01:00
parent 1a236dbefc
commit eb31a52b83
6 changed files with 36 additions and 7 deletions

View file

@ -1053,6 +1053,7 @@ if(GTEST_FOUND OR DOWNLOAD_GTEST)
test.cpp test.cpp
test.h test.h
thread.cpp thread.cpp
unix.cpp
) )
set(TESTS_EXTRA set(TESTS_EXTRA
src/game/server/teehistorian.cpp src/game/server/teehistorian.cpp

View file

@ -1826,7 +1826,7 @@ int net_init()
} }
#if defined(CONF_FAMILY_UNIX) #if defined(CONF_FAMILY_UNIX)
UNIXSOCKET net_unnamed_unix_create() UNIXSOCKET net_unix_create_unnamed()
{ {
return socket(AF_UNIX, SOCK_DGRAM, 0); 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; addr->sun_family = AF_UNIX;
str_copy(addr->sun_path, path, sizeof(addr->sun_path)); str_copy(addr->sun_path, path, sizeof(addr->sun_path));
} }
void net_unix_close(UNIXSOCKET sock)
{
close(sock);
}
#endif #endif
int fs_listdir_info(const char *dir, FS_LISTDIR_INFO_CALLBACK cb, int type, void *user) int fs_listdir_info(const char *dir, FS_LISTDIR_INFO_CALLBACK cb, int type, void *user)

View file

@ -837,13 +837,13 @@ int net_tcp_close(NETSOCKET sock);
/* Group: Network Unix Sockets */ /* Group: Network Unix Sockets */
/* /*
Function: net_unnamed_unix_create Function: net_unix_create_unnamed
Creates an unnamed unix dgram socket. Creates an unnamed unix datagram socket.
Returns: Returns:
On success it returns a handle to the socket. On failure it returns -1. 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 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. path - Path to the (named) unix socket.
*/ */
void net_unix_set_addr(UNIXSOCKETADDR *addr, const char *path); 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 #endif
/* Group: Strings */ /* Group: Strings */

View file

@ -863,7 +863,7 @@ void CServer::SendConnLoggingCommand(CONN_LOGGING_CMD Cmd, const NETADDR* pAddr)
mem_copy(&aData[5], pAddr->ip, 16); mem_copy(&aData[5], pAddr->ip, 16);
mem_copy(&aData[21], &pAddr->port, 2); 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 #endif
@ -2652,7 +2652,7 @@ void CServer::ConchainConnLoggingServerChange(IConsole::IResult *pResult, void *
// open socket to send new connections // open socket to send new connections
if(!pServer->m_ConnLoggingSocketCreated) if(!pServer->m_ConnLoggingSocketCreated)
{ {
pServer->m_ConnLoggingSocket = net_unnamed_unix_socket_create(); pServer->m_ConnLoggingSocket = net_unix_create_unnamed();
if(pServer->m_ConnLoggingSocket == -1) if(pServer->m_ConnLoggingSocket == -1)
{ {
pServer->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "Failed to created socket for communication with the connection logging server."); 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 // 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 #endif

View file

@ -1,5 +1,6 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <base/detect.h>
#include <engine/shared/config.h> #include <engine/shared/config.h>
#include <game/gamecore.h> #include <game/gamecore.h>
#include <game/server/teehistorian.h> #include <game/server/teehistorian.h>

12
src/test/unix.cpp Normal file
View file

@ -0,0 +1,12 @@
#include <gtest/gtest.h>
#include <base/system.h>
#if defined(CONF_FAMILY_UNIX)
TEST(Unix, Create)
{
UNIXSOCKET Socket = net_unix_create_unnamed();
ASSERT_GE(Socket, 0);
net_unix_close(Socket);
}
#endif