test_process_slow.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* Copyright (c) 2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_process_slow.c
  5. * \brief Slow test cases for the Process API.
  6. */
  7. #include "orconfig.h"
  8. #include "core/or/or.h"
  9. #include "core/mainloop/mainloop.h"
  10. #include "lib/evloop/compat_libevent.h"
  11. #include "lib/process/process.h"
  12. #include "lib/process/waitpid.h"
  13. #include "test/test.h"
  14. #ifndef BUILDDIR
  15. #define BUILDDIR "."
  16. #endif
  17. #ifdef _WIN32
  18. #define TEST_PROCESS "test-process.exe"
  19. #else
  20. #define TEST_PROCESS BUILDDIR "/src/test/test-process"
  21. #endif /* defined(_WIN32) */
  22. /** Timer that ticks once a second and stop the event loop after 5 ticks. */
  23. static periodic_timer_t *main_loop_timeout_timer;
  24. /** How many times have our timer ticked? */
  25. static int timer_tick_count;
  26. struct process_data_t {
  27. smartlist_t *stdout_data;
  28. smartlist_t *stderr_data;
  29. smartlist_t *stdin_data;
  30. process_exit_code_t exit_code;
  31. };
  32. typedef struct process_data_t process_data_t;
  33. static process_data_t *
  34. process_data_new(void)
  35. {
  36. process_data_t *process_data = tor_malloc_zero(sizeof(process_data_t));
  37. process_data->stdout_data = smartlist_new();
  38. process_data->stderr_data = smartlist_new();
  39. process_data->stdin_data = smartlist_new();
  40. return process_data;
  41. }
  42. static void
  43. process_data_free(process_data_t *process_data)
  44. {
  45. if (process_data == NULL)
  46. return;
  47. SMARTLIST_FOREACH(process_data->stdout_data, char *, x, tor_free(x));
  48. SMARTLIST_FOREACH(process_data->stderr_data, char *, x, tor_free(x));
  49. SMARTLIST_FOREACH(process_data->stdin_data, char *, x, tor_free(x));
  50. smartlist_free(process_data->stdout_data);
  51. smartlist_free(process_data->stderr_data);
  52. smartlist_free(process_data->stdin_data);
  53. tor_free(process_data);
  54. }
  55. static void
  56. process_stdout_callback(process_t *process, char *data, size_t size)
  57. {
  58. tt_ptr_op(process, OP_NE, NULL);
  59. tt_ptr_op(data, OP_NE, NULL);
  60. tt_int_op(strlen(data), OP_EQ, size);
  61. process_data_t *process_data = process_get_data(process);
  62. smartlist_add(process_data->stdout_data, tor_strdup(data));
  63. done:
  64. return;
  65. }
  66. static void
  67. process_stderr_callback(process_t *process, char *data, size_t size)
  68. {
  69. tt_ptr_op(process, OP_NE, NULL);
  70. tt_ptr_op(data, OP_NE, NULL);
  71. tt_int_op(strlen(data), OP_EQ, size);
  72. process_data_t *process_data = process_get_data(process);
  73. smartlist_add(process_data->stderr_data, tor_strdup(data));
  74. done:
  75. return;
  76. }
  77. static void
  78. process_exit_callback(process_t *process, process_exit_code_t exit_code)
  79. {
  80. tt_ptr_op(process, OP_NE, NULL);
  81. process_data_t *process_data = process_get_data(process);
  82. process_data->exit_code = exit_code;
  83. /* Our process died. Let's check the values it returned. */
  84. tor_shutdown_event_loop_and_exit(0);
  85. done:
  86. return;
  87. }
  88. #ifdef _WIN32
  89. static const char *
  90. get_win32_test_binary_path(void)
  91. {
  92. static char buffer[MAX_PATH];
  93. /* Get the absolute path of our binary: \path\to\test-slow.exe. */
  94. GetModuleFileNameA(GetModuleHandle(0), buffer, sizeof(buffer));
  95. /* Find our process name. */
  96. char *offset = strstr(buffer, "test-slow.exe");
  97. tt_ptr_op(offset, OP_NE, NULL);
  98. /* Change test-slow.exe to test-process.exe. */
  99. memcpy(offset, TEST_PROCESS, strlen(TEST_PROCESS));
  100. return buffer;
  101. done:
  102. return NULL;
  103. }
  104. #endif
  105. static void
  106. main_loop_timeout_cb(periodic_timer_t *timer, void *data)
  107. {
  108. /* Sanity check. */
  109. tt_ptr_op(timer, OP_EQ, main_loop_timeout_timer);
  110. tt_ptr_op(data, OP_EQ, NULL);
  111. /* Have we been called 10 times we exit. */
  112. timer_tick_count++;
  113. tt_int_op(timer_tick_count, OP_LT, 10);
  114. #ifndef _WIN32
  115. /* Call waitpid callbacks. */
  116. notify_pending_waitpid_callbacks();
  117. #endif
  118. return;
  119. done:
  120. /* Exit with an error. */
  121. tor_shutdown_event_loop_and_exit(-1);
  122. }
  123. static void
  124. run_main_loop(void)
  125. {
  126. int ret;
  127. /* Wake up after 1 seconds. */
  128. static const struct timeval interval = {1, 0};
  129. timer_tick_count = 0;
  130. main_loop_timeout_timer = periodic_timer_new(tor_libevent_get_base(),
  131. &interval,
  132. main_loop_timeout_cb,
  133. NULL);
  134. /* Run our main loop. */
  135. ret = do_main_loop();
  136. /* Clean up our main loop timeout timer. */
  137. tt_int_op(ret, OP_EQ, 0);
  138. done:
  139. periodic_timer_free(main_loop_timeout_timer);
  140. }
  141. static void
  142. test_callbacks(void *arg)
  143. {
  144. (void)arg;
  145. const char *filename = NULL;
  146. #ifdef _WIN32
  147. filename = get_win32_test_binary_path();
  148. #else
  149. filename = TEST_PROCESS;
  150. #endif
  151. /* Initialize Process subsystem. */
  152. process_init();
  153. /* Process callback data. */
  154. process_data_t *process_data = process_data_new();
  155. /* Setup our process. */
  156. process_t *process = process_new(filename);
  157. process_set_data(process, process_data);
  158. process_set_stdout_read_callback(process, process_stdout_callback);
  159. process_set_stderr_read_callback(process, process_stderr_callback);
  160. process_set_exit_callback(process, process_exit_callback);
  161. /* Set environment variable. */
  162. process_set_environment(process, "TOR_TEST_ENV", "Hello, from Tor!");
  163. /* Add some arguments. */
  164. process_append_argument(process, "This is the first one");
  165. process_append_argument(process, "Second one");
  166. process_append_argument(process, "Third: Foo bar baz");
  167. /* Run our process. */
  168. process_status_t status;
  169. status = process_exec(process);
  170. tt_int_op(status, OP_EQ, PROCESS_STATUS_RUNNING);
  171. /* Write some lines to stdin. */
  172. process_printf(process, "Hi process!\r\n");
  173. process_printf(process, "Can you read more than one line?\n");
  174. process_printf(process, "Can you read partial ...");
  175. process_printf(process, " lines?\r\n");
  176. /* Start our main loop. */
  177. run_main_loop();
  178. /* Check if our process is still running? */
  179. status = process_get_status(process);
  180. tt_int_op(status, OP_EQ, PROCESS_STATUS_NOT_RUNNING);
  181. /* We returned. Let's see what our event loop said. */
  182. tt_int_op(smartlist_len(process_data->stdout_data), OP_EQ, 12);
  183. tt_int_op(smartlist_len(process_data->stderr_data), OP_EQ, 3);
  184. tt_int_op(process_data->exit_code, OP_EQ, 0);
  185. /* Check stdout output. */
  186. char argv0_expected[256];
  187. tor_snprintf(argv0_expected, sizeof(argv0_expected),
  188. "argv[0] = '%s'", filename);
  189. tt_str_op(smartlist_get(process_data->stdout_data, 0), OP_EQ,
  190. argv0_expected);
  191. tt_str_op(smartlist_get(process_data->stdout_data, 1), OP_EQ,
  192. "argv[1] = 'This is the first one'");
  193. tt_str_op(smartlist_get(process_data->stdout_data, 2), OP_EQ,
  194. "argv[2] = 'Second one'");
  195. tt_str_op(smartlist_get(process_data->stdout_data, 3), OP_EQ,
  196. "argv[3] = 'Third: Foo bar baz'");
  197. tt_str_op(smartlist_get(process_data->stdout_data, 4), OP_EQ,
  198. "Environment variable TOR_TEST_ENV = 'Hello, from Tor!'");
  199. tt_str_op(smartlist_get(process_data->stdout_data, 5), OP_EQ,
  200. "Output on stdout");
  201. tt_str_op(smartlist_get(process_data->stdout_data, 6), OP_EQ,
  202. "This is a new line");
  203. tt_str_op(smartlist_get(process_data->stdout_data, 7), OP_EQ,
  204. "Partial line on stdout ...end of partial line on stdout");
  205. tt_str_op(smartlist_get(process_data->stdout_data, 8), OP_EQ,
  206. "Read line from stdin: 'Hi process!'");
  207. tt_str_op(smartlist_get(process_data->stdout_data, 9), OP_EQ,
  208. "Read line from stdin: 'Can you read more than one line?'");
  209. tt_str_op(smartlist_get(process_data->stdout_data, 10), OP_EQ,
  210. "Read line from stdin: 'Can you read partial ... lines?'");
  211. tt_str_op(smartlist_get(process_data->stdout_data, 11), OP_EQ,
  212. "We are done for here, thank you!");
  213. /* Check stderr output. */
  214. tt_str_op(smartlist_get(process_data->stderr_data, 0), OP_EQ,
  215. "Output on stderr");
  216. tt_str_op(smartlist_get(process_data->stderr_data, 1), OP_EQ,
  217. "This is a new line");
  218. tt_str_op(smartlist_get(process_data->stderr_data, 2), OP_EQ,
  219. "Partial line on stderr ...end of partial line on stderr");
  220. done:
  221. process_data_free(process_data);
  222. process_free(process);
  223. process_free_all();
  224. }
  225. static void
  226. test_callbacks_terminate(void *arg)
  227. {
  228. (void)arg;
  229. const char *filename = NULL;
  230. #ifdef _WIN32
  231. filename = get_win32_test_binary_path();
  232. #else
  233. filename = TEST_PROCESS;
  234. #endif
  235. /* Initialize Process subsystem. */
  236. process_init();
  237. /* Process callback data. */
  238. process_data_t *process_data = process_data_new();
  239. /* Setup our process. */
  240. process_t *process = process_new(filename);
  241. process_set_data(process, process_data);
  242. process_set_exit_callback(process, process_exit_callback);
  243. /* Run our process. */
  244. process_status_t status;
  245. status = process_exec(process);
  246. tt_int_op(status, OP_EQ, PROCESS_STATUS_RUNNING);
  247. /* Zap our process. */
  248. process_terminate(process);
  249. /* Start our main loop. */
  250. run_main_loop();
  251. /* Check if our process is still running? */
  252. status = process_get_status(process);
  253. tt_int_op(status, OP_EQ, PROCESS_STATUS_NOT_RUNNING);
  254. done:
  255. process_data_free(process_data);
  256. process_free(process);
  257. process_free_all();
  258. }
  259. struct testcase_t slow_process_tests[] = {
  260. { "callbacks", test_callbacks, 0, NULL, NULL },
  261. { "callbacks_terminate", test_callbacks_terminate, 0, NULL, NULL },
  262. END_OF_TESTCASES
  263. };