From 3560e6489d75c998f3e7c3fc2c14ce4c22e78ea0 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Mon, 30 Jun 2014 08:41:06 +0200 Subject: [PATCH] os_test: Added tests for os_mkdir_all() and os_remove_all(). --- t/Makefile.am | 1 + t/unit/libsysdb_test.c | 1 + t/unit/libsysdb_test.h | 4 ++ t/unit/utils/os_test.c | 120 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 t/unit/utils/os_test.c diff --git a/t/Makefile.am b/t/Makefile.am index f40d16f..5de153f 100644 --- a/t/Makefile.am +++ b/t/Makefile.am @@ -37,6 +37,7 @@ unit_libsysdb_test_SOURCES = \ unit/utils/channel_test.c \ unit/utils/dbi_test.c \ unit/utils/llist_test.c \ + unit/utils/os_test.c \ unit/utils/strbuf_test.c unit_libsysdb_test_CFLAGS = $(AM_CFLAGS) @CHECK_CFLAGS@ -I$(top_srcdir)/t/unit unit_libsysdb_test_LDADD = $(top_builddir)/src/libsysdb.la @CHECK_LIBS@ diff --git a/t/unit/libsysdb_test.c b/t/unit/libsysdb_test.c index 7d833d5..d2e0667 100644 --- a/t/unit/libsysdb_test.c +++ b/t/unit/libsysdb_test.c @@ -53,6 +53,7 @@ main(void) { util_channel_suite, NULL }, { util_dbi_suite, NULL }, { util_llist_suite, NULL }, + { util_os_suite, NULL }, { util_strbuf_suite, NULL }, }; diff --git a/t/unit/libsysdb_test.h b/t/unit/libsysdb_test.h index df5b904..de04d50 100644 --- a/t/unit/libsysdb_test.h +++ b/t/unit/libsysdb_test.h @@ -103,6 +103,10 @@ util_dbi_suite(void); Suite * util_llist_suite(void); +/* t/utils/os_test */ +Suite * +util_os_suite(void); + /* t/utils/strbuf_test */ Suite * util_strbuf_suite(void); diff --git a/t/unit/utils/os_test.c b/t/unit/utils/os_test.c new file mode 100644 index 0000000..9525f49 --- /dev/null +++ b/t/unit/utils/os_test.c @@ -0,0 +1,120 @@ +/* + * SysDB - t/unit/utils/os_test.c + * Copyright (C) 2014 Sebastian 'tokkee' Harl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#if HAVE_CONFIG_H +# include "config.h" +#endif + +#include "utils/os.h" +#include "libsysdb_test.h" + +#include + +#include +#include + +#include + +#include +#include +#include + +START_TEST(test_mkdir_remove) +{ + char tmpdir[] = "os_test_dir.XXXXXX"; + char testdir[1024]; + char testfile[1024]; + int check; + + mode_t mask; + struct stat st; + + if (! mkdtemp(tmpdir)) { + fail("INTERNAL ERROR: mkdtemp failed"); + return; + } + snprintf(testdir, sizeof(testdir), "%s/%s", tmpdir, "test1"); + + mask = umask(0022); + + check = sdb_mkdir_all(testdir, 0777); + fail_unless(check == 0, + "sdb_mkdir_all(%s, %o) = %d (errno = %d); expected: 0", + testdir, 0777, errno); + check = stat(testdir, &st); + fail_unless(check == 0, + "stat(%s) = %d (errno = %d); expected: 0", + testdir, check, errno); + fail_unless((st.st_mode & 0777) == 0755, + "sdb_mkdir_all(%s, %o) created permissons %o; expected: %o", + testdir, 0777, st.st_mode, 0755); + + check = sdb_mkdir_all(testdir, 0777); + fail_unless(check == 0, + "sdb_mkdir_all(%s, %o) = %d (errno = %d) (second attempt); " + "expected: 0", testdir, 0777, errno); + + /* populate the directory */ + snprintf(testfile, sizeof(testfile), "%s/%s", tmpdir, "testfile1"); + check = creat(testfile, 0666); + fail_unless(check >= 0, + "INTERNAL ERROR: creat(%s) = %d (errno = %d); expected: 0", + testfile, check, errno); + close(check); + snprintf(testfile, sizeof(testfile), "%s/%s", testdir, "testfile2"); + check = creat(testfile, 0666); + fail_unless(check >= 0, + "INTERNAL ERROR: creat(%s) = %d (errno = %d); expected: 0", + testfile, check, errno); + close(check); + + check = sdb_remove_all(tmpdir); + fail_unless(check == 0, + "sdb_remove_all(%s) = %d (errno = %d); expected: 0", + tmpdir, check, errno); + fail_unless(access(tmpdir, F_OK), + "sdb_remove_all(%s) did not remove the directory"); + + umask(mask); +} +END_TEST + +Suite * +util_os_suite(void) +{ + Suite *s = suite_create("utils::os"); + TCase *tc; + + tc = tcase_create("core"); + tcase_add_test(tc, test_mkdir_remove); + suite_add_tcase(s, tc); + + return s; +} /* util_os_suite */ + +/* vim: set tw=78 sw=4 ts=4 noexpandtab : */ + -- 2.30.2