run-tests.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 <errno.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #ifdef _WIN32
  25. # include <io.h>
  26. #else
  27. # include <unistd.h>
  28. #endif
  29. #include "uv.h"
  30. #include "runner.h"
  31. #include "task.h"
  32. /* Actual tests and helpers are defined in test-list.h */
  33. #include "test-list.h"
  34. int ipc_helper(int listen_after_write);
  35. int ipc_helper_heavy_traffic_deadlock_bug(void);
  36. int ipc_helper_tcp_connection(void);
  37. int ipc_helper_closed_handle(void);
  38. int ipc_send_recv_helper(void);
  39. int ipc_helper_bind_twice(void);
  40. int ipc_helper_send_zero(void);
  41. int stdio_over_pipes_helper(void);
  42. void spawn_stdin_stdout(void);
  43. void process_title_big_argv(void);
  44. int spawn_tcp_server_helper(void);
  45. static int maybe_run_test(int argc, char **argv);
  46. int main(int argc, char **argv) {
  47. #ifndef _WIN32
  48. if (0 == geteuid() && NULL == getenv("UV_RUN_AS_ROOT")) {
  49. fprintf(stderr, "The libuv test suite cannot be run as root.\n");
  50. return EXIT_FAILURE;
  51. }
  52. #endif
  53. platform_init(argc, argv);
  54. argv = uv_setup_args(argc, argv);
  55. switch (argc) {
  56. case 1: return run_tests(0);
  57. case 2: return maybe_run_test(argc, argv);
  58. case 3: return run_test_part(argv[1], argv[2]);
  59. case 4: return maybe_run_test(argc, argv);
  60. default:
  61. fprintf(stderr, "Too many arguments.\n");
  62. fflush(stderr);
  63. return EXIT_FAILURE;
  64. }
  65. #ifndef __SUNPRO_C
  66. return EXIT_SUCCESS;
  67. #endif
  68. }
  69. static int maybe_run_test(int argc, char **argv) {
  70. if (strcmp(argv[1], "--list") == 0) {
  71. print_tests(stdout);
  72. return 0;
  73. }
  74. if (strcmp(argv[1], "ipc_helper_listen_before_write") == 0) {
  75. return ipc_helper(0);
  76. }
  77. if (strcmp(argv[1], "ipc_helper_listen_after_write") == 0) {
  78. return ipc_helper(1);
  79. }
  80. if (strcmp(argv[1], "ipc_helper_heavy_traffic_deadlock_bug") == 0) {
  81. return ipc_helper_heavy_traffic_deadlock_bug();
  82. }
  83. if (strcmp(argv[1], "ipc_send_recv_helper") == 0) {
  84. return ipc_send_recv_helper();
  85. }
  86. if (strcmp(argv[1], "ipc_helper_tcp_connection") == 0) {
  87. return ipc_helper_tcp_connection();
  88. }
  89. if (strcmp(argv[1], "ipc_helper_closed_handle") == 0) {
  90. return ipc_helper_closed_handle();
  91. }
  92. if (strcmp(argv[1], "ipc_helper_bind_twice") == 0) {
  93. return ipc_helper_bind_twice();
  94. }
  95. if (strcmp(argv[1], "ipc_helper_send_zero") == 0) {
  96. return ipc_helper_send_zero();
  97. }
  98. if (strcmp(argv[1], "stdio_over_pipes_helper") == 0) {
  99. return stdio_over_pipes_helper();
  100. }
  101. if (strcmp(argv[1], "spawn_helper1") == 0) {
  102. notify_parent_process();
  103. return 1;
  104. }
  105. if (strcmp(argv[1], "spawn_helper2") == 0) {
  106. notify_parent_process();
  107. printf("hello world\n");
  108. return 1;
  109. }
  110. if (strcmp(argv[1], "spawn_tcp_server_helper") == 0) {
  111. notify_parent_process();
  112. return spawn_tcp_server_helper();
  113. }
  114. if (strcmp(argv[1], "spawn_helper3") == 0) {
  115. char buffer[256];
  116. notify_parent_process();
  117. ASSERT(buffer == fgets(buffer, sizeof(buffer) - 1, stdin));
  118. buffer[sizeof(buffer) - 1] = '\0';
  119. fputs(buffer, stdout);
  120. return 1;
  121. }
  122. if (strcmp(argv[1], "spawn_helper4") == 0) {
  123. notify_parent_process();
  124. /* Never surrender, never return! */
  125. while (1) uv_sleep(10000);
  126. }
  127. if (strcmp(argv[1], "spawn_helper5") == 0) {
  128. const char out[] = "fourth stdio!\n";
  129. notify_parent_process();
  130. {
  131. #ifdef _WIN32
  132. DWORD bytes;
  133. WriteFile((HANDLE) _get_osfhandle(3), out, sizeof(out) - 1, &bytes, NULL);
  134. #else
  135. ssize_t r;
  136. do
  137. r = write(3, out, sizeof(out) - 1);
  138. while (r == -1 && errno == EINTR);
  139. fsync(3);
  140. #endif
  141. }
  142. return 1;
  143. }
  144. if (strcmp(argv[1], "spawn_helper6") == 0) {
  145. int r;
  146. notify_parent_process();
  147. r = fprintf(stdout, "hello world\n");
  148. ASSERT(r > 0);
  149. r = fprintf(stderr, "hello errworld\n");
  150. ASSERT(r > 0);
  151. return 1;
  152. }
  153. if (strcmp(argv[1], "spawn_helper7") == 0) {
  154. int r;
  155. char *test;
  156. notify_parent_process();
  157. /* Test if the test value from the parent is still set */
  158. test = getenv("ENV_TEST");
  159. ASSERT(test != NULL);
  160. r = fprintf(stdout, "%s", test);
  161. ASSERT(r > 0);
  162. return 1;
  163. }
  164. #ifndef _WIN32
  165. if (strcmp(argv[1], "spawn_helper8") == 0) {
  166. int fd;
  167. notify_parent_process();
  168. ASSERT(sizeof(fd) == read(0, &fd, sizeof(fd)));
  169. ASSERT(fd > 2);
  170. # if defined(__PASE__) /* On IBMi PASE, write() returns 1 */
  171. ASSERT(1 == write(fd, "x", 1));
  172. # else
  173. ASSERT(-1 == write(fd, "x", 1));
  174. # endif /* !__PASE__ */
  175. return 1;
  176. }
  177. #endif /* !_WIN32 */
  178. if (strcmp(argv[1], "spawn_helper9") == 0) {
  179. notify_parent_process();
  180. spawn_stdin_stdout();
  181. return 1;
  182. }
  183. #ifndef _WIN32
  184. if (strcmp(argv[1], "spawn_helper_setuid_setgid") == 0) {
  185. uv_uid_t uid = atoi(argv[2]);
  186. uv_gid_t gid = atoi(argv[3]);
  187. ASSERT(uid == getuid());
  188. ASSERT(gid == getgid());
  189. notify_parent_process();
  190. return 1;
  191. }
  192. #endif /* !_WIN32 */
  193. if (strcmp(argv[1], "process_title_big_argv_helper") == 0) {
  194. notify_parent_process();
  195. process_title_big_argv();
  196. return 0;
  197. }
  198. return run_test(argv[1], 0, 1);
  199. }