1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) 2011, 2016 embedded brains GmbH & Co. KG |
---|
5 | * |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | #ifdef HAVE_CONFIG_H |
---|
29 | #include "config.h" |
---|
30 | #endif |
---|
31 | |
---|
32 | #include <tmacros.h> |
---|
33 | |
---|
34 | #include <time.h> |
---|
35 | #include <string.h> |
---|
36 | |
---|
37 | #include <rtems.h> |
---|
38 | #include <rtems/rtems/clockimpl.h> |
---|
39 | |
---|
40 | const char rtems_test_name[] = "SP 2038"; |
---|
41 | |
---|
42 | /* forward declarations to avoid warnings */ |
---|
43 | rtems_task Init(rtems_task_argument argument); |
---|
44 | |
---|
45 | #define ASSERT_SC(sc) rtems_test_assert((sc) == RTEMS_SUCCESSFUL) |
---|
46 | |
---|
47 | static const uint32_t sample_seconds [] = { |
---|
48 | 571213695UL, |
---|
49 | 602836095UL, |
---|
50 | 634372095UL, |
---|
51 | 665908095UL, |
---|
52 | 697444095UL, |
---|
53 | 729066495UL, |
---|
54 | 760602495UL, |
---|
55 | 792138495UL, |
---|
56 | 823674495UL, |
---|
57 | 855296895UL, |
---|
58 | 886832895UL, |
---|
59 | 918368895UL, |
---|
60 | 949904895UL, |
---|
61 | 981527295UL, |
---|
62 | 1013063295UL, |
---|
63 | 1044599295UL, |
---|
64 | 1076135295UL, |
---|
65 | 1107757695UL, |
---|
66 | 1139293695UL, |
---|
67 | 1170829695UL, |
---|
68 | 1202365695UL, |
---|
69 | 1233988095UL, |
---|
70 | 1265524095UL, |
---|
71 | 1297060095UL, |
---|
72 | 1328596095UL, |
---|
73 | 1360218495UL, |
---|
74 | 1391754495UL, |
---|
75 | 1423290495UL, |
---|
76 | 1454826495UL, |
---|
77 | 1486448895UL, |
---|
78 | 1517984895UL, |
---|
79 | 1549520895UL, |
---|
80 | 1581056895UL, |
---|
81 | 1612679295UL, |
---|
82 | 1644215295UL, |
---|
83 | 1675751295UL, |
---|
84 | 1707287295UL, |
---|
85 | 1738909695UL, |
---|
86 | 1770445695UL, |
---|
87 | 1801981695UL, |
---|
88 | 1833517695UL, |
---|
89 | 1865140095UL, |
---|
90 | 1896676095UL, |
---|
91 | 1928212095UL, |
---|
92 | 1959748095UL, |
---|
93 | 1991370495UL, |
---|
94 | 2022906495UL, |
---|
95 | 2054442495UL, |
---|
96 | 2085978495UL, |
---|
97 | 2117600895UL, |
---|
98 | 2149136895UL, |
---|
99 | 2180672895UL, |
---|
100 | 2212208895UL, |
---|
101 | 2243831295UL, |
---|
102 | 2275367295UL, |
---|
103 | 2306903295UL, |
---|
104 | 2338439295UL, |
---|
105 | 2370061695UL, |
---|
106 | 2401597695UL, |
---|
107 | 2433133695UL, |
---|
108 | 2464669695UL, |
---|
109 | 2496292095UL, |
---|
110 | 2527828095UL, |
---|
111 | 2559364095UL, |
---|
112 | 2590900095UL, |
---|
113 | 2622522495UL, |
---|
114 | 2654058495UL, |
---|
115 | 2685594495UL, |
---|
116 | 2717130495UL, |
---|
117 | 2748752895UL, |
---|
118 | 2780288895UL, |
---|
119 | 2811824895UL, |
---|
120 | 2843360895UL, |
---|
121 | 2874983295UL, |
---|
122 | 2906519295UL, |
---|
123 | 2938055295UL, |
---|
124 | 2969591295UL, |
---|
125 | 3001213695UL, |
---|
126 | 3032749695UL, |
---|
127 | 3064285695UL, |
---|
128 | 3095821695UL, |
---|
129 | 3127444095UL, |
---|
130 | 3158980095UL, |
---|
131 | 3190516095UL, |
---|
132 | 3222052095UL, |
---|
133 | 3253674495UL, |
---|
134 | 3285210495UL, |
---|
135 | 3316746495UL, |
---|
136 | 3348282495UL, |
---|
137 | 3379904895UL, |
---|
138 | 3411440895UL, |
---|
139 | 3442976895UL, |
---|
140 | 3474512895UL, |
---|
141 | 3506135295UL, |
---|
142 | 3537671295UL, |
---|
143 | 3569207295UL, |
---|
144 | 3600743295UL, |
---|
145 | 3632365695UL, |
---|
146 | 3663901695UL, |
---|
147 | 3695437695UL, |
---|
148 | 3726973695UL, |
---|
149 | 3758596095UL, |
---|
150 | 3790132095UL, |
---|
151 | 3821668095UL, |
---|
152 | 3853204095UL, |
---|
153 | 3884826495UL, |
---|
154 | 3916362495UL, |
---|
155 | 3947898495UL, |
---|
156 | 3979434495UL, |
---|
157 | 4011056895UL, |
---|
158 | 4042592895UL, |
---|
159 | 4074128895UL |
---|
160 | }; |
---|
161 | |
---|
162 | static const rtems_time_of_day nearly_problem_2038 = { |
---|
163 | .year = 2038, |
---|
164 | .month = 1, |
---|
165 | .day = 19, |
---|
166 | .hour = 3, |
---|
167 | .minute = 14, |
---|
168 | .second = 7 |
---|
169 | }; |
---|
170 | |
---|
171 | static const rtems_time_of_day problem_2038 = { |
---|
172 | .year = 2038, |
---|
173 | .month = 1, |
---|
174 | .day = 19, |
---|
175 | .hour = 3, |
---|
176 | .minute = 14, |
---|
177 | .second = 8 |
---|
178 | }; |
---|
179 | |
---|
180 | static const rtems_time_of_day tod_to_seconds_base = { |
---|
181 | .year = 0, |
---|
182 | .month = 2, |
---|
183 | .day = 7, |
---|
184 | .hour = 6, |
---|
185 | .minute = 28, |
---|
186 | .second = 15 |
---|
187 | }; |
---|
188 | |
---|
189 | static const rtems_time_of_day problem_2100 = { |
---|
190 | .year = 2100, |
---|
191 | .month = 2, |
---|
192 | .day = 28, |
---|
193 | .hour = 0, |
---|
194 | .minute = 0, |
---|
195 | .second = 0 |
---|
196 | }; |
---|
197 | |
---|
198 | static const rtems_time_of_day problem_2100_2 = { |
---|
199 | .year = 2100, |
---|
200 | .month = 2, |
---|
201 | .day = 29, |
---|
202 | .hour = 0, |
---|
203 | .minute = 0, |
---|
204 | .second = 0 |
---|
205 | }; |
---|
206 | |
---|
207 | static void test_tod_to_seconds(void) |
---|
208 | { |
---|
209 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
210 | size_t i = 0; |
---|
211 | size_t n = sizeof(sample_seconds) / sizeof(sample_seconds [0]); |
---|
212 | |
---|
213 | for (i = 0; i < n; ++i) { |
---|
214 | rtems_time_of_day tod = tod_to_seconds_base; |
---|
215 | uint32_t seconds = 0; |
---|
216 | rtems_interval seconds_as_interval = 0; |
---|
217 | |
---|
218 | tod.year = 1988 + i; |
---|
219 | seconds = _TOD_To_seconds(&tod); |
---|
220 | rtems_test_assert(seconds == sample_seconds [i]); |
---|
221 | |
---|
222 | sc = rtems_clock_set(&tod); |
---|
223 | ASSERT_SC(sc); |
---|
224 | |
---|
225 | sc = rtems_clock_get_seconds_since_epoch(&seconds_as_interval); |
---|
226 | ASSERT_SC(sc); |
---|
227 | rtems_test_assert(seconds_as_interval == sample_seconds [i]); |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | static void test_problem_year(void) |
---|
232 | { |
---|
233 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
234 | bool time_t_is_32_bit = sizeof(time_t) == 4; |
---|
235 | |
---|
236 | if (time_t_is_32_bit) { |
---|
237 | const rtems_time_of_day *nearly_problem = NULL; |
---|
238 | const rtems_time_of_day *problem = NULL; |
---|
239 | rtems_time_of_day now; |
---|
240 | |
---|
241 | nearly_problem = &nearly_problem_2038; |
---|
242 | problem = &problem_2038; |
---|
243 | |
---|
244 | sc = rtems_clock_set(nearly_problem); |
---|
245 | ASSERT_SC(sc); |
---|
246 | sc = rtems_clock_get_tod(&now); |
---|
247 | ASSERT_SC(sc); |
---|
248 | rtems_test_assert(memcmp(&now, nearly_problem, sizeof(now)) == 0); |
---|
249 | |
---|
250 | sc = rtems_clock_set(problem); |
---|
251 | ASSERT_SC(sc); |
---|
252 | sc = rtems_clock_get_tod(&now); |
---|
253 | ASSERT_SC(sc); |
---|
254 | rtems_test_assert(memcmp(&now, problem, sizeof(now)) == 0); |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | static void test_leap_year(void) |
---|
259 | { |
---|
260 | rtems_status_code test_status; |
---|
261 | const rtems_time_of_day *problem = &problem_2100; |
---|
262 | const rtems_time_of_day *problem2 = &problem_2100_2; |
---|
263 | // 2100 is not a leap year, so it should have 28 days |
---|
264 | test_status = _TOD_Validate(problem, TOD_ENABLE_TICKS_VALIDATION); |
---|
265 | rtems_test_assert(test_status == RTEMS_INVALID_CLOCK); |
---|
266 | test_status = _TOD_Validate(problem2, TOD_ENABLE_TICKS_VALIDATION); |
---|
267 | rtems_test_assert(test_status == RTEMS_INVALID_CLOCK); |
---|
268 | } |
---|
269 | |
---|
270 | static bool test_year_is_leap_year(uint32_t year) |
---|
271 | { |
---|
272 | return (((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0); |
---|
273 | } |
---|
274 | |
---|
275 | static void test_every_day(void) |
---|
276 | { |
---|
277 | rtems_time_of_day every_day = { |
---|
278 | .year = 1970, |
---|
279 | .month = 1, |
---|
280 | .day = 1, |
---|
281 | .hour = 0, |
---|
282 | .minute = 1, |
---|
283 | .second = 2 |
---|
284 | }; |
---|
285 | const int days_per_month[2][12] = { |
---|
286 | { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, |
---|
287 | { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } |
---|
288 | }; |
---|
289 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
290 | rtems_time_of_day now; |
---|
291 | |
---|
292 | for (every_day.year = 1988; every_day.year <= 2099; ++every_day.year) { |
---|
293 | int leap_year = test_year_is_leap_year(every_day.year) ? 1 : 0; |
---|
294 | for (every_day.month = 1; every_day.month <= 12; ++every_day.month) { |
---|
295 | int days = days_per_month[leap_year][every_day.month - 1]; |
---|
296 | for (every_day.day = 1; every_day.day <= days; ++every_day.day) { |
---|
297 | sc = rtems_clock_set(&every_day); |
---|
298 | ASSERT_SC(sc); |
---|
299 | sc = rtems_clock_get_tod(&now); |
---|
300 | ASSERT_SC(sc); |
---|
301 | rtems_test_assert(memcmp(&now, &every_day, sizeof(now)) == 0); |
---|
302 | } |
---|
303 | } |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | rtems_task Init(rtems_task_argument argument) |
---|
308 | { |
---|
309 | TEST_BEGIN(); |
---|
310 | |
---|
311 | test_tod_to_seconds(); |
---|
312 | test_every_day(); |
---|
313 | test_problem_year(); |
---|
314 | test_leap_year(); |
---|
315 | |
---|
316 | TEST_END(); |
---|
317 | |
---|
318 | rtems_test_exit(0); |
---|
319 | } |
---|
320 | |
---|
321 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
322 | #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER |
---|
323 | |
---|
324 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
325 | |
---|
326 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
327 | |
---|
328 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
329 | |
---|
330 | #define CONFIGURE_INIT |
---|
331 | |
---|
332 | #include <rtems/confdefs.h> |
---|