testprocess.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  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\v\r\n",
  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 int process_testWindowsCmdline(void *arg)
  845. {
  846. TestProcessData *data = (TestProcessData *)arg;
  847. const char *process_args[] = {
  848. data->childprocess_path,
  849. "--print-arguments",
  850. "--",
  851. "",
  852. " ",
  853. "a b c",
  854. "a\tb\tc\t",
  855. "\"a b\" c",
  856. "'a' 'b' 'c'",
  857. "%d%%%s",
  858. "\\t\\c",
  859. "evil\\",
  860. "a\\b\"c\\",
  861. "\"\\^&|<>%", /* characters with a special meaning */
  862. NULL
  863. };
  864. /* this will have the same result as process_args, but escaped in a different way */
  865. const char *process_cmdline_template =
  866. "%s "
  867. "--print-arguments "
  868. "-- "
  869. "\"\" "
  870. "\" \" "
  871. "a\" \"b\" \"c\t" /* using tab as delimiter */
  872. "\"a\tb\tc\t\" "
  873. "\"\"\"\"a b\"\"\" c\" "
  874. "\"'a' 'b' 'c'\" "
  875. "%%d%%%%%%s " /* will be passed to sprintf */
  876. "\\t\\c "
  877. "evil\\ "
  878. "a\\b\"\\\"\"c\\ "
  879. "\\\"\\^&|<>%%";
  880. char process_cmdline[65535];
  881. SDL_PropertiesID props;
  882. SDL_Process *process = NULL;
  883. char *buffer;
  884. int exit_code;
  885. int i;
  886. size_t total_read = 0;
  887. #ifndef SDL_PLATFORM_WINDOWS
  888. SDLTest_AssertPass("SDL_PROP_PROCESS_CREATE_CMDLINE_STRING only works on Windows");
  889. return TEST_SKIPPED;
  890. #endif
  891. props = SDL_CreateProperties();
  892. SDLTest_AssertCheck(props != 0, "SDL_CreateProperties()");
  893. if (!props) {
  894. goto failed;
  895. }
  896. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_APP);
  897. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_APP);
  898. SDL_SetBooleanProperty(props, SDL_PROP_PROCESS_CREATE_STDERR_TO_STDOUT_BOOLEAN, true);
  899. process = SDL_CreateProcessWithProperties(props);
  900. SDLTest_AssertCheck(process == NULL, "SDL_CreateProcessWithProperties() should fail");
  901. SDL_snprintf(process_cmdline, SDL_arraysize(process_cmdline), process_cmdline_template, data->childprocess_path);
  902. SDL_SetStringProperty(props, SDL_PROP_PROCESS_CREATE_CMDLINE_STRING, process_cmdline);
  903. process = SDL_CreateProcessWithProperties(props);
  904. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcessWithProperties()");
  905. if (!process) {
  906. goto failed;
  907. }
  908. exit_code = 0xdeadbeef;
  909. buffer = (char *)SDL_ReadProcess(process, &total_read, &exit_code);
  910. SDLTest_AssertCheck(buffer != NULL, "SDL_ReadProcess()");
  911. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  912. if (!buffer) {
  913. goto failed;
  914. }
  915. SDLTest_LogEscapedString("stdout of process: ", buffer, total_read);
  916. for (i = 3; process_args[i]; i++) {
  917. char line[64];
  918. SDL_snprintf(line, sizeof(line), "|%d=%s|", i - 3, process_args[i]);
  919. SDLTest_AssertCheck(!!SDL_strstr(buffer, line), "Check %s is in output", line);
  920. }
  921. SDL_free(buffer);
  922. SDLTest_AssertPass("About to destroy process");
  923. SDL_DestroyProcess(process);
  924. return TEST_COMPLETED;
  925. failed:
  926. SDL_DestroyProcess(process);
  927. return TEST_ABORTED;
  928. }
  929. static int process_testWindowsCmdlinePrecedence(void *arg)
  930. {
  931. TestProcessData *data = (TestProcessData *)arg;
  932. const char *process_args[] = {
  933. data->childprocess_path,
  934. "--print-arguments",
  935. "--",
  936. "argument 1",
  937. NULL
  938. };
  939. const char *process_cmdline_template = "%s --print-arguments -- \"argument 2\"";
  940. char process_cmdline[65535];
  941. SDL_PropertiesID props;
  942. SDL_Process *process = NULL;
  943. char *buffer;
  944. int exit_code;
  945. size_t total_read = 0;
  946. #ifndef SDL_PLATFORM_WINDOWS
  947. SDLTest_AssertPass("SDL_PROP_PROCESS_CREATE_CMDLINE_STRING only works on Windows");
  948. return TEST_SKIPPED;
  949. #endif
  950. props = SDL_CreateProperties();
  951. SDLTest_AssertCheck(props != 0, "SDL_CreateProperties()");
  952. if (!props) {
  953. goto failed;
  954. }
  955. SDL_snprintf(process_cmdline, SDL_arraysize(process_cmdline), process_cmdline_template, data->childprocess_path);
  956. SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)process_args);
  957. SDL_SetStringProperty(props, SDL_PROP_PROCESS_CREATE_CMDLINE_STRING, (const char *)process_cmdline);
  958. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_APP);
  959. SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_APP);
  960. SDL_SetBooleanProperty(props, SDL_PROP_PROCESS_CREATE_STDERR_TO_STDOUT_BOOLEAN, true);
  961. process = SDL_CreateProcessWithProperties(props);
  962. SDLTest_AssertCheck(process != NULL, "SDL_CreateProcessWithProperties()");
  963. if (!process) {
  964. goto failed;
  965. }
  966. exit_code = 0xdeadbeef;
  967. buffer = (char *)SDL_ReadProcess(process, &total_read, &exit_code);
  968. SDLTest_AssertCheck(buffer != NULL, "SDL_ReadProcess()");
  969. SDLTest_AssertCheck(exit_code == 0, "Exit code should be 0, is %d", exit_code);
  970. if (!buffer) {
  971. goto failed;
  972. }
  973. SDLTest_LogEscapedString("stdout of process: ", buffer, total_read);
  974. SDLTest_AssertCheck(!!SDL_strstr(buffer, "|0=argument 2|"), "Check |0=argument 2| is printed");
  975. SDL_free(buffer);
  976. SDLTest_AssertPass("About to destroy process");
  977. SDL_DestroyProcess(process);
  978. return TEST_COMPLETED;
  979. failed:
  980. SDL_DestroyProcess(process);
  981. return TEST_ABORTED;
  982. }
  983. static const SDLTest_TestCaseReference processTestArguments = {
  984. process_testArguments, "process_testArguments", "Test passing arguments to child process", TEST_ENABLED
  985. };
  986. static const SDLTest_TestCaseReference processTestExitCode = {
  987. process_testexitCode, "process_testExitCode", "Test exit codes", TEST_ENABLED
  988. };
  989. static const SDLTest_TestCaseReference processTestInheritedEnv = {
  990. process_testInheritedEnv, "process_testInheritedEnv", "Test inheriting environment from parent process", TEST_ENABLED
  991. };
  992. static const SDLTest_TestCaseReference processTestNewEnv = {
  993. process_testNewEnv, "process_testNewEnv", "Test creating new environment for child process", TEST_ENABLED
  994. };
  995. static const SDLTest_TestCaseReference processTestKill = {
  996. process_testKill, "process_testKill", "Test Killing a child process", TEST_ENABLED
  997. };
  998. static const SDLTest_TestCaseReference processTestStdinToStdout = {
  999. process_testStdinToStdout, "process_testStdinToStdout", "Test writing to stdin and reading from stdout", TEST_ENABLED
  1000. };
  1001. static const SDLTest_TestCaseReference processTestStdinToStderr = {
  1002. process_testStdinToStderr, "process_testStdinToStderr", "Test writing to stdin and reading from stderr", TEST_ENABLED
  1003. };
  1004. static const SDLTest_TestCaseReference processTestSimpleStdinToStdout = {
  1005. process_testSimpleStdinToStdout, "process_testSimpleStdinToStdout", "Test writing to stdin and reading from stdout using the simplified API", TEST_ENABLED
  1006. };
  1007. static const SDLTest_TestCaseReference processTestMultiprocessStdinToStdout = {
  1008. process_testMultiprocessStdinToStdout, "process_testMultiprocessStdinToStdout", "Test writing to stdin and reading from stdout using the simplified API", TEST_ENABLED
  1009. };
  1010. static const SDLTest_TestCaseReference processTestWriteToFinishedProcess = {
  1011. process_testWriteToFinishedProcess, "process_testWriteToFinishedProcess", "Test writing to stdin of terminated process", TEST_ENABLED
  1012. };
  1013. static const SDLTest_TestCaseReference processTestNonExistingExecutable = {
  1014. process_testNonExistingExecutable, "process_testNonExistingExecutable", "Test running a non-existing executable", TEST_ENABLED
  1015. };
  1016. static const SDLTest_TestCaseReference processTestBatBadButVulnerability = {
  1017. process_testBatBadButVulnerability, "process_testBatBadButVulnerability", "Test BatBadBut vulnerability: command injection through cmd.exe", TEST_ENABLED
  1018. };
  1019. static const SDLTest_TestCaseReference processTestFileRedirection = {
  1020. process_testFileRedirection, "process_testFileRedirection", "Test redirection from/to files", TEST_ENABLED
  1021. };
  1022. static const SDLTest_TestCaseReference processTestWindowsCmdline = {
  1023. process_testWindowsCmdline, "process_testWindowsCmdline", "Test passing cmdline directly to CreateProcess", TEST_ENABLED
  1024. };
  1025. static const SDLTest_TestCaseReference processTestWindowsCmdlinePrecedence = {
  1026. process_testWindowsCmdlinePrecedence, "process_testWindowsCmdlinePrecedence", "Test SDL_PROP_PROCESS_CREATE_CMDLINE_STRING precedence over SDL_PROP_PROCESS_CREATE_ARGS_POINTER", TEST_ENABLED
  1027. };
  1028. static const SDLTest_TestCaseReference *processTests[] = {
  1029. &processTestArguments,
  1030. &processTestExitCode,
  1031. &processTestInheritedEnv,
  1032. &processTestNewEnv,
  1033. &processTestKill,
  1034. &processTestStdinToStdout,
  1035. &processTestStdinToStderr,
  1036. &processTestSimpleStdinToStdout,
  1037. &processTestMultiprocessStdinToStdout,
  1038. &processTestWriteToFinishedProcess,
  1039. &processTestNonExistingExecutable,
  1040. &processTestBatBadButVulnerability,
  1041. &processTestFileRedirection,
  1042. &processTestWindowsCmdline,
  1043. &processTestWindowsCmdlinePrecedence,
  1044. NULL
  1045. };
  1046. static SDLTest_TestSuiteReference processTestSuite = {
  1047. "Process",
  1048. setUpProcess,
  1049. processTests,
  1050. NULL
  1051. };
  1052. static SDLTest_TestSuiteReference *testSuites[] = {
  1053. &processTestSuite,
  1054. NULL
  1055. };
  1056. int main(int argc, char *argv[])
  1057. {
  1058. int i;
  1059. int result;
  1060. SDLTest_CommonState *state;
  1061. SDLTest_TestSuiteRunner *runner;
  1062. /* Initialize test framework */
  1063. state = SDLTest_CommonCreateState(argv, 0);
  1064. if (!state) {
  1065. return 1;
  1066. }
  1067. runner = SDLTest_CreateTestSuiteRunner(state, testSuites);
  1068. /* Parse commandline */
  1069. for (i = 1; i < argc;) {
  1070. int consumed;
  1071. consumed = SDLTest_CommonArg(state, i);
  1072. if (!consumed) {
  1073. if (!parsed_args.childprocess_path) {
  1074. parsed_args.childprocess_path = argv[i];
  1075. consumed = 1;
  1076. }
  1077. }
  1078. if (consumed <= 0) {
  1079. SDLTest_CommonLogUsage(state, argv[0], options);
  1080. return 1;
  1081. }
  1082. i += consumed;
  1083. }
  1084. if (!parsed_args.childprocess_path) {
  1085. SDLTest_CommonLogUsage(state, argv[0], options);
  1086. return 1;
  1087. }
  1088. result = SDLTest_ExecuteTestSuiteRunner(runner);
  1089. SDL_Quit();
  1090. SDLTest_DestroyTestSuiteRunner(runner);
  1091. SDLTest_CommonDestroyState(state);
  1092. return result;
  1093. }