Code

os_test: Added tests for os_mkdir_all() and os_remove_all().
authorSebastian Harl <sh@tokkee.org>
Mon, 30 Jun 2014 06:41:06 +0000 (08:41 +0200)
committerSebastian Harl <sh@tokkee.org>
Mon, 30 Jun 2014 06:41:38 +0000 (08:41 +0200)
t/Makefile.am
t/unit/libsysdb_test.c
t/unit/libsysdb_test.h
t/unit/utils/os_test.c [new file with mode: 0644]

index f40d16f97a50f141e5d68e6efe806e5c4c10d174..5de153f16e5718cd9c316c5077dabb8ea5b54868 100644 (file)
@@ -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@
index 7d833d5e19daa130d7ce7ff578fbb55b4b5185a2..d2e0667cc984fccae7f4bfa91a36310eea42101a 100644 (file)
@@ -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 },
        };
 
index df5b904ac52bc3ecddb5143d561488a202aa3ef0..de04d50b3a7c241ebd71c37ec7170cc207f6affc 100644 (file)
@@ -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 (file)
index 0000000..9525f49
--- /dev/null
@@ -0,0 +1,120 @@
+/*
+ * SysDB - t/unit/utils/os_test.c
+ * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * 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 <errno.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <fcntl.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+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 : */
+