Apache HTTPD
testdate.c
Go to the documentation of this file.
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 */
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;
146 apr_time_t offset = 0;
148 char datestr[50];
149
150 for (year = 1970; year < 2038; ++year) {
151 secstodate = year2secs[year - 1970] + offset;
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) {
166 if (guess < 0)
167 guess *= -1;
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
185
187
188 ABTS_STR_EQUAL(tc, str_date, tests[i].output);
189
190 i++;
191 }
192}
193
195{
196 suite = ADD_SUITE(suite);
197
200
201 return suite;
202}
void abts_run_test(abts_suite *ts, test_func f, void *value)
Definition abts.c:175
#define ABTS_TRUE(a, b)
Definition abts.h:127
#define ADD_SUITE(suite)
Definition abts.h:67
#define ABTS_STR_EQUAL(a, b, c)
Definition abts.h:123
APR-UTIL date routines.
APR Miscellaneous library routines.
apr_bucket apr_bucket_brigade * a
apr_size_t size
const char * input
Definition apr_cstr.h:93
apr_seek_where_t apr_off_t * offset
void * data
#define APR_RFC822_DATE_LEN
Definition apr_time.h:186
apr_size_t apr_size_t const char apr_time_exp_t * tm
Definition apr_time.h:221
#define APR_TIME_C(val)
Definition apr_time.h:49
#define APR_USEC_PER_SEC
Definition apr_time.h:60
apr_int64_t apr_time_t
Definition apr_time.h:45
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
const char * output
Definition testdate.c:28
const char * input
Definition testdate.c:27
static void test_date_parse_http(abts_case *tc, void *data)
Definition testdate.c:142
static void gm_timestr_822(char *ts, apr_time_t sec)
Definition testdate.c:119
static void test_date_rfc(abts_case *tc, void *data)
Definition testdate.c:176
abts_suite * testdate(abts_suite *suite)
Definition testdate.c:194
static struct datetest tests[]
static apr_uint32_t lgc(apr_uint32_t a)
Definition testdate.c:134
const char month_snames[12][4]
Definition testdate.c:114
static const apr_time_t year2secs[]
Definition testdate.c:42
apr_status_t apr_rfc822_date(char *date_str, apr_time_t t)
Definition timestr.c:42