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.

285 líneas
6.5 KiB

hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
  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. /*
  90. struct make_address_test : public test_scaffold {
  91. make_address_test() {
  92. name = __FILE__ ":6";
  93. }
  94. virtual int run() {
  95. int error = 0;
  96. gp::address ipv4 = gp::make_ipv4("127.0.0.1", 0x1234);
  97. auto p = std::get<sockaddr_in>(ipv4);
  98. error += (p.sin_family != AF_INET);
  99. error += (p.sin_addr.s_addr != htonl(0x7F000001));
  100. error += (p.sin_port != htons(0x1234));
  101. try{
  102. gp::make_ipv4("not an IP", 1234);
  103. error += 1;
  104. }catch(...){}
  105. std::string filename = "/tmp/my_socket";
  106. gp::address unix = gp::make_unix(filename);
  107. auto q = std::get<sockaddr_un>(unix);
  108. error += (q.sun_family != AF_UNIX);
  109. error += strcmp(filename.c_str(), q.sun_path);
  110. try{
  111. std::string long_str(1024, 'p');
  112. gp::make_unix(long_str);
  113. error += 1;
  114. }catch(...){}
  115. return error;
  116. }
  117. };
  118. append_test dummy_l6923ml3(new make_address_test{});
  119. struct sockets_test : public test_scaffold {
  120. sockets_test() {
  121. name = __FILE__ ":7";
  122. }
  123. virtual int run() {
  124. int error = 0;
  125. auto v = gp::shared_fd::socket(gp::socket_domain::ip4, gp::socket_protocol::tcp_like);
  126. error += !(v.is_valid());
  127. v = gp::shared_fd::socket(gp::socket_domain::ip4, gp::socket_protocol::tcp_like);
  128. error += !(v.is_valid());
  129. auto pair_v = gp::shared_fd::unix_socket_pair(gp::socket_protocol::tcp_like, (gp::net_socket_opt_flags)0);
  130. error += !(pair_v.first.is_valid());
  131. error += !(pair_v.second.is_valid());
  132. pair_v = gp::shared_fd::unix_socket_pair(gp::socket_protocol::tcp_like, (gp::net_socket_opt_flags)-1);
  133. error += pair_v.first.is_valid();
  134. error += pair_v.second.is_valid();
  135. auto u_v = gp::shared_fd::unix_socket(gp::socket_protocol::tcp_like, (gp::socket_opt_flags)0);
  136. error += !(u_v.is_valid());
  137. u_v = gp::shared_fd::unix_socket(gp::socket_protocol::tcp_like, (gp::socket_opt_flags)-1);
  138. error += u_v.is_valid();
  139. return error;
  140. }
  141. };
  142. append_test dummy_r3321443(new sockets_test{});
  143. using namespace std::chrono_literals;
  144. struct sockets_co_test : public test_scaffold {
  145. sockets_co_test() {
  146. name = __FILE__ ":8";
  147. }
  148. virtual int run() {
  149. std::atomic_int error = 0;
  150. {
  151. auto v2 = gp::shared_fd::tcp_socket();
  152. error += !(v2.is_valid());
  153. std::thread sside([&](){
  154. auto v1 = gp::shared_fd::tcp_socket();
  155. v1.bind(gp::make_ipv4("0.0.0.0",1235));
  156. error += v1.has_failed();
  157. v1.listen();
  158. std::cout << "listens?:" << v1.is_valid()<<std::endl;
  159. error += !(v1.is_valid());
  160. gp::shared_fd listener = v1;
  161. gp::shared_fd v3;
  162. do
  163. {
  164. v3 = listener.accept();
  165. std::this_thread::sleep_for(500ms);
  166. std::cout << "accept_attempt:" << v3.error()<<std::endl;
  167. }while(listener.must_retry());
  168. puts("accepted");
  169. error += !(listener.has_failed());
  170. do
  171. {
  172. v3.write("potatoes");
  173. std::this_thread::sleep_for(1ms);
  174. }while(v3.must_retry());
  175. puts("sent");
  176. error += !(v3.has_failed());
  177. return;
  178. });
  179. std::this_thread::sleep_for(6ms);
  180. v2.connect(gp::make_ipv4("255.255.255.255",1235));
  181. error += v2.has_failed();
  182. do{
  183. v2.connect(gp::make_ipv4("127.0.0.1",1235));
  184. std::this_thread::sleep_for(500ms);
  185. std::cout << "connect_attempt:" << v2.is_valid() << " with " << v2.error()<<std::endl;
  186. }while(v2.error() != EISCONN);
  187. std::this_thread::sleep_for(1ms);
  188. auto status = v2.was_connection_refused();
  189. error += status;
  190. if(!status)
  191. {
  192. puts("connected");
  193. std::array<char, 32> buffer;
  194. std::string_view retstr;
  195. do
  196. {
  197. retstr = v2.read(std::string_view(buffer.begin(), buffer.size()));
  198. std::this_thread::sleep_for(1ms);
  199. }
  200. while(v2.must_retry());
  201. puts("received");
  202. std::cout<< "out:" << std::quoted(retstr) << std::endl;
  203. error += !(v2.has_failed());
  204. error += retstr != "potatoes";
  205. if(sside.joinable())
  206. sside.join();
  207. }
  208. return error;
  209. }
  210. return error;
  211. }
  212. };
  213. // append_test dummy_polmdf43(new sockets_co_test{});
  214. */