~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/test/testtime.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * 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 "apr_time.h"
 
18
#include "apr_errno.h"
 
19
#include "apr_general.h"
 
20
#include "apr_lib.h"
 
21
#include "testutil.h"
 
22
#include "apr_strings.h"
 
23
#include <time.h>
 
24
 
 
25
#define STR_SIZE 45
 
26
 
 
27
/* The time value is used throughout the tests, so just make this a global.
 
28
 * Also, we need a single value that we can test for the positive tests, so
 
29
 * I chose the number below, it corresponds to:
 
30
 *           2002-08-14 12:05:36.186711 -25200 [257 Sat].
 
31
 * Which happens to be when I wrote the new tests.
 
32
 */
 
33
static apr_time_t now = APR_INT64_C(1032030336186711);
 
34
 
 
35
static char* print_time (apr_pool_t *pool, const apr_time_exp_t *xt)
 
36
{
 
37
    return apr_psprintf (pool,
 
38
                         "%04d-%02d-%02d %02d:%02d:%02d.%06d %+05d [%d %s]%s",
 
39
                         xt->tm_year + 1900,
 
40
                         xt->tm_mon,
 
41
                         xt->tm_mday,
 
42
                         xt->tm_hour,
 
43
                         xt->tm_min,
 
44
                         xt->tm_sec,
 
45
                         xt->tm_usec,
 
46
                         xt->tm_gmtoff,
 
47
                         xt->tm_yday + 1,
 
48
                         apr_day_snames[xt->tm_wday],
 
49
                         (xt->tm_isdst ? " DST" : ""));
 
50
}
 
51
 
 
52
 
 
53
static void test_now(abts_case *tc, void *data)
 
54
{
 
55
    apr_time_t timediff;
 
56
    apr_time_t current;
 
57
    time_t os_now;
 
58
 
 
59
    current = apr_time_now();
 
60
    time(&os_now);
 
61
 
 
62
    timediff = os_now - (current / APR_USEC_PER_SEC); 
 
63
    /* Even though these are called so close together, there is the chance
 
64
     * that the time will be slightly off, so accept anything between -1 and
 
65
     * 1 second.
 
66
     */
 
67
    ABTS_ASSERT(tc, "apr_time and OS time do not agree", 
 
68
             (timediff > -2) && (timediff < 2));
 
69
}
 
70
 
 
71
static void test_gmtstr(abts_case *tc, void *data)
 
72
{
 
73
    apr_status_t rv;
 
74
    apr_time_exp_t xt;
 
75
 
 
76
    rv = apr_time_exp_gmt(&xt, now);
 
77
    if (rv == APR_ENOTIMPL) {
 
78
        ABTS_NOT_IMPL(tc, "apr_time_exp_gmt");
 
79
    }
 
80
    ABTS_TRUE(tc, rv == APR_SUCCESS);
 
81
    ABTS_STR_EQUAL(tc, "2002-08-14 19:05:36.186711 +0000 [257 Sat]", 
 
82
                      print_time(p, &xt));
 
83
}
 
84
 
 
85
static void test_exp_lt(abts_case *tc, void *data)
 
86
{
 
87
    apr_status_t rv;
 
88
    apr_time_exp_t xt;
 
89
    time_t posix_secs = (time_t)apr_time_sec(now);
 
90
    struct tm *posix_exp = localtime(&posix_secs);
 
91
 
 
92
    rv = apr_time_exp_lt(&xt, now);
 
93
    if (rv == APR_ENOTIMPL) {
 
94
        ABTS_NOT_IMPL(tc, "apr_time_exp_lt");
 
95
    }
 
96
    ABTS_TRUE(tc, rv == APR_SUCCESS);
 
97
 
 
98
#define CHK_FIELD(f) \
 
99
    ABTS_ASSERT(tc, "Mismatch in " #f, posix_exp->f == xt.f)
 
100
 
 
101
    CHK_FIELD(tm_sec);
 
102
    CHK_FIELD(tm_min);
 
103
    CHK_FIELD(tm_hour);
 
104
    CHK_FIELD(tm_mday);
 
105
    CHK_FIELD(tm_mon);
 
106
    CHK_FIELD(tm_year);
 
107
    CHK_FIELD(tm_wday);
 
108
    CHK_FIELD(tm_yday);
 
109
    CHK_FIELD(tm_isdst);
 
110
#undef CHK_FIELD
 
111
}
 
112
 
 
113
static void test_exp_get_gmt(abts_case *tc, void *data)
 
114
{
 
115
    apr_status_t rv;
 
116
    apr_time_exp_t xt;
 
117
    apr_time_t imp;
 
118
    apr_int64_t hr_off_64;
 
119
 
 
120
    rv = apr_time_exp_gmt(&xt, now);
 
121
    ABTS_TRUE(tc, rv == APR_SUCCESS);
 
122
    rv = apr_time_exp_get(&imp, &xt);
 
123
    if (rv == APR_ENOTIMPL) {
 
124
        ABTS_NOT_IMPL(tc, "apr_time_exp_get");
 
125
    }
 
126
    ABTS_TRUE(tc, rv == APR_SUCCESS);
 
127
    hr_off_64 = (apr_int64_t) xt.tm_gmtoff * APR_USEC_PER_SEC;
 
128
    ABTS_TRUE(tc, now + hr_off_64 == imp);
 
129
}
 
130
 
 
131
static void test_exp_get_lt(abts_case *tc, void *data)
 
132
{
 
133
    apr_status_t rv;
 
134
    apr_time_exp_t xt;
 
135
    apr_time_t imp;
 
136
    apr_int64_t hr_off_64;
 
137
 
 
138
    rv = apr_time_exp_lt(&xt, now);
 
139
    ABTS_TRUE(tc, rv == APR_SUCCESS);
 
140
    rv = apr_time_exp_get(&imp, &xt);
 
141
    if (rv == APR_ENOTIMPL) {
 
142
        ABTS_NOT_IMPL(tc, "apr_time_exp_get");
 
143
    }
 
144
    ABTS_TRUE(tc, rv == APR_SUCCESS);
 
145
    hr_off_64 = (apr_int64_t) xt.tm_gmtoff * APR_USEC_PER_SEC;
 
146
    ABTS_TRUE(tc, now + hr_off_64 == imp);
 
147
}
 
148
 
 
149
static void test_imp_gmt(abts_case *tc, void *data)
 
150
{
 
151
    apr_status_t rv;
 
152
    apr_time_exp_t xt;
 
153
    apr_time_t imp;
 
154
 
 
155
    rv = apr_time_exp_gmt(&xt, now);
 
156
    ABTS_TRUE(tc, rv == APR_SUCCESS);
 
157
    rv = apr_time_exp_gmt_get(&imp, &xt);
 
158
    if (rv == APR_ENOTIMPL) {
 
159
        ABTS_NOT_IMPL(tc, "apr_time_exp_gmt_get");
 
160
    }
 
161
    ABTS_TRUE(tc, rv == APR_SUCCESS);
 
162
    ABTS_TRUE(tc, now == imp);
 
163
}
 
164
 
 
165
static void test_rfcstr(abts_case *tc, void *data)
 
166
{
 
167
    apr_status_t rv;
 
168
    char str[STR_SIZE];
 
169
 
 
170
    rv = apr_rfc822_date(str, now);
 
171
    if (rv == APR_ENOTIMPL) {
 
172
        ABTS_NOT_IMPL(tc, "apr_rfc822_date");
 
173
    }
 
174
    ABTS_TRUE(tc, rv == APR_SUCCESS);
 
175
    ABTS_STR_EQUAL(tc, "Sat, 14 Sep 2002 19:05:36 GMT", str);
 
176
}
 
177
 
 
178
static void test_ctime(abts_case *tc, void *data)
 
179
{
 
180
    apr_status_t rv;
 
181
    char apr_str[STR_SIZE];
 
182
    char libc_str[STR_SIZE];
 
183
    apr_time_t now_sec = apr_time_sec(now);
 
184
    time_t posix_sec = (time_t) now_sec;
 
185
 
 
186
    rv = apr_ctime(apr_str, now);
 
187
    if (rv == APR_ENOTIMPL) {
 
188
        ABTS_NOT_IMPL(tc, "apr_ctime");
 
189
    }
 
190
    ABTS_TRUE(tc, rv == APR_SUCCESS);
 
191
    strcpy(libc_str, ctime(&posix_sec));
 
192
    *strchr(libc_str, '\n') = '\0';
 
193
 
 
194
    ABTS_STR_EQUAL(tc, libc_str, apr_str);
 
195
}
 
196
 
 
197
static void test_strftime(abts_case *tc, void *data)
 
198
{
 
199
    apr_status_t rv;
 
200
    apr_time_exp_t xt;
 
201
    char *str = NULL;
 
202
    apr_size_t sz;
 
203
 
 
204
    rv = apr_time_exp_gmt(&xt, now);
 
205
    str = apr_palloc(p, STR_SIZE + 1);
 
206
    rv = apr_strftime(str, &sz, STR_SIZE, "%R %A %d %B %Y", &xt);
 
207
    if (rv == APR_ENOTIMPL) {
 
208
        ABTS_NOT_IMPL(tc, "apr_strftime");
 
209
    }
 
210
    ABTS_TRUE(tc, rv == APR_SUCCESS);
 
211
    ABTS_STR_EQUAL(tc, "19:05 Saturday 14 September 2002", str);
 
212
}
 
213
 
 
214
static void test_strftimesmall(abts_case *tc, void *data)
 
215
{
 
216
    apr_status_t rv;
 
217
    apr_time_exp_t xt;
 
218
    char str[STR_SIZE];
 
219
    apr_size_t sz;
 
220
 
 
221
    rv = apr_time_exp_gmt(&xt, now);
 
222
    rv = apr_strftime(str, &sz, STR_SIZE, "%T", &xt);
 
223
    if (rv == APR_ENOTIMPL) {
 
224
        ABTS_NOT_IMPL(tc, "apr_strftime");
 
225
    }
 
226
    ABTS_TRUE(tc, rv == APR_SUCCESS);
 
227
    ABTS_STR_EQUAL(tc, "19:05:36", str);
 
228
}
 
229
 
 
230
static void test_exp_tz(abts_case *tc, void *data)
 
231
{
 
232
    apr_status_t rv;
 
233
    apr_time_exp_t xt;
 
234
    apr_int32_t hr_off = -5 * 3600; /* 5 hours in seconds */
 
235
 
 
236
    rv = apr_time_exp_tz(&xt, now, hr_off);
 
237
    if (rv == APR_ENOTIMPL) {
 
238
        ABTS_NOT_IMPL(tc, "apr_time_exp_tz");
 
239
    }
 
240
    ABTS_TRUE(tc, rv == APR_SUCCESS);
 
241
    ABTS_TRUE(tc, (xt.tm_usec == 186711) && 
 
242
                     (xt.tm_sec == 36) &&
 
243
                     (xt.tm_min == 5) && 
 
244
                     (xt.tm_hour == 14) &&
 
245
                     (xt.tm_mday == 14) &&
 
246
                     (xt.tm_mon == 8) &&
 
247
                     (xt.tm_year == 102) &&
 
248
                     (xt.tm_wday == 6) &&
 
249
                     (xt.tm_yday == 256));
 
250
}
 
251
 
 
252
static void test_strftimeoffset(abts_case *tc, void *data)
 
253
{
 
254
    apr_status_t rv;
 
255
    apr_time_exp_t xt;
 
256
    char str[STR_SIZE];
 
257
    apr_size_t sz;
 
258
    apr_int32_t hr_off = -5 * 3600; /* 5 hours in seconds */
 
259
 
 
260
    apr_time_exp_tz(&xt, now, hr_off);
 
261
    rv = apr_strftime(str, &sz, STR_SIZE, "%T", &xt);
 
262
    if (rv == APR_ENOTIMPL) {
 
263
        ABTS_NOT_IMPL(tc, "apr_strftime");
 
264
    }
 
265
    ABTS_TRUE(tc, rv == APR_SUCCESS);
 
266
}
 
267
 
 
268
/* 0.9.4 and earlier rejected valid dates in 2038 */
 
269
static void test_2038(abts_case *tc, void *data)
 
270
{
 
271
    apr_time_exp_t xt;
 
272
    apr_time_t t;
 
273
 
 
274
    /* 2038-01-19T03:14:07.000000Z */
 
275
    xt.tm_year = 138;
 
276
    xt.tm_mon = 0;
 
277
    xt.tm_mday = 19;
 
278
    xt.tm_hour = 3;
 
279
    xt.tm_min = 14;
 
280
    xt.tm_sec = 7;
 
281
    
 
282
    APR_ASSERT_SUCCESS(tc, "explode January 19th, 2038",
 
283
                       apr_time_exp_get(&t, &xt));
 
284
}
 
285
 
 
286
abts_suite *testtime(abts_suite *suite)
 
287
{
 
288
    suite = ADD_SUITE(suite)
 
289
 
 
290
    abts_run_test(suite, test_now, NULL);
 
291
    abts_run_test(suite, test_gmtstr, NULL);
 
292
    abts_run_test(suite, test_exp_lt, NULL);
 
293
    abts_run_test(suite, test_exp_get_gmt, NULL);
 
294
    abts_run_test(suite, test_exp_get_lt, NULL);
 
295
    abts_run_test(suite, test_imp_gmt, NULL);
 
296
    abts_run_test(suite, test_rfcstr, NULL);
 
297
    abts_run_test(suite, test_ctime, NULL);
 
298
    abts_run_test(suite, test_strftime, NULL);
 
299
    abts_run_test(suite, test_strftimesmall, NULL);
 
300
    abts_run_test(suite, test_exp_tz, NULL);
 
301
    abts_run_test(suite, test_strftimeoffset, NULL);
 
302
    abts_run_test(suite, test_2038, NULL);
 
303
 
 
304
    return suite;
 
305
}
 
306