Skip to content

Commit

Permalink
[test] Make sure that temporary directories are deleted
Browse files Browse the repository at this point in the history
Some tests did't delete temporary directories.
  • Loading branch information
monich committed Mar 11, 2016
1 parent a911b08 commit dce0250
Show file tree
Hide file tree
Showing 16 changed files with 212 additions and 96 deletions.
67 changes: 67 additions & 0 deletions mms-lib/test/common/test_util.c
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2016 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
*
* 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:
*/
45 changes: 45 additions & 0 deletions mms-lib/test/common/test_util.h
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2016 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
*
* 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:
*/
4 changes: 2 additions & 2 deletions 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
24 changes: 13 additions & 11 deletions 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 <slava.monich@jolla.com>
*
* 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
Expand All @@ -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"
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions 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
24 changes: 13 additions & 11 deletions 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 <slava.monich@jolla.com>
*
* 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
Expand All @@ -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"
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions 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
23 changes: 10 additions & 13 deletions 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 <slava.monich@jolla.com>
*
* This program is free software; you can redistribute it and/or modify
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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));
Expand Down
5 changes: 3 additions & 2 deletions 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
28 changes: 13 additions & 15 deletions mms-lib/test/test_retrieve/test_retrieve.c
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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));
Expand Down
3 changes: 2 additions & 1 deletion mms-lib/test/test_retrieve_no_proxy/Makefile
Expand Up @@ -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

0 comments on commit dce0250

Please sign in to comment.