Greenbone Vulnerability Management Libraries 22.32.0
nvti_tests.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009-2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6#include "nvti.c"
7
8#include <cgreen/cgreen.h>
9#include <cgreen/mocks.h>
10
13{
14}
16{
17}
18
19/* make_nvti */
20
21Ensure (nvti, nvti_new_never_returns_null)
22{
23 nvti_t *nvti;
24
25 nvti = nvti_new ();
26 assert_that (nvti, is_not_null);
28}
29
30/* nvti solution_method */
31
32Ensure (nvti, nvti_parse_timestamp)
33{
34 setenv ("TZ", "utc 0", 1);
35 tzset ();
36
37 assert_that (
38 parse_nvt_timestamp ("2018-09-07 11:08:31 +0200 (Fri, 07 Sep 2018)"),
39 is_equal_to (1536311311));
40 assert_that_double (
41 parse_nvt_timestamp ("2022-05-31 20:54:22 +0100 (Tue, 31 May 2022)"),
42 is_equal_to_double (1654026862));
43 assert_that_double (parse_nvt_timestamp ("2012-09-23 02:15:34 +0400"),
44 is_equal_to_double (1348352134));
45 assert_that_double (parse_nvt_timestamp ("Fri Feb 10 16:09:30 2023 +0100"),
46 is_equal_to_double (1676041770));
47}
48
49/* nvti solution_method */
50
51Ensure (nvti, nvti_set_solution_method_correct)
52{
53 nvti_t *nvti;
54 gchar *solution_method;
55
56 nvti = nvti_new ();
57 nvti_set_solution_method (nvti, "DebianAPTUpgrade");
58 solution_method = nvti_solution_method (nvti);
59
60 assert_that (solution_method, is_equal_to_string ("DebianAPTUpgrade"));
61
63}
64
65/* nvti_get_tag */
66
67Ensure (nvti, nvti_get_tag_gets_correct_value_one_tag)
68{
69 nvti_t *nvti;
70 gchar *tag;
71
72 nvti = nvti_new ();
73 nvti_set_tag (nvti, "a=1");
74 tag = nvti_get_tag (nvti, "a");
75
76 assert_that (tag, is_equal_to_string ("1"));
77
78 g_free (tag);
80}
81
82Ensure (nvti, nvti_get_tag_gets_correct_value_many_tags)
83{
84 nvti_t *nvti;
85 gchar *tag;
86
87 nvti = nvti_new ();
88 nvti_set_tag (nvti, "a=1|b=2|c=3");
89 tag = nvti_get_tag (nvti, "b");
90
91 assert_that (tag, is_equal_to_string ("2"));
92
93 g_free (tag);
95}
96
97Ensure (nvti, nvti_get_tag_handles_empty_tag)
98{
99 nvti_t *nvti;
100
101 nvti = nvti_new ();
102
103 assert_that (nvti_get_tag (nvti, "b"), is_null);
104
105 nvti_free (nvti);
106}
107
108Ensure (nvti, nvti_get_tag_handles_null_nvti)
109{
110 assert_that (nvti_get_tag (NULL, "example"), is_null);
111}
112
113Ensure (nvti, nvti_get_tag_handles_null_name)
114{
115 nvti_t *nvti;
116
117 nvti = nvti_new ();
118 nvti_set_tag (nvti, "example=1");
119
120 assert_that (nvti_get_tag (nvti, NULL), is_null);
121
122 nvti_free (nvti);
123}
124
125/* nvtis_add */
126
127Ensure (nvti, nvtis_add_does_not_use_oid_as_key)
128{
129 nvtis_t *nvtis;
130 nvti_t *nvti;
131 gchar *oid;
132
133 nvtis = nvtis_new ();
134
135 nvti = nvti_new ();
136 nvti_set_oid (nvti, "1");
137
138 oid = nvti_oid (nvti);
139
140 /* This should not use the pointer nvti->oid as the key, because nvti_set_oid
141 * could free nvti->oid. */
142 nvtis_add (nvtis, nvti);
143
144 /* Change the first character of the OID. */
145 *oid = '2';
146
147 /* To check that the key is not the same pointer as nvti->oid, check
148 * that changing the first character of nvti->oid did not affect the key. */
149 assert_that (nvtis_lookup (nvtis, "1"), is_not_null);
150 assert_that (nvtis_lookup (nvtis, "2"), is_null);
151
152 nvtis_free (nvtis);
153}
154
155/* nvti severity vector */
156
157Ensure (nvti, nvti_get_severity_vector_both)
158{
159 nvti_t *nvti;
160 gchar *vector;
161
162 nvti = nvti_new ();
163 nvti_set_tag (nvti, "cvss_base_vector=DEF");
164 nvti_set_tag (nvti, "severity_vector=ABC");
165
167
168 assert_that (vector, is_equal_to_string ("ABC"));
169
170 g_free (vector);
171 nvti_free (nvti);
172}
173
174Ensure (nvti, nvti_get_severity_vector_no_cvss_base)
175{
176 nvti_t *nvti;
177 gchar *vector;
178
179 nvti = nvti_new ();
180 nvti_set_tag (nvti, "severity_vector=ABC");
181
183
184 assert_that (vector, is_equal_to_string ("ABC"));
185
186 g_free (vector);
187 nvti_free (nvti);
188}
189
190Ensure (nvti, nvti_get_severity_vector_no_severity_vector)
191{
192 nvti_t *nvti;
193 gchar *vector;
194
195 nvti = nvti_new ();
196 nvti_set_tag (nvti, "cvss_base_vector=DEF");
197
199 assert_that (vector, is_equal_to_string ("DEF"));
200
201 g_free (vector);
202 nvti_free (nvti);
203}
204
205/* Test suite. */
206
207int
208main (int argc, char **argv)
209{
210 int ret;
211 TestSuite *suite;
212
213 suite = create_test_suite ();
214
215 add_test_with_context (suite, nvti, nvti_new_never_returns_null);
216
217 add_test_with_context (suite, nvti, nvti_get_tag_gets_correct_value_one_tag);
218 add_test_with_context (suite, nvti,
219 nvti_get_tag_gets_correct_value_many_tags);
220 add_test_with_context (suite, nvti, nvti_get_tag_handles_empty_tag);
221 add_test_with_context (suite, nvti, nvti_get_tag_handles_null_nvti);
222 add_test_with_context (suite, nvti, nvti_get_tag_handles_null_name);
223
224 add_test_with_context (suite, nvti, nvti_set_solution_method_correct);
225
226 add_test_with_context (suite, nvti, nvtis_add_does_not_use_oid_as_key);
227
228 add_test_with_context (suite, nvti, nvti_get_severity_vector_both);
229 add_test_with_context (suite, nvti,
230 nvti_get_severity_vector_no_severity_vector);
231 add_test_with_context (suite, nvti, nvti_get_severity_vector_no_cvss_base);
232 add_test_with_context (suite, nvti, nvti_parse_timestamp);
233
234 if (argc > 1)
235 ret = run_single_test (suite, argv[1], create_text_reporter ());
236 else
237 ret = run_test_suite (suite, create_text_reporter ());
238
239 destroy_test_suite (suite);
240
241 return ret;
242}
Implementation of API to handle NVT Info datasets.
nvti_t * nvti_new(void)
Create a new (empty) nvti structure.
Definition nvti.c:559
gchar * nvti_get_tag(const nvti_t *n, const gchar *name)
Get a tag value by a tag name.
Definition nvti.c:982
struct nvti nvti_t
The structure of a information record that corresponds to a NVT.
nvtis_t * nvtis_new(void)
Make a collection of NVT Infos.
Definition nvti.c:2208
int nvti_set_oid(nvti_t *n, const gchar *oid)
Set the OID of a NVT Info.
Definition nvti.c:1214
int nvti_set_tag(nvti_t *n, const gchar *tag)
Set the tags of a NVT.
Definition nvti.c:1625
gchar * nvti_severity_vector_from_tag(const nvti_t *n)
Get the severity score.
Definition nvti.c:900
gchar * nvti_solution_method(const nvti_t *n)
Get the solution method.
Definition nvti.c:952
gchar * nvti_oid(const nvti_t *n)
Get the OID string.
Definition nvti.c:611
int nvti_set_solution_method(nvti_t *n, const gchar *solution_method)
Set the solution method of a NVT.
Definition nvti.c:1534
nvti_t * nvtis_lookup(nvtis_t *nvtis, const char *oid)
Add an NVT Info to a collection of NVT Infos.
Definition nvti.c:2250
static time_t parse_nvt_timestamp(const gchar *str_time)
Try convert an NVT tag time string into epoch time or return 0 upon parse errors.
Definition nvti.c:295
void nvtis_add(nvtis_t *nvtis, nvti_t *nvti)
Add an NVT Info to a collection of NVT Infos.
Definition nvti.c:2233
void nvtis_free(nvtis_t *nvtis)
Free a collection of NVT Infos.
Definition nvti.c:2220
void nvti_free(nvti_t *n)
Free memory of a nvti structure.
Definition nvti.c:570
GHashTable nvtis_t
A collection of information records corresponding to NVTs.
Definition nvti.h:252
Ensure(nvti, nvti_new_never_returns_null)
Definition nvti_tests.c:21
BeforeEach(nvti)
Definition nvti_tests.c:12
int main(int argc, char **argv)
Definition nvti_tests.c:208
Describe(nvti)
AfterEach(nvti)
Definition nvti_tests.c:15
The structure of a information record that corresponds to a NVT.
Definition nvti.c:358