sys/t/fdpass-test.c: Add a simple test for file-descriptor passing.
authorMark Wooding <mdw@distorted.org.uk>
Tue, 25 Dec 2018 15:02:08 +0000 (15:02 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Tue, 25 Dec 2018 15:02:08 +0000 (15:02 +0000)
sys/Makefile.am
sys/t/fdpass-test.c [new file with mode: 0644]
sys/tests.at

index d2d861c..f1f48be 100644 (file)
@@ -52,6 +52,11 @@ pkginclude_HEADERS   += fdpass.h
 libsys_la_SOURCES      += fdpass.c
 LIBMANS                        += fdpass.3
 
+check_PROGRAMS         += t/fdpass.t
+t_fdpass_t_SOURCES      = t/fdpass-test.c
+t_fdpass_t_CPPFLAGS     = $(TEST_CPPFLAGS)
+t_fdpass_t_LDFLAGS      = -static
+
 ## Watching files for modification.
 pkginclude_HEADERS     += fwatch.h
 libsys_la_SOURCES      += fwatch.c
diff --git a/sys/t/fdpass-test.c b/sys/t/fdpass-test.c
new file mode 100644 (file)
index 0000000..0196638
--- /dev/null
@@ -0,0 +1,108 @@
+/* -*-c-*-
+ *
+ * Test the file-descriptor passing code
+ *
+ * (c) 2018 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of the mLib utilities library.
+ *
+ * mLib is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU Library General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * mLib is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with mLib.  If not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ * USA.
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include "fdpass.h"
+#include "quis.h"
+#include "report.h"
+
+#undef sun
+
+/*----- Main code ---------------------------------------------------------*/
+
+static void set_unix_addr(struct sockaddr_un *sun, const char *path)
+{
+  if (strlen(path) >= sizeof(sun->sun_path))
+    die(2, "pathname `%s' too long", path);
+  sun->sun_family = AF_UNIX;
+  strcpy(sun->sun_path, path);
+}
+
+const char sockmsg[] = "socket message", pipemsg[] = "pipe message";
+
+int main(int argc, char *argv[])
+{
+  struct sockaddr_un sun;
+  char buf[16];
+  int lsk, sk, fd, pfd[2];
+  socklen_t len;
+  ssize_t n;
+
+  ego(argv[0]);
+
+  if (argc == 3 && strcmp(argv[1], "server") == 0) {
+    set_unix_addr(&sun, argv[2]);
+    lsk = socket(PF_UNIX, SOCK_STREAM, 0);
+    if (lsk < 0) die(2, "socket: %s", strerror(errno));
+    if (bind(lsk, (struct sockaddr *)&sun, sizeof(sun)))
+      die(2, "bind: %s", strerror(errno));
+    if (listen(lsk, 5)) die(2, "listen: %s", strerror(errno));
+    len = sizeof(sun); sk = accept(lsk, (struct sockaddr *)&sun, &len);
+    if (sk < 0) die(2, "accept: %s", strerror(errno));
+    close(lsk);
+    n = fdpass_recv(sk, &fd, buf, sizeof(buf));
+    if (n < 0) die(2, "fdrecv: %s", strerror(errno));
+    close(sk);
+    if (fd == -1) die(2, "no fd found");
+    if (n != sizeof(sockmsg) || strcmp(buf, sockmsg) != 0)
+      die(2, "socket message mismatch (found `%.*s')", (int)n, buf);
+    n = read(fd, buf, sizeof(buf));
+    if (n < 0) die(2, "read: %s", strerror(errno));
+    if (n != sizeof(pipemsg) || strcmp(buf, pipemsg) != 0)
+      die(2, "pipe message mismatch (found `%.*s')", (int)n, buf);
+    close(fd);
+  } else if (argc == 3 && strcmp(argv[1], "client") == 0) {
+    set_unix_addr(&sun, argv[2]);
+    if (pipe(pfd)) die(2, "pipe: %s", strerror(errno));
+    sk = socket(PF_UNIX, SOCK_STREAM, 0);
+    if (sk < 0) die(2, "socket: %s", strerror(errno));
+    if (connect(sk, (struct sockaddr *)&sun, sizeof(sun)))
+      die(2, "connect: %s", strerror(errno));
+    if (fdpass_send(sk, pfd[0], sockmsg, sizeof(sockmsg)) < 0)
+      die(2, "fdsend: %s", strerror(errno));
+    close(pfd[0]);
+    close(sk);
+    if (write(pfd[1], pipemsg, sizeof(pipemsg)) < 0)
+      die(2, "write: %s", strerror(errno));
+    close(pfd[1]);
+  } else {
+    pquis(stderr, "usage: $ client|server SOCK\n");
+    exit(2);
+  }
+  return (0);
+}
+
+/*----- That's all, folks -------------------------------------------------*/
index a3e8d38..d5759bc 100644 (file)
 ### MA 02111-1307, USA.
 
 ###--------------------------------------------------------------------------
+### fdpass
+
+AT_SETUP([sys: fdpass])
+AT_KEYWORDS([sys fdpass])
+
+fdpass=BUILDDIR/t/fdpass.t
+
+AT_CHECK([
+  $fdpass server sock&
+  while ! [[ -S sock ]]; do sleep 1; done
+  $fdpass client sock && wait])
+
+AT_CLEANUP
+
+###--------------------------------------------------------------------------
 ### mdup
 
 AT_SETUP([sys: mdup])