2 #define HIBERNATE_PRIVATE
4 #define REPHIST_PRIVATE
11 #include "tor_queue.h"
13 #include "circuitlist.h"
15 #include "hibernate.h"
21 #include "statefile.h"
24 #define NS_MODULE status
26 #define NS_SUBMODULE count_circuits
29 * Test that count_circuits() is correctly counting the number of
33 static smartlist_t
* mock_global_circuitlist
= NULL
;
35 NS_DECL(smartlist_t
*, circuit_get_global_list
, (void));
38 NS(test_main
)(void *arg
)
40 /* Choose origin_circuit_t wlog. */
41 origin_circuit_t
*mock_circuit1
, *mock_circuit2
;
42 int expected_circuits
= 2, actual_circuits
;
46 mock_circuit1
= tor_malloc_zero(sizeof(origin_circuit_t
));
47 mock_circuit2
= tor_malloc_zero(sizeof(origin_circuit_t
));
48 mock_global_circuitlist
= smartlist_new();
49 smartlist_add(mock_global_circuitlist
, TO_CIRCUIT(mock_circuit1
));
50 smartlist_add(mock_global_circuitlist
, TO_CIRCUIT(mock_circuit2
));
52 NS_MOCK(circuit_get_global_list
);
54 actual_circuits
= count_circuits();
56 tt_assert(expected_circuits
== actual_circuits
);
59 tor_free(mock_circuit1
);
60 tor_free(mock_circuit2
);
61 smartlist_free(mock_global_circuitlist
);
62 mock_global_circuitlist
= NULL
;
63 NS_UNMOCK(circuit_get_global_list
);
67 NS(circuit_get_global_list
)(void)
69 return mock_global_circuitlist
;
73 #define NS_SUBMODULE secs_to_uptime
76 * Test that secs_to_uptime() is converting the number of seconds that
77 * Tor is up for into the appropriate string form containing hours and minutes.
81 NS(test_main
)(void *arg
)
87 expected
= "0:00 hours";
88 actual
= secs_to_uptime(0);
89 tt_str_op(actual
, OP_EQ
, expected
);
92 expected
= "0:00 hours";
93 actual
= secs_to_uptime(1);
94 tt_str_op(actual
, OP_EQ
, expected
);
97 expected
= "0:01 hours";
98 actual
= secs_to_uptime(60);
99 tt_str_op(actual
, OP_EQ
, expected
);
102 expected
= "0:59 hours";
103 actual
= secs_to_uptime(60 * 59);
104 tt_str_op(actual
, OP_EQ
, expected
);
107 expected
= "1:00 hours";
108 actual
= secs_to_uptime(60 * 60);
109 tt_str_op(actual
, OP_EQ
, expected
);
112 expected
= "23:59 hours";
113 actual
= secs_to_uptime(60 * 60 * 23 + 60 * 59);
114 tt_str_op(actual
, OP_EQ
, expected
);
117 expected
= "1 day 0:00 hours";
118 actual
= secs_to_uptime(60 * 60 * 23 + 60 * 60);
119 tt_str_op(actual
, OP_EQ
, expected
);
122 expected
= "1 day 0:00 hours";
123 actual
= secs_to_uptime(86400 + 1);
124 tt_str_op(actual
, OP_EQ
, expected
);
127 expected
= "1 day 0:01 hours";
128 actual
= secs_to_uptime(86400 + 60);
129 tt_str_op(actual
, OP_EQ
, expected
);
132 expected
= "10 days 0:00 hours";
133 actual
= secs_to_uptime(86400 * 10);
134 tt_str_op(actual
, OP_EQ
, expected
);
137 expected
= "10 days 0:00 hours";
138 actual
= secs_to_uptime(864000 + 1);
139 tt_str_op(actual
, OP_EQ
, expected
);
142 expected
= "10 days 0:01 hours";
143 actual
= secs_to_uptime(864000 + 60);
144 tt_str_op(actual
, OP_EQ
, expected
);
153 #define NS_SUBMODULE bytes_to_usage
156 * Test that bytes_to_usage() is correctly converting the number of bytes that
157 * Tor has read/written into the appropriate string form containing kilobytes,
158 * megabytes, or gigabytes.
162 NS(test_main
)(void *arg
)
164 const char *expected
;
169 actual
= bytes_to_usage(0);
170 tt_str_op(actual
, OP_EQ
, expected
);
174 actual
= bytes_to_usage(1);
175 tt_str_op(actual
, OP_EQ
, expected
);
179 actual
= bytes_to_usage(1024);
180 tt_str_op(actual
, OP_EQ
, expected
);
183 expected
= "1023 kB";
184 actual
= bytes_to_usage((1 << 20) - 1);
185 tt_str_op(actual
, OP_EQ
, expected
);
188 expected
= "1.00 MB";
189 actual
= bytes_to_usage((1 << 20));
190 tt_str_op(actual
, OP_EQ
, expected
);
193 expected
= "1.00 MB";
194 actual
= bytes_to_usage((1 << 20) + 5242);
195 tt_str_op(actual
, OP_EQ
, expected
);
198 expected
= "1.01 MB";
199 actual
= bytes_to_usage((1 << 20) + 5243);
200 tt_str_op(actual
, OP_EQ
, expected
);
203 expected
= "1024.00 MB";
204 actual
= bytes_to_usage((1 << 30) - 1);
205 tt_str_op(actual
, OP_EQ
, expected
);
208 expected
= "1.00 GB";
209 actual
= bytes_to_usage((1 << 30));
210 tt_str_op(actual
, OP_EQ
, expected
);
213 expected
= "1.00 GB";
214 actual
= bytes_to_usage((1 << 30) + 5368709);
215 tt_str_op(actual
, OP_EQ
, expected
);
218 expected
= "1.01 GB";
219 actual
= bytes_to_usage((1 << 30) + 5368710);
220 tt_str_op(actual
, OP_EQ
, expected
);
223 expected
= "10.00 GB";
224 actual
= bytes_to_usage((U64_LITERAL(1) << 30) * 10L);
225 tt_str_op(actual
, OP_EQ
, expected
);
234 #define NS_SUBMODULE ASPECT(log_heartbeat, fails)
237 * Tests that log_heartbeat() fails when in the public server mode,
238 * not hibernating, and we couldn't get the current routerinfo.
241 NS_DECL(double, tls_get_write_overhead_ratio
, (void));
242 NS_DECL(int, we_are_hibernating
, (void));
243 NS_DECL(int, public_server_mode
, (const or_options_t
*options
));
244 NS_DECL(const routerinfo_t
*, router_get_my_routerinfo
, (void));
247 NS(test_main
)(void *arg
)
249 int expected
, actual
;
252 NS_MOCK(tls_get_write_overhead_ratio
);
253 NS_MOCK(we_are_hibernating
);
254 NS_MOCK(public_server_mode
);
255 NS_MOCK(router_get_my_routerinfo
);
258 actual
= log_heartbeat(0);
260 tt_int_op(actual
, OP_EQ
, expected
);
263 NS_UNMOCK(tls_get_write_overhead_ratio
);
264 NS_UNMOCK(we_are_hibernating
);
265 NS_UNMOCK(public_server_mode
);
266 NS_UNMOCK(router_get_my_routerinfo
);
270 NS(tls_get_write_overhead_ratio
)(void)
276 NS(we_are_hibernating
)(void)
282 NS(public_server_mode
)(const or_options_t
*options
)
289 static const routerinfo_t
*
290 NS(router_get_my_routerinfo
)(void)
296 #define NS_SUBMODULE ASPECT(log_heartbeat, not_in_consensus)
299 * Tests that log_heartbeat() logs appropriately if we are not in the cached
303 NS_DECL(double, tls_get_write_overhead_ratio
, (void));
304 NS_DECL(int, we_are_hibernating
, (void));
305 NS_DECL(int, public_server_mode
, (const or_options_t
*options
));
306 NS_DECL(const routerinfo_t
*, router_get_my_routerinfo
, (void));
307 NS_DECL(const node_t
*, node_get_by_id
, (const char *identity_digest
));
308 NS_DECL(void, logv
, (int severity
, log_domain_mask_t domain
,
309 const char *funcname
, const char *suffix
, const char *format
, va_list ap
));
310 NS_DECL(int, server_mode
, (const or_options_t
*options
));
312 static routerinfo_t
*mock_routerinfo
;
313 extern int onion_handshakes_requested
[MAX_ONION_HANDSHAKE_TYPE
+1];
314 extern int onion_handshakes_assigned
[MAX_ONION_HANDSHAKE_TYPE
+1];
317 NS(test_main
)(void *arg
)
319 int expected
, actual
;
322 NS_MOCK(tls_get_write_overhead_ratio
);
323 NS_MOCK(we_are_hibernating
);
324 NS_MOCK(public_server_mode
);
325 NS_MOCK(router_get_my_routerinfo
);
326 NS_MOCK(node_get_by_id
);
328 NS_MOCK(server_mode
);
330 log_global_min_severity_
= LOG_DEBUG
;
331 onion_handshakes_requested
[ONION_HANDSHAKE_TYPE_TAP
] = 1;
332 onion_handshakes_assigned
[ONION_HANDSHAKE_TYPE_TAP
] = 1;
333 onion_handshakes_requested
[ONION_HANDSHAKE_TYPE_NTOR
] = 1;
334 onion_handshakes_assigned
[ONION_HANDSHAKE_TYPE_NTOR
] = 1;
337 actual
= log_heartbeat(0);
339 tt_int_op(actual
, OP_EQ
, expected
);
340 tt_int_op(CALLED(logv
), OP_EQ
, 5);
343 NS_UNMOCK(tls_get_write_overhead_ratio
);
344 NS_UNMOCK(we_are_hibernating
);
345 NS_UNMOCK(public_server_mode
);
346 NS_UNMOCK(router_get_my_routerinfo
);
347 NS_UNMOCK(node_get_by_id
);
349 NS_UNMOCK(server_mode
);
350 tor_free(mock_routerinfo
);
354 NS(tls_get_write_overhead_ratio
)(void)
360 NS(we_are_hibernating
)(void)
366 NS(public_server_mode
)(const or_options_t
*options
)
373 static const routerinfo_t
*
374 NS(router_get_my_routerinfo
)(void)
376 mock_routerinfo
= tor_malloc(sizeof(routerinfo_t
));
378 return mock_routerinfo
;
381 static const node_t
*
382 NS(node_get_by_id
)(const char *identity_digest
)
384 (void)identity_digest
;
390 NS(logv
)(int severity
, log_domain_mask_t domain
,
391 const char *funcname
, const char *suffix
, const char *format
, va_list ap
)
393 switch (CALLED(logv
))
396 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
397 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
398 tt_ptr_op(strstr(funcname
, "log_heartbeat"), OP_NE
, NULL
);
399 tt_ptr_op(suffix
, OP_EQ
, NULL
);
400 tt_str_op(format
, OP_EQ
,
401 "Heartbeat: It seems like we are not in the cached consensus.");
404 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
405 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
406 tt_ptr_op(strstr(funcname
, "log_heartbeat"), OP_NE
, NULL
);
407 tt_ptr_op(suffix
, OP_EQ
, NULL
);
408 tt_str_op(format
, OP_EQ
,
409 "Heartbeat: Tor's uptime is %s, with %d circuits open. "
410 "I've sent %s and received %s.%s");
411 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0:00 hours"); /* uptime */
412 tt_int_op(va_arg(ap
, int), OP_EQ
, 0); /* count_circuits() */
413 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_sent */
414 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_rcvd */
415 tt_str_op(va_arg(ap
, char *), OP_EQ
, ""); /* hibernating */
418 tt_int_op(severity
, OP_EQ
, LOG_INFO
);
421 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
422 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
423 tt_ptr_op(strstr(funcname
, "rep_hist_log_circuit_handshake_stats"),
425 tt_ptr_op(suffix
, OP_EQ
, NULL
);
426 tt_str_op(format
, OP_EQ
,
427 "Circuit handshake stats since last time: %d/%d TAP, %d/%d NTor.");
428 tt_int_op(va_arg(ap
, int), OP_EQ
, 1); /* handshakes assigned (TAP) */
429 tt_int_op(va_arg(ap
, int), OP_EQ
, 1); /* handshakes requested (TAP) */
430 tt_int_op(va_arg(ap
, int), OP_EQ
, 1); /* handshakes assigned (NTOR) */
431 tt_int_op(va_arg(ap
, int), OP_EQ
, 1); /* handshakes requested (NTOR) */
434 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
435 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
436 tt_ptr_op(strstr(funcname
, "rep_hist_log_link_protocol_counts"),
440 tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
449 NS(server_mode
)(const or_options_t
*options
)
457 #define NS_SUBMODULE ASPECT(log_heartbeat, simple)
460 * Tests that log_heartbeat() correctly logs heartbeat information
464 NS_DECL(double, tls_get_write_overhead_ratio
, (void));
465 NS_DECL(int, we_are_hibernating
, (void));
466 NS_DECL(int, public_server_mode
, (const or_options_t
*options
));
467 NS_DECL(long, get_uptime
, (void));
468 NS_DECL(uint64_t, get_bytes_read
, (void));
469 NS_DECL(uint64_t, get_bytes_written
, (void));
470 NS_DECL(void, logv
, (int severity
, log_domain_mask_t domain
,
471 const char *funcname
, const char *suffix
, const char *format
, va_list ap
));
472 NS_DECL(int, server_mode
, (const or_options_t
*options
));
474 static int NS(n_msgs
) = 0;
477 NS(test_main
)(void *arg
)
479 int expected
, actual
;
482 NS_MOCK(tls_get_write_overhead_ratio
);
483 NS_MOCK(we_are_hibernating
);
484 NS_MOCK(public_server_mode
);
486 NS_MOCK(get_bytes_read
);
487 NS_MOCK(get_bytes_written
);
489 NS_MOCK(server_mode
);
491 log_global_min_severity_
= LOG_DEBUG
;
494 actual
= log_heartbeat(0);
496 tt_int_op(actual
, OP_EQ
, expected
);
497 tt_int_op(NS(n_msgs
), OP_EQ
, 1);
500 NS_UNMOCK(tls_get_write_overhead_ratio
);
501 NS_UNMOCK(we_are_hibernating
);
502 NS_UNMOCK(public_server_mode
);
503 NS_UNMOCK(get_uptime
);
504 NS_UNMOCK(get_bytes_read
);
505 NS_UNMOCK(get_bytes_written
);
507 NS_UNMOCK(server_mode
);
511 NS(tls_get_write_overhead_ratio
)(void)
517 NS(we_are_hibernating
)(void)
523 NS(public_server_mode
)(const or_options_t
*options
)
537 NS(get_bytes_read
)(void)
543 NS(get_bytes_written
)(void)
549 NS(logv
)(int severity
, log_domain_mask_t domain
, const char *funcname
,
550 const char *suffix
, const char *format
, va_list ap
)
552 if (severity
== LOG_INFO
)
556 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
557 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
558 tt_ptr_op(strstr(funcname
, "log_heartbeat"), OP_NE
, NULL
);
559 tt_ptr_op(suffix
, OP_EQ
, NULL
);
560 tt_str_op(format
, OP_EQ
,
561 "Heartbeat: Tor's uptime is %s, with %d circuits open. "
562 "I've sent %s and received %s.%s");
563 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0:00 hours"); /* uptime */
564 tt_int_op(va_arg(ap
, int), OP_EQ
, 0); /* count_circuits() */
565 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_sent */
566 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_rcvd */
567 tt_str_op(va_arg(ap
, char *), OP_EQ
, " We are currently hibernating.");
574 NS(server_mode
)(const or_options_t
*options
)
582 #define NS_SUBMODULE ASPECT(log_heartbeat, calls_log_accounting)
585 * Tests that log_heartbeat() correctly logs heartbeat information
586 * and accounting information when configured.
589 NS_DECL(double, tls_get_write_overhead_ratio
, (void));
590 NS_DECL(int, we_are_hibernating
, (void));
591 NS_DECL(int, public_server_mode
, (const or_options_t
*options
));
592 NS_DECL(long, get_uptime
, (void));
593 NS_DECL(uint64_t, get_bytes_read
, (void));
594 NS_DECL(uint64_t, get_bytes_written
, (void));
595 NS_DECL(void, logv
, (int severity
, log_domain_mask_t domain
,
596 const char *funcname
, const char *suffix
, const char *format
, va_list ap
));
597 NS_DECL(int, server_mode
, (const or_options_t
*options
));
598 NS_DECL(or_state_t
*, get_or_state
, (void));
599 NS_DECL(int, accounting_is_enabled
, (const or_options_t
*options
));
600 NS_DECL(time_t, accounting_get_end_time
, (void));
602 static or_state_t
* NS(mock_state
) = NULL
;
603 static or_options_t
* NS(mock_options
) = NULL
;
606 NS(test_main
)(void *arg
)
608 int expected
, actual
;
611 NS_MOCK(tls_get_write_overhead_ratio
);
612 NS_MOCK(we_are_hibernating
);
613 NS_MOCK(public_server_mode
);
615 NS_MOCK(get_bytes_read
);
616 NS_MOCK(get_bytes_written
);
618 NS_MOCK(server_mode
);
619 NS_MOCK(get_or_state
);
620 NS_MOCK(accounting_is_enabled
);
621 NS_MOCK(accounting_get_end_time
);
623 log_global_min_severity_
= LOG_DEBUG
;
626 actual
= log_heartbeat(0);
628 tt_int_op(actual
, OP_EQ
, expected
);
629 tt_int_op(CALLED(logv
), OP_EQ
, 3);
632 NS_UNMOCK(tls_get_write_overhead_ratio
);
633 NS_UNMOCK(we_are_hibernating
);
634 NS_UNMOCK(public_server_mode
);
635 NS_UNMOCK(get_uptime
);
636 NS_UNMOCK(get_bytes_read
);
637 NS_UNMOCK(get_bytes_written
);
639 NS_UNMOCK(server_mode
);
640 NS_UNMOCK(accounting_is_enabled
);
641 NS_UNMOCK(accounting_get_end_time
);
642 tor_free_(NS(mock_state
));
643 tor_free_(NS(mock_options
));
647 NS(tls_get_write_overhead_ratio
)(void)
653 NS(we_are_hibernating
)(void)
659 NS(public_server_mode
)(const or_options_t
*options
)
673 NS(get_bytes_read
)(void)
679 NS(get_bytes_written
)(void)
685 NS(logv
)(int severity
, log_domain_mask_t domain
,
686 const char *funcname
, const char *suffix
, const char *format
, va_list ap
)
688 switch (CALLED(logv
))
691 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
692 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
693 tt_ptr_op(strstr(funcname
, "log_heartbeat"), OP_NE
, NULL
);
694 tt_ptr_op(suffix
, OP_EQ
, NULL
);
695 tt_str_op(format
, OP_EQ
,
696 "Heartbeat: Tor's uptime is %s, with %d circuits open. "
697 "I've sent %s and received %s.%s");
698 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0:00 hours"); /* uptime */
699 tt_int_op(va_arg(ap
, int), OP_EQ
, 0); /* count_circuits() */
700 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_sent */
701 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_rcvd */
702 tt_str_op(va_arg(ap
, char *), OP_EQ
, ""); /* hibernating */
705 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
706 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
707 tt_ptr_op(strstr(funcname
, "log_accounting"), OP_NE
, NULL
);
708 tt_ptr_op(suffix
, OP_EQ
, NULL
);
709 tt_str_op(format
, OP_EQ
,
710 "Heartbeat: Accounting enabled. Sent: %s / %s, Received: %s / %s. "
711 "The current accounting interval ends on %s, in %s.");
712 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* acc_sent */
713 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* acc_max */
714 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* acc_rcvd */
715 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* acc_max */
716 /* format_local_iso_time uses local tz, just check mins and secs. */
717 tt_ptr_op(strstr(va_arg(ap
, char *), ":01:00"),
718 OP_NE
, NULL
); /* end_buf */
719 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0:01 hours"); /* remaining */
722 tt_int_op(severity
, OP_EQ
, LOG_INFO
);
725 tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
734 NS(server_mode
)(const or_options_t
*options
)
742 NS(accounting_is_enabled
)(const or_options_t
*options
)
750 NS(accounting_get_end_time
)(void)
756 NS(get_or_state
)(void)
758 NS(mock_state
) = tor_malloc_zero(sizeof(or_state_t
));
759 NS(mock_state
)->AccountingBytesReadInInterval
= 0;
760 NS(mock_state
)->AccountingBytesWrittenInInterval
= 0;
762 return NS(mock_state
);
766 #define NS_SUBMODULE ASPECT(log_heartbeat, packaged_cell_fullness)
769 * Tests that log_heartbeat() correctly logs packaged cell
770 * fullness information.
773 NS_DECL(double, tls_get_write_overhead_ratio
, (void));
774 NS_DECL(int, we_are_hibernating
, (void));
775 NS_DECL(int, public_server_mode
, (const or_options_t
*options
));
776 NS_DECL(long, get_uptime
, (void));
777 NS_DECL(uint64_t, get_bytes_read
, (void));
778 NS_DECL(uint64_t, get_bytes_written
, (void));
779 NS_DECL(void, logv
, (int severity
, log_domain_mask_t domain
,
780 const char *funcname
, const char *suffix
, const char *format
, va_list ap
));
781 NS_DECL(int, server_mode
, (const or_options_t
*options
));
782 NS_DECL(int, accounting_is_enabled
, (const or_options_t
*options
));
785 NS(test_main
)(void *arg
)
787 int expected
, actual
;
790 NS_MOCK(tls_get_write_overhead_ratio
);
791 NS_MOCK(we_are_hibernating
);
792 NS_MOCK(public_server_mode
);
794 NS_MOCK(get_bytes_read
);
795 NS_MOCK(get_bytes_written
);
797 NS_MOCK(server_mode
);
798 NS_MOCK(accounting_is_enabled
);
799 log_global_min_severity_
= LOG_DEBUG
;
801 stats_n_data_bytes_packaged
= RELAY_PAYLOAD_SIZE
;
802 stats_n_data_cells_packaged
= 2;
804 actual
= log_heartbeat(0);
806 tt_int_op(actual
, OP_EQ
, expected
);
807 tt_int_op(CALLED(logv
), OP_EQ
, 2);
810 stats_n_data_bytes_packaged
= 0;
811 stats_n_data_cells_packaged
= 0;
812 NS_UNMOCK(tls_get_write_overhead_ratio
);
813 NS_UNMOCK(we_are_hibernating
);
814 NS_UNMOCK(public_server_mode
);
815 NS_UNMOCK(get_uptime
);
816 NS_UNMOCK(get_bytes_read
);
817 NS_UNMOCK(get_bytes_written
);
819 NS_UNMOCK(server_mode
);
820 NS_UNMOCK(accounting_is_enabled
);
824 NS(tls_get_write_overhead_ratio
)(void)
830 NS(we_are_hibernating
)(void)
836 NS(public_server_mode
)(const or_options_t
*options
)
850 NS(get_bytes_read
)(void)
856 NS(get_bytes_written
)(void)
862 NS(logv
)(int severity
, log_domain_mask_t domain
, const char *funcname
,
863 const char *suffix
, const char *format
, va_list ap
)
865 switch (CALLED(logv
))
868 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
869 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
870 tt_ptr_op(strstr(funcname
, "log_heartbeat"), OP_NE
, NULL
);
871 tt_ptr_op(suffix
, OP_EQ
, NULL
);
872 tt_str_op(format
, OP_EQ
,
873 "Heartbeat: Tor's uptime is %s, with %d circuits open. "
874 "I've sent %s and received %s.%s");
875 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0:00 hours"); /* uptime */
876 tt_int_op(va_arg(ap
, int), OP_EQ
, 0); /* count_circuits() */
877 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_sent */
878 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_rcvd */
879 tt_str_op(va_arg(ap
, char *), OP_EQ
, ""); /* hibernating */
882 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
883 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
884 tt_ptr_op(strstr(funcname
, "log_heartbeat"), OP_NE
, NULL
);
885 tt_ptr_op(suffix
, OP_EQ
, NULL
);
886 tt_str_op(format
, OP_EQ
,
887 "Average packaged cell fullness: %2.3f%%. "
888 "TLS write overhead: %.f%%");
889 tt_double_op(fabs(va_arg(ap
, double) - 50.0), <=, DBL_EPSILON
);
890 tt_double_op(fabs(va_arg(ap
, double) - 0.0), <=, DBL_EPSILON
);
893 tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
902 NS(server_mode
)(const or_options_t
*options
)
910 NS(accounting_is_enabled
)(const or_options_t
*options
)
918 #define NS_SUBMODULE ASPECT(log_heartbeat, tls_write_overhead)
921 * Tests that log_heartbeat() correctly logs the TLS write overhead information
922 * when the TLS write overhead ratio exceeds 1.
925 NS_DECL(double, tls_get_write_overhead_ratio
, (void));
926 NS_DECL(int, we_are_hibernating
, (void));
927 NS_DECL(int, public_server_mode
, (const or_options_t
*options
));
928 NS_DECL(long, get_uptime
, (void));
929 NS_DECL(uint64_t, get_bytes_read
, (void));
930 NS_DECL(uint64_t, get_bytes_written
, (void));
931 NS_DECL(void, logv
, (int severity
, log_domain_mask_t domain
,
932 const char *funcname
, const char *suffix
, const char *format
, va_list ap
));
933 NS_DECL(int, server_mode
, (const or_options_t
*options
));
934 NS_DECL(int, accounting_is_enabled
, (const or_options_t
*options
));
937 NS(test_main
)(void *arg
)
939 int expected
, actual
;
942 NS_MOCK(tls_get_write_overhead_ratio
);
943 NS_MOCK(we_are_hibernating
);
944 NS_MOCK(public_server_mode
);
946 NS_MOCK(get_bytes_read
);
947 NS_MOCK(get_bytes_written
);
949 NS_MOCK(server_mode
);
950 NS_MOCK(accounting_is_enabled
);
951 stats_n_data_cells_packaged
= 0;
952 log_global_min_severity_
= LOG_DEBUG
;
955 actual
= log_heartbeat(0);
957 tt_int_op(actual
, OP_EQ
, expected
);
958 tt_int_op(CALLED(logv
), OP_EQ
, 2);
961 NS_UNMOCK(tls_get_write_overhead_ratio
);
962 NS_UNMOCK(we_are_hibernating
);
963 NS_UNMOCK(public_server_mode
);
964 NS_UNMOCK(get_uptime
);
965 NS_UNMOCK(get_bytes_read
);
966 NS_UNMOCK(get_bytes_written
);
968 NS_UNMOCK(server_mode
);
969 NS_UNMOCK(accounting_is_enabled
);
973 NS(tls_get_write_overhead_ratio
)(void)
979 NS(we_are_hibernating
)(void)
985 NS(public_server_mode
)(const or_options_t
*options
)
999 NS(get_bytes_read
)(void)
1005 NS(get_bytes_written
)(void)
1011 NS(logv
)(int severity
, log_domain_mask_t domain
,
1012 const char *funcname
, const char *suffix
, const char *format
, va_list ap
)
1014 switch (CALLED(logv
))
1017 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
1018 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
1019 tt_ptr_op(strstr(funcname
, "log_heartbeat"), OP_NE
, NULL
);
1020 tt_ptr_op(suffix
, OP_EQ
, NULL
);
1021 tt_str_op(format
, OP_EQ
,
1022 "Heartbeat: Tor's uptime is %s, with %d circuits open. "
1023 "I've sent %s and received %s.%s");
1024 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0:00 hours"); /* uptime */
1025 tt_int_op(va_arg(ap
, int), OP_EQ
, 0); /* count_circuits() */
1026 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_sent */
1027 tt_str_op(va_arg(ap
, char *), OP_EQ
, "0 kB"); /* bw_rcvd */
1028 tt_str_op(va_arg(ap
, char *), OP_EQ
, ""); /* hibernating */
1031 tt_int_op(severity
, OP_EQ
, LOG_NOTICE
);
1032 tt_int_op(domain
, OP_EQ
, LD_HEARTBEAT
);
1033 tt_ptr_op(strstr(funcname
, "log_heartbeat"), OP_NE
, NULL
);
1034 tt_ptr_op(suffix
, OP_EQ
, NULL
);
1035 tt_str_op(format
, OP_EQ
,
1036 "Average packaged cell fullness: %2.3f%%. "
1037 "TLS write overhead: %.f%%");
1038 tt_int_op(fabs(va_arg(ap
, double) - 100.0) <= DBL_EPSILON
, OP_EQ
, 1);
1039 tt_double_op(fabs(va_arg(ap
, double) - 100.0), <=, DBL_EPSILON
);
1042 tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
1051 NS(server_mode
)(const or_options_t
*options
)
1059 NS(accounting_is_enabled
)(const or_options_t
*options
)
1068 struct testcase_t status_tests
[] = {
1069 TEST_CASE(count_circuits
),
1070 TEST_CASE(secs_to_uptime
),
1071 TEST_CASE(bytes_to_usage
),
1072 TEST_CASE_ASPECT(log_heartbeat
, fails
),
1073 TEST_CASE_ASPECT(log_heartbeat
, simple
),
1074 TEST_CASE_ASPECT(log_heartbeat
, not_in_consensus
),
1075 TEST_CASE_ASPECT(log_heartbeat
, calls_log_accounting
),
1076 TEST_CASE_ASPECT(log_heartbeat
, packaged_cell_fullness
),
1077 TEST_CASE_ASPECT(log_heartbeat
, tls_write_overhead
),