Greenbone Vulnerability Management Libraries 22.32.0
xmlutils_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 "xmlutils.c"
7
8#include <cgreen/cgreen.h>
9#include <cgreen/mocks.h>
10#include <glib/gstdio.h>
11
12static gchar *
13write_temp_xml (const char *xml)
14{
15 gchar *path = NULL;
16 GError *err = NULL;
17 if (!g_file_open_tmp ("xmliterXXXXXX", &path, NULL))
18 return NULL;
19 if (!g_file_set_contents (path, xml, -1, &err))
20 {
21 if (path)
22 g_unlink (path);
23 g_clear_error (&err);
24 g_free (path);
25 return NULL;
26 }
27 return path;
28}
29
30Describe (xmlutils);
31BeforeEach (xmlutils)
32{
33}
34AfterEach (xmlutils)
35{
36}
37
38/* parse_entity */
39
40Ensure (xmlutils, parse_entity_parses_simple_xml)
41{
42 entity_t entity, b;
43 const gchar *xml;
44
45 xml = "<a><b>1</b></a>";
46
47 assert_that (parse_entity (xml, &entity), is_equal_to (0));
48
49 assert_that (entity_name (entity), is_equal_to_string ("a"));
50
51 b = entity_child (entity, "b");
52 assert_that (entity_name (b), is_equal_to_string ("b"));
53
54 assert_that (entity_text (b), is_equal_to_string ("1"));
55
56 free_entity (entity);
57}
58
59Ensure (xmlutils, parse_entity_parses_xml_with_attributes)
60{
61 entity_t entity, b;
62 const gchar *xml;
63
64 xml = "<a><b ba1='test'>1</b></a>";
65
66 assert_that (parse_entity (xml, &entity), is_equal_to (0));
67
68 b = entity_child (entity, "b");
69
70 assert_that (entity_attribute (b, "ba1"), is_equal_to_string ("test"));
71
72 free_entity (entity);
73}
74
75Ensure (xmlutils, parse_entity_handles_declaration)
76{
77 entity_t entity, b;
78 const gchar *xml;
79
80 xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a><b ba1='test'>1</b></a>";
81
82 assert_that (parse_entity (xml, &entity), is_equal_to (0));
83
84 assert_that (entity_name (entity), is_equal_to_string ("a"));
85
86 b = entity_child (entity, "b");
87 assert_that (entity_name (b), is_equal_to_string ("b"));
88
89 assert_that (entity_text (b), is_equal_to_string ("1"));
90
91 free_entity (entity);
92}
93
94Ensure (xmlutils, parse_entity_handles_namespace)
95{
96 entity_t entity, b;
97 const gchar *xml;
98
99 xml =
100 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a><n:b ba1='test'>1</n:b></a>";
101
102 assert_that (parse_entity (xml, &entity), is_equal_to (0));
103
104 assert_that (entity_name (entity), is_equal_to_string ("a"));
105
106 b = entity_child (entity, "n:b");
107 assert_that (entity_name (b), is_equal_to_string ("n:b"));
108
109 assert_that (entity_text (b), is_equal_to_string ("1"));
110
111 free_entity (entity);
112}
113
114Ensure (xmlutils, parse_entity_oval_timestamp)
115{
116 gchar *generator_name;
117 entity_t generator, timestamp, entity;
118 const gchar *xml;
119
120 xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
121 "<oval_definitions "
122 "xsi:schemaLocation=\"http://oval.mitre.org/XMLSchema/"
123 "oval-definitions-5 oval-definitions-schema.xsd "
124 "http://oval.mitre.org/XMLSchema/oval-definitions-5#linux "
125 "linux-definitions-schema.xsd "
126 "http://oval.mitre.org/XMLSchema/oval-definitions-5#windows "
127 "windows-definitions-schema.xsd "
128 "http://oval.mitre.org/XMLSchema/oval-definitions-5#independent "
129 "independent-definitions-schema.xsd "
130 "http://oval.mitre.org/XMLSchema/oval-common-5 oval-common-schema.xsd "
131 "http://oval.mitre.org/XMLSchema/oval-definitions-5#unix "
132 "unix-definitions-schema.xsd\" "
133 "xmlns=\"http://oval.mitre.org/XMLSchema/oval-definitions-5\" "
134 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
135 "xmlns:oval=\"http://oval.mitre.org/XMLSchema/oval-common-5\" "
136 "xmlns:oval-def=\"http://oval.mitre.org/XMLSchema/oval-definitions-5\">"
137 " <generator>"
138 " <oval:product_name>The OVAL Repository</oval:product_name>"
139 " <oval:schema_version>5.10</oval:schema_version>"
140 " <oval:timestamp>2015-08-20T10:09:07.183-04:00</oval:timestamp>"
141 " </generator>"
142 "</oval_definitions>";
143
144 assert_that (parse_entity (xml, &entity), is_equal_to (0));
145
146 assert_that (entity_name (entity), is_equal_to_string ("oval_definitions"));
147 generator_name = g_strdup ("generator");
148 generator = entity_child (entity, generator_name);
149 g_free (generator_name);
150 assert_that (generator, is_not_null);
151 timestamp = entity_child (generator, "oval:timestamp");
152 assert_that (timestamp, is_not_null);
153 assert_that (entity_text (timestamp),
154 is_equal_to_string ("2015-08-20T10:09:07.183-04:00"));
155
156 free_entity (entity);
157}
158
159/* next_entities. */
160
161Ensure (xmlutils, next_entities_handles_multiple_children)
162{
163 entity_t entity, child;
164 entities_t children;
165 const gchar *xml;
166
167 xml = "<top><a>1</a><b></b><c>3</c></top>";
168
169 assert_that (parse_entity (xml, &entity), is_equal_to (0));
170
171 assert_that (entity_name (entity), is_equal_to_string ("top"));
172
173 children = entity->entities;
174
175 child = first_entity (children);
176 assert_that (child, is_not_null);
177 assert_that (entity_name (child), is_equal_to_string ("a"));
178 assert_that (entity_text (child), is_equal_to_string ("1"));
179 children = next_entities (children);
180
181 child = first_entity (children);
182 assert_that (child, is_not_null);
183 assert_that (entity_name (child), is_equal_to_string ("b"));
184 assert_that (entity_text (child), is_equal_to_string (""));
185 children = next_entities (children);
186
187 child = first_entity (children);
188 assert_that (child, is_not_null);
189 assert_that (entity_name (child), is_equal_to_string ("c"));
190 assert_that (entity_text (child), is_equal_to_string ("3"));
191 children = next_entities (children);
192
193 free_entity (entity);
194}
195
196/* parse_element */
197
198Ensure (xmlutils, parse_element_parses_simple_xml)
199{
200 element_t element, b;
201 const gchar *xml;
202 gchar *text;
203
204 xml = "<a><b>1</b></a>";
205
206 assert_that (parse_element (xml, &element), is_equal_to (0));
207
208 assert_that (element_name (element), is_equal_to_string ("a"));
209
210 b = element_child (element, "b");
211 assert_that (element_name (b), is_equal_to_string ("b"));
212
213 text = element_text (b);
214 assert_that (text, is_equal_to_string ("1"));
215 g_free (text);
216
217 element_free (element);
218}
219
220Ensure (xmlutils, parse_element_parses_xml_with_attributes)
221{
222 element_t element, b;
223 const gchar *xml;
224 gchar *attr;
225
226 xml = "<a><b ba1='test'>1</b></a>";
227
228 assert_that (parse_element (xml, &element), is_equal_to (0));
229
230 b = element_child (element, "b");
231
232 attr = element_attribute (b, "ba1");
233 assert_that (attr, is_equal_to_string ("test"));
234 g_free (attr);
235
236 element_free (element);
237}
238
239Ensure (xmlutils, parse_element_handles_declaration)
240{
241 element_t element, b;
242 const gchar *xml;
243 gchar *text;
244
245 xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a><b ba1='test'>1</b></a>";
246
247 assert_that (parse_element (xml, &element), is_equal_to (0));
248
249 assert_that (element_name (element), is_equal_to_string ("a"));
250
251 b = element_child (element, "b");
252 assert_that (element_name (b), is_equal_to_string ("b"));
253
254 text = element_text (b);
255 assert_that (text, is_equal_to_string ("1"));
256 g_free (text);
257
258 element_free (element);
259}
260
261Ensure (xmlutils, parse_element_handles_namespace)
262{
263 element_t element, b;
264 const gchar *xml;
265 gchar *text, *attr;
266
267 xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a><n:b ba1='test' "
268 "n2:ba2='test2'>1</n:b></a>";
269
270 assert_that (parse_element (xml, &element), is_equal_to (0));
271
272 assert_that (element_name (element), is_equal_to_string ("a"));
273
274 b = element_child (element, "n:b");
275 assert_that (element_name (b), is_equal_to_string ("n:b"));
276
277 text = element_text (b);
278 assert_that (text, is_equal_to_string ("1"));
279 g_free (text);
280
281 attr = element_attribute (b, "n2:ba2");
282 assert_that (attr, is_equal_to_string ("test2"));
283 g_free (attr);
284
285 element_free (element);
286}
287
288Ensure (xmlutils, parse_element_oval_timestamp)
289{
290 gchar *generator_name, *text;
291 element_t generator, timestamp, element;
292 const gchar *xml;
293
294 xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
295 "<oval_definitions "
296 "xsi:schemaLocation=\"http://oval.mitre.org/XMLSchema/"
297 "oval-definitions-5 oval-definitions-schema.xsd "
298 "http://oval.mitre.org/XMLSchema/oval-definitions-5#linux "
299 "linux-definitions-schema.xsd "
300 "http://oval.mitre.org/XMLSchema/oval-definitions-5#windows "
301 "windows-definitions-schema.xsd "
302 "http://oval.mitre.org/XMLSchema/oval-definitions-5#independent "
303 "independent-definitions-schema.xsd "
304 "http://oval.mitre.org/XMLSchema/oval-common-5 oval-common-schema.xsd "
305 "http://oval.mitre.org/XMLSchema/oval-definitions-5#unix "
306 "unix-definitions-schema.xsd\" "
307 "xmlns=\"http://oval.mitre.org/XMLSchema/oval-definitions-5\" "
308 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
309 "xmlns:oval=\"http://oval.mitre.org/XMLSchema/oval-common-5\" "
310 "xmlns:oval-def=\"http://oval.mitre.org/XMLSchema/oval-definitions-5\">"
311 " <generator>"
312 " <oval:product_name>The OVAL Repository</oval:product_name>"
313 " <oval:schema_version>5.10</oval:schema_version>"
314 " <oval:timestamp>2015-08-20T10:09:07.183-04:00</oval:timestamp>"
315 " </generator>"
316 "</oval_definitions>";
317
318 assert_that (parse_element (xml, &element), is_equal_to (0));
319
320 assert_that (element_name (element), is_equal_to_string ("oval_definitions"));
321 generator_name = g_strdup ("generator");
322 generator = element_child (element, generator_name);
323 g_free (generator_name);
324 assert_that (generator, is_not_null);
325 timestamp = element_child (generator, "oval:timestamp");
326 assert_that (timestamp, is_not_null);
327
328 text = element_text (timestamp);
329 assert_that (text, is_equal_to_string ("2015-08-20T10:09:07.183-04:00"));
330 g_free (text);
331
332 element_free (element);
333}
334
335Ensure (xmlutils, parse_element_item_metadata)
336{
337 element_t element, item, meta;
338 const gchar *xml;
339
340 xml = "<cpe-list>"
341 " <cpe-item "
342 "name=\"cpe:/"
343 "a:%240.99_kindle_books_project:%240.99_kindle_books:6::~~~android~~\">"
344 " <title xml:lang=\"en-US\">$0.99 Kindle Books project $0.99 Kindle "
345 "Books (aka com.kindle.books.for99) for android 6.0</title>"
346 " <references>"
347 " <reference "
348 "href=\"https://play.google.com/store/apps/"
349 "details?id=com.kindle.books.for99\">Product information</reference>"
350 " <reference "
351 "href=\"https://docs.google.com/spreadsheets/d/"
352 "1t5GXwjw82SyunALVJb2w0zi3FoLRIkfGPc7AMjRF0r4/"
353 "edit?pli=1#gid=1053404143\">Government Advisory</reference>"
354 " </references>"
355 " <meta:item-metadata nvd-id=\"289692\" status=\"FINAL\" "
356 "modification-date=\"2014-11-10T17:01:25.103Z\"/>"
357 " </cpe-item>"
358 "</cpe-list>";
359
360 assert_that (parse_element (xml, &element), is_equal_to (0));
361
362 assert_that (element_name (element), is_equal_to_string ("cpe-list"));
363 item = element_child (element, "cpe-item");
364 assert_that (item, is_not_null);
365 meta = element_child (item, "meta:item-metadata");
366 assert_that (meta, is_not_null);
367 assert_that (element_name (meta), is_equal_to_string ("meta:item-metadata"));
368
369 element_free (element);
370}
371
372Ensure (xmlutils, parse_element_item_metadata_with_namespace)
373{
374 element_t element, item, meta;
375 const gchar *xml;
376
377 xml = "<cpe-list xmlns=\"http://cpe.mitre.org/dictionary/2.0\" "
378 "xmlns:ns6=\"http://scap.nist.gov/schema/scap-core/0.1\" "
379 "xmlns:config=\"http://scap.nist.gov/schema/configuration/0.1\" "
380 "xmlns:meta=\"http://scap.nist.gov/schema/cpe-dictionary-metadata/"
381 "0.2\" xmlns:cpe-23=\"http://scap.nist.gov/schema/cpe-extension/2.3\" "
382 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
383 "xmlns:scap-core=\"http://scap.nist.gov/schema/scap-core/0.3\" "
384 "xsi:schemaLocation=\"http://cpe.mitre.org/dictionary/2.0 "
385 "https://scap.nist.gov/schema/cpe/2.2/cpe-dictionary_2.2.xsd "
386 "http://scap.nist.gov/schema/cpe-dictionary-metadata/0.2 "
387 "https://scap.nist.gov/schema/cpe/2.1/cpe-dictionary-metadata_0.2.xsd "
388 "http://scap.nist.gov/schema/scap-core/0.3 "
389 "https://scap.nist.gov/schema/nvd/scap-core_0.3.xsd "
390 "http://scap.nist.gov/schema/configuration/0.1 "
391 "https://scap.nist.gov/schema/nvd/configuration_0.1.xsd "
392 "http://scap.nist.gov/schema/scap-core/0.1 "
393 "https://scap.nist.gov/schema/nvd/scap-core_0.1.xsd\">"
394 " <cpe-item "
395 "name=\"cpe:/"
396 "a:%240.99_kindle_books_project:%240.99_kindle_books:6::~~~android~~\">"
397 " <title xml:lang=\"en-US\">$0.99 Kindle Books project $0.99 Kindle "
398 "Books (aka com.kindle.books.for99) for android 6.0</title>"
399 " <references>"
400 " <reference "
401 "href=\"https://play.google.com/store/apps/"
402 "details?id=com.kindle.books.for99\">Product information</reference>"
403 " <reference "
404 "href=\"https://docs.google.com/spreadsheets/d/"
405 "1t5GXwjw82SyunALVJb2w0zi3FoLRIkfGPc7AMjRF0r4/"
406 "edit?pli=1#gid=1053404143\">Government Advisory</reference>"
407 " </references>"
408 " <meta:item-metadata nvd-id=\"289692\" status=\"FINAL\" "
409 "modification-date=\"2014-11-10T17:01:25.103Z\"/>"
410 " </cpe-item>"
411 "</cpe-list>";
412
413 assert_that (parse_element (xml, &element), is_equal_to (0));
414
415 assert_that (element_name (element), is_equal_to_string ("cpe-list"));
416 item = element_child (element, "cpe-item");
417 assert_that (item, is_not_null);
418 meta = element_child (item, "item-metadata");
419 assert_that (meta, is_not_null);
420 // NB
421 assert_that (element_name (meta), is_equal_to_string ("item-metadata"));
422 // assert_that (element_name (meta), is_equal_to_string
423 // ("meta:item-metadata"));
424
425 element_free (element);
426}
427
428Ensure (xmlutils, parse_element_item_handles_cdata)
429{
430 element_t element;
431 const gchar *xml;
432 gchar *text;
433
434 xml =
435 "<description><![CDATA[Several vulnerabilities were discovered in the "
436 "Chromium browser. The Common Vulnerabilities and Exposures project "
437 "identifies the following problems: CVE-2011-1108 Google Chrome before "
438 "9.0.597.107 does not properly implement JavaScript dialogs, which allows "
439 "remote attackers to cause a denial of service or possibly have "
440 "unspecified other impact via a crafted HTML document. CVE-2011-1109 "
441 "Google Chrome before 9.0.597.107 does not properly process nodes in "
442 "Cascading Style Sheets stylesheets, which allows remote attackers to "
443 "cause a denial of service or possibly have unspecified other impact via "
444 "unknown vectors that lead to a &quot;stale pointer.&quot; CVE-2011-1113 "
445 "Google Chrome before 9.0.597.107 on 64-bit Linux platforms does not "
446 "properly perform pickle deserialization, which allows remote attackers to "
447 "cause a denial of service via unspecified vectors. CVE-2011-1114 Google "
448 "Chrome before 9.0.597.107 does not properly handle tables, which allows "
449 "remote attackers to cause a denial of service or possibly have "
450 "unspecified other impact via unknown vectors that lead to a &quot;stale "
451 "node.&quot; CVE-2011-1115 Google Chrome before 9.0.597.107 does not "
452 "properly render tables, which allows remote attackers to cause a denial "
453 "of service or possibly have unspecified other impact via unknown vectors "
454 "that lead to a &quot;stale pointer.&quot; CVE-2011-1121 Integer overflow "
455 "in Google Chrome before 9.0.597.107 allows remote attackers to cause a "
456 "denial of service or possibly have unspecified other impact via vectors "
457 "involving a TEXTAREA element. CVE-2011-1122 The WebGL implementation in "
458 "Google Chrome before 9.0.597.107 allows remote attackers to cause a "
459 "denial of service via unspecified vectors, aka Issue 71960. In addition, "
460 "this upload fixes the following issues : Out-of-bounds read in text "
461 "searching [69640] Memory corruption in SVG fonts. [72134] Memory "
462 "corruption with counter nodes. [69628] Stale node in box layout. [70027] "
463 "Cross-origin error message leak with workers. [70336] Stale pointer in "
464 "table painting. [72028] Stale pointer with SVG cursors. "
465 "[73746]]]></description>";
466
467 assert_that (parse_element (xml, &element), is_equal_to (0));
468 assert_that (element_name (element), is_equal_to_string ("description"));
469 text = element_text (element);
470 assert_that (
471 text,
472 is_equal_to_string (
473 "Several vulnerabilities were discovered in the Chromium browser. The "
474 "Common Vulnerabilities and Exposures project identifies the following "
475 "problems: CVE-2011-1108 Google Chrome before 9.0.597.107 does not "
476 "properly implement JavaScript dialogs, which allows remote attackers to "
477 "cause a denial of service or possibly have unspecified other impact via "
478 "a crafted HTML document. CVE-2011-1109 Google Chrome before 9.0.597.107 "
479 "does not properly process nodes in Cascading Style Sheets stylesheets, "
480 "which allows remote attackers to cause a denial of service or possibly "
481 "have unspecified other impact via unknown vectors that lead to a "
482 "&quot;stale pointer.&quot; CVE-2011-1113 Google Chrome before "
483 "9.0.597.107 on 64-bit Linux platforms does not properly perform pickle "
484 "deserialization, which allows remote attackers to cause a denial of "
485 "service via unspecified vectors. CVE-2011-1114 Google Chrome before "
486 "9.0.597.107 does not properly handle tables, which allows remote "
487 "attackers to cause a denial of service or possibly have unspecified "
488 "other impact via unknown vectors that lead to a &quot;stale node.&quot; "
489 "CVE-2011-1115 Google Chrome before 9.0.597.107 does not properly render "
490 "tables, which allows remote attackers to cause a denial of service or "
491 "possibly have unspecified other impact via unknown vectors that lead to "
492 "a &quot;stale pointer.&quot; CVE-2011-1121 Integer overflow in Google "
493 "Chrome before 9.0.597.107 allows remote attackers to cause a denial of "
494 "service or possibly have unspecified other impact via vectors involving "
495 "a TEXTAREA element. CVE-2011-1122 The WebGL implementation in Google "
496 "Chrome before 9.0.597.107 allows remote attackers to cause a denial of "
497 "service via unspecified vectors, aka Issue 71960. In addition, this "
498 "upload fixes the following issues : Out-of-bounds read in text "
499 "searching [69640] Memory corruption in SVG fonts. [72134] Memory "
500 "corruption with counter nodes. [69628] Stale node in box layout. "
501 "[70027] Cross-origin error message leak with workers. [70336] Stale "
502 "pointer in table painting. [72028] Stale pointer with SVG cursors. "
503 "[73746]"));
504 g_free (text);
505 element_free (element);
506}
507
508/* element_next. */
509
510Ensure (xmlutils, element_next_handles_multiple_children)
511{
512 element_t element, child;
513 const gchar *xml;
514 gchar *text;
515
516 xml = "<top><a>1</a><b>2</b><c>3</c></top>";
517
518 assert_that (parse_element (xml, &element), is_equal_to (0));
519
520 assert_that (element_name (element), is_equal_to_string ("top"));
521
522 child = element_first_child (element);
523 assert_that (child, is_not_null);
524 assert_that (element_name (child), is_equal_to_string ("a"));
525 text = element_text (child);
526 assert_that (text, is_equal_to_string ("1"));
527 g_free (text);
528
529 child = element_next (child);
530 assert_that (child, is_not_null);
531 assert_that (element_name (child), is_equal_to_string ("b"));
532 text = element_text (child);
533 assert_that (text, is_equal_to_string ("2"));
534 g_free (text);
535
536 child = element_next (child);
537 assert_that (child, is_not_null);
538 assert_that (element_name (child), is_equal_to_string ("c"));
539 text = element_text (child);
540 assert_that (text, is_equal_to_string ("3"));
541 g_free (text);
542
543 element_free (element);
544}
545
546Ensure (xmlutils, parse_element_free_using_child)
547{
548 element_t element;
549 const gchar *xml;
550
551 xml = "<a><b><c>1</c></b></a>";
552
553 assert_that (parse_element (xml, &element), is_equal_to (0));
554 assert_that (element_name (element), is_equal_to_string ("a"));
555 element = element_child (element, "b");
556 assert_that (element, is_not_null);
557 element = element_child (element, "c");
558 assert_that (element, is_not_null);
559 element_free (element);
560}
561
562Ensure (xmlutils, print_element_to_string_prints)
563{
564 element_t element;
565 const gchar *xml;
566 GString *str;
567
568 xml = "<a aa=\"1\">a text<b><c ca=\"x\" ca2=\"y\">1</c><d/><e></e></b> and "
569 "more a text</a>";
570 str = g_string_new ("");
571
572 assert_that (parse_element (xml, &element), is_equal_to (0));
573 print_element_to_string (element, str);
574 assert_that (str->str, is_equal_to_string (
575 "<a aa=\"1\">a text and more a text<b><c ca=\"x\" "
576 "ca2=\"y\">1</c><d></d><e></e></b></a>"));
577 g_string_free (str, TRUE);
578 element_free (element);
579}
580
581Ensure (xmlutils, depth1_returns_top_level_children_in_order)
582{
583 gchar *text;
584 const char *xml = "<root>"
585 " <a x='1'>A</a>"
586 " <b>B</b>"
587 " <c><d>D</d></c>"
588 "</root>";
589
590 gchar *path = write_temp_xml (xml);
591 assert_that (path, is_not_null);
592
594 assert_that (xml_file_iterator_init_from_file_path (it, path, 1),
595 is_equal_to (0));
596
597 gchar *err = NULL;
598 element_t e;
599
600 e = xml_file_iterator_next (it, &err);
601 assert_that (err, is_null);
602 assert_that (e, is_not_null);
603 assert_that (element_name (e), is_equal_to_string ("a"));
604 text = element_text (e);
605 assert_that (text, is_equal_to_string ("A"));
606 g_free (text);
607 element_free (e);
608
609 e = xml_file_iterator_next (it, &err);
610 assert_that (err, is_null);
611 assert_that (e, is_not_null);
612 assert_that (element_name (e), is_equal_to_string ("b"));
613 text = element_text (e);
614 assert_that (text, is_equal_to_string ("B"));
615 g_free (text);
616 element_free (e);
617
618 e = xml_file_iterator_next (it, &err);
619 assert_that (err, is_null);
620 assert_that (e, is_not_null);
621 assert_that (element_name (e), is_equal_to_string ("c"));
622 element_t d = element_child (e, "d");
623 assert_that (d, is_not_null);
624 text = element_text (d);
625 assert_that (text, is_equal_to_string ("D"));
626 g_free (text);
627 element_free (e);
628
629 e = xml_file_iterator_next (it, &err);
630 assert_that (e, is_null);
631 assert_that (err, is_null);
632
634 g_unlink (path);
635 g_free (path);
636}
637
638Ensure (xmlutils, depth2_returns_grandchildren)
639{
640 gchar *attr;
641 const char *xml = "<root>"
642 " <a>A</a>"
643 " <c><d id='1'>D</d><d id='2'>E</d></c>"
644 "</root>";
645
646 gchar *path = write_temp_xml (xml);
647 assert_that (path, is_not_null);
648
650 assert_that (xml_file_iterator_init_from_file_path (it, path, 2),
651 is_equal_to (0));
652
653 gchar *err = NULL;
654 element_t e;
655
656 // a has no grandchildren (depth2 under root), but iterator will yield
657 // the d nodes when closing their parent <c>
658 e = xml_file_iterator_next (it, &err);
659 assert_that (err, is_null);
660 assert_that (e, is_not_null);
661 assert_that (element_name (e), is_equal_to_string ("d"));
662 attr = element_attribute (e, "id");
663 assert_that (attr, is_equal_to_string ("1"));
664 g_free (attr);
665 element_free (e);
666
667 e = xml_file_iterator_next (it, &err);
668 assert_that (err, is_null);
669 assert_that (e, is_not_null);
670 assert_that (element_name (e), is_equal_to_string ("d"));
671 attr = element_attribute (e, "id");
672 assert_that (attr, is_equal_to_string ("2"));
673 g_free (attr);
674 element_free (e);
675
676 e = xml_file_iterator_next (it, &err);
677 assert_that (e, is_null);
678 assert_that (err, is_null);
679
681 g_unlink (path);
682 g_free (path);
683}
684
685Ensure (xmlutils, rewind_resets_state)
686{
687 const char *xml = "<root><x>1</x><y>2</y></root>";
688 gchar *path = write_temp_xml (xml);
689 assert_that (path, is_not_null);
690
692 assert_that (xml_file_iterator_init_from_file_path (it, path, 1),
693 is_equal_to (0));
694
695 gchar *err = NULL;
696 element_t e;
697
698 e = xml_file_iterator_next (it, &err);
699 assert_that (element_name (e), is_equal_to_string ("x"));
700 element_free (e);
701
702 assert_that (xml_file_iterator_rewind (it), is_equal_to (0));
703
704 e = xml_file_iterator_next (it, &err);
705 assert_that (element_name (e), is_equal_to_string ("x"));
706 element_free (e);
707
708 e = xml_file_iterator_next (it, &err);
709 assert_that (element_name (e), is_equal_to_string ("y"));
710 element_free (e);
711
713 g_unlink (path);
714 g_free (path);
715}
716
717/* Test suite. */
718
719int
720main (int argc, char **argv)
721{
722 int ret;
723 TestSuite *suite;
724
725 suite = create_test_suite ();
726
727 add_test_with_context (suite, xmlutils, parse_entity_parses_simple_xml);
728 add_test_with_context (suite, xmlutils,
729 parse_entity_parses_xml_with_attributes);
730 add_test_with_context (suite, xmlutils, parse_entity_handles_declaration);
731 add_test_with_context (suite, xmlutils, parse_entity_handles_namespace);
732 add_test_with_context (suite, xmlutils, parse_entity_oval_timestamp);
733
734 add_test_with_context (suite, xmlutils,
735 next_entities_handles_multiple_children);
736
737 add_test_with_context (suite, xmlutils, parse_element_parses_simple_xml);
738 add_test_with_context (suite, xmlutils,
739 parse_element_parses_xml_with_attributes);
740 add_test_with_context (suite, xmlutils, parse_element_handles_declaration);
741 add_test_with_context (suite, xmlutils, parse_element_handles_namespace);
742 add_test_with_context (suite, xmlutils, parse_element_oval_timestamp);
743 add_test_with_context (suite, xmlutils, parse_element_item_metadata);
744 add_test_with_context (suite, xmlutils,
745 parse_element_item_metadata_with_namespace);
746 add_test_with_context (suite, xmlutils, parse_element_item_handles_cdata);
747 add_test_with_context (suite, xmlutils, parse_element_free_using_child);
748
749 add_test_with_context (suite, xmlutils, print_element_to_string_prints);
750
751 add_test_with_context (suite, xmlutils,
752 element_next_handles_multiple_children);
753
754 add_test_with_context (suite, xmlutils,
755 depth1_returns_top_level_children_in_order);
756
757 add_test_with_context (suite, xmlutils, depth2_returns_grandchildren);
758
759 add_test_with_context (suite, xmlutils, rewind_resets_state);
760
761 if (argc > 1)
762 ret = run_single_test (suite, argv[1], create_text_reporter ());
763 else
764 ret = run_test_suite (suite, create_text_reporter ());
765
766 destroy_test_suite (suite);
767
768 return ret;
769}
entities_t entities
Children.
Definition xmlutils.h:56
Simple XML reader.
void element_free(element_t element)
Free an entire element tree.
Definition xmlutils.c:2035
const char * entity_attribute(entity_t entity, const char *name)
Get an attribute of an entity.
Definition xmlutils.c:216
char * entity_name(entity_t entity)
Get the name an entity.
Definition xmlutils.c:161
char * entity_text(entity_t entity)
Get the text an entity.
Definition xmlutils.c:145
const gchar * element_name(element_t element)
Get the name of an element.
Definition xmlutils.c:2052
xml_file_iterator_t xml_file_iterator_new(void)
Allocates a new, uninitialized XML file iterator.
Definition xmlutils.c:2859
entity_t first_entity(entities_t entities)
Return the first entity from an entities_t.
Definition xmlutils.c:82
gchar * element_text(element_t element)
Get text of an element.
Definition xmlutils.c:2137
int xml_file_iterator_init_from_file_path(xml_file_iterator_t iterator, const char *file_path, int output_depth)
Initializes an XML file iterator to read from a given path.
Definition xmlutils.c:2876
void print_element_to_string(element_t element, GString *string)
Print an XML element tree to a GString, appending it if string is not.
Definition xmlutils.c:2280
int xml_file_iterator_rewind(xml_file_iterator_t iterator)
Rewinds an XML file iterator by rewinding the file and creating a new XML parser context.
Definition xmlutils.c:2956
entities_t next_entities(entities_t entities)
Return all the entities from an entities_t after the first.
Definition xmlutils.c:67
gchar * element_attribute(element_t element, const gchar *name)
Get an attribute of an element.
Definition xmlutils.c:2162
element_t element_first_child(element_t element)
Get the first child of an element.
Definition xmlutils.c:2210
element_t xml_file_iterator_next(xml_file_iterator_t iterator, gchar **error)
Get the next subelement from a XML file iterator.
Definition xmlutils.c:3002
void free_entity(entity_t entity)
Free an entity, recursively.
Definition xmlutils.c:115
void xml_file_iterator_free(xml_file_iterator_t iterator)
Frees an XML file iterator and all of its internal data structures.
Definition xmlutils.c:2919
int parse_element(const gchar *string, element_t *element)
Read an XML element tree from a string.
Definition xmlutils.c:2003
entity_t entity_child(entity_t entity, const char *name)
Get a child of an entity.
Definition xmlutils.c:193
element_t element_next(element_t element)
Get the next sibling of an element.
Definition xmlutils.c:2230
element_t element_child(element_t element, const gchar *name)
Get a child of an element.
Definition xmlutils.c:2086
int parse_entity(const char *string, entity_t *entity)
Read an XML entity tree from a string.
Definition xmlutils.c:1511
struct entity_s * entity_t
Definition xmlutils.h:58
struct xml_file_iterator_struct * xml_file_iterator_t
Definition xmlutils.h:188
struct _xmlNode * element_t
Definition xmlutils.h:157
GSList * entities_t
Entities.
Definition xmlutils.h:46
Ensure(xmlutils, parse_entity_parses_simple_xml)
int main(int argc, char **argv)
AfterEach(xmlutils)
BeforeEach(xmlutils)
Describe(xmlutils)
static gchar * write_temp_xml(const char *xml)