1/* $NetBSD: t_strptime.c,v 1.12 2015/10/31 02:25:11 christos Exp $ */
2
3/*-
4 * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by David Laight.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__COPYRIGHT("@(#) Copyright (c) 2008\
34 The NetBSD Foundation, inc. All rights reserved.");
35__RCSID("$NetBSD: t_strptime.c,v 1.12 2015/10/31 02:25:11 christos Exp $");
36
37#include <time.h>
38#include <stdlib.h>
39#include <stdio.h>
40
41#include <atf-c.h>
42
43static void
44h_pass(const char *buf, const char *fmt, int len,
45    int tm_sec, int tm_min, int tm_hour, int tm_mday,
46    int tm_mon, int tm_year, int tm_wday, int tm_yday)
47{
48	struct tm tm = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, NULL };
49	const char *ret, *exp;
50
51	exp = buf + len;
52	ret = strptime(buf, fmt, &tm);
53
54#ifdef __FreeBSD__
55	ATF_CHECK_MSG(ret == exp,
56	    "strptime(\"%s\", \"%s\", tm): incorrect return code: "
57	    "expected: %p, got: %p", buf, fmt, exp, ret);
58
59#define H_REQUIRE_FIELD(field)						\
60		ATF_CHECK_MSG(tm.field == field,			\
61		    "strptime(\"%s\", \"%s\", tm): incorrect %s: "	\
62		    "expected: %d, but got: %d", buf, fmt,		\
63		    ___STRING(field), field, tm.field)
64#else
65	ATF_REQUIRE_MSG(ret == exp,
66	    "strptime(\"%s\", \"%s\", tm): incorrect return code: "
67	    "expected: %p, got: %p", buf, fmt, exp, ret);
68
69#define H_REQUIRE_FIELD(field)						\
70		ATF_REQUIRE_MSG(tm.field == field,			\
71		    "strptime(\"%s\", \"%s\", tm): incorrect %s: "	\
72		    "expected: %d, but got: %d", buf, fmt,		\
73		    ___STRING(field), field, tm.field)
74#endif
75
76	H_REQUIRE_FIELD(tm_sec);
77	H_REQUIRE_FIELD(tm_min);
78	H_REQUIRE_FIELD(tm_hour);
79	H_REQUIRE_FIELD(tm_mday);
80	H_REQUIRE_FIELD(tm_mon);
81	H_REQUIRE_FIELD(tm_year);
82	H_REQUIRE_FIELD(tm_wday);
83	H_REQUIRE_FIELD(tm_yday);
84
85#undef H_REQUIRE_FIELD
86}
87
88static void
89h_fail(const char *buf, const char *fmt)
90{
91	struct tm tm = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, NULL };
92
93#ifdef __FreeBSD__
94	ATF_CHECK_MSG(strptime(buf, fmt, &tm) == NULL, "strptime(\"%s\", "
95	    "\"%s\", &tm) should fail, but it didn't", buf, fmt);
96#else
97	ATF_REQUIRE_MSG(strptime(buf, fmt, &tm) == NULL, "strptime(\"%s\", "
98	    "\"%s\", &tm) should fail, but it didn't", buf, fmt);
99#endif
100}
101
102static struct {
103	const char *name;
104	long offs;
105} zt[] = {
106	{ "Z",				0 },
107	{ "UT",				0 },
108	{ "UTC",			0 },
109	{ "GMT",			0 },
110	{ "EST",			-18000 },
111	{ "EDT",			-14400 },
112	{ "CST",			-21600 },
113	{ "CDT",			-18000 },
114	{ "MST",			-25200 },
115	{ "MDT",			-21600 },
116	{ "PST",			-28800 },
117	{ "PDT",			-25200 },
118
119	{ "VST",			-1 },
120	{ "VDT",			-1 },
121
122	{ "+03",			10800 },
123	{ "-03",			-10800 },
124	{ "+0403",			14580 },
125	{ "-0403",			-14580 },
126	{ "+04:03",			14580 },
127	{ "-04:03",			-14580 },
128	{ "+14:00",			50400 },
129	{ "-14:00",			-50400 },
130	{ "+23:59",			86340 },
131	{ "-23:59",			-86340 },
132
133	{ "1",				-1 },
134	{ "03",				-1 },
135	{ "0304",			-1 },
136	{ "+1",				-1 },
137	{ "-203",			-1 },
138	{ "+12345",			-1 },
139	{ "+12:345",			-1 },
140	{ "+123:45",			-1 },
141	{ "+2400",			-1 },
142	{ "-2400",			-1 },
143	{ "+1060",			-1 },
144	{ "-1060",			-1 },
145
146	{ "A",				-3600 },
147	{ "B",				-7200 },
148	{ "C",				-10800 },
149	{ "D",				-14400 },
150	{ "E",				-18000 },
151	{ "F",				-21600 },
152	{ "G",				-25200 },
153	{ "H",				-28800 },
154	{ "I",				-32400 },
155	{ "L",				-39600 },
156	{ "M",				-43200 },
157	{ "N",				3600 },
158	{ "O",				7200 },
159	{ "P",				10800 },
160	{ "Q",				14400 },
161	{ "R",				18000 },
162	{ "T",				25200 },
163	{ "U",				28800 },
164	{ "V",				32400 },
165	{ "W",				36000 },
166	{ "X",				39600 },
167	{ "Y",				43200 },
168
169	{ "J",				-2 },
170
171	{ "America/Los_Angeles",	-28800 },
172	{ "America/New_York",		-18000 },
173	{ "EST4EDT",			-14400 },
174
175	{ "Bogus",			-1 },
176};
177
178static void
179ztest1(const char *name, const char *fmt, long value)
180{
181	struct tm tm;
182	char *rv;
183
184	memset(&tm, 0, sizeof(tm));
185	if ((rv = strptime(name, fmt, &tm)) == NULL)
186		tm.tm_gmtoff = -1;
187	else if (rv == name && fmt[1] == 'Z')
188		value = 0;
189
190	switch (value) {
191	case -2:
192		value = -timezone;
193		break;
194	case -1:
195		if (fmt[1] == 'Z')
196			value = 0;
197		break;
198	default:
199		break;
200	}
201
202	ATF_REQUIRE_MSG(tm.tm_gmtoff == value,
203	    "strptime(\"%s\", \"%s\", &tm): "
204	    "expected: tm.tm_gmtoff=%ld, got: tm.tm_gmtoff=%ld",
205	    name, fmt, value, tm.tm_gmtoff);
206	printf("%s %s %ld\n", name, fmt, tm.tm_gmtoff);
207}
208
209static void
210ztest(const char *fmt)
211{
212	setenv("TZ", "US/Eastern", 1);
213	ztest1("GMT", fmt, 0);
214	ztest1("UTC", fmt, 0);
215	ztest1("US/Eastern", fmt, -18000);
216	for (size_t i = 0; i < __arraycount(zt); i++)
217		ztest1(zt[i].name, fmt, zt[i].offs);
218}
219
220ATF_TC(common);
221
222ATF_TC_HEAD(common, tc)
223{
224
225	atf_tc_set_md_var(tc, "descr", "Checks strptime(3): various checks");
226}
227
228ATF_TC_BODY(common, tc)
229{
230
231#ifdef __FreeBSD__
232	atf_tc_expect_fail("There are various issues with strptime on FreeBSD");
233#endif
234
235	h_pass("Tue Jan 20 23:27:46 1998", "%a %b %d %T %Y",
236		24, 46, 27, 23, 20, 0, 98, 2, 19);
237	h_pass("Tue Jan 20 23:27:46 1998", "%a %b %d %H:%M:%S %Y",
238		24, 46, 27, 23, 20, 0, 98, 2, 19);
239	h_pass("Tue Jan 20 23:27:46 1998", "%c",
240		24, 46, 27, 23, 20, 0, 98, 2, 19);
241	h_pass("Fri Mar  4 20:05:34 2005", "%a %b %e %H:%M:%S %Y",
242		24, 34, 5, 20, 4, 2, 105, 5, 62);
243	h_pass("5\t3  4 8pm:05:34 2005", "%w%n%m%t%d%n%k%p:%M:%S %Y",
244		21, 34, 5, 20, 4, 2, 105, 5, 62);
245	h_pass("Fri Mar  4 20:05:34 2005", "%c",
246		24, 34, 5, 20, 4, 2, 105, 5, 62);
247}
248
249ATF_TC(day);
250
251ATF_TC_HEAD(day, tc)
252{
253
254	atf_tc_set_md_var(tc, "descr",
255			  "Checks strptime(3) day name conversions [aA]");
256}
257
258ATF_TC_BODY(day, tc)
259{
260
261	h_pass("Sun", "%a", 3, -1, -1, -1, -1, -1, -1, 0, -1);
262	h_pass("Sunday", "%a", 6, -1, -1, -1, -1, -1, -1, 0, -1);
263	h_pass("Mon", "%a", 3, -1, -1, -1, -1, -1, -1, 1, -1);
264	h_pass("Monday", "%a", 6, -1, -1, -1, -1, -1, -1, 1, -1);
265	h_pass("Tue", "%a", 3, -1, -1, -1, -1, -1, -1, 2, -1);
266	h_pass("Tuesday", "%a", 7, -1, -1, -1, -1, -1, -1, 2, -1);
267	h_pass("Wed", "%a", 3, -1, -1, -1, -1, -1, -1, 3, -1);
268	h_pass("Wednesday", "%a", 9, -1, -1, -1, -1, -1, -1, 3, -1);
269	h_pass("Thu", "%a", 3, -1, -1, -1, -1, -1, -1, 4, -1);
270	h_pass("Thursday", "%a", 8, -1, -1, -1, -1, -1, -1, 4, -1);
271	h_pass("Fri", "%a", 3, -1, -1, -1, -1, -1, -1, 5, -1);
272	h_pass("Friday", "%a", 6, -1, -1, -1, -1, -1, -1, 5, -1);
273	h_pass("Sat", "%a", 3, -1, -1, -1, -1, -1, -1, 6, -1);
274	h_pass("Saturday", "%a", 8, -1, -1, -1, -1, -1, -1, 6, -1);
275	h_pass("Saturn", "%a", 3, -1, -1, -1, -1, -1, -1, 6, -1);
276	h_fail("Moon", "%a");
277	h_pass("Sun", "%A", 3, -1, -1, -1, -1, -1, -1, 0, -1);
278	h_pass("Sunday", "%A", 6, -1, -1, -1, -1, -1, -1, 0, -1);
279	h_pass("Mon", "%A", 3, -1, -1, -1, -1, -1, -1, 1, -1);
280	h_pass("Monday", "%A", 6, -1, -1, -1, -1, -1, -1, 1, -1);
281	h_pass("Tue", "%A", 3, -1, -1, -1, -1, -1, -1, 2, -1);
282	h_pass("Tuesday", "%A", 7, -1, -1, -1, -1, -1, -1, 2, -1);
283	h_pass("Wed", "%A", 3, -1, -1, -1, -1, -1, -1, 3, -1);
284	h_pass("Wednesday", "%A", 9, -1, -1, -1, -1, -1, -1, 3, -1);
285	h_pass("Thu", "%A", 3, -1, -1, -1, -1, -1, -1, 4, -1);
286	h_pass("Thursday", "%A", 8, -1, -1, -1, -1, -1, -1, 4, -1);
287	h_pass("Fri", "%A", 3, -1, -1, -1, -1, -1, -1, 5, -1);
288	h_pass("Friday", "%A", 6, -1, -1, -1, -1, -1, -1, 5, -1);
289	h_pass("Sat", "%A", 3, -1, -1, -1, -1, -1, -1, 6, -1);
290	h_pass("Saturday", "%A", 8, -1, -1, -1, -1, -1, -1, 6, -1);
291	h_pass("Saturn", "%A", 3, -1, -1, -1, -1, -1, -1, 6, -1);
292	h_fail("Moon", "%A");
293
294	h_pass("mon", "%a", 3, -1, -1, -1, -1, -1, -1, 1, -1);
295	h_pass("tueSDay", "%A", 7, -1, -1, -1, -1, -1, -1, 2, -1);
296	h_pass("sunday", "%A", 6, -1, -1, -1, -1, -1, -1, 0, -1);
297#ifdef __NetBSD__
298	h_fail("sunday", "%EA");
299#else
300	h_pass("Sunday", "%EA", 6, -1, -1, -1, -1, -1, -1, 0, -1);
301#endif
302	h_pass("SaturDay", "%A", 8, -1, -1, -1, -1, -1, -1, 6, -1);
303#ifdef __NetBSD__
304	h_fail("SaturDay", "%OA");
305#else
306	h_pass("SaturDay", "%OA", 8, -1, -1, -1, -1, -1, -1, 6, -1);
307#endif
308}
309
310ATF_TC(hour);
311
312ATF_TC_HEAD(hour, tc)
313{
314
315	atf_tc_set_md_var(tc, "descr",
316			  "Checks strptime(3) hour conversions [IH]");
317}
318
319ATF_TC_BODY(hour, tc)
320{
321
322	h_fail("00", "%I");
323	h_fail("13", "%I");
324
325	h_pass("00", "%H", 2, -1, -1, 0, -1, -1, -1, -1, -1);
326	h_pass("12", "%H", 2, -1, -1, 12, -1, -1, -1, -1, -1);
327	h_pass("23", "%H", 2, -1, -1, 23, -1, -1, -1, -1, -1);
328	h_fail("24", "%H");
329}
330
331
332ATF_TC(month);
333
334ATF_TC_HEAD(month, tc)
335{
336
337	atf_tc_set_md_var(tc, "descr",
338			  "Checks strptime(3) month name conversions [bB]");
339}
340
341ATF_TC_BODY(month, tc)
342{
343
344	h_pass("Jan", "%b", 3, -1, -1, -1, -1, 0, -1, -1, -1);
345	h_pass("January", "%b", 7, -1, -1, -1, -1, 0, -1, -1, -1);
346	h_pass("Feb", "%b", 3, -1, -1, -1, -1, 1, -1, -1, -1);
347	h_pass("February", "%b", 8, -1, -1, -1, -1, 1, -1, -1, -1);
348	h_pass("Mar", "%b", 3, -1, -1, -1, -1, 2, -1, -1, -1);
349	h_pass("March", "%b", 5, -1, -1, -1, -1, 2, -1, -1, -1);
350	h_pass("Apr", "%b", 3, -1, -1, -1, -1, 3, -1, -1, -1);
351	h_pass("April", "%b", 5, -1, -1, -1, -1, 3, -1, -1, -1);
352	h_pass("May", "%b", 3, -1, -1, -1, -1, 4, -1, -1, -1);
353	h_pass("Jun", "%b", 3, -1, -1, -1, -1, 5, -1, -1, -1);
354	h_pass("June", "%b", 4, -1, -1, -1, -1, 5, -1, -1, -1);
355	h_pass("Jul", "%b", 3, -1, -1, -1, -1, 6, -1, -1, -1);
356	h_pass("July", "%b", 4, -1, -1, -1, -1, 6, -1, -1, -1);
357	h_pass("Aug", "%b", 3, -1, -1, -1, -1, 7, -1, -1, -1);
358	h_pass("August", "%b", 6, -1, -1, -1, -1, 7, -1, -1, -1);
359	h_pass("Sep", "%b", 3, -1, -1, -1, -1, 8, -1, -1, -1);
360	h_pass("September", "%b", 9, -1, -1, -1, -1, 8, -1, -1, -1);
361	h_pass("Oct", "%b", 3, -1, -1, -1, -1, 9, -1, -1, -1);
362	h_pass("October", "%b", 7, -1, -1, -1, -1, 9, -1, -1, -1);
363	h_pass("Nov", "%b", 3, -1, -1, -1, -1, 10, -1, -1, -1);
364	h_pass("November", "%b", 8, -1, -1, -1, -1, 10, -1, -1, -1);
365	h_pass("Dec", "%b", 3, -1, -1, -1, -1, 11, -1, -1, -1);
366	h_pass("December", "%b", 8, -1, -1, -1, -1, 11, -1, -1, -1);
367	h_pass("Mayor", "%b", 3, -1, -1, -1, -1, 4, -1, -1, -1);
368	h_pass("Mars", "%b", 3, -1, -1, -1, -1, 2, -1, -1, -1);
369	h_fail("Rover", "%b");
370	h_pass("Jan", "%B", 3, -1, -1, -1, -1, 0, -1, -1, -1);
371	h_pass("January", "%B", 7, -1, -1, -1, -1, 0, -1, -1, -1);
372	h_pass("Feb", "%B", 3, -1, -1, -1, -1, 1, -1, -1, -1);
373	h_pass("February", "%B", 8, -1, -1, -1, -1, 1, -1, -1, -1);
374	h_pass("Mar", "%B", 3, -1, -1, -1, -1, 2, -1, -1, -1);
375	h_pass("March", "%B", 5, -1, -1, -1, -1, 2, -1, -1, -1);
376	h_pass("Apr", "%B", 3, -1, -1, -1, -1, 3, -1, -1, -1);
377	h_pass("April", "%B", 5, -1, -1, -1, -1, 3, -1, -1, -1);
378	h_pass("May", "%B", 3, -1, -1, -1, -1, 4, -1, -1, -1);
379	h_pass("Jun", "%B", 3, -1, -1, -1, -1, 5, -1, -1, -1);
380	h_pass("June", "%B", 4, -1, -1, -1, -1, 5, -1, -1, -1);
381	h_pass("Jul", "%B", 3, -1, -1, -1, -1, 6, -1, -1, -1);
382	h_pass("July", "%B", 4, -1, -1, -1, -1, 6, -1, -1, -1);
383	h_pass("Aug", "%B", 3, -1, -1, -1, -1, 7, -1, -1, -1);
384	h_pass("August", "%B", 6, -1, -1, -1, -1, 7, -1, -1, -1);
385	h_pass("Sep", "%B", 3, -1, -1, -1, -1, 8, -1, -1, -1);
386	h_pass("September", "%B", 9, -1, -1, -1, -1, 8, -1, -1, -1);
387	h_pass("Oct", "%B", 3, -1, -1, -1, -1, 9, -1, -1, -1);
388	h_pass("October", "%B", 7, -1, -1, -1, -1, 9, -1, -1, -1);
389	h_pass("Nov", "%B", 3, -1, -1, -1, -1, 10, -1, -1, -1);
390	h_pass("November", "%B", 8, -1, -1, -1, -1, 10, -1, -1, -1);
391	h_pass("Dec", "%B", 3, -1, -1, -1, -1, 11, -1, -1, -1);
392	h_pass("December", "%B", 8, -1, -1, -1, -1, 11, -1, -1, -1);
393	h_pass("Mayor", "%B", 3, -1, -1, -1, -1, 4, -1, -1, -1);
394	h_pass("Mars", "%B", 3, -1, -1, -1, -1, 2, -1, -1, -1);
395	h_fail("Rover", "%B");
396
397	h_pass("september", "%b", 9, -1, -1, -1, -1, 8, -1, -1, -1);
398	h_pass("septembe", "%B", 3, -1, -1, -1, -1, 8, -1, -1, -1);
399}
400
401ATF_TC(seconds);
402
403ATF_TC_HEAD(seconds, tc)
404{
405
406	atf_tc_set_md_var(tc, "descr",
407			  "Checks strptime(3) seconds conversions [S]");
408}
409
410ATF_TC_BODY(seconds, tc)
411{
412
413	h_pass("0", "%S", 1, 0, -1, -1, -1, -1, -1, -1, -1);
414	h_pass("59", "%S", 2, 59, -1, -1, -1, -1, -1, -1, -1);
415	h_pass("60", "%S", 2, 60, -1, -1, -1, -1, -1, -1, -1);
416	h_pass("61", "%S", 2, 61, -1, -1, -1, -1, -1, -1, -1);
417	h_fail("62", "%S");
418}
419
420ATF_TC(year);
421
422ATF_TC_HEAD(year, tc)
423{
424
425	atf_tc_set_md_var(tc, "descr",
426			  "Checks strptime(3) century/year conversions [CyY]");
427}
428
429ATF_TC_BODY(year, tc)
430{
431
432	h_pass("x20y", "x%Cy", 4, -1, -1, -1, -1, -1, 100, -1, -1);
433	h_pass("x84y", "x%yy", 4, -1, -1, -1, -1, -1, 84, -1, -1);
434	h_pass("x2084y", "x%C%yy", 6, -1, -1, -1, -1, -1, 184, -1, -1);
435	h_pass("x8420y", "x%y%Cy", 6, -1, -1, -1, -1, -1, 184, -1, -1);
436	h_pass("%20845", "%%%C%y5", 6, -1, -1, -1, -1, -1, 184, -1, -1);
437	h_fail("%", "%E%");
438
439	h_pass("1980", "%Y", 4, -1, -1, -1, -1, -1, 80, -1, -1);
440	h_pass("1980", "%EY", 4, -1, -1, -1, -1, -1, 80, -1, -1);
441}
442
443ATF_TC(zone);
444
445ATF_TC_HEAD(zone, tc)
446{
447
448	atf_tc_set_md_var(tc, "descr",
449			  "Checks strptime(3) timezone conversion [z]");
450}
451
452
453ATF_TC_BODY(zone, tc)
454{
455	ztest("%z");
456}
457
458ATF_TC(Zone);
459
460ATF_TC_HEAD(Zone, tc)
461{
462
463	atf_tc_set_md_var(tc, "descr",
464			  "Checks strptime(3) timezone conversion [Z]");
465}
466
467
468ATF_TC_BODY(Zone, tc)
469{
470	ztest("%z");
471}
472
473ATF_TP_ADD_TCS(tp)
474{
475
476	ATF_TP_ADD_TC(tp, common);
477	ATF_TP_ADD_TC(tp, day);
478	ATF_TP_ADD_TC(tp, hour);
479	ATF_TP_ADD_TC(tp, month);
480	ATF_TP_ADD_TC(tp, seconds);
481	ATF_TP_ADD_TC(tp, year);
482	ATF_TP_ADD_TC(tp, zone);
483	ATF_TP_ADD_TC(tp, Zone);
484
485	return atf_no_error();
486}
487