diff --git a/mms-lib/test/test_retrieve_order/Makefile b/mms-lib/test/test_retrieve_order/Makefile index 0e3e572..b69dbbf 100644 --- a/mms-lib/test/test_retrieve_order/Makefile +++ b/mms-lib/test/test_retrieve_order/Makefile @@ -1,7 +1,6 @@ # -*- Mode: makefile-gmake -*- EXE = test_retrieve_order -SRC = $(EXE).c COMMON_SRC = test_connection.c test_connman.c test_handler.c test_http.c \ test_transfer_list.c test_util.c diff --git a/mms-lib/test/test_retrieve_order/test_retrieve_order.c b/mms-lib/test/test_retrieve_order/test_retrieve_order.c index 8c8b86e..e9500b0 100644 --- a/mms-lib/test/test_retrieve_order/test_retrieve_order.c +++ b/mms-lib/test/test_retrieve_order/test_retrieve_order.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 @@ -28,13 +28,9 @@ #include #include -#define RET_OK (0) -#define RET_ERR (1) -#define RET_TIMEOUT (2) +#define DATA_DIR "data" -#define DATA_DIR "data/" - -#define TEST_TIMEOUT (10) /* seconds */ +static TestOpt test_opt; #define TEST_ARRAY_AND_COUNT(a) a, G_N_ELEMENTS(a) @@ -109,39 +105,29 @@ test_free_receive_state( gpointer data) { TestReceiveStateEvent* event = data; + g_free(event->id); g_free(event); } static void -test_finish( +test_retrieve_order_finish( Test* test) { const TestDesc* desc = test->desc; - const char* name = desc->name; - if (test->ret == RET_OK) { - guint n = test->receive_states->len; - if (n != desc->num_receive_states) { - GERR("Unexpected number of state changes %u", n); - test->ret = RET_ERR; - } else { - guint i; - for (i=0; ireceive_states->pdata[i]; - const TestReceiveState* expected = desc->receive_states + i; - if (g_strcmp0(event->id, expected->id)) { - GERR("Unexpected task id %s at %u", event->id, i); - test->ret = RET_ERR; - } - if (event->state != expected->state) { - GERR("Unexpected state %d at %u", event->state, i); - test->ret = RET_ERR; - } - } - } + const guint n = test->receive_states->len; + guint i; + + g_assert_cmpuint(n, == ,desc->num_receive_states); + for (i = 0; i < n; i++) { + TestReceiveStateEvent* event = test->receive_states->pdata[i]; + const TestReceiveState* expected = desc->receive_states + i; + + g_assert_cmpstr(event->id, == ,expected->id); + g_assert_cmpint(event->state, == ,expected->state); } - GINFO("%s: %s", (test->ret == RET_OK) ? "OK" : "FAILED", name); + mms_handler_test_reset(test->handler); g_signal_handler_disconnect(test->handler, test->receive_state_event_id); g_main_loop_quit(test->loop); @@ -149,31 +135,17 @@ test_finish( static void -test_done( +test_retrieve_order_done( MMSDispatcherDelegate* delegate, MMSDispatcher* dispatcher) { Test* test = G_CAST(delegate,Test,delegate); + if (!mms_handler_test_receive_pending(test->handler, NULL)) { - test_finish(test); + test_retrieve_order_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); - if (test->http) test_http_close(test->http); - mms_connman_test_close_connection(test->cm); - mms_dispatcher_cancel(test->disp, NULL); - return FALSE; -} - static void test_receive_state_changed( @@ -184,6 +156,7 @@ test_receive_state_changed( { Test* test = user_data; TestReceiveStateEvent* event = g_new0(TestReceiveStateEvent, 1); + event->id = g_strdup(id); event->state = state; g_ptr_array_add(test->receive_states, event); @@ -195,231 +168,123 @@ test_free_mapped_files( gpointer data) { TestMappedFiles* files = data; + g_mapped_file_unref(files->notification_ind); g_mapped_file_unref(files->retrieve_conf); g_free(files); } static -gboolean -test_init( - Test* test, - const MMSConfig* config, - const TestDesc* desc, - gboolean debug) +void +run_test( + gconstpointer data) { + const TestDesc* desc = data; + MMSConfig config; + MMSSettings* settings; + GError* error = NULL; + TestDirs dirs; + Test test; guint i; - MMSSettings* settings = mms_settings_default_new(config); - GDEBUG(">>>>>>>>>> %s <<<<<<<<<<", desc->name); - memset(test, 0, sizeof(*test)); - test->config = config; - test->files = g_ptr_array_new_full(0, test_free_mapped_files); - test->receive_states = g_ptr_array_new_full(0, test_free_receive_state); - test->http = test_http_new(NULL, NULL, SOUP_STATUS_NONE); + test_dirs_init(&dirs, "test_retrieve_order"); + mms_lib_default_config(&config); + config.root_dir = dirs.root; + config.keep_temp_files = (test_opt.flags & TEST_FLAG_DEBUG) != 0; + config.network_idle_secs = 0; + config.attic_enabled = TRUE; + settings = mms_settings_default_new(&config); + + memset(&test, 0, sizeof(test)); + test.config = &config; + test.files = g_ptr_array_new_full(0, test_free_mapped_files); + test.receive_states = g_ptr_array_new_full(0, test_free_receive_state); + test.http = test_http_new(NULL, NULL, SOUP_STATUS_NONE); /* Open files */ - for (i=0; inum_message_files; i++) { - GError* err = NULL; + for (i = 0; i < desc->num_message_files; i++) { const char* subdir = desc->name; - char* ni = g_strconcat(DATA_DIR, subdir, "/", + char* ni = g_build_filename(DATA_DIR, subdir, desc->message_files[i].notification_ind, NULL); - char* rc = g_strconcat(DATA_DIR, subdir, "/", + char* rc = g_build_filename(DATA_DIR, subdir, desc->message_files[i].retrieve_conf, NULL); TestMappedFiles* files = g_new0(TestMappedFiles, 1); - files->notification_ind = g_mapped_file_new(ni, FALSE, &err); - if (err) { - GERR("%s", GERRMSG(err)); - g_error_free(err); - err = NULL; - } - files->retrieve_conf = g_mapped_file_new(rc, FALSE, &err); - test_http_add_response(test->http, files->retrieve_conf, + + files->notification_ind = g_mapped_file_new(ni, FALSE, &error); + g_assert(!error); + files->retrieve_conf = g_mapped_file_new(rc, FALSE, &error); + g_assert(!error); + test_http_add_response(test.http, files->retrieve_conf, MMS_CONTENT_TYPE, SOUP_STATUS_OK); + /* Add empty responses for ack */ - test_http_add_response(test->http, NULL, NULL, SOUP_STATUS_OK); - if (err) { - GERR("%s", GERRMSG(err)); - g_error_free(err); - err = NULL; - } - g_ptr_array_add(test->files, files); + test_http_add_response(test.http, NULL, NULL, SOUP_STATUS_OK); + g_ptr_array_add(test.files, files); g_free(ni); g_free(rc); } - test->desc = desc; - test->cm = mms_connman_test_new(); - test->handler = mms_handler_test_new(); - test->disp = mms_dispatcher_new(settings, test->cm, test->handler, NULL); - test->loop = g_main_loop_new(NULL, FALSE); - if (!debug) { - test->timeout_id = g_timeout_add_seconds(TEST_TIMEOUT, - test_timeout, test); + test.desc = desc; + test.cm = mms_connman_test_new(); + test.handler = mms_handler_test_new(); + test.disp = mms_dispatcher_new(settings, test.cm, test.handler, NULL); + test.loop = g_main_loop_new(NULL, FALSE); + test.delegate.fn_done = test_retrieve_order_done; + mms_dispatcher_set_delegate(test.disp, &test.delegate); + mms_connman_test_set_port(test.cm, test_http_get_port(test.http), TRUE); + + test.receive_state_event_id = + mms_handler_test_add_receive_state_fn(test.handler, + test_receive_state_changed, &test); + + /* Simulate push and run the event loop */ + for (i = 0; i < test.files->len; i++) { + TestMappedFiles* files = test.files->pdata[i]; + GBytes* push = g_bytes_new_static( + g_mapped_file_get_contents(files->notification_ind), + g_mapped_file_get_length(files->notification_ind)); + + g_assert(mms_dispatcher_handle_push(test.disp, "TestConnection", push, + &error)); + g_bytes_unref(push); } - test->delegate.fn_done = test_done; - mms_dispatcher_set_delegate(test->disp, &test->delegate); - mms_connman_test_set_port(test->cm, test_http_get_port(test->http), TRUE); - - test->receive_state_event_id = - mms_handler_test_add_receive_state_fn(test->handler, - test_receive_state_changed, test); - - test->ret = RET_OK; - return TRUE; + g_assert(mms_dispatcher_start(test.disp)); + test_run_loop(&test_opt, test.loop); + + /* Done */ + test_http_close(test.http); + test_http_unref(test.http); + 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_ptr_array_unref(test.files); + g_ptr_array_unref(test.receive_states); + + mms_settings_unref(settings); + test_dirs_cleanup(&dirs, TRUE); } -static -void -test_finalize( - Test* test) -{ - if (test->timeout_id) { - g_source_remove(test->timeout_id); - test->timeout_id = 0; - } - if (test->http) { - test_http_close(test->http); - test_http_unref(test->http); - } - 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_ptr_array_unref(test->files); - g_ptr_array_unref(test->receive_states); -} +#define TEST_(x) "/RetrieveOrder/" x -static -int -test_order_once( - const MMSConfig* config, - const TestDesc* desc, - gboolean debug) +int main(int argc, char* argv[]) { - Test test; - if (test_init(&test, config, desc, debug)) { - guint i; - for (i=0; ilen && test.ret == RET_OK; i++) { - TestMappedFiles* files = test.files->pdata[i]; - GError* error = NULL; - GBytes* push = g_bytes_new_static( - g_mapped_file_get_contents(files->notification_ind), - g_mapped_file_get_length(files->notification_ind)); - if (!mms_dispatcher_handle_push(test.disp, "TestConnection", - push, &error)) { - g_error_free(error); - test.ret = RET_ERR; - } - g_bytes_unref(push); - } - if (test.ret == RET_OK) { - if (mms_dispatcher_start(test.disp)) { - g_main_loop_run(test.loop); - } else { - test.ret = RET_ERR; - } - } - if (test.ret != RET_OK) { - GINFO("FAILED: %s", desc->name); - } - test_finalize(&test); - return test.ret; - } else { - return RET_ERR; - } -} + int ret; + guint i; -static -int -test_order( - const MMSConfig* config, - const char* name, - gboolean debug) -{ - int i, ret; - if (name) { - const TestDesc* found = NULL; - for (i=0, ret = RET_ERR; iname, name)) { - ret = test_order_once(config, test, debug); - found = test; - break; - } - } - if (!found) GERR("No such test: %s", name); - } else { - for (i=0, ret = RET_OK; iname); -int main(int argc, char* argv[]) -{ - int ret = RET_ERR; - gboolean keep_temp = FALSE; - gboolean verbose = FALSE; - gboolean debug = FALSE; - GError* error = NULL; - GOptionContext* options; - GOptionEntry entries[] = { - { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, - "Enable verbose output", NULL }, - { "keep", 'k', 0, G_OPTION_ARG_NONE, &keep_temp, - "Keep temporary files", NULL }, - { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug, - "Disable timeout for debugging", NULL }, - { NULL } - }; - - options = g_option_context_new("[TEST] - MMS task order test"); - g_option_context_add_main_entries(options, entries, NULL); - if (g_option_context_parse(options, &argc, &argv, &error)) { - const char* test = "test_retrieve_order"; - MMSConfig config; - TestDirs dirs; - - mms_lib_init(argv[0]); - gutil_log_default.name = test; - if (verbose) { - gutil_log_default.level = GLOG_LEVEL_VERBOSE; - } else { - gutil_log_timestamp = FALSE; - gutil_log_default.level = GLOG_LEVEL_INFO; - mms_task_http_log.level = - mms_task_decode_log.level = - mms_task_retrieve_log.level = - mms_task_notification_log.level = GLOG_LEVEL_NONE; - } - - test_dirs_init(&dirs, test); - mms_lib_default_config(&config); - config.root_dir = dirs.root; - config.keep_temp_files = keep_temp; - config.network_idle_secs = 0; - if (argc < 2) { - ret = test_order(&config, NULL, debug); - } else { - int i; - for (i=1, ret = RET_OK; i