Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion framework/areg/base/NESocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,58 @@
************************************************************************/
struct sockaddr_in;

#ifndef UNITTEST
#define NESocketWrapper NESocket
#endif

namespace NESocketWrapper
{
// OS specific methods

/**
* \brief OS specific socket initialization. Required in Win32 to initialize resources.
* \return Returns true if initialization succeeded.
**/
bool _osInitSocket(void);

/**
* \brief OS specific socket release. Required in Win32 to release resources.
*/
void _osReleaseSocket(void);

/**
* \brief OS specific socket close.
*/
void _osCloseSocket(SOCKETHANDLE hSocket);

/**
* \brief OS specific send data implementation. All checkups and validations should
* be done before calling the method.
* \return Returns number of bytes sent via network.
*/
int _osSendData(SOCKETHANDLE hSocket, const unsigned char* dataBuffer, int dataLength, int blockMaxSize);

/**
* \brief OS specific receive data implementation. All checkups and validations should
* be done before calling the method.
* \return Returns number of bytes received via network.
*/
int _osRecvData(SOCKETHANDLE hSocket, unsigned char* dataBuffer, int dataLength, int blockMaxSize);

/**
* \brief OS specific implementation of socket control call.
* \return Returns true if operation succeeded.
*/
bool _osControl(SOCKETHANDLE hSocket, int cmd, unsigned long & arg);

/**
* \brief OS specific implementation of retrieving socket option.
* On output the 'value' indicates the value of the option,
* which is valid only if function returns true.
*/
bool _osGetOption(SOCKETHANDLE hSocket, int level, int name, unsigned long & value);
}

/**
* \brief NESocket namespace is a wrapper of basic socket functionalities
* like create and close socket, connect client and server, accept
Expand Down Expand Up @@ -372,7 +424,10 @@ namespace NESocket
* \return Returns true if socket is initialized and socket function might be called
* in the current process.
**/
AREG_API bool socketInitialize( void );
inline AREG_API bool socketInitialize( void )
{
return NESocketWrapper::_osInitSocket();
}

/**
* \brief NESocket::socketRelease
Expand Down
53 changes: 0 additions & 53 deletions framework/areg/base/private/NESocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,54 +37,6 @@

#include <utility>

namespace NESocket
{
// OS specific methods

/**
* \brief OS specific socket initialization. Required in Win32 to initialize resources.
* \return Returns true if initialization succeeded.
**/
bool _osInitSocket(void);

/**
* \brief OS specific socket release. Required in Win32 to release resources.
*/
void _osReleaseSocket(void);

/**
* \brief OS specific socket close.
*/
void _osCloseSocket(SOCKETHANDLE hSocket);

/**
* \brief OS specific send data implementation. All checkups and validations should
* be done before calling the method.
* \return Returns number of bytes sent via network.
*/
int _osSendData(SOCKETHANDLE hSocket, const unsigned char* dataBuffer, int dataLength, int blockMaxSize);

/**
* \brief OS specific receive data implementation. All checkups and validations should
* be done before calling the method.
* \return Returns number of bytes received via network.
*/
int _osRecvData(SOCKETHANDLE hSocket, unsigned char* dataBuffer, int dataLength, int blockMaxSize);

/**
* \brief OS specific implementation of socket control call.
* \return Returns true if operation succeeded.
*/
bool _osControl(SOCKETHANDLE hSocket, int cmd, unsigned long & arg);

/**
* \brief OS specific implementation of retrieving socket option.
* On output the 'value' indicates the value of the option,
* which is valid only if function returns true.
*/
bool _osGetOption(SOCKETHANDLE hSocket, int level, int name, unsigned long & value);
}

DEF_LOG_SCOPE(areg_base_NESocket_clientSocketConnect);
DEF_LOG_SCOPE(areg_base_NESocket_serverSocketConnect);
DEF_LOG_SCOPE(areg_base_NESocket_serverAcceptConnection);
Expand Down Expand Up @@ -706,11 +658,6 @@ AREG_API_IMPL unsigned int NESocket::pendingRead(SOCKETHANDLE hSocket)
return (isSocketHandleValid(hSocket) && _osControl(hSocket, FIONREAD, result) ? static_cast<unsigned int>(result) : 0);
}

AREG_API_IMPL bool NESocket::socketInitialize(void)
{
return _osInitSocket();
}

AREG_API_IMPL void NESocket::socketRelease(void)
{
_osReleaseSocket();
Expand Down
4 changes: 3 additions & 1 deletion tests/units/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ macro(macro_add_unit_test test_project)
list(APPEND _tests "${AREG_UNIT_TEST_BASE}/${item}")
endforeach()

list(APPEND _libs "GTest::gtest_main" "GTest::gtest")
list(APPEND _libs "GTest::gtest_main" "GTest::gtest" "GTest::gmock")
addExecutableEx(${test_project} "" "${_tests}" "${_libs}")
target_compile_definitions(${test_project} PRIVATE UNITTEST=1)
gtest_discover_tests(${test_project} DISCOVERY_TIMEOUT 60)

unset(_tests)
Expand All @@ -50,4 +51,5 @@ macro_add_unit_test("${AREG_UNIT_TEST_PROJECT}"
TERingStackTest.cpp
TESortedLinkedListTest.cpp
TEStackTest.cpp
NESocketTest.cpp
)
54 changes: 54 additions & 0 deletions tests/units/NESocketTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "units/GUnitTest.hpp"
#include "areg/base/NESocket.hpp"
#include <gmock/gmock.h>

#include <memory>


namespace
{
class MockSocket
{
public:
MOCK_METHOD(bool, _osInitSocket, ());
};

std::unique_ptr<MockSocket> mockSocket {nullptr};
}

namespace NESocketWrapper
{
bool _osInitSocket()
{
if (mockSocket)
{
return mockSocket->_osInitSocket();
}
return false;
}
}

class NESocketTest : public testing::Test
{
public:
void SetUp() override;
void TearDown() override;
};

void NESocketTest::SetUp()
{
mockSocket = std::make_unique<MockSocket>();
}

void NESocketTest::TearDown()
{
mockSocket.reset();
}


TEST_F(NESocketTest, testInitSocket)
{
EXPECT_CALL(*mockSocket, _osInitSocket).WillOnce(testing::Return(true));
NESocket::socketInitialize();
}

Loading