test_controller_events.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* Copyright (c) 2013-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define CONNECTION_PRIVATE
  4. #define TOR_CHANNEL_INTERNAL_
  5. #define CONTROL_PRIVATE
  6. #include "or.h"
  7. #include "channel.h"
  8. #include "channeltls.h"
  9. #include "connection.h"
  10. #include "control.h"
  11. #include "test.h"
  12. static void
  13. add_testing_cell_stats_entry(circuit_t *circ, uint8_t command,
  14. unsigned int waiting_time,
  15. unsigned int removed, unsigned int exitward)
  16. {
  17. testing_cell_stats_entry_t *ent = tor_malloc_zero(
  18. sizeof(testing_cell_stats_entry_t));
  19. ent->command = command;
  20. ent->waiting_time = waiting_time;
  21. ent->removed = removed;
  22. ent->exitward = exitward;
  23. if (!circ->testing_cell_stats)
  24. circ->testing_cell_stats = smartlist_new();
  25. smartlist_add(circ->testing_cell_stats, ent);
  26. }
  27. static void
  28. test_cntev_sum_up_cell_stats(void *arg)
  29. {
  30. or_circuit_t *or_circ;
  31. circuit_t *circ;
  32. cell_stats_t *cell_stats = NULL;
  33. (void)arg;
  34. /* This circuit is fake. */
  35. or_circ = tor_malloc_zero(sizeof(or_circuit_t));
  36. or_circ->base_.magic = OR_CIRCUIT_MAGIC;
  37. or_circ->base_.purpose = CIRCUIT_PURPOSE_OR;
  38. circ = TO_CIRCUIT(or_circ);
  39. /* A single RELAY cell was added to the appward queue. */
  40. cell_stats = tor_malloc_zero(sizeof(cell_stats_t));
  41. add_testing_cell_stats_entry(circ, CELL_RELAY, 0, 0, 0);
  42. sum_up_cell_stats_by_command(circ, cell_stats);
  43. tt_u64_op(1, OP_EQ, cell_stats->added_cells_appward[CELL_RELAY]);
  44. /* A single RELAY cell was added to the exitward queue. */
  45. add_testing_cell_stats_entry(circ, CELL_RELAY, 0, 0, 1);
  46. sum_up_cell_stats_by_command(circ, cell_stats);
  47. tt_u64_op(1, OP_EQ, cell_stats->added_cells_exitward[CELL_RELAY]);
  48. /* A single RELAY cell was removed from the appward queue where it spent
  49. * 20 msec. */
  50. add_testing_cell_stats_entry(circ, CELL_RELAY, 2, 1, 0);
  51. sum_up_cell_stats_by_command(circ, cell_stats);
  52. tt_u64_op(20, OP_EQ, cell_stats->total_time_appward[CELL_RELAY]);
  53. tt_u64_op(1, OP_EQ, cell_stats->removed_cells_appward[CELL_RELAY]);
  54. /* A single RELAY cell was removed from the exitward queue where it
  55. * spent 30 msec. */
  56. add_testing_cell_stats_entry(circ, CELL_RELAY, 3, 1, 1);
  57. sum_up_cell_stats_by_command(circ, cell_stats);
  58. tt_u64_op(30, OP_EQ, cell_stats->total_time_exitward[CELL_RELAY]);
  59. tt_u64_op(1, OP_EQ, cell_stats->removed_cells_exitward[CELL_RELAY]);
  60. done:
  61. tor_free(cell_stats);
  62. tor_free(or_circ);
  63. }
  64. static void
  65. test_cntev_append_cell_stats(void *arg)
  66. {
  67. smartlist_t *event_parts;
  68. char *cp = NULL;
  69. const char *key = "Z";
  70. uint64_t include_if_non_zero[CELL_COMMAND_MAX_ + 1],
  71. number_to_include[CELL_COMMAND_MAX_ + 1];
  72. (void)arg;
  73. event_parts = smartlist_new();
  74. memset(include_if_non_zero, 0,
  75. (CELL_COMMAND_MAX_ + 1) * sizeof(uint64_t));
  76. memset(number_to_include, 0,
  77. (CELL_COMMAND_MAX_ + 1) * sizeof(uint64_t));
  78. /* All array entries empty. */
  79. append_cell_stats_by_command(event_parts, key,
  80. include_if_non_zero,
  81. number_to_include);
  82. tt_int_op(0, OP_EQ, smartlist_len(event_parts));
  83. /* There's a RELAY cell to include, but the corresponding field in
  84. * include_if_non_zero is still zero. */
  85. number_to_include[CELL_RELAY] = 1;
  86. append_cell_stats_by_command(event_parts, key,
  87. include_if_non_zero,
  88. number_to_include);
  89. tt_int_op(0, OP_EQ, smartlist_len(event_parts));
  90. /* Now include single RELAY cell. */
  91. include_if_non_zero[CELL_RELAY] = 2;
  92. append_cell_stats_by_command(event_parts, key,
  93. include_if_non_zero,
  94. number_to_include);
  95. cp = smartlist_pop_last(event_parts);
  96. tt_str_op("Z=relay:1", OP_EQ, cp);
  97. tor_free(cp);
  98. /* Add four CREATE cells. */
  99. include_if_non_zero[CELL_CREATE] = 3;
  100. number_to_include[CELL_CREATE] = 4;
  101. append_cell_stats_by_command(event_parts, key,
  102. include_if_non_zero,
  103. number_to_include);
  104. cp = smartlist_pop_last(event_parts);
  105. tt_str_op("Z=create:4,relay:1", OP_EQ, cp);
  106. done:
  107. tor_free(cp);
  108. smartlist_free(event_parts);
  109. }
  110. static void
  111. test_cntev_format_cell_stats(void *arg)
  112. {
  113. char *event_string = NULL;
  114. origin_circuit_t *ocirc = NULL;
  115. or_circuit_t *or_circ = NULL;
  116. cell_stats_t *cell_stats = NULL;
  117. channel_tls_t *n_chan=NULL, *p_chan=NULL;
  118. (void)arg;
  119. n_chan = tor_malloc_zero(sizeof(channel_tls_t));
  120. n_chan->base_.global_identifier = 1;
  121. ocirc = tor_malloc_zero(sizeof(origin_circuit_t));
  122. ocirc->base_.magic = ORIGIN_CIRCUIT_MAGIC;
  123. ocirc->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
  124. ocirc->global_identifier = 2;
  125. ocirc->base_.n_circ_id = 3;
  126. ocirc->base_.n_chan = &(n_chan->base_);
  127. /* Origin circuit was completely idle. */
  128. cell_stats = tor_malloc_zero(sizeof(cell_stats_t));
  129. format_cell_stats(&event_string, TO_CIRCUIT(ocirc), cell_stats);
  130. tt_str_op("ID=2 OutboundQueue=3 OutboundConn=1", OP_EQ, event_string);
  131. tor_free(event_string);
  132. /* Origin circuit had 4 RELAY cells added to its exitward queue. */
  133. cell_stats->added_cells_exitward[CELL_RELAY] = 4;
  134. format_cell_stats(&event_string, TO_CIRCUIT(ocirc), cell_stats);
  135. tt_str_op("ID=2 OutboundQueue=3 OutboundConn=1 OutboundAdded=relay:4",
  136. OP_EQ, event_string);
  137. tor_free(event_string);
  138. /* Origin circuit also had 5 CREATE2 cells added to its exitward
  139. * queue. */
  140. cell_stats->added_cells_exitward[CELL_CREATE2] = 5;
  141. format_cell_stats(&event_string, TO_CIRCUIT(ocirc), cell_stats);
  142. tt_str_op("ID=2 OutboundQueue=3 OutboundConn=1 OutboundAdded=relay:4,"
  143. "create2:5", OP_EQ, event_string);
  144. tor_free(event_string);
  145. /* Origin circuit also had 7 RELAY cells removed from its exitward queue
  146. * which together spent 6 msec in the queue. */
  147. cell_stats->total_time_exitward[CELL_RELAY] = 6;
  148. cell_stats->removed_cells_exitward[CELL_RELAY] = 7;
  149. format_cell_stats(&event_string, TO_CIRCUIT(ocirc), cell_stats);
  150. tt_str_op("ID=2 OutboundQueue=3 OutboundConn=1 OutboundAdded=relay:4,"
  151. "create2:5 OutboundRemoved=relay:7 OutboundTime=relay:6",
  152. OP_EQ, event_string);
  153. tor_free(event_string);
  154. p_chan = tor_malloc_zero(sizeof(channel_tls_t));
  155. p_chan->base_.global_identifier = 2;
  156. or_circ = tor_malloc_zero(sizeof(or_circuit_t));
  157. or_circ->base_.magic = OR_CIRCUIT_MAGIC;
  158. or_circ->base_.purpose = CIRCUIT_PURPOSE_OR;
  159. or_circ->p_circ_id = 8;
  160. or_circ->p_chan = &(p_chan->base_);
  161. or_circ->base_.n_circ_id = 9;
  162. or_circ->base_.n_chan = &(n_chan->base_);
  163. tor_free(cell_stats);
  164. /* OR circuit was idle. */
  165. cell_stats = tor_malloc_zero(sizeof(cell_stats_t));
  166. format_cell_stats(&event_string, TO_CIRCUIT(or_circ), cell_stats);
  167. tt_str_op("InboundQueue=8 InboundConn=2 OutboundQueue=9 OutboundConn=1",
  168. OP_EQ, event_string);
  169. tor_free(event_string);
  170. /* OR circuit had 3 RELAY cells added to its appward queue. */
  171. cell_stats->added_cells_appward[CELL_RELAY] = 3;
  172. format_cell_stats(&event_string, TO_CIRCUIT(or_circ), cell_stats);
  173. tt_str_op("InboundQueue=8 InboundConn=2 InboundAdded=relay:3 "
  174. "OutboundQueue=9 OutboundConn=1", OP_EQ, event_string);
  175. tor_free(event_string);
  176. /* OR circuit had 7 RELAY cells removed from its appward queue which
  177. * together spent 6 msec in the queue. */
  178. cell_stats->total_time_appward[CELL_RELAY] = 6;
  179. cell_stats->removed_cells_appward[CELL_RELAY] = 7;
  180. format_cell_stats(&event_string, TO_CIRCUIT(or_circ), cell_stats);
  181. tt_str_op("InboundQueue=8 InboundConn=2 InboundAdded=relay:3 "
  182. "InboundRemoved=relay:7 InboundTime=relay:6 "
  183. "OutboundQueue=9 OutboundConn=1", OP_EQ, event_string);
  184. done:
  185. tor_free(cell_stats);
  186. tor_free(event_string);
  187. tor_free(or_circ);
  188. tor_free(ocirc);
  189. tor_free(p_chan);
  190. tor_free(n_chan);
  191. }
  192. static void
  193. test_cntev_event_mask(void *arg)
  194. {
  195. unsigned int test_event, selected_event;
  196. (void)arg;
  197. /* Check that nothing is interesting when no events are set */
  198. control_testing_set_global_event_mask(EVENT_MASK_NONE_);
  199. /* Check that nothing is interesting between EVENT_MIN_ and EVENT_MAX_ */
  200. for (test_event = EVENT_MIN_; test_event <= EVENT_MAX_; test_event++)
  201. tt_assert(!control_event_is_interesting(test_event));
  202. /* Check that nothing is interesting outside EVENT_MIN_ to EVENT_MAX_
  203. * This will break if control_event_is_interesting() checks its arguments */
  204. for (test_event = 0; test_event < EVENT_MIN_; test_event++)
  205. tt_assert(!control_event_is_interesting(test_event));
  206. for (test_event = EVENT_MAX_ + 1;
  207. test_event < EVENT_CAPACITY_;
  208. test_event++)
  209. tt_assert(!control_event_is_interesting(test_event));
  210. /* Check that all valid events are interesting when all events are set */
  211. control_testing_set_global_event_mask(EVENT_MASK_ALL_);
  212. /* Check that everything is interesting between EVENT_MIN_ and EVENT_MAX_ */
  213. for (test_event = EVENT_MIN_; test_event <= EVENT_MAX_; test_event++)
  214. tt_assert(control_event_is_interesting(test_event));
  215. /* Check that nothing is interesting outside EVENT_MIN_ to EVENT_MAX_
  216. * This will break if control_event_is_interesting() checks its arguments */
  217. for (test_event = 0; test_event < EVENT_MIN_; test_event++)
  218. tt_assert(!control_event_is_interesting(test_event));
  219. for (test_event = EVENT_MAX_ + 1;
  220. test_event < EVENT_CAPACITY_;
  221. test_event++)
  222. tt_assert(!control_event_is_interesting(test_event));
  223. /* Check that only that event is interesting when a single event is set */
  224. for (selected_event = EVENT_MIN_;
  225. selected_event <= EVENT_MAX_;
  226. selected_event++) {
  227. control_testing_set_global_event_mask(EVENT_MASK_(selected_event));
  228. /* Check that only this event is interesting
  229. * between EVENT_MIN_ and EVENT_MAX_ */
  230. for (test_event = EVENT_MIN_; test_event <= EVENT_MAX_; test_event++) {
  231. if (test_event == selected_event) {
  232. tt_assert(control_event_is_interesting(test_event));
  233. } else {
  234. tt_assert(!control_event_is_interesting(test_event));
  235. }
  236. }
  237. /* Check that nothing is interesting outside EVENT_MIN_ to EVENT_MAX_
  238. * This will break if control_event_is_interesting checks its arguments */
  239. for (test_event = 0; test_event < EVENT_MIN_; test_event++)
  240. tt_assert(!control_event_is_interesting(test_event));
  241. for (test_event = EVENT_MAX_ + 1;
  242. test_event < EVENT_CAPACITY_;
  243. test_event++)
  244. tt_assert(!control_event_is_interesting(test_event));
  245. }
  246. /* Check that only that event is not-interesting
  247. * when a single event is un-set */
  248. for (selected_event = EVENT_MIN_;
  249. selected_event <= EVENT_MAX_;
  250. selected_event++) {
  251. control_testing_set_global_event_mask(
  252. EVENT_MASK_ALL_
  253. & ~(EVENT_MASK_(selected_event))
  254. );
  255. /* Check that only this event is not-interesting
  256. * between EVENT_MIN_ and EVENT_MAX_ */
  257. for (test_event = EVENT_MIN_; test_event <= EVENT_MAX_; test_event++) {
  258. if (test_event == selected_event) {
  259. tt_assert(!control_event_is_interesting(test_event));
  260. } else {
  261. tt_assert(control_event_is_interesting(test_event));
  262. }
  263. }
  264. /* Check that nothing is interesting outside EVENT_MIN_ to EVENT_MAX_
  265. * This will break if control_event_is_interesting checks its arguments */
  266. for (test_event = 0; test_event < EVENT_MIN_; test_event++)
  267. tt_assert(!control_event_is_interesting(test_event));
  268. for (test_event = EVENT_MAX_ + 1;
  269. test_event < EVENT_CAPACITY_;
  270. test_event++)
  271. tt_assert(!control_event_is_interesting(test_event));
  272. }
  273. done:
  274. ;
  275. }
  276. #define TEST(name, flags) \
  277. { #name, test_cntev_ ## name, flags, 0, NULL }
  278. struct testcase_t controller_event_tests[] = {
  279. TEST(sum_up_cell_stats, TT_FORK),
  280. TEST(append_cell_stats, TT_FORK),
  281. TEST(format_cell_stats, TT_FORK),
  282. TEST(event_mask, TT_FORK),
  283. END_OF_TESTCASES
  284. };