1 /* Copyright (C) 2019-2021 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);
BeforeEach(attack)26 BeforeEach (attack)
27 {
28 }
AfterEach(attack)29 AfterEach (attack)
30 {
31 }
32 
33 /* comm_send_status */
34 
35 gchar *given_name = NULL;
36 gchar *given_value = NULL;
37 
38 int
__wrap_redis_push_str(kb_t kb,const char * name,const char * value)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 int
__wrap_redis_lnk_reset(kb_t kb)49 __wrap_redis_lnk_reset (kb_t kb)
50 {
51   (void) kb; /* Used. */
52   mock ();
53   return 0;
54 }
55 
Ensure(attack,comm_send_status_returns_neg1_for_null_args)56 Ensure (attack, comm_send_status_returns_neg1_for_null_args)
57 {
58   struct kb kb_struct;
59   kb_t kb;
60 
61   /* Create a dummy kb. */
62   kb = &kb_struct;
63 
64   never_expect (__wrap_redis_push_str);
65   assert_that (comm_send_status (NULL, "example", 0, 100), is_equal_to (-1));
66   assert_that (comm_send_status (kb, NULL, 0, 100), is_equal_to (-1));
67 }
68 
Ensure(attack,comm_send_status_error_if_hostname_too_big)69 Ensure (attack, comm_send_status_error_if_hostname_too_big)
70 {
71   struct kb kb_struct;
72   kb_t kb;
73   gchar *long_host;
74   int index;
75 
76   /* Create a dummy kb. */
77   kb = &kb_struct;
78 
79   long_host = g_malloc (2049);
80   for (index = 0; index < 2049; index++)
81     long_host[index] = 'a';
82   long_host[2048] = '\0';
83 
84   never_expect (__wrap_redis_push_str);
85   assert_that (comm_send_status (kb, long_host, 0, 100), is_equal_to (-1));
86 
87   g_free (long_host);
88 }
89 
Ensure(attack,comm_send_status_sends_correct_text)90 Ensure (attack, comm_send_status_sends_correct_text)
91 {
92   struct kb kb_struct;
93   struct kb_operations kb_ops_struct;
94   kb_t kb;
95 
96   /* Create a dummy kb. */
97   kb = &kb_struct;
98 
99   /* We can't wrap kb_item_push_str because it is inline, so we have to do
100    * a little hacking. */
101   kb_ops_struct.kb_push_str = __wrap_redis_push_str;
102   kb_ops_struct.kb_lnk_reset = __wrap_redis_lnk_reset;
103   kb->kb_ops = &kb_ops_struct;
104 
105   expect (__wrap_redis_push_str);
106   expect (__wrap_redis_lnk_reset);
107   assert_that (comm_send_status (kb, "127.0.0.1", 11, 67), is_equal_to (0));
108   assert_that (strcmp (given_name, "internal/status"), is_equal_to (0));
109   assert_that (strcmp (given_value, "127.0.0.1/11/67"), is_equal_to (0));
110 
111   g_free (given_name);
112   g_free (given_value);
113 }
114 
115 int
main(int argc,char ** argv)116 main (int argc, char **argv)
117 {
118   TestSuite *suite;
119 
120   suite = create_test_suite ();
121 
122   add_test_with_context (suite, attack,
123                          comm_send_status_returns_neg1_for_null_args);
124   add_test_with_context (suite, attack,
125                          comm_send_status_error_if_hostname_too_big);
126   add_test_with_context (suite, attack, comm_send_status_sends_correct_text);
127 
128   if (argc > 1)
129     return run_single_test (suite, argv[1], create_text_reporter ());
130 
131   return run_test_suite (suite, create_text_reporter ());
132 }
133