Prop210: Refactor connection_get_* to produce lists and counts
[tor.git] / src / test / test_status.c
blobcbc8af188cb8af3df2349deaab0b97c24e1d3100
1 #define STATUS_PRIVATE
2 #define HIBERNATE_PRIVATE
3 #define LOG_PRIVATE
4 #define REPHIST_PRIVATE
6 #include <float.h>
7 #include <math.h>
9 #include "or.h"
10 #include "torlog.h"
11 #include "tor_queue.h"
12 #include "status.h"
13 #include "circuitlist.h"
14 #include "config.h"
15 #include "hibernate.h"
16 #include "rephist.h"
17 #include "relay.h"
18 #include "router.h"
19 #include "main.h"
20 #include "nodelist.h"
21 #include "statefile.h"
22 #include "test.h"
24 #define NS_MODULE status
26 #define NS_SUBMODULE count_circuits
29 * Test that count_circuits() is correctly counting the number of
30 * global circuits.
33 static smartlist_t * mock_global_circuitlist = NULL;
35 NS_DECL(smartlist_t *, circuit_get_global_list, (void));
37 static 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;
44 (void)arg;
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);
58 done:
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);
66 static smartlist_t *
67 NS(circuit_get_global_list)(void)
69 return mock_global_circuitlist;
72 #undef NS_SUBMODULE
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.
80 static void
81 NS(test_main)(void *arg)
83 const char *expected;
84 char *actual;
85 (void)arg;
87 expected = "0:00 hours";
88 actual = secs_to_uptime(0);
89 tt_str_op(actual, OP_EQ, expected);
90 tor_free(actual);
92 expected = "0:00 hours";
93 actual = secs_to_uptime(1);
94 tt_str_op(actual, OP_EQ, expected);
95 tor_free(actual);
97 expected = "0:01 hours";
98 actual = secs_to_uptime(60);
99 tt_str_op(actual, OP_EQ, expected);
100 tor_free(actual);
102 expected = "0:59 hours";
103 actual = secs_to_uptime(60 * 59);
104 tt_str_op(actual, OP_EQ, expected);
105 tor_free(actual);
107 expected = "1:00 hours";
108 actual = secs_to_uptime(60 * 60);
109 tt_str_op(actual, OP_EQ, expected);
110 tor_free(actual);
112 expected = "23:59 hours";
113 actual = secs_to_uptime(60 * 60 * 23 + 60 * 59);
114 tt_str_op(actual, OP_EQ, expected);
115 tor_free(actual);
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);
120 tor_free(actual);
122 expected = "1 day 0:00 hours";
123 actual = secs_to_uptime(86400 + 1);
124 tt_str_op(actual, OP_EQ, expected);
125 tor_free(actual);
127 expected = "1 day 0:01 hours";
128 actual = secs_to_uptime(86400 + 60);
129 tt_str_op(actual, OP_EQ, expected);
130 tor_free(actual);
132 expected = "10 days 0:00 hours";
133 actual = secs_to_uptime(86400 * 10);
134 tt_str_op(actual, OP_EQ, expected);
135 tor_free(actual);
137 expected = "10 days 0:00 hours";
138 actual = secs_to_uptime(864000 + 1);
139 tt_str_op(actual, OP_EQ, expected);
140 tor_free(actual);
142 expected = "10 days 0:01 hours";
143 actual = secs_to_uptime(864000 + 60);
144 tt_str_op(actual, OP_EQ, expected);
145 tor_free(actual);
147 done:
148 if (actual != NULL)
149 tor_free(actual);
152 #undef NS_SUBMODULE
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.
161 static void
162 NS(test_main)(void *arg)
164 const char *expected;
165 char *actual;
166 (void)arg;
168 expected = "0 kB";
169 actual = bytes_to_usage(0);
170 tt_str_op(actual, OP_EQ, expected);
171 tor_free(actual);
173 expected = "0 kB";
174 actual = bytes_to_usage(1);
175 tt_str_op(actual, OP_EQ, expected);
176 tor_free(actual);
178 expected = "1 kB";
179 actual = bytes_to_usage(1024);
180 tt_str_op(actual, OP_EQ, expected);
181 tor_free(actual);
183 expected = "1023 kB";
184 actual = bytes_to_usage((1 << 20) - 1);
185 tt_str_op(actual, OP_EQ, expected);
186 tor_free(actual);
188 expected = "1.00 MB";
189 actual = bytes_to_usage((1 << 20));
190 tt_str_op(actual, OP_EQ, expected);
191 tor_free(actual);
193 expected = "1.00 MB";
194 actual = bytes_to_usage((1 << 20) + 5242);
195 tt_str_op(actual, OP_EQ, expected);
196 tor_free(actual);
198 expected = "1.01 MB";
199 actual = bytes_to_usage((1 << 20) + 5243);
200 tt_str_op(actual, OP_EQ, expected);
201 tor_free(actual);
203 expected = "1024.00 MB";
204 actual = bytes_to_usage((1 << 30) - 1);
205 tt_str_op(actual, OP_EQ, expected);
206 tor_free(actual);
208 expected = "1.00 GB";
209 actual = bytes_to_usage((1 << 30));
210 tt_str_op(actual, OP_EQ, expected);
211 tor_free(actual);
213 expected = "1.00 GB";
214 actual = bytes_to_usage((1 << 30) + 5368709);
215 tt_str_op(actual, OP_EQ, expected);
216 tor_free(actual);
218 expected = "1.01 GB";
219 actual = bytes_to_usage((1 << 30) + 5368710);
220 tt_str_op(actual, OP_EQ, expected);
221 tor_free(actual);
223 expected = "10.00 GB";
224 actual = bytes_to_usage((U64_LITERAL(1) << 30) * 10L);
225 tt_str_op(actual, OP_EQ, expected);
226 tor_free(actual);
228 done:
229 if (actual != NULL)
230 tor_free(actual);
233 #undef NS_SUBMODULE
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));
246 static void
247 NS(test_main)(void *arg)
249 int expected, actual;
250 (void)arg;
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);
257 expected = -1;
258 actual = log_heartbeat(0);
260 tt_int_op(actual, OP_EQ, expected);
262 done:
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);
269 static double
270 NS(tls_get_write_overhead_ratio)(void)
272 return 2.0;
275 static int
276 NS(we_are_hibernating)(void)
278 return 0;
281 static int
282 NS(public_server_mode)(const or_options_t *options)
284 (void)options;
286 return 1;
289 static const routerinfo_t *
290 NS(router_get_my_routerinfo)(void)
292 return NULL;
295 #undef NS_SUBMODULE
296 #define NS_SUBMODULE ASPECT(log_heartbeat, not_in_consensus)
299 * Tests that log_heartbeat() logs appropriately if we are not in the cached
300 * consensus.
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];
316 static void
317 NS(test_main)(void *arg)
319 int expected, actual;
320 (void)arg;
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);
327 NS_MOCK(logv);
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;
336 expected = 0;
337 actual = log_heartbeat(0);
339 tt_int_op(actual, OP_EQ, expected);
340 tt_int_op(CALLED(logv), OP_EQ, 5);
342 done:
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);
348 NS_UNMOCK(logv);
349 NS_UNMOCK(server_mode);
350 tor_free(mock_routerinfo);
353 static double
354 NS(tls_get_write_overhead_ratio)(void)
356 return 1.0;
359 static int
360 NS(we_are_hibernating)(void)
362 return 0;
365 static int
366 NS(public_server_mode)(const or_options_t *options)
368 (void)options;
370 return 1;
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;
386 return NULL;
389 static void
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))
395 case 0:
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.");
402 break;
403 case 1:
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 */
416 break;
417 case 2:
418 tt_int_op(severity, OP_EQ, LOG_INFO);
419 break;
420 case 3:
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"),
424 OP_NE, NULL);
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) */
432 break;
433 case 4:
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"),
437 OP_NE, NULL);
438 break;
439 default:
440 tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
441 break;
444 done:
445 CALLED(logv)++;
448 static int
449 NS(server_mode)(const or_options_t *options)
451 (void)options;
453 return 0;
456 #undef NS_SUBMODULE
457 #define NS_SUBMODULE ASPECT(log_heartbeat, simple)
460 * Tests that log_heartbeat() correctly logs heartbeat information
461 * normally.
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;
476 static void
477 NS(test_main)(void *arg)
479 int expected, actual;
480 (void)arg;
482 NS_MOCK(tls_get_write_overhead_ratio);
483 NS_MOCK(we_are_hibernating);
484 NS_MOCK(public_server_mode);
485 NS_MOCK(get_uptime);
486 NS_MOCK(get_bytes_read);
487 NS_MOCK(get_bytes_written);
488 NS_MOCK(logv);
489 NS_MOCK(server_mode);
491 log_global_min_severity_ = LOG_DEBUG;
493 expected = 0;
494 actual = log_heartbeat(0);
496 tt_int_op(actual, OP_EQ, expected);
497 tt_int_op(NS(n_msgs), OP_EQ, 1);
499 done:
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);
506 NS_UNMOCK(logv);
507 NS_UNMOCK(server_mode);
510 static double
511 NS(tls_get_write_overhead_ratio)(void)
513 return 1.0;
516 static int
517 NS(we_are_hibernating)(void)
519 return 1;
522 static int
523 NS(public_server_mode)(const or_options_t *options)
525 (void)options;
527 return 0;
530 static long
531 NS(get_uptime)(void)
533 return 0;
536 static uint64_t
537 NS(get_bytes_read)(void)
539 return 0;
542 static uint64_t
543 NS(get_bytes_written)(void)
545 return 0;
548 static 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)
553 return;
554 ++NS(n_msgs);
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.");
569 done:
573 static int
574 NS(server_mode)(const or_options_t *options)
576 (void)options;
578 return 0;
581 #undef NS_SUBMODULE
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;
605 static void
606 NS(test_main)(void *arg)
608 int expected, actual;
609 (void)arg;
611 NS_MOCK(tls_get_write_overhead_ratio);
612 NS_MOCK(we_are_hibernating);
613 NS_MOCK(public_server_mode);
614 NS_MOCK(get_uptime);
615 NS_MOCK(get_bytes_read);
616 NS_MOCK(get_bytes_written);
617 NS_MOCK(logv);
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;
625 expected = 0;
626 actual = log_heartbeat(0);
628 tt_int_op(actual, OP_EQ, expected);
629 tt_int_op(CALLED(logv), OP_EQ, 3);
631 done:
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);
638 NS_UNMOCK(logv);
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));
646 static double
647 NS(tls_get_write_overhead_ratio)(void)
649 return 1.0;
652 static int
653 NS(we_are_hibernating)(void)
655 return 0;
658 static int
659 NS(public_server_mode)(const or_options_t *options)
661 (void)options;
663 return 0;
666 static long
667 NS(get_uptime)(void)
669 return 0;
672 static uint64_t
673 NS(get_bytes_read)(void)
675 return 0;
678 static uint64_t
679 NS(get_bytes_written)(void)
681 return 0;
684 static 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))
690 case 0:
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 */
703 break;
704 case 1:
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 */
720 break;
721 case 2:
722 tt_int_op(severity, OP_EQ, LOG_INFO);
723 break;
724 default:
725 tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
726 break;
729 done:
730 CALLED(logv)++;
733 static int
734 NS(server_mode)(const or_options_t *options)
736 (void)options;
738 return 1;
741 static int
742 NS(accounting_is_enabled)(const or_options_t *options)
744 (void)options;
746 return 1;
749 static time_t
750 NS(accounting_get_end_time)(void)
752 return 60;
755 static or_state_t *
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);
765 #undef NS_SUBMODULE
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));
784 static void
785 NS(test_main)(void *arg)
787 int expected, actual;
788 (void)arg;
790 NS_MOCK(tls_get_write_overhead_ratio);
791 NS_MOCK(we_are_hibernating);
792 NS_MOCK(public_server_mode);
793 NS_MOCK(get_uptime);
794 NS_MOCK(get_bytes_read);
795 NS_MOCK(get_bytes_written);
796 NS_MOCK(logv);
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;
803 expected = 0;
804 actual = log_heartbeat(0);
806 tt_int_op(actual, OP_EQ, expected);
807 tt_int_op(CALLED(logv), OP_EQ, 2);
809 done:
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);
818 NS_UNMOCK(logv);
819 NS_UNMOCK(server_mode);
820 NS_UNMOCK(accounting_is_enabled);
823 static double
824 NS(tls_get_write_overhead_ratio)(void)
826 return 1.0;
829 static int
830 NS(we_are_hibernating)(void)
832 return 0;
835 static int
836 NS(public_server_mode)(const or_options_t *options)
838 (void)options;
840 return 0;
843 static long
844 NS(get_uptime)(void)
846 return 0;
849 static uint64_t
850 NS(get_bytes_read)(void)
852 return 0;
855 static uint64_t
856 NS(get_bytes_written)(void)
858 return 0;
861 static 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))
867 case 0:
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 */
880 break;
881 case 1:
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);
891 break;
892 default:
893 tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
894 break;
897 done:
898 CALLED(logv)++;
901 static int
902 NS(server_mode)(const or_options_t *options)
904 (void)options;
906 return 0;
909 static int
910 NS(accounting_is_enabled)(const or_options_t *options)
912 (void)options;
914 return 0;
917 #undef NS_SUBMODULE
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));
936 static void
937 NS(test_main)(void *arg)
939 int expected, actual;
940 (void)arg;
942 NS_MOCK(tls_get_write_overhead_ratio);
943 NS_MOCK(we_are_hibernating);
944 NS_MOCK(public_server_mode);
945 NS_MOCK(get_uptime);
946 NS_MOCK(get_bytes_read);
947 NS_MOCK(get_bytes_written);
948 NS_MOCK(logv);
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;
954 expected = 0;
955 actual = log_heartbeat(0);
957 tt_int_op(actual, OP_EQ, expected);
958 tt_int_op(CALLED(logv), OP_EQ, 2);
960 done:
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);
967 NS_UNMOCK(logv);
968 NS_UNMOCK(server_mode);
969 NS_UNMOCK(accounting_is_enabled);
972 static double
973 NS(tls_get_write_overhead_ratio)(void)
975 return 2.0;
978 static int
979 NS(we_are_hibernating)(void)
981 return 0;
984 static int
985 NS(public_server_mode)(const or_options_t *options)
987 (void)options;
989 return 0;
992 static long
993 NS(get_uptime)(void)
995 return 0;
998 static uint64_t
999 NS(get_bytes_read)(void)
1001 return 0;
1004 static uint64_t
1005 NS(get_bytes_written)(void)
1007 return 0;
1010 static 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))
1016 case 0:
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 */
1029 break;
1030 case 1:
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);
1040 break;
1041 default:
1042 tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
1043 break;
1046 done:
1047 CALLED(logv)++;
1050 static int
1051 NS(server_mode)(const or_options_t *options)
1053 (void)options;
1055 return 0;
1058 static int
1059 NS(accounting_is_enabled)(const or_options_t *options)
1061 (void)options;
1063 return 0;
1066 #undef NS_SUBMODULE
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),
1078 END_OF_TESTCASES