unit: Add util function for unit tests
authorDaniel Wagner <daniel.wagner@bmw-carit.de>
Tue, 7 Jun 2011 13:42:21 +0000 (15:42 +0200)
committerDaniel Wagner <daniel.wagner@bmw-carit.de>
Tue, 7 Jun 2011 13:42:57 +0000 (15:42 +0200)
Makefile.am
unit/test-connman.h [new file with mode: 0644]
unit/test-session.c
unit/utils.c [new file with mode: 0644]

index 6e661c7..c7e706a 100644 (file)
@@ -183,7 +183,8 @@ tools_iptables_test_LDADD = @GLIB_LIBS@ @XTABLES_LIBS@
 
 tools_private_network_test_LDADD = @GLIB_LIBS@ @DBUS_LIBS@
 
-unit_test_session_SOURCES = $(gdbus_sources) unit/test-session.c
+unit_test_session_SOURCES = $(gdbus_sources) src/log.c src/dbus.c \
+               unit/test-session.c unit/utils.c
 unit_test_session_LDADD = @GLIB_LIBS@ @DBUS_LIBS@
 unit_objects += $(unit_test_session_OBJECTS)
 endif
diff --git a/unit/test-connman.h b/unit/test-connman.h
new file mode 100644 (file)
index 0000000..7c76332
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program 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 General Public License for more details.
+ *
+ *  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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#include <glib.h>
+
+#include <connman/dbus.h>
+
+#include "connman.h"
+
+struct test_session;
+
+struct test_fix {
+       gpointer user_data;
+
+       GMainLoop *main_loop;
+       DBusConnection *main_connection;
+
+       /* session test cases */
+       unsigned int max_sessions;
+       struct test_session *session;
+};
+
+/* utils.c */
+typedef void (* util_test_setup_cb) (struct test_fix *fix,
+                                       gconstpointer data);
+typedef void (* util_test_teardown_cb) (struct test_fix *fix,
+                                       gconstpointer data);
+
+gboolean util_quit_loop(gpointer fix);
+guint util_idle_call(struct test_fix *fix, GSourceFunc func,
+                       GDestroyNotify notify);
+guint util_call(struct test_fix *fix, GSourceFunc func,
+               GDestroyNotify notify);
+void util_test_add(const char *test_name, GSourceFunc test_func,
+                       util_test_setup_cb setup_cb,
+                       util_test_teardown_cb teardown_cb);
+void util_setup(struct test_fix *fix, gconstpointer data);
+void util_teardown(struct test_fix *fix, gconstpointer data);
+
+typedef void (* notify_cb) (struct test_session *session);
+
+struct test_session {
+       gpointer user_data;
+
+       struct test_fix *fix;
+       DBusConnection *connection;
+
+       notify_cb notify;
+};
+
+/* #define DEBUG */
+#ifdef DEBUG
+#include <stdio.h>
+
+#define LOG(fmt, arg...) do { \
+       fprintf(stdout, "%s:%s() " fmt "\n", \
+                       __FILE__, __FUNCTION__ , ## arg); \
+} while (0)
+#else
+#define LOG(fmt, arg...)
+#endif
index 152c80f..aaed275 100644 (file)
 #include <config.h>
 #endif
 
-#include <gdbus.h>
+#include "gdbus/gdbus.h"
 
-#include "connman.h"
+#include "test-connman.h"
+
+static gboolean test_empty(gpointer data)
+{
+       struct test_fix *fix = data;
+
+       util_idle_call(fix, util_quit_loop, NULL);
+
+       return FALSE;
+}
+
+static void setup_cb(struct test_fix *fix, gconstpointer data)
+{
+       util_setup(fix, data);
+}
+
+static void teardown_cb(struct test_fix *fix, gconstpointer data)
+{
+       util_teardown(fix, data);
+}
 
 int main(int argc, char *argv[])
 {
        g_test_init(&argc, &argv, NULL);
 
+       util_test_add("/empty",
+               test_empty, setup_cb, teardown_cb);
+
        return g_test_run();
 }
diff --git a/unit/utils.c b/unit/utils.c
new file mode 100644 (file)
index 0000000..bd42439
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2011  BWM CarIT GmbH. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program 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 General Public License for more details.
+ *
+ *  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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+
+#include "gdbus/gdbus.h"
+
+#include "test-connman.h"
+
+#define ENABLE_WRAPPER 1
+
+gboolean util_quit_loop(gpointer data)
+{
+       struct test_fix *fix = data;
+
+       g_main_loop_quit(fix->main_loop);
+
+       return FALSE;
+}
+
+guint util_idle_call(struct test_fix *fix, GSourceFunc func,
+                       GDestroyNotify notify)
+{
+       GSource *source;
+       guint id;
+
+       source = g_idle_source_new();
+       g_source_set_callback(source, func, fix, notify);
+       id = g_source_attach(source, g_main_loop_get_context(fix->main_loop));
+       g_source_unref(source);
+
+       return id;
+}
+
+guint util_call(struct test_fix *fix, GSourceFunc func,
+               GDestroyNotify notify)
+{
+       GSource *source;
+       guint id;
+
+       source = g_timeout_source_new(0);
+       g_source_set_callback(source, func, fix, notify);
+       id = g_source_attach(source, g_main_loop_get_context(fix->main_loop));
+       g_source_unref(source);
+
+       return id;
+}
+
+void util_setup(struct test_fix *fix, gconstpointer data)
+{
+       fix->main_loop = g_main_loop_new(NULL, FALSE);
+       fix->main_connection = g_dbus_setup_private(DBUS_BUS_SYSTEM,
+                                                       NULL, NULL);
+}
+
+void util_teardown(struct test_fix *fix, gconstpointer data)
+{
+       dbus_connection_close(fix->main_connection);
+       dbus_connection_unref(fix->main_connection);
+
+       g_main_loop_unref(fix->main_loop);
+}
+
+static void util_wrapper(struct test_fix *fix, gconstpointer data)
+{
+       GSourceFunc func = data;
+#if ENABLE_WRAPPER
+       if (g_test_trap_fork(60 * 1000 * 1000, 0) == TRUE) {
+               util_call(fix, func, NULL);
+               g_main_loop_run(fix->main_loop);
+               exit(0);
+       }
+
+       g_test_trap_assert_passed();
+#else
+       util_call(fix, func, NULL);
+       g_main_loop_run(fix->main_loop);
+#endif
+}
+
+void util_test_add(const char *test_name, GSourceFunc test_func,
+                       util_test_setup_cb setup_cb,
+                       util_test_teardown_cb teardown_cb)
+{
+       g_test_add(test_name, struct test_fix, test_func,
+               setup_cb, util_wrapper, teardown_cb);
+}