uv: Upgrade to v0.11.17
[platform/upstream/nodejs.git] / deps / uv / test / test-spawn.c
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
22 #include "uv.h"
23 #include "task.h"
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #ifndef _WIN32
30 #include <unistd.h>
31 #endif
32
33
34 static int close_cb_called;
35 static int exit_cb_called;
36 static uv_process_t process;
37 static uv_timer_t timer;
38 static uv_process_options_t options;
39 static char exepath[1024];
40 static size_t exepath_size = 1024;
41 static char* args[3];
42 static int no_term_signal;
43
44 #define OUTPUT_SIZE 1024
45 static char output[OUTPUT_SIZE];
46 static int output_used;
47
48
49 static void close_cb(uv_handle_t* handle) {
50   printf("close_cb\n");
51   close_cb_called++;
52 }
53
54 static void exit_cb(uv_process_t* process,
55                     int64_t exit_status,
56                     int term_signal) {
57   printf("exit_cb\n");
58   exit_cb_called++;
59   ASSERT(exit_status == 1);
60   ASSERT(term_signal == 0);
61   uv_close((uv_handle_t*)process, close_cb);
62 }
63
64
65 static void fail_cb(uv_process_t* process,
66                     int64_t exit_status,
67                     int term_signal) {
68   ASSERT(0 && "fail_cb called");
69 }
70
71
72 static void kill_cb(uv_process_t* process,
73                     int64_t exit_status,
74                     int term_signal) {
75   int err;
76
77   printf("exit_cb\n");
78   exit_cb_called++;
79 #ifdef _WIN32
80   ASSERT(exit_status == 1);
81 #else
82   ASSERT(exit_status == 0);
83 #endif
84   ASSERT(no_term_signal || term_signal == 15);
85   uv_close((uv_handle_t*)process, close_cb);
86
87   /*
88    * Sending signum == 0 should check if the
89    * child process is still alive, not kill it.
90    * This process should be dead.
91    */
92   err = uv_kill(process->pid, 0);
93   ASSERT(err == UV_ESRCH);
94 }
95
96 static void detach_failure_cb(uv_process_t* process,
97                               int64_t exit_status,
98                               int term_signal) {
99   printf("detach_cb\n");
100   exit_cb_called++;
101 }
102
103 static void on_alloc(uv_handle_t* handle,
104                      size_t suggested_size,
105                      uv_buf_t* buf) {
106   buf->base = output + output_used;
107   buf->len = OUTPUT_SIZE - output_used;
108 }
109
110
111 static void on_read(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
112   if (nread > 0) {
113     output_used += nread;
114   } else if (nread < 0) {
115     ASSERT(nread == UV_EOF);
116     uv_close((uv_handle_t*)tcp, close_cb);
117   }
118 }
119
120
121 static void write_cb(uv_write_t* req, int status) {
122   ASSERT(status == 0);
123   uv_close((uv_handle_t*)req->handle, close_cb);
124 }
125
126
127 static void init_process_options(char* test, uv_exit_cb exit_cb) {
128   /* Note spawn_helper1 defined in test/run-tests.c */
129   int r = uv_exepath(exepath, &exepath_size);
130   ASSERT(r == 0);
131   exepath[exepath_size] = '\0';
132   args[0] = exepath;
133   args[1] = test;
134   args[2] = NULL;
135   options.file = exepath;
136   options.args = args;
137   options.exit_cb = exit_cb;
138   options.flags = 0;
139 }
140
141
142 static void timer_cb(uv_timer_t* handle, int status) {
143   uv_process_kill(&process, /* SIGTERM */ 15);
144   uv_close((uv_handle_t*)handle, close_cb);
145 }
146
147
148 TEST_IMPL(spawn_fails) {
149   int r;
150
151   init_process_options("", fail_cb);
152   options.file = options.args[0] = "program-that-had-better-not-exist";
153
154   r = uv_spawn(uv_default_loop(), &process, &options);
155   ASSERT(r == UV_ENOENT || r == UV_EACCES);
156   ASSERT(0 == uv_is_active((uv_handle_t*) &process));
157   ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
158
159   MAKE_VALGRIND_HAPPY();
160   return 0;
161 }
162
163
164 TEST_IMPL(spawn_exit_code) {
165   int r;
166
167   init_process_options("spawn_helper1", exit_cb);
168
169   r = uv_spawn(uv_default_loop(), &process, &options);
170   ASSERT(r == 0);
171
172   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
173   ASSERT(r == 0);
174
175   ASSERT(exit_cb_called == 1);
176   ASSERT(close_cb_called == 1);
177
178   MAKE_VALGRIND_HAPPY();
179   return 0;
180 }
181
182
183 TEST_IMPL(spawn_stdout) {
184   int r;
185   uv_pipe_t out;
186   uv_stdio_container_t stdio[2];
187
188   init_process_options("spawn_helper2", exit_cb);
189
190   uv_pipe_init(uv_default_loop(), &out, 0);
191   options.stdio = stdio;
192   options.stdio[0].flags = UV_IGNORE;
193   options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
194   options.stdio[1].data.stream = (uv_stream_t*)&out;
195   options.stdio_count = 2;
196
197   r = uv_spawn(uv_default_loop(), &process, &options);
198   ASSERT(r == 0);
199
200   r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
201   ASSERT(r == 0);
202
203   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
204   ASSERT(r == 0);
205
206   ASSERT(exit_cb_called == 1);
207   ASSERT(close_cb_called == 2); /* Once for process once for the pipe. */
208   printf("output is: %s", output);
209   ASSERT(strcmp("hello world\n", output) == 0);
210
211   MAKE_VALGRIND_HAPPY();
212   return 0;
213 }
214
215
216 TEST_IMPL(spawn_stdout_to_file) {
217   int r;
218   uv_file file;
219   uv_fs_t fs_req;
220   uv_stdio_container_t stdio[2];
221
222   /* Setup. */
223   unlink("stdout_file");
224
225   init_process_options("spawn_helper2", exit_cb);
226
227   r = uv_fs_open(uv_default_loop(), &fs_req, "stdout_file", O_CREAT | O_RDWR,
228       S_IRUSR | S_IWUSR, NULL);
229   ASSERT(r != -1);
230   uv_fs_req_cleanup(&fs_req);
231
232   file = r;
233
234   options.stdio = stdio;
235   options.stdio[0].flags = UV_IGNORE;
236   options.stdio[1].flags = UV_INHERIT_FD;
237   options.stdio[1].data.fd = file;
238   options.stdio_count = 2;
239
240   r = uv_spawn(uv_default_loop(), &process, &options);
241   ASSERT(r == 0);
242
243   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
244   ASSERT(r == 0);
245
246   ASSERT(exit_cb_called == 1);
247   ASSERT(close_cb_called == 1);
248
249   r = uv_fs_read(uv_default_loop(), &fs_req, file, output, sizeof(output),
250       0, NULL);
251   ASSERT(r == 12);
252   uv_fs_req_cleanup(&fs_req);
253
254   r = uv_fs_close(uv_default_loop(), &fs_req, file, NULL);
255   ASSERT(r == 0);
256   uv_fs_req_cleanup(&fs_req);
257
258   printf("output is: %s", output);
259   ASSERT(strcmp("hello world\n", output) == 0);
260
261   /* Cleanup. */
262   unlink("stdout_file");
263
264   MAKE_VALGRIND_HAPPY();
265   return 0;
266 }
267
268
269 TEST_IMPL(spawn_stdin) {
270   int r;
271   uv_pipe_t out;
272   uv_pipe_t in;
273   uv_write_t write_req;
274   uv_buf_t buf;
275   uv_stdio_container_t stdio[2];
276   char buffer[] = "hello-from-spawn_stdin";
277
278   init_process_options("spawn_helper3", exit_cb);
279
280   uv_pipe_init(uv_default_loop(), &out, 0);
281   uv_pipe_init(uv_default_loop(), &in, 0);
282   options.stdio = stdio;
283   options.stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
284   options.stdio[0].data.stream = (uv_stream_t*)&in;
285   options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
286   options.stdio[1].data.stream = (uv_stream_t*)&out;
287   options.stdio_count = 2;
288
289   r = uv_spawn(uv_default_loop(), &process, &options);
290   ASSERT(r == 0);
291
292   buf.base = buffer;
293   buf.len = sizeof(buffer);
294   r = uv_write(&write_req, (uv_stream_t*)&in, &buf, 1, write_cb);
295   ASSERT(r == 0);
296
297   r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
298   ASSERT(r == 0);
299
300   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
301   ASSERT(r == 0);
302
303   ASSERT(exit_cb_called == 1);
304   ASSERT(close_cb_called == 3); /* Once for process twice for the pipe. */
305   ASSERT(strcmp(buffer, output) == 0);
306
307   MAKE_VALGRIND_HAPPY();
308   return 0;
309 }
310
311
312 TEST_IMPL(spawn_stdio_greater_than_3) {
313   int r;
314   uv_pipe_t pipe;
315   uv_stdio_container_t stdio[4];
316
317   init_process_options("spawn_helper5", exit_cb);
318
319   uv_pipe_init(uv_default_loop(), &pipe, 0);
320   options.stdio = stdio;
321   options.stdio[0].flags = UV_IGNORE;
322   options.stdio[1].flags = UV_IGNORE;
323   options.stdio[2].flags = UV_IGNORE;
324   options.stdio[3].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
325   options.stdio[3].data.stream = (uv_stream_t*)&pipe;
326   options.stdio_count = 4;
327
328   r = uv_spawn(uv_default_loop(), &process, &options);
329   ASSERT(r == 0);
330
331   r = uv_read_start((uv_stream_t*) &pipe, on_alloc, on_read);
332   ASSERT(r == 0);
333
334   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
335   ASSERT(r == 0);
336
337   ASSERT(exit_cb_called == 1);
338   ASSERT(close_cb_called == 2); /* Once for process once for the pipe. */
339   printf("output from stdio[3] is: %s", output);
340   ASSERT(strcmp("fourth stdio!\n", output) == 0);
341
342   MAKE_VALGRIND_HAPPY();
343   return 0;
344 }
345
346
347 TEST_IMPL(spawn_ignored_stdio) {
348   int r;
349
350   init_process_options("spawn_helper6", exit_cb);
351
352   options.stdio = NULL;
353   options.stdio_count = 0;
354
355   r = uv_spawn(uv_default_loop(), &process, &options);
356   ASSERT(r == 0);
357
358   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
359   ASSERT(r == 0);
360
361   ASSERT(exit_cb_called == 1);
362   ASSERT(close_cb_called == 1);
363
364   MAKE_VALGRIND_HAPPY();
365   return 0;
366 }
367
368
369 TEST_IMPL(spawn_and_kill) {
370   int r;
371
372   init_process_options("spawn_helper4", kill_cb);
373
374   r = uv_spawn(uv_default_loop(), &process, &options);
375   ASSERT(r == 0);
376
377   r = uv_timer_init(uv_default_loop(), &timer);
378   ASSERT(r == 0);
379
380   r = uv_timer_start(&timer, timer_cb, 500, 0);
381   ASSERT(r == 0);
382
383   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
384   ASSERT(r == 0);
385
386   ASSERT(exit_cb_called == 1);
387   ASSERT(close_cb_called == 2); /* Once for process and once for timer. */
388
389   MAKE_VALGRIND_HAPPY();
390   return 0;
391 }
392
393
394 TEST_IMPL(spawn_preserve_env) {
395   int r;
396   uv_pipe_t out;
397   uv_stdio_container_t stdio[2];
398
399   init_process_options("spawn_helper7", exit_cb);
400
401   uv_pipe_init(uv_default_loop(), &out, 0);
402   options.stdio = stdio;
403   options.stdio[0].flags = UV_IGNORE;
404   options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
405   options.stdio[1].data.stream = (uv_stream_t*) &out;
406   options.stdio_count = 2;
407
408   r = putenv("ENV_TEST=testval");
409   ASSERT(r == 0);
410
411   /* Explicitly set options.env to NULL to test for env clobbering. */
412   options.env = NULL;
413
414   r = uv_spawn(uv_default_loop(), &process, &options);
415   ASSERT(r == 0);
416
417   r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
418   ASSERT(r == 0);
419
420   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
421   ASSERT(r == 0);
422
423   ASSERT(exit_cb_called == 1);
424   ASSERT(close_cb_called == 2);
425
426   printf("output is: %s", output);
427   ASSERT(strcmp("testval", output) == 0);
428
429   MAKE_VALGRIND_HAPPY();
430   return 0;
431 }
432
433
434 TEST_IMPL(spawn_detached) {
435   int r;
436
437   init_process_options("spawn_helper4", detach_failure_cb);
438
439   options.flags |= UV_PROCESS_DETACHED;
440
441   r = uv_spawn(uv_default_loop(), &process, &options);
442   ASSERT(r == 0);
443
444   uv_unref((uv_handle_t*)&process);
445
446   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
447   ASSERT(r == 0);
448
449   ASSERT(exit_cb_called == 0);
450
451   r = uv_kill(process.pid, 0);
452   ASSERT(r == 0);
453
454   r = uv_kill(process.pid, 15);
455   ASSERT(r == 0);
456
457   MAKE_VALGRIND_HAPPY();
458   return 0;
459 }
460
461 TEST_IMPL(spawn_and_kill_with_std) {
462   int r;
463   uv_pipe_t in, out, err;
464   uv_write_t write;
465   char message[] = "Nancy's joining me because the message this evening is "
466                    "not my message but ours.";
467   uv_buf_t buf;
468   uv_stdio_container_t stdio[3];
469
470   init_process_options("spawn_helper4", kill_cb);
471
472   r = uv_pipe_init(uv_default_loop(), &in, 0);
473   ASSERT(r == 0);
474
475   r = uv_pipe_init(uv_default_loop(), &out, 0);
476   ASSERT(r == 0);
477
478   r = uv_pipe_init(uv_default_loop(), &err, 0);
479   ASSERT(r == 0);
480
481   options.stdio = stdio;
482   options.stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
483   options.stdio[0].data.stream = (uv_stream_t*)&in;
484   options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
485   options.stdio[1].data.stream = (uv_stream_t*)&out;
486   options.stdio[2].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
487   options.stdio[2].data.stream = (uv_stream_t*)&err;
488   options.stdio_count = 3;
489
490   r = uv_spawn(uv_default_loop(), &process, &options);
491   ASSERT(r == 0);
492
493   buf = uv_buf_init(message, sizeof message);
494   r = uv_write(&write, (uv_stream_t*) &in, &buf, 1, write_cb);
495   ASSERT(r == 0);
496
497   r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
498   ASSERT(r == 0);
499
500   r = uv_read_start((uv_stream_t*) &err, on_alloc, on_read);
501   ASSERT(r == 0);
502
503   r = uv_timer_init(uv_default_loop(), &timer);
504   ASSERT(r == 0);
505
506   r = uv_timer_start(&timer, timer_cb, 500, 0);
507   ASSERT(r == 0);
508
509   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
510   ASSERT(r == 0);
511
512   ASSERT(exit_cb_called == 1);
513   ASSERT(close_cb_called == 5); /* process x 1, timer x 1, stdio x 3. */
514
515   MAKE_VALGRIND_HAPPY();
516   return 0;
517 }
518
519
520 TEST_IMPL(spawn_and_ping) {
521   uv_write_t write_req;
522   uv_pipe_t in, out;
523   uv_buf_t buf;
524   uv_stdio_container_t stdio[2];
525   int r;
526
527   init_process_options("spawn_helper3", exit_cb);
528   buf = uv_buf_init("TEST", 4);
529
530   uv_pipe_init(uv_default_loop(), &out, 0);
531   uv_pipe_init(uv_default_loop(), &in, 0);
532   options.stdio = stdio;
533   options.stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
534   options.stdio[0].data.stream = (uv_stream_t*)&in;
535   options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
536   options.stdio[1].data.stream = (uv_stream_t*)&out;
537   options.stdio_count = 2;
538
539   r = uv_spawn(uv_default_loop(), &process, &options);
540   ASSERT(r == 0);
541
542   /* Sending signum == 0 should check if the
543    * child process is still alive, not kill it.
544    */
545   r = uv_process_kill(&process, 0);
546   ASSERT(r == 0);
547
548   r = uv_write(&write_req, (uv_stream_t*)&in, &buf, 1, write_cb);
549   ASSERT(r == 0);
550
551   r = uv_read_start((uv_stream_t*)&out, on_alloc, on_read);
552   ASSERT(r == 0);
553
554   ASSERT(exit_cb_called == 0);
555
556   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
557   ASSERT(r == 0);
558
559   ASSERT(exit_cb_called == 1);
560   ASSERT(strcmp(output, "TEST") == 0);
561
562   MAKE_VALGRIND_HAPPY();
563   return 0;
564 }
565
566
567 TEST_IMPL(kill) {
568   int r;
569
570 #ifdef _WIN32
571   no_term_signal = 1;
572 #endif
573
574   init_process_options("spawn_helper4", kill_cb);
575
576   r = uv_spawn(uv_default_loop(), &process, &options);
577   ASSERT(r == 0);
578
579   /* Sending signum == 0 should check if the
580    * child process is still alive, not kill it.
581    */
582   r = uv_kill(process.pid, 0);
583   ASSERT(r == 0);
584
585   /* Kill the process. */
586   r = uv_kill(process.pid, /* SIGTERM */ 15);
587   ASSERT(r == 0);
588
589   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
590   ASSERT(r == 0);
591
592   ASSERT(exit_cb_called == 1);
593   ASSERT(close_cb_called == 1);
594
595   MAKE_VALGRIND_HAPPY();
596   return 0;
597 }
598
599
600 #ifdef _WIN32
601 TEST_IMPL(spawn_detect_pipe_name_collisions_on_windows) {
602   int r;
603   uv_pipe_t out;
604   char name[64];
605   HANDLE pipe_handle;
606   uv_stdio_container_t stdio[2];
607
608   init_process_options("spawn_helper2", exit_cb);
609
610   uv_pipe_init(uv_default_loop(), &out, 0);
611   options.stdio = stdio;
612   options.stdio[0].flags = UV_IGNORE;
613   options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
614   options.stdio[1].data.stream = (uv_stream_t*)&out;
615   options.stdio_count = 2;
616
617   /* Create a pipe that'll cause a collision. */
618   _snprintf(name,
619             sizeof(name),
620             "\\\\.\\pipe\\uv\\%p-%d",
621             &out,
622             GetCurrentProcessId());
623   pipe_handle = CreateNamedPipeA(name,
624                                 PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,
625                                 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
626                                 10,
627                                 65536,
628                                 65536,
629                                 0,
630                                 NULL);
631   ASSERT(pipe_handle != INVALID_HANDLE_VALUE);
632
633   r = uv_spawn(uv_default_loop(), &process, &options);
634   ASSERT(r == 0);
635
636   r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
637   ASSERT(r == 0);
638
639   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
640   ASSERT(r == 0);
641
642   ASSERT(exit_cb_called == 1);
643   ASSERT(close_cb_called == 2); /* Once for process once for the pipe. */
644   printf("output is: %s", output);
645   ASSERT(strcmp("hello world\n", output) == 0);
646
647   MAKE_VALGRIND_HAPPY();
648   return 0;
649 }
650
651
652 int make_program_args(char** args, int verbatim_arguments, WCHAR** dst_ptr);
653 WCHAR* quote_cmd_arg(const WCHAR *source, WCHAR *target);
654
655 TEST_IMPL(argument_escaping) {
656   const WCHAR* test_str[] = {
657     L"HelloWorld",
658     L"Hello World",
659     L"Hello\"World",
660     L"Hello World\\",
661     L"Hello\\\"World",
662     L"Hello\\World",
663     L"Hello\\\\World",
664     L"Hello World\\",
665     L"c:\\path\\to\\node.exe --eval \"require('c:\\\\path\\\\to\\\\test.js')\""
666   };
667   const int count = sizeof(test_str) / sizeof(*test_str);
668   WCHAR** test_output;
669   WCHAR* command_line;
670   WCHAR** cracked;
671   size_t total_size = 0;
672   int i;
673   int num_args;
674   int result;
675
676   char* verbatim[] = {
677     "cmd.exe",
678     "/c",
679     "c:\\path\\to\\node.exe --eval \"require('c:\\\\path\\\\to\\\\test.js')\"",
680     NULL
681   };
682   WCHAR* verbatim_output;
683   WCHAR* non_verbatim_output;
684
685   test_output = calloc(count, sizeof(WCHAR*));
686   for (i = 0; i < count; ++i) {
687     test_output[i] = calloc(2 * (wcslen(test_str[i]) + 2), sizeof(WCHAR));
688     quote_cmd_arg(test_str[i], test_output[i]);
689     wprintf(L"input : %s\n", test_str[i]);
690     wprintf(L"output: %s\n", test_output[i]);
691     total_size += wcslen(test_output[i]) + 1;
692   }
693   command_line = calloc(total_size + 1, sizeof(WCHAR));
694   for (i = 0; i < count; ++i) {
695     wcscat(command_line, test_output[i]);
696     wcscat(command_line, L" ");
697   }
698   command_line[total_size - 1] = L'\0';
699
700   wprintf(L"command_line: %s\n", command_line);
701
702   cracked = CommandLineToArgvW(command_line, &num_args);
703   for (i = 0; i < num_args; ++i) {
704     wprintf(L"%d: %s\t%s\n", i, test_str[i], cracked[i]);
705     ASSERT(wcscmp(test_str[i], cracked[i]) == 0);
706   }
707
708   LocalFree(cracked);
709   for (i = 0; i < count; ++i) {
710     free(test_output[i]);
711   }
712
713   result = make_program_args(verbatim, 1, &verbatim_output);
714   ASSERT(result == 0);
715   result = make_program_args(verbatim, 0, &non_verbatim_output);
716   ASSERT(result == 0);
717
718   wprintf(L"    verbatim_output: %s\n", verbatim_output);
719   wprintf(L"non_verbatim_output: %s\n", non_verbatim_output);
720
721   ASSERT(wcscmp(verbatim_output,
722                 L"cmd.exe /c c:\\path\\to\\node.exe --eval "
723                 L"\"require('c:\\\\path\\\\to\\\\test.js')\"") == 0);
724   ASSERT(wcscmp(non_verbatim_output,
725                 L"cmd.exe /c \"c:\\path\\to\\node.exe --eval "
726                 L"\\\"require('c:\\\\path\\\\to\\\\test.js')\\\"\"") == 0);
727
728   free(verbatim_output);
729   free(non_verbatim_output);
730
731   return 0;
732 }
733
734 int make_program_env(char** env_block, WCHAR** dst_ptr);
735
736 TEST_IMPL(environment_creation) {
737   int i;
738   char* environment[] = {
739     "FOO=BAR",
740     "SYSTEM=ROOT", /* substring of a supplied var name */
741     "SYSTEMROOTED=OMG", /* supplied var name is a substring */
742     "TEMP=C:\\Temp",
743     "BAZ=QUX",
744     NULL
745   };
746
747   WCHAR expected[512];
748   WCHAR* ptr = expected;
749   int result;
750   WCHAR* str;
751   WCHAR* env;
752
753   for (i = 0; i < sizeof(environment) / sizeof(environment[0]) - 1; i++) {
754     ptr += uv_utf8_to_utf16(environment[i],
755                             ptr,
756                             expected + sizeof(expected) - ptr);
757   }
758
759   memcpy(ptr, L"SYSTEMROOT=", sizeof(L"SYSTEMROOT="));
760   ptr += sizeof(L"SYSTEMROOT=")/sizeof(WCHAR) - 1;
761   ptr += GetEnvironmentVariableW(L"SYSTEMROOT",
762                                  ptr,
763                                  expected + sizeof(expected) - ptr);
764   ++ptr;
765
766   memcpy(ptr, L"SYSTEMDRIVE=", sizeof(L"SYSTEMDRIVE="));
767   ptr += sizeof(L"SYSTEMDRIVE=")/sizeof(WCHAR) - 1;
768   ptr += GetEnvironmentVariableW(L"SYSTEMDRIVE",
769                                  ptr,
770                                  expected + sizeof(expected) - ptr);
771   ++ptr;
772   *ptr = '\0';
773
774   result = make_program_env(environment, &env);
775   ASSERT(result == 0);
776
777   for (str = env; *str; str += wcslen(str) + 1) {
778     wprintf(L"%s\n", str);
779   }
780
781   ASSERT(wcscmp(expected, env) == 0);
782
783   return 0;
784 }
785 #endif
786
787 #ifndef _WIN32
788 TEST_IMPL(spawn_setuid_setgid) {
789   int r;
790
791   /* if not root, then this will fail. */
792   uv_uid_t uid = getuid();
793   if (uid != 0) {
794     fprintf(stderr, "spawn_setuid_setgid skipped: not root\n");
795     return 0;
796   }
797
798   init_process_options("spawn_helper1", exit_cb);
799
800   /* become the "nobody" user. */
801   struct passwd* pw;
802   pw = getpwnam("nobody");
803   ASSERT(pw != NULL);
804   options.uid = pw->pw_uid;
805   options.gid = pw->pw_gid;
806   options.flags = UV_PROCESS_SETUID | UV_PROCESS_SETGID;
807
808   r = uv_spawn(uv_default_loop(), &process, &options);
809   ASSERT(r == 0);
810
811   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
812   ASSERT(r == 0);
813
814   ASSERT(exit_cb_called == 1);
815   ASSERT(close_cb_called == 1);
816
817   MAKE_VALGRIND_HAPPY();
818   return 0;
819 }
820 #endif
821
822
823 #ifndef _WIN32
824 TEST_IMPL(spawn_setuid_fails) {
825   int r;
826
827   /* if root, become nobody. */
828   uv_uid_t uid = getuid();
829   if (uid == 0) {
830     struct passwd* pw;
831     pw = getpwnam("nobody");
832     ASSERT(pw != NULL);
833     ASSERT(0 == setgid(pw->pw_gid));
834     ASSERT(0 == setuid(pw->pw_uid));
835   }
836
837   init_process_options("spawn_helper1", fail_cb);
838
839   options.flags |= UV_PROCESS_SETUID;
840   options.uid = 0;
841
842   r = uv_spawn(uv_default_loop(), &process, &options);
843   ASSERT(r == UV_EPERM);
844
845   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
846   ASSERT(r == 0);
847
848   ASSERT(close_cb_called == 0);
849
850   MAKE_VALGRIND_HAPPY();
851   return 0;
852 }
853
854
855 TEST_IMPL(spawn_setgid_fails) {
856   int r;
857
858   /* if root, become nobody. */
859   uv_uid_t uid = getuid();
860   if (uid == 0) {
861     struct passwd* pw;
862     pw = getpwnam("nobody");
863     ASSERT(pw != NULL);
864     ASSERT(0 == setgid(pw->pw_gid));
865     ASSERT(0 == setuid(pw->pw_uid));
866   }
867
868   init_process_options("spawn_helper1", fail_cb);
869
870   options.flags |= UV_PROCESS_SETGID;
871   options.gid = 0;
872
873   r = uv_spawn(uv_default_loop(), &process, &options);
874   ASSERT(r == UV_EPERM);
875
876   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
877   ASSERT(r == 0);
878
879   ASSERT(close_cb_called == 0);
880
881   MAKE_VALGRIND_HAPPY();
882   return 0;
883 }
884 #endif
885
886
887 #ifdef _WIN32
888
889 static void exit_cb_unexpected(uv_process_t* process,
890                                int64_t exit_status,
891                                int term_signal) {
892   ASSERT(0 && "should not have been called");
893 }
894
895
896 TEST_IMPL(spawn_setuid_fails) {
897   int r;
898
899   init_process_options("spawn_helper1", exit_cb_unexpected);
900
901   options.flags |= UV_PROCESS_SETUID;
902   options.uid = (uv_uid_t) -42424242;
903
904   r = uv_spawn(uv_default_loop(), &process, &options);
905   ASSERT(r == UV_ENOTSUP);
906
907   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
908   ASSERT(r == 0);
909
910   ASSERT(close_cb_called == 0);
911
912   MAKE_VALGRIND_HAPPY();
913   return 0;
914 }
915
916
917 TEST_IMPL(spawn_setgid_fails) {
918   int r;
919
920   init_process_options("spawn_helper1", exit_cb_unexpected);
921
922   options.flags |= UV_PROCESS_SETGID;
923   options.gid = (uv_gid_t) -42424242;
924
925   r = uv_spawn(uv_default_loop(), &process, &options);
926   ASSERT(r == UV_ENOTSUP);
927
928   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
929   ASSERT(r == 0);
930
931   ASSERT(close_cb_called == 0);
932
933   MAKE_VALGRIND_HAPPY();
934   return 0;
935 }
936 #endif
937
938
939 TEST_IMPL(spawn_auto_unref) {
940   init_process_options("spawn_helper1", NULL);
941   ASSERT(0 == uv_spawn(uv_default_loop(), &process, &options));
942   ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
943   ASSERT(0 == uv_is_closing((uv_handle_t*) &process));
944   uv_close((uv_handle_t*) &process, NULL);
945   ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
946   ASSERT(1 == uv_is_closing((uv_handle_t*) &process));
947   MAKE_VALGRIND_HAPPY();
948   return 0;
949 }