test_accounting.c 2.4 KB

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