summaryrefslogtreecommitdiff
path: root/src/test/test.c
diff options
context:
space:
mode:
authorKarsten Loesing <karsten.loesing@gmx.net>2010-08-11 14:13:08 +0200
committerKarsten Loesing <karsten.loesing@gmx.net>2010-08-11 16:19:54 +0200
commitacd25558b825f5d1254738fc45f5c7f0ccb3fb94 (patch)
treec3da824f4215d8affb30936549f26efd6c2a8f26 /src/test/test.c
parent863b6c439e6bfc64f602ecb9526940fa966ca9fe (diff)
downloadtor-acd25558b825f5d1254738fc45f5c7f0ccb3fb94.tar.gz
tor-acd25558b825f5d1254738fc45f5c7f0ccb3fb94.zip
Refactor exit port statistics code and add unit tests.
Diffstat (limited to 'src/test/test.c')
-rw-r--r--src/test/test.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/test/test.c b/src/test/test.c
index 9948ecf992..ff166cef25 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -1150,6 +1150,57 @@ test_geoip(void)
tor_free(s);
}
+/** Run unit tests for stats code. */
+static void
+test_stats(void)
+{
+ time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
+ char *s = NULL;
+
+ /* We shouldn't collect exit stats without initializing them. */
+ rep_hist_note_exit_stream_opened(80);
+ rep_hist_note_exit_bytes(80, 100, 10000);
+ s = rep_hist_exit_stats_history(now + 86400);
+ test_assert(!s);
+
+ /* Initialize stats, note some streams and bytes, and generate history
+ * string. */
+ rep_hist_exit_stats_init(now);
+ rep_hist_note_exit_stream_opened(80);
+ rep_hist_note_exit_bytes(80, 100, 10000);
+ rep_hist_note_exit_stream_opened(443);
+ rep_hist_note_exit_bytes(443, 100, 10000);
+ rep_hist_note_exit_bytes(443, 100, 10000);
+ s = rep_hist_exit_stats_history(now + 86400);
+ test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
+ "exit-kibibytes-written 80=1,443=1,other=0\n"
+ "exit-kibibytes-read 80=10,443=20,other=0\n"
+ "exit-streams-opened 80=4,443=4,other=0\n", s);
+ tor_free(s);
+
+ /* Stop collecting stats, add some bytes, and ensure we don't generate
+ * a history string. */
+ rep_hist_exit_stats_term();
+ rep_hist_note_exit_bytes(80, 100, 10000);
+ s = rep_hist_exit_stats_history(now + 86400);
+ test_assert(!s);
+
+ /* Re-start stats, add some bytes, reset stats, and see what history we
+ * get when observing no streams or bytes at all. */
+ rep_hist_exit_stats_init(now);
+ rep_hist_note_exit_stream_opened(80);
+ rep_hist_note_exit_bytes(80, 100, 10000);
+ rep_hist_reset_exit_stats(now);
+ s = rep_hist_exit_stats_history(now + 86400);
+ test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
+ "exit-kibibytes-written other=0\n"
+ "exit-kibibytes-read other=0\n"
+ "exit-streams-opened other=0\n", s);
+
+ done:
+ tor_free(s);
+}
+
static void *
legacy_test_setup(const struct testcase_t *testcase)
{
@@ -1190,6 +1241,7 @@ static struct testcase_t test_array[] = {
ENT(policies),
ENT(rend_fns),
ENT(geoip),
+ ENT(stats),
DISABLED(bench_aes),
DISABLED(bench_dmap),