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.
This commit is contained in:
Karsten Loesing 2020-12-16 09:58:19 +01:00 committed by Nick Mathewson
parent c934fced31
commit 5dd6304f36
2 changed files with 23 additions and 7 deletions

View File

@ -3176,12 +3176,12 @@ load_stats_file(const char *filename, const char *ts_tag, time_t now,
goto end;
}
/* Do we have enough for parsing a timestamp? */
if (strlen(contents) < strlen(ts_tag) + 1 + sizeof(timestr)) {
if (strlen(ts_tok) < strlen(ts_tag) + 1 + sizeof(timestr)) {
log_warn(LD_BUG, "Token %s malformed in file %s", ts_tag, filename);
goto end;
}
/* Parse timestamp in order to validate it is not too old. */
strlcpy(timestr, contents + strlen(ts_tag) + 1, sizeof(timestr));
strlcpy(timestr, ts_tok + strlen(ts_tag) + 1, sizeof(timestr));
if (parse_iso_time(timestr, &written) < 0) {
log_warn(LD_BUG, "Token %s has a malformed timestamp in file %s",
ts_tag, filename);

View File

@ -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);
}