testprocess.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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 stderr
  12. */
  13. typedef struct {
  14. const char *childprocess_path;
  15. } TestProcessData;
  16. static TestProcessData parsed_args;
  17. static void SDLCALL setUpProcess(void **arg) {
  18. *arg = &parsed_args;
  19. }
  20. static const char *options[] = {
  21. "/path/to/childprocess" EXE,
  22. NULL
  23. };
  24. static char **CreateArguments(int ignore, ...) {
  25. va_list ap;
  26. size_t count = 1;
  27. size_t i;
  28. char **result;
  29. va_start(ap, ignore);
  30. for (;;) {
  31. const char *keyN = va_arg(ap, const char *);
  32. if (!keyN) {
  33. break;
  34. }
  35. count += 1;
  36. }
  37. va_end(ap);
  38. result = SDL_calloc(count, sizeof(char *));
  39. i = 0;
  40. va_start(ap, ignore);
  41. for (;;) {
  42. const char *keyN = va_arg(ap, const char *);
  43. if (!keyN) {
  44. break;
  45. }
  46. result[i++] = SDL_strdup(keyN);
  47. }
  48. va_end(ap);
  49. return result;
  50. }
  51. static void DestroyStringArray(char **list) {
  52. char **current;
  53. if (!list) {
  54. return;
  55. }
  56. for (current = list; *current; current++) {
  57. SDL_free(*current);
  58. }
  59. SDL_free(list);
  60. }
  61. static int SDLCALL process_testArguments(void *arg)
  62. {
  63. TestProcessData *data = (TestProcessData *)arg;
  64. const char *process_args[] = {
  65. data->childprocess_path,
  66. "--print-arguments",
  67. "--",
  68. "",
  69. " ",
  70. "a b c",
  71. "a\tb\tc\t",
  72. "\"a b\" c",
  73. "'a' 'b' 'c'",
  74. "%d%%%s",
  75. "\\t\\c",
  76. "evil\\",
  77. "a\\b\"c\\",
  78. "\"\\^&|<>%", /* characters with a special meaning */
  79. NULL
  80. };
  81. SDL_Process *process = NULL;
  82. char *buffer;
  83. int exit_code;
  84. int i;
  85. size_t total_read = 0;
  86. process = SDL_CreateProcess(process_args, true);
  87. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcess()");
  88. if (!process) {
  89. goto failed;
  90. }
  91. exit_code = 0xdeadbeef;
  92. buffer = (char *)SDL_ReadProcess(process, &total_read, &exit_code);
  93. SDLTest_AssertCheck(buffer != NULL, "SDL_ReadProcess()");
  94. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  95. if (!buffer) {
  96. goto failed;
  97. }
  98. SDLTest_LogEscapedString("stdout of process: ", buffer, total_read);
  99. for (i = 3; process_args[i]; i++) {
  100. char line[64];
  101. SDL_snprintf(line, sizeof(line), "|%d=%s|", i - 3, process_args[i]);
  102. SDLTest_AssertCheck(!!SDL_strstr(buffer, line), "Check %s is in output", line);
  103. }
  104. SDL_free(buffer);
  105. SDLTest_AssertPass("About to destroy process");
  106. SDL_DestroyProcess(process);
  107. return TEST_COMPLETED;
  108. failed:
  109. SDL_DestroyProcess(process);
  110. return TEST_ABORTED;
  111. }
  112. static int SDLCALL process_testexitCode(void *arg)
  113. {
  114. TestProcessData *data = (TestProcessData *)arg;
  115. int i;
  116. int exit_codes[] = {
  117. 0, 13, 31, 127, 255
  118. };
  119. for (i = 0; i < SDL_arraysize(exit_codes); i++) {
  120. bool wait_result;
  121. SDL_Process *process = NULL;
  122. char **process_args = NULL;
  123. char number_buffer[8];
  124. int exit_code;
  125. SDL_snprintf(number_buffer, sizeof(number_buffer), "%d", exit_codes[i]);
  126. process_args = CreateArguments(0, data->childprocess_path, "--exit-code", number_buffer, NULL);
  127. process = SDL_CreateProcess((const char * const *)process_args, false);
  128. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcess()");
  129. if (!process) {
  130. goto failed;
  131. }
  132. exit_code = 0xdeadbeef;
  133. SDLTest_AssertPass("About to wait on process (first time)");
  134. wait_result = SDL_WaitProcess(process, true, &exit_code);
  135. SDLTest_AssertCheck(wait_result == true, "SDL_WaitProcess(): Process should have closed immediately");
  136. SDLTest_AssertCheck(exit_code == exit_codes[i], "SDL_WaitProcess(): Exit code should be %d, is %d", exit_codes[i], exit_code);
  137. exit_code = 0xdeadbeef;
  138. SDLTest_AssertPass("About to wait on process (second time)");
  139. wait_result = SDL_WaitProcess(process, true, &exit_code);
  140. SDLTest_AssertCheck(wait_result == true, "SDL_WaitProcess(): Process should have closed immediately");
  141. SDLTest_AssertCheck(exit_code == exit_codes[i], "SDL_WaitProcess(): Exit code should be %d, is %d", exit_codes[i], exit_code);
  142. SDLTest_AssertPass("About to destroy process");
  143. SDL_DestroyProcess(process);
  144. DestroyStringArray(process_args);
  145. continue;
  146. failed:
  147. SDL_DestroyProcess(process);
  148. DestroyStringArray(process_args);
  149. return TEST_ABORTED;
  150. }
  151. return TEST_COMPLETED;
  152. #if 0
  153. failed:
  154. SDL_DestroyProcess(process);
  155. DestroyStringArray(process_args);
  156. return TEST_ABORTED;
  157. #endif
  158. }
  159. static int SDLCALL process_testInheritedEnv(void *arg)
  160. {
  161. TestProcessData *data = (TestProcessData *)arg;
  162. const char *process_args[] = {
  163. data->childprocess_path,
  164. "--print-environment",
  165. NULL,
  166. };
  167. SDL_PropertiesID props;
  168. SDL_Process *process = NULL;
  169. Sint64 pid;
  170. int exit_code;
  171. char random_env1[64];
  172. char random_env2[64];
  173. static const char *const TEST_ENV_KEY1 = "testprocess_inherited_var";
  174. static const char *const TEST_ENV_KEY2 = "testprocess_other_var";
  175. char *test_env_val1 = NULL;
  176. char *test_env_val2 = NULL;
  177. char *buffer = NULL;
  178. test_env_val1 = SDLTest_RandomAsciiStringOfSize(32);
  179. SDL_snprintf(random_env1, sizeof(random_env1), "%s=%s", TEST_ENV_KEY1, test_env_val1);
  180. SDLTest_AssertPass("Setting parent environment variable %s=%s", TEST_ENV_KEY1, test_env_val1);
  181. SDL_SetEnvironmentVariable(SDL_GetEnvironment(), TEST_ENV_KEY1, test_env_val1, true);
  182. SDL_UnsetEnvironmentVariable(SDL_GetEnvironment(), TEST_ENV_KEY2);
  183. props = SDL_CreateProperties();
  184. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)process_args);
  185. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_APP);
  186. process = SDL_CreateProcessWithProperties(props);
  187. SDL_DestroyProperties(props);
  188. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcessWithProperties()");
  189. if (!process) {
  190. goto failed;
  191. }
  192. test_env_val2 = SDLTest_RandomAsciiStringOfSize(32);
  193. SDL_snprintf(random_env2, sizeof(random_env2), "%s=%s", TEST_ENV_KEY2, test_env_val2);
  194. SDLTest_AssertPass("Setting parent environment variable %s=%s", TEST_ENV_KEY2, test_env_val2);
  195. SDL_SetEnvironmentVariable(SDL_GetEnvironment(),TEST_ENV_KEY2, test_env_val2, true);
  196. SDLTest_AssertCheck(SDL_strcmp(test_env_val1, test_env_val2) != 0, "Sanity checking the 2 random environment variables are not identical");
  197. props = SDL_GetProcessProperties(process);
  198. SDLTest_AssertCheck(props != 0, "SDL_GetProcessProperties()");
  199. pid = SDL_GetNumberProperty(props, SDL_PROP_PROCESS_PID_NUMBER, 0);
  200. SDLTest_AssertCheck(pid != 0, "Checking process ID, expected non-zero, got %" SDL_PRIs64, pid);
  201. exit_code = 0xdeadbeef;
  202. buffer = (char *)SDL_ReadProcess(process, NULL, &exit_code);
  203. SDLTest_AssertCheck(buffer != NULL, "SDL_ReadProcess()");
  204. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  205. SDLTest_AssertCheck(SDL_strstr(buffer, random_env1) != NULL, "Environment of child should contain \"%s\"", test_env_val1);
  206. SDLTest_AssertCheck(SDL_strstr(buffer, random_env2) == NULL, "Environment of child should not contain \"%s\"", test_env_val2);
  207. SDLTest_AssertPass("About to destroy process");
  208. SDL_DestroyProcess(process);
  209. SDL_free(test_env_val1);
  210. SDL_free(test_env_val2);
  211. SDL_free(buffer);
  212. return TEST_COMPLETED;
  213. failed:
  214. SDL_free(test_env_val1);
  215. SDL_free(test_env_val2);
  216. SDL_DestroyProcess(process);
  217. SDL_free(buffer);
  218. return TEST_ABORTED;
  219. }
  220. static int SDLCALL process_testNewEnv(void *arg)
  221. {
  222. TestProcessData *data = (TestProcessData *)arg;
  223. const char *process_args[] = {
  224. data->childprocess_path,
  225. "--print-environment",
  226. NULL,
  227. };
  228. SDL_Environment *process_env;
  229. SDL_PropertiesID props;
  230. SDL_Process *process = NULL;
  231. Sint64 pid;
  232. int exit_code;
  233. char random_env1[64];
  234. char random_env2[64];
  235. static const char *const TEST_ENV_KEY1 = "testprocess_inherited_var";
  236. static const char *const TEST_ENV_KEY2 = "testprocess_other_var";
  237. char *test_env_val1 = NULL;
  238. char *test_env_val2 = NULL;
  239. char *buffer = NULL;
  240. size_t total_read = 0;
  241. test_env_val1 = SDLTest_RandomAsciiStringOfSize(32);
  242. SDL_snprintf(random_env1, sizeof(random_env1), "%s=%s", TEST_ENV_KEY1, test_env_val1);
  243. SDLTest_AssertPass("Unsetting parent environment variable %s", TEST_ENV_KEY1);
  244. SDL_UnsetEnvironmentVariable(SDL_GetEnvironment(), TEST_ENV_KEY1);
  245. process_env = SDL_CreateEnvironment(true);
  246. SDL_SetEnvironmentVariable(process_env, "PATH", SDL_GetEnvironmentVariable(SDL_GetEnvironment(), "PATH"), true);
  247. SDL_SetEnvironmentVariable(process_env, "LD_LIBRARY_PATH", SDL_GetEnvironmentVariable(SDL_GetEnvironment(), "LD_LIBRARY_PATH"), true);
  248. SDL_SetEnvironmentVariable(process_env, "DYLD_LIBRARY_PATH", SDL_GetEnvironmentVariable(SDL_GetEnvironment(), "DYLD_LIBRARY_PATH"), true);
  249. SDL_SetEnvironmentVariable(process_env, TEST_ENV_KEY1, test_env_val1, true);
  250. test_env_val2 = SDLTest_RandomAsciiStringOfSize(32);
  251. SDL_snprintf(random_env2, sizeof(random_env2), "%s=%s", TEST_ENV_KEY2, test_env_val1);
  252. SDLTest_AssertPass("Setting parent environment variable %s=%s", TEST_ENV_KEY2, test_env_val2);
  253. SDL_SetEnvironmentVariable(SDL_GetEnvironment(), TEST_ENV_KEY2, test_env_val2, true);
  254. SDLTest_AssertCheck(SDL_strcmp(test_env_val1, test_env_val2) != 0, "Sanity checking the 2 random environment variables are not identical");
  255. props = SDL_CreateProperties();
  256. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)process_args);
  257. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER, process_env);
  258. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_APP);
  259. process = SDL_CreateProcessWithProperties(props);
  260. SDL_DestroyProperties(props);
  261. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcessWithProperties()");
  262. if (!process) {
  263. goto failed;
  264. }
  265. props = SDL_GetProcessProperties(process);
  266. SDLTest_AssertCheck(props != 0, "SDL_GetProcessProperties()");
  267. pid = SDL_GetNumberProperty(props, SDL_PROP_PROCESS_PID_NUMBER, 0);
  268. SDLTest_AssertCheck(pid != 0, "Checking process ID, expected non-zero, got %" SDL_PRIs64, pid);
  269. exit_code = 0xdeadbeef;
  270. buffer = (char *)SDL_ReadProcess(process, &total_read, &exit_code);
  271. SDLTest_AssertCheck(buffer != NULL, "SDL_ReadProcess()");
  272. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  273. SDLTest_LogEscapedString("Text read from subprocess: ", buffer, total_read);
  274. SDLTest_AssertCheck(SDL_strstr(buffer, random_env1) != NULL, "Environment of child should contain \"%s\"", random_env1);
  275. SDLTest_AssertCheck(SDL_strstr(buffer, random_env2) == NULL, "Environment of child should not contain \"%s\"", random_env1);
  276. SDLTest_AssertPass("About to destroy process");
  277. SDL_DestroyProcess(process);
  278. SDL_DestroyEnvironment(process_env);
  279. SDL_free(test_env_val1);
  280. SDL_free(test_env_val2);
  281. SDL_free(buffer);
  282. return TEST_COMPLETED;
  283. failed:
  284. SDL_DestroyProcess(process);
  285. SDL_DestroyEnvironment(process_env);
  286. SDL_free(test_env_val1);
  287. SDL_free(test_env_val2);
  288. SDL_free(buffer);
  289. return TEST_ABORTED;
  290. }
  291. static int SDLCALL process_testKill(void *arg)
  292. {
  293. TestProcessData *data = (TestProcessData *)arg;
  294. const char *process_args[] = {
  295. data->childprocess_path,
  296. "--stdin",
  297. NULL,
  298. };
  299. SDL_Process *process = NULL;
  300. SDL_PropertiesID props;
  301. Sint64 pid;
  302. int result;
  303. int exit_code;
  304. SDLTest_AssertPass("About to call SDL_CreateProcess(true)");
  305. process = SDL_CreateProcess(process_args, true);
  306. if (!process) {
  307. goto failed;
  308. }
  309. props = SDL_GetProcessProperties(process);
  310. SDLTest_AssertCheck(props != 0, "SDL_GetProcessProperties()");
  311. pid = SDL_GetNumberProperty(props, SDL_PROP_PROCESS_PID_NUMBER, 0);
  312. SDLTest_AssertCheck(pid != 0, "Checking process ID, expected non-zero, got %" SDL_PRIs64, pid);
  313. exit_code = 0xdeadbeef;
  314. SDLTest_AssertPass("About to call SDL_WaitProcess(false)");
  315. result = SDL_WaitProcess(process, false, &exit_code);
  316. SDLTest_AssertCheck(result == false, "Process should not have exited yet");
  317. SDLTest_AssertPass("About to call SDL_KillProcess(false)");
  318. result = SDL_KillProcess(process, false);
  319. SDLTest_AssertCheck(result == true, "Process should have exited");
  320. exit_code = 0;
  321. SDLTest_AssertPass("About to call SDL_WaitProcess(true)");
  322. result = SDL_WaitProcess(process, true, &exit_code);
  323. SDLTest_AssertCheck(result == true, "Process should have exited");
  324. SDLTest_AssertCheck(exit_code != 0, "Exit code should be non-zero, is %d", exit_code);
  325. SDLTest_AssertPass("About to destroy process");
  326. SDL_DestroyProcess(process);
  327. return TEST_COMPLETED;
  328. failed:
  329. SDL_DestroyProcess(process);
  330. return TEST_ABORTED;
  331. }
  332. static int process_testStdinToStdout(void *arg)
  333. {
  334. TestProcessData *data = (TestProcessData *)arg;
  335. const char *process_args[] = {
  336. data->childprocess_path,
  337. "--stdin-to-stdout",
  338. NULL,
  339. };
  340. SDL_PropertiesID props;
  341. SDL_Process *process = NULL;
  342. Sint64 pid;
  343. SDL_IOStream *process_stdin = NULL;
  344. SDL_IOStream *process_stdout = NULL;
  345. SDL_IOStream *process_stderr = NULL;
  346. size_t text_in_size = 1 * 1024 * 1024;
  347. char *text_in = NULL;
  348. size_t total_written;
  349. size_t total_read;
  350. bool wait_result;
  351. int exit_code;
  352. SDL_IOStream *stdout_stream = NULL;
  353. char *stdout_stream_buf;
  354. int iteration_count = 0;
  355. text_in = SDLTest_RandomAsciiStringOfSize((int)text_in_size);
  356. /* Make sure text_in does not contain EOF */
  357. for (;;) {
  358. char *e = SDL_strstr(text_in, "EOF");
  359. if (!e) {
  360. break;
  361. }
  362. e[0] = 'N';
  363. }
  364. text_in[text_in_size - 3] = 'E';
  365. text_in[text_in_size - 2] = 'O';
  366. text_in[text_in_size - 1] = 'F';
  367. stdout_stream = SDL_IOFromDynamicMem();
  368. props = SDL_CreateProperties();
  369. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)process_args);
  370. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_APP);
  371. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_APP);
  372. process = SDL_CreateProcessWithProperties(props);
  373. SDL_DestroyProperties(props);
  374. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcessWithProperties()");
  375. if (!process) {
  376. goto failed;
  377. }
  378. props = SDL_GetProcessProperties(process);
  379. SDLTest_AssertCheck(props != 0, "SDL_GetProcessProperties()");
  380. pid = SDL_GetNumberProperty(props, SDL_PROP_PROCESS_PID_NUMBER, 0);
  381. SDLTest_AssertCheck(pid != 0, "Checking process ID, expected non-zero, got %" SDL_PRIs64, pid);
  382. process_stdin = SDL_GetProcessInput(process);
  383. SDLTest_AssertCheck(process_stdin != NULL, "SDL_GetPointerProperty(SDL_PROP_PROCESS_STDIN_POINTER) returns a valid IO stream");
  384. process_stdout = SDL_GetProcessOutput(process);
  385. SDLTest_AssertCheck(process_stdout != NULL, "SDL_GetPointerProperty(SDL_PROP_PROCESS_STDOUT_POINTER) returns a valid IO stream");
  386. process_stderr = (SDL_IOStream *)SDL_GetPointerProperty(props, SDL_PROP_PROCESS_STDERR_POINTER, NULL);
  387. SDLTest_AssertCheck(process_stderr == NULL, "SDL_GetPointerProperty(SDL_PROP_PROCESS_STDERR_POINTER) returns NULL");
  388. if (!process_stdin || !process_stdout) {
  389. goto failed;
  390. }
  391. total_written = 0;
  392. total_read = 0;
  393. for (;;) {
  394. int log_this_iteration = (iteration_count % 32) == 32;
  395. char local_buffer[16 * 4094];
  396. size_t amount_read;
  397. SDL_IOStatus io_status;
  398. if (total_written != text_in_size) {
  399. size_t amount_written;
  400. if (log_this_iteration) {
  401. SDLTest_AssertPass("About to SDL_WriteIO (%dth time)", iteration_count);
  402. }
  403. amount_written = SDL_WriteIO(process_stdin, text_in + total_written, text_in_size - total_written);
  404. if (log_this_iteration) {
  405. SDLTest_Log("SDL_WriteIO() -> %u (%dth time)", (unsigned)amount_written, iteration_count);
  406. }
  407. if (amount_written == 0) {
  408. io_status = SDL_GetIOStatus(process_stdin);
  409. if (io_status != SDL_IO_STATUS_NOT_READY) {
  410. SDLTest_Log("SDL_GetIOStatus(process_stdin) returns %d, breaking.", io_status);
  411. break;
  412. }
  413. }
  414. total_written += amount_written;
  415. SDL_FlushIO(process_stdin);
  416. }
  417. /* FIXME: this needs a rate limit */
  418. if (log_this_iteration) {
  419. SDLTest_AssertPass("About to SDL_ReadIO (%dth time)", iteration_count);
  420. }
  421. amount_read = SDL_ReadIO(process_stdout, local_buffer, sizeof(local_buffer));
  422. if (log_this_iteration) {
  423. SDLTest_Log("SDL_ReadIO() -> %u (%dth time)", (unsigned)amount_read, iteration_count);
  424. }
  425. if (amount_read == 0) {
  426. io_status = SDL_GetIOStatus(process_stdout);
  427. if (io_status != SDL_IO_STATUS_NOT_READY) {
  428. SDLTest_Log("SDL_GetIOStatus(process_stdout) returned %d, breaking.", io_status);
  429. break;
  430. }
  431. } else {
  432. total_read += amount_read;
  433. SDL_WriteIO(stdout_stream, local_buffer, amount_read);
  434. stdout_stream_buf = SDL_GetPointerProperty(SDL_GetIOProperties(stdout_stream), SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL);
  435. if (SDL_strstr(stdout_stream_buf, "EOF")) {
  436. SDLTest_Log("Found EOF in stdout");
  437. break;
  438. }
  439. }
  440. SDL_Delay(10);
  441. }
  442. SDLTest_Log("Wrote %" SDL_PRIu64 " bytes to process.stdin", (Uint64)total_written);
  443. SDLTest_Log("Read %" SDL_PRIu64 " bytes from process.stdout",(Uint64)total_read);
  444. stdout_stream_buf = SDL_GetPointerProperty(SDL_GetIOProperties(stdout_stream), SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL);
  445. SDLTest_CompareMemory(stdout_stream_buf, total_written, text_in, text_in_size);
  446. exit_code = 0xdeadbeef;
  447. wait_result = SDL_WaitProcess(process, false, &exit_code);
  448. SDLTest_AssertCheck(wait_result == false, "Process should not have closed yet");
  449. SDLTest_AssertPass("About to close stdin");
  450. /* Closing stdin of `subprocessstdin --stdin-to-stdout` should close the process */
  451. SDL_CloseIO(process_stdin);
  452. process_stdin = SDL_GetProcessInput(process);
  453. SDLTest_AssertCheck(process_stdin == NULL, "SDL_GetPointerProperty(SDL_PROP_PROCESS_STDIN_POINTER) is cleared after close");
  454. SDLTest_AssertPass("About to wait on process");
  455. exit_code = 0xdeadbeef;
  456. wait_result = SDL_WaitProcess(process, true, &exit_code);
  457. SDLTest_AssertCheck(wait_result == true, "Process should have closed when closing stdin");
  458. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  459. if (!wait_result) {
  460. bool killed;
  461. SDL_Log("About to kill process");
  462. killed = SDL_KillProcess(process, true);
  463. SDLTest_AssertCheck(killed, "SDL_KillProcess succeeded");
  464. }
  465. SDLTest_AssertPass("About to destroy process");
  466. SDL_DestroyProcess(process);
  467. SDL_CloseIO(stdout_stream);
  468. SDL_free(text_in);
  469. return TEST_COMPLETED;
  470. failed:
  471. SDL_DestroyProcess(process);
  472. SDL_CloseIO(stdout_stream);
  473. SDL_free(text_in);
  474. return TEST_ABORTED;
  475. }
  476. static int process_testStdinToStderr(void *arg)
  477. {
  478. TestProcessData *data = (TestProcessData *)arg;
  479. const char *process_args[] = {
  480. data->childprocess_path,
  481. "--stdin-to-stderr",
  482. NULL,
  483. };
  484. SDL_Process *process = NULL;
  485. SDL_IOStream *process_stdin = NULL;
  486. SDL_IOStream *process_stdout = NULL;
  487. SDL_IOStream *process_stderr = NULL;
  488. const char *text_in = "Tests whether we can write to stdin and read from stderr\r\n{'succes': true, 'message': 'Success!'}\r\nYippie ka yee\r\nEOF";
  489. size_t result;
  490. int exit_code;
  491. SDL_PropertiesID props;
  492. char buffer[256];
  493. size_t amount_read;
  494. props = SDL_CreateProperties();
  495. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)process_args);
  496. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_APP);
  497. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_NULL);
  498. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDERR_NUMBER, SDL_PROCESS_STDIO_APP);
  499. process = SDL_CreateProcessWithProperties(props);
  500. SDL_DestroyProperties(props);
  501. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcessWithProperties()");
  502. if (!process) {
  503. goto failed;
  504. }
  505. SDLTest_AssertPass("About to write to process");
  506. process_stdin = SDL_GetProcessInput(process);
  507. SDLTest_AssertCheck(process_stdin != NULL, "SDL_GetProcessInput()");
  508. result = SDL_WriteIO(process_stdin, text_in, SDL_strlen(text_in));
  509. SDLTest_AssertCheck(result == SDL_strlen(text_in), "SDL_WriteIO() wrote %d, expected %d", (int)result, (int)SDL_strlen(text_in));
  510. SDL_CloseIO(process_stdin);
  511. process_stdout = SDL_GetProcessOutput(process);
  512. SDLTest_AssertCheck(process_stdout == NULL, "Process has no stdout");
  513. process_stderr = SDL_GetPointerProperty(SDL_GetProcessProperties(process), SDL_PROP_PROCESS_STDERR_POINTER, NULL);
  514. SDLTest_AssertCheck(process_stderr != NULL, "Process has stderr");
  515. exit_code = 0xdeadbeef;
  516. result = SDL_WaitProcess(process, true, &exit_code);
  517. SDLTest_AssertCheck(result == true, "Process should have finished");
  518. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  519. amount_read = SDL_ReadIO(process_stderr, buffer, sizeof(buffer));
  520. SDLTest_CompareMemory(buffer, amount_read, text_in, SDL_strlen(text_in));
  521. SDLTest_AssertPass("About to destroy process");
  522. SDL_DestroyProcess(process);
  523. return TEST_COMPLETED;
  524. failed:
  525. SDL_DestroyProcess(process);
  526. return TEST_ABORTED;
  527. }
  528. static int process_testSimpleStdinToStdout(void *arg)
  529. {
  530. TestProcessData *data = (TestProcessData *)arg;
  531. const char *process_args[] = {
  532. data->childprocess_path,
  533. "--stdin-to-stdout",
  534. NULL,
  535. };
  536. SDL_Process *process = NULL;
  537. SDL_IOStream *input = NULL;
  538. 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";
  539. char *buffer;
  540. size_t result;
  541. int exit_code;
  542. size_t total_read = 0;
  543. process = SDL_CreateProcess(process_args, true);
  544. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcess()");
  545. if (!process) {
  546. goto failed;
  547. }
  548. SDLTest_AssertPass("About to write to process");
  549. input = SDL_GetProcessInput(process);
  550. SDLTest_AssertCheck(input != NULL, "SDL_GetProcessInput()");
  551. result = SDL_WriteIO(input, text_in, SDL_strlen(text_in));
  552. SDLTest_AssertCheck(result == SDL_strlen(text_in), "SDL_WriteIO() wrote %d, expected %d", (int)result, (int)SDL_strlen(text_in));
  553. SDL_CloseIO(input);
  554. input = SDL_GetProcessInput(process);
  555. SDLTest_AssertCheck(input == NULL, "SDL_GetProcessInput() after close");
  556. exit_code = 0xdeadbeef;
  557. buffer = (char *)SDL_ReadProcess(process, &total_read, &exit_code);
  558. SDLTest_AssertCheck(buffer != NULL, "SDL_ReadProcess()");
  559. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  560. if (!buffer) {
  561. goto failed;
  562. }
  563. SDLTest_LogEscapedString("Expected text read from subprocess: %s", text_in, SDL_strlen(text_in));
  564. SDLTest_LogEscapedString("Actual text read from subprocess: %s", buffer, total_read);
  565. SDLTest_AssertCheck(total_read == SDL_strlen(text_in), "Expected to read %u bytes, actually read %u bytes", (unsigned)SDL_strlen(text_in), (unsigned)total_read);
  566. SDLTest_AssertCheck(SDL_strcmp(buffer, text_in) == 0, "Subprocess stdout should match text written to stdin");
  567. SDL_free(buffer);
  568. SDLTest_AssertPass("About to destroy process");
  569. SDL_DestroyProcess(process);
  570. return TEST_COMPLETED;
  571. failed:
  572. SDL_DestroyProcess(process);
  573. return TEST_ABORTED;
  574. }
  575. static int process_testMultiprocessStdinToStdout(void *arg)
  576. {
  577. TestProcessData *data = (TestProcessData *)arg;
  578. const char *process_args[] = {
  579. data->childprocess_path,
  580. "--stdin-to-stdout",
  581. NULL,
  582. };
  583. SDL_Process *process1 = NULL;
  584. SDL_Process *process2 = NULL;
  585. SDL_PropertiesID props;
  586. SDL_IOStream *input = NULL;
  587. 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";
  588. char *buffer;
  589. size_t result;
  590. int exit_code;
  591. size_t total_read = 0;
  592. bool finished;
  593. process1 = SDL_CreateProcess(process_args, true);
  594. SDLTest_AssertCheck(process1 != NULL, "SDL_CreateProcess()");
  595. if (!process1) {
  596. goto failed;
  597. }
  598. props = SDL_CreateProperties();
  599. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)process_args);
  600. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_REDIRECT);
  601. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_POINTER, SDL_GetPointerProperty(SDL_GetProcessProperties(process1), SDL_PROP_PROCESS_STDOUT_POINTER, NULL));
  602. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_APP);
  603. SDLTest_AssertPass("About to call SDL_CreateProcessWithProperties");
  604. process2 = SDL_CreateProcessWithProperties(props);
  605. SDL_DestroyProperties(props);
  606. SDLTest_AssertCheck(process2 != NULL, "SDL_CreateProcess()");
  607. if (!process2) {
  608. goto failed;
  609. }
  610. SDLTest_AssertPass("About to write to process");
  611. input = SDL_GetProcessInput(process1);
  612. SDLTest_AssertCheck(input != NULL, "SDL_GetProcessInput()");
  613. result = SDL_WriteIO(input, text_in, SDL_strlen(text_in));
  614. SDLTest_AssertCheck(result == SDL_strlen(text_in), "SDL_WriteIO() wrote %d, expected %d", (int)result, (int)SDL_strlen(text_in));
  615. SDL_CloseIO(input);
  616. exit_code = 0xdeadbeef;
  617. finished = SDL_WaitProcess(process1, true, &exit_code);
  618. SDLTest_AssertCheck(finished == true, "process 1 should have finished");
  619. SDLTest_AssertCheck(exit_code == 0, "Exit code of process 1 should be 0, is %d", exit_code);
  620. exit_code = 0xdeadbeef;
  621. buffer = (char *)SDL_ReadProcess(process2, &total_read, &exit_code);
  622. SDLTest_AssertCheck(buffer != NULL, "SDL_ReadProcess()");
  623. SDLTest_AssertCheck(exit_code == 0, "Exit code of process 2 should be 0, is %d", exit_code);
  624. if (!buffer) {
  625. goto failed;
  626. }
  627. SDLTest_LogEscapedString("Expected text read from subprocess: ", text_in, SDL_strlen(text_in));
  628. SDLTest_LogEscapedString("Actual text read from subprocess: ", buffer, total_read);
  629. SDLTest_AssertCheck(total_read == SDL_strlen(text_in), "Expected to read %u bytes, actually read %u bytes", (unsigned)SDL_strlen(text_in), (unsigned)total_read);
  630. SDLTest_AssertCheck(SDL_strcmp(buffer, text_in) == 0, "Subprocess stdout should match text written to stdin");
  631. SDL_free(buffer);
  632. SDLTest_AssertPass("About to destroy processes");
  633. SDL_DestroyProcess(process1);
  634. SDL_DestroyProcess(process2);
  635. return TEST_COMPLETED;
  636. failed:
  637. SDL_DestroyProcess(process1);
  638. SDL_DestroyProcess(process2);
  639. return TEST_ABORTED;
  640. }
  641. static int process_testWriteToFinishedProcess(void *arg)
  642. {
  643. TestProcessData *data = (TestProcessData *)arg;
  644. const char *process_args[] = {
  645. data->childprocess_path,
  646. NULL,
  647. };
  648. SDL_Process *process = NULL;
  649. bool result;
  650. int exit_code;
  651. SDL_IOStream *process_stdin;
  652. const char *text_in = "text_in";
  653. SDLTest_AssertPass("About to call SDL_CreateProcess");
  654. process = SDL_CreateProcess(process_args, true);
  655. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcess()");
  656. if (!process) {
  657. goto failed;
  658. }
  659. exit_code = 0xdeadbeef;
  660. SDLTest_AssertPass("About to call SDL_WaitProcess");
  661. result = SDL_WaitProcess(process, true, &exit_code);
  662. SDLTest_AssertCheck(result, "SDL_WaitProcess()");
  663. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  664. process_stdin = SDL_GetProcessInput(process);
  665. SDLTest_AssertCheck(process_stdin != NULL, "SDL_GetProcessInput returns non-Null SDL_IOStream");
  666. SDLTest_AssertPass("About to call SDL_WriteIO on dead child process");
  667. SDL_WriteIO(process_stdin, text_in, SDL_strlen(text_in));
  668. SDLTest_AssertPass("About to destroy process");
  669. SDL_DestroyProcess(process);
  670. return TEST_COMPLETED;
  671. failed:
  672. SDL_DestroyProcess(process);
  673. return TEST_ABORTED;
  674. }
  675. static int process_testNonExistingExecutable(void *arg)
  676. {
  677. static const int STEM_LENGTH = 16;
  678. char **process_args;
  679. char *random_stem;
  680. char *random_path;
  681. SDL_Process *process = NULL;
  682. random_stem = SDLTest_RandomAsciiStringOfSize(STEM_LENGTH);
  683. random_path = SDL_malloc(STEM_LENGTH + SDL_strlen(EXE) + 1);
  684. SDL_snprintf(random_path, STEM_LENGTH + SDL_strlen(EXE) + 1, "%s%s", random_stem, EXE);
  685. SDL_free(random_stem);
  686. SDLTest_AssertCheck(!SDL_GetPathInfo(random_path, NULL), "%s does not exist", random_path);
  687. process_args = CreateArguments(0, random_path, NULL);
  688. SDL_free(random_path);
  689. SDLTest_AssertPass("About to call SDL_CreateProcess");
  690. process = SDL_CreateProcess((const char * const *)process_args, false);
  691. SDLTest_AssertCheck(process == NULL, "SDL_CreateProcess() should have failed (%s)", SDL_GetError());
  692. DestroyStringArray(process_args);
  693. return TEST_COMPLETED;
  694. }
  695. static int process_testBatBadButVulnerability(void *arg)
  696. {
  697. TestProcessData *data = (TestProcessData *)arg;
  698. char *inject_arg = NULL;
  699. char **process_args = NULL;
  700. char *text_out = NULL;
  701. size_t len_text_out;
  702. int exitcode;
  703. SDL_Process *process = NULL;
  704. SDL_IOStream *child_bat;
  705. char buffer[256];
  706. #ifndef SDL_PLATFORM_WINDOWS
  707. SDLTest_AssertPass("The BatBadBut vulnerability only applied to Windows");
  708. return TEST_SKIPPED;
  709. #endif
  710. /* FIXME: remove child.bat at end of loop and/or create in temporary directory */
  711. child_bat = SDL_IOFromFile("child_batbadbut.bat", "w");
  712. SDL_IOprintf(child_bat, "@echo off\necho Hello from child_batbadbut.bat\necho \"|bat1=%%1|\"\n");
  713. SDL_CloseIO(child_bat);
  714. inject_arg = SDL_malloc(SDL_strlen(data->childprocess_path) + 100);
  715. SDL_snprintf(inject_arg, SDL_strlen(data->childprocess_path) + 100, "\"&%s --version --print-arguments --stdout OWNEDSTDOUT\"", data->childprocess_path);
  716. process_args = CreateArguments(0, "child_batbadbut.bat", inject_arg, NULL);
  717. SDLTest_AssertPass("About to call SDL_CreateProcess");
  718. process = SDL_CreateProcess((const char * const*)process_args, true);
  719. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcess");
  720. if (!process) {
  721. goto cleanup;
  722. }
  723. text_out = SDL_ReadProcess(process, &len_text_out, &exitcode);
  724. SDLTest_AssertCheck(exitcode == 0, "process exited with exitcode 0, was %d", exitcode);
  725. SDLTest_AssertCheck(text_out != NULL, "SDL_ReadProcess returned data");
  726. SDLTest_LogEscapedString("Output: ", text_out, len_text_out);
  727. if (!text_out) {
  728. goto cleanup;
  729. }
  730. SDLTest_AssertCheck(SDL_strstr(text_out, "Hello from child_batbadbut") != NULL, "stdout contains 'Hello from child'");
  731. SDLTest_AssertCheck(SDL_strstr(text_out, "SDL version") == NULL, "stdout should not contain SDL version");
  732. SDL_snprintf(buffer, sizeof(buffer), "|bat1=\"\"\"&%s\"\"|", process_args[1] + 2);
  733. SDLTest_LogEscapedString("stdout should contain: ", buffer, SDL_strlen(buffer));
  734. SDLTest_AssertCheck(SDL_strstr(text_out, buffer) != NULL, "Verify first argument");
  735. cleanup:
  736. SDL_free(text_out);
  737. SDL_DestroyProcess(process);
  738. SDL_free(inject_arg);
  739. DestroyStringArray(process_args);
  740. return TEST_COMPLETED;
  741. }
  742. static int process_testFileRedirection(void *arg)
  743. {
  744. TestProcessData *data = (TestProcessData *)arg;
  745. SDL_PropertiesID props = 0;
  746. const char * process_args[] = {
  747. data->childprocess_path,
  748. "--stdin-to-stdout",
  749. "--stdin-to-stderr",
  750. NULL,
  751. };
  752. const char TEXT_REF[] = "This is input for the child process";
  753. static const char *PATH_STDIN = "test_redirection_stdin.txt";
  754. static const char *PATH_STDOUT = "test_redirection_stdout.txt";
  755. static const char *PATH_STDERR = "test_redirection_stderr.txt";
  756. char *text_out = NULL;
  757. size_t len_text_out;
  758. int exitcode;
  759. bool result;
  760. SDL_Process *process = NULL;
  761. SDL_IOStream *stream;
  762. SDL_IOStream *input_stream = NULL;
  763. SDL_IOStream *output_stream = NULL;
  764. SDL_IOStream *error_stream = NULL;
  765. stream = SDL_IOFromFile(PATH_STDIN, "w");
  766. SDLTest_AssertCheck(stream != NULL, "SDL_IOFromFile(\"%s\", \"w\")", PATH_STDIN);
  767. if (!stream) {
  768. goto cleanup;
  769. }
  770. SDL_WriteIO(stream, TEXT_REF, sizeof(TEXT_REF));
  771. SDL_CloseIO(stream);
  772. input_stream = SDL_IOFromFile(PATH_STDIN, "r");
  773. SDLTest_AssertCheck(input_stream != NULL, "SDL_IOFromFile(\"%s\", \"r\")", PATH_STDIN);
  774. if (!input_stream) {
  775. goto cleanup;
  776. }
  777. output_stream = SDL_IOFromFile(PATH_STDOUT, "w");
  778. SDLTest_AssertCheck(output_stream != NULL, "SDL_IOFromFile(\"%s\", \"w\")", PATH_STDOUT);
  779. if (!output_stream) {
  780. goto cleanup;
  781. }
  782. error_stream = SDL_IOFromFile(PATH_STDERR, "w");
  783. SDLTest_AssertCheck(error_stream != NULL, "SDL_IOFromFile(\"%s\", \"w\")", PATH_STDERR);
  784. if (!error_stream) {
  785. goto cleanup;
  786. }
  787. props = SDL_CreateProperties();
  788. SDLTest_AssertCheck(props != 0, "SDL_CreateProperties()");
  789. if (!props) {
  790. goto cleanup;
  791. }
  792. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)process_args);
  793. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_REDIRECT);
  794. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_POINTER, (void *)input_stream);
  795. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_REDIRECT);
  796. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_POINTER, (void *)output_stream);
  797. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDERR_NUMBER, SDL_PROCESS_STDIO_REDIRECT);
  798. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_STDERR_POINTER, (void *)error_stream);
  799. process = SDL_CreateProcessWithProperties(props);
  800. SDL_DestroyProperties(props);
  801. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcessWithProperties (%s)", SDL_GetError());
  802. if (!process) {
  803. goto cleanup;
  804. }
  805. exitcode = 0xdeadbeef;
  806. text_out = SDL_ReadProcess(process, &len_text_out, &exitcode);
  807. SDLTest_AssertCheck(text_out == NULL, "SDL_ReadProcess should not be able to close a redirected process (%s)", SDL_GetError());
  808. SDLTest_AssertCheck(len_text_out == 0, "length written by SDL_ReadProcess should be 0");
  809. SDL_free(text_out);
  810. text_out = NULL;
  811. exitcode = 0xdeadbeef;
  812. result = SDL_WaitProcess(process, true, &exitcode);
  813. SDLTest_AssertCheck(result, "process must have exited");
  814. SDLTest_AssertCheck(exitcode == 0, "process exited with exitcode 0, was %d", exitcode);
  815. SDL_CloseIO(input_stream);
  816. input_stream = NULL;
  817. SDL_CloseIO(output_stream);
  818. output_stream = NULL;
  819. SDL_CloseIO(error_stream);
  820. error_stream = NULL;
  821. text_out = SDL_LoadFile(PATH_STDOUT, &len_text_out);
  822. SDLTest_AssertCheck(text_out != NULL, "SDL_LoadFile(\"%s\") succeeded (%s)", PATH_STDOUT, SDL_GetError());
  823. SDLTest_AssertPass("Comparing stdout with reference");
  824. SDLTest_CompareMemory(text_out, len_text_out, TEXT_REF, sizeof(TEXT_REF));
  825. SDL_free(text_out);
  826. text_out = SDL_LoadFile(PATH_STDERR, &len_text_out);
  827. SDLTest_AssertCheck(text_out != NULL, "SDL_LoadFile(\"%s\") succeeded (%s)", PATH_STDERR, SDL_GetError());
  828. SDLTest_AssertPass("Comparing stderr with reference");
  829. SDLTest_CompareMemory(text_out, len_text_out, TEXT_REF, sizeof(TEXT_REF));
  830. SDL_free(text_out);
  831. cleanup:
  832. SDL_CloseIO(input_stream);
  833. SDL_CloseIO(output_stream);
  834. SDL_CloseIO(error_stream);
  835. SDL_DestroyProcess(process);
  836. return TEST_COMPLETED;
  837. }
  838. static const SDLTest_TestCaseReference processTestArguments = {
  839. process_testArguments, "process_testArguments", "Test passing arguments to child process", TEST_ENABLED
  840. };
  841. static const SDLTest_TestCaseReference processTestExitCode = {
  842. process_testexitCode, "process_testExitCode", "Test exit codes", TEST_ENABLED
  843. };
  844. static const SDLTest_TestCaseReference processTestInheritedEnv = {
  845. process_testInheritedEnv, "process_testInheritedEnv", "Test inheriting environment from parent process", TEST_ENABLED
  846. };
  847. static const SDLTest_TestCaseReference processTestNewEnv = {
  848. process_testNewEnv, "process_testNewEnv", "Test creating new environment for child process", TEST_ENABLED
  849. };
  850. static const SDLTest_TestCaseReference processTestKill = {
  851. process_testKill, "process_testKill", "Test Killing a child process", TEST_ENABLED
  852. };
  853. static const SDLTest_TestCaseReference processTestStdinToStdout = {
  854. process_testStdinToStdout, "process_testStdinToStdout", "Test writing to stdin and reading from stdout", TEST_ENABLED
  855. };
  856. static const SDLTest_TestCaseReference processTestStdinToStderr = {
  857. process_testStdinToStderr, "process_testStdinToStderr", "Test writing to stdin and reading from stderr", TEST_ENABLED
  858. };
  859. static const SDLTest_TestCaseReference processTestSimpleStdinToStdout = {
  860. process_testSimpleStdinToStdout, "process_testSimpleStdinToStdout", "Test writing to stdin and reading from stdout using the simplified API", TEST_ENABLED
  861. };
  862. static const SDLTest_TestCaseReference processTestMultiprocessStdinToStdout = {
  863. process_testMultiprocessStdinToStdout, "process_testMultiprocessStdinToStdout", "Test writing to stdin and reading from stdout using the simplified API", TEST_ENABLED
  864. };
  865. static const SDLTest_TestCaseReference processTestWriteToFinishedProcess = {
  866. process_testWriteToFinishedProcess, "process_testWriteToFinishedProcess", "Test writing to stdin of terminated process", TEST_ENABLED
  867. };
  868. static const SDLTest_TestCaseReference processTestNonExistingExecutable = {
  869. process_testNonExistingExecutable, "process_testNonExistingExecutable", "Test running a non-existing executable", TEST_ENABLED
  870. };
  871. static const SDLTest_TestCaseReference processTestBatBadButVulnerability = {
  872. process_testBatBadButVulnerability, "process_testBatBadButVulnerability", "Test BatBadBut vulnerability: command injection through cmd.exe", TEST_ENABLED
  873. };
  874. static const SDLTest_TestCaseReference processTestFileRedirection = {
  875. process_testFileRedirection, "process_testFileRedirection", "Test redirection from/to files", TEST_ENABLED
  876. };
  877. static const SDLTest_TestCaseReference *processTests[] = {
  878. &processTestArguments,
  879. &processTestExitCode,
  880. &processTestInheritedEnv,
  881. &processTestNewEnv,
  882. &processTestKill,
  883. &processTestStdinToStdout,
  884. &processTestStdinToStderr,
  885. &processTestSimpleStdinToStdout,
  886. &processTestMultiprocessStdinToStdout,
  887. &processTestWriteToFinishedProcess,
  888. &processTestNonExistingExecutable,
  889. &processTestBatBadButVulnerability,
  890. &processTestFileRedirection,
  891. NULL
  892. };
  893. static SDLTest_TestSuiteReference processTestSuite = {
  894. "Process",
  895. setUpProcess,
  896. processTests,
  897. NULL
  898. };
  899. static SDLTest_TestSuiteReference *testSuites[] = {
  900. &processTestSuite,
  901. NULL
  902. };
  903. int main(int argc, char *argv[])
  904. {
  905. int i;
  906. int result;
  907. SDLTest_CommonState *state;
  908. SDLTest_TestSuiteRunner *runner;
  909. /* Initialize test framework */
  910. state = SDLTest_CommonCreateState(argv, 0);
  911. if (!state) {
  912. return 1;
  913. }
  914. runner = SDLTest_CreateTestSuiteRunner(state, testSuites);
  915. /* Parse commandline */
  916. for (i = 1; i < argc;) {
  917. int consumed;
  918. consumed = SDLTest_CommonArg(state, i);
  919. if (!consumed) {
  920. if (!parsed_args.childprocess_path) {
  921. parsed_args.childprocess_path = argv[i];
  922. consumed = 1;
  923. }
  924. }
  925. if (consumed <= 0) {
  926. SDLTest_CommonLogUsage(state, argv[0], options);
  927. return 1;
  928. }
  929. i += consumed;
  930. }
  931. if (!parsed_args.childprocess_path) {
  932. SDLTest_CommonLogUsage(state, argv[0], options);
  933. return 1;
  934. }
  935. result = SDLTest_ExecuteTestSuiteRunner(runner);
  936. SDL_Quit();
  937. SDLTest_DestroyTestSuiteRunner(runner);
  938. SDLTest_CommonDestroyState(state);
  939. return result;
  940. }