test_util_slow.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, 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. #else
  96. tt_assert(process_handle->stdout_pipe >= 0);
  97. tt_assert(process_handle->stderr_pipe >= 0);
  98. #endif
  99. /* Check stdout */
  100. pos = tor_read_all_from_process_stdout(process_handle, stdout_buf,
  101. sizeof(stdout_buf) - 1);
  102. tt_assert(pos >= 0);
  103. stdout_buf[pos] = '\0';
  104. tt_int_op(strlen(expected_out),OP_EQ, pos);
  105. tt_str_op(expected_out,OP_EQ, stdout_buf);
  106. notify_pending_waitpid_callbacks();
  107. /* Check it terminated correctly */
  108. retval = tor_get_exit_code(process_handle, 1, &exit_code);
  109. tt_int_op(PROCESS_EXIT_EXITED,OP_EQ, retval);
  110. tt_int_op(expected_exit,OP_EQ, exit_code);
  111. // TODO: Make test-child exit with something other than 0
  112. #ifndef _WIN32
  113. notify_pending_waitpid_callbacks();
  114. tt_ptr_op(process_handle->waitpid_cb, OP_EQ, NULL);
  115. #endif
  116. /* Check stderr */
  117. pos = tor_read_all_from_process_stderr(process_handle, stderr_buf,
  118. sizeof(stderr_buf) - 1);
  119. tt_assert(pos >= 0);
  120. stderr_buf[pos] = '\0';
  121. tt_str_op(expected_err,OP_EQ, stderr_buf);
  122. tt_int_op(strlen(expected_err),OP_EQ, pos);
  123. notify_pending_waitpid_callbacks();
  124. done:
  125. if (process_handle)
  126. tor_process_handle_destroy(process_handle, 1);
  127. }
  128. /** Check that we can launch a process and read the output */
  129. static void
  130. test_util_spawn_background_ok(void *ptr)
  131. {
  132. const char *argv[] = {TEST_CHILD, "--test", NULL};
  133. const char *expected_out = "OUT"EOL "--test"EOL "SLEEPING"EOL "DONE" EOL;
  134. const char *expected_err = "ERR"EOL;
  135. (void)ptr;
  136. run_util_spawn_background(argv, expected_out, expected_err, 0,
  137. PROCESS_STATUS_RUNNING);
  138. }
  139. /** Check that failing to find the executable works as expected */
  140. static void
  141. test_util_spawn_background_fail(void *ptr)
  142. {
  143. const char *argv[] = {BUILDDIR "/src/test/no-such-file", "--test", NULL};
  144. const char *expected_err = "";
  145. char expected_out[1024];
  146. char code[32];
  147. #ifdef _WIN32
  148. const int expected_status = PROCESS_STATUS_ERROR;
  149. #else
  150. /* TODO: Once we can signal failure to exec, set this to be
  151. * PROCESS_STATUS_RUNNING_OR_ERROR */
  152. const int expected_status = PROCESS_STATUS_RUNNING_OR_NOTRUNNING;
  153. #endif
  154. memset(expected_out, 0xf0, sizeof(expected_out));
  155. memset(code, 0xf0, sizeof(code));
  156. (void)ptr;
  157. tor_snprintf(code, sizeof(code), "%x/%x",
  158. 9 /* CHILD_STATE_FAILEXEC */ , ENOENT);
  159. tor_snprintf(expected_out, sizeof(expected_out),
  160. "ERR: Failed to spawn background process - code %s\n", code);
  161. run_util_spawn_background(argv, expected_out, expected_err, 255,
  162. expected_status);
  163. }
  164. /** Test that reading from a handle returns a partial read rather than
  165. * blocking */
  166. static void
  167. test_util_spawn_background_partial_read_impl(int exit_early)
  168. {
  169. const int expected_exit = 0;
  170. const int expected_status = PROCESS_STATUS_RUNNING;
  171. int retval, exit_code;
  172. ssize_t pos = -1;
  173. process_handle_t *process_handle=NULL;
  174. int status;
  175. char stdout_buf[100], stderr_buf[100];
  176. const char *argv[] = {TEST_CHILD, "--test", NULL};
  177. const char *expected_out[] = { "OUT" EOL "--test" EOL "SLEEPING" EOL,
  178. "DONE" EOL,
  179. NULL };
  180. const char *expected_err = "ERR" EOL;
  181. #ifndef _WIN32
  182. int eof = 0;
  183. #endif
  184. int expected_out_ctr;
  185. if (exit_early) {
  186. argv[1] = "--hang";
  187. expected_out[0] = "OUT"EOL "--hang"EOL "SLEEPING" EOL;
  188. }
  189. /* Start the program */
  190. #ifdef _WIN32
  191. status = tor_spawn_background(NULL, argv, NULL, &process_handle);
  192. #else
  193. status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
  194. #endif
  195. tt_int_op(expected_status,OP_EQ, status);
  196. tt_assert(process_handle);
  197. tt_int_op(expected_status,OP_EQ, process_handle->status);
  198. /* Check stdout */
  199. for (expected_out_ctr = 0; expected_out[expected_out_ctr] != NULL;) {
  200. #ifdef _WIN32
  201. pos = tor_read_all_handle(process_handle->stdout_pipe, stdout_buf,
  202. sizeof(stdout_buf) - 1, NULL);
  203. #else
  204. /* Check that we didn't read the end of file last time */
  205. tt_assert(!eof);
  206. pos = tor_read_all_handle(process_handle->stdout_handle, stdout_buf,
  207. sizeof(stdout_buf) - 1, NULL, &eof);
  208. #endif
  209. log_info(LD_GENERAL, "tor_read_all_handle() returned %d", (int)pos);
  210. /* We would have blocked, keep on trying */
  211. if (0 == pos)
  212. continue;
  213. tt_assert(pos > 0);
  214. stdout_buf[pos] = '\0';
  215. tt_str_op(expected_out[expected_out_ctr],OP_EQ, stdout_buf);
  216. tt_int_op(strlen(expected_out[expected_out_ctr]),OP_EQ, pos);
  217. expected_out_ctr++;
  218. }
  219. if (exit_early) {
  220. tor_process_handle_destroy(process_handle, 1);
  221. process_handle = NULL;
  222. goto done;
  223. }
  224. /* The process should have exited without writing more */
  225. #ifdef _WIN32
  226. pos = tor_read_all_handle(process_handle->stdout_pipe, stdout_buf,
  227. sizeof(stdout_buf) - 1,
  228. process_handle);
  229. tt_int_op(0,OP_EQ, pos);
  230. #else
  231. if (!eof) {
  232. /* We should have got all the data, but maybe not the EOF flag */
  233. pos = tor_read_all_handle(process_handle->stdout_handle, stdout_buf,
  234. sizeof(stdout_buf) - 1,
  235. process_handle, &eof);
  236. tt_int_op(0,OP_EQ, pos);
  237. tt_assert(eof);
  238. }
  239. /* Otherwise, we got the EOF on the last read */
  240. #endif
  241. /* Check it terminated correctly */
  242. retval = tor_get_exit_code(process_handle, 1, &exit_code);
  243. tt_int_op(PROCESS_EXIT_EXITED,OP_EQ, retval);
  244. tt_int_op(expected_exit,OP_EQ, exit_code);
  245. // TODO: Make test-child exit with something other than 0
  246. /* Check stderr */
  247. pos = tor_read_all_from_process_stderr(process_handle, stderr_buf,
  248. sizeof(stderr_buf) - 1);
  249. tt_assert(pos >= 0);
  250. stderr_buf[pos] = '\0';
  251. tt_str_op(expected_err,OP_EQ, stderr_buf);
  252. tt_int_op(strlen(expected_err),OP_EQ, pos);
  253. done:
  254. tor_process_handle_destroy(process_handle, 1);
  255. }
  256. static void
  257. test_util_spawn_background_partial_read(void *arg)
  258. {
  259. (void)arg;
  260. test_util_spawn_background_partial_read_impl(0);
  261. }
  262. static void
  263. test_util_spawn_background_exit_early(void *arg)
  264. {
  265. (void)arg;
  266. test_util_spawn_background_partial_read_impl(1);
  267. }
  268. static void
  269. test_util_spawn_background_waitpid_notify(void *arg)
  270. {
  271. int retval, exit_code;
  272. process_handle_t *process_handle=NULL;
  273. int status;
  274. int ms_timer;
  275. const char *argv[] = {TEST_CHILD, "--fast", NULL};
  276. (void) arg;
  277. #ifdef _WIN32
  278. status = tor_spawn_background(NULL, argv, NULL, &process_handle);
  279. #else
  280. status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
  281. #endif
  282. tt_int_op(status, OP_EQ, PROCESS_STATUS_RUNNING);
  283. tt_ptr_op(process_handle, OP_NE, NULL);
  284. /* We're not going to look at the stdout/stderr output this time. Instead,
  285. * we're testing whether notify_pending_waitpid_calbacks() can report the
  286. * process exit (on unix) and/or whether tor_get_exit_code() can notice it
  287. * (on windows) */
  288. #ifndef _WIN32
  289. ms_timer = 30*1000;
  290. tt_ptr_op(process_handle->waitpid_cb, OP_NE, NULL);
  291. while (process_handle->waitpid_cb && ms_timer > 0) {
  292. tor_sleep_msec(100);
  293. ms_timer -= 100;
  294. notify_pending_waitpid_callbacks();
  295. }
  296. tt_int_op(ms_timer, OP_GT, 0);
  297. tt_ptr_op(process_handle->waitpid_cb, OP_EQ, NULL);
  298. #endif
  299. ms_timer = 30*1000;
  300. while (((retval = tor_get_exit_code(process_handle, 0, &exit_code))
  301. == PROCESS_EXIT_RUNNING) && ms_timer > 0) {
  302. tor_sleep_msec(100);
  303. ms_timer -= 100;
  304. }
  305. tt_int_op(ms_timer, OP_GT, 0);
  306. tt_int_op(retval, OP_EQ, PROCESS_EXIT_EXITED);
  307. done:
  308. tor_process_handle_destroy(process_handle, 1);
  309. }
  310. #undef TEST_CHILD
  311. #undef EOL
  312. #undef MATCH_PROCESS_STATUS
  313. #ifndef _WIN32
  314. #undef PROCESS_STATUS_RUNNING_OR_NOTRUNNING
  315. #undef IS_RUNNING_OR_NOTRUNNING
  316. #endif
  317. #define UTIL_TEST(name, flags) \
  318. { #name, test_util_ ## name, flags, NULL, NULL }
  319. struct testcase_t slow_util_tests[] = {
  320. UTIL_TEST(spawn_background_ok, 0),
  321. UTIL_TEST(spawn_background_fail, 0),
  322. UTIL_TEST(spawn_background_partial_read, 0),
  323. UTIL_TEST(spawn_background_exit_early, 0),
  324. UTIL_TEST(spawn_background_waitpid_notify, 0),
  325. END_OF_TESTCASES
  326. };