diff --git a/mms-lib/test/common/test_util.c b/mms-lib/test/common/test_util.c index 51732e2..c63aecb 100644 --- a/mms-lib/test/common/test_util.c +++ b/mms-lib/test/common/test_util.c @@ -94,6 +94,33 @@ test_init( GLOG_LEVEL_NONE; } +/* Run main loop with a timeout */ + +static +gboolean +test_timeout_expired( + gpointer data) +{ + g_assert_not_reached(); + return G_SOURCE_REMOVE; +} + +void +test_run_loop( + const TestOpt* opt, + GMainLoop* loop) +{ + if (opt->flags & TEST_FLAG_DEBUG) { + g_main_loop_run(loop); + } else { + const guint timeout_id = g_timeout_add_seconds(TEST_TIMEOUT_SEC, + test_timeout_expired, NULL); + + g_main_loop_run(loop); + g_source_remove(timeout_id); + } +} + /* * Local Variables: * mode: C diff --git a/mms-lib/test/common/test_util.h b/mms-lib/test/common/test_util.h index 19a69af..90db1c9 100644 --- a/mms-lib/test/common/test_util.h +++ b/mms-lib/test/common/test_util.h @@ -18,6 +18,8 @@ #include "mms_lib_types.h" +#define TEST_TIMEOUT_SEC (10) + typedef struct test_opt { int flags; } TestOpt; @@ -47,6 +49,12 @@ test_init( int* argc, char** argv); +/* Run main loop with a timeout */ +void +test_run_loop( + const TestOpt* opt, + GMainLoop* loop); + #endif /* TEST_UTIL_H */ /* diff --git a/mms-lib/test/test_delivery_ind/Makefile b/mms-lib/test/test_delivery_ind/Makefile index 55e1a47..24a9956 100644 --- a/mms-lib/test/test_delivery_ind/Makefile +++ b/mms-lib/test/test_delivery_ind/Makefile @@ -1,7 +1,6 @@ # -*- Mode: makefile-gmake -*- EXE = test_delivery_ind -SRC = $(EXE).c COMMON_SRC = test_connection.c test_connman.c test_handler.c \ test_transfer_list.c test_util.c diff --git a/mms-lib/test/test_delivery_ind/test_delivery_ind.c b/mms-lib/test/test_delivery_ind/test_delivery_ind.c index 60f0a03..4578a7f 100644 --- a/mms-lib/test/test_delivery_ind/test_delivery_ind.c +++ b/mms-lib/test/test_delivery_ind/test_delivery_ind.c @@ -1,6 +1,6 @@ /* - * Copyright (C) 2013-2018 Jolla Ltd. - * Copyright (C) 2013-2018 Slava Monich + * Copyright (C) 2013-2020 Jolla Ltd. + * Copyright (C) 2013-2020 Slava Monich * * 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 @@ -8,7 +8,7 @@ * * 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 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ @@ -25,11 +25,9 @@ #include #include -#define RET_OK (0) -#define RET_ERR (1) -#define RET_TIMEOUT (2) +#define DATA_DIR "data" -#define DATA_DIR "data/" +static TestOpt test_opt; typedef struct test_desc { const char* name; @@ -46,10 +44,8 @@ typedef struct test { MMSConnMan* cm; MMSHandler* handler; MMSDispatcher* disp; - GMappedFile* notification_ind; + GMappedFile* pdu_file; GMainLoop* loop; - guint timeout_id; - int ret; } Test; static const TestDesc delivery_tests[] = { @@ -73,203 +69,107 @@ static const TestDesc delivery_tests[] = { static void -test_finish( +test_delivery_ind_done_finish( Test* test) { const TestDesc* desc = test->desc; - const char* name = desc->name; - if (test->ret == RET_OK) { - MMS_DELIVERY_STATUS ds; - ds = mms_handler_test_delivery_status(test->handler, test->id); - if (ds != desc->status) { - test->ret = RET_ERR; - GERR("%s status %d, expected %d", name, ds, desc->status); - } - } - GINFO("%s: %s", (test->ret == RET_OK) ? "OK" : "FAILED", name); + MMS_DELIVERY_STATUS ds = mms_handler_test_delivery_status + (test->handler, test->id); + + g_assert_cmpint(ds, == ,desc->status); mms_handler_test_reset(test->handler); g_main_loop_quit(test->loop); } static void -test_done( +test_delivery_ind_done( MMSDispatcherDelegate* delegate, MMSDispatcher* dispatcher) { Test* test = G_CAST(delegate,Test,delegate); - if (!mms_handler_test_receive_pending(test->handler, NULL)) { - test_finish(test); - } -} - -static -gboolean -test_timeout( - gpointer data) -{ - Test* test = data; - test->timeout_id = 0; - test->ret = RET_TIMEOUT; - GINFO("%s TIMEOUT", test->desc->name); - mms_connman_test_close_connection(test->cm); - mms_dispatcher_cancel(test->disp, NULL); - return FALSE; -} -static -gboolean -test_init( - Test* test, - const MMSConfig* config, - const TestDesc* desc) -{ - gboolean ok = FALSE; - GError* error = NULL; - const char* dir = desc->name; - char* ni = g_strconcat(DATA_DIR, dir, "/", desc->ind_file, NULL); - memset(test, 0, sizeof(*test)); - test->config = config; - test->notification_ind = g_mapped_file_new(ni, FALSE, &error); - if (test->notification_ind) { - MMSSettings* set = mms_settings_default_new(config); - test->desc = desc; - test->cm = mms_connman_test_new(); - test->handler = mms_handler_test_new(); - test->disp = mms_dispatcher_new(set, test->cm, test->handler, NULL); - test->loop = g_main_loop_new(NULL, FALSE); - test->timeout_id = g_timeout_add_seconds(10, test_timeout, test); - test->delegate.fn_done = test_done; - mms_dispatcher_set_delegate(test->disp, &test->delegate); - test->id = g_strdup(mms_handler_test_send_new(test->handler, "IMSI")); - mms_handler_message_sent(test->handler, test->id, desc->mmsid); - mms_settings_unref(set); - test->ret = RET_ERR; - ok = TRUE; - } else { - GERR("%s", GERRMSG(error)); - g_error_free(error); + if (!mms_handler_test_receive_pending(test->handler, NULL)) { + test_delivery_ind_done_finish(test); } - g_free(ni); - return ok; } static void -test_finalize( - Test* test) -{ - if (test->timeout_id) { - g_source_remove(test->timeout_id); - test->timeout_id = 0; - } - mms_connman_test_close_connection(test->cm); - mms_connman_unref(test->cm); - mms_handler_unref(test->handler); - mms_dispatcher_unref(test->disp); - g_main_loop_unref(test->loop); - g_mapped_file_unref(test->notification_ind); - g_free(test->id); -} - -static -int -test_run_one( - const MMSConfig* config, - const TestDesc* desc) +run_test( + gconstpointer data) { + const TestDesc* desc = data; + char* ni = g_build_filename(DATA_DIR, desc->name, desc->ind_file, NULL); + MMSSettings* settings; + MMSConfig config; Test test; - if (test_init(&test, config, desc)) { - GError* error = NULL; - GBytes* push = g_bytes_new_static( - g_mapped_file_get_contents(test.notification_ind), - g_mapped_file_get_length(test.notification_ind)); - if (mms_dispatcher_handle_push(test.disp, "TestConnection", - push, &error)) { - if (mms_dispatcher_start(test.disp)) { - test.ret = RET_OK; - g_main_loop_run(test.loop); - } else { - GINFO("%s FAILED", desc->name); - } - } else { - GERR("%s", GERRMSG(error)); - GINFO("%s FAILED", desc->name); - g_error_free(error); - } - g_bytes_unref(push); - test_finalize(&test); - return test.ret; - } else { - return RET_ERR; - } -} + TestDirs dirs; + GBytes* push; + GError* error = NULL; -static -int -test_run( - const MMSConfig* config, - const char* name) -{ - int i, ret; - if (name) { - const TestDesc* found = NULL; - for (i=0, ret = RET_ERR; iname, name)) { - ret = test_run_one(config, test); - found = test; - break; - } - } - if (!found) GERR("No such test: %s", name); - } else { - for (i=0, ret = RET_OK; immsid); + mms_settings_unref(settings); + + g_assert(mms_dispatcher_handle_push(test.disp, "Connection", push, &error)); + g_assert(mms_dispatcher_start(test.disp)); + + test_run_loop(&test_opt, test.loop); + + mms_connman_test_close_connection(test.cm); + mms_connman_unref(test.cm); + mms_handler_unref(test.handler); + mms_dispatcher_unref(test.disp); + g_main_loop_unref(test.loop); + g_mapped_file_unref(test.pdu_file); + g_free(test.id); + + test_dirs_cleanup(&dirs, TRUE); + + g_bytes_unref(push); + g_free(ni); } +#define TEST_(x) "/DeliveryInd/" x + int main(int argc, char* argv[]) { int ret; - MMSConfig config; - const char* test = "test_delivery_ind"; - const char* testcase = NULL; + guint i; mms_lib_init(argv[0]); - mms_lib_default_config(&config); - gutil_log_default.name = test; - - if (argc > 1 && !strcmp(argv[1], "-v")) { - gutil_log_default.level = GLOG_LEVEL_VERBOSE; - memmove(argv + 1, argv + 2, (argc-2)*sizeof(argv[0])); - argc--; - } else { - gutil_log_timestamp = FALSE; - gutil_log_default.level = GLOG_LEVEL_INFO; - mms_task_notification_log.level = GLOG_LEVEL_NONE; + g_test_init(&argc, &argv, NULL); + test_init(&test_opt, &argc, argv); + for (i = 0; i < G_N_ELEMENTS(delivery_tests); i++) { + const TestDesc* test = delivery_tests + i; + char* name = g_strdup_printf(TEST_("%s"), test->name); + + g_test_add_data_func(name, test, run_test); + g_free(name); } - - if (argc == 2 && argv[1][0] != '-') { - testcase = argv[1]; - } - - if (argc == 1 || testcase) { - TestDirs dirs; - test_dirs_init(&dirs, test); - config.root_dir = dirs.root; - config.network_idle_secs = 0; - config.attic_enabled = TRUE; - ret = test_run(&config, testcase); - test_dirs_cleanup(&dirs, TRUE); - } else { - printf("Usage: test_delivery_ind [-v] [TEST]\n"); - ret = RET_ERR; - } - + ret = g_test_run(); mms_lib_deinit(); return ret; }