]> granicus.if.org Git - icinga2/blob - test/icinga-legacytimeperiod.cpp
Implement unit tests for the time period parser
[icinga2] / test / icinga-legacytimeperiod.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "icinga/legacytimeperiod.hpp"
21 #include <boost/test/unit_test.hpp>
22
23 using namespace icinga;
24
25 BOOST_AUTO_TEST_SUITE(icinga_legacytimeperiod);
26
27 struct GlobalTimezoneFixture
28 {
29         char *tz;
30
31         GlobalTimezoneFixture(void)
32         {
33                 tz = getenv("TZ");
34                 setenv("TZ", "", 1);
35                 tzset();
36         }
37
38         ~GlobalTimezoneFixture(void)
39         {
40                 if (tz)
41                         setenv("TZ", tz, 1);
42                 else
43                         unsetenv("TZ");
44                 tzset();
45         }
46 };
47
48 BOOST_GLOBAL_FIXTURE(GlobalTimezoneFixture);
49
50 BOOST_AUTO_TEST_CASE(simple)
51 {
52         tm beg, end, ref;
53         timegm(&ref);
54
55         // check parsing of "YYYY-MM-DD" specs
56         LegacyTimePeriod::ParseTimeSpec("2016-01-01", &beg, &end, &ref);
57         BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1451606400);
58         BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1451692800);
59
60         LegacyTimePeriod::ParseTimeSpec("2015-12-31", &beg, &end, &ref);
61         BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1451520000);
62         BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1451606400);
63
64         BOOST_CHECK_THROW(LegacyTimePeriod::ParseTimeSpec("2015-12-32", &beg, &end, &ref),
65             std::invalid_argument);
66
67         BOOST_CHECK_THROW(LegacyTimePeriod::ParseTimeSpec("2015-28-01", &beg, &end, &ref),
68             std::invalid_argument);
69
70         // check parsing of "day X" and "day -X" specs
71         ref.tm_year = 2016 - 1900;
72         ref.tm_mon = 1;
73         LegacyTimePeriod::ParseTimeSpec("day 2", &beg, &end, &ref);
74         BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1454371200); // 2016-02-02
75         BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1454457600); // 2016-02-03
76
77         ref.tm_year = 2018 - 1900;
78         ref.tm_mon = 11;
79         LegacyTimePeriod::ParseTimeSpec("day 31", &beg, &end, &ref);
80         BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1546214400); // 2018-12-31
81         BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1546300800); // 2019-01-01
82
83         ref.tm_year = 2012 - 1900;
84         ref.tm_mon = 6;
85         LegacyTimePeriod::ParseTimeSpec("day -1", &beg, &end, &ref);
86         BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1343692800); // 2012-07-31
87         BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1343779200); // 2012-08-01
88
89         ref.tm_year = 2016 - 1900; // leap year
90         ref.tm_mon = 1;
91         LegacyTimePeriod::ParseTimeSpec("day -1", &beg, &end, &ref);
92         BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1456704000); // 2016-02-29
93         BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1456790400); // 2016-03-01
94 }
95
96 BOOST_AUTO_TEST_SUITE_END()