test_hs_dos.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include "test/test.h"
  10. #include "test/test_helpers.h"
  11. #include "test/log_test_helpers.h"
  12. #include "app/config/config.h"
  13. #include "core/or/circuitlist.h"
  14. #include "core/or/circuituse.h"
  15. #include "core/or/or_circuit_st.h"
  16. #include "feature/hs/hs_dos.h"
  17. #include "feature/nodelist/networkstatus.h"
  18. static void
  19. setup_mock_consensus(void)
  20. {
  21. current_ns_consensus = tor_malloc_zero(sizeof(networkstatus_t));
  22. current_ns_consensus->net_params = smartlist_new();
  23. smartlist_add(current_ns_consensus->net_params,
  24. (void *) "HiddenServiceEnableIntroDoSDefense=1");
  25. hs_dos_consensus_has_changed(current_ns_consensus);
  26. }
  27. static void
  28. free_mock_consensus(void)
  29. {
  30. smartlist_free(current_ns_consensus->net_params);
  31. tor_free(current_ns_consensus);
  32. }
  33. static void
  34. test_can_send_intro2(void *arg)
  35. {
  36. uint32_t now = (uint32_t) approx_time();
  37. or_circuit_t *or_circ = NULL;
  38. (void) arg;
  39. hs_init();
  40. hs_dos_init();
  41. get_options_mutable()->ORPort_set = 1;
  42. setup_mock_consensus();
  43. or_circ = or_circuit_new(1, NULL);
  44. /* Make that circuit a service intro point. */
  45. circuit_change_purpose(TO_CIRCUIT(or_circ), CIRCUIT_PURPOSE_INTRO_POINT);
  46. /* Initialize the INTRODUCE2 token bucket for the rate limiting. */
  47. token_bucket_ctr_init(&or_circ->introduce2_bucket, hs_dos_get_intro2_rate(),
  48. hs_dos_get_intro2_burst(), now);
  49. /* Brand new circuit, we should be able to send INTRODUCE2 cells. */
  50. tt_int_op(true, OP_EQ, hs_dos_can_send_intro2(or_circ));
  51. /* Simulate that 10 cells have arrived in 1 second. There should be no
  52. * refill since the bucket is already at maximum on the first cell. */
  53. update_approx_time(++now);
  54. for (int i = 0; i < 10; i++) {
  55. tt_int_op(true, OP_EQ, hs_dos_can_send_intro2(or_circ));
  56. }
  57. tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ,
  58. hs_dos_get_intro2_burst() - 10);
  59. /* Fully refill the bucket minus 1 cell. */
  60. update_approx_time(++now);
  61. tt_int_op(true, OP_EQ, hs_dos_can_send_intro2(or_circ));
  62. tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ,
  63. hs_dos_get_intro2_burst() - 1);
  64. /* Receive an INTRODUCE2 at each second. We should have the bucket full
  65. * since at every second it gets refilled. */
  66. for (int i = 0; i < 10; i++) {
  67. update_approx_time(++now);
  68. tt_int_op(true, OP_EQ, hs_dos_can_send_intro2(or_circ));
  69. }
  70. /* Last check if we can send the cell decrements the bucket so minus 1. */
  71. tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ,
  72. hs_dos_get_intro2_burst() - 1);
  73. /* Manually reset bucket for next test. */
  74. token_bucket_ctr_reset(&or_circ->introduce2_bucket, now);
  75. tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ,
  76. hs_dos_get_intro2_burst());
  77. /* Do a full burst in the current second which should empty the bucket and
  78. * we shouldn't be allowed to send one more cell after that. We go minus 1
  79. * cell else the very last check if we can send the INTRO2 cell returns
  80. * false because the bucket goes down to 0. */
  81. for (uint32_t i = 0; i < hs_dos_get_intro2_burst() - 1; i++) {
  82. tt_int_op(true, OP_EQ, hs_dos_can_send_intro2(or_circ));
  83. }
  84. tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ, 1);
  85. /* Get the last remaining cell, we shouldn't be allowed to send it. */
  86. tt_int_op(false, OP_EQ, hs_dos_can_send_intro2(or_circ));
  87. tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ, 0);
  88. /* Make sure the next 100 cells aren't allowed and bucket stays at 0. */
  89. for (int i = 0; i < 100; i++) {
  90. tt_int_op(false, OP_EQ, hs_dos_can_send_intro2(or_circ));
  91. tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ, 0);
  92. }
  93. /* One second has passed, we should have the rate minus 1 cell added. */
  94. update_approx_time(++now);
  95. tt_int_op(true, OP_EQ, hs_dos_can_send_intro2(or_circ));
  96. tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ,
  97. hs_dos_get_intro2_rate() - 1);
  98. done:
  99. circuit_free_(TO_CIRCUIT(or_circ));
  100. hs_free_all();
  101. free_mock_consensus();
  102. }
  103. struct testcase_t hs_dos_tests[] = {
  104. { "can_send_intro2", test_can_send_intro2, TT_FORK,
  105. NULL, NULL },
  106. END_OF_TESTCASES
  107. };