[tor-commits] [tor/release-0.2.9] Bug 23512: Test recording bytes in circ queues.

teor at torproject.org teor at torproject.org
Wed Mar 13 23:06:43 UTC 2019


commit 6af352172d3dbf1571c8cb62cd0a618f4e2355d1
Author: Mike Perry <mikeperry-git at torproject.org>
Date:   Thu Sep 13 23:03:32 2018 +0000

    Bug 23512: Test recording bytes in circ queues.
---
 src/or/rephist.c      |  13 ++++--
 src/or/rephist.h      |   2 +
 src/test/test_relay.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++---
 3 files changed, 118 insertions(+), 9 deletions(-)

diff --git a/src/or/rephist.c b/src/or/rephist.c
index 7223c3759..211480284 100644
--- a/src/or/rephist.c
+++ b/src/or/rephist.c
@@ -88,6 +88,11 @@
 static void bw_arrays_init(void);
 static void predicted_ports_init(void);
 
+typedef struct bw_array_t bw_array_t;
+STATIC uint64_t find_largest_max(bw_array_t *b);
+STATIC void commit_max(bw_array_t *b);
+STATIC void advance_obs(bw_array_t *b);
+
 /** Total number of bytes currently allocated in fields used by rephist.c. */
 uint64_t rephist_total_alloc=0;
 /** Number of or_history_t objects currently allocated. */
@@ -1236,7 +1241,7 @@ typedef struct bw_array_t {
 } bw_array_t;
 
 /** Shift the current period of b forward by one. */
-static void
+STATIC void
 commit_max(bw_array_t *b)
 {
   /* Store total from current period. */
@@ -1256,7 +1261,7 @@ commit_max(bw_array_t *b)
 }
 
 /** Shift the current observation time of <b>b</b> forward by one second. */
-static inline void
+STATIC void
 advance_obs(bw_array_t *b)
 {
   int nextidx;
@@ -1331,7 +1336,7 @@ bw_array_free(bw_array_t *b)
 /** Recent history of bandwidth observations for read operations. */
 static bw_array_t *read_array = NULL;
 /** Recent history of bandwidth observations for write operations. */
-static bw_array_t *write_array = NULL;
+STATIC bw_array_t *write_array = NULL;
 /** Recent history of bandwidth observations for read operations for the
     directory protocol. */
 static bw_array_t *dir_read_array = NULL;
@@ -1408,7 +1413,7 @@ rep_hist_note_dir_bytes_read(uint64_t num_bytes, time_t when)
  * most bandwidth used in any NUM_SECS_ROLLING_MEASURE period for the last
  * NUM_SECS_BW_SUM_IS_VALID seconds.)
  */
-static uint64_t
+STATIC uint64_t
 find_largest_max(bw_array_t *b)
 {
   int i;
diff --git a/src/or/rephist.h b/src/or/rephist.h
index c1fc7ef1a..837bb8d04 100644
--- a/src/or/rephist.h
+++ b/src/or/rephist.h
@@ -117,6 +117,8 @@ extern uint32_t rephist_total_num;
 #ifdef TOR_UNIT_TESTS
 extern int onion_handshakes_requested[MAX_ONION_HANDSHAKE_TYPE+1];
 extern int onion_handshakes_assigned[MAX_ONION_HANDSHAKE_TYPE+1];
+typedef struct bw_array_t bw_array_t;
+extern bw_array_t *write_array;
 #endif
 
 #endif
diff --git a/src/test/test_relay.c b/src/test/test_relay.c
index 4713c79ea..1ec538395 100644
--- a/src/test/test_relay.c
+++ b/src/test/test_relay.c
@@ -4,6 +4,9 @@
 #include "or.h"
 #define CIRCUITBUILD_PRIVATE
 #include "circuitbuild.h"
+#include "circuitlist.h"
+#include "rephist.h"
+#include "channeltls.h"
 #define RELAY_PRIVATE
 #include "relay.h"
 /* For init/free stuff */
@@ -16,6 +19,9 @@
 static or_circuit_t * new_fake_orcirc(channel_t *nchan, channel_t *pchan);
 
 static void test_relay_append_cell_to_circuit_queue(void *arg);
+uint64_t find_largest_max(bw_array_t *b);
+void commit_max(bw_array_t *b);
+void advance_obs(bw_array_t *b);
 
 static or_circuit_t *
 new_fake_orcirc(channel_t *nchan, channel_t *pchan)
@@ -27,10 +33,9 @@ new_fake_orcirc(channel_t *nchan, channel_t *pchan)
   circ = &(orcirc->base_);
   circ->magic = OR_CIRCUIT_MAGIC;
 
-  circ->n_chan = nchan;
-  circ->n_circ_id = get_unique_circ_id_by_chan(nchan);
-  circ->n_mux = NULL; /* ?? */
+  circuit_set_n_circid_chan(circ, get_unique_circ_id_by_chan(nchan), nchan);
   cell_queue_init(&(circ->n_chan_cells));
+
   circ->n_hop = NULL;
   circ->streams_blocked_on_n_chan = 0;
   circ->streams_blocked_on_p_chan = 0;
@@ -43,14 +48,109 @@ new_fake_orcirc(channel_t *nchan, channel_t *pchan)
   circ->deliver_window = CIRCWINDOW_START_MAX;
   circ->n_chan_create_cell = NULL;
 
-  orcirc->p_chan = pchan;
-  orcirc->p_circ_id = get_unique_circ_id_by_chan(pchan);
+  /* for assert_circ_ok */
+  orcirc->p_crypto = (void*)1;
+  orcirc->n_crypto = (void*)1;
+  orcirc->n_digest = (void*)1;
+  orcirc->p_digest = (void*)1;
+
+  circuit_set_p_circid_chan(orcirc, get_unique_circ_id_by_chan(pchan), pchan);
   cell_queue_init(&(orcirc->p_chan_cells));
 
   return orcirc;
 }
 
 static void
+test_relay_close_circuit(void *arg)
+{
+  channel_t *nchan = NULL, *pchan = NULL;
+  or_circuit_t *orcirc = NULL;
+  cell_t *cell = NULL;
+  int old_count, new_count;
+
+  (void)arg;
+
+  /* Make fake channels to be nchan and pchan for the circuit */
+  nchan = new_fake_channel();
+  tt_assert(nchan);
+
+  pchan = new_fake_channel();
+  tt_assert(pchan);
+
+  /* We'll need chans with working cmuxes */
+  nchan->cmux = circuitmux_alloc();
+  pchan->cmux = circuitmux_alloc();
+
+  /* Make a fake orcirc */
+  orcirc = new_fake_orcirc(nchan, pchan);
+  tt_assert(orcirc);
+  circuitmux_attach_circuit(nchan->cmux, TO_CIRCUIT(orcirc),
+                            CELL_DIRECTION_OUT);
+  circuitmux_attach_circuit(pchan->cmux, TO_CIRCUIT(orcirc),
+                            CELL_DIRECTION_IN);
+
+  /* Make a cell */
+  cell = tor_malloc_zero(sizeof(cell_t));
+  make_fake_cell(cell);
+
+  MOCK(scheduler_channel_has_waiting_cells,
+       scheduler_channel_has_waiting_cells_mock);
+
+  /* Append it */
+  old_count = get_mock_scheduler_has_waiting_cells_count();
+  append_cell_to_circuit_queue(TO_CIRCUIT(orcirc), nchan, cell,
+                               CELL_DIRECTION_OUT, 0);
+  new_count = get_mock_scheduler_has_waiting_cells_count();
+  tt_int_op(new_count, OP_EQ, old_count + 1);
+
+  /* Now try the reverse direction */
+  old_count = get_mock_scheduler_has_waiting_cells_count();
+  append_cell_to_circuit_queue(TO_CIRCUIT(orcirc), pchan, cell,
+                               CELL_DIRECTION_IN, 0);
+  new_count = get_mock_scheduler_has_waiting_cells_count();
+  tt_int_op(new_count, OP_EQ, old_count + 1);
+
+  /* Ensure our write totals are 0 */
+  tt_int_op(find_largest_max(write_array), OP_EQ, 0);
+
+  /* Mark the circuit for close */
+  circuit_mark_for_close(TO_CIRCUIT(orcirc), 0);
+
+  /* Check our write totals. */
+  advance_obs(write_array);
+  commit_max(write_array);
+  /* Check for two cells plus overhead */
+  tt_int_op(find_largest_max(write_array), OP_EQ,
+                             2*(get_cell_network_size(nchan->wide_circ_ids)
+                                +TLS_PER_CELL_OVERHEAD));
+
+  UNMOCK(scheduler_channel_has_waiting_cells);
+
+  /* Get rid of the fake channels */
+  MOCK(scheduler_release_channel, scheduler_release_channel_mock);
+  channel_mark_for_close(nchan);
+  channel_mark_for_close(pchan);
+  UNMOCK(scheduler_release_channel);
+
+  /* Shut down channels */
+  channel_free_all();
+
+ done:
+  tor_free(cell);
+  if (orcirc) {
+    circuitmux_detach_circuit(nchan->cmux, TO_CIRCUIT(orcirc));
+    circuitmux_detach_circuit(pchan->cmux, TO_CIRCUIT(orcirc));
+    cell_queue_clear(&orcirc->base_.n_chan_cells);
+    cell_queue_clear(&orcirc->p_chan_cells);
+  }
+  tor_free(orcirc);
+  free_fake_channel(nchan);
+  free_fake_channel(pchan);
+
+  return;
+}
+
+static void
 test_relay_append_cell_to_circuit_queue(void *arg)
 {
   channel_t *nchan = NULL, *pchan = NULL;
@@ -129,6 +229,8 @@ test_relay_append_cell_to_circuit_queue(void *arg)
 struct testcase_t relay_tests[] = {
   { "append_cell_to_circuit_queue", test_relay_append_cell_to_circuit_queue,
     TT_FORK, NULL, NULL },
+  { "close_circ_rephist", test_relay_close_circuit,
+    TT_FORK, NULL, NULL },
   END_OF_TESTCASES
 };
 





More information about the tor-commits mailing list