General Purpose library for Freestanding C++ and POSIX systems
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

286 líneas
6.6 KiB

  1. #include "shared_fd.hpp"
  2. #include "test_scaffold.h"
  3. #include <thread>
  4. #include <chrono>
  5. #include <iostream>
  6. #include <iomanip>
  7. struct create_test : public test_scaffold {
  8. create_test() {
  9. name = __FILE__ ":1";
  10. }
  11. virtual int run() {
  12. auto fd = gp::shared_fd::create("./bin/test_n", int(gp::open_modes::user_read) | int(gp::open_modes::user_write));
  13. return fd.is_valid()?0:1;
  14. }
  15. };
  16. append_test dummy_sdfhuisd3(new create_test{});
  17. struct open_test : public test_scaffold {
  18. open_test() {
  19. name = __FILE__ ":2";
  20. }
  21. virtual int run() {
  22. auto fd = gp::shared_fd::open("./bin/test_n", int(gp::open_options::append));
  23. return fd.is_valid()?0:1;
  24. }
  25. };
  26. append_test dummy_sdf564dd3(new open_test{});
  27. struct manip_test : public test_scaffold {
  28. manip_test() {
  29. name = __FILE__ ":3";
  30. }
  31. virtual int run() {
  32. auto fd = gp::shared_fd::open("./bin/test_n", int(gp::open_options::append));
  33. int error = fd.is_valid()?0:1;
  34. error += !(fd.get()>=0);
  35. auto fd_cpy = fd;
  36. error += fd_cpy.is_valid()?0:1;
  37. gp::shared_fd constr_cpy(fd_cpy);
  38. error += constr_cpy.is_valid()?0:1;
  39. gp::shared_fd constr_mv(std::move(constr_cpy));
  40. error += constr_mv.is_valid()?0:1;
  41. gp::shared_fd assign_cpy;
  42. assign_cpy.operator=(fd_cpy);
  43. error += assign_cpy.is_valid()?0:1;
  44. gp::shared_fd assign_mv;
  45. assign_mv.operator=(std::move(assign_cpy));
  46. error += assign_mv.is_valid()?0:1;
  47. error += (!assign_cpy.is_valid())?0:1;
  48. return error;
  49. }
  50. };
  51. append_test dummy_lkjs64dd3(new manip_test{});
  52. struct rw_test : public test_scaffold {
  53. rw_test() {
  54. name = __FILE__ ":4";
  55. }
  56. virtual int run() {
  57. auto fd = gp::shared_fd::open("./bin/test_n", int(gp::open_options::write));
  58. int error = fd.is_valid()?0:1;
  59. fd.write("potatoes");
  60. error += fd.has_failed();
  61. fd = gp::shared_fd::open("./bin/test_n", int(gp::open_options::read));
  62. std::array<char,8> buffer;
  63. auto str = fd.read(std::string_view(buffer.begin(), buffer.size()));
  64. error += fd.has_failed();
  65. error += (str != "potatoes");
  66. return error;
  67. }
  68. };
  69. append_test dummy_l6z5e4rdd3(new rw_test{});
  70. struct rw_err_test : public test_scaffold {
  71. rw_err_test() {
  72. name = __FILE__ ":5";
  73. }
  74. virtual int run() {
  75. auto fd = gp::shared_fd::create("./bin/test_n", int(gp::open_modes::user_read) | int(gp::open_modes::user_write));
  76. fd = gp::shared_fd::open("./bin/test_n", int(gp::open_options::read));
  77. int error = fd.is_valid()?0:1;
  78. fd.write("potatoes");
  79. error += fd.has_failed();
  80. fd = gp::shared_fd::open("./bin/test_n", int(gp::open_options::write));
  81. error += fd.is_valid()?0:1;
  82. std::array<char,8> buffer;
  83. auto str = fd.read(std::string_view(buffer.begin(), buffer.size()));
  84. error += fd.has_failed();
  85. return error == 2 ? 0 : 1;
  86. }
  87. };
  88. append_test dummy_l6987erd3(new rw_err_test{});
  89. struct make_address_test : public test_scaffold {
  90. make_address_test() {
  91. name = __FILE__ ":6";
  92. }
  93. virtual int run() {
  94. int error = 0;
  95. gp::address ipv4 = gp::make_ipv4("127.0.0.1", 0x1234);
  96. auto p = std::get<sockaddr_in>(ipv4);
  97. error += (p.sin_family != AF_INET);
  98. error += (p.sin_addr.s_addr != htonl(0x7F000001));
  99. error += (p.sin_port != htons(0x1234));
  100. try{
  101. gp::make_ipv4("not an IP", 1234);
  102. error += 1;
  103. }catch(...){}
  104. std::string filename = "/tmp/my_socket";
  105. gp::address unix = gp::make_unix(filename);
  106. auto q = std::get<sockaddr_un>(unix);
  107. error += (q.sun_family != AF_UNIX);
  108. error += strcmp(filename.c_str(), q.sun_path);
  109. try{
  110. std::string long_str(1024, 'p');
  111. gp::make_unix(long_str);
  112. error += 1;
  113. }catch(...){}
  114. return error;
  115. }
  116. };
  117. append_test dummy_l6923ml3(new make_address_test{});
  118. struct sockets_test : public test_scaffold {
  119. sockets_test() {
  120. name = __FILE__ ":7";
  121. }
  122. virtual int run() {
  123. int error = 0;
  124. auto v = gp::shared_fd::socket(gp::socket_domain::ip4, gp::socket_protocol::tcp_like);
  125. error += !(v.is_valid());
  126. v = gp::shared_fd::socket(gp::socket_domain::ip4, gp::socket_protocol::tcp_like);
  127. error += !(v.is_valid());
  128. auto pair_v = gp::shared_fd::unix_socket_pair(gp::socket_protocol::tcp_like, (gp::net_socket_opt_flags)0);
  129. error += !(pair_v.first.is_valid());
  130. error += !(pair_v.second.is_valid());
  131. pair_v = gp::shared_fd::unix_socket_pair(gp::socket_protocol::tcp_like, (gp::net_socket_opt_flags)-1);
  132. error += pair_v.first.is_valid();
  133. error += pair_v.second.is_valid();
  134. auto u_v = gp::shared_fd::unix_socket(gp::socket_protocol::tcp_like, (gp::socket_opt_flags)0);
  135. error += !(u_v.is_valid());
  136. u_v = gp::shared_fd::unix_socket(gp::socket_protocol::tcp_like, (gp::socket_opt_flags)-1);
  137. error += u_v.is_valid();
  138. return error;
  139. }
  140. };
  141. append_test dummy_r3321443(new sockets_test{});
  142. using namespace std::chrono_literals;
  143. struct sockets_co_test : public test_scaffold {
  144. sockets_co_test() {
  145. name = __FILE__ ":8";
  146. }
  147. virtual int run() {
  148. std::atomic_int error = 0;
  149. {
  150. auto v1 = gp::shared_fd::tcp_socket();
  151. error += !(v1.is_valid());
  152. auto v2 = gp::shared_fd::tcp_socket();
  153. error += !(v2.is_valid());
  154. v1.bind(gp::make_ipv4("254.254.254.254",1234));
  155. error += !(v1.has_failed());
  156. v1.bind(gp::make_ipv4("0.0.0.0",1235));
  157. error += v1.has_failed();
  158. v1.listen();
  159. std::cout << "listens?:" << v1.is_valid()<<std::endl;
  160. std::thread sside([&](){
  161. gp::shared_fd listener = v1;
  162. gp::shared_fd v3;
  163. do
  164. {
  165. v3 = listener.accept();
  166. std::this_thread::sleep_for(500ms);
  167. std::cout << "accept_attempt:" << v3.is_valid()<<std::endl;
  168. }while(listener.must_retry());
  169. puts("accepted");
  170. error += !(listener.has_failed());
  171. do
  172. {
  173. v3.write("potatoes");
  174. std::this_thread::sleep_for(1ms);
  175. }while(v3.must_retry());
  176. puts("sent");
  177. error += !(v3.has_failed());
  178. return;
  179. });
  180. std::this_thread::sleep_for(1ms);
  181. v2.connect(gp::make_ipv4("255.255.255.255",1235));
  182. error += v2.has_failed();
  183. do{
  184. v2.connect(gp::make_ipv4("127.0.0.1",1235));
  185. std::this_thread::sleep_for(500ms);
  186. std::cout << "connect_attempt:" << v2.is_valid() << " with " << v2.error()<<std::endl;
  187. }while(v2.error() != EISCONN);
  188. std::this_thread::sleep_for(1ms);
  189. auto status = v2.was_connection_refused();
  190. error += status;
  191. if(!status)
  192. {
  193. puts("connected");
  194. std::array<char, 32> buffer;
  195. std::string_view retstr;
  196. do
  197. {
  198. retstr = v2.read(std::string_view(buffer.begin(), buffer.size()));
  199. std::this_thread::sleep_for(1ms);
  200. }
  201. while(v2.must_retry());
  202. puts("received");
  203. std::cout<< "out:" << std::quoted(retstr) << std::endl;
  204. error += !(v2.has_failed());
  205. error += retstr != "potatoes";
  206. if(sside.joinable())
  207. sside.join();
  208. }
  209. return error;
  210. }
  211. return error;
  212. }
  213. };
  214. //append_test dummy_polmdf43(new sockets_co_test{});