"Fossies" - the Fresh Open Source Software Archive

Member "tor-0.4.1.6/src/test/test_circuituse.c" (10 Jun 2019, 7679 Bytes) of package /linux/misc/tor-0.4.1.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* Copyright (c) 2001-2004, Roger Dingledine.
    2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
    3  * Copyright (c) 2007-2019, The Tor Project, Inc. */
    4 /* See LICENSE for licensing information */
    5 
    6 #define CIRCUITLIST_PRIVATE
    7 
    8 #include "core/or/or.h"
    9 #include "test/test.h"
   10 #include "test/test_helpers.h"
   11 #include "app/config/config.h"
   12 #include "core/or/circuitlist.h"
   13 #include "core/or/circuituse.h"
   14 #include "core/or/circuitbuild.h"
   15 #include "feature/nodelist/nodelist.h"
   16 
   17 #include "core/or/cpath_build_state_st.h"
   18 #include "core/or/origin_circuit_st.h"
   19 
   20 static void
   21 test_circuit_is_available_for_use_ret_false_when_marked_for_close(void *arg)
   22 {
   23   (void)arg;
   24 
   25   circuit_t *circ = tor_malloc(sizeof(circuit_t));
   26   circ->marked_for_close = 1;
   27 
   28   tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
   29 
   30   done:
   31     tor_free(circ);
   32 }
   33 
   34 static void
   35 test_circuit_is_available_for_use_ret_false_when_timestamp_dirty(void *arg)
   36 {
   37   (void)arg;
   38 
   39   circuit_t *circ = tor_malloc(sizeof(circuit_t));
   40   circ->timestamp_dirty = 1;
   41 
   42   tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
   43 
   44   done:
   45     tor_free(circ);
   46 }
   47 
   48 static void
   49 test_circuit_is_available_for_use_ret_false_for_non_general_purpose(void *arg)
   50 {
   51   (void)arg;
   52 
   53   circuit_t *circ = tor_malloc(sizeof(circuit_t));
   54   circ->purpose = CIRCUIT_PURPOSE_REND_POINT_WAITING;
   55 
   56   tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
   57 
   58   done:
   59     tor_free(circ);
   60 }
   61 
   62 static void
   63 test_circuit_is_available_for_use_ret_false_for_non_general_origin(void *arg)
   64 {
   65   (void)arg;
   66 
   67   circuit_t *circ = tor_malloc(sizeof(circuit_t));
   68   circ->purpose = CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT;
   69 
   70   tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
   71 
   72   done:
   73     tor_free(circ);
   74 }
   75 
   76 static void
   77 test_circuit_is_available_for_use_ret_false_for_non_origin_purpose(void *arg)
   78 {
   79   (void)arg;
   80 
   81   circuit_t *circ = tor_malloc(sizeof(circuit_t));
   82   circ->purpose = CIRCUIT_PURPOSE_OR;
   83 
   84   tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
   85 
   86   done:
   87     tor_free(circ);
   88 }
   89 
   90 static void
   91 test_circuit_is_available_for_use_ret_false_unusable_for_new_conns(void *arg)
   92 {
   93   (void)arg;
   94 
   95   circuit_t *circ = dummy_origin_circuit_new(30);
   96   mark_circuit_unusable_for_new_conns(TO_ORIGIN_CIRCUIT(circ));
   97 
   98   tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
   99 
  100   done:
  101     circuit_free(circ);
  102 }
  103 
  104 static void
  105 test_circuit_is_available_for_use_returns_false_for_onehop_tunnel(void *arg)
  106 {
  107   (void)arg;
  108 
  109   circuit_t *circ = dummy_origin_circuit_new(30);
  110   origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(circ);
  111   oc->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  112   oc->build_state->onehop_tunnel = 1;
  113 
  114   tt_int_op(0, OP_EQ, circuit_is_available_for_use(circ));
  115 
  116   done:
  117     circuit_free(circ);
  118 }
  119 
  120 static void
  121 test_circuit_is_available_for_use_returns_true_for_clean_circuit(void *arg)
  122 {
  123   (void)arg;
  124 
  125   circuit_t *circ = dummy_origin_circuit_new(30);
  126   origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(circ);
  127   oc->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  128   oc->build_state->onehop_tunnel = 0;
  129 
  130   tt_int_op(1, OP_EQ, circuit_is_available_for_use(circ));
  131 
  132   done:
  133     circuit_free(circ);
  134 }
  135 
  136 static int
  137 mock_circuit_all_predicted_ports_handled(time_t now,
  138                                          int *need_uptime,
  139                                          int *need_capacity)
  140 {
  141   (void)now;
  142 
  143   if (need_uptime && need_capacity)
  144     return 0;
  145   return 1;
  146 }
  147 
  148 static consensus_path_type_t
  149 mock_router_have_unknown_consensus_path(void)
  150 {
  151   return CONSENSUS_PATH_UNKNOWN;
  152 }
  153 
  154 static consensus_path_type_t
  155 mock_router_have_exit_consensus_path(void)
  156 {
  157   return CONSENSUS_PATH_EXIT;
  158 }
  159 
  160 static void
  161 test_needs_exit_circuits_ret_false_for_predicted_ports_and_path(void *arg)
  162 {
  163   (void)arg;
  164 
  165   MOCK(circuit_all_predicted_ports_handled,
  166        mock_circuit_all_predicted_ports_handled);
  167   int needs_uptime = 1;
  168   int needs_capacity = 0;
  169 
  170   time_t now = time(NULL);
  171   tt_int_op(0, OP_EQ,
  172             needs_exit_circuits(now, &needs_uptime, &needs_capacity));
  173 
  174   done:
  175     UNMOCK(circuit_all_predicted_ports_handled);
  176 }
  177 
  178 static void
  179 test_needs_exit_circuits_ret_false_for_non_exit_consensus_path(void *arg)
  180 {
  181   (void)arg;
  182 
  183   MOCK(circuit_all_predicted_ports_handled,
  184        mock_circuit_all_predicted_ports_handled);
  185   int needs_uptime = 1;
  186   int needs_capacity = 1;
  187   MOCK(router_have_consensus_path, mock_router_have_unknown_consensus_path);
  188 
  189   time_t now = time(NULL);
  190   tt_int_op(0, OP_EQ,
  191             needs_exit_circuits(now, &needs_uptime, &needs_capacity));
  192 
  193   done:
  194     UNMOCK(circuit_all_predicted_ports_handled);
  195     UNMOCK(router_have_consensus_path);
  196 }
  197 
  198 static void
  199 test_needs_exit_circuits_ret_true_for_predicted_ports_and_path(void *arg)
  200 {
  201   (void)arg;
  202 
  203   MOCK(circuit_all_predicted_ports_handled,
  204        mock_circuit_all_predicted_ports_handled);
  205   int needs_uptime = 1;
  206   int needs_capacity = 1;
  207   MOCK(router_have_consensus_path, mock_router_have_exit_consensus_path);
  208 
  209   time_t now = time(NULL);
  210   tt_int_op(1, OP_EQ,
  211             needs_exit_circuits(now, &needs_uptime, &needs_capacity));
  212 
  213   done:
  214     UNMOCK(circuit_all_predicted_ports_handled);
  215     UNMOCK(router_have_consensus_path);
  216 }
  217 
  218 static void
  219 test_needs_circuits_for_build_ret_false_consensus_path_unknown(void *arg)
  220 {
  221   (void)arg;
  222   MOCK(router_have_consensus_path, mock_router_have_unknown_consensus_path);
  223   tt_int_op(0, OP_EQ, needs_circuits_for_build(0));
  224   done: ;
  225 }
  226 
  227 static void
  228 test_needs_circuits_for_build_ret_false_if_num_less_than_max(void *arg)
  229 {
  230   (void)arg;
  231   MOCK(router_have_consensus_path, mock_router_have_exit_consensus_path);
  232   tt_int_op(0, OP_EQ, needs_circuits_for_build(13));
  233   done:
  234     UNMOCK(router_have_consensus_path);
  235 }
  236 
  237 static void
  238 test_needs_circuits_for_build_returns_true_when_more_are_needed(void *arg)
  239 {
  240   (void)arg;
  241   MOCK(router_have_consensus_path, mock_router_have_exit_consensus_path);
  242   tt_int_op(1, OP_EQ, needs_circuits_for_build(0));
  243   done:
  244     UNMOCK(router_have_consensus_path);
  245 }
  246 
  247 struct testcase_t circuituse_tests[] = {
  248  { "marked",
  249    test_circuit_is_available_for_use_ret_false_when_marked_for_close,
  250    TT_FORK, NULL, NULL
  251  },
  252  { "timestamp",
  253    test_circuit_is_available_for_use_ret_false_when_timestamp_dirty,
  254    TT_FORK, NULL, NULL
  255  },
  256  { "non_general",
  257    test_circuit_is_available_for_use_ret_false_for_non_general_purpose,
  258    TT_FORK, NULL, NULL
  259  },
  260  { "non_general",
  261   test_circuit_is_available_for_use_ret_false_for_non_general_origin,
  262    TT_FORK, NULL, NULL
  263  },
  264  { "origin",
  265    test_circuit_is_available_for_use_ret_false_for_non_origin_purpose,
  266    TT_FORK, NULL, NULL
  267  },
  268  { "clean",
  269    test_circuit_is_available_for_use_ret_false_unusable_for_new_conns,
  270    TT_FORK, NULL, NULL
  271  },
  272  { "onehop",
  273    test_circuit_is_available_for_use_returns_false_for_onehop_tunnel,
  274    TT_FORK, NULL, NULL
  275  },
  276  { "clean_circ",
  277    test_circuit_is_available_for_use_returns_true_for_clean_circuit,
  278    TT_FORK, NULL, NULL
  279  },
  280  { "exit_f",
  281    test_needs_exit_circuits_ret_false_for_predicted_ports_and_path,
  282    TT_FORK, NULL, NULL
  283  },
  284  { "exit_t",
  285    test_needs_exit_circuits_ret_true_for_predicted_ports_and_path,
  286    TT_FORK, NULL, NULL
  287  },
  288  { "non_exit",
  289    test_needs_exit_circuits_ret_false_for_non_exit_consensus_path,
  290    TT_FORK, NULL, NULL
  291  },
  292  { "true",
  293    test_needs_exit_circuits_ret_true_for_predicted_ports_and_path,
  294    TT_FORK, NULL, NULL
  295  },
  296  { "consensus_path_unknown",
  297    test_needs_circuits_for_build_ret_false_consensus_path_unknown,
  298    TT_FORK, NULL, NULL
  299  },
  300  { "less_than_max",
  301    test_needs_circuits_for_build_ret_false_if_num_less_than_max,
  302    TT_FORK, NULL, NULL
  303  },
  304  { "more_needed",
  305    test_needs_circuits_for_build_returns_true_when_more_are_needed,
  306    TT_FORK, NULL, NULL
  307  },
  308   END_OF_TESTCASES
  309 };
  310