dispatch_io_net.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (c) 2010-2011 Apple Inc. All rights reserved.
  3. *
  4. * @APPLE_APACHE_LICENSE_HEADER_START@
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * @APPLE_APACHE_LICENSE_HEADER_END@
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  23. #include <netdb.h>
  24. #include <netinet/in.h>
  25. #include <spawn.h>
  26. #include <sys/socket.h>
  27. #include <sys/param.h>
  28. #include <unistd.h>
  29. #elif defined(_WIN32)
  30. #include <WinSock2.h>
  31. #include <WS2tcpip.h>
  32. #include <Windows.h>
  33. #endif
  34. #include <errno.h>
  35. #include <sys/types.h>
  36. #include <fcntl.h>
  37. #include <sys/stat.h>
  38. #ifdef __APPLE__
  39. #include <crt_externs.h>
  40. #include <mach-o/dyld.h>
  41. #endif
  42. #include <Block.h>
  43. #include <bsdtests.h>
  44. #include "dispatch_test.h"
  45. #include <dispatch/dispatch.h>
  46. #if !defined(_WIN32)
  47. extern char **environ;
  48. #endif
  49. #ifndef DISPATCHTEST_IO
  50. #if DISPATCH_API_VERSION >= 20100226 && DISPATCH_API_VERSION != 20101110
  51. #define DISPATCHTEST_IO 1
  52. #endif
  53. #endif
  54. #if defined(__linux__) || defined(__FreeBSD__) || defined(_WIN32) || defined(__OpenBSD__)
  55. #define _NSGetExecutablePath(ef,bs) (*(bs)=(size_t)snprintf(ef,*(bs),"%s",argv[0]),0)
  56. #endif
  57. #if defined(_WIN32)
  58. typedef USHORT in_port_t;
  59. #endif
  60. #if !defined(_WIN32)
  61. #define closesocket(x) close(x)
  62. #endif
  63. #if DISPATCHTEST_IO
  64. int
  65. main(int argc, char** argv)
  66. {
  67. struct hostent *he;
  68. int sockfd = -1, clientfd = -1;
  69. dispatch_fd_t read_fd = -1, fd = -1;
  70. struct sockaddr_in addr1, addr2, server;
  71. socklen_t addr2len;
  72. socklen_t addr1len;
  73. pid_t clientid;
  74. #if defined(_WIN32)
  75. WSADATA wsa;
  76. int err = WSAStartup(MAKEWORD(2, 2), &wsa);
  77. if (err != 0) {
  78. fprintf(stderr, "WSAStartup failed with %d\n", err);
  79. test_stop();
  80. }
  81. #endif
  82. if (argc == 3) {
  83. // Client
  84. dispatch_test_start(NULL);
  85. if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  86. test_errno("Client-socket()", errno, 0);
  87. test_stop();
  88. }
  89. if ((he = gethostbyname("localhost")) == NULL) {
  90. fprintf(stderr, "Client-gethostbyname() failed\n");
  91. test_stop();
  92. }
  93. memcpy(&server.sin_addr, he->h_addr_list[0], (size_t)he->h_length);
  94. server.sin_family = AF_INET;
  95. server.sin_port = (in_port_t)atoi(argv[1]);
  96. fprintf(stderr, "Client-connecting on port ... %d\n", server.sin_port);
  97. if (connect(sockfd, (struct sockaddr *)&server, sizeof(server))) {
  98. test_errno("client-connect()", errno, 0);
  99. test_stop();
  100. }
  101. // Read from the socket and compare the contents are what we expect
  102. const char *path = argv[2];
  103. fd = dispatch_test_fd_open(path, O_RDONLY);
  104. if (fd == -1) {
  105. test_errno("client-open", errno, 0);
  106. test_stop();
  107. }
  108. // The reference file path given to us by the server was produced by
  109. // dispatch_test_get_large_file(). It may point to a temporary file, and
  110. // we are responsible for cleaning it up because we are the last to
  111. // access it.
  112. dispatch_test_release_large_file(path);
  113. #ifdef F_NOCACHE
  114. if (fcntl(fd, F_NOCACHE, 1)) {
  115. test_errno("client-fcntl F_NOCACHE", errno, 0);
  116. test_stop();
  117. }
  118. #else
  119. // investigate what the impact of lack of file cache disabling has
  120. // for this test
  121. #endif
  122. size_t size = (size_t)dispatch_test_fd_lseek(fd, 0, SEEK_END);
  123. dispatch_test_fd_lseek(fd, 0, SEEK_SET);
  124. __block dispatch_data_t g_d1 = dispatch_data_empty;
  125. __block dispatch_data_t g_d2 = dispatch_data_empty;
  126. __block int g_error = 0;
  127. dispatch_group_t g = dispatch_group_create();
  128. dispatch_group_enter(g);
  129. dispatch_read(fd, size, dispatch_get_global_queue(0, 0),
  130. ^(dispatch_data_t d1, int error) {
  131. test_errno("Client-dict-read error", error, 0);
  132. test_long("Client-dict-dispatch data size",
  133. (long)dispatch_data_get_size(d1), (long)size);
  134. dispatch_retain(d1);
  135. g_d1 = d1;
  136. dispatch_group_leave(g);
  137. });
  138. __block void (^b)(dispatch_data_t, int);
  139. b = Block_copy(^(dispatch_data_t d2, int error) {
  140. dispatch_data_t concat = dispatch_data_create_concat(g_d2, d2);
  141. dispatch_release(g_d2);
  142. g_d2 = concat;
  143. if (!error && dispatch_data_get_size(d2)) {
  144. dispatch_read(sockfd, SIZE_MAX,
  145. dispatch_get_global_queue(0, 0), b);
  146. } else {
  147. g_error = error;
  148. dispatch_group_leave(g);
  149. }
  150. });
  151. dispatch_group_enter(g);
  152. dispatch_read(sockfd, SIZE_MAX, dispatch_get_global_queue(0, 0), b);
  153. test_group_wait(g);
  154. test_errno("Client-read error", g_error, 0);
  155. test_long("Client-dispatch data size", (long)dispatch_data_get_size(g_d2),
  156. (long)size);
  157. size_t dict_contig_size, socket_contig_size;
  158. const void *dict_contig_buf, *socket_contig_buf;
  159. dispatch_data_t dict_data = dispatch_data_create_map(g_d1,
  160. &dict_contig_buf, &dict_contig_size);
  161. dispatch_data_t socket_data = dispatch_data_create_map(g_d2,
  162. &socket_contig_buf, &socket_contig_size);
  163. test_long("Client-dispatch data contents",
  164. memcmp(dict_contig_buf, socket_contig_buf,
  165. MIN(dict_contig_size, socket_contig_size)), 0);
  166. dispatch_test_fd_close(fd);
  167. closesocket(sockfd);
  168. dispatch_release(g_d1);
  169. dispatch_release(g_d2);
  170. dispatch_release(dict_data);
  171. dispatch_release(socket_data);
  172. dispatch_release(g);
  173. test_stop();
  174. } else {
  175. // Server
  176. dispatch_test_start("Dispatch IO Network test");
  177. if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
  178. test_errno("Server-socket()", errno, 0);
  179. test_stop();
  180. }
  181. addr1.sin_family = AF_INET;
  182. addr1.sin_addr.s_addr = INADDR_ANY;
  183. addr1.sin_port = 0;
  184. if (bind(sockfd, (struct sockaddr *)&addr1, sizeof(struct sockaddr)) ==
  185. -1) {
  186. test_errno("Server-bind()", errno, 0);
  187. test_stop();
  188. }
  189. addr1len = sizeof(struct sockaddr);
  190. if (getsockname(sockfd, (struct sockaddr*)&addr1, &addr1len) == -1) {
  191. test_errno("Server-getsockname()", errno, 0);
  192. test_stop();
  193. }
  194. if(listen(sockfd, 3) == -1) {
  195. test_errno("Server-listen()", errno, 0);
  196. test_stop();
  197. }
  198. fprintf(stderr, "Server started and listening on port %d\n",
  199. addr1.sin_port);
  200. char exec_filename [256] = {};
  201. size_t bufsize = 256;
  202. if (_NSGetExecutablePath(exec_filename, &bufsize) == -1) {
  203. fprintf(stderr, "Failed to get path name for running executable\n");
  204. test_stop();
  205. }
  206. char port_str[10] = {};
  207. snprintf(port_str, 10, " %d", addr1.sin_port);
  208. // The client must read from the same test file as the server. It will
  209. // unlink the file as soon as it can, so the server must open it before
  210. // starting the client process.
  211. char *path = dispatch_test_get_large_file();
  212. read_fd = dispatch_test_fd_open(path, O_RDONLY);
  213. if (read_fd == -1) {
  214. test_errno("open", errno, 0);
  215. goto stop_test;
  216. }
  217. char *arguments [4] = {};
  218. arguments[0] = exec_filename;
  219. arguments[1] = port_str;
  220. arguments[2] = path;
  221. arguments[3] = NULL;
  222. #ifdef HAVE_POSIX_SPAWNP
  223. int error;
  224. if ((error = posix_spawnp(&clientid, exec_filename, NULL, NULL,
  225. arguments, environ)) != 0) {
  226. test_errno("Server-posix_spawnp()", error, 0);
  227. goto stop_test;
  228. }
  229. #elif defined(_WIN32)
  230. WCHAR *cmdline = argv_to_command_line(arguments);
  231. if (!cmdline) {
  232. fprintf(stderr, "argv_to_command_line() failed\n");
  233. test_stop();
  234. }
  235. STARTUPINFOW si = {.cb = sizeof(si)};
  236. PROCESS_INFORMATION pi;
  237. BOOL created = CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, 0, NULL,
  238. NULL, &si, &pi);
  239. DWORD error = GetLastError();
  240. free(cmdline);
  241. if (!created) {
  242. print_winapi_error("CreateProcessW", error);
  243. test_stop();
  244. }
  245. clientid = (pid_t)pi.dwProcessId;
  246. #elif defined(__unix__)
  247. clientid = fork();
  248. if (clientid == -1) {
  249. test_errno("Server-fork()", errno, 0);
  250. test_stop();
  251. } else if (clientid == 0) {
  252. // Child process
  253. if (execve(exec_filename, arguments, environ) == -1) {
  254. perror(exec_filename);
  255. _Exit(EXIT_FAILURE);
  256. }
  257. }
  258. #else
  259. #error "dispatch_io_net not implemented on this platform"
  260. #endif
  261. addr2len = sizeof(struct sockaddr_in);
  262. clientfd = accept(sockfd, (struct sockaddr *)&addr2, &addr2len);
  263. if(clientfd == -1) {
  264. test_errno("Server-accept()", errno, 0);
  265. goto stop_test;
  266. }
  267. fprintf(stderr, "Server accepted connection. Server now writing\n");
  268. #ifdef F_NOCACHE
  269. if (fcntl(read_fd, F_NOCACHE, 1)) {
  270. test_errno("fcntl F_NOCACHE", errno, 0);
  271. goto stop_test;
  272. }
  273. #else
  274. // investigate what the impact of lack of file cache disabling has
  275. // for this test
  276. #endif
  277. size_t size = (size_t)dispatch_test_fd_lseek(read_fd, 0, SEEK_END);
  278. dispatch_test_fd_lseek(read_fd, 0, SEEK_SET);
  279. dispatch_group_t g = dispatch_group_create();
  280. dispatch_group_enter(g);
  281. dispatch_read(read_fd, size, dispatch_get_global_queue(0, 0),
  282. ^(dispatch_data_t d, int r_err){
  283. fprintf(stderr, "Server-dispatch_read()\n");
  284. test_errno("Server-read error", r_err, 0);
  285. test_long("Server-dispatch data size", (long)dispatch_data_get_size(d),
  286. (long)size);
  287. // convenience method handlers should only be called once
  288. if (dispatch_data_get_size(d)!= size) {
  289. fprintf(stderr, "Reading of data didn't complete\n");
  290. dispatch_test_fd_close(read_fd);
  291. closesocket(clientfd);
  292. closesocket(sockfd);
  293. test_stop();
  294. }
  295. dispatch_group_enter(g);
  296. dispatch_write(clientfd, d, dispatch_get_global_queue(0, 0),
  297. ^(dispatch_data_t remaining, int w_err) {
  298. test_errno("Server-write error", w_err, 0);
  299. test_ptr_null("Server-dispatch write remaining data",remaining);
  300. // convenience method handlers should only be called once
  301. if (remaining) {
  302. fprintf(stderr, "Server-dispatch_write() incomplete .. "
  303. "%zu bytes\n", dispatch_data_get_size(remaining));
  304. dispatch_test_fd_close(read_fd);
  305. closesocket(clientfd);
  306. closesocket(sockfd);
  307. test_stop();
  308. }
  309. closesocket(clientfd); // Sending the client EOF
  310. dispatch_group_leave(g);
  311. });
  312. dispatch_test_fd_close(read_fd);
  313. dispatch_group_leave(g);
  314. });
  315. test_group_wait(g);
  316. dispatch_release(g);
  317. fprintf(stderr, "Shutting down server\n");
  318. closesocket(sockfd);
  319. free(path);
  320. test_stop();
  321. stop_test:
  322. if (path != NULL) {
  323. dispatch_test_release_large_file(path);
  324. free(path);
  325. }
  326. dispatch_test_fd_close(read_fd);
  327. closesocket(clientfd);
  328. closesocket(sockfd);
  329. test_stop();
  330. }
  331. }
  332. #else
  333. int
  334. main()
  335. {
  336. dispatch_test_start("Dispatch IO Network test - No Dispatch IO");
  337. test_stop();
  338. }
  339. #endif