testtime.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #include "fspr_time.h"
  17. #include "fspr_errno.h"
  18. #include "fspr_general.h"
  19. #include "fspr_lib.h"
  20. #include "testutil.h"
  21. #include "fspr_strings.h"
  22. #include <time.h>
  23. #define STR_SIZE 45
  24. /* The time value is used throughout the tests, so just make this a global.
  25. * Also, we need a single value that we can test for the positive tests, so
  26. * I chose the number below, it corresponds to:
  27. * 2002-08-14 12:05:36.186711 -25200 [257 Sat].
  28. * Which happens to be when I wrote the new tests.
  29. */
  30. static fspr_time_t now = APR_INT64_C(1032030336186711);
  31. static char* print_time (fspr_pool_t *pool, const fspr_time_exp_t *xt)
  32. {
  33. return fspr_psprintf (pool,
  34. "%04d-%02d-%02d %02d:%02d:%02d.%06d %+05d [%d %s]%s",
  35. xt->tm_year + 1900,
  36. xt->tm_mon,
  37. xt->tm_mday,
  38. xt->tm_hour,
  39. xt->tm_min,
  40. xt->tm_sec,
  41. xt->tm_usec,
  42. xt->tm_gmtoff,
  43. xt->tm_yday + 1,
  44. fspr_day_snames[xt->tm_wday],
  45. (xt->tm_isdst ? " DST" : ""));
  46. }
  47. static void test_now(abts_case *tc, void *data)
  48. {
  49. fspr_time_t timediff;
  50. fspr_time_t current;
  51. time_t os_now;
  52. current = fspr_time_now();
  53. time(&os_now);
  54. timediff = os_now - (current / APR_USEC_PER_SEC);
  55. /* Even though these are called so close together, there is the chance
  56. * that the time will be slightly off, so accept anything between -1 and
  57. * 1 second.
  58. */
  59. ABTS_ASSERT(tc, "fspr_time and OS time do not agree",
  60. (timediff > -2) && (timediff < 2));
  61. }
  62. static void test_gmtstr(abts_case *tc, void *data)
  63. {
  64. fspr_status_t rv;
  65. fspr_time_exp_t xt;
  66. rv = fspr_time_exp_gmt(&xt, now);
  67. if (rv == APR_ENOTIMPL) {
  68. ABTS_NOT_IMPL(tc, "fspr_time_exp_gmt");
  69. }
  70. ABTS_TRUE(tc, rv == APR_SUCCESS);
  71. ABTS_STR_EQUAL(tc, "2002-08-14 19:05:36.186711 +0000 [257 Sat]",
  72. print_time(p, &xt));
  73. }
  74. static void test_exp_lt(abts_case *tc, void *data)
  75. {
  76. fspr_status_t rv;
  77. fspr_time_exp_t xt;
  78. time_t posix_secs = (time_t)fspr_time_sec(now);
  79. struct tm *posix_exp = localtime(&posix_secs);
  80. rv = fspr_time_exp_lt(&xt, now);
  81. if (rv == APR_ENOTIMPL) {
  82. ABTS_NOT_IMPL(tc, "fspr_time_exp_lt");
  83. }
  84. ABTS_TRUE(tc, rv == APR_SUCCESS);
  85. #define CHK_FIELD(f) \
  86. ABTS_ASSERT(tc, "Mismatch in " #f, posix_exp->f == xt.f)
  87. CHK_FIELD(tm_sec);
  88. CHK_FIELD(tm_min);
  89. CHK_FIELD(tm_hour);
  90. CHK_FIELD(tm_mday);
  91. CHK_FIELD(tm_mon);
  92. CHK_FIELD(tm_year);
  93. CHK_FIELD(tm_wday);
  94. CHK_FIELD(tm_yday);
  95. CHK_FIELD(tm_isdst);
  96. #undef CHK_FIELD
  97. }
  98. static void test_exp_get_gmt(abts_case *tc, void *data)
  99. {
  100. fspr_status_t rv;
  101. fspr_time_exp_t xt;
  102. fspr_time_t imp;
  103. fspr_int64_t hr_off_64;
  104. rv = fspr_time_exp_gmt(&xt, now);
  105. ABTS_TRUE(tc, rv == APR_SUCCESS);
  106. rv = fspr_time_exp_get(&imp, &xt);
  107. if (rv == APR_ENOTIMPL) {
  108. ABTS_NOT_IMPL(tc, "fspr_time_exp_get");
  109. }
  110. ABTS_TRUE(tc, rv == APR_SUCCESS);
  111. hr_off_64 = (fspr_int64_t) xt.tm_gmtoff * APR_USEC_PER_SEC;
  112. ABTS_TRUE(tc, now + hr_off_64 == imp);
  113. }
  114. static void test_exp_get_lt(abts_case *tc, void *data)
  115. {
  116. fspr_status_t rv;
  117. fspr_time_exp_t xt;
  118. fspr_time_t imp;
  119. fspr_int64_t hr_off_64;
  120. rv = fspr_time_exp_lt(&xt, now);
  121. ABTS_TRUE(tc, rv == APR_SUCCESS);
  122. rv = fspr_time_exp_get(&imp, &xt);
  123. if (rv == APR_ENOTIMPL) {
  124. ABTS_NOT_IMPL(tc, "fspr_time_exp_get");
  125. }
  126. ABTS_TRUE(tc, rv == APR_SUCCESS);
  127. hr_off_64 = (fspr_int64_t) xt.tm_gmtoff * APR_USEC_PER_SEC;
  128. ABTS_TRUE(tc, now + hr_off_64 == imp);
  129. }
  130. static void test_imp_gmt(abts_case *tc, void *data)
  131. {
  132. fspr_status_t rv;
  133. fspr_time_exp_t xt;
  134. fspr_time_t imp;
  135. rv = fspr_time_exp_gmt(&xt, now);
  136. ABTS_TRUE(tc, rv == APR_SUCCESS);
  137. rv = fspr_time_exp_gmt_get(&imp, &xt);
  138. if (rv == APR_ENOTIMPL) {
  139. ABTS_NOT_IMPL(tc, "fspr_time_exp_gmt_get");
  140. }
  141. ABTS_TRUE(tc, rv == APR_SUCCESS);
  142. ABTS_TRUE(tc, now == imp);
  143. }
  144. static void test_rfcstr(abts_case *tc, void *data)
  145. {
  146. fspr_status_t rv;
  147. char str[STR_SIZE];
  148. rv = fspr_rfc822_date(str, now);
  149. if (rv == APR_ENOTIMPL) {
  150. ABTS_NOT_IMPL(tc, "fspr_rfc822_date");
  151. }
  152. ABTS_TRUE(tc, rv == APR_SUCCESS);
  153. ABTS_STR_EQUAL(tc, "Sat, 14 Sep 2002 19:05:36 GMT", str);
  154. }
  155. static void test_ctime(abts_case *tc, void *data)
  156. {
  157. fspr_status_t rv;
  158. char fspr_str[STR_SIZE];
  159. char libc_str[STR_SIZE];
  160. fspr_time_t now_sec = fspr_time_sec(now);
  161. time_t posix_sec = (time_t) now_sec;
  162. rv = fspr_ctime(fspr_str, now);
  163. if (rv == APR_ENOTIMPL) {
  164. ABTS_NOT_IMPL(tc, "fspr_ctime");
  165. }
  166. ABTS_TRUE(tc, rv == APR_SUCCESS);
  167. strcpy(libc_str, ctime(&posix_sec));
  168. *strchr(libc_str, '\n') = '\0';
  169. ABTS_STR_EQUAL(tc, libc_str, fspr_str);
  170. }
  171. static void test_strftime(abts_case *tc, void *data)
  172. {
  173. fspr_status_t rv;
  174. fspr_time_exp_t xt;
  175. char *str = NULL;
  176. fspr_size_t sz;
  177. rv = fspr_time_exp_gmt(&xt, now);
  178. str = fspr_palloc(p, STR_SIZE + 1);
  179. rv = fspr_strftime(str, &sz, STR_SIZE, "%R %A %d %B %Y", &xt);
  180. if (rv == APR_ENOTIMPL) {
  181. ABTS_NOT_IMPL(tc, "fspr_strftime");
  182. }
  183. ABTS_TRUE(tc, rv == APR_SUCCESS);
  184. ABTS_STR_EQUAL(tc, "19:05 Saturday 14 September 2002", str);
  185. }
  186. static void test_strftimesmall(abts_case *tc, void *data)
  187. {
  188. fspr_status_t rv;
  189. fspr_time_exp_t xt;
  190. char str[STR_SIZE];
  191. fspr_size_t sz;
  192. rv = fspr_time_exp_gmt(&xt, now);
  193. rv = fspr_strftime(str, &sz, STR_SIZE, "%T", &xt);
  194. if (rv == APR_ENOTIMPL) {
  195. ABTS_NOT_IMPL(tc, "fspr_strftime");
  196. }
  197. ABTS_TRUE(tc, rv == APR_SUCCESS);
  198. ABTS_STR_EQUAL(tc, "19:05:36", str);
  199. }
  200. static void test_exp_tz(abts_case *tc, void *data)
  201. {
  202. fspr_status_t rv;
  203. fspr_time_exp_t xt;
  204. fspr_int32_t hr_off = -5 * 3600; /* 5 hours in seconds */
  205. rv = fspr_time_exp_tz(&xt, now, hr_off);
  206. if (rv == APR_ENOTIMPL) {
  207. ABTS_NOT_IMPL(tc, "fspr_time_exp_tz");
  208. }
  209. ABTS_TRUE(tc, rv == APR_SUCCESS);
  210. ABTS_TRUE(tc, (xt.tm_usec == 186711) &&
  211. (xt.tm_sec == 36) &&
  212. (xt.tm_min == 5) &&
  213. (xt.tm_hour == 14) &&
  214. (xt.tm_mday == 14) &&
  215. (xt.tm_mon == 8) &&
  216. (xt.tm_year == 102) &&
  217. (xt.tm_wday == 6) &&
  218. (xt.tm_yday == 256));
  219. }
  220. static void test_strftimeoffset(abts_case *tc, void *data)
  221. {
  222. fspr_status_t rv;
  223. fspr_time_exp_t xt;
  224. char str[STR_SIZE];
  225. fspr_size_t sz;
  226. fspr_int32_t hr_off = -5 * 3600; /* 5 hours in seconds */
  227. fspr_time_exp_tz(&xt, now, hr_off);
  228. rv = fspr_strftime(str, &sz, STR_SIZE, "%T", &xt);
  229. if (rv == APR_ENOTIMPL) {
  230. ABTS_NOT_IMPL(tc, "fspr_strftime");
  231. }
  232. ABTS_TRUE(tc, rv == APR_SUCCESS);
  233. }
  234. /* 0.9.4 and earlier rejected valid dates in 2038 */
  235. static void test_2038(abts_case *tc, void *data)
  236. {
  237. fspr_time_exp_t xt;
  238. fspr_time_t t;
  239. /* 2038-01-19T03:14:07.000000Z */
  240. xt.tm_year = 138;
  241. xt.tm_mon = 0;
  242. xt.tm_mday = 19;
  243. xt.tm_hour = 3;
  244. xt.tm_min = 14;
  245. xt.tm_sec = 7;
  246. APR_ASSERT_SUCCESS(tc, "explode January 19th, 2038",
  247. fspr_time_exp_get(&t, &xt));
  248. }
  249. abts_suite *testtime(abts_suite *suite)
  250. {
  251. suite = ADD_SUITE(suite)
  252. abts_run_test(suite, test_now, NULL);
  253. abts_run_test(suite, test_gmtstr, NULL);
  254. abts_run_test(suite, test_exp_lt, NULL);
  255. abts_run_test(suite, test_exp_get_gmt, NULL);
  256. abts_run_test(suite, test_exp_get_lt, NULL);
  257. abts_run_test(suite, test_imp_gmt, NULL);
  258. abts_run_test(suite, test_rfcstr, NULL);
  259. abts_run_test(suite, test_ctime, NULL);
  260. abts_run_test(suite, test_strftime, NULL);
  261. abts_run_test(suite, test_strftimesmall, NULL);
  262. abts_run_test(suite, test_exp_tz, NULL);
  263. abts_run_test(suite, test_strftimeoffset, NULL);
  264. abts_run_test(suite, test_2038, NULL);
  265. return suite;
  266. }