From 6e558e98f040387e027aad0576c0d841ae7bd2f7 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Mon, 6 Jun 2016 17:28:41 +0200 Subject: [PATCH] Add unit test for rsync Signed-off-by: Joachim Nilsson --- rsync.c | 111 ---------------------------------------------- rsync.mk | 4 -- tests/.gitignore | 1 + tests/Makefile.am | 1 + tests/rsync.c | 108 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+), 115 deletions(-) delete mode 100644 rsync.mk create mode 100644 tests/rsync.c diff --git a/rsync.c b/rsync.c index f5075ff..c1ba3d3 100644 --- a/rsync.c +++ b/rsync.c @@ -193,119 +193,8 @@ static int prune(char *dst, char **new_files, int new_num) return result; } -#ifdef UNITTEST -#define BASE "/tmp/.unittest/" -#define SRC BASE "src/" -#define DST BASE "dst/" - -static int verbose = 0; -static char *files[] = { - SRC "sub1/1.tst", - SRC "sub1/2.tst", - SRC "sub1/3.tst", - SRC "sub2/4.tst", - SRC "sub2/5.tst", - SRC "sub2/6.tst", - SRC "sub3/7.tst", - SRC "sub3/8.tst", - SRC "sub3/9.tst", - NULL -}; - -void cleanup_test(void) -{ - system("rm -rf " BASE); -} - -void setup_test(void) -{ - int i; - char cmd[256]; - mode_t dir_modes[] = { 755, 700 }; - mode_t file_modes[] = { 644, 600 }; - - cleanup_test(); - - mkdir(BASE, 0755); - mkdir(SRC, 0755); - mkdir(DST, 0755); - - for (i = 0; files[i]; i++) { - snprintf(cmd, sizeof(cmd), "mkdir -m %d -p `dirname %s`", - dir_modes[i % 2], files[i]); - system(cmd); - - snprintf(cmd, sizeof(cmd), "touch %s; chmod %d %s", files[i], - file_modes[i % 2], files[i]); - system(cmd); - } -} - -static void check_tree(char *heading, char *dir) -{ - if (verbose) { - char cmd[128]; - - if (heading) - puts(heading); - - tree(dir, 1); - } -} - -int run_test(void) -{ - int result = 0; - -#if 0 - setup_test(); - check_tree("Before:", BASE); - - result += rsync(SRC, DST, 0, NULL); - check_tree("After:", BASE); - cleanup_test(); -#endif - - setup_test(); - result += rsync(BASE "src", DST, 0, NULL); - check_tree("Only partial rsync of src <-- No slash!", BASE); -#if 0 - cleanup_test(); - setup_test(); - result += rsync(BASE "src/sub1", BASE "dst", 0, NULL); - check_tree("Only partial rsync of src/sub1 <-- No slashes!!", BASE); - - cleanup_test(); - setup_test(); - result += rsync(BASE "src/sub1/", DST, 0, NULL); - check_tree("Only partial rsync of src/sub1/", BASE); - - cleanup_test(); - setup_test(); - result += rsync(BASE "src/sub1", DST, 0, NULL); - check_tree("Only partial rsync of src/sub1 <-- No slash!", BASE); - - result += rsync("/etc", "/var/tmp", 0, NULL); - check_tree("Real life test:", "/var/tmp"); -#endif - - return result; -} - -int main(int argc, char *argv[]) -{ - if (argc > 1) - verbose = !strncmp("-v", argv[1], 2); - - atexit(cleanup_test); - - return run_test(); -} -#endif /* UNITTEST */ - /** * Local Variables: - * compile-command: "make V=1 -f rsync.mk" * indent-tabs-mode: t * c-file-style: "linux" * End: diff --git a/rsync.mk b/rsync.mk deleted file mode 100644 index 033c3f3..0000000 --- a/rsync.mk +++ /dev/null @@ -1,4 +0,0 @@ -OBJS = dir.o copyfile.o fexist.o fisdir.o fmode.o tree.o -TARGET = clean $(OBJS) rsync.test - -include rules.mk diff --git a/tests/.gitignore b/tests/.gitignore index 82770e3..8f11a2f 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -4,5 +4,6 @@ fsendfile lfile makepath pidfile +rsync tempfile tree diff --git a/tests/Makefile.am b/tests/Makefile.am index 3a4b2ac..f19fbe0 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -6,6 +6,7 @@ TESTS += fsendfile TESTS += lfile TESTS += makepath TESTS += pidfile +TESTS += rsync TESTS += tempfile TESTS += tree diff --git a/tests/rsync.c b/tests/rsync.c new file mode 100644 index 0000000..45f00a2 --- /dev/null +++ b/tests/rsync.c @@ -0,0 +1,108 @@ +#include +#include "check.h" + +#define BASE "/tmp/.unittest/" +#define SRC BASE "src/" +#define DST BASE "dst/" + +static int verbose = 0; +static char *files[] = { + SRC "sub1/1.tst", + SRC "sub1/2.tst", + SRC "sub1/3.tst", + SRC "sub2/4.tst", + SRC "sub2/5.tst", + SRC "sub2/6.tst", + SRC "sub3/7.tst", + SRC "sub3/8.tst", + SRC "sub3/9.tst", + NULL +}; + +void cleanup_test(void) +{ + system("rm -rf " BASE); +} + +void setup_test(void) +{ + int i; + char cmd[256]; + mode_t dir_modes[] = { 755, 700 }; + mode_t file_modes[] = { 644, 600 }; + + cleanup_test(); + + mkdir(BASE, 0755); + mkdir(SRC, 0755); + mkdir(DST, 0755); + + for (i = 0; files[i]; i++) { + snprintf(cmd, sizeof(cmd), "mkdir -m %d -p `dirname %s`", + dir_modes[i % 2], files[i]); + system(cmd); + + snprintf(cmd, sizeof(cmd), "touch %s; chmod %d %s", files[i], + file_modes[i % 2], files[i]); + system(cmd); + } +} + +static void check_tree(char *heading, char *dir) +{ + if (verbose) { + if (heading) + puts(heading); + + tree(dir, 1); + } +} + +int run_test(void) +{ + int result = 0; + +#if 0 + setup_test(); + check_tree("Before:", BASE); + + result += rsync(SRC, DST, 0, NULL); + check_tree("After:", BASE); + cleanup_test(); +#endif + + setup_test(); + result += rsync(BASE "src", DST, 0, NULL); + check_tree("Only partial rsync of src <-- No slash!", BASE); +#if 0 + cleanup_test(); + setup_test(); + result += rsync(BASE "src/sub1", BASE "dst", 0, NULL); + check_tree("Only partial rsync of src/sub1 <-- No slashes!!", BASE); + + cleanup_test(); + setup_test(); + result += rsync(BASE "src/sub1/", DST, 0, NULL); + check_tree("Only partial rsync of src/sub1/", BASE); + + cleanup_test(); + setup_test(); + result += rsync(BASE "src/sub1", DST, 0, NULL); + check_tree("Only partial rsync of src/sub1 <-- No slash!", BASE); + + result += rsync("/etc", "/var/tmp", 0, NULL); + check_tree("Real life test:", "/var/tmp"); +#endif + + return result; +} + +int main(int argc, char *argv[]) +{ + if (argc > 1) + verbose = !strncmp("-v", argv[1], 2); + + atexit(cleanup_test); + + return run_test(); +}