summaryrefslogtreecommitdiffabout
path: root/tests
authorSergey Poznyakoff <gray@gnu.org>2019-06-11 14:30:25 (GMT)
committer Sergey Poznyakoff <gray@gnu.org>2019-06-11 14:30:25 (GMT)
commit07e7ee3d732b60e0b7f5b242bbfb0cdec8e99e7f (patch) (unidiff)
treed8106ba7d4e4e92f4bb4f49427eb6e2db10897a3 /tests
parentbf6506cdc46446eada5090a428c2407ebd17468a (diff)
downloadpies-07e7ee3d732b60e0b7f5b242bbfb0cdec8e99e7f.tar.gz
pies-07e7ee3d732b60e0b7f5b242bbfb0cdec8e99e7f.tar.bz2
Test pass-fd components
* tests/recvfd.c: New source. * tests/.gitignore: Update. * tests/passfd.at: New test. * tests/Makefile.am: Add new test. * tests/testsuite.at: Add new test. * tests/nt.c: Check all revents bits.
Diffstat (limited to 'tests') (more/less context) (ignore whitespace changes)
-rw-r--r--tests/.gitignore1
-rw-r--r--tests/Makefile.am3
-rw-r--r--tests/nt.c10
-rw-r--r--tests/passfd.at57
-rw-r--r--tests/recvfd.c223
-rw-r--r--tests/testsuite.at1
-rw-r--r--tests/to.c16
7 files changed, 305 insertions, 6 deletions
diff --git a/tests/.gitignore b/tests/.gitignore
index 68159e3..7d2613a 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -8,3 +8,4 @@ testsuite.dir
8testsuite.log 8testsuite.log
9to 9to
10nt 10nt
11recvfd
diff --git a/tests/Makefile.am b/tests/Makefile.am
index fa1d6be..1fe78dc 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -55,6 +55,7 @@ TESTSUITE_AT = \
55 envop.at\ 55 envop.at\
56 inet.at\ 56 inet.at\
57 maxinst.at\ 57 maxinst.at\
58 passfd.at\
58 respawn.at\ 59 respawn.at\
59 redirect.at\ 60 redirect.at\
60 ret-exec.at\ 61 ret-exec.at\
@@ -67,7 +68,7 @@ TESTSUITE_AT = \
67TESTSUITE = $(srcdir)/testsuite 68TESTSUITE = $(srcdir)/testsuite
68M4=m4 69M4=m4
69 70
70noinst_PROGRAMS = envtest to lines nt 71noinst_PROGRAMS = envtest to lines nt recvfd
71nt_SOURCES = nt.c iobuf.h 72nt_SOURCES = nt.c iobuf.h
72AM_CPPFLAGS = \ 73AM_CPPFLAGS = \
73 -I$(top_srcdir)/lib\ 74 -I$(top_srcdir)/lib\
diff --git a/tests/nt.c b/tests/nt.c
index d30e447..98a4077 100644
--- a/tests/nt.c
+++ b/tests/nt.c
@@ -1,4 +1,4 @@
1/* This file is part of GNU Pies. 1/* This file is part of GNU Pies testsuite.
2 Copyright (C) 2019 Sergey Poznyakoff 2 Copyright (C) 2019 Sergey Poznyakoff
3 3
4 GNU Pies is free software; you can redistribute it and/or modify 4 GNU Pies is free software; you can redistribute it and/or modify
@@ -272,15 +272,15 @@ netcat (char const *urlstr)
272 & (srv->state | ((srv->state & POLLOUT) ? POLLHUP : 0))); 272 & (srv->state | ((srv->state & POLLOUT) ? POLLHUP : 0)));
273 if (events) 273 if (events)
274 { 274 {
275 if (events & POLLIN)
276 netcat_stream_read (srv);
277 if (events & POLLOUT)
278 netcat_stream_write (srv);
275 if (events & POLLHUP) 279 if (events & POLLHUP)
276 { 280 {
277 //grecs_error (NULL, 0, "HUP on %s", srv->id); 281 //grecs_error (NULL, 0, "HUP on %s", srv->id);
278 netcat_stream_disconnect (srv, srv->state); 282 netcat_stream_disconnect (srv, srv->state);
279 } 283 }
280 else if (events & POLLIN)
281 netcat_stream_read (srv);
282 else if (events & POLLOUT)
283 netcat_stream_write (srv);
284 } 284 }
285 if (srv->state == 0 || srv->pollfd->fd == -1) 285 if (srv->state == 0 || srv->pollfd->fd == -1)
286 netcat_server_remove (srv); 286 netcat_server_remove (srv);
diff --git a/tests/passfd.at b/tests/passfd.at
new file mode 100644
index 0000000..4a685a3
--- a/dev/null
+++ b/tests/passfd.at
@@ -0,0 +1,57 @@
1# This file is part of GNU pies testsuite. -*- Autotest -*-
2# Copyright (C) 2016-2019 Sergey Poznyakoff
3#
4# GNU pies is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 3, or (at your option)
7# any later version.
8#
9# GNU pies is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with GNU pies. If not, see <http://www.gnu.org/licenses/>.
16AT_SETUP([pass-fd component])
17AT_CHECK([
18PIES_XFAIL_CHECK
19PIES_CONTROL_INIT
20
21AT_DATA([input],
22[now is
23the time
24stop
25])
26
27: ${PIES_TEST_INET_SOCKET:=unix://$PWD/in.sock}
28PIES_FD_SOCKET=$PWD/pfd.sock
29
30cat > pies.conf <<_EOT
31component pfd {
32 command "recvfd '$PIES_FD_SOCKET' $auxdir/in.test $PWD/inlog";
33 mode pass-fd;
34 pass-fd-timeout 3;
35 pass-fd-socket "$PIES_FD_SOCKET";
36 socket "$PIES_TEST_INET_SOCKET";
37 stderr file "$PWD/log.err";
38}
39component controller {
40 command "nt $PIES_TEST_INET_SOCKET -i input";
41}
42_EOT
43
44set -e
45to 10 \
46 pies --foreground --stderr \
47 --config-file control.conf --config-file pies.conf --debug 1 2>errlog
48
49cat inlog
50cat log.err >&2
51],
52[0],
53[now is
54the time
55stop
56])
57AT_CLEANUP \ No newline at end of file
diff --git a/tests/recvfd.c b/tests/recvfd.c
new file mode 100644
index 0000000..82455c7
--- a/dev/null
+++ b/tests/recvfd.c
@@ -0,0 +1,223 @@
1/* This file is part of GNU Pies testsuite.
2 Copyright (C) 2019 Sergey Poznyakoff
3
4 GNU Pies is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 GNU Pies is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GNU Pies. If not, see <http://www.gnu.org/licenses/>. */
16
17#include <config.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <unistd.h>
21#include <errno.h>
22#include <sys/types.h>
23#include <sys/socket.h>
24#include <sys/un.h>
25#include <sys/stat.h>
26#include <sys/wait.h>
27#include <signal.h>
28#include "libpies.h"
29
30char const *progname;
31
32void
33usage (void)
34{
35 fprintf (stderr, "usage: %s SOCKET COMMAND ARGS...\n", progname);
36 fprintf (stderr, "Test tool for pass-fd pies components.\n");
37 fprintf (stderr, "Listens on the file descriptor obtained from SOCKET.\n");
38 fprintf (stderr, "For each connection, execs COMMAND ARGS as a separate process.\n");
39 exit (64);
40}
41
42static int
43listen_socket (char const *socket_name)
44{
45 struct sockaddr_un addr;
46 int sockfd;
47
48 if (strlen (socket_name) > sizeof addr.sun_path)
49 {
50 fprintf (stderr, "%s: UNIX socket name too long\n", progname);
51 return -1;
52 }
53 addr.sun_family = AF_UNIX;
54 strcpy (addr.sun_path, socket_name);
55
56 sockfd = socket (PF_UNIX, SOCK_STREAM, 0);
57 if (sockfd == -1)
58 {
59 perror ("socket");
60 exit (1);
61 }
62
63 umask (0117);
64 if (bind (sockfd, (struct sockaddr *) &addr, sizeof (addr)) < 0)
65 {
66 perror ("bind");
67 exit (1);
68 }
69
70 if (listen (sockfd, 8) < 0)
71 {
72 perror ("listen");
73 exit (1);
74 }
75 return sockfd;
76}
77
78static int
79read_fd (int fd)
80{
81 struct msghdr msg;
82 struct iovec iov[1];
83 char base[1];
84
85#if HAVE_STRUCT_MSGHDR_MSG_CONTROL
86 union
87 {
88 struct cmsghdr cm;
89 char control[CMSG_SPACE (sizeof (int))];
90 } control_un;
91 struct cmsghdr *cmptr;
92
93 msg.msg_control = control_un.control;
94 msg.msg_controllen = sizeof (control_un.control);
95#elif HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
96 int newfd;
97
98 msg.msg_accrights = (caddr_t) &newfd;
99 msg.msg_accrightslen = sizeof (int);
100#else
101 fprintf (stderr, "no way to get fd\n");
102 exit (77);
103#endif
104
105 msg.msg_name = NULL;
106 msg.msg_namelen = 0;
107
108 iov[0].iov_base = base;
109 iov[0].iov_len = sizeof (base);
110
111 msg.msg_iov = iov;
112 msg.msg_iovlen = 1;
113
114 if (recvmsg (fd, &msg, 0) > 0)
115 {
116#if HAVE_STRUCT_MSGHDR_MSG_CONTROL
117 if ((cmptr = CMSG_FIRSTHDR (&msg)) != NULL
118 && cmptr->cmsg_len == CMSG_LEN (sizeof (int))
119 && cmptr->cmsg_level == SOL_SOCKET
120 && cmptr->cmsg_type == SCM_RIGHTS)
121 return *((int*) CMSG_DATA (cmptr));
122#elif HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
123 if (msg.msg_accrightslen == sizeof (int))
124 return newfd;
125#endif
126 }
127 return -1;
128}
129
130static int
131get_fd (int lfd)
132{
133 int sfd, fd = accept (lfd, NULL, NULL);
134 if (fd == -1)
135 {
136 perror ("accept");
137 exit (1);
138 }
139
140 sfd = read_fd (fd);
141 close (fd);
142 return sfd;
143}
144
145static void
146sigchld (int sig)
147{
148 pid_t pid;
149
150 while ((pid = waitpid ((pid_t)-1, NULL, WNOHANG)) >= 0)
151 ;
152 signal (sig, sigchld);
153}
154
155static void
156sigquit (int sig)
157{
158 kill (0, sig);
159 exit (0);
160}
161
162int
163main (int argc, char **argv)
164{
165 int sfd, fd;
166
167 progname = argv[0];
168
169 if (argc < 3)
170 usage ();
171
172 sfd = listen_socket (argv[1]);
173
174 argc -= 2;
175 argv += 2;
176
177 fd = get_fd (sfd);
178 close (sfd);
179
180 signal (SIGCHLD, sigchld);
181 signal (SIGTERM, sigquit);
182 signal (SIGHUP, sigquit);
183 signal (SIGINT, sigquit);
184 signal (SIGQUIT, sigquit);
185
186 while (1)
187 {
188 int cfd = accept (fd, NULL, NULL);
189 if (cfd == -1)
190 {
191 perror ("accept");
192 exit (1);
193 }
194
195 pid_t pid = fork ();
196 if (pid == 0)
197 {
198 int i;
199
200 for (i = getmaxfd (); i >= 0; i--)
201 if (i != cfd)
202 close (i);
203
204 if (cfd != 0)
205 dup2 (cfd, 0);
206 if (cfd != 1)
207 dup2 (cfd, 1);
208 if (cfd != 2)
209 dup2 (cfd, 2);
210 if (cfd > 2)
211 close (cfd);
212
213 execvp (argv[0], argv);
214 exit (127);
215 }
216 if (pid == -1)
217 {
218 perror ("fork");
219 }
220 close (cfd);
221 }
222 return 0;
223}
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 0682b51..843d235 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -71,6 +71,7 @@ m4_include([shutdown.at])
71m4_include([shell.at]) 71m4_include([shell.at])
72m4_include([inet.at]) 72m4_include([inet.at])
73m4_include([maxinst.at]) 73m4_include([maxinst.at])
74m4_include([passfd.at])
74 75
75m4_include([envop.at]) 76m4_include([envop.at])
76m4_include([env.at]) \ No newline at end of file 77m4_include([env.at]) \ No newline at end of file
diff --git a/tests/to.c b/tests/to.c
index 6874bfd..0bfabac 100644
--- a/tests/to.c
+++ b/tests/to.c
@@ -1,3 +1,19 @@
1/* This file is part of GNU Pies testsuite.
2 Copyright (C) 2019 Sergey Poznyakoff
3
4 GNU Pies is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 GNU Pies is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GNU Pies. If not, see <http://www.gnu.org/licenses/>. */
16
1#include <config.h> 17#include <config.h>
2#include <stdlib.h> 18#include <stdlib.h>
3#include <stdio.h> 19#include <stdio.h>

Return to:

Send suggestions and report system problems to the System administrator.