LCOV - code coverage report
Current view: top level - test - test_compat_libevent.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 93 94 98.9 %
Date: 2021-11-24 03:28:48 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /* Copyright (c) 2010-2021, The Tor Project, Inc. */
       2             : /* See LICENSE for licensing information */
       3             : 
       4             : #define COMPAT_LIBEVENT_PRIVATE
       5             : #include "orconfig.h"
       6             : #include "core/or/or.h"
       7             : 
       8             : #include "test/test.h"
       9             : 
      10             : #include "lib/evloop/compat_libevent.h"
      11             : 
      12             : #include <event2/event.h>
      13             : 
      14             : #include "test/log_test_helpers.h"
      15             : 
      16             : static void
      17           1 : test_compat_libevent_logging_callback(void *ignored)
      18             : {
      19           1 :   (void)ignored;
      20           1 :   setup_full_capture_of_logs(LOG_DEBUG);
      21             : 
      22           1 :   libevent_logging_callback(_EVENT_LOG_DEBUG, "hello world");
      23           1 :   expect_log_msg("Message from libevent: hello world\n");
      24           1 :   expect_log_severity(LOG_DEBUG);
      25           1 :   tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
      26             : 
      27           1 :   mock_clean_saved_logs();
      28           1 :   libevent_logging_callback(_EVENT_LOG_MSG, "hello world another time");
      29           1 :   expect_log_msg("Message from libevent: hello world another time\n");
      30           1 :   expect_log_severity(LOG_INFO);
      31           1 :   tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
      32             : 
      33           1 :   mock_clean_saved_logs();
      34           1 :   libevent_logging_callback(_EVENT_LOG_WARN, "hello world a third time");
      35           1 :   expect_log_msg("Warning from libevent: hello world a third time\n");
      36           1 :   expect_log_severity(LOG_WARN);
      37           1 :   tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
      38             : 
      39           1 :   mock_clean_saved_logs();
      40           1 :   libevent_logging_callback(_EVENT_LOG_ERR, "hello world a fourth time");
      41           1 :   expect_log_msg("Error from libevent: hello world a fourth time\n");
      42           1 :   expect_log_severity(LOG_ERR);
      43           1 :   tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
      44             : 
      45           1 :   mock_clean_saved_logs();
      46           1 :   libevent_logging_callback(42, "hello world a fifth time");
      47           1 :   expect_log_msg("Message [42] from libevent: hello world a fifth time\n");
      48           1 :   expect_log_severity(LOG_WARN);
      49           1 :   tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
      50             : 
      51           1 :   mock_clean_saved_logs();
      52           1 :   libevent_logging_callback(_EVENT_LOG_DEBUG,
      53             :                             "012345678901234567890123456789"
      54             :                             "012345678901234567890123456789"
      55             :                             "012345678901234567890123456789"
      56             :                             "012345678901234567890123456789"
      57             :                             "012345678901234567890123456789"
      58             :                             "012345678901234567890123456789"
      59             :                             "012345678901234567890123456789"
      60             :                             "012345678901234567890123456789"
      61             :                             "012345678901234567890123456789"
      62             :                             "012345678901234567890123456789"
      63             :                             "012345678901234567890123456789"
      64             :                             "012345678901234567890123456789"
      65             :                             );
      66           1 :   expect_log_msg("Message from libevent: "
      67             :                             "012345678901234567890123456789"
      68             :                             "012345678901234567890123456789"
      69             :                             "012345678901234567890123456789"
      70             :                             "012345678901234567890123456789"
      71             :                             "012345678901234567890123456789"
      72             :                             "012345678901234567890123456789"
      73             :                             "012345678901234567890123456789"
      74             :                             "012345678901234567890123456789"
      75             :                             "012345678901234567890123456789"
      76             :                             "012345678901234567890123456789"
      77             :                             "012345678901234567890123456789"
      78           1 :             "012345678901234567890123456789\n");
      79           1 :   expect_log_severity(LOG_DEBUG);
      80           1 :   tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
      81             : 
      82           1 :   mock_clean_saved_logs();
      83           1 :   libevent_logging_callback(42, "xxx\n");
      84           1 :   expect_log_msg("Message [42] from libevent: xxx\n");
      85           1 :   expect_log_severity(LOG_WARN);
      86           1 :   tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
      87             : 
      88           1 :   suppress_libevent_log_msg("something");
      89           1 :   mock_clean_saved_logs();
      90           1 :   libevent_logging_callback(_EVENT_LOG_MSG, "hello there");
      91           1 :   expect_log_msg("Message from libevent: hello there\n");
      92           1 :   expect_log_severity(LOG_INFO);
      93           1 :   tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
      94             : 
      95           1 :   mock_clean_saved_logs();
      96           1 :   libevent_logging_callback(_EVENT_LOG_MSG, "hello there something else");
      97           1 :   expect_no_log_msg("hello there something else");
      98           1 :   if (mock_saved_logs())
      99           0 :     tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 0);
     100             : 
     101             :   // No way of verifying the result of this, it seems =/
     102           1 :   configure_libevent_logging();
     103             : 
     104           1 :  done:
     105           1 :   suppress_libevent_log_msg(NULL);
     106           1 :   teardown_capture_of_logs();
     107           1 : }
     108             : 
     109             : static void
     110           1 : test_compat_libevent_header_version(void *ignored)
     111             : {
     112           1 :   (void)ignored;
     113           1 :   const char *res;
     114             : 
     115           1 :   res = tor_libevent_get_header_version_str();
     116           1 :   tt_str_op(res, OP_EQ, LIBEVENT_VERSION);
     117             : 
     118           1 :  done:
     119           1 :   (void)0;
     120           1 : }
     121             : 
     122             : /* Test for postloop events */
     123             : 
     124             : /* Event callback to increment a counter. */
     125             : static void
     126           5 : increment_int_counter_cb(periodic_timer_t *timer, void *arg)
     127             : {
     128           5 :   (void)timer;
     129           5 :   int *ctr = arg;
     130           5 :   ++*ctr;
     131           5 : }
     132             : 
     133             : static int activated_counter = 0;
     134             : 
     135             : /* Mainloop event callback to activate another mainloop event */
     136             : static void
     137      104570 : activate_event_cb(mainloop_event_t *ev, void *arg)
     138             : {
     139      104570 :   (void)ev;
     140      104570 :   mainloop_event_t **other_event = arg;
     141      104570 :   mainloop_event_activate(*other_event);
     142      104570 :   ++activated_counter;
     143      104570 : }
     144             : 
     145             : static void
     146           1 : test_compat_libevent_postloop_events(void *arg)
     147             : {
     148           1 :   (void)arg;
     149           1 :   mainloop_event_t *a = NULL, *b = NULL;
     150           1 :   periodic_timer_t *timed = NULL;
     151             : 
     152             :   /* If postloop events don't work, then these events will activate one
     153             :    * another ad infinitum and, and the periodic event will never occur. */
     154           1 :   b = mainloop_event_postloop_new(activate_event_cb, &a);
     155           1 :   a = mainloop_event_postloop_new(activate_event_cb, &b);
     156             : 
     157           1 :   int counter = 0;
     158           1 :   struct timeval fifty_ms = { 0, 10 * 1000 };
     159           1 :   timed = periodic_timer_new(tor_libevent_get_base(), &fifty_ms,
     160             :                              increment_int_counter_cb, &counter);
     161             : 
     162           1 :   mainloop_event_activate(a);
     163      104570 :   int r;
     164      104570 :   do {
     165      104570 :     r = tor_libevent_run_event_loop(tor_libevent_get_base(), 0);
     166      104570 :     if (r == -1)
     167             :       break;
     168      104570 :   } while (counter < 5);
     169             : 
     170           1 :   tt_int_op(activated_counter, OP_GE, 2);
     171             : 
     172           1 :  done:
     173           1 :   mainloop_event_free(a);
     174           1 :   mainloop_event_free(b);
     175           1 :   periodic_timer_free(timed);
     176           1 : }
     177             : 
     178             : struct testcase_t compat_libevent_tests[] = {
     179             :   { "logging_callback", test_compat_libevent_logging_callback,
     180             :     TT_FORK, NULL, NULL },
     181             :   { "header_version", test_compat_libevent_header_version, 0, NULL, NULL },
     182             :   { "postloop_events", test_compat_libevent_postloop_events,
     183             :     TT_FORK, NULL, NULL },
     184             :   END_OF_TESTCASES
     185             : };

Generated by: LCOV version 1.14