LCOV - code coverage report
Current view: top level - test - test_status.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 419 429 97.7 %
Date: 2021-11-24 03:28:48 Functions: 51 53 96.2 %

          Line data    Source code
       1             : /* Copyright (c) 2014-2021, The Tor Project, Inc. */
       2             : /* See LICENSE for licensing information */
       3             : 
       4             : #define STATUS_PRIVATE
       5             : #define HIBERNATE_PRIVATE
       6             : #define LOG_PRIVATE
       7             : #define REPHIST_PRIVATE
       8             : 
       9             : #include "orconfig.h"
      10             : 
      11             : #include <float.h>
      12             : #include <math.h>
      13             : 
      14             : #include "core/or/or.h"
      15             : #include "lib/log/log.h"
      16             : #include "tor_queue.h"
      17             : #include "core/or/status.h"
      18             : #include "core/or/circuitlist.h"
      19             : #include "app/config/config.h"
      20             : #include "feature/hibernate/hibernate.h"
      21             : #include "feature/stats/rephist.h"
      22             : #include "core/or/relay.h"
      23             : #include "feature/relay/router.h"
      24             : #include "feature/relay/routermode.h"
      25             : #include "core/mainloop/mainloop.h"
      26             : #include "feature/nodelist/nodelist.h"
      27             : #include "app/config/statefile.h"
      28             : #include "lib/tls/tortls.h"
      29             : #include "test/log_test_helpers.h"
      30             : 
      31             : #include "core/or/origin_circuit_st.h"
      32             : #include "app/config/or_state_st.h"
      33             : #include "feature/nodelist/routerinfo_st.h"
      34             : 
      35             : #include "test/test.h"
      36             : 
      37             : /*
      38             :  * Test that count_circuits() is correctly counting the number of
      39             :  * global circuits.
      40             :  */
      41             : 
      42             : static smartlist_t * mock_global_circuitlist = NULL;
      43             : 
      44             : static smartlist_t * status_count_circuits_circuit_get_global_list(void);
      45             : 
      46             : static void
      47           1 : test_status_count_circuits(void *arg)
      48             : {
      49             :   /* Choose origin_circuit_t wlog. */
      50           1 :   origin_circuit_t *mock_circuit1, *mock_circuit2;
      51           1 :   int expected_circuits = 2, actual_circuits;
      52             : 
      53           1 :   (void)arg;
      54             : 
      55           1 :   mock_circuit1 = tor_malloc_zero(sizeof(origin_circuit_t));
      56           1 :   mock_circuit2 = tor_malloc_zero(sizeof(origin_circuit_t));
      57           1 :   mock_global_circuitlist = smartlist_new();
      58           1 :   smartlist_add(mock_global_circuitlist, TO_CIRCUIT(mock_circuit1));
      59           1 :   smartlist_add(mock_global_circuitlist, TO_CIRCUIT(mock_circuit2));
      60             : 
      61           1 :   MOCK(circuit_get_global_list,
      62             :        status_count_circuits_circuit_get_global_list);
      63             : 
      64           1 :   actual_circuits = count_circuits();
      65             : 
      66           1 :   tt_assert(expected_circuits == actual_circuits);
      67             : 
      68           1 :  done:
      69           1 :   tor_free(mock_circuit1);
      70           1 :   tor_free(mock_circuit2);
      71           1 :   smartlist_free(mock_global_circuitlist);
      72           1 :   mock_global_circuitlist = NULL;
      73           1 :   UNMOCK(circuit_get_global_list);
      74           1 : }
      75             : 
      76             : static smartlist_t *
      77           1 : status_count_circuits_circuit_get_global_list(void)
      78             : {
      79           1 :   return mock_global_circuitlist;
      80             : }
      81             : 
      82             : /*
      83             :  * Test that secs_to_uptime() is converting the number of seconds that
      84             :  * Tor is up for into the appropriate string form containing hours and minutes.
      85             :  */
      86             : 
      87             : static void
      88           1 : test_status_secs_to_uptime(void *arg)
      89             : {
      90           1 :   const char *expected;
      91           1 :   char *actual;
      92           1 :   (void)arg;
      93             : 
      94           1 :   expected = "0:00 hours";
      95           1 :   actual = secs_to_uptime(0);
      96           1 :   tt_str_op(actual, OP_EQ, expected);
      97           1 :   tor_free(actual);
      98             : 
      99           1 :   expected = "0:00 hours";
     100           1 :   actual = secs_to_uptime(1);
     101           1 :   tt_str_op(actual, OP_EQ, expected);
     102           1 :   tor_free(actual);
     103             : 
     104           1 :   expected = "0:01 hours";
     105           1 :   actual = secs_to_uptime(60);
     106           1 :   tt_str_op(actual, OP_EQ, expected);
     107           1 :   tor_free(actual);
     108             : 
     109           1 :   expected = "0:59 hours";
     110           1 :   actual = secs_to_uptime(60 * 59);
     111           1 :   tt_str_op(actual, OP_EQ, expected);
     112           1 :   tor_free(actual);
     113             : 
     114           1 :   expected = "1:00 hours";
     115           1 :   actual = secs_to_uptime(60 * 60);
     116           1 :   tt_str_op(actual, OP_EQ, expected);
     117           1 :   tor_free(actual);
     118             : 
     119           1 :   expected = "23:59 hours";
     120           1 :   actual = secs_to_uptime(60 * 60 * 23 + 60 * 59);
     121           1 :   tt_str_op(actual, OP_EQ, expected);
     122           1 :   tor_free(actual);
     123             : 
     124           1 :   expected = "1 day 0:00 hours";
     125           1 :   actual = secs_to_uptime(60 * 60 * 23 + 60 * 60);
     126           1 :   tt_str_op(actual, OP_EQ, expected);
     127           1 :   tor_free(actual);
     128             : 
     129           1 :   expected = "1 day 0:00 hours";
     130           1 :   actual = secs_to_uptime(86400 + 1);
     131           1 :   tt_str_op(actual, OP_EQ, expected);
     132           1 :   tor_free(actual);
     133             : 
     134           1 :   expected = "1 day 0:01 hours";
     135           1 :   actual = secs_to_uptime(86400 + 60);
     136           1 :   tt_str_op(actual, OP_EQ, expected);
     137           1 :   tor_free(actual);
     138             : 
     139           1 :   expected = "10 days 0:00 hours";
     140           1 :   actual = secs_to_uptime(86400 * 10);
     141           1 :   tt_str_op(actual, OP_EQ, expected);
     142           1 :   tor_free(actual);
     143             : 
     144           1 :   expected = "10 days 0:00 hours";
     145           1 :   actual = secs_to_uptime(864000 + 1);
     146           1 :   tt_str_op(actual, OP_EQ, expected);
     147           1 :   tor_free(actual);
     148             : 
     149           1 :   expected = "10 days 0:01 hours";
     150           1 :   actual = secs_to_uptime(864000 + 60);
     151           1 :   tt_str_op(actual, OP_EQ, expected);
     152           1 :   tor_free(actual);
     153             : 
     154           0 :   done:
     155           1 :     if (actual != NULL)
     156           0 :       tor_free(actual);
     157           1 : }
     158             : 
     159             : /*
     160             :  * Test that bytes_to_usage() is correctly converting the number of bytes that
     161             :  * Tor has read/written into the appropriate string form containing kilobytes,
     162             :  * megabytes, or gigabytes.
     163             :  */
     164             : 
     165             : static void
     166           1 : test_status_bytes_to_usage(void *arg)
     167             : {
     168           1 :   const char *expected;
     169           1 :   char *actual;
     170           1 :   (void)arg;
     171             : 
     172           1 :   expected = "0 kB";
     173           1 :   actual = bytes_to_usage(0);
     174           1 :   tt_str_op(actual, OP_EQ, expected);
     175           1 :   tor_free(actual);
     176             : 
     177           1 :   expected = "0 kB";
     178           1 :   actual = bytes_to_usage(1);
     179           1 :   tt_str_op(actual, OP_EQ, expected);
     180           1 :   tor_free(actual);
     181             : 
     182           1 :   expected = "1 kB";
     183           1 :   actual = bytes_to_usage(1024);
     184           1 :   tt_str_op(actual, OP_EQ, expected);
     185           1 :   tor_free(actual);
     186             : 
     187           1 :   expected = "1023 kB";
     188           1 :   actual = bytes_to_usage((1 << 20) - 1);
     189           1 :   tt_str_op(actual, OP_EQ, expected);
     190           1 :   tor_free(actual);
     191             : 
     192           1 :   expected = "1.00 MB";
     193           1 :   actual = bytes_to_usage((1 << 20));
     194           1 :   tt_str_op(actual, OP_EQ, expected);
     195           1 :   tor_free(actual);
     196             : 
     197           1 :   expected = "1.00 MB";
     198           1 :   actual = bytes_to_usage((1 << 20) + 5242);
     199           1 :   tt_str_op(actual, OP_EQ, expected);
     200           1 :   tor_free(actual);
     201             : 
     202           1 :   expected = "1.01 MB";
     203           1 :   actual = bytes_to_usage((1 << 20) + 5243);
     204           1 :   tt_str_op(actual, OP_EQ, expected);
     205           1 :   tor_free(actual);
     206             : 
     207           1 :   expected = "1024.00 MB";
     208           1 :   actual = bytes_to_usage((1 << 30) - 1);
     209           1 :   tt_str_op(actual, OP_EQ, expected);
     210           1 :   tor_free(actual);
     211             : 
     212           1 :   expected = "1.00 GB";
     213           1 :   actual = bytes_to_usage((1 << 30));
     214           1 :   tt_str_op(actual, OP_EQ, expected);
     215           1 :   tor_free(actual);
     216             : 
     217           1 :   expected = "1.00 GB";
     218           1 :   actual = bytes_to_usage((1 << 30) + 5368709);
     219           1 :   tt_str_op(actual, OP_EQ, expected);
     220           1 :   tor_free(actual);
     221             : 
     222           1 :   expected = "1.01 GB";
     223           1 :   actual = bytes_to_usage((1 << 30) + 5368710);
     224           1 :   tt_str_op(actual, OP_EQ, expected);
     225           1 :   tor_free(actual);
     226             : 
     227           1 :   expected = "10.00 GB";
     228           1 :   actual = bytes_to_usage((UINT64_C(1) << 30) * 10L);
     229           1 :   tt_str_op(actual, OP_EQ, expected);
     230           1 :   tor_free(actual);
     231             : 
     232           0 :   done:
     233           1 :     if (actual != NULL)
     234           0 :       tor_free(actual);
     235           1 : }
     236             : 
     237             : /*
     238             :  * Tests that log_heartbeat() fails when in the public server mode,
     239             :  * not hibernating, and we couldn't get the current routerinfo.
     240             :  */
     241             : 
     242             : static double status_hb_fails_tls_get_write_overhead_ratio(void);
     243             : static int status_hb_fails_we_are_hibernating(void);
     244             : static int status_hb_fails_public_server_mode(const or_options_t *options);
     245             : static const routerinfo_t * status_hb_fails_router_get_my_routerinfo(void);
     246             : 
     247             : static void
     248           1 : test_status_hb_fails(void *arg)
     249             : {
     250           1 :   int expected, actual;
     251           1 :   (void)arg;
     252             : 
     253           1 :   MOCK(tls_get_write_overhead_ratio,
     254             :        status_hb_fails_tls_get_write_overhead_ratio);
     255           1 :   MOCK(we_are_hibernating,
     256             :        status_hb_fails_we_are_hibernating);
     257           1 :   MOCK(public_server_mode,
     258             :        status_hb_fails_public_server_mode);
     259           1 :   MOCK(router_get_my_routerinfo,
     260             :        status_hb_fails_router_get_my_routerinfo);
     261             : 
     262           1 :   expected = -1;
     263           1 :   actual = log_heartbeat(0);
     264             : 
     265           1 :   tt_int_op(actual, OP_EQ, expected);
     266             : 
     267           1 :   done:
     268           1 :     UNMOCK(tls_get_write_overhead_ratio);
     269           1 :     UNMOCK(we_are_hibernating);
     270           1 :     UNMOCK(public_server_mode);
     271           1 :     UNMOCK(router_get_my_routerinfo);
     272           1 : }
     273             : 
     274             : static double
     275           1 : status_hb_fails_tls_get_write_overhead_ratio(void)
     276             : {
     277           1 :   return 2.0;
     278             : }
     279             : 
     280             : static int
     281           1 : status_hb_fails_we_are_hibernating(void)
     282             : {
     283           1 :   return 0;
     284             : }
     285             : 
     286             : static int
     287           1 : status_hb_fails_public_server_mode(const or_options_t *options)
     288             : {
     289           1 :   (void)options;
     290             : 
     291           1 :   return 1;
     292             : }
     293             : 
     294             : static const routerinfo_t *
     295           1 : status_hb_fails_router_get_my_routerinfo(void)
     296             : {
     297           1 :   return NULL;
     298             : }
     299             : 
     300             : /*
     301             :  * Tests that log_heartbeat() logs appropriately if we are not in the cached
     302             :  * consensus.
     303             :  */
     304             : 
     305             : static double status_hb_not_in_consensus_tls_get_write_overhead_ratio(void);
     306             : static int status_hb_not_in_consensus_we_are_hibernating(void);
     307             : static int status_hb_not_in_consensus_public_server_mode(
     308             :              const or_options_t *options);
     309             : static const routerinfo_t *status_hb_not_in_consensus_get_my_routerinfo(void);
     310             : static const node_t * status_hb_not_in_consensus_node_get_by_id(
     311             :              const char *identity_digest);
     312             : static int status_hb_not_in_consensus_server_mode(const or_options_t *options);
     313             : 
     314             : static routerinfo_t *mock_routerinfo;
     315             : 
     316             : static void
     317           1 : test_status_hb_not_in_consensus(void *arg)
     318             : {
     319           1 :   int expected, actual;
     320           1 :   (void)arg;
     321             : 
     322           1 :   MOCK(tls_get_write_overhead_ratio,
     323             :        status_hb_not_in_consensus_tls_get_write_overhead_ratio);
     324           1 :   MOCK(we_are_hibernating,
     325             :        status_hb_not_in_consensus_we_are_hibernating);
     326           1 :   MOCK(public_server_mode,
     327             :        status_hb_not_in_consensus_public_server_mode);
     328           1 :   MOCK(router_get_my_routerinfo,
     329             :        status_hb_not_in_consensus_get_my_routerinfo);
     330           1 :   MOCK(node_get_by_id,
     331             :        status_hb_not_in_consensus_node_get_by_id);
     332           1 :   MOCK(server_mode,
     333             :        status_hb_not_in_consensus_server_mode);
     334             : 
     335           1 :   log_global_min_severity_ = LOG_DEBUG;
     336           1 :   onion_handshakes_requested[ONION_HANDSHAKE_TYPE_TAP] = 1;
     337           1 :   onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_TAP] = 1;
     338           1 :   onion_handshakes_requested[ONION_HANDSHAKE_TYPE_NTOR] = 1;
     339           1 :   onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_NTOR] = 1;
     340             : 
     341           1 :   expected = 0;
     342           1 :   setup_capture_of_logs(LOG_INFO);
     343           1 :   actual = log_heartbeat(0);
     344           1 :   tt_int_op(actual, OP_EQ, expected);
     345             : 
     346           1 :   expect_log_msg("Heartbeat: It seems like we are "
     347           1 :                  "not in the cached consensus.\n");
     348           1 :   expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
     349             :                  "with 0 circuits open. "
     350             :                  "I've sent 0 kB and received 0 kB. "
     351             :                  "I've received 0 connections on IPv4 and 0 on IPv6. "
     352           1 :                  "I've made 0 connections with IPv4 and 0 with IPv6.\n");
     353           1 :   expect_log_msg("Average packaged cell fullness: 100.000%. "
     354           1 :                  "TLS write overhead: 0%\n");
     355           1 :   expect_log_msg("Circuit handshake stats since last time: 1/1 TAP, "
     356           1 :                  "1/1 NTor.\n");
     357           1 :   expect_log_msg("Since startup we initiated 0 and received 0 v1 "
     358             :                  "connections; initiated 0 and received 0 v2 connections; "
     359             :                  "initiated 0 and received 0 v3 connections; "
     360             :                  "initiated 0 and received 0 v4 connections; "
     361           1 :                  "initiated 0 and received 0 v5 connections.\n");
     362           1 :   expect_log_msg("Heartbeat: DoS mitigation since startup: 0 circuits killed "
     363             :                  "with too many cells, [DoSCircuitCreationEnabled disabled], "
     364             :                  "[DoSConnectionEnabled disabled], "
     365             :                  "[DoSRefuseSingleHopClientRendezvous disabled], "
     366           1 :                  "0 INTRODUCE2 rejected.\n");
     367           1 :   tt_int_op(mock_saved_log_n_entries(), OP_EQ, 6);
     368             : 
     369           1 :  done:
     370           1 :     teardown_capture_of_logs();
     371           1 :     UNMOCK(tls_get_write_overhead_ratio);
     372           1 :     UNMOCK(we_are_hibernating);
     373           1 :     UNMOCK(public_server_mode);
     374           1 :     UNMOCK(router_get_my_routerinfo);
     375           1 :     UNMOCK(node_get_by_id);
     376           1 :     UNMOCK(server_mode);
     377           1 :     tor_free(mock_routerinfo);
     378           1 : }
     379             : 
     380             : static double
     381           1 : status_hb_not_in_consensus_tls_get_write_overhead_ratio(void)
     382             : {
     383           1 :   return 1.0;
     384             : }
     385             : 
     386             : static int
     387           1 : status_hb_not_in_consensus_we_are_hibernating(void)
     388             : {
     389           1 :   return 0;
     390             : }
     391             : 
     392             : static int
     393           3 : status_hb_not_in_consensus_public_server_mode(const or_options_t *options)
     394             : {
     395           3 :   (void)options;
     396             : 
     397           3 :   return 1;
     398             : }
     399             : 
     400             : static const routerinfo_t *
     401           1 : status_hb_not_in_consensus_get_my_routerinfo(void)
     402             : {
     403           1 :   mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
     404             : 
     405           1 :   return mock_routerinfo;
     406             : }
     407             : 
     408             : static const node_t *
     409           1 : status_hb_not_in_consensus_node_get_by_id(const char *identity_digest)
     410             : {
     411           1 :   (void)identity_digest;
     412             : 
     413           1 :   return NULL;
     414             : }
     415             : 
     416             : static int
     417           1 : status_hb_not_in_consensus_server_mode(const or_options_t *options)
     418             : {
     419           1 :   (void)options;
     420             : 
     421           1 :   return 0;
     422             : }
     423             : 
     424             : /*
     425             :  * Tests that log_heartbeat() correctly logs heartbeat information
     426             :  * normally.
     427             :  */
     428             : 
     429             : static double status_hb_simple_tls_get_write_overhead_ratio(void);
     430             : static int status_hb_simple_we_are_hibernating(void);
     431             : static int status_hb_simple_public_server_mode(const or_options_t *options);
     432             : static long status_hb_simple_get_uptime(void);
     433             : static uint64_t status_hb_simple_get_bytes_read(void);
     434             : static uint64_t status_hb_simple_get_bytes_written(void);
     435             : static int status_hb_simple_server_mode(const or_options_t *options);
     436             : 
     437             : static void
     438           1 : test_status_hb_simple(void *arg)
     439             : {
     440           1 :   int expected, actual;
     441           1 :   (void)arg;
     442             : 
     443           1 :   MOCK(tls_get_write_overhead_ratio,
     444             :        status_hb_simple_tls_get_write_overhead_ratio);
     445           1 :   MOCK(we_are_hibernating,
     446             :        status_hb_simple_we_are_hibernating);
     447           1 :   MOCK(public_server_mode,
     448             :        status_hb_simple_public_server_mode);
     449           1 :   MOCK(get_uptime,
     450             :        status_hb_simple_get_uptime);
     451           1 :   MOCK(get_bytes_read,
     452             :        status_hb_simple_get_bytes_read);
     453           1 :   MOCK(get_bytes_written,
     454             :        status_hb_simple_get_bytes_written);
     455           1 :   MOCK(server_mode,
     456             :        status_hb_simple_server_mode);
     457             : 
     458           1 :   log_global_min_severity_ = LOG_DEBUG;
     459             : 
     460           1 :   setup_capture_of_logs(LOG_INFO);
     461           1 :   expected = 0;
     462           1 :   actual = log_heartbeat(0);
     463             : 
     464           1 :   tt_int_op(actual, OP_EQ, expected);
     465             : 
     466           1 :   expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
     467             :                  "with 0 circuits open. "
     468             :                  "I've sent 0 kB and received 0 kB. "
     469             :                  "I've received 0 connections on IPv4 and 0 on IPv6. "
     470             :                  "I've made 0 connections with IPv4 and 0 with IPv6. "
     471           1 :                  "We are currently hibernating.\n");
     472             : 
     473           1 :   done:
     474           1 :     teardown_capture_of_logs();
     475           1 :     UNMOCK(tls_get_write_overhead_ratio);
     476           1 :     UNMOCK(we_are_hibernating);
     477           1 :     UNMOCK(public_server_mode);
     478           1 :     UNMOCK(get_uptime);
     479           1 :     UNMOCK(get_bytes_read);
     480           1 :     UNMOCK(get_bytes_written);
     481           1 :     UNMOCK(server_mode);
     482           1 : }
     483             : 
     484             : static double
     485           1 : status_hb_simple_tls_get_write_overhead_ratio(void)
     486             : {
     487           1 :   return 1.0;
     488             : }
     489             : 
     490             : static int
     491           1 : status_hb_simple_we_are_hibernating(void)
     492             : {
     493           1 :   return 1;
     494             : }
     495             : 
     496             : static int
     497           2 : status_hb_simple_public_server_mode(const or_options_t *options)
     498             : {
     499           2 :   (void)options;
     500             : 
     501           2 :   return 0;
     502             : }
     503             : 
     504             : static long
     505           1 : status_hb_simple_get_uptime(void)
     506             : {
     507           1 :   return 0;
     508             : }
     509             : 
     510             : static uint64_t
     511           1 : status_hb_simple_get_bytes_read(void)
     512             : {
     513           1 :   return 0;
     514             : }
     515             : 
     516             : static uint64_t
     517           1 : status_hb_simple_get_bytes_written(void)
     518             : {
     519           1 :   return 0;
     520             : }
     521             : 
     522             : static int
     523           1 : status_hb_simple_server_mode(const or_options_t *options)
     524             : {
     525           1 :   (void)options;
     526             : 
     527           1 :   return 0;
     528             : }
     529             : 
     530             : /*
     531             :  * Tests that log_heartbeat() correctly logs heartbeat information
     532             :  * and accounting information when configured.
     533             :  */
     534             : 
     535             : static double status_hb_calls_log_accounting_tls_get_write_overhead_ratio(
     536             :                        void);
     537             : static int status_hb_calls_log_accounting_we_are_hibernating(void);
     538             : static int status_hb_calls_log_accounting_public_server_mode(
     539             :                       const or_options_t *options);
     540             : static long status_hb_calls_log_accounting_get_uptime(void);
     541             : static uint64_t status_hb_calls_log_accounting_get_bytes_read(void);
     542             : static uint64_t status_hb_calls_log_accounting_get_bytes_written(void);
     543             : static int status_hb_calls_log_accounting_server_mode(
     544             :                       const or_options_t *options);
     545             : static or_state_t * status_hb_calls_log_accounting_get_or_state(void);
     546             : static int status_hb_calls_log_accounting_accounting_is_enabled(
     547             :                       const or_options_t *options);
     548             : static time_t status_hb_calls_log_accounting_accounting_get_end_time(void);
     549             : 
     550             : static or_state_t * status_hb_calls_log_accounting_mock_state = NULL;
     551             : static or_options_t * status_hb_calls_log_accounting_mock_options = NULL;
     552             : 
     553             : static void
     554           1 : test_status_hb_calls_log_accounting(void *arg)
     555             : {
     556           1 :   int expected, actual;
     557           1 :   (void)arg;
     558             : 
     559           1 :   MOCK(tls_get_write_overhead_ratio,
     560             :        status_hb_calls_log_accounting_tls_get_write_overhead_ratio);
     561           1 :   MOCK(we_are_hibernating,
     562             :        status_hb_calls_log_accounting_we_are_hibernating);
     563           1 :   MOCK(public_server_mode,
     564             :        status_hb_calls_log_accounting_public_server_mode);
     565           1 :   MOCK(get_uptime,
     566             :        status_hb_calls_log_accounting_get_uptime);
     567           1 :   MOCK(get_bytes_read,
     568             :        status_hb_calls_log_accounting_get_bytes_read);
     569           1 :   MOCK(get_bytes_written,
     570             :        status_hb_calls_log_accounting_get_bytes_written);
     571           1 :   MOCK(server_mode,
     572             :        status_hb_calls_log_accounting_server_mode);
     573           1 :   MOCK(get_or_state,
     574             :        status_hb_calls_log_accounting_get_or_state);
     575           1 :   MOCK(accounting_is_enabled,
     576             :        status_hb_calls_log_accounting_accounting_is_enabled);
     577           1 :   MOCK(accounting_get_end_time,
     578             :        status_hb_calls_log_accounting_accounting_get_end_time);
     579             : 
     580           1 :   log_global_min_severity_ = LOG_DEBUG;
     581             : 
     582           1 :   setup_capture_of_logs(LOG_NOTICE);
     583           1 :   expected = 0;
     584           1 :   actual = log_heartbeat(0);
     585             : 
     586           1 :   tt_int_op(actual, OP_EQ, expected);
     587             : 
     588           1 :   expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
     589             :                  "with 0 circuits open. "
     590             :                  "I've sent 0 kB and received 0 kB. "
     591             :                  "I've received 0 connections on IPv4 and 0 on IPv6. "
     592           1 :                  "I've made 0 connections with IPv4 and 0 with IPv6.\n");
     593             : 
     594           1 :   expect_log_msg_containing("Heartbeat: Accounting enabled. Sent: 0 kB, "
     595             :                             "Received: 0 kB, Used: 0 kB / 0 kB, Rule: max. "
     596           1 :                             "The current accounting interval ends on ");
     597           1 :   tt_int_op(mock_saved_log_n_entries(), OP_EQ, 2);
     598             : 
     599           1 :   done:
     600           1 :     teardown_capture_of_logs();
     601           1 :     UNMOCK(tls_get_write_overhead_ratio);
     602           1 :     UNMOCK(we_are_hibernating);
     603           1 :     UNMOCK(public_server_mode);
     604           1 :     UNMOCK(get_uptime);
     605           1 :     UNMOCK(get_bytes_read);
     606           1 :     UNMOCK(get_bytes_written);
     607           1 :     UNMOCK(server_mode);
     608           1 :     UNMOCK(accounting_is_enabled);
     609           1 :     UNMOCK(accounting_get_end_time);
     610           1 :     tor_free_(status_hb_calls_log_accounting_mock_state);
     611           1 :     tor_free_(status_hb_calls_log_accounting_mock_options);
     612           1 : }
     613             : 
     614             : static double
     615           1 : status_hb_calls_log_accounting_tls_get_write_overhead_ratio(void)
     616             : {
     617           1 :   return 1.0;
     618             : }
     619             : 
     620             : static int
     621           1 : status_hb_calls_log_accounting_we_are_hibernating(void)
     622             : {
     623           1 :   return 0;
     624             : }
     625             : 
     626             : static int
     627           2 : status_hb_calls_log_accounting_public_server_mode(const or_options_t *options)
     628             : {
     629           2 :   (void)options;
     630             : 
     631           2 :   return 0;
     632             : }
     633             : 
     634             : static long
     635           1 : status_hb_calls_log_accounting_get_uptime(void)
     636             : {
     637           1 :   return 0;
     638             : }
     639             : 
     640             : static uint64_t
     641           1 : status_hb_calls_log_accounting_get_bytes_read(void)
     642             : {
     643           1 :   return 0;
     644             : }
     645             : 
     646             : static uint64_t
     647           1 : status_hb_calls_log_accounting_get_bytes_written(void)
     648             : {
     649           1 :   return 0;
     650             : }
     651             : 
     652             : static int
     653           1 : status_hb_calls_log_accounting_server_mode(const or_options_t *options)
     654             : {
     655           1 :   (void)options;
     656             : 
     657           1 :   return 1;
     658             : }
     659             : 
     660             : static int
     661           1 : status_hb_calls_log_accounting_accounting_is_enabled(
     662             :                                         const or_options_t *options)
     663             : {
     664           1 :   (void)options;
     665             : 
     666           1 :   return 1;
     667             : }
     668             : 
     669             : static time_t
     670           1 : status_hb_calls_log_accounting_accounting_get_end_time(void)
     671             : {
     672           1 :   return 60;
     673             : }
     674             : 
     675             : static or_state_t *
     676           1 : status_hb_calls_log_accounting_get_or_state(void)
     677             : {
     678           2 :   status_hb_calls_log_accounting_mock_state =
     679           1 :     tor_malloc_zero(sizeof(or_state_t));
     680           1 :   status_hb_calls_log_accounting_mock_state
     681           1 :     ->AccountingBytesReadInInterval = 0;
     682           1 :   status_hb_calls_log_accounting_mock_state
     683           1 :     ->AccountingBytesWrittenInInterval = 0;
     684             : 
     685           1 :   return status_hb_calls_log_accounting_mock_state;
     686             : }
     687             : 
     688             : /*
     689             :  * Tests that log_heartbeat() correctly logs packaged cell
     690             :  * fullness information.
     691             :  */
     692             : 
     693             : static double status_hb_packaged_cell_fullness_tls_get_write_overhead_ratio(
     694             :                           void);
     695             : static int status_hb_packaged_cell_fullness_we_are_hibernating(void);
     696             : static int status_hb_packaged_cell_fullness_public_server_mode(
     697             :                           const or_options_t *options);
     698             : static long status_hb_packaged_cell_fullness_get_uptime(void);
     699             : static uint64_t status_hb_packaged_cell_fullness_get_bytes_read(void);
     700             : static uint64_t status_hb_packaged_cell_fullness_get_bytes_written(void);
     701             : static int status_hb_packaged_cell_fullness_server_mode(
     702             :                           const or_options_t *options);
     703             : static int status_hb_packaged_cell_fullness_accounting_is_enabled(
     704             :                           const or_options_t *options);
     705             : 
     706             : static void
     707           1 : test_status_hb_packaged_cell_fullness(void *arg)
     708             : {
     709           1 :   int expected, actual;
     710           1 :   (void)arg;
     711             : 
     712           1 :   MOCK(tls_get_write_overhead_ratio,
     713             :        status_hb_packaged_cell_fullness_tls_get_write_overhead_ratio);
     714           1 :   MOCK(we_are_hibernating,
     715             :        status_hb_packaged_cell_fullness_we_are_hibernating);
     716           1 :   MOCK(public_server_mode,
     717             :        status_hb_packaged_cell_fullness_public_server_mode);
     718           1 :   MOCK(get_uptime,
     719             :        status_hb_packaged_cell_fullness_get_uptime);
     720           1 :   MOCK(get_bytes_read,
     721             :        status_hb_packaged_cell_fullness_get_bytes_read);
     722           1 :   MOCK(get_bytes_written,
     723             :        status_hb_packaged_cell_fullness_get_bytes_written);
     724           1 :   MOCK(server_mode,
     725             :        status_hb_packaged_cell_fullness_server_mode);
     726           1 :   MOCK(accounting_is_enabled,
     727             :        status_hb_packaged_cell_fullness_accounting_is_enabled);
     728           1 :   log_global_min_severity_ = LOG_DEBUG;
     729             : 
     730           1 :   stats_n_data_bytes_packaged = RELAY_PAYLOAD_SIZE;
     731           1 :   stats_n_data_cells_packaged = 2;
     732           1 :   expected = 0;
     733           1 :   setup_capture_of_logs(LOG_INFO);
     734           1 :   actual = log_heartbeat(0);
     735             : 
     736           1 :   tt_int_op(actual, OP_EQ, expected);
     737           1 :   expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
     738             :                  "with 0 circuits open. "
     739             :                  "I've sent 0 kB and received 0 kB. "
     740             :                  "I've received 0 connections on IPv4 and 0 on IPv6. "
     741           1 :                  "I've made 0 connections with IPv4 and 0 with IPv6.\n");
     742           1 :   expect_log_msg("Average packaged cell fullness: 50.000%. "
     743           1 :                  "TLS write overhead: 0%\n");
     744             : 
     745           1 :   done:
     746           1 :     teardown_capture_of_logs();
     747           1 :     stats_n_data_bytes_packaged = 0;
     748           1 :     stats_n_data_cells_packaged = 0;
     749           1 :     UNMOCK(tls_get_write_overhead_ratio);
     750           1 :     UNMOCK(we_are_hibernating);
     751           1 :     UNMOCK(public_server_mode);
     752           1 :     UNMOCK(get_uptime);
     753           1 :     UNMOCK(get_bytes_read);
     754           1 :     UNMOCK(get_bytes_written);
     755           1 :     UNMOCK(server_mode);
     756           1 :     UNMOCK(accounting_is_enabled);
     757           1 : }
     758             : 
     759             : static double
     760           1 : status_hb_packaged_cell_fullness_tls_get_write_overhead_ratio(void)
     761             : {
     762           1 :   return 1.0;
     763             : }
     764             : 
     765             : static int
     766           1 : status_hb_packaged_cell_fullness_we_are_hibernating(void)
     767             : {
     768           1 :   return 0;
     769             : }
     770             : 
     771             : static int
     772           2 : status_hb_packaged_cell_fullness_public_server_mode(
     773             :                                      const or_options_t *options)
     774             : {
     775           2 :   (void)options;
     776             : 
     777           2 :   return 0;
     778             : }
     779             : 
     780             : static long
     781           1 : status_hb_packaged_cell_fullness_get_uptime(void)
     782             : {
     783           1 :   return 0;
     784             : }
     785             : 
     786             : static uint64_t
     787           1 : status_hb_packaged_cell_fullness_get_bytes_read(void)
     788             : {
     789           1 :   return 0;
     790             : }
     791             : 
     792             : static uint64_t
     793           1 : status_hb_packaged_cell_fullness_get_bytes_written(void)
     794             : {
     795           1 :   return 0;
     796             : }
     797             : 
     798             : static int
     799           1 : status_hb_packaged_cell_fullness_server_mode(const or_options_t *options)
     800             : {
     801           1 :   (void)options;
     802             : 
     803           1 :   return 0;
     804             : }
     805             : 
     806             : static int
     807           0 : status_hb_packaged_cell_fullness_accounting_is_enabled(
     808             :                                              const or_options_t *options)
     809             : {
     810           0 :   (void)options;
     811             : 
     812           0 :   return 0;
     813             : }
     814             : 
     815             : /*
     816             :  * Tests that log_heartbeat() correctly logs the TLS write overhead information
     817             :  * when the TLS write overhead ratio exceeds 1.
     818             :  */
     819             : 
     820             : static double status_hb_tls_write_overhead_tls_get_write_overhead_ratio(void);
     821             : static int status_hb_tls_write_overhead_we_are_hibernating(void);
     822             : static int status_hb_tls_write_overhead_public_server_mode(
     823             :                const or_options_t *options);
     824             : static long status_hb_tls_write_overhead_get_uptime(void);
     825             : static uint64_t status_hb_tls_write_overhead_get_bytes_read(void);
     826             : static uint64_t status_hb_tls_write_overhead_get_bytes_written(void);
     827             : static int status_hb_tls_write_overhead_server_mode(
     828             :                const or_options_t *options);
     829             : static int status_hb_tls_write_overhead_accounting_is_enabled(
     830             :                const or_options_t *options);
     831             : 
     832             : static void
     833           1 : test_status_hb_tls_write_overhead(void *arg)
     834             : {
     835           1 :   int expected, actual;
     836           1 :   (void)arg;
     837             : 
     838           1 :   MOCK(tls_get_write_overhead_ratio,
     839             :        status_hb_tls_write_overhead_tls_get_write_overhead_ratio);
     840           1 :   MOCK(we_are_hibernating,
     841             :        status_hb_tls_write_overhead_we_are_hibernating);
     842           1 :   MOCK(public_server_mode,
     843             :        status_hb_tls_write_overhead_public_server_mode);
     844           1 :   MOCK(get_uptime,
     845             :        status_hb_tls_write_overhead_get_uptime);
     846           1 :   MOCK(get_bytes_read,
     847             :        status_hb_tls_write_overhead_get_bytes_read);
     848           1 :   MOCK(get_bytes_written,
     849             :        status_hb_tls_write_overhead_get_bytes_written);
     850           1 :   MOCK(server_mode,
     851             :        status_hb_tls_write_overhead_server_mode);
     852           1 :   MOCK(accounting_is_enabled,
     853             :        status_hb_tls_write_overhead_accounting_is_enabled);
     854           1 :   stats_n_data_cells_packaged = 0;
     855           1 :   log_global_min_severity_ = LOG_DEBUG;
     856             : 
     857           1 :   expected = 0;
     858           1 :   setup_capture_of_logs(LOG_NOTICE);
     859           1 :   actual = log_heartbeat(0);
     860             : 
     861           1 :   tt_int_op(actual, OP_EQ, expected);
     862           1 :   expect_log_msg("Heartbeat: Tor's uptime is 0:00 hours, "
     863             :                  "with 0 circuits open. "
     864             :                  "I've sent 0 kB and received 0 kB. "
     865             :                  "I've received 0 connections on IPv4 and 0 on IPv6. "
     866           1 :                  "I've made 0 connections with IPv4 and 0 with IPv6.\n");
     867           1 :   expect_log_msg("Average packaged cell fullness: 100.000%. "
     868           1 :                  "TLS write overhead: 100%\n");
     869             : 
     870           1 :   done:
     871           1 :     teardown_capture_of_logs();
     872           1 :     UNMOCK(tls_get_write_overhead_ratio);
     873           1 :     UNMOCK(we_are_hibernating);
     874           1 :     UNMOCK(public_server_mode);
     875           1 :     UNMOCK(get_uptime);
     876           1 :     UNMOCK(get_bytes_read);
     877           1 :     UNMOCK(get_bytes_written);
     878           1 :     UNMOCK(server_mode);
     879           1 :     UNMOCK(accounting_is_enabled);
     880           1 : }
     881             : 
     882             : static double
     883           1 : status_hb_tls_write_overhead_tls_get_write_overhead_ratio(void)
     884             : {
     885           1 :   return 2.0;
     886             : }
     887             : 
     888             : static int
     889           1 : status_hb_tls_write_overhead_we_are_hibernating(void)
     890             : {
     891           1 :   return 0;
     892             : }
     893             : 
     894             : static int
     895           2 : status_hb_tls_write_overhead_public_server_mode(const or_options_t *options)
     896             : {
     897           2 :   (void)options;
     898             : 
     899           2 :   return 0;
     900             : }
     901             : 
     902             : static long
     903           1 : status_hb_tls_write_overhead_get_uptime(void)
     904             : {
     905           1 :   return 0;
     906             : }
     907             : 
     908             : static uint64_t
     909           1 : status_hb_tls_write_overhead_get_bytes_read(void)
     910             : {
     911           1 :   return 0;
     912             : }
     913             : 
     914             : static uint64_t
     915           1 : status_hb_tls_write_overhead_get_bytes_written(void)
     916             : {
     917           1 :   return 0;
     918             : }
     919             : 
     920             : static int
     921           1 : status_hb_tls_write_overhead_server_mode(const or_options_t *options)
     922             : {
     923           1 :   (void)options;
     924             : 
     925           1 :   return 0;
     926             : }
     927             : 
     928             : static int
     929           0 : status_hb_tls_write_overhead_accounting_is_enabled(const or_options_t *options)
     930             : {
     931           0 :   (void)options;
     932             : 
     933           0 :   return 0;
     934             : }
     935             : 
     936             : struct testcase_t status_tests[] = {
     937             :   { "count_circuits", test_status_count_circuits, TT_FORK, NULL, NULL },
     938             :   { "secs_to_uptime", test_status_secs_to_uptime, TT_FORK, NULL, NULL },
     939             :   { "bytes_to_usage", test_status_bytes_to_usage, TT_FORK, NULL, NULL },
     940             :   { "hb_fails", test_status_hb_fails, TT_FORK, NULL, NULL },
     941             :   { "hb_simple", test_status_hb_simple, TT_FORK, NULL, NULL },
     942             :   { "hb_not_in_consensus", test_status_hb_not_in_consensus,
     943             :     TT_FORK, NULL, NULL },
     944             :   { "hb_calls_log_accounting", test_status_hb_calls_log_accounting,
     945             :     TT_FORK, NULL, NULL },
     946             :   { "hb_packaged_cell_fullness", test_status_hb_packaged_cell_fullness,
     947             :     TT_FORK, NULL, NULL },
     948             :   { "hb_tls_write_overhead", test_status_hb_tls_write_overhead,
     949             :     TT_FORK, NULL, NULL },
     950             :   END_OF_TESTCASES
     951             : };

Generated by: LCOV version 1.14