Remove unused function: dns_randfn_() in dns.c.
[tor.git] / src / test / test_status.c
blob9c47469975324a1552a85e2d59d78ccd23c263ac
1 /* Copyright (c) 2014-2019, 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"
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
42 * global circuits.
45 static smartlist_t * mock_global_circuitlist = NULL;
47 NS_DECL(smartlist_t *, circuit_get_global_list, (void));
49 static 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;
56 (void)arg;
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);
70 done:
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);
78 static smartlist_t *
79 NS(circuit_get_global_list)(void)
81 return mock_global_circuitlist;
84 #undef NS_SUBMODULE
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.
92 static void
93 NS(test_main)(void *arg)
95 const char *expected;
96 char *actual;
97 (void)arg;
99 expected = "0:00 hours";
100 actual = secs_to_uptime(0);
101 tt_str_op(actual, OP_EQ, expected);
102 tor_free(actual);
104 expected = "0:00 hours";
105 actual = secs_to_uptime(1);
106 tt_str_op(actual, OP_EQ, expected);
107 tor_free(actual);
109 expected = "0:01 hours";
110 actual = secs_to_uptime(60);
111 tt_str_op(actual, OP_EQ, expected);
112 tor_free(actual);
114 expected = "0:59 hours";
115 actual = secs_to_uptime(60 * 59);
116 tt_str_op(actual, OP_EQ, expected);
117 tor_free(actual);
119 expected = "1:00 hours";
120 actual = secs_to_uptime(60 * 60);
121 tt_str_op(actual, OP_EQ, expected);
122 tor_free(actual);
124 expected = "23:59 hours";
125 actual = secs_to_uptime(60 * 60 * 23 + 60 * 59);
126 tt_str_op(actual, OP_EQ, expected);
127 tor_free(actual);
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);
132 tor_free(actual);
134 expected = "1 day 0:00 hours";
135 actual = secs_to_uptime(86400 + 1);
136 tt_str_op(actual, OP_EQ, expected);
137 tor_free(actual);
139 expected = "1 day 0:01 hours";
140 actual = secs_to_uptime(86400 + 60);
141 tt_str_op(actual, OP_EQ, expected);
142 tor_free(actual);
144 expected = "10 days 0:00 hours";
145 actual = secs_to_uptime(86400 * 10);
146 tt_str_op(actual, OP_EQ, expected);
147 tor_free(actual);
149 expected = "10 days 0:00 hours";
150 actual = secs_to_uptime(864000 + 1);
151 tt_str_op(actual, OP_EQ, expected);
152 tor_free(actual);
154 expected = "10 days 0:01 hours";
155 actual = secs_to_uptime(864000 + 60);
156 tt_str_op(actual, OP_EQ, expected);
157 tor_free(actual);
159 done:
160 if (actual != NULL)
161 tor_free(actual);
164 #undef NS_SUBMODULE
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.
173 static void
174 NS(test_main)(void *arg)
176 const char *expected;
177 char *actual;
178 (void)arg;
180 expected = "0 kB";
181 actual = bytes_to_usage(0);
182 tt_str_op(actual, OP_EQ, expected);
183 tor_free(actual);
185 expected = "0 kB";
186 actual = bytes_to_usage(1);
187 tt_str_op(actual, OP_EQ, expected);
188 tor_free(actual);
190 expected = "1 kB";
191 actual = bytes_to_usage(1024);
192 tt_str_op(actual, OP_EQ, expected);
193 tor_free(actual);
195 expected = "1023 kB";
196 actual = bytes_to_usage((1 << 20) - 1);
197 tt_str_op(actual, OP_EQ, expected);
198 tor_free(actual);
200 expected = "1.00 MB";
201 actual = bytes_to_usage((1 << 20));
202 tt_str_op(actual, OP_EQ, expected);
203 tor_free(actual);
205 expected = "1.00 MB";
206 actual = bytes_to_usage((1 << 20) + 5242);
207 tt_str_op(actual, OP_EQ, expected);
208 tor_free(actual);
210 expected = "1.01 MB";
211 actual = bytes_to_usage((1 << 20) + 5243);
212 tt_str_op(actual, OP_EQ, expected);
213 tor_free(actual);
215 expected = "1024.00 MB";
216 actual = bytes_to_usage((1 << 30) - 1);
217 tt_str_op(actual, OP_EQ, expected);
218 tor_free(actual);
220 expected = "1.00 GB";
221 actual = bytes_to_usage((1 << 30));
222 tt_str_op(actual, OP_EQ, expected);
223 tor_free(actual);
225 expected = "1.00 GB";
226 actual = bytes_to_usage((1 << 30) + 5368709);
227 tt_str_op(actual, OP_EQ, expected);
228 tor_free(actual);
230 expected = "1.01 GB";
231 actual = bytes_to_usage((1 << 30) + 5368710);
232 tt_str_op(actual, OP_EQ, expected);
233 tor_free(actual);
235 expected = "10.00 GB";
236 actual = bytes_to_usage((UINT64_C(1) << 30) * 10L);
237 tt_str_op(actual, OP_EQ, expected);
238 tor_free(actual);
240 done:
241 if (actual != NULL)
242 tor_free(actual);
245 #undef NS_SUBMODULE
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));
258 static void
259 NS(test_main)(void *arg)
261 int expected, actual;
262 (void)arg;
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);
269 expected = -1;
270 actual = log_heartbeat(0);
272 tt_int_op(actual, OP_EQ, expected);
274 done:
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);
281 static double
282 NS(tls_get_write_overhead_ratio)(void)
284 return 2.0;
287 static int
288 NS(we_are_hibernating)(void)
290 return 0;
293 static int
294 NS(public_server_mode)(const or_options_t *options)
296 (void)options;
298 return 1;
301 static const routerinfo_t *
302 NS(router_get_my_routerinfo)(void)
304 return NULL;
307 #undef NS_SUBMODULE
308 #define NS_SUBMODULE ASPECT(log_heartbeat, not_in_consensus)
311 * Tests that log_heartbeat() logs appropriately if we are not in the cached
312 * consensus.
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;
326 static void
327 NS(test_main)(void *arg)
329 int expected, actual;
330 (void)arg;
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);
337 NS_MOCK(logv);
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;
346 expected = 0;
347 actual = log_heartbeat(0);
349 tt_int_op(actual, OP_EQ, expected);
350 tt_int_op(CALLED(logv), OP_EQ, 6);
352 done:
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);
358 NS_UNMOCK(logv);
359 NS_UNMOCK(server_mode);
360 tor_free(mock_routerinfo);
363 static double
364 NS(tls_get_write_overhead_ratio)(void)
366 return 1.0;
369 static int
370 NS(we_are_hibernating)(void)
372 return 0;
375 static int
376 NS(public_server_mode)(const or_options_t *options)
378 (void)options;
380 return 1;
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;
396 return NULL;
399 static void
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))
405 case 0:
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.");
412 break;
413 case 1:
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 */
426 break;
427 case 2:
428 tt_int_op(severity, OP_EQ, LOG_INFO);
429 break;
430 case 3:
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"),
434 OP_NE, NULL);
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) */
442 break;
443 case 4:
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"),
447 OP_NE, NULL);
448 break;
449 case 5:
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, "");
458 break;
459 default:
460 tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
461 break;
464 done:
465 CALLED(logv)++;
468 static int
469 NS(server_mode)(const or_options_t *options)
471 (void)options;
473 return 0;
476 #undef NS_SUBMODULE
477 #define NS_SUBMODULE ASPECT(log_heartbeat, simple)
480 * Tests that log_heartbeat() correctly logs heartbeat information
481 * normally.
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;
496 static void
497 NS(test_main)(void *arg)
499 int expected, actual;
500 (void)arg;
502 NS_MOCK(tls_get_write_overhead_ratio);
503 NS_MOCK(we_are_hibernating);
504 NS_MOCK(public_server_mode);
505 NS_MOCK(get_uptime);
506 NS_MOCK(get_bytes_read);
507 NS_MOCK(get_bytes_written);
508 NS_MOCK(logv);
509 NS_MOCK(server_mode);
511 log_global_min_severity_ = LOG_DEBUG;
513 expected = 0;
514 actual = log_heartbeat(0);
516 tt_int_op(actual, OP_EQ, expected);
517 tt_int_op(NS(n_msgs), OP_EQ, 1);
519 done:
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);
526 NS_UNMOCK(logv);
527 NS_UNMOCK(server_mode);
530 static double
531 NS(tls_get_write_overhead_ratio)(void)
533 return 1.0;
536 static int
537 NS(we_are_hibernating)(void)
539 return 1;
542 static int
543 NS(public_server_mode)(const or_options_t *options)
545 (void)options;
547 return 0;
550 static long
551 NS(get_uptime)(void)
553 return 0;
556 static uint64_t
557 NS(get_bytes_read)(void)
559 return 0;
562 static uint64_t
563 NS(get_bytes_written)(void)
565 return 0;
568 static 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)
573 return;
574 ++NS(n_msgs);
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.");
589 done:
593 static int
594 NS(server_mode)(const or_options_t *options)
596 (void)options;
598 return 0;
601 #undef NS_SUBMODULE
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;
625 static void
626 NS(test_main)(void *arg)
628 int expected, actual;
629 (void)arg;
631 NS_MOCK(tls_get_write_overhead_ratio);
632 NS_MOCK(we_are_hibernating);
633 NS_MOCK(public_server_mode);
634 NS_MOCK(get_uptime);
635 NS_MOCK(get_bytes_read);
636 NS_MOCK(get_bytes_written);
637 NS_MOCK(logv);
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;
645 expected = 0;
646 actual = log_heartbeat(0);
648 tt_int_op(actual, OP_EQ, expected);
649 tt_int_op(CALLED(logv), OP_EQ, 3);
651 done:
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);
658 NS_UNMOCK(logv);
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));
666 static double
667 NS(tls_get_write_overhead_ratio)(void)
669 return 1.0;
672 static int
673 NS(we_are_hibernating)(void)
675 return 0;
678 static int
679 NS(public_server_mode)(const or_options_t *options)
681 (void)options;
683 return 0;
686 static long
687 NS(get_uptime)(void)
689 return 0;
692 static uint64_t
693 NS(get_bytes_read)(void)
695 return 0;
698 static uint64_t
699 NS(get_bytes_written)(void)
701 return 0;
704 static 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))
710 case 0:
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 */
723 break;
724 case 1:
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 */
743 break;
744 case 2:
745 tt_int_op(severity, OP_EQ, LOG_INFO);
746 break;
747 default:
748 tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
749 break;
752 done:
753 CALLED(logv)++;
756 static int
757 NS(server_mode)(const or_options_t *options)
759 (void)options;
761 return 1;
764 static int
765 NS(accounting_is_enabled)(const or_options_t *options)
767 (void)options;
769 return 1;
772 static time_t
773 NS(accounting_get_end_time)(void)
775 return 60;
778 static or_state_t *
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);
788 #undef NS_SUBMODULE
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));
807 static void
808 NS(test_main)(void *arg)
810 int expected, actual;
811 (void)arg;
813 NS_MOCK(tls_get_write_overhead_ratio);
814 NS_MOCK(we_are_hibernating);
815 NS_MOCK(public_server_mode);
816 NS_MOCK(get_uptime);
817 NS_MOCK(get_bytes_read);
818 NS_MOCK(get_bytes_written);
819 NS_MOCK(logv);
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;
826 expected = 0;
827 actual = log_heartbeat(0);
829 tt_int_op(actual, OP_EQ, expected);
830 tt_int_op(CALLED(logv), OP_EQ, 2);
832 done:
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);
841 NS_UNMOCK(logv);
842 NS_UNMOCK(server_mode);
843 NS_UNMOCK(accounting_is_enabled);
846 static double
847 NS(tls_get_write_overhead_ratio)(void)
849 return 1.0;
852 static int
853 NS(we_are_hibernating)(void)
855 return 0;
858 static int
859 NS(public_server_mode)(const or_options_t *options)
861 (void)options;
863 return 0;
866 static long
867 NS(get_uptime)(void)
869 return 0;
872 static uint64_t
873 NS(get_bytes_read)(void)
875 return 0;
878 static uint64_t
879 NS(get_bytes_written)(void)
881 return 0;
884 static 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))
890 case 0:
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 */
903 break;
904 case 1:
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);
914 break;
915 default:
916 tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
917 break;
920 done:
921 CALLED(logv)++;
924 static int
925 NS(server_mode)(const or_options_t *options)
927 (void)options;
929 return 0;
932 static int
933 NS(accounting_is_enabled)(const or_options_t *options)
935 (void)options;
937 return 0;
940 #undef NS_SUBMODULE
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));
959 static void
960 NS(test_main)(void *arg)
962 int expected, actual;
963 (void)arg;
965 NS_MOCK(tls_get_write_overhead_ratio);
966 NS_MOCK(we_are_hibernating);
967 NS_MOCK(public_server_mode);
968 NS_MOCK(get_uptime);
969 NS_MOCK(get_bytes_read);
970 NS_MOCK(get_bytes_written);
971 NS_MOCK(logv);
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;
977 expected = 0;
978 actual = log_heartbeat(0);
980 tt_int_op(actual, OP_EQ, expected);
981 tt_int_op(CALLED(logv), OP_EQ, 2);
983 done:
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);
990 NS_UNMOCK(logv);
991 NS_UNMOCK(server_mode);
992 NS_UNMOCK(accounting_is_enabled);
995 static double
996 NS(tls_get_write_overhead_ratio)(void)
998 return 2.0;
1001 static int
1002 NS(we_are_hibernating)(void)
1004 return 0;
1007 static int
1008 NS(public_server_mode)(const or_options_t *options)
1010 (void)options;
1012 return 0;
1015 static long
1016 NS(get_uptime)(void)
1018 return 0;
1021 static uint64_t
1022 NS(get_bytes_read)(void)
1024 return 0;
1027 static uint64_t
1028 NS(get_bytes_written)(void)
1030 return 0;
1033 static 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))
1039 case 0:
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 */
1052 break;
1053 case 1:
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);
1063 break;
1064 default:
1065 tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
1066 break;
1069 done:
1070 CALLED(logv)++;
1073 static int
1074 NS(server_mode)(const or_options_t *options)
1076 (void)options;
1078 return 0;
1081 static int
1082 NS(accounting_is_enabled)(const or_options_t *options)
1084 (void)options;
1086 return 0;
1089 #undef NS_SUBMODULE
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),
1101 END_OF_TESTCASES