tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

test_status.c (25947B)


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