upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testtime.c
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 "apr_time.h"
18 #include "apr_errno.h"
19 #include "apr_general.h"
20 #include "apr_lib.h"
21 #include "test_apr.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(CuTest *tc)
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     CuAssert(tc, "apr_time and OS time do not agree", 
68              (timediff > -2) && (timediff < 2));
69 }
70
71 static void test_gmtstr(CuTest *tc)
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         CuNotImpl(tc, "apr_time_exp_gmt");
79     }
80     CuAssertTrue(tc, rv == APR_SUCCESS);
81     CuAssertStrEquals(tc, "2002-08-14 19:05:36.186711 +0000 [257 Sat]", 
82                       print_time(p, &xt));
83 }
84
85 static void test_exp_lt(CuTest *tc)
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         CuNotImpl(tc, "apr_time_exp_lt");
95     }
96     CuAssertTrue(tc, rv == APR_SUCCESS);
97
98 #define CHK_FIELD(f) \
99     CuAssert(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(CuTest *tc)
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     CuAssertTrue(tc, rv == APR_SUCCESS);
122     rv = apr_time_exp_get(&imp, &xt);
123     if (rv == APR_ENOTIMPL) {
124         CuNotImpl(tc, "apr_time_exp_get");
125     }
126     CuAssertTrue(tc, rv == APR_SUCCESS);
127     hr_off_64 = (apr_int64_t) xt.tm_gmtoff * APR_USEC_PER_SEC;
128     CuAssertTrue(tc, now + hr_off_64 == imp);
129 }
130
131 static void test_exp_get_lt(CuTest *tc)
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     CuAssertTrue(tc, rv == APR_SUCCESS);
140     rv = apr_time_exp_get(&imp, &xt);
141     if (rv == APR_ENOTIMPL) {
142         CuNotImpl(tc, "apr_time_exp_get");
143     }
144     CuAssertTrue(tc, rv == APR_SUCCESS);
145     hr_off_64 = (apr_int64_t) xt.tm_gmtoff * APR_USEC_PER_SEC;
146     CuAssertTrue(tc, now + hr_off_64 == imp);
147 }
148
149 static void test_imp_gmt(CuTest *tc)
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     CuAssertTrue(tc, rv == APR_SUCCESS);
157     rv = apr_time_exp_gmt_get(&imp, &xt);
158     if (rv == APR_ENOTIMPL) {
159         CuNotImpl(tc, "apr_time_exp_gmt_get");
160     }
161     CuAssertTrue(tc, rv == APR_SUCCESS);
162     CuAssertTrue(tc, now == imp);
163 }
164
165 static void test_rfcstr(CuTest *tc)
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         CuNotImpl(tc, "apr_rfc822_date");
173     }
174     CuAssertTrue(tc, rv == APR_SUCCESS);
175     CuAssertStrEquals(tc, "Sat, 14 Sep 2002 19:05:36 GMT", str);
176 }
177
178 static void test_ctime(CuTest *tc)
179 {
180     apr_status_t rv;
181     char apr_str[STR_SIZE];
182     char libc_str[STR_SIZE];
183     time_t posix_sec = (time_t)apr_time_sec(now);
184
185     rv = apr_ctime(apr_str, now);
186     if (rv == APR_ENOTIMPL) {
187         CuNotImpl(tc, "apr_ctime");
188     }
189     CuAssertTrue(tc, rv == APR_SUCCESS);
190     strcpy(libc_str, ctime(&posix_sec));
191     *strchr(libc_str, '\n') = '\0';
192
193     CuAssertStrEquals(tc, libc_str, apr_str);
194 }
195
196 static void test_strftime(CuTest *tc)
197 {
198     apr_status_t rv;
199     apr_time_exp_t xt;
200     char *str = NULL;
201     apr_size_t sz;
202
203     rv = apr_time_exp_gmt(&xt, now);
204     str = apr_palloc(p, STR_SIZE + 1);
205     rv = apr_strftime(str, &sz, STR_SIZE, "%R %A %d %B %Y", &xt);
206     if (rv == APR_ENOTIMPL) {
207         CuNotImpl(tc, "apr_strftime");
208     }
209     CuAssertTrue(tc, rv == APR_SUCCESS);
210     CuAssertStrEquals(tc, "19:05 Saturday 14 September 2002", str);
211 }
212
213 static void test_strftimesmall(CuTest *tc)
214 {
215     apr_status_t rv;
216     apr_time_exp_t xt;
217     char str[STR_SIZE];
218     apr_size_t sz;
219
220     rv = apr_time_exp_gmt(&xt, now);
221     rv = apr_strftime(str, &sz, STR_SIZE, "%T", &xt);
222     if (rv == APR_ENOTIMPL) {
223         CuNotImpl(tc, "apr_strftime");
224     }
225     CuAssertTrue(tc, rv == APR_SUCCESS);
226     CuAssertStrEquals(tc, "19:05:36", str);
227 }
228
229 static void test_exp_tz(CuTest *tc)
230 {
231     apr_status_t rv;
232     apr_time_exp_t xt;
233     apr_int32_t hr_off = -5 * 3600; /* 5 hours in seconds */
234
235     rv = apr_time_exp_tz(&xt, now, hr_off);
236     if (rv == APR_ENOTIMPL) {
237         CuNotImpl(tc, "apr_time_exp_tz");
238     }
239     CuAssertTrue(tc, rv == APR_SUCCESS);
240     CuAssertTrue(tc, (xt.tm_usec == 186711) && 
241                      (xt.tm_sec == 36) &&
242                      (xt.tm_min == 5) && 
243                      (xt.tm_hour == 14) &&
244                      (xt.tm_mday == 14) &&
245                      (xt.tm_mon == 8) &&
246                      (xt.tm_year == 102) &&
247                      (xt.tm_wday == 6) &&
248                      (xt.tm_yday == 256));
249 }
250
251 static void test_strftimeoffset(CuTest *tc)
252 {
253     apr_status_t rv;
254     apr_time_exp_t xt;
255     char str[STR_SIZE];
256     apr_size_t sz;
257     apr_int32_t hr_off = -5 * 3600; /* 5 hours in seconds */
258
259     apr_time_exp_tz(&xt, now, hr_off);
260     rv = apr_strftime(str, &sz, STR_SIZE, "%T", &xt);
261     if (rv == APR_ENOTIMPL) {
262         CuNotImpl(tc, "apr_strftime");
263     }
264     CuAssertTrue(tc, rv == APR_SUCCESS);
265 }
266
267 /* 0.9.4 and earlier rejected valid dates in 2038 */
268 static void test_2038(CuTest *tc)
269 {
270     apr_time_exp_t xt;
271     apr_time_t t;
272
273     /* 2038-01-19T03:14:07.000000Z */
274     xt.tm_year = 138;
275     xt.tm_mon = 0;
276     xt.tm_mday = 19;
277     xt.tm_hour = 3;
278     xt.tm_min = 14;
279     xt.tm_sec = 7;
280     
281     apr_assert_success(tc, "explode January 19th, 2038",
282                        apr_time_exp_get(&t, &xt));
283 }
284
285 CuSuite *testtime(void)
286 {
287     CuSuite *suite = CuSuiteNew("Time");
288
289     SUITE_ADD_TEST(suite, test_now);
290     SUITE_ADD_TEST(suite, test_gmtstr);
291     SUITE_ADD_TEST(suite, test_exp_lt);
292     SUITE_ADD_TEST(suite, test_exp_get_gmt);
293     SUITE_ADD_TEST(suite, test_exp_get_lt);
294     SUITE_ADD_TEST(suite, test_imp_gmt);
295     SUITE_ADD_TEST(suite, test_rfcstr);
296     SUITE_ADD_TEST(suite, test_ctime);
297     SUITE_ADD_TEST(suite, test_strftime);
298     SUITE_ADD_TEST(suite, test_strftimesmall);
299     SUITE_ADD_TEST(suite, test_exp_tz);
300     SUITE_ADD_TEST(suite, test_strftimeoffset);
301     SUITE_ADD_TEST(suite, test_2038);
302
303     return suite;
304 }
305