test-utc-mktime.c revision 67905cc9328270abbe5342b1e77d7ba4e8134552
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk/* Copyright (c) 2007-2017 Dovecot authors, see the included COPYING file */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk#include "test-lib.h"
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk#include "utc-mktime.h"
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkstruct test_utc_mktime {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk int year, month, day, hour, min, sec;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk time_t out;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk};
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkvoid test_utc_mktime(void)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk{
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk static const struct test_utc_mktime tests[] = {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk#ifdef TIME_T_SIGNED
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk { 1969, 12, 31, 23, 59, 59, -1 },
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk { 1901, 12, 13, 20, 45, 53, -2147483647 },
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk#endif
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk#if (TIME_T_MAX_BITS > 32 || !defined(TIME_T_SIGNED))
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk { 2106, 2, 7, 6, 28, 15, 4294967295 },
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk#endif
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk { 2007, 11, 7, 1, 7, 20, 1194397640 },
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk { 1970, 1, 1, 0, 0, 0, 0 },
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk { 2038, 1, 19, 3, 14, 7, 2147483647 },
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk { INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, -1 },
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk { 2038, 1, 19, 3, 14, 8, 2147483648 },
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk { 2106, 2, 7, 6, 28, 15, 4294967295 },
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk#if TIME_T_MAX_BITS > 32
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk { 2106, 2, 7, 6, 28, 16, 4294967296 },
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk#endif
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /* June leap second */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk { 2015, 6, 30, 23, 59, 59, 1435708799 },
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk { 2015, 6, 30, 23, 59, 60, 1435708799 },
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk { 2015, 7, 1, 0, 0, 0, 1435708800 },
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /* Invalid leap second */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk { 2017, 1, 24, 16, 40, 60, 1485276059 },
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /* Dec leap second */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk { 2016, 12, 31, 23, 59, 59, 1483228799 },
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk { 2016, 12, 31, 23, 59, 60, 1483228799 },
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk { 2017, 1, 1, 0, 0, 0, 1483228800 },
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk };
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk struct tm tm;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk unsigned int i;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk time_t t;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk bool success;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk for (i = 0; i < N_ELEMENTS(tests); i++) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk const struct test_utc_mktime *test = &tests[i];
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk i_zero(&tm);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk tm.tm_year = test->year - 1900;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk tm.tm_mon = test->month - 1;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk tm.tm_mday = test->day;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk tm.tm_hour = test->hour;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk tm.tm_min = test->min;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk tm.tm_sec = test->sec;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk t = utc_mktime(&tm);
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk success = t == test->out;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk test_out_reason(t_strdup_printf("utc_mktime(%d)", i), success,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk success ? NULL : t_strdup_printf("%ld != %ld",
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk (long)t, (long)test->out));
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk}
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk