1 /* Copyright (C) 2019-2020 Greenbone Networks GmbH
2  *
3  * SPDX-License-Identifier: AGPL-3.0-or-later
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as
7  * published by the Free Software Foundation, either version 3 of the
8  * 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 Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "manage_utils.c"
20 
21 #include <cgreen/cgreen.h>
22 #include <cgreen/mocks.h>
23 
24 Describe (manage_utils);
BeforeEach(manage_utils)25 BeforeEach (manage_utils) {}
AfterEach(manage_utils)26 AfterEach (manage_utils) {}
27 
28 /* add_months */
29 
Ensure(manage_utils,add_months_0_months)30 Ensure (manage_utils, add_months_0_months)
31 {
32   assert_that (add_months (1572596056, 0), is_equal_to (1572596056));
33 }
34 
Ensure(manage_utils,add_months_negative_months)35 Ensure (manage_utils, add_months_negative_months)
36 {
37   assert_that (add_months (1554163199, -1), is_equal_to (1551484799));
38   assert_that (add_months (1556755199, -2), is_equal_to (1551484799));
39 }
40 
Ensure(manage_utils,add_months_positive_months)41 Ensure (manage_utils, add_months_positive_months)
42 {
43   assert_that (add_months (1551484799, 1), is_equal_to (1554163199));
44   assert_that (add_months (1551484799, 2), is_equal_to (1556755199));
45 }
46 
47 /* icalendar_next_time_from_string */
48 
49 #define EPOCH_2020JAN1_UTC 1577836800
50 #define EPOCH_2030JAN1_UTC 1893456000
51 #define EPOCH_2020JAN1_HAR 1577829600
52 
53 static time_t
get_next_time(time_t first,time_t now,int period,int offset)54 get_next_time (time_t first, time_t now, int period, int offset)
55 {
56   time_t to_next;
57 
58   assert (offset > 0);
59 
60   to_next = ((now - first) + offset * period - 1);
61   to_next -= to_next % period;
62 
63   return first + to_next;
64 }
65 
66 static time_t
verify_next(time_t next,time_t first,time_t now,int period)67 verify_next (time_t next, time_t first, time_t now, int period)
68 {
69   /* There's a gap between getting now and getting next.  This means next
70    * could be the first or second occurrence of the period after now. */
71 
72   return next == get_next_time (first, now, 2 * 60, 1)
73          || next == get_next_time (first, now, 2 * 60, 2);
74 }
75 
Ensure(manage_utils,icalendar_next_time_from_string_utc)76 Ensure (manage_utils, icalendar_next_time_from_string_utc)
77 {
78   time_t next, now;
79 
80   /* Start in past. */
81   now = time (NULL);
82   next = icalendar_next_time_from_string
83           ("BEGIN:VCALENDAR\n"
84            "VERSION:2.0\n"
85            "BEGIN:VEVENT\n"
86            "DTSTART:20200101T000000Z\n"
87            "RRULE:FREQ=MINUTELY;INTERVAL=2\n"
88            "DURATION:PT0S\n"
89            "UID:a486116b-8058-4b1e-9fc5-0eeec5948792\n"
90            "DTSTAMP:19700101T000000Z\n"
91            "END:VEVENT\n"
92            "END:VCALENDAR\n",
93            "UTC",
94            0);
95   assert_that (verify_next (next, EPOCH_2020JAN1_UTC, now, 2 * 60),
96                is_equal_to (1));
97 
98   /* Start in future. */
99   next = icalendar_next_time_from_string
100           ("BEGIN:VCALENDAR\n"
101            "VERSION:2.0\n"
102            "BEGIN:VEVENT\n"
103            "DTSTART:20300101T000000Z\n"
104            "RRULE:FREQ=MINUTELY;INTERVAL=2\n"
105            "DURATION:PT0S\n"
106            "UID:a486116b-8058-4b1e-9fc5-0eeec5948792\n"
107            "DTSTAMP:19700101T000000Z\n"
108            "END:VEVENT\n"
109            "END:VCALENDAR\n",
110            "UTC",
111            0);
112   assert_that (next, is_equal_to (EPOCH_2030JAN1_UTC));
113 }
114 
Ensure(manage_utils,icalendar_next_time_from_string_tz)115 Ensure (manage_utils, icalendar_next_time_from_string_tz)
116 {
117   time_t next, now;
118 
119   now = time (NULL);
120 
121   next = icalendar_next_time_from_string
122           ("BEGIN:VCALENDAR\n"
123            "VERSION:2.0\n"
124            /* Timezone definition. */
125            "BEGIN:VTIMEZONE\n"
126            "TZID:/freeassociation.sourceforge.net/Africa/Harare\n"
127            "X-LIC-LOCATION:Africa/Harare\n"
128            "BEGIN:STANDARD\n"
129            "TZNAME:CAT\n"
130            "DTSTART:19030301T000000\n"
131            "TZOFFSETFROM:+021020\n"
132            "TZOFFSETTO:+0200\n"
133            "END:STANDARD\n"
134            "END:VTIMEZONE\n"
135            /* Event. */
136            "BEGIN:VEVENT\n"
137            "DTSTART;TZID=/freeassociation.sourceforge.net/Africa/Harare:\n"
138            " 20200101T000000\n"
139            "RRULE:FREQ=MINUTELY;INTERVAL=2\n"
140            "DURATION:PT0S\n"
141            "UID:a486116b-8058-4b1e-9fc5-0eeec5948792\n"
142            "DTSTAMP:19700101T000000Z\n"
143            "END:VEVENT\n"
144            "END:VCALENDAR\n",
145            "Africa/Harare",
146            0);
147 
148   assert_that (verify_next (next, EPOCH_2020JAN1_HAR, now, 2 * 60), is_equal_to (1));
149 }
150 
151 /* Hosts test */
152 
Ensure(manage_utils,clean_hosts_string_zeroes)153 Ensure (manage_utils, clean_hosts_string_zeroes)
154 {
155   gchar *clean_str;
156 
157   // Simple IP address
158   clean_str = clean_hosts_string ("000.001.002.003");
159   assert_that (clean_str, is_equal_to_string ("0.1.2.3"));
160   g_free (clean_str);
161 
162   // Long form range
163   clean_str = clean_hosts_string ("000.001.002.003-000.001.010.100");
164   assert_that (clean_str, is_equal_to_string ("0.1.2.3-0.1.10.100"));
165   g_free (clean_str);
166 
167   // Short form range
168   clean_str = clean_hosts_string ("000.001.002.003-004");
169   assert_that (clean_str, is_equal_to_string ("0.1.2.3-4"));
170   g_free (clean_str);
171 
172   // CIDR notation range
173   clean_str = clean_hosts_string ("000.001.002.003/004");
174   assert_that (clean_str, is_equal_to_string ("0.1.2.3/4"));
175   g_free (clean_str);
176 
177   // Hostname with multiple zeroes (should stay the same)
178   clean_str = clean_hosts_string ("server001.example.com");
179   assert_that (clean_str, is_equal_to_string ("server001.example.com"));
180   g_free (clean_str);
181 
182   // List of addresses and ranges
183   clean_str = clean_hosts_string ("000.001.002.003,  040.050.060.070-80,"
184                                   " 123.012.001.001-123.012.001.010");
185   assert_that (clean_str,
186                is_equal_to_string ("0.1.2.3, 40.50.60.70-80,"
187                                    " 123.12.1.1-123.12.1.10"));
188   g_free (clean_str);
189 }
190 
191 /* Test suite. */
192 
193 int
main(int argc,char ** argv)194 main (int argc, char **argv)
195 {
196   TestSuite *suite;
197 
198   suite = create_test_suite ();
199 
200   add_test_with_context (suite, manage_utils, add_months_0_months);
201   add_test_with_context (suite, manage_utils, add_months_negative_months);
202   add_test_with_context (suite, manage_utils, add_months_positive_months);
203 
204   add_test_with_context (suite, manage_utils, icalendar_next_time_from_string_utc);
205   add_test_with_context (suite, manage_utils, icalendar_next_time_from_string_tz);
206 
207   add_test_with_context (suite, manage_utils, clean_hosts_string_zeroes);
208 
209   if (argc > 1)
210     return run_single_test (suite, argv[1], create_text_reporter ());
211 
212   return run_test_suite (suite, create_text_reporter ());
213 }
214