test-ipc-send-recv.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 <stdio.h>
  24. #include <string.h>
  25. /* See test-ipc.ctx */
  26. void spawn_helper(uv_pipe_t* channel,
  27. uv_process_t* process,
  28. const char* helper);
  29. union handles {
  30. uv_handle_t handle;
  31. uv_stream_t stream;
  32. uv_pipe_t pipe;
  33. uv_tcp_t tcp;
  34. uv_tty_t tty;
  35. };
  36. struct echo_ctx {
  37. uv_pipe_t channel;
  38. uv_write_t write_req;
  39. uv_handle_type expected_type;
  40. union handles send;
  41. union handles recv;
  42. };
  43. static struct echo_ctx ctx;
  44. static int num_recv_handles;
  45. static void alloc_cb(uv_handle_t* handle,
  46. size_t suggested_size,
  47. uv_buf_t* buf) {
  48. /* we're not actually reading anything so a small buffer is okay */
  49. static char slab[8];
  50. buf->base = slab;
  51. buf->len = sizeof(slab);
  52. }
  53. static void recv_cb(uv_pipe_t* handle,
  54. ssize_t nread,
  55. const uv_buf_t* buf,
  56. uv_handle_type pending) {
  57. int r;
  58. ASSERT(pending == ctx.expected_type);
  59. ASSERT(handle == &ctx.channel);
  60. ASSERT(nread >= 0);
  61. if (pending == UV_NAMED_PIPE)
  62. r = uv_pipe_init(ctx.channel.loop, &ctx.recv.pipe, 0);
  63. else if (pending == UV_TCP)
  64. r = uv_tcp_init(ctx.channel.loop, &ctx.recv.tcp);
  65. else
  66. abort();
  67. ASSERT(r == 0);
  68. r = uv_accept((uv_stream_t*)&ctx.channel, &ctx.recv.stream);
  69. ASSERT(r == 0);
  70. uv_close((uv_handle_t*)&ctx.channel, NULL);
  71. uv_close(&ctx.send.handle, NULL);
  72. uv_close(&ctx.recv.handle, NULL);
  73. num_recv_handles++;
  74. }
  75. static int run_test(void) {
  76. uv_process_t process;
  77. uv_buf_t buf;
  78. int r;
  79. spawn_helper(&ctx.channel, &process, "ipc_send_recv_helper");
  80. buf = uv_buf_init(".", 1);
  81. r = uv_write2(&ctx.write_req,
  82. (uv_stream_t*)&ctx.channel,
  83. &buf, 1,
  84. &ctx.send.stream,
  85. NULL);
  86. ASSERT(r == 0);
  87. r = uv_read2_start((uv_stream_t*)&ctx.channel, alloc_cb, recv_cb);
  88. ASSERT(r == 0);
  89. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  90. ASSERT(r == 0);
  91. ASSERT(num_recv_handles == 1);
  92. return 0;
  93. }
  94. TEST_IMPL(ipc_send_recv_pipe) {
  95. int r;
  96. ctx.expected_type = UV_NAMED_PIPE;
  97. r = uv_pipe_init(uv_default_loop(), &ctx.send.pipe, 1);
  98. ASSERT(r == 0);
  99. r = uv_pipe_bind(&ctx.send.pipe, TEST_PIPENAME);
  100. ASSERT(r == 0);
  101. r = run_test();
  102. ASSERT(r == 0);
  103. MAKE_VALGRIND_HAPPY();
  104. return 0;
  105. }
  106. TEST_IMPL(ipc_send_recv_tcp) {
  107. struct sockaddr_in addr;
  108. int r;
  109. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  110. ctx.expected_type = UV_TCP;
  111. r = uv_tcp_init(uv_default_loop(), &ctx.send.tcp);
  112. ASSERT(r == 0);
  113. r = uv_tcp_bind(&ctx.send.tcp, (const struct sockaddr*) &addr, 0);
  114. ASSERT(r == 0);
  115. r = run_test();
  116. ASSERT(r == 0);
  117. MAKE_VALGRIND_HAPPY();
  118. return 0;
  119. }
  120. /* Everything here runs in a child process. */
  121. static void write2_cb(uv_write_t* req, int status) {
  122. ASSERT(status == 0);
  123. uv_close(&ctx.recv.handle, NULL);
  124. uv_close((uv_handle_t*)&ctx.channel, NULL);
  125. }
  126. static void read2_cb(uv_pipe_t* handle,
  127. ssize_t nread,
  128. const uv_buf_t* rdbuf,
  129. uv_handle_type pending) {
  130. uv_buf_t wrbuf;
  131. int r;
  132. ASSERT(pending == UV_NAMED_PIPE || pending == UV_TCP);
  133. ASSERT(handle == &ctx.channel);
  134. ASSERT(nread >= 0);
  135. wrbuf = uv_buf_init(".", 1);
  136. if (pending == UV_NAMED_PIPE)
  137. r = uv_pipe_init(ctx.channel.loop, &ctx.recv.pipe, 0);
  138. else if (pending == UV_TCP)
  139. r = uv_tcp_init(ctx.channel.loop, &ctx.recv.tcp);
  140. else
  141. abort();
  142. ASSERT(r == 0);
  143. r = uv_accept((uv_stream_t*)handle, &ctx.recv.stream);
  144. ASSERT(r == 0);
  145. r = uv_write2(&ctx.write_req,
  146. (uv_stream_t*)&ctx.channel,
  147. &wrbuf,
  148. 1,
  149. &ctx.recv.stream,
  150. write2_cb);
  151. ASSERT(r == 0);
  152. }
  153. /* stdin is a duplex channel over which a handle is sent.
  154. * We receive it and send it back where it came from.
  155. */
  156. int ipc_send_recv_helper(void) {
  157. int r;
  158. memset(&ctx, 0, sizeof(ctx));
  159. r = uv_pipe_init(uv_default_loop(), &ctx.channel, 1);
  160. ASSERT(r == 0);
  161. uv_pipe_open(&ctx.channel, 0);
  162. ASSERT(1 == uv_is_readable((uv_stream_t*)&ctx.channel));
  163. ASSERT(1 == uv_is_writable((uv_stream_t*)&ctx.channel));
  164. ASSERT(0 == uv_is_closing((uv_handle_t*)&ctx.channel));
  165. r = uv_read2_start((uv_stream_t*)&ctx.channel, alloc_cb, read2_cb);
  166. ASSERT(r == 0);
  167. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  168. ASSERT(r == 0);
  169. MAKE_VALGRIND_HAPPY();
  170. return 0;
  171. }