test_util_slow.c 12 KB

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