1 /* Test harness for pipe-filter-ii.
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__); \
54 prepare_write (size_t *num_bytes_p, void *private_data)
56 struct locals *l = (struct locals *) private_data;
57 if (l->nwritten < l->size)
59 *num_bytes_p = l->size - l->nwritten;
60 return l->input + l->nwritten;
67 done_write (void *data_written, size_t num_bytes_written, void *private_data)
69 struct locals *l = (struct locals *) private_data;
70 l->nwritten += num_bytes_written;
74 prepare_read (size_t *num_bytes_p, void *private_data)
76 struct locals *l = (struct locals *) private_data;
77 *num_bytes_p = sizeof (l->buf);
81 /* Callback that ignores the data that has been read. */
84 ignore_done_read (void *data_read, size_t num_bytes_read, void *private_data)
88 /* Callback that outputs the data that has been read. */
91 output_done_read (void *data_read, size_t num_bytes_read, void *private_data)
93 full_write (STDOUT_FILENO, data_read, num_bytes_read);
97 main (int argc, char **argv)
99 const char *path[] = { NULL, NULL };
101 set_program_name (argv[0]);
105 /* Test writing to a nonexistent program traps sooner or later. */
114 path[0] = "/nonexistent/blah";
115 rc = pipe_filter_ii_execute ("pipe-filter-test", path[0], path, true, false,
116 prepare_write, done_write,
117 prepare_read, ignore_done_read,
119 ASSERT (rc == 127 || rc == -1);
120 printf ("Test 1 passed.\n");
124 /* Test returning the exit status. */
130 l.size = strlen (l.input);
134 rc = pipe_filter_ii_execute ("pipe-filter-test", path[0], path, false, false,
135 prepare_write, done_write,
136 prepare_read, ignore_done_read,
139 printf ("Test 2 passed.\n");
143 /* Now test asynchronous I/O. */
148 l.input = "1 50\n51\n100";
149 l.size = strlen (l.input);
153 rc = pipe_filter_ii_execute ("pipe-filter-test", path[0], path, false, true,
154 prepare_write, done_write,
155 prepare_read, output_done_read,
158 printf ("Test 3 passed.\n");