tor/src/test/test_compat_libevent.c
Nick Mathewson 23c77f79fd Remove all usage of the NS*() macros in test*.c
This is an automatically generated commit, made with the following
kludgey perl script. It results in a number of wide lines, which
I'll clean up in a subsequent commit.

#/usr/bin/perl -w -i
$mod = "NS_MODULE";
$submod = "NS_SUBMODULE";
$last_was_empty = 0;

while (<>) {

    s/\bASPECT\(\s*(\w+)\s*,\s*(\w+)\s*\)/$1_$2/;

    if (/# *define +NS_MODULE +(\w+)/) {
        $mod = $1;
        next;
    } elsif (/# *define +NS_SUBMODULE +(\w+)/) {
        $submod = $1;
        next;
    }

    next if (/#undef NS_(SUB)?MODULE/);

    s/NS\(\s*test_main\s*\)/test_${mod}_${submod}/;
    s/NS\(\s*(\w+)\s*\)/${mod}_${submod}_$1/g;
    s/NS_FULL\(\\s*(\w+)\s*,\s*(\w+),\s*(\w+)\s*\)/$1_$2_$3/;
    s/^(\s*)NS_MOCK\(\s*(\w+)\s*\)/$1MOCK($2,\n$1     ${mod}_${submod}_$2)/;
    s/NS_UNMOCK\(\s*(\w+)\s*\)/UNMOCK($1)/;
    s/TEST_CASE\(\s*(\w+)\s*\)/{ "$1", test_${mod}_$1, TT_FORK, NULL, NULL }/;
    s/TEST_CASE_ASPECT\(\s*(\w+)\s*,\s*(\w+)\s*\)/{ "$1_$2", test_${mod}_$1_$2, TT_FORK, NULL, NULL }/;
    s/NS_DECL\(\s*([^,]+)\s*,\s*([^,]+)\s*,\s*(\(.*)\);/static $1 ${mod}_${submod}_$2$3;\nATTR_UNUSED static int ${mod}_${submod}_$2_called = 0;/;
    s/\bCALLED\(\s*(\w+)\s*\)/${mod}_${submod}_$1_called/;

    if (/^$/) {
        print if (! $last_was_empty);
        $last_was_empty = 1;
    } else {
        $last_was_empty = 0;
        print;
    }

    if (eof) {
        $mod = "NS_MODULE";
        $submod = "NS_SUBMODULE";
        $last_was_empty = 0;
    }
}

# Please enter the commit message for your changes. Lines starting
# with '#' will be kept; you may remove them yourself if you want to.
# An empty message aborts the commit.
#
# Date:      Thu Jan 9 10:26:10 2020 -0500
#
# On branch disable_ns_macro
# Changes to be committed:
#	modified:   src/test/test_accounting.c
#	modified:   src/test/test_compat_libevent.c
#	modified:   src/test/test_dir.c
#	modified:   src/test/test_dir_handle_get.c
#	modified:   src/test/test_dns.c
#	modified:   src/test/test_options.c
#	modified:   src/test/test_procmon.c
#	modified:   src/test/test_rendcache.c
#	modified:   src/test/test_router.c
#	modified:   src/test/test_routerset.c
#	modified:   src/test/test_status.c
#	modified:   src/test/test_tortls.c
#	modified:   src/test/test_tortls_openssl.c
#	modified:   src/test/test_util_format.c
#	modified:   src/test/test_util_process.c
#
# Untracked files:
#	experiments/
#	locate_options.sh
#	un_ns.pl
#

# Please enter the commit message for your changes. Lines starting
# with '#' will be kept; you may remove them yourself if you want to.
# An empty message aborts the commit.
#
# Date:      Thu Jan 9 10:26:10 2020 -0500
#
# On branch disable_ns_macro
# Changes to be committed:
#	modified:   src/test/test_accounting.c
#	modified:   src/test/test_compat_libevent.c
#	modified:   src/test/test_dir.c
#	modified:   src/test/test_dir_handle_get.c
#	modified:   src/test/test_dns.c
#	modified:   src/test/test_options.c
#	modified:   src/test/test_procmon.c
#	modified:   src/test/test_rendcache.c
#	modified:   src/test/test_router.c
#	modified:   src/test/test_routerset.c
#	modified:   src/test/test_status.c
#	modified:   src/test/test_tortls.c
#	modified:   src/test/test_tortls_openssl.c
#	modified:   src/test/test_util_format.c
#	modified:   src/test/test_util_process.c
#
# Untracked files:
#	experiments/
#	locate_options.sh
#	un_ns.pl
#
2020-01-09 11:09:22 -05:00

185 lines
6.1 KiB
C

/* Copyright (c) 2010-2020, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#define COMPAT_LIBEVENT_PRIVATE
#include "orconfig.h"
#include "core/or/or.h"
#include "test/test.h"
#include "lib/evloop/compat_libevent.h"
#include <event2/event.h>
#include "test/log_test_helpers.h"
static void
test_compat_libevent_logging_callback(void *ignored)
{
(void)ignored;
setup_full_capture_of_logs(LOG_DEBUG);
libevent_logging_callback(_EVENT_LOG_DEBUG, "hello world");
expect_log_msg("Message from libevent: hello world\n");
expect_log_severity(LOG_DEBUG);
tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
mock_clean_saved_logs();
libevent_logging_callback(_EVENT_LOG_MSG, "hello world another time");
expect_log_msg("Message from libevent: hello world another time\n");
expect_log_severity(LOG_INFO);
tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
mock_clean_saved_logs();
libevent_logging_callback(_EVENT_LOG_WARN, "hello world a third time");
expect_log_msg("Warning from libevent: hello world a third time\n");
expect_log_severity(LOG_WARN);
tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
mock_clean_saved_logs();
libevent_logging_callback(_EVENT_LOG_ERR, "hello world a fourth time");
expect_log_msg("Error from libevent: hello world a fourth time\n");
expect_log_severity(LOG_ERR);
tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
mock_clean_saved_logs();
libevent_logging_callback(42, "hello world a fifth time");
expect_log_msg("Message [42] from libevent: hello world a fifth time\n");
expect_log_severity(LOG_WARN);
tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
mock_clean_saved_logs();
libevent_logging_callback(_EVENT_LOG_DEBUG,
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
);
expect_log_msg("Message from libevent: "
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789"
"012345678901234567890123456789\n");
expect_log_severity(LOG_DEBUG);
tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
mock_clean_saved_logs();
libevent_logging_callback(42, "xxx\n");
expect_log_msg("Message [42] from libevent: xxx\n");
expect_log_severity(LOG_WARN);
tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
suppress_libevent_log_msg("something");
mock_clean_saved_logs();
libevent_logging_callback(_EVENT_LOG_MSG, "hello there");
expect_log_msg("Message from libevent: hello there\n");
expect_log_severity(LOG_INFO);
tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 1);
mock_clean_saved_logs();
libevent_logging_callback(_EVENT_LOG_MSG, "hello there something else");
expect_no_log_msg("hello there something else");
if (mock_saved_logs())
tt_int_op(smartlist_len(mock_saved_logs()), OP_EQ, 0);
// No way of verifying the result of this, it seems =/
configure_libevent_logging();
done:
suppress_libevent_log_msg(NULL);
teardown_capture_of_logs();
}
static void
test_compat_libevent_header_version(void *ignored)
{
(void)ignored;
const char *res;
res = tor_libevent_get_header_version_str();
tt_str_op(res, OP_EQ, LIBEVENT_VERSION);
done:
(void)0;
}
/* Test for postloop events */
/* Event callback to increment a counter. */
static void
increment_int_counter_cb(periodic_timer_t *timer, void *arg)
{
(void)timer;
int *ctr = arg;
++*ctr;
}
static int activated_counter = 0;
/* Mainloop event callback to activate another mainloop event */
static void
activate_event_cb(mainloop_event_t *ev, void *arg)
{
(void)ev;
mainloop_event_t **other_event = arg;
mainloop_event_activate(*other_event);
++activated_counter;
}
static void
test_compat_libevent_postloop_events(void *arg)
{
(void)arg;
mainloop_event_t *a = NULL, *b = NULL;
periodic_timer_t *timed = NULL;
/* If postloop events don't work, then these events will activate one
* another ad infinitum and, and the periodic event will never occur. */
b = mainloop_event_postloop_new(activate_event_cb, &a);
a = mainloop_event_postloop_new(activate_event_cb, &b);
int counter = 0;
struct timeval fifty_ms = { 0, 10 * 1000 };
timed = periodic_timer_new(tor_libevent_get_base(), &fifty_ms,
increment_int_counter_cb, &counter);
mainloop_event_activate(a);
int r;
do {
r = tor_libevent_run_event_loop(tor_libevent_get_base(), 0);
if (r == -1)
break;
} while (counter < 5);
tt_int_op(activated_counter, OP_GE, 2);
done:
mainloop_event_free(a);
mainloop_event_free(b);
periodic_timer_free(timed);
}
struct testcase_t compat_libevent_tests[] = {
{ "logging_callback", test_compat_libevent_logging_callback,
TT_FORK, NULL, NULL },
{ "header_version", test_compat_libevent_header_version, 0, NULL, NULL },
{ "postloop_events", test_compat_libevent_postloop_events,
TT_FORK, NULL, NULL },
END_OF_TESTCASES
};