OpenVAS Scanner  7.0.0~git
attack_tests.c
Go to the documentation of this file.
1 /* Copyright (C) 2019 Greenbone Networks GmbH
2  *
3  * SPDX-License-Identifier: GPL-2.0-or-later
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #include "attack.c"
21 
22 #include <cgreen/cgreen.h>
23 #include <cgreen/mocks.h>
24 
25 Describe (attack);
26 BeforeEach (attack)
27 {
28 }
29 AfterEach (attack)
30 {
31 }
32 
33 /* comm_send_status */
34 
35 gchar *given_name = NULL;
36 gchar *given_value = NULL;
37 
38 int
39 __wrap_redis_push_str (kb_t kb, const char *name, const char *value)
40 {
41  (void) kb; /* Used. */
42  given_name = g_strdup (name);
43  given_value = g_strdup (value);
44  mock ();
45  return 0;
46 }
47 
48 Ensure (attack, comm_send_status_returns_neg1_for_null_args)
49 {
50  struct kb kb_struct;
51  kb_t kb;
52 
53  /* Create a dummy kb. */
54  kb = &kb_struct;
55 
56  never_expect (__wrap_redis_push_str);
57  assert_that (comm_send_status (NULL, "example", 0, 100), is_equal_to (-1));
58  assert_that (comm_send_status (kb, NULL, 0, 100), is_equal_to (-1));
59 }
60 
61 Ensure (attack, comm_send_status_error_if_hostname_too_big)
62 {
63  struct kb kb_struct;
64  kb_t kb;
65  gchar *long_host;
66  int index;
67 
68  /* Create a dummy kb. */
69  kb = &kb_struct;
70 
71  long_host = g_malloc (2049);
72  for (index = 0; index < 2049; index++)
73  long_host[index] = 'a';
74  long_host[2048] = '\0';
75 
76  never_expect (__wrap_redis_push_str);
77  assert_that (comm_send_status (kb, long_host, 0, 100), is_equal_to (-1));
78 
79  g_free (long_host);
80 }
81 
82 Ensure (attack, comm_send_status_sends_correct_text)
83 {
84  struct kb kb_struct;
85  struct kb_operations kb_ops_struct;
86  kb_t kb;
87 
88  /* Create a dummy kb. */
89  kb = &kb_struct;
90 
91  /* We can't wrap kb_item_push_str because it is inline, so we have to do
92  * a little hacking. */
93  kb_ops_struct.kb_push_str = __wrap_redis_push_str;
94  kb->kb_ops = &kb_ops_struct;
95 
96  expect (__wrap_redis_push_str);
97  assert_that (comm_send_status (kb, "127.0.0.1", 11, 67), is_equal_to (0));
98  assert_that (strcmp (given_name, "internal/status"), is_equal_to (0));
99  assert_that (strcmp (given_value, "11/67"), is_equal_to (0));
100 
101  g_free (given_name);
102  g_free (given_value);
103 }
104 
105 int
106 main (int argc, char **argv)
107 {
108  TestSuite *suite;
109 
110  suite = create_test_suite ();
111 
112  add_test_with_context (suite, attack,
113  comm_send_status_returns_neg1_for_null_args);
114  add_test_with_context (suite, attack,
115  comm_send_status_error_if_hostname_too_big);
116  add_test_with_context (suite, attack, comm_send_status_sends_correct_text);
117 
118  if (argc > 1)
119  return run_single_test (suite, argv[1], create_text_reporter ());
120 
121  return run_test_suite (suite, create_text_reporter ());
122 }
AfterEach
AfterEach(attack)
Definition: attack_tests.c:29
comm_send_status
static int comm_send_status(kb_t kb, char *hostname, int curr, int max)
Sends the status of a host's scan.
Definition: attack.c:156
name
const char * name
Definition: nasl_init.c:377
given_name
gchar * given_name
Definition: attack_tests.c:35
given_value
gchar * given_value
Definition: attack_tests.c:36
__wrap_redis_push_str
int __wrap_redis_push_str(kb_t kb, const char *name, const char *value)
Definition: attack_tests.c:39
attack.c
Launches the plugins, and manages multithreading.
Describe
Describe(attack)
Ensure
Ensure(attack, comm_send_status_returns_neg1_for_null_args)
Definition: attack_tests.c:48
BeforeEach
BeforeEach(attack)
Definition: attack_tests.c:26
main
int main(int argc, char **argv)
Definition: attack_tests.c:106