test_logging.c 4.7 KB

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