[tor-commits] [tor/master] test/circuitstats: Refactor common code

nickm at torproject.org nickm at torproject.org
Tue Jun 9 19:45:23 UTC 2020


commit 9949b545c8890abfb192ce4d2397e0db959645b7
Author: teor <teor at riseup.net>
Date:   Thu May 14 10:38:09 2020 +1000

    test/circuitstats: Refactor common code
    
    Refactor common circuitstats test code into its own function.
    
    Part of 33222.
---
 src/test/test_circuitstats.c | 85 ++++++++++++++++++++++++++++----------------
 1 file changed, 54 insertions(+), 31 deletions(-)

diff --git a/src/test/test_circuitstats.c b/src/test/test_circuitstats.c
index 3660e4295..4322da189 100644
--- a/src/test/test_circuitstats.c
+++ b/src/test/test_circuitstats.c
@@ -23,6 +23,12 @@
 #include "core/or/extend_info_st.h"
 #include "core/or/origin_circuit_st.h"
 
+static origin_circuit_t *new_test_origin_circuit(
+                                             bool has_opened,
+                                             struct timeval circ_start_time,
+                                             int path_len,
+                                             extend_info_t **ei_list);
+
 static origin_circuit_t *add_opened_threehop(void);
 static origin_circuit_t *build_unopened_fourhop(struct timeval);
 static origin_circuit_t *subtest_fourhop_circuit(struct timeval, int);
@@ -43,50 +49,67 @@ mock_circuit_mark_for_close(circuit_t *circ, int reason, int line,
 }
 
 static origin_circuit_t *
-add_opened_threehop(void)
+new_test_origin_circuit(bool has_opened,
+                        struct timeval circ_start_time,
+                        int path_len,
+                        extend_info_t **ei_list)
 {
   origin_circuit_t *origin_circ = origin_circuit_new();
-  extend_info_t fakehop;
-  memset(&fakehop, 0, sizeof(fakehop));
 
   TO_CIRCUIT(origin_circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
 
   origin_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
-  origin_circ->build_state->desired_path_len = DEFAULT_ROUTE_LEN;
-
-  cpath_append_hop(&origin_circ->cpath, &fakehop);
-  cpath_append_hop(&origin_circ->cpath, &fakehop);
-  cpath_append_hop(&origin_circ->cpath, &fakehop);
-
-  origin_circ->has_opened = 1;
-  TO_CIRCUIT(origin_circ)->state = CIRCUIT_STATE_OPEN;
-  TO_CIRCUIT(origin_circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
+  origin_circ->build_state->desired_path_len = path_len;
+
+  if (ei_list) {
+    for (int i = 0; i < path_len; i++) {
+      extend_info_t *ei = ei_list[i];
+      cpath_append_hop(&origin_circ->cpath, ei);
+    }
+  }
+
+  if (has_opened) {
+    origin_circ->has_opened = 1;
+    TO_CIRCUIT(origin_circ)->state = CIRCUIT_STATE_OPEN;
+  } else {
+    TO_CIRCUIT(origin_circ)->timestamp_began = circ_start_time;
+    TO_CIRCUIT(origin_circ)->timestamp_created = circ_start_time;
+  }
 
   return origin_circ;
 }
 
 static origin_circuit_t *
-build_unopened_fourhop(struct timeval circ_start_time)
+add_opened_threehop(void)
 {
-  origin_circuit_t *origin_circ = origin_circuit_new();
-  extend_info_t *fakehop = tor_malloc_zero(sizeof(extend_info_t));
-  memset(fakehop, 0, sizeof(extend_info_t));
-
-  TO_CIRCUIT(origin_circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
-  TO_CIRCUIT(origin_circ)->timestamp_began = circ_start_time;
-  TO_CIRCUIT(origin_circ)->timestamp_created = circ_start_time;
-
-  origin_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
-  origin_circ->build_state->desired_path_len = 4;
-
-  cpath_append_hop(&origin_circ->cpath, fakehop);
-  cpath_append_hop(&origin_circ->cpath, fakehop);
-  cpath_append_hop(&origin_circ->cpath, fakehop);
-  cpath_append_hop(&origin_circ->cpath, fakehop);
-
-  tor_free(fakehop);
+  struct timeval circ_start_time;
+  memset(&circ_start_time, 0, sizeof(circ_start_time));
+  extend_info_t fakehop;
+  memset(&fakehop, 0, sizeof(fakehop));
+  extend_info_t *fakehop_list[DEFAULT_ROUTE_LEN] = {&fakehop,
+                                                    &fakehop,
+                                                    &fakehop};
+
+  return new_test_origin_circuit(true,
+                                 circ_start_time,
+                                 DEFAULT_ROUTE_LEN,
+                                 fakehop_list);
+}
 
-  return origin_circ;
+static origin_circuit_t *
+build_unopened_fourhop(struct timeval circ_start_time)
+{
+  extend_info_t fakehop;
+  memset(&fakehop, 0, sizeof(fakehop));
+  extend_info_t *fakehop_list[4] = {&fakehop,
+                                    &fakehop,
+                                    &fakehop,
+                                    &fakehop};
+
+  return new_test_origin_circuit(false,
+                                 circ_start_time,
+                                 4,
+                                 fakehop_list);
 }
 
 static origin_circuit_t *





More information about the tor-commits mailing list