test-ipc-send-recv.c 5.3 KB

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