sock_ut.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "sock.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <library/cpp/threading/future/legacy_future.h>
  4. #include <util/string/split.h>
  5. #include <util/system/fs.h>
  6. Y_UNIT_TEST_SUITE(TSocketTest) {
  7. Y_UNIT_TEST(InetDgramTest) {
  8. char buf[256];
  9. TSockAddrInetDgram servAddr(IpFromString("127.0.0.1"), 0);
  10. TSockAddrInetDgram cliAddr(IpFromString("127.0.0.1"), 0);
  11. TSockAddrInetDgram servFromAddr;
  12. TSockAddrInetDgram cliFromAddr;
  13. TInetDgramSocket cliSock;
  14. TInetDgramSocket servSock;
  15. cliSock.CheckSock();
  16. servSock.CheckSock();
  17. TBaseSocket::Check(cliSock.Bind(&cliAddr));
  18. TBaseSocket::Check(servSock.Bind(&servAddr));
  19. // client
  20. const char reqStr[] = "Hello, world!!!";
  21. TBaseSocket::Check(cliSock.SendTo(reqStr, sizeof(reqStr), &servAddr));
  22. // server
  23. TBaseSocket::Check(servSock.RecvFrom(buf, 256, &servFromAddr));
  24. UNIT_ASSERT(strcmp(reqStr, buf) == 0);
  25. const char repStr[] = "The World's greatings to you";
  26. TBaseSocket::Check(servSock.SendTo(repStr, sizeof(repStr), &servFromAddr));
  27. // client
  28. TBaseSocket::Check(cliSock.RecvFrom(buf, 256, &cliFromAddr));
  29. UNIT_ASSERT(strcmp(repStr, buf) == 0);
  30. }
  31. void RunLocalDgramTest(const char* localServerSockName, const char* localClientSockName) {
  32. char buf[256];
  33. TSockAddrLocalDgram servAddr(localServerSockName);
  34. TSockAddrLocalDgram cliAddr(localClientSockName);
  35. TSockAddrLocalDgram servFromAddr;
  36. TSockAddrLocalDgram cliFromAddr;
  37. TLocalDgramSocket cliSock;
  38. TLocalDgramSocket servSock;
  39. cliSock.CheckSock();
  40. servSock.CheckSock();
  41. TBaseSocket::Check(cliSock.Bind(&cliAddr), "bind client");
  42. TBaseSocket::Check(servSock.Bind(&servAddr), "bind server");
  43. // client
  44. const char reqStr[] = "Hello, world!!!";
  45. TBaseSocket::Check(cliSock.SendTo(reqStr, sizeof(reqStr), &servAddr), "send from client");
  46. // server
  47. TBaseSocket::Check(servSock.RecvFrom(buf, 256, &servFromAddr), "receive from client");
  48. UNIT_ASSERT(strcmp(reqStr, buf) == 0);
  49. const char repStr[] = "The World's greatings to you";
  50. TBaseSocket::Check(servSock.SendTo(repStr, sizeof(repStr), &servFromAddr), "send to client");
  51. // client
  52. TBaseSocket::Check(cliSock.RecvFrom(buf, 256, &cliFromAddr), "receive from server");
  53. UNIT_ASSERT(strcmp(repStr, buf) == 0);
  54. }
  55. Y_UNIT_TEST(LocalDgramTest) {
  56. const char* localServerSockName = "./serv_sock";
  57. const char* localClientSockName = "./cli_sock";
  58. RunLocalDgramTest(localServerSockName, localClientSockName);
  59. NFs::Remove(localServerSockName);
  60. NFs::Remove(localClientSockName);
  61. }
  62. template <class A, class S>
  63. void RunInetStreamTest(const char* ip) {
  64. char buf[256];
  65. A servAddr(ip, 0);
  66. A newAddr;
  67. S cliSock;
  68. S servSock;
  69. S newSock;
  70. cliSock.CheckSock();
  71. servSock.CheckSock();
  72. newSock.CheckSock();
  73. // server
  74. int yes = 1;
  75. CheckedSetSockOpt(servSock, SOL_SOCKET, SO_REUSEADDR, yes, "servSock, SO_REUSEADDR");
  76. TBaseSocket::Check(servSock.Bind(&servAddr), "bind");
  77. TBaseSocket::Check(servSock.Listen(10), "listen");
  78. // client
  79. TBaseSocket::Check(cliSock.Connect(&servAddr), "connect");
  80. // server
  81. TBaseSocket::Check(servSock.Accept(&newSock, &newAddr), "accept");
  82. // client
  83. const char reqStr[] = "Hello, world!!!";
  84. TBaseSocket::Check(cliSock.Send(reqStr, sizeof(reqStr)), "send");
  85. // server - new
  86. TBaseSocket::Check(newSock.Recv(buf, 256), "recv");
  87. UNIT_ASSERT(strcmp(reqStr, buf) == 0);
  88. const char repStr[] = "The World's greatings to you";
  89. TBaseSocket::Check(newSock.Send(repStr, sizeof(repStr)), "send");
  90. // client
  91. TBaseSocket::Check(cliSock.Recv(buf, 256), "recv");
  92. UNIT_ASSERT(strcmp(repStr, buf) == 0);
  93. }
  94. Y_UNIT_TEST(InetStreamTest) {
  95. RunInetStreamTest<TSockAddrInetStream, TInetStreamSocket>("127.0.0.1");
  96. }
  97. Y_UNIT_TEST(Inet6StreamTest) {
  98. RunInetStreamTest<TSockAddrInet6Stream, TInet6StreamSocket>("::1");
  99. }
  100. void RunLocalStreamTest(const char* localServerSockName) {
  101. char buf[256];
  102. TSockAddrLocalStream servAddr(localServerSockName);
  103. TSockAddrLocalStream newAddr;
  104. TLocalStreamSocket cliSock;
  105. TLocalStreamSocket servSock;
  106. TLocalStreamSocket newSock;
  107. cliSock.CheckSock();
  108. servSock.CheckSock();
  109. newSock.CheckSock();
  110. // server
  111. TBaseSocket::Check(servSock.Bind(&servAddr), "bind");
  112. TBaseSocket::Check(servSock.Listen(10), "listen");
  113. NThreading::TLegacyFuture<void> f([&]() {
  114. // server
  115. TBaseSocket::Check(servSock.Accept(&newSock, &newAddr), "accept");
  116. });
  117. // client
  118. TBaseSocket::Check(cliSock.Connect(&servAddr), "connect");
  119. f.Get();
  120. // client
  121. const char reqStr[] = "Hello, world!!!";
  122. TBaseSocket::Check(cliSock.Send(reqStr, sizeof(reqStr)), "send");
  123. // server - new
  124. TBaseSocket::Check(newSock.Recv(buf, 256), "recv");
  125. UNIT_ASSERT(strcmp(reqStr, buf) == 0);
  126. const char repStr[] = "The World's greatings to you";
  127. TBaseSocket::Check(newSock.Send(repStr, sizeof(repStr)), "send");
  128. // client
  129. TBaseSocket::Check(cliSock.Recv(buf, 256), "recv");
  130. UNIT_ASSERT(strcmp(repStr, buf) == 0);
  131. }
  132. Y_UNIT_TEST(LocalStreamTest) {
  133. const char* localServerSockName = "./serv_sock2";
  134. RunLocalStreamTest(localServerSockName);
  135. NFs::Remove(localServerSockName);
  136. }
  137. Y_UNIT_TEST(DetermingPath) {
  138. const TString connectionString = "/var/run/some.sock http://localhost/endpoint";
  139. TStringBuf sockPath, endpoint;
  140. StringSplitter(connectionString).Split(' ').SkipEmpty().CollectInto(&sockPath, &endpoint);
  141. TSockAddrLocal sal;
  142. sal.Set(sockPath);
  143. UNIT_ASSERT_STRINGS_EQUAL(sal.ToString(), "/var/run/some.sock");
  144. }
  145. }