testprocess.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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. /* Wait for the child process to finish initializing */
  318. SDL_Delay(500);
  319. SDLTest_AssertPass("About to call SDL_KillProcess(true)");
  320. result = SDL_KillProcess(process, true);
  321. SDLTest_AssertCheck(result == true, "Process should have exited");
  322. exit_code = 0;
  323. SDLTest_AssertPass("About to call SDL_WaitProcess(true)");
  324. result = SDL_WaitProcess(process, true, &exit_code);
  325. SDLTest_AssertCheck(result == true, "Process should have exited");
  326. SDLTest_AssertCheck(exit_code != 0, "Exit code should be non-zero, is %d", exit_code);
  327. SDLTest_AssertPass("About to destroy process");
  328. SDL_DestroyProcess(process);
  329. return TEST_COMPLETED;
  330. failed:
  331. SDL_DestroyProcess(process);
  332. return TEST_ABORTED;
  333. }
  334. static int process_testStdinToStdout(void *arg)
  335. {
  336. TestProcessData *data = (TestProcessData *)arg;
  337. const char *process_args[] = {
  338. data->childprocess_path,
  339. "--stdin-to-stdout",
  340. NULL,
  341. };
  342. SDL_PropertiesID props;
  343. SDL_Process *process = NULL;
  344. Sint64 pid;
  345. SDL_IOStream *process_stdin = NULL;
  346. SDL_IOStream *process_stdout = NULL;
  347. SDL_IOStream *process_stderr = NULL;
  348. size_t text_in_size = 1 * 1024 * 1024;
  349. char *text_in = NULL;
  350. size_t total_written;
  351. size_t total_read;
  352. bool wait_result;
  353. int exit_code;
  354. SDL_IOStream *stdout_stream = NULL;
  355. char *stdout_stream_buf;
  356. int iteration_count = 0;
  357. text_in = SDLTest_RandomAsciiStringOfSize((int)text_in_size);
  358. /* Make sure text_in does not contain EOF */
  359. for (;;) {
  360. char *e = SDL_strstr(text_in, "EOF");
  361. if (!e) {
  362. break;
  363. }
  364. e[0] = 'N';
  365. }
  366. text_in[text_in_size - 3] = 'E';
  367. text_in[text_in_size - 2] = 'O';
  368. text_in[text_in_size - 1] = 'F';
  369. stdout_stream = SDL_IOFromDynamicMem();
  370. props = SDL_CreateProperties();
  371. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)process_args);
  372. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_APP);
  373. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_APP);
  374. process = SDL_CreateProcessWithProperties(props);
  375. SDL_DestroyProperties(props);
  376. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcessWithProperties()");
  377. if (!process) {
  378. goto failed;
  379. }
  380. props = SDL_GetProcessProperties(process);
  381. SDLTest_AssertCheck(props != 0, "SDL_GetProcessProperties()");
  382. pid = SDL_GetNumberProperty(props, SDL_PROP_PROCESS_PID_NUMBER, 0);
  383. SDLTest_AssertCheck(pid != 0, "Checking process ID, expected non-zero, got %" SDL_PRIs64, pid);
  384. process_stdin = SDL_GetProcessInput(process);
  385. SDLTest_AssertCheck(process_stdin != NULL, "SDL_GetPointerProperty(SDL_PROP_PROCESS_STDIN_POINTER) returns a valid IO stream");
  386. process_stdout = SDL_GetProcessOutput(process);
  387. SDLTest_AssertCheck(process_stdout != NULL, "SDL_GetPointerProperty(SDL_PROP_PROCESS_STDOUT_POINTER) returns a valid IO stream");
  388. process_stderr = (SDL_IOStream *)SDL_GetPointerProperty(props, SDL_PROP_PROCESS_STDERR_POINTER, NULL);
  389. SDLTest_AssertCheck(process_stderr == NULL, "SDL_GetPointerProperty(SDL_PROP_PROCESS_STDERR_POINTER) returns NULL");
  390. if (!process_stdin || !process_stdout) {
  391. goto failed;
  392. }
  393. total_written = 0;
  394. total_read = 0;
  395. for (;;) {
  396. int log_this_iteration = (iteration_count % 32) == 32;
  397. char local_buffer[16 * 4094];
  398. size_t amount_read;
  399. SDL_IOStatus io_status;
  400. if (total_written != text_in_size) {
  401. size_t amount_written;
  402. if (log_this_iteration) {
  403. SDLTest_AssertPass("About to SDL_WriteIO (%dth time)", iteration_count);
  404. }
  405. amount_written = SDL_WriteIO(process_stdin, text_in + total_written, text_in_size - total_written);
  406. if (log_this_iteration) {
  407. SDLTest_Log("SDL_WriteIO() -> %u (%dth time)", (unsigned)amount_written, iteration_count);
  408. }
  409. if (amount_written == 0) {
  410. io_status = SDL_GetIOStatus(process_stdin);
  411. if (io_status != SDL_IO_STATUS_NOT_READY) {
  412. SDLTest_Log("SDL_GetIOStatus(process_stdin) returns %d, breaking.", io_status);
  413. break;
  414. }
  415. }
  416. total_written += amount_written;
  417. SDL_FlushIO(process_stdin);
  418. }
  419. /* FIXME: this needs a rate limit */
  420. if (log_this_iteration) {
  421. SDLTest_AssertPass("About to SDL_ReadIO (%dth time)", iteration_count);
  422. }
  423. amount_read = SDL_ReadIO(process_stdout, local_buffer, sizeof(local_buffer));
  424. if (log_this_iteration) {
  425. SDLTest_Log("SDL_ReadIO() -> %u (%dth time)", (unsigned)amount_read, iteration_count);
  426. }
  427. if (amount_read == 0) {
  428. io_status = SDL_GetIOStatus(process_stdout);
  429. if (io_status != SDL_IO_STATUS_NOT_READY) {
  430. SDLTest_Log("SDL_GetIOStatus(process_stdout) returned %d, breaking.", io_status);
  431. break;
  432. }
  433. } else {
  434. total_read += amount_read;
  435. SDL_WriteIO(stdout_stream, local_buffer, amount_read);
  436. stdout_stream_buf = SDL_GetPointerProperty(SDL_GetIOProperties(stdout_stream), SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL);
  437. if (SDL_strstr(stdout_stream_buf, "EOF")) {
  438. SDLTest_Log("Found EOF in stdout");
  439. break;
  440. }
  441. }
  442. SDL_Delay(10);
  443. }
  444. SDLTest_Log("Wrote %" SDL_PRIu64 " bytes to process.stdin", (Uint64)total_written);
  445. SDLTest_Log("Read %" SDL_PRIu64 " bytes from process.stdout",(Uint64)total_read);
  446. stdout_stream_buf = SDL_GetPointerProperty(SDL_GetIOProperties(stdout_stream), SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL);
  447. SDLTest_CompareMemory(stdout_stream_buf, total_written, text_in, text_in_size);
  448. exit_code = 0xdeadbeef;
  449. wait_result = SDL_WaitProcess(process, false, &exit_code);
  450. SDLTest_AssertCheck(wait_result == false, "Process should not have closed yet");
  451. SDLTest_AssertPass("About to close stdin");
  452. /* Closing stdin of `subprocessstdin --stdin-to-stdout` should close the process */
  453. SDL_CloseIO(process_stdin);
  454. process_stdin = SDL_GetProcessInput(process);
  455. SDLTest_AssertCheck(process_stdin == NULL, "SDL_GetPointerProperty(SDL_PROP_PROCESS_STDIN_POINTER) is cleared after close");
  456. SDLTest_AssertPass("About to wait on process");
  457. exit_code = 0xdeadbeef;
  458. wait_result = SDL_WaitProcess(process, true, &exit_code);
  459. SDLTest_AssertCheck(wait_result == true, "Process should have closed when closing stdin");
  460. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  461. if (!wait_result) {
  462. bool killed;
  463. SDL_Log("About to kill process");
  464. killed = SDL_KillProcess(process, true);
  465. SDLTest_AssertCheck(killed, "SDL_KillProcess succeeded");
  466. }
  467. SDLTest_AssertPass("About to destroy process");
  468. SDL_DestroyProcess(process);
  469. SDL_CloseIO(stdout_stream);
  470. SDL_free(text_in);
  471. return TEST_COMPLETED;
  472. failed:
  473. SDL_DestroyProcess(process);
  474. SDL_CloseIO(stdout_stream);
  475. SDL_free(text_in);
  476. return TEST_ABORTED;
  477. }
  478. static int process_testStdinToStderr(void *arg)
  479. {
  480. TestProcessData *data = (TestProcessData *)arg;
  481. const char *process_args[] = {
  482. data->childprocess_path,
  483. "--stdin-to-stderr",
  484. NULL,
  485. };
  486. SDL_Process *process = NULL;
  487. SDL_IOStream *process_stdin = NULL;
  488. SDL_IOStream *process_stdout = NULL;
  489. SDL_IOStream *process_stderr = NULL;
  490. 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";
  491. size_t result;
  492. int exit_code;
  493. SDL_PropertiesID props;
  494. char buffer[256];
  495. size_t amount_read;
  496. props = SDL_CreateProperties();
  497. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)process_args);
  498. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_APP);
  499. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_NULL);
  500. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDERR_NUMBER, SDL_PROCESS_STDIO_APP);
  501. process = SDL_CreateProcessWithProperties(props);
  502. SDL_DestroyProperties(props);
  503. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcessWithProperties()");
  504. if (!process) {
  505. goto failed;
  506. }
  507. SDLTest_AssertPass("About to write to process");
  508. process_stdin = SDL_GetProcessInput(process);
  509. SDLTest_AssertCheck(process_stdin != NULL, "SDL_GetProcessInput()");
  510. result = SDL_WriteIO(process_stdin, text_in, SDL_strlen(text_in));
  511. SDLTest_AssertCheck(result == SDL_strlen(text_in), "SDL_WriteIO() wrote %d, expected %d", (int)result, (int)SDL_strlen(text_in));
  512. SDL_CloseIO(process_stdin);
  513. process_stdout = SDL_GetProcessOutput(process);
  514. SDLTest_AssertCheck(process_stdout == NULL, "Process has no stdout");
  515. process_stderr = SDL_GetPointerProperty(SDL_GetProcessProperties(process), SDL_PROP_PROCESS_STDERR_POINTER, NULL);
  516. SDLTest_AssertCheck(process_stderr != NULL, "Process has stderr");
  517. exit_code = 0xdeadbeef;
  518. result = SDL_WaitProcess(process, true, &exit_code);
  519. SDLTest_AssertCheck(result == true, "Process should have finished");
  520. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  521. amount_read = SDL_ReadIO(process_stderr, buffer, sizeof(buffer));
  522. SDLTest_CompareMemory(buffer, amount_read, text_in, SDL_strlen(text_in));
  523. SDLTest_AssertPass("About to destroy process");
  524. SDL_DestroyProcess(process);
  525. return TEST_COMPLETED;
  526. failed:
  527. SDL_DestroyProcess(process);
  528. return TEST_ABORTED;
  529. }
  530. static int process_testSimpleStdinToStdout(void *arg)
  531. {
  532. TestProcessData *data = (TestProcessData *)arg;
  533. const char *process_args[] = {
  534. data->childprocess_path,
  535. "--stdin-to-stdout",
  536. NULL,
  537. };
  538. SDL_Process *process = NULL;
  539. SDL_IOStream *input = NULL;
  540. 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";
  541. char *buffer;
  542. size_t result;
  543. int exit_code;
  544. size_t total_read = 0;
  545. process = SDL_CreateProcess(process_args, true);
  546. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcess()");
  547. if (!process) {
  548. goto failed;
  549. }
  550. SDLTest_AssertPass("About to write to process");
  551. input = SDL_GetProcessInput(process);
  552. SDLTest_AssertCheck(input != NULL, "SDL_GetProcessInput()");
  553. result = SDL_WriteIO(input, text_in, SDL_strlen(text_in));
  554. SDLTest_AssertCheck(result == SDL_strlen(text_in), "SDL_WriteIO() wrote %d, expected %d", (int)result, (int)SDL_strlen(text_in));
  555. SDL_CloseIO(input);
  556. input = SDL_GetProcessInput(process);
  557. SDLTest_AssertCheck(input == NULL, "SDL_GetProcessInput() after close");
  558. exit_code = 0xdeadbeef;
  559. buffer = (char *)SDL_ReadProcess(process, &total_read, &exit_code);
  560. SDLTest_AssertCheck(buffer != NULL, "SDL_ReadProcess()");
  561. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  562. if (!buffer) {
  563. goto failed;
  564. }
  565. SDLTest_LogEscapedString("Expected text read from subprocess: %s", text_in, SDL_strlen(text_in));
  566. SDLTest_LogEscapedString("Actual text read from subprocess: %s", buffer, total_read);
  567. 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);
  568. SDLTest_AssertCheck(SDL_strcmp(buffer, text_in) == 0, "Subprocess stdout should match text written to stdin");
  569. SDL_free(buffer);
  570. SDLTest_AssertPass("About to destroy process");
  571. SDL_DestroyProcess(process);
  572. return TEST_COMPLETED;
  573. failed:
  574. SDL_DestroyProcess(process);
  575. return TEST_ABORTED;
  576. }
  577. static int process_testMultiprocessStdinToStdout(void *arg)
  578. {
  579. TestProcessData *data = (TestProcessData *)arg;
  580. const char *process_args[] = {
  581. data->childprocess_path,
  582. "--stdin-to-stdout",
  583. "--log-stdin",
  584. NULL,
  585. NULL,
  586. };
  587. SDL_Process *process1 = NULL;
  588. SDL_Process *process2 = NULL;
  589. SDL_PropertiesID props;
  590. SDL_IOStream *input = NULL;
  591. 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";
  592. char *buffer;
  593. size_t result;
  594. int exit_code;
  595. size_t total_read = 0;
  596. bool finished;
  597. process_args[3] = "child1-stdin.txt";
  598. process1 = SDL_CreateProcess(process_args, true);
  599. SDLTest_AssertCheck(process1 != NULL, "SDL_CreateProcess()");
  600. if (!process1) {
  601. goto failed;
  602. }
  603. props = SDL_CreateProperties();
  604. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)process_args);
  605. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_REDIRECT);
  606. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_POINTER, SDL_GetPointerProperty(SDL_GetProcessProperties(process1), SDL_PROP_PROCESS_STDOUT_POINTER, NULL));
  607. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_APP);
  608. SDLTest_AssertPass("About to call SDL_CreateProcessWithProperties");
  609. process_args[3] = "child2-stdin.txt";
  610. process2 = SDL_CreateProcessWithProperties(props);
  611. SDL_DestroyProperties(props);
  612. SDLTest_AssertCheck(process2 != NULL, "SDL_CreateProcess()");
  613. if (!process2) {
  614. goto failed;
  615. }
  616. SDLTest_AssertPass("About to write to process");
  617. input = SDL_GetProcessInput(process1);
  618. SDLTest_AssertCheck(input != NULL, "SDL_GetProcessInput()");
  619. result = SDL_WriteIO(input, text_in, SDL_strlen(text_in));
  620. SDLTest_AssertCheck(result == SDL_strlen(text_in), "SDL_WriteIO() wrote %d, expected %d", (int)result, (int)SDL_strlen(text_in));
  621. SDL_CloseIO(input);
  622. exit_code = 0xdeadbeef;
  623. finished = SDL_WaitProcess(process1, true, &exit_code);
  624. SDLTest_AssertCheck(finished == true, "process 1 should have finished");
  625. SDLTest_AssertCheck(exit_code == 0, "Exit code of process 1 should be 0, is %d", exit_code);
  626. exit_code = 0xdeadbeef;
  627. buffer = (char *)SDL_ReadProcess(process2, &total_read, &exit_code);
  628. SDLTest_AssertCheck(buffer != NULL, "SDL_ReadProcess()");
  629. SDLTest_AssertCheck(exit_code == 0, "Exit code of process 2 should be 0, is %d", exit_code);
  630. if (!buffer) {
  631. goto failed;
  632. }
  633. SDLTest_LogEscapedString("Expected text read from subprocess: ", text_in, SDL_strlen(text_in));
  634. SDLTest_LogEscapedString("Actual text read from subprocess: ", buffer, total_read);
  635. 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);
  636. SDLTest_AssertCheck(SDL_strcmp(buffer, text_in) == 0, "Subprocess stdout should match text written to stdin");
  637. SDL_free(buffer);
  638. SDLTest_AssertPass("About to destroy processes");
  639. SDL_DestroyProcess(process1);
  640. SDL_DestroyProcess(process2);
  641. return TEST_COMPLETED;
  642. failed:
  643. SDL_DestroyProcess(process1);
  644. SDL_DestroyProcess(process2);
  645. return TEST_ABORTED;
  646. }
  647. static int process_testWriteToFinishedProcess(void *arg)
  648. {
  649. TestProcessData *data = (TestProcessData *)arg;
  650. const char *process_args[] = {
  651. data->childprocess_path,
  652. NULL,
  653. };
  654. SDL_Process *process = NULL;
  655. bool result;
  656. int exit_code;
  657. SDL_IOStream *process_stdin;
  658. const char *text_in = "text_in";
  659. SDLTest_AssertPass("About to call SDL_CreateProcess");
  660. process = SDL_CreateProcess(process_args, true);
  661. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcess()");
  662. if (!process) {
  663. goto failed;
  664. }
  665. exit_code = 0xdeadbeef;
  666. SDLTest_AssertPass("About to call SDL_WaitProcess");
  667. result = SDL_WaitProcess(process, true, &exit_code);
  668. SDLTest_AssertCheck(result, "SDL_WaitProcess()");
  669. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  670. process_stdin = SDL_GetProcessInput(process);
  671. SDLTest_AssertCheck(process_stdin != NULL, "SDL_GetProcessInput returns non-Null SDL_IOStream");
  672. SDLTest_AssertPass("About to call SDL_WriteIO on dead child process");
  673. SDL_WriteIO(process_stdin, text_in, SDL_strlen(text_in));
  674. SDLTest_AssertPass("About to destroy process");
  675. SDL_DestroyProcess(process);
  676. return TEST_COMPLETED;
  677. failed:
  678. SDL_DestroyProcess(process);
  679. return TEST_ABORTED;
  680. }
  681. static int process_testNonExistingExecutable(void *arg)
  682. {
  683. static const int STEM_LENGTH = 16;
  684. char **process_args;
  685. char *random_stem;
  686. char *random_path;
  687. SDL_Process *process = NULL;
  688. random_stem = SDLTest_RandomAsciiStringOfSize(STEM_LENGTH);
  689. random_path = SDL_malloc(STEM_LENGTH + SDL_strlen(EXE) + 1);
  690. SDL_snprintf(random_path, STEM_LENGTH + SDL_strlen(EXE) + 1, "%s%s", random_stem, EXE);
  691. SDL_free(random_stem);
  692. SDLTest_AssertCheck(!SDL_GetPathInfo(random_path, NULL), "%s does not exist", random_path);
  693. process_args = CreateArguments(0, random_path, NULL);
  694. SDL_free(random_path);
  695. SDLTest_AssertPass("About to call SDL_CreateProcess");
  696. process = SDL_CreateProcess((const char * const *)process_args, false);
  697. SDLTest_AssertCheck(process == NULL, "SDL_CreateProcess() should have failed (%s)", SDL_GetError());
  698. DestroyStringArray(process_args);
  699. return TEST_COMPLETED;
  700. }
  701. static int process_testBatBadButVulnerability(void *arg)
  702. {
  703. TestProcessData *data = (TestProcessData *)arg;
  704. char *inject_arg = NULL;
  705. char **process_args = NULL;
  706. char *text_out = NULL;
  707. size_t len_text_out;
  708. int exitcode;
  709. SDL_Process *process = NULL;
  710. SDL_IOStream *child_bat;
  711. char buffer[256];
  712. #ifndef SDL_PLATFORM_WINDOWS
  713. SDLTest_AssertPass("The BatBadBut vulnerability only applied to Windows");
  714. return TEST_SKIPPED;
  715. #endif
  716. /* FIXME: remove child.bat at end of loop and/or create in temporary directory */
  717. child_bat = SDL_IOFromFile("child_batbadbut.bat", "w");
  718. SDL_IOprintf(child_bat, "@echo off\necho Hello from child_batbadbut.bat\necho \"|bat1=%%1|\"\n");
  719. SDL_CloseIO(child_bat);
  720. inject_arg = SDL_malloc(SDL_strlen(data->childprocess_path) + 100);
  721. SDL_snprintf(inject_arg, SDL_strlen(data->childprocess_path) + 100, "\"&%s --version --print-arguments --stdout OWNEDSTDOUT\"", data->childprocess_path);
  722. process_args = CreateArguments(0, "child_batbadbut.bat", inject_arg, NULL);
  723. SDLTest_AssertPass("About to call SDL_CreateProcess");
  724. process = SDL_CreateProcess((const char * const*)process_args, true);
  725. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcess");
  726. if (!process) {
  727. goto cleanup;
  728. }
  729. text_out = SDL_ReadProcess(process, &len_text_out, &exitcode);
  730. SDLTest_AssertCheck(exitcode == 0, "process exited with exitcode 0, was %d", exitcode);
  731. SDLTest_AssertCheck(text_out != NULL, "SDL_ReadProcess returned data");
  732. SDLTest_LogEscapedString("Output: ", text_out, len_text_out);
  733. if (!text_out) {
  734. goto cleanup;
  735. }
  736. SDLTest_AssertCheck(SDL_strstr(text_out, "Hello from child_batbadbut") != NULL, "stdout contains 'Hello from child'");
  737. SDLTest_AssertCheck(SDL_strstr(text_out, "SDL version") == NULL, "stdout should not contain SDL version");
  738. SDL_snprintf(buffer, sizeof(buffer), "|bat1=\"\"\"&%s\"\"|", process_args[1] + 2);
  739. SDLTest_LogEscapedString("stdout should contain: ", buffer, SDL_strlen(buffer));
  740. SDLTest_AssertCheck(SDL_strstr(text_out, buffer) != NULL, "Verify first argument");
  741. cleanup:
  742. SDL_free(text_out);
  743. SDL_DestroyProcess(process);
  744. SDL_free(inject_arg);
  745. DestroyStringArray(process_args);
  746. return TEST_COMPLETED;
  747. }
  748. static int process_testFileRedirection(void *arg)
  749. {
  750. TestProcessData *data = (TestProcessData *)arg;
  751. SDL_PropertiesID props = 0;
  752. const char * process_args[] = {
  753. data->childprocess_path,
  754. "--stdin-to-stdout",
  755. "--stdin-to-stderr",
  756. NULL,
  757. };
  758. const char TEXT_REF[] = "This is input for the child process";
  759. static const char *PATH_STDIN = "test_redirection_stdin.txt";
  760. static const char *PATH_STDOUT = "test_redirection_stdout.txt";
  761. static const char *PATH_STDERR = "test_redirection_stderr.txt";
  762. char *text_out = NULL;
  763. size_t len_text_out;
  764. int exitcode;
  765. bool result;
  766. SDL_Process *process = NULL;
  767. SDL_IOStream *stream;
  768. SDL_IOStream *input_stream = NULL;
  769. SDL_IOStream *output_stream = NULL;
  770. SDL_IOStream *error_stream = NULL;
  771. stream = SDL_IOFromFile(PATH_STDIN, "w");
  772. SDLTest_AssertCheck(stream != NULL, "SDL_IOFromFile(\"%s\", \"w\")", PATH_STDIN);
  773. if (!stream) {
  774. goto cleanup;
  775. }
  776. SDL_WriteIO(stream, TEXT_REF, sizeof(TEXT_REF));
  777. SDL_CloseIO(stream);
  778. input_stream = SDL_IOFromFile(PATH_STDIN, "r");
  779. SDLTest_AssertCheck(input_stream != NULL, "SDL_IOFromFile(\"%s\", \"r\")", PATH_STDIN);
  780. if (!input_stream) {
  781. goto cleanup;
  782. }
  783. output_stream = SDL_IOFromFile(PATH_STDOUT, "w");
  784. SDLTest_AssertCheck(output_stream != NULL, "SDL_IOFromFile(\"%s\", \"w\")", PATH_STDOUT);
  785. if (!output_stream) {
  786. goto cleanup;
  787. }
  788. error_stream = SDL_IOFromFile(PATH_STDERR, "w");
  789. SDLTest_AssertCheck(error_stream != NULL, "SDL_IOFromFile(\"%s\", \"w\")", PATH_STDERR);
  790. if (!error_stream) {
  791. goto cleanup;
  792. }
  793. props = SDL_CreateProperties();
  794. SDLTest_AssertCheck(props != 0, "SDL_CreateProperties()");
  795. if (!props) {
  796. goto cleanup;
  797. }
  798. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)process_args);
  799. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_REDIRECT);
  800. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_POINTER, (void *)input_stream);
  801. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_REDIRECT);
  802. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_POINTER, (void *)output_stream);
  803. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDERR_NUMBER, SDL_PROCESS_STDIO_REDIRECT);
  804. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_STDERR_POINTER, (void *)error_stream);
  805. process = SDL_CreateProcessWithProperties(props);
  806. SDL_DestroyProperties(props);
  807. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcessWithProperties (%s)", SDL_GetError());
  808. if (!process) {
  809. goto cleanup;
  810. }
  811. exitcode = 0xdeadbeef;
  812. text_out = SDL_ReadProcess(process, &len_text_out, &exitcode);
  813. SDLTest_AssertCheck(text_out == NULL, "SDL_ReadProcess should not be able to close a redirected process (%s)", SDL_GetError());
  814. SDLTest_AssertCheck(len_text_out == 0, "length written by SDL_ReadProcess should be 0");
  815. SDL_free(text_out);
  816. text_out = NULL;
  817. exitcode = 0xdeadbeef;
  818. result = SDL_WaitProcess(process, true, &exitcode);
  819. SDLTest_AssertCheck(result, "process must have exited");
  820. SDLTest_AssertCheck(exitcode == 0, "process exited with exitcode 0, was %d", exitcode);
  821. SDL_CloseIO(input_stream);
  822. input_stream = NULL;
  823. SDL_CloseIO(output_stream);
  824. output_stream = NULL;
  825. SDL_CloseIO(error_stream);
  826. error_stream = NULL;
  827. text_out = SDL_LoadFile(PATH_STDOUT, &len_text_out);
  828. SDLTest_AssertCheck(text_out != NULL, "SDL_LoadFile(\"%s\") succeeded (%s)", PATH_STDOUT, SDL_GetError());
  829. SDLTest_AssertPass("Comparing stdout with reference");
  830. SDLTest_CompareMemory(text_out, len_text_out, TEXT_REF, sizeof(TEXT_REF));
  831. SDL_free(text_out);
  832. text_out = SDL_LoadFile(PATH_STDERR, &len_text_out);
  833. SDLTest_AssertCheck(text_out != NULL, "SDL_LoadFile(\"%s\") succeeded (%s)", PATH_STDERR, SDL_GetError());
  834. SDLTest_AssertPass("Comparing stderr with reference");
  835. SDLTest_CompareMemory(text_out, len_text_out, TEXT_REF, sizeof(TEXT_REF));
  836. SDL_free(text_out);
  837. cleanup:
  838. SDL_CloseIO(input_stream);
  839. SDL_CloseIO(output_stream);
  840. SDL_CloseIO(error_stream);
  841. SDL_DestroyProcess(process);
  842. return TEST_COMPLETED;
  843. }
  844. static const SDLTest_TestCaseReference processTestArguments = {
  845. process_testArguments, "process_testArguments", "Test passing arguments to child process", TEST_ENABLED
  846. };
  847. static const SDLTest_TestCaseReference processTestExitCode = {
  848. process_testexitCode, "process_testExitCode", "Test exit codes", TEST_ENABLED
  849. };
  850. static const SDLTest_TestCaseReference processTestInheritedEnv = {
  851. process_testInheritedEnv, "process_testInheritedEnv", "Test inheriting environment from parent process", TEST_ENABLED
  852. };
  853. static const SDLTest_TestCaseReference processTestNewEnv = {
  854. process_testNewEnv, "process_testNewEnv", "Test creating new environment for child process", TEST_ENABLED
  855. };
  856. static const SDLTest_TestCaseReference processTestKill = {
  857. process_testKill, "process_testKill", "Test Killing a child process", TEST_ENABLED
  858. };
  859. static const SDLTest_TestCaseReference processTestStdinToStdout = {
  860. process_testStdinToStdout, "process_testStdinToStdout", "Test writing to stdin and reading from stdout", TEST_ENABLED
  861. };
  862. static const SDLTest_TestCaseReference processTestStdinToStderr = {
  863. process_testStdinToStderr, "process_testStdinToStderr", "Test writing to stdin and reading from stderr", TEST_ENABLED
  864. };
  865. static const SDLTest_TestCaseReference processTestSimpleStdinToStdout = {
  866. process_testSimpleStdinToStdout, "process_testSimpleStdinToStdout", "Test writing to stdin and reading from stdout using the simplified API", TEST_ENABLED
  867. };
  868. static const SDLTest_TestCaseReference processTestMultiprocessStdinToStdout = {
  869. process_testMultiprocessStdinToStdout, "process_testMultiprocessStdinToStdout", "Test writing to stdin and reading from stdout using the simplified API", TEST_ENABLED
  870. };
  871. static const SDLTest_TestCaseReference processTestWriteToFinishedProcess = {
  872. process_testWriteToFinishedProcess, "process_testWriteToFinishedProcess", "Test writing to stdin of terminated process", TEST_ENABLED
  873. };
  874. static const SDLTest_TestCaseReference processTestNonExistingExecutable = {
  875. process_testNonExistingExecutable, "process_testNonExistingExecutable", "Test running a non-existing executable", TEST_ENABLED
  876. };
  877. static const SDLTest_TestCaseReference processTestBatBadButVulnerability = {
  878. process_testBatBadButVulnerability, "process_testBatBadButVulnerability", "Test BatBadBut vulnerability: command injection through cmd.exe", TEST_ENABLED
  879. };
  880. static const SDLTest_TestCaseReference processTestFileRedirection = {
  881. process_testFileRedirection, "process_testFileRedirection", "Test redirection from/to files", TEST_ENABLED
  882. };
  883. static const SDLTest_TestCaseReference *processTests[] = {
  884. &processTestArguments,
  885. &processTestExitCode,
  886. &processTestInheritedEnv,
  887. &processTestNewEnv,
  888. &processTestKill,
  889. &processTestStdinToStdout,
  890. &processTestStdinToStderr,
  891. &processTestSimpleStdinToStdout,
  892. &processTestMultiprocessStdinToStdout,
  893. &processTestWriteToFinishedProcess,
  894. &processTestNonExistingExecutable,
  895. &processTestBatBadButVulnerability,
  896. &processTestFileRedirection,
  897. NULL
  898. };
  899. static SDLTest_TestSuiteReference processTestSuite = {
  900. "Process",
  901. setUpProcess,
  902. processTests,
  903. NULL
  904. };
  905. static SDLTest_TestSuiteReference *testSuites[] = {
  906. &processTestSuite,
  907. NULL
  908. };
  909. int main(int argc, char *argv[])
  910. {
  911. int i;
  912. int result;
  913. SDLTest_CommonState *state;
  914. SDLTest_TestSuiteRunner *runner;
  915. /* Initialize test framework */
  916. state = SDLTest_CommonCreateState(argv, 0);
  917. if (!state) {
  918. return 1;
  919. }
  920. runner = SDLTest_CreateTestSuiteRunner(state, testSuites);
  921. /* Parse commandline */
  922. for (i = 1; i < argc;) {
  923. int consumed;
  924. consumed = SDLTest_CommonArg(state, i);
  925. if (!consumed) {
  926. if (!parsed_args.childprocess_path) {
  927. parsed_args.childprocess_path = argv[i];
  928. consumed = 1;
  929. }
  930. }
  931. if (consumed <= 0) {
  932. SDLTest_CommonLogUsage(state, argv[0], options);
  933. return 1;
  934. }
  935. i += consumed;
  936. }
  937. if (!parsed_args.childprocess_path) {
  938. SDLTest_CommonLogUsage(state, argv[0], options);
  939. return 1;
  940. }
  941. result = SDLTest_ExecuteTestSuiteRunner(runner);
  942. SDL_Quit();
  943. SDLTest_DestroyTestSuiteRunner(runner);
  944. SDLTest_CommonDestroyState(state);
  945. return result;
  946. }