1 /* Copyright (c) 2014-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
5 #define HIBERNATE_PRIVATE
7 #define REPHIST_PRIVATE
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"
30 #include "core/or/origin_circuit_st.h"
31 #include "app/config/or_state_st.h"
32 #include "feature/nodelist/routerinfo_st.h"
34 #include "test/test.h"
36 #define NS_MODULE status
38 #define NS_SUBMODULE count_circuits
41 * Test that count_circuits() is correctly counting the number of
45 static smartlist_t
* mock_global_circuitlist
= NULL
;
47 NS_DECL(smartlist_t
*, circuit_get_global_list
, (void));
50 NS(test_main
)(void *arg
)
52 /* Choose origin_circuit_t wlog. */
53 origin_circuit_t
*mock_circuit1
, *mock_circuit2
;
54 int expected_circuits
= 2, actual_circuits
;
58 mock_circuit1
= tor_malloc_zero(sizeof(origin_circuit_t
));
59 mock_circuit2
= tor_malloc_zero(sizeof(origin_circuit_t
));
60 mock_global_circuitlist
= smartlist_new();
61 smartlist_add(mock_global_circuitlist
, TO_CIRCUIT(mock_circuit1
));
62 smartlist_add(mock_global_circuitlist
, TO_CIRCUIT(mock_circuit2
));
64 NS_MOCK(circuit_get_global_list
);
66 actual_circuits
= count_circuits();
68 tt_assert(expected_circuits
== actual_circuits
);
71 tor_free(mock_circuit1
);
72 tor_free(mock_circuit2
);
73 smartlist_free(mock_global_circuitlist
);
74 mock_global_circuitlist
= NULL
;
75 NS_UNMOCK(circuit_get_global_list
);
79 NS(circuit_get_global_list
)(void)
81 return mock_global_circuitlist
;
85 #define NS_SUBMODULE secs_to_uptime
88 * Test that secs_to_uptime() is converting the number of seconds that
89 * Tor is up for into the appropriate string form containing hours and minutes.
93 NS(test_main
)(void *arg
)
99 expected
= "0:00 hours";
100 actual
= secs_to_uptime(0);
101 tt_str_op(actual
, OP_EQ
, expected
);
104 expected
= "0:00 hours";
105 actual
= secs_to_uptime(1);
106 tt_str_op(actual
, OP_EQ
, expected
);
109 expected
= "0:01 hours";
110 actual
= secs_to_uptime(60);
111 tt_str_op(actual
, OP_EQ
, expected
);
114 expected
= "0:59 hours";
115 actual
= secs_to_uptime(60 * 59);
116 tt_str_op(actual
, OP_EQ
, expected
);
119 expected
= "1:00 hours";
120 actual
= secs_to_uptime(60 * 60);
121 tt_str_op(actual
, OP_EQ
, expected
);
124 expected
= "23:59 hours";
125 actual
= secs_to_uptime(60 * 60 * 23 + 60 * 59);
126 tt_str_op(actual
, OP_EQ
, expected
);
129 expected
= "1 day 0:00 hours";
130 actual
= secs_to_uptime(60 * 60 * 23 + 60 * 60);
131 tt_str_op(actual
, OP_EQ
, expected
);
134 expected
= "1 day 0:00 hours";
135 actual
= secs_to_uptime(86400 + 1);
136 tt_str_op(actual
, OP_EQ
, expected
);
139 expected
= "1 day 0:01 hours";
140 actual
= secs_to_uptime(86400 + 60);
141 tt_str_op(actual
, OP_EQ
, expected
);
144 expected
= "10 days 0:00 hours";
145 actual
= secs_to_uptime(86400 * 10);
146 tt_str_op(actual
, OP_EQ
, expected
);
149 expected
= "10 days 0:00 hours";
150 actual
= secs_to_uptime(864000 + 1);
151 tt_str_op(actual
, OP_EQ
, expected
);
154 expected
= "10 days 0:01 hours";
155 actual
= secs_to_uptime(864000 + 60);
156 tt_str_op(actual
, OP_EQ
, expected
);
165 #define NS_SUBMODULE bytes_to_usage
168 * Test that bytes_to_usage() is correctly converting the number of bytes that
169 * Tor has read/written into the appropriate string form containing kilobytes,
170 * megabytes, or gigabytes.
174 NS(test_main
)(void *arg
)
176 const char *expected
;
181 actual
= bytes_to_usage(0);
182 tt_str_op(actual
, OP_EQ
, expected
);
186 actual
= bytes_to_usage(1);
187 tt_str_op(actual
, OP_EQ
, expected
);
191 actual
= bytes_to_usage(1024);
192 tt_str_op(actual
, OP_EQ
, expected
);
195 expected
= "1023 kB";
196 actual
= bytes_to_usage((1 << 20) - 1);
197 tt_str_op(actual
, OP_EQ
, expected
);
200 expected
= "1.00 MB";
201 actual
= bytes_to_usage((1 << 20));
202 tt_str_op(actual
, OP_EQ
, expected
);
205 expected
= "1.00 MB";
206 actual
= bytes_to_usage((1 << 20) + 5242);
207 tt_str_op(actual
, OP_EQ
, expected
);
210 expected
= "1.01 MB";
211 actual
= bytes_to_usage((1 << 20) + 5243);
212 tt_str_op(actual
, OP_EQ
, expected
);
215 expected
= "1024.00 MB";
216 actual
= bytes_to_usage((1 << 30) - 1);
217 tt_str_op(actual
, OP_EQ
, expected
);
220 expected
= "1.00 GB";
221 actual
= bytes_to_usage((1 << 30));
222 tt_str_op(actual
, OP_EQ
, expected
);
225 expected
= "1.00 GB";
226 actual
= bytes_to_usage((1 << 30) + 5368709);
227 tt_str_op(actual
, OP_EQ
, expected
);
230 expected
= "1.01 GB";
231 actual
= bytes_to_usage((1 << 30) + 5368710);
232 tt_str_op(actual
, OP_EQ
, expected
);
235 expected
= "10.00 GB";
236 actual
= bytes_to_usage((UINT64_C(1) << 30) * 10L);
237 tt_str_op(actual
, OP_EQ
, expected
);
246 #define NS_SUBMODULE ASPECT(log_heartbeat, fails)
249 * Tests that log_heartbeat() fails when in the public server mode,
250 * not hibernating, and we couldn't get the current routerinfo.
253 NS_DECL(double, tls_get_write_overhead_ratio
, (void));
254 NS_DECL(int, we_are_hibernating
, (void));
255 NS_DECL(int, public_server_mode
, (const or_options_t
*options
));
256 NS_DECL(const routerinfo_t
*, router_get_my_routerinfo
, (void));
259 NS(test_main
)(void *arg
)
261 int expected
, actual
;
264 NS_MOCK(tls_get_write_overhead_ratio
);
265 NS_MOCK(we_are_hibernating
);
266 NS_MOCK(public_server_mode
);
267 NS_MOCK(router_get_my_routerinfo
);
270 actual
= log_heartbeat(0);
272 tt_int_op(actual
, OP_EQ
, expected
);
275 NS_UNMOCK(tls_get_write_overhead_ratio
);
276 NS_UNMOCK(we_are_hibernating
);
277 NS_UNMOCK(public_server_mode
);
278 NS_UNMOCK(router_get_my_routerinfo
);
282 NS(tls_get_write_overhead_ratio
)(void)
288 NS(we_are_hibernating
)(void)
294 NS(public_server_mode
)(const or_options_t
*options
)
301 static const routerinfo_t
*
302 NS(router_get_my_routerinfo
)(void)
308 #define NS_SUBMODULE ASPECT(log_heartbeat, not_in_consensus)
311 * Tests that log_heartbeat() logs appropriately if we are not in the cached
315 NS_DECL(double, tls_get_write_overhead_ratio
, (void));
316 NS_DECL(int, we_are_hibernating
, (void));
317 NS_DECL(int, public_server_mode
, (const or_options_t
*options
));
318 NS_DECL(const routerinfo_t
*, router_get_my_routerinfo
, (void));
319 NS_DECL(const node_t
*, node_get_by_id
, (const char *identity_digest
));
320 NS_DECL(void, logv
, (int severity
, log_domain_mask_t domain
,
321 const char *funcname
, const char *suffix
, const char *format
, va_list ap
));
322 NS_DECL(int, server_mode
, (const or_options_t
*options
));
324 static routerinfo_t
*mock_routerinfo
;
327 NS(test_main
)(void *arg
)
329 int expected
, actual
;
332 NS_MOCK(tls_get_write_overhead_ratio
);
333 NS_MOCK(we_are_hibernating
);
334 NS_MOCK(public_server_mode
);
335 NS_MOCK(router_get_my_routerinfo
);
336 NS_MOCK(node_get_by_id
);
338 NS_MOCK(server_mode
);
340 log_global_min_severity_
= LOG_DEBUG
;
341 onion_handshakes_requested
[ONION_HANDSHAKE_TYPE_TAP
] = 1;
342 onion_handshakes_assigned
[ONION_HANDSHAKE_TYPE_TAP
] = 1;
343 onion_handshakes_requested
[ONION_HANDSHAKE_TYPE_NTOR
] = 1;
344 onion_handshakes_assigned
[ONION_HANDSHAKE_TYPE_NTOR
] = 1;
347 actual
= log_heartbeat(0);
349 tt_int_op(actual
, OP_EQ
, expected
);
350 tt_int_op(CALLED(logv
), OP_EQ
, 6);
353 NS_UNMOCK(tls_get_write_overhead_ratio
);
354 NS_UNMOCK(we_are_hibernating
);
355 NS_UNMOCK(public_server_mode
);
356 NS_UNMOCK(router_get_my_routerinfo
);
357 NS_UNMOCK(node_get_by_id
);
359 NS_UNMOCK(server_mode
);
360 tor_free(mock_routerinfo
);
364 NS(tls_get_write_overhead_ratio
)(void)
370 NS(we_are_hibernating
)(void)
376 NS(public_server_mode
)(const or_options_t
*options
)
383 static const routerinfo_t
*
384 NS(router_get_my_routerinfo
)(void)
386 mock_routerinfo
= tor_malloc(sizeof(routerinfo_t
));
388 return mock_routerinfo
;
391 static const node_t
*
392 NS(node_get_by_id
)(const char *identity_digest
)
394 (void)identity_digest
;
400 NS(logv
)(int severity
, log_domain_mask_t domain
,
401 const char *funcname
, const char *suffix
, const char *format
, va_list ap
)
403 switch (CALLED(logv
))
406 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
407 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
408 tt_ptr_op(strstr(funcname
, "log_heartbeat"), OP_NE
, NULL
);
409 tt_ptr_op(suffix
, OP_EQ
, NULL
);
410 tt_str_op(format
, OP_EQ
,
411 "Heartbeat: It seems like we are not in the cached consensus.");
414 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
415 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
416 tt_ptr_op(strstr(funcname
, "log_heartbeat"), OP_NE
, NULL
);
417 tt_ptr_op(suffix
, OP_EQ
, NULL
);
418 tt_str_op(format
, OP_EQ
,
419 "Heartbeat: Tor's uptime is %s, with %d circuits open. "
420 "I've sent %s and received %s.%s");
421 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0:00 hours"); /* uptime */
422 tt_int_op(va_arg(ap
, int), OP_EQ
, 0); /* count_circuits() */
423 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_sent */
424 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_rcvd */
425 tt_str_op(va_arg(ap
, char *), OP_EQ
, ""); /* hibernating */
428 tt_int_op(severity
, OP_EQ
, LOG_INFO
);
431 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
432 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
433 tt_ptr_op(strstr(funcname
, "rep_hist_log_circuit_handshake_stats"),
435 tt_ptr_op(suffix
, OP_EQ
, NULL
);
436 tt_str_op(format
, OP_EQ
,
437 "Circuit handshake stats since last time: %d/%d TAP, %d/%d NTor.");
438 tt_int_op(va_arg(ap
, int), OP_EQ
, 1); /* handshakes assigned (TAP) */
439 tt_int_op(va_arg(ap
, int), OP_EQ
, 1); /* handshakes requested (TAP) */
440 tt_int_op(va_arg(ap
, int), OP_EQ
, 1); /* handshakes assigned (NTOR) */
441 tt_int_op(va_arg(ap
, int), OP_EQ
, 1); /* handshakes requested (NTOR) */
444 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
445 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
446 tt_ptr_op(strstr(funcname
, "rep_hist_log_link_protocol_counts"),
450 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
451 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
452 tt_str_op(format
, OP_EQ
, "DoS mitigation since startup:%s%s%s%s");
453 tt_str_op(va_arg(ap
, char *), OP_EQ
,
454 " 0 circuits killed with too many cells.");
455 tt_str_op(va_arg(ap
, char *), OP_EQ
, " [cc not enabled]");
456 tt_str_op(va_arg(ap
, char *), OP_EQ
, " [conn not enabled]");
457 tt_str_op(va_arg(ap
, char *), OP_EQ
, "");
460 tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
469 NS(server_mode
)(const or_options_t
*options
)
477 #define NS_SUBMODULE ASPECT(log_heartbeat, simple)
480 * Tests that log_heartbeat() correctly logs heartbeat information
484 NS_DECL(double, tls_get_write_overhead_ratio
, (void));
485 NS_DECL(int, we_are_hibernating
, (void));
486 NS_DECL(int, public_server_mode
, (const or_options_t
*options
));
487 NS_DECL(long, get_uptime
, (void));
488 NS_DECL(uint64_t, get_bytes_read
, (void));
489 NS_DECL(uint64_t, get_bytes_written
, (void));
490 NS_DECL(void, logv
, (int severity
, log_domain_mask_t domain
,
491 const char *funcname
, const char *suffix
, const char *format
, va_list ap
));
492 NS_DECL(int, server_mode
, (const or_options_t
*options
));
494 static int NS(n_msgs
) = 0;
497 NS(test_main
)(void *arg
)
499 int expected
, actual
;
502 NS_MOCK(tls_get_write_overhead_ratio
);
503 NS_MOCK(we_are_hibernating
);
504 NS_MOCK(public_server_mode
);
506 NS_MOCK(get_bytes_read
);
507 NS_MOCK(get_bytes_written
);
509 NS_MOCK(server_mode
);
511 log_global_min_severity_
= LOG_DEBUG
;
514 actual
= log_heartbeat(0);
516 tt_int_op(actual
, OP_EQ
, expected
);
517 tt_int_op(NS(n_msgs
), OP_EQ
, 1);
520 NS_UNMOCK(tls_get_write_overhead_ratio
);
521 NS_UNMOCK(we_are_hibernating
);
522 NS_UNMOCK(public_server_mode
);
523 NS_UNMOCK(get_uptime
);
524 NS_UNMOCK(get_bytes_read
);
525 NS_UNMOCK(get_bytes_written
);
527 NS_UNMOCK(server_mode
);
531 NS(tls_get_write_overhead_ratio
)(void)
537 NS(we_are_hibernating
)(void)
543 NS(public_server_mode
)(const or_options_t
*options
)
557 NS(get_bytes_read
)(void)
563 NS(get_bytes_written
)(void)
569 NS(logv
)(int severity
, log_domain_mask_t domain
, const char *funcname
,
570 const char *suffix
, const char *format
, va_list ap
)
572 if (severity
== LOG_INFO
)
576 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
577 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
578 tt_ptr_op(strstr(funcname
, "log_heartbeat"), OP_NE
, NULL
);
579 tt_ptr_op(suffix
, OP_EQ
, NULL
);
580 tt_str_op(format
, OP_EQ
,
581 "Heartbeat: Tor's uptime is %s, with %d circuits open. "
582 "I've sent %s and received %s.%s");
583 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0:00 hours"); /* uptime */
584 tt_int_op(va_arg(ap
, int), OP_EQ
, 0); /* count_circuits() */
585 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_sent */
586 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_rcvd */
587 tt_str_op(va_arg(ap
, char *), OP_EQ
, " We are currently hibernating.");
594 NS(server_mode
)(const or_options_t
*options
)
602 #define NS_SUBMODULE ASPECT(log_heartbeat, calls_log_accounting)
605 * Tests that log_heartbeat() correctly logs heartbeat information
606 * and accounting information when configured.
609 NS_DECL(double, tls_get_write_overhead_ratio
, (void));
610 NS_DECL(int, we_are_hibernating
, (void));
611 NS_DECL(int, public_server_mode
, (const or_options_t
*options
));
612 NS_DECL(long, get_uptime
, (void));
613 NS_DECL(uint64_t, get_bytes_read
, (void));
614 NS_DECL(uint64_t, get_bytes_written
, (void));
615 NS_DECL(void, logv
, (int severity
, log_domain_mask_t domain
,
616 const char *funcname
, const char *suffix
, const char *format
, va_list ap
));
617 NS_DECL(int, server_mode
, (const or_options_t
*options
));
618 NS_DECL(or_state_t
*, get_or_state
, (void));
619 NS_DECL(int, accounting_is_enabled
, (const or_options_t
*options
));
620 NS_DECL(time_t, accounting_get_end_time
, (void));
622 static or_state_t
* NS(mock_state
) = NULL
;
623 static or_options_t
* NS(mock_options
) = NULL
;
626 NS(test_main
)(void *arg
)
628 int expected
, actual
;
631 NS_MOCK(tls_get_write_overhead_ratio
);
632 NS_MOCK(we_are_hibernating
);
633 NS_MOCK(public_server_mode
);
635 NS_MOCK(get_bytes_read
);
636 NS_MOCK(get_bytes_written
);
638 NS_MOCK(server_mode
);
639 NS_MOCK(get_or_state
);
640 NS_MOCK(accounting_is_enabled
);
641 NS_MOCK(accounting_get_end_time
);
643 log_global_min_severity_
= LOG_DEBUG
;
646 actual
= log_heartbeat(0);
648 tt_int_op(actual
, OP_EQ
, expected
);
649 tt_int_op(CALLED(logv
), OP_EQ
, 3);
652 NS_UNMOCK(tls_get_write_overhead_ratio
);
653 NS_UNMOCK(we_are_hibernating
);
654 NS_UNMOCK(public_server_mode
);
655 NS_UNMOCK(get_uptime
);
656 NS_UNMOCK(get_bytes_read
);
657 NS_UNMOCK(get_bytes_written
);
659 NS_UNMOCK(server_mode
);
660 NS_UNMOCK(accounting_is_enabled
);
661 NS_UNMOCK(accounting_get_end_time
);
662 tor_free_(NS(mock_state
));
663 tor_free_(NS(mock_options
));
667 NS(tls_get_write_overhead_ratio
)(void)
673 NS(we_are_hibernating
)(void)
679 NS(public_server_mode
)(const or_options_t
*options
)
693 NS(get_bytes_read
)(void)
699 NS(get_bytes_written
)(void)
705 NS(logv
)(int severity
, log_domain_mask_t domain
,
706 const char *funcname
, const char *suffix
, const char *format
, va_list ap
)
708 switch (CALLED(logv
))
711 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
712 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
713 tt_ptr_op(strstr(funcname
, "log_heartbeat"), OP_NE
, NULL
);
714 tt_ptr_op(suffix
, OP_EQ
, NULL
);
715 tt_str_op(format
, OP_EQ
,
716 "Heartbeat: Tor's uptime is %s, with %d circuits open. "
717 "I've sent %s and received %s.%s");
718 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0:00 hours"); /* uptime */
719 tt_int_op(va_arg(ap
, int), OP_EQ
, 0); /* count_circuits() */
720 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_sent */
721 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_rcvd */
722 tt_str_op(va_arg(ap
, char *), OP_EQ
, ""); /* hibernating */
725 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
726 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
727 tt_ptr_op(strstr(funcname
, "log_accounting"), OP_NE
, NULL
);
728 tt_ptr_op(suffix
, OP_EQ
, NULL
);
729 tt_str_op(format
, OP_EQ
,
730 "Heartbeat: Accounting enabled. Sent: %s, Received: %s, Used: %s / "
731 "%s, Rule: %s. The current accounting interval ends on %s, in %s.");
732 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* acc_sent */
733 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* acc_rcvd */
734 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* acc_used */
735 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* acc_max */
736 tt_str_op(va_arg(ap
, char *), OP_EQ
, "max"); /* acc_rule */
737 /* format_local_iso_time uses local tz, so we can't just compare
738 * the string against a constant */
739 char datetime
[ISO_TIME_LEN
+1];
740 format_local_iso_time(datetime
, 60);
741 tt_str_op(va_arg(ap
, char *), OP_EQ
, datetime
); /* end_buf */
742 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0:01 hours"); /* remaining */
745 tt_int_op(severity
, OP_EQ
, LOG_INFO
);
748 tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
757 NS(server_mode
)(const or_options_t
*options
)
765 NS(accounting_is_enabled
)(const or_options_t
*options
)
773 NS(accounting_get_end_time
)(void)
779 NS(get_or_state
)(void)
781 NS(mock_state
) = tor_malloc_zero(sizeof(or_state_t
));
782 NS(mock_state
)->AccountingBytesReadInInterval
= 0;
783 NS(mock_state
)->AccountingBytesWrittenInInterval
= 0;
785 return NS(mock_state
);
789 #define NS_SUBMODULE ASPECT(log_heartbeat, packaged_cell_fullness)
792 * Tests that log_heartbeat() correctly logs packaged cell
793 * fullness information.
796 NS_DECL(double, tls_get_write_overhead_ratio
, (void));
797 NS_DECL(int, we_are_hibernating
, (void));
798 NS_DECL(int, public_server_mode
, (const or_options_t
*options
));
799 NS_DECL(long, get_uptime
, (void));
800 NS_DECL(uint64_t, get_bytes_read
, (void));
801 NS_DECL(uint64_t, get_bytes_written
, (void));
802 NS_DECL(void, logv
, (int severity
, log_domain_mask_t domain
,
803 const char *funcname
, const char *suffix
, const char *format
, va_list ap
));
804 NS_DECL(int, server_mode
, (const or_options_t
*options
));
805 NS_DECL(int, accounting_is_enabled
, (const or_options_t
*options
));
808 NS(test_main
)(void *arg
)
810 int expected
, actual
;
813 NS_MOCK(tls_get_write_overhead_ratio
);
814 NS_MOCK(we_are_hibernating
);
815 NS_MOCK(public_server_mode
);
817 NS_MOCK(get_bytes_read
);
818 NS_MOCK(get_bytes_written
);
820 NS_MOCK(server_mode
);
821 NS_MOCK(accounting_is_enabled
);
822 log_global_min_severity_
= LOG_DEBUG
;
824 stats_n_data_bytes_packaged
= RELAY_PAYLOAD_SIZE
;
825 stats_n_data_cells_packaged
= 2;
827 actual
= log_heartbeat(0);
829 tt_int_op(actual
, OP_EQ
, expected
);
830 tt_int_op(CALLED(logv
), OP_EQ
, 2);
833 stats_n_data_bytes_packaged
= 0;
834 stats_n_data_cells_packaged
= 0;
835 NS_UNMOCK(tls_get_write_overhead_ratio
);
836 NS_UNMOCK(we_are_hibernating
);
837 NS_UNMOCK(public_server_mode
);
838 NS_UNMOCK(get_uptime
);
839 NS_UNMOCK(get_bytes_read
);
840 NS_UNMOCK(get_bytes_written
);
842 NS_UNMOCK(server_mode
);
843 NS_UNMOCK(accounting_is_enabled
);
847 NS(tls_get_write_overhead_ratio
)(void)
853 NS(we_are_hibernating
)(void)
859 NS(public_server_mode
)(const or_options_t
*options
)
873 NS(get_bytes_read
)(void)
879 NS(get_bytes_written
)(void)
885 NS(logv
)(int severity
, log_domain_mask_t domain
, const char *funcname
,
886 const char *suffix
, const char *format
, va_list ap
)
888 switch (CALLED(logv
))
891 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
892 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
893 tt_ptr_op(strstr(funcname
, "log_heartbeat"), OP_NE
, NULL
);
894 tt_ptr_op(suffix
, OP_EQ
, NULL
);
895 tt_str_op(format
, OP_EQ
,
896 "Heartbeat: Tor's uptime is %s, with %d circuits open. "
897 "I've sent %s and received %s.%s");
898 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0:00 hours"); /* uptime */
899 tt_int_op(va_arg(ap
, int), OP_EQ
, 0); /* count_circuits() */
900 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_sent */
901 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_rcvd */
902 tt_str_op(va_arg(ap
, char *), OP_EQ
, ""); /* hibernating */
905 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
906 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
907 tt_ptr_op(strstr(funcname
, "log_heartbeat"), OP_NE
, NULL
);
908 tt_ptr_op(suffix
, OP_EQ
, NULL
);
909 tt_str_op(format
, OP_EQ
,
910 "Average packaged cell fullness: %2.3f%%. "
911 "TLS write overhead: %.f%%");
912 tt_double_op(fabs(va_arg(ap
, double) - 50.0), OP_LE
, DBL_EPSILON
);
913 tt_double_op(fabs(va_arg(ap
, double) - 0.0), OP_LE
, DBL_EPSILON
);
916 tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
925 NS(server_mode
)(const or_options_t
*options
)
933 NS(accounting_is_enabled
)(const or_options_t
*options
)
941 #define NS_SUBMODULE ASPECT(log_heartbeat, tls_write_overhead)
944 * Tests that log_heartbeat() correctly logs the TLS write overhead information
945 * when the TLS write overhead ratio exceeds 1.
948 NS_DECL(double, tls_get_write_overhead_ratio
, (void));
949 NS_DECL(int, we_are_hibernating
, (void));
950 NS_DECL(int, public_server_mode
, (const or_options_t
*options
));
951 NS_DECL(long, get_uptime
, (void));
952 NS_DECL(uint64_t, get_bytes_read
, (void));
953 NS_DECL(uint64_t, get_bytes_written
, (void));
954 NS_DECL(void, logv
, (int severity
, log_domain_mask_t domain
,
955 const char *funcname
, const char *suffix
, const char *format
, va_list ap
));
956 NS_DECL(int, server_mode
, (const or_options_t
*options
));
957 NS_DECL(int, accounting_is_enabled
, (const or_options_t
*options
));
960 NS(test_main
)(void *arg
)
962 int expected
, actual
;
965 NS_MOCK(tls_get_write_overhead_ratio
);
966 NS_MOCK(we_are_hibernating
);
967 NS_MOCK(public_server_mode
);
969 NS_MOCK(get_bytes_read
);
970 NS_MOCK(get_bytes_written
);
972 NS_MOCK(server_mode
);
973 NS_MOCK(accounting_is_enabled
);
974 stats_n_data_cells_packaged
= 0;
975 log_global_min_severity_
= LOG_DEBUG
;
978 actual
= log_heartbeat(0);
980 tt_int_op(actual
, OP_EQ
, expected
);
981 tt_int_op(CALLED(logv
), OP_EQ
, 2);
984 NS_UNMOCK(tls_get_write_overhead_ratio
);
985 NS_UNMOCK(we_are_hibernating
);
986 NS_UNMOCK(public_server_mode
);
987 NS_UNMOCK(get_uptime
);
988 NS_UNMOCK(get_bytes_read
);
989 NS_UNMOCK(get_bytes_written
);
991 NS_UNMOCK(server_mode
);
992 NS_UNMOCK(accounting_is_enabled
);
996 NS(tls_get_write_overhead_ratio
)(void)
1002 NS(we_are_hibernating
)(void)
1008 NS(public_server_mode
)(const or_options_t
*options
)
1016 NS(get_uptime
)(void)
1022 NS(get_bytes_read
)(void)
1028 NS(get_bytes_written
)(void)
1034 NS(logv
)(int severity
, log_domain_mask_t domain
,
1035 const char *funcname
, const char *suffix
, const char *format
, va_list ap
)
1037 switch (CALLED(logv
))
1040 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
1041 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
1042 tt_ptr_op(strstr(funcname
, "log_heartbeat"), OP_NE
, NULL
);
1043 tt_ptr_op(suffix
, OP_EQ
, NULL
);
1044 tt_str_op(format
, OP_EQ
,
1045 "Heartbeat: Tor's uptime is %s, with %d circuits open. "
1046 "I've sent %s and received %s.%s");
1047 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0:00 hours"); /* uptime */
1048 tt_int_op(va_arg(ap
, int), OP_EQ
, 0); /* count_circuits() */
1049 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_sent */
1050 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_rcvd */
1051 tt_str_op(va_arg(ap
, char *), OP_EQ
, ""); /* hibernating */
1054 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
1055 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
1056 tt_ptr_op(strstr(funcname
, "log_heartbeat"), OP_NE
, NULL
);
1057 tt_ptr_op(suffix
, OP_EQ
, NULL
);
1058 tt_str_op(format
, OP_EQ
,
1059 "Average packaged cell fullness: %2.3f%%. "
1060 "TLS write overhead: %.f%%");
1061 tt_int_op(fabs(va_arg(ap
, double) - 100.0) <= DBL_EPSILON
, OP_EQ
, 1);
1062 tt_double_op(fabs(va_arg(ap
, double) - 100.0), OP_LE
, DBL_EPSILON
);
1065 tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
1074 NS(server_mode
)(const or_options_t
*options
)
1082 NS(accounting_is_enabled
)(const or_options_t
*options
)
1091 struct testcase_t status_tests
[] = {
1092 TEST_CASE(count_circuits
),
1093 TEST_CASE(secs_to_uptime
),
1094 TEST_CASE(bytes_to_usage
),
1095 TEST_CASE_ASPECT(log_heartbeat
, fails
),
1096 TEST_CASE_ASPECT(log_heartbeat
, simple
),
1097 TEST_CASE_ASPECT(log_heartbeat
, not_in_consensus
),
1098 TEST_CASE_ASPECT(log_heartbeat
, calls_log_accounting
),
1099 TEST_CASE_ASPECT(log_heartbeat
, packaged_cell_fullness
),
1100 TEST_CASE_ASPECT(log_heartbeat
, tls_write_overhead
),