7 #include <gio/gunixinputstream.h>
8 #include <gio/gfiledescriptorbased.h>
12 #define LINEEND "\r\n"
20 get_test_subprocess_args (const char *mode,
21 ...) G_GNUC_NULL_TERMINATED;
24 get_test_subprocess_args (const char *mode,
33 ret = g_ptr_array_new_with_free_func (g_free);
36 binname = "gsubprocess-testprog.exe";
38 binname = "gsubprocess-testprog";
41 path = g_test_build_filename (G_TEST_BUILT, binname, NULL);
42 g_ptr_array_add (ret, path);
43 g_ptr_array_add (ret, g_strdup (mode));
45 va_start (args, mode);
46 while ((arg = va_arg (args, gpointer)) != NULL)
47 g_ptr_array_add (ret, g_strdup (arg));
50 g_ptr_array_add (ret, NULL);
57 GError *local_error = NULL;
58 GError **error = &local_error;
63 args = get_test_subprocess_args ("noop", NULL);
64 proc = g_subprocess_newv ((const gchar * const *) args->pdata, G_SUBPROCESS_FLAGS_NONE, error);
65 g_ptr_array_free (args, TRUE);
66 g_assert_no_error (local_error);
67 id = g_subprocess_get_identifier (proc);
68 g_assert (id != NULL);
70 g_subprocess_wait_check (proc, NULL, error);
71 g_assert_no_error (local_error);
72 g_assert (g_subprocess_get_successful (proc));
74 g_object_unref (proc);
78 check_ready (GObject *source,
85 ret = g_subprocess_wait_check_finish (G_SUBPROCESS (source),
89 g_assert_no_error (error);
91 g_object_unref (source);
95 test_noop_all_to_null (void)
97 GError *local_error = NULL;
98 GError **error = &local_error;
102 args = get_test_subprocess_args ("noop", NULL);
103 proc = g_subprocess_newv ((const gchar * const *) args->pdata,
104 G_SUBPROCESS_FLAGS_STDOUT_SILENCE | G_SUBPROCESS_FLAGS_STDERR_SILENCE,
106 g_ptr_array_free (args, TRUE);
107 g_assert_no_error (local_error);
109 g_subprocess_wait_check_async (proc, NULL, check_ready, NULL);
113 test_noop_no_wait (void)
115 GError *local_error = NULL;
116 GError **error = &local_error;
120 args = get_test_subprocess_args ("noop", NULL);
121 proc = g_subprocess_newv ((const gchar * const *) args->pdata, G_SUBPROCESS_FLAGS_NONE, error);
122 g_ptr_array_free (args, TRUE);
123 g_assert_no_error (local_error);
125 g_object_unref (proc);
129 test_noop_stdin_inherit (void)
131 GError *local_error = NULL;
132 GError **error = &local_error;
136 args = get_test_subprocess_args ("noop", NULL);
137 proc = g_subprocess_newv ((const gchar * const *) args->pdata, G_SUBPROCESS_FLAGS_STDIN_INHERIT, error);
138 g_ptr_array_free (args, TRUE);
139 g_assert_no_error (local_error);
141 g_subprocess_wait_check (proc, NULL, error);
142 g_assert_no_error (local_error);
144 g_object_unref (proc);
149 test_search_path (void)
151 GError *local_error = NULL;
152 GError **error = &local_error;
155 proc = g_subprocess_new (G_SUBPROCESS_FLAGS_NONE, error, "true", NULL);
156 g_assert_no_error (local_error);
158 g_subprocess_wait_check (proc, NULL, error);
159 g_assert_no_error (local_error);
161 g_object_unref (proc);
168 GError *local_error = NULL;
169 GError **error = &local_error;
173 args = get_test_subprocess_args ("exit1", NULL);
174 proc = g_subprocess_newv ((const gchar * const *) args->pdata, G_SUBPROCESS_FLAGS_NONE, error);
175 g_ptr_array_free (args, TRUE);
176 g_assert_no_error (local_error);
178 g_subprocess_wait_check (proc, NULL, error);
179 g_assert_error (local_error, G_SPAWN_EXIT_ERROR, 1);
180 g_clear_error (error);
182 g_object_unref (proc);
186 splice_to_string (GInputStream *stream,
189 GMemoryOutputStream *buffer = NULL;
192 buffer = (GMemoryOutputStream*)g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
193 if (g_output_stream_splice ((GOutputStream*)buffer, stream, 0, NULL, error) < 0)
196 if (!g_output_stream_write ((GOutputStream*)buffer, "\0", 1, NULL, error))
199 if (!g_output_stream_close ((GOutputStream*)buffer, NULL, error))
202 ret = g_memory_output_stream_steal_data (buffer);
204 g_clear_object (&buffer);
211 GError *local_error = NULL;
212 GError **error = &local_error;
215 GInputStream *stdout;
218 args = get_test_subprocess_args ("echo", "hello", "world!", NULL);
219 proc = g_subprocess_newv ((const gchar * const *) args->pdata, G_SUBPROCESS_FLAGS_STDOUT_PIPE, error);
220 g_ptr_array_free (args, TRUE);
221 g_assert_no_error (local_error);
223 stdout = g_subprocess_get_stdout_pipe (proc);
225 result = splice_to_string (stdout, error);
226 g_assert_no_error (local_error);
228 g_assert_cmpstr (result, ==, "hello" LINEEND "world!" LINEEND);
231 g_object_unref (proc);
236 test_echo_merged (void)
238 GError *local_error = NULL;
239 GError **error = &local_error;
242 GInputStream *stdout;
245 args = get_test_subprocess_args ("echo-stdout-and-stderr", "merge", "this", NULL);
246 proc = g_subprocess_newv ((const gchar * const *) args->pdata,
247 G_SUBPROCESS_FLAGS_STDOUT_PIPE | G_SUBPROCESS_FLAGS_STDERR_MERGE,
249 g_ptr_array_free (args, TRUE);
250 g_assert_no_error (local_error);
252 stdout = g_subprocess_get_stdout_pipe (proc);
253 result = splice_to_string (stdout, error);
254 g_assert_no_error (local_error);
256 g_assert_cmpstr (result, ==, "merge\nmerge\nthis\nthis\n");
259 g_object_unref (proc);
264 guint events_pending;
269 test_cat_on_input_splice_complete (GObject *object,
270 GAsyncResult *result,
273 TestCatData *data = user_data;
274 GError *error = NULL;
276 (void)g_output_stream_splice_finish ((GOutputStream*)object, result, &error);
277 g_assert_no_error (error);
279 data->events_pending--;
280 if (data->events_pending == 0)
281 g_main_loop_quit (data->loop);
287 GError *local_error = NULL;
288 GError **error = &local_error;
293 GInputStream *input_buf_stream = NULL;
294 GOutputStream *output_buf_stream = NULL;
295 GOutputStream *stdin_stream = NULL;
296 GInputStream *stdout_stream = NULL;
299 memset (&data, 0, sizeof (data));
300 data.loop = g_main_loop_new (NULL, TRUE);
302 args = get_test_subprocess_args ("cat", NULL);
303 proc = g_subprocess_newv ((const gchar * const *) args->pdata,
304 G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE,
306 g_ptr_array_free (args, TRUE);
307 g_assert_no_error (local_error);
309 stdin_stream = g_subprocess_get_stdin_pipe (proc);
310 stdout_stream = g_subprocess_get_stdout_pipe (proc);
312 input_buf = g_bytes_new_static ("hello, world!", strlen ("hello, world!"));
313 input_buf_stream = g_memory_input_stream_new_from_bytes (input_buf);
314 g_bytes_unref (input_buf);
316 output_buf_stream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
318 g_output_stream_splice_async (stdin_stream, input_buf_stream, G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
319 G_PRIORITY_DEFAULT, NULL, test_cat_on_input_splice_complete,
321 data.events_pending++;
322 g_output_stream_splice_async (output_buf_stream, stdout_stream, G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
323 G_PRIORITY_DEFAULT, NULL, test_cat_on_input_splice_complete,
325 data.events_pending++;
327 g_main_loop_run (data.loop);
329 g_subprocess_wait_check (proc, NULL, error);
330 g_assert_no_error (local_error);
332 output_buf = g_memory_output_stream_steal_as_bytes ((GMemoryOutputStream*)output_buf_stream);
334 g_assert_cmpint (g_bytes_get_size (output_buf), ==, 13);
335 g_assert_cmpint (memcmp (g_bytes_get_data (output_buf, NULL), "hello, world!", 13), ==, 0);
337 g_bytes_unref (output_buf);
338 g_main_loop_unref (data.loop);
339 g_object_unref (input_buf_stream);
340 g_object_unref (output_buf_stream);
341 g_object_unref (proc);
345 cancel_soon (gpointer user_data)
347 GCancellable *cancellable = user_data;
349 g_usleep (G_TIME_SPAN_SECOND);
350 g_cancellable_cancel (cancellable);
351 g_object_unref (cancellable);
359 GCancellable *cancellable;
360 GError *error = NULL;
367 g_test_skip ("This test has not been ported to Win32");
372 cat = g_subprocess_new (G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE, &error, "cat", NULL);
373 g_assert_no_error (error);
376 /* Make sure that reading stdout blocks (until we cancel) */
377 cancellable = g_cancellable_new ();
378 g_thread_unref (g_thread_new ("cancel thread", cancel_soon, g_object_ref (cancellable)));
379 s = g_input_stream_read (g_subprocess_get_stdout_pipe (cat), &buffer, sizeof buffer, cancellable, &error);
380 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
381 g_assert_cmpint (s, ==, -1);
382 g_object_unref (cancellable);
383 g_clear_error (&error);
385 /* Close the stream (EOF on cat's stdin) */
386 result = g_output_stream_close (g_subprocess_get_stdin_pipe (cat), NULL, &error);
387 g_assert_no_error (error);
390 /* Now check that reading cat's stdout gets us an EOF (since it quit) */
391 s = g_input_stream_read (g_subprocess_get_stdout_pipe (cat), &buffer, sizeof buffer, NULL, &error);
392 g_assert_no_error (error);
395 /* Check that the process has exited as a result of the EOF */
396 result = g_subprocess_wait (cat, NULL, &error);
397 g_assert_no_error (error);
398 g_assert (g_subprocess_get_if_exited (cat));
399 g_assert_cmpint (g_subprocess_get_exit_status (cat), ==, 0);
402 g_object_unref (cat);
406 guint events_pending;
407 gboolean caught_error;
412 GOutputStream *first_stdin;
413 } TestMultiSpliceData;
416 on_one_multi_splice_done (GObject *obj,
420 TestMultiSpliceData *data = user_data;
422 if (!data->caught_error)
424 if (g_output_stream_splice_finish ((GOutputStream*)obj, res, &data->error) < 0)
425 data->caught_error = TRUE;
428 data->events_pending--;
429 if (data->events_pending == 0)
430 g_main_loop_quit (data->loop);
434 on_idle_multisplice (gpointer user_data)
436 TestMultiSpliceData *data = user_data;
438 /* We write 2^1 + 2^2 ... + 2^10 or 2047 copies of "Hello World!\n"
441 if (data->counter >= 2047 || data->caught_error)
443 if (!g_output_stream_close (data->first_stdin, NULL, &data->error))
444 data->caught_error = TRUE;
445 data->events_pending--;
446 if (data->events_pending == 0)
448 g_main_loop_quit (data->loop);
455 for (i = 0; i < data->counter; i++)
458 if (!g_output_stream_write_all (data->first_stdin, "hello world!\n",
459 strlen ("hello world!\n"), &bytes_written,
462 data->caught_error = TRUE;
472 on_subprocess_exited (GObject *object,
473 GAsyncResult *result,
476 GSubprocess *subprocess = G_SUBPROCESS (object);
477 TestMultiSpliceData *data = user_data;
478 GError *error = NULL;
480 if (!g_subprocess_wait_finish (subprocess, result, &error))
482 if (!data->caught_error)
484 data->caught_error = TRUE;
485 g_propagate_error (&data->error, error);
488 g_spawn_check_exit_status (g_subprocess_get_exit_status (subprocess), &error);
489 g_assert_no_error (error);
490 data->events_pending--;
491 if (data->events_pending == 0)
492 g_main_loop_quit (data->loop);
498 GError *local_error = NULL;
499 GError **error = &local_error;
501 GSubprocessLauncher *launcher;
505 GOutputStream *first_stdin;
506 GInputStream *first_stdout;
507 GOutputStream *second_stdin;
508 GInputStream *second_stdout;
509 GOutputStream *third_stdin;
510 GInputStream *third_stdout;
511 GOutputStream *membuf;
512 TestMultiSpliceData data;
513 int splice_flags = G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET;
515 args = get_test_subprocess_args ("cat", NULL);
516 launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE);
517 first = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, error);
518 g_assert_no_error (local_error);
519 second = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, error);
520 g_assert_no_error (local_error);
521 third = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, error);
522 g_assert_no_error (local_error);
524 g_ptr_array_free (args, TRUE);
526 membuf = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
528 first_stdin = g_subprocess_get_stdin_pipe (first);
529 first_stdout = g_subprocess_get_stdout_pipe (first);
530 second_stdin = g_subprocess_get_stdin_pipe (second);
531 second_stdout = g_subprocess_get_stdout_pipe (second);
532 third_stdin = g_subprocess_get_stdin_pipe (third);
533 third_stdout = g_subprocess_get_stdout_pipe (third);
535 memset (&data, 0, sizeof (data));
536 data.loop = g_main_loop_new (NULL, TRUE);
538 data.first_stdin = first_stdin;
540 data.events_pending++;
541 g_output_stream_splice_async (second_stdin, first_stdout, splice_flags, G_PRIORITY_DEFAULT,
542 NULL, on_one_multi_splice_done, &data);
543 data.events_pending++;
544 g_output_stream_splice_async (third_stdin, second_stdout, splice_flags, G_PRIORITY_DEFAULT,
545 NULL, on_one_multi_splice_done, &data);
546 data.events_pending++;
547 g_output_stream_splice_async (membuf, third_stdout, splice_flags, G_PRIORITY_DEFAULT,
548 NULL, on_one_multi_splice_done, &data);
550 data.events_pending++;
551 g_timeout_add (250, on_idle_multisplice, &data);
553 data.events_pending++;
554 g_subprocess_wait_async (first, NULL, on_subprocess_exited, &data);
555 data.events_pending++;
556 g_subprocess_wait_async (second, NULL, on_subprocess_exited, &data);
557 data.events_pending++;
558 g_subprocess_wait_async (third, NULL, on_subprocess_exited, &data);
560 g_main_loop_run (data.loop);
562 g_assert (!data.caught_error);
563 g_assert_no_error (data.error);
565 g_assert_cmpint (g_memory_output_stream_get_data_size ((GMemoryOutputStream*)membuf), ==, 26611);
567 g_main_loop_unref (data.loop);
568 g_object_unref (membuf);
569 g_object_unref (launcher);
570 g_object_unref (first);
571 g_object_unref (second);
572 g_object_unref (third);
579 } TestAsyncCommunicateData;
582 on_communicate_complete (GObject *proc,
583 GAsyncResult *result,
586 TestAsyncCommunicateData *data = user_data;
587 GBytes *stdout = NULL;
588 char *stdout_str = NULL;
589 const guint8 *stdout_data;
592 data->running = FALSE;
594 (void) g_subprocess_communicate_utf8_finish ((GSubprocess*)proc, result,
595 &stdout_str, NULL, &data->error);
597 (void) g_subprocess_communicate_finish ((GSubprocess*)proc, result,
598 &stdout, NULL, &data->error);
604 g_assert (stdout != NULL);
605 stdout_data = g_bytes_get_data (stdout, &stdout_len);
609 g_assert (stdout_str != NULL);
610 stdout_data = (guint8*)stdout_str;
611 stdout_len = strlen (stdout_str);
614 g_assert_cmpint (stdout_len, ==, 11);
615 g_assert (memcmp (stdout_data, "hello world", 11) == 0);
617 g_bytes_unref (stdout);
622 test_communicate_async (void)
624 GError *error = NULL;
626 TestAsyncCommunicateData data = { 0, };
628 GCancellable *cancellable = NULL;
630 const char *hellostring;
632 args = get_test_subprocess_args ("cat", NULL);
633 proc = g_subprocess_newv ((const gchar* const*)args->pdata,
634 G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE,
636 g_assert_no_error (error);
637 g_ptr_array_free (args, TRUE);
639 hellostring = "hello world";
640 input = g_bytes_new_static (hellostring, strlen (hellostring));
642 g_subprocess_communicate_async (proc, input,
644 on_communicate_complete,
649 g_main_context_iteration (NULL, TRUE);
651 g_assert_no_error (data.error);
653 g_bytes_unref (input);
654 g_object_unref (proc);
658 test_communicate (void)
660 GError *error = NULL;
663 GCancellable *cancellable = NULL;
665 const gchar *hellostring;
667 const gchar *stdout_data;
670 args = get_test_subprocess_args ("cat", NULL);
671 proc = g_subprocess_newv ((const gchar* const*)args->pdata,
672 G_SUBPROCESS_FLAGS_STDIN_PIPE
673 | G_SUBPROCESS_FLAGS_STDOUT_PIPE
674 | G_SUBPROCESS_FLAGS_STDERR_MERGE,
676 g_assert_no_error (error);
677 g_ptr_array_free (args, TRUE);
679 hellostring = "hello world";
680 input = g_bytes_new_static (hellostring, strlen (hellostring));
682 g_subprocess_communicate (proc, input, cancellable, &stdout, NULL, &error);
683 g_assert_no_error (error);
684 stdout_data = g_bytes_get_data (stdout, &stdout_len);
686 g_assert_cmpint (stdout_len, ==, 11);
687 g_assert (memcmp (stdout_data, "hello world", 11) == 0);
688 g_bytes_unref (stdout);
690 g_bytes_unref (input);
691 g_bytes_unref (stdout);
692 g_object_unref (proc);
696 test_communicate_utf8_async (void)
698 GError *error = NULL;
700 TestAsyncCommunicateData data = { 0, };
702 GCancellable *cancellable = NULL;
704 args = get_test_subprocess_args ("cat", NULL);
705 proc = g_subprocess_newv ((const gchar* const*)args->pdata,
706 G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE,
708 g_assert_no_error (error);
709 g_ptr_array_free (args, TRUE);
712 g_subprocess_communicate_utf8_async (proc, "hello world",
714 on_communicate_complete,
719 g_main_context_iteration (NULL, TRUE);
721 g_assert_no_error (data.error);
723 g_object_unref (proc);
727 test_communicate_utf8 (void)
729 GError *error = NULL;
732 GCancellable *cancellable = NULL;
733 const gchar *stdin_buf;
736 args = get_test_subprocess_args ("cat", NULL);
737 proc = g_subprocess_newv ((const gchar* const*)args->pdata,
738 G_SUBPROCESS_FLAGS_STDIN_PIPE
739 | G_SUBPROCESS_FLAGS_STDOUT_PIPE
740 | G_SUBPROCESS_FLAGS_STDERR_MERGE,
742 g_assert_no_error (error);
743 g_ptr_array_free (args, TRUE);
745 stdin_buf = "hello world";
747 g_subprocess_communicate_utf8 (proc, stdin_buf, cancellable, &stdout_buf, NULL, &error);
748 g_assert_no_error (error);
750 g_assert (strcmp (stdout_buf, "hello world") == 0);
753 g_object_unref (proc);
757 test_communicate_utf8_invalid (void)
759 GError *error = NULL;
761 TestAsyncCommunicateData data = { 0, };
763 GCancellable *cancellable = NULL;
765 args = get_test_subprocess_args ("cat", NULL);
766 proc = g_subprocess_newv ((const gchar* const*)args->pdata,
767 G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE,
769 g_assert_no_error (error);
770 g_ptr_array_free (args, TRUE);
773 g_subprocess_communicate_utf8_async (proc, "\xFF\xFF",
775 on_communicate_complete,
780 g_main_context_iteration (NULL, TRUE);
782 g_assert_error (data.error, G_IO_ERROR, G_IO_ERROR_FAILED);
783 g_error_free (data.error);
785 g_object_unref (proc);
789 send_terminate (gpointer user_data)
791 GSubprocess *proc = user_data;
793 g_subprocess_force_exit (proc);
799 on_request_quit_exited (GObject *object,
800 GAsyncResult *result,
803 GSubprocess *subprocess = G_SUBPROCESS (object);
804 GError *error = NULL;
806 g_subprocess_wait_finish (subprocess, result, &error);
807 g_assert_no_error (error);
809 g_assert (g_subprocess_get_if_signaled (subprocess));
810 g_assert (g_subprocess_get_term_sig (subprocess) == 9);
812 g_spawn_check_exit_status (g_subprocess_get_status (subprocess), &error);
813 g_assert (error != NULL);
814 g_clear_error (&error);
816 g_main_loop_quit ((GMainLoop*)user_data);
820 test_terminate (void)
822 GError *local_error = NULL;
823 GError **error = &local_error;
828 args = get_test_subprocess_args ("sleep-forever", NULL);
829 proc = g_subprocess_newv ((const gchar * const *) args->pdata, G_SUBPROCESS_FLAGS_NONE, error);
830 g_ptr_array_free (args, TRUE);
831 g_assert_no_error (local_error);
833 loop = g_main_loop_new (NULL, TRUE);
835 g_subprocess_wait_async (proc, NULL, on_request_quit_exited, loop);
837 g_timeout_add_seconds (3, send_terminate, proc);
839 g_main_loop_run (loop);
841 g_main_loop_unref (loop);
842 g_object_unref (proc);
847 send_signal (gpointer user_data)
849 GSubprocess *proc = user_data;
851 g_subprocess_send_signal (proc, SIGKILL);
859 GError *local_error = NULL;
860 GError **error = &local_error;
865 args = get_test_subprocess_args ("sleep-forever", NULL);
866 proc = g_subprocess_newv ((const gchar * const *) args->pdata, G_SUBPROCESS_FLAGS_NONE, error);
867 g_ptr_array_free (args, TRUE);
868 g_assert_no_error (local_error);
870 loop = g_main_loop_new (NULL, TRUE);
872 g_subprocess_wait_async (proc, NULL, on_request_quit_exited, loop);
874 g_timeout_add_seconds (3, send_signal, proc);
876 g_main_loop_run (loop);
878 g_main_loop_unref (loop);
879 g_object_unref (proc);
886 GError *local_error = NULL;
887 GError **error = &local_error;
888 GSubprocessLauncher *launcher;
891 GInputStream *stdout;
893 gchar *envp[] = { "ONE=1", "TWO=1", "THREE=3", "FOUR=1", NULL };
896 args = get_test_subprocess_args ("env", NULL);
897 launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_NONE);
898 g_subprocess_launcher_set_flags (launcher, G_SUBPROCESS_FLAGS_STDOUT_PIPE);
899 g_subprocess_launcher_set_environ (launcher, envp);
900 g_subprocess_launcher_setenv (launcher, "TWO", "2", TRUE);
901 g_subprocess_launcher_setenv (launcher, "THREE", "1", FALSE);
902 g_subprocess_launcher_unsetenv (launcher, "FOUR");
904 g_assert_null (g_subprocess_launcher_getenv (launcher, "FOUR"));
906 proc = g_subprocess_launcher_spawn (launcher, error, args->pdata[0], "env", NULL);
907 g_ptr_array_free (args, TRUE);
908 g_assert_no_error (local_error);
910 stdout = g_subprocess_get_stdout_pipe (proc);
912 result = splice_to_string (stdout, error);
913 split = g_strsplit (result, "\n", -1);
914 g_assert_cmpstr (g_environ_getenv (split, "ONE"), ==, "1");
915 g_assert_cmpstr (g_environ_getenv (split, "TWO"), ==, "2");
916 g_assert_cmpstr (g_environ_getenv (split, "THREE"), ==, "3");
917 g_assert_null (g_environ_getenv (split, "FOUR"));
921 g_object_unref (proc);
927 GError *local_error = NULL;
928 GError **error = &local_error;
929 GSubprocessLauncher *launcher;
932 GInputStream *stdout;
934 const char *basename;
936 args = get_test_subprocess_args ("cwd", NULL);
937 launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE);
938 g_subprocess_launcher_set_flags (launcher, G_SUBPROCESS_FLAGS_STDOUT_PIPE);
939 g_subprocess_launcher_set_cwd (launcher, "/tmp");
941 proc = g_subprocess_launcher_spawnv (launcher, (const char * const *)args->pdata, error);
942 g_ptr_array_free (args, TRUE);
943 g_assert_no_error (local_error);
945 stdout = g_subprocess_get_stdout_pipe (proc);
947 result = splice_to_string (stdout, error);
949 basename = g_strrstr (result, "/");
950 g_assert (basename != NULL);
951 g_assert_cmpstr (basename, ==, "/tmp" LINEEND);
954 g_object_unref (proc);
958 test_stdout_file (void)
960 GError *local_error = NULL;
961 GError **error = &local_error;
962 GSubprocessLauncher *launcher;
966 GFileIOStream *iostream;
967 GOutputStream *stdin;
968 const char *test_data = "this is some test data\n";
972 tmpfile = g_file_new_tmp ("gsubprocessXXXXXX", &iostream, error);
973 g_assert_no_error (local_error);
974 g_clear_object (&iostream);
976 tmp_file_path = g_file_get_path (tmpfile);
978 args = get_test_subprocess_args ("cat", NULL);
979 launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDIN_PIPE);
980 g_subprocess_launcher_set_stdout_file_path (launcher, tmp_file_path);
981 proc = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, error);
982 g_ptr_array_free (args, TRUE);
983 g_assert_no_error (local_error);
985 stdin = g_subprocess_get_stdin_pipe (proc);
987 g_output_stream_write_all (stdin, test_data, strlen (test_data), NULL, NULL, error);
988 g_assert_no_error (local_error);
990 g_output_stream_close (stdin, NULL, error);
991 g_assert_no_error (local_error);
993 g_subprocess_wait_check (proc, NULL, error);
995 g_object_unref (launcher);
996 g_object_unref (proc);
998 g_file_load_contents (tmpfile, NULL, &tmp_contents, NULL, NULL, error);
999 g_assert_no_error (local_error);
1001 g_assert_cmpstr (test_data, ==, tmp_contents);
1002 g_free (tmp_contents);
1004 (void) g_file_delete (tmpfile, NULL, NULL);
1005 g_object_unref (tmpfile);
1006 g_free (tmp_file_path);
1010 test_stdout_fd (void)
1012 GError *local_error = NULL;
1013 GError **error = &local_error;
1014 GSubprocessLauncher *launcher;
1018 GFileIOStream *iostream;
1019 GFileDescriptorBased *descriptor_stream;
1020 GOutputStream *stdin;
1021 const char *test_data = "this is some test data\n";
1024 tmpfile = g_file_new_tmp ("gsubprocessXXXXXX", &iostream, error);
1025 g_assert_no_error (local_error);
1027 args = get_test_subprocess_args ("cat", NULL);
1028 launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDIN_PIPE);
1029 descriptor_stream = G_FILE_DESCRIPTOR_BASED (g_io_stream_get_output_stream (G_IO_STREAM (iostream)));
1030 g_subprocess_launcher_take_stdout_fd (launcher, dup (g_file_descriptor_based_get_fd (descriptor_stream)));
1031 proc = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, error);
1032 g_ptr_array_free (args, TRUE);
1033 g_assert_no_error (local_error);
1035 g_clear_object (&iostream);
1037 stdin = g_subprocess_get_stdin_pipe (proc);
1039 g_output_stream_write_all (stdin, test_data, strlen (test_data), NULL, NULL, error);
1040 g_assert_no_error (local_error);
1042 g_output_stream_close (stdin, NULL, error);
1043 g_assert_no_error (local_error);
1045 g_subprocess_wait_check (proc, NULL, error);
1047 g_object_unref (launcher);
1048 g_object_unref (proc);
1050 g_file_load_contents (tmpfile, NULL, &tmp_contents, NULL, NULL, error);
1051 g_assert_no_error (local_error);
1053 g_assert_cmpstr (test_data, ==, tmp_contents);
1054 g_free (tmp_contents);
1056 (void) g_file_delete (tmpfile, NULL, NULL);
1057 g_object_unref (tmpfile);
1061 child_setup (gpointer user_data)
1063 dup2 (GPOINTER_TO_INT (user_data), 1);
1067 test_child_setup (void)
1069 GError *local_error = NULL;
1070 GError **error = &local_error;
1071 GSubprocessLauncher *launcher;
1075 GFileIOStream *iostream;
1076 GOutputStream *stdin;
1077 const char *test_data = "this is some test data\n";
1081 tmpfile = g_file_new_tmp ("gsubprocessXXXXXX", &iostream, error);
1082 g_assert_no_error (local_error);
1084 fd = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (g_io_stream_get_output_stream (G_IO_STREAM (iostream))));
1086 args = get_test_subprocess_args ("cat", NULL);
1087 launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDIN_PIPE);
1088 g_subprocess_launcher_set_child_setup (launcher, child_setup, GINT_TO_POINTER (fd), NULL);
1089 proc = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, error);
1090 g_ptr_array_free (args, TRUE);
1091 g_assert_no_error (local_error);
1093 g_clear_object (&iostream);
1095 stdin = g_subprocess_get_stdin_pipe (proc);
1097 g_output_stream_write_all (stdin, test_data, strlen (test_data), NULL, NULL, error);
1098 g_assert_no_error (local_error);
1100 g_output_stream_close (stdin, NULL, error);
1101 g_assert_no_error (local_error);
1103 g_subprocess_wait_check (proc, NULL, error);
1105 g_object_unref (launcher);
1106 g_object_unref (proc);
1108 g_file_load_contents (tmpfile, NULL, &tmp_contents, NULL, NULL, error);
1109 g_assert_no_error (local_error);
1111 g_assert_cmpstr (test_data, ==, tmp_contents);
1112 g_free (tmp_contents);
1114 (void) g_file_delete (tmpfile, NULL, NULL);
1115 g_object_unref (tmpfile);
1121 GError *local_error = NULL;
1122 GError **error = &local_error;
1123 GInputStream *child_input;
1124 GDataInputStream *child_datainput;
1125 GSubprocessLauncher *launcher;
1128 int basic_pipefds[2];
1129 int needdup_pipefds[2];
1133 char *needdup_fd_str;
1135 g_unix_open_pipe (basic_pipefds, FD_CLOEXEC, error);
1136 g_assert_no_error (local_error);
1137 g_unix_open_pipe (needdup_pipefds, FD_CLOEXEC, error);
1138 g_assert_no_error (local_error);
1140 basic_fd_str = g_strdup_printf ("%d", basic_pipefds[1]);
1141 needdup_fd_str = g_strdup_printf ("%d", needdup_pipefds[1] + 1);
1143 args = get_test_subprocess_args ("write-to-fds", basic_fd_str, needdup_fd_str, NULL);
1144 launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_NONE);
1145 g_subprocess_launcher_take_fd (launcher, basic_pipefds[1], basic_pipefds[1]);
1146 g_subprocess_launcher_take_fd (launcher, needdup_pipefds[1], needdup_pipefds[1] + 1);
1147 proc = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, error);
1148 g_ptr_array_free (args, TRUE);
1149 g_assert_no_error (local_error);
1151 g_free (basic_fd_str);
1152 g_free (needdup_fd_str);
1154 child_input = g_unix_input_stream_new (basic_pipefds[0], TRUE);
1155 child_datainput = g_data_input_stream_new (child_input);
1156 buf = g_data_input_stream_read_line_utf8 (child_datainput, &len, NULL, error);
1157 g_assert_no_error (local_error);
1158 g_assert_cmpstr (buf, ==, "hello world");
1159 g_object_unref (child_datainput);
1160 g_object_unref (child_input);
1163 child_input = g_unix_input_stream_new (needdup_pipefds[0], TRUE);
1164 child_datainput = g_data_input_stream_new (child_input);
1165 buf = g_data_input_stream_read_line_utf8 (child_datainput, &len, NULL, error);
1166 g_assert_no_error (local_error);
1167 g_assert_cmpstr (buf, ==, "hello world");
1169 g_object_unref (child_datainput);
1170 g_object_unref (child_input);
1172 g_object_unref (launcher);
1173 g_object_unref (proc);
1179 main (int argc, char **argv)
1181 g_test_init (&argc, &argv, NULL);
1183 g_test_add_func ("/gsubprocess/noop", test_noop);
1184 g_test_add_func ("/gsubprocess/noop-all-to-null", test_noop_all_to_null);
1185 g_test_add_func ("/gsubprocess/noop-no-wait", test_noop_no_wait);
1186 g_test_add_func ("/gsubprocess/noop-stdin-inherit", test_noop_stdin_inherit);
1188 g_test_add_func ("/gsubprocess/search-path", test_search_path);
1189 g_test_add_func ("/gsubprocess/signal", test_signal);
1191 g_test_add_func ("/gsubprocess/exit1", test_exit1);
1192 g_test_add_func ("/gsubprocess/echo1", test_echo1);
1194 g_test_add_func ("/gsubprocess/echo-merged", test_echo_merged);
1196 g_test_add_func ("/gsubprocess/cat-utf8", test_cat_utf8);
1197 g_test_add_func ("/gsubprocess/cat-eof", test_cat_eof);
1198 g_test_add_func ("/gsubprocess/multi1", test_multi_1);
1199 g_test_add_func ("/gsubprocess/communicate", test_communicate);
1200 g_test_add_func ("/gsubprocess/communicate-async", test_communicate_async);
1201 g_test_add_func ("/gsubprocess/communicate-utf8", test_communicate_utf8);
1202 g_test_add_func ("/gsubprocess/communicate-utf8-async", test_communicate_utf8_async);
1203 g_test_add_func ("/gsubprocess/communicate-utf8-invalid", test_communicate_utf8_invalid);
1204 g_test_add_func ("/gsubprocess/terminate", test_terminate);
1205 g_test_add_func ("/gsubprocess/env", test_env);
1206 g_test_add_func ("/gsubprocess/cwd", test_cwd);
1208 g_test_add_func ("/gsubprocess/stdout-file", test_stdout_file);
1209 g_test_add_func ("/gsubprocess/stdout-fd", test_stdout_fd);
1210 g_test_add_func ("/gsubprocess/child-setup", test_child_setup);
1211 g_test_add_func ("/gsubprocess/pass-fd", test_pass_fd);
1214 return g_test_run ();