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 
21 Ensure (nvti, nvti_new_never_returns_null)
22 {
23  assert_that (nvti_new (), is_not_null);
24 }
25 
26 /* nvti solution_method */
27 
28 Ensure (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");
36 
37  assert_that (solution_method, is_equal_to_string ("DebianAPTUpgrade"));
38 
39  nvti_free (nvti);
40 }
41 
42 /* nvti_get_tag */
43 
44 Ensure (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);
56  nvti_free (nvti);
57 }
58 
59 Ensure (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);
71  nvti_free (nvti);
72 }
73 
74 Ensure (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 
82  nvti_free (nvti);
83 }
84 
85 Ensure (nvti, nvti_get_tag_handles_null_nvti)
86 {
87  assert_that (nvti_get_tag (NULL, "example"), is_null);
88 }
89 
90 Ensure (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 
99  nvti_free (nvti);
100 }
101 
102 /* nvtis_add */
103 
104 Ensure (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 
132 Ensure (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 
146 Ensure (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 
159 Ensure (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 
174 int
175 main (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 }
nvti_severity_vector_from_tag
gchar * nvti_severity_vector_from_tag(const nvti_t *n)
Get the severity score.
Definition: nvti.c:938
nvtis_t
GHashTable nvtis_t
A collection of information records corresponding to NVTs.
Definition: nvti.h:252
Describe
Describe(nvti)
nvti_get_tag
gchar * nvti_get_tag(const nvti_t *n, const gchar *name)
Get a tag value by a tag name.
Definition: nvti.c:1020
nvti_oid
gchar * nvti_oid(const nvti_t *n)
Get the OID string.
Definition: nvti.c:649
nvti::solution_method
gchar * solution_method
The solution method.
Definition: nvti.c:408
nvtis_add
void nvtis_add(nvtis_t *nvtis, nvti_t *nvti)
Add an NVT Info to a collection of NVT Infos.
Definition: nvti.c:2271
nvti_set_solution_method
int nvti_set_solution_method(nvti_t *n, const gchar *solution_method)
Set the solution method of a NVT.
Definition: nvti.c:1572
nvti_solution_method
gchar * nvti_solution_method(const nvti_t *n)
Get the solution method.
Definition: nvti.c:990
AfterEach
AfterEach(nvti)
Definition: nvti_tests.c:15
nvti
The structure of a information record that corresponds to a NVT.
Definition: nvti.c:394
nvti::oid
gchar * oid
Object ID.
Definition: nvti.c:395
nvti_free
void nvti_free(nvti_t *n)
Free memory of a nvti structure.
Definition: nvti.c:608
nvtis_new
nvtis_t * nvtis_new(void)
Make a collection of NVT Infos.
Definition: nvti.c:2246
nvtis_lookup
nvti_t * nvtis_lookup(nvtis_t *nvtis, const char *oid)
Add an NVT Info to a collection of NVT Infos.
Definition: nvti.c:2288
nvti_set_tag
int nvti_set_tag(nvti_t *n, const gchar *tag)
Set the tags of a NVT.
Definition: nvti.c:1663
nvti::tag
gchar * tag
List of tags attached to this NVT.
Definition: nvti.c:410
main
int main(int argc, char **argv)
Definition: nvti_tests.c:175
nvti_new
nvti_t * nvti_new(void)
Create a new (empty) nvti structure.
Definition: nvti.c:597
BeforeEach
BeforeEach(nvti)
Definition: nvti_tests.c:12
Ensure
Ensure(nvti, nvti_new_never_returns_null)
Definition: nvti_tests.c:21
nvti.c
Implementation of API to handle NVT Info datasets.
nvti_set_oid
int nvti_set_oid(nvti_t *n, const gchar *oid)
Set the OID of a NVT Info.
Definition: nvti.c:1252