check_vio_ext.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * libcsync -- a library to sync a directory with another
  3. *
  4. * Copyright (c) 2015-2013 by Klaas Freitag <freitag@owncloud.com>
  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 <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <stdio.h>
  26. #include "csync_private.h"
  27. #include "std/c_utf8.h"
  28. #include "vio/csync_vio.h"
  29. #ifdef _WIN32
  30. #include <windows.h>
  31. #define CSYNC_TEST_DIR "C:/tmp/csync_test"
  32. #else
  33. #define CSYNC_TEST_DIR "/tmp/csync_test"
  34. #endif
  35. #define MKDIR_MASK (S_IRWXU |S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
  36. #include "torture.h"
  37. #define WD_BUFFER_SIZE 255
  38. static mbchar_t wd_buffer[WD_BUFFER_SIZE];
  39. using statevar = struct {
  40. CSYNC *csync;
  41. char *result;
  42. char *ignored_dir;
  43. };
  44. /* remove the complete test dir */
  45. static int wipe_testdir()
  46. {
  47. int rc = 0;
  48. #ifdef _WIN32
  49. /* The windows system call to rd bails out if the dir is not existing
  50. * Check first.
  51. */
  52. WIN32_FIND_DATA FindFileData;
  53. mbchar_t *dir = c_utf8_path_to_locale(CSYNC_TEST_DIR);
  54. HANDLE handle = FindFirstFile(dir, &FindFileData);
  55. c_free_locale_string(dir);
  56. int found = handle != INVALID_HANDLE_VALUE;
  57. if(found) {
  58. FindClose(handle);
  59. rc = system("rd /s /q C:\\tmp\\csync_test");
  60. }
  61. #else
  62. rc = system("rm -rf /tmp/csync_test/");
  63. #endif
  64. return rc;
  65. }
  66. static int setup_testenv(void **state) {
  67. int rc = 0;
  68. rc = wipe_testdir();
  69. assert_int_equal(rc, 0);
  70. mbchar_t *dir = c_utf8_path_to_locale(CSYNC_TEST_DIR);
  71. rc = _tmkdir(dir, MKDIR_MASK);
  72. assert_int_equal(rc, 0);
  73. assert_non_null(_tgetcwd(wd_buffer, WD_BUFFER_SIZE));
  74. rc = _tchdir(dir);
  75. assert_int_equal(rc, 0);
  76. c_free_locale_string(dir);
  77. /* --- initialize csync */
  78. statevar *mystate = (statevar*)malloc( sizeof(statevar) );
  79. mystate->result = nullptr;
  80. mystate->csync = new CSYNC("/tmp/check_csync1", new OCC::SyncJournalDb(""));
  81. mystate->csync->current = LOCAL_REPLICA;
  82. *state = mystate;
  83. return 0;
  84. }
  85. static void output( const char *text )
  86. {
  87. mbchar_t *wtext = c_utf8_string_to_locale(text);
  88. #ifdef _WIN32
  89. wprintf(L"OOOO %ls (%ld)\n", wtext, strlen(text));
  90. #else
  91. printf("%s\n", wtext);
  92. #endif
  93. c_free_locale_string(wtext);
  94. }
  95. static int teardown(void **state) {
  96. statevar *sv = (statevar*) *state;
  97. CSYNC *csync = sv->csync;
  98. int rc = 0;
  99. output("================== Tearing down!\n");
  100. auto statedb = csync->statedb;
  101. delete csync;
  102. delete statedb;
  103. rc = _tchdir(wd_buffer);
  104. assert_int_equal(rc, 0);
  105. rc = wipe_testdir();
  106. assert_int_equal(rc, 0);
  107. *state = nullptr;
  108. return 0;
  109. }
  110. /* This function takes a relative path, prepends it with the CSYNC_TEST_DIR
  111. * and creates each sub directory.
  112. */
  113. static void create_dirs( const char *path )
  114. {
  115. int rc = 0;
  116. char *mypath = (char*)c_malloc( 2+strlen(CSYNC_TEST_DIR)+strlen(path));
  117. *mypath = '\0';
  118. strcat(mypath, CSYNC_TEST_DIR);
  119. strcat(mypath, "/");
  120. strcat(mypath, path);
  121. char *p = mypath+strlen(CSYNC_TEST_DIR)+1; /* start behind the offset */
  122. int i = 0;
  123. assert_non_null(path);
  124. while( *(p+i) ) {
  125. if( *(p+i) == '/' ) {
  126. p[i] = '\0';
  127. mbchar_t *mb_dir = c_utf8_path_to_locale(mypath);
  128. /* wprintf(L"OOOO %ls (%ld)\n", mb_dir, strlen(mypath)); */
  129. rc = _tmkdir(mb_dir, MKDIR_MASK);
  130. c_free_locale_string(mb_dir);
  131. assert_int_equal(rc, 0);
  132. p[i] = '/';
  133. }
  134. i++;
  135. }
  136. SAFE_FREE(mypath);
  137. }
  138. /*
  139. * This function uses the vio_opendir, vio_readdir and vio_closedir functions
  140. * to traverse a file tree that was created before by the create_dir function.
  141. *
  142. * It appends a listing to the result member of the incoming struct in *state
  143. * that can be compared later to what was expected in the calling functions.
  144. *
  145. * The int parameter cnt contains the number of seen files (not dirs) in the
  146. * whole tree.
  147. *
  148. */
  149. static void traverse_dir(void **state, const char *dir, int *cnt)
  150. {
  151. csync_vio_handle_t *dh = nullptr;
  152. std::unique_ptr<csync_file_stat_t> dirent;
  153. statevar *sv = (statevar*) *state;
  154. CSYNC *csync = sv->csync;
  155. char *subdir = nullptr;
  156. char *subdir_out = nullptr;
  157. int rc = 0;
  158. int is_dir = 0;
  159. /* Format: Smuggle in the C: for unix platforms as its urgently needed
  160. * on Windows and the test can be nicely cross platform this way. */
  161. #ifdef _WIN32
  162. const char *format_str = "%s %s";
  163. #else
  164. const char *format_str = "%s C:%s";
  165. #endif
  166. dh = csync_vio_opendir(csync, dir);
  167. assert_non_null(dh);
  168. while( (dirent = csync_vio_readdir(csync, dh)) ) {
  169. assert_non_null(dirent.get());
  170. if (!dirent->original_path.isEmpty()) {
  171. sv->ignored_dir = c_strdup(dirent->original_path);
  172. continue;
  173. }
  174. assert_false(dirent->path.isEmpty());
  175. if( c_streq( dirent->path, "..") || c_streq( dirent->path, "." )) {
  176. continue;
  177. }
  178. is_dir = (dirent->type == ItemTypeDirectory) ? 1:0;
  179. assert_int_not_equal( asprintf( &subdir, "%s/%s", dir, dirent->path.constData() ), -1 );
  180. assert_int_not_equal( asprintf( &subdir_out, format_str,
  181. is_dir ? "<DIR>":" ",
  182. subdir), -1 );
  183. if( is_dir ) {
  184. if( !sv->result ) {
  185. sv->result = c_strdup( subdir_out);
  186. } else {
  187. int newlen = 1+strlen(sv->result)+strlen(subdir_out);
  188. char *tmp = sv->result;
  189. sv->result = (char*)c_malloc(newlen);
  190. strcpy( sv->result, tmp);
  191. SAFE_FREE(tmp);
  192. strcat( sv->result, subdir_out );
  193. }
  194. } else {
  195. *cnt = *cnt +1;
  196. }
  197. output(subdir_out);
  198. if( is_dir ) {
  199. traverse_dir( state, subdir, cnt);
  200. }
  201. SAFE_FREE(subdir);
  202. SAFE_FREE(subdir_out);
  203. }
  204. rc = csync_vio_closedir(csync, dh);
  205. assert_int_equal(rc, 0);
  206. }
  207. static void create_file( const char *path, const char *name, const char *content)
  208. {
  209. #ifdef _WIN32
  210. char *filepath = c_malloc( 2+strlen(CSYNC_TEST_DIR)+strlen(path) + strlen(name) );
  211. *filepath = '\0';
  212. strcpy(filepath, CSYNC_TEST_DIR);
  213. strcat(filepath, "/");
  214. strcat(filepath, path);
  215. strcat(filepath, name);
  216. DWORD dwWritten; // number of bytes written to file
  217. HANDLE hFile;
  218. mbchar_t *w_fname = c_utf8_path_to_locale(filepath);
  219. hFile=CreateFile(w_fname, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0,
  220. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  221. assert_int_equal( 0, hFile==INVALID_HANDLE_VALUE );
  222. int len = strlen(content);
  223. mbchar_t *dst = nullptr;
  224. dst = c_utf8_string_to_locale(content);
  225. WriteFile(hFile, dst, len * sizeof(mbchar_t), &dwWritten, 0);
  226. CloseHandle(hFile);
  227. SAFE_FREE(dst);
  228. c_free_locale_string(w_fname);
  229. #else
  230. char *filepath = (char*)c_malloc( 1+strlen(path) + strlen(name) );
  231. *filepath = '\0';
  232. strcpy(filepath, path);
  233. strcat(filepath, name);
  234. FILE *sink = nullptr;
  235. sink = fopen(filepath,"w");
  236. fprintf (sink, "we got: %s",content);
  237. fclose(sink);
  238. SAFE_FREE(filepath);
  239. #endif
  240. }
  241. static void check_readdir_shorttree(void **state)
  242. {
  243. statevar *sv = (statevar*) *state;
  244. const char *t1 = "alibaba/und/die/vierzig/räuber/";
  245. create_dirs( t1 );
  246. int files_cnt = 0;
  247. traverse_dir(state, CSYNC_TEST_DIR, &files_cnt);
  248. assert_string_equal( sv->result,
  249. "<DIR> C:/tmp/csync_test/alibaba"
  250. "<DIR> C:/tmp/csync_test/alibaba/und"
  251. "<DIR> C:/tmp/csync_test/alibaba/und/die"
  252. "<DIR> C:/tmp/csync_test/alibaba/und/die/vierzig"
  253. "<DIR> C:/tmp/csync_test/alibaba/und/die/vierzig/räuber" );
  254. assert_int_equal(files_cnt, 0);
  255. }
  256. static void check_readdir_with_content(void **state)
  257. {
  258. statevar *sv = (statevar*) *state;
  259. int files_cnt = 0;
  260. const char *t1 = "warum/nur/40/Räuber/";
  261. create_dirs( t1 );
  262. create_file( t1, "Räuber Max.txt", "Der Max ist ein schlimmer finger");
  263. create_file( t1, "пя́тница.txt", "Am Freitag tanzt der Ürk");
  264. traverse_dir(state, CSYNC_TEST_DIR, &files_cnt);
  265. assert_string_equal( sv->result,
  266. "<DIR> C:/tmp/csync_test/warum"
  267. "<DIR> C:/tmp/csync_test/warum/nur"
  268. "<DIR> C:/tmp/csync_test/warum/nur/40"
  269. "<DIR> C:/tmp/csync_test/warum/nur/40/Räuber");
  270. /* " C:/tmp/csync_test/warum/nur/40/Räuber/Räuber Max.txt"
  271. " C:/tmp/csync_test/warum/nur/40/Räuber/пя́тница.txt"); */
  272. assert_int_equal(files_cnt, 2); /* Two files in the sub dir */
  273. }
  274. static void check_readdir_longtree(void **state)
  275. {
  276. statevar *sv = (statevar*) *state;
  277. /* Strange things here: Compilers only support strings with length of 4k max.
  278. * The expected result string is longer, so it needs to be split up in r1, r2 and r3
  279. */
  280. /* create the test tree */
  281. const char *t1 = "vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE/OOOOOOOOH/AND/NE/BOTTLE/VOLL/RUM/undnochmalallezusammen/VierZig/MannaufDesTotenManns/KISTE/ooooooooooooooooooooooooooohhhhhh/und/BESSER/ZWEI/Butteln/VOLL RUM/";
  282. create_dirs( t1 );
  283. const char *r1 =
  284. "<DIR> C:/tmp/csync_test/vierzig"
  285. "<DIR> C:/tmp/csync_test/vierzig/mann"
  286. "<DIR> C:/tmp/csync_test/vierzig/mann/auf"
  287. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des"
  288. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten"
  289. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann"
  290. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste"
  291. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh"
  292. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and"
  293. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne"
  294. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle"
  295. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll"
  296. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum";
  297. const char *r2 =
  298. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und"
  299. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so"
  300. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen"
  301. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir"
  302. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG"
  303. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN"
  304. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF"
  305. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES"
  306. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN"
  307. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS"
  308. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE"
  309. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE/OOOOOOOOH";
  310. const char *r3 =
  311. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE/OOOOOOOOH/AND"
  312. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE/OOOOOOOOH/AND/NE"
  313. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE/OOOOOOOOH/AND/NE/BOTTLE"
  314. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE/OOOOOOOOH/AND/NE/BOTTLE/VOLL"
  315. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE/OOOOOOOOH/AND/NE/BOTTLE/VOLL/RUM"
  316. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE/OOOOOOOOH/AND/NE/BOTTLE/VOLL/RUM/undnochmalallezusammen"
  317. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE/OOOOOOOOH/AND/NE/BOTTLE/VOLL/RUM/undnochmalallezusammen/VierZig"
  318. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE/OOOOOOOOH/AND/NE/BOTTLE/VOLL/RUM/undnochmalallezusammen/VierZig/MannaufDesTotenManns"
  319. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE/OOOOOOOOH/AND/NE/BOTTLE/VOLL/RUM/undnochmalallezusammen/VierZig/MannaufDesTotenManns/KISTE"
  320. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE/OOOOOOOOH/AND/NE/BOTTLE/VOLL/RUM/undnochmalallezusammen/VierZig/MannaufDesTotenManns/KISTE/ooooooooooooooooooooooooooohhhhhh"
  321. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE/OOOOOOOOH/AND/NE/BOTTLE/VOLL/RUM/undnochmalallezusammen/VierZig/MannaufDesTotenManns/KISTE/ooooooooooooooooooooooooooohhhhhh/und"
  322. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE/OOOOOOOOH/AND/NE/BOTTLE/VOLL/RUM/undnochmalallezusammen/VierZig/MannaufDesTotenManns/KISTE/ooooooooooooooooooooooooooohhhhhh/und/BESSER"
  323. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE/OOOOOOOOH/AND/NE/BOTTLE/VOLL/RUM/undnochmalallezusammen/VierZig/MannaufDesTotenManns/KISTE/ooooooooooooooooooooooooooohhhhhh/und/BESSER/ZWEI"
  324. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE/OOOOOOOOH/AND/NE/BOTTLE/VOLL/RUM/undnochmalallezusammen/VierZig/MannaufDesTotenManns/KISTE/ooooooooooooooooooooooooooohhhhhh/und/BESSER/ZWEI/Butteln"
  325. "<DIR> C:/tmp/csync_test/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE/OOOOOOOOH/AND/NE/BOTTLE/VOLL/RUM/undnochmalallezusammen/VierZig/MannaufDesTotenManns/KISTE/ooooooooooooooooooooooooooohhhhhh/und/BESSER/ZWEI/Butteln/VOLL RUM";
  326. /* assemble the result string ... */
  327. int overall_len = 1+strlen(r1)+strlen(r2)+strlen(r3);
  328. int files_cnt = 0;
  329. char *result = (char*)c_malloc(overall_len);
  330. *result = '\0';
  331. strcat(result, r1);
  332. strcat(result, r2);
  333. strcat(result, r3);
  334. traverse_dir(state, CSYNC_TEST_DIR, &files_cnt);
  335. assert_int_equal(files_cnt, 0);
  336. /* and compare. */
  337. assert_string_equal( sv->result, result);
  338. }
  339. // https://github.com/owncloud/client/issues/3128 https://github.com/owncloud/client/issues/2777
  340. static void check_readdir_bigunicode(void **state)
  341. {
  342. statevar *sv = (statevar*) *state;
  343. // 1: ? ASCII: 239 - EF
  344. // 2: ? ASCII: 187 - BB
  345. // 3: ? ASCII: 191 - BF
  346. // 4: ASCII: 32 - 20
  347. char *p = 0;
  348. asprintf( &p, "%s/%s", CSYNC_TEST_DIR, "goodone/" );
  349. int rc = _tmkdir(p, MKDIR_MASK);
  350. assert_int_equal(rc, 0);
  351. SAFE_FREE(p);
  352. const char *t1 = "goodone/ugly\xEF\xBB\xBF\x32" ".txt"; // file with encoding error
  353. asprintf( &p, "%s/%s", CSYNC_TEST_DIR, t1 );
  354. rc = _tmkdir(p, MKDIR_MASK);
  355. SAFE_FREE(p);
  356. assert_int_equal(rc, 0);
  357. int files_cnt = 0;
  358. traverse_dir(state, CSYNC_TEST_DIR, &files_cnt);
  359. const char *expected_result = "<DIR> C:/tmp/csync_test/goodone"
  360. "<DIR> C:/tmp/csync_test/goodone/ugly\xEF\xBB\xBF\x32" ".txt"
  361. ;
  362. assert_string_equal( sv->result, expected_result);
  363. assert_int_equal(files_cnt, 0);
  364. }
  365. int torture_run_tests(void)
  366. {
  367. const struct CMUnitTest tests[] = {
  368. cmocka_unit_test_setup_teardown(check_readdir_shorttree, setup_testenv, teardown),
  369. cmocka_unit_test_setup_teardown(check_readdir_with_content, setup_testenv, teardown),
  370. cmocka_unit_test_setup_teardown(check_readdir_longtree, setup_testenv, teardown),
  371. cmocka_unit_test_setup_teardown(check_readdir_bigunicode, setup_testenv, teardown),
  372. };
  373. return cmocka_run_group_tests(tests, nullptr, nullptr);
  374. }