Merge commit '441aa3044f43e5572f58c354f01e6bc070acd5c7'
[gnulib.git] / tests / test-pipe-filter-ii2-main.c
1 /* Test harness for pipe-filter-ii.
2
3    Copyright (C) 2009 Free Software Foundation, Inc.
4    Written by Paolo Bonzini <bonzini@gnu.org>, 2009.
5
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.
10
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.
15
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/>.  */
18
19 #include <config.h>
20
21 #include "pipe-filter.h"
22
23 #include <stdio.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <signal.h>
28
29 #include "full-write.h"
30 #include "progname.h"
31
32 #define ASSERT(expr) \
33   do                                                                         \
34     {                                                                        \
35       if (!(expr))                                                           \
36         {                                                                    \
37           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
38           fflush (stderr);                                                   \
39           abort ();                                                          \
40         }                                                                    \
41     }                                                                        \
42   while (0)
43
44 struct locals
45 {
46   const char *input;
47   size_t size;
48   size_t nwritten;
49   size_t nread;
50   char buf[5];
51 };
52
53 static const void *
54 prepare_write (size_t *num_bytes_p, void *private_data)
55 {
56   struct locals *l = (struct locals *) private_data;
57   if (l->nwritten < l->size)
58     {
59       *num_bytes_p = l->size - l->nwritten;
60       return l->input + l->nwritten;
61     }
62   else
63     return NULL;
64 }
65
66 static void
67 done_write (void *data_written, size_t num_bytes_written, void *private_data)
68 {
69   struct locals *l = (struct locals *) private_data;
70   l->nwritten += num_bytes_written;
71 }
72
73 static void *
74 prepare_read (size_t *num_bytes_p, void *private_data)
75 {
76   struct locals *l = (struct locals *) private_data;
77   *num_bytes_p = sizeof (l->buf);
78   return l->buf;
79 }
80
81 /* Callback that ignores the data that has been read.  */
82
83 static void
84 ignore_done_read (void *data_read, size_t num_bytes_read, void *private_data)
85 {
86 }
87
88 /* Callback that outputs the data that has been read.  */
89
90 static void
91 output_done_read (void *data_read, size_t num_bytes_read, void *private_data)
92 {
93   full_write (STDOUT_FILENO, data_read, num_bytes_read);
94 }
95
96 int
97 main (int argc, char **argv)
98 {
99   const char *path[] = { NULL, NULL };
100
101   set_program_name (argv[0]);
102
103   ASSERT (argc == 2);
104
105   /* Test writing to a nonexistent program traps sooner or later.  */
106   {
107     struct locals l;
108     int rc;
109
110     l.input = "";
111     l.size = 1;
112     l.nwritten = 0;
113     l.nread = 0;
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,
118                                  &l);
119     ASSERT (rc == 127 || rc == -1);
120     printf ("Test 1 passed.\n");
121     fflush (stdout);
122   }
123
124   /* Test returning the exit status.  */
125   {
126     struct locals l;
127     int rc;
128
129     l.input = "1 -1";
130     l.size = strlen (l.input);
131     l.nwritten = 0;
132     l.nread = 0;
133     path[0] = argv[1];
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,
137                                  &l);
138     ASSERT (rc == 1);
139     printf ("Test 2 passed.\n");
140     fflush (stdout);
141   }
142
143   /* Now test asynchronous I/O.  */
144   {
145     struct locals l;
146     int rc;
147
148     l.input = "1 50\n51\n100";
149     l.size = strlen (l.input);
150     l.nwritten = 0;
151     l.nread = 0;
152     path[0] = argv[1];
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,
156                                  &l);
157     ASSERT (rc == 0);
158     printf ("Test 3 passed.\n");
159     fflush (stdout);
160   }
161
162   return 0;
163 }