summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRonny Chevalier <chevalier.ronny@gmail.com>2014-10-29 22:28:50 +0100
committerTom Gundersen <teg@jklm.no>2014-10-31 10:57:22 +0100
commit641d1f99b8c4c5427a1fedcb4740586a130ac6cf (patch)
treea133a5397cd1a8dd4efb98f45ba6f66f4eac881c
parent5895b62f1da8db93677d2bc369dff34fcaa97f91 (diff)
tests: add test-copy
-rw-r--r--.gitignore1
-rw-r--r--Makefile.am9
-rw-r--r--src/test/test-copy.c115
3 files changed, 124 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index 7d5f04dca3..23522eddd2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -159,6 +159,7 @@
/test-compress-benchmark
/test-condition-util
/test-conf-files
+/test-copy
/test-coredump-vacuum
/test-daemon
/test-date
diff --git a/Makefile.am b/Makefile.am
index 00465e8367..3f1ace0d6c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1360,7 +1360,8 @@ tests += \
test-condition-util \
test-uid-range \
test-bus-policy \
- test-locale-util
+ test-locale-util \
+ test-copy
EXTRA_DIST += \
test/a.service \
@@ -1510,6 +1511,12 @@ test_locale_util_SOURCES = \
test_locale_util_LDADD = \
libsystemd-shared.la
+test_copy_SOURCES = \
+ src/test/test-copy.c
+
+test_copy_LDADD = \
+ libsystemd-shared.la
+
test_condition_util_SOURCES = \
src/test/test-condition-util.c
diff --git a/src/test/test-copy.c b/src/test/test-copy.c
new file mode 100644
index 0000000000..6aa86a03be
--- /dev/null
+++ b/src/test/test-copy.c
@@ -0,0 +1,115 @@
+/***
+ This file is part of systemd
+
+ Copyright 2014 Ronny Chevalier
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <unistd.h>
+
+#include "copy.h"
+#include "path-util.h"
+#include "fileio.h"
+#include "mkdir.h"
+#include "strv.h"
+#include "macro.h"
+#include "util.h"
+
+static void test_copy_file(void) {
+ _cleanup_free_ char *buf = NULL;
+ char fn[] = "/tmp/test-copy_file.XXXXXX";
+ char fn_copy[] = "/tmp/test-copy_file.XXXXXX";
+ size_t sz = 0;
+ int fd;
+
+ fd = mkostemp_safe(fn, O_RDWR|O_CLOEXEC);
+ assert_se(fd >= 0);
+ close(fd);
+
+ fd = mkostemp_safe(fn_copy, O_RDWR|O_CLOEXEC);
+ assert_se(fd >= 0);
+ close(fd);
+
+ assert_se(write_string_file(fn, "foo bar bar bar foo") == 0);
+
+ assert_se(copy_file(fn, fn_copy, 0, 0644) == 0);
+
+ assert_se(read_full_file(fn_copy, &buf, &sz) == 0);
+ assert_se(streq(buf, "foo bar bar bar foo\n"));
+
+ unlink(fn);
+ unlink(fn_copy);
+}
+
+static void test_copy_tree(void) {
+ char original_dir[] = "/tmp/test-copy_tree/";
+ char copy_dir[] = "/tmp/test-copy_tree-copy/";
+ char **files = STRV_MAKE("file", "dir1/file", "dir1/dir2/file", "dir1/dir2/dir3/dir4/dir5/file");
+ char **links = STRV_MAKE("link", "file",
+ "link2", "dir1/file");
+ char **p, **link;
+
+ rm_rf_dangerous(copy_dir, false, true, false);
+ rm_rf_dangerous(original_dir, false, true, false);
+
+ STRV_FOREACH(p, files) {
+ char *f = strappenda(original_dir, *p);
+
+ assert_se(mkdir_parents(f, 0755) >= 0);
+ assert_se(write_string_file(f, "file") == 0);
+ }
+
+ STRV_FOREACH_PAIR(link, p, links) {
+ char *f = strappenda(original_dir, *p);
+ char *l = strappenda(original_dir, *link);
+
+ assert_se(mkdir_parents(l, 0755) >= 0);
+ assert_se(symlink(f, l) == 0);
+ }
+
+ assert_se(copy_tree(original_dir, copy_dir, true) == 0);
+
+ STRV_FOREACH(p, files) {
+ _cleanup_free_ char *buf = NULL;
+ size_t sz = 0;
+ char *f = strappenda(copy_dir, *p);
+
+ assert_se(access(f, F_OK) == 0);
+ assert_se(read_full_file(f, &buf, &sz) == 0);
+ assert_se(streq(buf, "file\n"));
+ }
+
+ STRV_FOREACH_PAIR(link, p, links) {
+ _cleanup_free_ char *target = NULL;
+ char *f = strappenda(original_dir, *p);
+ char *l = strappenda(copy_dir, *link);
+
+ assert_se(readlink_and_canonicalize(l, &target) == 0);
+ assert_se(path_equal(f, target));
+ }
+
+ assert_se(copy_tree(original_dir, copy_dir, false) < 0);
+ assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, false) < 0);
+
+ rm_rf_dangerous(copy_dir, false, true, false);
+ rm_rf_dangerous(original_dir, false, true, false);
+}
+
+int main(int argc, char *argv[]) {
+ test_copy_file();
+ test_copy_tree();
+
+ return 0;
+}