testprocess.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. #include <SDL3/SDL.h>
  2. #include <SDL3/SDL_main.h>
  3. #include <SDL3/SDL_test.h>
  4. #ifdef SDL_PLATFORM_WINDOWS
  5. #define EXE ".exe"
  6. #else
  7. #define EXE ""
  8. #endif
  9. /*
  10. * FIXME: Additional tests:
  11. * - stdin to stdout
  12. * - stdin to stderr
  13. * - read env, using env set by parent process
  14. * - exit codes
  15. * - kill process
  16. * - waiting twice on process
  17. * - executing a non-existing program
  18. * - executing a process linking to a shared library not in the search paths
  19. * - piping processes
  20. * - forwarding SDL_IOFromFile stream to process
  21. * - forwarding process to SDL_IOFromFile stream
  22. */
  23. typedef struct {
  24. const char *childprocess_path;
  25. } TestProcessData;
  26. static TestProcessData parsed_args;
  27. static void SDLCALL setUpProcess(void **arg) {
  28. *arg = &parsed_args;
  29. }
  30. static const char *options[] = { "/path/to/childprocess" EXE, NULL };
  31. static char *env_key_val_string(const char *key) {
  32. const char *env = SDL_getenv(key);
  33. size_t size_result;
  34. char *result;
  35. if (env == NULL) {
  36. return NULL;
  37. }
  38. size_result = SDL_strlen(key) + SDL_strlen(env) + 2;
  39. result = SDL_malloc(size_result);
  40. SDL_snprintf(result, size_result, "%s=%s", key, env);
  41. return result;
  42. }
  43. static char **DuplicateEnvironment(const char *key0, ...)
  44. {
  45. va_list ap;
  46. size_t count = 1;
  47. size_t i;
  48. const char *keyN;
  49. char **result;
  50. if (key0) {
  51. if (SDL_strchr(key0, '=') || SDL_getenv(key0)) {
  52. count += 1;
  53. }
  54. va_start(ap, key0);
  55. for (;;) {
  56. keyN = va_arg(ap, const char *);
  57. if (keyN) {
  58. if (SDL_strchr(keyN, '=') || SDL_getenv(keyN)) {
  59. count += 1;
  60. }
  61. } else {
  62. break;
  63. }
  64. }
  65. va_end(ap);
  66. }
  67. result = SDL_calloc(count, sizeof(char *));
  68. i = 0;
  69. if (key0) {
  70. if (SDL_strchr(key0, '=')) {
  71. result[i++] = SDL_strdup(key0);
  72. } else if (SDL_getenv(key0)) {
  73. result[i++] = env_key_val_string(key0);
  74. }
  75. va_start(ap, key0);
  76. for (;;) {
  77. keyN = va_arg(ap, const char *);
  78. if (keyN) {
  79. if (SDL_strchr(keyN, '=')) {
  80. result[i++] = SDL_strdup(keyN);
  81. } else if (SDL_getenv(keyN)) {
  82. result[i++] = env_key_val_string(keyN);
  83. }
  84. } else {
  85. break;
  86. }
  87. }
  88. va_end(ap);
  89. }
  90. return result;
  91. }
  92. static void DestroyEnvironment(char **environment) {
  93. char **envp;
  94. if (!environment) {
  95. return;
  96. }
  97. for (envp = environment; *envp; envp++) {
  98. SDL_free(*envp);
  99. }
  100. SDL_free(environment);
  101. }
  102. static int SDLCALL process_testArguments(void *arg)
  103. {
  104. TestProcessData *data = (TestProcessData *)arg;
  105. const char *process_args[] = {
  106. data->childprocess_path,
  107. "--print-arguments",
  108. "--",
  109. "",
  110. " ",
  111. "a b c",
  112. "a\tb\tc\t",
  113. "\"a b\" c",
  114. "'a' 'b' 'c'",
  115. "%d%%%s",
  116. "\\t\\c",
  117. NULL
  118. };
  119. SDL_Process *process = NULL;
  120. char *buffer;
  121. int exit_code;
  122. int i;
  123. process = SDL_CreateProcess(process_args, SDL_TRUE);
  124. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcess()");
  125. if (!process) {
  126. goto failed;
  127. }
  128. exit_code = 0xdeadbeef;
  129. buffer = (char *)SDL_ReadProcess(process, NULL, &exit_code);
  130. SDLTest_AssertCheck(buffer != NULL, "SDL_ReadProcess()");
  131. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  132. if (!buffer) {
  133. goto failed;
  134. }
  135. for (i = 3; process_args[i]; i++) {
  136. char line[64];
  137. SDL_snprintf(line, sizeof(line), "|%d=%s|", i - 3, process_args[i]);
  138. SDLTest_AssertCheck(!!SDL_strstr(buffer, line), "Check %s is in output", line);
  139. }
  140. SDL_free(buffer);
  141. SDLTest_AssertPass("About to destroy process");
  142. SDL_DestroyProcess(process);
  143. return TEST_COMPLETED;
  144. failed:
  145. SDL_DestroyProcess(process);
  146. return TEST_ABORTED;
  147. }
  148. static int SDLCALL process_testInheritedEnv(void *arg)
  149. {
  150. TestProcessData *data = (TestProcessData *)arg;
  151. const char *process_args[] = {
  152. data->childprocess_path,
  153. "--print-environment",
  154. "--expect-env", NULL,
  155. NULL,
  156. };
  157. SDL_PropertiesID props;
  158. SDL_Process *process = NULL;
  159. Sint64 pid;
  160. SDL_IOStream *process_stdout = NULL;
  161. char buffer[256];
  162. SDL_bool wait_result;
  163. int exit_code;
  164. static const char *const TEST_ENV_KEY = "testprocess_environment";
  165. char *test_env_val = NULL;
  166. test_env_val = SDLTest_RandomAsciiStringOfSize(32);
  167. SDLTest_AssertPass("Setting parent environment variable %s=%s", TEST_ENV_KEY, test_env_val);
  168. SDL_setenv(TEST_ENV_KEY, test_env_val, 1);
  169. SDL_snprintf(buffer, sizeof(buffer), "%s=%s", TEST_ENV_KEY, test_env_val);
  170. process_args[3] = buffer;
  171. props = SDL_CreateProperties();
  172. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)process_args);
  173. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_APP);
  174. process = SDL_CreateProcessWithProperties(props);
  175. SDL_DestroyProperties(props);
  176. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcessWithProperties()");
  177. if (!process) {
  178. goto failed;
  179. }
  180. props = SDL_GetProcessProperties(process);
  181. SDLTest_AssertCheck(props != 0, "SDL_GetProcessProperties()");
  182. pid = SDL_GetNumberProperty(props, SDL_PROP_PROCESS_PID_NUMBER, 0);
  183. SDLTest_AssertCheck(pid != 0, "Checking process ID, expected non-zero, got %" SDL_PRIs64, pid);
  184. process_stdout = (SDL_IOStream *)SDL_GetPointerProperty(props, SDL_PROP_PROCESS_STDOUT_POINTER, NULL);
  185. SDLTest_AssertCheck(process_stdout != NULL, "SDL_GetPointerProperty(SDL_PROP_PROCESS_STDOUT_POINTER) returns a valid IO stream");
  186. if (!process_stdout) {
  187. goto failed;
  188. }
  189. for (;;) {
  190. size_t amount_read;
  191. amount_read = SDL_ReadIO(process_stdout, buffer, sizeof(buffer) - 1);
  192. if (amount_read > 0) {
  193. buffer[amount_read] = '\0';
  194. SDLTest_Log("READ: %s", buffer);
  195. } else if (SDL_GetIOStatus(process_stdout) != SDL_IO_STATUS_NOT_READY) {
  196. break;
  197. }
  198. SDL_Delay(10);
  199. }
  200. SDLTest_AssertPass("About to wait on process");
  201. exit_code = 0xdeadbeef;
  202. wait_result = SDL_WaitProcess(process, SDL_TRUE, &exit_code);
  203. SDLTest_AssertCheck(wait_result == SDL_TRUE, "Process should have closed when closing stdin");
  204. SDLTest_AssertPass("exit_code will be != 0 when environment variable was not set");
  205. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  206. SDLTest_AssertPass("About to destroy process");
  207. SDL_DestroyProcess(process);
  208. SDL_free(test_env_val);
  209. return TEST_COMPLETED;
  210. failed:
  211. SDL_free(test_env_val);
  212. SDL_DestroyProcess(process);
  213. return TEST_ABORTED;
  214. }
  215. static int SDLCALL process_testNewEnv(void *arg)
  216. {
  217. TestProcessData *data = (TestProcessData *)arg;
  218. const char *process_args[] = {
  219. data->childprocess_path,
  220. "--print-environment",
  221. "--expect-env", NULL,
  222. NULL,
  223. };
  224. char **process_env;
  225. SDL_PropertiesID props;
  226. SDL_Process *process = NULL;
  227. Sint64 pid;
  228. SDL_IOStream *process_stdout = NULL;
  229. char buffer[256];
  230. SDL_bool wait_result;
  231. int exit_code;
  232. static const char *const TEST_ENV_KEY = "testprocess_environment";
  233. char *test_env_val = NULL;
  234. test_env_val = SDLTest_RandomAsciiStringOfSize(32);
  235. SDL_snprintf(buffer, sizeof(buffer), "%s=%s", TEST_ENV_KEY, test_env_val);
  236. process_args[3] = buffer;
  237. process_env = DuplicateEnvironment("PATH", "LD_LIBRARY_PATH", "DYLD_LIBRARY_PATH", buffer, NULL);
  238. props = SDL_CreateProperties();
  239. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)process_args);
  240. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER, (void *)process_env);
  241. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_APP);
  242. process = SDL_CreateProcessWithProperties(props);
  243. SDL_DestroyProperties(props);
  244. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcessWithProperties()");
  245. if (!process) {
  246. goto failed;
  247. }
  248. props = SDL_GetProcessProperties(process);
  249. SDLTest_AssertCheck(props != 0, "SDL_GetProcessProperties()");
  250. pid = SDL_GetNumberProperty(props, SDL_PROP_PROCESS_PID_NUMBER, 0);
  251. SDLTest_AssertCheck(pid != 0, "Checking process ID, expected non-zero, got %" SDL_PRIs64, pid);
  252. process_stdout = (SDL_IOStream *)SDL_GetPointerProperty(props, SDL_PROP_PROCESS_STDOUT_POINTER, NULL);
  253. SDLTest_AssertCheck(process_stdout != NULL, "SDL_GetPointerProperty(SDL_PROP_PROCESS_STDOUT_POINTER) returns a valid IO stream");
  254. if (!process_stdout) {
  255. goto failed;
  256. }
  257. for (;;) {
  258. size_t amount_read;
  259. amount_read = SDL_ReadIO(process_stdout, buffer, sizeof(buffer) - 1);
  260. if (amount_read > 0) {
  261. buffer[amount_read] = '\0';
  262. SDLTest_Log("READ: %s", buffer);
  263. } else if (SDL_GetIOStatus(process_stdout) != SDL_IO_STATUS_NOT_READY) {
  264. break;
  265. }
  266. SDL_Delay(10);
  267. }
  268. SDLTest_AssertPass("About to wait on process");
  269. exit_code = 0xdeadbeef;
  270. wait_result = SDL_WaitProcess(process, SDL_TRUE, &exit_code);
  271. SDLTest_AssertCheck(wait_result == SDL_TRUE, "Process should have closed when closing stdin");
  272. SDLTest_AssertPass("exit_code will be != 0 when environment variable was not set");
  273. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  274. SDLTest_AssertPass("About to destroy process");
  275. SDL_DestroyProcess(process);
  276. DestroyEnvironment(process_env);
  277. SDL_free(test_env_val);
  278. return TEST_COMPLETED;
  279. failed:
  280. SDL_free(test_env_val);
  281. SDL_DestroyProcess(process);
  282. DestroyEnvironment(process_env);
  283. return TEST_ABORTED;
  284. }
  285. static int process_testStdinToStdout(void *arg)
  286. {
  287. TestProcessData *data = (TestProcessData *)arg;
  288. const char *process_args[] = {
  289. data->childprocess_path,
  290. "--stdin-to-stdout",
  291. NULL,
  292. };
  293. const char **process_env = NULL;
  294. SDL_PropertiesID props;
  295. SDL_Process *process = NULL;
  296. Sint64 pid;
  297. SDL_IOStream *process_stdin = NULL;
  298. SDL_IOStream *process_stdout = NULL;
  299. const char *text_in = "Tests whether we can write to stdin and read from stdout\r\n{'succes': true, 'message': 'Success!'}\r\nYippie ka yee\r\nEOF";
  300. size_t amount_written;
  301. size_t amount_to_write;
  302. char buffer[128];
  303. size_t total_read;
  304. SDL_bool wait_result;
  305. int exit_code;
  306. props = SDL_CreateProperties();
  307. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)process_args);
  308. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER, (void *)process_env);
  309. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_APP);
  310. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_APP);
  311. process = SDL_CreateProcessWithProperties(props);
  312. SDL_DestroyProperties(props);
  313. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcessWithProperties()");
  314. if (!process) {
  315. goto failed;
  316. }
  317. props = SDL_GetProcessProperties(process);
  318. SDLTest_AssertCheck(props != 0, "SDL_GetProcessProperties()");
  319. pid = SDL_GetNumberProperty(props, SDL_PROP_PROCESS_PID_NUMBER, 0);
  320. SDLTest_AssertCheck(pid != 0, "Checking process ID, expected non-zero, got %" SDL_PRIs64, pid);
  321. process_stdin = (SDL_IOStream *)SDL_GetPointerProperty(props, SDL_PROP_PROCESS_STDIN_POINTER, NULL);
  322. SDLTest_AssertCheck(process_stdin != NULL, "SDL_GetPointerProperty(SDL_PROP_PROCESS_STDIN_POINTER) returns a valid IO stream");
  323. process_stdout = (SDL_IOStream *)SDL_GetPointerProperty(props, SDL_PROP_PROCESS_STDOUT_POINTER, NULL);
  324. SDLTest_AssertCheck(process_stdout != NULL, "SDL_GetPointerProperty(SDL_PROP_PROCESS_STDOUT_POINTER) returns a valid IO stream");
  325. if (!process_stdin || !process_stdout) {
  326. goto failed;
  327. }
  328. SDLTest_AssertPass("About to write to process");
  329. amount_to_write = SDL_strlen(text_in);
  330. amount_written = SDL_WriteIO(process_stdin, text_in, amount_to_write);
  331. SDLTest_AssertCheck(amount_written == amount_to_write, "SDL_WriteIO(subprocess.stdin) wrote %" SDL_PRIu64 " bytes, expected %" SDL_PRIu64, (Uint64)amount_written, (Uint64)amount_to_write);
  332. if (amount_to_write != amount_written) {
  333. goto failed;
  334. }
  335. SDL_FlushIO(process_stdin);
  336. total_read = 0;
  337. buffer[0] = '\0';
  338. for (;;) {
  339. size_t amount_read;
  340. if (total_read >= sizeof(buffer) - 1) {
  341. SDLTest_AssertCheck(0, "Buffer is too small for input data.");
  342. goto failed;
  343. }
  344. SDLTest_AssertPass("About to read from process");
  345. amount_read = SDL_ReadIO(process_stdout, buffer + total_read, sizeof(buffer) - total_read - 1);
  346. if (amount_read == 0 && SDL_GetIOStatus(process_stdout) != SDL_IO_STATUS_NOT_READY) {
  347. break;
  348. }
  349. total_read += amount_read;
  350. buffer[total_read] = '\0';
  351. if (total_read >= sizeof(buffer) - 1 || SDL_strstr(buffer, "EOF")) {
  352. break;
  353. }
  354. SDL_Delay(10);
  355. }
  356. SDLTest_Log("Text read from subprocess: %s", buffer);
  357. SDLTest_AssertCheck(SDL_strcmp(buffer, text_in) == 0, "Subprocess stdout should match text written to stdin");
  358. SDLTest_AssertPass("About to close stdin");
  359. /* Closing stdin of `subprocessstdin --stdin-to-stdout` should close the process */
  360. SDL_CloseIO(process_stdin);
  361. process_stdin = (SDL_IOStream *)SDL_GetPointerProperty(props, SDL_PROP_PROCESS_STDIN_POINTER, NULL);
  362. SDLTest_AssertCheck(process_stdin == NULL, "SDL_GetPointerProperty(SDL_PROP_PROCESS_STDIN_POINTER) is cleared after close");
  363. SDLTest_AssertPass("About to wait on process");
  364. exit_code = 0xdeadbeef;
  365. wait_result = SDL_WaitProcess(process, SDL_TRUE, &exit_code);
  366. SDLTest_AssertCheck(wait_result == SDL_TRUE, "Process should have closed when closing stdin");
  367. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  368. if (!wait_result) {
  369. SDL_bool killed;
  370. SDL_Log("About to kill process");
  371. killed = SDL_KillProcess(process, SDL_TRUE);
  372. SDLTest_AssertCheck(killed, "SDL_KillProcess succeeded");
  373. }
  374. SDLTest_AssertPass("About to destroy process");
  375. SDL_DestroyProcess(process);
  376. return TEST_COMPLETED;
  377. failed:
  378. SDL_DestroyProcess(process);
  379. return TEST_ABORTED;
  380. }
  381. static int process_testSimpleStdinToStdout(void *arg)
  382. {
  383. TestProcessData *data = (TestProcessData *)arg;
  384. const char *process_args[] = {
  385. data->childprocess_path,
  386. "--stdin-to-stdout",
  387. NULL,
  388. };
  389. SDL_Process *process = NULL;
  390. const char *text_in = "Tests whether we can write to stdin and read from stdout\r\n{'succes': true, 'message': 'Success!'}\r\nYippie ka yee\r\nEOF";
  391. char *buffer;
  392. SDL_bool result;
  393. int exit_code;
  394. process = SDL_CreateProcess(process_args, SDL_TRUE);
  395. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcess()");
  396. if (!process) {
  397. goto failed;
  398. }
  399. SDLTest_AssertPass("About to write to process");
  400. result = SDL_WriteProcess(process, text_in, SDL_strlen(text_in), SDL_TRUE);
  401. SDLTest_AssertCheck(result, "SDL_WriteProcess()");
  402. if (!result) {
  403. goto failed;
  404. }
  405. exit_code = 0xdeadbeef;
  406. buffer = (char *)SDL_ReadProcess(process, NULL, &exit_code);
  407. SDLTest_AssertCheck(buffer != NULL, "SDL_ReadProcess()");
  408. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  409. if (!buffer) {
  410. goto failed;
  411. }
  412. SDLTest_Log("Text read from subprocess: %s", buffer);
  413. SDLTest_AssertCheck(SDL_strcmp(buffer, text_in) == 0, "Subprocess stdout should match text written to stdin");
  414. SDL_free(buffer);
  415. SDLTest_AssertPass("About to destroy process");
  416. SDL_DestroyProcess(process);
  417. return TEST_COMPLETED;
  418. failed:
  419. SDL_DestroyProcess(process);
  420. return TEST_ABORTED;
  421. }
  422. static int process_testMultiprocessStdinToStdout(void *arg)
  423. {
  424. TestProcessData *data = (TestProcessData *)arg;
  425. const char *process_args[] = {
  426. data->childprocess_path,
  427. "--stdin-to-stdout",
  428. NULL,
  429. };
  430. SDL_Process *process1 = NULL;
  431. SDL_Process *process2 = NULL;
  432. SDL_PropertiesID props;
  433. const char *text_in = "Tests whether we can write to stdin and read from stdout\r\n{'succes': true, 'message': 'Success!'}\r\nYippie ka yee\r\nEOF";
  434. char *buffer;
  435. SDL_bool result;
  436. int exit_code;
  437. process1 = SDL_CreateProcess(process_args, SDL_TRUE);
  438. SDLTest_AssertCheck(process1 != NULL, "SDL_CreateProcess()");
  439. if (!process1) {
  440. goto failed;
  441. }
  442. props = SDL_CreateProperties();
  443. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)process_args);
  444. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_REDIRECT);
  445. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_POINTER, SDL_GetPointerProperty(SDL_GetProcessProperties(process1), SDL_PROP_PROCESS_STDOUT_POINTER, NULL));
  446. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_APP);
  447. process2 = SDL_CreateProcessWithProperties(props);
  448. SDL_DestroyProperties(props);
  449. SDLTest_AssertCheck(process2 != NULL, "SDL_CreateProcess()");
  450. if (!process2) {
  451. goto failed;
  452. }
  453. SDLTest_AssertPass("About to write to process");
  454. result = SDL_WriteProcess(process1, text_in, SDL_strlen(text_in), SDL_TRUE);
  455. SDLTest_AssertCheck(result, "SDL_WriteProcess()");
  456. if (!result) {
  457. goto failed;
  458. }
  459. exit_code = 0xdeadbeef;
  460. buffer = (char *)SDL_ReadProcess(process2, NULL, &exit_code);
  461. SDLTest_AssertCheck(buffer != NULL, "SDL_ReadProcess()");
  462. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  463. if (!buffer) {
  464. goto failed;
  465. }
  466. SDLTest_Log("Text read from subprocess: %s", buffer);
  467. SDLTest_AssertCheck(SDL_strcmp(buffer, text_in) == 0, "Subprocess stdout should match text written to stdin");
  468. SDL_free(buffer);
  469. SDLTest_AssertPass("About to destroy processes");
  470. SDL_DestroyProcess(process1);
  471. SDL_DestroyProcess(process2);
  472. return TEST_COMPLETED;
  473. failed:
  474. SDL_DestroyProcess(process1);
  475. SDL_DestroyProcess(process2);
  476. return TEST_ABORTED;
  477. }
  478. static const SDLTest_TestCaseReference processTestArguments = {
  479. process_testArguments, "process_testArguments", "Test passing arguments to child process", TEST_ENABLED
  480. };
  481. static const SDLTest_TestCaseReference processTestIneritedEnv = {
  482. process_testInheritedEnv, "process_testInheritedEnv", "Test inheriting environment from parent process", TEST_ENABLED
  483. };
  484. static const SDLTest_TestCaseReference processTestNewEnv = {
  485. process_testNewEnv, "process_testNewEnv", "Test creating new environment for child process", TEST_ENABLED
  486. };
  487. static const SDLTest_TestCaseReference processTestStdinToStdout = {
  488. process_testStdinToStdout, "process_testStdinToStdout", "Test writing to stdin and reading from stdout", TEST_ENABLED
  489. };
  490. static const SDLTest_TestCaseReference processTestSimpleStdinToStdout = {
  491. process_testSimpleStdinToStdout, "process_testSimpleStdinToStdout", "Test writing to stdin and reading from stdout using the simplified API", TEST_ENABLED
  492. };
  493. static const SDLTest_TestCaseReference processTestMultiprocessStdinToStdout = {
  494. process_testMultiprocessStdinToStdout, "process_testMultiprocessStdinToStdout", "Test writing to stdin and reading from stdout using the simplified API", TEST_ENABLED
  495. };
  496. static const SDLTest_TestCaseReference *processTests[] = {
  497. &processTestArguments,
  498. &processTestIneritedEnv,
  499. &processTestNewEnv,
  500. &processTestStdinToStdout,
  501. &processTestSimpleStdinToStdout,
  502. &processTestMultiprocessStdinToStdout,
  503. NULL
  504. };
  505. static SDLTest_TestSuiteReference processTestSuite = {
  506. "Process",
  507. setUpProcess,
  508. processTests,
  509. NULL
  510. };
  511. static SDLTest_TestSuiteReference *testSuites[] = {
  512. &processTestSuite,
  513. NULL
  514. };
  515. int main(int argc, char *argv[])
  516. {
  517. int i;
  518. int result;
  519. SDLTest_CommonState *state;
  520. SDLTest_TestSuiteRunner *runner;
  521. /* Initialize test framework */
  522. state = SDLTest_CommonCreateState(argv, 0);
  523. if (!state) {
  524. return 1;
  525. }
  526. runner = SDLTest_CreateTestSuiteRunner(state, testSuites);
  527. /* Enable standard application logging */
  528. SDL_SetLogPriority(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_INFO);
  529. /* Parse commandline */
  530. for (i = 1; i < argc;) {
  531. int consumed;
  532. consumed = SDLTest_CommonArg(state, i);
  533. if (!consumed) {
  534. if (!parsed_args.childprocess_path) {
  535. parsed_args.childprocess_path = argv[i];
  536. consumed = 1;
  537. }
  538. }
  539. if (consumed <= 0) {
  540. SDLTest_CommonLogUsage(state, argv[0], options);
  541. return 1;
  542. }
  543. i += consumed;
  544. }
  545. if (!parsed_args.childprocess_path) {
  546. SDLTest_CommonLogUsage(state, argv[0], options);
  547. return 1;
  548. }
  549. result = SDLTest_ExecuteTestSuiteRunner(runner);
  550. SDL_Quit();
  551. SDLTest_DestroyTestSuiteRunner(runner);
  552. SDLTest_CommonDestroyState(state);
  553. return result;
  554. }