Greenbone Vulnerability Management Libraries 22.8.0
hosts_tests.c File Reference
#include "hosts.c"
#include "networking.h"
#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>
Include dependency graph for hosts_tests.c:

Go to the source code of this file.

Macros

#define TEN   "0123456789"
 
#define SIXTY   TEN TEN TEN TEN TEN TEN
 
#define HUNDRED   TEN TEN TEN TEN TEN TEN TEN TEN TEN TEN
 

Functions

 Describe (hosts)
 
 BeforeEach (hosts)
 
 AfterEach (hosts)
 
 Ensure (hosts, gvm_hosts_new_never_returns_null)
 
 Ensure (hosts, gvm_get_host_type_returns_host_type_ipv4)
 
 Ensure (hosts, gvm_get_host_type_returns_host_type_ipv6)
 
 Ensure (hosts, gvm_get_host_type_returns_host_type_hostname)
 
 Ensure (hosts, gvm_get_host_type_returns_host_type_cidr_block)
 
 Ensure (hosts, gvm_get_host_type_returns_host_type_cidr6_block)
 
 Ensure (hosts, gvm_get_host_type_returns_host_type_range_short)
 
 Ensure (hosts, gvm_get_host_type_returns_host_type_range6_short)
 
 Ensure (hosts, gvm_get_host_type_returns_host_type_range_long)
 
 Ensure (hosts, gvm_get_host_type_returns_host_type_range6_long)
 
 Ensure (hosts, gvm_get_host_type_returns_error)
 
 Ensure (hosts, gvm_hosts_new_with_max_returns_success)
 
 Ensure (hosts, gvm_hosts_new_with_max_returns_error)
 
 Ensure (hosts, gvm_hosts_move_host_to_end)
 
 Ensure (hosts, gvm_hosts_allowed_only)
 
int main (int argc, char **argv)
 

Macro Definition Documentation

◆ HUNDRED

#define HUNDRED   TEN TEN TEN TEN TEN TEN TEN TEN TEN TEN

Definition at line 48 of file hosts_tests.c.

◆ SIXTY

#define SIXTY   TEN TEN TEN TEN TEN TEN

Definition at line 47 of file hosts_tests.c.

◆ TEN

#define TEN   "0123456789"

Definition at line 46 of file hosts_tests.c.

Function Documentation

◆ AfterEach()

AfterEach ( hosts  )

Definition at line 16 of file hosts_tests.c.

17{
18}

◆ BeforeEach()

BeforeEach ( hosts  )

Definition at line 13 of file hosts_tests.c.

14{
15}

◆ Describe()

Describe ( hosts  )

◆ Ensure() [1/15]

Ensure ( hosts  ,
gvm_get_host_type_returns_error   
)

Definition at line 155 of file hosts_tests.c.

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}
int gvm_get_host_type(const gchar *str_stripped)
Determines the host type in a buffer.
Definition: hosts.c:810
#define SIXTY
Definition: hosts_tests.c:47

References gvm_get_host_type(), and SIXTY.

Here is the call graph for this function:

◆ Ensure() [2/15]

Ensure ( hosts  ,
gvm_get_host_type_returns_host_type_cidr6_block   
)

Definition at line 84 of file hosts_tests.c.

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}
@ HOST_TYPE_CIDR6_BLOCK
Definition: hosts.h:41

References gvm_get_host_type(), and HOST_TYPE_CIDR6_BLOCK.

Here is the call graph for this function:

◆ Ensure() [3/15]

Ensure ( hosts  ,
gvm_get_host_type_returns_host_type_cidr_block   
)

Definition at line 70 of file hosts_tests.c.

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}
@ HOST_TYPE_CIDR_BLOCK
Definition: hosts.h:37

References gvm_get_host_type(), and HOST_TYPE_CIDR_BLOCK.

Here is the call graph for this function:

◆ Ensure() [4/15]

Ensure ( hosts  ,
gvm_get_host_type_returns_host_type_hostname   
)

Definition at line 50 of file hosts_tests.c.

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}
@ HOST_TYPE_NAME
Definition: hosts.h:35

References gvm_get_host_type(), HOST_TYPE_NAME, and SIXTY.

Here is the call graph for this function:

◆ Ensure() [5/15]

Ensure ( hosts  ,
gvm_get_host_type_returns_host_type_ipv4   
)

Definition at line 29 of file hosts_tests.c.

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}
@ HOST_TYPE_IPV4
Definition: hosts.h:36

References gvm_get_host_type(), and HOST_TYPE_IPV4.

Here is the call graph for this function:

◆ Ensure() [6/15]

Ensure ( hosts  ,
gvm_get_host_type_returns_host_type_ipv6   
)

Definition at line 39 of file hosts_tests.c.

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}
@ HOST_TYPE_IPV6
Definition: hosts.h:40

References gvm_get_host_type(), and HOST_TYPE_IPV6.

Here is the call graph for this function:

◆ Ensure() [7/15]

Ensure ( hosts  ,
gvm_get_host_type_returns_host_type_range6_long   
)

Definition at line 146 of file hosts_tests.c.

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}
@ HOST_TYPE_RANGE6_LONG
Definition: hosts.h:42

References gvm_get_host_type(), and HOST_TYPE_RANGE6_LONG.

Here is the call graph for this function:

◆ Ensure() [8/15]

Ensure ( hosts  ,
gvm_get_host_type_returns_host_type_range6_short   
)

Definition at line 116 of file hosts_tests.c.

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}
@ HOST_TYPE_RANGE6_SHORT
Definition: hosts.h:43

References gvm_get_host_type(), and HOST_TYPE_RANGE6_SHORT.

Here is the call graph for this function:

◆ Ensure() [9/15]

Ensure ( hosts  ,
gvm_get_host_type_returns_host_type_range_long   
)

Definition at line 124 of file hosts_tests.c.

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}
@ HOST_TYPE_RANGE_LONG
Definition: hosts.h:39

References gvm_get_host_type(), and HOST_TYPE_RANGE_LONG.

Here is the call graph for this function:

◆ Ensure() [10/15]

Ensure ( hosts  ,
gvm_get_host_type_returns_host_type_range_short   
)

Definition at line 94 of file hosts_tests.c.

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}
@ HOST_TYPE_RANGE_SHORT
Definition: hosts.h:38

References gvm_get_host_type(), and HOST_TYPE_RANGE_SHORT.

Here is the call graph for this function:

◆ Ensure() [11/15]

Ensure ( hosts  ,
gvm_hosts_allowed_only   
)

Definition at line 241 of file hosts_tests.c.

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}
gchar * gvm_host_value_str(const gvm_host_t *host)
Gets a host's value in printable format.
Definition: hosts.c:2267
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
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
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
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
void gvm_hosts_free(gvm_hosts_t *hosts)
Frees memory occupied by an gvm_hosts_t structure.
Definition: hosts.c:1358
The structure for a single host object.
Definition: hosts.h:61
The structure for Hosts collection.
Definition: hosts.h:88

References gvm_host_value_str(), gvm_hosts_allowed_only(), gvm_hosts_count(), gvm_hosts_free(), gvm_hosts_new(), and gvm_hosts_next().

Here is the call graph for this function:

◆ Ensure() [12/15]

Ensure ( hosts  ,
gvm_hosts_move_host_to_end   
)

Definition at line 205 of file hosts_tests.c.

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}
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
size_t current
Definition: hosts.h:92
gvm_host_t ** hosts
Definition: hosts.h:90

References gvm_hosts::current, gvm_host_value_str(), gvm_hosts_count(), gvm_hosts_free(), gvm_hosts_move_current_host_to_end(), gvm_hosts_new(), gvm_hosts_next(), and gvm_hosts::hosts.

Here is the call graph for this function:

◆ Ensure() [13/15]

Ensure ( hosts  ,
gvm_hosts_new_never_returns_null   
)

Definition at line 22 of file hosts_tests.c.

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}

References gvm_hosts_new().

Here is the call graph for this function:

◆ Ensure() [14/15]

Ensure ( hosts  ,
gvm_hosts_new_with_max_returns_error   
)

Definition at line 192 of file hosts_tests.c.

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}
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

References gvm_hosts_new_with_max().

Here is the call graph for this function:

◆ Ensure() [15/15]

Ensure ( hosts  ,
gvm_hosts_new_with_max_returns_success   
)

Definition at line 184 of file hosts_tests.c.

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}

References gvm_hosts_new_with_max().

Here is the call graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 276 of file hosts_tests.c.

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}

References gvm_hosts_allowed_only().

Here is the call graph for this function: