123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- /*
- * Author: Patrick-Christopher Mattulat
- * Company: Lynar Studios
- * E-Mail: webmaster@lynarstudios.com
- * Created: 2020-11-16
- * Changed: 2022-12-28
- *
- * */
- #include <ls_std/ls_std_os.hpp>
- #include <gtest/gtest.h>
- #include <ls_std/ls_std_network.hpp>
- #include <ls_std/ls_std_core.hpp>
- #include <ls_std_core_test.hpp>
- #include <ls_std_common_test.hpp>
- using namespace ls::std::network;
- using namespace ::testing;
- using namespace ::std;
- using namespace ls::std::core;
- using namespace ls::std::core::type;
- using namespace ls_std_core_test;
- using namespace ls_std_common_test;
- namespace
- {
- class SocketTest : public ::testing::Test
- {
- protected:
- SocketTest() = default;
- ~SocketTest() override = default;
- void SetUp() override
- {}
- void TearDown() override
- {}
- static SocketParameter generateSocketParameter()
- {
- SocketParameter socketParameter{};
- socketParameter.protocolFamilyType = ProtocolFamilyType::LS_STD_PROTOCOL_FAMILY_TYPE_IPV4;
- SocketAddress socketAddress{};
- socketAddress.protocolType = ProtocolType::LS_STD_PROTOCOL_TYPE_TCP;
- socketAddress.ipAddress = "127.0.0.1";
- socketAddress.port = 2220;
- socketParameter.socketAddress = socketAddress;
- return socketParameter;
- }
- };
- TEST_F(SocketTest, getClassName)
- {
- SocketParameter parameter = generateSocketParameter();
- #if LS_STD_UNIX_PLATFORM
- shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
- parameter.posixSocket = mockSocket;
- EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
- ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
- #endif
- ASSERT_STREQ("Socket", Socket{parameter}.getClassName().c_str());
- }
- TEST_F(SocketTest, read)
- {
- SocketParameter parameter = generateSocketParameter();
- #if LS_STD_UNIX_PLATFORM
- shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
- shared_ptr<MockPosixReader> mockReader = make_shared<MockPosixReader>();
- parameter.posixSocket = mockSocket;
- parameter.posixReader = mockReader;
- EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockReader, read(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockReader, read(_, _, _)).WillByDefault(Return(1));
- EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
- ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
- #endif
- parameter.readBufferSize = 32;
- Socket socket{parameter};
- ASSERT_FALSE(socket.read().empty());
- }
- TEST_F(SocketTest, read_no_buffer_size_set)
- {
- SocketParameter parameter = generateSocketParameter();
- #if LS_STD_UNIX_PLATFORM
- shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
- parameter.posixSocket = mockSocket;
- EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
- ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
- #endif
- Socket socket{parameter};
- EXPECT_THROW({
- try
- {
- byte_field data = socket.read();
- }
- catch (const IllegalArgumentException &_exception)
- {
- throw;
- }
- }, IllegalArgumentException);
- }
- TEST_F(SocketTest, read_api_call_failed)
- {
- SocketParameter parameter = generateSocketParameter();
- #if LS_STD_UNIX_PLATFORM
- shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
- shared_ptr<MockPosixReader> mockReader = make_shared<MockPosixReader>();
- parameter.posixSocket = mockSocket;
- parameter.posixReader = mockReader;
- EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockReader, read(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockReader, read(_, _, _)).WillByDefault(Return(-1));
- EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
- ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
- #endif
- parameter.readBufferSize = 32;
- Socket socket{parameter};
- EXPECT_THROW({
- try
- {
- byte_field data = socket.read();
- }
- catch (const FileOperationException &_exception)
- {
- throw;
- }
- }, FileOperationException);
- }
- TEST_F(SocketTest, write)
- {
- SocketParameter parameter = generateSocketParameter();
- #if LS_STD_UNIX_PLATFORM
- shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
- shared_ptr<MockPosixWriter> mockWriter = make_shared<MockPosixWriter>();
- parameter.posixSocket = mockSocket;
- parameter.posixWriter = mockWriter;
- EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockWriter, write(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockWriter, write(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
- ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
- #endif
- Socket socket{parameter};
- ASSERT_TRUE(socket.write("Hello Server!"));
- }
- TEST_F(SocketTest, accept)
- {
- SocketParameter parameter = generateSocketParameter();
- #if LS_STD_UNIX_PLATFORM
- shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
- parameter.posixSocket = mockSocket;
- EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockSocket, accept(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, accept(_, _, _)).WillByDefault(Return(15));
- EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
- ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
- #endif
- Socket socket{parameter};
- ASSERT_EQ(2, socket.accept());
- }
- TEST_F(SocketTest, accept_wrong_protocol)
- {
- SocketParameter parameter = generateSocketParameter();
- parameter.socketAddress.protocolType = LS_STD_PROTOCOL_TYPE_UDP;
- #if LS_STD_UNIX_PLATFORM
- shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
- parameter.posixSocket = mockSocket;
- EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
- ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
- #endif
- Socket socket{parameter};
- EXPECT_THROW({
- try
- {
- connection_id listened = socket.accept();
- }
- catch (const WrongProtocolException &_exception)
- {
- throw;
- }
- }, WrongProtocolException);
- }
- TEST_F(SocketTest, accept_not_accepted)
- {
- SocketParameter parameter = generateSocketParameter();
- #if LS_STD_UNIX_PLATFORM
- shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
- parameter.posixSocket = mockSocket;
- EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockSocket, accept(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, accept(_, _, _)).WillByDefault(Return(-1));
- EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
- ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
- #endif
- Socket socket{parameter};
- EXPECT_THROW({
- try
- {
- connection_id listened = socket.accept();
- }
- catch (const SocketOperationFailedException &_exception)
- {
- throw;
- }
- }, SocketOperationFailedException);
- }
- TEST_F(SocketTest, bind)
- {
- SocketParameter parameter = generateSocketParameter();
- #if LS_STD_UNIX_PLATFORM
- shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
- parameter.posixSocket = mockSocket;
- EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockSocket, bind(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, bind(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
- ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
- #endif
- Socket socket{parameter};
- ASSERT_TRUE(socket.bind());
- }
- TEST_F(SocketTest, close)
- {
- SocketParameter parameter = generateSocketParameter();
- #if LS_STD_UNIX_PLATFORM
- shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
- parameter.posixSocket = mockSocket;
- EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
- ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
- #endif
- Socket socket{parameter};
- ASSERT_TRUE(socket.close());
- }
- TEST_F(SocketTest, connect)
- {
- SocketParameter parameter = generateSocketParameter();
- #if LS_STD_UNIX_PLATFORM
- shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
- parameter.posixSocket = mockSocket;
- EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockSocket, connect(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, connect(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
- ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
- #endif
- Socket socket{parameter};
- ASSERT_TRUE(socket.connect());
- }
- TEST_F(SocketTest, handle)
- {
- SocketParameter parameter = generateSocketParameter();
- #if LS_STD_UNIX_PLATFORM
- shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
- parameter.posixSocket = mockSocket;
- EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockSocket, accept(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, accept(_, _, _)).WillByDefault(Return(5));
- EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
- ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
- #endif
- Socket socket{parameter};
- connection_id acceptedConnection = socket.accept();
- ASSERT_TRUE(socket.handle(acceptedConnection));
- }
- TEST_F(SocketTest, handle_no_accepted_connection)
- {
- SocketParameter parameter = generateSocketParameter();
- #if LS_STD_UNIX_PLATFORM
- shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
- parameter.posixSocket = mockSocket;
- EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
- ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
- #endif
- Socket socket{parameter};
- ASSERT_FALSE(socket.handle(13));
- }
- TEST_F(SocketTest, handleV2)
- {
- SocketParameter parameter = generateSocketParameter();
- #if LS_STD_UNIX_PLATFORM
- shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
- parameter.posixSocket = mockSocket;
- EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
- ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
- #endif
- Socket socket{parameter};
- ASSERT_TRUE(socket.handle());
- }
- TEST_F(SocketTest, isInitialized)
- {
- SocketParameter parameter = generateSocketParameter();
- #if LS_STD_UNIX_PLATFORM
- shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
- parameter.posixSocket = mockSocket;
- EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
- ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
- #endif
- Socket socket{parameter};
- ASSERT_TRUE(socket.isInitialized());
- }
- TEST_F(SocketTest, listen)
- {
- SocketParameter parameter = generateSocketParameter();
- #if LS_STD_UNIX_PLATFORM
- shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
- parameter.posixSocket = mockSocket;
- EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockSocket, listen(_, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, listen(_, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
- ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
- #endif
- Socket socket{parameter};
- ASSERT_TRUE(socket.listen());
- }
- TEST_F(SocketTest, listen_wrong_protocol)
- {
- SocketParameter parameter = generateSocketParameter();
- parameter.socketAddress.protocolType = LS_STD_PROTOCOL_TYPE_UDP;
- #if LS_STD_UNIX_PLATFORM
- shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
- parameter.posixSocket = mockSocket;
- EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
- ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
- EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
- ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
- #endif
- Socket socket{parameter};
- EXPECT_THROW({
- try
- {
- bool listened = socket.listen();
- }
- catch (const WrongProtocolException &_exception)
- {
- throw;
- }
- }, WrongProtocolException);
- }
- }
|