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 "utils.c"
20 
21 #include <cgreen/cgreen.h>
22 
23 Describe (utils);
BeforeEach(utils)24 BeforeEach (utils) {}
AfterEach(utils)25 AfterEach (utils) {}
26 
27 /* gvm_usleep */
28 
Ensure(utils,gvm_usleep_sleep_for_0)29 Ensure (utils, gvm_usleep_sleep_for_0)
30 {
31   assert_that (gvm_usleep (0), is_equal_to (0));
32 }
33 
Ensure(utils,gvm_usleep_sleep_for_1)34 Ensure (utils, gvm_usleep_sleep_for_1)
35 {
36   assert_that (gvm_usleep (1), is_equal_to (0));
37 }
38 
39 /* gvm_sleep */
40 
Ensure(utils,gvm_sleep_sleep_for_0)41 Ensure (utils, gvm_sleep_sleep_for_0)
42 {
43   assert_that (gvm_sleep (0), is_equal_to (0));
44 }
45 
46 /* parse_iso_time_tz */
47 
Ensure(utils,parse_iso_time_tz_with_offset)48 Ensure (utils, parse_iso_time_tz_with_offset)
49 {
50   assert_that (parse_iso_time_tz ("2020-06-01T01:02:03+04:30",
51                                   "Europe/Berlin"),
52                is_equal_to (1590957123));
53 
54   assert_that (parse_iso_time_tz ("2020-06-01T01:02:03-0123",
55                                   "Europe/Berlin"),
56                is_equal_to (1590978303));
57 }
58 
Ensure(utils,parse_iso_time_tz_with_z)59 Ensure (utils, parse_iso_time_tz_with_z)
60 {
61   assert_that (parse_iso_time_tz ("2020-06-01T01:02:03Z",
62                                   "Europe/Berlin"),
63                is_equal_to (1590973323));
64 }
65 
Ensure(utils,parse_iso_time_tz_with_fallback_tz)66 Ensure (utils, parse_iso_time_tz_with_fallback_tz)
67 {
68   assert_that (parse_iso_time_tz ("2020-06-01T01:02:03",
69                                   "Australia/Sydney"),
70                is_equal_to (1590937323));
71 
72   assert_that (parse_iso_time_tz ("2020-01-01T01:02:03",
73                                   "Australia/Adelaide"),
74                is_equal_to (1577802723));
75 
76   assert_that (parse_iso_time_tz ("2020-01-01T01:02:03",
77                                   NULL),
78                is_equal_to (1577840523));
79 }
80 
Ensure(utils,parse_iso_time_tz_variants)81 Ensure (utils, parse_iso_time_tz_variants)
82 {
83   assert_that (parse_iso_time_tz ("2020-06-01T01:02Z",
84                                   "Europe/Berlin"),
85                is_equal_to (1590973320));
86 
87   assert_that (parse_iso_time_tz ("2020-06-01 01:02:03.123+0000",
88                                   "Australia/Sydney"),
89                is_equal_to (1590973323));
90 }
91 
92 /* Number of nanoseconds in a second. */
93 #define NANOSECONDS 1000000000
94 
95 static long long
timespec_subtract(struct timespec * end,struct timespec * start)96 timespec_subtract (struct timespec *end, struct timespec *start)
97 {
98   return (end->tv_sec * NANOSECONDS + start->tv_nsec)
99          - (start->tv_sec * NANOSECONDS + start->tv_nsec);
100 }
101 
Ensure(utils,gvm_sleep_sleep_for_1)102 Ensure (utils, gvm_sleep_sleep_for_1)
103 {
104   struct timespec start, end;
105 
106   assert_that (clock_gettime (CLOCK_REALTIME, &start), is_equal_to (0));
107   assert_that (gvm_sleep (1), is_equal_to (0));
108   assert_that (clock_gettime (CLOCK_REALTIME, &end), is_equal_to (0));
109   assert_that (timespec_subtract (&end, &start), is_greater_than (NANOSECONDS - 1));
110 }
111 
112 /* Test suite. */
113 
114 int
main(int argc,char ** argv)115 main (int argc, char **argv)
116 {
117   TestSuite *suite;
118 
119   suite = create_test_suite ();
120 
121   add_test_with_context (suite, utils, gvm_usleep_sleep_for_0);
122   add_test_with_context (suite, utils, gvm_usleep_sleep_for_1);
123 
124   add_test_with_context (suite, utils, gvm_sleep_sleep_for_0);
125   add_test_with_context (suite, utils, gvm_sleep_sleep_for_1);
126 
127   add_test_with_context (suite, utils, parse_iso_time_tz_with_offset);
128   add_test_with_context (suite, utils, parse_iso_time_tz_with_z);
129   add_test_with_context (suite, utils, parse_iso_time_tz_with_fallback_tz);
130   add_test_with_context (suite, utils, parse_iso_time_tz_variants);
131 
132   if (argc > 1)
133     return run_single_test (suite, argv[1], create_text_reporter ());
134 
135   return run_test_suite (suite, create_text_reporter ());
136 }
137