test_controller_events.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /* Copyright (c) 2013-2015, 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. help_test_bucket_note_empty(uint32_t expected_msec_since_midnight,
  14. int tokens_before, size_t tokens_removed,
  15. uint32_t msec_since_epoch)
  16. {
  17. uint32_t timestamp_var = 0;
  18. struct timeval tvnow;
  19. tvnow.tv_sec = msec_since_epoch / 1000;
  20. tvnow.tv_usec = (msec_since_epoch % 1000) * 1000;
  21. connection_buckets_note_empty_ts(&timestamp_var, tokens_before,
  22. tokens_removed, &tvnow);
  23. tt_int_op(expected_msec_since_midnight, OP_EQ, timestamp_var);
  24. done:
  25. ;
  26. }
  27. static void
  28. test_cntev_bucket_note_empty(void *arg)
  29. {
  30. (void)arg;
  31. /* Two cases with nothing to note, because bucket was empty before;
  32. * 86442200 == 1970-01-02 00:00:42.200000 */
  33. help_test_bucket_note_empty(0, 0, 0, 86442200);
  34. help_test_bucket_note_empty(0, -100, 100, 86442200);
  35. /* Nothing to note, because bucket has not been emptied. */
  36. help_test_bucket_note_empty(0, 101, 100, 86442200);
  37. /* Bucket was emptied, note 42200 msec since midnight. */
  38. help_test_bucket_note_empty(42200, 101, 101, 86442200);
  39. help_test_bucket_note_empty(42200, 101, 102, 86442200);
  40. }
  41. static void
  42. test_cntev_bucket_millis_empty(void *arg)
  43. {
  44. struct timeval tvnow;
  45. (void)arg;
  46. /* 1970-01-02 00:00:42.200000 */
  47. tvnow.tv_sec = 86400 + 42;
  48. tvnow.tv_usec = 200000;
  49. /* Bucket has not been refilled. */
  50. tt_int_op(0, OP_EQ, bucket_millis_empty(0, 42120, 0, 100, &tvnow));
  51. tt_int_op(0, OP_EQ, bucket_millis_empty(-10, 42120, -10, 100, &tvnow));
  52. /* Bucket was not empty. */
  53. tt_int_op(0, OP_EQ, bucket_millis_empty(10, 42120, 20, 100, &tvnow));
  54. /* Bucket has been emptied 80 msec ago and has just been refilled. */
  55. tt_int_op(80, OP_EQ, bucket_millis_empty(-20, 42120, -10, 100, &tvnow));
  56. tt_int_op(80, OP_EQ, bucket_millis_empty(-10, 42120, 0, 100, &tvnow));
  57. tt_int_op(80, OP_EQ, bucket_millis_empty(0, 42120, 10, 100, &tvnow));
  58. /* Bucket has been emptied 180 msec ago, last refill was 100 msec ago
  59. * which was insufficient to make it positive, so cap msec at 100. */
  60. tt_int_op(100, OP_EQ, bucket_millis_empty(0, 42020, 1, 100, &tvnow));
  61. /* 1970-01-02 00:00:00:050000 */
  62. tvnow.tv_sec = 86400;
  63. tvnow.tv_usec = 50000;
  64. /* Last emptied 30 msec before midnight, tvnow is 50 msec after
  65. * midnight, that's 80 msec in total. */
  66. tt_int_op(80, OP_EQ, bucket_millis_empty(0, 86400000 - 30, 1, 100, &tvnow));
  67. done:
  68. ;
  69. }
  70. static void
  71. add_testing_cell_stats_entry(circuit_t *circ, uint8_t command,
  72. unsigned int waiting_time,
  73. unsigned int removed, unsigned int exitward)
  74. {
  75. testing_cell_stats_entry_t *ent = tor_malloc_zero(
  76. sizeof(testing_cell_stats_entry_t));
  77. ent->command = command;
  78. ent->waiting_time = waiting_time;
  79. ent->removed = removed;
  80. ent->exitward = exitward;
  81. if (!circ->testing_cell_stats)
  82. circ->testing_cell_stats = smartlist_new();
  83. smartlist_add(circ->testing_cell_stats, ent);
  84. }
  85. static void
  86. test_cntev_sum_up_cell_stats(void *arg)
  87. {
  88. or_circuit_t *or_circ;
  89. circuit_t *circ;
  90. cell_stats_t *cell_stats = NULL;
  91. (void)arg;
  92. /* This circuit is fake. */
  93. or_circ = tor_malloc_zero(sizeof(or_circuit_t));
  94. or_circ->base_.magic = OR_CIRCUIT_MAGIC;
  95. or_circ->base_.purpose = CIRCUIT_PURPOSE_OR;
  96. circ = TO_CIRCUIT(or_circ);
  97. /* A single RELAY cell was added to the appward queue. */
  98. cell_stats = tor_malloc_zero(sizeof(cell_stats_t));
  99. add_testing_cell_stats_entry(circ, CELL_RELAY, 0, 0, 0);
  100. sum_up_cell_stats_by_command(circ, cell_stats);
  101. tt_u64_op(1, OP_EQ, cell_stats->added_cells_appward[CELL_RELAY]);
  102. /* A single RELAY cell was added to the exitward queue. */
  103. add_testing_cell_stats_entry(circ, CELL_RELAY, 0, 0, 1);
  104. sum_up_cell_stats_by_command(circ, cell_stats);
  105. tt_u64_op(1, OP_EQ, cell_stats->added_cells_exitward[CELL_RELAY]);
  106. /* A single RELAY cell was removed from the appward queue where it spent
  107. * 20 msec. */
  108. add_testing_cell_stats_entry(circ, CELL_RELAY, 2, 1, 0);
  109. sum_up_cell_stats_by_command(circ, cell_stats);
  110. tt_u64_op(20, OP_EQ, cell_stats->total_time_appward[CELL_RELAY]);
  111. tt_u64_op(1, OP_EQ, cell_stats->removed_cells_appward[CELL_RELAY]);
  112. /* A single RELAY cell was removed from the exitward queue where it
  113. * spent 30 msec. */
  114. add_testing_cell_stats_entry(circ, CELL_RELAY, 3, 1, 1);
  115. sum_up_cell_stats_by_command(circ, cell_stats);
  116. tt_u64_op(30, OP_EQ, cell_stats->total_time_exitward[CELL_RELAY]);
  117. tt_u64_op(1, OP_EQ, cell_stats->removed_cells_exitward[CELL_RELAY]);
  118. done:
  119. tor_free(cell_stats);
  120. tor_free(or_circ);
  121. }
  122. static void
  123. test_cntev_append_cell_stats(void *arg)
  124. {
  125. smartlist_t *event_parts;
  126. char *cp = NULL;
  127. const char *key = "Z";
  128. uint64_t include_if_non_zero[CELL_COMMAND_MAX_ + 1],
  129. number_to_include[CELL_COMMAND_MAX_ + 1];
  130. (void)arg;
  131. event_parts = smartlist_new();
  132. memset(include_if_non_zero, 0,
  133. (CELL_COMMAND_MAX_ + 1) * sizeof(uint64_t));
  134. memset(number_to_include, 0,
  135. (CELL_COMMAND_MAX_ + 1) * sizeof(uint64_t));
  136. /* All array entries empty. */
  137. append_cell_stats_by_command(event_parts, key,
  138. include_if_non_zero,
  139. number_to_include);
  140. tt_int_op(0, OP_EQ, smartlist_len(event_parts));
  141. /* There's a RELAY cell to include, but the corresponding field in
  142. * include_if_non_zero is still zero. */
  143. number_to_include[CELL_RELAY] = 1;
  144. append_cell_stats_by_command(event_parts, key,
  145. include_if_non_zero,
  146. number_to_include);
  147. tt_int_op(0, OP_EQ, smartlist_len(event_parts));
  148. /* Now include single RELAY cell. */
  149. include_if_non_zero[CELL_RELAY] = 2;
  150. append_cell_stats_by_command(event_parts, key,
  151. include_if_non_zero,
  152. number_to_include);
  153. cp = smartlist_pop_last(event_parts);
  154. tt_str_op("Z=relay:1", OP_EQ, cp);
  155. tor_free(cp);
  156. /* Add four CREATE cells. */
  157. include_if_non_zero[CELL_CREATE] = 3;
  158. number_to_include[CELL_CREATE] = 4;
  159. append_cell_stats_by_command(event_parts, key,
  160. include_if_non_zero,
  161. number_to_include);
  162. cp = smartlist_pop_last(event_parts);
  163. tt_str_op("Z=create:4,relay:1", OP_EQ, cp);
  164. done:
  165. tor_free(cp);
  166. smartlist_free(event_parts);
  167. }
  168. static void
  169. test_cntev_format_cell_stats(void *arg)
  170. {
  171. char *event_string = NULL;
  172. origin_circuit_t *ocirc = NULL;
  173. or_circuit_t *or_circ = NULL;
  174. cell_stats_t *cell_stats = NULL;
  175. channel_tls_t *n_chan=NULL, *p_chan=NULL;
  176. (void)arg;
  177. n_chan = tor_malloc_zero(sizeof(channel_tls_t));
  178. n_chan->base_.global_identifier = 1;
  179. ocirc = tor_malloc_zero(sizeof(origin_circuit_t));
  180. ocirc->base_.magic = ORIGIN_CIRCUIT_MAGIC;
  181. ocirc->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
  182. ocirc->global_identifier = 2;
  183. ocirc->base_.n_circ_id = 3;
  184. ocirc->base_.n_chan = &(n_chan->base_);
  185. /* Origin circuit was completely idle. */
  186. cell_stats = tor_malloc_zero(sizeof(cell_stats_t));
  187. format_cell_stats(&event_string, TO_CIRCUIT(ocirc), cell_stats);
  188. tt_str_op("ID=2 OutboundQueue=3 OutboundConn=1", OP_EQ, event_string);
  189. tor_free(event_string);
  190. /* Origin circuit had 4 RELAY cells added to its exitward queue. */
  191. cell_stats->added_cells_exitward[CELL_RELAY] = 4;
  192. format_cell_stats(&event_string, TO_CIRCUIT(ocirc), cell_stats);
  193. tt_str_op("ID=2 OutboundQueue=3 OutboundConn=1 OutboundAdded=relay:4",
  194. OP_EQ, event_string);
  195. tor_free(event_string);
  196. /* Origin circuit also had 5 CREATE2 cells added to its exitward
  197. * queue. */
  198. cell_stats->added_cells_exitward[CELL_CREATE2] = 5;
  199. format_cell_stats(&event_string, TO_CIRCUIT(ocirc), cell_stats);
  200. tt_str_op("ID=2 OutboundQueue=3 OutboundConn=1 OutboundAdded=relay:4,"
  201. "create2:5", OP_EQ, event_string);
  202. tor_free(event_string);
  203. /* Origin circuit also had 7 RELAY cells removed from its exitward queue
  204. * which together spent 6 msec in the queue. */
  205. cell_stats->total_time_exitward[CELL_RELAY] = 6;
  206. cell_stats->removed_cells_exitward[CELL_RELAY] = 7;
  207. format_cell_stats(&event_string, TO_CIRCUIT(ocirc), cell_stats);
  208. tt_str_op("ID=2 OutboundQueue=3 OutboundConn=1 OutboundAdded=relay:4,"
  209. "create2:5 OutboundRemoved=relay:7 OutboundTime=relay:6",
  210. OP_EQ, event_string);
  211. tor_free(event_string);
  212. p_chan = tor_malloc_zero(sizeof(channel_tls_t));
  213. p_chan->base_.global_identifier = 2;
  214. or_circ = tor_malloc_zero(sizeof(or_circuit_t));
  215. or_circ->base_.magic = OR_CIRCUIT_MAGIC;
  216. or_circ->base_.purpose = CIRCUIT_PURPOSE_OR;
  217. or_circ->p_circ_id = 8;
  218. or_circ->p_chan = &(p_chan->base_);
  219. or_circ->base_.n_circ_id = 9;
  220. or_circ->base_.n_chan = &(n_chan->base_);
  221. tor_free(cell_stats);
  222. /* OR circuit was idle. */
  223. cell_stats = tor_malloc_zero(sizeof(cell_stats_t));
  224. format_cell_stats(&event_string, TO_CIRCUIT(or_circ), cell_stats);
  225. tt_str_op("InboundQueue=8 InboundConn=2 OutboundQueue=9 OutboundConn=1",
  226. OP_EQ, event_string);
  227. tor_free(event_string);
  228. /* OR circuit had 3 RELAY cells added to its appward queue. */
  229. cell_stats->added_cells_appward[CELL_RELAY] = 3;
  230. format_cell_stats(&event_string, TO_CIRCUIT(or_circ), cell_stats);
  231. tt_str_op("InboundQueue=8 InboundConn=2 InboundAdded=relay:3 "
  232. "OutboundQueue=9 OutboundConn=1", OP_EQ, event_string);
  233. tor_free(event_string);
  234. /* OR circuit had 7 RELAY cells removed from its appward queue which
  235. * together spent 6 msec in the queue. */
  236. cell_stats->total_time_appward[CELL_RELAY] = 6;
  237. cell_stats->removed_cells_appward[CELL_RELAY] = 7;
  238. format_cell_stats(&event_string, TO_CIRCUIT(or_circ), cell_stats);
  239. tt_str_op("InboundQueue=8 InboundConn=2 InboundAdded=relay:3 "
  240. "InboundRemoved=relay:7 InboundTime=relay:6 "
  241. "OutboundQueue=9 OutboundConn=1", OP_EQ, event_string);
  242. done:
  243. tor_free(cell_stats);
  244. tor_free(event_string);
  245. tor_free(or_circ);
  246. tor_free(ocirc);
  247. tor_free(p_chan);
  248. tor_free(n_chan);
  249. }
  250. static void
  251. test_cntev_event_mask(void *arg)
  252. {
  253. unsigned int test_event, selected_event;
  254. (void)arg;
  255. /* Check that nothing is interesting when no events are set */
  256. control_testing_set_global_event_mask(EVENT_MASK_NONE_);
  257. /* Check that nothing is interesting between EVENT_MIN_ and EVENT_MAX_ */
  258. for (test_event = EVENT_MIN_; test_event <= EVENT_MAX_; test_event++)
  259. tt_assert(!control_event_is_interesting(test_event));
  260. /* Check that nothing is interesting outside EVENT_MIN_ to EVENT_MAX_
  261. * This will break if control_event_is_interesting() checks its arguments */
  262. for (test_event = 0; test_event < EVENT_MIN_; test_event++)
  263. tt_assert(!control_event_is_interesting(test_event));
  264. for (test_event = EVENT_MAX_ + 1;
  265. test_event < EVENT_CAPACITY_;
  266. test_event++)
  267. tt_assert(!control_event_is_interesting(test_event));
  268. /* Check that all valid events are interesting when all events are set */
  269. control_testing_set_global_event_mask(EVENT_MASK_ALL_);
  270. /* Check that everything is interesting between EVENT_MIN_ and EVENT_MAX_ */
  271. for (test_event = EVENT_MIN_; test_event <= EVENT_MAX_; test_event++)
  272. tt_assert(control_event_is_interesting(test_event));
  273. /* Check that nothing is interesting outside EVENT_MIN_ to EVENT_MAX_
  274. * This will break if control_event_is_interesting() checks its arguments */
  275. for (test_event = 0; test_event < EVENT_MIN_; test_event++)
  276. tt_assert(!control_event_is_interesting(test_event));
  277. for (test_event = EVENT_MAX_ + 1;
  278. test_event < EVENT_CAPACITY_;
  279. test_event++)
  280. tt_assert(!control_event_is_interesting(test_event));
  281. /* Check that only that event is interesting when a single event is set */
  282. for (selected_event = EVENT_MIN_;
  283. selected_event <= EVENT_MAX_;
  284. selected_event++) {
  285. control_testing_set_global_event_mask(EVENT_MASK_(selected_event));
  286. /* Check that only this event is interesting
  287. * between EVENT_MIN_ and EVENT_MAX_ */
  288. for (test_event = EVENT_MIN_; test_event <= EVENT_MAX_; test_event++) {
  289. if (test_event == selected_event) {
  290. tt_assert(control_event_is_interesting(test_event));
  291. } else {
  292. tt_assert(!control_event_is_interesting(test_event));
  293. }
  294. }
  295. /* Check that nothing is interesting outside EVENT_MIN_ to EVENT_MAX_
  296. * This will break if control_event_is_interesting checks its arguments */
  297. for (test_event = 0; test_event < EVENT_MIN_; test_event++)
  298. tt_assert(!control_event_is_interesting(test_event));
  299. for (test_event = EVENT_MAX_ + 1;
  300. test_event < EVENT_CAPACITY_;
  301. test_event++)
  302. tt_assert(!control_event_is_interesting(test_event));
  303. }
  304. /* Check that only that event is not-interesting
  305. * when a single event is un-set */
  306. for (selected_event = EVENT_MIN_;
  307. selected_event <= EVENT_MAX_;
  308. selected_event++) {
  309. control_testing_set_global_event_mask(
  310. EVENT_MASK_ALL_
  311. & ~(EVENT_MASK_(selected_event))
  312. );
  313. /* Check that only this event is not-interesting
  314. * between EVENT_MIN_ and EVENT_MAX_ */
  315. for (test_event = EVENT_MIN_; test_event <= EVENT_MAX_; test_event++) {
  316. if (test_event == selected_event) {
  317. tt_assert(!control_event_is_interesting(test_event));
  318. } else {
  319. tt_assert(control_event_is_interesting(test_event));
  320. }
  321. }
  322. /* Check that nothing is interesting outside EVENT_MIN_ to EVENT_MAX_
  323. * This will break if control_event_is_interesting checks its arguments */
  324. for (test_event = 0; test_event < EVENT_MIN_; test_event++)
  325. tt_assert(!control_event_is_interesting(test_event));
  326. for (test_event = EVENT_MAX_ + 1;
  327. test_event < EVENT_CAPACITY_;
  328. test_event++)
  329. tt_assert(!control_event_is_interesting(test_event));
  330. }
  331. done:
  332. ;
  333. }
  334. #define TEST(name, flags) \
  335. { #name, test_cntev_ ## name, flags, 0, NULL }
  336. struct testcase_t controller_event_tests[] = {
  337. TEST(bucket_note_empty, TT_FORK),
  338. TEST(bucket_millis_empty, TT_FORK),
  339. TEST(sum_up_cell_stats, TT_FORK),
  340. TEST(append_cell_stats, TT_FORK),
  341. TEST(format_cell_stats, TT_FORK),
  342. TEST(event_mask, TT_FORK),
  343. END_OF_TESTCASES
  344. };