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.
18 #include "apr_errno.h"
19 #include "apr_general.h"
22 #include "apr_strings.h"
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.
33 static apr_time_t now = APR_INT64_C(1032030336186711);
35 static char* print_time (apr_pool_t *pool, const apr_time_exp_t *xt)
37 return apr_psprintf (pool,
38 "%04d-%02d-%02d %02d:%02d:%02d.%06d %+05d [%d %s]%s",
48 apr_day_snames[xt->tm_wday],
49 (xt->tm_isdst ? " DST" : ""));
53 static void test_now(CuTest *tc)
59 current = apr_time_now();
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
67 CuAssert(tc, "apr_time and OS time do not agree",
68 (timediff > -2) && (timediff < 2));
71 static void test_gmtstr(CuTest *tc)
76 rv = apr_time_exp_gmt(&xt, now);
77 if (rv == APR_ENOTIMPL) {
78 CuNotImpl(tc, "apr_time_exp_gmt");
80 CuAssertTrue(tc, rv == APR_SUCCESS);
81 CuAssertStrEquals(tc, "2002-08-14 19:05:36.186711 +0000 [257 Sat]",
85 static void test_exp_lt(CuTest *tc)
89 time_t posix_secs = (time_t)apr_time_sec(now);
90 struct tm *posix_exp = localtime(&posix_secs);
92 rv = apr_time_exp_lt(&xt, now);
93 if (rv == APR_ENOTIMPL) {
94 CuNotImpl(tc, "apr_time_exp_lt");
96 CuAssertTrue(tc, rv == APR_SUCCESS);
98 #define CHK_FIELD(f) \
99 CuAssert(tc, "Mismatch in " #f, posix_exp->f == xt.f)
113 static void test_exp_get_gmt(CuTest *tc)
118 apr_int64_t hr_off_64;
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");
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);
131 static void test_exp_get_lt(CuTest *tc)
136 apr_int64_t hr_off_64;
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");
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);
149 static void test_imp_gmt(CuTest *tc)
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");
161 CuAssertTrue(tc, rv == APR_SUCCESS);
162 CuAssertTrue(tc, now == imp);
165 static void test_rfcstr(CuTest *tc)
170 rv = apr_rfc822_date(str, now);
171 if (rv == APR_ENOTIMPL) {
172 CuNotImpl(tc, "apr_rfc822_date");
174 CuAssertTrue(tc, rv == APR_SUCCESS);
175 CuAssertStrEquals(tc, "Sat, 14 Sep 2002 19:05:36 GMT", str);
178 static void test_ctime(CuTest *tc)
181 char apr_str[STR_SIZE];
182 char libc_str[STR_SIZE];
183 time_t posix_sec = (time_t)apr_time_sec(now);
185 rv = apr_ctime(apr_str, now);
186 if (rv == APR_ENOTIMPL) {
187 CuNotImpl(tc, "apr_ctime");
189 CuAssertTrue(tc, rv == APR_SUCCESS);
190 strcpy(libc_str, ctime(&posix_sec));
191 *strchr(libc_str, '\n') = '\0';
193 CuAssertStrEquals(tc, libc_str, apr_str);
196 static void test_strftime(CuTest *tc)
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");
209 CuAssertTrue(tc, rv == APR_SUCCESS);
210 CuAssertStrEquals(tc, "19:05 Saturday 14 September 2002", str);
213 static void test_strftimesmall(CuTest *tc)
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");
225 CuAssertTrue(tc, rv == APR_SUCCESS);
226 CuAssertStrEquals(tc, "19:05:36", str);
229 static void test_exp_tz(CuTest *tc)
233 apr_int32_t hr_off = -5 * 3600; /* 5 hours in seconds */
235 rv = apr_time_exp_tz(&xt, now, hr_off);
236 if (rv == APR_ENOTIMPL) {
237 CuNotImpl(tc, "apr_time_exp_tz");
239 CuAssertTrue(tc, rv == APR_SUCCESS);
240 CuAssertTrue(tc, (xt.tm_usec == 186711) &&
243 (xt.tm_hour == 14) &&
244 (xt.tm_mday == 14) &&
246 (xt.tm_year == 102) &&
248 (xt.tm_yday == 256));
251 static void test_strftimeoffset(CuTest *tc)
257 apr_int32_t hr_off = -5 * 3600; /* 5 hours in seconds */
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");
264 CuAssertTrue(tc, rv == APR_SUCCESS);
267 /* 0.9.4 and earlier rejected valid dates in 2038 */
268 static void test_2038(CuTest *tc)
273 /* 2038-01-19T03:14:07.000000Z */
281 apr_assert_success(tc, "explode January 19th, 2038",
282 apr_time_exp_get(&t, &xt));
285 CuSuite *testtime(void)
287 CuSuite *suite = CuSuiteNew("Time");
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);