t_parsedate.c revision 1.10 1 /* $NetBSD: t_parsedate.c,v 1.10 2014/10/07 22:33:52 apb Exp $ */
2 /*-
3 * Copyright (c) 2010 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_parsedate.c,v 1.10 2014/10/07 22:33:52 apb Exp $");
33
34 #include <atf-c.h>
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <time.h>
38 #include <util.h>
39
40 /*
41 * ANY is used as a placeholder for values that do not need to be
42 * checked. The actual value is arbitrary. We don't use -1
43 * because some tests might want to use -1 as a literal value.
44 */
45 #define ANY -30215
46
47 /* parsecheck --
48 * call parsedate(), then call time_to_tm() on the result,
49 * and check that year/month/day/hour/minute/second are as expected.
50 *
51 * time_to_tm should usually be localtime_r or gmtime_r.
52 *
53 * Don't check values specified as ANY.
54 */
55 static void
56 parsecheck(const char *datestr, const time_t *reftime, const int *zoff,
57 struct tm * time_to_tm(const time_t *, struct tm *),
58 int year, int month, int day, int hour, int minute, int second)
59 {
60 time_t t;
61 struct tm tm;
62
63 ATF_CHECK_MSG((t = parsedate(datestr, reftime, zoff)) != -1,
64 "parsedate(\"%s\",,) returned -1\n", datestr);
65 ATF_CHECK(time_to_tm(&t, &tm) != NULL);
66 if (year != ANY)
67 ATF_CHECK_MSG(tm.tm_year + 1900 == year,
68 "parsedate(\"%s\",,) expected year %d got %d (+1900)\n",
69 datestr, year, (int)tm.tm_year);
70 if (month != ANY)
71 ATF_CHECK_MSG(tm.tm_mon + 1 == month,
72 "parsedate(\"%s\",,) expected month %d got %d (+1)\n",
73 datestr, month, (int)tm.tm_mon);
74 if (day != ANY)
75 ATF_CHECK_MSG(tm.tm_mday == day,
76 "parsedate(\"%s\",,) expected day %d got %d\n",
77 datestr, day, (int)tm.tm_mday);
78 if (hour != ANY)
79 ATF_CHECK_MSG(tm.tm_hour == hour,
80 "parsedate(\"%s\",,) expected hour %d got %d\n",
81 datestr, hour, (int)tm.tm_hour);
82 if (minute != ANY)
83 ATF_CHECK_MSG(tm.tm_min == minute,
84 "parsedate(\"%s\",,) expected minute %d got %d\n",
85 datestr, minute, (int)tm.tm_min);
86 if (second != ANY)
87 ATF_CHECK_MSG(tm.tm_sec == second,
88 "parsedate(\"%s\",,) expected second %d got %d\n",
89 datestr, second, (int)tm.tm_sec);
90 }
91
92 ATF_TC(dates);
93
94 ATF_TC_HEAD(dates, tc)
95 {
96 atf_tc_set_md_var(tc, "descr", "Test unambiguous dates"
97 " (PR lib/44255)");
98 }
99
100 ATF_TC_BODY(dates, tc)
101 {
102
103 parsecheck("9/10/69", NULL, NULL, localtime_r,
104 2069, 9, 10, 0, 0, 0); /* year < 70: add 2000 */
105 parsecheck("9/10/70", NULL, NULL, localtime_r,
106 1970, 9, 10, 0, 0, 0); /* 70 <= year < 100: add 1900 */
107 parsecheck("69-09-10", NULL, NULL, localtime_r,
108 69, 9, 10, 0, 0, 0); /* ISO8601 year remains unchanged */
109 parsecheck("70-09-10", NULL, NULL, localtime_r,
110 70, 9, 10, 0, 0, 0); /* ISO8601 year remains unchanged */
111 parsecheck("2006-11-17", NULL, NULL, localtime_r,
112 2006, 11, 17, 0, 0, 0);
113 parsecheck("10/1/2000", NULL, NULL, localtime_r,
114 2000, 10, 1, 0, 0, 0); /* month/day/year */
115 parsecheck("20 Jun 1994", NULL, NULL, localtime_r,
116 1994, 6, 20, 0, 0, 0);
117 parsecheck("23jun2001", NULL, NULL, localtime_r,
118 2001, 6, 23, 0, 0, 0);
119 parsecheck("1-sep-06", NULL, NULL, localtime_r,
120 2006, 9, 1, 0, 0, 0);
121 parsecheck("1/11", NULL, NULL, localtime_r,
122 ANY, 1, 11, 0, 0, 0); /* month/day */
123 parsecheck("1500-01-02", NULL, NULL, localtime_r,
124 1500, 1, 2, 0, 0, 0);
125 parsecheck("9999-12-21", NULL, NULL, localtime_r,
126 9999, 12, 21, 0, 0, 0);
127 }
128
129 ATF_TC(times);
130
131 ATF_TC_HEAD(times, tc)
132 {
133 atf_tc_set_md_var(tc, "descr", "Test times"
134 " (PR lib/44255)");
135 }
136
137 ATF_TC_BODY(times, tc)
138 {
139
140 parsecheck("10:01", NULL, NULL, localtime_r,
141 ANY, ANY, ANY, 10, 1, 0);
142 parsecheck("10:12pm", NULL, NULL, localtime_r,
143 ANY, ANY, ANY, 22, 12, 0);
144 parsecheck("12:11:01.000012", NULL, NULL, localtime_r,
145 ANY, ANY, ANY, 12, 11, 1);
146 parsecheck("12:21-0500", NULL, NULL, gmtime_r,
147 ANY, ANY, ANY, 12+5, 21, 0);
148 }
149
150 ATF_TC(relative);
151
152 ATF_TC_HEAD(relative, tc)
153 {
154 atf_tc_set_md_var(tc, "descr", "Test relative items"
155 " (PR lib/44255)");
156 }
157
158 ATF_TC_BODY(relative, tc)
159 {
160
161 ATF_CHECK(parsedate("-1 month", NULL, NULL) != -1);
162 ATF_CHECK(parsedate("last friday", NULL, NULL) != -1);
163 ATF_CHECK(parsedate("one week ago", NULL, NULL) != -1);
164 ATF_CHECK(parsedate("this thursday", NULL, NULL) != -1);
165 ATF_CHECK(parsedate("next sunday", NULL, NULL) != -1);
166 ATF_CHECK(parsedate("+2 years", NULL, NULL) != -1);
167 }
168
169 ATF_TC(atsecs);
170
171 ATF_TC_HEAD(atsecs, tc)
172 {
173 atf_tc_set_md_var(tc, "descr", "Test seconds past the epoch");
174 }
175
176 ATF_TC_BODY(atsecs, tc)
177 {
178 int tzoff;
179
180 /* "@0" -> (time_t)0, regardless of timezone */
181 ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
182 putenv(__UNCONST("TZ=Europe/Berlin"));
183 tzset();
184 ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
185 putenv(__UNCONST("TZ=America/New_York"));
186 tzset();
187 ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
188 tzoff = 0;
189 ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
190 tzoff = 3600;
191 ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
192 tzoff = -3600;
193 ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
194
195 /* -1 or other negative numbers are not errors */
196 errno = 0;
197 ATF_CHECK(parsedate("@-1", NULL, &tzoff) == (time_t)-1 && errno == 0);
198 ATF_CHECK(parsedate("@-2", NULL, &tzoff) == (time_t)-2 && errno == 0);
199
200 /* junk is an error */
201 errno = 0;
202 ATF_CHECK(parsedate("@junk", NULL, NULL) == (time_t)-1 && errno != 0);
203 }
204
205 ATF_TP_ADD_TCS(tp)
206 {
207 ATF_TP_ADD_TC(tp, dates);
208 ATF_TP_ADD_TC(tp, times);
209 ATF_TP_ADD_TC(tp, relative);
210 ATF_TP_ADD_TC(tp, atsecs);
211
212 return atf_no_error();
213 }
214