Greenbone Vulnerability Management Libraries 22.40.0
gmp_tests.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6#include "gmp.c"
7
8#include <cgreen/cgreen.h>
9#include <cgreen/mocks.h>
10
11/* Mock implementations of external functions */
12
13int
14__wrap_gvm_server_sendf (gnutls_session_t *session, const char *fmt, ...);
15int
16__wrap_gvm_server_sendf (gnutls_session_t *session, const char *fmt, ...)
17{
18 return (int) mock (session, fmt);
19}
20
21int
22__wrap_gvm_server_sendf_xml (gnutls_session_t *session, const char *fmt, ...);
23int
24__wrap_gvm_server_sendf_xml (gnutls_session_t *session, const char *fmt, ...)
25{
26 return (int) mock (session, fmt);
27}
28
29int
30__wrap_gvm_server_sendf_xml_quiet (gnutls_session_t *session, const char *fmt,
31 ...);
32int
33__wrap_gvm_server_sendf_xml_quiet (gnutls_session_t *session, const char *fmt,
34 ...)
35{
36 return (int) mock (session, fmt);
37}
38
39int
40__wrap_read_entity (gnutls_session_t *session, entity_t *entity);
41int
42__wrap_read_entity (gnutls_session_t *session, entity_t *entity)
43{
44 int result = (int) mock (session, entity);
45 if (result == 0 && entity != NULL)
46 {
47 *entity = (entity_t) mock ();
48 }
49 return result;
50}
51
52int
53__wrap_try_read_entity (gnutls_session_t *session, int timeout,
54 entity_t *entity);
55int
56__wrap_try_read_entity (gnutls_session_t *session, int timeout,
57 entity_t *entity)
58{
59 int result = (int) mock (session, timeout, entity);
60 if (result == 0 && entity != NULL)
61 {
62 *entity = (entity_t) mock ();
63 }
64 return result;
65}
66
67int
68__wrap_gvm_connection_sendf (gvm_connection_t *connection, const char *fmt,
69 ...);
70int
71__wrap_gvm_connection_sendf (gvm_connection_t *connection, const char *fmt, ...)
72{
73 return (int) mock (connection, fmt);
74}
75
76int
78 const char *fmt, ...);
79int
81 const char *fmt, ...)
82{
83 return (int) mock (connection, fmt);
84}
85
86int
88int
90{
91 int result = (int) mock (connection, entity);
92 if (result == 0 && entity != NULL)
93 {
94 *entity = (entity_t) mock ();
95 }
96 return result;
97}
98
99int
100__wrap_try_read_entity_c (gvm_connection_t *connection, int timeout,
101 entity_t *entity);
102int
104 entity_t *entity)
105{
106 int result = (int) mock (connection, timeout, entity);
107 if (result == 0 && entity != NULL)
108 {
109 *entity = (entity_t) mock ();
110 }
111 return result;
112}
113
116{
117}
118
120{
121}
122
123/* Helper function to create mock entities */
124static entity_t
125create_mock_entity (const char *name, const char *text)
126{
127 entity_t entity = g_malloc0 (sizeof (struct entity_s));
128 entity->name = g_strdup (name);
129 entity->text = g_strdup (text);
130 entity->attributes =
131 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
132 return entity;
133}
134
135/* Helper function to add attributes to mock entities */
136static void
137add_mock_attribute (entity_t entity, const char *name, const char *value)
138{
139 g_hash_table_insert (entity->attributes, g_strdup (name), g_strdup (value));
140}
141
142/* Helper function to add parent to mock entity */
143static void
145{
146 parent->entities = g_slist_append (parent->entities, child);
147}
148
149/* gmp_task_status */
150
151Ensure (gmp, gmp_task_status_returns_correct_string_for_valid_response)
152{
153 entity_t response = create_mock_entity ("get_tasks_response", "");
154 entity_t task = create_mock_entity ("task", "");
155 response->entities = g_slist_append (NULL, task);
156 entity_t status = create_mock_entity ("status", "Running");
157 task->entities = g_slist_append (NULL, status);
158
159 const char *result = gmp_task_status (response);
160
161 assert_that (result, is_not_null);
162 assert_that (result, is_equal_to_string ("Running"));
163
164 free_entity (response);
165}
166
167Ensure (gmp, gmp_task_status_returns_null_when_no_task)
168{
169 entity_t response = create_mock_entity ("get_tasks_response", "");
170
171 const char *result = gmp_task_status (response);
172
173 assert_that (result, is_null);
174
175 free_entity (response);
176}
177
178Ensure (gmp, gmp_task_status_returns_null_when_no_status)
179{
180 entity_t response = create_mock_entity ("get_tasks_response", "");
181 entity_t task = create_mock_entity ("task", "");
182 response->entities = g_slist_append (NULL, task);
183
184 const char *result = gmp_task_status (response);
185
186 assert_that (result, is_null);
187
188 free_entity (response);
189}
190
191/* gmp_read_create_response */
192
193Ensure (gmp, gmp_read_create_response_returns_uuid_on_success)
194{
195 entity_t mock_entity = create_mock_entity ("create_response", "");
196 add_mock_attribute (mock_entity, "status", "201");
197 add_mock_attribute (mock_entity, "id",
198 "12345678-1234-1234-1234-123456789012");
199
200 expect (__wrap_read_entity, will_return (0));
201 expect (__wrap_read_entity, will_return (mock_entity));
202
203 gchar *uuid = NULL;
204 int result = gmp_read_create_response (NULL, &uuid);
205
206 assert_that (result, is_equal_to (201));
207 assert_that (uuid, is_not_null);
208 assert_that (uuid,
209 is_equal_to_string ("12345678-1234-1234-1234-123456789012"));
210
211 g_free (uuid);
212}
213
214Ensure (gmp, gmp_read_create_response_succeeds_without_uuid_parameter)
215{
216 entity_t mock_entity = create_mock_entity ("create_response", "");
217 add_mock_attribute (mock_entity, "status", "201");
218 add_mock_attribute (mock_entity, "id",
219 "12345678-1234-1234-1234-123456789012");
220
221 expect (__wrap_read_entity, will_return (0));
222 expect (__wrap_read_entity, will_return (mock_entity));
223
224 int result = gmp_read_create_response (NULL, NULL);
225
226 assert_that (result, is_equal_to (201));
227}
228
229Ensure (gmp, gmp_read_create_response_returns_error_when_no_status)
230{
231 entity_t mock_entity = create_mock_entity ("create_response", "");
232
233 expect (__wrap_read_entity, will_return (0));
234 expect (__wrap_read_entity, will_return (mock_entity));
235
236 gchar *uuid = NULL;
237 int result = gmp_read_create_response (NULL, &uuid);
238
239 assert_that (result, is_equal_to (-1));
240 assert_that (uuid, is_null);
241}
242
243Ensure (gmp, gmp_read_create_response_returns_error_on_read_failure)
244{
245 expect (__wrap_read_entity, will_return (-1));
246
247 gchar *uuid = NULL;
248 int result = gmp_read_create_response (NULL, &uuid);
249
250 assert_that (result, is_equal_to (-1));
251 assert_that (uuid, is_null);
252}
253
254/* gmp_check_response */
255
256Ensure (gmp, gmp_check_response_succeeds_with_valid_response)
257{
258 entity_t mock_entity = create_mock_entity ("response", "");
259 add_mock_attribute (mock_entity, "status", "200");
260
261 expect (__wrap_read_entity, will_return (0));
262 expect (__wrap_read_entity, will_return (mock_entity));
263
264 entity_t entity = NULL;
265 int result = gmp_check_response (NULL, &entity);
266
267 assert_that (result, is_equal_to (0));
268 assert_that (entity, is_not_null);
269
270 free_entity (mock_entity);
271}
272
273Ensure (gmp, gmp_check_response_returns_gmp_error_code)
274{
275 entity_t mock_entity = create_mock_entity ("response", "");
276 add_mock_attribute (mock_entity, "status", "400");
277
278 expect (__wrap_read_entity, will_return (0));
279 expect (__wrap_read_entity, will_return (mock_entity));
280
281 entity_t entity = NULL;
282 int result = gmp_check_response (NULL, &entity);
283
284 assert_that (result, is_equal_to (400));
285 assert_that (entity, is_null);
286}
287
288Ensure (gmp, gmp_check_response_returns_error_on_read_failure)
289{
290 expect (__wrap_read_entity, will_return (-1));
291
292 entity_t entity = NULL;
293 int result = gmp_check_response (NULL, &entity);
294
295 assert_that (result, is_equal_to (-1));
296 assert_that (entity, is_null);
297}
298
299Ensure (gmp, gmp_check_response_returns_error_when_no_status)
300{
301 entity_t mock_entity = create_mock_entity ("response", "");
302
303 expect (__wrap_read_entity, will_return (0));
304 expect (__wrap_read_entity, will_return (mock_entity));
305
306 entity_t entity = NULL;
307 int result = gmp_check_response (NULL, &entity);
308
309 assert_that (result, is_equal_to (-1));
310 assert_that (entity, is_null);
311}
312
313/* gmp_ping */
314
315Ensure (gmp, gmp_ping_succeeds_with_valid_response)
316{
317 expect (__wrap_gvm_server_sendf, will_return (0));
318
319 entity_t mock_entity = create_mock_entity ("get_version_response", "");
320 add_mock_attribute (mock_entity, "status", "200");
321
322 expect (__wrap_try_read_entity, will_return (0));
323 expect (__wrap_try_read_entity, will_return (mock_entity));
324
325 int result = gmp_ping (NULL, 0);
326
327 assert_that (result, is_equal_to (0));
328}
329
330Ensure (gmp, gmp_ping_returns_error_on_send_failure)
331{
332 expect (__wrap_gvm_server_sendf, will_return (-1));
333
334 int result = gmp_ping (NULL, 0);
335
336 assert_that (result, is_equal_to (-1));
337}
338
339Ensure (gmp, gmp_ping_returns_error_on_read_failure)
340{
341 expect (__wrap_gvm_server_sendf, will_return (0));
342 expect (__wrap_try_read_entity, will_return (-1));
343
344 int result = gmp_ping (NULL, 0);
345
346 assert_that (result, is_equal_to (-1));
347}
348
349Ensure (gmp, gmp_ping_returns_error_with_invalid_status)
350{
351 expect (__wrap_gvm_server_sendf, will_return (0));
352
353 entity_t mock_entity = create_mock_entity ("get_version_response", "");
354 add_mock_attribute (mock_entity, "status", "500");
355
356 expect (__wrap_try_read_entity, will_return (0));
357 expect (__wrap_try_read_entity, will_return (mock_entity));
358
359 int result = gmp_ping (NULL, 0);
360
361 assert_that (result, is_equal_to (-1));
362}
363
364Ensure (gmp, gmp_authenticate_info_ext_c_returns_success_and_sets_outputs)
365{
366 entity_t response = create_mock_entity ("authenticate_response", "");
367 add_mock_attribute (response, "status", "200");
368
369 entity_t timezone = create_mock_entity ("timezone", "Europe/Berlin");
370 entity_t role = create_mock_entity ("role", "Admin");
371 entity_t pw_warning =
372 create_mock_entity ("password_warning", "Password is short");
373 entity_t token = create_mock_entity ("token", "jwt-token-value");
374
375 add_mock_child (response, timezone);
376 add_mock_child (response, role);
377 add_mock_child (response, pw_warning);
378 add_mock_child (response, token);
379
380 expect (__wrap_gvm_connection_sendf_xml_quiet, will_return (0));
381 expect (__wrap_try_read_entity_c, will_return (0));
382 expect (__wrap_try_read_entity_c, will_return (response));
383
384 char *out_role = NULL;
385 char *out_timezone = NULL;
386 char *out_pw_warning = NULL;
387 char *out_jwt = NULL;
388
390 opts.timeout = 10;
391 opts.username = "admin";
392 opts.password = "secret";
393 opts.role = &out_role;
394 opts.timezone = &out_timezone;
395 opts.pw_warning = &out_pw_warning;
396 opts.jwt_requested = 1;
397 opts.jwt = &out_jwt;
398
399 int result = gmp_authenticate_info_ext_c (NULL, opts);
400
401 assert_that (result, is_equal_to (0));
402 assert_that (out_role, is_equal_to_string ("Admin"));
403 assert_that (out_timezone, is_equal_to_string ("Europe/Berlin"));
404 assert_that (out_pw_warning, is_equal_to_string ("Password is short"));
405 assert_that (out_jwt, is_equal_to_string ("jwt-token-value"));
406
407 g_free (out_role);
408 g_free (out_timezone);
409 g_free (out_pw_warning);
410 g_free (out_jwt);
411}
412
413Ensure (gmp, gmp_authenticate_info_ext_c_succeeds_without_optional_outputs)
414{
415 entity_t response = create_mock_entity ("authenticate_response", "");
416 add_mock_attribute (response, "status", "200");
417
418 expect (__wrap_gvm_connection_sendf_xml_quiet, will_return (0));
419 expect (__wrap_try_read_entity_c, will_return (0));
420 expect (__wrap_try_read_entity_c, will_return (response));
421
423 opts.timeout = 10;
424 opts.username = "admin";
425 opts.password = "secret";
426
427 int result = gmp_authenticate_info_ext_c (NULL, opts);
428
429 assert_that (result, is_equal_to (0));
430}
431
432Ensure (gmp, gmp_authenticate_info_ext_c_returns_send_error)
433{
434 expect (__wrap_gvm_connection_sendf_xml_quiet, will_return (-1));
435
437 opts.username = "admin";
438 opts.password = "secret";
439
440 int result = gmp_authenticate_info_ext_c (NULL, opts);
441
442 assert_that (result, is_equal_to (-1));
443}
444
445Ensure (gmp, gmp_authenticate_info_ext_c_returns_timeout_on_try_read_timeout)
446{
447 expect (__wrap_gvm_connection_sendf_xml_quiet, will_return (0));
448 expect (__wrap_try_read_entity_c, will_return (-4));
449
451 opts.timeout = 10;
452 opts.username = "admin";
453 opts.password = "secret";
454
455 int result = gmp_authenticate_info_ext_c (NULL, opts);
456
457 assert_that (result, is_equal_to (3));
458}
459
460Ensure (gmp, gmp_authenticate_info_ext_c_returns_error_on_read_failure)
461{
462 expect (__wrap_gvm_connection_sendf_xml_quiet, will_return (0));
463 expect (__wrap_try_read_entity_c, will_return (-1));
464
466 opts.timeout = 10;
467 opts.username = "admin";
468 opts.password = "secret";
469
470 int result = gmp_authenticate_info_ext_c (NULL, opts);
471
472 assert_that (result, is_equal_to (-1));
473}
474
475Ensure (gmp, gmp_authenticate_info_ext_c_returns_error_when_status_empty)
476{
477 entity_t response = create_mock_entity ("authenticate_response", "");
478 add_mock_attribute (response, "status", "");
479
480 expect (__wrap_gvm_connection_sendf_xml_quiet, will_return (0));
481 expect (__wrap_try_read_entity_c, will_return (0));
482 expect (__wrap_try_read_entity_c, will_return (response));
483
485 opts.timeout = 10;
486 opts.username = "admin";
487 opts.password = "secret";
488
489 int result = gmp_authenticate_info_ext_c (NULL, opts);
490
491 assert_that (result, is_equal_to (-1));
492}
493
494Ensure (gmp, gmp_authenticate_info_ext_c_returns_2_on_auth_failure)
495{
496 entity_t response = create_mock_entity ("authenticate_response", "");
497 add_mock_attribute (response, "status", "400");
498
499 expect (__wrap_gvm_connection_sendf_xml_quiet, will_return (0));
500 expect (__wrap_try_read_entity_c, will_return (0));
501 expect (__wrap_try_read_entity_c, will_return (response));
502
504 opts.timeout = 10;
505 opts.username = "admin";
506 opts.password = "wrong";
507
508 int result = gmp_authenticate_info_ext_c (NULL, opts);
509
510 assert_that (result, is_equal_to (2));
511}
512
514 gmp_authenticate_info_ext_c_leaves_jwt_null_when_requested_but_missing)
515{
516 entity_t response = create_mock_entity ("authenticate_response", "");
517 add_mock_attribute (response, "status", "200");
518
519 expect (__wrap_gvm_connection_sendf_xml_quiet, will_return (0));
520 expect (__wrap_try_read_entity_c, will_return (0));
521 expect (__wrap_try_read_entity_c, will_return (response));
522
523 char *out_jwt = (char *) 0x1;
524
526 opts.timeout = 10;
527 opts.username = "admin";
528 opts.password = "secret";
529 opts.jwt_requested = 1;
530 opts.jwt = &out_jwt;
531
532 int result = gmp_authenticate_info_ext_c (NULL, opts);
533
534 assert_that (result, is_equal_to (0));
535 assert_that (out_jwt, is_null);
536}
537
539 gmp_authenticate_info_ext_c_leaves_optional_outputs_null_when_absent)
540{
541 entity_t response = create_mock_entity ("authenticate_response", "");
542 add_mock_attribute (response, "status", "200");
543
544 expect (__wrap_gvm_connection_sendf_xml_quiet, will_return (0));
545 expect (__wrap_try_read_entity_c, will_return (0));
546 expect (__wrap_try_read_entity_c, will_return (response));
547
548 char *out_role = (char *) 0x1;
549 char *out_timezone = (char *) 0x1;
550 char *out_pw_warning = (char *) 0x1;
551
553 opts.timeout = 10;
554 opts.username = "admin";
555 opts.password = "secret";
556 opts.role = &out_role;
557 opts.timezone = &out_timezone;
558 opts.pw_warning = &out_pw_warning;
559
560 int result = gmp_authenticate_info_ext_c (NULL, opts);
561
562 assert_that (result, is_equal_to (0));
563 assert_that (out_role, is_null);
564 assert_that (out_timezone, is_null);
565 assert_that (out_pw_warning, is_null);
566}
567
568/* Test suite. */
569
570int
571main (int argc, char **argv)
572{
573 int ret;
574 TestSuite *suite;
575
576 suite = create_test_suite ();
577
578 add_test_with_context (
579 suite, gmp, gmp_task_status_returns_correct_string_for_valid_response);
580 add_test_with_context (suite, gmp, gmp_task_status_returns_null_when_no_task);
581 add_test_with_context (suite, gmp,
582 gmp_task_status_returns_null_when_no_status);
583
584 add_test_with_context (suite, gmp,
585 gmp_read_create_response_returns_uuid_on_success);
586 add_test_with_context (
587 suite, gmp, gmp_read_create_response_succeeds_without_uuid_parameter);
588 add_test_with_context (suite, gmp,
589 gmp_read_create_response_returns_error_when_no_status);
590 add_test_with_context (
591 suite, gmp, gmp_read_create_response_returns_error_on_read_failure);
592
593 add_test_with_context (suite, gmp,
594 gmp_check_response_succeeds_with_valid_response);
595 add_test_with_context (suite, gmp, gmp_check_response_returns_gmp_error_code);
596 add_test_with_context (suite, gmp,
597 gmp_check_response_returns_error_on_read_failure);
598 add_test_with_context (suite, gmp,
599 gmp_check_response_returns_error_when_no_status);
600
601 add_test_with_context (suite, gmp, gmp_ping_succeeds_with_valid_response);
602 add_test_with_context (suite, gmp, gmp_ping_returns_error_on_send_failure);
603 add_test_with_context (suite, gmp, gmp_ping_returns_error_on_read_failure);
604 add_test_with_context (suite, gmp,
605 gmp_ping_returns_error_with_invalid_status);
606 add_test_with_context (
607 suite, gmp, gmp_authenticate_info_ext_c_returns_success_and_sets_outputs);
608 add_test_with_context (
609 suite, gmp, gmp_authenticate_info_ext_c_succeeds_without_optional_outputs);
610 add_test_with_context (suite, gmp,
611 gmp_authenticate_info_ext_c_returns_send_error);
612 add_test_with_context (
613 suite, gmp,
614 gmp_authenticate_info_ext_c_returns_timeout_on_try_read_timeout);
615 add_test_with_context (
616 suite, gmp, gmp_authenticate_info_ext_c_returns_error_on_read_failure);
617 add_test_with_context (
618 suite, gmp, gmp_authenticate_info_ext_c_returns_error_when_status_empty);
619 add_test_with_context (suite, gmp,
620 gmp_authenticate_info_ext_c_returns_2_on_auth_failure);
621 add_test_with_context (
622 suite, gmp,
623 gmp_authenticate_info_ext_c_leaves_jwt_null_when_requested_but_missing);
624 add_test_with_context (
625 suite, gmp,
626 gmp_authenticate_info_ext_c_leaves_optional_outputs_null_when_absent);
627
628 if (argc > 1)
629 ret = run_single_test (suite, argv[1], create_text_reporter ());
630 else
631 ret = run_test_suite (suite, create_text_reporter ());
632
633 destroy_test_suite (suite);
634
635 return ret;
636}
API for Greenbone Management Protocol communication.
const char * gmp_task_status(entity_t response)
Get the task status from a GMP GET_TASKS response.
Definition gmp.c:48
int gmp_ping(gnutls_session_t *session, int timeout)
"Ping" the manager.
Definition gmp.c:190
int gmp_authenticate_info_ext_c(gvm_connection_t *connection, gmp_authenticate_info_opts_t opts)
Authenticate with the manager.
Definition gmp.c:445
int gmp_read_create_response(gnutls_session_t *session, gchar **uuid)
Read response status and resource UUID.
Definition gmp.c:890
static int gmp_check_response(gnutls_session_t *session, entity_t *entity)
Read response and convert status of response to a return value.
Definition gmp.c:70
static const gmp_authenticate_info_opts_t gmp_authenticate_info_opts_defaults
Sensible default values for gmp_authenticate_info_opts_t.
Definition gmp.h:41
int __wrap_read_entity(gnutls_session_t *session, entity_t *entity)
Definition gmp_tests.c:42
static void add_mock_attribute(entity_t entity, const char *name, const char *value)
Definition gmp_tests.c:137
int __wrap_gvm_server_sendf(gnutls_session_t *session, const char *fmt,...)
Definition gmp_tests.c:16
int __wrap_gvm_connection_sendf(gvm_connection_t *connection, const char *fmt,...)
Definition gmp_tests.c:71
int main(int argc, char **argv)
Definition gmp_tests.c:571
int __wrap_gvm_connection_sendf_xml_quiet(gvm_connection_t *connection, const char *fmt,...)
Definition gmp_tests.c:80
static entity_t create_mock_entity(const char *name, const char *text)
Definition gmp_tests.c:125
int __wrap_gvm_server_sendf_xml(gnutls_session_t *session, const char *fmt,...)
Definition gmp_tests.c:24
int __wrap_try_read_entity_c(gvm_connection_t *connection, int timeout, entity_t *entity)
Definition gmp_tests.c:103
BeforeEach(gmp)
Definition gmp_tests.c:115
Describe(gmp)
Ensure(gmp, gmp_task_status_returns_correct_string_for_valid_response)
Definition gmp_tests.c:151
AfterEach(gmp)
Definition gmp_tests.c:119
int __wrap_gvm_server_sendf_xml_quiet(gnutls_session_t *session, const char *fmt,...)
Definition gmp_tests.c:33
int __wrap_read_entity_c(gvm_connection_t *connection, entity_t *entity)
Definition gmp_tests.c:89
int __wrap_try_read_entity(gnutls_session_t *session, int timeout, entity_t *entity)
Definition gmp_tests.c:56
static void add_mock_child(entity_t parent, entity_t child)
Definition gmp_tests.c:144
XML element.
Definition xmlutils.h:52
entities_t entities
Children.
Definition xmlutils.h:56
char * text
Text.
Definition xmlutils.h:54
GHashTable * attributes
Attributes.
Definition xmlutils.h:55
char * name
Name.
Definition xmlutils.h:53
Struct holding options for authentication.
Definition gmp.h:27
const char * password
Username.
Definition gmp.h:30
int timeout
Timeout for authentication.
Definition gmp.h:28
int jwt_requested
Flag for JWT. 0 = false, 1 = true.
Definition gmp.h:34
char ** pw_warning
[out] Password warning, NULL if password is okay.
Definition gmp.h:33
char ** timezone
[out] Timezone if any, else NULL.
Definition gmp.h:32
char ** jwt
[out] JWT token value, NULL if not requested.
Definition gmp.h:35
const char * username
Password.
Definition gmp.h:29
char ** role
[out] Role.
Definition gmp.h:31
Connection.
Definition serverutils.h:30
void free_entity(entity_t entity)
Free an entity, recursively.
Definition xmlutils.c:115
struct entity_s * entity_t
Definition xmlutils.h:58