1 | //===-- Unittests for asctime ---------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "src/errno/libc_errno.h" |
10 | #include "src/time/asctime.h" |
11 | #include "test/UnitTest/Test.h" |
12 | #include "test/src/time/TmHelper.h" |
13 | |
14 | static inline char *call_asctime(struct tm *tm_data, int year, int month, |
15 | int mday, int hour, int min, int sec, int wday, |
16 | int yday) { |
17 | LIBC_NAMESPACE::tmhelper::testing::initialize_tm_data( |
18 | tm_data, year, month, mday, hour, min, sec, wday, yday); |
19 | return LIBC_NAMESPACE::asctime(timeptr: tm_data); |
20 | } |
21 | |
22 | TEST(LlvmLibcAsctime, Nullptr) { |
23 | char *result; |
24 | result = LIBC_NAMESPACE::asctime(timeptr: nullptr); |
25 | ASSERT_ERRNO_EQ(EINVAL); |
26 | ASSERT_STREQ(nullptr, result); |
27 | } |
28 | |
29 | // Weekdays are in the range 0 to 6. Test passing invalid value in wday. |
30 | TEST(LlvmLibcAsctime, InvalidWday) { |
31 | struct tm tm_data; |
32 | |
33 | // Test with wday = -1. |
34 | call_asctime(tm_data: &tm_data, |
35 | year: 1970, // year |
36 | month: 1, // month |
37 | mday: 1, // day |
38 | hour: 0, // hr |
39 | min: 0, // min |
40 | sec: 0, // sec |
41 | wday: -1, // wday |
42 | yday: 0); // yday |
43 | ASSERT_ERRNO_EQ(EINVAL); |
44 | |
45 | // Test with wday = 7. |
46 | call_asctime(tm_data: &tm_data, |
47 | year: 1970, // year |
48 | month: 1, // month |
49 | mday: 1, // day |
50 | hour: 0, // hr |
51 | min: 0, // min |
52 | sec: 0, // sec |
53 | wday: 7, // wday |
54 | yday: 0); // yday |
55 | ASSERT_ERRNO_EQ(EINVAL); |
56 | } |
57 | |
58 | // Months are from January to December. Test passing invalid value in month. |
59 | TEST(LlvmLibcAsctime, InvalidMonth) { |
60 | struct tm tm_data; |
61 | |
62 | // Test with month = 0. |
63 | call_asctime(tm_data: &tm_data, |
64 | year: 1970, // year |
65 | month: 0, // month |
66 | mday: 1, // day |
67 | hour: 0, // hr |
68 | min: 0, // min |
69 | sec: 0, // sec |
70 | wday: 4, // wday |
71 | yday: 0); // yday |
72 | ASSERT_ERRNO_EQ(EINVAL); |
73 | |
74 | // Test with month = 13. |
75 | call_asctime(tm_data: &tm_data, |
76 | year: 1970, // year |
77 | month: 13, // month |
78 | mday: 1, // day |
79 | hour: 0, // hr |
80 | min: 0, // min |
81 | sec: 0, // sec |
82 | wday: 4, // wday |
83 | yday: 0); // yday |
84 | ASSERT_ERRNO_EQ(EINVAL); |
85 | } |
86 | |
87 | TEST(LlvmLibcAsctime, ValidWeekdays) { |
88 | struct tm tm_data; |
89 | char *result; |
90 | // 1970-01-01 00:00:00. |
91 | result = call_asctime(tm_data: &tm_data, |
92 | year: 1970, // year |
93 | month: 1, // month |
94 | mday: 1, // day |
95 | hour: 0, // hr |
96 | min: 0, // min |
97 | sec: 0, // sec |
98 | wday: 4, // wday |
99 | yday: 0); // yday |
100 | ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n" , result); |
101 | |
102 | // 1970-01-03 00:00:00. |
103 | result = call_asctime(tm_data: &tm_data, |
104 | year: 1970, // year |
105 | month: 1, // month |
106 | mday: 3, // day |
107 | hour: 0, // hr |
108 | min: 0, // min |
109 | sec: 0, // sec |
110 | wday: 6, // wday |
111 | yday: 0); // yday |
112 | ASSERT_STREQ("Sat Jan 3 00:00:00 1970\n" , result); |
113 | |
114 | // 1970-01-04 00:00:00. |
115 | result = call_asctime(tm_data: &tm_data, |
116 | year: 1970, // year |
117 | month: 1, // month |
118 | mday: 4, // day |
119 | hour: 0, // hr |
120 | min: 0, // min |
121 | sec: 0, // sec |
122 | wday: 0, // wday |
123 | yday: 0); // yday |
124 | ASSERT_STREQ("Sun Jan 4 00:00:00 1970\n" , result); |
125 | } |
126 | |
127 | TEST(LlvmLibcAsctime, ValidMonths) { |
128 | struct tm tm_data; |
129 | char *result; |
130 | // 1970-01-01 00:00:00. |
131 | result = call_asctime(tm_data: &tm_data, |
132 | year: 1970, // year |
133 | month: 1, // month |
134 | mday: 1, // day |
135 | hour: 0, // hr |
136 | min: 0, // min |
137 | sec: 0, // sec |
138 | wday: 4, // wday |
139 | yday: 0); // yday |
140 | ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n" , result); |
141 | |
142 | // 1970-02-01 00:00:00. |
143 | result = call_asctime(tm_data: &tm_data, |
144 | year: 1970, // year |
145 | month: 2, // month |
146 | mday: 1, // day |
147 | hour: 0, // hr |
148 | min: 0, // min |
149 | sec: 0, // sec |
150 | wday: 0, // wday |
151 | yday: 0); // yday |
152 | ASSERT_STREQ("Sun Feb 1 00:00:00 1970\n" , result); |
153 | |
154 | // 1970-12-31 23:59:59. |
155 | result = call_asctime(tm_data: &tm_data, |
156 | year: 1970, // year |
157 | month: 12, // month |
158 | mday: 31, // day |
159 | hour: 23, // hr |
160 | min: 59, // min |
161 | sec: 59, // sec |
162 | wday: 4, // wday |
163 | yday: 0); // yday |
164 | ASSERT_STREQ("Thu Dec 31 23:59:59 1970\n" , result); |
165 | } |
166 | |
167 | TEST(LlvmLibcAsctime, EndOf32BitEpochYear) { |
168 | struct tm tm_data; |
169 | char *result; |
170 | // Test for maximum value of a signed 32-bit integer. |
171 | // Test implementation can encode time for Tue 19 January 2038 03:14:07 UTC. |
172 | result = call_asctime(tm_data: &tm_data, |
173 | year: 2038, // year |
174 | month: 1, // month |
175 | mday: 19, // day |
176 | hour: 3, // hr |
177 | min: 14, // min |
178 | sec: 7, // sec |
179 | wday: 2, // wday |
180 | yday: 7); // yday |
181 | ASSERT_STREQ("Tue Jan 19 03:14:07 2038\n" , result); |
182 | } |
183 | |
184 | TEST(LlvmLibcAsctime, Max64BitYear) { |
185 | if (sizeof(time_t) == 4) |
186 | return; |
187 | // Mon Jan 1 12:50:50 2170 (200 years from 1970), |
188 | struct tm tm_data; |
189 | char *result; |
190 | result = call_asctime(tm_data: &tm_data, |
191 | year: 2170, // year |
192 | month: 1, // month |
193 | mday: 1, // day |
194 | hour: 12, // hr |
195 | min: 50, // min |
196 | sec: 50, // sec |
197 | wday: 1, // wday |
198 | yday: 50); // yday |
199 | ASSERT_STREQ("Mon Jan 1 12:50:50 2170\n" , result); |
200 | |
201 | // Test for Tue Jan 1 12:50:50 in 2,147,483,647th year. |
202 | // This test would cause buffer overflow and thus asctime returns nullptr. |
203 | result = call_asctime(tm_data: &tm_data, |
204 | year: 2147483647, // year |
205 | month: 1, // month |
206 | mday: 1, // day |
207 | hour: 12, // hr |
208 | min: 50, // min |
209 | sec: 50, // sec |
210 | wday: 2, // wday |
211 | yday: 50); // yday |
212 | ASSERT_ERRNO_EQ(EOVERFLOW); |
213 | ASSERT_STREQ(nullptr, result); |
214 | } |
215 | |