aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEsteban Manchado Velázquez <emanchado@demiurgo.org>2012-02-20 17:40:37 +0100
committerNick Mathewson <nickm@torproject.org>2012-05-16 12:14:48 -0400
commitd0d9c3d71e1fb88557d684c59cf8a920712b16f1 (patch)
tree78b18ac9779556d974963b5db11d0488cfcc5689 /src
parent3ed4c5dc05a1954a8c68d0f7e9fb802f3155d308 (diff)
downloadtor-d0d9c3d71e1fb88557d684c59cf8a920712b16f1.tar
tor-d0d9c3d71e1fb88557d684c59cf8a920712b16f1.tar.gz
Fix parse_http_time and add tests
* It seems parse_http_time wasn't parsing correctly any date with commas (RFCs 1123 and 850). Fix that. * It seems parse_http_time was reporting the wrong month (they start at 0, not 1). Fix that. * Add some tests for parse_http_time, covering all three formats.
Diffstat (limited to 'src')
-rw-r--r--src/common/util.c8
-rw-r--r--src/test/test_util.c48
2 files changed, 52 insertions, 4 deletions
diff --git a/src/common/util.c b/src/common/util.c
index 7d2fc4dea..391b02f34 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -1416,13 +1416,13 @@ parse_http_time(const char *date, struct tm *tm)
/* First, try RFC1123 or RFC850 format: skip the weekday. */
if ((cp = strchr(date, ','))) {
- ++cp;
- if (tor_sscanf(date, "%2u %3s %4u %2u:%2u:%2u GMT",
+ cp += 2;
+ if (tor_sscanf(cp, "%2u %3s %4u %2u:%2u:%2u GMT",
&tm_mday, month, &tm_year,
&tm_hour, &tm_min, &tm_sec) == 6) {
/* rfc1123-date */
tm_year -= 1900;
- } else if (tor_sscanf(date, "%2u-%3s-%2u %2u:%2u:%2u GMT",
+ } else if (tor_sscanf(cp, "%2u-%3s-%2u %2u:%2u:%2u GMT",
&tm_mday, month, &tm_year,
&tm_hour, &tm_min, &tm_sec) == 6) {
/* rfc850-date */
@@ -1449,7 +1449,7 @@ parse_http_time(const char *date, struct tm *tm)
/* Okay, now decode the month. */
for (i = 0; i < 12; ++i) {
if (!strcasecmp(MONTH_NAMES[i], month)) {
- tm->tm_mon = i+1;
+ tm->tm_mon = i;
}
}
diff --git a/src/test/test_util.c b/src/test/test_util.c
index ee745c5cf..5845d779b 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -87,6 +87,53 @@ test_util_time(void)
}
static void
+test_util_parse_http_time(void *arg)
+{
+ struct tm a_time;
+
+ (void)arg;
+
+ /* Test parse_http_time */
+
+ test_eq(-1, parse_http_time("", &a_time));
+ test_eq(-1, parse_http_time("Sunday, 32 Aug 2004 00:48:22 GMT", &a_time));
+ test_eq(-1, parse_http_time("Sunday, 3 Aug 1869 00:48:22 GMT", &a_time));
+ test_eq(-1, parse_http_time("Sunday, 32-Aug-94 00:48:22 GMT", &a_time));
+ test_eq(-1, parse_http_time("Sunday, 3-Ago-04 00:48:22", &a_time));
+ test_eq(-1, parse_http_time("Sunday, August the third", &a_time));
+
+ test_eq(0, parse_http_time("Wednesday, 04 Aug 1994 00:48:22 GMT", &a_time));
+ test_eq((time_t)775961302UL, tor_timegm(&a_time));
+ test_eq(0, parse_http_time("Wednesday, 4 Aug 1994 0:48:22 GMT", &a_time));
+ test_eq((time_t)775961302UL, tor_timegm(&a_time));
+ test_eq(0, parse_http_time("Miercoles, 4 Aug 1994 0:48:22 GMT", &a_time));
+ test_eq((time_t)775961302UL, tor_timegm(&a_time));
+ test_eq(0, parse_http_time("Wednesday, 04-Aug-94 00:48:22 GMT", &a_time));
+ test_eq((time_t)775961302UL, tor_timegm(&a_time));
+ test_eq(0, parse_http_time("Wednesday, 4-Aug-94 0:48:22 GMT", &a_time));
+ test_eq((time_t)775961302UL, tor_timegm(&a_time));
+ test_eq(0, parse_http_time("Miercoles, 4-Aug-94 0:48:22 GMT", &a_time));
+ test_eq((time_t)775961302UL, tor_timegm(&a_time));
+ test_eq(0, parse_http_time("Wed Aug 04 00:48:22 1994", &a_time));
+ test_eq((time_t)775961302UL, tor_timegm(&a_time));
+ test_eq(0, parse_http_time("Wed Aug 4 0:48:22 1994", &a_time));
+ test_eq((time_t)775961302UL, tor_timegm(&a_time));
+ test_eq(0, parse_http_time("Mie Aug 4 0:48:22 1994", &a_time));
+ test_eq((time_t)775961302UL, tor_timegm(&a_time));
+ test_eq(-1, parse_http_time("2004-08-zz 99-99x99 GMT", &a_time));
+ test_eq(-1, parse_http_time("2011-03-32 00:00:00 GMT", &a_time));
+ test_eq(-1, parse_http_time("2011-03-30 24:00:00 GMT", &a_time));
+ test_eq(-1, parse_http_time("2011-03-30 23:60:00 GMT", &a_time));
+ test_eq(-1, parse_http_time("2011-03-30 23:59:62 GMT", &a_time));
+ test_eq(-1, parse_http_time("1969-03-30 23:59:59 GMT", &a_time));
+ test_eq(-1, parse_http_time("2011-00-30 23:59:59 GMT", &a_time));
+ test_eq(-1, parse_http_time("2011-03-30 23:59", &a_time));
+
+ done:
+ ;
+}
+
+static void
test_util_config_line(void)
{
char buf[1024];
@@ -1314,6 +1361,7 @@ test_util_di_ops(void)
struct testcase_t util_tests[] = {
UTIL_LEGACY(time),
+ UTIL_TEST(parse_http_time, 0),
UTIL_LEGACY(config_line),
UTIL_LEGACY(strmisc),
UTIL_LEGACY(pow2),