[telepathy-salut/master] add check-gibber-unix-transport.c

Guillaume Desmottes guillaume.desmottes at collabora.co.uk
Fri May 8 08:45:05 PDT 2009


---
 lib/gibber/tests/check-gibber-unix-transport.c |  143 ++++++++++++++++++++++++
 1 files changed, 143 insertions(+), 0 deletions(-)
 create mode 100644 lib/gibber/tests/check-gibber-unix-transport.c

diff --git a/lib/gibber/tests/check-gibber-unix-transport.c b/lib/gibber/tests/check-gibber-unix-transport.c
new file mode 100644
index 0000000..dfbc9f8
--- /dev/null
+++ b/lib/gibber/tests/check-gibber-unix-transport.c
@@ -0,0 +1,143 @@
+/*
+ * check-unix-transport.c - Test for GibberUnixTransport
+ * Copyright (C) 2009 Collabora Ltd.
+ * @author Guillaume Desmottes <guillaume.desmottes at collabora.co.uk>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <sys/un.h>
+
+#include <gibber/gibber-unix-transport.h>
+#include <gibber/gibber-listener.h>
+
+#include <check.h>
+#include "check-gibber.h"
+
+gboolean got_connection;
+GibberUnixTransport *unix_transport;
+
+static void
+new_connection_cb (GibberListener *listener,
+                   GibberTransport *connection,
+                   struct sockaddr *addr,
+                   guint size,
+                   GMainLoop *loop)
+{
+  int fd, opt, ret;
+  struct iovec iov;
+  struct msghdr msg;
+  ssize_t received;
+  char control[CMSG_SPACE (sizeof (struct ucred))];
+  struct cmsghdr *ch;
+  struct ucred *cred;
+  const gchar *data = "What a nice data";
+  gchar buffer[128];
+
+  got_connection = TRUE;
+
+  /* Block receiving so the data won't be consummed by transport's GIOSource */
+  gibber_transport_block_receiving (connection, TRUE);
+
+  g_assert (gibber_unix_transport_send_credentials (unix_transport,
+        (guint8 *) data, strlen (data) + 1));
+
+  fd = GIBBER_FD_TRANSPORT (connection)->fd;
+
+  opt = 1;
+  ret = setsockopt (fd, SOL_SOCKET, SO_PASSCRED, &opt, sizeof (opt));
+  g_assert (ret != -1);
+
+  memset (buffer, 0, sizeof (buffer));
+  memset (&iov, 0, sizeof (iov));
+  iov.iov_base = buffer;
+  iov.iov_len = sizeof (buffer);
+
+  memset (&msg, 0, sizeof (msg));
+  msg.msg_iov = &iov;
+  msg.msg_iovlen = 1;
+  msg.msg_control = control;
+  msg.msg_controllen = sizeof (control);
+
+  received = recvmsg (fd, &msg, 0);
+  /* check the received data */
+  g_assert (received != -1);
+  g_assert (strcmp (data, buffer) == 0);
+
+  /* check the credentials */
+  ch = CMSG_FIRSTHDR (&msg);
+  g_assert (ch != NULL);
+  cred = (struct ucred *) CMSG_DATA (ch);
+  g_assert (cred->pid == getpid ());
+  g_assert (cred->uid == getuid ());
+  g_assert (cred->gid == getgid ());
+
+  g_main_loop_quit (loop);
+}
+
+START_TEST (test_send_credentials)
+{
+  GibberListener *listener_unix;
+  int ret;
+  GMainLoop *mainloop;
+  GError *error = NULL;
+  gchar *path = "/tmp/check-gibber-unix-transport-socket";
+
+  ret = unlink (path);
+  fail_if (ret == -1 && errno != ENOENT);
+
+  mainloop = g_main_loop_new (NULL, FALSE);
+
+  listener_unix = gibber_listener_new ();
+  fail_if (listener_unix == NULL);
+
+  g_signal_connect (listener_unix, "new-connection",
+      G_CALLBACK (new_connection_cb), mainloop);
+
+  ret = gibber_listener_listen_socket (listener_unix, path, FALSE, &error);
+  fail_if (ret != TRUE);
+
+  unix_transport = gibber_unix_transport_new ();
+  ret = gibber_unix_transport_connect (unix_transport, path, &error);
+  fail_if (ret != TRUE);
+
+  if (!got_connection)
+    g_main_loop_run (mainloop);
+
+  fail_if (!got_connection, "Failed to connect");
+
+  g_object_unref (listener_unix);
+  g_object_unref (unix_transport);
+  g_main_loop_unref (mainloop);
+} END_TEST
+
+TCase *
+make_gibber_unix_transport_tcase (void)
+{
+  TCase *tc = tcase_create ("GibberUnixTransport");
+  tcase_add_test (tc, test_send_credentials);
+  return tc;
+}
-- 
1.5.6.5




More information about the telepathy-commits mailing list