1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "abts.h" 18#include "testutil.h" 19#include "apr_date.h" 20#include "apr_general.h" 21 22#if APR_HAVE_TIME_H 23#include <time.h> 24#endif /* APR_HAVE_TIME_H */ 25 26static struct datetest { 27 const char *input; 28 const char *output; 29} tests[] = { 30 { "Mon, 27 Feb 1995 20:49:44 -0800", "Tue, 28 Feb 1995 04:49:44 GMT" }, 31 { "Fri, 1 Jul 2005 11:34:25 -0400", "Fri, 01 Jul 2005 15:34:25 GMT" }, 32 { "Monday, 27-Feb-95 20:49:44 -0800", "Tue, 28 Feb 1995 04:49:44 GMT" }, 33 { "Tue, 4 Mar 1997 12:43:52 +0200", "Tue, 04 Mar 1997 10:43:52 GMT" }, 34 { "Mon, 27 Feb 95 20:49:44 -0800", "Tue, 28 Feb 1995 04:49:44 GMT" }, 35 { "Tue, 4 Mar 97 12:43:52 +0200", "Tue, 04 Mar 1997 10:43:52 GMT" }, 36 { "Tue, 4 Mar 97 12:43:52 +0200", "Tue, 04 Mar 1997 10:43:52 GMT" }, 37 { "Mon, 27 Feb 95 20:49 GMT", "Mon, 27 Feb 1995 20:49:00 GMT" }, 38 { "Tue, 4 Mar 97 12:43 GMT", "Tue, 04 Mar 1997 12:43:00 GMT" }, 39 { NULL, NULL } 40}; 41 42static const apr_time_t year2secs[] = { 43 APR_INT64_C(0), /* 1970 */ 44 APR_INT64_C(31536000), /* 1971 */ 45 APR_INT64_C(63072000), /* 1972 */ 46 APR_INT64_C(94694400), /* 1973 */ 47 APR_INT64_C(126230400), /* 1974 */ 48 APR_INT64_C(157766400), /* 1975 */ 49 APR_INT64_C(189302400), /* 1976 */ 50 APR_INT64_C(220924800), /* 1977 */ 51 APR_INT64_C(252460800), /* 1978 */ 52 APR_INT64_C(283996800), /* 1979 */ 53 APR_INT64_C(315532800), /* 1980 */ 54 APR_INT64_C(347155200), /* 1981 */ 55 APR_INT64_C(378691200), /* 1982 */ 56 APR_INT64_C(410227200), /* 1983 */ 57 APR_INT64_C(441763200), /* 1984 */ 58 APR_INT64_C(473385600), /* 1985 */ 59 APR_INT64_C(504921600), /* 1986 */ 60 APR_INT64_C(536457600), /* 1987 */ 61 APR_INT64_C(567993600), /* 1988 */ 62 APR_INT64_C(599616000), /* 1989 */ 63 APR_INT64_C(631152000), /* 1990 */ 64 APR_INT64_C(662688000), /* 1991 */ 65 APR_INT64_C(694224000), /* 1992 */ 66 APR_INT64_C(725846400), /* 1993 */ 67 APR_INT64_C(757382400), /* 1994 */ 68 APR_INT64_C(788918400), /* 1995 */ 69 APR_INT64_C(820454400), /* 1996 */ 70 APR_INT64_C(852076800), /* 1997 */ 71 APR_INT64_C(883612800), /* 1998 */ 72 APR_INT64_C(915148800), /* 1999 */ 73 APR_INT64_C(946684800), /* 2000 */ 74 APR_INT64_C(978307200), /* 2001 */ 75 APR_INT64_C(1009843200), /* 2002 */ 76 APR_INT64_C(1041379200), /* 2003 */ 77 APR_INT64_C(1072915200), /* 2004 */ 78 APR_INT64_C(1104537600), /* 2005 */ 79 APR_INT64_C(1136073600), /* 2006 */ 80 APR_INT64_C(1167609600), /* 2007 */ 81 APR_INT64_C(1199145600), /* 2008 */ 82 APR_INT64_C(1230768000), /* 2009 */ 83 APR_INT64_C(1262304000), /* 2010 */ 84 APR_INT64_C(1293840000), /* 2011 */ 85 APR_INT64_C(1325376000), /* 2012 */ 86 APR_INT64_C(1356998400), /* 2013 */ 87 APR_INT64_C(1388534400), /* 2014 */ 88 APR_INT64_C(1420070400), /* 2015 */ 89 APR_INT64_C(1451606400), /* 2016 */ 90 APR_INT64_C(1483228800), /* 2017 */ 91 APR_INT64_C(1514764800), /* 2018 */ 92 APR_INT64_C(1546300800), /* 2019 */ 93 APR_INT64_C(1577836800), /* 2020 */ 94 APR_INT64_C(1609459200), /* 2021 */ 95 APR_INT64_C(1640995200), /* 2022 */ 96 APR_INT64_C(1672531200), /* 2023 */ 97 APR_INT64_C(1704067200), /* 2024 */ 98 APR_INT64_C(1735689600), /* 2025 */ 99 APR_INT64_C(1767225600), /* 2026 */ 100 APR_INT64_C(1798761600), /* 2027 */ 101 APR_INT64_C(1830297600), /* 2028 */ 102 APR_INT64_C(1861920000), /* 2029 */ 103 APR_INT64_C(1893456000), /* 2030 */ 104 APR_INT64_C(1924992000), /* 2031 */ 105 APR_INT64_C(1956528000), /* 2032 */ 106 APR_INT64_C(1988150400), /* 2033 */ 107 APR_INT64_C(2019686400), /* 2034 */ 108 APR_INT64_C(2051222400), /* 2035 */ 109 APR_INT64_C(2082758400), /* 2036 */ 110 APR_INT64_C(2114380800), /* 2037 */ 111 APR_INT64_C(2145916800) /* 2038 */ 112}; 113 114const char month_snames[12][4] = { 115 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" 116}; 117 118/* XXX: non-portable */ 119static void gm_timestr_822(char *ts, apr_time_t sec) 120{ 121 static const char *const days[7]= 122 {"Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; 123 struct tm *tms; 124 time_t ls = (time_t)sec; 125 126 tms = gmtime(&ls); 127 128 sprintf(ts, "%s, %.2d %s %d %.2d:%.2d:%.2d GMT", days[tms->tm_wday], 129 tms->tm_mday, month_snames[tms->tm_mon], tms->tm_year + 1900, 130 tms->tm_hour, tms->tm_min, tms->tm_sec); 131} 132 133/* Linear congruential generator */ 134static apr_uint32_t lgc(apr_uint32_t a) 135{ 136 apr_uint64_t z = a; 137 z *= 279470273; 138 z %= APR_UINT64_C(4294967291); 139 return (apr_uint32_t)z; 140} 141 142static void test_date_parse_http(abts_case *tc, void *data) 143{ 144 int year, i; 145 apr_time_t guess; 146 apr_time_t offset = 0; 147 apr_time_t secstodate, newsecs; 148 char datestr[50]; 149 150 for (year = 1970; year < 2038; ++year) { 151 secstodate = year2secs[year - 1970] + offset; 152 gm_timestr_822(datestr, secstodate); 153 secstodate *= APR_USEC_PER_SEC; 154 newsecs = apr_date_parse_http(datestr); 155 ABTS_TRUE(tc, secstodate == newsecs); 156 } 157 158#if APR_HAS_RANDOM 159 apr_generate_random_bytes((unsigned char *)&guess, sizeof(guess)); 160#else 161 guess = apr_time_now() % APR_TIME_C(4294967291); 162#endif 163 164 for (i = 0; i < 10000; ++i) { 165 guess = (time_t)lgc((apr_uint32_t)guess); 166 if (guess < 0) 167 guess *= -1; 168 secstodate = guess + offset; 169 gm_timestr_822(datestr, secstodate); 170 secstodate *= APR_USEC_PER_SEC; 171 newsecs = apr_date_parse_http(datestr); 172 ABTS_TRUE(tc, secstodate == newsecs); 173 } 174} 175 176static void test_date_rfc(abts_case *tc, void *data) 177{ 178 apr_time_t date; 179 int i = 0; 180 181 while (tests[i].input) { 182 char str_date[APR_RFC822_DATE_LEN] = { 0 }; 183 184 date = apr_date_parse_rfc(tests[i].input); 185 186 apr_rfc822_date(str_date, date); 187 188 ABTS_STR_EQUAL(tc, str_date, tests[i].output); 189 190 i++; 191 } 192} 193 194abts_suite *testdate(abts_suite *suite) 195{ 196 suite = ADD_SUITE(suite); 197 198 abts_run_test(suite, test_date_parse_http, NULL); 199 abts_run_test(suite, test_date_rfc, NULL); 200 201 return suite; 202} 203