From: Daniel Wagner <[email protected]>

---
 Makefile.am         |    3 +-
 unit/manager-api.c  |  145 ++++++++++++++++++++++++++
 unit/session-api.c  |  283 ++++++++++++++++++++++++++++++++++++++++++++++++++
 unit/test-connman.h |   86 +++++++++++++++
 unit/test-session.c |  288 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 5 files changed, 802 insertions(+), 3 deletions(-)
 create mode 100644 unit/manager-api.c
 create mode 100644 unit/session-api.c
 create mode 100644 unit/test-connman.h

diff --git a/Makefile.am b/Makefile.am
index 6e661c7..14a654f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -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/manager-api.c unit/session-api.c unit/test-session.c
 unit_test_session_LDADD = @GLIB_LIBS@ @DBUS_LIBS@
 unit_objects += $(unit_test_session_OBJECTS)
 endif
diff --git a/unit/manager-api.c b/unit/manager-api.c
new file mode 100644
index 0000000..81cf4b0
--- /dev/null
+++ b/unit/manager-api.c
@@ -0,0 +1,145 @@
+/*
+ *
+ *  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 <stdio.h>
+
+#include "test-connman.h"
+
+DBusMessage *manager_get_services(DBusConnection *connection)
+{
+       DBusMessage *message, *reply;
+       DBusError error;
+
+       message = dbus_message_new_method_call(CONNMAN_SERVICE,
+                                               CONNMAN_MANAGER_PATH,
+                                               CONNMAN_MANAGER_INTERFACE,
+                                                       "GetServices");
+       if (message == NULL)
+               return NULL;
+
+       dbus_error_init(&error);
+
+       reply = dbus_connection_send_with_reply_and_block(connection,
+                                                       message, -1, &error);
+       if (reply == NULL) {
+               if (dbus_error_is_set(&error) == TRUE) {
+                       LOG("%s", error.message);
+                       dbus_error_free(&error);
+               } else {
+                       LOG("Failed to get properties");
+               }
+               dbus_message_unref(message);
+               return NULL;
+       }
+
+       dbus_message_unref(message);
+
+       return reply;
+}
+
+DBusMessage *manager_create_session(DBusConnection *connection,
+                                       struct test_session_info *info,
+                                       const char *notifier_path)
+{
+       DBusMessage *message, *reply;
+       DBusError error;
+       DBusMessageIter array, dict;
+
+       message = dbus_message_new_method_call(CONNMAN_SERVICE,
+                                               CONNMAN_MANAGER_PATH,
+                                               CONNMAN_MANAGER_INTERFACE,
+                                                       "CreateSession");
+       if (message == NULL)
+               return NULL;
+
+       dbus_error_init(&error);
+
+       dbus_message_iter_init_append(message, &array);
+
+       connman_dbus_dict_open(&array, &dict);
+
+       session_append_settings(&dict, info);
+
+       connman_dbus_dict_close(&array, &dict);
+
+       dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
+                               &notifier_path);
+
+       reply = dbus_connection_send_with_reply_and_block(connection,
+                                                       message, -1, &error);
+       if (reply == NULL) {
+               if (dbus_error_is_set(&error) == TRUE) {
+                       LOG("%s", error.message);
+                       dbus_error_free(&error);
+               } else {
+                       LOG("Failed to get properties");
+               }
+               dbus_message_unref(message);
+               return NULL;
+       }
+
+       dbus_message_unref(message);
+
+       return reply;
+}
+
+DBusMessage *manager_destroy_session(DBusConnection *connection,
+                                       const char *notifier_path)
+{
+       DBusMessage *message, *reply;
+       DBusError error;
+       DBusMessageIter array;
+
+       message = dbus_message_new_method_call(CONNMAN_SERVICE,
+                                               CONNMAN_MANAGER_PATH,
+                                               CONNMAN_MANAGER_INTERFACE,
+                                                       "DestroySession");
+       if (message == NULL)
+               return NULL;
+
+       dbus_error_init(&error);
+
+       dbus_message_iter_init_append(message, &array);
+
+       dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH,
+                               &notifier_path);
+
+       reply = dbus_connection_send_with_reply_and_block(connection,
+                                                       message, -1, &error);
+       if (reply == NULL) {
+               if (dbus_error_is_set(&error) == TRUE) {
+                       LOG("%s", error.message);
+                       dbus_error_free(&error);
+               } else {
+                       LOG("%s", error.message);
+               }
+               dbus_message_unref(message);
+               return NULL;
+       }
+
+       dbus_message_unref(message);
+
+       return reply;
+}
diff --git a/unit/session-api.c b/unit/session-api.c
new file mode 100644
index 0000000..1db6592
--- /dev/null
+++ b/unit/session-api.c
@@ -0,0 +1,283 @@
+/*
+ *
+ *  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 <gdbus/gdbus.h>
+
+#include "test-connman.h"
+
+
+static const char *roamingpolicy2string(enum connman_session_roaming_policy 
policy)
+{
+       switch (policy) {
+       case CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN:
+               break;
+       case CONNMAN_SESSION_ROAMING_POLICY_DEFAULT:
+               return "default";
+       case CONNMAN_SESSION_ROAMING_POLICY_ALWAYS:
+               return "always";
+       case CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN:
+               return "forbidden";
+       case CONNMAN_SESSION_ROAMING_POLICY_NATIONAL:
+               return "national";
+       case CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL:
+               return "international";
+       }
+
+       return "";
+}
+
+static enum connman_session_roaming_policy string2roamingpolicy(const char 
*policy)
+{
+       if (g_strcmp0(policy, "default") == 0)
+               return CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
+       else if (g_strcmp0(policy, "always") == 0)
+               return CONNMAN_SESSION_ROAMING_POLICY_ALWAYS;
+       else if (g_strcmp0(policy, "forbidden") == 0)
+               return CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
+       else if (g_strcmp0(policy, "national") == 0)
+               return CONNMAN_SESSION_ROAMING_POLICY_NATIONAL;
+       else if (g_strcmp0(policy, "international") == 0)
+               return CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL;
+       else
+               return CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
+}
+
+static void cleanup_bearer_info(gpointer data, gpointer user_data)
+{
+       struct test_bearer_info *info = data;
+
+       g_free(info->name);
+       g_free(info);
+}
+
+static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
+{
+       struct test_bearer_info *info;
+       DBusMessageIter array;
+       GSList *list = NULL;
+
+       dbus_message_iter_recurse(iter, &array);
+
+       while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
+               char *bearer = NULL;
+
+               dbus_message_iter_get_basic(&array, &bearer);
+
+               info = g_try_new0(struct test_bearer_info, 1);
+               if (info == NULL) {
+                       g_slist_foreach(list, cleanup_bearer_info, NULL);
+                       g_slist_free(list);
+
+                       return NULL;
+               }
+
+               info->name = g_strdup(bearer);
+
+               list = g_slist_append(list, info);
+
+               dbus_message_iter_next(&array);
+       }
+
+       return list;
+}
+
+static DBusMessage *notify_release(DBusConnection *conn,
+                                       DBusMessage *msg, void *user_data)
+{
+       DBG("");
+
+       return NULL;
+}
+
+static DBusMessage *notify_update(DBusConnection *conn,
+                                       DBusMessage *msg, void *user_data)
+{
+       struct test_session *session = user_data;
+       struct test_session_info *info = session->info;
+       DBusMessageIter iter, value;
+       const char *name;
+       GSList *allowed_bearers;
+
+       DBG("session %p notify %s", session, session->notify_path);
+
+       if (dbus_message_iter_init(msg, &iter) == FALSE)
+               return NULL;
+
+       dbus_message_iter_get_basic(&iter, &name);
+       dbus_message_iter_next(&iter);
+       dbus_message_iter_recurse(&iter, &value);
+
+       switch (dbus_message_iter_get_arg_type(&value)) {
+       case DBUS_TYPE_ARRAY:
+               if (g_str_equal(name, "AllowedBearers") == TRUE) {
+                       allowed_bearers = session_parse_allowed_bearers(&value);
+
+                       g_slist_foreach(info->allowed_bearers,
+                                       cleanup_bearer_info, NULL);
+                       g_slist_free(info->allowed_bearers);
+
+                       info->allowed_bearers = allowed_bearers;
+               } else {
+                       goto err;
+               }
+               break;
+       case DBUS_TYPE_BOOLEAN:
+               if (g_str_equal(name, "Priority") == TRUE) {
+                       dbus_message_iter_get_basic(&value,
+                                       &info->priority);
+
+               } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
+                       dbus_message_iter_get_basic(&value,
+                                       &info->avoid_handover);
+
+               } else if (g_str_equal(name, "StayConnected") == TRUE) {
+                       dbus_message_iter_get_basic(&value,
+                                       &info->stay_connected);
+
+               } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
+                       dbus_message_iter_get_basic(&value,
+                                       &info->ecall);
+
+               } else {
+                       goto err;
+               }
+               break;
+       case DBUS_TYPE_UINT32:
+               if (g_str_equal(name, "PeriodicConnect") == TRUE) {
+                       dbus_message_iter_get_basic(&value,
+                                       &info->periodic_connect);
+
+               } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
+                       dbus_message_iter_get_basic(&value,
+                                       &info->idle_timeout);
+
+               } else {
+                       goto err;
+               }
+               break;
+       case DBUS_TYPE_STRING:
+               if (g_str_equal(name, "RoamingPolicy") == TRUE) {
+                       const char *val;
+                       dbus_message_iter_get_basic(&value, &val);
+                       info->roaming_policy =
+                                       string2roamingpolicy(val);
+
+               } else {
+                       goto err;
+               }
+               break;
+       default:
+               goto err;
+       }
+
+err:
+       return NULL;
+}
+
+static GDBusMethodTable notify_methods[] = {
+       { "Release", "",      "", notify_release },
+       { "Update",  "a{sv}", "", notify_update  },
+       { },
+};
+
+int session_notify_register(struct test_session *session,
+                               const char *notify_path)
+{
+       if (g_dbus_register_interface(session->session_bus, notify_path,
+                       CONNMAN_NOTIFICATION_INTERFACE,
+                       notify_methods, NULL, NULL,
+                       session, NULL) == FALSE) {
+               return -EINVAL;
+       }
+
+       session->notify_path = notify_path;
+
+       return 0;
+}
+
+int session_notify_unregister(struct test_session *session,
+                               const char *notify_path)
+{
+       if (g_dbus_unregister_interface(session->session_bus, notify_path,
+                               CONNMAN_NOTIFICATION_INTERFACE) == FALSE) {
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
+{
+       struct test_session_info *info = user_data;
+       GSList *list;
+
+       for (list = info->allowed_bearers;
+                       list != NULL; list = list->next) {
+               struct test_bearer_info *info = list->data;
+
+               dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
+                                               &info->name);
+       }
+}
+
+void session_append_settings(DBusMessageIter *dict,
+                               struct test_session_info *info)
+{
+       const char *policy;
+
+       connman_dbus_dict_append_basic(dict, "Priority",
+                                               DBUS_TYPE_BOOLEAN,
+                                               &info->priority);
+
+       connman_dbus_dict_append_array(dict, "AllowedBearers",
+                                               DBUS_TYPE_STRING,
+                                               append_allowed_bearers,
+                                               info);
+
+       connman_dbus_dict_append_basic(dict, "AvoidHandover",
+                                               DBUS_TYPE_BOOLEAN,
+                                               &info->avoid_handover);
+
+       connman_dbus_dict_append_basic(dict, "StayConnected",
+                                               DBUS_TYPE_BOOLEAN,
+                                               &info->stay_connected);
+
+       connman_dbus_dict_append_basic(dict, "PeriodicConnect",
+                                               DBUS_TYPE_UINT32,
+                                               &info->periodic_connect);
+
+       connman_dbus_dict_append_basic(dict, "IdleTimeout",
+                                               DBUS_TYPE_UINT32,
+                                               &info->idle_timeout);
+
+       connman_dbus_dict_append_basic(dict, "EmergencyCall",
+                                               DBUS_TYPE_BOOLEAN,
+                                               &info->ecall);
+
+       policy = roamingpolicy2string(info->roaming_policy);
+       connman_dbus_dict_append_basic(dict, "RoamingPolicy",
+                                               DBUS_TYPE_STRING,
+                                               &policy);
+}
diff --git a/unit/test-connman.h b/unit/test-connman.h
new file mode 100644
index 0000000..a54b678
--- /dev/null
+++ b/unit/test-connman.h
@@ -0,0 +1,86 @@
+/*
+ *
+ *  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"
+
+enum connman_session_roaming_policy {
+       CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN          = 0,
+       CONNMAN_SESSION_ROAMING_POLICY_DEFAULT          = 1,
+       CONNMAN_SESSION_ROAMING_POLICY_ALWAYS           = 2,
+       CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN        = 3,
+       CONNMAN_SESSION_ROAMING_POLICY_NATIONAL         = 4,
+       CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL    = 5,
+};
+
+struct test_session_info {
+       connman_bool_t online;
+       connman_bool_t priority;
+       GSList *allowed_bearers;
+       connman_bool_t avoid_handover;
+       connman_bool_t stay_connected;
+       unsigned int periodic_connect;
+       unsigned int idle_timeout;
+       connman_bool_t ecall;
+       enum connman_session_roaming_policy roaming_policy;
+       unsigned int marker;
+};
+
+struct test_session {
+       DBusConnection *system_bus;
+       DBusConnection *session_bus;
+
+       char *owner;
+       const char *session_path;
+       const char *notify_path;
+       guint notify_watch;
+
+       struct test_session_info *info;
+};
+
+struct test_bearer_info {
+       char *name;
+};
+
+DBusMessage *manager_get_services(DBusConnection *connection);
+DBusMessage *manager_create_session(DBusConnection *connection,
+                                       struct test_session_info *info,
+                                       const char *notifier_path);
+DBusMessage *manager_destroy_session(DBusConnection *connection,
+                                       const char *notifier_path);
+
+void session_append_settings(DBusMessageIter *dict,
+                               struct test_session_info *info);
+int session_notify_register(struct test_session *session,
+                               const char *notify_path);
+int session_notify_unregister(struct test_session *session,
+                               const char *notify_path);
+
+#ifdef DEBUG
+#define LOG(fmt, arg...) do { \
+       fprintf(stdout, fmt, ##arg); \
+} while (0)
+#else
+#define LOG(fmt, arg...)
+#endif
diff --git a/unit/test-session.c b/unit/test-session.c
index 152c80f..ff7dfae 100644
--- a/unit/test-session.c
+++ b/unit/test-session.c
@@ -23,13 +23,297 @@
 #include <config.h>
 #endif
 
-#include <gdbus.h>
+#include <glib.h>
+#include <glib/gprintf.h>
 
-#include "connman.h"
+#include <stdlib.h>
+
+#include "connman/dbus.h"
+#include "gdbus/gdbus.h"
+#include "test-connman.h"
+
+#define ENABLE_WRAPPER 0
+#define SESSION_MAX 5
+
+struct session_fix {
+       GMainLoop *mainloop;
+       unsigned int max_sessions;
+       struct test_session *session;
+};
+
+static int test_session_init(struct session_fix *fix, unsigned int 
max_sessions)
+{
+       unsigned int i;
+
+       fix->max_sessions = max_sessions;
+       fix->session = g_try_new0(struct test_session, max_sessions);
+
+       for (i = 0; i < max_sessions; i++) {
+               fix->session[i].info = g_try_new0(struct test_session_info, 1);
+               fix->session[i].system_bus = 
g_dbus_setup_private(DBUS_BUS_SYSTEM, NULL, NULL);
+               fix->session[i].session_bus = 
g_dbus_setup_private(DBUS_BUS_SESSION, NULL, NULL);
+       }
+
+       return 0;
+}
+
+static void test_session_cleanup(struct session_fix *fix)
+{
+       unsigned int i;
+
+       for (i = 0; i < fix->max_sessions; i++) {
+               dbus_connection_close(fix->session[i].session_bus);
+               dbus_connection_close(fix->session[i].system_bus);
+               dbus_connection_unref(fix->session[i].session_bus);
+               dbus_connection_unref(fix->session[i].system_bus);
+
+               g_free(fix->session[i].info);
+       }
+
+       g_free(fix->session);
+}
+
+static void session_setup(struct session_fix *fix, gconstpointer data)
+{
+       fix->mainloop = g_main_loop_new(NULL, FALSE);
+}
+
+static void session_teardown(struct session_fix *fix, gconstpointer data)
+{
+       g_main_loop_unref(fix->mainloop);
+}
+
+static gboolean quit_loop_and_cleanup(gpointer data)
+{
+       struct session_fix *fix = data;
+
+       g_main_loop_quit(fix->mainloop);
+
+       test_session_cleanup(fix);
+
+       return FALSE;
+}
+
+static guint quit_on_idle_and_cleanup(struct session_fix *fix)
+{
+       GSource *source;
+       guint id;
+
+       source = g_idle_source_new();
+       g_source_set_callback(source, quit_loop_and_cleanup, fix, NULL);
+       id = g_source_attach(source, g_main_loop_get_context(fix->mainloop));
+       g_source_unref(source);
+
+       return id;
+}
+
+static void session_wrapper(struct session_fix *fix, gconstpointer data)
+{
+       GSourceFunc func = data;
+#if ENABLE_WRAPPER
+       if (g_test_trap_fork(60 * 1000 * 1000, 0) == TRUE) {
+               g_timeout_add_seconds(0, func, fix);
+               g_main_loop_run(fix->mainloop);
+               exit(0);
+       }
+
+       g_test_trap_assert_passed();
+#else
+       g_timeout_add_seconds(0, func, fix);
+       g_main_loop_run(fix->mainloop);
+#endif
+}
+
+static connman_bool_t is_connman_running(DBusConnection *connection)
+{
+       DBusError error;
+       connman_bool_t running;
+
+       dbus_error_init(&error);
+
+       running = dbus_bus_name_has_owner(connection, CONNMAN_SERVICE, &error);
+
+       if (dbus_error_is_set(&error) == TRUE) {
+               fprintf(stderr, "%s\n", error.message);
+               dbus_error_free(&error);
+
+               return FALSE;
+       }
+
+       return running;
+}
+
+static gboolean test_session_create_no_notify(gpointer data)
+{
+       struct session_fix *fix = data;
+       DBusMessage *msg;
+
+       test_session_init(fix, 1);
+
+       msg = manager_create_session(fix->session->system_bus,
+                                       fix->session->info, "/foo");
+       g_assert(msg != NULL);
+
+       g_assert(is_connman_running(fix->session->system_bus) == TRUE);
+       quit_on_idle_and_cleanup(fix);
+
+       return FALSE;
+}
+
+static gboolean test_session_destroy_no_notify(gpointer data)
+{
+       struct session_fix *fix = data;
+       DBusMessage *msg;
+
+       test_session_init(fix, 1);
+
+       msg = manager_destroy_session(fix->session->system_bus, "/foo");
+       g_assert(msg == NULL);
+
+       g_assert(is_connman_running(fix->session->system_bus) == TRUE);
+       quit_on_idle_and_cleanup(fix);
+
+       return FALSE;
+}
+
+static gboolean test_session_create(gpointer data)
+{
+       struct session_fix *fix = data;
+       DBusMessage *msg;
+       int err;
+
+       test_session_init(fix, 1);
+
+       err = session_notify_register(fix->session, "/foo");
+       g_assert(err == 0);
+
+       msg = manager_create_session(fix->session->system_bus,
+                                       fix->session->info, "/foo");
+       g_assert(msg != NULL);
+
+       err = session_notify_unregister(fix->session, "/foo");
+       g_assert(err == 0);
+
+       g_assert(is_connman_running(fix->session->system_bus) == TRUE);
+       quit_on_idle_and_cleanup(fix);
+
+       return FALSE;
+}
+
+static gboolean test_session_create_already_exists(gpointer data)
+{
+       struct session_fix *fix = data;
+       DBusMessage *msg;
+       int err;
+
+       test_session_init(fix, 2);
+
+       err = session_notify_register(&fix->session[0], "/foo");
+       g_assert(err == 0);
+
+       msg = manager_create_session(fix->session[0].system_bus,
+                                       fix->session[0].info, "/foo");
+       g_assert(msg != NULL);
+
+       msg = manager_create_session(fix->session[1].system_bus,
+                                       fix->session[1].info, "/foo");
+       g_assert(msg == NULL);
+
+       err = session_notify_unregister(&fix->session[0], "/foo");
+       g_assert(err == 0);
+
+       g_assert(is_connman_running(fix->session[0].system_bus) == TRUE);
+       quit_on_idle_and_cleanup(fix);
+
+       return FALSE;
+}
+
+static gboolean test_session_create_destroy(gpointer data)
+{
+       struct session_fix *fix = data;
+       DBusMessage *msg;
+       int err;
+
+       test_session_init(fix, 2);
+
+       err = session_notify_register(&fix->session[0], "/foo");
+       g_assert(err == 0);
+
+       msg = manager_create_session(fix->session[0].system_bus,
+                                       fix->session[0].info, "/foo");
+       g_assert(msg != NULL);
+
+       msg = manager_destroy_session(fix->session[0].system_bus, "/foo");
+       g_assert(msg == NULL);
+
+       g_assert(is_connman_running(fix->session[0].system_bus) == TRUE);
+       quit_on_idle_and_cleanup(fix);
+
+       return FALSE;
+}
+
+static gboolean test_session_create_many(gpointer data)
+{
+       struct session_fix *fix = data;
+       DBusMessage *msg;
+       char *name;
+       int err;
+       int i, max;
+
+       max = 100;
+
+       test_session_init(fix, max);
+
+       for (i = 0; i < max; i++) {
+               name = g_strdup_printf("/foo/%d", i);
+
+               err = session_notify_register(fix->session, name);
+               g_assert(err == 0);
+
+               msg = manager_create_session(fix->session->system_bus,
+                                       fix->session->info, name);
+               g_assert(msg != NULL);
+
+               g_free(name);
+       }
+
+       for (i = 0; i < max; i++) {
+               name = g_strdup_printf("/foo/%d", i);
+
+               msg = manager_destroy_session(fix->session->system_bus, name);
+               g_assert(msg == NULL);
+
+               g_free(name);
+       }
+
+       g_assert(is_connman_running(fix->session[0].system_bus) == TRUE);
+       quit_on_idle_and_cleanup(fix);
+
+       return FALSE;
+}
 
 int main(int argc, char *argv[])
 {
        g_test_init(&argc, &argv, NULL);
 
+       g_test_add("/manager/session create no notify", struct session_fix,
+               test_session_create_no_notify,
+               session_setup, session_wrapper, session_teardown);
+       g_test_add("/manager/session destroy no notify", struct session_fix,
+               test_session_destroy_no_notify,
+               session_setup, session_wrapper, session_teardown);
+       g_test_add("/manager/session create", struct session_fix,
+               test_session_create,
+               session_setup, session_wrapper, session_teardown);
+       g_test_add("/manager/session create already exists", struct session_fix,
+               test_session_create_already_exists,
+               session_setup, session_wrapper, session_teardown);
+       g_test_add("/manager/session create destroy", struct session_fix,
+               test_session_create_destroy,
+               session_setup, session_wrapper, session_teardown);
+       g_test_add("/manager/session create many", struct session_fix,
+               test_session_create_many,
+               session_setup, session_wrapper, session_teardown);
+
        return g_test_run();
 }
-- 
1.7.4.4

_______________________________________________
connman mailing list
[email protected]
http://lists.connman.net/listinfo/connman

Reply via email to