1 /* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
2 
3 #include "test-lib.h"
4 #include "utc-mktime.h"
5 
6 struct test_utc_mktime {
7 	int year, month, day, hour, min, sec;
8 	time_t out;
9 };
10 
test_utc_mktime(void)11 void test_utc_mktime(void)
12 {
13 	static const struct test_utc_mktime tests[] = {
14 #ifdef TIME_T_SIGNED
15 		{ 1969, 12, 31, 23, 59, 59, -1 },
16 		{ 1901, 12, 13, 20, 45, 53, -2147483647 },
17 #endif
18 #if (TIME_T_MAX_BITS > 32 || !defined(TIME_T_SIGNED))
19 		{ 2106, 2, 7, 6, 28, 15, 4294967295 },
20 		{ 2038, 1, 19, 3, 14, 8, 2147483648 },
21 #endif
22 		{ 2007, 11, 7, 1, 7, 20, 1194397640 },
23 		{ 1970, 1, 1, 0, 0, 0, 0 },
24 		{ 2038, 1, 19, 3, 14, 7, 2147483647 },
25 		{ INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, -1 },
26 #if TIME_T_MAX_BITS > 32
27 		{ 2106, 2, 7, 6, 28, 16, 4294967296 },
28 #endif
29 		/* June leap second */
30 		{ 2015, 6, 30, 23, 59, 59, 1435708799 },
31 		{ 2015, 6, 30, 23, 59, 60, 1435708799 },
32 		{ 2015, 7, 1, 0, 0, 0, 1435708800 },
33 		/* Invalid leap second */
34 		{ 2017, 1, 24, 16, 40, 60, 1485276059 },
35 		/* Dec leap second */
36 		{ 2016, 12, 31, 23, 59, 59, 1483228799 },
37 		{ 2016, 12, 31, 23, 59, 60, 1483228799 },
38 		{ 2017, 1, 1, 0, 0, 0, 1483228800 },
39 	};
40 	struct tm tm;
41 	unsigned int i;
42 	time_t t;
43 	bool success;
44 
45 	for (i = 0; i < N_ELEMENTS(tests); i++) {
46 		const struct test_utc_mktime *test = &tests[i];
47 		i_zero(&tm);
48 		tm.tm_year = test->year - 1900;
49 		tm.tm_mon = test->month - 1;
50 		tm.tm_mday = test->day;
51 		tm.tm_hour = test->hour;
52 		tm.tm_min = test->min;
53 		tm.tm_sec = test->sec;
54 
55 		t = utc_mktime(&tm);
56 		success = t == test->out;
57 		test_out_reason(t_strdup_printf("utc_mktime(%d)", i), success,
58 				success ? NULL : t_strdup_printf("%ld != %ld",
59 							(long)t, (long)test->out));
60 	}
61 }
62