summaryrefslogtreecommitdiff
path: root/tests/lib/libutil/t_parsedate.c
blob: 8a4c569829fb51a867b8385abf5bd40079c6d2a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* $NetBSD: t_parsedate.c,v 1.13 2014/10/08 17:23:03 apb Exp $ */
/*-
 * Copyright (c) 2010 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__RCSID("$NetBSD: t_parsedate.c,v 1.13 2014/10/08 17:23:03 apb Exp $");

#include <atf-c.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <util.h>

/*
 * ANY is used as a placeholder for values that do not need to be
 * checked.  The actual value is arbitrary.  We don't use -1
 * because some tests might want to use -1 as a literal value.
 */
#define ANY -30215

/* parsecheck --
 * call parsedate(), then call time_to_tm() on the result,
 * and check that year/month/day/hour/minute/second are as expected.
 *
 * time_to_tm should usually be localtime_r or gmtime_r.
 *
 * Don't check values specified as ANY.
 */
static void
parsecheck(const char *datestr, const time_t *reftime, const int *zoff,
	struct tm * time_to_tm(const time_t *, struct tm *),
	int year, int month, int day, int hour, int minute, int second)
{
	time_t t;
	struct tm tm;
	char argstr[128];

	/*
	 * printable version of the args.
	 *
	 * Note that printf("%.*d", 0, 0)) prints nothing at all,
	 * while printf("%.*d", 1, val) prints the value as usual.
	 */
	snprintf(argstr, sizeof(argstr), "%s%s%s, %s%.*jd, %s%.*d",
		/* NULL or \"<datestr>\" */
		(datestr ? "\"" : ""),
		(datestr ? datestr : "NULL"),
		(datestr ? "\"" : ""),
		/* NULL or *reftime */
		(reftime ? "" : "NULL"),
		(reftime ? 1 : 0), 
		(reftime ? (intmax_t)*reftime : (intmax_t)0), 
		/* NULL or *zoff */
		(zoff ? "" : "NULL"),
		(zoff ? 1 : 0), 
		(zoff ? *zoff : 0));

	ATF_CHECK_MSG((t = parsedate(datestr, reftime, zoff)) != -1,
	    "parsedate(%s) returned -1\n", argstr);
	ATF_CHECK(time_to_tm(&t, &tm) != NULL);
	if (year != ANY)
		ATF_CHECK_MSG(tm.tm_year + 1900 == year,
		    "parsedate(%s) expected year %d got %d (+1900)\n",
		    argstr, year, (int)tm.tm_year);
	if (month != ANY)
		ATF_CHECK_MSG(tm.tm_mon + 1 == month,
		    "parsedate(%s) expected month %d got %d (+1)\n",
		    argstr, month, (int)tm.tm_mon);
	if (day != ANY)
		ATF_CHECK_MSG(tm.tm_mday == day,
		    "parsedate(%s) expected day %d got %d\n",
		    argstr, day, (int)tm.tm_mday);
	if (hour != ANY)
		ATF_CHECK_MSG(tm.tm_hour == hour,
		    "parsedate(%s) expected hour %d got %d\n",
		    argstr, hour, (int)tm.tm_hour);
	if (minute != ANY)
		ATF_CHECK_MSG(tm.tm_min == minute,
		    "parsedate(%s) expected minute %d got %d\n",
		    argstr, minute, (int)tm.tm_min);
	if (second != ANY)
		ATF_CHECK_MSG(tm.tm_sec == second,
		    "parsedate(%s) expected second %d got %d\n",
		    argstr, second, (int)tm.tm_sec);
}

ATF_TC(dates);

ATF_TC_HEAD(dates, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test unambiguous dates"
	    " (PR lib/44255)");
}

ATF_TC_BODY(dates, tc)
{

	parsecheck("9/10/69", NULL, NULL, localtime_r,
		2069, 9, 10, 0, 0, 0); /* year < 70: add 2000 */
	parsecheck("9/10/70", NULL, NULL, localtime_r,
		1970, 9, 10, 0, 0, 0); /* 70 <= year < 100: add 1900 */
	parsecheck("69-09-10", NULL, NULL, localtime_r,
		69, 9, 10, 0, 0, 0); /* ISO8601 year remains unchanged */
	parsecheck("70-09-10", NULL, NULL, localtime_r,
		70, 9, 10, 0, 0, 0); /* ISO8601 year remains unchanged */
	parsecheck("2006-11-17", NULL, NULL, localtime_r,
		2006, 11, 17, 0, 0, 0);
	parsecheck("10/1/2000", NULL, NULL, localtime_r,
		2000, 10, 1, 0, 0, 0); /* month/day/year */
	parsecheck("20 Jun 1994", NULL, NULL, localtime_r,
		1994, 6, 20, 0, 0, 0);
	parsecheck("23jun2001", NULL, NULL, localtime_r,
		2001, 6, 23, 0, 0, 0);
	parsecheck("1-sep-06", NULL, NULL, localtime_r,
		2006, 9, 1, 0, 0, 0);
	parsecheck("1/11", NULL, NULL, localtime_r,
		ANY, 1, 11, 0, 0, 0); /* month/day */
	parsecheck("1500-01-02", NULL, NULL, localtime_r,
		1500, 1, 2, 0, 0, 0);
	parsecheck("9999-12-21", NULL, NULL, localtime_r,
		9999, 12, 21, 0, 0, 0);
}

ATF_TC(times);

ATF_TC_HEAD(times, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test times"
	    " (PR lib/44255)");
}

ATF_TC_BODY(times, tc)
{

	parsecheck("10:01", NULL, NULL, localtime_r,
		ANY, ANY, ANY, 10, 1, 0);
	parsecheck("10:12pm", NULL, NULL, localtime_r,
		ANY, ANY, ANY, 22, 12, 0);
	parsecheck("12:11:01.000012", NULL, NULL, localtime_r,
		ANY, ANY, ANY, 12, 11, 1);
	parsecheck("12:21-0500", NULL, NULL, gmtime_r,
		ANY, ANY, ANY, 12+5, 21, 0);
}

ATF_TC(dsttimes);

ATF_TC_HEAD(dsttimes, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test DST transition times"
	    " (PR lib/47916)");
}

ATF_TC_BODY(dsttimes, tc)
{
	struct tm tm;
	time_t t;
	int tzoff;

	putenv(__UNCONST("TZ=EST"));
	tzset();
	parsecheck("12:0", NULL, NULL, localtime_r,
		ANY, ANY, ANY, 12, 0, 0);

	putenv(__UNCONST("TZ=Japan"));
	tzset();
	parsecheck("12:0", NULL, NULL, localtime_r,
		ANY, ANY, ANY, 12, 0, 0);

	/*
	 * When the effective local time is Tue Jul  9 13:21:53 BST 2013,
	 * check mktime("14:00")
	 */
	putenv(__UNCONST("TZ=Europe/London"));
	tzset();
	tm = (struct tm){
		.tm_year = 2013-1900, .tm_mon = 7-1, .tm_mday = 9,
		.tm_hour = 13, .tm_min = 21, .tm_sec = 53,
		.tm_isdst = 0 };
	t = mktime(&tm);
	ATF_CHECK(t != (time_t)-1);
	parsecheck("14:00", &t, NULL, localtime_r,
		2013, 7, 9, 14, 0, 0);
	tzoff = -60; /* British Summer Time */
	parsecheck("14:00", &t, &tzoff, localtime_r,
		2013, 7, 9, 14, 0, 0);
}

ATF_TC(relative);

ATF_TC_HEAD(relative, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test relative items"
	    " (PR lib/44255)");
}

ATF_TC_BODY(relative, tc)
{

	ATF_CHECK(parsedate("-1 month", NULL, NULL) != -1);
	ATF_CHECK(parsedate("last friday", NULL, NULL) != -1);
	ATF_CHECK(parsedate("one week ago", NULL, NULL) != -1);
	ATF_CHECK(parsedate("this thursday", NULL, NULL) != -1);
	ATF_CHECK(parsedate("next sunday", NULL, NULL) != -1);
	ATF_CHECK(parsedate("+2 years", NULL, NULL) != -1);
}

ATF_TC(atsecs);

ATF_TC_HEAD(atsecs, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test seconds past the epoch");
}

ATF_TC_BODY(atsecs, tc)
{
	int tzoff;

	/* "@0" -> (time_t)0, regardless of timezone */
	ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
	putenv(__UNCONST("TZ=Europe/Berlin"));
	tzset();
	ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
	putenv(__UNCONST("TZ=America/New_York"));
	tzset();
	ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
	tzoff = 0;
	ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
	tzoff = 3600;
	ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
	tzoff = -3600;
	ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);

	/* -1 or other negative numbers are not errors */
	errno = 0;
	ATF_CHECK(parsedate("@-1", NULL, &tzoff) == (time_t)-1 && errno == 0);
	ATF_CHECK(parsedate("@-2", NULL, &tzoff) == (time_t)-2 && errno == 0);

	/* junk is an error */
	errno = 0;
	ATF_CHECK(parsedate("@junk", NULL, NULL) == (time_t)-1 && errno != 0);
}

ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, dates);
	ATF_TP_ADD_TC(tp, times);
	ATF_TP_ADD_TC(tp, dsttimes);
	ATF_TP_ADD_TC(tp, relative);
	ATF_TP_ADD_TC(tp, atsecs);

	return atf_no_error();
}