~ubuntu-branches/ubuntu/hardy/apache2/hardy-proposed

« back to all changes in this revision

Viewing changes to srclib/apr-util/test/testdate.c

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Fritsch
  • Date: 2008-01-17 20:27:56 UTC
  • mto: This revision was merged to the branch mainline in revision 26.
  • Revision ID: james.westby@ubuntu.com-20080117202756-hv38rjknhwa2ilwi
Tags: upstream-2.2.8
ImportĀ upstreamĀ versionĀ 2.2.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This program tests the date_parse_http routine in ../main/util_date.c.
2
 
 *
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.
7
 
 * 
8
 
 *     gcc -g -O2 -I../main -o test_date ../main/util_date.o test_date.c
9
 
 *     test_date | egrep '^No '
10
 
 * 
11
 
 * Roy Fielding, 1996
 
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.
12
15
 */
13
16
 
14
 
#include <stdio.h>
15
 
#include <stdlib.h>
 
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
16
23
#include <time.h>
17
 
#include "apr_date.h"
18
 
 
19
 
#ifndef srand48
20
 
#define srand48 srandom
21
 
#endif
22
 
 
23
 
#ifndef mrand48
24
 
#define mrand48 random
25
 
#endif
26
 
 
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 */
 
25
 
 
26
static 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
};
30
41
 
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"
105
116
};
106
117
 
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)
108
120
{
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;
113
125
 
114
126
    tms = gmtime(&ls);
115
 
 
 
127
 
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);
119
131
}
120
132
 
121
 
void gm_timestr_850(char *ts, apr_time_t sec)
122
 
{
123
 
    static const char *const days[7]=
124
 
           {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 
125
 
            "Saturday"};
126
 
    struct tm *tms;
127
 
    int year;
128
 
    time_t ls = (time_t)sec;
129
 
 
130
 
    tms = gmtime(&ls);
131
 
 
132
 
    year = tms->tm_year;
133
 
    if (year >= 100) year -= 100;
134
 
 
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);
138
 
}
139
 
 
140
 
void gm_timestr_ccc(char *ts, apr_time_t sec)
141
 
{
142
 
    static const char *const days[7]=
143
 
       {"Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
144
 
    struct tm *tms;
145
 
    time_t ls = (time_t)sec;
146
 
 
147
 
    tms = gmtime(&ls);
148
 
 
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);
152
 
}
153
 
 
154
 
int main (void)
 
133
/* Linear congruential generator */
 
134
static 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
 
 
142
static void test_date_parse_http(abts_case *tc, void *data)
155
143
{
156
144
    int year, i;
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];
163
149
 
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);
174
 
        else
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);
177
156
    }
178
 
    
179
 
    srand48(978245L);
 
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
180
163
 
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);
 
166
        if (guess < 0)
 
167
            guess *= -1;
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);
193
 
        else
194
 
            printf("No* %" APR_TIME_T_FMT " %" APR_TIME_T_FMT " %s\n", 
195
 
                   secstodate, newsecs, datestr);
196
 
    }
197
 
    exit(0);
 
172
        ABTS_TRUE(tc, secstodate == newsecs);
 
173
    }
 
174
}
 
175
 
 
176
static 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
 
 
194
abts_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;
198
202
}