Greenbone Vulnerability Management Libraries 22.32.0
hosts_tests.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009-2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6#include "hosts.c"
7#include "networking.h"
8
9#include <cgreen/cgreen.h>
10#include <cgreen/mocks.h>
11
12Describe (hosts);
14{
15}
16AfterEach (hosts)
17{
18}
19
20/* make_hosts */
21
22Ensure (hosts, gvm_hosts_new_never_returns_null)
23{
24 gvm_hosts_t *hosts;
25
26 hosts = gvm_hosts_new ("");
27 assert_that (hosts, is_not_null);
28 gvm_hosts_free (hosts);
29
30 hosts = gvm_hosts_new ("172.10.1.1");
31 assert_that (hosts, is_not_null);
32 gvm_hosts_free (hosts);
33
34 hosts = gvm_hosts_new ("172.10.1.1/24");
35 assert_that (hosts, is_not_null);
36 gvm_hosts_free (hosts);
37}
38
39Ensure (hosts, gvm_get_host_type_returns_host_type_ipv4)
40{
41 assert_that (gvm_get_host_type ("192.168.0.4"), is_equal_to (HOST_TYPE_IPV4));
42 assert_that (gvm_get_host_type ("1.1.1.1"), is_equal_to (HOST_TYPE_IPV4));
43 assert_that (gvm_get_host_type ("0.0.0.0"), is_equal_to (HOST_TYPE_IPV4));
44 assert_that (gvm_get_host_type ("255.255.255.255"),
45 is_equal_to (HOST_TYPE_IPV4));
46 assert_that (gvm_get_host_type ("10.1.1.1"), is_equal_to (HOST_TYPE_IPV4));
47}
48
49Ensure (hosts, gvm_get_host_type_returns_host_type_ipv6)
50{
51 assert_that (gvm_get_host_type ("::ffee"), is_equal_to (HOST_TYPE_IPV6));
52 assert_that (gvm_get_host_type ("0001:1:1:1::1"),
53 is_equal_to (HOST_TYPE_IPV6));
54}
55
56#define TEN "0123456789"
57#define SIXTY TEN TEN TEN TEN TEN TEN
58#define HUNDRED TEN TEN TEN TEN TEN TEN TEN TEN TEN TEN
59
60Ensure (hosts, gvm_get_host_type_returns_host_type_hostname)
61{
62 assert_that (gvm_get_host_type ("www.greenbone.net"),
63 is_equal_to (HOST_TYPE_NAME));
64 assert_that (gvm_get_host_type ("www.example_underscore.net"),
65 is_equal_to (HOST_TYPE_NAME));
66 assert_that (gvm_get_host_type ("www.example-dash.net"),
67 is_equal_to (HOST_TYPE_NAME));
68 assert_that (gvm_get_host_type ("greenbone.net"),
69 is_equal_to (HOST_TYPE_NAME));
70 assert_that (gvm_get_host_type ("g"), is_equal_to (HOST_TYPE_NAME));
71 assert_that (gvm_get_host_type ("123.com"), is_equal_to (HOST_TYPE_NAME));
72 /* Lengths. */
73 assert_that (gvm_get_host_type (SIXTY "123.short.enough.com"),
74 is_equal_to (0));
75 assert_that (gvm_get_host_type (SIXTY "." SIXTY "." SIXTY "." SIXTY "."
76 "56789.com"),
77 is_equal_to (0));
78}
79
80Ensure (hosts, gvm_get_host_type_returns_host_type_cidr_block)
81{
82 assert_that (gvm_get_host_type ("192.168.0.0/24"),
83 is_equal_to (HOST_TYPE_CIDR_BLOCK));
84 assert_that (gvm_get_host_type ("1.1.1.1/8"),
85 is_equal_to (HOST_TYPE_CIDR_BLOCK));
86 assert_that (gvm_get_host_type ("192.168.1.128/25"),
87 is_equal_to (HOST_TYPE_CIDR_BLOCK));
88 assert_that (gvm_get_host_type ("10.0.0.1/16"),
89 is_equal_to (HOST_TYPE_CIDR_BLOCK));
90 assert_that (gvm_get_host_type ("10.1.1.0/30"),
91 is_equal_to (HOST_TYPE_CIDR_BLOCK));
92}
93
94Ensure (hosts, gvm_get_host_type_returns_host_type_cidr6_block)
95{
96 assert_that (gvm_get_host_type ("::ffee:1/64"),
97 is_equal_to (HOST_TYPE_CIDR6_BLOCK));
98 assert_that (gvm_get_host_type ("2001:db8::/78"),
99 is_equal_to (HOST_TYPE_CIDR6_BLOCK));
100 assert_that (gvm_get_host_type ("2001:db8:0000:0000:001f:ffff:ffff:1/1"),
101 is_equal_to (HOST_TYPE_CIDR6_BLOCK));
102}
103
104Ensure (hosts, gvm_get_host_type_returns_host_type_range_short)
105{
106 assert_that (gvm_get_host_type ("192.168.10.1-9"),
107 is_equal_to (HOST_TYPE_RANGE_SHORT));
108 assert_that (gvm_get_host_type ("192.168.10.1-50"),
109 is_equal_to (HOST_TYPE_RANGE_SHORT));
110 assert_that (gvm_get_host_type ("192.168.10.1-255"),
111 is_equal_to (HOST_TYPE_RANGE_SHORT));
112 assert_that (gvm_get_host_type ("1.1.1.1-9"),
113 is_equal_to (HOST_TYPE_RANGE_SHORT));
114 assert_that (gvm_get_host_type ("1.1.1.1-50"),
115 is_equal_to (HOST_TYPE_RANGE_SHORT));
116 assert_that (gvm_get_host_type ("1.1.1.1-255"),
117 is_equal_to (HOST_TYPE_RANGE_SHORT));
118 assert_that (gvm_get_host_type ("255.255.255.1-9"),
119 is_equal_to (HOST_TYPE_RANGE_SHORT));
120 assert_that (gvm_get_host_type ("255.255.255.1-50"),
121 is_equal_to (HOST_TYPE_RANGE_SHORT));
122 assert_that (gvm_get_host_type ("255.255.255.1-255"),
123 is_equal_to (HOST_TYPE_RANGE_SHORT));
124}
125
126Ensure (hosts, gvm_get_host_type_returns_host_type_range6_short)
127{
128 assert_that (gvm_get_host_type ("::ffee:1-fe50"),
129 is_equal_to (HOST_TYPE_RANGE6_SHORT));
130 assert_that (gvm_get_host_type ("2000::-ffff"),
131 is_equal_to (HOST_TYPE_RANGE6_SHORT));
132}
133
134Ensure (hosts, gvm_get_host_type_returns_host_type_range_long)
135{
136 assert_that (gvm_get_host_type ("192.168.10.1-192.168.10.9"),
137 is_equal_to (HOST_TYPE_RANGE_LONG));
138 assert_that (gvm_get_host_type ("192.168.10.1-192.168.10.50"),
139 is_equal_to (HOST_TYPE_RANGE_LONG));
140 assert_that (gvm_get_host_type ("192.168.10.1-192.168.10.255"),
141 is_equal_to (HOST_TYPE_RANGE_LONG));
142 assert_that (gvm_get_host_type ("1.1.1.1-1.1.1.9"),
143 is_equal_to (HOST_TYPE_RANGE_LONG));
144 assert_that (gvm_get_host_type ("1.1.1.1-1.1.1.50"),
145 is_equal_to (HOST_TYPE_RANGE_LONG));
146 assert_that (gvm_get_host_type ("1.1.1.1-1.1.1.255"),
147 is_equal_to (HOST_TYPE_RANGE_LONG));
148 assert_that (gvm_get_host_type ("255.255.255.1-255.255.255.9"),
149 is_equal_to (HOST_TYPE_RANGE_LONG));
150 assert_that (gvm_get_host_type ("255.255.255.1-255.255.255.50"),
151 is_equal_to (HOST_TYPE_RANGE_LONG));
152 assert_that (gvm_get_host_type ("255.255.255.1-255.255.255.255"),
153 is_equal_to (HOST_TYPE_RANGE_LONG));
154}
155
156Ensure (hosts, gvm_get_host_type_returns_host_type_range6_long)
157{
158 assert_that (
159 gvm_get_host_type ("2001:db0::-2001:0dbf:ffff:ffff:ffff:ffff:ffff:ffff"),
160 is_equal_to (HOST_TYPE_RANGE6_LONG));
161 assert_that (gvm_get_host_type ("::1:200:7-::1:205:500"),
162 is_equal_to (HOST_TYPE_RANGE6_LONG));
163}
164
165Ensure (hosts, gvm_get_host_type_returns_error)
166{
167 assert_that (gvm_get_host_type (""), is_equal_to (-1));
168 assert_that (gvm_get_host_type ("."), is_equal_to (-1));
169
170 /* Invalid chars. */
171 assert_that (gvm_get_host_type ("a,b"), is_equal_to (-1));
172 assert_that (gvm_get_host_type ("="), is_equal_to (-1));
173
174 /* Numeric TLD. */
175 assert_that (gvm_get_host_type ("a.123"), is_equal_to (-1));
176
177 /* IP with too many parts. */
178 assert_that (gvm_get_host_type ("192.168.10.1.1"), is_equal_to (-1));
179
180 /* IP with numbers out of bounds. */
181 assert_that (gvm_get_host_type ("256.168.10.1"), is_equal_to (-1));
182 assert_that (gvm_get_host_type ("192.256.10.1"), is_equal_to (-1));
183 assert_that (gvm_get_host_type ("192.168.256.1"), is_equal_to (-1));
184 assert_that (gvm_get_host_type ("192.168.10.256"), is_equal_to (-1));
185 assert_that (gvm_get_host_type ("192.168.10.855"), is_equal_to (-1));
186
187 /* Lengths. */
188 assert_that (gvm_get_host_type (SIXTY "1234.too.long.com"), is_equal_to (-1));
189 assert_that (gvm_get_host_type (SIXTY "." SIXTY "." SIXTY "." SIXTY "."
190 "567890.com"),
191 is_equal_to (-1));
192}
193
194Ensure (hosts, gvm_hosts_new_with_max_returns_success)
195{
196 gvm_hosts_t *hosts;
197
198 hosts = gvm_hosts_new_with_max ("127.0.0.1", 1);
199 assert_that (hosts, is_not_null);
200 gvm_hosts_free (hosts);
201
202 hosts = gvm_hosts_new_with_max ("127.0.0.1", 2000);
203 assert_that (hosts, is_not_null);
204 gvm_hosts_free (hosts);
205
206 hosts = gvm_hosts_new_with_max ("127.0.0.1,127.0.0.2", 2);
207 assert_that (hosts, is_not_null);
208 gvm_hosts_free (hosts);
209
210 hosts = gvm_hosts_new_with_max ("127.0.0.1, 127.0.0.2", 2);
211 assert_that (hosts, is_not_null);
212 gvm_hosts_free (hosts);
213}
214
215Ensure (hosts, gvm_hosts_new_with_max_returns_error)
216{
217 /* Host error. */
218 assert_that (gvm_hosts_new_with_max ("a.123", 2), is_null);
219
220 /* More than max_hosts hosts. */
221 assert_that (gvm_hosts_new_with_max ("127.0.0.1, 127.0.0.2", 1), is_null);
222
223 /* Wrong separator. */
224 assert_that (gvm_hosts_new_with_max ("127.0.0.1 127.0.0.2", 2), is_null);
225 assert_that (gvm_hosts_new_with_max ("127.0.0.1|127.0.0.2", 2), is_null);
226}
227
228// This is a macro so the line number below is clear on failure.
229#define ASSERT_HOST_EQUALS(hosts, i, string) \
230 { \
231 gchar *value; \
232 \
233 value = gvm_host_value_str (hosts->hosts[i]); \
234 assert_true_with_message ( \
235 g_strcmp0 (value, string) == 0, \
236 "Expected hosts->hosts[%d] to be %s but it was %s", i, string, value); \
237 g_free (value); \
238 }
239
240static int
241host_value_eq (gvm_host_t *host, gchar *string)
242{
243 int ret;
244 gchar *value;
245
246 value = gvm_host_value_str (host);
247 ret = g_strcmp0 (value, string);
248 g_free (value);
249 return ret;
250}
251
252Ensure (hosts, gvm_hosts_move_host_to_end)
253{
254 gvm_hosts_t *hosts = NULL;
255 gvm_host_t *host = NULL;
256 int totalhosts;
257 size_t current;
258
259 hosts = gvm_hosts_new ("192.168.0.0/28");
260
261 ASSERT_HOST_EQUALS (hosts, 0, "192.168.0.1");
262 ASSERT_HOST_EQUALS (hosts, 1, "192.168.0.2");
263 ASSERT_HOST_EQUALS (hosts, 2, "192.168.0.3");
264 ASSERT_HOST_EQUALS (hosts, 3, "192.168.0.4");
265 ASSERT_HOST_EQUALS (hosts, 4, "192.168.0.5");
266 ASSERT_HOST_EQUALS (hosts, 5, "192.168.0.6");
267 ASSERT_HOST_EQUALS (hosts, 6, "192.168.0.7");
268 ASSERT_HOST_EQUALS (hosts, 7, "192.168.0.8");
269 ASSERT_HOST_EQUALS (hosts, 8, "192.168.0.9");
270 ASSERT_HOST_EQUALS (hosts, 9, "192.168.0.10");
271 ASSERT_HOST_EQUALS (hosts, 10, "192.168.0.11");
272 ASSERT_HOST_EQUALS (hosts, 11, "192.168.0.12");
273 ASSERT_HOST_EQUALS (hosts, 12, "192.168.0.13");
274 ASSERT_HOST_EQUALS (hosts, 13, "192.168.0.14");
275
276 // Get first host
277 host = gvm_hosts_next (hosts);
278
279 totalhosts = gvm_hosts_count (hosts);
280 assert_that (totalhosts, is_equal_to (14));
281
282 while (host_value_eq (host, "192.168.0.9"))
283 {
284 host = gvm_hosts_next (hosts);
285 }
286 assert_that (host_value_eq (host, "192.168.0.9"), is_equal_to (0));
287
288 current = hosts->current;
290 assert_that (hosts->current, is_equal_to (current - 1));
291
292 host = gvm_hosts_next (hosts);
293 assert_that (host_value_eq (host, "192.168.0.10"), is_equal_to (0));
294 assert_that (host_value_eq (hosts->hosts[totalhosts - 1], "192.168.0.9"),
295 is_equal_to (0));
296
297 ASSERT_HOST_EQUALS (hosts, 0, "192.168.0.1");
298 ASSERT_HOST_EQUALS (hosts, 1, "192.168.0.2");
299 ASSERT_HOST_EQUALS (hosts, 2, "192.168.0.3");
300 ASSERT_HOST_EQUALS (hosts, 3, "192.168.0.4");
301 ASSERT_HOST_EQUALS (hosts, 4, "192.168.0.5");
302 ASSERT_HOST_EQUALS (hosts, 5, "192.168.0.6");
303 ASSERT_HOST_EQUALS (hosts, 6, "192.168.0.7");
304 ASSERT_HOST_EQUALS (hosts, 7, "192.168.0.8");
305 ASSERT_HOST_EQUALS (hosts, 8, "192.168.0.10");
306 ASSERT_HOST_EQUALS (hosts, 9, "192.168.0.11");
307 ASSERT_HOST_EQUALS (hosts, 10, "192.168.0.12");
308 ASSERT_HOST_EQUALS (hosts, 11, "192.168.0.13");
309 ASSERT_HOST_EQUALS (hosts, 12, "192.168.0.14");
310 ASSERT_HOST_EQUALS (hosts, 13, "192.168.0.9");
311
312 gvm_hosts_free (hosts);
313}
314
316{
317 gvm_hosts_t *hosts = NULL;
318 gvm_host_t *host = NULL;
319 int totalhosts;
320 GSList *removed = NULL;
321 gchar *value;
322
323 hosts = gvm_hosts_new ("192.168.0.1,192.168.0.2,192.168.0.3");
324
325 removed = gvm_hosts_allowed_only (hosts, NULL, NULL);
326 totalhosts = gvm_hosts_count (hosts);
327 assert_that (totalhosts, is_equal_to (3));
328
329 removed = gvm_hosts_allowed_only (hosts, "192.168.0.2", NULL);
330 totalhosts = gvm_hosts_count (hosts);
331 assert_that (totalhosts, is_equal_to (2));
332 assert_that (g_slist_length (removed), is_equal_to (1));
333 g_slist_free_full (removed, g_free);
334
335 removed = gvm_hosts_allowed_only (hosts, NULL, "192.168.0.3");
336 totalhosts = gvm_hosts_count (hosts);
337 assert_that (totalhosts, is_equal_to (1));
338 assert_that (g_slist_length (removed), is_equal_to (1));
339 g_slist_free_full (removed, g_free);
340
341 host = gvm_hosts_next (hosts);
342 value = gvm_host_value_str (host);
343 assert_that (g_strcmp0 (value, "192.168.0.3"), is_equal_to (0));
344 g_free (value);
345
346 gvm_hosts_free (hosts);
347}
348
349/* Test suite. */
350
351int
352main (int argc, char **argv)
353{
354 int ret;
355 TestSuite *suite;
356
357 suite = create_test_suite ();
358
359 add_test_with_context (suite, hosts, gvm_hosts_new_never_returns_null);
360 add_test_with_context (suite, hosts,
361 gvm_get_host_type_returns_host_type_ipv4);
362 add_test_with_context (suite, hosts,
363 gvm_get_host_type_returns_host_type_ipv6);
364 add_test_with_context (suite, hosts,
365 gvm_get_host_type_returns_host_type_hostname);
366 add_test_with_context (suite, hosts,
367 gvm_get_host_type_returns_host_type_cidr_block);
368 add_test_with_context (suite, hosts,
369 gvm_get_host_type_returns_host_type_cidr6_block);
370 add_test_with_context (suite, hosts,
371 gvm_get_host_type_returns_host_type_range_short);
372 add_test_with_context (suite, hosts,
373 gvm_get_host_type_returns_host_type_range6_short);
374 add_test_with_context (suite, hosts,
375 gvm_get_host_type_returns_host_type_range_long);
376 add_test_with_context (suite, hosts,
377 gvm_get_host_type_returns_host_type_range6_long);
378 add_test_with_context (suite, hosts, gvm_get_host_type_returns_error);
379
380 add_test_with_context (suite, hosts, gvm_hosts_new_with_max_returns_error);
381 add_test_with_context (suite, hosts, gvm_hosts_new_with_max_returns_success);
382
383 add_test_with_context (suite, hosts, gvm_hosts_move_host_to_end);
384 add_test_with_context (suite, hosts, gvm_hosts_allowed_only);
385
386 if (argc > 1)
387 ret = run_single_test (suite, argv[1], create_text_reporter ());
388 else
389 ret = run_test_suite (suite, create_text_reporter ());
390
391 destroy_test_suite (suite);
392
393 return ret;
394}
Implementation of an API to handle Hosts objects.
gchar * gvm_host_value_str(const gvm_host_t *host)
Gets a host's value in printable format.
Definition hosts.c:2290
gvm_hosts_t * gvm_hosts_new(const gchar *hosts_str)
Creates a new gvm_hosts_t structure and the associated hosts objects from the provided hosts_str.
Definition hosts.c:1300
gvm_host_t * gvm_hosts_next(gvm_hosts_t *hosts)
Gets the next gvm_host_t from a gvm_hosts_t structure. The state of iteration is kept internally with...
Definition hosts.c:1314
GSList * gvm_hosts_allowed_only(gvm_hosts_t *hosts, const char *deny_hosts_str, const char *allow_hosts_str)
Returns a list of hosts after a host authorization check.
Definition hosts.c:1658
gvm_hosts_t * gvm_hosts_new_with_max(const gchar *hosts_str, unsigned int max_hosts)
Creates a new gvm_hosts_t structure and the associated hosts objects from the provided hosts_str.
Definition hosts.c:1103
void gvm_hosts_move_current_host_to_end(gvm_hosts_t *hosts)
Move the current gvm_host_t from a gvm_hosts_t structure to the end of the hosts list.
Definition hosts.c:1332
int gvm_get_host_type(const gchar *str_stripped)
Determines the host type in a buffer.
Definition hosts.c:813
unsigned int gvm_hosts_count(const gvm_hosts_t *hosts)
Gets the count of single hosts objects in a hosts collection.
Definition hosts.c:2106
void gvm_hosts_free(gvm_hosts_t *hosts)
Frees memory occupied by an gvm_hosts_t structure.
Definition hosts.c:1375
@ HOST_TYPE_RANGE_SHORT
Definition hosts.h:38
@ HOST_TYPE_RANGE6_SHORT
Definition hosts.h:43
@ HOST_TYPE_RANGE_LONG
Definition hosts.h:39
@ HOST_TYPE_RANGE6_LONG
Definition hosts.h:42
@ HOST_TYPE_NAME
Definition hosts.h:35
@ HOST_TYPE_IPV6
Definition hosts.h:40
@ HOST_TYPE_CIDR6_BLOCK
Definition hosts.h:41
@ HOST_TYPE_IPV4
Definition hosts.h:36
@ HOST_TYPE_CIDR_BLOCK
Definition hosts.h:37
struct gvm_host gvm_host_t
Definition hosts.h:48
struct gvm_hosts gvm_hosts_t
Definition hosts.h:50
Ensure(hosts, gvm_hosts_new_never_returns_null)
Definition hosts_tests.c:22
int main(int argc, char **argv)
AfterEach(hosts)
Definition hosts_tests.c:16
#define ASSERT_HOST_EQUALS(hosts, i, string)
#define SIXTY
Definition hosts_tests.c:57
Describe(hosts)
BeforeEach(hosts)
Definition hosts_tests.c:13
static int host_value_eq(gvm_host_t *host, gchar *string)
GVM Networking related API.
size_t current
Definition hosts.h:92
gvm_host_t ** hosts
Definition hosts.h:90