Use spaces for indentation, not tabs.
[gnulib.git] / tests / test-pipe-filter-gi2-main.c
1 /* Test harness for pipe-filter-gi.
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 /* 0.1 sec pause */
45 static void
46 small_nap (void)
47 {
48   usleep (100000);
49 }
50
51 static char static_buf[5];
52
53 static void *
54 prepare_read (size_t *num_bytes_p, void *private_data)
55 {
56   *num_bytes_p = sizeof (static_buf);
57   return static_buf;
58 }
59
60 /* Callback that ignores the data that has been read.  */
61
62 static void
63 ignore_done_read (void *data_read, size_t num_bytes_read, void *private_data)
64 {
65 }
66
67 /* Callback that outputs the data that has been read.  */
68
69 static void
70 output_done_read (void *data_read, size_t num_bytes_read, void *private_data)
71 {
72   full_write (STDOUT_FILENO, data_read, num_bytes_read);
73 }
74
75 static void
76 pipe_filter_gi_write_string (struct pipe_filter_gi *f, const char *string)
77 {
78   pipe_filter_gi_write (f, string, strlen (string));
79 }
80
81 int
82 main (int argc, char **argv)
83 {
84   struct pipe_filter_gi *f;
85   const char *path[] = { NULL, NULL };
86
87   set_program_name (argv[0]);
88
89   ASSERT (argc == 2);
90
91   /* Test writing to a nonexistent program traps sooner or later.  */
92   {
93     int rc;
94
95     path[0] = "/nonexistent/blah";
96     f = pipe_filter_gi_create ("pipe-filter-test", path[0], path, true, false,
97                                prepare_read, ignore_done_read, NULL);
98     small_nap ();
99     rc = pipe_filter_gi_write (f, "", 1);
100     ASSERT (rc == 127 || rc == -1);
101     rc = pipe_filter_gi_close (f);
102     ASSERT (rc == 127 || rc == -1);
103     printf ("Test 1 passed.\n");
104     fflush (stdout);
105   }
106
107   /* Test returning the exit status.  */
108   {
109     path[0] = argv[1];
110     f = pipe_filter_gi_create ("pipe-filter-test", path[0], path, false, false,
111                                prepare_read, ignore_done_read, NULL);
112     pipe_filter_gi_write_string (f, "1 -1");
113     ASSERT (pipe_filter_gi_close (f) == 1);
114     printf ("Test 2 passed.\n");
115     fflush (stdout);
116   }
117
118   /* Same, but test returning the status in pipe_filter_gi_write.  */
119   {
120     int rc;
121
122     path[0] = argv[1];
123     f = pipe_filter_gi_create ("pipe-filter-test", path[0], path, false, false,
124                                prepare_read, ignore_done_read, NULL);
125     pipe_filter_gi_write_string (f, "1 -1\n"); /* tell the child to terminate */
126     small_nap (); /* let the child terminate */
127     rc = pipe_filter_gi_write (f, " ", 1); /* write to a closed pipe */
128     ASSERT (rc == -1 && errno == EPIPE);
129     /* Closing the filter must report same error again, for consistency with
130        pipe_filter_ii_execute.  The subprocess' exit status is not returned.  */
131     rc = pipe_filter_gi_close (f);
132     ASSERT (rc == -1 && errno == EPIPE);
133     printf ("Test 3 passed.\n");
134     fflush (stdout);
135   }
136
137   /* Now test asynchronous I/O.  */
138   {
139     path[0] = argv[1];
140     f = pipe_filter_gi_create ("pipe-filter-test", path[0], path, false, true,
141                                prepare_read, output_done_read, NULL);
142     pipe_filter_gi_write_string (f, "1 50\n");
143     pipe_filter_gi_write_string (f, "51\n");
144     pipe_filter_gi_write_string (f, "100");
145     ASSERT (pipe_filter_gi_close (f) == 0);
146     printf ("Test 4 passed.\n");
147     fflush (stdout);
148   }
149
150   return 0;
151 }