aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2014-01-09 12:47:24 -0500
committerNick Mathewson <nickm@torproject.org>2014-02-12 12:48:11 -0500
commit52d222aafbc21d674624fdd4c8fc834a40af69c7 (patch)
treefc1664f9cfb3797ce7041635372fea08780b9ec3 /src/test
parentd379fc6e0ffce916753e5ef1ac0783703d150fa5 (diff)
downloadtor-52d222aafbc21d674624fdd4c8fc834a40af69c7.tar
tor-52d222aafbc21d674624fdd4c8fc834a40af69c7.tar.gz
Add tests for buffer time tracking.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/test_buffers.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/test/test_buffers.c b/src/test/test_buffers.c
index ab1e5dfca..a410770b0 100644
--- a/src/test/test_buffers.c
+++ b/src/test/test_buffers.c
@@ -516,6 +516,78 @@ test_buffer_allocation_tracking(void *arg)
buf_shrink_freelists(1);
}
+static void
+test_buffer_time_tracking(void *arg)
+{
+ buf_t *buf=NULL, *buf2=NULL;
+ struct timeval tv0;
+ const time_t START = 1389288246;
+ const uint32_t START_MSEC = (uint32_t) ((uint64_t)START * 1000);
+ int i;
+ char tmp[4096];
+ (void)arg;
+
+ crypto_rand(tmp, sizeof(tmp));
+
+ tv0.tv_sec = START;
+ tv0.tv_usec = 0;
+
+ buf = buf_new_with_capacity(3000); /* rounds up to next power of 2. */
+ tt_assert(buf);
+
+ /* Empty buffer means the timestamp is 0. */
+ tt_int_op(0, ==, buf_get_oldest_chunk_timestamp(buf, START_MSEC));
+ tt_int_op(0, ==, buf_get_oldest_chunk_timestamp(buf, START_MSEC+1000));
+
+ tor_gettimeofday_cache_set(&tv0);
+ write_to_buf("ABCDEFG", 7, buf);
+ tt_int_op(1000, ==, buf_get_oldest_chunk_timestamp(buf, START_MSEC+1000));
+
+ buf2 = buf_copy(buf);
+ tt_assert(buf2);
+ tt_int_op(1234, ==, buf_get_oldest_chunk_timestamp(buf2, START_MSEC+1234));
+
+ /* Now add more bytes; enough to overflow the first chunk. */
+ tv0.tv_usec += 123 * 1000;
+ tor_gettimeofday_cache_set(&tv0);
+ for (i = 0; i < 600; ++i)
+ write_to_buf("ABCDEFG", 7, buf);
+ tt_int_op(4207, ==, buf_datalen(buf));
+
+ /* The oldest bytes are still in the front. */
+ tt_int_op(2000, ==, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2000));
+
+ /* Once those bytes are dropped, the chunk is still on the first
+ * timestamp. */
+ fetch_from_buf(tmp, 100, buf);
+ tt_int_op(2000, ==, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2000));
+
+ /* But once we discard the whole first chunk, we get the data in the second
+ * chunk. */
+ fetch_from_buf(tmp, 4000, buf);
+ tt_int_op(107, ==, buf_datalen(buf));
+ tt_int_op(2000, ==, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2123));
+
+ /* This time we'll be grabbing a chunk from the freelist, and making sure
+ its time gets updated */
+ tv0.tv_sec += 5;
+ tv0.tv_usec = 617*1000;
+ tor_gettimeofday_cache_set(&tv0);
+ for (i = 0; i < 600; ++i)
+ write_to_buf("ABCDEFG", 7, buf);
+ tt_int_op(4307, ==, buf_datalen(buf));
+
+ tt_int_op(2000, ==, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2123));
+ fetch_from_buf(tmp, 4000, buf);
+ fetch_from_buf(tmp, 306, buf);
+ tt_int_op(0, ==, buf_get_oldest_chunk_timestamp(buf, START_MSEC+5617));
+ tt_int_op(383, ==, buf_get_oldest_chunk_timestamp(buf, START_MSEC+6000));
+
+ done:
+ buf_free(buf);
+ buf_free(buf2);
+}
+
struct testcase_t buffer_tests[] = {
{ "basic", test_buffers_basic, TT_FORK, NULL, NULL },
{ "copy", test_buffer_copy, TT_FORK, NULL, NULL },
@@ -523,6 +595,7 @@ struct testcase_t buffer_tests[] = {
{ "ext_or_cmd", test_buffer_ext_or_cmd, TT_FORK, NULL, NULL },
{ "allocation_tracking", test_buffer_allocation_tracking, TT_FORK,
NULL, NULL },
+ { "time_tracking", test_buffer_time_tracking, TT_FORK, NULL, NULL },
END_OF_TESTCASES
};