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