diff --git a/mms-lib/test/common/test_util.c b/mms-lib/test/common/test_util.c new file mode 100644 index 0000000..fe15e2f --- /dev/null +++ b/mms-lib/test/common/test_util.c @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2016 Jolla Ltd. + * Contact: 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 + * 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. + * + */ + +#include "test_util.h" +#include "mms_file_util.h" +#include "mms_log.h" + +void +test_dirs_init( + TestDirs* dirs, + const char* test) +{ + const char* tmp = g_get_tmp_dir(); + dirs->root = g_mkdtemp(g_strconcat(tmp, "/", test, "_XXXXXX", NULL)); + dirs->msg = g_strconcat(dirs->root, "/" MMS_MESSAGE_DIR, NULL); + dirs->attic = g_strconcat(dirs->root, "/" MMS_ATTIC_DIR, NULL); + MMS_VERBOSE("Temporary directory %s", dirs->root); +} + +static +void +test_dir_remove( + const char* dir) +{ + if (rmdir(dir) < 0) { + if (errno != ENOENT) { + MMS_ERR("Failed to remove %s: %s", dir, strerror(errno)); + } + } else { + MMS_VERBOSE("Deleted %s", dir); + } +} + +void +test_dirs_cleanup( + TestDirs* dirs, + gboolean remove) +{ + if (remove) { + test_dir_remove(dirs->attic); + test_dir_remove(dirs->msg); + test_dir_remove(dirs->root); + } + g_free(dirs->root); + g_free(dirs->msg); + g_free(dirs->attic); +} + +/* + * Local Variables: + * mode: C + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/mms-lib/test/common/test_util.h b/mms-lib/test/common/test_util.h new file mode 100644 index 0000000..476b33c --- /dev/null +++ b/mms-lib/test/common/test_util.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2016 Jolla Ltd. + * Contact: 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 + * 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. + * + */ + +#ifndef TEST_UTIL_H +#define TEST_UTIL_H + +#include "mms_lib_types.h" + +typedef struct test_dirs { + char* root; + char* msg; + char* attic; +} TestDirs; + +void +test_dirs_init( + TestDirs* dirs, + const char* test); + +void +test_dirs_cleanup( + TestDirs* dirs, + gboolean remove); + +#endif /* TEST_UTIL_H */ + +/* + * Local Variables: + * mode: C + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/mms-lib/test/test_delivery_ind/Makefile b/mms-lib/test/test_delivery_ind/Makefile index 7198a71..6cf6ff7 100644 --- a/mms-lib/test/test_delivery_ind/Makefile +++ b/mms-lib/test/test_delivery_ind/Makefile @@ -1,7 +1,7 @@ # -*- Mode: makefile-gmake -*- EXE = test_delivery_ind -SRC = test_delivery_ind.c -COMMON_SRC = test_connection.c test_connman.c test_handler.c +SRC = $(EXE).c +COMMON_SRC = test_connection.c test_connman.c test_handler.c test_util.c include ../common/Makefile 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 e16d132..760b496 100644 --- a/mms-lib/test/test_delivery_ind/test_delivery_ind.c +++ b/mms-lib/test/test_delivery_ind/test_delivery_ind.c @@ -1,5 +1,6 @@ /* - * Copyright (C) 2013-2014 Jolla Ltd. + * Copyright (C) 2013-2016 Jolla Ltd. + * Contact: 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 @@ -14,6 +15,7 @@ #include "test_connman.h" #include "test_handler.h" +#include "test_util.h" #include "mms_file_util.h" #include "mms_lib_log.h" @@ -232,11 +234,12 @@ int main(int argc, char* argv[]) { int ret; MMSConfig config; - const char* test_name = NULL; + const char* test = "test_delivery_ind"; + const char* testcase = NULL; mms_lib_init(argv[0]); mms_lib_default_config(&config); - mms_log_default.name = "test_delivery_ind"; + mms_log_default.name = test; if (argc > 1 && !strcmp(argv[1], "-v")) { mms_log_default.level = MMS_LOGLEVEL_VERBOSE; @@ -249,18 +252,17 @@ int main(int argc, char* argv[]) } if (argc == 2 && argv[1][0] != '-') { - test_name = argv[1]; + testcase = argv[1]; } - if (argc == 1 || test_name) { - char* tmpd = g_mkdtemp(g_strdup("/tmp/test_delivery_ind_XXXXXX")); - MMS_VERBOSE("Temporary directory %s", tmpd); - config.root_dir = tmpd; + if (argc == 1 || testcase) { + TestDirs dirs; + test_dirs_init(&dirs, test); + config.root_dir = dirs.root; config.idle_secs = 0; config.attic_enabled = TRUE; - ret = test_run(&config, test_name); - remove(tmpd); - g_free(tmpd); + ret = test_run(&config, testcase); + test_dirs_cleanup(&dirs, TRUE); } else { printf("Usage: test_delivery_ind [-v] [TEST]\n"); ret = RET_ERR; diff --git a/mms-lib/test/test_read_ind/Makefile b/mms-lib/test/test_read_ind/Makefile index c41bb26..12e3c09 100644 --- a/mms-lib/test/test_read_ind/Makefile +++ b/mms-lib/test/test_read_ind/Makefile @@ -1,7 +1,7 @@ # -*- Mode: makefile-gmake -*- EXE = test_read_ind -SRC = test_read_ind.c -COMMON_SRC = test_connection.c test_connman.c test_handler.c +SRC = $(EXE).c +COMMON_SRC = test_connection.c test_connman.c test_handler.c test_util.c include ../common/Makefile diff --git a/mms-lib/test/test_read_ind/test_read_ind.c b/mms-lib/test/test_read_ind/test_read_ind.c index 798dead..037a343 100644 --- a/mms-lib/test/test_read_ind/test_read_ind.c +++ b/mms-lib/test/test_read_ind/test_read_ind.c @@ -1,5 +1,6 @@ /* - * Copyright (C) 2013-2014 Jolla Ltd. + * Copyright (C) 2013-2016 Jolla Ltd. + * Contact: 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 @@ -14,6 +15,7 @@ #include "test_connman.h" #include "test_handler.h" +#include "test_util.h" #include "mms_file_util.h" #include "mms_lib_log.h" @@ -232,11 +234,12 @@ int main(int argc, char* argv[]) { int ret; MMSConfig config; - const char* test_name = NULL; + const char* test = "test_read_ind"; + const char* testcase = NULL; mms_lib_init(argv[0]); mms_lib_default_config(&config); - mms_log_default.name = "test_read_ind"; + mms_log_default.name = test; if (argc > 1 && !strcmp(argv[1], "-v")) { mms_log_default.level = MMS_LOGLEVEL_VERBOSE; @@ -249,18 +252,17 @@ int main(int argc, char* argv[]) } if (argc == 2 && argv[1][0] != '-') { - test_name = argv[1]; + testcase = argv[1]; } - if (argc == 1 || test_name) { - char* tmpd = g_mkdtemp(g_strdup("/tmp/test_read_ind_XXXXXX")); - MMS_VERBOSE("Temporary directory %s", tmpd); - config.root_dir = tmpd; + if (argc == 1 || testcase) { + TestDirs dirs; + test_dirs_init(&dirs, test); + config.root_dir = dirs.root; config.idle_secs = 0; config.attic_enabled = TRUE; - ret = test_run(&config, test_name); - remove(tmpd); - g_free(tmpd); + ret = test_run(&config, testcase); + test_dirs_cleanup(&dirs, TRUE); } else { printf("Usage: test_read_ind [-v] [TEST]\n"); ret = RET_ERR; diff --git a/mms-lib/test/test_read_report/Makefile b/mms-lib/test/test_read_report/Makefile index 9b6e278..2c81c9d 100644 --- a/mms-lib/test/test_read_report/Makefile +++ b/mms-lib/test/test_read_report/Makefile @@ -1,7 +1,8 @@ # -*- Mode: makefile-gmake -*- EXE = test_read_report -SRC = test_read_report.c -COMMON_SRC = test_connection.c test_connman.c test_handler.c test_http.c +SRC = $(EXE).c +COMMON_SRC = test_connection.c test_connman.c test_handler.c test_http.c \ + test_util.c include ../common/Makefile diff --git a/mms-lib/test/test_read_report/test_read_report.c b/mms-lib/test/test_read_report/test_read_report.c index d6c0ec8..f5228b7 100644 --- a/mms-lib/test/test_read_report/test_read_report.c +++ b/mms-lib/test/test_read_report/test_read_report.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013-2015 Jolla Ltd. + * Copyright (C) 2013-2016 Jolla Ltd. * Contact: Slava Monich * * This program is free software; you can redistribute it and/or modify @@ -16,6 +16,7 @@ #include "test_connman.h" #include "test_handler.h" #include "test_http.h" +#include "test_util.h" #include "mms_log.h" #include "mms_codec.h" @@ -253,15 +254,12 @@ int main(int argc, char* argv[]) options = g_option_context_new("[TEST] - MMS read report test"); g_option_context_add_main_entries(options, entries, NULL); if (g_option_context_parse(options, &argc, &argv, &error)) { + const char* test = "test_read_report"; MMSConfig config; - char* tmpd = g_mkdtemp(g_strdup("/tmp/test_read_report_XXXXXX")); - char* msgdir = g_strconcat(tmpd, "/msg", NULL); + TestDirs dirs; mms_lib_init(argv[0]); - mms_lib_default_config(&config); - config.idle_secs = 0; - config.root_dir = tmpd; - mms_log_default.name = "test_read_report"; + mms_log_default.name = test; if (verbose) { mms_log_default.level = MMS_LOGLEVEL_VERBOSE; } else { @@ -272,7 +270,10 @@ int main(int argc, char* argv[]) mms_log_stdout_timestamp = FALSE; } - MMS_VERBOSE("Temporary directory %s", tmpd); + test_dirs_init(&dirs, test); + mms_lib_default_config(&config); + config.idle_secs = 0; + config.root_dir = dirs.root; if (argc < 2) { ret = test_read_report(&config, NULL, debug); } else { @@ -282,11 +283,7 @@ int main(int argc, char* argv[]) if (ret == RET_OK && test_status != RET_OK) ret = test_status; } } - rmdir(msgdir); - rmdir(tmpd); - remove(tmpd); - g_free(tmpd); - g_free(msgdir); + test_dirs_cleanup(&dirs, TRUE); mms_lib_deinit(); } else { fprintf(stderr, "%s\n", MMS_ERRMSG(error)); diff --git a/mms-lib/test/test_retrieve/Makefile b/mms-lib/test/test_retrieve/Makefile index 4c05429..f68d65c 100644 --- a/mms-lib/test/test_retrieve/Makefile +++ b/mms-lib/test/test_retrieve/Makefile @@ -1,7 +1,8 @@ # -*- Mode: makefile-gmake -*- EXE = test_retrieve -SRC = test_retrieve.c -COMMON_SRC = test_connection.c test_connman.c test_handler.c test_http.c +SRC = $(EXE).c +COMMON_SRC = test_connection.c test_connman.c test_handler.c test_http.c \ + test_util.c include ../common/Makefile diff --git a/mms-lib/test/test_retrieve/test_retrieve.c b/mms-lib/test/test_retrieve/test_retrieve.c index db0a25b..b617ef2 100644 --- a/mms-lib/test/test_retrieve/test_retrieve.c +++ b/mms-lib/test/test_retrieve/test_retrieve.c @@ -16,6 +16,7 @@ #include "test_connman.h" #include "test_handler.h" #include "test_http.h" +#include "test_util.h" #include "mms_codec.h" #include "mms_file_util.h" @@ -758,18 +759,12 @@ int main(int argc, char* argv[]) options = g_option_context_new("[TEST] - MMS retrieve test"); g_option_context_add_main_entries(options, entries, NULL); if (g_option_context_parse(options, &argc, &argv, &error)) { + const char* test = "test_retrieve"; MMSConfig config; - char* tmpd = g_mkdtemp(g_strdup("/tmp/test_retrieve_XXXXXX")); - char* msgdir = g_strconcat(tmpd, "/msg", NULL); - - mms_lib_init(argv[0]); - mms_lib_default_config(&config); - config.root_dir = tmpd; - config.keep_temp_files = keep_temp; - config.idle_secs = 0; - config.attic_enabled = TRUE; + TestDirs dirs; - mms_log_set_type(MMS_LOG_TYPE_STDOUT, "test_retrieve"); + mms_lib_init(argv[0]); + mms_log_set_type(MMS_LOG_TYPE_STDOUT, test); if (verbose) { mms_log_default.level = MMS_LOGLEVEL_VERBOSE; } else { @@ -781,7 +776,13 @@ int main(int argc, char* argv[]) mms_log_stdout_timestamp = FALSE; } - MMS_VERBOSE("Temporary directory %s", tmpd); + test_dirs_init(&dirs, test); + mms_lib_default_config(&config); + config.root_dir = dirs.root; + config.keep_temp_files = keep_temp; + config.idle_secs = 0; + config.attic_enabled = TRUE; + if (argc < 2) { ret = test_retrieve(&config, NULL, debug); } else { @@ -792,10 +793,7 @@ int main(int argc, char* argv[]) } } - rmdir(msgdir); - rmdir(tmpd); - g_free(msgdir); - g_free(tmpd); + test_dirs_cleanup(&dirs, !keep_temp); mms_lib_deinit(); } else { fprintf(stderr, "%s\n", MMS_ERRMSG(error)); diff --git a/mms-lib/test/test_retrieve_no_proxy/Makefile b/mms-lib/test/test_retrieve_no_proxy/Makefile index 420dace..6ea9b62 100644 --- a/mms-lib/test/test_retrieve_no_proxy/Makefile +++ b/mms-lib/test/test_retrieve_no_proxy/Makefile @@ -2,6 +2,7 @@ EXE = test_retrieve_no_proxy SRC = $(EXE).c -COMMON_SRC = test_connection.c test_connman.c test_handler.c test_http.c +COMMON_SRC = test_connection.c test_connman.c test_handler.c test_http.c \ + test_util.c include ../common/Makefile diff --git a/mms-lib/test/test_retrieve_no_proxy/test_retrieve_no_proxy.c b/mms-lib/test/test_retrieve_no_proxy/test_retrieve_no_proxy.c index 0f4e081..e03d6b7 100644 --- a/mms-lib/test/test_retrieve_no_proxy/test_retrieve_no_proxy.c +++ b/mms-lib/test/test_retrieve_no_proxy/test_retrieve_no_proxy.c @@ -1,5 +1,6 @@ /* - * Copyright (C) 2013-2014 Jolla Ltd. + * Copyright (C) 2013-2016 Jolla Ltd. + * Contact: 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 @@ -15,6 +16,7 @@ #include "test_connman.h" #include "test_handler.h" #include "test_http.h" +#include "test_util.h" #include "mms_log.h" #include "mms_codec.h" @@ -227,10 +229,11 @@ int main(int argc, char* argv[]) { int ret; MMSConfig config; + const char* test = "test_retrieve_no_proxy"; mms_lib_init(argv[0]); mms_lib_default_config(&config); - mms_log_default.name = "test_retrieve_no_proxy"; + mms_log_default.name = test; if (argc > 1 && !strcmp(argv[1], "-v")) { mms_log_default.level = MMS_LOGLEVEL_VERBOSE; @@ -239,19 +242,19 @@ int main(int argc, char* argv[]) mms_log_default.level = MMS_LOGLEVEL_INFO; mms_task_decode_log.level = mms_task_retrieve_log.level = + mms_task_http_log.level = mms_task_notification_log.level = MMS_LOGLEVEL_NONE; mms_log_stdout_timestamp = FALSE; } if (argc == 1) { - char* tmpd = g_mkdtemp(g_strdup("/tmp/test_retrieve_XXXXXX")); - MMS_VERBOSE("Temporary directory %s", tmpd); - config.root_dir = tmpd; + TestDirs dirs; + test_dirs_init(&dirs, test); + config.root_dir = dirs.root; config.idle_secs = 0; config.attic_enabled = TRUE; ret = test_retrieve_no_proxy(&config); - remove(tmpd); - g_free(tmpd); + test_dirs_cleanup(&dirs, TRUE); } else { printf("Usage: test_retrieve [-v] [TEST]\n"); ret = RET_ERR; diff --git a/mms-lib/test/test_retrieve_order/Makefile b/mms-lib/test/test_retrieve_order/Makefile index 41cad99..b731162 100644 --- a/mms-lib/test/test_retrieve_order/Makefile +++ b/mms-lib/test/test_retrieve_order/Makefile @@ -2,6 +2,7 @@ EXE = test_retrieve_order SRC = $(EXE).c -COMMON_SRC = test_connection.c test_connman.c test_handler.c test_http.c +COMMON_SRC = test_connection.c test_connman.c test_handler.c test_http.c \ + test_util.c include ../common/Makefile 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 83651d6..90001c1 100644 --- a/mms-lib/test/test_retrieve_order/test_retrieve_order.c +++ b/mms-lib/test/test_retrieve_order/test_retrieve_order.c @@ -16,6 +16,7 @@ #include "test_connman.h" #include "test_handler.h" #include "test_http.h" +#include "test_util.h" #include "mms_codec.h" #include "mms_file_util.h" @@ -375,21 +376,15 @@ int main(int argc, char* argv[]) { NULL } }; - options = g_option_context_new("[TEST] - MMS retrieve test"); + 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; - char* tmpd = g_mkdtemp(g_strdup("/tmp/test_retrieve_order_XXXXXX")); - char* msgdir = g_strconcat(tmpd, "/msg", NULL); + TestDirs dirs; mms_lib_init(argv[0]); - mms_lib_default_config(&config); - config.root_dir = tmpd; - config.keep_temp_files = keep_temp; - config.idle_secs = 0; - config.attic_enabled = TRUE; - - mms_log_set_type(MMS_LOG_TYPE_STDOUT, "test_retrieve_order"); + mms_log_default.name = test; if (verbose) { mms_log_default.level = MMS_LOGLEVEL_VERBOSE; } else { @@ -401,7 +396,11 @@ int main(int argc, char* argv[]) mms_log_stdout_timestamp = FALSE; } - MMS_VERBOSE("Temporary directory %s", tmpd); + test_dirs_init(&dirs, test); + mms_lib_default_config(&config); + config.root_dir = dirs.root; + config.keep_temp_files = keep_temp; + config.idle_secs = 0; if (argc < 2) { ret = test_order(&config, NULL, debug); } else { @@ -412,10 +411,7 @@ int main(int argc, char* argv[]) } } - rmdir(msgdir); - rmdir(tmpd); - g_free(msgdir); - g_free(tmpd); + test_dirs_cleanup(&dirs, TRUE); mms_lib_deinit(); } else { fprintf(stderr, "%s\n", MMS_ERRMSG(error)); diff --git a/mms-lib/test/test_send/Makefile b/mms-lib/test/test_send/Makefile index 199e23c..d9ab3a2 100644 --- a/mms-lib/test/test_send/Makefile +++ b/mms-lib/test/test_send/Makefile @@ -1,7 +1,8 @@ # -*- Mode: makefile-gmake -*- EXE = test_send -SRC = test_send.c -COMMON_SRC = test_connection.c test_connman.c test_handler.c test_http.c +SRC = $(EXE).c +COMMON_SRC = test_connection.c test_connman.c test_handler.c test_http.c \ + test_util.c include ../common/Makefile diff --git a/mms-lib/test/test_send/test_send.c b/mms-lib/test/test_send/test_send.c index 7aa7c73..eec5d62 100644 --- a/mms-lib/test/test_send/test_send.c +++ b/mms-lib/test/test_send/test_send.c @@ -16,6 +16,7 @@ #include "test_connman.h" #include "test_handler.h" #include "test_http.h" +#include "test_util.h" #include "mms_log.h" #include "mms_codec.h" @@ -528,17 +529,12 @@ int main(int argc, char* argv[]) options = g_option_context_new("[TEST] - MMS send test"); g_option_context_add_main_entries(options, entries, NULL); if (g_option_context_parse(options, &argc, &argv, NULL) && argc < 3) { + const char* test = "test_send"; + const char* testcase = (argc == 2) ? argv[1] : NULL; MMSConfig config; - const char* test_name = (argc == 2) ? argv[1] : NULL; - char* tmpd = g_mkdtemp(g_strdup("/tmp/test_send_XXXXXX")); - MMS_VERBOSE("Temporary directory %s", tmpd); + TestDirs dirs; - mms_lib_default_config(&config); - config.keep_temp_files = keep_temp; - config.root_dir = tmpd; - config.idle_secs = 0; - - mms_log_set_type(MMS_LOG_TYPE_STDOUT, "test_send"); + mms_log_set_type(MMS_LOG_TYPE_STDOUT, test); if (verbose) { mms_log_default.level = MMS_LOGLEVEL_VERBOSE; } else { @@ -548,9 +544,14 @@ int main(int argc, char* argv[]) mms_log_stdout_timestamp = FALSE; } - ret = test_run(&config, test_name, debug); - remove(tmpd); - g_free(tmpd); + test_dirs_init(&dirs, test); + mms_lib_default_config(&config); + config.keep_temp_files = keep_temp; + config.root_dir = dirs.root; + config.idle_secs = 0; + + ret = test_run(&config, testcase, debug); + test_dirs_cleanup(&dirs, !keep_temp); } else { printf("Usage: test_send [-v] [TEST]\n"); ret = RET_ERR;