test_accounting.c 2.3 KB

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