Merge branch 'fix-changelogs' into 'main'
[tor.git] / src / test / test_status.c
blob4ceb81f3a5270da244456cc86291a319dc7499b1
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_assigned[ONION_HANDSHAKE_TYPE_TAP] = 1;
337 onion_handshakes_requested[ONION_HANDSHAKE_TYPE_TAP] = 2;
338 onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_NTOR] = 3;
339 onion_handshakes_requested[ONION_HANDSHAKE_TYPE_NTOR] = 4;
340 onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_NTOR_V3] = 5;
341 onion_handshakes_requested[ONION_HANDSHAKE_TYPE_NTOR_V3] = 6;
343 expected = 0;
344 setup_capture_of_logs(LOG_INFO);
345 actual = log_heartbeat(0);
346 tt_int_op(actual, OP_EQ, expected);
348 expect_log_msg("Heartbeat: It seems like we are "
349 "not in the cached consensus.\n");
350 expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
351 "with 0 circuits open. "
352 "I've sent 0 kB and received 0 kB. "
353 "I've received 0 connections on IPv4 and 0 on IPv6. "
354 "I've made 0 connections with IPv4 and 0 with IPv6.\n");
355 expect_log_msg("Average packaged cell fullness: 100.000%. "
356 "TLS write overhead: 0%\n");
357 expect_log_msg("Circuit handshake stats since last time: 1/2 TAP, "
358 "3/4 NTor, 5/6 NTor (v3).\n");
359 expect_log_msg("Since startup we initiated 0 and received 0 v1 "
360 "connections; initiated 0 and received 0 v2 connections; "
361 "initiated 0 and received 0 v3 connections; "
362 "initiated 0 and received 0 v4 connections; "
363 "initiated 0 and received 0 v5 connections.\n");
364 expect_log_msg("Heartbeat: DoS mitigation since startup: 0 circuits killed "
365 "with too many cells, [DoSCircuitCreationEnabled disabled], "
366 "[DoSConnectionEnabled disabled], "
367 "[DoSRefuseSingleHopClientRendezvous disabled], "
368 "[DoSStreamCreationEnabled disabled], "
369 "0 INTRODUCE2 rejected.\n");
370 tt_int_op(mock_saved_log_n_entries(), OP_EQ, 6);
372 done:
373 teardown_capture_of_logs();
374 UNMOCK(tls_get_write_overhead_ratio);
375 UNMOCK(we_are_hibernating);
376 UNMOCK(public_server_mode);
377 UNMOCK(router_get_my_routerinfo);
378 UNMOCK(node_get_by_id);
379 UNMOCK(server_mode);
380 tor_free(mock_routerinfo);
383 static double
384 status_hb_not_in_consensus_tls_get_write_overhead_ratio(void)
386 return 1.0;
389 static int
390 status_hb_not_in_consensus_we_are_hibernating(void)
392 return 0;
395 static int
396 status_hb_not_in_consensus_public_server_mode(const or_options_t *options)
398 (void)options;
400 return 1;
403 static const routerinfo_t *
404 status_hb_not_in_consensus_get_my_routerinfo(void)
406 mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
408 return mock_routerinfo;
411 static const node_t *
412 status_hb_not_in_consensus_node_get_by_id(const char *identity_digest)
414 (void)identity_digest;
416 return NULL;
419 static int
420 status_hb_not_in_consensus_server_mode(const or_options_t *options)
422 (void)options;
424 return 0;
428 * Tests that log_heartbeat() correctly logs heartbeat information
429 * normally.
432 static double status_hb_simple_tls_get_write_overhead_ratio(void);
433 static int status_hb_simple_we_are_hibernating(void);
434 static int status_hb_simple_public_server_mode(const or_options_t *options);
435 static long status_hb_simple_get_uptime(void);
436 static uint64_t status_hb_simple_get_bytes_read(void);
437 static uint64_t status_hb_simple_get_bytes_written(void);
438 static int status_hb_simple_server_mode(const or_options_t *options);
440 static void
441 test_status_hb_simple(void *arg)
443 int expected, actual;
444 (void)arg;
446 MOCK(tls_get_write_overhead_ratio,
447 status_hb_simple_tls_get_write_overhead_ratio);
448 MOCK(we_are_hibernating,
449 status_hb_simple_we_are_hibernating);
450 MOCK(public_server_mode,
451 status_hb_simple_public_server_mode);
452 MOCK(get_uptime,
453 status_hb_simple_get_uptime);
454 MOCK(get_bytes_read,
455 status_hb_simple_get_bytes_read);
456 MOCK(get_bytes_written,
457 status_hb_simple_get_bytes_written);
458 MOCK(server_mode,
459 status_hb_simple_server_mode);
461 log_global_min_severity_ = LOG_DEBUG;
463 setup_capture_of_logs(LOG_INFO);
464 expected = 0;
465 actual = log_heartbeat(0);
467 tt_int_op(actual, OP_EQ, expected);
469 expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
470 "with 0 circuits open. "
471 "I've sent 0 kB and received 0 kB. "
472 "I've received 0 connections on IPv4 and 0 on IPv6. "
473 "I've made 0 connections with IPv4 and 0 with IPv6. "
474 "We are currently hibernating.\n");
476 done:
477 teardown_capture_of_logs();
478 UNMOCK(tls_get_write_overhead_ratio);
479 UNMOCK(we_are_hibernating);
480 UNMOCK(public_server_mode);
481 UNMOCK(get_uptime);
482 UNMOCK(get_bytes_read);
483 UNMOCK(get_bytes_written);
484 UNMOCK(server_mode);
487 static double
488 status_hb_simple_tls_get_write_overhead_ratio(void)
490 return 1.0;
493 static int
494 status_hb_simple_we_are_hibernating(void)
496 return 1;
499 static int
500 status_hb_simple_public_server_mode(const or_options_t *options)
502 (void)options;
504 return 0;
507 static long
508 status_hb_simple_get_uptime(void)
510 return 0;
513 static uint64_t
514 status_hb_simple_get_bytes_read(void)
516 return 0;
519 static uint64_t
520 status_hb_simple_get_bytes_written(void)
522 return 0;
525 static int
526 status_hb_simple_server_mode(const or_options_t *options)
528 (void)options;
530 return 0;
534 * Tests that log_heartbeat() correctly logs heartbeat information
535 * and accounting information when configured.
538 static double status_hb_calls_log_accounting_tls_get_write_overhead_ratio(
539 void);
540 static int status_hb_calls_log_accounting_we_are_hibernating(void);
541 static int status_hb_calls_log_accounting_public_server_mode(
542 const or_options_t *options);
543 static long status_hb_calls_log_accounting_get_uptime(void);
544 static uint64_t status_hb_calls_log_accounting_get_bytes_read(void);
545 static uint64_t status_hb_calls_log_accounting_get_bytes_written(void);
546 static int status_hb_calls_log_accounting_server_mode(
547 const or_options_t *options);
548 static or_state_t * status_hb_calls_log_accounting_get_or_state(void);
549 static int status_hb_calls_log_accounting_accounting_is_enabled(
550 const or_options_t *options);
551 static time_t status_hb_calls_log_accounting_accounting_get_end_time(void);
553 static or_state_t * status_hb_calls_log_accounting_mock_state = NULL;
554 static or_options_t * status_hb_calls_log_accounting_mock_options = NULL;
556 static void
557 test_status_hb_calls_log_accounting(void *arg)
559 int expected, actual;
560 (void)arg;
562 MOCK(tls_get_write_overhead_ratio,
563 status_hb_calls_log_accounting_tls_get_write_overhead_ratio);
564 MOCK(we_are_hibernating,
565 status_hb_calls_log_accounting_we_are_hibernating);
566 MOCK(public_server_mode,
567 status_hb_calls_log_accounting_public_server_mode);
568 MOCK(get_uptime,
569 status_hb_calls_log_accounting_get_uptime);
570 MOCK(get_bytes_read,
571 status_hb_calls_log_accounting_get_bytes_read);
572 MOCK(get_bytes_written,
573 status_hb_calls_log_accounting_get_bytes_written);
574 MOCK(server_mode,
575 status_hb_calls_log_accounting_server_mode);
576 MOCK(get_or_state,
577 status_hb_calls_log_accounting_get_or_state);
578 MOCK(accounting_is_enabled,
579 status_hb_calls_log_accounting_accounting_is_enabled);
580 MOCK(accounting_get_end_time,
581 status_hb_calls_log_accounting_accounting_get_end_time);
583 log_global_min_severity_ = LOG_DEBUG;
585 setup_capture_of_logs(LOG_NOTICE);
586 expected = 0;
587 actual = log_heartbeat(0);
589 tt_int_op(actual, OP_EQ, expected);
591 expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
592 "with 0 circuits open. "
593 "I've sent 0 kB and received 0 kB. "
594 "I've received 0 connections on IPv4 and 0 on IPv6. "
595 "I've made 0 connections with IPv4 and 0 with IPv6.\n");
597 expect_log_msg_containing("Heartbeat: Accounting enabled. Sent: 0 kB, "
598 "Received: 0 kB, Used: 0 kB / 0 kB, Rule: max. "
599 "The current accounting interval ends on ");
600 tt_int_op(mock_saved_log_n_entries(), OP_EQ, 2);
602 done:
603 teardown_capture_of_logs();
604 UNMOCK(tls_get_write_overhead_ratio);
605 UNMOCK(we_are_hibernating);
606 UNMOCK(public_server_mode);
607 UNMOCK(get_uptime);
608 UNMOCK(get_bytes_read);
609 UNMOCK(get_bytes_written);
610 UNMOCK(server_mode);
611 UNMOCK(accounting_is_enabled);
612 UNMOCK(accounting_get_end_time);
613 tor_free_(status_hb_calls_log_accounting_mock_state);
614 tor_free_(status_hb_calls_log_accounting_mock_options);
617 static double
618 status_hb_calls_log_accounting_tls_get_write_overhead_ratio(void)
620 return 1.0;
623 static int
624 status_hb_calls_log_accounting_we_are_hibernating(void)
626 return 0;
629 static int
630 status_hb_calls_log_accounting_public_server_mode(const or_options_t *options)
632 (void)options;
634 return 0;
637 static long
638 status_hb_calls_log_accounting_get_uptime(void)
640 return 0;
643 static uint64_t
644 status_hb_calls_log_accounting_get_bytes_read(void)
646 return 0;
649 static uint64_t
650 status_hb_calls_log_accounting_get_bytes_written(void)
652 return 0;
655 static int
656 status_hb_calls_log_accounting_server_mode(const or_options_t *options)
658 (void)options;
660 return 1;
663 static int
664 status_hb_calls_log_accounting_accounting_is_enabled(
665 const or_options_t *options)
667 (void)options;
669 return 1;
672 static time_t
673 status_hb_calls_log_accounting_accounting_get_end_time(void)
675 return 60;
678 static or_state_t *
679 status_hb_calls_log_accounting_get_or_state(void)
681 status_hb_calls_log_accounting_mock_state =
682 tor_malloc_zero(sizeof(or_state_t));
683 status_hb_calls_log_accounting_mock_state
684 ->AccountingBytesReadInInterval = 0;
685 status_hb_calls_log_accounting_mock_state
686 ->AccountingBytesWrittenInInterval = 0;
688 return status_hb_calls_log_accounting_mock_state;
692 * Tests that log_heartbeat() correctly logs packaged cell
693 * fullness information.
696 static double status_hb_packaged_cell_fullness_tls_get_write_overhead_ratio(
697 void);
698 static int status_hb_packaged_cell_fullness_we_are_hibernating(void);
699 static int status_hb_packaged_cell_fullness_public_server_mode(
700 const or_options_t *options);
701 static long status_hb_packaged_cell_fullness_get_uptime(void);
702 static uint64_t status_hb_packaged_cell_fullness_get_bytes_read(void);
703 static uint64_t status_hb_packaged_cell_fullness_get_bytes_written(void);
704 static int status_hb_packaged_cell_fullness_server_mode(
705 const or_options_t *options);
706 static int status_hb_packaged_cell_fullness_accounting_is_enabled(
707 const or_options_t *options);
709 static void
710 test_status_hb_packaged_cell_fullness(void *arg)
712 int expected, actual;
713 (void)arg;
715 MOCK(tls_get_write_overhead_ratio,
716 status_hb_packaged_cell_fullness_tls_get_write_overhead_ratio);
717 MOCK(we_are_hibernating,
718 status_hb_packaged_cell_fullness_we_are_hibernating);
719 MOCK(public_server_mode,
720 status_hb_packaged_cell_fullness_public_server_mode);
721 MOCK(get_uptime,
722 status_hb_packaged_cell_fullness_get_uptime);
723 MOCK(get_bytes_read,
724 status_hb_packaged_cell_fullness_get_bytes_read);
725 MOCK(get_bytes_written,
726 status_hb_packaged_cell_fullness_get_bytes_written);
727 MOCK(server_mode,
728 status_hb_packaged_cell_fullness_server_mode);
729 MOCK(accounting_is_enabled,
730 status_hb_packaged_cell_fullness_accounting_is_enabled);
731 log_global_min_severity_ = LOG_DEBUG;
733 stats_n_data_bytes_packaged = RELAY_PAYLOAD_SIZE;
734 stats_n_data_cells_packaged = 2;
735 expected = 0;
736 setup_capture_of_logs(LOG_INFO);
737 actual = log_heartbeat(0);
739 tt_int_op(actual, OP_EQ, expected);
740 expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
741 "with 0 circuits open. "
742 "I've sent 0 kB and received 0 kB. "
743 "I've received 0 connections on IPv4 and 0 on IPv6. "
744 "I've made 0 connections with IPv4 and 0 with IPv6.\n");
745 expect_log_msg("Average packaged cell fullness: 50.000%. "
746 "TLS write overhead: 0%\n");
748 done:
749 teardown_capture_of_logs();
750 stats_n_data_bytes_packaged = 0;
751 stats_n_data_cells_packaged = 0;
752 UNMOCK(tls_get_write_overhead_ratio);
753 UNMOCK(we_are_hibernating);
754 UNMOCK(public_server_mode);
755 UNMOCK(get_uptime);
756 UNMOCK(get_bytes_read);
757 UNMOCK(get_bytes_written);
758 UNMOCK(server_mode);
759 UNMOCK(accounting_is_enabled);
762 static double
763 status_hb_packaged_cell_fullness_tls_get_write_overhead_ratio(void)
765 return 1.0;
768 static int
769 status_hb_packaged_cell_fullness_we_are_hibernating(void)
771 return 0;
774 static int
775 status_hb_packaged_cell_fullness_public_server_mode(
776 const or_options_t *options)
778 (void)options;
780 return 0;
783 static long
784 status_hb_packaged_cell_fullness_get_uptime(void)
786 return 0;
789 static uint64_t
790 status_hb_packaged_cell_fullness_get_bytes_read(void)
792 return 0;
795 static uint64_t
796 status_hb_packaged_cell_fullness_get_bytes_written(void)
798 return 0;
801 static int
802 status_hb_packaged_cell_fullness_server_mode(const or_options_t *options)
804 (void)options;
806 return 0;
809 static int
810 status_hb_packaged_cell_fullness_accounting_is_enabled(
811 const or_options_t *options)
813 (void)options;
815 return 0;
819 * Tests that log_heartbeat() correctly logs the TLS write overhead information
820 * when the TLS write overhead ratio exceeds 1.
823 static double status_hb_tls_write_overhead_tls_get_write_overhead_ratio(void);
824 static int status_hb_tls_write_overhead_we_are_hibernating(void);
825 static int status_hb_tls_write_overhead_public_server_mode(
826 const or_options_t *options);
827 static long status_hb_tls_write_overhead_get_uptime(void);
828 static uint64_t status_hb_tls_write_overhead_get_bytes_read(void);
829 static uint64_t status_hb_tls_write_overhead_get_bytes_written(void);
830 static int status_hb_tls_write_overhead_server_mode(
831 const or_options_t *options);
832 static int status_hb_tls_write_overhead_accounting_is_enabled(
833 const or_options_t *options);
835 static void
836 test_status_hb_tls_write_overhead(void *arg)
838 int expected, actual;
839 (void)arg;
841 MOCK(tls_get_write_overhead_ratio,
842 status_hb_tls_write_overhead_tls_get_write_overhead_ratio);
843 MOCK(we_are_hibernating,
844 status_hb_tls_write_overhead_we_are_hibernating);
845 MOCK(public_server_mode,
846 status_hb_tls_write_overhead_public_server_mode);
847 MOCK(get_uptime,
848 status_hb_tls_write_overhead_get_uptime);
849 MOCK(get_bytes_read,
850 status_hb_tls_write_overhead_get_bytes_read);
851 MOCK(get_bytes_written,
852 status_hb_tls_write_overhead_get_bytes_written);
853 MOCK(server_mode,
854 status_hb_tls_write_overhead_server_mode);
855 MOCK(accounting_is_enabled,
856 status_hb_tls_write_overhead_accounting_is_enabled);
857 stats_n_data_cells_packaged = 0;
858 log_global_min_severity_ = LOG_DEBUG;
860 expected = 0;
861 setup_capture_of_logs(LOG_NOTICE);
862 actual = log_heartbeat(0);
864 tt_int_op(actual, OP_EQ, expected);
865 expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
866 "with 0 circuits open. "
867 "I've sent 0 kB and received 0 kB. "
868 "I've received 0 connections on IPv4 and 0 on IPv6. "
869 "I've made 0 connections with IPv4 and 0 with IPv6.\n");
870 expect_log_msg("Average packaged cell fullness: 100.000%. "
871 "TLS write overhead: 100%\n");
873 done:
874 teardown_capture_of_logs();
875 UNMOCK(tls_get_write_overhead_ratio);
876 UNMOCK(we_are_hibernating);
877 UNMOCK(public_server_mode);
878 UNMOCK(get_uptime);
879 UNMOCK(get_bytes_read);
880 UNMOCK(get_bytes_written);
881 UNMOCK(server_mode);
882 UNMOCK(accounting_is_enabled);
885 static double
886 status_hb_tls_write_overhead_tls_get_write_overhead_ratio(void)
888 return 2.0;
891 static int
892 status_hb_tls_write_overhead_we_are_hibernating(void)
894 return 0;
897 static int
898 status_hb_tls_write_overhead_public_server_mode(const or_options_t *options)
900 (void)options;
902 return 0;
905 static long
906 status_hb_tls_write_overhead_get_uptime(void)
908 return 0;
911 static uint64_t
912 status_hb_tls_write_overhead_get_bytes_read(void)
914 return 0;
917 static uint64_t
918 status_hb_tls_write_overhead_get_bytes_written(void)
920 return 0;
923 static int
924 status_hb_tls_write_overhead_server_mode(const or_options_t *options)
926 (void)options;
928 return 0;
931 static int
932 status_hb_tls_write_overhead_accounting_is_enabled(const or_options_t *options)
934 (void)options;
936 return 0;
939 struct testcase_t status_tests[] = {
940 { "count_circuits", test_status_count_circuits, TT_FORK, NULL, NULL },
941 { "secs_to_uptime", test_status_secs_to_uptime, TT_FORK, NULL, NULL },
942 { "bytes_to_usage", test_status_bytes_to_usage, TT_FORK, NULL, NULL },
943 { "hb_fails", test_status_hb_fails, TT_FORK, NULL, NULL },
944 { "hb_simple", test_status_hb_simple, TT_FORK, NULL, NULL },
945 { "hb_not_in_consensus", test_status_hb_not_in_consensus,
946 TT_FORK, NULL, NULL },
947 { "hb_calls_log_accounting", test_status_hb_calls_log_accounting,
948 TT_FORK, NULL, NULL },
949 { "hb_packaged_cell_fullness", test_status_hb_packaged_cell_fullness,
950 TT_FORK, NULL, NULL },
951 { "hb_tls_write_overhead", test_status_hb_tls_write_overhead,
952 TT_FORK, NULL, NULL },
953 END_OF_TESTCASES