check_vio.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #include <cstring>
  24. #include <cerrno>
  25. #include "csync_private.h"
  26. #include "std/c_utf8.h"
  27. #include "vio/csync_vio.h"
  28. #include "torture.h"
  29. #define CSYNC_TEST_DIR "/tmp/csync_test/"
  30. #define CSYNC_TEST_DIRS "/tmp/csync_test/this/is/a/mkdirs/test"
  31. #define CSYNC_TEST_FILE "/tmp/csync_test/file.txt"
  32. #define MKDIR_MASK (S_IRWXU |S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
  33. #define WD_BUFFER_SIZE 255
  34. static char wd_buffer[WD_BUFFER_SIZE];
  35. static int setup(void **state)
  36. {
  37. CSYNC *csync = nullptr;
  38. int rc = 0;
  39. assert_non_null(getcwd(wd_buffer, WD_BUFFER_SIZE));
  40. rc = system("rm -rf /tmp/csync_test");
  41. assert_int_equal(rc, 0);
  42. csync = new CSYNC("/tmp/check_csync1", new OCC::SyncJournalDb(""));
  43. csync->current = LOCAL_REPLICA;
  44. *state = csync;
  45. return 0;
  46. }
  47. static int setup_dir(void **state) {
  48. int rc = 0;
  49. mbchar_t *dir = c_utf8_path_to_locale(CSYNC_TEST_DIR);
  50. setup(state);
  51. rc = _tmkdir(dir, MKDIR_MASK);
  52. c_free_locale_string(dir);
  53. assert_int_equal(rc, 0);
  54. assert_non_null(getcwd(wd_buffer, WD_BUFFER_SIZE));
  55. rc = chdir(CSYNC_TEST_DIR);
  56. assert_int_equal(rc, 0);
  57. return 0;
  58. }
  59. static int teardown(void **state) {
  60. CSYNC *csync = (CSYNC*)*state;
  61. int rc = 0;
  62. auto statedb = csync->statedb;
  63. delete csync;
  64. delete statedb;
  65. rc = chdir(wd_buffer);
  66. assert_int_equal(rc, 0);
  67. rc = system("rm -rf /tmp/csync_test/");
  68. assert_int_equal(rc, 0);
  69. *state = nullptr;
  70. return 0;
  71. }
  72. /*
  73. * Test directory function
  74. */
  75. static void check_csync_vio_opendir(void **state)
  76. {
  77. CSYNC *csync = (CSYNC*)*state;
  78. csync_vio_handle_t *dh = nullptr;
  79. int rc = 0;
  80. dh = csync_vio_opendir(csync, CSYNC_TEST_DIR);
  81. assert_non_null(dh);
  82. rc = csync_vio_closedir(csync, dh);
  83. assert_int_equal(rc, 0);
  84. }
  85. static void check_csync_vio_opendir_perm(void **state)
  86. {
  87. CSYNC *csync = (CSYNC*)*state;
  88. csync_vio_handle_t *dh = nullptr;
  89. int rc = 0;
  90. mbchar_t *dir = c_utf8_path_to_locale(CSYNC_TEST_DIR);
  91. assert_non_null(dir);
  92. rc = _tmkdir(dir, (S_IWUSR|S_IXUSR));
  93. assert_int_equal(rc, 0);
  94. dh = csync_vio_opendir(csync, CSYNC_TEST_DIR);
  95. assert_null(dh);
  96. assert_int_equal(errno, EACCES);
  97. _tchmod(dir, MKDIR_MASK);
  98. c_free_locale_string(dir);
  99. }
  100. static void check_csync_vio_closedir_null(void **state)
  101. {
  102. CSYNC *csync = (CSYNC*)*state;
  103. int rc = 0;
  104. rc = csync_vio_closedir(csync, nullptr);
  105. assert_int_equal(rc, -1);
  106. }
  107. int torture_run_tests(void)
  108. {
  109. const struct CMUnitTest tests[] = {
  110. cmocka_unit_test_setup_teardown(check_csync_vio_opendir, setup_dir, teardown),
  111. cmocka_unit_test_setup_teardown(check_csync_vio_opendir_perm, setup, teardown),
  112. cmocka_unit_test(check_csync_vio_closedir_null),
  113. };
  114. return cmocka_run_group_tests(tests, nullptr, nullptr);
  115. }