1
0

test-stdio-over-pipes.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. close_cb_called++;
  41. }
  42. static void exit_cb(uv_process_t* process,
  43. int64_t exit_status,
  44. int term_signal) {
  45. printf("exit_cb\n");
  46. exit_cb_called++;
  47. ASSERT(exit_status == 0);
  48. ASSERT(term_signal == 0);
  49. uv_close((uv_handle_t*)process, close_cb);
  50. uv_close((uv_handle_t*)&in, close_cb);
  51. uv_close((uv_handle_t*)&out, close_cb);
  52. }
  53. static void init_process_options(char* test, uv_exit_cb exit_cb) {
  54. int r = uv_exepath(exepath, &exepath_size);
  55. ASSERT(r == 0);
  56. exepath[exepath_size] = '\0';
  57. args[0] = exepath;
  58. args[1] = test;
  59. args[2] = NULL;
  60. options.file = exepath;
  61. options.args = args;
  62. options.exit_cb = exit_cb;
  63. }
  64. static void on_alloc(uv_handle_t* handle,
  65. size_t suggested_size,
  66. uv_buf_t* buf) {
  67. buf->base = output + output_used;
  68. buf->len = OUTPUT_SIZE - output_used;
  69. }
  70. static void after_write(uv_write_t* req, int status) {
  71. if (status) {
  72. fprintf(stderr, "uv_write error: %s\n", uv_strerror(status));
  73. ASSERT(0);
  74. }
  75. /* Free the read/write buffer and the request */
  76. free(req);
  77. after_write_cb_called++;
  78. }
  79. static void on_read(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* rdbuf) {
  80. uv_write_t* req;
  81. uv_buf_t wrbuf;
  82. int r;
  83. ASSERT(nread > 0 || nread == UV_EOF);
  84. if (nread > 0) {
  85. output_used += nread;
  86. if (output_used == 12) {
  87. ASSERT(memcmp("hello world\n", output, 12) == 0);
  88. wrbuf = uv_buf_init(output, output_used);
  89. req = malloc(sizeof(*req));
  90. r = uv_write(req, (uv_stream_t*)&in, &wrbuf, 1, after_write);
  91. ASSERT(r == 0);
  92. }
  93. }
  94. on_read_cb_called++;
  95. }
  96. TEST_IMPL(stdio_over_pipes) {
  97. int r;
  98. uv_process_t process;
  99. uv_stdio_container_t stdio[2];
  100. loop = uv_default_loop();
  101. init_process_options("stdio_over_pipes_helper", exit_cb);
  102. uv_pipe_init(loop, &out, 0);
  103. uv_pipe_init(loop, &in, 0);
  104. options.stdio = stdio;
  105. options.stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
  106. options.stdio[0].data.stream = (uv_stream_t*)&in;
  107. options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  108. options.stdio[1].data.stream = (uv_stream_t*)&out;
  109. options.stdio_count = 2;
  110. r = uv_spawn(loop, &process, &options);
  111. ASSERT(r == 0);
  112. r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
  113. ASSERT(r == 0);
  114. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  115. ASSERT(r == 0);
  116. ASSERT(on_read_cb_called > 1);
  117. ASSERT(after_write_cb_called == 1);
  118. ASSERT(exit_cb_called == 1);
  119. ASSERT(close_cb_called == 3);
  120. ASSERT(memcmp("hello world\n", output, 12) == 0);
  121. ASSERT(output_used == 12);
  122. MAKE_VALGRIND_HAPPY();
  123. return 0;
  124. }
  125. /* Everything here runs in a child process. */
  126. static int on_pipe_read_called;
  127. static int after_write_called;
  128. static uv_pipe_t stdin_pipe;
  129. static uv_pipe_t stdout_pipe;
  130. static void on_pipe_read(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
  131. ASSERT(nread > 0);
  132. ASSERT(memcmp("hello world\n", buf->base, nread) == 0);
  133. on_pipe_read_called++;
  134. free(buf->base);
  135. uv_close((uv_handle_t*)&stdin_pipe, close_cb);
  136. uv_close((uv_handle_t*)&stdout_pipe, close_cb);
  137. }
  138. static void after_pipe_write(uv_write_t* req, int status) {
  139. ASSERT(status == 0);
  140. after_write_called++;
  141. }
  142. static void on_read_alloc(uv_handle_t* handle,
  143. size_t suggested_size,
  144. uv_buf_t* buf) {
  145. buf->base = malloc(suggested_size);
  146. buf->len = suggested_size;
  147. }
  148. int stdio_over_pipes_helper(void) {
  149. /* Write several buffers to test that the write order is preserved. */
  150. char* buffers[] = {
  151. "he",
  152. "ll",
  153. "o ",
  154. "wo",
  155. "rl",
  156. "d",
  157. "\n"
  158. };
  159. uv_write_t write_req[ARRAY_SIZE(buffers)];
  160. uv_buf_t buf[ARRAY_SIZE(buffers)];
  161. unsigned int i;
  162. int r;
  163. uv_loop_t* loop = uv_default_loop();
  164. ASSERT(UV_NAMED_PIPE == uv_guess_handle(0));
  165. ASSERT(UV_NAMED_PIPE == uv_guess_handle(1));
  166. r = uv_pipe_init(loop, &stdin_pipe, 0);
  167. ASSERT(r == 0);
  168. r = uv_pipe_init(loop, &stdout_pipe, 0);
  169. ASSERT(r == 0);
  170. uv_pipe_open(&stdin_pipe, 0);
  171. uv_pipe_open(&stdout_pipe, 1);
  172. /* Unref both stdio handles to make sure that all writes complete. */
  173. uv_unref((uv_handle_t*)&stdin_pipe);
  174. uv_unref((uv_handle_t*)&stdout_pipe);
  175. for (i = 0; i < ARRAY_SIZE(buffers); i++) {
  176. buf[i] = uv_buf_init((char*)buffers[i], strlen(buffers[i]));
  177. }
  178. for (i = 0; i < ARRAY_SIZE(buffers); i++) {
  179. r = uv_write(&write_req[i], (uv_stream_t*)&stdout_pipe, &buf[i], 1,
  180. after_pipe_write);
  181. ASSERT(r == 0);
  182. }
  183. uv_run(loop, UV_RUN_DEFAULT);
  184. ASSERT(after_write_called == 7);
  185. ASSERT(on_pipe_read_called == 0);
  186. ASSERT(close_cb_called == 0);
  187. uv_ref((uv_handle_t*)&stdout_pipe);
  188. uv_ref((uv_handle_t*)&stdin_pipe);
  189. r = uv_read_start((uv_stream_t*)&stdin_pipe, on_read_alloc, on_pipe_read);
  190. ASSERT(r == 0);
  191. uv_run(loop, UV_RUN_DEFAULT);
  192. ASSERT(after_write_called == 7);
  193. ASSERT(on_pipe_read_called == 1);
  194. ASSERT(close_cb_called == 2);
  195. MAKE_VALGRIND_HAPPY();
  196. return 0;
  197. }