test_logging.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* Copyright (c) 2013-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define CONFIG_PRIVATE
  4. #include "orconfig.h"
  5. #include "or/or.h"
  6. #include "or/config.h"
  7. #include "lib/err/torerr.h"
  8. #include "lib/log/torlog.h"
  9. #include "test/test.h"
  10. #include "lib/process/subprocess.h"
  11. static void
  12. dummy_cb_fn(int severity, uint32_t domain, const char *msg)
  13. {
  14. (void)severity; (void)domain; (void)msg;
  15. }
  16. static void
  17. test_get_sigsafe_err_fds(void *arg)
  18. {
  19. const int *fds;
  20. int n;
  21. log_severity_list_t include_bug, no_bug, no_bug2;
  22. (void) arg;
  23. init_logging(1);
  24. n = tor_log_get_sigsafe_err_fds(&fds);
  25. tt_int_op(n, OP_EQ, 1);
  26. tt_int_op(fds[0], OP_EQ, STDERR_FILENO);
  27. set_log_severity_config(LOG_WARN, LOG_ERR, &include_bug);
  28. set_log_severity_config(LOG_WARN, LOG_ERR, &no_bug);
  29. no_bug.masks[0] &= ~(LD_BUG|LD_GENERAL);
  30. set_log_severity_config(LOG_INFO, LOG_NOTICE, &no_bug2);
  31. /* Add some logs; make sure the output is as expected. */
  32. mark_logs_temp();
  33. add_stream_log(&include_bug, "dummy-1", 3);
  34. add_stream_log(&no_bug, "dummy-2", 4);
  35. add_stream_log(&no_bug2, "dummy-3", 5);
  36. add_callback_log(&include_bug, dummy_cb_fn);
  37. close_temp_logs();
  38. tor_log_update_sigsafe_err_fds();
  39. n = tor_log_get_sigsafe_err_fds(&fds);
  40. tt_int_op(n, OP_EQ, 2);
  41. tt_int_op(fds[0], OP_EQ, STDERR_FILENO);
  42. tt_int_op(fds[1], OP_EQ, 3);
  43. /* Allow STDOUT to replace STDERR. */
  44. add_stream_log(&include_bug, "dummy-4", STDOUT_FILENO);
  45. tor_log_update_sigsafe_err_fds();
  46. n = tor_log_get_sigsafe_err_fds(&fds);
  47. tt_int_op(n, OP_EQ, 2);
  48. tt_int_op(fds[0], OP_EQ, 3);
  49. tt_int_op(fds[1], OP_EQ, STDOUT_FILENO);
  50. /* But don't allow it to replace explicit STDERR. */
  51. add_stream_log(&include_bug, "dummy-5", STDERR_FILENO);
  52. tor_log_update_sigsafe_err_fds();
  53. n = tor_log_get_sigsafe_err_fds(&fds);
  54. tt_int_op(n, OP_EQ, 3);
  55. tt_int_op(fds[0], OP_EQ, STDERR_FILENO);
  56. tt_int_op(fds[1], OP_EQ, STDOUT_FILENO);
  57. tt_int_op(fds[2], OP_EQ, 3);
  58. /* Don't overflow the array. */
  59. {
  60. int i;
  61. for (i=5; i<20; ++i) {
  62. add_stream_log(&include_bug, "x-dummy", i);
  63. }
  64. }
  65. tor_log_update_sigsafe_err_fds();
  66. n = tor_log_get_sigsafe_err_fds(&fds);
  67. tt_int_op(n, OP_EQ, 8);
  68. done:
  69. ;
  70. }
  71. static void
  72. test_sigsafe_err(void *arg)
  73. {
  74. const char *fn=get_fname("sigsafe_err_log");
  75. char *content=NULL;
  76. log_severity_list_t include_bug;
  77. smartlist_t *lines = smartlist_new();
  78. (void)arg;
  79. set_log_severity_config(LOG_WARN, LOG_ERR, &include_bug);
  80. init_logging(1);
  81. mark_logs_temp();
  82. open_and_add_file_log(&include_bug, fn, 0);
  83. tor_log_update_sigsafe_err_fds();
  84. close_temp_logs();
  85. close(STDERR_FILENO);
  86. log_err(LD_BUG, "Say, this isn't too cool.");
  87. tor_log_err_sigsafe("Minimal.\n", NULL);
  88. set_log_time_granularity(100*1000);
  89. tor_log_err_sigsafe("Testing any ",
  90. "attempt to manually log ",
  91. "from a signal.\n",
  92. NULL);
  93. mark_logs_temp();
  94. close_temp_logs();
  95. close(STDERR_FILENO);
  96. content = read_file_to_str(fn, 0, NULL);
  97. tt_ptr_op(content, OP_NE, NULL);
  98. tor_split_lines(lines, content, (int)strlen(content));
  99. tt_int_op(smartlist_len(lines), OP_GE, 5);
  100. if (strstr(smartlist_get(lines, 0), "opening new log file"))
  101. smartlist_del_keeporder(lines, 0);
  102. tt_assert(strstr(smartlist_get(lines, 0), "Say, this isn't too cool"));
  103. /* Next line is blank. */
  104. tt_assert(!strcmpstart(smartlist_get(lines, 1), "=============="));
  105. tt_assert(!strcmpstart(smartlist_get(lines, 2), "Minimal."));
  106. /* Next line is blank. */
  107. tt_assert(!strcmpstart(smartlist_get(lines, 3), "=============="));
  108. tt_str_op(smartlist_get(lines, 4), OP_EQ,
  109. "Testing any attempt to manually log from a signal.");
  110. done:
  111. tor_free(content);
  112. smartlist_free(lines);
  113. }
  114. static void
  115. test_ratelim(void *arg)
  116. {
  117. (void) arg;
  118. ratelim_t ten_min = RATELIM_INIT(10*60);
  119. const time_t start = 1466091600;
  120. time_t now = start;
  121. /* Initially, we're ready. */
  122. char *msg = NULL;
  123. msg = rate_limit_log(&ten_min, now);
  124. tt_ptr_op(msg, OP_NE, NULL);
  125. tt_str_op(msg, OP_EQ, ""); /* nothing was suppressed. */
  126. tt_int_op(ten_min.last_allowed, OP_EQ, now);
  127. tor_free(msg);
  128. int i;
  129. for (i = 0; i < 9; ++i) {
  130. now += 60; /* one minute has passed. */
  131. msg = rate_limit_log(&ten_min, now);
  132. tt_ptr_op(msg, OP_EQ, NULL);
  133. tt_int_op(ten_min.last_allowed, OP_EQ, start);
  134. tt_int_op(ten_min.n_calls_since_last_time, OP_EQ, i + 1);
  135. }
  136. now += 240; /* Okay, we can be done. */
  137. msg = rate_limit_log(&ten_min, now);
  138. tt_ptr_op(msg, OP_NE, NULL);
  139. tt_str_op(msg, OP_EQ,
  140. " [9 similar message(s) suppressed in last 600 seconds]");
  141. done:
  142. tor_free(msg);
  143. }
  144. struct testcase_t logging_tests[] = {
  145. { "sigsafe_err_fds", test_get_sigsafe_err_fds, TT_FORK, NULL, NULL },
  146. { "sigsafe_err", test_sigsafe_err, TT_FORK, NULL, NULL },
  147. { "ratelim", test_ratelim, 0, NULL, NULL },
  148. END_OF_TESTCASES
  149. };