diff options
author | Daniel Wagner <daniel.wagner@bmw-carit.de> | 2013-03-12 18:16:33 +0100 |
---|---|---|
committer | Patrik Flykt <patrik.flykt@linux.intel.com> | 2013-03-18 14:31:25 +0200 |
commit | 516813cd4372bef22681137adf9d13c985bb7b99 (patch) | |
tree | 970abceba43d30f6b31838aa70c54345e818bfa5 /tools | |
parent | 1838dd68416a52ab47b9c7e42f4442c093989174 (diff) | |
download | connman-516813cd4372bef22681137adf9d13c985bb7b99.tar.gz connman-516813cd4372bef22681137adf9d13c985bb7b99.tar.bz2 connman-516813cd4372bef22681137adf9d13c985bb7b99.zip |
test-session: Move file to tools and rename it to session-test
These tests depens on ConnMan running and therefore not really a
unit tests. Let's move it to tools.
Diffstat (limited to 'tools')
-rw-r--r-- | tools/manager-api.c | 262 | ||||
-rw-r--r-- | tools/session-api.c | 330 | ||||
-rw-r--r-- | tools/session-test.c | 588 | ||||
-rw-r--r-- | tools/session-test.h | 145 | ||||
-rw-r--r-- | tools/session-utils.c | 261 |
5 files changed, 1586 insertions, 0 deletions
diff --git a/tools/manager-api.c b/tools/manager-api.c new file mode 100644 index 00000000..2a182d7f --- /dev/null +++ b/tools/manager-api.c @@ -0,0 +1,262 @@ +/* + * + * 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 "session-test.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_get_properties(DBusConnection *connection) +{ + DBusMessage *message, *reply; + DBusError error; + + message = dbus_message_new_method_call(CONNMAN_SERVICE, + CONNMAN_MANAGER_PATH, + CONNMAN_MANAGER_INTERFACE, + "GetProperties"); + 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("%s", error.message); + } + 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, + ¬ifier_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, + ¬ifier_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); +} + +int manager_parse_properties(DBusMessage *msg, + struct test_manager *manager) +{ + DBusMessageIter iter, array; + + 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_STRING: + if (g_str_equal(key, "State") == TRUE) { + const char *val; + dbus_message_iter_get_basic(&value, &val); + + if (manager->state != NULL) + g_free(manager->state); + + LOG("State %s", val); + + manager->state = g_strdup(val); + } + break; + default: + break; + } + dbus_message_iter_next(&array); + } + + return 0; +} diff --git a/tools/session-api.c b/tools/session-api.c new file mode 100644 index 00000000..72c0b09d --- /dev/null +++ b/tools/session-api.c @@ -0,0 +1,330 @@ +/* + * + * 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 <errno.h> + +#include <gdbus.h> + +#include "session-test.h" + +static enum connman_session_state string2state(const char *state) +{ + if (g_strcmp0(state, "connected") == 0) + return CONNMAN_SESSION_STATE_CONNECTED; + if (g_strcmp0(state, "online") == 0) + return CONNMAN_SESSION_STATE_ONLINE; + + return CONNMAN_SESSION_STATE_DISCONNECTED; +} + +static enum connman_session_type string2type(const char *type) +{ + if (g_strcmp0(type, "any") == 0) + return CONNMAN_SESSION_TYPE_ANY; + if (g_strcmp0(type, "local") == 0) + return CONNMAN_SESSION_TYPE_LOCAL; + if (g_strcmp0(type, "internet") == 0) + return CONNMAN_SESSION_TYPE_INTERNET; + + return CONNMAN_SESSION_TYPE_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_STRING: + if (g_str_equal(key, "State") == TRUE) { + const char *val; + dbus_message_iter_get_basic(&value, &val); + + info->state = string2state(val); + } else 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, "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 if (g_str_equal(key, "ConnectionType") + == TRUE) { + const char *val; + dbus_message_iter_get_basic(&value, &val); + + info->type = string2type(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 const GDBusMethodTable notify_methods[] = { + { GDBUS_METHOD("Release", NULL, NULL, notify_release) }, + { GDBUS_METHOD("Update", + GDBUS_ARGS({ "settings", "a{sv}" }), NULL, + 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 *bearer_info = list->data; + + dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, + &bearer_info->name); + } +} + +void session_append_settings(DBusMessageIter *dict, + struct test_session_info *info) +{ + if (info->allowed_bearers == NULL) + return; + + connman_dbus_dict_append_array(dict, "AllowedBearers", + DBUS_TYPE_STRING, + append_allowed_bearers, + info); +} + +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/tools/session-test.c b/tools/session-test.c new file mode 100644 index 00000000..c296b95c --- /dev/null +++ b/tools/session-test.c @@ -0,0 +1,588 @@ +/* + * + * 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 <gdbus.h> + +#include "session-test.h" + +enum test_session_state { + TEST_SESSION_STATE_0 = 0, + TEST_SESSION_STATE_1 = 1, + TEST_SESSION_STATE_2 = 2, + TEST_SESSION_STATE_3 = 3, +}; + +static enum test_session_state get_session_state(struct test_session *session) +{ + return GPOINTER_TO_UINT(session->fix->user_data); +} + +static void set_session_state(struct test_session *session, + enum test_session_state state) +{ + session->fix->user_data = GUINT_TO_POINTER(state); +} + +static struct test_session *get_session(struct test_session *session, + unsigned int index) +{ + return &session->fix->session[index]; +} + +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); + g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR); + + dbus_message_unref(msg); + + 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); + + 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); + + 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); + g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR); + + dbus_message_unref(msg); + + 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); + + util_idle_call(fix, util_quit_loop, util_session_destroy); + + return FALSE; +} + +static gboolean test_session_create_already_exists(gpointer data) +{ + struct test_fix *fix = data; + struct test_session *session0, *session1; + DBusMessage *msg; + + util_session_create(fix, 2); + session0 = &fix->session[0]; + session1 = &fix->session[1]; + + session0->notify_path = g_strdup("/foo"); + session1->notify_path = session0->notify_path; + + util_session_init(session0); + + msg = manager_create_session(session1->connection, + session1->info, + session1->notify_path); + g_assert(msg == NULL); + + util_session_cleanup(session0); + + 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); + + 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 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); + g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR); + + dbus_message_unref(msg); +} + +static void test_session_connect_notify(struct test_session *session) +{ + LOG("session %p state %d", session, session->info->state); + + if (session->info->state == CONNMAN_SESSION_STATE_DISCONNECTED) + return; + + util_session_cleanup(session); + + 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); + g_assert(dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_ERROR); + + dbus_message_unref(msg); + + return FALSE; +} + +static void test_session_disconnect_notify(struct test_session *session) +{ + LOG("session %p state %d", session, session->info->state); + + if (session->info->state >= CONNMAN_SESSION_STATE_CONNECTED) + return; + + util_session_cleanup(session); + + 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 test_session_connect_disconnect_notify(struct test_session *session) +{ + enum test_session_state state = get_session_state(session); + enum test_session_state next_state = state; + DBusMessage *msg; + + LOG("state %d session %p %s state %d", state, session, + session->notify_path, session->info->state); + + switch (state) { + case TEST_SESSION_STATE_0: + if (session->info->state == CONNMAN_SESSION_STATE_DISCONNECTED) + next_state = TEST_SESSION_STATE_1; + break; + case TEST_SESSION_STATE_1: + if (session->info->state >= CONNMAN_SESSION_STATE_CONNECTED) + next_state = TEST_SESSION_STATE_2; + break; + case TEST_SESSION_STATE_2: + if (session->info->state == CONNMAN_SESSION_STATE_DISCONNECTED) + next_state = TEST_SESSION_STATE_3; + default: + break; + } + + if (state == next_state) + return; + + set_session_state(session, next_state); + + LOG("next_state %d", next_state); + + switch (next_state) { + case TEST_SESSION_STATE_1: + msg = session_connect(session->connection, session); + g_assert(msg != NULL); + dbus_message_unref(msg); + return; + case TEST_SESSION_STATE_2: + msg = session_disconnect(session->connection, session); + g_assert(msg != NULL); + dbus_message_unref(msg); + return; + case TEST_SESSION_STATE_3: + util_session_cleanup(session); + util_idle_call(session->fix, util_quit_loop, + util_session_destroy); + return; + default: + return; + } +} + +static gboolean test_session_connect_disconnect(gpointer data) +{ + struct test_fix *fix = data; + struct test_session *session; + + /* + * +-------------------+ + * | START | + * +-------------------+ + * | + * | connect foo + * v + * +-------------------+ + * | FOO-CONNECTED | + * +-------------------+ + * | + * | disconnect foo + * v + * +-------------------+ + * | END | + * +-------------------+ + */ + + util_session_create(fix, 1); + session = fix->session; + + session->notify_path = g_strdup("/foo"); + session->notify = test_session_connect_disconnect_notify; + + util_session_init(session); + + set_session_state(session, TEST_SESSION_STATE_0); + + return FALSE; +} + +static void test_session_connect_free_ride_notify(struct test_session *session) +{ + struct test_session *session0 = get_session(session, 0); + struct test_session *session1 = get_session(session, 1); + enum test_session_state state = get_session_state(session); + enum test_session_state next_state = state; + DBusMessage *msg; + + LOG("state %d session %p %s state %d", state, session, + session->notify_path, session->info->state); + + switch (state) { + case TEST_SESSION_STATE_0: + if (session0->info->state == CONNMAN_SESSION_STATE_DISCONNECTED + && session1->info->state == + CONNMAN_SESSION_STATE_DISCONNECTED) { + next_state = TEST_SESSION_STATE_1; + } + + break; + case TEST_SESSION_STATE_1: + if (session0->info->state >= CONNMAN_SESSION_STATE_CONNECTED && + session1->info->state >= + CONNMAN_SESSION_STATE_CONNECTED) { + next_state = TEST_SESSION_STATE_2; + } + + break; + case TEST_SESSION_STATE_2: + if (session0->info->state == CONNMAN_SESSION_STATE_DISCONNECTED + && session1->info->state == + CONNMAN_SESSION_STATE_DISCONNECTED) { + next_state = TEST_SESSION_STATE_3; + } + + break; + case TEST_SESSION_STATE_3: + + return; + } + + if (state == next_state) + return; + + set_session_state(session, next_state); + + LOG("next_state %d", next_state); + + switch (next_state) { + case TEST_SESSION_STATE_0: + + return; + case TEST_SESSION_STATE_1: + msg = session_connect(session0->connection, session0); + g_assert(msg != NULL); + dbus_message_unref(msg); + + return; + + case TEST_SESSION_STATE_2: + msg = session_disconnect(session0->connection, session0); + g_assert(msg != NULL); + dbus_message_unref(msg); + + return; + case TEST_SESSION_STATE_3: + util_session_cleanup(session0); + util_session_cleanup(session1); + + util_idle_call(session0->fix, util_quit_loop, + util_session_destroy); + + return; + } +} + +static gboolean test_session_connect_free_ride(gpointer data) +{ + struct test_fix *fix = data; + struct test_session *session0, *session1; + + /* + * +-------------------+ + * | START | + * +-------------------+ + * | + * | connect foo + * v + * +-------------------+ + * | FOO-CONNECTED | + * +-------------------+ + * | + * | free-ride bar + * v + * +-------------------+ + * | FOO-BAR-CONNECTED | + * +-------------------+ + * | + * | disconnect foo + * v + * +-------------------+ + * | END | + * +-------------------+ + */ + + util_session_create(fix, 2); + session0 = &fix->session[0]; + session1 = &fix->session[1]; + + session0->notify_path = g_strdup("/foo"); + session1->notify_path = g_strdup("/bar"); + session0->notify = test_session_connect_free_ride_notify; + session1->notify = test_session_connect_free_ride_notify; + + util_session_init(session0); + util_session_init(session1); + + set_session_state(session0, TEST_SESSION_STATE_0); + + return FALSE; +} + +static connman_bool_t is_online(struct test_fix *fix) +{ + if (g_strcmp0(fix->manager.state, "online") == 0) + return TRUE; + + return FALSE; +} + +static gboolean enable_session_mode(gpointer data) +{ + struct test_fix *fix = data; + + set_session_mode(fix, TRUE); + + if (is_online(fix) == FALSE) + util_idle_call(fix, util_quit_loop, NULL); + + return FALSE; +} + +static gboolean manager_state_changed(gpointer data) +{ + struct test_fix *fix = data; + + if (is_online(fix) == FALSE) { + fix->manager_changed = NULL; + util_idle_call(fix, util_quit_loop, NULL); + } + + 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) +{ + fix->manager_changed = manager_state_changed; + + util_setup(fix, data); + util_call(fix, enable_session_mode, NULL); + + g_main_loop_run(fix->main_loop); + + fix->manager_changed = NULL; +} + +static void teardown_cb(struct test_fix *fix, gconstpointer data) +{ + util_call(fix, disable_session_mode, NULL); + util_idle_call(fix, util_quit_loop, 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 destroy", + test_session_create_destroy, 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 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); + util_test_add("/session/connect disconnect", + test_session_connect_disconnect, setup_cb, teardown_cb); + util_test_add("/session/connect free-ride", + test_session_connect_free_ride, setup_cb, teardown_cb); + + return g_test_run(); +} diff --git a/tools/session-test.h b/tools/session-test.h new file mode 100644 index 00000000..a21f5269 --- /dev/null +++ b/tools/session-test.h @@ -0,0 +1,145 @@ +/* + * + * Connection Manager + * + * Copyright (C) 2007-2012 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 "../src/connman.h" + +struct test_session; + +struct test_manager { + char *state; +}; + +struct test_fix { + gpointer user_data; + + GMainLoop *main_loop; + DBusConnection *main_connection; + guint watch; + guint manager_watch; + + struct test_manager manager; + GSourceFunc manager_changed; + + /* 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); + +typedef void (* notify_cb) (struct test_session *session); + +enum connman_session_state { + CONNMAN_SESSION_STATE_DISCONNECTED = 0, + CONNMAN_SESSION_STATE_CONNECTED = 1, + CONNMAN_SESSION_STATE_ONLINE = 2, +}; + +struct test_session_info { + enum connman_session_state state; + char *name; + char *bearer; + char *interface; + enum connman_session_type type; + /* ipv4, ipv6 dicts */ + GSList *allowed_bearers; +}; + +struct test_session { + gpointer user_data; + + struct test_fix *fix; + DBusConnection *connection; + + char *session_path; + char *notify_path; + notify_cb notify; + + struct test_session_info *info; +}; + +struct test_bearer_info { + char *name; +}; + +/* session-api.c */ +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_get_properties(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); +int manager_parse_properties(DBusMessage *msg, + struct test_manager *manager); + +/* #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/tools/session-utils.c b/tools/session-utils.c new file mode 100644 index 00000000..6a3d9918 --- /dev/null +++ b/tools/session-utils.c @@ -0,0 +1,261 @@ +/* + * + * 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.h> + +#include "session-test.h" + +#define ENABLE_WRAPPER 1 +#define PROPERTY_CHANGED "PropertyChanged" + +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; +} + +static void connman_died(DBusConnection *connection, void *user_data) +{ + g_assert(FALSE); +} + +static void manager_changed(struct test_fix *fix, + DBusMessageIter *entry) +{ + DBusMessageIter iter; + const char *key; + const char *value; + int type; + + dbus_message_iter_get_basic(entry, &key); + + LOG("key %s", key); + + dbus_message_iter_next(entry); + + dbus_message_iter_recurse(entry, &iter); + + type = dbus_message_iter_get_arg_type(&iter); + + if (type != DBUS_TYPE_STRING) + return; + + dbus_message_iter_get_basic(&iter, &value); + + if (g_str_equal(key, "State") == TRUE) { + LOG("State %s", value); + + if (fix->manager.state != NULL) + g_free(fix->manager.state); + + fix->manager.state = g_strdup(value); + } + + if (fix->manager_changed != NULL) + fix->manager_changed(fix); +} + +static gboolean handle_manager_changed(DBusConnection *connection, + DBusMessage *message, + void *user_data) +{ + struct test_fix *fix = user_data; + + DBusMessageIter iter; + + if (dbus_message_iter_init(message, &iter)) + manager_changed(fix, &iter); + + return TRUE; +} + +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) +{ + DBusMessage *msg; + + fix->main_loop = g_main_loop_new(NULL, FALSE); + fix->main_connection = g_dbus_setup_private(DBUS_BUS_SYSTEM, + NULL, NULL); + fix->watch = g_dbus_add_service_watch(fix->main_connection, + CONNMAN_SERVICE, + NULL, + connman_died, + NULL, NULL); + fix->manager_watch = g_dbus_add_signal_watch(fix->main_connection, + CONNMAN_SERVICE, NULL, + CONNMAN_MANAGER_INTERFACE, + PROPERTY_CHANGED, + handle_manager_changed, + fix, NULL); + + msg = manager_get_properties(fix->main_connection); + manager_parse_properties(msg, &fix->manager); + dbus_message_unref(msg); +} + +void util_teardown(struct test_fix *fix, gconstpointer data) +{ + g_dbus_remove_watch(fix->main_connection, fix->watch); + g_dbus_remove_watch(fix->main_connection, fix->manager_watch); + 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); +} + +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); + + session->notify = NULL; + g_free(session->notify_path); + g_free(session->session_path); +} |