From 361014255f5f096b742af56be2df4243febbd24b Mon Sep 17 00:00:00 2001 From: Mike Perry Date: Thu, 14 Sep 2017 01:18:30 +0000 Subject: [PATCH] Bug 23077: Make channelpadding tests use mocked time. --- changes/bug23077 | 4 +++ src/test/test_channelpadding.c | 56 ++++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 changes/bug23077 diff --git a/changes/bug23077 b/changes/bug23077 new file mode 100644 index 000000000..5ed1c5674 --- /dev/null +++ b/changes/bug23077 @@ -0,0 +1,4 @@ + o Minor bugfixes (unit tests): + - Fix a channelpadding unit test failure on extremely slow systems + by using mocked time instead of actual time. Fixes bug 23077; bugfix on + 0.3.1.1-alpha. diff --git a/src/test/test_channelpadding.c b/src/test/test_channelpadding.c index 3b889991b..bdc61d329 100644 --- a/src/test/test_channelpadding.c +++ b/src/test/test_channelpadding.c @@ -1,6 +1,7 @@ #define TOR_CHANNEL_INTERNAL_ #define MAIN_PRIVATE #define NETWORKSTATUS_PRIVATE +#define TOR_TIMERS_PRIVATE #include "or.h" #include "test.h" #include "testsupport.h" @@ -29,6 +30,8 @@ void test_channelpadding_decide_to_pad_channel(void *arg); void dummy_nop_timer(void); +#define NSEC_PER_MSEC (1000*1000) + /* Thing to cast to fake tor_tls_t * to appease assert_connection_ok() */ static int fake_tortls = 0; /* Bleh... */ @@ -350,6 +353,7 @@ test_channelpadding_consensus(void *arg) channelpadding_decision_t decision; or_options_t *options = get_options_mutable(); int64_t val; + int64_t new_time; (void)arg; tor_libevent_postfork(); @@ -372,7 +376,9 @@ test_channelpadding_consensus(void *arg) */ channel_t *chan; routerstatus_t *relay = tor_malloc_zero(sizeof(routerstatus_t)); - monotime_init(); + monotime_enable_test_mocking(); + monotime_set_mock_time_nsec(1); + monotime_coarse_set_mock_time_nsec(1); timers_initialize(); connection_array = smartlist_new(); @@ -399,7 +405,10 @@ test_channelpadding_consensus(void *arg) tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED); // Wait for the timer - event_base_loop(tor_libevent_get_base(), 0); + new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC; + monotime_coarse_set_mock_time_nsec(new_time); + monotime_set_mock_time_nsec(new_time); + timers_run_pending(); tt_int_op(tried_to_write_cell, OP_EQ, 1); tt_assert(!chan->pending_padding_callback); @@ -452,7 +461,10 @@ test_channelpadding_consensus(void *arg) tt_i64_op(val, OP_LE, 200); // Wait for the timer - event_base_loop(tor_libevent_get_base(), 0); + new_time = (monotime_coarse_absolute_msec()+201)*NSEC_PER_MSEC; + monotime_set_mock_time_nsec(new_time); + monotime_coarse_set_mock_time_nsec(new_time); + timers_run_pending(); tt_int_op(tried_to_write_cell, OP_EQ, 1); tt_assert(!chan->pending_padding_callback); @@ -555,6 +567,7 @@ test_channelpadding_consensus(void *arg) tor_free(current_ns_consensus); timers_shutdown(); + monotime_disable_test_mocking(); channel_free_all(); return; @@ -725,12 +738,16 @@ test_channelpadding_decide_to_pad_channel(void *arg) * + We should not send padding */ channel_t *chan; + int64_t new_time; connection_array = smartlist_new(); (void)arg; tor_libevent_postfork(); monotime_init(); + monotime_enable_test_mocking(); + monotime_set_mock_time_nsec(1); + monotime_coarse_set_mock_time_nsec(1); timers_initialize(); setup_full_capture_of_logs(LOG_WARN); channelpadding_new_consensus_params(NULL); @@ -762,7 +779,10 @@ test_channelpadding_decide_to_pad_channel(void *arg) tt_int_op(tried_to_write_cell, OP_EQ, 0); // Wait for the timer from case #2b - event_base_loop(tor_libevent_get_base(), 0); + new_time = (monotime_coarse_absolute_msec() + 1000)*NSEC_PER_MSEC; + monotime_set_mock_time_nsec(new_time); + monotime_coarse_set_mock_time_nsec(new_time); + timers_run_pending(); tt_int_op(tried_to_write_cell, OP_EQ, 1); tt_assert(!chan->pending_padding_callback); @@ -779,7 +799,10 @@ test_channelpadding_decide_to_pad_channel(void *arg) tt_int_op(decision, OP_EQ, CHANNELPADDING_PADDING_ALREADY_SCHEDULED); // Wait for the timer - event_base_loop(tor_libevent_get_base(), 0); + new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC; + monotime_coarse_set_mock_time_nsec(new_time); + monotime_set_mock_time_nsec(new_time); + timers_run_pending(); tt_int_op(tried_to_write_cell, OP_EQ, 1); tt_assert(!chan->pending_padding_callback); @@ -811,7 +834,11 @@ test_channelpadding_decide_to_pad_channel(void *arg) channel_timestamp_active(chan); // We don't expect any timer callbacks here. Make a dummy one to be sure. - dummy_nop_timer(); + // Wait for the timer + new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC; + monotime_coarse_set_mock_time_nsec(new_time); + monotime_set_mock_time_nsec(new_time); + timers_run_pending(); tt_int_op(tried_to_write_cell, OP_EQ, 0); tt_assert(!chan->pending_padding_callback); @@ -828,7 +855,10 @@ test_channelpadding_decide_to_pad_channel(void *arg) chan->state = CHANNEL_STATE_MAINT; // We don't expect any timer callbacks here. Make a dummy one to be sure. - dummy_nop_timer(); + new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC; + monotime_coarse_set_mock_time_nsec(new_time); + monotime_set_mock_time_nsec(new_time); + timers_run_pending(); tt_int_op(tried_to_write_cell, OP_EQ, 0); tt_assert(!chan->pending_padding_callback); @@ -843,7 +873,11 @@ test_channelpadding_decide_to_pad_channel(void *arg) tt_int_op(tried_to_write_cell, OP_EQ, 0); // Wait for the timer - event_base_loop(tor_libevent_get_base(), 0); + new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC; + monotime_coarse_set_mock_time_nsec(new_time); + monotime_set_mock_time_nsec(new_time); + timers_run_pending(); + tt_int_op(tried_to_write_cell, OP_EQ, 1); tt_assert(!chan->pending_padding_callback); @@ -870,7 +904,10 @@ test_channelpadding_decide_to_pad_channel(void *arg) free_fake_channeltls((channel_tls_t*)chan); // We don't expect any timer callbacks here. Make a dummy one to be sure. - dummy_nop_timer(); + new_time = (monotime_coarse_absolute_msec()+101)*NSEC_PER_MSEC; + monotime_coarse_set_mock_time_nsec(new_time); + monotime_set_mock_time_nsec(new_time); + timers_run_pending(); tt_int_op(tried_to_write_cell, OP_EQ, 0); @@ -878,6 +915,7 @@ test_channelpadding_decide_to_pad_channel(void *arg) smartlist_free(connection_array); teardown_capture_of_logs(); + monotime_disable_test_mocking(); timers_shutdown(); channel_free_all();