1
/* This program tests the date_parse_http routine in ../main/util_date.c.
3
* It is only semiautomated in that I would run it, modify the code to
4
* use a different algorithm or seed, recompile and run again, etc.
5
* Obviously it should use an argument for that, but I never got around
6
* to changing the implementation.
8
* gcc -g -O2 -I../main -o test_date ../main/util_date.o test_date.c
9
* test_date | egrep '^No '
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
20
#include "apr_general.h"
20
#define srand48 srandom
24
#define mrand48 random
27
void gm_timestr_822(char *ts, apr_time_t sec);
28
void gm_timestr_850(char *ts, apr_time_t sec);
29
void gm_timestr_ccc(char *ts, apr_time_t sec);
24
#endif /* APR_HAVE_TIME_H */
26
static struct datetest {
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" },
31
42
static const apr_time_t year2secs[] = {
32
43
APR_INT64_C(0), /* 1970 */
104
115
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
107
void gm_timestr_822(char *ts, apr_time_t sec)
118
/* XXX: non-portable */
119
static void gm_timestr_822(char *ts, apr_time_t sec)
109
121
static const char *const days[7]=
110
122
{"Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
112
124
time_t ls = (time_t)sec;
114
126
tms = gmtime(&ls);
116
128
sprintf(ts, "%s, %.2d %s %d %.2d:%.2d:%.2d GMT", days[tms->tm_wday],
117
129
tms->tm_mday, month_snames[tms->tm_mon], tms->tm_year + 1900,
118
130
tms->tm_hour, tms->tm_min, tms->tm_sec);
121
void gm_timestr_850(char *ts, apr_time_t sec)
123
static const char *const days[7]=
124
{"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
128
time_t ls = (time_t)sec;
133
if (year >= 100) year -= 100;
135
sprintf(ts, "%s, %.2d-%s-%.2d %.2d:%.2d:%.2d GMT", days[tms->tm_wday],
136
tms->tm_mday, month_snames[tms->tm_mon], year,
137
tms->tm_hour, tms->tm_min, tms->tm_sec);
140
void gm_timestr_ccc(char *ts, apr_time_t sec)
142
static const char *const days[7]=
143
{"Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
145
time_t ls = (time_t)sec;
149
sprintf(ts, "%s %s %2d %.2d:%.2d:%.2d %d", days[tms->tm_wday],
150
month_snames[tms->tm_mon], tms->tm_mday,
151
tms->tm_hour, tms->tm_min, tms->tm_sec, tms->tm_year + 1900);
133
/* Linear congruential generator */
134
static apr_uint32_t lgc(apr_uint32_t a)
138
z %= APR_UINT64_C(4294967291);
139
return (apr_uint32_t)z;
142
static void test_date_parse_http(abts_case *tc, void *data)
157
145
apr_time_t guess;
158
146
apr_time_t offset = 0;
159
/* apr_time_t offset = 0; */
160
/* apr_time_t offset = ((31 + 28) * 24 * 3600) - 1; */
161
147
apr_time_t secstodate, newsecs;
162
148
char datestr[50];
166
152
gm_timestr_822(datestr, secstodate);
167
153
secstodate *= APR_USEC_PER_SEC;
168
154
newsecs = apr_date_parse_http(datestr);
169
if (secstodate == newsecs)
170
printf("Yes %4d %19" APR_TIME_T_FMT " %s\n", year, secstodate, datestr);
171
else if (newsecs == APR_DATE_BAD)
172
printf("No %4d %19" APR_TIME_T_FMT " %19" APR_TIME_T_FMT " %s\n",
173
year, secstodate, newsecs, datestr);
175
printf("No* %4d %19" APR_TIME_T_FMT " %19" APR_TIME_T_FMT " %s\n",
176
year, secstodate, newsecs, datestr);
155
ABTS_TRUE(tc, secstodate == newsecs);
159
apr_generate_random_bytes((unsigned char *)&guess, sizeof(guess));
161
guess = apr_time_now() % APR_TIME_C(4294967291);
181
164
for (i = 0; i < 10000; ++i) {
182
guess = (time_t)mrand48();
183
if (guess < 0) guess *= -1;
165
guess = (time_t)lgc((apr_uint32_t)guess);
184
168
secstodate = guess + offset;
185
169
gm_timestr_822(datestr, secstodate);
186
170
secstodate *= APR_USEC_PER_SEC;
187
171
newsecs = apr_date_parse_http(datestr);
188
if (secstodate == newsecs)
189
printf("Yes %" APR_TIME_T_FMT " %s\n", secstodate, datestr);
190
else if (newsecs == APR_DATE_BAD)
191
printf("No %" APR_TIME_T_FMT " %" APR_TIME_T_FMT " %s\n",
192
secstodate, newsecs, datestr);
194
printf("No* %" APR_TIME_T_FMT " %" APR_TIME_T_FMT " %s\n",
195
secstodate, newsecs, datestr);
172
ABTS_TRUE(tc, secstodate == newsecs);
176
static void test_date_rfc(abts_case *tc, void *data)
181
while (tests[i].input) {
182
char str_date[APR_RFC822_DATE_LEN] = { 0 };
184
date = apr_date_parse_rfc(tests[i].input);
186
apr_rfc822_date(str_date, date);
188
ABTS_STR_EQUAL(tc, str_date, tests[i].output);
194
abts_suite *testdate(abts_suite *suite)
196
suite = ADD_SUITE(suite);
198
abts_run_test(suite, test_date_parse_http, NULL);
199
abts_run_test(suite, test_date_rfc, NULL);