testprocess.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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. "--log-stdin",
  582. NULL,
  583. NULL,
  584. };
  585. SDL_Process *process1 = NULL;
  586. SDL_Process *process2 = NULL;
  587. SDL_PropertiesID props;
  588. SDL_IOStream *input = NULL;
  589. 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";
  590. char *buffer;
  591. size_t result;
  592. int exit_code;
  593. size_t total_read = 0;
  594. bool finished;
  595. process_args[3] = "child1-stdin.txt";
  596. process1 = SDL_CreateProcess(process_args, true);
  597. SDLTest_AssertCheck(process1 != NULL, "SDL_CreateProcess()");
  598. if (!process1) {
  599. goto failed;
  600. }
  601. props = SDL_CreateProperties();
  602. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)process_args);
  603. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_REDIRECT);
  604. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_POINTER, SDL_GetPointerProperty(SDL_GetProcessProperties(process1), SDL_PROP_PROCESS_STDOUT_POINTER, NULL));
  605. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_APP);
  606. SDLTest_AssertPass("About to call SDL_CreateProcessWithProperties");
  607. process_args[3] = "child2-stdin.txt";
  608. process2 = SDL_CreateProcessWithProperties(props);
  609. SDL_DestroyProperties(props);
  610. SDLTest_AssertCheck(process2 != NULL, "SDL_CreateProcess()");
  611. if (!process2) {
  612. goto failed;
  613. }
  614. SDLTest_AssertPass("About to write to process");
  615. input = SDL_GetProcessInput(process1);
  616. SDLTest_AssertCheck(input != NULL, "SDL_GetProcessInput()");
  617. result = SDL_WriteIO(input, text_in, SDL_strlen(text_in));
  618. SDLTest_AssertCheck(result == SDL_strlen(text_in), "SDL_WriteIO() wrote %d, expected %d", (int)result, (int)SDL_strlen(text_in));
  619. SDL_CloseIO(input);
  620. exit_code = 0xdeadbeef;
  621. finished = SDL_WaitProcess(process1, true, &exit_code);
  622. SDLTest_AssertCheck(finished == true, "process 1 should have finished");
  623. SDLTest_AssertCheck(exit_code == 0, "Exit code of process 1 should be 0, is %d", exit_code);
  624. exit_code = 0xdeadbeef;
  625. buffer = (char *)SDL_ReadProcess(process2, &total_read, &exit_code);
  626. SDLTest_AssertCheck(buffer != NULL, "SDL_ReadProcess()");
  627. SDLTest_AssertCheck(exit_code == 0, "Exit code of process 2 should be 0, is %d", exit_code);
  628. if (!buffer) {
  629. goto failed;
  630. }
  631. SDLTest_LogEscapedString("Expected text read from subprocess: ", text_in, SDL_strlen(text_in));
  632. SDLTest_LogEscapedString("Actual text read from subprocess: ", buffer, total_read);
  633. 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);
  634. SDLTest_AssertCheck(SDL_strcmp(buffer, text_in) == 0, "Subprocess stdout should match text written to stdin");
  635. SDL_free(buffer);
  636. SDLTest_AssertPass("About to destroy processes");
  637. SDL_DestroyProcess(process1);
  638. SDL_DestroyProcess(process2);
  639. return TEST_COMPLETED;
  640. failed:
  641. SDL_DestroyProcess(process1);
  642. SDL_DestroyProcess(process2);
  643. return TEST_ABORTED;
  644. }
  645. static int process_testWriteToFinishedProcess(void *arg)
  646. {
  647. TestProcessData *data = (TestProcessData *)arg;
  648. const char *process_args[] = {
  649. data->childprocess_path,
  650. NULL,
  651. };
  652. SDL_Process *process = NULL;
  653. bool result;
  654. int exit_code;
  655. SDL_IOStream *process_stdin;
  656. const char *text_in = "text_in";
  657. SDLTest_AssertPass("About to call SDL_CreateProcess");
  658. process = SDL_CreateProcess(process_args, true);
  659. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcess()");
  660. if (!process) {
  661. goto failed;
  662. }
  663. exit_code = 0xdeadbeef;
  664. SDLTest_AssertPass("About to call SDL_WaitProcess");
  665. result = SDL_WaitProcess(process, true, &exit_code);
  666. SDLTest_AssertCheck(result, "SDL_WaitProcess()");
  667. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  668. process_stdin = SDL_GetProcessInput(process);
  669. SDLTest_AssertCheck(process_stdin != NULL, "SDL_GetProcessInput returns non-Null SDL_IOStream");
  670. SDLTest_AssertPass("About to call SDL_WriteIO on dead child process");
  671. SDL_WriteIO(process_stdin, text_in, SDL_strlen(text_in));
  672. SDLTest_AssertPass("About to destroy process");
  673. SDL_DestroyProcess(process);
  674. return TEST_COMPLETED;
  675. failed:
  676. SDL_DestroyProcess(process);
  677. return TEST_ABORTED;
  678. }
  679. static int process_testNonExistingExecutable(void *arg)
  680. {
  681. static const int STEM_LENGTH = 16;
  682. char **process_args;
  683. char *random_stem;
  684. char *random_path;
  685. SDL_Process *process = NULL;
  686. random_stem = SDLTest_RandomAsciiStringOfSize(STEM_LENGTH);
  687. random_path = SDL_malloc(STEM_LENGTH + SDL_strlen(EXE) + 1);
  688. SDL_snprintf(random_path, STEM_LENGTH + SDL_strlen(EXE) + 1, "%s%s", random_stem, EXE);
  689. SDL_free(random_stem);
  690. SDLTest_AssertCheck(!SDL_GetPathInfo(random_path, NULL), "%s does not exist", random_path);
  691. process_args = CreateArguments(0, random_path, NULL);
  692. SDL_free(random_path);
  693. SDLTest_AssertPass("About to call SDL_CreateProcess");
  694. process = SDL_CreateProcess((const char * const *)process_args, false);
  695. SDLTest_AssertCheck(process == NULL, "SDL_CreateProcess() should have failed (%s)", SDL_GetError());
  696. DestroyStringArray(process_args);
  697. return TEST_COMPLETED;
  698. }
  699. static int process_testBatBadButVulnerability(void *arg)
  700. {
  701. TestProcessData *data = (TestProcessData *)arg;
  702. char *inject_arg = NULL;
  703. char **process_args = NULL;
  704. char *text_out = NULL;
  705. size_t len_text_out;
  706. int exitcode;
  707. SDL_Process *process = NULL;
  708. SDL_IOStream *child_bat;
  709. char buffer[256];
  710. #ifndef SDL_PLATFORM_WINDOWS
  711. SDLTest_AssertPass("The BatBadBut vulnerability only applied to Windows");
  712. return TEST_SKIPPED;
  713. #endif
  714. /* FIXME: remove child.bat at end of loop and/or create in temporary directory */
  715. child_bat = SDL_IOFromFile("child_batbadbut.bat", "w");
  716. SDL_IOprintf(child_bat, "@echo off\necho Hello from child_batbadbut.bat\necho \"|bat1=%%1|\"\n");
  717. SDL_CloseIO(child_bat);
  718. inject_arg = SDL_malloc(SDL_strlen(data->childprocess_path) + 100);
  719. SDL_snprintf(inject_arg, SDL_strlen(data->childprocess_path) + 100, "\"&%s --version --print-arguments --stdout OWNEDSTDOUT\"", data->childprocess_path);
  720. process_args = CreateArguments(0, "child_batbadbut.bat", inject_arg, NULL);
  721. SDLTest_AssertPass("About to call SDL_CreateProcess");
  722. process = SDL_CreateProcess((const char * const*)process_args, true);
  723. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcess");
  724. if (!process) {
  725. goto cleanup;
  726. }
  727. text_out = SDL_ReadProcess(process, &len_text_out, &exitcode);
  728. SDLTest_AssertCheck(exitcode == 0, "process exited with exitcode 0, was %d", exitcode);
  729. SDLTest_AssertCheck(text_out != NULL, "SDL_ReadProcess returned data");
  730. SDLTest_LogEscapedString("Output: ", text_out, len_text_out);
  731. if (!text_out) {
  732. goto cleanup;
  733. }
  734. SDLTest_AssertCheck(SDL_strstr(text_out, "Hello from child_batbadbut") != NULL, "stdout contains 'Hello from child'");
  735. SDLTest_AssertCheck(SDL_strstr(text_out, "SDL version") == NULL, "stdout should not contain SDL version");
  736. SDL_snprintf(buffer, sizeof(buffer), "|bat1=\"\"\"&%s\"\"|", process_args[1] + 2);
  737. SDLTest_LogEscapedString("stdout should contain: ", buffer, SDL_strlen(buffer));
  738. SDLTest_AssertCheck(SDL_strstr(text_out, buffer) != NULL, "Verify first argument");
  739. cleanup:
  740. SDL_free(text_out);
  741. SDL_DestroyProcess(process);
  742. SDL_free(inject_arg);
  743. DestroyStringArray(process_args);
  744. return TEST_COMPLETED;
  745. }
  746. static int process_testFileRedirection(void *arg)
  747. {
  748. TestProcessData *data = (TestProcessData *)arg;
  749. SDL_PropertiesID props = 0;
  750. const char * process_args[] = {
  751. data->childprocess_path,
  752. "--stdin-to-stdout",
  753. "--stdin-to-stderr",
  754. NULL,
  755. };
  756. const char TEXT_REF[] = "This is input for the child process";
  757. static const char *PATH_STDIN = "test_redirection_stdin.txt";
  758. static const char *PATH_STDOUT = "test_redirection_stdout.txt";
  759. static const char *PATH_STDERR = "test_redirection_stderr.txt";
  760. char *text_out = NULL;
  761. size_t len_text_out;
  762. int exitcode;
  763. bool result;
  764. SDL_Process *process = NULL;
  765. SDL_IOStream *stream;
  766. SDL_IOStream *input_stream = NULL;
  767. SDL_IOStream *output_stream = NULL;
  768. SDL_IOStream *error_stream = NULL;
  769. stream = SDL_IOFromFile(PATH_STDIN, "w");
  770. SDLTest_AssertCheck(stream != NULL, "SDL_IOFromFile(\"%s\", \"w\")", PATH_STDIN);
  771. if (!stream) {
  772. goto cleanup;
  773. }
  774. SDL_WriteIO(stream, TEXT_REF, sizeof(TEXT_REF));
  775. SDL_CloseIO(stream);
  776. input_stream = SDL_IOFromFile(PATH_STDIN, "r");
  777. SDLTest_AssertCheck(input_stream != NULL, "SDL_IOFromFile(\"%s\", \"r\")", PATH_STDIN);
  778. if (!input_stream) {
  779. goto cleanup;
  780. }
  781. output_stream = SDL_IOFromFile(PATH_STDOUT, "w");
  782. SDLTest_AssertCheck(output_stream != NULL, "SDL_IOFromFile(\"%s\", \"w\")", PATH_STDOUT);
  783. if (!output_stream) {
  784. goto cleanup;
  785. }
  786. error_stream = SDL_IOFromFile(PATH_STDERR, "w");
  787. SDLTest_AssertCheck(error_stream != NULL, "SDL_IOFromFile(\"%s\", \"w\")", PATH_STDERR);
  788. if (!error_stream) {
  789. goto cleanup;
  790. }
  791. props = SDL_CreateProperties();
  792. SDLTest_AssertCheck(props != 0, "SDL_CreateProperties()");
  793. if (!props) {
  794. goto cleanup;
  795. }
  796. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)process_args);
  797. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_REDIRECT);
  798. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_POINTER, (void *)input_stream);
  799. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_REDIRECT);
  800. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_POINTER, (void *)output_stream);
  801. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDERR_NUMBER, SDL_PROCESS_STDIO_REDIRECT);
  802. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_STDERR_POINTER, (void *)error_stream);
  803. process = SDL_CreateProcessWithProperties(props);
  804. SDL_DestroyProperties(props);
  805. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcessWithProperties (%s)", SDL_GetError());
  806. if (!process) {
  807. goto cleanup;
  808. }
  809. exitcode = 0xdeadbeef;
  810. text_out = SDL_ReadProcess(process, &len_text_out, &exitcode);
  811. SDLTest_AssertCheck(text_out == NULL, "SDL_ReadProcess should not be able to close a redirected process (%s)", SDL_GetError());
  812. SDLTest_AssertCheck(len_text_out == 0, "length written by SDL_ReadProcess should be 0");
  813. SDL_free(text_out);
  814. text_out = NULL;
  815. exitcode = 0xdeadbeef;
  816. result = SDL_WaitProcess(process, true, &exitcode);
  817. SDLTest_AssertCheck(result, "process must have exited");
  818. SDLTest_AssertCheck(exitcode == 0, "process exited with exitcode 0, was %d", exitcode);
  819. SDL_CloseIO(input_stream);
  820. input_stream = NULL;
  821. SDL_CloseIO(output_stream);
  822. output_stream = NULL;
  823. SDL_CloseIO(error_stream);
  824. error_stream = NULL;
  825. text_out = SDL_LoadFile(PATH_STDOUT, &len_text_out);
  826. SDLTest_AssertCheck(text_out != NULL, "SDL_LoadFile(\"%s\") succeeded (%s)", PATH_STDOUT, SDL_GetError());
  827. SDLTest_AssertPass("Comparing stdout with reference");
  828. SDLTest_CompareMemory(text_out, len_text_out, TEXT_REF, sizeof(TEXT_REF));
  829. SDL_free(text_out);
  830. text_out = SDL_LoadFile(PATH_STDERR, &len_text_out);
  831. SDLTest_AssertCheck(text_out != NULL, "SDL_LoadFile(\"%s\") succeeded (%s)", PATH_STDERR, SDL_GetError());
  832. SDLTest_AssertPass("Comparing stderr with reference");
  833. SDLTest_CompareMemory(text_out, len_text_out, TEXT_REF, sizeof(TEXT_REF));
  834. SDL_free(text_out);
  835. cleanup:
  836. SDL_CloseIO(input_stream);
  837. SDL_CloseIO(output_stream);
  838. SDL_CloseIO(error_stream);
  839. SDL_DestroyProcess(process);
  840. return TEST_COMPLETED;
  841. }
  842. static const SDLTest_TestCaseReference processTestArguments = {
  843. process_testArguments, "process_testArguments", "Test passing arguments to child process", TEST_ENABLED
  844. };
  845. static const SDLTest_TestCaseReference processTestExitCode = {
  846. process_testexitCode, "process_testExitCode", "Test exit codes", TEST_ENABLED
  847. };
  848. static const SDLTest_TestCaseReference processTestInheritedEnv = {
  849. process_testInheritedEnv, "process_testInheritedEnv", "Test inheriting environment from parent process", TEST_ENABLED
  850. };
  851. static const SDLTest_TestCaseReference processTestNewEnv = {
  852. process_testNewEnv, "process_testNewEnv", "Test creating new environment for child process", TEST_ENABLED
  853. };
  854. static const SDLTest_TestCaseReference processTestKill = {
  855. process_testKill, "process_testKill", "Test Killing a child process", TEST_ENABLED
  856. };
  857. static const SDLTest_TestCaseReference processTestStdinToStdout = {
  858. process_testStdinToStdout, "process_testStdinToStdout", "Test writing to stdin and reading from stdout", TEST_ENABLED
  859. };
  860. static const SDLTest_TestCaseReference processTestStdinToStderr = {
  861. process_testStdinToStderr, "process_testStdinToStderr", "Test writing to stdin and reading from stderr", TEST_ENABLED
  862. };
  863. static const SDLTest_TestCaseReference processTestSimpleStdinToStdout = {
  864. process_testSimpleStdinToStdout, "process_testSimpleStdinToStdout", "Test writing to stdin and reading from stdout using the simplified API", TEST_ENABLED
  865. };
  866. static const SDLTest_TestCaseReference processTestMultiprocessStdinToStdout = {
  867. process_testMultiprocessStdinToStdout, "process_testMultiprocessStdinToStdout", "Test writing to stdin and reading from stdout using the simplified API", TEST_ENABLED
  868. };
  869. static const SDLTest_TestCaseReference processTestWriteToFinishedProcess = {
  870. process_testWriteToFinishedProcess, "process_testWriteToFinishedProcess", "Test writing to stdin of terminated process", TEST_ENABLED
  871. };
  872. static const SDLTest_TestCaseReference processTestNonExistingExecutable = {
  873. process_testNonExistingExecutable, "process_testNonExistingExecutable", "Test running a non-existing executable", TEST_ENABLED
  874. };
  875. static const SDLTest_TestCaseReference processTestBatBadButVulnerability = {
  876. process_testBatBadButVulnerability, "process_testBatBadButVulnerability", "Test BatBadBut vulnerability: command injection through cmd.exe", TEST_ENABLED
  877. };
  878. static const SDLTest_TestCaseReference processTestFileRedirection = {
  879. process_testFileRedirection, "process_testFileRedirection", "Test redirection from/to files", TEST_ENABLED
  880. };
  881. static const SDLTest_TestCaseReference *processTests[] = {
  882. &processTestArguments,
  883. &processTestExitCode,
  884. &processTestInheritedEnv,
  885. &processTestNewEnv,
  886. &processTestKill,
  887. &processTestStdinToStdout,
  888. &processTestStdinToStderr,
  889. &processTestSimpleStdinToStdout,
  890. &processTestMultiprocessStdinToStdout,
  891. &processTestWriteToFinishedProcess,
  892. &processTestNonExistingExecutable,
  893. &processTestBatBadButVulnerability,
  894. &processTestFileRedirection,
  895. NULL
  896. };
  897. static SDLTest_TestSuiteReference processTestSuite = {
  898. "Process",
  899. setUpProcess,
  900. processTests,
  901. NULL
  902. };
  903. static SDLTest_TestSuiteReference *testSuites[] = {
  904. &processTestSuite,
  905. NULL
  906. };
  907. int main(int argc, char *argv[])
  908. {
  909. int i;
  910. int result;
  911. SDLTest_CommonState *state;
  912. SDLTest_TestSuiteRunner *runner;
  913. /* Initialize test framework */
  914. state = SDLTest_CommonCreateState(argv, 0);
  915. if (!state) {
  916. return 1;
  917. }
  918. runner = SDLTest_CreateTestSuiteRunner(state, testSuites);
  919. /* Parse commandline */
  920. for (i = 1; i < argc;) {
  921. int consumed;
  922. consumed = SDLTest_CommonArg(state, i);
  923. if (!consumed) {
  924. if (!parsed_args.childprocess_path) {
  925. parsed_args.childprocess_path = argv[i];
  926. consumed = 1;
  927. }
  928. }
  929. if (consumed <= 0) {
  930. SDLTest_CommonLogUsage(state, argv[0], options);
  931. return 1;
  932. }
  933. i += consumed;
  934. }
  935. if (!parsed_args.childprocess_path) {
  936. SDLTest_CommonLogUsage(state, argv[0], options);
  937. return 1;
  938. }
  939. result = SDLTest_ExecuteTestSuiteRunner(runner);
  940. SDL_Quit();
  941. SDLTest_DestroyTestSuiteRunner(runner);
  942. SDLTest_CommonDestroyState(state);
  943. return result;
  944. }