1 /* Test harness for pipe-filter-gi.
3 Copyright (C) 2009 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 <http://www.gnu.org/licenses/>. */
21 #include "pipe-filter.h"
29 #include "full-write.h"
32 #define ASSERT(expr) \
37 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
55 static char static_buf[5];
58 prepare_read (size_t *num_bytes_p, void *private_data)
60 *num_bytes_p = sizeof (static_buf);
64 /* Callback that ignores the data that has been read. */
67 ignore_done_read (void *data_read, size_t num_bytes_read, void *private_data)
71 /* Callback that outputs the data that has been read. */
74 output_done_read (void *data_read, size_t num_bytes_read, void *private_data)
76 full_write (STDOUT_FILENO, data_read, num_bytes_read);
80 pipe_filter_gi_write_string (struct pipe_filter_gi *f, const char *string)
82 pipe_filter_gi_write (f, string, strlen (string));
86 main (int argc, char **argv)
88 struct pipe_filter_gi *f;
89 const char *path[] = { NULL, NULL };
91 set_program_name (argv[0]);
95 /* Test writing to a nonexistent program traps sooner or later. */
99 path[0] = "/nonexistent/blah";
100 f = pipe_filter_gi_create ("pipe-filter-test", path[0], path, true, false,
101 prepare_read, ignore_done_read, NULL);
103 rc = pipe_filter_gi_write (f, "", 1);
104 ASSERT (rc == 127 || rc == -1);
105 rc = pipe_filter_gi_close (f);
106 ASSERT (rc == 127 || rc == -1);
107 printf ("Test 1 passed.\n");
111 /* Test returning the exit status. */
114 f = pipe_filter_gi_create ("pipe-filter-test", path[0], path, false, false,
115 prepare_read, ignore_done_read, NULL);
116 pipe_filter_gi_write_string (f, "1 -1");
117 ASSERT (pipe_filter_gi_close (f) == 1);
118 printf ("Test 2 passed.\n");
122 /* Same, but test returning the status in pipe_filter_gi_write. */
127 f = pipe_filter_gi_create ("pipe-filter-test", path[0], path, false, false,
128 prepare_read, ignore_done_read, NULL);
129 pipe_filter_gi_write_string (f, "1 -1\n"); /* tell the child to terminate */
130 small_nap (); /* let the child terminate */
131 rc = pipe_filter_gi_write (f, " ", 1); /* write to a closed pipe */
132 ASSERT (rc == -1 && errno == EPIPE);
133 /* Closing the filter must report same error again, for consistency with
134 pipe_filter_ii_execute. The subprocess' exit status is not returned. */
135 rc = pipe_filter_gi_close (f);
136 ASSERT (rc == -1 && errno == EPIPE);
137 printf ("Test 3 passed.\n");
141 /* Now test asynchronous I/O. */
144 f = pipe_filter_gi_create ("pipe-filter-test", path[0], path, false, true,
145 prepare_read, output_done_read, NULL);
146 pipe_filter_gi_write_string (f, "1 50\n");
147 pipe_filter_gi_write_string (f, "51\n");
148 pipe_filter_gi_write_string (f, "100");
149 ASSERT (pipe_filter_gi_close (f) == 0);
150 printf ("Test 4 passed.\n");