SocketTest.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-16
  6. * Changed: 2022-12-12
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std_network.hpp>
  11. using namespace ls::std::network;
  12. using namespace ::testing;
  13. using namespace ls_std_network_test;
  14. using namespace ::std;
  15. using namespace ls::std::core;
  16. namespace
  17. {
  18. class SocketTest : public ::testing::Test
  19. {
  20. protected:
  21. SocketTest() = default;
  22. ~SocketTest() override = default;
  23. void SetUp() override
  24. {}
  25. void TearDown() override
  26. {}
  27. static SocketParameter generateSocketParameter()
  28. {
  29. SocketParameter socketParameter{};
  30. socketParameter.protocolFamilyType = ProtocolFamilyType::PROTOCOL_FAMILY_TYPE_IPV4;
  31. SocketAddress socketAddress{};
  32. socketAddress.protocolType = ProtocolType::PROTOCOL_TYPE_TCP;
  33. socketAddress.ipAddress = "127.0.0.1";
  34. socketAddress.port = 2220;
  35. socketParameter.socketAddress = socketAddress;
  36. return socketParameter;
  37. }
  38. };
  39. TEST_F(SocketTest, getClassName)
  40. {
  41. ASSERT_STREQ("Socket", Socket{generateSocketParameter()}.getClassName().c_str());
  42. }
  43. TEST_F(SocketTest, accept)
  44. {
  45. SocketParameter parameter = generateSocketParameter();
  46. #if defined(unix) || defined(__APPLE__)
  47. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  48. parameter.posixSocket = mockSocket;
  49. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  50. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  51. EXPECT_CALL(*mockSocket, accept(_, _, _)).Times(AtLeast(1));
  52. ON_CALL(*mockSocket, accept(_, _, _)).WillByDefault(Return(0));
  53. #endif
  54. Socket socket{parameter};
  55. ASSERT_TRUE(socket.accept());
  56. }
  57. TEST_F(SocketTest, accept_wrong_protocol)
  58. {
  59. SocketParameter parameter = generateSocketParameter();
  60. parameter.socketAddress.protocolType = PROTOCOL_TYPE_UDP;
  61. #if defined(unix) || defined(__APPLE__)
  62. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  63. parameter.posixSocket = mockSocket;
  64. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  65. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  66. #endif
  67. Socket socket{parameter};
  68. EXPECT_THROW({
  69. try
  70. {
  71. bool listened = socket.accept();
  72. }
  73. catch (const WrongProtocolException &_exception)
  74. {
  75. throw;
  76. }
  77. }, WrongProtocolException);
  78. }
  79. TEST_F(SocketTest, bind)
  80. {
  81. SocketParameter parameter = generateSocketParameter();
  82. #if defined(unix) || defined(__APPLE__)
  83. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  84. parameter.posixSocket = mockSocket;
  85. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  86. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  87. EXPECT_CALL(*mockSocket, bind(_, _, _)).Times(AtLeast(1));
  88. ON_CALL(*mockSocket, bind(_, _, _)).WillByDefault(Return(0));
  89. #endif
  90. Socket socket{parameter};
  91. ASSERT_TRUE(socket.bind());
  92. }
  93. TEST_F(SocketTest, close)
  94. {
  95. SocketParameter parameter = generateSocketParameter();
  96. #if defined(unix) || defined(__APPLE__)
  97. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  98. parameter.posixSocket = mockSocket;
  99. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  100. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  101. EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
  102. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  103. #endif
  104. Socket socket{parameter};
  105. ASSERT_TRUE(socket.close());
  106. }
  107. TEST_F(SocketTest, connect)
  108. {
  109. SocketParameter parameter = generateSocketParameter();
  110. #if defined(unix) || defined(__APPLE__)
  111. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  112. parameter.posixSocket = mockSocket;
  113. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  114. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  115. EXPECT_CALL(*mockSocket, connect(_, _, _)).Times(AtLeast(1));
  116. ON_CALL(*mockSocket, connect(_, _, _)).WillByDefault(Return(0));
  117. #endif
  118. Socket socket{parameter};
  119. ASSERT_TRUE(socket.connect());
  120. }
  121. TEST_F(SocketTest, isInitialized)
  122. {
  123. Socket socket{generateSocketParameter()};
  124. ASSERT_TRUE(socket.isInitialized());
  125. }
  126. TEST_F(SocketTest, listen)
  127. {
  128. SocketParameter parameter = generateSocketParameter();
  129. #if defined(unix) || defined(__APPLE__)
  130. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  131. parameter.posixSocket = mockSocket;
  132. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  133. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  134. EXPECT_CALL(*mockSocket, listen(_, _)).Times(AtLeast(1));
  135. ON_CALL(*mockSocket, listen(_, _)).WillByDefault(Return(0));
  136. #endif
  137. Socket socket{parameter};
  138. ASSERT_TRUE(socket.listen());
  139. }
  140. TEST_F(SocketTest, listen_wrong_protocol)
  141. {
  142. SocketParameter parameter = generateSocketParameter();
  143. parameter.socketAddress.protocolType = PROTOCOL_TYPE_UDP;
  144. #if defined(unix) || defined(__APPLE__)
  145. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  146. parameter.posixSocket = mockSocket;
  147. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  148. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  149. #endif
  150. Socket socket{parameter};
  151. EXPECT_THROW({
  152. try
  153. {
  154. bool listened = socket.listen();
  155. }
  156. catch (const WrongProtocolException &_exception)
  157. {
  158. throw;
  159. }
  160. }, WrongProtocolException);
  161. }
  162. }