OpenVAS Scanner 22.7.9
attack_tests.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6#include "attack.c"
7
8#include <cgreen/cgreen.h>
9#include <cgreen/mocks.h>
10
11Describe (attack);
12BeforeEach (attack)
13{
14}
15AfterEach (attack)
16{
17}
18
19/* comm_send_status */
20
21gchar *given_name = NULL;
22gchar *given_value = NULL;
23
24static int
25__wrap_redis_push_str (kb_t kb, const char *name, const char *value)
26{
27 (void) kb; /* Used. */
28 given_name = g_strdup (name);
29 given_value = g_strdup (value);
30 mock ();
31 return 0;
32}
33
34static int
36{
37 (void) kb; /* Used. */
38 mock ();
39 return 0;
40}
41
42Ensure (attack, comm_send_status_returns_neg1_for_null_args)
43{
44 struct kb kb_struct;
45 kb_t kb;
46
47 /* Create a dummy kb. */
48 kb = &kb_struct;
49
50 never_expect (__wrap_redis_push_str);
51 assert_that (comm_send_status (NULL, "example", 0, 100), is_equal_to (-1));
52 assert_that (comm_send_status (kb, NULL, 0, 100), is_equal_to (-1));
53}
54
55Ensure (attack, comm_send_status_error_if_hostname_too_big)
56{
57 struct kb kb_struct;
58 kb_t kb;
59 gchar *long_host;
60 int index;
61
62 /* Create a dummy kb. */
63 kb = &kb_struct;
64
65 long_host = g_malloc (2049);
66 for (index = 0; index < 2049; index++)
67 long_host[index] = 'a';
68 long_host[2048] = '\0';
69
70 never_expect (__wrap_redis_push_str);
71 assert_that (comm_send_status (kb, long_host, 0, 100), is_equal_to (-1));
72
73 g_free (long_host);
74}
75
76Ensure (attack, comm_send_status_sends_correct_text)
77{
78 struct kb kb_struct;
79 struct kb_operations kb_ops_struct;
80 kb_t kb;
81
82 /* Create a dummy kb. */
83 kb = &kb_struct;
84
85 /* We can't wrap kb_item_push_str because it is inline, so we have to do
86 * a little hacking. */
87 kb_ops_struct.kb_push_str = __wrap_redis_push_str;
88 kb_ops_struct.kb_lnk_reset = __wrap_redis_lnk_reset;
89 kb->kb_ops = &kb_ops_struct;
90
91 set_main_kb (kb);
92 expect (__wrap_redis_push_str);
94 assert_that (comm_send_status (kb, "127.0.0.1", 11, 67), is_equal_to (0));
95 assert_that (strcmp (given_name, "internal/status"), is_equal_to (0));
96 assert_that (strcmp (given_value, "127.0.0.1/11/67"), is_equal_to (0));
97
98 g_free (given_name);
99 g_free (given_value);
100}
101
102int
103main (int argc, char **argv)
104{
105 TestSuite *suite;
106
107 suite = create_test_suite ();
108
109 add_test_with_context (suite, attack,
110 comm_send_status_returns_neg1_for_null_args);
111 add_test_with_context (suite, attack,
112 comm_send_status_error_if_hostname_too_big);
113 add_test_with_context (suite, attack, comm_send_status_sends_correct_text);
114
115 if (argc > 1)
116 return run_single_test (suite, argv[1], create_text_reporter ());
117
118 return run_test_suite (suite, create_text_reporter ());
119}
Launches the plugins, and manages multithreading.
static int comm_send_status(kb_t main_kb, char *ip_str, int curr, int max)
Sends the progress status of of a host's scan.
Definition: attack.c:204
gchar * given_value
Definition: attack_tests.c:22
Describe(attack)
BeforeEach(attack)
Definition: attack_tests.c:12
int main(int argc, char **argv)
Definition: attack_tests.c:103
static int __wrap_redis_push_str(kb_t kb, const char *name, const char *value)
Definition: attack_tests.c:25
gchar * given_name
Definition: attack_tests.c:21
AfterEach(attack)
Definition: attack_tests.c:15
static int __wrap_redis_lnk_reset(kb_t kb)
Definition: attack_tests.c:35
Ensure(attack, comm_send_status_returns_neg1_for_null_args)
Definition: attack_tests.c:42
void set_main_kb(kb_t kb)
sets the shared database between ospd and openvas as a main_kb for further usage. @description this s...
Definition: kb_cache.c:27
const char * name
Definition: nasl_init.c:411