test-stdio-over-pipes.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <stdlib.h>
  24. #include <string.h>
  25. static char exepath[1024];
  26. static size_t exepath_size = 1024;
  27. static char* args[3];
  28. static uv_process_options_t options;
  29. static int close_cb_called;
  30. static int exit_cb_called;
  31. static int on_read_cb_called;
  32. static int after_write_cb_called;
  33. static uv_pipe_t in;
  34. static uv_pipe_t out;
  35. static uv_loop_t* loop;
  36. #define OUTPUT_SIZE 1024
  37. static char output[OUTPUT_SIZE];
  38. static int output_used;
  39. static void close_cb(uv_handle_t* handle) {
  40. printf("close_cb\n");
  41. close_cb_called++;
  42. }
  43. static void exit_cb(uv_process_t* process, int exit_status, int term_signal) {
  44. printf("exit_cb\n");
  45. exit_cb_called++;
  46. ASSERT(exit_status == 0);
  47. ASSERT(term_signal == 0);
  48. uv_close((uv_handle_t*)process, close_cb);
  49. uv_close((uv_handle_t*)&in, close_cb);
  50. uv_close((uv_handle_t*)&out, close_cb);
  51. }
  52. static void init_process_options(char* test, uv_exit_cb exit_cb) {
  53. int r = uv_exepath(exepath, &exepath_size);
  54. ASSERT(r == 0);
  55. exepath[exepath_size] = '\0';
  56. args[0] = exepath;
  57. args[1] = test;
  58. args[2] = NULL;
  59. options.file = exepath;
  60. options.args = args;
  61. options.exit_cb = exit_cb;
  62. }
  63. static uv_buf_t on_alloc(uv_handle_t* handle, size_t suggested_size) {
  64. return uv_buf_init(output + output_used, OUTPUT_SIZE - output_used);
  65. }
  66. static void after_write(uv_write_t* req, int status) {
  67. if (status) {
  68. uv_err_t err = uv_last_error(loop);
  69. fprintf(stderr, "uv_write error: %s\n", uv_strerror(err));
  70. ASSERT(0);
  71. }
  72. /* Free the read/write buffer and the request */
  73. free(req);
  74. after_write_cb_called++;
  75. }
  76. static void on_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t rdbuf) {
  77. uv_write_t* req;
  78. uv_buf_t wrbuf;
  79. int r;
  80. ASSERT(nread > 0 || uv_last_error(uv_default_loop()).code == UV_EOF);
  81. if (nread > 0) {
  82. output_used += nread;
  83. if (output_used == 12) {
  84. ASSERT(memcmp("hello world\n", output, 12) == 0);
  85. wrbuf = uv_buf_init(output, output_used);
  86. req = malloc(sizeof(*req));
  87. r = uv_write(req, (uv_stream_t*)&in, &wrbuf, 1, after_write);
  88. ASSERT(r == 0);
  89. }
  90. }
  91. on_read_cb_called++;
  92. }
  93. TEST_IMPL(stdio_over_pipes) {
  94. int r;
  95. uv_process_t process;
  96. uv_stdio_container_t stdio[2];
  97. loop = uv_default_loop();
  98. init_process_options("stdio_over_pipes_helper", exit_cb);
  99. uv_pipe_init(loop, &out, 0);
  100. uv_pipe_init(loop, &in, 0);
  101. options.stdio = stdio;
  102. options.stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
  103. options.stdio[0].data.stream = (uv_stream_t*)&in;
  104. options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  105. options.stdio[1].data.stream = (uv_stream_t*)&out;
  106. options.stdio_count = 2;
  107. r = uv_spawn(loop, &process, options);
  108. ASSERT(r == 0);
  109. r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
  110. ASSERT(r == 0);
  111. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  112. ASSERT(r == 0);
  113. ASSERT(on_read_cb_called > 1);
  114. ASSERT(after_write_cb_called == 1);
  115. ASSERT(exit_cb_called == 1);
  116. ASSERT(close_cb_called == 3);
  117. ASSERT(memcmp("hello world\n", output, 12) == 0);
  118. ASSERT(output_used == 12);
  119. MAKE_VALGRIND_HAPPY();
  120. return 0;
  121. }
  122. /* Everything here runs in a child process. */
  123. static int on_pipe_read_called;
  124. static int after_write_called;
  125. static uv_pipe_t stdin_pipe;
  126. static uv_pipe_t stdout_pipe;
  127. static void on_pipe_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) {
  128. ASSERT(nread > 0);
  129. ASSERT(memcmp("hello world\n", buf.base, nread) == 0);
  130. on_pipe_read_called++;
  131. free(buf.base);
  132. uv_close((uv_handle_t*)&stdin_pipe, close_cb);
  133. uv_close((uv_handle_t*)&stdout_pipe, close_cb);
  134. }
  135. static void after_pipe_write(uv_write_t* req, int status) {
  136. ASSERT(status == 0);
  137. after_write_called++;
  138. }
  139. static uv_buf_t on_read_alloc(uv_handle_t* handle, size_t suggested_size) {
  140. return uv_buf_init(malloc(suggested_size), suggested_size);
  141. }
  142. int stdio_over_pipes_helper(void) {
  143. /* Write several buffers to test that the write order is preserved. */
  144. char* buffers[] = {
  145. "he",
  146. "ll",
  147. "o ",
  148. "wo",
  149. "rl",
  150. "d",
  151. "\n"
  152. };
  153. uv_write_t write_req[ARRAY_SIZE(buffers)];
  154. uv_buf_t buf[ARRAY_SIZE(buffers)];
  155. unsigned int i;
  156. int r;
  157. uv_loop_t* loop = uv_default_loop();
  158. ASSERT(UV_NAMED_PIPE == uv_guess_handle(0));
  159. ASSERT(UV_NAMED_PIPE == uv_guess_handle(1));
  160. r = uv_pipe_init(loop, &stdin_pipe, 0);
  161. ASSERT(r == 0);
  162. r = uv_pipe_init(loop, &stdout_pipe, 0);
  163. ASSERT(r == 0);
  164. uv_pipe_open(&stdin_pipe, 0);
  165. uv_pipe_open(&stdout_pipe, 1);
  166. /* Unref both stdio handles to make sure that all writes complete. */
  167. uv_unref((uv_handle_t*)&stdin_pipe);
  168. uv_unref((uv_handle_t*)&stdout_pipe);
  169. for (i = 0; i < ARRAY_SIZE(buffers); i++) {
  170. buf[i] = uv_buf_init((char*)buffers[i], strlen(buffers[i]));
  171. }
  172. for (i = 0; i < ARRAY_SIZE(buffers); i++) {
  173. r = uv_write(&write_req[i], (uv_stream_t*)&stdout_pipe, &buf[i], 1,
  174. after_pipe_write);
  175. ASSERT(r == 0);
  176. }
  177. uv_run(loop, UV_RUN_DEFAULT);
  178. ASSERT(after_write_called == 7);
  179. ASSERT(on_pipe_read_called == 0);
  180. ASSERT(close_cb_called == 0);
  181. uv_ref((uv_handle_t*)&stdout_pipe);
  182. uv_ref((uv_handle_t*)&stdin_pipe);
  183. r = uv_read_start((uv_stream_t*)&stdin_pipe, on_read_alloc,
  184. on_pipe_read);
  185. ASSERT(r == 0);
  186. uv_run(loop, UV_RUN_DEFAULT);
  187. ASSERT(after_write_called == 7);
  188. ASSERT(on_pipe_read_called == 1);
  189. ASSERT(close_cb_called == 2);
  190. MAKE_VALGRIND_HAPPY();
  191. return 0;
  192. }