test-spawn.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <fcntl.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #ifndef _WIN32
  28. #include <unistd.h>
  29. #endif
  30. static int close_cb_called;
  31. static int exit_cb_called;
  32. static uv_process_t process;
  33. static uv_timer_t timer;
  34. static uv_process_options_t options;
  35. static char exepath[1024];
  36. static size_t exepath_size = 1024;
  37. static char* args[3];
  38. static int no_term_signal;
  39. #define OUTPUT_SIZE 1024
  40. static char output[OUTPUT_SIZE];
  41. static int output_used;
  42. static void close_cb(uv_handle_t* handle) {
  43. printf("close_cb\n");
  44. close_cb_called++;
  45. }
  46. static void exit_cb(uv_process_t* process, int exit_status, int term_signal) {
  47. printf("exit_cb\n");
  48. exit_cb_called++;
  49. ASSERT(exit_status == 1);
  50. ASSERT(term_signal == 0);
  51. uv_close((uv_handle_t*)process, close_cb);
  52. }
  53. static void exit_cb_failure_expected(uv_process_t* process, int exit_status,
  54. int term_signal) {
  55. printf("exit_cb\n");
  56. exit_cb_called++;
  57. ASSERT(exit_status == -1);
  58. ASSERT(term_signal == 0);
  59. uv_close((uv_handle_t*)process, close_cb);
  60. }
  61. static void kill_cb(uv_process_t* process, int exit_status, int term_signal) {
  62. uv_err_t err;
  63. printf("exit_cb\n");
  64. exit_cb_called++;
  65. #ifdef _WIN32
  66. ASSERT(exit_status == 1);
  67. #else
  68. ASSERT(exit_status == 0);
  69. #endif
  70. ASSERT(no_term_signal || term_signal == 15);
  71. uv_close((uv_handle_t*)process, close_cb);
  72. /*
  73. * Sending signum == 0 should check if the
  74. * child process is still alive, not kill it.
  75. * This process should be dead.
  76. */
  77. err = uv_kill(process->pid, 0);
  78. ASSERT(err.code == UV_ESRCH);
  79. }
  80. static void detach_failure_cb(uv_process_t* process, int exit_status, int term_signal) {
  81. printf("detach_cb\n");
  82. exit_cb_called++;
  83. }
  84. static uv_buf_t on_alloc(uv_handle_t* handle, size_t suggested_size) {
  85. uv_buf_t buf;
  86. buf.base = output + output_used;
  87. buf.len = OUTPUT_SIZE - output_used;
  88. return buf;
  89. }
  90. static void on_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) {
  91. uv_err_t err = uv_last_error(uv_default_loop());
  92. if (nread > 0) {
  93. output_used += nread;
  94. } else if (nread < 0) {
  95. ASSERT(err.code == UV_EOF);
  96. uv_close((uv_handle_t*)tcp, close_cb);
  97. }
  98. }
  99. static void write_cb(uv_write_t* req, int status) {
  100. ASSERT(status == 0);
  101. uv_close((uv_handle_t*)req->handle, close_cb);
  102. }
  103. static void init_process_options(char* test, uv_exit_cb exit_cb) {
  104. /* Note spawn_helper1 defined in test/run-tests.c */
  105. int r = uv_exepath(exepath, &exepath_size);
  106. ASSERT(r == 0);
  107. exepath[exepath_size] = '\0';
  108. args[0] = exepath;
  109. args[1] = test;
  110. args[2] = NULL;
  111. options.file = exepath;
  112. options.args = args;
  113. options.exit_cb = exit_cb;
  114. options.flags = 0;
  115. }
  116. static void timer_cb(uv_timer_t* handle, int status) {
  117. uv_process_kill(&process, /* SIGTERM */ 15);
  118. uv_close((uv_handle_t*)handle, close_cb);
  119. }
  120. TEST_IMPL(spawn_fails) {
  121. init_process_options("", exit_cb_failure_expected);
  122. options.file = options.args[0] = "program-that-had-better-not-exist";
  123. ASSERT(0 == uv_spawn(uv_default_loop(), &process, options));
  124. ASSERT(0 != uv_is_active((uv_handle_t*)&process));
  125. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  126. ASSERT(uv_last_error(uv_default_loop()).code == UV_ENOENT);
  127. MAKE_VALGRIND_HAPPY();
  128. return 0;
  129. }
  130. TEST_IMPL(spawn_exit_code) {
  131. int r;
  132. init_process_options("spawn_helper1", exit_cb);
  133. r = uv_spawn(uv_default_loop(), &process, options);
  134. ASSERT(r == 0);
  135. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  136. ASSERT(r == 0);
  137. ASSERT(exit_cb_called == 1);
  138. ASSERT(close_cb_called == 1);
  139. MAKE_VALGRIND_HAPPY();
  140. return 0;
  141. }
  142. TEST_IMPL(spawn_stdout) {
  143. int r;
  144. uv_pipe_t out;
  145. uv_stdio_container_t stdio[2];
  146. init_process_options("spawn_helper2", exit_cb);
  147. uv_pipe_init(uv_default_loop(), &out, 0);
  148. options.stdio = stdio;
  149. options.stdio[0].flags = UV_IGNORE;
  150. options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  151. options.stdio[1].data.stream = (uv_stream_t*)&out;
  152. options.stdio_count = 2;
  153. r = uv_spawn(uv_default_loop(), &process, options);
  154. ASSERT(r == 0);
  155. r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
  156. ASSERT(r == 0);
  157. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  158. ASSERT(r == 0);
  159. ASSERT(exit_cb_called == 1);
  160. ASSERT(close_cb_called == 2); /* Once for process once for the pipe. */
  161. printf("output is: %s", output);
  162. ASSERT(strcmp("hello world\n", output) == 0);
  163. MAKE_VALGRIND_HAPPY();
  164. return 0;
  165. }
  166. TEST_IMPL(spawn_stdout_to_file) {
  167. int r;
  168. uv_file file;
  169. uv_fs_t fs_req;
  170. uv_stdio_container_t stdio[2];
  171. /* Setup. */
  172. unlink("stdout_file");
  173. init_process_options("spawn_helper2", exit_cb);
  174. r = uv_fs_open(uv_default_loop(), &fs_req, "stdout_file", O_CREAT | O_RDWR,
  175. S_IREAD | S_IWRITE, NULL);
  176. ASSERT(r != -1);
  177. uv_fs_req_cleanup(&fs_req);
  178. file = r;
  179. options.stdio = stdio;
  180. options.stdio[0].flags = UV_IGNORE;
  181. options.stdio[1].flags = UV_INHERIT_FD;
  182. options.stdio[1].data.fd = file;
  183. options.stdio_count = 2;
  184. r = uv_spawn(uv_default_loop(), &process, options);
  185. ASSERT(r == 0);
  186. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  187. ASSERT(r == 0);
  188. ASSERT(exit_cb_called == 1);
  189. ASSERT(close_cb_called == 1);
  190. r = uv_fs_read(uv_default_loop(), &fs_req, file, output, sizeof(output),
  191. 0, NULL);
  192. ASSERT(r == 12);
  193. uv_fs_req_cleanup(&fs_req);
  194. r = uv_fs_close(uv_default_loop(), &fs_req, file, NULL);
  195. ASSERT(r == 0);
  196. uv_fs_req_cleanup(&fs_req);
  197. printf("output is: %s", output);
  198. ASSERT(strcmp("hello world\n", output) == 0);
  199. /* Cleanup. */
  200. unlink("stdout_file");
  201. MAKE_VALGRIND_HAPPY();
  202. return 0;
  203. }
  204. TEST_IMPL(spawn_stdin) {
  205. int r;
  206. uv_pipe_t out;
  207. uv_pipe_t in;
  208. uv_write_t write_req;
  209. uv_buf_t buf;
  210. uv_stdio_container_t stdio[2];
  211. char buffer[] = "hello-from-spawn_stdin";
  212. init_process_options("spawn_helper3", exit_cb);
  213. uv_pipe_init(uv_default_loop(), &out, 0);
  214. uv_pipe_init(uv_default_loop(), &in, 0);
  215. options.stdio = stdio;
  216. options.stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
  217. options.stdio[0].data.stream = (uv_stream_t*)&in;
  218. options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  219. options.stdio[1].data.stream = (uv_stream_t*)&out;
  220. options.stdio_count = 2;
  221. r = uv_spawn(uv_default_loop(), &process, options);
  222. ASSERT(r == 0);
  223. buf.base = buffer;
  224. buf.len = sizeof(buffer);
  225. r = uv_write(&write_req, (uv_stream_t*)&in, &buf, 1, write_cb);
  226. ASSERT(r == 0);
  227. r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
  228. ASSERT(r == 0);
  229. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  230. ASSERT(r == 0);
  231. ASSERT(exit_cb_called == 1);
  232. ASSERT(close_cb_called == 3); /* Once for process twice for the pipe. */
  233. ASSERT(strcmp(buffer, output) == 0);
  234. MAKE_VALGRIND_HAPPY();
  235. return 0;
  236. }
  237. TEST_IMPL(spawn_stdio_greater_than_3) {
  238. int r;
  239. uv_pipe_t pipe;
  240. uv_stdio_container_t stdio[4];
  241. init_process_options("spawn_helper5", exit_cb);
  242. uv_pipe_init(uv_default_loop(), &pipe, 0);
  243. options.stdio = stdio;
  244. options.stdio[0].flags = UV_IGNORE;
  245. options.stdio[1].flags = UV_IGNORE;
  246. options.stdio[2].flags = UV_IGNORE;
  247. options.stdio[3].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  248. options.stdio[3].data.stream = (uv_stream_t*)&pipe;
  249. options.stdio_count = 4;
  250. r = uv_spawn(uv_default_loop(), &process, options);
  251. ASSERT(r == 0);
  252. r = uv_read_start((uv_stream_t*) &pipe, on_alloc, on_read);
  253. ASSERT(r == 0);
  254. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  255. ASSERT(r == 0);
  256. ASSERT(exit_cb_called == 1);
  257. ASSERT(close_cb_called == 2); /* Once for process once for the pipe. */
  258. printf("output from stdio[3] is: %s", output);
  259. ASSERT(strcmp("fourth stdio!\n", output) == 0);
  260. MAKE_VALGRIND_HAPPY();
  261. return 0;
  262. }
  263. TEST_IMPL(spawn_ignored_stdio) {
  264. int r;
  265. init_process_options("spawn_helper6", exit_cb);
  266. options.stdio = NULL;
  267. options.stdio_count = 0;
  268. r = uv_spawn(uv_default_loop(), &process, options);
  269. ASSERT(r == 0);
  270. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  271. ASSERT(r == 0);
  272. ASSERT(exit_cb_called == 1);
  273. ASSERT(close_cb_called == 1);
  274. MAKE_VALGRIND_HAPPY();
  275. return 0;
  276. }
  277. TEST_IMPL(spawn_and_kill) {
  278. int r;
  279. init_process_options("spawn_helper4", kill_cb);
  280. r = uv_spawn(uv_default_loop(), &process, options);
  281. ASSERT(r == 0);
  282. r = uv_timer_init(uv_default_loop(), &timer);
  283. ASSERT(r == 0);
  284. r = uv_timer_start(&timer, timer_cb, 500, 0);
  285. ASSERT(r == 0);
  286. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  287. ASSERT(r == 0);
  288. ASSERT(exit_cb_called == 1);
  289. ASSERT(close_cb_called == 2); /* Once for process and once for timer. */
  290. MAKE_VALGRIND_HAPPY();
  291. return 0;
  292. }
  293. TEST_IMPL(spawn_preserve_env) {
  294. int r;
  295. uv_pipe_t out;
  296. uv_stdio_container_t stdio[2];
  297. init_process_options("spawn_helper7", exit_cb);
  298. uv_pipe_init(uv_default_loop(), &out, 0);
  299. options.stdio = stdio;
  300. options.stdio[0].flags = UV_IGNORE;
  301. options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  302. options.stdio[1].data.stream = (uv_stream_t*) &out;
  303. options.stdio_count = 2;
  304. r = putenv("ENV_TEST=testval");
  305. ASSERT(r == 0);
  306. /* Explicitly set options.env to NULL to test for env clobbering. */
  307. options.env = NULL;
  308. r = uv_spawn(uv_default_loop(), &process, options);
  309. ASSERT(r == 0);
  310. r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
  311. ASSERT(r == 0);
  312. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  313. ASSERT(r == 0);
  314. ASSERT(exit_cb_called == 1);
  315. ASSERT(close_cb_called == 2);
  316. printf("output is: %s", output);
  317. ASSERT(strcmp("testval", output) == 0);
  318. MAKE_VALGRIND_HAPPY();
  319. return 0;
  320. }
  321. TEST_IMPL(spawn_detached) {
  322. int r;
  323. uv_err_t err;
  324. init_process_options("spawn_helper4", detach_failure_cb);
  325. options.flags |= UV_PROCESS_DETACHED;
  326. r = uv_spawn(uv_default_loop(), &process, options);
  327. ASSERT(r == 0);
  328. uv_unref((uv_handle_t*)&process);
  329. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  330. ASSERT(r == 0);
  331. ASSERT(exit_cb_called == 0);
  332. err = uv_kill(process.pid, 0);
  333. ASSERT(err.code == 0);
  334. err = uv_kill(process.pid, 15);
  335. ASSERT(err.code == 0);
  336. MAKE_VALGRIND_HAPPY();
  337. return 0;
  338. }
  339. TEST_IMPL(spawn_and_kill_with_std) {
  340. int r;
  341. uv_pipe_t in, out, err;
  342. uv_write_t write;
  343. char message[] = "Nancy's joining me because the message this evening is "
  344. "not my message but ours.";
  345. uv_buf_t buf;
  346. uv_stdio_container_t stdio[3];
  347. init_process_options("spawn_helper4", kill_cb);
  348. r = uv_pipe_init(uv_default_loop(), &in, 0);
  349. ASSERT(r == 0);
  350. r = uv_pipe_init(uv_default_loop(), &out, 0);
  351. ASSERT(r == 0);
  352. r = uv_pipe_init(uv_default_loop(), &err, 0);
  353. ASSERT(r == 0);
  354. options.stdio = stdio;
  355. options.stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
  356. options.stdio[0].data.stream = (uv_stream_t*)&in;
  357. options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  358. options.stdio[1].data.stream = (uv_stream_t*)&out;
  359. options.stdio[2].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  360. options.stdio[2].data.stream = (uv_stream_t*)&err;
  361. options.stdio_count = 3;
  362. r = uv_spawn(uv_default_loop(), &process, options);
  363. ASSERT(r == 0);
  364. buf = uv_buf_init(message, sizeof message);
  365. r = uv_write(&write, (uv_stream_t*) &in, &buf, 1, write_cb);
  366. ASSERT(r == 0);
  367. r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
  368. ASSERT(r == 0);
  369. r = uv_read_start((uv_stream_t*) &err, on_alloc, on_read);
  370. ASSERT(r == 0);
  371. r = uv_timer_init(uv_default_loop(), &timer);
  372. ASSERT(r == 0);
  373. r = uv_timer_start(&timer, timer_cb, 500, 0);
  374. ASSERT(r == 0);
  375. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  376. ASSERT(r == 0);
  377. ASSERT(exit_cb_called == 1);
  378. ASSERT(close_cb_called == 5); /* process x 1, timer x 1, stdio x 3. */
  379. MAKE_VALGRIND_HAPPY();
  380. return 0;
  381. }
  382. TEST_IMPL(spawn_and_ping) {
  383. uv_write_t write_req;
  384. uv_pipe_t in, out;
  385. uv_buf_t buf;
  386. uv_stdio_container_t stdio[2];
  387. int r;
  388. init_process_options("spawn_helper3", exit_cb);
  389. buf = uv_buf_init("TEST", 4);
  390. uv_pipe_init(uv_default_loop(), &out, 0);
  391. uv_pipe_init(uv_default_loop(), &in, 0);
  392. options.stdio = stdio;
  393. options.stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
  394. options.stdio[0].data.stream = (uv_stream_t*)&in;
  395. options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  396. options.stdio[1].data.stream = (uv_stream_t*)&out;
  397. options.stdio_count = 2;
  398. r = uv_spawn(uv_default_loop(), &process, options);
  399. ASSERT(r == 0);
  400. /* Sending signum == 0 should check if the
  401. * child process is still alive, not kill it.
  402. */
  403. r = uv_process_kill(&process, 0);
  404. ASSERT(r == 0);
  405. r = uv_write(&write_req, (uv_stream_t*)&in, &buf, 1, write_cb);
  406. ASSERT(r == 0);
  407. r = uv_read_start((uv_stream_t*)&out, on_alloc, on_read);
  408. ASSERT(r == 0);
  409. ASSERT(exit_cb_called == 0);
  410. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  411. ASSERT(r == 0);
  412. ASSERT(exit_cb_called == 1);
  413. ASSERT(strcmp(output, "TEST") == 0);
  414. MAKE_VALGRIND_HAPPY();
  415. return 0;
  416. }
  417. TEST_IMPL(kill) {
  418. int r;
  419. uv_err_t err;
  420. #ifdef _WIN32
  421. no_term_signal = 1;
  422. #endif
  423. init_process_options("spawn_helper4", kill_cb);
  424. r = uv_spawn(uv_default_loop(), &process, options);
  425. ASSERT(r == 0);
  426. /* Sending signum == 0 should check if the
  427. * child process is still alive, not kill it.
  428. */
  429. err = uv_kill(process.pid, 0);
  430. ASSERT(err.code == UV_OK);
  431. /* Kill the process. */
  432. err = uv_kill(process.pid, /* SIGTERM */ 15);
  433. ASSERT(err.code == UV_OK);
  434. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  435. ASSERT(r == 0);
  436. ASSERT(exit_cb_called == 1);
  437. ASSERT(close_cb_called == 1);
  438. MAKE_VALGRIND_HAPPY();
  439. return 0;
  440. }
  441. #ifdef _WIN32
  442. TEST_IMPL(spawn_detect_pipe_name_collisions_on_windows) {
  443. int r;
  444. uv_pipe_t out;
  445. char name[64];
  446. HANDLE pipe_handle;
  447. uv_stdio_container_t stdio[2];
  448. init_process_options("spawn_helper2", exit_cb);
  449. uv_pipe_init(uv_default_loop(), &out, 0);
  450. options.stdio = stdio;
  451. options.stdio[0].flags = UV_IGNORE;
  452. options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  453. options.stdio[1].data.stream = (uv_stream_t*)&out;
  454. options.stdio_count = 2;
  455. /* Create a pipe that'll cause a collision. */
  456. _snprintf(name, sizeof(name), "\\\\.\\pipe\\uv\\%p-%d", &out, GetCurrentProcessId());
  457. pipe_handle = CreateNamedPipeA(name,
  458. PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,
  459. PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
  460. 10,
  461. 65536,
  462. 65536,
  463. 0,
  464. NULL);
  465. ASSERT(pipe_handle != INVALID_HANDLE_VALUE);
  466. r = uv_spawn(uv_default_loop(), &process, options);
  467. ASSERT(r == 0);
  468. r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
  469. ASSERT(r == 0);
  470. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  471. ASSERT(r == 0);
  472. ASSERT(exit_cb_called == 1);
  473. ASSERT(close_cb_called == 2); /* Once for process once for the pipe. */
  474. printf("output is: %s", output);
  475. ASSERT(strcmp("hello world\n", output) == 0);
  476. MAKE_VALGRIND_HAPPY();
  477. return 0;
  478. }
  479. uv_err_t make_program_args(char** args, int verbatim_arguments, WCHAR** dst_ptr);
  480. WCHAR* quote_cmd_arg(const WCHAR *source, WCHAR *target);
  481. TEST_IMPL(argument_escaping) {
  482. const WCHAR* test_str[] = {
  483. L"HelloWorld",
  484. L"Hello World",
  485. L"Hello\"World",
  486. L"Hello World\\",
  487. L"Hello\\\"World",
  488. L"Hello\\World",
  489. L"Hello\\\\World",
  490. L"Hello World\\",
  491. L"c:\\path\\to\\node.exe --eval \"require('c:\\\\path\\\\to\\\\test.js')\""
  492. };
  493. const int count = sizeof(test_str) / sizeof(*test_str);
  494. WCHAR** test_output;
  495. WCHAR* command_line;
  496. WCHAR** cracked;
  497. size_t total_size = 0;
  498. int i;
  499. int num_args;
  500. uv_err_t result;
  501. char* verbatim[] = {
  502. "cmd.exe",
  503. "/c",
  504. "c:\\path\\to\\node.exe --eval \"require('c:\\\\path\\\\to\\\\test.js')\"",
  505. NULL
  506. };
  507. WCHAR* verbatim_output;
  508. WCHAR* non_verbatim_output;
  509. test_output = calloc(count, sizeof(WCHAR*));
  510. for (i = 0; i < count; ++i) {
  511. test_output[i] = calloc(2 * (wcslen(test_str[i]) + 2), sizeof(WCHAR));
  512. quote_cmd_arg(test_str[i], test_output[i]);
  513. wprintf(L"input : %s\n", test_str[i]);
  514. wprintf(L"output: %s\n", test_output[i]);
  515. total_size += wcslen(test_output[i]) + 1;
  516. }
  517. command_line = calloc(total_size + 1, sizeof(WCHAR));
  518. for (i = 0; i < count; ++i) {
  519. wcscat(command_line, test_output[i]);
  520. wcscat(command_line, L" ");
  521. }
  522. command_line[total_size - 1] = L'\0';
  523. wprintf(L"command_line: %s\n", command_line);
  524. cracked = CommandLineToArgvW(command_line, &num_args);
  525. for (i = 0; i < num_args; ++i) {
  526. wprintf(L"%d: %s\t%s\n", i, test_str[i], cracked[i]);
  527. ASSERT(wcscmp(test_str[i], cracked[i]) == 0);
  528. }
  529. LocalFree(cracked);
  530. for (i = 0; i < count; ++i) {
  531. free(test_output[i]);
  532. }
  533. result = make_program_args(verbatim, 1, &verbatim_output);
  534. ASSERT(result.code == UV_OK);
  535. result = make_program_args(verbatim, 0, &non_verbatim_output);
  536. ASSERT(result.code == UV_OK);
  537. wprintf(L" verbatim_output: %s\n", verbatim_output);
  538. wprintf(L"non_verbatim_output: %s\n", non_verbatim_output);
  539. ASSERT(wcscmp(verbatim_output, L"cmd.exe /c c:\\path\\to\\node.exe --eval \"require('c:\\\\path\\\\to\\\\test.js')\"") == 0);
  540. ASSERT(wcscmp(non_verbatim_output, L"cmd.exe /c \"c:\\path\\to\\node.exe --eval \\\"require('c:\\\\path\\\\to\\\\test.js')\\\"\"") == 0);
  541. free(verbatim_output);
  542. free(non_verbatim_output);
  543. return 0;
  544. }
  545. uv_err_t make_program_env(char** env_block, WCHAR** dst_ptr);
  546. TEST_IMPL(environment_creation) {
  547. int i;
  548. char* environment[] = {
  549. "FOO=BAR",
  550. "SYSTEM=ROOT", /* substring of a supplied var name */
  551. "SYSTEMROOTED=OMG", /* supplied var name is a substring */
  552. "TEMP=C:\\Temp",
  553. "BAZ=QUX",
  554. NULL
  555. };
  556. WCHAR expected[512];
  557. WCHAR* ptr = expected;
  558. uv_err_t result;
  559. WCHAR* str;
  560. WCHAR* env;
  561. for (i = 0; i < sizeof(environment) / sizeof(environment[0]) - 1; i++) {
  562. ptr += uv_utf8_to_utf16(environment[i], ptr, expected + sizeof(expected) - ptr);
  563. }
  564. memcpy(ptr, L"SYSTEMROOT=", sizeof(L"SYSTEMROOT="));
  565. ptr += sizeof(L"SYSTEMROOT=")/sizeof(WCHAR) - 1;
  566. ptr += GetEnvironmentVariableW(L"SYSTEMROOT", ptr, expected + sizeof(expected) - ptr);
  567. ++ptr;
  568. memcpy(ptr, L"SYSTEMDRIVE=", sizeof(L"SYSTEMDRIVE="));
  569. ptr += sizeof(L"SYSTEMDRIVE=")/sizeof(WCHAR) - 1;
  570. ptr += GetEnvironmentVariableW(L"SYSTEMDRIVE", ptr, expected + sizeof(expected) - ptr);
  571. ++ptr;
  572. *ptr = '\0';
  573. result = make_program_env(environment, &env);
  574. ASSERT(result.code == UV_OK);
  575. for (str = env; *str; str += wcslen(str) + 1) {
  576. wprintf(L"%s\n", str);
  577. }
  578. ASSERT(wcscmp(expected, env) == 0);
  579. return 0;
  580. }
  581. #endif
  582. #ifndef _WIN32
  583. TEST_IMPL(spawn_setuid_setgid) {
  584. int r;
  585. /* if not root, then this will fail. */
  586. uv_uid_t uid = getuid();
  587. if (uid != 0) {
  588. fprintf(stderr, "spawn_setuid_setgid skipped: not root\n");
  589. return 0;
  590. }
  591. init_process_options("spawn_helper1", exit_cb);
  592. /* become the "nobody" user. */
  593. struct passwd* pw;
  594. pw = getpwnam("nobody");
  595. ASSERT(pw != NULL);
  596. options.uid = pw->pw_uid;
  597. options.gid = pw->pw_gid;
  598. options.flags = UV_PROCESS_SETUID | UV_PROCESS_SETGID;
  599. r = uv_spawn(uv_default_loop(), &process, options);
  600. ASSERT(r == 0);
  601. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  602. ASSERT(r == 0);
  603. ASSERT(exit_cb_called == 1);
  604. ASSERT(close_cb_called == 1);
  605. MAKE_VALGRIND_HAPPY();
  606. return 0;
  607. }
  608. #endif
  609. #ifndef _WIN32
  610. TEST_IMPL(spawn_setuid_fails) {
  611. int r;
  612. /* if root, become nobody. */
  613. uv_uid_t uid = getuid();
  614. if (uid == 0) {
  615. struct passwd* pw;
  616. pw = getpwnam("nobody");
  617. ASSERT(pw != NULL);
  618. r = setuid(pw->pw_uid);
  619. ASSERT(r == 0);
  620. }
  621. init_process_options("spawn_helper1", exit_cb_failure_expected);
  622. options.flags |= UV_PROCESS_SETUID;
  623. options.uid = (uv_uid_t) -42424242;
  624. r = uv_spawn(uv_default_loop(), &process, options);
  625. ASSERT(r == 0);
  626. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  627. ASSERT(r == 0);
  628. ASSERT(exit_cb_called == 1);
  629. ASSERT(close_cb_called == 1);
  630. MAKE_VALGRIND_HAPPY();
  631. return 0;
  632. }
  633. TEST_IMPL(spawn_setgid_fails) {
  634. int r;
  635. /* if root, become nobody. */
  636. uv_uid_t uid = getuid();
  637. if (uid == 0) {
  638. struct passwd* pw;
  639. pw = getpwnam("nobody");
  640. ASSERT(pw != NULL);
  641. r = setuid(pw->pw_uid);
  642. ASSERT(r == 0);
  643. }
  644. init_process_options("spawn_helper1", exit_cb_failure_expected);
  645. options.flags |= UV_PROCESS_SETGID;
  646. options.gid = (uv_gid_t) -42424242;
  647. r = uv_spawn(uv_default_loop(), &process, options);
  648. ASSERT(r == 0);
  649. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  650. ASSERT(r == 0);
  651. ASSERT(exit_cb_called == 1);
  652. ASSERT(close_cb_called == 1);
  653. MAKE_VALGRIND_HAPPY();
  654. return 0;
  655. }
  656. #endif
  657. #ifdef _WIN32
  658. static void exit_cb_unexpected(uv_process_t* process,
  659. int exit_status,
  660. int term_signal) {
  661. ASSERT(0 && "should not have been called");
  662. }
  663. TEST_IMPL(spawn_setuid_fails) {
  664. int r;
  665. init_process_options("spawn_helper1", exit_cb_unexpected);
  666. options.flags |= UV_PROCESS_SETUID;
  667. options.uid = (uv_uid_t) -42424242;
  668. r = uv_spawn(uv_default_loop(), &process, options);
  669. ASSERT(r == -1);
  670. ASSERT(uv_last_error(uv_default_loop()).code == UV_ENOTSUP);
  671. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  672. ASSERT(r == 0);
  673. ASSERT(close_cb_called == 0);
  674. MAKE_VALGRIND_HAPPY();
  675. return 0;
  676. }
  677. TEST_IMPL(spawn_setgid_fails) {
  678. int r;
  679. init_process_options("spawn_helper1", exit_cb_unexpected);
  680. options.flags |= UV_PROCESS_SETGID;
  681. options.gid = (uv_gid_t) -42424242;
  682. r = uv_spawn(uv_default_loop(), &process, options);
  683. ASSERT(r == -1);
  684. ASSERT(uv_last_error(uv_default_loop()).code == UV_ENOTSUP);
  685. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  686. ASSERT(r == 0);
  687. ASSERT(close_cb_called == 0);
  688. MAKE_VALGRIND_HAPPY();
  689. return 0;
  690. }
  691. #endif
  692. TEST_IMPL(spawn_auto_unref) {
  693. init_process_options("spawn_helper1", NULL);
  694. ASSERT(0 == uv_spawn(uv_default_loop(), &process, options));
  695. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  696. ASSERT(0 == uv_is_closing((uv_handle_t*) &process));
  697. uv_close((uv_handle_t*) &process, NULL);
  698. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  699. ASSERT(0 != uv_is_closing((uv_handle_t*) &process));
  700. MAKE_VALGRIND_HAPPY();
  701. return 0;
  702. }