check_vio_ext.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 <cstring>
  24. #include <cerrno>
  25. #include <cstdio>
  26. #include "csync.h"
  27. #include "vio/csync_vio_local.h"
  28. #include <QDir>
  29. static const auto CSYNC_TEST_DIR = []{ return QStringLiteral("%1/csync_test").arg(QDir::tempPath());}();
  30. #include "torture.h"
  31. namespace {
  32. int oc_mkdir(const QString &path)
  33. {
  34. return QDir(path).mkpath(path) ? 0 : -1;
  35. }
  36. }
  37. #define WD_BUFFER_SIZE 255
  38. static mbchar_t wd_buffer[WD_BUFFER_SIZE];
  39. using statevar = struct {
  40. QByteArray result;
  41. QByteArray ignored_dir;
  42. };
  43. /* remove the complete test dir */
  44. static int wipe_testdir()
  45. {
  46. QDir tmp(CSYNC_TEST_DIR);
  47. if (tmp.exists())
  48. {
  49. return tmp.removeRecursively() ? 0 : 1;
  50. }
  51. return 0;
  52. }
  53. static int setup_testenv(void **state) {
  54. int rc = 0;
  55. rc = wipe_testdir();
  56. assert_int_equal(rc, 0);
  57. auto dir = CSYNC_TEST_DIR;
  58. rc = oc_mkdir(dir);
  59. assert_int_equal(rc, 0);
  60. assert_non_null(_tgetcwd(wd_buffer, WD_BUFFER_SIZE));
  61. #ifdef Q_OS_WIN
  62. rc = _tchdir(dir.toStdWString().data());
  63. #else
  64. rc = _tchdir(dir.toLocal8Bit().constData());
  65. #endif
  66. assert_int_equal(rc, 0);
  67. /* --- initialize csync */
  68. auto mystate = new statevar;
  69. *state = mystate;
  70. return 0;
  71. }
  72. static void output( const char *text )
  73. {
  74. printf("%s\n", text);
  75. }
  76. static int teardown(void **state) {
  77. int rc = -1;
  78. output("================== Tearing down!\n");
  79. rc = _tchdir(wd_buffer);
  80. assert_int_equal(rc, 0);
  81. rc = wipe_testdir();
  82. assert_int_equal(rc, 0);
  83. delete reinterpret_cast<statevar*>(*state);
  84. return 0;
  85. }
  86. /* This function takes a relative path, prepends it with the CSYNC_TEST_DIR
  87. * and creates each sub directory.
  88. */
  89. static void create_dirs( const char *path )
  90. {
  91. int rc = -1;
  92. auto _mypath = QStringLiteral("%1/%2").arg(CSYNC_TEST_DIR, QString::fromUtf8(path)).toUtf8();
  93. char *mypath = _mypath.data();
  94. char *p = mypath + CSYNC_TEST_DIR.size() + 1; /* start behind the offset */
  95. int i = 0;
  96. assert_non_null(path);
  97. while( *(p+i) ) {
  98. if( *(p+i) == '/' ) {
  99. p[i] = '\0';
  100. auto mb_dir = QString::fromUtf8(mypath);
  101. rc = oc_mkdir(mb_dir);
  102. if(rc)
  103. {
  104. rc = errno;
  105. }
  106. assert_int_equal(rc, 0);
  107. p[i] = '/';
  108. }
  109. i++;
  110. }
  111. }
  112. /*
  113. * This function uses the vio_opendir, vio_readdir and vio_closedir functions
  114. * to traverse a file tree that was created before by the create_dir function.
  115. *
  116. * It appends a listing to the result member of the incoming struct in *state
  117. * that can be compared later to what was expected in the calling functions.
  118. *
  119. * The int parameter cnt contains the number of seen files (not dirs) in the
  120. * whole tree.
  121. *
  122. */
  123. static void traverse_dir(void **state, const QString &dir, int *cnt)
  124. {
  125. csync_vio_handle_t *dh = nullptr;
  126. std::unique_ptr<csync_file_stat_t> dirent;
  127. auto sv = (statevar*) *state;
  128. QByteArray subdir;
  129. QByteArray subdir_out;
  130. int rc = -1;
  131. int is_dir = 0;
  132. dh = csync_vio_local_opendir(dir);
  133. assert_non_null(dh);
  134. OCC::Vfs *vfs = nullptr;
  135. while( (dirent = csync_vio_local_readdir(dh, vfs)) ) {
  136. assert_non_null(dirent.get());
  137. if (!dirent->original_path.isEmpty()) {
  138. sv->ignored_dir = dirent->original_path;
  139. continue;
  140. }
  141. assert_false(dirent->path.isEmpty());
  142. if( dirent->path == ".." || dirent->path == "." ) {
  143. continue;
  144. }
  145. is_dir = (dirent->type == ItemTypeDirectory) ? 1:0;
  146. subdir = dir.toUtf8() + "/" + dirent->path;
  147. subdir_out = (is_dir ? "<DIR> ":" ") + subdir;
  148. if( is_dir ) {
  149. if( sv->result.isNull() ) {
  150. sv->result = subdir_out;
  151. } else {
  152. sv->result += subdir_out;
  153. }
  154. } else {
  155. *cnt = *cnt +1;
  156. }
  157. output(subdir_out.constData());
  158. if( is_dir ) {
  159. traverse_dir(state, QString::fromUtf8(subdir), cnt);
  160. }
  161. }
  162. rc = csync_vio_local_closedir(dh);
  163. assert_int_equal(rc, 0);
  164. }
  165. static void create_file( const char *path, const char *name, const char *content)
  166. {
  167. QFile file(QStringLiteral("%1/%2%3").arg(CSYNC_TEST_DIR, QString::fromUtf8(path), QString::fromUtf8(name)));
  168. assert_int_equal(1, file.open(QIODevice::WriteOnly | QIODevice::NewOnly));
  169. file.write(content);
  170. }
  171. static void check_readdir_shorttree(void **state)
  172. {
  173. auto sv = (statevar*) *state;
  174. const char *t1 = "alibaba/und/die/vierzig/räuber/";
  175. create_dirs( t1 );
  176. int files_cnt = 0;
  177. traverse_dir(state, CSYNC_TEST_DIR, &files_cnt);
  178. assert_string_equal(sv->result.constData(),
  179. QString::fromUtf8("<DIR> %1/alibaba"
  180. "<DIR> %1/alibaba/und"
  181. "<DIR> %1/alibaba/und/die"
  182. "<DIR> %1/alibaba/und/die/vierzig"
  183. "<DIR> %1/alibaba/und/die/vierzig/räuber")
  184. .arg(CSYNC_TEST_DIR)
  185. .toUtf8()
  186. .constData());
  187. assert_int_equal(files_cnt, 0);
  188. }
  189. static void check_readdir_with_content(void **state)
  190. {
  191. auto sv = (statevar*) *state;
  192. int files_cnt = 0;
  193. const char *t1 = "warum/nur/40/Räuber/";
  194. create_dirs( t1 );
  195. create_file( t1, "Räuber Max.txt", "Der Max ist ein schlimmer finger");
  196. create_file( t1, "пя́тница.txt", "Am Freitag tanzt der Ürk");
  197. traverse_dir(state, CSYNC_TEST_DIR, &files_cnt);
  198. assert_string_equal(sv->result.constData(),
  199. QString::fromUtf8("<DIR> %1/warum"
  200. "<DIR> %1/warum/nur"
  201. "<DIR> %1/warum/nur/40"
  202. "<DIR> %1/warum/nur/40/Räuber")
  203. .arg(CSYNC_TEST_DIR)
  204. .toUtf8()
  205. .constData());
  206. /* " %1/warum/nur/40/Räuber/Räuber Max.txt"
  207. " %1/warum/nur/40/Räuber/пя́тница.txt"; */
  208. assert_int_equal(files_cnt, 2); /* Two files in the sub dir */
  209. }
  210. static void check_readdir_longtree(void **state)
  211. {
  212. auto sv = (statevar*) *state;
  213. /* Strange things here: Compilers only support strings with length of 4k max.
  214. * The expected result string is longer, so it needs to be split up in r1, r2 and r3
  215. */
  216. /* create the test tree */
  217. 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/";
  218. create_dirs( t1 );
  219. const auto r1 = QString::fromUtf8(
  220. "<DIR> %1/vierzig"
  221. "<DIR> %1/vierzig/mann"
  222. "<DIR> %1/vierzig/mann/auf"
  223. "<DIR> %1/vierzig/mann/auf/des"
  224. "<DIR> %1/vierzig/mann/auf/des/toten"
  225. "<DIR> %1/vierzig/mann/auf/des/toten/Mann"
  226. "<DIR> %1/vierzig/mann/auf/des/toten/Mann/kiste"
  227. "<DIR> %1/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh"
  228. "<DIR> %1/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and"
  229. "<DIR> %1/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne"
  230. "<DIR> %1/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle"
  231. "<DIR> %1/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll"
  232. "<DIR> %1/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum").arg(CSYNC_TEST_DIR);
  233. const auto r2 = QString::fromUtf8(
  234. "<DIR> %1/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und"
  235. "<DIR> %1/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so"
  236. "<DIR> %1/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen"
  237. "<DIR> %1/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir"
  238. "<DIR> %1/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG"
  239. "<DIR> %1/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN"
  240. "<DIR> %1/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF"
  241. "<DIR> %1/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES"
  242. "<DIR> %1/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN"
  243. "<DIR> %1/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS"
  244. "<DIR> %1/vierzig/mann/auf/des/toten/Mann/kiste/ooooooooooooooooooooooh/and/ne/bottle/voll/rum/und/so/singen/wir/VIERZIG/MANN/AUF/DES/TOTEN/MANNS/KISTE"
  245. "<DIR> %1/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").arg(CSYNC_TEST_DIR);
  246. const auto r3 = QString::fromUtf8(
  247. "<DIR> %1/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"
  248. "<DIR> %1/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"
  249. "<DIR> %1/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"
  250. "<DIR> %1/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"
  251. "<DIR> %1/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"
  252. "<DIR> %1/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"
  253. "<DIR> %1/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"
  254. "<DIR> %1/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"
  255. "<DIR> %1/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"
  256. "<DIR> %1/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"
  257. "<DIR> %1/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"
  258. "<DIR> %1/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"
  259. "<DIR> %1/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"
  260. "<DIR> %1/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"
  261. "<DIR> %1/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").arg(CSYNC_TEST_DIR);
  262. /* assemble the result string ... */
  263. const auto result = (r1 + r2 + r3).toUtf8();
  264. int files_cnt = 0;
  265. traverse_dir(state, CSYNC_TEST_DIR, &files_cnt);
  266. assert_int_equal(files_cnt, 0);
  267. /* and compare. */
  268. assert_string_equal(sv->result.constData(), result.constData());
  269. }
  270. // https://github.com/owncloud/client/issues/3128 https://github.com/owncloud/client/issues/2777
  271. static void check_readdir_bigunicode(void **state)
  272. {
  273. auto sv = (statevar*) *state;
  274. // 1: ? ASCII: 239 - EF
  275. // 2: ? ASCII: 187 - BB
  276. // 3: ? ASCII: 191 - BF
  277. // 4: ASCII: 32 - 20
  278. QString p = QStringLiteral("%1/%2").arg(CSYNC_TEST_DIR, QStringLiteral("goodone/"));
  279. int rc = oc_mkdir(p);
  280. assert_int_equal(rc, 0);
  281. p = QStringLiteral("%1/goodone/ugly\xEF\xBB\xBF\x32.txt").arg(CSYNC_TEST_DIR); // file with encoding error
  282. rc = oc_mkdir(p);
  283. assert_int_equal(rc, 0);
  284. int files_cnt = 0;
  285. traverse_dir(state, CSYNC_TEST_DIR, &files_cnt);
  286. const auto expected_result = QStringLiteral("<DIR> %1/goodone"
  287. "<DIR> %1/goodone/ugly\xEF\xBB\xBF\x32.txt")
  288. .arg(CSYNC_TEST_DIR);
  289. assert_string_equal(sv->result.constData(), expected_result.toUtf8().constData());
  290. assert_int_equal(files_cnt, 0);
  291. }
  292. int torture_run_tests(void)
  293. {
  294. const struct CMUnitTest tests[] = {
  295. cmocka_unit_test_setup_teardown(check_readdir_shorttree, setup_testenv, teardown),
  296. cmocka_unit_test_setup_teardown(check_readdir_with_content, setup_testenv, teardown),
  297. cmocka_unit_test_setup_teardown(check_readdir_longtree, setup_testenv, teardown),
  298. cmocka_unit_test_setup_teardown(check_readdir_bigunicode, setup_testenv, teardown),
  299. };
  300. return cmocka_run_group_tests(tests, nullptr, nullptr);
  301. }