t_mktime.c revision 1.3 1 1.3 apb /* $NetBSD: t_mktime.c,v 1.3 2011/12/17 19:04:07 apb Exp $ */
2 1.1 pgoyette
3 1.1 pgoyette /*-
4 1.1 pgoyette * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.1 pgoyette * All rights reserved.
6 1.1 pgoyette *
7 1.1 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.1 pgoyette * modification, are permitted provided that the following conditions
9 1.1 pgoyette * are met:
10 1.1 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.1 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.1 pgoyette * documentation and/or other materials provided with the distribution.
15 1.1 pgoyette *
16 1.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 pgoyette * POSSIBILITY OF SUCH DAMAGE.
27 1.1 pgoyette */
28 1.1 pgoyette
29 1.1 pgoyette #include <atf-c.h>
30 1.1 pgoyette
31 1.1 pgoyette #include <err.h>
32 1.1 pgoyette #include <errno.h>
33 1.1 pgoyette #include <string.h>
34 1.1 pgoyette #include <time.h>
35 1.1 pgoyette
36 1.3 apb ATF_TC(mktime_negyear);
37 1.1 pgoyette
38 1.3 apb ATF_TC_HEAD(mktime_negyear, tc)
39 1.1 pgoyette {
40 1.1 pgoyette
41 1.1 pgoyette atf_tc_set_md_var(tc, "descr", "Test mktime(3) with negative year");
42 1.1 pgoyette }
43 1.1 pgoyette
44 1.3 apb ATF_TC_BODY(mktime_negyear, tc)
45 1.1 pgoyette {
46 1.1 pgoyette struct tm tms;
47 1.1 pgoyette
48 1.1 pgoyette (void)memset(&tms, 0, sizeof(tms));
49 1.1 pgoyette tms.tm_year = ~0;
50 1.1 pgoyette
51 1.1 pgoyette errno = 0;
52 1.1 pgoyette
53 1.1 pgoyette ATF_REQUIRE_ERRNO(errno, mktime(&tms) != (time_t)-1);
54 1.1 pgoyette }
55 1.1 pgoyette
56 1.3 apb ATF_TC(timegm_epoch);
57 1.3 apb
58 1.3 apb ATF_TC_HEAD(timegm_epoch, tc)
59 1.3 apb {
60 1.3 apb
61 1.3 apb atf_tc_set_md_var(tc, "descr", "Test timegm(3) close to the epoch");
62 1.3 apb }
63 1.3 apb
64 1.3 apb ATF_TC_BODY(timegm_epoch, tc)
65 1.3 apb {
66 1.3 apb struct tm tms;
67 1.3 apb
68 1.3 apb /* midnight on 1 Jan 1970 */
69 1.3 apb (void)memset(&tms, 0, sizeof(tms));
70 1.3 apb errno = 0;
71 1.3 apb tms.tm_year = 1970 - 1900;
72 1.3 apb tms.tm_mday = 1;
73 1.3 apb ATF_REQUIRE_ERRNO(errno, timegm(&tms) == (time_t)0);
74 1.3 apb
75 1.3 apb /* one second after midnight on 1 Jan 1970 */
76 1.3 apb (void)memset(&tms, 0, sizeof(tms));
77 1.3 apb errno = 0;
78 1.3 apb tms.tm_year = 1970 - 1900;
79 1.3 apb tms.tm_mday = 1;
80 1.3 apb tms.tm_sec = 1;
81 1.3 apb ATF_REQUIRE_ERRNO(errno, timegm(&tms) == (time_t)1);
82 1.3 apb
83 1.3 apb /*
84 1.3 apb * 1969-12-31 23:59:59 = one second before the epoch.
85 1.3 apb * Result should be -1 with errno = 0.
86 1.3 apb */
87 1.3 apb (void)memset(&tms, 0, sizeof(tms));
88 1.3 apb errno = 0;
89 1.3 apb tms.tm_year = 1969 - 1900;
90 1.3 apb tms.tm_mon = 12 - 1;
91 1.3 apb tms.tm_mday = 31;
92 1.3 apb tms.tm_hour = 23;
93 1.3 apb tms.tm_min = 59;
94 1.3 apb tms.tm_sec = 59;
95 1.3 apb ATF_REQUIRE_ERRNO(errno, timegm(&tms) == (time_t)-1);
96 1.3 apb
97 1.3 apb /*
98 1.3 apb * Another way of getting one second before the epoch:
99 1.3 apb * Set date to 1 Jan 1970, and time to -1 second.
100 1.3 apb */
101 1.3 apb (void)memset(&tms, 0, sizeof(tms));
102 1.3 apb errno = 0;
103 1.3 apb tms.tm_year = 1970 - 1900;
104 1.3 apb tms.tm_mday = 1;
105 1.3 apb tms.tm_sec = -1;
106 1.3 apb ATF_REQUIRE_ERRNO(errno, timegm(&tms) == (time_t)-1);
107 1.3 apb
108 1.3 apb /*
109 1.3 apb * Two seconds before the epoch.
110 1.3 apb */
111 1.3 apb (void)memset(&tms, 0, sizeof(tms));
112 1.3 apb errno = 0;
113 1.3 apb tms.tm_year = 1970 - 1900;
114 1.3 apb tms.tm_mday = 1;
115 1.3 apb tms.tm_sec = -2;
116 1.3 apb ATF_REQUIRE_ERRNO(errno, timegm(&tms) == (time_t)-2);
117 1.3 apb
118 1.3 apb }
119 1.3 apb
120 1.3 apb
121 1.1 pgoyette ATF_TP_ADD_TCS(tp)
122 1.1 pgoyette {
123 1.1 pgoyette
124 1.3 apb ATF_TP_ADD_TC(tp, mktime_negyear);
125 1.3 apb ATF_TP_ADD_TC(tp, timegm_epoch);
126 1.1 pgoyette
127 1.1 pgoyette return atf_no_error();
128 1.1 pgoyette }
129