summaryrefslogtreecommitdiffabout
path: root/tests
authorSergey Poznyakoff <gray@gnu.org>2019-06-12 16:23:39 (GMT)
committer Sergey Poznyakoff <gray@gnu.org>2019-06-12 16:29:45 (GMT)
commit85563afeac954a50de7e4c207f43b57a4e63e474 (patch) (unidiff)
tree967054eb92014811e067f1d56c99e12c268f1824 /tests
parentf5c72b5e74ea7aaf1375f763f977e3249c6b7fc4 (diff)
downloadpies-85563afeac954a50de7e4c207f43b57a4e63e474.tar.gz
pies-85563afeac954a50de7e4c207f43b57a4e63e474.tar.bz2
More tests for built-in services
* tests/builtin.at: Check tcpmux services. * tests/nt.c: Add tcpmux support, improve error checking. * tests/chargen.c: Improve error checking.
Diffstat (limited to 'tests') (more/less context) (ignore whitespace changes)
-rw-r--r--tests/builtin.at77
-rw-r--r--tests/chargen.c11
-rw-r--r--tests/nt.c145
3 files changed, 211 insertions, 22 deletions
diff --git a/tests/builtin.at b/tests/builtin.at
index 40f65fa..893f213 100644
--- a/tests/builtin.at
+++ b/tests/builtin.at
@@ -129,3 +129,80 @@ to the input.
129 129
130m4_popdef([IT_BUILTIN]) 130m4_popdef([IT_BUILTIN])
131m4_popdef([IT_SPECIAL]) 131m4_popdef([IT_SPECIAL])
132
133# IT_TCPMUX([KW],[ID],[INPUT],[OUTPUT])
134m4_define([IT_TCPMUX],
135[AT_SETUP([tcpmux: $1])
136AT_KEYWORDS([inetd builtin internal tcpmux $1])
137AT_CHECK([
138PIES_XFAIL_CHECK
139PIES_CONTROL_INIT
140: ${PIES_TEST_INET_SOCKET:=unix://$PWD/in.sock}
141m4_if([$3],[],[],[AT_DATA([input],[$3])])
142cat > pies.conf <<_EOT
143component master {
144 socket "$PIES_TEST_INET_SOCKET";
145 service tcpmux;
146 flags internal;
147}
148component one {
149 service one;
150 flags (tcpmuxplus);
151 tcpmux-master master;
152 command "$auxdir/in.test $PWD/one.log";
153 stderr file "$PWD/one.err";
154}
155component two {
156 service two;
157 flags (tcpmuxplus);
158 tcpmux-master master;
159 command "$auxdir/in.test $PWD/two.log";
160 stderr file "$PWD/two.err";
161}
162component test {
163 command "nt -t [$2]m4_if([$3],[],,[ -i $PWD/input]) -o $PWD/test.out '$PIES_TEST_INET_SOCKET'";
164 stderr file "$PWD/test.err";
165 return-code * {
166 action disable;
167 exec "piesctl --no-netrc --url=$PIES_CTLSOCK shutdown";
168 }
169}
170_EOT
171
172set -e
173to 10 \
174 pies --foreground --stderr \
175 --config-file control.conf --config-file pies.conf --debug 1 2>errlog
176
177for err in one.err two.err test.err
178do
179 if test -s $err; then
180 echo "$err:"
181 cat $err
182 fi
183done >&2
184
185if test -f test.out; then
186 cat test.out | tr -d '\r'
187fi
188],
189[0],
190[$4])
191AT_CLEANUP
192])
193
194IT_TCPMUX([help],[help],[],
195[one
196two
197])
198
199IT_TCPMUX([service],[one],
200[Test one
201quit
202],
203[OK Test one
204])
205
206m4_popdef([IT_TCPMUX])
207
208
diff --git a/tests/chargen.c b/tests/chargen.c
index a3b3290..75d096c 100644
--- a/tests/chargen.c
+++ b/tests/chargen.c
@@ -76,13 +76,20 @@ main (int argc, char **argv)
76 76
77 if (pies_url_create (&url, argv[1])) 77 if (pies_url_create (&url, argv[1]))
78 { 78 {
79 perror (argv[0]); 79 perror (argv[1]);
80 return 64; 80 return 64;
81 } 81 }
82 82
83 fd = url_connect (url, NULL); 83 fd = url_connect (url, NULL);
84 if (fd == -1)
85 return 1;
84 fp = fdopen (fd, "r"); 86 fp = fdopen (fd, "r");
85 87 if (!fp)
88 {
89 perror ("fdopen");
90 return 1;
91 }
92
86 first = next_char (0); 93 first = next_char (0);
87 c = first; 94 c = first;
88 do 95 do
diff --git a/tests/nt.c b/tests/nt.c
index 876ffb1..c7f4be7 100644
--- a/tests/nt.c
+++ b/tests/nt.c
@@ -136,7 +136,7 @@ netcat_stream_read (netcat_server_t *srv)
136 if (errno == EINTR) 136 if (errno == EINTR)
137 return 0; 137 return 0;
138 grecs_error (NULL, errno, "%s: read", srv->id); 138 grecs_error (NULL, errno, "%s: read", srv->id);
139 srv->pollfd->events &= ~POLLIN; 139 netcat_stream_disconnect (srv, POLLIN);
140 return -1; 140 return -1;
141 } 141 }
142 if (n == 0 || !peer_is_state (srv, POLLOUT)) 142 if (n == 0 || !peer_is_state (srv, POLLOUT))
@@ -185,11 +185,12 @@ netcat_stream_write (netcat_server_t *srv)
185 if (errno == EINTR) 185 if (errno == EINTR)
186 return 0; 186 return 0;
187 grecs_error (NULL, errno, "%s: write", srv->id); 187 grecs_error (NULL, errno, "%s: write", srv->id);
188 netcat_stream_disconnect (srv, POLLOUT);
188 return -1; 189 return -1;
189 } 190 }
190 if (n == 0) 191 if (n == 0)
191 { 192 {
192 // FIXME: eof 193 netcat_stream_disconnect (srv, POLLOUT);
193 return -1; 194 return -1;
194 } 195 }
195 return 0; 196 return 0;
@@ -220,8 +221,99 @@ disconnect_stdout (netcat_server_t *srv)
220 exit (0); 221 exit (0);
221} 222}
222 223
224void
225fd_write (int fd, char const *str, size_t len)
226{
227 while (len)
228 {
229 ssize_t n = write (fd, str, len);
230 if (n == -1)
231 {
232 perror ("socket write");
233 exit (1);
234 }
235 if (n == 0)
236 {
237 fprintf (stderr, "zero write\n");
238 exit (1);
239 }
240 len -= n;
241 str += n;
242 }
243}
244
245void
246fd_writeln (int fd, char const *str)
247{
248 fd_write (fd, str, strlen (str));
249 fd_write (fd, "\r\n", 2);
250}
251
252int
253fd_getc (int fd)
254{
255 char c;
256 ssize_t n = read (fd, &c, 1);
257 if (n == -1)
258 {
259 perror ("socket read");
260 exit (1);
261 }
262 if (n == 0)
263 c = EOF;
264 return c;
265}
266
267static void
268tcpmux_init (int fd, char const *service)
269{
270 int c;
271
272 fd_writeln (fd, service);
273
274 if (strcmp (service, "help") == 0)
275 {
276 while ((c = fd_getc (fd)) != EOF)
277 {
278 fputc (c, stdout);
279 }
280 close (fd);
281 exit (0);
282 }
283
284 c = fd_getc (fd);
285 if (c == 0)
286 {
287 fprintf (stderr, "socket read: unexpected eof\n");
288 exit (1);
289 }
290 if (c == '+')
291 {
292 while ((c = fd_getc (fd)) != '\n')
293 {
294 if (c == EOF)
295 {
296 fprintf (stderr, "socket read: unexpected eof\n");
297 exit (1);
298 }
299 }
300 }
301 else
302 {
303 fprintf (stderr, "service rejected: ");
304 do
305 {
306 if (c != '\r')
307 fputc (c, stderr);
308 }
309 while ((c = fd_getc (fd)) != 0 && c != '\n');
310 fputc ('\n', stderr);
311 exit (1);
312 }
313}
314
223static int 315static int
224netcat (char const *urlstr) 316netcat (char const *urlstr, char const *tcpmux_service)
225{ 317{
226 int fd; 318 int fd;
227 struct pies_url *url; 319 struct pies_url *url;
@@ -239,6 +331,9 @@ netcat (char const *urlstr)
239 if (fd == -1) 331 if (fd == -1)
240 return 1; 332 return 1;
241 333
334 if (tcpmux_service)
335 tcpmux_init (fd, tcpmux_service);
336
242 pfd[0].fd = 0; 337 pfd[0].fd = 0;
243 pfd[0].events = 0; 338 pfd[0].events = 0;
244 srvin = netcat_server_create ("stdin", &pfd[0], POLLIN, disconnect_stdin, NULL); 339 srvin = netcat_server_create ("stdin", &pfd[0], POLLIN, disconnect_stdin, NULL);
@@ -265,25 +360,26 @@ netcat (char const *urlstr)
265 for (srv = server_head; srv; ) 360 for (srv = server_head; srv; )
266 { 361 {
267 netcat_server_t *next = srv->next; 362 netcat_server_t *next = srv->next;
268 int events;
269 363
270 events = (srv->pollfd->events|POLLHUP) 364 if ((srv->pollfd->revents & srv->state) & POLLIN)
271 & (srv->pollfd->revents 365 netcat_stream_read (srv);
272 & (srv->state | ((srv->state & POLLOUT) ? POLLHUP : 0))); 366 if ((srv->pollfd->revents & srv->state) & POLLOUT)
273 if (events) 367 netcat_stream_write (srv);
368 if ((srv->state & POLLOUT) && (srv->pollfd->revents & POLLHUP))
274 { 369 {
275 if (events & POLLIN) 370 //grecs_error (NULL, 0, "HUP on %s", srv->id);
276 netcat_stream_read (srv); 371 netcat_stream_disconnect (srv, srv->state);
277 if (events & POLLOUT) 372 }
278 netcat_stream_write (srv); 373 if (srv->state == 0 || srv->pollfd->fd == -1)
279 if (events & POLLHUP) 374 {
375 netcat_server_t *peer = srv->peer;
376 if (peer && peer->pollfd->events == 0)
280 { 377 {
281 //grecs_error (NULL, 0, "HUP on %s", srv->id); 378 netcat_stream_disconnect (peer, peer->state);
282 netcat_stream_disconnect (srv, srv->state); 379 netcat_server_remove (peer);
283 } 380 }
381 netcat_server_remove (srv);
284 } 382 }
285 if (srv->state == 0 || srv->pollfd->fd == -1)
286 netcat_server_remove (srv);
287 srv = next; 383 srv = next;
288 } 384 }
289 } 385 }
@@ -311,17 +407,22 @@ redirect (int sfd, char const *name)
311static void 407static void
312usage (FILE *fp) 408usage (FILE *fp)
313{ 409{
314 fprintf (fp, "usage: nt [-i IFILE] [-o OFILE] URL\n"); 410 fprintf (fp, "usage: nt [-i IFILE] [-o OFILE] [-t SERVICE] URL\n");
315 fprintf (fp, "Reads data from stdin (or IFILE) and sends them to URL.\n"); 411 fprintf (fp, "Reads data from stdin (or IFILE) and sends them to URL.\n");
316 fprintf (fp, "Reads replies from URL and sends them to stdout (or OFILE).\n"); 412 fprintf (fp, "Reads replies from URL and sends them to stdout (or OFILE).\n");
413 fprintf (fp, "\nOPTIONS\n\n");
414 fprintf (fp, " -t SERVICE use TCPMUX service\n");
415 fprintf (fp, " -i IFILE read input from IFILE\n");
416 fprintf (fp, " -o OFILE write output to OFILE\n");
317} 417}
318 418
319int 419int
320main (int argc, char **argv) 420main (int argc, char **argv)
321{ 421{
322 int c; 422 int c;
423 char const *tcpmux_service = NULL;
323 424
324 while ((c = getopt (argc, argv, "i:o:")) != EOF) 425 while ((c = getopt (argc, argv, "i:o:t:")) != EOF)
325 { 426 {
326 switch (c) 427 switch (c)
327 { 428 {
@@ -333,6 +434,10 @@ main (int argc, char **argv)
333 redirect (1, optarg); 434 redirect (1, optarg);
334 break; 435 break;
335 436
437 case 't':
438 tcpmux_service = optarg;
439 break;
440
336 default: 441 default:
337 exit (64); 442 exit (64);
338 } 443 }
@@ -347,5 +452,5 @@ main (int argc, char **argv)
347 exit (64); 452 exit (64);
348 } 453 }
349 454
350 return netcat (argv[0]); 455 return netcat (argv[0], tcpmux_service);
351} 456}

Return to:

Send suggestions and report system problems to the System administrator.