check_csync_log.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "torture.h"
  23. #include "csync.h"
  24. #include "csync_log.c"
  25. #include "c_private.h"
  26. static int setup(void **state) {
  27. CSYNC *csync;
  28. int rc;
  29. rc = system("mkdir -p /tmp/check_csync1");
  30. assert_int_equal(rc, 0);
  31. csync_create(&csync, "/tmp/check_csync1");
  32. *state = csync;
  33. return 0;
  34. }
  35. static int teardown(void **state) {
  36. CSYNC *csync = *state;
  37. int rc;
  38. rc = csync_destroy(csync);
  39. rc = system("rm -rf /tmp/check_csync");
  40. assert_int_equal(rc, 0);
  41. rc = system("rm -rf /tmp/check_csync1");
  42. assert_int_equal(rc, 0);
  43. *state = NULL;
  44. return 0;
  45. }
  46. static void check_log_callback(int verbosity,
  47. const char *function,
  48. const char *buffer)
  49. {
  50. int rc;
  51. assert_true(verbosity >= 0);
  52. assert_non_null(function);
  53. assert_false(function[0] == '\0');
  54. assert_non_null(buffer);
  55. assert_false(buffer[0] == '\0');
  56. rc = system("touch /tmp/check_csync1/cb_called");
  57. assert_int_equal(rc, 0);
  58. }
  59. static void check_set_log_level(void **state)
  60. {
  61. int rc;
  62. (void) state;
  63. rc = csync_set_log_level(-5);
  64. assert_int_equal(rc, -1);
  65. rc = csync_set_log_level(5);
  66. assert_int_equal(rc, 0);
  67. rc = csync_get_log_level();
  68. assert_int_equal(rc, 5);
  69. }
  70. static void check_set_auth_callback(void **state)
  71. {
  72. csync_log_callback log_fn;
  73. int rc;
  74. (void) state;
  75. rc = csync_set_log_callback(NULL);
  76. assert_int_equal(rc, -1);
  77. rc = csync_set_log_callback(check_log_callback);
  78. assert_int_equal(rc, 0);
  79. log_fn = csync_get_log_callback();
  80. assert_non_null(log_fn);
  81. assert_true(log_fn == &check_log_callback);
  82. }
  83. static void check_logging(void **state)
  84. {
  85. int rc;
  86. csync_stat_t sb;
  87. mbchar_t *path;
  88. path = c_utf8_path_to_locale("/tmp/check_csync1/cb_called");
  89. (void) state; /* unused */
  90. assert_non_null(path);
  91. rc = csync_set_log_level(1);
  92. assert_int_equal(rc, 0);
  93. rc = csync_set_log_callback(check_log_callback);
  94. assert_int_equal(rc, 0);
  95. csync_log(1, __func__, "rc = %d", rc);
  96. rc = _tstat(path, &sb);
  97. c_free_locale_string(path);
  98. assert_int_equal(rc, 0);
  99. }
  100. int torture_run_tests(void)
  101. {
  102. const struct CMUnitTest tests[] = {
  103. cmocka_unit_test(check_set_log_level),
  104. cmocka_unit_test(check_set_auth_callback),
  105. cmocka_unit_test_setup_teardown(check_logging, setup, teardown),
  106. };
  107. return cmocka_run_group_tests(tests, NULL, NULL);
  108. }