Greenbone Vulnerability Management Libraries 22.8.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 assert_that (nvti_new (), is_not_null);
24}
25
26/* nvti solution_method */
27
28Ensure (nvti, nvti_set_solution_method_correct)
29{
30 nvti_t *nvti;
31 gchar *solution_method;
32
33 nvti = nvti_new ();
34 nvti_set_solution_method (nvti, "DebianAPTUpgrade");
35 solution_method = nvti_solution_method (nvti);
36
37 assert_that (solution_method, is_equal_to_string ("DebianAPTUpgrade"));
38
40}
41
42/* nvti_get_tag */
43
44Ensure (nvti, nvti_get_tag_gets_correct_value_one_tag)
45{
46 nvti_t *nvti;
47 gchar *tag;
48
49 nvti = nvti_new ();
50 nvti_set_tag (nvti, "a=1");
51 tag = nvti_get_tag (nvti, "a");
52
53 assert_that (tag, is_equal_to_string ("1"));
54
55 g_free (tag);
57}
58
59Ensure (nvti, nvti_get_tag_gets_correct_value_many_tags)
60{
61 nvti_t *nvti;
62 gchar *tag;
63
64 nvti = nvti_new ();
65 nvti_set_tag (nvti, "a=1|b=2|c=3");
66 tag = nvti_get_tag (nvti, "b");
67
68 assert_that (tag, is_equal_to_string ("2"));
69
70 g_free (tag);
72}
73
74Ensure (nvti, nvti_get_tag_handles_empty_tag)
75{
76 nvti_t *nvti;
77
78 nvti = nvti_new ();
79
80 assert_that (nvti_get_tag (nvti, "b"), is_null);
81
83}
84
85Ensure (nvti, nvti_get_tag_handles_null_nvti)
86{
87 assert_that (nvti_get_tag (NULL, "example"), is_null);
88}
89
90Ensure (nvti, nvti_get_tag_handles_null_name)
91{
92 nvti_t *nvti;
93
94 nvti = nvti_new ();
95 nvti_set_tag (nvti, "example=1");
96
97 assert_that (nvti_get_tag (nvti, NULL), is_null);
98
100}
101
102/* nvtis_add */
103
104Ensure (nvti, nvtis_add_does_not_use_oid_as_key)
105{
106 nvtis_t *nvtis;
107 nvti_t *nvti;
108 gchar *oid;
109
110 nvtis = nvtis_new ();
111
112 nvti = nvti_new ();
113 nvti_set_oid (nvti, "1");
114
115 oid = nvti_oid (nvti);
116
117 /* This should not use the pointer nvti->oid as the key, because nvti_set_oid
118 * could free nvti->oid. */
119 nvtis_add (nvtis, nvti);
120
121 /* Change the first character of the OID. */
122 *oid = '2';
123
124 /* To check that the key is not the same pointer as nvti->oid, check
125 * that changing the first character of nvti->oid did not affect the key. */
126 assert_that (nvtis_lookup (nvtis, "1"), is_not_null);
127 assert_that (nvtis_lookup (nvtis, "2"), is_null);
128}
129
130/* nvti severity vector */
131
132Ensure (nvti, nvti_get_severity_vector_both)
133{
134 nvti_t *nvti;
135
136 nvti = nvti_new ();
137 nvti_set_tag (nvti, "cvss_base_vector=DEF");
138 nvti_set_tag (nvti, "severity_vector=ABC");
139
140 assert_that (nvti_severity_vector_from_tag (nvti),
141 is_equal_to_string ("ABC"));
142
143 nvti_free (nvti);
144}
145
146Ensure (nvti, nvti_get_severity_vector_no_cvss_base)
147{
148 nvti_t *nvti;
149
150 nvti = nvti_new ();
151 nvti_set_tag (nvti, "severity_vector=ABC");
152
153 assert_that (nvti_severity_vector_from_tag (nvti),
154 is_equal_to_string ("ABC"));
155
156 nvti_free (nvti);
157}
158
159Ensure (nvti, nvti_get_severity_vector_no_severity_vector)
160{
161 nvti_t *nvti;
162
163 nvti = nvti_new ();
164 nvti_set_tag (nvti, "cvss_base_vector=DEF");
165
166 assert_that (nvti_severity_vector_from_tag (nvti),
167 is_equal_to_string ("DEF"));
168
169 nvti_free (nvti);
170}
171
172/* Test suite. */
173
174int
175main (int argc, char **argv)
176{
177 TestSuite *suite;
178
179 suite = create_test_suite ();
180
181 add_test_with_context (suite, nvti, nvti_new_never_returns_null);
182
183 add_test_with_context (suite, nvti, nvti_get_tag_gets_correct_value_one_tag);
184 add_test_with_context (suite, nvti,
185 nvti_get_tag_gets_correct_value_many_tags);
186 add_test_with_context (suite, nvti, nvti_get_tag_handles_empty_tag);
187 add_test_with_context (suite, nvti, nvti_get_tag_handles_null_nvti);
188 add_test_with_context (suite, nvti, nvti_get_tag_handles_null_name);
189
190 add_test_with_context (suite, nvti, nvti_set_solution_method_correct);
191
192 add_test_with_context (suite, nvti, nvtis_add_does_not_use_oid_as_key);
193
194 add_test_with_context (suite, nvti, nvti_get_severity_vector_both);
195 add_test_with_context (suite, nvti,
196 nvti_get_severity_vector_no_severity_vector);
197 add_test_with_context (suite, nvti, nvti_get_severity_vector_no_cvss_base);
198
199 if (argc > 1)
200 return run_single_test (suite, argv[1], create_text_reporter ());
201
202 return run_test_suite (suite, create_text_reporter ());
203}
Implementation of API to handle NVT Info datasets.
nvti_t * nvti_new(void)
Create a new (empty) nvti structure.
Definition: nvti.c:597
gchar * nvti_get_tag(const nvti_t *n, const gchar *name)
Get a tag value by a tag name.
Definition: nvti.c:1020
nvtis_t * nvtis_new(void)
Make a collection of NVT Infos.
Definition: nvti.c:2246
int nvti_set_oid(nvti_t *n, const gchar *oid)
Set the OID of a NVT Info.
Definition: nvti.c:1252
int nvti_set_tag(nvti_t *n, const gchar *tag)
Set the tags of a NVT.
Definition: nvti.c:1663
gchar * nvti_severity_vector_from_tag(const nvti_t *n)
Get the severity score.
Definition: nvti.c:938
gchar * nvti_solution_method(const nvti_t *n)
Get the solution method.
Definition: nvti.c:990
gchar * nvti_oid(const nvti_t *n)
Get the OID string.
Definition: nvti.c:649
int nvti_set_solution_method(nvti_t *n, const gchar *solution_method)
Set the solution method of a NVT.
Definition: nvti.c:1572
nvti_t * nvtis_lookup(nvtis_t *nvtis, const char *oid)
Add an NVT Info to a collection of NVT Infos.
Definition: nvti.c:2288
void nvtis_add(nvtis_t *nvtis, nvti_t *nvti)
Add an NVT Info to a collection of NVT Infos.
Definition: nvti.c:2271
void nvti_free(nvti_t *n)
Free memory of a nvti structure.
Definition: nvti.c:608
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:175
Describe(nvti)
AfterEach(nvti)
Definition: nvti_tests.c:15
The structure of a information record that corresponds to a NVT.
Definition: nvti.c:394