1 /* Copyright (C) 2009-2021 Greenbone Networks GmbH
2  *
3  * SPDX-License-Identifier: GPL-2.0-or-later
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
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #include "hosts.c"
21 #include "networking.h"
22 
23 #include <cgreen/cgreen.h>
24 #include <cgreen/mocks.h>
25 
26 Describe (hosts);
BeforeEach(hosts)27 BeforeEach (hosts)
28 {
29 }
AfterEach(hosts)30 AfterEach (hosts)
31 {
32 }
33 
34 /* make_hosts */
35 
Ensure(hosts,gvm_hosts_new_never_returns_null)36 Ensure (hosts, gvm_hosts_new_never_returns_null)
37 {
38   assert_that (gvm_hosts_new (""), is_not_null);
39   assert_that (gvm_hosts_new ("172.10.1.1"), is_not_null);
40   assert_that (gvm_hosts_new ("172.10.1.1/24"), is_not_null);
41 }
42 
Ensure(hosts,gvm_get_host_type_returns_host_type_ipv4)43 Ensure (hosts, gvm_get_host_type_returns_host_type_ipv4)
44 {
45   assert_that (gvm_get_host_type ("192.168.0.4"), is_equal_to (HOST_TYPE_IPV4));
46   assert_that (gvm_get_host_type ("1.1.1.1"), is_equal_to (HOST_TYPE_IPV4));
47   assert_that (gvm_get_host_type ("0.0.0.0"), is_equal_to (HOST_TYPE_IPV4));
48   assert_that (gvm_get_host_type ("255.255.255.255"),
49                is_equal_to (HOST_TYPE_IPV4));
50   assert_that (gvm_get_host_type ("10.1.1.1"), is_equal_to (HOST_TYPE_IPV4));
51 }
52 
Ensure(hosts,gvm_get_host_type_returns_host_type_ipv6)53 Ensure (hosts, gvm_get_host_type_returns_host_type_ipv6)
54 {
55   assert_that (gvm_get_host_type ("::ffee"), is_equal_to (HOST_TYPE_IPV6));
56   assert_that (gvm_get_host_type ("0001:1:1:1::1"),
57                is_equal_to (HOST_TYPE_IPV6));
58 }
59 
60 #define TEN "0123456789"
61 #define SIXTY TEN TEN TEN TEN TEN TEN
62 #define HUNDRED TEN TEN TEN TEN TEN TEN TEN TEN TEN TEN
63 
Ensure(hosts,gvm_get_host_type_returns_host_type_hostname)64 Ensure (hosts, gvm_get_host_type_returns_host_type_hostname)
65 {
66   assert_that (gvm_get_host_type ("www.greenbone.net"),
67                is_equal_to (HOST_TYPE_NAME));
68   assert_that (gvm_get_host_type ("www.example_underscore.net"),
69                is_equal_to (HOST_TYPE_NAME));
70   assert_that (gvm_get_host_type ("www.example-dash.net"),
71                is_equal_to (HOST_TYPE_NAME));
72   assert_that (gvm_get_host_type ("greenbone.net"),
73                is_equal_to (HOST_TYPE_NAME));
74   assert_that (gvm_get_host_type ("g"), is_equal_to (HOST_TYPE_NAME));
75   assert_that (gvm_get_host_type ("123.com"), is_equal_to (HOST_TYPE_NAME));
76   /* Lengths. */
77   assert_that (gvm_get_host_type (SIXTY "123.short.enough.com"),
78                is_equal_to (0));
79   assert_that (gvm_get_host_type (SIXTY "." SIXTY "." SIXTY "." SIXTY "."
80                                         "56789.com"),
81                is_equal_to (0));
82 }
83 
Ensure(hosts,gvm_get_host_type_returns_host_type_cidr_block)84 Ensure (hosts, gvm_get_host_type_returns_host_type_cidr_block)
85 {
86   assert_that (gvm_get_host_type ("192.168.0.0/24"),
87                is_equal_to (HOST_TYPE_CIDR_BLOCK));
88   assert_that (gvm_get_host_type ("1.1.1.1/8"),
89                is_equal_to (HOST_TYPE_CIDR_BLOCK));
90   assert_that (gvm_get_host_type ("192.168.1.128/25"),
91                is_equal_to (HOST_TYPE_CIDR_BLOCK));
92   assert_that (gvm_get_host_type ("10.0.0.1/16"),
93                is_equal_to (HOST_TYPE_CIDR_BLOCK));
94   assert_that (gvm_get_host_type ("10.1.1.0/30"),
95                is_equal_to (HOST_TYPE_CIDR_BLOCK));
96 }
97 
Ensure(hosts,gvm_get_host_type_returns_host_type_cidr6_block)98 Ensure (hosts, gvm_get_host_type_returns_host_type_cidr6_block)
99 {
100   assert_that (gvm_get_host_type ("::ffee:1/64"),
101                is_equal_to (HOST_TYPE_CIDR6_BLOCK));
102   assert_that (gvm_get_host_type ("2001:db8::/78"),
103                is_equal_to (HOST_TYPE_CIDR6_BLOCK));
104   assert_that (gvm_get_host_type ("2001:db8:0000:0000:001f:ffff:ffff:1/1"),
105                is_equal_to (HOST_TYPE_CIDR6_BLOCK));
106 }
107 
Ensure(hosts,gvm_get_host_type_returns_host_type_range_short)108 Ensure (hosts, gvm_get_host_type_returns_host_type_range_short)
109 {
110   assert_that (gvm_get_host_type ("192.168.10.1-9"),
111                is_equal_to (HOST_TYPE_RANGE_SHORT));
112   assert_that (gvm_get_host_type ("192.168.10.1-50"),
113                is_equal_to (HOST_TYPE_RANGE_SHORT));
114   assert_that (gvm_get_host_type ("192.168.10.1-255"),
115                is_equal_to (HOST_TYPE_RANGE_SHORT));
116   assert_that (gvm_get_host_type ("1.1.1.1-9"),
117                is_equal_to (HOST_TYPE_RANGE_SHORT));
118   assert_that (gvm_get_host_type ("1.1.1.1-50"),
119                is_equal_to (HOST_TYPE_RANGE_SHORT));
120   assert_that (gvm_get_host_type ("1.1.1.1-255"),
121                is_equal_to (HOST_TYPE_RANGE_SHORT));
122   assert_that (gvm_get_host_type ("255.255.255.1-9"),
123                is_equal_to (HOST_TYPE_RANGE_SHORT));
124   assert_that (gvm_get_host_type ("255.255.255.1-50"),
125                is_equal_to (HOST_TYPE_RANGE_SHORT));
126   assert_that (gvm_get_host_type ("255.255.255.1-255"),
127                is_equal_to (HOST_TYPE_RANGE_SHORT));
128 }
129 
Ensure(hosts,gvm_get_host_type_returns_host_type_range6_short)130 Ensure (hosts, gvm_get_host_type_returns_host_type_range6_short)
131 {
132   assert_that (gvm_get_host_type ("::ffee:1-fe50"),
133                is_equal_to (HOST_TYPE_RANGE6_SHORT));
134   assert_that (gvm_get_host_type ("2000::-ffff"),
135                is_equal_to (HOST_TYPE_RANGE6_SHORT));
136 }
137 
Ensure(hosts,gvm_get_host_type_returns_host_type_range_long)138 Ensure (hosts, gvm_get_host_type_returns_host_type_range_long)
139 {
140   assert_that (gvm_get_host_type ("192.168.10.1-192.168.10.9"),
141                is_equal_to (HOST_TYPE_RANGE_LONG));
142   assert_that (gvm_get_host_type ("192.168.10.1-192.168.10.50"),
143                is_equal_to (HOST_TYPE_RANGE_LONG));
144   assert_that (gvm_get_host_type ("192.168.10.1-192.168.10.255"),
145                is_equal_to (HOST_TYPE_RANGE_LONG));
146   assert_that (gvm_get_host_type ("1.1.1.1-1.1.1.9"),
147                is_equal_to (HOST_TYPE_RANGE_LONG));
148   assert_that (gvm_get_host_type ("1.1.1.1-1.1.1.50"),
149                is_equal_to (HOST_TYPE_RANGE_LONG));
150   assert_that (gvm_get_host_type ("1.1.1.1-1.1.1.255"),
151                is_equal_to (HOST_TYPE_RANGE_LONG));
152   assert_that (gvm_get_host_type ("255.255.255.1-255.255.255.9"),
153                is_equal_to (HOST_TYPE_RANGE_LONG));
154   assert_that (gvm_get_host_type ("255.255.255.1-255.255.255.50"),
155                is_equal_to (HOST_TYPE_RANGE_LONG));
156   assert_that (gvm_get_host_type ("255.255.255.1-255.255.255.255"),
157                is_equal_to (HOST_TYPE_RANGE_LONG));
158 }
159 
Ensure(hosts,gvm_get_host_type_returns_host_type_range6_long)160 Ensure (hosts, gvm_get_host_type_returns_host_type_range6_long)
161 {
162   assert_that (
163     gvm_get_host_type ("2001:db0::-2001:0dbf:ffff:ffff:ffff:ffff:ffff:ffff"),
164     is_equal_to (HOST_TYPE_RANGE6_LONG));
165   assert_that (gvm_get_host_type ("::1:200:7-::1:205:500"),
166                is_equal_to (HOST_TYPE_RANGE6_LONG));
167 }
168 
Ensure(hosts,gvm_get_host_type_returns_error)169 Ensure (hosts, gvm_get_host_type_returns_error)
170 {
171   assert_that (gvm_get_host_type (""), is_equal_to (-1));
172   assert_that (gvm_get_host_type ("."), is_equal_to (-1));
173 
174   /* Invalid chars. */
175   assert_that (gvm_get_host_type ("a,b"), is_equal_to (-1));
176   assert_that (gvm_get_host_type ("="), is_equal_to (-1));
177 
178   /* Numeric TLD. */
179   assert_that (gvm_get_host_type ("a.123"), is_equal_to (-1));
180 
181   /* IP with too many parts. */
182   assert_that (gvm_get_host_type ("192.168.10.1.1"), is_equal_to (-1));
183 
184   /* IP with numbers out of bounds. */
185   assert_that (gvm_get_host_type ("256.168.10.1"), is_equal_to (-1));
186   assert_that (gvm_get_host_type ("192.256.10.1"), is_equal_to (-1));
187   assert_that (gvm_get_host_type ("192.168.256.1"), is_equal_to (-1));
188   assert_that (gvm_get_host_type ("192.168.10.256"), is_equal_to (-1));
189   assert_that (gvm_get_host_type ("192.168.10.855"), is_equal_to (-1));
190 
191   /* Lengths. */
192   assert_that (gvm_get_host_type (SIXTY "1234.too.long.com"), is_equal_to (-1));
193   assert_that (gvm_get_host_type (SIXTY "." SIXTY "." SIXTY "." SIXTY "."
194                                         "567890.com"),
195                is_equal_to (-1));
196 }
197 
Ensure(hosts,gvm_hosts_new_with_max_returns_success)198 Ensure (hosts, gvm_hosts_new_with_max_returns_success)
199 {
200   assert_that (gvm_hosts_new_with_max ("127.0.0.1", 1), is_not_null);
201   assert_that (gvm_hosts_new_with_max ("127.0.0.1", 2000), is_not_null);
202   assert_that (gvm_hosts_new_with_max ("127.0.0.1,127.0.0.2", 2), is_not_null);
203   assert_that (gvm_hosts_new_with_max ("127.0.0.1, 127.0.0.2", 2), is_not_null);
204 }
205 
Ensure(hosts,gvm_hosts_new_with_max_returns_error)206 Ensure (hosts, gvm_hosts_new_with_max_returns_error)
207 {
208   /* Host error. */
209   assert_that (gvm_hosts_new_with_max ("a.123", 2), is_null);
210 
211   /* More than max_hosts hosts. */
212   assert_that (gvm_hosts_new_with_max ("127.0.0.1, 127.0.0.2", 1), is_null);
213 
214   /* Wrong separator. */
215   assert_that (gvm_hosts_new_with_max ("127.0.0.1 127.0.0.2", 2), is_null);
216   assert_that (gvm_hosts_new_with_max ("127.0.0.1|127.0.0.2", 2), is_null);
217 }
218 
Ensure(hosts,gvm_hosts_move_host_to_end)219 Ensure (hosts, gvm_hosts_move_host_to_end)
220 {
221   gvm_hosts_t *hosts = NULL;
222   gvm_host_t *host = NULL;
223   int totalhosts;
224   size_t current;
225 
226   hosts = gvm_hosts_new ("192.168.0.0/28");
227 
228   // Get first host
229   host = gvm_hosts_next (hosts);
230 
231   totalhosts = gvm_hosts_count (hosts);
232   assert_that (totalhosts, is_equal_to (14));
233 
234   while (g_strcmp0 (gvm_host_value_str (host), "192.168.0.9"))
235     {
236       host = gvm_hosts_next (hosts);
237     }
238   assert_that (g_strcmp0 (gvm_host_value_str (host), "192.168.0.9"),
239                is_equal_to (0));
240 
241   current = hosts->current;
242   gvm_hosts_move_current_host_to_end (hosts);
243   assert_that (hosts->current, is_equal_to (current - 1));
244 
245   host = gvm_hosts_next (hosts);
246   assert_that (g_strcmp0 (gvm_host_value_str (host), "192.168.0.10"),
247                is_equal_to (0));
248   assert_that (g_strcmp0 (gvm_host_value_str (hosts->hosts[totalhosts - 1]),
249                           "192.168.0.9"),
250                is_equal_to (0));
251 
252   gvm_hosts_free (hosts);
253 }
254 
255 /* Test suite. */
256 
257 int
main(int argc,char ** argv)258 main (int argc, char **argv)
259 {
260   TestSuite *suite;
261 
262   suite = create_test_suite ();
263 
264   add_test_with_context (suite, hosts, gvm_hosts_new_never_returns_null);
265   add_test_with_context (suite, hosts,
266                          gvm_get_host_type_returns_host_type_ipv4);
267   add_test_with_context (suite, hosts,
268                          gvm_get_host_type_returns_host_type_ipv6);
269   add_test_with_context (suite, hosts,
270                          gvm_get_host_type_returns_host_type_hostname);
271   add_test_with_context (suite, hosts,
272                          gvm_get_host_type_returns_host_type_cidr_block);
273   add_test_with_context (suite, hosts,
274                          gvm_get_host_type_returns_host_type_cidr6_block);
275   add_test_with_context (suite, hosts,
276                          gvm_get_host_type_returns_host_type_range_short);
277   add_test_with_context (suite, hosts,
278                          gvm_get_host_type_returns_host_type_range6_short);
279   add_test_with_context (suite, hosts,
280                          gvm_get_host_type_returns_host_type_range_long);
281   add_test_with_context (suite, hosts,
282                          gvm_get_host_type_returns_host_type_range6_long);
283   add_test_with_context (suite, hosts, gvm_get_host_type_returns_error);
284 
285   add_test_with_context (suite, hosts, gvm_hosts_new_with_max_returns_error);
286   add_test_with_context (suite, hosts, gvm_hosts_new_with_max_returns_success);
287 
288   add_test_with_context (suite, hosts, gvm_hosts_move_host_to_end);
289 
290   if (argc > 1)
291     return run_single_test (suite, argv[1], create_text_reporter ());
292 
293   return run_test_suite (suite, create_text_reporter ());
294 }
295