dns: Wake up a dormant tor with a DNSPort request
[tor.git] / src / test / test_status.c
blob1d371645aecb0385f9d60430d6446117ab29a584
1 /* Copyright (c) 2014-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #define STATUS_PRIVATE
5 #define HIBERNATE_PRIVATE
6 #define LOG_PRIVATE
7 #define REPHIST_PRIVATE
9 #include "orconfig.h"
11 #include <float.h>
12 #include <math.h>
14 #include "core/or/or.h"
15 #include "lib/log/log.h"
16 #include "tor_queue.h"
17 #include "core/or/status.h"
18 #include "core/or/circuitlist.h"
19 #include "app/config/config.h"
20 #include "feature/hibernate/hibernate.h"
21 #include "feature/stats/rephist.h"
22 #include "core/or/relay.h"
23 #include "feature/relay/router.h"
24 #include "feature/relay/routermode.h"
25 #include "core/mainloop/mainloop.h"
26 #include "feature/nodelist/nodelist.h"
27 #include "app/config/statefile.h"
28 #include "lib/tls/tortls.h"
29 #include "test/log_test_helpers.h"
31 #include "core/or/origin_circuit_st.h"
32 #include "app/config/or_state_st.h"
33 #include "feature/nodelist/routerinfo_st.h"
35 #include "test/test.h"
38 * Test that count_circuits() is correctly counting the number of
39 * global circuits.
42 static smartlist_t * mock_global_circuitlist = NULL;
44 static smartlist_t * status_count_circuits_circuit_get_global_list(void);
46 static void
47 test_status_count_circuits(void *arg)
49 /* Choose origin_circuit_t wlog. */
50 origin_circuit_t *mock_circuit1, *mock_circuit2;
51 int expected_circuits = 2, actual_circuits;
53 (void)arg;
55 mock_circuit1 = tor_malloc_zero(sizeof(origin_circuit_t));
56 mock_circuit2 = tor_malloc_zero(sizeof(origin_circuit_t));
57 mock_global_circuitlist = smartlist_new();
58 smartlist_add(mock_global_circuitlist, TO_CIRCUIT(mock_circuit1));
59 smartlist_add(mock_global_circuitlist, TO_CIRCUIT(mock_circuit2));
61 MOCK(circuit_get_global_list,
62 status_count_circuits_circuit_get_global_list);
64 actual_circuits = count_circuits();
66 tt_assert(expected_circuits == actual_circuits);
68 done:
69 tor_free(mock_circuit1);
70 tor_free(mock_circuit2);
71 smartlist_free(mock_global_circuitlist);
72 mock_global_circuitlist = NULL;
73 UNMOCK(circuit_get_global_list);
76 static smartlist_t *
77 status_count_circuits_circuit_get_global_list(void)
79 return mock_global_circuitlist;
83 * Test that secs_to_uptime() is converting the number of seconds that
84 * Tor is up for into the appropriate string form containing hours and minutes.
87 static void
88 test_status_secs_to_uptime(void *arg)
90 const char *expected;
91 char *actual;
92 (void)arg;
94 expected = "0:00 hours";
95 actual = secs_to_uptime(0);
96 tt_str_op(actual, OP_EQ, expected);
97 tor_free(actual);
99 expected = "0:00 hours";
100 actual = secs_to_uptime(1);
101 tt_str_op(actual, OP_EQ, expected);
102 tor_free(actual);
104 expected = "0:01 hours";
105 actual = secs_to_uptime(60);
106 tt_str_op(actual, OP_EQ, expected);
107 tor_free(actual);
109 expected = "0:59 hours";
110 actual = secs_to_uptime(60 * 59);
111 tt_str_op(actual, OP_EQ, expected);
112 tor_free(actual);
114 expected = "1:00 hours";
115 actual = secs_to_uptime(60 * 60);
116 tt_str_op(actual, OP_EQ, expected);
117 tor_free(actual);
119 expected = "23:59 hours";
120 actual = secs_to_uptime(60 * 60 * 23 + 60 * 59);
121 tt_str_op(actual, OP_EQ, expected);
122 tor_free(actual);
124 expected = "1 day 0:00 hours";
125 actual = secs_to_uptime(60 * 60 * 23 + 60 * 60);
126 tt_str_op(actual, OP_EQ, expected);
127 tor_free(actual);
129 expected = "1 day 0:00 hours";
130 actual = secs_to_uptime(86400 + 1);
131 tt_str_op(actual, OP_EQ, expected);
132 tor_free(actual);
134 expected = "1 day 0:01 hours";
135 actual = secs_to_uptime(86400 + 60);
136 tt_str_op(actual, OP_EQ, expected);
137 tor_free(actual);
139 expected = "10 days 0:00 hours";
140 actual = secs_to_uptime(86400 * 10);
141 tt_str_op(actual, OP_EQ, expected);
142 tor_free(actual);
144 expected = "10 days 0:00 hours";
145 actual = secs_to_uptime(864000 + 1);
146 tt_str_op(actual, OP_EQ, expected);
147 tor_free(actual);
149 expected = "10 days 0:01 hours";
150 actual = secs_to_uptime(864000 + 60);
151 tt_str_op(actual, OP_EQ, expected);
152 tor_free(actual);
154 done:
155 if (actual != NULL)
156 tor_free(actual);
160 * Test that bytes_to_usage() is correctly converting the number of bytes that
161 * Tor has read/written into the appropriate string form containing kilobytes,
162 * megabytes, or gigabytes.
165 static void
166 test_status_bytes_to_usage(void *arg)
168 const char *expected;
169 char *actual;
170 (void)arg;
172 expected = "0 kB";
173 actual = bytes_to_usage(0);
174 tt_str_op(actual, OP_EQ, expected);
175 tor_free(actual);
177 expected = "0 kB";
178 actual = bytes_to_usage(1);
179 tt_str_op(actual, OP_EQ, expected);
180 tor_free(actual);
182 expected = "1 kB";
183 actual = bytes_to_usage(1024);
184 tt_str_op(actual, OP_EQ, expected);
185 tor_free(actual);
187 expected = "1023 kB";
188 actual = bytes_to_usage((1 << 20) - 1);
189 tt_str_op(actual, OP_EQ, expected);
190 tor_free(actual);
192 expected = "1.00 MB";
193 actual = bytes_to_usage((1 << 20));
194 tt_str_op(actual, OP_EQ, expected);
195 tor_free(actual);
197 expected = "1.00 MB";
198 actual = bytes_to_usage((1 << 20) + 5242);
199 tt_str_op(actual, OP_EQ, expected);
200 tor_free(actual);
202 expected = "1.01 MB";
203 actual = bytes_to_usage((1 << 20) + 5243);
204 tt_str_op(actual, OP_EQ, expected);
205 tor_free(actual);
207 expected = "1024.00 MB";
208 actual = bytes_to_usage((1 << 30) - 1);
209 tt_str_op(actual, OP_EQ, expected);
210 tor_free(actual);
212 expected = "1.00 GB";
213 actual = bytes_to_usage((1 << 30));
214 tt_str_op(actual, OP_EQ, expected);
215 tor_free(actual);
217 expected = "1.00 GB";
218 actual = bytes_to_usage((1 << 30) + 5368709);
219 tt_str_op(actual, OP_EQ, expected);
220 tor_free(actual);
222 expected = "1.01 GB";
223 actual = bytes_to_usage((1 << 30) + 5368710);
224 tt_str_op(actual, OP_EQ, expected);
225 tor_free(actual);
227 expected = "10.00 GB";
228 actual = bytes_to_usage((UINT64_C(1) << 30) * 10L);
229 tt_str_op(actual, OP_EQ, expected);
230 tor_free(actual);
232 done:
233 if (actual != NULL)
234 tor_free(actual);
238 * Tests that log_heartbeat() fails when in the public server mode,
239 * not hibernating, and we couldn't get the current routerinfo.
242 static double status_hb_fails_tls_get_write_overhead_ratio(void);
243 static int status_hb_fails_we_are_hibernating(void);
244 static int status_hb_fails_public_server_mode(const or_options_t *options);
245 static const routerinfo_t * status_hb_fails_router_get_my_routerinfo(void);
247 static void
248 test_status_hb_fails(void *arg)
250 int expected, actual;
251 (void)arg;
253 MOCK(tls_get_write_overhead_ratio,
254 status_hb_fails_tls_get_write_overhead_ratio);
255 MOCK(we_are_hibernating,
256 status_hb_fails_we_are_hibernating);
257 MOCK(public_server_mode,
258 status_hb_fails_public_server_mode);
259 MOCK(router_get_my_routerinfo,
260 status_hb_fails_router_get_my_routerinfo);
262 expected = -1;
263 actual = log_heartbeat(0);
265 tt_int_op(actual, OP_EQ, expected);
267 done:
268 UNMOCK(tls_get_write_overhead_ratio);
269 UNMOCK(we_are_hibernating);
270 UNMOCK(public_server_mode);
271 UNMOCK(router_get_my_routerinfo);
274 static double
275 status_hb_fails_tls_get_write_overhead_ratio(void)
277 return 2.0;
280 static int
281 status_hb_fails_we_are_hibernating(void)
283 return 0;
286 static int
287 status_hb_fails_public_server_mode(const or_options_t *options)
289 (void)options;
291 return 1;
294 static const routerinfo_t *
295 status_hb_fails_router_get_my_routerinfo(void)
297 return NULL;
301 * Tests that log_heartbeat() logs appropriately if we are not in the cached
302 * consensus.
305 static double status_hb_not_in_consensus_tls_get_write_overhead_ratio(void);
306 static int status_hb_not_in_consensus_we_are_hibernating(void);
307 static int status_hb_not_in_consensus_public_server_mode(
308 const or_options_t *options);
309 static const routerinfo_t *status_hb_not_in_consensus_get_my_routerinfo(void);
310 static const node_t * status_hb_not_in_consensus_node_get_by_id(
311 const char *identity_digest);
312 static int status_hb_not_in_consensus_server_mode(const or_options_t *options);
314 static routerinfo_t *mock_routerinfo;
316 static void
317 test_status_hb_not_in_consensus(void *arg)
319 int expected, actual;
320 (void)arg;
322 MOCK(tls_get_write_overhead_ratio,
323 status_hb_not_in_consensus_tls_get_write_overhead_ratio);
324 MOCK(we_are_hibernating,
325 status_hb_not_in_consensus_we_are_hibernating);
326 MOCK(public_server_mode,
327 status_hb_not_in_consensus_public_server_mode);
328 MOCK(router_get_my_routerinfo,
329 status_hb_not_in_consensus_get_my_routerinfo);
330 MOCK(node_get_by_id,
331 status_hb_not_in_consensus_node_get_by_id);
332 MOCK(server_mode,
333 status_hb_not_in_consensus_server_mode);
335 log_global_min_severity_ = LOG_DEBUG;
336 onion_handshakes_requested[ONION_HANDSHAKE_TYPE_TAP] = 1;
337 onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_TAP] = 1;
338 onion_handshakes_requested[ONION_HANDSHAKE_TYPE_NTOR] = 1;
339 onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_NTOR] = 1;
341 expected = 0;
342 setup_capture_of_logs(LOG_INFO);
343 actual = log_heartbeat(0);
344 tt_int_op(actual, OP_EQ, expected);
346 expect_log_msg("Heartbeat: It seems like we are "
347 "not in the cached consensus.\n");
348 expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
349 "with 0 circuits open. "
350 "I've sent 0 kB and received 0 kB. "
351 "I've received 0 connections on IPv4 and 0 on IPv6. "
352 "I've made 0 connections with IPv4 and 0 with IPv6.\n");
353 expect_log_msg("Average packaged cell fullness: 100.000%. "
354 "TLS write overhead: 0%\n");
355 expect_log_msg("Circuit handshake stats since last time: 1/1 TAP, "
356 "1/1 NTor.\n");
357 expect_log_msg("Since startup we initiated 0 and received 0 v1 "
358 "connections; initiated 0 and received 0 v2 connections; "
359 "initiated 0 and received 0 v3 connections; "
360 "initiated 0 and received 0 v4 connections; "
361 "initiated 0 and received 0 v5 connections.\n");
362 expect_log_msg("Heartbeat: DoS mitigation since startup: 0 circuits killed "
363 "with too many cells, [DoSCircuitCreationEnabled disabled], "
364 "[DoSConnectionEnabled disabled], "
365 "[DoSRefuseSingleHopClientRendezvous disabled], "
366 "0 INTRODUCE2 rejected.\n");
367 tt_int_op(mock_saved_log_n_entries(), OP_EQ, 6);
369 done:
370 teardown_capture_of_logs();
371 UNMOCK(tls_get_write_overhead_ratio);
372 UNMOCK(we_are_hibernating);
373 UNMOCK(public_server_mode);
374 UNMOCK(router_get_my_routerinfo);
375 UNMOCK(node_get_by_id);
376 UNMOCK(server_mode);
377 tor_free(mock_routerinfo);
380 static double
381 status_hb_not_in_consensus_tls_get_write_overhead_ratio(void)
383 return 1.0;
386 static int
387 status_hb_not_in_consensus_we_are_hibernating(void)
389 return 0;
392 static int
393 status_hb_not_in_consensus_public_server_mode(const or_options_t *options)
395 (void)options;
397 return 1;
400 static const routerinfo_t *
401 status_hb_not_in_consensus_get_my_routerinfo(void)
403 mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
405 return mock_routerinfo;
408 static const node_t *
409 status_hb_not_in_consensus_node_get_by_id(const char *identity_digest)
411 (void)identity_digest;
413 return NULL;
416 static int
417 status_hb_not_in_consensus_server_mode(const or_options_t *options)
419 (void)options;
421 return 0;
425 * Tests that log_heartbeat() correctly logs heartbeat information
426 * normally.
429 static double status_hb_simple_tls_get_write_overhead_ratio(void);
430 static int status_hb_simple_we_are_hibernating(void);
431 static int status_hb_simple_public_server_mode(const or_options_t *options);
432 static long status_hb_simple_get_uptime(void);
433 static uint64_t status_hb_simple_get_bytes_read(void);
434 static uint64_t status_hb_simple_get_bytes_written(void);
435 static int status_hb_simple_server_mode(const or_options_t *options);
437 static void
438 test_status_hb_simple(void *arg)
440 int expected, actual;
441 (void)arg;
443 MOCK(tls_get_write_overhead_ratio,
444 status_hb_simple_tls_get_write_overhead_ratio);
445 MOCK(we_are_hibernating,
446 status_hb_simple_we_are_hibernating);
447 MOCK(public_server_mode,
448 status_hb_simple_public_server_mode);
449 MOCK(get_uptime,
450 status_hb_simple_get_uptime);
451 MOCK(get_bytes_read,
452 status_hb_simple_get_bytes_read);
453 MOCK(get_bytes_written,
454 status_hb_simple_get_bytes_written);
455 MOCK(server_mode,
456 status_hb_simple_server_mode);
458 log_global_min_severity_ = LOG_DEBUG;
460 setup_capture_of_logs(LOG_INFO);
461 expected = 0;
462 actual = log_heartbeat(0);
464 tt_int_op(actual, OP_EQ, expected);
466 expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
467 "with 0 circuits open. "
468 "I've sent 0 kB and received 0 kB. "
469 "I've received 0 connections on IPv4 and 0 on IPv6. "
470 "I've made 0 connections with IPv4 and 0 with IPv6. "
471 "We are currently hibernating.\n");
473 done:
474 teardown_capture_of_logs();
475 UNMOCK(tls_get_write_overhead_ratio);
476 UNMOCK(we_are_hibernating);
477 UNMOCK(public_server_mode);
478 UNMOCK(get_uptime);
479 UNMOCK(get_bytes_read);
480 UNMOCK(get_bytes_written);
481 UNMOCK(server_mode);
484 static double
485 status_hb_simple_tls_get_write_overhead_ratio(void)
487 return 1.0;
490 static int
491 status_hb_simple_we_are_hibernating(void)
493 return 1;
496 static int
497 status_hb_simple_public_server_mode(const or_options_t *options)
499 (void)options;
501 return 0;
504 static long
505 status_hb_simple_get_uptime(void)
507 return 0;
510 static uint64_t
511 status_hb_simple_get_bytes_read(void)
513 return 0;
516 static uint64_t
517 status_hb_simple_get_bytes_written(void)
519 return 0;
522 static int
523 status_hb_simple_server_mode(const or_options_t *options)
525 (void)options;
527 return 0;
531 * Tests that log_heartbeat() correctly logs heartbeat information
532 * and accounting information when configured.
535 static double status_hb_calls_log_accounting_tls_get_write_overhead_ratio(
536 void);
537 static int status_hb_calls_log_accounting_we_are_hibernating(void);
538 static int status_hb_calls_log_accounting_public_server_mode(
539 const or_options_t *options);
540 static long status_hb_calls_log_accounting_get_uptime(void);
541 static uint64_t status_hb_calls_log_accounting_get_bytes_read(void);
542 static uint64_t status_hb_calls_log_accounting_get_bytes_written(void);
543 static int status_hb_calls_log_accounting_server_mode(
544 const or_options_t *options);
545 static or_state_t * status_hb_calls_log_accounting_get_or_state(void);
546 static int status_hb_calls_log_accounting_accounting_is_enabled(
547 const or_options_t *options);
548 static time_t status_hb_calls_log_accounting_accounting_get_end_time(void);
550 static or_state_t * status_hb_calls_log_accounting_mock_state = NULL;
551 static or_options_t * status_hb_calls_log_accounting_mock_options = NULL;
553 static void
554 test_status_hb_calls_log_accounting(void *arg)
556 int expected, actual;
557 (void)arg;
559 MOCK(tls_get_write_overhead_ratio,
560 status_hb_calls_log_accounting_tls_get_write_overhead_ratio);
561 MOCK(we_are_hibernating,
562 status_hb_calls_log_accounting_we_are_hibernating);
563 MOCK(public_server_mode,
564 status_hb_calls_log_accounting_public_server_mode);
565 MOCK(get_uptime,
566 status_hb_calls_log_accounting_get_uptime);
567 MOCK(get_bytes_read,
568 status_hb_calls_log_accounting_get_bytes_read);
569 MOCK(get_bytes_written,
570 status_hb_calls_log_accounting_get_bytes_written);
571 MOCK(server_mode,
572 status_hb_calls_log_accounting_server_mode);
573 MOCK(get_or_state,
574 status_hb_calls_log_accounting_get_or_state);
575 MOCK(accounting_is_enabled,
576 status_hb_calls_log_accounting_accounting_is_enabled);
577 MOCK(accounting_get_end_time,
578 status_hb_calls_log_accounting_accounting_get_end_time);
580 log_global_min_severity_ = LOG_DEBUG;
582 setup_capture_of_logs(LOG_NOTICE);
583 expected = 0;
584 actual = log_heartbeat(0);
586 tt_int_op(actual, OP_EQ, expected);
588 expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
589 "with 0 circuits open. "
590 "I've sent 0 kB and received 0 kB. "
591 "I've received 0 connections on IPv4 and 0 on IPv6. "
592 "I've made 0 connections with IPv4 and 0 with IPv6.\n");
594 expect_log_msg_containing("Heartbeat: Accounting enabled. Sent: 0 kB, "
595 "Received: 0 kB, Used: 0 kB / 0 kB, Rule: max. "
596 "The current accounting interval ends on ");
597 tt_int_op(mock_saved_log_n_entries(), OP_EQ, 2);
599 done:
600 teardown_capture_of_logs();
601 UNMOCK(tls_get_write_overhead_ratio);
602 UNMOCK(we_are_hibernating);
603 UNMOCK(public_server_mode);
604 UNMOCK(get_uptime);
605 UNMOCK(get_bytes_read);
606 UNMOCK(get_bytes_written);
607 UNMOCK(server_mode);
608 UNMOCK(accounting_is_enabled);
609 UNMOCK(accounting_get_end_time);
610 tor_free_(status_hb_calls_log_accounting_mock_state);
611 tor_free_(status_hb_calls_log_accounting_mock_options);
614 static double
615 status_hb_calls_log_accounting_tls_get_write_overhead_ratio(void)
617 return 1.0;
620 static int
621 status_hb_calls_log_accounting_we_are_hibernating(void)
623 return 0;
626 static int
627 status_hb_calls_log_accounting_public_server_mode(const or_options_t *options)
629 (void)options;
631 return 0;
634 static long
635 status_hb_calls_log_accounting_get_uptime(void)
637 return 0;
640 static uint64_t
641 status_hb_calls_log_accounting_get_bytes_read(void)
643 return 0;
646 static uint64_t
647 status_hb_calls_log_accounting_get_bytes_written(void)
649 return 0;
652 static int
653 status_hb_calls_log_accounting_server_mode(const or_options_t *options)
655 (void)options;
657 return 1;
660 static int
661 status_hb_calls_log_accounting_accounting_is_enabled(
662 const or_options_t *options)
664 (void)options;
666 return 1;
669 static time_t
670 status_hb_calls_log_accounting_accounting_get_end_time(void)
672 return 60;
675 static or_state_t *
676 status_hb_calls_log_accounting_get_or_state(void)
678 status_hb_calls_log_accounting_mock_state =
679 tor_malloc_zero(sizeof(or_state_t));
680 status_hb_calls_log_accounting_mock_state
681 ->AccountingBytesReadInInterval = 0;
682 status_hb_calls_log_accounting_mock_state
683 ->AccountingBytesWrittenInInterval = 0;
685 return status_hb_calls_log_accounting_mock_state;
689 * Tests that log_heartbeat() correctly logs packaged cell
690 * fullness information.
693 static double status_hb_packaged_cell_fullness_tls_get_write_overhead_ratio(
694 void);
695 static int status_hb_packaged_cell_fullness_we_are_hibernating(void);
696 static int status_hb_packaged_cell_fullness_public_server_mode(
697 const or_options_t *options);
698 static long status_hb_packaged_cell_fullness_get_uptime(void);
699 static uint64_t status_hb_packaged_cell_fullness_get_bytes_read(void);
700 static uint64_t status_hb_packaged_cell_fullness_get_bytes_written(void);
701 static int status_hb_packaged_cell_fullness_server_mode(
702 const or_options_t *options);
703 static int status_hb_packaged_cell_fullness_accounting_is_enabled(
704 const or_options_t *options);
706 static void
707 test_status_hb_packaged_cell_fullness(void *arg)
709 int expected, actual;
710 (void)arg;
712 MOCK(tls_get_write_overhead_ratio,
713 status_hb_packaged_cell_fullness_tls_get_write_overhead_ratio);
714 MOCK(we_are_hibernating,
715 status_hb_packaged_cell_fullness_we_are_hibernating);
716 MOCK(public_server_mode,
717 status_hb_packaged_cell_fullness_public_server_mode);
718 MOCK(get_uptime,
719 status_hb_packaged_cell_fullness_get_uptime);
720 MOCK(get_bytes_read,
721 status_hb_packaged_cell_fullness_get_bytes_read);
722 MOCK(get_bytes_written,
723 status_hb_packaged_cell_fullness_get_bytes_written);
724 MOCK(server_mode,
725 status_hb_packaged_cell_fullness_server_mode);
726 MOCK(accounting_is_enabled,
727 status_hb_packaged_cell_fullness_accounting_is_enabled);
728 log_global_min_severity_ = LOG_DEBUG;
730 stats_n_data_bytes_packaged = RELAY_PAYLOAD_SIZE;
731 stats_n_data_cells_packaged = 2;
732 expected = 0;
733 setup_capture_of_logs(LOG_INFO);
734 actual = log_heartbeat(0);
736 tt_int_op(actual, OP_EQ, expected);
737 expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
738 "with 0 circuits open. "
739 "I've sent 0 kB and received 0 kB. "
740 "I've received 0 connections on IPv4 and 0 on IPv6. "
741 "I've made 0 connections with IPv4 and 0 with IPv6.\n");
742 expect_log_msg("Average packaged cell fullness: 50.000%. "
743 "TLS write overhead: 0%\n");
745 done:
746 teardown_capture_of_logs();
747 stats_n_data_bytes_packaged = 0;
748 stats_n_data_cells_packaged = 0;
749 UNMOCK(tls_get_write_overhead_ratio);
750 UNMOCK(we_are_hibernating);
751 UNMOCK(public_server_mode);
752 UNMOCK(get_uptime);
753 UNMOCK(get_bytes_read);
754 UNMOCK(get_bytes_written);
755 UNMOCK(server_mode);
756 UNMOCK(accounting_is_enabled);
759 static double
760 status_hb_packaged_cell_fullness_tls_get_write_overhead_ratio(void)
762 return 1.0;
765 static int
766 status_hb_packaged_cell_fullness_we_are_hibernating(void)
768 return 0;
771 static int
772 status_hb_packaged_cell_fullness_public_server_mode(
773 const or_options_t *options)
775 (void)options;
777 return 0;
780 static long
781 status_hb_packaged_cell_fullness_get_uptime(void)
783 return 0;
786 static uint64_t
787 status_hb_packaged_cell_fullness_get_bytes_read(void)
789 return 0;
792 static uint64_t
793 status_hb_packaged_cell_fullness_get_bytes_written(void)
795 return 0;
798 static int
799 status_hb_packaged_cell_fullness_server_mode(const or_options_t *options)
801 (void)options;
803 return 0;
806 static int
807 status_hb_packaged_cell_fullness_accounting_is_enabled(
808 const or_options_t *options)
810 (void)options;
812 return 0;
816 * Tests that log_heartbeat() correctly logs the TLS write overhead information
817 * when the TLS write overhead ratio exceeds 1.
820 static double status_hb_tls_write_overhead_tls_get_write_overhead_ratio(void);
821 static int status_hb_tls_write_overhead_we_are_hibernating(void);
822 static int status_hb_tls_write_overhead_public_server_mode(
823 const or_options_t *options);
824 static long status_hb_tls_write_overhead_get_uptime(void);
825 static uint64_t status_hb_tls_write_overhead_get_bytes_read(void);
826 static uint64_t status_hb_tls_write_overhead_get_bytes_written(void);
827 static int status_hb_tls_write_overhead_server_mode(
828 const or_options_t *options);
829 static int status_hb_tls_write_overhead_accounting_is_enabled(
830 const or_options_t *options);
832 static void
833 test_status_hb_tls_write_overhead(void *arg)
835 int expected, actual;
836 (void)arg;
838 MOCK(tls_get_write_overhead_ratio,
839 status_hb_tls_write_overhead_tls_get_write_overhead_ratio);
840 MOCK(we_are_hibernating,
841 status_hb_tls_write_overhead_we_are_hibernating);
842 MOCK(public_server_mode,
843 status_hb_tls_write_overhead_public_server_mode);
844 MOCK(get_uptime,
845 status_hb_tls_write_overhead_get_uptime);
846 MOCK(get_bytes_read,
847 status_hb_tls_write_overhead_get_bytes_read);
848 MOCK(get_bytes_written,
849 status_hb_tls_write_overhead_get_bytes_written);
850 MOCK(server_mode,
851 status_hb_tls_write_overhead_server_mode);
852 MOCK(accounting_is_enabled,
853 status_hb_tls_write_overhead_accounting_is_enabled);
854 stats_n_data_cells_packaged = 0;
855 log_global_min_severity_ = LOG_DEBUG;
857 expected = 0;
858 setup_capture_of_logs(LOG_NOTICE);
859 actual = log_heartbeat(0);
861 tt_int_op(actual, OP_EQ, expected);
862 expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
863 "with 0 circuits open. "
864 "I've sent 0 kB and received 0 kB. "
865 "I've received 0 connections on IPv4 and 0 on IPv6. "
866 "I've made 0 connections with IPv4 and 0 with IPv6.\n");
867 expect_log_msg("Average packaged cell fullness: 100.000%. "
868 "TLS write overhead: 100%\n");
870 done:
871 teardown_capture_of_logs();
872 UNMOCK(tls_get_write_overhead_ratio);
873 UNMOCK(we_are_hibernating);
874 UNMOCK(public_server_mode);
875 UNMOCK(get_uptime);
876 UNMOCK(get_bytes_read);
877 UNMOCK(get_bytes_written);
878 UNMOCK(server_mode);
879 UNMOCK(accounting_is_enabled);
882 static double
883 status_hb_tls_write_overhead_tls_get_write_overhead_ratio(void)
885 return 2.0;
888 static int
889 status_hb_tls_write_overhead_we_are_hibernating(void)
891 return 0;
894 static int
895 status_hb_tls_write_overhead_public_server_mode(const or_options_t *options)
897 (void)options;
899 return 0;
902 static long
903 status_hb_tls_write_overhead_get_uptime(void)
905 return 0;
908 static uint64_t
909 status_hb_tls_write_overhead_get_bytes_read(void)
911 return 0;
914 static uint64_t
915 status_hb_tls_write_overhead_get_bytes_written(void)
917 return 0;
920 static int
921 status_hb_tls_write_overhead_server_mode(const or_options_t *options)
923 (void)options;
925 return 0;
928 static int
929 status_hb_tls_write_overhead_accounting_is_enabled(const or_options_t *options)
931 (void)options;
933 return 0;
936 struct testcase_t status_tests[] = {
937 { "count_circuits", test_status_count_circuits, TT_FORK, NULL, NULL },
938 { "secs_to_uptime", test_status_secs_to_uptime, TT_FORK, NULL, NULL },
939 { "bytes_to_usage", test_status_bytes_to_usage, TT_FORK, NULL, NULL },
940 { "hb_fails", test_status_hb_fails, TT_FORK, NULL, NULL },
941 { "hb_simple", test_status_hb_simple, TT_FORK, NULL, NULL },
942 { "hb_not_in_consensus", test_status_hb_not_in_consensus,
943 TT_FORK, NULL, NULL },
944 { "hb_calls_log_accounting", test_status_hb_calls_log_accounting,
945 TT_FORK, NULL, NULL },
946 { "hb_packaged_cell_fullness", test_status_hb_packaged_cell_fullness,
947 TT_FORK, NULL, NULL },
948 { "hb_tls_write_overhead", test_status_hb_tls_write_overhead,
949 TT_FORK, NULL, NULL },
950 END_OF_TESTCASES