1 /* $NetBSD: t_mktime.c,v 1.5 2012/03/18 07:33:58 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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 the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <atf-c.h>
30 
31 #include <err.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <time.h>
35 
36 ATF_TC(localtime_r_gmt);
37 ATF_TC_HEAD(localtime_r_gmt, tc)
38 {
39 	atf_tc_set_md_var(tc, "descr", "Test that localtime_r(3) "
40 	    "returns localtime, not GMT (PR lib/28324)");
41 }
42 
43 ATF_TC_BODY(localtime_r_gmt, tc)
44 {
45 	struct tm *t;
46 	struct tm tt;
47 	time_t x;
48 
49 	x = time(NULL);
50 	localtime_r(&x, &tt);
51 	t = localtime(&x);
52 
53 	if (t->tm_sec != tt.tm_sec || t->tm_min != tt.tm_min ||
54 	    t->tm_hour != tt.tm_hour || t->tm_mday != tt.tm_mday)
55 		atf_tc_fail("inconsistencies between "
56 		    "localtime(3) and localtime_r(3)");
57 }
58 
59 ATF_TC(mktime_negyear);
60 ATF_TC_HEAD(mktime_negyear, tc)
61 {
62 	atf_tc_set_md_var(tc, "descr", "Test mktime(3) with negative year");
63 }
64 
65 ATF_TC_BODY(mktime_negyear, tc)
66 {
67 #ifdef __FreeBSD__
68 	atf_tc_expect_fail("needs work");
69 #endif
70 	struct tm tms;
71 	time_t t;
72 
73 	(void)memset(&tms, 0, sizeof(tms));
74 	tms.tm_year = ~0;
75 
76 	errno = 0;
77 	t = mktime(&tms);
78 	ATF_REQUIRE_ERRNO(0, t != (time_t)-1);
79 }
80 
81 ATF_TC(timegm_epoch);
82 ATF_TC_HEAD(timegm_epoch, tc)
83 {
84 	atf_tc_set_md_var(tc, "descr", "Test timegm(3) close to the epoch");
85 }
86 
87 ATF_TC_BODY(timegm_epoch, tc)
88 {
89 	struct tm tms;
90 	time_t t;
91 
92 	/* midnight on 1 Jan 1970 */
93 	(void)memset(&tms, 0, sizeof(tms));
94 	errno = 0;
95 	tms.tm_year = 1970 - 1900;
96 	tms.tm_mday = 1;
97 	t = timegm(&tms);
98 	ATF_REQUIRE_ERRNO(0, t == (time_t)0);
99 
100 	/* one second after midnight on 1 Jan 1970 */
101 	(void)memset(&tms, 0, sizeof(tms));
102 	errno = 0;
103 	tms.tm_year = 1970 - 1900;
104 	tms.tm_mday = 1;
105 	tms.tm_sec = 1;
106 	t = timegm(&tms);
107 	ATF_REQUIRE_ERRNO(0, t == (time_t)1);
108 
109 	/*
110 	 * 1969-12-31 23:59:59 = one second before the epoch.
111 	 * Result should be -1 with errno = 0.
112 	 */
113 	(void)memset(&tms, 0, sizeof(tms));
114 	errno = 0;
115 	tms.tm_year = 1969 - 1900;
116 	tms.tm_mon = 12 - 1;
117 	tms.tm_mday = 31;
118 	tms.tm_hour = 23;
119 	tms.tm_min = 59;
120 	tms.tm_sec = 59;
121 	t = timegm(&tms);
122 	ATF_REQUIRE_ERRNO(0, t == (time_t)-1);
123 
124 	/*
125 	 * Another way of getting one second before the epoch:
126 	 * Set date to 1 Jan 1970, and time to -1 second.
127 	 */
128 	(void)memset(&tms, 0, sizeof(tms));
129 	errno = 0;
130 	tms.tm_year = 1970 - 1900;
131 	tms.tm_mday = 1;
132 	tms.tm_sec = -1;
133 	t = timegm(&tms);
134 	ATF_REQUIRE_ERRNO(0, t == (time_t)-1);
135 
136 	/*
137 	 * Two seconds before the epoch.
138 	 */
139 	(void)memset(&tms, 0, sizeof(tms));
140 	errno = 0;
141 	tms.tm_year = 1970 - 1900;
142 	tms.tm_mday = 1;
143 	tms.tm_sec = -2;
144 	t = timegm(&tms);
145 	ATF_REQUIRE_ERRNO(0, t == (time_t)-2);
146 
147 }
148 
149 
150 ATF_TP_ADD_TCS(tp)
151 {
152 
153 	ATF_TP_ADD_TC(tp, localtime_r_gmt);
154 	ATF_TP_ADD_TC(tp, mktime_negyear);
155 	ATF_TP_ADD_TC(tp, timegm_epoch);
156 
157 	return atf_no_error();
158 }
159