testprocess.c 46 KB

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