X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=tools%2Fsession-utils.c;h=47f0de1f1509bcc121c36ff5b1e273e9134fd641;hb=b397386967c5057c4dcf73f63f7ffa16141efea4;hp=6a3d991829eab6235beeda3afea3636241ecb89a;hpb=516813cd4372bef22681137adf9d13c985bb7b99;p=platform%2Fupstream%2Fconnman.git diff --git a/tools/session-utils.c b/tools/session-utils.c old mode 100644 new mode 100755 index 6a3d991..47f0de1 --- a/tools/session-utils.c +++ b/tools/session-utils.c @@ -2,7 +2,7 @@ * * Connection Manager * - * Copyright (C) 2011 BWM CarIT GmbH. All rights reserved. + * Copyright (C) 2011-2014 BMW Car IT GmbH. * * 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 @@ -25,34 +25,70 @@ #include +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" + #include +#include "../src/shared/util.h" #include "session-test.h" #define ENABLE_WRAPPER 1 #define PROPERTY_CHANGED "PropertyChanged" -gboolean util_quit_loop(gpointer data) +void util_quit_loop(struct test_fix *fix) { - struct test_fix *fix = data; - g_main_loop_quit(fix->main_loop); +} + +static gboolean func_cb(gpointer data) +{ + struct cb_data *cbd = data; + util_test_func_t cb = cbd->cb; + struct test_fix *fix = cbd->user_data; + + (*cb)(fix); return FALSE; } -guint util_idle_call(struct test_fix *fix, GSourceFunc func, - GDestroyNotify notify) +static void destroy_cb(gpointer data) { + struct cb_data *cbd = data; + util_test_func_t cb = cbd->data; + struct test_fix *fix = cbd->user_data; + + if (cb) + (*cb)(fix); + + g_free(cbd); +} + +void util_call(struct test_fix *fix, util_test_func_t func, + util_test_func_t destroy) +{ + struct cb_data *cbd = cb_data_new(func, fix); 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)); + cbd->data = destroy; + + source = g_timeout_source_new(0); + g_source_set_callback(source, func_cb, cbd, destroy_cb); + g_source_attach(source, g_main_loop_get_context(fix->main_loop)); g_source_unref(source); +} + +void util_idle_call(struct test_fix *fix, util_test_func_t func, + util_test_func_t destroy) +{ + struct cb_data *cbd = cb_data_new(func, fix); + GSource *source; - return id; + cbd->data = destroy; + + source = g_idle_source_new(); + g_source_set_callback(source, func_cb, cbd, destroy_cb); + g_source_attach(source, g_main_loop_get_context(fix->main_loop)); + g_source_unref(source); } static void connman_died(DBusConnection *connection, void *user_data) @@ -60,8 +96,7 @@ static void connman_died(DBusConnection *connection, void *user_data) g_assert(FALSE); } -static void manager_changed(struct test_fix *fix, - DBusMessageIter *entry) +static void manager_changed(struct test_fix *fix, DBusMessageIter *entry) { DBusMessageIter iter; const char *key; @@ -83,16 +118,16 @@ static void manager_changed(struct test_fix *fix, dbus_message_iter_get_basic(&iter, &value); - if (g_str_equal(key, "State") == TRUE) { + if (g_str_equal(key, "State")) { LOG("State %s", value); - if (fix->manager.state != NULL) + if (fix->manager.state) g_free(fix->manager.state); fix->manager.state = g_strdup(value); } - if (fix->manager_changed != NULL) + if (fix->manager_changed) fix->manager_changed(fix); } @@ -110,24 +145,13 @@ static gboolean handle_manager_changed(DBusConnection *connection, 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) +static struct test_fix *create_fix(void) { + struct test_fix *fix; DBusMessage *msg; + fix = g_new0(struct test_fix, 1); + fix->main_loop = g_main_loop_new(NULL, FALSE); fix->main_connection = g_dbus_setup_private(DBUS_BUS_SYSTEM, NULL, NULL); @@ -146,9 +170,11 @@ void util_setup(struct test_fix *fix, gconstpointer data) msg = manager_get_properties(fix->main_connection); manager_parse_properties(msg, &fix->manager); dbus_message_unref(msg); + + return fix; } -void util_teardown(struct test_fix *fix, gconstpointer data) +static void cleanup_fix(struct test_fix *fix) { g_dbus_remove_watch(fix->main_connection, fix->watch); g_dbus_remove_watch(fix->main_connection, fix->manager_watch); @@ -156,31 +182,62 @@ void util_teardown(struct test_fix *fix, gconstpointer data) dbus_connection_unref(fix->main_connection); g_main_loop_unref(fix->main_loop); + + g_free(fix); } -static void util_wrapper(struct test_fix *fix, gconstpointer data) +struct test_data_cb { + util_test_func_t func; + util_test_func_t setup; + util_test_func_t teardown; +}; + +static void run_test_cb(gpointer fixture, gconstpointer data) { - GSourceFunc func = data; + const struct test_data_cb *cbd = data; + struct test_fix *fix; + + fix = create_fix(); + + util_call(fix, cbd->setup, NULL); + g_main_loop_run(fix->main_loop); + #if ENABLE_WRAPPER - if (g_test_trap_fork(60 * 1000 * 1000, 0) == TRUE) { - util_call(fix, func, NULL); + if (g_test_trap_fork(60 * 1000 * 1000, 0)) { + util_call(fix, cbd->func, NULL); g_main_loop_run(fix->main_loop); exit(0); } g_test_trap_assert_passed(); #else - util_call(fix, func, NULL); + util_call(fix, cbd->func, NULL); g_main_loop_run(fix->main_loop); #endif + + util_call(fix, cbd->teardown, NULL); + g_main_loop_run(fix->main_loop); + + cleanup_fix(fix); } -void util_test_add(const char *test_name, GSourceFunc test_func, - util_test_setup_cb setup_cb, - util_test_teardown_cb teardown_cb) +static void cleanup_test_cb(gpointer fixture, gconstpointer data) { - g_test_add(test_name, struct test_fix, test_func, - setup_cb, util_wrapper, teardown_cb); + struct test_data_cb *cbd = (void *)data; + + g_free(cbd); +} + +void util_test_add(const char *test_name, util_test_func_t test_func, + util_test_func_t setup, util_test_func_t teardown) +{ + struct test_data_cb *cbd = g_new0(struct test_data_cb, 1); + + cbd->func = test_func; + cbd->setup = setup; + cbd->teardown = teardown; + + g_test_add_vtable(test_name, 0, cbd, NULL, run_test_cb, cleanup_test_cb); } void util_session_create(struct test_fix *fix, unsigned int max_sessions) @@ -198,10 +255,8 @@ void util_session_create(struct test_fix *fix, unsigned int max_sessions) } } -void util_session_destroy(gpointer data) +void util_session_destroy(struct test_fix *fix) { - struct test_fix *fix = data; - unsigned int i; for (i = 0; i < fix->max_sessions; i++) { @@ -225,7 +280,7 @@ void util_session_init(struct test_session *session) msg = manager_create_session(session->connection, session->info, session->notify_path); - g_assert(msg != NULL); + g_assert(msg); dbus_message_iter_init(msg, &iter); dbus_message_iter_get_basic(&iter, &path); @@ -241,7 +296,7 @@ void util_session_cleanup(struct test_session *session) msg = manager_destroy_session(session->connection, session->session_path); - g_assert(msg != NULL); + g_assert(msg); dbus_message_unref(msg); err = session_notify_unregister(session, @@ -254,6 +309,8 @@ void util_session_cleanup(struct test_session *session) g_slist_foreach(session->info->allowed_bearers, bearer_info_cleanup, NULL); g_slist_free(session->info->allowed_bearers); + g_free(session->info->allowed_interface); + g_free(session->info->context_identifier); session->notify = NULL; g_free(session->notify_path);