1 /* Copyright (C) 2009-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 "nvti.c"
21
22 #include <cgreen/cgreen.h>
23 #include <cgreen/mocks.h>
24
25 Describe (nvti);
BeforeEach(nvti)26 BeforeEach (nvti)
27 {
28 }
AfterEach(nvti)29 AfterEach (nvti)
30 {
31 }
32
33 /* make_nvti */
34
Ensure(nvti,nvti_new_never_returns_null)35 Ensure (nvti, nvti_new_never_returns_null)
36 {
37 assert_that (nvti_new (), is_not_null);
38 }
39
40 /* nvti solution_method */
41
Ensure(nvti,nvti_set_solution_method_correct)42 Ensure (nvti, nvti_set_solution_method_correct)
43 {
44 nvti_t *nvti;
45 gchar *solution_method;
46
47 nvti = nvti_new ();
48 nvti_set_solution_method (nvti, "DebianAPTUpgrade");
49 solution_method = nvti_solution_method (nvti);
50
51 assert_that (solution_method, is_equal_to_string ("DebianAPTUpgrade"));
52
53 nvti_free (nvti);
54 }
55
56 /* nvti_get_tag */
57
Ensure(nvti,nvti_get_tag_gets_correct_value_one_tag)58 Ensure (nvti, nvti_get_tag_gets_correct_value_one_tag)
59 {
60 nvti_t *nvti;
61 gchar *tag;
62
63 nvti = nvti_new ();
64 nvti_set_tag (nvti, "a=1");
65 tag = nvti_get_tag (nvti, "a");
66
67 assert_that (tag, is_equal_to_string ("1"));
68
69 g_free (tag);
70 nvti_free (nvti);
71 }
72
Ensure(nvti,nvti_get_tag_gets_correct_value_many_tags)73 Ensure (nvti, nvti_get_tag_gets_correct_value_many_tags)
74 {
75 nvti_t *nvti;
76 gchar *tag;
77
78 nvti = nvti_new ();
79 nvti_set_tag (nvti, "a=1|b=2|c=3");
80 tag = nvti_get_tag (nvti, "b");
81
82 assert_that (tag, is_equal_to_string ("2"));
83
84 g_free (tag);
85 nvti_free (nvti);
86 }
87
Ensure(nvti,nvti_get_tag_handles_empty_tag)88 Ensure (nvti, nvti_get_tag_handles_empty_tag)
89 {
90 nvti_t *nvti;
91
92 nvti = nvti_new ();
93
94 assert_that (nvti_get_tag (nvti, "b"), is_null);
95
96 nvti_free (nvti);
97 }
98
Ensure(nvti,nvti_get_tag_handles_null_nvti)99 Ensure (nvti, nvti_get_tag_handles_null_nvti)
100 {
101 assert_that (nvti_get_tag (NULL, "example"), is_null);
102 }
103
Ensure(nvti,nvti_get_tag_handles_null_name)104 Ensure (nvti, nvti_get_tag_handles_null_name)
105 {
106 nvti_t *nvti;
107
108 nvti = nvti_new ();
109 nvti_set_tag (nvti, "example=1");
110
111 assert_that (nvti_get_tag (nvti, NULL), is_null);
112
113 nvti_free (nvti);
114 }
115
116 /* nvtis_add */
117
Ensure(nvti,nvtis_add_does_not_use_oid_as_key)118 Ensure (nvti, nvtis_add_does_not_use_oid_as_key)
119 {
120 nvtis_t *nvtis;
121 nvti_t *nvti;
122 gchar *oid;
123
124 nvtis = nvtis_new ();
125
126 nvti = nvti_new ();
127 nvti_set_oid (nvti, "1");
128
129 oid = nvti_oid (nvti);
130
131 /* This should not use the pointer nvti->oid as the key, because nvti_set_oid
132 * could free nvti->oid. */
133 nvtis_add (nvtis, nvti);
134
135 /* Change the first character of the OID. */
136 *oid = '2';
137
138 /* To check that the key is not the same pointer as nvti->oid, check
139 * that changing the first character of nvti->oid did not affect the key. */
140 assert_that (nvtis_lookup (nvtis, "1"), is_not_null);
141 assert_that (nvtis_lookup (nvtis, "2"), is_null);
142 }
143
144 /* Test suite. */
145
146 int
main(int argc,char ** argv)147 main (int argc, char **argv)
148 {
149 TestSuite *suite;
150
151 suite = create_test_suite ();
152
153 add_test_with_context (suite, nvti, nvti_new_never_returns_null);
154
155 add_test_with_context (suite, nvti, nvti_get_tag_gets_correct_value_one_tag);
156 add_test_with_context (suite, nvti,
157 nvti_get_tag_gets_correct_value_many_tags);
158 add_test_with_context (suite, nvti, nvti_get_tag_handles_empty_tag);
159 add_test_with_context (suite, nvti, nvti_get_tag_handles_null_nvti);
160 add_test_with_context (suite, nvti, nvti_get_tag_handles_null_name);
161
162 add_test_with_context (suite, nvti, nvti_set_solution_method_correct);
163
164 add_test_with_context (suite, nvti, nvtis_add_does_not_use_oid_as_key);
165
166 if (argc > 1)
167 return run_single_test (suite, argv[1], create_text_reporter ());
168
169 return run_test_suite (suite, create_text_reporter ());
170 }
171