check_csync_exclude.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * libcsync -- a library to sync a directory with another
  3. *
  4. * Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "config_csync.h"
  21. #include <string.h>
  22. #include <time.h>
  23. #include <sys/time.h>
  24. #include "torture.h"
  25. #define CSYNC_TEST 1
  26. #include "csync_exclude.c"
  27. #define EXCLUDE_LIST_FILE SOURCEDIR"/../sync-exclude.lst"
  28. static int setup(void **state) {
  29. CSYNC *csync;
  30. csync_create(&csync, "/tmp/check_csync1");
  31. *state = csync;
  32. return 0;
  33. }
  34. static int setup_init(void **state) {
  35. CSYNC *csync;
  36. int rc;
  37. csync_create(&csync, "/tmp/check_csync1");
  38. rc = csync_exclude_load(EXCLUDE_LIST_FILE, &(csync->excludes));
  39. assert_int_equal(rc, 0);
  40. /* and add some unicode stuff */
  41. rc = _csync_exclude_add(&(csync->excludes), "*.💩");
  42. assert_int_equal(rc, 0);
  43. rc = _csync_exclude_add(&(csync->excludes), "пятницы.*");
  44. assert_int_equal(rc, 0);
  45. rc = _csync_exclude_add(&(csync->excludes), "*/*.out");
  46. assert_int_equal(rc, 0);
  47. rc = _csync_exclude_add(&(csync->excludes), "latex*/*.run.xml");
  48. assert_int_equal(rc, 0);
  49. rc = _csync_exclude_add(&(csync->excludes), "latex/*/*.tex.tmp");
  50. assert_int_equal(rc, 0);
  51. *state = csync;
  52. return 0;
  53. }
  54. static int teardown(void **state) {
  55. CSYNC *csync = *state;
  56. int rc;
  57. rc = csync_destroy(csync);
  58. assert_int_equal(rc, 0);
  59. rc = system("rm -rf /tmp/check_csync1");
  60. assert_int_equal(rc, 0);
  61. rc = system("rm -rf /tmp/check_csync2");
  62. assert_int_equal(rc, 0);
  63. *state = NULL;
  64. return 0;
  65. }
  66. static void check_csync_exclude_add(void **state)
  67. {
  68. CSYNC *csync = *state;
  69. _csync_exclude_add(&(csync->excludes), "/tmp/check_csync1/*");
  70. assert_string_equal(csync->excludes->vector[0], "/tmp/check_csync1/*");
  71. }
  72. static void check_csync_exclude_load(void **state)
  73. {
  74. CSYNC *csync = *state;
  75. int rc;
  76. rc = csync_exclude_load(EXCLUDE_LIST_FILE, &(csync->excludes) );
  77. assert_int_equal(rc, 0);
  78. assert_string_equal(csync->excludes->vector[0], "*~");
  79. assert_int_not_equal(csync->excludes->count, 0);
  80. }
  81. static void check_csync_excluded(void **state)
  82. {
  83. CSYNC *csync = *state;
  84. int rc;
  85. rc = csync_excluded_no_ctx(csync->excludes, "", CSYNC_FTW_TYPE_FILE);
  86. assert_int_equal(rc, CSYNC_NOT_EXCLUDED);
  87. rc = csync_excluded_no_ctx(csync->excludes, "/", CSYNC_FTW_TYPE_FILE);
  88. assert_int_equal(rc, CSYNC_NOT_EXCLUDED);
  89. rc = csync_excluded_no_ctx(csync->excludes, "krawel_krawel", CSYNC_FTW_TYPE_FILE);
  90. assert_int_equal(rc, CSYNC_NOT_EXCLUDED);
  91. rc = csync_excluded_no_ctx(csync->excludes, ".kde/share/config/kwin.eventsrc", CSYNC_FTW_TYPE_FILE);
  92. assert_int_equal(rc, CSYNC_NOT_EXCLUDED);
  93. rc = csync_excluded_no_ctx(csync->excludes, ".htaccess/cache-maximegalon/cache1.txt", CSYNC_FTW_TYPE_FILE);
  94. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  95. rc = csync_excluded_no_ctx(csync->excludes, "mozilla/.htaccess", CSYNC_FTW_TYPE_DIR);
  96. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  97. /*
  98. * Test for patterns in subdirs. '.beagle' is defined as a pattern and has
  99. * to be found in top dir as well as in directories underneath.
  100. */
  101. rc = csync_excluded_no_ctx(csync->excludes, ".apdisk", CSYNC_FTW_TYPE_DIR);
  102. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  103. rc = csync_excluded_no_ctx(csync->excludes, "foo/.apdisk", CSYNC_FTW_TYPE_DIR);
  104. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  105. rc = csync_excluded_no_ctx(csync->excludes, "foo/bar/.apdisk", CSYNC_FTW_TYPE_DIR);
  106. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  107. rc = csync_excluded_no_ctx(csync->excludes, ".java", CSYNC_FTW_TYPE_FILE);
  108. assert_int_equal(rc, CSYNC_NOT_EXCLUDED);
  109. /* Files in the ignored dir .java will also be ignored. */
  110. rc = csync_excluded_no_ctx(csync->excludes, ".apdisk/totally_amazing.jar", CSYNC_FTW_TYPE_FILE);
  111. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  112. /* and also in subdirs */
  113. rc = csync_excluded_no_ctx(csync->excludes, "projects/.apdisk/totally_amazing.jar", CSYNC_FTW_TYPE_FILE);
  114. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  115. /* csync-journal is ignored in general silently. */
  116. rc = csync_excluded_no_ctx(csync->excludes, ".csync_journal.db", CSYNC_FTW_TYPE_FILE);
  117. assert_int_equal(rc, CSYNC_FILE_SILENTLY_EXCLUDED);
  118. rc = csync_excluded_no_ctx(csync->excludes, ".csync_journal.db.ctmp", CSYNC_FTW_TYPE_FILE);
  119. assert_int_equal(rc, CSYNC_FILE_SILENTLY_EXCLUDED);
  120. rc = csync_excluded_no_ctx(csync->excludes, "subdir/.csync_journal.db", CSYNC_FTW_TYPE_FILE);
  121. assert_int_equal(rc, CSYNC_FILE_SILENTLY_EXCLUDED);
  122. /* also the new form of the database name */
  123. rc = csync_excluded_no_ctx(csync->excludes, "._sync_5bdd60bdfcfa.db", CSYNC_FTW_TYPE_FILE);
  124. assert_int_equal(rc, CSYNC_FILE_SILENTLY_EXCLUDED);
  125. rc = csync_excluded_no_ctx(csync->excludes, "._sync_5bdd60bdfcfa.db.ctmp", CSYNC_FTW_TYPE_FILE);
  126. assert_int_equal(rc, CSYNC_FILE_SILENTLY_EXCLUDED);
  127. rc = csync_excluded_no_ctx(csync->excludes, "._sync_5bdd60bdfcfa.db-shm", CSYNC_FTW_TYPE_FILE);
  128. assert_int_equal(rc, CSYNC_FILE_SILENTLY_EXCLUDED);
  129. rc = csync_excluded_no_ctx(csync->excludes, "subdir/._sync_5bdd60bdfcfa.db", CSYNC_FTW_TYPE_FILE);
  130. assert_int_equal(rc, CSYNC_FILE_SILENTLY_EXCLUDED);
  131. /* pattern ]*.directory - ignore and remove */
  132. rc = csync_excluded_no_ctx(csync->excludes, "my.~directory", CSYNC_FTW_TYPE_FILE);
  133. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_AND_REMOVE);
  134. rc = csync_excluded_no_ctx(csync->excludes, "/a_folder/my.~directory", CSYNC_FTW_TYPE_FILE);
  135. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_AND_REMOVE);
  136. /* Not excluded because the pattern .netscape/cache requires directory. */
  137. rc = csync_excluded_no_ctx(csync->excludes, ".netscape/cache", CSYNC_FTW_TYPE_FILE);
  138. assert_int_equal(rc, CSYNC_NOT_EXCLUDED);
  139. /* Not excluded */
  140. rc = csync_excluded_no_ctx(csync->excludes, "unicode/中文.hé", CSYNC_FTW_TYPE_FILE);
  141. assert_int_equal(rc, CSYNC_NOT_EXCLUDED);
  142. /* excluded */
  143. rc = csync_excluded_no_ctx(csync->excludes, "unicode/пятницы.txt", CSYNC_FTW_TYPE_FILE);
  144. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  145. rc = csync_excluded_no_ctx(csync->excludes, "unicode/中文.💩", CSYNC_FTW_TYPE_FILE);
  146. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  147. /* path wildcards */
  148. rc = csync_excluded_no_ctx(csync->excludes, "foobar/my_manuscript.out", CSYNC_FTW_TYPE_FILE);
  149. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  150. rc = csync_excluded_no_ctx(csync->excludes, "latex_tmp/my_manuscript.run.xml", CSYNC_FTW_TYPE_FILE);
  151. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  152. rc = csync_excluded_no_ctx(csync->excludes, "word_tmp/my_manuscript.run.xml", CSYNC_FTW_TYPE_FILE);
  153. assert_int_equal(rc, CSYNC_NOT_EXCLUDED);
  154. rc = csync_excluded_no_ctx(csync->excludes, "latex/my_manuscript.tex.tmp", CSYNC_FTW_TYPE_FILE);
  155. assert_int_equal(rc, CSYNC_NOT_EXCLUDED);
  156. rc = csync_excluded_no_ctx(csync->excludes, "latex/songbook/my_manuscript.tex.tmp", CSYNC_FTW_TYPE_FILE);
  157. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  158. #ifdef _WIN32
  159. rc = csync_excluded_no_ctx(csync->excludes, "file_trailing_space ", CSYNC_FTW_TYPE_FILE);
  160. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_TRAILING_SPACE);
  161. rc = csync_excluded_no_ctx(csync->excludes, "file_trailing_dot.", CSYNC_FTW_TYPE_FILE);
  162. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_INVALID_CHAR);
  163. rc = csync_excluded_no_ctx(csync->excludes, "AUX", CSYNC_FTW_TYPE_FILE);
  164. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_INVALID_CHAR);
  165. rc = csync_excluded_no_ctx(csync->excludes, "file_invalid_char<", CSYNC_FTW_TYPE_FILE);
  166. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_INVALID_CHAR);
  167. #endif
  168. }
  169. static void check_csync_excluded_traversal(void **state)
  170. {
  171. CSYNC *csync = *state;
  172. int rc;
  173. _csync_exclude_add( &(csync->excludes), "/exclude" );
  174. /* Check toplevel dir, the pattern only works for toplevel dir. */
  175. rc = csync_excluded_traversal(csync->excludes, "/exclude", CSYNC_FTW_TYPE_DIR);
  176. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  177. rc = csync_excluded_traversal(csync->excludes, "/foo/exclude", CSYNC_FTW_TYPE_DIR);
  178. assert_int_equal(rc, CSYNC_NOT_EXCLUDED);
  179. /* check for a file called exclude. Must still work */
  180. rc = csync_excluded_traversal(csync->excludes, "/exclude", CSYNC_FTW_TYPE_FILE);
  181. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  182. rc = csync_excluded_traversal(csync->excludes, "/foo/exclude", CSYNC_FTW_TYPE_FILE);
  183. assert_int_equal(rc, CSYNC_NOT_EXCLUDED);
  184. /* Add an exclude for directories only: excl/ */
  185. _csync_exclude_add( &(csync->excludes), "excl/" );
  186. rc = csync_excluded_traversal(csync->excludes, "/excl", CSYNC_FTW_TYPE_DIR);
  187. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  188. rc = csync_excluded_traversal(csync->excludes, "meep/excl", CSYNC_FTW_TYPE_DIR);
  189. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  190. rc = csync_excluded_traversal(csync->excludes, "meep/excl/file", CSYNC_FTW_TYPE_FILE);
  191. assert_int_equal(rc, CSYNC_NOT_EXCLUDED); // because leading dirs aren't checked!
  192. rc = csync_excluded_traversal(csync->excludes, "/excl", CSYNC_FTW_TYPE_FILE);
  193. assert_int_equal(rc, CSYNC_NOT_EXCLUDED);
  194. _csync_exclude_add(&csync->excludes, "/excludepath/withsubdir");
  195. rc = csync_excluded_traversal(csync->excludes, "/excludepath/withsubdir", CSYNC_FTW_TYPE_DIR);
  196. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  197. rc = csync_excluded_traversal(csync->excludes, "/excludepath/withsubdir", CSYNC_FTW_TYPE_FILE);
  198. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  199. rc = csync_excluded_traversal(csync->excludes, "/excludepath/withsubdir2", CSYNC_FTW_TYPE_DIR);
  200. assert_int_equal(rc, CSYNC_NOT_EXCLUDED);
  201. rc = csync_excluded_traversal(csync->excludes, "/excludepath/withsubdir/foo", CSYNC_FTW_TYPE_DIR);
  202. assert_int_equal(rc, CSYNC_NOT_EXCLUDED); // because leading dirs aren't checked!
  203. }
  204. static void check_csync_pathes(void **state)
  205. {
  206. CSYNC *csync = *state;
  207. int rc;
  208. _csync_exclude_add( &(csync->excludes), "/exclude" );
  209. /* Check toplevel dir, the pattern only works for toplevel dir. */
  210. rc = csync_excluded_no_ctx(csync->excludes, "/exclude", CSYNC_FTW_TYPE_DIR);
  211. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  212. rc = csync_excluded_no_ctx(csync->excludes, "/foo/exclude", CSYNC_FTW_TYPE_DIR);
  213. assert_int_equal(rc, CSYNC_NOT_EXCLUDED);
  214. /* check for a file called exclude. Must still work */
  215. rc = csync_excluded_no_ctx(csync->excludes, "/exclude", CSYNC_FTW_TYPE_FILE);
  216. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  217. rc = csync_excluded_no_ctx(csync->excludes, "/foo/exclude", CSYNC_FTW_TYPE_FILE);
  218. assert_int_equal(rc, CSYNC_NOT_EXCLUDED);
  219. /* Add an exclude for directories only: excl/ */
  220. _csync_exclude_add( &(csync->excludes), "excl/" );
  221. rc = csync_excluded_no_ctx(csync->excludes, "/excl", CSYNC_FTW_TYPE_DIR);
  222. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  223. rc = csync_excluded_no_ctx(csync->excludes, "meep/excl", CSYNC_FTW_TYPE_DIR);
  224. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  225. rc = csync_excluded_no_ctx(csync->excludes, "meep/excl/file", CSYNC_FTW_TYPE_FILE);
  226. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  227. rc = csync_excluded_no_ctx(csync->excludes, "/excl", CSYNC_FTW_TYPE_FILE);
  228. assert_int_equal(rc, CSYNC_NOT_EXCLUDED);
  229. _csync_exclude_add(&csync->excludes, "/excludepath/withsubdir");
  230. rc = csync_excluded_no_ctx(csync->excludes, "/excludepath/withsubdir", CSYNC_FTW_TYPE_DIR);
  231. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  232. rc = csync_excluded_no_ctx(csync->excludes, "/excludepath/withsubdir", CSYNC_FTW_TYPE_FILE);
  233. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  234. rc = csync_excluded_no_ctx(csync->excludes, "/excludepath/withsubdir2", CSYNC_FTW_TYPE_DIR);
  235. assert_int_equal(rc, CSYNC_NOT_EXCLUDED);
  236. rc = csync_excluded_no_ctx(csync->excludes, "/excludepath/withsubdir/foo", CSYNC_FTW_TYPE_DIR);
  237. assert_int_equal(rc, CSYNC_FILE_EXCLUDE_LIST);
  238. }
  239. static void check_csync_is_windows_reserved_word() {
  240. assert_true(csync_is_windows_reserved_word("CON"));
  241. assert_true(csync_is_windows_reserved_word("con"));
  242. assert_true(csync_is_windows_reserved_word("CON."));
  243. assert_true(csync_is_windows_reserved_word("con."));
  244. assert_true(csync_is_windows_reserved_word("CON.ference"));
  245. assert_false(csync_is_windows_reserved_word("CONference"));
  246. assert_false(csync_is_windows_reserved_word("conference"));
  247. assert_false(csync_is_windows_reserved_word("conf.erence"));
  248. assert_false(csync_is_windows_reserved_word("co"));
  249. assert_true(csync_is_windows_reserved_word("A:"));
  250. assert_true(csync_is_windows_reserved_word("a:"));
  251. assert_true(csync_is_windows_reserved_word("z:"));
  252. assert_true(csync_is_windows_reserved_word("Z:"));
  253. assert_true(csync_is_windows_reserved_word("M:"));
  254. assert_true(csync_is_windows_reserved_word("m:"));
  255. }
  256. static void check_csync_excluded_performance(void **state)
  257. {
  258. CSYNC *csync = *state;
  259. const int N = 10000;
  260. int totalRc = 0;
  261. int i = 0;
  262. // Being able to use QElapsedTimer for measurement would be nice...
  263. {
  264. struct timeval before, after;
  265. gettimeofday(&before, 0);
  266. for (i = 0; i < N; ++i) {
  267. totalRc += csync_excluded_no_ctx(csync->excludes, "/this/is/quite/a/long/path/with/many/components", CSYNC_FTW_TYPE_DIR);
  268. totalRc += csync_excluded_no_ctx(csync->excludes, "/1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/29", CSYNC_FTW_TYPE_FILE);
  269. }
  270. assert_int_equal(totalRc, CSYNC_NOT_EXCLUDED); // mainly to avoid optimization
  271. gettimeofday(&after, 0);
  272. const double total = (after.tv_sec - before.tv_sec)
  273. + (after.tv_usec - before.tv_usec) / 1.0e6;
  274. const double perCallMs = total / 2 / N * 1000;
  275. printf("csync_excluded: %f ms per call\n", perCallMs);
  276. }
  277. {
  278. struct timeval before, after;
  279. gettimeofday(&before, 0);
  280. for (i = 0; i < N; ++i) {
  281. totalRc += csync_excluded_traversal(csync->excludes, "/this/is/quite/a/long/path/with/many/components", CSYNC_FTW_TYPE_DIR);
  282. totalRc += csync_excluded_traversal(csync->excludes, "/1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/29", CSYNC_FTW_TYPE_FILE);
  283. }
  284. assert_int_equal(totalRc, CSYNC_NOT_EXCLUDED); // mainly to avoid optimization
  285. gettimeofday(&after, 0);
  286. const double total = (after.tv_sec - before.tv_sec)
  287. + (after.tv_usec - before.tv_usec) / 1.0e6;
  288. const double perCallMs = total / 2 / N * 1000;
  289. printf("csync_excluded_traversal: %f ms per call\n", perCallMs);
  290. }
  291. }
  292. static void check_csync_exclude_expand_escapes(void **state)
  293. {
  294. (void)state;
  295. const char *str = csync_exclude_expand_escapes(
  296. "keep \\' \\\" \\? \\\\ \\a \\b \\f \\n \\r \\t \\v \\z");
  297. assert_true(0 == strcmp(
  298. str, "keep ' \" ? \\ \a \b \f \n \r \t \v \\z"));
  299. SAFE_FREE(str);
  300. str = csync_exclude_expand_escapes("");
  301. assert_true(0 == strcmp(str, ""));
  302. SAFE_FREE(str);
  303. str = csync_exclude_expand_escapes("\\");
  304. assert_true(0 == strcmp(str, "\\"));
  305. SAFE_FREE(str);
  306. }
  307. int torture_run_tests(void)
  308. {
  309. const struct CMUnitTest tests[] = {
  310. cmocka_unit_test_setup_teardown(check_csync_exclude_add, setup, teardown),
  311. cmocka_unit_test_setup_teardown(check_csync_exclude_load, setup, teardown),
  312. cmocka_unit_test_setup_teardown(check_csync_excluded, setup_init, teardown),
  313. cmocka_unit_test_setup_teardown(check_csync_excluded_traversal, setup_init, teardown),
  314. cmocka_unit_test_setup_teardown(check_csync_pathes, setup_init, teardown),
  315. cmocka_unit_test_setup_teardown(check_csync_is_windows_reserved_word, setup_init, teardown),
  316. cmocka_unit_test_setup_teardown(check_csync_excluded_performance, setup_init, teardown),
  317. cmocka_unit_test(check_csync_exclude_expand_escapes),
  318. };
  319. return cmocka_run_group_tests(tests, NULL, NULL);
  320. }