[tor-commits] [tor/master] Clean up bridge-stats code.
nickm at torproject.org
nickm at torproject.org
Thu Aug 4 19:47:09 UTC 2011
commit d690a99dfe65ba164aa3de0662be2a8fedc69b01
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date: Thu Aug 4 14:45:24 2011 +0200
Clean up bridge-stats code.
Only write a bridge-stats string if bridge stats have been
initialized. This behavior is similar to dirreq-stats, entry-stats,
etc.
Also add a few unit tests for the bridge-stats code.
---
src/or/geoip.c | 8 +++++---
src/or/geoip.h | 1 +
src/test/test.c | 23 ++++++++++++++++++++++-
3 files changed, 28 insertions(+), 4 deletions(-)
diff --git a/src/or/geoip.c b/src/or/geoip.c
index e525b2a..67dea96 100644
--- a/src/or/geoip.c
+++ b/src/or/geoip.c
@@ -1197,8 +1197,8 @@ static char *bridge_stats_extrainfo = NULL;
/** Return a newly allocated string holding our bridge usage stats by country
* in a format suitable for inclusion in an extrainfo document. Return NULL on
* failure. */
-static char *
-format_bridge_stats_extrainfo(time_t now)
+char *
+geoip_format_bridge_stats(time_t now)
{
char *out = NULL, *data = NULL;
long duration = now - start_of_bridge_stats_interval;
@@ -1206,6 +1206,8 @@ format_bridge_stats_extrainfo(time_t now)
if (duration < 0)
return NULL;
+ if (!start_of_bridge_stats_interval)
+ return NULL; /* Not initialized. */
format_iso_time(written, now);
data = geoip_get_client_history(GEOIP_CLIENT_CONNECT);
@@ -1255,7 +1257,7 @@ geoip_bridge_stats_write(time_t now)
geoip_remove_old_clients(start_of_bridge_stats_interval);
/* Generate formatted string */
- val = format_bridge_stats_extrainfo(now);
+ val = geoip_format_bridge_stats(now);
if (val == NULL)
goto done;
diff --git a/src/or/geoip.h b/src/or/geoip.h
index 3372260..ce38419 100644
--- a/src/or/geoip.h
+++ b/src/or/geoip.h
@@ -53,6 +53,7 @@ void geoip_entry_stats_term(void);
void geoip_reset_entry_stats(time_t now);
char *geoip_format_entry_stats(time_t now);
void geoip_bridge_stats_init(time_t now);
+char *geoip_format_bridge_stats(time_t now);
time_t geoip_bridge_stats_write(time_t now);
void geoip_bridge_stats_term(void);
const char *geoip_get_bridge_stats_extrainfo(time_t);
diff --git a/src/test/test.c b/src/test/test.c
index fe8146a..605e44c 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -1483,7 +1483,10 @@ test_geoip(void)
int i, j;
time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
char *s = NULL;
- const char *dirreq_stats_1 =
+ const char *bridge_stats_1 =
+ "bridge-stats-end 2010-08-12 13:27:30 (86400 s)\n"
+ "bridge-ips zz=24,xy=8\n",
+ *dirreq_stats_1 =
"dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
"dirreq-v3-ips ab=8\n"
"dirreq-v2-ips \n"
@@ -1594,6 +1597,24 @@ test_geoip(void)
test_assert(s);
test_streq("zz=24,xy=8", s);
+ /* Start testing bridge statistics by making sure that we don't output
+ * bridge stats without initializing them. */
+ s = geoip_format_bridge_stats(now + 86400);
+ test_assert(!s);
+
+ /* Initialize stats and generate the bridge-stats history string out of
+ * the connecting clients added above. */
+ geoip_bridge_stats_init(now);
+ s = geoip_format_bridge_stats(now + 86400);
+ test_streq(bridge_stats_1, s);
+ tor_free(s);
+
+ /* Stop collecting bridge stats and make sure we don't write a history
+ * string anymore. */
+ geoip_bridge_stats_term();
+ s = geoip_format_bridge_stats(now + 86400);
+ test_assert(!s);
+
/* Stop being a bridge and start being a directory mirror that gathers
* directory request statistics. */
geoip_bridge_stats_term();
More information about the tor-commits
mailing list