maint.mk: Update system header list for #include syntax checks.
[gnulib.git] / tests / test-pipe-filter-gi2-main.c
blobff8966a3fcf4f4c235b8b0d1fb9d63ec6000906e
1 /* Test harness for pipe-filter-gi.
3 Copyright (C) 2009-2024 Free Software Foundation, Inc.
4 Written by Paolo Bonzini <bonzini@gnu.org>, 2009.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 #include <config.h>
21 #include "pipe-filter.h"
23 #include <stdio.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <signal.h>
29 #include "binary-io.h"
30 #include "full-write.h"
31 #include "macros.h"
33 /* 0.1 sec pause */
34 static void
35 small_nap (void)
37 usleep (100000);
40 static char static_buf[5];
42 static void *
43 prepare_read (size_t *num_bytes_p, void *private_data)
45 *num_bytes_p = sizeof (static_buf);
46 return static_buf;
49 /* Callback that ignores the data that has been read. */
51 static void
52 ignore_done_read (void *data_read, size_t num_bytes_read, void *private_data)
56 /* Callback that outputs the data that has been read. */
58 static void
59 output_done_read (void *data_read, size_t num_bytes_read, void *private_data)
61 full_write (STDOUT_FILENO, data_read, num_bytes_read);
64 static void
65 pipe_filter_gi_write_string (struct pipe_filter_gi *f, const char *string)
67 pipe_filter_gi_write (f, string, strlen (string));
70 int
71 main (int argc, char **argv)
73 struct pipe_filter_gi *f;
74 const char *path[] = { NULL, NULL };
76 ASSERT (argc == 2);
78 set_binary_mode (STDOUT_FILENO, O_BINARY);
80 /* Test writing to a nonexistent program traps sooner or later. */
82 int rc;
84 path[0] = "/nonexistent/blah";
85 f = pipe_filter_gi_create ("pipe-filter-test", path[0], path, true, false,
86 prepare_read, ignore_done_read, NULL);
87 small_nap ();
88 rc = pipe_filter_gi_write (f, "", 1);
89 ASSERT (rc == 127 || rc == -1);
90 rc = pipe_filter_gi_close (f);
91 ASSERT (rc == 127 || rc == -1);
92 printf ("Test 1 passed.\n");
93 fflush (stdout);
96 /* Test returning the exit status. */
98 path[0] = argv[1];
99 f = pipe_filter_gi_create ("pipe-filter-test", path[0], path, false, false,
100 prepare_read, ignore_done_read, NULL);
101 pipe_filter_gi_write_string (f, "1 -1");
102 ASSERT (pipe_filter_gi_close (f) == 1);
103 printf ("Test 2 passed.\n");
104 fflush (stdout);
107 /* Same, but test returning the status in pipe_filter_gi_write. */
109 int rc;
111 path[0] = argv[1];
112 f = pipe_filter_gi_create ("pipe-filter-test", path[0], path, false, false,
113 prepare_read, ignore_done_read, NULL);
114 pipe_filter_gi_write_string (f, "1 -1\n"); /* tell the child to terminate */
115 small_nap (); /* let the child terminate */
116 rc = pipe_filter_gi_write (f, " ", 1); /* write to a closed pipe */
117 ASSERT (rc == -1 && errno == EPIPE);
118 /* Closing the filter must report same error again, for consistency with
119 pipe_filter_ii_execute. The subprocess' exit status is not returned. */
120 rc = pipe_filter_gi_close (f);
121 ASSERT (rc == -1 && errno == EPIPE);
122 printf ("Test 3 passed.\n");
123 fflush (stdout);
126 /* Now test asynchronous I/O. */
128 path[0] = argv[1];
129 f = pipe_filter_gi_create ("pipe-filter-test", path[0], path, false, true,
130 prepare_read, output_done_read, NULL);
131 pipe_filter_gi_write_string (f, "1 50\n");
132 pipe_filter_gi_write_string (f, "51\n");
133 pipe_filter_gi_write_string (f, "100");
134 ASSERT (pipe_filter_gi_close (f) == 0);
135 printf ("Test 4 passed.\n");
136 fflush (stdout);
139 return test_exit_status;