aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_stats.c
diff options
context:
space:
mode:
authorKarsten Loesing <karsten.loesing@gmx.net>2020-12-16 09:58:19 +0100
committerNick Mathewson <nickm@torproject.org>2020-12-21 13:18:20 -0500
commit5dd6304f36a9cb9b7f778abe5b84cf6a4e9890a6 (patch)
tree8466c3e6f5d9a174bdd3fe7297ecea67e6d3fb03 /src/test/test_stats.c
parentc934fced31a2dd6d53b129b6270109bc48c1ebb0 (diff)
downloadtor-5dd6304f36a9cb9b7f778abe5b84cf6a4e9890a6.tar.gz
tor-5dd6304f36a9cb9b7f778abe5b84cf6a4e9890a6.zip
Fix timestamp parser in new load_stats_file.
The previous parser only considered stats files _starting_ with the timestamp tag, not stats files having the timestamp tag in a later position. While this applies to all current stats files, a future stats file might look differently. Better to fix the function now than be surprised in another 9 years from now. This commit also adds a test case for such future stats, and it fixes stats file paths in newly added unit tests.
Diffstat (limited to 'src/test/test_stats.c')
-rw-r--r--src/test/test_stats.c26
1 files changed, 21 insertions, 5 deletions
diff --git a/src/test/test_stats.c b/src/test/test_stats.c
index 154acd4f9f..7262477d77 100644
--- a/src/test/test_stats.c
+++ b/src/test/test_stats.c
@@ -499,13 +499,12 @@ static void
test_load_stats_file(void *arg)
{
int ret;
- char *content = NULL, *read_file_content = NULL;
- const char *fname = NULL;
+ char *content = NULL, *read_file_content = NULL, *fname = NULL;
(void) arg;
/* Load conn-stats. */
- fname = get_fname("conn-stats");
+ fname = get_datadir_fname("conn-stats");
tt_assert(fname);
read_file_content = tor_strdup(
"conn-bi-direct 2020-12-13 15:48:53 (86400 s) 12,34,56,78\n"
@@ -516,7 +515,7 @@ test_load_stats_file(void *arg)
tt_str_op(read_file_content, OP_EQ, content);
/* Load hidserv-stats. */
- fname = get_fname("hidserv-stats");
+ fname = get_datadir_fname("hidserv-stats");
tt_assert(fname);
tor_free(read_file_content);
read_file_content = tor_strdup(
@@ -525,6 +524,7 @@ test_load_stats_file(void *arg)
"bin_size=1024\n"
"hidserv-dir-onions-seen 53 delta_f=8 epsilon=0.30 bin_size=8\n");
write_str_to_file(fname, read_file_content, 0);
+ tor_free(fname);
tor_free(content);
ret = load_stats_file("hidserv-stats", "hidserv-stats-end", 1607874000,
&content);
@@ -532,7 +532,7 @@ test_load_stats_file(void *arg)
tt_str_op(read_file_content, OP_EQ, content);
/* Load dirreq-stats. */
- fname = get_fname("dirreq-stats");
+ fname = get_datadir_fname("dirreq-stats");
tt_assert(fname);
tor_free(read_file_content);
read_file_content = tor_strdup(
@@ -546,13 +546,29 @@ test_load_stats_file(void *arg)
"d1=133653,d2=221050,q1=261242,d3=300622,d4=399758,md=539051,d6=721322,"
"d7=959866,q3=1103363,d8=1302035,d9=2046125,max=113404000\n");
write_str_to_file(fname, read_file_content, 0);
+ tor_free(fname);
tor_free(content);
ret = load_stats_file("dirreq-stats", "dirreq-stats-end", 1607874000,
&content);
tt_int_op(ret, OP_EQ, 1);
tt_str_op(read_file_content, OP_EQ, content);
+ /* Attempt to load future-stats file not starting with timestamp tag. */
+ fname = get_datadir_fname("future-stats");
+ tt_assert(fname);
+ tor_free(read_file_content);
+ read_file_content = tor_strdup(
+ "future-stuff-at-file-start\n"
+ "future-stats 2020-12-13 15:48:53 (86400 s)\n");
+ write_str_to_file(fname, read_file_content, 0);
+ tor_free(fname);
+ tor_free(content);
+ ret = load_stats_file("future-stats", "future-stats", 1607874000, &content);
+ tt_int_op(ret, OP_EQ, 1);
+ tt_str_op(read_file_content, OP_EQ, content);
+
done:
+ tor_free(fname);
tor_free(read_file_content);
tor_free(content);
}