test_hs_dos.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* Copyright (c) 2017-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs_cell.c
  5. * \brief Test hidden service cell functionality.
  6. */
  7. #define CIRCUITLIST_PRIVATE
  8. #define NETWORKSTATUS_PRIVATE
  9. #define HS_DOS_PRIVATE
  10. #define HS_INTROPOINT_PRIVATE
  11. #include "test/test.h"
  12. #include "test/test_helpers.h"
  13. #include "test/log_test_helpers.h"
  14. #include "app/config/config.h"
  15. #include "core/or/circuitlist.h"
  16. #include "core/or/circuituse.h"
  17. #include "core/or/or_circuit_st.h"
  18. #include "feature/hs/hs_dos.h"
  19. #include "feature/hs/hs_intropoint.h"
  20. #include "feature/nodelist/networkstatus.h"
  21. static void
  22. setup_mock_consensus(void)
  23. {
  24. current_ns_consensus = tor_malloc_zero(sizeof(networkstatus_t));
  25. current_ns_consensus->net_params = smartlist_new();
  26. smartlist_add(current_ns_consensus->net_params,
  27. (void *) "HiddenServiceEnableIntroDoSDefense=1");
  28. hs_dos_consensus_has_changed(current_ns_consensus);
  29. }
  30. static void
  31. free_mock_consensus(void)
  32. {
  33. smartlist_free(current_ns_consensus->net_params);
  34. tor_free(current_ns_consensus);
  35. }
  36. static void
  37. test_can_send_intro2(void *arg)
  38. {
  39. uint32_t now = (uint32_t) approx_time();
  40. or_circuit_t *or_circ = NULL;
  41. (void) arg;
  42. hs_init();
  43. hs_dos_init();
  44. get_options_mutable()->ORPort_set = 1;
  45. setup_mock_consensus();
  46. or_circ = or_circuit_new(1, NULL);
  47. /* Make that circuit a service intro point. */
  48. circuit_change_purpose(TO_CIRCUIT(or_circ), CIRCUIT_PURPOSE_INTRO_POINT);
  49. hs_dos_setup_default_intro2_defenses(or_circ);
  50. or_circ->introduce2_dos_defense_enabled = 1;
  51. /* Brand new circuit, we should be able to send INTRODUCE2 cells. */
  52. tt_int_op(true, OP_EQ, hs_dos_can_send_intro2(or_circ));
  53. /* Simulate that 10 cells have arrived in 1 second. There should be no
  54. * refill since the bucket is already at maximum on the first cell. */
  55. update_approx_time(++now);
  56. for (int i = 0; i < 10; i++) {
  57. tt_int_op(true, OP_EQ, hs_dos_can_send_intro2(or_circ));
  58. }
  59. tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ,
  60. get_intro2_burst_consensus_param(NULL) - 10);
  61. /* Fully refill the bucket minus 1 cell. */
  62. update_approx_time(++now);
  63. tt_int_op(true, OP_EQ, hs_dos_can_send_intro2(or_circ));
  64. tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ,
  65. get_intro2_burst_consensus_param(NULL) - 1);
  66. /* Receive an INTRODUCE2 at each second. We should have the bucket full
  67. * since at every second it gets refilled. */
  68. for (int i = 0; i < 10; i++) {
  69. update_approx_time(++now);
  70. tt_int_op(true, OP_EQ, hs_dos_can_send_intro2(or_circ));
  71. }
  72. /* Last check if we can send the cell decrements the bucket so minus 1. */
  73. tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ,
  74. get_intro2_burst_consensus_param(NULL) - 1);
  75. /* Manually reset bucket for next test. */
  76. token_bucket_ctr_reset(&or_circ->introduce2_bucket, now);
  77. tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ,
  78. get_intro2_burst_consensus_param(NULL));
  79. /* Do a full burst in the current second which should empty the bucket and
  80. * we shouldn't be allowed to send one more cell after that. We go minus 1
  81. * cell else the very last check if we can send the INTRO2 cell returns
  82. * false because the bucket goes down to 0. */
  83. for (uint32_t i = 0; i < get_intro2_burst_consensus_param(NULL) - 1; i++) {
  84. tt_int_op(true, OP_EQ, hs_dos_can_send_intro2(or_circ));
  85. }
  86. tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ, 1);
  87. /* Get the last remaining cell, we shouldn't be allowed to send it. */
  88. tt_int_op(false, OP_EQ, hs_dos_can_send_intro2(or_circ));
  89. tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ, 0);
  90. /* Make sure the next 100 cells aren't allowed and bucket stays at 0. */
  91. for (int i = 0; i < 100; i++) {
  92. tt_int_op(false, OP_EQ, hs_dos_can_send_intro2(or_circ));
  93. tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ, 0);
  94. }
  95. /* One second has passed, we should have the rate minus 1 cell added. */
  96. update_approx_time(++now);
  97. tt_int_op(true, OP_EQ, hs_dos_can_send_intro2(or_circ));
  98. tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ,
  99. get_intro2_rate_consensus_param(NULL) - 1);
  100. done:
  101. circuit_free_(TO_CIRCUIT(or_circ));
  102. hs_free_all();
  103. free_mock_consensus();
  104. }
  105. static void
  106. test_validate_dos_extension_params(void *arg)
  107. {
  108. bool ret;
  109. (void) arg;
  110. /* Validate the default values. */
  111. ret = cell_dos_extension_parameters_are_valid(
  112. get_intro2_rate_consensus_param(NULL),
  113. get_intro2_burst_consensus_param(NULL));
  114. tt_assert(ret);
  115. /* Valid custom rate/burst. */
  116. ret = cell_dos_extension_parameters_are_valid(17, 42);
  117. tt_assert(ret);
  118. ret = cell_dos_extension_parameters_are_valid(INT32_MAX, INT32_MAX);
  119. tt_assert(ret);
  120. /* Invalid rate. */
  121. ret = cell_dos_extension_parameters_are_valid(UINT64_MAX, 42);
  122. tt_assert(!ret);
  123. /* Invalid burst. */
  124. ret = cell_dos_extension_parameters_are_valid(42, UINT64_MAX);
  125. tt_assert(!ret);
  126. /* Value of 0 is valid (but should disable defenses) */
  127. ret = cell_dos_extension_parameters_are_valid(0, 0);
  128. tt_assert(ret);
  129. /* Can't have burst smaller than rate. */
  130. ret = cell_dos_extension_parameters_are_valid(42, 40);
  131. tt_assert(!ret);
  132. done:
  133. return;
  134. }
  135. struct testcase_t hs_dos_tests[] = {
  136. { "can_send_intro2", test_can_send_intro2, TT_FORK,
  137. NULL, NULL },
  138. { "validate_dos_extension_params", test_validate_dos_extension_params,
  139. TT_FORK, NULL, NULL },
  140. END_OF_TESTCASES
  141. };