[tracker] libtracker-common: Tests for tracker-dbus functions



commit a0ff643590b56774610bd9c1899815eba1a00157
Author: Ivan Frade <ivan frade nokia com>
Date:   Sat Mar 20 22:51:04 2010 +0200

    libtracker-common: Tests for tracker-dbus functions

 tests/libtracker-common/Makefile.am         |    4 +-
 tests/libtracker-common/mock-dbus.c         |   92 +++++++
 tests/libtracker-common/tracker-dbus-test.c |  382 ++++++++++++++++++++++++++-
 3 files changed, 470 insertions(+), 8 deletions(-)
---
diff --git a/tests/libtracker-common/Makefile.am b/tests/libtracker-common/Makefile.am
index dbdc648..494c21d 100644
--- a/tests/libtracker-common/Makefile.am
+++ b/tests/libtracker-common/Makefile.am
@@ -22,7 +22,9 @@ INCLUDES = 								\
 	$(DBUS_CFLAGS)
 
 tracker_dbus_SOURCES = 							\
-	tracker-dbus-test.c
+	tracker-dbus-test.c						\
+	mock-dbus.c							\
+	mock-dbus-gproxy.c
 
 tracker_dbus_LDADD =							\
 	$(top_builddir)/tests/common/libtracker-testcommon.la 		\
diff --git a/tests/libtracker-common/mock-dbus.c b/tests/libtracker-common/mock-dbus.c
new file mode 100644
index 0000000..78e2082
--- /dev/null
+++ b/tests/libtracker-common/mock-dbus.c
@@ -0,0 +1,92 @@
+#include <glib-object.h>
+#include <gobject/gvaluecollector.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
+#include <dbus/dbus-glib-bindings.h>
+#include "mock-dbus-gproxy.h"
+#include <sys/types.h>
+#include <unistd.h>
+#include <string.h>
+
+static gint connection_counter = 0;
+
+DBusGConnection * 	
+dbus_g_bus_get (DBusBusType type, GError **error)
+{
+        connection_counter += 1;
+        return (DBusGConnection *)g_strdup ("hi");
+}
+
+DBusGProxy *
+dbus_g_proxy_new_for_name (DBusGConnection *conn,
+                           const gchar *service,
+                           const gchar *path,
+                           const gchar *interface)
+{
+        return (DBusGProxy *)mock_dbus_gproxy_new ();
+}
+
+DBusGConnection *
+dbus_g_connection_ref (DBusGConnection *conn)
+{
+        connection_counter += 1;
+        return conn;
+}
+
+void
+dbus_g_connection_unref (DBusGConnection *connection)
+{
+        connection_counter -= 1;
+}
+
+gchar *
+dbus_g_method_get_sender (DBusGMethodInvocation *context)
+{
+        return g_strdup ("hardcoded sender");
+}
+
+gboolean
+dbus_g_proxy_call (DBusGProxy *proxy,
+                   const gchar *function_name,
+                   GError  **error,
+                   GType first_arg_type, ...)
+{
+        va_list  args;
+        GType    arg_type;
+        gchar   *local_error = NULL;
+
+        va_start (args, first_arg_type);
+        if (g_strcmp0 (function_name, "GetConnectionUnixProcessID") == 0) {
+                /*
+                 * G_TYPE_STRING, sender
+                 * G_TYPE_INVALID,
+                 * G_TYPE_UINT, pid
+                 * G_TYPE_INVALID,
+                 */
+                GValue value = { 0, };
+
+                /* Input string (ignore) */
+                g_value_init (&value, G_TYPE_STRING);
+                G_VALUE_COLLECT (&value, args, 0, &local_error);
+                g_value_unset (&value);
+
+                arg_type = va_arg (args, GType);
+                g_assert (arg_type == G_TYPE_INVALID);
+
+                arg_type = va_arg (args, GType);
+                g_assert (arg_type == G_TYPE_UINT);
+
+                g_value_init (&value, arg_type);
+                g_value_set_uint (&value, (guint)getpid ());
+                G_VALUE_LCOPY (&value, 
+                               args, 0,
+                               &local_error);
+                g_value_unset (&value);
+                                
+                
+        } else {
+                g_critical ("dbus_g_proxy_call '%s' unsupported", function_name);
+        }
+        va_end (args);
+        return TRUE;
+}
diff --git a/tests/libtracker-common/tracker-dbus-test.c b/tests/libtracker-common/tracker-dbus-test.c
index d7b99d2..ac56302 100644
--- a/tests/libtracker-common/tracker-dbus-test.c
+++ b/tests/libtracker-common/tracker-dbus-test.c
@@ -17,9 +17,9 @@
  * Boston, MA  02110-1301, USA.
  */
 #include <glib.h>
-
+#include <gio/gio.h>
 #include <libtracker-common/tracker-dbus.h>
-
+#include <stdlib.h>
 #include <tracker-test-helpers.h>
 
 static void
@@ -155,6 +155,11 @@ test_dbus_request_failed (void)
 {
 	GError *error = NULL;
 
+        /* 
+         * For some (unknown) reason, this calls don't appear in the
+         * coverage evaluation.
+         */
+
 	/* Default case: we set the error */
 	if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
 		tracker_dbus_request_failed (1, NULL, &error, "Test Error message");
@@ -187,6 +192,347 @@ test_dbus_request_failed (void)
 	g_test_trap_assert_stderr ("*Unset error and no error message*");
 }
 
+static void
+test_dbus_gvalue ()
+{
+        GValue *result = NULL;
+        
+        result = tracker_dbus_gvalue_slice_new (G_TYPE_INT);
+        g_assert (G_VALUE_TYPE (result) == G_TYPE_INT);
+        g_assert (result);
+
+        tracker_dbus_gvalue_slice_free (result);
+}
+
+static void
+test_dbus_utils_str_to_strv ()
+{
+        const gchar *str = "test string";
+        gchar **result = NULL;
+
+        result = tracker_dbus_str_to_strv (str);
+        g_assert (result);
+        g_assert_cmpint (g_strv_length (result), ==, 1);
+        g_strfreev (result);
+        
+}
+
+static GQueue *
+helper_get_queue_with_gfiles (int n)
+{
+        GQueue *queue;
+        gchar *filename;
+        GFile       *file;
+        gint    i;
+
+        queue = g_queue_new ();
+        for (i = 0; i < n; i++) {
+                filename = g_strdup_printf ("/test/file/%d", i);
+                file = g_file_new_for_path (filename);
+                g_queue_push_head (queue, file);
+                g_free (filename);
+        }
+        return queue;
+}
+
+static void
+test_dbus_gfile_queue_to_strv ()
+{
+        GQueue *queue;
+        int     i;
+        gchar  *filename;
+        gchar **result = NULL;
+
+        queue = helper_get_queue_with_gfiles (10);
+        result = tracker_dbus_queue_gfile_to_strv (queue, 100);
+        g_assert (result);
+        g_assert_cmpint (g_strv_length (result), ==, 10);
+        g_assert_cmpint (g_queue_get_length (queue), ==, 0);
+
+        /* 
+         * It is a FILO queue
+         */
+        for (i = 0; i < 10; i++) {
+                filename = g_strdup_printf ("/test/file/%d", 9 - i);
+                g_assert_cmpstr (result[i], ==, filename);
+                g_free (filename);
+        }
+
+        /* test max < number of elements in the queue */
+        queue = helper_get_queue_with_gfiles (10);
+        result = tracker_dbus_queue_gfile_to_strv (queue, 5);
+        g_assert (result);
+        g_assert_cmpint (g_strv_length (result), ==, 5);
+        g_assert_cmpint (g_queue_get_length (queue), ==, 5);
+        
+}
+
+static void
+test_dbus_request ()
+{
+        guint request_id, next_request_id;
+        DBusGMethodInvocation *context = (DBusGMethodInvocation *)g_strdup ("aaa");
+
+        tracker_dbus_enable_client_lookup (FALSE);
+
+        /* Ridicoulos but well... */
+        request_id = tracker_dbus_get_next_request_id ();
+        next_request_id = tracker_dbus_get_next_request_id ();
+        g_assert_cmpint (next_request_id, >, request_id);
+
+        /* Checking the logging output */
+        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT)) {
+                tracker_dbus_request_new (request_id, context, 
+                                          "Test request (%s))", "--TestNewOK--");
+                exit (0);
+        }
+        g_test_trap_assert_passed ();
+        g_test_trap_assert_stdout ("*TestNewOK*");
+
+
+        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
+                tracker_dbus_request_comment (request_id, context, 
+                                              "Well (%s)", "--TestCommentOK--");
+                exit (0);
+        }
+        g_test_trap_assert_passed ();
+        g_test_trap_assert_stderr ("*TestCommentOK*");
+
+          
+        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT)) {
+                tracker_dbus_request_success (request_id, context);
+                exit (0);
+        }
+        g_test_trap_assert_passed ();
+        g_test_trap_assert_stdout ("*Success*");
+}
+
+static void
+test_dbus_request_client_lookup ()
+{
+        guint request_id;
+        DBusGMethodInvocation *context = (DBusGMethodInvocation *)g_strdup ("aaa");
+
+        tracker_dbus_enable_client_lookup (TRUE);
+
+        request_id = tracker_dbus_get_next_request_id ();
+
+        /* Checking the logging output */
+        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT)) {
+                tracker_dbus_request_new (request_id, context, 
+                                          "Test request (%s))", "--TestNewOK--");
+                exit (0);
+        }
+        g_test_trap_assert_passed ();
+        g_test_trap_assert_stdout ("*TestNewOK*");
+        g_test_trap_assert_stdout ("*lt-tracker-dbus*");
+
+        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
+                tracker_dbus_request_comment (request_id, context, 
+                                              "Well (%s)", "--TestCommentOK--");
+                exit (0);
+        }
+        g_test_trap_assert_passed ();
+        g_test_trap_assert_stderr ("*TestCommentOK*");
+        g_test_trap_assert_stderr ("*lt-tracker-dbus*");
+
+
+        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT)) {
+                tracker_dbus_request_info (request_id, context, 
+                                           "Test info %s", "--TestInfoOK--");
+                exit (0);
+        }
+        g_test_trap_assert_passed ();
+        g_test_trap_assert_stdout ("*TestInfoOK*");
+        g_test_trap_assert_stdout ("*lt-tracker-dbus*");
+
+        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT)) {
+                tracker_dbus_request_debug (request_id, context, 
+                                            "Test debug %s", "--TestDebugOK--");
+                exit (0);
+        }
+        g_test_trap_assert_passed ();
+        g_test_trap_assert_stdout ("*TestDebugOK*");
+        g_test_trap_assert_stdout ("*lt-tracker-dbus*");
+
+        if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT)) {
+                tracker_dbus_request_success (request_id, context);
+                exit (0);
+        }
+        g_test_trap_assert_passed ();
+        g_test_trap_assert_stdout ("*Success*");
+        g_test_trap_assert_stdout ("*lt-tracker-dbus*");
+
+        /* Force client shutdown */
+        tracker_dbus_enable_client_lookup (FALSE);
+
+}
+
+static void
+test_dbus_request_client_lookup_monothread ()
+{
+        guint request_id;
+        DBusGMethodInvocation *context = (DBusGMethodInvocation *)g_strdup ("aaa");
+
+        /*
+         * Run everything in the same fork to check the clients_shutdown code
+         */
+        if (g_test_trap_fork (0, 
+                              G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR)) {
+        
+                tracker_dbus_enable_client_lookup (TRUE);
+
+                request_id = tracker_dbus_get_next_request_id ();
+                tracker_dbus_request_new (request_id, context, 
+                                          "Test request (%s))", "--TestNewOK--");
+                tracker_dbus_request_comment (request_id, context, 
+                                              "Well (%s)", "--TestCommentOK--");
+/*
+                tracker_dbus_request_failed (request_id, context, NULL,
+                                             "--TestFailedOK--");
+                tracker_quark = tracker_dbus_error_quark ();
+                error = g_error_new (tracker_quark, -1, "test_using_g_error");
+                tracker_dbus_request_failed (tracker_quark, error);
+*/                tracker_dbus_request_success (request_id, context);
+                
+                /* Force client shutdown */
+                tracker_dbus_enable_client_lookup (FALSE);
+                exit (0);
+        }
+        g_test_trap_assert_passed ();
+}
+
+static void
+test_dbus_request_failed_coverage ()
+{
+        GQuark tracker_quark;
+        guint request_id;
+        GError *error = NULL;
+        DBusGMethodInvocation *context = (DBusGMethodInvocation *)g_strdup ("aaa");
+
+        /*
+         * Repeat the failed test case in one thread to get coverage
+         */
+        if (g_test_trap_fork (0, 
+                              G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR)) {
+        
+                tracker_dbus_enable_client_lookup (TRUE);
+
+                request_id = tracker_dbus_get_next_request_id ();
+                tracker_dbus_request_new (request_id, context, 
+                                          "Test request (%s))", "--TestNewOK--");
+                /* direct message */
+                tracker_dbus_request_failed (request_id, context, NULL,
+                                             "--TestFailedOK--");
+
+                /* Using GError */
+                tracker_quark = tracker_dbus_error_quark ();
+                error = g_error_new (tracker_quark, -1, "test_using_g_error");
+                tracker_dbus_request_failed (request_id, context, &error, NULL);
+
+                tracker_dbus_request_success (request_id, context);
+                
+                /* Force client shutdown */
+                tracker_dbus_enable_client_lookup (FALSE);
+                exit (0);
+        }
+        g_test_trap_assert_passed ();
+}
+
+static gint hook_new_called = 0;
+static gint hook_done_called = 0;
+
+static void
+hook_test_new_request (guint request_id,
+                       gpointer user_data)
+{
+        hook_new_called += 1;
+}
+
+static void
+hook_test_done_request (guint request_id,
+                        gpointer user_data)
+{
+        hook_done_called += 1;
+}
+
+static void
+test_dbus_hooks ()
+{
+        gint request_id;
+        DBusGMethodInvocation *context = (DBusGMethodInvocation *)g_strdup ("aaa");
+        TrackerDBusRequestHandler *handler;
+
+        tracker_dbus_request_unblock_hooks ();
+
+        handler = tracker_dbus_request_add_hook (hook_test_new_request,
+                                                 hook_test_done_request,
+                                                 NULL);
+        g_assert (hook_new_called == 0 && hook_done_called == 0);
+
+        request_id = tracker_dbus_get_next_request_id ();
+        tracker_dbus_request_new (request_id, context, "Test Message. Is OK");
+        
+        g_assert_cmpint (hook_new_called, ==, 1);
+        g_assert_cmpint (hook_done_called, ==, 0);
+
+        tracker_dbus_request_success (request_id, context);
+        g_assert_cmpint (hook_new_called, ==, 1);
+        g_assert_cmpint (hook_done_called, ==, 1);
+
+        tracker_dbus_request_failed (request_id, context, NULL, "Test Message. Is OK");
+        g_assert_cmpint (hook_new_called, ==, 1);
+        g_assert_cmpint (hook_done_called, ==, 2);
+
+        /* Block the hooks and check that the callbacks are not invoked */
+        tracker_dbus_request_block_hooks ();
+
+        request_id = tracker_dbus_get_next_request_id ();
+        tracker_dbus_request_new (request_id, context, "Test Message. Is OK");
+        g_assert_cmpint (hook_new_called, ==, 1);
+        g_assert_cmpint (hook_done_called, ==, 2);
+
+        tracker_dbus_request_success (request_id, context);
+        g_assert_cmpint (hook_new_called, ==, 1);
+        g_assert_cmpint (hook_done_called, ==, 2);
+
+        tracker_dbus_request_failed (request_id, context, NULL, "Test Message. Is OK");
+        g_assert_cmpint (hook_new_called, ==, 1);
+        g_assert_cmpint (hook_done_called, ==, 2);
+
+        /* Unlock, but remove the hooks */
+        tracker_dbus_request_unblock_hooks ();
+        tracker_dbus_request_remove_hook (handler);
+
+        request_id = tracker_dbus_get_next_request_id ();
+        tracker_dbus_request_new (request_id, context, "Test Message. Is OK");
+        g_assert_cmpint (hook_new_called, ==, 1);
+        g_assert_cmpint (hook_done_called, ==, 2);
+
+        tracker_dbus_request_failed (request_id, context, NULL, "Test Message. Is OK");
+        g_assert_cmpint (hook_new_called, ==, 1);
+        g_assert_cmpint (hook_done_called, ==, 2);
+}
+
+static void
+test_dbus_dbus_data ()
+{
+        /* just for coverage numbers... nothing to test here */
+        const gchar     *one = "one";
+        const gchar     *two = "two";
+        TrackerDBusData *data = NULL;
+        gint             request_id;
+
+        request_id = tracker_dbus_get_next_request_id ();
+        data = tracker_dbus_data_new ((const gpointer)one, (const gpointer)two);
+        
+        g_assert (data);
+        g_assert_cmpint (data->id, ==, request_id+1);
+        g_assert_cmpstr ((gchar *)data->data1, ==, one);
+        g_assert_cmpstr ((gchar *)data->data2, ==, two);
+
+}
+
 int
 main (int argc, char **argv) {
 
@@ -197,13 +543,35 @@ main (int argc, char **argv) {
 	g_test_init (&argc, &argv, NULL);
 
 	/* disabled non-UTF-8 tests to not break test report generation */
-
-	g_test_add_func ("/libtracker-common/tracker-dbus/slist_to_strv_ok", test_slist_to_strv);
 	/* g_test_add_func ("/libtracker-common/tracker-dbus/slist_to_strv_nonutf8", test_slist_to_strv_nonutf8); */
-	g_test_add_func ("/libtracker-common/tracker-dbus/async_queue_to_strv_ok", test_async_queue_to_strv);
 	/* g_test_add_func ("/libtracker-common/tracker-dbus/async_queue_to_strv_nonutf8", test_async_queue_to_strv_nonutf8); */
-	g_test_add_func ("/libtracker-common/tracker-dbus/free_ptr_array", test_results_ptr_array_free);
-	g_test_add_func ("/libtracker-common/tracker-dbus/dbus_request_failed", test_dbus_request_failed);
+
+	g_test_add_func ("/libtracker-common/tracker-dbus/slist_to_strv_ok", 
+                         test_slist_to_strv);
+	g_test_add_func ("/libtracker-common/tracker-dbus/async_queue_to_strv_ok", 
+                         test_async_queue_to_strv);
+	g_test_add_func ("/libtracker-common/tracker-dbus/free_ptr_array", 
+                         test_results_ptr_array_free);
+        g_test_add_func ("/libtracker-common/tracker-dbus/gvalue",
+                         test_dbus_gvalue);
+        g_test_add_func ("/libtracker-common/tracker-dbus/utils",
+                         test_dbus_utils_str_to_strv);
+        g_test_add_func ("/libtracker-common/tracker-dbus/gfile_queue_to_strv",
+                         test_dbus_gfile_queue_to_strv);
+        g_test_add_func ("/libtracker-common/tracker-dbus/request",
+                         test_dbus_request);
+        g_test_add_func ("/libtracker-common/tracker-dbus/request-client-lookup",
+                         test_dbus_request_client_lookup);
+        g_test_add_func ("/libtracker-common/tracker-dbus/request-client-lookup",
+                         test_dbus_request_client_lookup_monothread);
+        g_test_add_func ("/libtracker-common/tracker-dbus/request_failed",
+                         test_dbus_request_failed);
+        g_test_add_func ("/libtracker-common/tracker-dbus/request_failed_coverage",
+                         test_dbus_request_failed_coverage);
+        g_test_add_func ("/libtracker-common/tracker-dbus/hooks",
+                         test_dbus_hooks);
+        g_test_add_func ("/libtracker-common/tracker-dbus/dbus_data",
+                         test_dbus_dbus_data);
 
 	result = g_test_run ();
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]