test_accounting.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "or.h"
  2. #include "test.h"
  3. #define HIBERNATE_PRIVATE
  4. #include "hibernate.h"
  5. #include "config.h"
  6. #define STATEFILE_PRIVATE
  7. #include "statefile.h"
  8. #define NS_MODULE accounting
  9. #define NS_SUBMODULE limits
  10. /*
  11. * Test to make sure accounting triggers hibernation
  12. * correctly with both sum or max rules set
  13. */
  14. static or_state_t *or_state;
  15. NS_DECL(or_state_t *, get_or_state, (void));
  16. static or_state_t *
  17. NS(get_or_state)(void)
  18. {
  19. return or_state;
  20. }
  21. static void
  22. test_accounting_limits(void *arg)
  23. {
  24. or_options_t *options = get_options_mutable();
  25. time_t fake_time = time(NULL);
  26. (void) arg;
  27. NS_MOCK(get_or_state);
  28. or_state = or_state_new();
  29. options->AccountingMax = 100;
  30. options->AccountingRule = ACCT_MAX;
  31. tor_assert(accounting_is_enabled(options));
  32. configure_accounting(fake_time);
  33. accounting_add_bytes(10, 0, 1);
  34. fake_time += 1;
  35. consider_hibernation(fake_time);
  36. tor_assert(we_are_hibernating() == 0);
  37. accounting_add_bytes(90, 0, 1);
  38. fake_time += 1;
  39. consider_hibernation(fake_time);
  40. tor_assert(we_are_hibernating() == 1);
  41. options->AccountingMax = 200;
  42. options->AccountingRule = ACCT_SUM;
  43. accounting_add_bytes(0, 10, 1);
  44. fake_time += 1;
  45. consider_hibernation(fake_time);
  46. tor_assert(we_are_hibernating() == 0);
  47. accounting_add_bytes(0, 90, 1);
  48. fake_time += 1;
  49. consider_hibernation(fake_time);
  50. tor_assert(we_are_hibernating() == 1);
  51. options->AccountingRule = ACCT_OUT;
  52. accounting_add_bytes(100, 10, 1);
  53. fake_time += 1;
  54. consider_hibernation(fake_time);
  55. tor_assert(we_are_hibernating() == 0);
  56. accounting_add_bytes(0, 90, 1);
  57. fake_time += 1;
  58. consider_hibernation(fake_time);
  59. tor_assert(we_are_hibernating() == 1);
  60. options->AccountingMax = 300;
  61. options->AccountingRule = ACCT_IN;
  62. accounting_add_bytes(10, 100, 1);
  63. fake_time += 1;
  64. consider_hibernation(fake_time);
  65. tor_assert(we_are_hibernating() == 0);
  66. accounting_add_bytes(90, 0, 1);
  67. fake_time += 1;
  68. consider_hibernation(fake_time);
  69. tor_assert(we_are_hibernating() == 1);
  70. goto done;
  71. done:
  72. NS_UNMOCK(get_or_state);
  73. or_state_free(or_state);
  74. }
  75. #undef NS_SUBMODULE
  76. struct testcase_t accounting_tests[] = {
  77. { "bwlimits", test_accounting_limits, TT_FORK, NULL, NULL },
  78. END_OF_TESTCASES
  79. };