Apache HTTPD
testtime.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 "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-09-14 12:05:36.186711 -25200 [257 Sat].
31 * Which happens to be when I wrote the new tests.
32 */
33static apr_time_t now = APR_INT64_C(1032030336186711);
34/* 2012-08-11 16:00:55.151600 -14400 [224 Sat] DST */
35static apr_time_t leap_year_now = APR_INT64_C(1344715255151600);
36
37static char* print_time (apr_pool_t *pool, const apr_time_exp_t *xt)
38{
39 return apr_psprintf (pool,
40 "%04d-%02d-%02d %02d:%02d:%02d.%06d %+05d [%d %s]%s",
41 xt->tm_year + 1900,
42 xt->tm_mon + 1,
43 xt->tm_mday,
44 xt->tm_hour,
45 xt->tm_min,
46 xt->tm_sec,
47 xt->tm_usec,
48 xt->tm_gmtoff,
49 xt->tm_yday + 1,
51 (xt->tm_isdst ? " DST" : ""));
52}
53
54
55static void test_now(abts_case *tc, void *data)
56{
58 apr_time_t current;
60
61 current = apr_time_now();
62 time(&os_now);
63
64 timediff = os_now - (current / APR_USEC_PER_SEC);
65 /* Even though these are called so close together, there is the chance
66 * that the time will be slightly off, so accept anything between -1 and
67 * 1 second.
68 */
69 ABTS_ASSERT(tc, "apr_time and OS time do not agree",
70 (timediff > -2) && (timediff < 2));
71}
72
73static void test_gmtstr(abts_case *tc, void *data)
74{
75 apr_status_t rv;
77
78 rv = apr_time_exp_gmt(&xt, now);
79 if (rv == APR_ENOTIMPL) {
80 ABTS_NOT_IMPL(tc, "apr_time_exp_gmt");
81 }
82 ABTS_TRUE(tc, rv == APR_SUCCESS);
83 ABTS_STR_EQUAL(tc, "2002-09-14 19:05:36.186711 +0000 [257 Sat]",
84 print_time(p, &xt));
85}
86
87static void test_exp_lt(abts_case *tc, void *data)
88{
89 apr_time_t test_times[] = {0, 0, 0};
90 int i;
91
92 test_times[0] = now;
94
95 for (i = 0; test_times[i] != 0; i++) {
96 apr_status_t rv;
99 struct tm *posix_exp = localtime(&posix_secs);
100
101 rv = apr_time_exp_lt(&xt, test_times[i]);
102 if (rv == APR_ENOTIMPL) {
103 ABTS_NOT_IMPL(tc, "apr_time_exp_lt");
104 }
105 ABTS_TRUE(tc, rv == APR_SUCCESS);
106
107#define CHK_FIELD(f) \
108 ABTS_ASSERT(tc, "Mismatch in " #f, posix_exp->f == xt.f)
109
110 CHK_FIELD(tm_sec);
111 CHK_FIELD(tm_min);
112 CHK_FIELD(tm_hour);
113 CHK_FIELD(tm_mday);
114 CHK_FIELD(tm_mon);
115 CHK_FIELD(tm_year);
116 CHK_FIELD(tm_wday);
117 CHK_FIELD(tm_yday);
118 CHK_FIELD(tm_isdst);
119#undef CHK_FIELD
120 }
121}
122
123static void test_exp_get_gmt(abts_case *tc, void *data)
124{
125 apr_status_t rv;
129
130 rv = apr_time_exp_gmt(&xt, now);
131 ABTS_TRUE(tc, rv == APR_SUCCESS);
132 rv = apr_time_exp_get(&imp, &xt);
133 if (rv == APR_ENOTIMPL) {
134 ABTS_NOT_IMPL(tc, "apr_time_exp_get");
135 }
136 ABTS_TRUE(tc, rv == APR_SUCCESS);
138 ABTS_TRUE(tc, now + hr_off_64 == imp);
139}
140
141static void test_exp_get_lt(abts_case *tc, void *data)
142{
143 apr_status_t rv;
147
148 rv = apr_time_exp_lt(&xt, now);
149 ABTS_TRUE(tc, rv == APR_SUCCESS);
150 rv = apr_time_exp_get(&imp, &xt);
151 if (rv == APR_ENOTIMPL) {
152 ABTS_NOT_IMPL(tc, "apr_time_exp_get");
153 }
154 ABTS_TRUE(tc, rv == APR_SUCCESS);
156 ABTS_TRUE(tc, now + hr_off_64 == imp);
157}
158
159static void test_imp_gmt(abts_case *tc, void *data)
160{
161 apr_status_t rv;
164
165 rv = apr_time_exp_gmt(&xt, now);
166 ABTS_TRUE(tc, rv == APR_SUCCESS);
167 rv = apr_time_exp_gmt_get(&imp, &xt);
168 if (rv == APR_ENOTIMPL) {
169 ABTS_NOT_IMPL(tc, "apr_time_exp_gmt_get");
170 }
171 ABTS_TRUE(tc, rv == APR_SUCCESS);
172 ABTS_TRUE(tc, now == imp);
173}
174
175static void test_rfcstr(abts_case *tc, void *data)
176{
177 apr_status_t rv;
178 char str[STR_SIZE];
179
180 rv = apr_rfc822_date(str, now);
181 if (rv == APR_ENOTIMPL) {
182 ABTS_NOT_IMPL(tc, "apr_rfc822_date");
183 }
184 ABTS_TRUE(tc, rv == APR_SUCCESS);
185 ABTS_STR_EQUAL(tc, "Sat, 14 Sep 2002 19:05:36 GMT", str);
186}
187
188static void test_ctime(abts_case *tc, void *data)
189{
190 apr_status_t rv;
191 char apr_str[STR_SIZE];
192 char libc_str[STR_SIZE];
195
196 rv = apr_ctime(apr_str, now);
197 if (rv == APR_ENOTIMPL) {
198 ABTS_NOT_IMPL(tc, "apr_ctime");
199 }
200 ABTS_TRUE(tc, rv == APR_SUCCESS);
201 strcpy(libc_str, ctime(&posix_sec));
202 *strchr(libc_str, '\n') = '\0';
203
205}
206
207static void test_strftime(abts_case *tc, void *data)
208{
209 apr_status_t rv;
211 char *str = NULL;
213
214 rv = apr_time_exp_gmt(&xt, now);
215 str = apr_palloc(p, STR_SIZE + 1);
216 rv = apr_strftime(str, &sz, STR_SIZE, "%R %A %d %B %Y", &xt);
217 if (rv == APR_ENOTIMPL) {
218 ABTS_NOT_IMPL(tc, "apr_strftime");
219 }
220 ABTS_TRUE(tc, rv == APR_SUCCESS);
221 ABTS_STR_EQUAL(tc, "19:05 Saturday 14 September 2002", str);
222}
223
224static void test_strftimesmall(abts_case *tc, void *data)
225{
226 apr_status_t rv;
228 char str[STR_SIZE];
230
231 rv = apr_time_exp_gmt(&xt, now);
232 rv = apr_strftime(str, &sz, STR_SIZE, "%T", &xt);
233 if (rv == APR_ENOTIMPL) {
234 ABTS_NOT_IMPL(tc, "apr_strftime");
235 }
236 ABTS_TRUE(tc, rv == APR_SUCCESS);
237 ABTS_STR_EQUAL(tc, "19:05:36", str);
238}
239
240static void test_exp_tz(abts_case *tc, void *data)
241{
242 apr_status_t rv;
244 apr_int32_t hr_off = -5 * 3600; /* 5 hours in seconds */
245
246 rv = apr_time_exp_tz(&xt, now, hr_off);
247 if (rv == APR_ENOTIMPL) {
248 ABTS_NOT_IMPL(tc, "apr_time_exp_tz");
249 }
250 ABTS_TRUE(tc, rv == APR_SUCCESS);
251 ABTS_TRUE(tc, (xt.tm_usec == 186711) &&
252 (xt.tm_sec == 36) &&
253 (xt.tm_min == 5) &&
254 (xt.tm_hour == 14) &&
255 (xt.tm_mday == 14) &&
256 (xt.tm_mon == 8) &&
257 (xt.tm_year == 102) &&
258 (xt.tm_wday == 6) &&
259 (xt.tm_yday == 256));
260}
261
262static void test_strftimeoffset(abts_case *tc, void *data)
263{
264 apr_status_t rv;
266 char str[STR_SIZE];
268 apr_int32_t hr_off = -5 * 3600; /* 5 hours in seconds */
269
271 rv = apr_strftime(str, &sz, STR_SIZE, "%T", &xt);
272 if (rv == APR_ENOTIMPL) {
273 ABTS_NOT_IMPL(tc, "apr_strftime");
274 }
275 ABTS_TRUE(tc, rv == APR_SUCCESS);
276}
277
278/* 0.9.4 and earlier rejected valid dates in 2038 */
279static void test_2038(abts_case *tc, void *data)
280{
283
284 /* 2038-01-19T03:14:07.000000Z */
285 xt.tm_year = 138;
286 xt.tm_mon = 0;
287 xt.tm_mday = 19;
288 xt.tm_hour = 3;
289 xt.tm_min = 14;
290 xt.tm_sec = 7;
291
292 APR_ASSERT_SUCCESS(tc, "explode January 19th, 2038",
293 apr_time_exp_get(&t, &xt));
294}
295
297{
298 suite = ADD_SUITE(suite)
299
300 abts_run_test(suite, test_now, NULL);
313
314 return suite;
315}
316
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_NOT_IMPL(a, b)
Definition abts.h:129
#define ABTS_STR_EQUAL(a, b, c)
Definition abts.h:123
#define ABTS_ASSERT(a, b, c)
Definition abts.h:130
#define APR_ASSERT_SUCCESS(tc, ctxt, rv)
Definition testutil.h:58
APR Error Codes.
APR Miscellaneous library routines.
APR general purpose library routines.
APR Strings library.
APR Time Library.
#define APR_ENOTIMPL
Definition apr_errno.h:476
apr_size_t size
const char int apr_pool_t * pool
Definition apr_cstr.h:84
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
void * data
apr_interval_time_t t
apr_size_t apr_size_t const char apr_time_exp_t * tm
Definition apr_time.h:221
APR_DECLARE_DATA const char apr_day_snames[7][4]
Definition timestr.c:37
#define APR_USEC_PER_SEC
Definition apr_time.h:60
apr_int64_t apr_time_t
Definition apr_time.h:45
#define apr_time_sec(time)
Definition apr_time.h:63
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
apr_int32_t tm_gmtoff
Definition apr_time.h:119
apr_int32_t tm_sec
Definition apr_time.h:101
apr_int32_t tm_hour
Definition apr_time.h:105
apr_int32_t tm_year
Definition apr_time.h:111
apr_int32_t tm_isdst
Definition apr_time.h:117
apr_int32_t tm_min
Definition apr_time.h:103
apr_int32_t tm_wday
Definition apr_time.h:113
apr_int32_t tm_mday
Definition apr_time.h:107
apr_int32_t tm_mon
Definition apr_time.h:109
apr_int32_t tm_yday
Definition apr_time.h:115
apr_int32_t tm_usec
Definition apr_time.h:99
static void test_2038(abts_case *tc, void *data)
Definition testtime.c:279
static void test_ctime(abts_case *tc, void *data)
Definition testtime.c:188
static void test_strftimeoffset(abts_case *tc, void *data)
Definition testtime.c:262
#define STR_SIZE
Definition testtime.c:25
static void test_strftimesmall(abts_case *tc, void *data)
Definition testtime.c:224
static char * print_time(apr_pool_t *pool, const apr_time_exp_t *xt)
Definition testtime.c:37
#define CHK_FIELD(f)
static void test_exp_tz(abts_case *tc, void *data)
Definition testtime.c:240
static void test_exp_lt(abts_case *tc, void *data)
Definition testtime.c:87
static void test_now(abts_case *tc, void *data)
Definition testtime.c:55
abts_suite * testtime(abts_suite *suite)
Definition testtime.c:296
static apr_time_t now
Definition testtime.c:33
static void test_exp_get_gmt(abts_case *tc, void *data)
Definition testtime.c:123
static apr_time_t leap_year_now
Definition testtime.c:35
static void test_exp_get_lt(abts_case *tc, void *data)
Definition testtime.c:141
static void test_imp_gmt(abts_case *tc, void *data)
Definition testtime.c:159
static void test_rfcstr(abts_case *tc, void *data)
Definition testtime.c:175
static void test_strftime(abts_case *tc, void *data)
Definition testtime.c:207
static void test_gmtstr(abts_case *tc, void *data)
Definition testtime.c:73
apr_status_t apr_strftime(char *s, apr_size_t *retsize, apr_size_t max, const char *format, apr_time_exp_t *xt)
Definition timestr.c:132
apr_status_t apr_ctime(char *date_str, apr_time_t t)
Definition timestr.c:90
apr_status_t apr_rfc822_date(char *date_str, apr_time_t t)
Definition timestr.c:42
#define str