SocketTest.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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-25
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std_network.hpp>
  11. #include <ls_std/ls_std_core.hpp>
  12. #include <ls_std_core_test.hpp>
  13. using namespace ls::std::network;
  14. using namespace ::testing;
  15. using namespace ::std;
  16. using namespace ls::std::core;
  17. using namespace ls::std::core::type;
  18. using namespace ls_std_core_test;
  19. namespace
  20. {
  21. class SocketTest : public ::testing::Test
  22. {
  23. protected:
  24. SocketTest() = default;
  25. ~SocketTest() override = default;
  26. void SetUp() override
  27. {}
  28. void TearDown() override
  29. {}
  30. static SocketParameter generateSocketParameter()
  31. {
  32. SocketParameter socketParameter{};
  33. socketParameter.protocolFamilyType = ProtocolFamilyType::PROTOCOL_FAMILY_TYPE_IPV4;
  34. SocketAddress socketAddress{};
  35. socketAddress.protocolType = ProtocolType::PROTOCOL_TYPE_TCP;
  36. socketAddress.ipAddress = "127.0.0.1";
  37. socketAddress.port = 2220;
  38. socketParameter.socketAddress = socketAddress;
  39. return socketParameter;
  40. }
  41. };
  42. TEST_F(SocketTest, getClassName)
  43. {
  44. ASSERT_STREQ("Socket", Socket{generateSocketParameter()}.getClassName().c_str());
  45. }
  46. TEST_F(SocketTest, read)
  47. {
  48. SocketParameter parameter = generateSocketParameter();
  49. #if defined(unix) || defined(__APPLE__)
  50. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  51. shared_ptr<MockPosixReader> mockReader = make_shared<MockPosixReader>();
  52. parameter.posixSocket = mockSocket;
  53. parameter.posixReader = mockReader;
  54. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  55. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  56. EXPECT_CALL(*mockReader, read(_, _, _)).Times(AtLeast(1));
  57. ON_CALL(*mockReader, read(_, _, _)).WillByDefault(Return(1));
  58. #endif
  59. parameter.readBufferSize = 32;
  60. Socket socket{parameter};
  61. ASSERT_FALSE(socket.read().empty());
  62. }
  63. TEST_F(SocketTest, read_no_buffer_size_set)
  64. {
  65. SocketParameter parameter = generateSocketParameter();
  66. #if defined(unix) || defined(__APPLE__)
  67. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  68. parameter.posixSocket = mockSocket;
  69. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  70. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  71. #endif
  72. Socket socket{parameter};
  73. EXPECT_THROW({
  74. try
  75. {
  76. byte_field data = socket.read();
  77. }
  78. catch (const IllegalArgumentException &_exception)
  79. {
  80. throw;
  81. }
  82. }, IllegalArgumentException);
  83. }
  84. TEST_F(SocketTest, accept)
  85. {
  86. SocketParameter parameter = generateSocketParameter();
  87. #if defined(unix) || defined(__APPLE__)
  88. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  89. parameter.posixSocket = mockSocket;
  90. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  91. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  92. EXPECT_CALL(*mockSocket, accept(_, _, _)).Times(AtLeast(1));
  93. ON_CALL(*mockSocket, accept(_, _, _)).WillByDefault(Return(0));
  94. #endif
  95. Socket socket{parameter};
  96. ASSERT_TRUE(socket.accept());
  97. }
  98. TEST_F(SocketTest, accept_wrong_protocol)
  99. {
  100. SocketParameter parameter = generateSocketParameter();
  101. parameter.socketAddress.protocolType = PROTOCOL_TYPE_UDP;
  102. #if defined(unix) || defined(__APPLE__)
  103. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  104. parameter.posixSocket = mockSocket;
  105. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  106. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  107. #endif
  108. Socket socket{parameter};
  109. EXPECT_THROW({
  110. try
  111. {
  112. bool listened = socket.accept();
  113. }
  114. catch (const WrongProtocolException &_exception)
  115. {
  116. throw;
  117. }
  118. }, WrongProtocolException);
  119. }
  120. TEST_F(SocketTest, bind)
  121. {
  122. SocketParameter parameter = generateSocketParameter();
  123. #if defined(unix) || defined(__APPLE__)
  124. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  125. parameter.posixSocket = mockSocket;
  126. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  127. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  128. EXPECT_CALL(*mockSocket, bind(_, _, _)).Times(AtLeast(1));
  129. ON_CALL(*mockSocket, bind(_, _, _)).WillByDefault(Return(0));
  130. #endif
  131. Socket socket{parameter};
  132. ASSERT_TRUE(socket.bind());
  133. }
  134. TEST_F(SocketTest, close)
  135. {
  136. SocketParameter parameter = generateSocketParameter();
  137. #if defined(unix) || defined(__APPLE__)
  138. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  139. parameter.posixSocket = mockSocket;
  140. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  141. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  142. EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
  143. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  144. #endif
  145. Socket socket{parameter};
  146. ASSERT_TRUE(socket.close());
  147. }
  148. TEST_F(SocketTest, connect)
  149. {
  150. SocketParameter parameter = generateSocketParameter();
  151. #if defined(unix) || defined(__APPLE__)
  152. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  153. parameter.posixSocket = mockSocket;
  154. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  155. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  156. EXPECT_CALL(*mockSocket, connect(_, _, _)).Times(AtLeast(1));
  157. ON_CALL(*mockSocket, connect(_, _, _)).WillByDefault(Return(0));
  158. #endif
  159. Socket socket{parameter};
  160. ASSERT_TRUE(socket.connect());
  161. }
  162. TEST_F(SocketTest, isInitialized)
  163. {
  164. Socket socket{generateSocketParameter()};
  165. ASSERT_TRUE(socket.isInitialized());
  166. }
  167. TEST_F(SocketTest, listen)
  168. {
  169. SocketParameter parameter = generateSocketParameter();
  170. #if defined(unix) || defined(__APPLE__)
  171. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  172. parameter.posixSocket = mockSocket;
  173. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  174. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  175. EXPECT_CALL(*mockSocket, listen(_, _)).Times(AtLeast(1));
  176. ON_CALL(*mockSocket, listen(_, _)).WillByDefault(Return(0));
  177. #endif
  178. Socket socket{parameter};
  179. ASSERT_TRUE(socket.listen());
  180. }
  181. TEST_F(SocketTest, listen_wrong_protocol)
  182. {
  183. SocketParameter parameter = generateSocketParameter();
  184. parameter.socketAddress.protocolType = PROTOCOL_TYPE_UDP;
  185. #if defined(unix) || defined(__APPLE__)
  186. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  187. parameter.posixSocket = mockSocket;
  188. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  189. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  190. #endif
  191. Socket socket{parameter};
  192. EXPECT_THROW({
  193. try
  194. {
  195. bool listened = socket.listen();
  196. }
  197. catch (const WrongProtocolException &_exception)
  198. {
  199. throw;
  200. }
  201. }, WrongProtocolException);
  202. }
  203. }