test_util_slow.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define UTIL_PRIVATE
  7. #define SUBPROCESS_PRIVATE
  8. #include "common/util.h"
  9. #include "lib/process/waitpid.h"
  10. #include "lib/crypt_ops/crypto.h"
  11. #include "lib/log/torlog.h"
  12. #include "lib/process/subprocess.h"
  13. #include "test/test.h"
  14. #ifndef BUILDDIR
  15. #define BUILDDIR "."
  16. #endif
  17. #ifdef _WIN32
  18. #define notify_pending_waitpid_callbacks() STMT_NIL
  19. #define TEST_CHILD "test-child.exe"
  20. #define EOL "\r\n"
  21. #else
  22. #define TEST_CHILD (BUILDDIR "/src/test/test-child")
  23. #define EOL "\n"
  24. #endif /* defined(_WIN32) */
  25. #ifdef _WIN32
  26. /* I've assumed Windows doesn't have the gap between fork and exec
  27. * that causes the race condition on unix-like platforms */
  28. #define MATCH_PROCESS_STATUS(s1,s2) ((s1) == (s2))
  29. #else /* !(defined(_WIN32)) */
  30. /* work around a race condition of the timing of SIGCHLD handler updates
  31. * to the process_handle's fields, and checks of those fields
  32. *
  33. * TODO: Once we can signal failure to exec, change PROCESS_STATUS_RUNNING to
  34. * PROCESS_STATUS_ERROR (and similarly with *_OR_NOTRUNNING) */
  35. #define PROCESS_STATUS_RUNNING_OR_NOTRUNNING (PROCESS_STATUS_RUNNING+1)
  36. #define IS_RUNNING_OR_NOTRUNNING(s) \
  37. ((s) == PROCESS_STATUS_RUNNING || (s) == PROCESS_STATUS_NOTRUNNING)
  38. /* well, this is ugly */
  39. #define MATCH_PROCESS_STATUS(s1,s2) \
  40. ( (s1) == (s2) \
  41. ||((s1) == PROCESS_STATUS_RUNNING_OR_NOTRUNNING \
  42. && IS_RUNNING_OR_NOTRUNNING(s2)) \
  43. ||((s2) == PROCESS_STATUS_RUNNING_OR_NOTRUNNING \
  44. && IS_RUNNING_OR_NOTRUNNING(s1)))
  45. #endif /* defined(_WIN32) */
  46. /** Helper function for testing tor_spawn_background */
  47. static void
  48. run_util_spawn_background(const char *argv[], const char *expected_out,
  49. const char *expected_err, int expected_exit,
  50. int expected_status)
  51. {
  52. int retval, exit_code;
  53. ssize_t pos;
  54. process_handle_t *process_handle=NULL;
  55. char stdout_buf[100], stderr_buf[100];
  56. int status;
  57. /* Start the program */
  58. #ifdef _WIN32
  59. status = tor_spawn_background(NULL, argv, NULL, &process_handle);
  60. #else
  61. status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
  62. #endif
  63. notify_pending_waitpid_callbacks();
  64. /* the race condition doesn't affect status,
  65. * because status isn't updated by the SIGCHLD handler,
  66. * but we still need to handle PROCESS_STATUS_RUNNING_OR_NOTRUNNING */
  67. tt_assert(MATCH_PROCESS_STATUS(expected_status, status));
  68. if (status == PROCESS_STATUS_ERROR) {
  69. tt_ptr_op(process_handle, OP_EQ, NULL);
  70. return;
  71. }
  72. tt_ptr_op(process_handle, OP_NE, NULL);
  73. /* When a spawned process forks, fails, then exits very quickly,
  74. * (this typically occurs when exec fails)
  75. * there is a race condition between the SIGCHLD handler
  76. * updating the process_handle's fields, and this test
  77. * checking the process status in those fields.
  78. * The SIGCHLD update can occur before or after the code below executes.
  79. * This causes intermittent failures in spawn_background_fail(),
  80. * typically when the machine is under load.
  81. * We use PROCESS_STATUS_RUNNING_OR_NOTRUNNING to avoid this issue. */
  82. /* the race condition affects the change in
  83. * process_handle->status from RUNNING to NOTRUNNING */
  84. tt_assert(MATCH_PROCESS_STATUS(expected_status, process_handle->status));
  85. #ifndef _WIN32
  86. notify_pending_waitpid_callbacks();
  87. /* the race condition affects the change in
  88. * process_handle->waitpid_cb to NULL,
  89. * so we skip the check if expected_status is ambiguous,
  90. * that is, PROCESS_STATUS_RUNNING_OR_NOTRUNNING */
  91. tt_assert(process_handle->waitpid_cb != NULL
  92. || expected_status == PROCESS_STATUS_RUNNING_OR_NOTRUNNING);
  93. #endif /* !defined(_WIN32) */
  94. #ifdef _WIN32
  95. tt_assert(process_handle->stdout_pipe != INVALID_HANDLE_VALUE);
  96. tt_assert(process_handle->stderr_pipe != INVALID_HANDLE_VALUE);
  97. tt_assert(process_handle->stdin_pipe != INVALID_HANDLE_VALUE);
  98. #else
  99. tt_assert(process_handle->stdout_pipe >= 0);
  100. tt_assert(process_handle->stderr_pipe >= 0);
  101. tt_assert(process_handle->stdin_pipe >= 0);
  102. #endif /* defined(_WIN32) */
  103. /* Check stdout */
  104. pos = tor_read_all_from_process_stdout(process_handle, stdout_buf,
  105. sizeof(stdout_buf) - 1);
  106. tt_assert(pos >= 0);
  107. stdout_buf[pos] = '\0';
  108. tt_int_op(strlen(expected_out),OP_EQ, pos);
  109. tt_str_op(expected_out,OP_EQ, stdout_buf);
  110. notify_pending_waitpid_callbacks();
  111. /* Check it terminated correctly */
  112. retval = tor_get_exit_code(process_handle, 1, &exit_code);
  113. tt_int_op(PROCESS_EXIT_EXITED,OP_EQ, retval);
  114. tt_int_op(expected_exit,OP_EQ, exit_code);
  115. // TODO: Make test-child exit with something other than 0
  116. #ifndef _WIN32
  117. notify_pending_waitpid_callbacks();
  118. tt_ptr_op(process_handle->waitpid_cb, OP_EQ, NULL);
  119. #endif
  120. /* Check stderr */
  121. pos = tor_read_all_from_process_stderr(process_handle, stderr_buf,
  122. sizeof(stderr_buf) - 1);
  123. tt_assert(pos >= 0);
  124. stderr_buf[pos] = '\0';
  125. tt_str_op(expected_err,OP_EQ, stderr_buf);
  126. tt_int_op(strlen(expected_err),OP_EQ, pos);
  127. notify_pending_waitpid_callbacks();
  128. done:
  129. if (process_handle)
  130. tor_process_handle_destroy(process_handle, 1);
  131. }
  132. /** Check that we can launch a process and read the output */
  133. static void
  134. test_util_spawn_background_ok(void *ptr)
  135. {
  136. const char *argv[] = {TEST_CHILD, "--test", NULL};
  137. const char *expected_out = "OUT"EOL "--test"EOL "SLEEPING"EOL "DONE" EOL;
  138. const char *expected_err = "ERR"EOL;
  139. (void)ptr;
  140. run_util_spawn_background(argv, expected_out, expected_err, 0,
  141. PROCESS_STATUS_RUNNING);
  142. }
  143. /** Check that failing to find the executable works as expected */
  144. static void
  145. test_util_spawn_background_fail(void *ptr)
  146. {
  147. const char *argv[] = {BUILDDIR "/src/test/no-such-file", "--test", NULL};
  148. const char *expected_err = "";
  149. char expected_out[1024];
  150. char code[32];
  151. #ifdef _WIN32
  152. const int expected_status = PROCESS_STATUS_ERROR;
  153. #else
  154. /* TODO: Once we can signal failure to exec, set this to be
  155. * PROCESS_STATUS_RUNNING_OR_ERROR */
  156. const int expected_status = PROCESS_STATUS_RUNNING_OR_NOTRUNNING;
  157. #endif /* defined(_WIN32) */
  158. memset(expected_out, 0xf0, sizeof(expected_out));
  159. memset(code, 0xf0, sizeof(code));
  160. (void)ptr;
  161. tor_snprintf(code, sizeof(code), "%x/%x",
  162. 9 /* CHILD_STATE_FAILEXEC */ , ENOENT);
  163. tor_snprintf(expected_out, sizeof(expected_out),
  164. "ERR: Failed to spawn background process - code %s\n", code);
  165. run_util_spawn_background(argv, expected_out, expected_err, 255,
  166. expected_status);
  167. }
  168. /** Test that reading from a handle returns a partial read rather than
  169. * blocking */
  170. static void
  171. test_util_spawn_background_partial_read_impl(int exit_early)
  172. {
  173. const int expected_exit = 0;
  174. const int expected_status = PROCESS_STATUS_RUNNING;
  175. int retval, exit_code;
  176. ssize_t pos = -1;
  177. process_handle_t *process_handle=NULL;
  178. int status;
  179. char stdout_buf[100], stderr_buf[100];
  180. const char *argv[] = {TEST_CHILD, "--test", NULL};
  181. const char *expected_out[] = { "OUT" EOL "--test" EOL "SLEEPING" EOL,
  182. "DONE" EOL,
  183. NULL };
  184. const char *expected_err = "ERR" EOL;
  185. #ifndef _WIN32
  186. int eof = 0;
  187. #endif
  188. int expected_out_ctr;
  189. if (exit_early) {
  190. argv[1] = "--hang";
  191. expected_out[0] = "OUT"EOL "--hang"EOL "SLEEPING" EOL;
  192. }
  193. /* Start the program */
  194. #ifdef _WIN32
  195. status = tor_spawn_background(NULL, argv, NULL, &process_handle);
  196. #else
  197. status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
  198. #endif
  199. tt_int_op(expected_status,OP_EQ, status);
  200. tt_assert(process_handle);
  201. tt_int_op(expected_status,OP_EQ, process_handle->status);
  202. /* Check stdout */
  203. for (expected_out_ctr = 0; expected_out[expected_out_ctr] != NULL;) {
  204. #ifdef _WIN32
  205. pos = tor_read_all_handle(process_handle->stdout_pipe, stdout_buf,
  206. sizeof(stdout_buf) - 1, NULL);
  207. #else
  208. /* Check that we didn't read the end of file last time */
  209. tt_assert(!eof);
  210. pos = tor_read_all_handle(process_handle->stdout_pipe, stdout_buf,
  211. sizeof(stdout_buf) - 1, NULL, &eof);
  212. #endif /* defined(_WIN32) */
  213. log_info(LD_GENERAL, "tor_read_all_handle() returned %d", (int)pos);
  214. /* We would have blocked, keep on trying */
  215. if (0 == pos)
  216. continue;
  217. tt_assert(pos > 0);
  218. stdout_buf[pos] = '\0';
  219. tt_str_op(expected_out[expected_out_ctr],OP_EQ, stdout_buf);
  220. tt_int_op(strlen(expected_out[expected_out_ctr]),OP_EQ, pos);
  221. expected_out_ctr++;
  222. }
  223. if (exit_early) {
  224. tor_process_handle_destroy(process_handle, 1);
  225. process_handle = NULL;
  226. goto done;
  227. }
  228. /* The process should have exited without writing more */
  229. #ifdef _WIN32
  230. pos = tor_read_all_handle(process_handle->stdout_pipe, stdout_buf,
  231. sizeof(stdout_buf) - 1,
  232. process_handle);
  233. tt_int_op(0,OP_EQ, pos);
  234. #else /* !(defined(_WIN32)) */
  235. if (!eof) {
  236. /* We should have got all the data, but maybe not the EOF flag */
  237. pos = tor_read_all_handle(process_handle->stdout_pipe, stdout_buf,
  238. sizeof(stdout_buf) - 1,
  239. process_handle, &eof);
  240. tt_int_op(0,OP_EQ, pos);
  241. tt_assert(eof);
  242. }
  243. /* Otherwise, we got the EOF on the last read */
  244. #endif /* defined(_WIN32) */
  245. /* Check it terminated correctly */
  246. retval = tor_get_exit_code(process_handle, 1, &exit_code);
  247. tt_int_op(PROCESS_EXIT_EXITED,OP_EQ, retval);
  248. tt_int_op(expected_exit,OP_EQ, exit_code);
  249. // TODO: Make test-child exit with something other than 0
  250. /* Check stderr */
  251. pos = tor_read_all_from_process_stderr(process_handle, stderr_buf,
  252. sizeof(stderr_buf) - 1);
  253. tt_assert(pos >= 0);
  254. stderr_buf[pos] = '\0';
  255. tt_str_op(expected_err,OP_EQ, stderr_buf);
  256. tt_int_op(strlen(expected_err),OP_EQ, pos);
  257. done:
  258. tor_process_handle_destroy(process_handle, 1);
  259. }
  260. static void
  261. test_util_spawn_background_partial_read(void *arg)
  262. {
  263. (void)arg;
  264. test_util_spawn_background_partial_read_impl(0);
  265. }
  266. static void
  267. test_util_spawn_background_exit_early(void *arg)
  268. {
  269. (void)arg;
  270. test_util_spawn_background_partial_read_impl(1);
  271. }
  272. static void
  273. test_util_spawn_background_waitpid_notify(void *arg)
  274. {
  275. int retval, exit_code;
  276. process_handle_t *process_handle=NULL;
  277. int status;
  278. int ms_timer;
  279. const char *argv[] = {TEST_CHILD, "--fast", NULL};
  280. (void) arg;
  281. #ifdef _WIN32
  282. status = tor_spawn_background(NULL, argv, NULL, &process_handle);
  283. #else
  284. status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
  285. #endif
  286. tt_int_op(status, OP_EQ, PROCESS_STATUS_RUNNING);
  287. tt_ptr_op(process_handle, OP_NE, NULL);
  288. /* We're not going to look at the stdout/stderr output this time. Instead,
  289. * we're testing whether notify_pending_waitpid_calbacks() can report the
  290. * process exit (on unix) and/or whether tor_get_exit_code() can notice it
  291. * (on windows) */
  292. #ifndef _WIN32
  293. ms_timer = 30*1000;
  294. tt_ptr_op(process_handle->waitpid_cb, OP_NE, NULL);
  295. while (process_handle->waitpid_cb && ms_timer > 0) {
  296. tor_sleep_msec(100);
  297. ms_timer -= 100;
  298. notify_pending_waitpid_callbacks();
  299. }
  300. tt_int_op(ms_timer, OP_GT, 0);
  301. tt_ptr_op(process_handle->waitpid_cb, OP_EQ, NULL);
  302. #endif /* !defined(_WIN32) */
  303. ms_timer = 30*1000;
  304. while (((retval = tor_get_exit_code(process_handle, 0, &exit_code))
  305. == PROCESS_EXIT_RUNNING) && ms_timer > 0) {
  306. tor_sleep_msec(100);
  307. ms_timer -= 100;
  308. }
  309. tt_int_op(ms_timer, OP_GT, 0);
  310. tt_int_op(retval, OP_EQ, PROCESS_EXIT_EXITED);
  311. done:
  312. tor_process_handle_destroy(process_handle, 1);
  313. }
  314. #undef TEST_CHILD
  315. #undef EOL
  316. #undef MATCH_PROCESS_STATUS
  317. #ifndef _WIN32
  318. #undef PROCESS_STATUS_RUNNING_OR_NOTRUNNING
  319. #undef IS_RUNNING_OR_NOTRUNNING
  320. #endif
  321. #define UTIL_TEST(name, flags) \
  322. { #name, test_util_ ## name, flags, NULL, NULL }
  323. struct testcase_t slow_util_tests[] = {
  324. UTIL_TEST(spawn_background_ok, 0),
  325. UTIL_TEST(spawn_background_fail, 0),
  326. UTIL_TEST(spawn_background_partial_read, 0),
  327. UTIL_TEST(spawn_background_exit_early, 0),
  328. UTIL_TEST(spawn_background_waitpid_notify, 0),
  329. END_OF_TESTCASES
  330. };