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
);
187 GCancellable
*cancellable
;
189 } TestExit1CancelData
;
192 test_exit1_cancel_idle_quit_cb (gpointer user_data
)
194 GMainLoop
*loop
= user_data
;
195 g_main_loop_quit (loop
);
196 return G_SOURCE_REMOVE
;
200 test_exit1_cancel_wait_check_cb (GObject
*source
,
201 GAsyncResult
*result
,
204 GSubprocess
*subprocess
= G_SUBPROCESS (source
);
205 TestExit1CancelData
*data
= user_data
;
207 GError
*error
= NULL
;
209 g_assert_false (data
->cb_called
);
210 data
->cb_called
= TRUE
;
212 ret
= g_subprocess_wait_check_finish (subprocess
, result
, &error
);
214 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_CANCELLED
);
215 g_clear_error (&error
);
217 g_idle_add (test_exit1_cancel_idle_quit_cb
, data
->loop
);
221 test_exit1_cancel (void)
223 GError
*local_error
= NULL
;
224 GError
**error
= &local_error
;
227 TestExit1CancelData data
= { 0 };
229 g_test_bug ("786456");
231 args
= get_test_subprocess_args ("exit1", NULL
);
232 proc
= g_subprocess_newv ((const gchar
* const *) args
->pdata
, G_SUBPROCESS_FLAGS_NONE
, error
);
233 g_ptr_array_free (args
, TRUE
);
234 g_assert_no_error (local_error
);
236 data
.loop
= g_main_loop_new (NULL
, FALSE
);
237 data
.cancellable
= g_cancellable_new ();
238 g_subprocess_wait_check_async (proc
, data
.cancellable
, test_exit1_cancel_wait_check_cb
, &data
);
240 g_subprocess_wait_check (proc
, NULL
, error
);
241 g_assert_error (local_error
, G_SPAWN_EXIT_ERROR
, 1);
242 g_clear_error (error
);
244 g_cancellable_cancel (data
.cancellable
);
245 g_main_loop_run (data
.loop
);
247 g_object_unref (proc
);
248 g_main_loop_unref (data
.loop
);
249 g_clear_object (&data
.cancellable
);
253 test_exit1_cancel_in_cb_wait_check_cb (GObject
*source
,
254 GAsyncResult
*result
,
257 GSubprocess
*subprocess
= G_SUBPROCESS (source
);
258 TestExit1CancelData
*data
= user_data
;
260 GError
*error
= NULL
;
262 g_assert_false (data
->cb_called
);
263 data
->cb_called
= TRUE
;
265 ret
= g_subprocess_wait_check_finish (subprocess
, result
, &error
);
267 g_assert_error (error
, G_SPAWN_EXIT_ERROR
, 1);
268 g_clear_error (&error
);
270 g_cancellable_cancel (data
->cancellable
);
272 g_idle_add (test_exit1_cancel_idle_quit_cb
, data
->loop
);
276 test_exit1_cancel_in_cb (void)
278 GError
*local_error
= NULL
;
279 GError
**error
= &local_error
;
282 TestExit1CancelData data
= { 0 };
284 g_test_bug ("786456");
286 args
= get_test_subprocess_args ("exit1", NULL
);
287 proc
= g_subprocess_newv ((const gchar
* const *) args
->pdata
, G_SUBPROCESS_FLAGS_NONE
, error
);
288 g_ptr_array_free (args
, TRUE
);
289 g_assert_no_error (local_error
);
291 data
.loop
= g_main_loop_new (NULL
, FALSE
);
292 data
.cancellable
= g_cancellable_new ();
293 g_subprocess_wait_check_async (proc
, data
.cancellable
, test_exit1_cancel_in_cb_wait_check_cb
, &data
);
295 g_subprocess_wait_check (proc
, NULL
, error
);
296 g_assert_error (local_error
, G_SPAWN_EXIT_ERROR
, 1);
297 g_clear_error (error
);
299 g_main_loop_run (data
.loop
);
301 g_object_unref (proc
);
302 g_main_loop_unref (data
.loop
);
303 g_clear_object (&data
.cancellable
);
307 splice_to_string (GInputStream
*stream
,
310 GMemoryOutputStream
*buffer
= NULL
;
313 buffer
= (GMemoryOutputStream
*)g_memory_output_stream_new (NULL
, 0, g_realloc
, g_free
);
314 if (g_output_stream_splice ((GOutputStream
*)buffer
, stream
, 0, NULL
, error
) < 0)
317 if (!g_output_stream_write ((GOutputStream
*)buffer
, "\0", 1, NULL
, error
))
320 if (!g_output_stream_close ((GOutputStream
*)buffer
, NULL
, error
))
323 ret
= g_memory_output_stream_steal_data (buffer
);
325 g_clear_object (&buffer
);
332 GError
*local_error
= NULL
;
333 GError
**error
= &local_error
;
336 GInputStream
*stdout
;
339 args
= get_test_subprocess_args ("echo", "hello", "world!", NULL
);
340 proc
= g_subprocess_newv ((const gchar
* const *) args
->pdata
, G_SUBPROCESS_FLAGS_STDOUT_PIPE
, error
);
341 g_ptr_array_free (args
, TRUE
);
342 g_assert_no_error (local_error
);
344 stdout
= g_subprocess_get_stdout_pipe (proc
);
346 result
= splice_to_string (stdout
, error
);
347 g_assert_no_error (local_error
);
349 g_assert_cmpstr (result
, ==, "hello" LINEEND
"world!" LINEEND
);
352 g_object_unref (proc
);
357 test_echo_merged (void)
359 GError
*local_error
= NULL
;
360 GError
**error
= &local_error
;
363 GInputStream
*stdout
;
366 args
= get_test_subprocess_args ("echo-stdout-and-stderr", "merge", "this", NULL
);
367 proc
= g_subprocess_newv ((const gchar
* const *) args
->pdata
,
368 G_SUBPROCESS_FLAGS_STDOUT_PIPE
| G_SUBPROCESS_FLAGS_STDERR_MERGE
,
370 g_ptr_array_free (args
, TRUE
);
371 g_assert_no_error (local_error
);
373 stdout
= g_subprocess_get_stdout_pipe (proc
);
374 result
= splice_to_string (stdout
, error
);
375 g_assert_no_error (local_error
);
377 g_assert_cmpstr (result
, ==, "merge\nmerge\nthis\nthis\n");
380 g_object_unref (proc
);
385 guint events_pending
;
390 test_cat_on_input_splice_complete (GObject
*object
,
391 GAsyncResult
*result
,
394 TestCatData
*data
= user_data
;
395 GError
*error
= NULL
;
397 (void)g_output_stream_splice_finish ((GOutputStream
*)object
, result
, &error
);
398 g_assert_no_error (error
);
400 data
->events_pending
--;
401 if (data
->events_pending
== 0)
402 g_main_loop_quit (data
->loop
);
408 GError
*local_error
= NULL
;
409 GError
**error
= &local_error
;
414 GInputStream
*input_buf_stream
= NULL
;
415 GOutputStream
*output_buf_stream
= NULL
;
416 GOutputStream
*stdin_stream
= NULL
;
417 GInputStream
*stdout_stream
= NULL
;
420 memset (&data
, 0, sizeof (data
));
421 data
.loop
= g_main_loop_new (NULL
, TRUE
);
423 args
= get_test_subprocess_args ("cat", NULL
);
424 proc
= g_subprocess_newv ((const gchar
* const *) args
->pdata
,
425 G_SUBPROCESS_FLAGS_STDIN_PIPE
| G_SUBPROCESS_FLAGS_STDOUT_PIPE
,
427 g_ptr_array_free (args
, TRUE
);
428 g_assert_no_error (local_error
);
430 stdin_stream
= g_subprocess_get_stdin_pipe (proc
);
431 stdout_stream
= g_subprocess_get_stdout_pipe (proc
);
433 input_buf
= g_bytes_new_static ("hello, world!", strlen ("hello, world!"));
434 input_buf_stream
= g_memory_input_stream_new_from_bytes (input_buf
);
435 g_bytes_unref (input_buf
);
437 output_buf_stream
= g_memory_output_stream_new (NULL
, 0, g_realloc
, g_free
);
439 g_output_stream_splice_async (stdin_stream
, input_buf_stream
, G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE
| G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET
,
440 G_PRIORITY_DEFAULT
, NULL
, test_cat_on_input_splice_complete
,
442 data
.events_pending
++;
443 g_output_stream_splice_async (output_buf_stream
, stdout_stream
, G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE
| G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET
,
444 G_PRIORITY_DEFAULT
, NULL
, test_cat_on_input_splice_complete
,
446 data
.events_pending
++;
448 g_main_loop_run (data
.loop
);
450 g_subprocess_wait_check (proc
, NULL
, error
);
451 g_assert_no_error (local_error
);
453 output_buf
= g_memory_output_stream_steal_as_bytes ((GMemoryOutputStream
*)output_buf_stream
);
455 g_assert_cmpmem (g_bytes_get_data (output_buf
, NULL
),
456 g_bytes_get_size (output_buf
),
457 "hello, world!", 13);
459 g_bytes_unref (output_buf
);
460 g_main_loop_unref (data
.loop
);
461 g_object_unref (input_buf_stream
);
462 g_object_unref (output_buf_stream
);
463 g_object_unref (proc
);
467 cancel_soon (gpointer user_data
)
469 GCancellable
*cancellable
= user_data
;
471 g_usleep (G_TIME_SPAN_SECOND
);
472 g_cancellable_cancel (cancellable
);
473 g_object_unref (cancellable
);
481 GCancellable
*cancellable
;
482 GError
*error
= NULL
;
489 g_test_skip ("This test has not been ported to Win32");
494 cat
= g_subprocess_new (G_SUBPROCESS_FLAGS_STDIN_PIPE
| G_SUBPROCESS_FLAGS_STDOUT_PIPE
, &error
, "cat", NULL
);
495 g_assert_no_error (error
);
498 /* Make sure that reading stdout blocks (until we cancel) */
499 cancellable
= g_cancellable_new ();
500 g_thread_unref (g_thread_new ("cancel thread", cancel_soon
, g_object_ref (cancellable
)));
501 s
= g_input_stream_read (g_subprocess_get_stdout_pipe (cat
), &buffer
, sizeof buffer
, cancellable
, &error
);
502 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_CANCELLED
);
503 g_assert_cmpint (s
, ==, -1);
504 g_object_unref (cancellable
);
505 g_clear_error (&error
);
507 /* Close the stream (EOF on cat's stdin) */
508 result
= g_output_stream_close (g_subprocess_get_stdin_pipe (cat
), NULL
, &error
);
509 g_assert_no_error (error
);
512 /* Now check that reading cat's stdout gets us an EOF (since it quit) */
513 s
= g_input_stream_read (g_subprocess_get_stdout_pipe (cat
), &buffer
, sizeof buffer
, NULL
, &error
);
514 g_assert_no_error (error
);
517 /* Check that the process has exited as a result of the EOF */
518 result
= g_subprocess_wait (cat
, NULL
, &error
);
519 g_assert_no_error (error
);
520 g_assert (g_subprocess_get_if_exited (cat
));
521 g_assert_cmpint (g_subprocess_get_exit_status (cat
), ==, 0);
524 g_object_unref (cat
);
528 guint events_pending
;
529 gboolean caught_error
;
534 GOutputStream
*first_stdin
;
535 } TestMultiSpliceData
;
538 on_one_multi_splice_done (GObject
*obj
,
542 TestMultiSpliceData
*data
= user_data
;
544 if (!data
->caught_error
)
546 if (g_output_stream_splice_finish ((GOutputStream
*)obj
, res
, &data
->error
) < 0)
547 data
->caught_error
= TRUE
;
550 data
->events_pending
--;
551 if (data
->events_pending
== 0)
552 g_main_loop_quit (data
->loop
);
556 on_idle_multisplice (gpointer user_data
)
558 TestMultiSpliceData
*data
= user_data
;
560 /* We write 2^1 + 2^2 ... + 2^10 or 2047 copies of "Hello World!\n"
563 if (data
->counter
>= 2047 || data
->caught_error
)
565 if (!g_output_stream_close (data
->first_stdin
, NULL
, &data
->error
))
566 data
->caught_error
= TRUE
;
567 data
->events_pending
--;
568 if (data
->events_pending
== 0)
570 g_main_loop_quit (data
->loop
);
577 for (i
= 0; i
< data
->counter
; i
++)
580 if (!g_output_stream_write_all (data
->first_stdin
, "hello world!\n",
581 strlen ("hello world!\n"), &bytes_written
,
584 data
->caught_error
= TRUE
;
594 on_subprocess_exited (GObject
*object
,
595 GAsyncResult
*result
,
598 GSubprocess
*subprocess
= G_SUBPROCESS (object
);
599 TestMultiSpliceData
*data
= user_data
;
600 GError
*error
= NULL
;
602 if (!g_subprocess_wait_finish (subprocess
, result
, &error
))
604 if (!data
->caught_error
)
606 data
->caught_error
= TRUE
;
607 g_propagate_error (&data
->error
, error
);
610 g_spawn_check_exit_status (g_subprocess_get_exit_status (subprocess
), &error
);
611 g_assert_no_error (error
);
612 data
->events_pending
--;
613 if (data
->events_pending
== 0)
614 g_main_loop_quit (data
->loop
);
620 GError
*local_error
= NULL
;
621 GError
**error
= &local_error
;
623 GSubprocessLauncher
*launcher
;
627 GOutputStream
*first_stdin
;
628 GInputStream
*first_stdout
;
629 GOutputStream
*second_stdin
;
630 GInputStream
*second_stdout
;
631 GOutputStream
*third_stdin
;
632 GInputStream
*third_stdout
;
633 GOutputStream
*membuf
;
634 TestMultiSpliceData data
;
635 int splice_flags
= G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE
| G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET
;
637 args
= get_test_subprocess_args ("cat", NULL
);
638 launcher
= g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDIN_PIPE
| G_SUBPROCESS_FLAGS_STDOUT_PIPE
);
639 first
= g_subprocess_launcher_spawnv (launcher
, (const gchar
* const *) args
->pdata
, error
);
640 g_assert_no_error (local_error
);
641 second
= g_subprocess_launcher_spawnv (launcher
, (const gchar
* const *) args
->pdata
, error
);
642 g_assert_no_error (local_error
);
643 third
= g_subprocess_launcher_spawnv (launcher
, (const gchar
* const *) args
->pdata
, error
);
644 g_assert_no_error (local_error
);
646 g_ptr_array_free (args
, TRUE
);
648 membuf
= g_memory_output_stream_new (NULL
, 0, g_realloc
, g_free
);
650 first_stdin
= g_subprocess_get_stdin_pipe (first
);
651 first_stdout
= g_subprocess_get_stdout_pipe (first
);
652 second_stdin
= g_subprocess_get_stdin_pipe (second
);
653 second_stdout
= g_subprocess_get_stdout_pipe (second
);
654 third_stdin
= g_subprocess_get_stdin_pipe (third
);
655 third_stdout
= g_subprocess_get_stdout_pipe (third
);
657 memset (&data
, 0, sizeof (data
));
658 data
.loop
= g_main_loop_new (NULL
, TRUE
);
660 data
.first_stdin
= first_stdin
;
662 data
.events_pending
++;
663 g_output_stream_splice_async (second_stdin
, first_stdout
, splice_flags
, G_PRIORITY_DEFAULT
,
664 NULL
, on_one_multi_splice_done
, &data
);
665 data
.events_pending
++;
666 g_output_stream_splice_async (third_stdin
, second_stdout
, splice_flags
, G_PRIORITY_DEFAULT
,
667 NULL
, on_one_multi_splice_done
, &data
);
668 data
.events_pending
++;
669 g_output_stream_splice_async (membuf
, third_stdout
, splice_flags
, G_PRIORITY_DEFAULT
,
670 NULL
, on_one_multi_splice_done
, &data
);
672 data
.events_pending
++;
673 g_timeout_add (250, on_idle_multisplice
, &data
);
675 data
.events_pending
++;
676 g_subprocess_wait_async (first
, NULL
, on_subprocess_exited
, &data
);
677 data
.events_pending
++;
678 g_subprocess_wait_async (second
, NULL
, on_subprocess_exited
, &data
);
679 data
.events_pending
++;
680 g_subprocess_wait_async (third
, NULL
, on_subprocess_exited
, &data
);
682 g_main_loop_run (data
.loop
);
684 g_assert (!data
.caught_error
);
685 g_assert_no_error (data
.error
);
687 g_assert_cmpint (g_memory_output_stream_get_data_size ((GMemoryOutputStream
*)membuf
), ==, 26611);
689 g_main_loop_unref (data
.loop
);
690 g_object_unref (membuf
);
691 g_object_unref (launcher
);
692 g_object_unref (first
);
693 g_object_unref (second
);
694 g_object_unref (third
);
701 } TestAsyncCommunicateData
;
704 on_communicate_complete (GObject
*proc
,
705 GAsyncResult
*result
,
708 TestAsyncCommunicateData
*data
= user_data
;
709 GBytes
*stdout
= NULL
;
710 char *stdout_str
= NULL
;
711 const guint8
*stdout_data
;
714 data
->running
= FALSE
;
716 (void) g_subprocess_communicate_utf8_finish ((GSubprocess
*)proc
, result
,
717 &stdout_str
, NULL
, &data
->error
);
719 (void) g_subprocess_communicate_finish ((GSubprocess
*)proc
, result
,
720 &stdout
, NULL
, &data
->error
);
726 g_assert (stdout
!= NULL
);
727 stdout_data
= g_bytes_get_data (stdout
, &stdout_len
);
731 g_assert (stdout_str
!= NULL
);
732 stdout_data
= (guint8
*)stdout_str
;
733 stdout_len
= strlen (stdout_str
);
736 g_assert_cmpmem (stdout_data
, stdout_len
, "hello world", 11);
738 g_bytes_unref (stdout
);
743 test_communicate_async (void)
745 GError
*error
= NULL
;
747 TestAsyncCommunicateData data
= { 0, };
749 GCancellable
*cancellable
= NULL
;
751 const char *hellostring
;
753 args
= get_test_subprocess_args ("cat", NULL
);
754 proc
= g_subprocess_newv ((const gchar
* const*)args
->pdata
,
755 G_SUBPROCESS_FLAGS_STDIN_PIPE
| G_SUBPROCESS_FLAGS_STDOUT_PIPE
,
757 g_assert_no_error (error
);
758 g_ptr_array_free (args
, TRUE
);
760 hellostring
= "hello world";
761 input
= g_bytes_new_static (hellostring
, strlen (hellostring
));
763 g_subprocess_communicate_async (proc
, input
,
765 on_communicate_complete
,
770 g_main_context_iteration (NULL
, TRUE
);
772 g_assert_no_error (data
.error
);
774 g_bytes_unref (input
);
775 g_object_unref (proc
);
779 test_communicate (void)
781 GError
*error
= NULL
;
784 GCancellable
*cancellable
= NULL
;
786 const gchar
*hellostring
;
788 const gchar
*stdout_data
;
791 args
= get_test_subprocess_args ("cat", NULL
);
792 proc
= g_subprocess_newv ((const gchar
* const*)args
->pdata
,
793 G_SUBPROCESS_FLAGS_STDIN_PIPE
794 | G_SUBPROCESS_FLAGS_STDOUT_PIPE
795 | G_SUBPROCESS_FLAGS_STDERR_MERGE
,
797 g_assert_no_error (error
);
798 g_ptr_array_free (args
, TRUE
);
800 hellostring
= "hello world";
801 input
= g_bytes_new_static (hellostring
, strlen (hellostring
));
803 g_subprocess_communicate (proc
, input
, cancellable
, &stdout
, NULL
, &error
);
804 g_assert_no_error (error
);
805 stdout_data
= g_bytes_get_data (stdout
, &stdout_len
);
807 g_assert_cmpmem (stdout_data
, stdout_len
, "hello world", 11);
809 g_bytes_unref (input
);
810 g_bytes_unref (stdout
);
811 g_object_unref (proc
);
815 test_communicate_utf8_async (void)
817 GError
*error
= NULL
;
819 TestAsyncCommunicateData data
= { 0, };
821 GCancellable
*cancellable
= NULL
;
823 args
= get_test_subprocess_args ("cat", NULL
);
824 proc
= g_subprocess_newv ((const gchar
* const*)args
->pdata
,
825 G_SUBPROCESS_FLAGS_STDIN_PIPE
| G_SUBPROCESS_FLAGS_STDOUT_PIPE
,
827 g_assert_no_error (error
);
828 g_ptr_array_free (args
, TRUE
);
831 g_subprocess_communicate_utf8_async (proc
, "hello world",
833 on_communicate_complete
,
838 g_main_context_iteration (NULL
, TRUE
);
840 g_assert_no_error (data
.error
);
842 g_object_unref (proc
);
846 test_communicate_utf8 (void)
848 GError
*error
= NULL
;
851 GCancellable
*cancellable
= NULL
;
852 const gchar
*stdin_buf
;
855 args
= get_test_subprocess_args ("cat", NULL
);
856 proc
= g_subprocess_newv ((const gchar
* const*)args
->pdata
,
857 G_SUBPROCESS_FLAGS_STDIN_PIPE
858 | G_SUBPROCESS_FLAGS_STDOUT_PIPE
859 | G_SUBPROCESS_FLAGS_STDERR_MERGE
,
861 g_assert_no_error (error
);
862 g_ptr_array_free (args
, TRUE
);
864 stdin_buf
= "hello world";
866 g_subprocess_communicate_utf8 (proc
, stdin_buf
, cancellable
, &stdout_buf
, NULL
, &error
);
867 g_assert_no_error (error
);
869 g_assert (strcmp (stdout_buf
, "hello world") == 0);
872 g_object_unref (proc
);
876 test_communicate_nothing (void)
878 GError
*error
= NULL
;
881 GCancellable
*cancellable
= NULL
;
884 args
= get_test_subprocess_args ("cat", NULL
);
885 proc
= g_subprocess_newv ((const gchar
* const*)args
->pdata
,
886 G_SUBPROCESS_FLAGS_STDIN_PIPE
887 | G_SUBPROCESS_FLAGS_STDOUT_PIPE
888 | G_SUBPROCESS_FLAGS_STDERR_MERGE
,
890 g_assert_no_error (error
);
891 g_ptr_array_free (args
, TRUE
);
893 g_subprocess_communicate_utf8 (proc
, "", cancellable
, &stdout_buf
, NULL
, &error
);
894 g_assert_no_error (error
);
896 g_assert_cmpstr (stdout_buf
, ==, "");
900 g_object_unref (proc
);
904 test_communicate_utf8_invalid (void)
906 GError
*error
= NULL
;
908 TestAsyncCommunicateData data
= { 0, };
910 GCancellable
*cancellable
= NULL
;
912 args
= get_test_subprocess_args ("cat", NULL
);
913 proc
= g_subprocess_newv ((const gchar
* const*)args
->pdata
,
914 G_SUBPROCESS_FLAGS_STDIN_PIPE
| G_SUBPROCESS_FLAGS_STDOUT_PIPE
,
916 g_assert_no_error (error
);
917 g_ptr_array_free (args
, TRUE
);
920 g_subprocess_communicate_utf8_async (proc
, "\xFF\xFF",
922 on_communicate_complete
,
927 g_main_context_iteration (NULL
, TRUE
);
929 g_assert_error (data
.error
, G_IO_ERROR
, G_IO_ERROR_FAILED
);
930 g_error_free (data
.error
);
932 g_object_unref (proc
);
936 send_terminate (gpointer user_data
)
938 GSubprocess
*proc
= user_data
;
940 g_subprocess_force_exit (proc
);
946 on_request_quit_exited (GObject
*object
,
947 GAsyncResult
*result
,
950 GSubprocess
*subprocess
= G_SUBPROCESS (object
);
951 GError
*error
= NULL
;
953 g_subprocess_wait_finish (subprocess
, result
, &error
);
954 g_assert_no_error (error
);
956 g_assert (g_subprocess_get_if_signaled (subprocess
));
957 g_assert (g_subprocess_get_term_sig (subprocess
) == 9);
959 g_spawn_check_exit_status (g_subprocess_get_status (subprocess
), &error
);
960 g_assert (error
!= NULL
);
961 g_clear_error (&error
);
963 g_main_loop_quit ((GMainLoop
*)user_data
);
967 test_terminate (void)
969 GError
*local_error
= NULL
;
970 GError
**error
= &local_error
;
975 args
= get_test_subprocess_args ("sleep-forever", NULL
);
976 proc
= g_subprocess_newv ((const gchar
* const *) args
->pdata
, G_SUBPROCESS_FLAGS_NONE
, error
);
977 g_ptr_array_free (args
, TRUE
);
978 g_assert_no_error (local_error
);
980 loop
= g_main_loop_new (NULL
, TRUE
);
982 g_subprocess_wait_async (proc
, NULL
, on_request_quit_exited
, loop
);
984 g_timeout_add_seconds (3, send_terminate
, proc
);
986 g_main_loop_run (loop
);
988 g_main_loop_unref (loop
);
989 g_object_unref (proc
);
994 send_signal (gpointer user_data
)
996 GSubprocess
*proc
= user_data
;
998 g_subprocess_send_signal (proc
, SIGKILL
);
1006 GError
*local_error
= NULL
;
1007 GError
**error
= &local_error
;
1012 args
= get_test_subprocess_args ("sleep-forever", NULL
);
1013 proc
= g_subprocess_newv ((const gchar
* const *) args
->pdata
, G_SUBPROCESS_FLAGS_NONE
, error
);
1014 g_ptr_array_free (args
, TRUE
);
1015 g_assert_no_error (local_error
);
1017 loop
= g_main_loop_new (NULL
, TRUE
);
1019 g_subprocess_wait_async (proc
, NULL
, on_request_quit_exited
, loop
);
1021 g_timeout_add_seconds (3, send_signal
, proc
);
1023 g_main_loop_run (loop
);
1025 g_main_loop_unref (loop
);
1026 g_object_unref (proc
);
1033 GError
*local_error
= NULL
;
1034 GError
**error
= &local_error
;
1035 GSubprocessLauncher
*launcher
;
1038 GInputStream
*stdout
;
1040 gchar
*envp
[] = { "ONE=1", "TWO=1", "THREE=3", "FOUR=1", NULL
};
1043 args
= get_test_subprocess_args ("env", NULL
);
1044 launcher
= g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_NONE
);
1045 g_subprocess_launcher_set_flags (launcher
, G_SUBPROCESS_FLAGS_STDOUT_PIPE
);
1046 g_subprocess_launcher_set_environ (launcher
, envp
);
1047 g_subprocess_launcher_setenv (launcher
, "TWO", "2", TRUE
);
1048 g_subprocess_launcher_setenv (launcher
, "THREE", "1", FALSE
);
1049 g_subprocess_launcher_unsetenv (launcher
, "FOUR");
1051 g_assert_null (g_subprocess_launcher_getenv (launcher
, "FOUR"));
1053 proc
= g_subprocess_launcher_spawn (launcher
, error
, args
->pdata
[0], "env", NULL
);
1054 g_ptr_array_free (args
, TRUE
);
1055 g_assert_no_error (local_error
);
1057 stdout
= g_subprocess_get_stdout_pipe (proc
);
1059 result
= splice_to_string (stdout
, error
);
1060 split
= g_strsplit (result
, "\n", -1);
1061 g_assert_cmpstr (g_environ_getenv (split
, "ONE"), ==, "1");
1062 g_assert_cmpstr (g_environ_getenv (split
, "TWO"), ==, "2");
1063 g_assert_cmpstr (g_environ_getenv (split
, "THREE"), ==, "3");
1064 g_assert_null (g_environ_getenv (split
, "FOUR"));
1068 g_object_unref (proc
);
1069 g_object_unref (launcher
);
1072 /* Test that explicitly inheriting and modifying the parent process’
1073 * environment works. */
1075 test_env_inherit (void)
1077 GError
*local_error
= NULL
;
1078 GError
**error
= &local_error
;
1079 GSubprocessLauncher
*launcher
;
1082 GInputStream
*stdout
;
1086 g_setenv ("TEST_ENV_INHERIT1", "1", TRUE
);
1087 g_setenv ("TEST_ENV_INHERIT2", "2", TRUE
);
1089 args
= get_test_subprocess_args ("env", NULL
);
1090 launcher
= g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_NONE
);
1091 g_subprocess_launcher_set_flags (launcher
, G_SUBPROCESS_FLAGS_STDOUT_PIPE
);
1092 g_subprocess_launcher_set_environ (launcher
, NULL
);
1093 g_subprocess_launcher_setenv (launcher
, "TWO", "2", TRUE
);
1094 g_subprocess_launcher_unsetenv (launcher
, "TEST_ENV_INHERIT1");
1096 g_assert_null (g_subprocess_launcher_getenv (launcher
, "TEST_ENV_INHERIT1"));
1097 g_assert_cmpstr (g_subprocess_launcher_getenv (launcher
, "TEST_ENV_INHERIT2"), ==, "2");
1098 g_assert_cmpstr (g_subprocess_launcher_getenv (launcher
, "TWO"), ==, "2");
1100 proc
= g_subprocess_launcher_spawn (launcher
, error
, args
->pdata
[0], "env", NULL
);
1101 g_ptr_array_free (args
, TRUE
);
1102 g_assert_no_error (local_error
);
1104 stdout
= g_subprocess_get_stdout_pipe (proc
);
1106 result
= splice_to_string (stdout
, error
);
1107 split
= g_strsplit (result
, "\n", -1);
1108 g_assert_null (g_environ_getenv (split
, "TEST_ENV_INHERIT1"));
1109 g_assert_cmpstr (g_environ_getenv (split
, "TEST_ENV_INHERIT2"), ==, "2");
1110 g_assert_cmpstr (g_environ_getenv (split
, "TWO"), ==, "2");
1114 g_object_unref (proc
);
1115 g_object_unref (launcher
);
1121 GError
*local_error
= NULL
;
1122 GError
**error
= &local_error
;
1123 GSubprocessLauncher
*launcher
;
1126 GInputStream
*stdout
;
1128 const char *basename
;
1130 args
= get_test_subprocess_args ("cwd", NULL
);
1131 launcher
= g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE
);
1132 g_subprocess_launcher_set_flags (launcher
, G_SUBPROCESS_FLAGS_STDOUT_PIPE
);
1133 g_subprocess_launcher_set_cwd (launcher
, "/tmp");
1135 proc
= g_subprocess_launcher_spawnv (launcher
, (const char * const *)args
->pdata
, error
);
1136 g_ptr_array_free (args
, TRUE
);
1137 g_assert_no_error (local_error
);
1139 stdout
= g_subprocess_get_stdout_pipe (proc
);
1141 result
= splice_to_string (stdout
, error
);
1143 basename
= g_strrstr (result
, "/");
1144 g_assert (basename
!= NULL
);
1145 g_assert_cmpstr (basename
, ==, "/tmp" LINEEND
);
1148 g_object_unref (proc
);
1149 g_object_unref (launcher
);
1153 test_stdout_file (void)
1155 GError
*local_error
= NULL
;
1156 GError
**error
= &local_error
;
1157 GSubprocessLauncher
*launcher
;
1161 GFileIOStream
*iostream
;
1162 GOutputStream
*stdin
;
1163 const char *test_data
= "this is some test data\n";
1165 char *tmp_file_path
;
1167 tmpfile
= g_file_new_tmp ("gsubprocessXXXXXX", &iostream
, error
);
1168 g_assert_no_error (local_error
);
1169 g_clear_object (&iostream
);
1171 tmp_file_path
= g_file_get_path (tmpfile
);
1173 args
= get_test_subprocess_args ("cat", NULL
);
1174 launcher
= g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDIN_PIPE
);
1175 g_subprocess_launcher_set_stdout_file_path (launcher
, tmp_file_path
);
1176 proc
= g_subprocess_launcher_spawnv (launcher
, (const gchar
* const *) args
->pdata
, error
);
1177 g_ptr_array_free (args
, TRUE
);
1178 g_assert_no_error (local_error
);
1180 stdin
= g_subprocess_get_stdin_pipe (proc
);
1182 g_output_stream_write_all (stdin
, test_data
, strlen (test_data
), NULL
, NULL
, error
);
1183 g_assert_no_error (local_error
);
1185 g_output_stream_close (stdin
, NULL
, error
);
1186 g_assert_no_error (local_error
);
1188 g_subprocess_wait_check (proc
, NULL
, error
);
1190 g_object_unref (launcher
);
1191 g_object_unref (proc
);
1193 g_file_load_contents (tmpfile
, NULL
, &tmp_contents
, NULL
, NULL
, error
);
1194 g_assert_no_error (local_error
);
1196 g_assert_cmpstr (test_data
, ==, tmp_contents
);
1197 g_free (tmp_contents
);
1199 (void) g_file_delete (tmpfile
, NULL
, NULL
);
1200 g_object_unref (tmpfile
);
1201 g_free (tmp_file_path
);
1205 test_stdout_fd (void)
1207 GError
*local_error
= NULL
;
1208 GError
**error
= &local_error
;
1209 GSubprocessLauncher
*launcher
;
1213 GFileIOStream
*iostream
;
1214 GFileDescriptorBased
*descriptor_stream
;
1215 GOutputStream
*stdin
;
1216 const char *test_data
= "this is some test data\n";
1219 tmpfile
= g_file_new_tmp ("gsubprocessXXXXXX", &iostream
, error
);
1220 g_assert_no_error (local_error
);
1222 args
= get_test_subprocess_args ("cat", NULL
);
1223 launcher
= g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDIN_PIPE
);
1224 descriptor_stream
= G_FILE_DESCRIPTOR_BASED (g_io_stream_get_output_stream (G_IO_STREAM (iostream
)));
1225 g_subprocess_launcher_take_stdout_fd (launcher
, dup (g_file_descriptor_based_get_fd (descriptor_stream
)));
1226 proc
= g_subprocess_launcher_spawnv (launcher
, (const gchar
* const *) args
->pdata
, error
);
1227 g_ptr_array_free (args
, TRUE
);
1228 g_assert_no_error (local_error
);
1230 g_clear_object (&iostream
);
1232 stdin
= g_subprocess_get_stdin_pipe (proc
);
1234 g_output_stream_write_all (stdin
, test_data
, strlen (test_data
), NULL
, NULL
, error
);
1235 g_assert_no_error (local_error
);
1237 g_output_stream_close (stdin
, NULL
, error
);
1238 g_assert_no_error (local_error
);
1240 g_subprocess_wait_check (proc
, NULL
, error
);
1242 g_object_unref (launcher
);
1243 g_object_unref (proc
);
1245 g_file_load_contents (tmpfile
, NULL
, &tmp_contents
, NULL
, NULL
, error
);
1246 g_assert_no_error (local_error
);
1248 g_assert_cmpstr (test_data
, ==, tmp_contents
);
1249 g_free (tmp_contents
);
1251 (void) g_file_delete (tmpfile
, NULL
, NULL
);
1252 g_object_unref (tmpfile
);
1256 child_setup (gpointer user_data
)
1258 dup2 (GPOINTER_TO_INT (user_data
), 1);
1262 test_child_setup (void)
1264 GError
*local_error
= NULL
;
1265 GError
**error
= &local_error
;
1266 GSubprocessLauncher
*launcher
;
1270 GFileIOStream
*iostream
;
1271 GOutputStream
*stdin
;
1272 const char *test_data
= "this is some test data\n";
1276 tmpfile
= g_file_new_tmp ("gsubprocessXXXXXX", &iostream
, error
);
1277 g_assert_no_error (local_error
);
1279 fd
= g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (g_io_stream_get_output_stream (G_IO_STREAM (iostream
))));
1281 args
= get_test_subprocess_args ("cat", NULL
);
1282 launcher
= g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDIN_PIPE
);
1283 g_subprocess_launcher_set_child_setup (launcher
, child_setup
, GINT_TO_POINTER (fd
), NULL
);
1284 proc
= g_subprocess_launcher_spawnv (launcher
, (const gchar
* const *) args
->pdata
, error
);
1285 g_ptr_array_free (args
, TRUE
);
1286 g_assert_no_error (local_error
);
1288 g_clear_object (&iostream
);
1290 stdin
= g_subprocess_get_stdin_pipe (proc
);
1292 g_output_stream_write_all (stdin
, test_data
, strlen (test_data
), NULL
, NULL
, error
);
1293 g_assert_no_error (local_error
);
1295 g_output_stream_close (stdin
, NULL
, error
);
1296 g_assert_no_error (local_error
);
1298 g_subprocess_wait_check (proc
, NULL
, error
);
1300 g_object_unref (launcher
);
1301 g_object_unref (proc
);
1303 g_file_load_contents (tmpfile
, NULL
, &tmp_contents
, NULL
, NULL
, error
);
1304 g_assert_no_error (local_error
);
1306 g_assert_cmpstr (test_data
, ==, tmp_contents
);
1307 g_free (tmp_contents
);
1309 (void) g_file_delete (tmpfile
, NULL
, NULL
);
1310 g_object_unref (tmpfile
);
1316 GError
*local_error
= NULL
;
1317 GError
**error
= &local_error
;
1318 GInputStream
*child_input
;
1319 GDataInputStream
*child_datainput
;
1320 GSubprocessLauncher
*launcher
;
1323 int basic_pipefds
[2];
1324 int needdup_pipefds
[2];
1328 char *needdup_fd_str
;
1330 g_unix_open_pipe (basic_pipefds
, FD_CLOEXEC
, error
);
1331 g_assert_no_error (local_error
);
1332 g_unix_open_pipe (needdup_pipefds
, FD_CLOEXEC
, error
);
1333 g_assert_no_error (local_error
);
1335 basic_fd_str
= g_strdup_printf ("%d", basic_pipefds
[1]);
1336 needdup_fd_str
= g_strdup_printf ("%d", needdup_pipefds
[1] + 1);
1338 args
= get_test_subprocess_args ("write-to-fds", basic_fd_str
, needdup_fd_str
, NULL
);
1339 launcher
= g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_NONE
);
1340 g_subprocess_launcher_take_fd (launcher
, basic_pipefds
[1], basic_pipefds
[1]);
1341 g_subprocess_launcher_take_fd (launcher
, needdup_pipefds
[1], needdup_pipefds
[1] + 1);
1342 proc
= g_subprocess_launcher_spawnv (launcher
, (const gchar
* const *) args
->pdata
, error
);
1343 g_ptr_array_free (args
, TRUE
);
1344 g_assert_no_error (local_error
);
1346 g_free (basic_fd_str
);
1347 g_free (needdup_fd_str
);
1349 child_input
= g_unix_input_stream_new (basic_pipefds
[0], TRUE
);
1350 child_datainput
= g_data_input_stream_new (child_input
);
1351 buf
= g_data_input_stream_read_line_utf8 (child_datainput
, &len
, NULL
, error
);
1352 g_assert_no_error (local_error
);
1353 g_assert_cmpstr (buf
, ==, "hello world");
1354 g_object_unref (child_datainput
);
1355 g_object_unref (child_input
);
1358 child_input
= g_unix_input_stream_new (needdup_pipefds
[0], TRUE
);
1359 child_datainput
= g_data_input_stream_new (child_input
);
1360 buf
= g_data_input_stream_read_line_utf8 (child_datainput
, &len
, NULL
, error
);
1361 g_assert_no_error (local_error
);
1362 g_assert_cmpstr (buf
, ==, "hello world");
1364 g_object_unref (child_datainput
);
1365 g_object_unref (child_input
);
1367 g_object_unref (launcher
);
1368 g_object_unref (proc
);
1374 test_launcher_environment (void)
1376 GSubprocessLauncher
*launcher
;
1377 GError
*error
= NULL
;
1382 g_setenv ("A", "B", TRUE
);
1383 g_setenv ("C", "D", TRUE
);
1385 launcher
= g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE
);
1387 /* unset a variable */
1388 g_subprocess_launcher_unsetenv (launcher
, "A");
1390 /* and set a diffferent one */
1391 g_subprocess_launcher_setenv (launcher
, "E", "F", TRUE
);
1393 args
= get_test_subprocess_args ("printenv", "A", "C", "E", NULL
);
1394 proc
= g_subprocess_launcher_spawnv (launcher
, (const gchar
**) args
->pdata
, &error
);
1395 g_assert_no_error (error
);
1398 g_subprocess_communicate_utf8 (proc
, NULL
, NULL
, &out
, NULL
, &error
);
1399 g_assert_no_error (error
);
1401 g_assert_cmpstr (out
, ==, "C=D\nE=F\n");
1404 g_object_unref (proc
);
1405 g_object_unref (launcher
);
1406 g_ptr_array_unref (args
);
1410 main (int argc
, char **argv
)
1412 g_test_init (&argc
, &argv
, NULL
);
1413 g_test_bug_base ("https://bugzilla.gnome.org/");
1415 g_test_add_func ("/gsubprocess/noop", test_noop
);
1416 g_test_add_func ("/gsubprocess/noop-all-to-null", test_noop_all_to_null
);
1417 g_test_add_func ("/gsubprocess/noop-no-wait", test_noop_no_wait
);
1418 g_test_add_func ("/gsubprocess/noop-stdin-inherit", test_noop_stdin_inherit
);
1420 g_test_add_func ("/gsubprocess/search-path", test_search_path
);
1421 g_test_add_func ("/gsubprocess/signal", test_signal
);
1423 g_test_add_func ("/gsubprocess/exit1", test_exit1
);
1424 g_test_add_func ("/gsubprocess/exit1/cancel", test_exit1_cancel
);
1425 g_test_add_func ("/gsubprocess/exit1/cancel_in_cb", test_exit1_cancel_in_cb
);
1426 g_test_add_func ("/gsubprocess/echo1", test_echo1
);
1428 g_test_add_func ("/gsubprocess/echo-merged", test_echo_merged
);
1430 g_test_add_func ("/gsubprocess/cat-utf8", test_cat_utf8
);
1431 g_test_add_func ("/gsubprocess/cat-eof", test_cat_eof
);
1432 g_test_add_func ("/gsubprocess/multi1", test_multi_1
);
1433 g_test_add_func ("/gsubprocess/communicate", test_communicate
);
1434 g_test_add_func ("/gsubprocess/communicate-async", test_communicate_async
);
1435 g_test_add_func ("/gsubprocess/communicate-utf8", test_communicate_utf8
);
1436 g_test_add_func ("/gsubprocess/communicate-utf8-async", test_communicate_utf8_async
);
1437 g_test_add_func ("/gsubprocess/communicate-utf8-invalid", test_communicate_utf8_invalid
);
1438 g_test_add_func ("/gsubprocess/communicate-nothing", test_communicate_nothing
);
1439 g_test_add_func ("/gsubprocess/terminate", test_terminate
);
1440 g_test_add_func ("/gsubprocess/env", test_env
);
1441 g_test_add_func ("/gsubprocess/env/inherit", test_env_inherit
);
1442 g_test_add_func ("/gsubprocess/cwd", test_cwd
);
1444 g_test_add_func ("/gsubprocess/stdout-file", test_stdout_file
);
1445 g_test_add_func ("/gsubprocess/stdout-fd", test_stdout_fd
);
1446 g_test_add_func ("/gsubprocess/child-setup", test_child_setup
);
1447 g_test_add_func ("/gsubprocess/pass-fd", test_pass_fd
);
1449 g_test_add_func ("/gsubprocess/launcher-environment", test_launcher_environment
);
1451 return g_test_run ();