Merge "Optional autogen.sh flag --enable-kdbus-transport added allowing to compile...
[platform/upstream/dbus.git] / dbus / dbus-message-util.c
index 1b13943..a5ce510 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
 
+#include <config.h>
 #include "dbus-internals.h"
 #include "dbus-test.h"
 #include "dbus-message-private.h"
@@ -44,7 +45,7 @@
  * @{
  */
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
 /**
  * Reads arguments from a message iterator given a variable argument
  * list. Only arguments of basic type and arrays of fixed-length
@@ -75,11 +76,11 @@ dbus_message_iter_get_args (DBusMessageIter *iter,
 
   return retval;
 }
-#endif /* DBUS_BUILD_TESTS */
+#endif /* DBUS_ENABLE_EMBEDDED_TESTS */
 
 /** @} */
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
 #include "dbus-test.h"
 #include "dbus-message-factory.h"
 #include <stdio.h>
@@ -137,17 +138,71 @@ check_memleaks (void)
     }
 }
 
-void
-_dbus_check_fdleaks(void)
-{
+#ifdef __linux__
+struct DBusInitialFDs {
+    fd_set set;
+};
+#endif
 
+DBusInitialFDs *
+_dbus_check_fdleaks_enter (void)
+{
 #ifdef __linux__
+  DIR *d;
+  DBusInitialFDs *fds;
 
+  /* this is plain malloc so it won't interfere with leak checking */
+  fds = malloc (sizeof (DBusInitialFDs));
+  _dbus_assert (fds != NULL);
+
+  /* This works on Linux only */
+
+  if ((d = opendir ("/proc/self/fd")))
+    {
+      struct dirent *de;
+
+      while ((de = readdir(d)))
+        {
+          long l;
+          char *e = NULL;
+          int fd;
+
+          if (de->d_name[0] == '.')
+            continue;
+
+          errno = 0;
+          l = strtol (de->d_name, &e, 10);
+          _dbus_assert (errno == 0 && e && !*e);
+
+          fd = (int) l;
+
+          if (fd < 3)
+            continue;
+
+          if (fd == dirfd (d))
+            continue;
+
+          FD_SET (fd, &fds->set);
+        }
+
+      closedir (d);
+    }
+
+  return fds;
+#else
+  return NULL;
+#endif
+}
+
+void
+_dbus_check_fdleaks_leave (DBusInitialFDs *fds)
+{
+#ifdef __linux__
   DIR *d;
 
   /* This works on Linux only */
 
-  if ((d = opendir("/proc/self/fd")))
+  if ((d = opendir ("/proc/self/fd")))
     {
       struct dirent *de;
 
@@ -161,23 +216,30 @@ _dbus_check_fdleaks(void)
             continue;
 
           errno = 0;
-          l = strtol(de->d_name, &e, 10);
-          _dbus_assert(errno == 0 && e && !*e);
+          l = strtol (de->d_name, &e, 10);
+          _dbus_assert (errno == 0 && e && !*e);
 
           fd = (int) l;
 
           if (fd < 3)
             continue;
 
-          if (fd == dirfd(d))
+          if (fd == dirfd (d))
             continue;
 
-          _dbus_warn("file descriptor %i leaked in %s.\n", fd, __FILE__);
-          _dbus_assert_not_reached("fdleaks");
+          if (FD_ISSET (fd, &fds->set))
+            continue;
+
+          _dbus_warn ("file descriptor %i leaked in %s.\n", fd, __FILE__);
+          _dbus_assert_not_reached ("fdleaks");
         }
 
-      closedir(d);
+      closedir (d);
     }
+
+  free (fds);
+#else
+  _dbus_assert (fds == NULL);
 #endif
 }
 
@@ -557,8 +619,8 @@ process_test_subdir (const DBusString          *test_base_dir,
         {
           if (_dbus_string_ends_with_c_str (&filename, ".message"))
             {
-              _dbus_warn ("Could not load %s, message builder language no longer supported\n",
-                          _dbus_string_get_const_data (&filename));
+              printf ("SKIP: Could not load %s, message builder language no longer supported\n",
+                      _dbus_string_get_const_data (&filename));
             }
           
           _dbus_verbose ("Skipping non-.message file %s\n",
@@ -994,9 +1056,14 @@ _dbus_message_test (const char *test_data_dir)
   unsigned char v_BYTE;
   unsigned char v2_BYTE;
   dbus_bool_t v_BOOLEAN;
+  DBusMessageIter iter, array_iter, struct_iter;
 #ifdef HAVE_UNIX_FD_PASSING
   int v_UNIX_FD;
 #endif
+  char **decomposed;
+  DBusInitialFDs *initial_fds;
+
+  initial_fds = _dbus_check_fdleaks_enter ();
 
   message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
                                           "/org/freedesktop/TestPath",
@@ -1092,6 +1159,34 @@ _dbus_message_test (const char *test_data_dir)
   _dbus_assert (strcmp (dbus_message_get_member (message),
                         "Bar") == 0);
 
+  /* Path decomposing */
+  dbus_message_set_path (message, NULL);
+  dbus_message_get_path_decomposed (message, &decomposed);
+  _dbus_assert (decomposed == NULL);
+  dbus_free_string_array (decomposed);
+
+  dbus_message_set_path (message, "/");
+  dbus_message_get_path_decomposed (message, &decomposed);
+  _dbus_assert (decomposed != NULL);
+  _dbus_assert (decomposed[0] == NULL);
+  dbus_free_string_array (decomposed);
+
+  dbus_message_set_path (message, "/a/b");
+  dbus_message_get_path_decomposed (message, &decomposed);
+  _dbus_assert (decomposed != NULL);
+  _dbus_assert (strcmp (decomposed[0], "a") == 0);
+  _dbus_assert (strcmp (decomposed[1], "b") == 0);
+  _dbus_assert (decomposed[2] == NULL);
+  dbus_free_string_array (decomposed);
+
+  dbus_message_set_path (message, "/spam/eggs");
+  dbus_message_get_path_decomposed (message, &decomposed);
+  _dbus_assert (decomposed != NULL);
+  _dbus_assert (strcmp (decomposed[0], "spam") == 0);
+  _dbus_assert (strcmp (decomposed[1], "eggs") == 0);
+  _dbus_assert (decomposed[2] == NULL);
+  dbus_free_string_array (decomposed);
+
   dbus_message_unref (message);
 
   /* Test the vararg functions */
@@ -1363,7 +1458,36 @@ _dbus_message_test (const char *test_data_dir)
   _dbus_message_loader_unref (loader);
 
   check_memleaks ();
-  _dbus_check_fdleaks();
+  _dbus_check_fdleaks_leave (initial_fds);
+  initial_fds = _dbus_check_fdleaks_enter ();
+
+  /* Check that we can abandon a container */
+  message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
+                                         "/org/freedesktop/TestPath",
+                                         "Foo.TestInterface",
+                                         "Method");
+
+  dbus_message_iter_init_append (message, &iter);
+
+  _dbus_assert (dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
+                                                 (DBUS_STRUCT_BEGIN_CHAR_AS_STRING
+                                                  DBUS_TYPE_STRING_AS_STRING
+                                                  DBUS_TYPE_STRING_AS_STRING
+                                                  DBUS_STRUCT_END_CHAR_AS_STRING),
+                                                 &array_iter));
+  _dbus_assert (dbus_message_iter_open_container (&array_iter, DBUS_TYPE_STRUCT,
+                                                 NULL, &struct_iter));
+
+  s = "peaches";
+  _dbus_assert (dbus_message_iter_append_basic (&struct_iter, DBUS_TYPE_STRING,
+                                               &s));
+
+  /* uh-oh, error, try and unwind */
+
+  dbus_message_iter_abandon_container (&array_iter, &struct_iter);
+  dbus_message_iter_abandon_container (&array_iter, &iter);
+
+  dbus_message_unref (message);
 
   /* Load all the sample messages from the message factory */
   {
@@ -1399,16 +1523,23 @@ _dbus_message_test (const char *test_data_dir)
   }
 
   check_memleaks ();
-  _dbus_check_fdleaks();
+  _dbus_check_fdleaks_leave (initial_fds);
 
   /* Now load every message in test_data_dir if we have one */
   if (test_data_dir == NULL)
     return TRUE;
 
-  return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
+  initial_fds = _dbus_check_fdleaks_enter ();
+
+  if (!dbus_internal_do_not_use_foreach_message_file (test_data_dir,
                                                         (DBusForeachMessageFileFunc)
                                                         dbus_internal_do_not_use_try_message_file,
-                                                        NULL);  
+                                                        NULL))
+    _dbus_assert_not_reached ("foreach_message_file test failed");
+
+  _dbus_check_fdleaks_leave (initial_fds);
+
+  return TRUE;
 }
 
-#endif /* DBUS_BUILD_TESTS */
+#endif /* DBUS_ENABLE_EMBEDDED_TESTS */