Greenbone Vulnerability Management Libraries 22.32.0
cpeutils_tests.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019-2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6#include "cpeutils.c"
7
8#include <cgreen/cgreen.h>
9#include <cgreen/mocks.h>
10
11Describe (cpeutils);
12BeforeEach (cpeutils)
13{
14}
15
16AfterEach (cpeutils)
17{
18}
19
20/* parse_entity */
21
23{
24 cpe_struct_t cpe;
25 char *uri_cpe;
26
27 uri_cpe = "cpe:/a:microsoft:internet_explorer:8.0.6001:beta";
28 cpe_struct_init (&cpe);
29 uri_cpe_to_cpe_struct (uri_cpe, &cpe);
30 assert_that (cpe.part, is_equal_to_string ("a"));
31 assert_that (cpe.vendor, is_equal_to_string ("microsoft"));
32 assert_that (cpe.product, is_equal_to_string ("internet_explorer"));
33 assert_that (cpe.version, is_equal_to_string ("8\\.0\\.6001"));
34 assert_that (cpe.update, is_equal_to_string ("beta"));
35 assert_that (cpe.edition, is_equal_to_string ("ANY"));
36 assert_that (cpe.language, is_equal_to_string ("ANY"));
37 cpe_struct_free (&cpe);
38
39 uri_cpe = "cpe:/a:microsoft:internet_explorer:8.%2a:sp%3f";
40 cpe_struct_init (&cpe);
41 uri_cpe_to_cpe_struct (uri_cpe, &cpe);
42 assert_that (cpe.part, is_equal_to_string ("a"));
43 assert_that (cpe.vendor, is_equal_to_string ("microsoft"));
44 assert_that (cpe.product, is_equal_to_string ("internet_explorer"));
45 assert_that (cpe.version, is_equal_to_string ("8\\.\\*"));
46 assert_that (cpe.update, is_equal_to_string ("sp\\?"));
47 assert_that (cpe.edition, is_equal_to_string ("ANY"));
48 assert_that (cpe.language, is_equal_to_string ("ANY"));
49 cpe_struct_free (&cpe);
50
51 uri_cpe = "cpe:/a:hp:insight_diagnostics:7.4.0.1570::~~online~win2003~x64~";
52 cpe_struct_init (&cpe);
53 uri_cpe_to_cpe_struct (uri_cpe, &cpe);
54 assert_that (cpe.part, is_equal_to_string ("a"));
55 assert_that (cpe.vendor, is_equal_to_string ("hp"));
56 assert_that (cpe.product, is_equal_to_string ("insight_diagnostics"));
57 assert_that (cpe.version, is_equal_to_string ("7\\.4\\.0\\.1570"));
58 assert_that (cpe.update, is_equal_to_string ("ANY"));
59 assert_that (cpe.edition, is_equal_to_string ("ANY"));
60 assert_that (cpe.sw_edition, is_equal_to_string ("online"));
61 assert_that (cpe.target_sw, is_equal_to_string ("win2003"));
62 assert_that (cpe.target_hw, is_equal_to_string ("x64"));
63 assert_that (cpe.other, is_equal_to_string ("ANY"));
64 assert_that (cpe.language, is_equal_to_string ("ANY"));
65 cpe_struct_free (&cpe);
66
67 uri_cpe =
68 "cpe:/a:hp:insight_diagnostics:7.4.0.1570::~~online~win2003~x64~other";
69 cpe_struct_init (&cpe);
70 uri_cpe_to_cpe_struct (uri_cpe, &cpe);
71 assert_that (cpe.part, is_equal_to_string ("a"));
72 assert_that (cpe.vendor, is_equal_to_string ("hp"));
73 assert_that (cpe.product, is_equal_to_string ("insight_diagnostics"));
74 assert_that (cpe.version, is_equal_to_string ("7\\.4\\.0\\.1570"));
75 assert_that (cpe.update, is_equal_to_string ("ANY"));
76 assert_that (cpe.edition, is_equal_to_string ("ANY"));
77 assert_that (cpe.sw_edition, is_equal_to_string ("online"));
78 assert_that (cpe.target_sw, is_equal_to_string ("win2003"));
79 assert_that (cpe.target_hw, is_equal_to_string ("x64"));
80 assert_that (cpe.other, is_equal_to_string ("other"));
81 assert_that (cpe.language, is_equal_to_string ("ANY"));
82 cpe_struct_free (&cpe);
83
84 uri_cpe =
85 "cpe:/"
86 "a:hp:insight_diagnostics:7.4.0.1570::~~online~win2003~x64~other:english";
87 cpe_struct_init (&cpe);
88 uri_cpe_to_cpe_struct (uri_cpe, &cpe);
89 assert_that (cpe.part, is_equal_to_string ("a"));
90 assert_that (cpe.vendor, is_equal_to_string ("hp"));
91 assert_that (cpe.product, is_equal_to_string ("insight_diagnostics"));
92 assert_that (cpe.version, is_equal_to_string ("7\\.4\\.0\\.1570"));
93 assert_that (cpe.update, is_equal_to_string ("ANY"));
94 assert_that (cpe.edition, is_equal_to_string ("ANY"));
95 assert_that (cpe.sw_edition, is_equal_to_string ("online"));
96 assert_that (cpe.target_sw, is_equal_to_string ("win2003"));
97 assert_that (cpe.target_hw, is_equal_to_string ("x64"));
98 assert_that (cpe.other, is_equal_to_string ("other"));
99 assert_that (cpe.language, is_equal_to_string ("english"));
100 cpe_struct_free (&cpe);
101
102 uri_cpe = "This is a ~:SIGNAL:~ test.";
103 cpe_struct_init (&cpe);
104 uri_cpe_to_cpe_struct (uri_cpe, &cpe);
105 cpe_struct_free (&cpe);
106}
107
109{
110 cpe_struct_t cpe;
111 char *fs_cpe;
112
113 fs_cpe = "cpe:2.3:a:microsoft:internet_explorer:8.0.6001:beta:*:*:*:*:*:*";
114 cpe_struct_init (&cpe);
115 fs_cpe_to_cpe_struct (fs_cpe, &cpe);
116 assert_that (cpe.part, is_equal_to_string ("a"));
117 assert_that (cpe.vendor, is_equal_to_string ("microsoft"));
118 assert_that (cpe.product, is_equal_to_string ("internet_explorer"));
119 assert_that (cpe.version, is_equal_to_string ("8\\.0\\.6001"));
120 assert_that (cpe.update, is_equal_to_string ("beta"));
121 assert_that (cpe.edition, is_equal_to_string ("ANY"));
122 assert_that (cpe.language, is_equal_to_string ("ANY"));
123 assert_that (cpe.sw_edition, is_equal_to_string ("ANY"));
124 assert_that (cpe.target_sw, is_equal_to_string ("ANY"));
125 assert_that (cpe.target_hw, is_equal_to_string ("ANY"));
126 assert_that (cpe.other, is_equal_to_string ("ANY"));
127 cpe_struct_free (&cpe);
128
129 fs_cpe = "This is a ~:SIGNAL:~ test.";
130 cpe_struct_init (&cpe);
131 fs_cpe_to_cpe_struct (fs_cpe, &cpe);
132 cpe_struct_free (&cpe);
133}
134
136{
137 cpe_struct_t cpe;
138 char *uri_cpe;
139
140 cpe_struct_init (&cpe);
141 cpe.part = "a";
142 cpe.vendor = "microsoft";
143 cpe.product = "internet_explorer";
144 cpe.version = "8\\.0\\.6001";
145 cpe.update = "beta";
146 cpe.edition = "ANY";
147
148 uri_cpe = cpe_struct_to_uri_cpe (&cpe);
149 assert_that (uri_cpe, is_equal_to_string (
150 "cpe:/a:microsoft:internet_explorer:8.0.6001:beta"));
151 g_free (uri_cpe);
152}
153
155{
156 cpe_struct_t cpe;
157 char *fs_cpe;
158
159 cpe_struct_init (&cpe);
160 cpe.part = "a";
161 cpe.vendor = "microsoft";
162 cpe.product = "internet_explorer";
163 cpe.version = "8\\.0\\.6001";
164 cpe.update = "beta";
165 cpe.edition = "ANY";
166
167 fs_cpe = cpe_struct_to_fs_cpe (&cpe);
168 assert_that (
169 fs_cpe,
170 is_equal_to_string (
171 "cpe:2.3:a:microsoft:internet_explorer:8.0.6001:beta:*:*:*:*:*:*"));
172 g_free (fs_cpe);
173}
174
176{
177 char *uri_cpe = "cpe:/a:microsoft:internet_explorer:8.0.6001:beta";
178 char *fs_cpe = uri_cpe_to_fs_cpe (uri_cpe);
179 assert_that (
180 fs_cpe,
181 is_equal_to_string (
182 "cpe:2.3:a:microsoft:internet_explorer:8.0.6001:beta:*:*:*:*:*:*"));
183 g_free (fs_cpe);
184
185 uri_cpe = "cpe:/a:hp:insight_diagnostics:7.4.0.1570:-:~~online~win2003~x64~";
186 fs_cpe = uri_cpe_to_fs_cpe (uri_cpe);
187 assert_that (fs_cpe,
188 is_equal_to_string ("cpe:2.3:a:hp:insight_diagnostics:7.4.0."
189 "1570:-:*:*:online:win2003:x64:*"));
190 g_free (fs_cpe);
191
192 uri_cpe = "This is a ~:SIGNAL:~ test.";
193 fs_cpe = uri_cpe_to_fs_cpe (uri_cpe);
194 g_free (fs_cpe);
195}
196
198{
199 char *fs_cpe =
200 "cpe:2.3:a:microsoft:internet_explorer:8.0.6001:beta:*:*:*:*:*:*";
201 char *uri_cpe = fs_cpe_to_uri_cpe (fs_cpe);
202 assert_that (uri_cpe, is_equal_to_string (
203 "cpe:/a:microsoft:internet_explorer:8.0.6001:beta"));
204 g_free (uri_cpe);
205
206 fs_cpe =
207 "cpe:2.3:a:hp:insight_diagnostics:7.4.0.1570:-:*:*:online:win2003:x64:*";
208 uri_cpe = fs_cpe_to_uri_cpe (fs_cpe);
209 assert_that (
210 uri_cpe,
211 is_equal_to_string (
212 "cpe:/a:hp:insight_diagnostics:7.4.0.1570:-:~~online~win2003~x64~"));
213 g_free (uri_cpe);
214
215 fs_cpe =
216 "cpe:2.3:a:hp:insight_diagnostics:7\\:4.0.1570:-:*:*:online:win2003:x64:*";
217 uri_cpe = fs_cpe_to_uri_cpe (fs_cpe);
218 assert_that (
219 uri_cpe,
220 is_equal_to_string (
221 "cpe:/a:hp:insight_diagnostics:7%3A4.0.1570:-:~~online~win2003~x64~"));
222 g_free (uri_cpe);
223
224 fs_cpe =
225 "cpe:2.3:a:hp:insight_diagnostics:7.4.0.1570:-:*:*:online:win\\:2003:x64:*";
226 uri_cpe = fs_cpe_to_uri_cpe (fs_cpe);
227 assert_that (
228 uri_cpe,
229 is_equal_to_string (
230 "cpe:/a:hp:insight_diagnostics:7.4.0.1570:-:~~online~win%3A2003~x64~"));
231 g_free (uri_cpe);
232
233 fs_cpe = "cpe:2.3:a:hp:insight_diagnostics:7.4.0.1570:-:*:*:online:win\\:\\:"
234 "2003:x64:*";
235 uri_cpe = fs_cpe_to_uri_cpe (fs_cpe);
236 assert_that (
237 uri_cpe,
238 is_equal_to_string (
239 "cpe:/"
240 "a:hp:insight_diagnostics:7.4.0.1570:-:~~online~win%3A%3A2003~x64~"));
241 g_free (uri_cpe);
242
243 fs_cpe = "cpe:2.3:a:hp:insight_diagnostics:7.4.0.1570:-:*:*:online:"
244 "win2003\\\\:x64:*";
245 uri_cpe = fs_cpe_to_uri_cpe (fs_cpe);
246 assert_that (
247 uri_cpe,
248 is_equal_to_string (
249 "cpe:/a:hp:insight_diagnostics:7.4.0.1570:-:~~online~win2003%5C~x64~"));
250 g_free (uri_cpe);
251
252 fs_cpe = "This is a ~:SIGNAL:~ test.";
253 uri_cpe = fs_cpe_to_uri_cpe (fs_cpe);
254 g_free (uri_cpe);
255}
256
258{
259 cpe_struct_t cpe1, cpe2;
260 char *fs_cpe1, *fs_cpe2;
261
262 fs_cpe1 = "cpe:2.3:a:microsoft:internet_explorer:8.0.6001:beta:*:*:*:*:*:*";
263 cpe_struct_init (&cpe1);
264 fs_cpe_to_cpe_struct (fs_cpe1, &cpe1);
265 assert_that (cpe_struct_match (&cpe1, &cpe1), is_equal_to (TRUE));
266
267 fs_cpe2 = "cpe:2.3:a:microsoft:internet_explorer:*:beta:*:*:*:*:*:*";
268 cpe_struct_init (&cpe2);
269 fs_cpe_to_cpe_struct (fs_cpe2, &cpe2);
270 assert_that (cpe_struct_match (&cpe2, &cpe1), is_equal_to (TRUE));
271
272 assert_that (cpe_struct_match (&cpe1, &cpe2), is_equal_to (FALSE));
273
274 fs_cpe2 = "cpe:2.3:a:microsoft:internet_explorer:*:-:*:*:*:*:*:*";
275 cpe_struct_free (&cpe2);
276 cpe_struct_init (&cpe2);
277 fs_cpe_to_cpe_struct (fs_cpe2, &cpe2);
278 assert_that (cpe_struct_match (&cpe2, &cpe1), is_equal_to (FALSE));
279
280 cpe_struct_free (&cpe1);
281 cpe_struct_free (&cpe2);
282}
283
285{
286 const char *uri_cpe;
287 gchar *uri_product;
288
289 uri_cpe = "cpe:/a:hp:insight_diagnostics:7.4.0.1570:-:~~online~win2003~x64~";
290
291 uri_product = uri_cpe_to_uri_product (uri_cpe);
292 assert_string_equal (uri_product, "cpe:/a:hp:insight_diagnostics");
293 g_free (uri_product);
294}
295
296/* Test suite. */
297int
298main (int argc, char **argv)
299{
300 int ret;
301 TestSuite *suite;
302
303 suite = create_test_suite ();
304
305 add_test_with_context (suite, cpeutils, uri_cpe_to_cpe_struct);
306 add_test_with_context (suite, cpeutils, fs_cpe_to_cpe_struct);
307 add_test_with_context (suite, cpeutils, cpe_struct_to_uri_cpe);
308 add_test_with_context (suite, cpeutils, cpe_struct_to_fs_cpe);
309 add_test_with_context (suite, cpeutils, uri_cpe_to_fs_cpe);
310 add_test_with_context (suite, cpeutils, fs_cpe_to_uri_cpe);
311 add_test_with_context (suite, cpeutils, cpe_struct_match);
312 add_test_with_context (suite, cpeutils, uri_cpe_to_uri_product);
313
314 if (argc > 1)
315 ret = run_single_test (suite, argv[1], create_text_reporter ());
316 else
317 ret = run_test_suite (suite, create_text_reporter ());
318
319 destroy_test_suite (suite);
320
321 return ret;
322}
Functions to convert different CPE notations into each other.
char * fs_cpe_to_uri_cpe(const char *fs_cpe)
Convert a formatted string CPE to a URI CPE.
Definition cpeutils.c:160
gboolean cpe_struct_match(cpe_struct_t *source, cpe_struct_t *target)
Returns if source is a match for target. That means that source is a superset of target.
Definition cpeutils.c:1295
char * uri_cpe_to_fs_cpe(const char *uri_cpe)
Convert a URI CPE to a formatted string CPE.
Definition cpeutils.c:100
char * cpe_struct_to_fs_cpe(const cpe_struct_t *cpe)
Convert a CPE struct into a formatted string CPE.
Definition cpeutils.c:402
void fs_cpe_to_cpe_struct(const char *fs_cpe, cpe_struct_t *cpe)
Read a formatted string CPE into the CPE struct.
Definition cpeutils.c:366
char * cpe_struct_to_uri_cpe(const cpe_struct_t *cpe)
Convert a CPE struct into a URI CPE.
Definition cpeutils.c:240
void cpe_struct_init(cpe_struct_t *cpe)
Initialize a CPE struct.
Definition cpeutils.c:1190
void cpe_struct_free(cpe_struct_t *cpe)
Free a CPE struct.
Definition cpeutils.c:1214
char * uri_cpe_to_uri_product(const char *uri_cpe)
Convert a URI CPE to a formatted string product.
Definition cpeutils.c:140
void uri_cpe_to_cpe_struct(const char *uri_cpe, cpe_struct_t *cpe)
Read a URI CPE into the CPE struct.
Definition cpeutils.c:200
AfterEach(cpeutils)
BeforeEach(cpeutils)
int main(int argc, char **argv)
Describe(cpeutils)
Ensure(cpeutils, uri_cpe_to_cpe_struct)
XML context.
Definition cpeutils.h:23
char * sw_edition
Definition cpeutils.h:30
char * target_sw
Definition cpeutils.h:31
char * vendor
Definition cpeutils.h:25
char * version
Definition cpeutils.h:27
char * update
Definition cpeutils.h:28
char * product
Definition cpeutils.h:26
char * part
Definition cpeutils.h:24
char * edition
Definition cpeutils.h:29
char * language
Definition cpeutils.h:34
char * target_hw
Definition cpeutils.h:32
char * other
Definition cpeutils.h:33