1 /* Test harness for pipe-filter-ii.
2
3 Copyright (C) 2009-2020 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 <https://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 "macros.h"
31
32 struct locals
33 {
34 const char *input;
35 size_t size;
36 size_t nwritten;
37 size_t nread;
38 char buf[5];
39 };
40
41 static const void *
prepare_write(size_t * num_bytes_p,void * private_data)42 prepare_write (size_t *num_bytes_p, void *private_data)
43 {
44 struct locals *l = (struct locals *) private_data;
45 if (l->nwritten < l->size)
46 {
47 *num_bytes_p = l->size - l->nwritten;
48 return l->input + l->nwritten;
49 }
50 else
51 return NULL;
52 }
53
54 static void
done_write(void * data_written,size_t num_bytes_written,void * private_data)55 done_write (void *data_written, size_t num_bytes_written, void *private_data)
56 {
57 struct locals *l = (struct locals *) private_data;
58 l->nwritten += num_bytes_written;
59 }
60
61 static void *
prepare_read(size_t * num_bytes_p,void * private_data)62 prepare_read (size_t *num_bytes_p, void *private_data)
63 {
64 struct locals *l = (struct locals *) private_data;
65 *num_bytes_p = sizeof (l->buf);
66 return l->buf;
67 }
68
69 /* Callback that ignores the data that has been read. */
70
71 static void
ignore_done_read(void * data_read,size_t num_bytes_read,void * private_data)72 ignore_done_read (void *data_read, size_t num_bytes_read, void *private_data)
73 {
74 }
75
76 /* Callback that outputs the data that has been read. */
77
78 static void
output_done_read(void * data_read,size_t num_bytes_read,void * private_data)79 output_done_read (void *data_read, size_t num_bytes_read, void *private_data)
80 {
81 full_write (STDOUT_FILENO, data_read, num_bytes_read);
82 }
83
84 int
main(int argc,char ** argv)85 main (int argc, char **argv)
86 {
87 const char *path[] = { NULL, NULL };
88
89 ASSERT (argc == 2);
90
91 /* Test writing to a nonexistent program traps sooner or later. */
92 {
93 struct locals l;
94 int rc;
95
96 l.input = "";
97 l.size = 1;
98 l.nwritten = 0;
99 l.nread = 0;
100 path[0] = "/nonexistent/blah";
101 rc = pipe_filter_ii_execute ("pipe-filter-test", path[0], path, true, false,
102 prepare_write, done_write,
103 prepare_read, ignore_done_read,
104 &l);
105 ASSERT (rc == 127 || rc == -1);
106 printf ("Test 1 passed.\n");
107 fflush (stdout);
108 }
109
110 /* Test returning the exit status. */
111 {
112 struct locals l;
113 int rc;
114
115 l.input = "1 -1";
116 l.size = strlen (l.input);
117 l.nwritten = 0;
118 l.nread = 0;
119 path[0] = argv[1];
120 rc = pipe_filter_ii_execute ("pipe-filter-test", path[0], path, false, false,
121 prepare_write, done_write,
122 prepare_read, ignore_done_read,
123 &l);
124 ASSERT (rc == 1);
125 printf ("Test 2 passed.\n");
126 fflush (stdout);
127 }
128
129 /* Now test asynchronous I/O. */
130 {
131 struct locals l;
132 int rc;
133
134 l.input = "1 50\n51\n100";
135 l.size = strlen (l.input);
136 l.nwritten = 0;
137 l.nread = 0;
138 path[0] = argv[1];
139 rc = pipe_filter_ii_execute ("pipe-filter-test", path[0], path, false, true,
140 prepare_write, done_write,
141 prepare_read, output_done_read,
142 &l);
143 ASSERT (rc == 0);
144 printf ("Test 3 passed.\n");
145 fflush (stdout);
146 }
147
148 return 0;
149 }
150