dbus-1.9.16
[platform/upstream/dbus.git] / test / fdpass.c
index fa958da..a74ce81 100644 (file)
@@ -31,6 +31,7 @@
 
 #include <glib.h>
 
+#include <stdlib.h>
 #include <string.h>
 
 #ifdef G_OS_UNIX
@@ -57,9 +58,11 @@ _DBUS_STATIC_ASSERT (MAX_MESSAGE_UNIX_FDS <= 253);
 /* Arbitrary; included here to avoid relying on the default. */
 #define MAX_INCOMING_UNIX_FDS (MAX_MESSAGE_UNIX_FDS * 4)
 
-/* Arbitrary, except that MAX_MESSAGE_UNIX_FDS * SOME_MESSAGES must be
+/* Arbitrary, except that MAX_MESSAGE_UNIX_FDS * SOME_MESSAGES should be
  * less than the process's file descriptor limit. */
-#define SOME_MESSAGES 50
+#define SOME_MESSAGES 20
+/* To cover some situations on Linux we want this to be true. */
+_DBUS_STATIC_ASSERT (MAX_MESSAGE_UNIX_FDS * SOME_MESSAGES > 256);
 
 /* Linux won't allow more than 253 fds per sendmsg(). */
 #define TOO_MANY_FDS 255
@@ -90,14 +93,6 @@ typedef struct {
     int fd_before;
 } Fixture;
 
-#if !GLIB_CHECK_VERSION (2, 38, 0)
-#define g_test_skip(s) my_test_skip (s)
-static inline void my_test_skip (const gchar *s)
-{
-  g_message ("SKIP: %s", s);
-}
-#endif
-
 #ifdef HAVE_UNIX_FD_PASSING
 
 static void oom (const gchar *doing) G_GNUC_NORETURN;
@@ -106,6 +101,7 @@ static void
 oom (const gchar *doing)
 {
   g_error ("out of memory (%s)", doing);
+  abort ();
 }
 
 static void
@@ -195,7 +191,7 @@ test_connect (Fixture *f,
 
   while (f->left_server_conn == NULL)
     {
-      g_print (".");
+      test_progress ('.');
       test_main_context_iterate (f->ctx, TRUE);
     }
 
@@ -208,7 +204,7 @@ test_connect (Fixture *f,
 
   while (f->right_server_conn == NULL)
     {
-      g_print (".");
+      test_progress ('.');
       test_main_context_iterate (f->ctx, TRUE);
     }
 
@@ -225,7 +221,7 @@ test_connect (Fixture *f,
       !dbus_connection_get_is_authenticated (f->left_server_conn) ||
       !dbus_connection_get_is_authenticated (f->right_server_conn))
     {
-      g_printerr ("*");
+      test_progress ('*');
       test_main_context_iterate (f->ctx, TRUE);
     }
 
@@ -307,7 +303,7 @@ test_relay (Fixture *f,
 
   while (g_queue_get_length (&f->messages) < 1)
     {
-      g_print (".");
+      test_progress ('.');
       test_main_context_iterate (f->ctx, TRUE);
     }
 
@@ -390,7 +386,7 @@ test_limit (Fixture *f,
 
   while (g_queue_get_length (&f->messages) < 1)
     {
-      g_print (".");
+      test_progress ('.');
       test_main_context_iterate (f->ctx, TRUE);
     }
 
@@ -425,7 +421,7 @@ test_too_many (Fixture *f,
 {
 #ifdef HAVE_UNIX_FD_PASSING
   DBusMessage *outgoing;
-  int i;
+  unsigned int i;
 
   test_connect (f, data);
 
@@ -450,7 +446,7 @@ test_too_many (Fixture *f,
   while (dbus_connection_get_is_connected (f->left_client_conn) ||
       dbus_connection_get_is_connected (f->left_server_conn))
     {
-      g_print (".");
+      test_progress ('.');
       test_main_context_iterate (f->ctx, TRUE);
     }
 
@@ -473,7 +469,7 @@ test_too_many_split (Fixture *f,
 #ifdef HAVE_UNIX_FD_PASSING
   DBusMessage *outgoing;
   int i;
-  int left_client_socket;
+  DBusSocket left_client_socket;
   char *payload;
   int payload_len;
   DBusString buffer;
@@ -536,7 +532,8 @@ test_too_many_split (Fixture *f,
   /* This is blatant cheating, and the API documentation specifically
    * tells you not use this function in this way. Never do this
    * in application code. */
-  if (!dbus_connection_get_socket (f->left_client_conn, &left_client_socket))
+  if (!dbus_connection_get_socket (f->left_client_conn,
+                                   &left_client_socket.fd))
     g_error ("'unix:' DBusConnection should have had a socket");
 
   /* Just to be sure that we're at a message boundary. */
@@ -583,7 +580,7 @@ test_too_many_split (Fixture *f,
   while (dbus_connection_get_is_connected (f->left_client_conn) ||
       dbus_connection_get_is_connected (f->left_server_conn))
     {
-      g_print (".");
+      test_progress ('.');
       test_main_context_iterate (f->ctx, TRUE);
     }
 
@@ -604,7 +601,7 @@ test_flood (Fixture *f,
     gconstpointer data)
 {
 #ifdef HAVE_UNIX_FD_PASSING
-  int i, j;
+  unsigned int i, j;
   DBusMessage *outgoing[SOME_MESSAGES];
   dbus_uint32_t serial;
 
@@ -639,7 +636,7 @@ test_flood (Fixture *f,
 
   while (g_queue_get_length (&f->messages) < SOME_MESSAGES)
     {
-      g_print (".");
+      test_progress ('.');
       test_main_context_iterate (f->ctx, TRUE);
     }
 
@@ -707,7 +704,7 @@ test_odd_limit (Fixture *f,
       while (dbus_connection_get_is_connected (f->left_client_conn) ||
           dbus_connection_get_is_connected (f->left_server_conn))
         {
-          g_print (".");
+          test_progress ('.');
           test_main_context_iterate (f->ctx, TRUE);
         }
 
@@ -726,7 +723,7 @@ test_odd_limit (Fixture *f,
       /* We're at or under the limit. The message gets through intact. */
       while (g_queue_get_length (&f->messages) < 1)
         {
-          g_print (".");
+          test_progress ('.');
           test_main_context_iterate (f->ctx, TRUE);
         }
 
@@ -809,6 +806,25 @@ main (int argc,
 {
   test_init (&argc, &argv);
 
+#ifdef HAVE_GETRLIMIT
+    {
+      struct rlimit lim;
+
+      if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
+        g_error ("Failed to get RLIMIT_NOFILE limit: %s", g_strerror (errno));
+
+      if (lim.rlim_cur != RLIM_INFINITY &&
+          /* only run if we have a fairly generous margin of error
+           * for stdout, stderr, duplicates, the D-Bus connection, etc. */
+          lim.rlim_cur < 2 * MAX_MESSAGE_UNIX_FDS * SOME_MESSAGES)
+        {
+          g_message ("not enough RLIMIT_NOFILE to run this test");
+          /* Autotools exit code for "all skipped" */
+          return 77;
+        }
+    }
+#endif
+
   g_test_add ("/relay", Fixture, NULL, setup,
       test_relay, teardown);
   g_test_add ("/limit", Fixture, NULL, setup,