test_accounting.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. goto done;
  52. done:
  53. NS_UNMOCK(get_or_state);
  54. or_state_free(or_state);
  55. }
  56. #undef NS_SUBMODULE
  57. struct testcase_t accounting_tests[] = {
  58. { "bwlimits", test_accounting_limits, TT_FORK, NULL, NULL },
  59. END_OF_TESTCASES
  60. };