From: Daniel Wagner <[email protected]>

Note that this patch will be splitted into smaller patches.
---
 Makefile.am         |    4 +-
 unit/manager-api.c  |  189 ++++++++++++++++++++++++
 unit/session-api.c  |  406 +++++++++++++++++++++++++++++++++++++++++++++++++++
 unit/test-connman.h |  146 ++++++++++++++++++
 unit/test-session.c |  313 +++++++++++++++++++++++++++++++++++++++-
 unit/utils.c        |  183 +++++++++++++++++++++++
 6 files changed, 1238 insertions(+), 3 deletions(-)
 create mode 100644 unit/manager-api.c
 create mode 100644 unit/session-api.c
 create mode 100644 unit/test-connman.h
 create mode 100644 unit/utils.c

diff --git a/Makefile.am b/Makefile.am
index 6e661c7..6545aff 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -183,7 +183,9 @@ 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/utils.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..02bf58a
--- /dev/null
+++ b/unit/manager-api.c
@@ -0,0 +1,189 @@
+/*
+ *
+ *  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"
+
+static DBusMessage *set_property(DBusConnection *connection,
+                               const char *property, int type, void *value)
+{
+       DBusMessage *message, *reply;
+       DBusError error;
+       DBusMessageIter iter;
+
+       message = dbus_message_new_method_call(CONNMAN_SERVICE,
+                                               CONNMAN_MANAGER_PATH,
+                                               CONNMAN_MANAGER_INTERFACE,
+                                               "SetProperty");
+       if (message == NULL)
+               return NULL;
+
+       dbus_message_iter_init_append(message, &iter);
+       connman_dbus_property_append_basic(&iter, property, type, value);
+
+       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_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;
+}
+
+DBusMessage *manager_set_session_mode(DBusConnection *connection,
+                                       connman_bool_t enable)
+{
+       return set_property(connection, "SessionMode",
+                               DBUS_TYPE_BOOLEAN, &enable);
+}
diff --git a/unit/session-api.c b/unit/session-api.c
new file mode 100644
index 0000000..63d6c46
--- /dev/null
+++ b/unit/session-api.c
@@ -0,0 +1,406 @@
+/*
+ *
+ *  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;
+}
+
+void bearer_info_cleanup(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, bearer_info_cleanup, 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)
+{
+       struct test_session *session = user_data;
+
+       LOG("session %p", session);
+
+       if (session->notify != NULL)
+               session->notify(session);
+
+       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, array;
+       GSList *allowed_bearers;
+
+       LOG("session %p notify %s", session, session->notify_path);
+
+       dbus_message_iter_init(msg, &iter);
+       dbus_message_iter_recurse(&iter, &array);
+
+       while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
+               DBusMessageIter entry, value;
+               const char *key;
+
+               dbus_message_iter_recurse(&array, &entry);
+               dbus_message_iter_get_basic(&entry, &key);
+
+               dbus_message_iter_next(&entry);
+               dbus_message_iter_recurse(&entry, &value);
+
+               switch (dbus_message_iter_get_arg_type(&value)) {
+               case DBUS_TYPE_ARRAY:
+                       if (g_str_equal(key, "AllowedBearers") == TRUE) {
+                               allowed_bearers = 
session_parse_allowed_bearers(&value);
+
+                               g_slist_foreach(info->allowed_bearers,
+                                               bearer_info_cleanup, NULL);
+                               g_slist_free(info->allowed_bearers);
+
+                               info->allowed_bearers = allowed_bearers;
+
+                       } else if (g_str_equal(key, "IPv4") == TRUE) {
+                               /* XXX */
+
+                       } else if (g_str_equal(key, "IPv6") == TRUE) {
+                               /* XXX */
+
+                       } else {
+                               g_assert(FALSE);
+                               return __connman_error_invalid_arguments(msg);
+                       }
+                       break;
+               case DBUS_TYPE_BOOLEAN:
+                       if (g_str_equal(key, "Online") == TRUE) {
+                               dbus_message_iter_get_basic(&value,
+                                                       &info->online);
+                       } else if (g_str_equal(key, "Priority") == TRUE) {
+                               dbus_message_iter_get_basic(&value,
+                                                       &info->priority);
+
+                       } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
+                               dbus_message_iter_get_basic(&value,
+                                                       &info->avoid_handover);
+
+                       } else if (g_str_equal(key, "StayConnected") == TRUE) {
+                               dbus_message_iter_get_basic(&value,
+                                                       &info->stay_connected);
+
+                       } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
+                               dbus_message_iter_get_basic(&value,
+                                                       &info->ecall);
+
+                       } else {
+                               g_assert(FALSE);
+                               return __connman_error_invalid_arguments(msg);
+                       }
+                       break;
+               case DBUS_TYPE_UINT32:
+                       if (g_str_equal(key, "PeriodicConnect") == TRUE) {
+                               dbus_message_iter_get_basic(&value,
+                                                       
&info->periodic_connect);
+
+                       } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
+                               dbus_message_iter_get_basic(&value,
+                                                       &info->idle_timeout);
+
+                       } else if (g_str_equal(key, "SessionMarker") == TRUE) {
+                               dbus_message_iter_get_basic(&value,
+                                                       &info->marker);
+
+                       } else {
+                               g_assert(FALSE);
+                               return __connman_error_invalid_arguments(msg);
+                       }
+                       break;
+               case DBUS_TYPE_STRING:
+                       if (g_str_equal(key, "Bearer") == TRUE) {
+                               const char *val;
+                               dbus_message_iter_get_basic(&value, &val);
+
+                               if (info->bearer != NULL)
+                                       g_free(info->bearer);
+
+                               info->bearer = g_strdup(val);
+
+                       } else if (g_str_equal(key, "Name") == TRUE) {
+                               const char *val;
+                               dbus_message_iter_get_basic(&value, &val);
+
+                               if (info->name != NULL)
+                                       g_free(info->name);
+
+                               info->name = g_strdup(val);
+
+                       } else if (g_str_equal(key, "RoamingPolicy") == TRUE) {
+                               const char *val;
+                               dbus_message_iter_get_basic(&value, &val);
+                               info->roaming_policy =
+                                       string2roamingpolicy(val);
+
+                       } else if (g_str_equal(key, "Interface") == TRUE) {
+                               const char *val;
+                               dbus_message_iter_get_basic(&value, &val);
+
+                               if (info->interface != NULL)
+                                       g_free(info->interface);
+
+                               info->interface = g_strdup(val);
+
+                       } else {
+                               g_assert(FALSE);
+                               return __connman_error_invalid_arguments(msg);
+                       }
+                       break;
+               default:
+                       g_assert(FALSE);
+                       return __connman_error_invalid_arguments(msg);
+               }
+               dbus_message_iter_next(&array);
+       }
+
+       if (session->notify != NULL)
+               session->notify(session);
+
+       return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
+}
+
+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->connection, notify_path,
+                       CONNMAN_NOTIFICATION_INTERFACE,
+                       notify_methods, NULL, NULL,
+                       session, NULL) == FALSE) {
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+int session_notify_unregister(struct test_session *session,
+                               const char *notify_path)
+{
+       if (g_dbus_unregister_interface(session->connection, 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);
+}
+
+DBusMessage *session_connect(DBusConnection *connection,
+                               struct test_session *session)
+{
+       DBusMessage *message, *reply;
+       DBusError error;
+
+       message = dbus_message_new_method_call(CONNMAN_SERVICE,
+                                               session->session_path,
+                                               CONNMAN_SESSION_INTERFACE,
+                                                       "Connect");
+       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 *session_disconnect(DBusConnection *connection,
+                                       struct test_session *session)
+{
+       DBusMessage *message, *reply;
+       DBusError error;
+
+       message = dbus_message_new_method_call(CONNMAN_SERVICE,
+                                               session->session_path,
+                                               CONNMAN_SESSION_INTERFACE,
+                                                       "Disconnect");
+       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;
+}
diff --git a/unit/test-connman.h b/unit/test-connman.h
new file mode 100644
index 0000000..f9d563d
--- /dev/null
+++ b/unit/test-connman.h
@@ -0,0 +1,146 @@
+/*
+ *
+ *  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);
+
+void util_session_create(struct test_fix *fix, unsigned int max_sessions);
+void util_session_destroy(gpointer fix);
+void util_session_init(struct test_session *session);
+void util_session_cleanup(struct test_session *session);
+
+
+/* session-api.c */
+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,
+};
+
+typedef void (* notify_cb) (struct test_session *session);
+
+struct test_session_info {
+       char *bearer;
+       connman_bool_t online;
+       char *name;
+       /* ipv4, ipv6 dicts */
+       GSList *allowed_bearers;
+       connman_bool_t priority;
+       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;
+       char *interface;
+       unsigned int marker;
+};
+
+struct test_session {
+       struct test_fix *fix;
+       gpointer user_data;
+
+       DBusConnection *connection;
+
+       char *session_path;
+       char *notify_path;
+       notify_cb notify;
+
+       struct test_session_info *info;
+};
+
+struct test_bearer_info {
+       char *name;
+};
+
+void bearer_info_cleanup(gpointer bearer_info, gpointer user_data);
+
+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);
+
+DBusMessage *session_connect(DBusConnection *connection,
+                               struct test_session *session);
+DBusMessage *session_disconnect(DBusConnection *connection,
+                                       struct test_session *session);
+
+/* manager-api.c */
+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);
+DBusMessage *manager_set_session_mode(DBusConnection *connection,
+                                       connman_bool_t enable);
+
+
+/* #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
diff --git a/unit/test-session.c b/unit/test-session.c
index 152c80f..6a928a6 100644
--- a/unit/test-session.c
+++ b/unit/test-session.c
@@ -23,13 +23,322 @@
 #include <config.h>
 #endif
 
-#include <gdbus.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
 
-#include "connman.h"
+#include "gdbus/gdbus.h"
+
+#include "test-connman.h"
+
+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 test_fix *fix = data;
+       DBusMessage *msg;
+
+       util_session_create(fix, 1);
+
+       msg = manager_create_session(fix->session->connection,
+                                       fix->session->info, "/foo");
+       g_assert(msg != NULL);
+       dbus_message_unref(msg);
+
+       g_assert(is_connman_running(fix->session->connection) == TRUE);
+       util_idle_call(fix, util_quit_loop, util_session_destroy);
+
+       return FALSE;
+}
+
+static gboolean test_session_destroy_no_notify(gpointer data)
+{
+       struct test_fix *fix = data;
+       DBusMessage *msg;
+
+       util_session_create(fix, 1);
+
+       msg = manager_destroy_session(fix->session->connection, "/foo");
+       g_assert(msg == NULL);
+
+       g_assert(is_connman_running(fix->session->connection) == TRUE);
+       util_idle_call(fix, util_quit_loop, util_session_destroy);
+
+       return FALSE;
+}
+
+static void test_session_create_notify(struct test_session *session)
+{
+       LOG("session %p", session);
+
+       g_assert(is_connman_running(session->connection) == TRUE);
+       util_idle_call(session->fix, util_quit_loop, util_session_destroy);
+}
+
+static gboolean test_session_create(gpointer data)
+{
+       struct test_fix *fix = data;
+       struct test_session *session;
+       DBusMessage *msg;
+       int err;
+
+       util_session_create(fix, 1);
+       session = fix->session;
+
+       session->notify_path = "/foo";
+       session->notify = test_session_create_notify;
+
+       err = session_notify_register(session, session->notify_path);
+       g_assert(err == 0);
+
+       msg = manager_create_session(session->connection,
+                                       session->info,
+                                       session->notify_path);
+       g_assert(msg != NULL);
+       dbus_message_unref(msg);
+
+       return FALSE;
+}
+
+static gboolean test_session_create_already_exists(gpointer data)
+{
+       struct test_fix *fix = data;
+       struct test_session *session1, *session2;
+       DBusMessage *msg;
+
+       util_session_create(fix, 2);
+       session1 = &fix->session[0];
+       session2 = &fix->session[1];
+
+       session1->notify_path = g_strdup("/foo");
+       session2->notify_path = session1->notify_path;
+
+       util_session_init(session1);
+
+       msg = manager_create_session(session2->connection,
+                                       session2->info,
+                                       session2->notify_path);
+       g_assert(msg == NULL);
+
+       util_session_cleanup(session1);
+
+       g_assert(is_connman_running(session1->connection) == TRUE);
+       util_idle_call(fix, util_quit_loop, util_session_destroy);
+
+       return FALSE;
+}
+
+static gboolean test_session_create_destroy(gpointer data)
+{
+       struct test_fix *fix = data;
+       struct test_session *session;
+
+       util_session_create(fix, 1);
+       session = fix->session;
+
+       session->notify_path = g_strdup("/foo");
+
+       util_session_init(fix->session);
+       util_session_cleanup(fix->session);
+
+       g_assert(is_connman_running(session->connection) == TRUE);
+       util_idle_call(fix, util_quit_loop, util_session_destroy);
+
+       return FALSE;
+}
+
+static void test_session_create_many_notify(struct test_session *session)
+{
+       unsigned int nr;
+
+       LOG("session %p", session);
+
+       g_assert(is_connman_running(session->connection) == TRUE);
+
+       nr = GPOINTER_TO_UINT(session->fix->user_data);
+       nr--;
+       session->fix->user_data = GUINT_TO_POINTER(nr);
+
+       if (nr > 0)
+               return;
+
+       util_idle_call(session->fix, util_quit_loop, util_session_destroy);
+}
+
+static gboolean test_session_create_many(gpointer data)
+{
+       struct test_fix *fix = data;
+       struct test_session *session;
+       unsigned int i, max;
+
+       max = 100;
+
+       fix->user_data = GUINT_TO_POINTER(max);
+
+       util_session_create(fix, max);
+
+       for (i = 0; i < max; i++) {
+               session = &fix->session[i];
+
+               session->notify_path = g_strdup_printf("/foo/%d", i);
+               session->notify = test_session_create_many_notify;
+
+               util_session_init(session);
+       }
+
+       return FALSE;
+}
+
+static void test_session_connect_notify(struct test_session *session)
+{
+       LOG("session %p", session);
+
+       if (session->info->online != TRUE)
+               return;
+
+       util_session_cleanup(session);
+
+       g_assert(is_connman_running(session->connection) == TRUE);
+       util_idle_call(session->fix, util_quit_loop, util_session_destroy);
+}
+
+static gboolean test_session_connect(gpointer data)
+{
+       struct test_fix *fix = data;
+       struct test_session *session;
+       DBusMessage *msg;
+
+       util_session_create(fix, 1);
+       session = fix->session;
+
+       session->notify_path = g_strdup("/foo");
+       session->notify =  test_session_connect_notify;
+       util_session_init(session);
+
+       msg = session_connect(session->connection, session);
+       g_assert(msg != NULL);
+       dbus_message_unref(msg);
+
+       return FALSE;
+}
+
+static void test_session_disconnect_notify(struct test_session *session)
+{
+       LOG("session %p", session);
+
+       if (session->info->online != FALSE)
+               return;
+
+       util_session_cleanup(session);
+
+       g_assert(is_connman_running(session->connection) == TRUE);
+       util_idle_call(session->fix, util_quit_loop, util_session_destroy);
+}
+
+static gboolean test_session_disconnect(gpointer data)
+{
+       struct test_fix *fix = data;
+       struct test_session *session;
+       DBusMessage *msg;
+
+       util_session_create(fix, 1);
+       session = fix->session;
+
+       session->notify_path = g_strdup("/foo");
+       session->notify =  test_session_disconnect_notify;
+       util_session_init(session);
+
+       msg = session_disconnect(session->connection, session);
+       g_assert(msg != NULL);
+       dbus_message_unref(msg);
+
+       return FALSE;
+}
+
+static void set_session_mode(struct test_fix *fix,
+                                       connman_bool_t enable)
+{
+       DBusMessage *msg;
+
+       msg = manager_set_session_mode(fix->main_connection, enable);
+       g_assert(msg != NULL);
+       dbus_message_unref(msg);
+
+       util_idle_call(fix, util_quit_loop, NULL);
+}
+
+static gboolean enable_session_mode(gpointer data)
+{
+       struct test_fix *fix = data;
+
+       set_session_mode(fix, TRUE);
+
+       return FALSE;
+}
+
+static gboolean disable_session_mode(gpointer data)
+{
+       struct test_fix *fix = data;
+
+       set_session_mode(fix, FALSE);
+
+       return FALSE;
+}
+
+static void setup_cb(struct test_fix *fix, gconstpointer data)
+{
+       util_setup(fix, data);
+
+       util_call(fix, enable_session_mode, NULL);
+       g_main_loop_run(fix->main_loop);
+}
+
+static void teardown_cb(struct test_fix *fix, gconstpointer data)
+{
+       util_call(fix, disable_session_mode, NULL);
+       g_main_loop_run(fix->main_loop);
+
+       util_teardown(fix, data);
+}
 
 int main(int argc, char *argv[])
 {
        g_test_init(&argc, &argv, NULL);
 
+       util_test_add("/manager/session create no notify",
+               test_session_create_no_notify, setup_cb, teardown_cb);
+       util_test_add("/manager/session destroy no notify",
+               test_session_destroy_no_notify, setup_cb, teardown_cb);
+       util_test_add("/manager/session create",
+               test_session_create, setup_cb, teardown_cb);
+       util_test_add("/manager/session create already exists",
+               test_session_create_already_exists, setup_cb, teardown_cb);
+       util_test_add("/manager/session create destroy",
+               test_session_create_destroy, setup_cb, teardown_cb);
+       util_test_add("/manager/session create many",
+               test_session_create_many, setup_cb, teardown_cb);
+
+       util_test_add("/session/connect",
+               test_session_connect, setup_cb, teardown_cb);
+       util_test_add("/session/disconnect",
+               test_session_disconnect, setup_cb, teardown_cb);
+
        return g_test_run();
 }
diff --git a/unit/utils.c b/unit/utils.c
new file mode 100644
index 0000000..ec9f69b
--- /dev/null
+++ b/unit/utils.c
@@ -0,0 +1,183 @@
+/*
+ *
+ *  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"
+
+#define ENABLE_WRAPPER 0
+
+void util_session_create(struct test_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].fix = fix;
+               fix->session[i].info = g_try_new0(struct test_session_info, 1);
+               fix->session[i].connection = g_dbus_setup_private(
+                                               DBUS_BUS_SYSTEM, NULL, NULL);
+       }
+}
+
+void util_session_destroy(gpointer data)
+{
+       struct test_fix *fix = data;
+
+       unsigned int i;
+
+       for (i = 0; i < fix->max_sessions; i++) {
+               dbus_connection_close(fix->session[i].connection);
+               g_free(fix->session[i].info);
+       }
+
+       g_free(fix->session);
+}
+
+void util_session_init(struct test_session *session)
+{
+       DBusMessage *msg;
+       DBusMessageIter iter;
+       const char *path;
+       int err;
+
+       err = session_notify_register(session, session->notify_path);
+       g_assert(err == 0);
+
+       msg = manager_create_session(session->connection,
+                                       session->info,
+                                       session->notify_path);
+       g_assert(msg != NULL);
+       dbus_message_iter_init(msg, &iter);
+
+       dbus_message_iter_get_basic(&iter, &path);
+       session->session_path = g_strdup(path);
+
+       dbus_message_unref(msg);
+}
+
+void util_session_cleanup(struct test_session *session)
+{
+       DBusMessage *msg;
+       int err;
+
+       msg = manager_destroy_session(session->connection,
+                                       session->session_path);
+       g_assert(msg != NULL);
+       dbus_message_unref(msg);
+
+       err = session_notify_unregister(session,
+                                       session->notify_path);
+       g_assert(err == 0);
+
+       g_free(session->info->bearer);
+       g_free(session->info->name);
+       g_free(session->info->interface);
+       g_slist_foreach(session->info->allowed_bearers,
+                       bearer_info_cleanup, NULL);
+       g_slist_free(session->info->allowed_bearers);
+
+       g_free(session->notify_path);
+       g_free(session->session_path);
+}
+
+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);
+}
-- 
1.7.4.4

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

Reply via email to