SocketTest.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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-28
  7. *
  8. * */
  9. #include <ls_std/ls_std_os.hpp>
  10. #include <gtest/gtest.h>
  11. #include <ls_std/ls_std_network.hpp>
  12. #include <ls_std/ls_std_core.hpp>
  13. #include <ls_std_core_test.hpp>
  14. #include <ls_std_common_test.hpp>
  15. using namespace ls::std::network;
  16. using namespace ::testing;
  17. using namespace ::std;
  18. using namespace ls::std::core;
  19. using namespace ls::std::core::type;
  20. using namespace ls_std_core_test;
  21. using namespace ls_std_common_test;
  22. namespace
  23. {
  24. class SocketTest : public ::testing::Test
  25. {
  26. protected:
  27. SocketTest() = default;
  28. ~SocketTest() override = default;
  29. void SetUp() override
  30. {}
  31. void TearDown() override
  32. {}
  33. static SocketParameter generateSocketParameter()
  34. {
  35. SocketParameter socketParameter{};
  36. socketParameter.protocolFamilyType = ProtocolFamilyType::LS_STD_PROTOCOL_FAMILY_TYPE_IPV4;
  37. SocketAddress socketAddress{};
  38. socketAddress.protocolType = ProtocolType::LS_STD_PROTOCOL_TYPE_TCP;
  39. socketAddress.ipAddress = "127.0.0.1";
  40. socketAddress.port = 2220;
  41. socketParameter.socketAddress = socketAddress;
  42. return socketParameter;
  43. }
  44. };
  45. TEST_F(SocketTest, getClassName)
  46. {
  47. SocketParameter parameter = generateSocketParameter();
  48. #if LS_STD_UNIX_PLATFORM
  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, close(_)).Times(AtLeast(1));
  54. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  55. #endif
  56. ASSERT_STREQ("Socket", Socket{parameter}.getClassName().c_str());
  57. }
  58. TEST_F(SocketTest, read)
  59. {
  60. SocketParameter parameter = generateSocketParameter();
  61. #if LS_STD_UNIX_PLATFORM
  62. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  63. shared_ptr<MockPosixReader> mockReader = make_shared<MockPosixReader>();
  64. parameter.posixSocket = mockSocket;
  65. parameter.posixReader = mockReader;
  66. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  67. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  68. EXPECT_CALL(*mockReader, read(_, _, _)).Times(AtLeast(1));
  69. ON_CALL(*mockReader, read(_, _, _)).WillByDefault(Return(1));
  70. EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
  71. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  72. #endif
  73. parameter.readBufferSize = 32;
  74. Socket socket{parameter};
  75. ASSERT_FALSE(socket.read().empty());
  76. }
  77. TEST_F(SocketTest, read_no_buffer_size_set)
  78. {
  79. SocketParameter parameter = generateSocketParameter();
  80. #if LS_STD_UNIX_PLATFORM
  81. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  82. parameter.posixSocket = mockSocket;
  83. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  84. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  85. EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
  86. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  87. #endif
  88. Socket socket{parameter};
  89. EXPECT_THROW({
  90. try
  91. {
  92. byte_field data = socket.read();
  93. }
  94. catch (const IllegalArgumentException &_exception)
  95. {
  96. throw;
  97. }
  98. }, IllegalArgumentException);
  99. }
  100. TEST_F(SocketTest, read_api_call_failed)
  101. {
  102. SocketParameter parameter = generateSocketParameter();
  103. #if LS_STD_UNIX_PLATFORM
  104. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  105. shared_ptr<MockPosixReader> mockReader = make_shared<MockPosixReader>();
  106. parameter.posixSocket = mockSocket;
  107. parameter.posixReader = mockReader;
  108. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  109. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  110. EXPECT_CALL(*mockReader, read(_, _, _)).Times(AtLeast(1));
  111. ON_CALL(*mockReader, read(_, _, _)).WillByDefault(Return(-1));
  112. EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
  113. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  114. #endif
  115. parameter.readBufferSize = 32;
  116. Socket socket{parameter};
  117. EXPECT_THROW({
  118. try
  119. {
  120. byte_field data = socket.read();
  121. }
  122. catch (const FileOperationException &_exception)
  123. {
  124. throw;
  125. }
  126. }, FileOperationException);
  127. }
  128. TEST_F(SocketTest, write)
  129. {
  130. SocketParameter parameter = generateSocketParameter();
  131. #if LS_STD_UNIX_PLATFORM
  132. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  133. shared_ptr<MockPosixWriter> mockWriter = make_shared<MockPosixWriter>();
  134. parameter.posixSocket = mockSocket;
  135. parameter.posixWriter = mockWriter;
  136. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  137. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  138. EXPECT_CALL(*mockWriter, write(_, _, _)).Times(AtLeast(1));
  139. ON_CALL(*mockWriter, write(_, _, _)).WillByDefault(Return(0));
  140. EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
  141. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  142. #endif
  143. Socket socket{parameter};
  144. ASSERT_TRUE(socket.write("Hello Server!"));
  145. }
  146. TEST_F(SocketTest, accept)
  147. {
  148. SocketParameter parameter = generateSocketParameter();
  149. #if LS_STD_UNIX_PLATFORM
  150. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  151. parameter.posixSocket = mockSocket;
  152. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  153. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  154. EXPECT_CALL(*mockSocket, accept(_, _, _)).Times(AtLeast(1));
  155. ON_CALL(*mockSocket, accept(_, _, _)).WillByDefault(Return(15));
  156. EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
  157. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  158. #endif
  159. Socket socket{parameter};
  160. ASSERT_EQ(2, socket.accept());
  161. }
  162. TEST_F(SocketTest, accept_wrong_protocol)
  163. {
  164. SocketParameter parameter = generateSocketParameter();
  165. parameter.socketAddress.protocolType = LS_STD_PROTOCOL_TYPE_UDP;
  166. #if LS_STD_UNIX_PLATFORM
  167. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  168. parameter.posixSocket = mockSocket;
  169. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  170. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  171. EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
  172. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  173. #endif
  174. Socket socket{parameter};
  175. EXPECT_THROW({
  176. try
  177. {
  178. connection_id listened = socket.accept();
  179. }
  180. catch (const WrongProtocolException &_exception)
  181. {
  182. throw;
  183. }
  184. }, WrongProtocolException);
  185. }
  186. TEST_F(SocketTest, accept_not_accepted)
  187. {
  188. SocketParameter parameter = generateSocketParameter();
  189. #if LS_STD_UNIX_PLATFORM
  190. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  191. parameter.posixSocket = mockSocket;
  192. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  193. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  194. EXPECT_CALL(*mockSocket, accept(_, _, _)).Times(AtLeast(1));
  195. ON_CALL(*mockSocket, accept(_, _, _)).WillByDefault(Return(-1));
  196. EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
  197. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  198. #endif
  199. Socket socket{parameter};
  200. EXPECT_THROW({
  201. try
  202. {
  203. connection_id listened = socket.accept();
  204. }
  205. catch (const SocketOperationFailedException &_exception)
  206. {
  207. throw;
  208. }
  209. }, SocketOperationFailedException);
  210. }
  211. TEST_F(SocketTest, bind)
  212. {
  213. SocketParameter parameter = generateSocketParameter();
  214. #if LS_STD_UNIX_PLATFORM
  215. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  216. parameter.posixSocket = mockSocket;
  217. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  218. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  219. EXPECT_CALL(*mockSocket, bind(_, _, _)).Times(AtLeast(1));
  220. ON_CALL(*mockSocket, bind(_, _, _)).WillByDefault(Return(0));
  221. EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
  222. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  223. #endif
  224. Socket socket{parameter};
  225. ASSERT_TRUE(socket.bind());
  226. }
  227. TEST_F(SocketTest, close)
  228. {
  229. SocketParameter parameter = generateSocketParameter();
  230. #if LS_STD_UNIX_PLATFORM
  231. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  232. parameter.posixSocket = mockSocket;
  233. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  234. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  235. EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
  236. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  237. #endif
  238. Socket socket{parameter};
  239. ASSERT_TRUE(socket.close());
  240. }
  241. TEST_F(SocketTest, connect)
  242. {
  243. SocketParameter parameter = generateSocketParameter();
  244. #if LS_STD_UNIX_PLATFORM
  245. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  246. parameter.posixSocket = mockSocket;
  247. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  248. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  249. EXPECT_CALL(*mockSocket, connect(_, _, _)).Times(AtLeast(1));
  250. ON_CALL(*mockSocket, connect(_, _, _)).WillByDefault(Return(0));
  251. EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
  252. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  253. #endif
  254. Socket socket{parameter};
  255. ASSERT_TRUE(socket.connect());
  256. }
  257. TEST_F(SocketTest, handle)
  258. {
  259. SocketParameter parameter = generateSocketParameter();
  260. #if LS_STD_UNIX_PLATFORM
  261. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  262. parameter.posixSocket = mockSocket;
  263. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  264. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  265. EXPECT_CALL(*mockSocket, accept(_, _, _)).Times(AtLeast(1));
  266. ON_CALL(*mockSocket, accept(_, _, _)).WillByDefault(Return(5));
  267. EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
  268. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  269. #endif
  270. Socket socket{parameter};
  271. connection_id acceptedConnection = socket.accept();
  272. ASSERT_TRUE(socket.handle(acceptedConnection));
  273. }
  274. TEST_F(SocketTest, handle_no_accepted_connection)
  275. {
  276. SocketParameter parameter = generateSocketParameter();
  277. #if LS_STD_UNIX_PLATFORM
  278. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  279. parameter.posixSocket = mockSocket;
  280. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  281. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  282. EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
  283. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  284. #endif
  285. Socket socket{parameter};
  286. ASSERT_FALSE(socket.handle(13));
  287. }
  288. TEST_F(SocketTest, handleV2)
  289. {
  290. SocketParameter parameter = generateSocketParameter();
  291. #if LS_STD_UNIX_PLATFORM
  292. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  293. parameter.posixSocket = mockSocket;
  294. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  295. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  296. EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
  297. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  298. #endif
  299. Socket socket{parameter};
  300. ASSERT_TRUE(socket.handle());
  301. }
  302. TEST_F(SocketTest, isInitialized)
  303. {
  304. SocketParameter parameter = generateSocketParameter();
  305. #if LS_STD_UNIX_PLATFORM
  306. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  307. parameter.posixSocket = mockSocket;
  308. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  309. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  310. EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
  311. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  312. #endif
  313. Socket socket{parameter};
  314. ASSERT_TRUE(socket.isInitialized());
  315. }
  316. TEST_F(SocketTest, listen)
  317. {
  318. SocketParameter parameter = generateSocketParameter();
  319. #if LS_STD_UNIX_PLATFORM
  320. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  321. parameter.posixSocket = mockSocket;
  322. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  323. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  324. EXPECT_CALL(*mockSocket, listen(_, _)).Times(AtLeast(1));
  325. ON_CALL(*mockSocket, listen(_, _)).WillByDefault(Return(0));
  326. EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
  327. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  328. #endif
  329. Socket socket{parameter};
  330. ASSERT_TRUE(socket.listen());
  331. }
  332. TEST_F(SocketTest, listen_wrong_protocol)
  333. {
  334. SocketParameter parameter = generateSocketParameter();
  335. parameter.socketAddress.protocolType = LS_STD_PROTOCOL_TYPE_UDP;
  336. #if LS_STD_UNIX_PLATFORM
  337. shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
  338. parameter.posixSocket = mockSocket;
  339. EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
  340. ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
  341. EXPECT_CALL(*mockSocket, close(_)).Times(AtLeast(1));
  342. ON_CALL(*mockSocket, close(_)).WillByDefault(Return(0));
  343. #endif
  344. Socket socket{parameter};
  345. EXPECT_THROW({
  346. try
  347. {
  348. bool listened = socket.listen();
  349. }
  350. catch (const WrongProtocolException &_exception)
  351. {
  352. throw;
  353. }
  354. }, WrongProtocolException);
  355. }
  356. }