SocketTest.cpp 6.0 KB

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