blob: 3b5a22397c8c4d705ea4e7fdb14e5f0b02eeb436 [file] [log] [blame]
Radek Krejcid91dbaf2018-09-21 15:51:39 +02001/*
2 * @file xml.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from xml.c
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci2d7a47b2019-05-16 13:34:10 +020015#define _DEFAULT_SOURCE
16#define _GNU_SOURCE
17
Radek Krejcid91dbaf2018-09-21 15:51:39 +020018#include <stdarg.h>
19#include <stddef.h>
20#include <setjmp.h>
21#include <cmocka.h>
22
Radek Krejci2d7a47b2019-05-16 13:34:10 +020023#include <stdlib.h>
Radek Krejcid91dbaf2018-09-21 15:51:39 +020024#include <stdio.h>
25#include <string.h>
26
Radek Krejci2d7a47b2019-05-16 13:34:10 +020027#include "../../src/xml.h"
28
29LY_ERR lyxml_ns_add(struct lyxml_context *context, const char *prefix, size_t prefix_len, char *uri);
30LY_ERR lyxml_ns_rm(struct lyxml_context *context);
Radek Krejcid91dbaf2018-09-21 15:51:39 +020031
32#define BUFSIZE 1024
33char logbuf[BUFSIZE] = {0};
34
35/* set to 0 to printing error messages to stderr instead of checking them in code */
36#define ENABLE_LOGGER_CHECKING 1
37
38static void
39logger(LY_LOG_LEVEL level, const char *msg, const char *path)
40{
41 (void) level; /* unused */
42
43 if (path) {
44 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
45 } else {
46 strncpy(logbuf, msg, BUFSIZE - 1);
47 }
48}
49
50static int
51logger_setup(void **state)
52{
53 (void) state; /* unused */
54#if ENABLE_LOGGER_CHECKING
55 ly_set_log_clb(logger, 1);
56#endif
57 return 0;
58}
59
60void
61logbuf_clean(void)
62{
63 logbuf[0] = '\0';
64}
65
66#if ENABLE_LOGGER_CHECKING
67# define logbuf_assert(str) assert_string_equal(logbuf, str)
68#else
69# define logbuf_assert(str)
70#endif
71
72static void
73test_element(void **state)
74{
75 (void) state; /* unused */
76
77 size_t name_len, prefix_len;
Radek Krejci28e8cb52019-03-08 11:31:31 +010078 size_t buf_len, len;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020079 const char *name, *prefix;
Radek Krejci28e8cb52019-03-08 11:31:31 +010080 char *buf = NULL, *out = NULL;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020081 const char *str, *p;
Radek Krejci28e8cb52019-03-08 11:31:31 +010082 int dynamic;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020083
84 struct lyxml_context ctx;
85 memset(&ctx, 0, sizeof ctx);
86 ctx.line = 1;
87
88 /* empty */
89 str = "";
Radek Krejci7a7fa902018-09-25 17:08:21 +020090 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +020091 assert_null(name);
Radek Krejcib1890642018-10-03 14:05:40 +020092 assert_int_equal(LYXML_END, ctx.status);
Radek Krejcid91dbaf2018-09-21 15:51:39 +020093 assert_true(str[0] == '\0');
Radek Krejcif2c721d2019-06-03 16:37:58 +020094 ctx.status = 0;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020095
Radek Krejcib1890642018-10-03 14:05:40 +020096 /* end element */
97 str = "</element>";
98 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejci3fbc9872019-04-16 16:50:01 +020099 logbuf_assert("Opening and closing elements tag missmatch (\"element\"). Line number 1.");
Radek Krejcib1890642018-10-03 14:05:40 +0200100
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200101 /* no element */
102 logbuf_clean();
103 str = p = "no data present";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200104 assert_int_equal(LY_EINVAL, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200105 assert_null(name);
106 assert_ptr_equal(p, str); /* input data not eaten */
107 logbuf_assert("");
108
109 /* not supported DOCTYPE */
110 str = p = "<!DOCTYPE greeting SYSTEM \"hello.dtd\"><greeting/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200111 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200112 assert_null(name);
113 assert_ptr_equal(p, str); /* input data not eaten */
114 logbuf_assert("Document Type Declaration not supported. Line number 1.");
115
Radek Krejcic5c31bb2019-04-08 14:40:52 +0200116 /* invalid XML */
117 str = p = "<!NONSENCE/>";
118 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
119 assert_null(name);
120 assert_ptr_equal(p, str); /* input data not eaten */
121 logbuf_assert("Unknown XML section \"<!NONSENCE/>\". Line number 1.");
122
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200123 /* unqualified element */
124 str = " < element/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200125 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200126 assert_null(prefix);
127 assert_false(strncmp("element", name, name_len));
128 assert_int_equal(7, name_len);
Radek Krejcifad79c92019-06-04 11:43:30 +0200129 assert_int_equal(LYXML_END, ctx.status);
Radek Krejcib1890642018-10-03 14:05:40 +0200130 assert_string_equal("", str);
Radek Krejci28e8cb52019-03-08 11:31:31 +0100131 assert_int_equal(0, ctx.elements.count);
Radek Krejcifad79c92019-06-04 11:43:30 +0200132 lyxml_context_clear(&ctx);
Radek Krejci28e8cb52019-03-08 11:31:31 +0100133
134 str = " < element attr=\'x\'/>";
135 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
136 assert_int_equal(LYXML_ATTRIBUTE, ctx.status);
137 assert_string_equal("attr=\'x\'/>", str);
138 assert_int_equal(1, ctx.elements.count);
139 assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
140 assert_int_equal(LYXML_ATTR_CONTENT, ctx.status);
141 assert_string_equal("\'x\'/>", str);
142 assert_int_equal(1, ctx.elements.count);
143 assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejcifad79c92019-06-04 11:43:30 +0200144 assert_int_equal(LYXML_END, ctx.status);
Radek Krejci28e8cb52019-03-08 11:31:31 +0100145 assert_string_equal("", str);
146 assert_int_equal(0, ctx.elements.count);
Radek Krejcifad79c92019-06-04 11:43:30 +0200147 lyxml_context_clear(&ctx);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200148
Radek Krejcifb7c6582018-09-21 16:12:45 +0200149 str = "<?xml version=\"1.0\"?> <!-- comment --> <![CDATA[<greeting>Hello, world!</greeting>]]> <?TEST xxx?> <element/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200150 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200151 assert_null(prefix);
152 assert_false(strncmp("element", name, name_len));
153 assert_int_equal(7, name_len);
Radek Krejcifad79c92019-06-04 11:43:30 +0200154 assert_int_equal(LYXML_END, ctx.status);
Radek Krejcib1890642018-10-03 14:05:40 +0200155 assert_string_equal("", str);
Radek Krejcifad79c92019-06-04 11:43:30 +0200156 lyxml_context_clear(&ctx);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200157
158 str = "<element xmlns=\"urn\"></element>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200159 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200160 assert_null(prefix);
161 assert_false(strncmp("element", name, name_len));
162 assert_int_equal(7, name_len);
Radek Krejcib1890642018-10-03 14:05:40 +0200163 assert_int_equal(LYXML_ATTRIBUTE, ctx.status);
164 assert_string_equal("xmlns=\"urn\"></element>", str);
165 /* cleean context by getting closing tag */
166 str += 12;
167 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcifad79c92019-06-04 11:43:30 +0200168 lyxml_context_clear(&ctx);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200169
170 /* qualified element */
171 str = " < yin:element/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200172 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200173 assert_false(strncmp("yin", prefix, prefix_len));
174 assert_false(strncmp("element", name, name_len));
175 assert_int_equal(3, prefix_len);
176 assert_int_equal(7, name_len);
Radek Krejcifad79c92019-06-04 11:43:30 +0200177 assert_int_equal(LYXML_END, ctx.status);
Radek Krejcib1890642018-10-03 14:05:40 +0200178 assert_string_equal("", str);
Radek Krejcifad79c92019-06-04 11:43:30 +0200179 lyxml_context_clear(&ctx);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200180
181 str = "<yin:element xmlns=\"urn\"></element>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200182 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200183 assert_false(strncmp("yin", prefix, prefix_len));
184 assert_false(strncmp("element", name, name_len));
185 assert_int_equal(3, prefix_len);
186 assert_int_equal(7, name_len);
Radek Krejcib1890642018-10-03 14:05:40 +0200187 assert_int_equal(LYXML_ATTRIBUTE, ctx.status);
188 assert_string_equal("xmlns=\"urn\"></element>", str);
189 /* cleean context by getting closing tag */
190 str += 12;
191 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejci3fbc9872019-04-16 16:50:01 +0200192 logbuf_assert("Opening and closing elements tag missmatch (\"element\"). Line number 1.");
Radek Krejcia93621b2018-10-18 11:13:38 +0200193 str = "</yin:element/>";
194 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
195 logbuf_assert("Unexpected data \"/>\" in closing element tag. Line number 1.");
Radek Krejcib1890642018-10-03 14:05:40 +0200196 lyxml_context_clear(&ctx);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200197
198 /* UTF8 characters */
199 str = "<𠜎€𠜎Øn:𠜎€𠜎Øn/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200200 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200201 assert_false(strncmp("𠜎€𠜎Øn", prefix, prefix_len));
202 assert_false(strncmp("𠜎€𠜎Øn", name, name_len));
203 assert_int_equal(14, prefix_len);
204 assert_int_equal(14, name_len);
Radek Krejcifad79c92019-06-04 11:43:30 +0200205 assert_int_equal(LYXML_END, ctx.status);
Radek Krejcib1890642018-10-03 14:05:40 +0200206 assert_string_equal("", str);
Radek Krejcifad79c92019-06-04 11:43:30 +0200207 lyxml_context_clear(&ctx);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200208
209 /* invalid UTF-8 character */
210 str = "<¢:element>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200211 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200212 logbuf_assert("Identifier \"¢:element>\" starts with invalid character. Line number 1.");
213 str = "<yin:c⁐element>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200214 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200215 logbuf_assert("Invalid character sequence \"⁐element>\", expected whitespace or element tag termination ('>' or '/>'. Line number 1.");
Radek Krejcifad79c92019-06-04 11:43:30 +0200216 lyxml_context_clear(&ctx);
Radek Krejci339e2de2019-05-17 14:28:24 +0200217
218 /* mixed content */
219 str = "<a>text <b>x</b></a>";
220 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
221 assert_string_equal("text <b>x</b></a>", str);
222 assert_int_equal(LYXML_ELEM_CONTENT, ctx.status);
223 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
224 logbuf_assert("Mixed XML content is not allowed (text <b>). Line number 1.");
225 lyxml_context_clear(&ctx);
226
Radek Krejcid972c252018-09-25 13:23:39 +0200227}
228
229static void
230test_attribute(void **state)
231{
232 (void) state; /* unused */
233
234 size_t name_len, prefix_len;
235 const char *name, *prefix;
236 const char *str, *p;
237
238 struct lyxml_context ctx;
239 memset(&ctx, 0, sizeof ctx);
240 ctx.line = 1;
241
242 /* empty - without element tag termination */
243 str = "";
Radek Krejcifad79c92019-06-04 11:43:30 +0200244 assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200245
Radek Krejcid972c252018-09-25 13:23:39 +0200246 /* not an attribute */
247 str = p = "unknown/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200248 assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200249 assert_ptr_equal(p, str); /* input data not eaten */
250 logbuf_assert("Invalid character sequence \"/>\", expected whitespace or '='. Line number 1.");
251 str = p = "unknown />";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200252 assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200253 assert_ptr_equal(p, str); /* input data not eaten */
254 logbuf_assert("Invalid character sequence \"/>\", expected '='. Line number 1.");
255 str = p = "xxx=/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200256 assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200257 assert_ptr_equal(p, str); /* input data not eaten */
258 logbuf_assert("Invalid character sequence \"/>\", expected either single or double quotation mark. Line number 1.");
259 str = p = "xxx\n = yyy/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200260 assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200261 assert_ptr_equal(p, str); /* input data not eaten */
262 logbuf_assert("Invalid character sequence \"yyy/>\", expected either single or double quotation mark. Line number 2.");
263
264 /* valid attribute */
265 str = "xmlns=\"urn\">";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200266 assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejci49165732019-05-15 16:41:58 +0200267 assert_null(name);
Radek Krejcid972c252018-09-25 13:23:39 +0200268 assert_null(prefix);
Radek Krejci49165732019-05-15 16:41:58 +0200269 assert_int_equal(0, name_len);
Radek Krejcid972c252018-09-25 13:23:39 +0200270 assert_int_equal(0, prefix_len);
Radek Krejci49165732019-05-15 16:41:58 +0200271 assert_int_equal(1, ctx.ns.count);
272 assert_string_equal("", str);
273 assert_int_equal(LYXML_ELEM_CONTENT, ctx.status);
Radek Krejcid972c252018-09-25 13:23:39 +0200274
Radek Krejci49165732019-05-15 16:41:58 +0200275 str = "xmlns:nc\n = \'urn\'>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200276 assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejci49165732019-05-15 16:41:58 +0200277 assert_null(name);
278 assert_null(prefix);
279 assert_int_equal(0, name_len);
280 assert_int_equal(0, prefix_len);
Radek Krejcid972c252018-09-25 13:23:39 +0200281 assert_int_equal(3, ctx.line);
Radek Krejci49165732019-05-15 16:41:58 +0200282 assert_int_equal(2, ctx.ns.count);
283 assert_string_equal("", str);
284 assert_int_equal(LYXML_ELEM_CONTENT, ctx.status);
Radek Krejci28e8cb52019-03-08 11:31:31 +0100285
Radek Krejci49165732019-05-15 16:41:58 +0200286 lyxml_context_clear(&ctx);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200287}
288
Radek Krejci7a7fa902018-09-25 17:08:21 +0200289static void
290test_text(void **state)
291{
292 (void) state; /* unused */
293
Radek Krejcid70d1072018-10-09 14:20:47 +0200294 size_t buf_len, len;
295 int dynamic;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200296 const char *str, *p;
Radek Krejcid70d1072018-10-09 14:20:47 +0200297 char *buf = NULL, *out = NULL;
Radek Krejci339e2de2019-05-17 14:28:24 +0200298 const char *prefix, *name;
299 size_t prefix_len, name_len;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200300
301 struct lyxml_context ctx;
302 memset(&ctx, 0, sizeof ctx);
303 ctx.line = 1;
304
305 /* empty attribute value */
Radek Krejcib1890642018-10-03 14:05:40 +0200306 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200307 str = "\"\"";
Radek Krejcid70d1072018-10-09 14:20:47 +0200308 assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
309 assert_null(buf);
310 assert_ptr_equal(&str[-1], out);
311 assert_int_equal(0, dynamic);
312 assert_int_equal(0, len);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200313 assert_true(str[0] == '\0'); /* everything eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200314 assert_int_equal(LYXML_ATTRIBUTE, ctx.status);
315
316 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200317 str = "\'\'";
Radek Krejcid70d1072018-10-09 14:20:47 +0200318 assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
319 assert_null(buf);
320 assert_ptr_equal(&str[-1], out);
321 assert_int_equal(0, dynamic);
322 assert_int_equal(0, len);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200323 assert_true(str[0] == '\0'); /* everything eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200324 assert_int_equal(LYXML_ATTRIBUTE, ctx.status);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200325
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200326 /* empty element content - only formating before defining child */
Radek Krejcib1890642018-10-03 14:05:40 +0200327 ctx.status = LYXML_ELEM_CONTENT;
Radek Krejci339e2de2019-05-17 14:28:24 +0200328 str = "<x>\n <y>";
329 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid70d1072018-10-09 14:20:47 +0200330 assert_int_equal(LY_EINVAL, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
331 assert_null(buf);
Radek Krejci339e2de2019-05-17 14:28:24 +0200332 assert_string_equal("<y>", str);
333 lyxml_context_clear(&ctx);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200334
Radek Krejci7a7fa902018-09-25 17:08:21 +0200335 /* empty element content is invalid - missing content terminating character < */
Radek Krejcib1890642018-10-03 14:05:40 +0200336 ctx.status = LYXML_ELEM_CONTENT;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200337 str = "";
Radek Krejcid70d1072018-10-09 14:20:47 +0200338 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
339 assert_null(buf);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200340 logbuf_assert("Unexpected end-of-input. Line number 2.");
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200341
Radek Krejcib1890642018-10-03 14:05:40 +0200342 ctx.status = LYXML_ELEM_CONTENT;
Radek Krejci117d2082018-09-26 10:05:14 +0200343 str = p = "xxx";
Radek Krejcid70d1072018-10-09 14:20:47 +0200344 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
345 assert_null(buf);
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200346 logbuf_assert("Unexpected end-of-input. Line number 2.");
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200347 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejci7a7fa902018-09-25 17:08:21 +0200348
Radek Krejci7a7fa902018-09-25 17:08:21 +0200349 /* valid strings */
Radek Krejcib1890642018-10-03 14:05:40 +0200350 ctx.status = LYXML_ELEM_CONTENT;
Radek Krejci339e2de2019-05-17 14:28:24 +0200351 str = "<a>€𠜎Øn \n&lt;&amp;&quot;&apos;&gt; &#82;&#x4f;&#x4B;</a>";
352 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid70d1072018-10-09 14:20:47 +0200353 assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
354 assert_int_not_equal(0, dynamic);
355 assert_non_null(buf);
356 assert_ptr_equal(out, buf);
357 assert_int_equal(22, buf_len);
358 assert_int_equal(21, len);
359 assert_string_equal("€𠜎Øn \n<&\"\'> ROK", buf);
Radek Krejcif2c721d2019-06-03 16:37:58 +0200360 assert_string_equal("</a>", str);
Radek Krejcib1890642018-10-03 14:05:40 +0200361 assert_int_equal(LYXML_ELEMENT, ctx.status);
Radek Krejci339e2de2019-05-17 14:28:24 +0200362 lyxml_context_clear(&ctx);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200363
Radek Krejci117d2082018-09-26 10:05:14 +0200364 /* test using n-bytes UTF8 hexadecimal code points */
Radek Krejcib1890642018-10-03 14:05:40 +0200365 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci117d2082018-09-26 10:05:14 +0200366 str = "\'&#x0024;&#x00A2;&#x20ac;&#x10348;\'";
Radek Krejcid70d1072018-10-09 14:20:47 +0200367 assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
368 assert_int_not_equal(0, dynamic);
369 assert_non_null(buf);
370 assert_ptr_equal(out, buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200371 assert_int_equal(22, buf_len);
Radek Krejcid70d1072018-10-09 14:20:47 +0200372 assert_int_equal(10, len);
373 assert_string_equal("$¢€𐍈", buf);
Radek Krejcib1890642018-10-03 14:05:40 +0200374 assert_int_equal(LYXML_ATTRIBUTE, ctx.status);
Radek Krejci117d2082018-09-26 10:05:14 +0200375
Radek Krejcid70d1072018-10-09 14:20:47 +0200376 free(buf);
377 buf = NULL;
378
Radek Krejci7a7fa902018-09-25 17:08:21 +0200379 /* invalid characters in string */
Radek Krejcib1890642018-10-03 14:05:40 +0200380 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200381 str = p = "\'&#x52\'";
Radek Krejcid70d1072018-10-09 14:20:47 +0200382 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200383 logbuf_assert("Invalid character sequence \"'\", expected ;. Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200384 assert_null(buf);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200385 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200386 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200387 str = p = "\"&#82\"";
Radek Krejcid70d1072018-10-09 14:20:47 +0200388 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200389 logbuf_assert("Invalid character sequence \"\"\", expected ;. Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200390 assert_null(buf);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200391 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200392 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200393 str = p = "\"&nonsence;\"";
Radek Krejcid70d1072018-10-09 14:20:47 +0200394 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200395 logbuf_assert("Entity reference \"&nonsence;\" not supported, only predefined references allowed. Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200396 assert_null(buf);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200397 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200398 ctx.status = LYXML_ELEM_CONTENT;
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200399 str = p = "&#o122;";
Radek Krejcid70d1072018-10-09 14:20:47 +0200400 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200401 logbuf_assert("Invalid character reference \"&#o122;\". Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200402 assert_null(buf);
Radek Krejci117d2082018-09-26 10:05:14 +0200403 assert_ptr_equal(p, str); /* input data not eaten */
404
Radek Krejcib1890642018-10-03 14:05:40 +0200405 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci117d2082018-09-26 10:05:14 +0200406 str = p = "\'&#x06;\'";
Radek Krejcid70d1072018-10-09 14:20:47 +0200407 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200408 logbuf_assert("Invalid character reference \"&#x06;\'\" (0x00000006). Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200409 assert_null(buf);
Radek Krejci117d2082018-09-26 10:05:14 +0200410 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200411 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci117d2082018-09-26 10:05:14 +0200412 str = p = "\'&#xfdd0;\'";
Radek Krejcid70d1072018-10-09 14:20:47 +0200413 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200414 logbuf_assert("Invalid character reference \"&#xfdd0;\'\" (0x0000fdd0). Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200415 assert_null(buf);
Radek Krejci117d2082018-09-26 10:05:14 +0200416 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200417 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci117d2082018-09-26 10:05:14 +0200418 str = p = "\'&#xffff;\'";
Radek Krejcid70d1072018-10-09 14:20:47 +0200419 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200420 logbuf_assert("Invalid character reference \"&#xffff;\'\" (0x0000ffff). Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200421 assert_null(buf);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200422 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejci7a7fa902018-09-25 17:08:21 +0200423}
424
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200425static void
426test_ns(void **state)
427{
428 (void) state; /* unused */
429
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200430 const struct lyxml_ns *ns;
431
432 struct lyxml_context ctx;
433 memset(&ctx, 0, sizeof ctx);
434 ctx.line = 1;
435
Radek Krejcie0734d22019-04-05 15:54:28 +0200436 /* simulate adding open element1 into context */
437 ctx.elements.count++;
438 /* processing namespace definitions */
Radek Krejci17a78d82019-05-15 15:49:55 +0200439 assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, NULL, 0, strdup("urn:default")));
440 assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, "nc", 2, strdup("urn:nc1")));
Radek Krejcie0734d22019-04-05 15:54:28 +0200441 /* simulate adding open element2 into context */
442 ctx.elements.count++;
443 /* processing namespace definitions */
Radek Krejci17a78d82019-05-15 15:49:55 +0200444 assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, "nc", 2, strdup("urn:nc2")));
Radek Krejcie0734d22019-04-05 15:54:28 +0200445 assert_int_equal(3, ctx.ns.count);
446 assert_int_not_equal(0, ctx.ns.size);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200447
448 ns = lyxml_ns_get(&ctx, NULL, 0);
449 assert_non_null(ns);
450 assert_null(ns->prefix);
451 assert_string_equal("urn:default", ns->uri);
452
453 ns = lyxml_ns_get(&ctx, "nc", 2);
454 assert_non_null(ns);
455 assert_string_equal("nc", ns->prefix);
456 assert_string_equal("urn:nc2", ns->uri);
457
Radek Krejcie0734d22019-04-05 15:54:28 +0200458 /* simulate closing element2 */
459 ctx.elements.count--;
Radek Krejci17dca992019-05-17 10:53:27 +0200460 lyxml_ns_rm(&ctx);
Radek Krejcie0734d22019-04-05 15:54:28 +0200461 assert_int_equal(2, ctx.ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200462
463 ns = lyxml_ns_get(&ctx, "nc", 2);
464 assert_non_null(ns);
465 assert_string_equal("nc", ns->prefix);
466 assert_string_equal("urn:nc1", ns->uri);
467
Radek Krejcie0734d22019-04-05 15:54:28 +0200468 /* simulate closing element1 */
469 ctx.elements.count--;
Radek Krejci17dca992019-05-17 10:53:27 +0200470 lyxml_ns_rm(&ctx);
Radek Krejcie0734d22019-04-05 15:54:28 +0200471 assert_int_equal(0, ctx.ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200472
473 assert_null(lyxml_ns_get(&ctx, "nc", 2));
474 assert_null(lyxml_ns_get(&ctx, NULL, 0));
475}
476
Radek Krejcie0734d22019-04-05 15:54:28 +0200477static void
478test_ns2(void **state)
479{
480 (void) state; /* unused */
481
Radek Krejcie0734d22019-04-05 15:54:28 +0200482 struct lyxml_context ctx;
483 memset(&ctx, 0, sizeof ctx);
484 ctx.line = 1;
485
Radek Krejcie0734d22019-04-05 15:54:28 +0200486 /* simulate adding open element1 into context */
487 ctx.elements.count++;
488 /* default namespace defined in parent element1 */
Radek Krejci17a78d82019-05-15 15:49:55 +0200489 assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, NULL, 0, strdup("urn:default")));
Radek Krejcie0734d22019-04-05 15:54:28 +0200490 assert_int_equal(1, ctx.ns.count);
491 /* going into child element1 */
Radek Krejcie0734d22019-04-05 15:54:28 +0200492 /* simulate adding open element1 into context */
493 ctx.elements.count++;
494 /* no namespace defined, going out (first, simulate closing of so far open element) */
495 ctx.elements.count--;
Radek Krejci17dca992019-05-17 10:53:27 +0200496 lyxml_ns_rm(&ctx);
Radek Krejcie0734d22019-04-05 15:54:28 +0200497 assert_int_equal(1, ctx.ns.count);
498 /* nothing else, going out of the parent element1 (first, simulate closing of so far open element) */
499 ctx.elements.count--;
Radek Krejci17dca992019-05-17 10:53:27 +0200500 lyxml_ns_rm(&ctx);
Radek Krejcie0734d22019-04-05 15:54:28 +0200501 assert_int_equal(0, ctx.ns.count);
502}
503
Radek Krejcifad79c92019-06-04 11:43:30 +0200504static void
505test_simple_xml(void **state)
506{
507 (void)state; /* unused */
508 size_t name_len, prefix_len;
509 const char *prefix, *name;
510 char *test_in = NULL, *to_free = NULL;
511 struct lyxml_context ctx;
512
513 char *buf = NULL, *output = NULL;
514 size_t buf_size, length;
515 int dynamic;
516 char *test_input = "<elem1 attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>";
517
518 memset(&ctx, 0, sizeof ctx);
519 ctx.line = 1;
520 to_free = test_in = malloc(strlen(test_input) + 1);
521 /* test input */
522 strcpy(test_in, test_input);
523
524 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, (const char **)&test_in, &prefix, &prefix_len, &name, &name_len)); /* <elem1 */
525 assert_int_equal(ctx.status, LYXML_ATTRIBUTE);
526
527 assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, (const char **)&test_in, &prefix, &prefix_len, &name, &name_len)); /* attr1= */
528 assert_int_equal(ctx.status, LYXML_ATTR_CONTENT);
529
530 assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, (const char **)&test_in, &buf, &buf_size, &output, &length, &dynamic)); /* "value" */
531 assert_int_equal(ctx.status, LYXML_ELEM_CONTENT);
532
533 /* try to get string content of elem1 whitespace is removed and EINVAL is expected in this case */
534 assert_int_equal(LY_EINVAL, lyxml_get_string(&ctx, (const char **)&test_in, &buf, &buf_size, &output, &length, &dynamic));
535 assert_int_equal(ctx.status, LYXML_ELEM_CONTENT);
536
537 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, (const char **)&test_in, &prefix, &prefix_len, &name, &name_len)); /* <elem2 */
538 assert_int_equal(ctx.status, LYXML_ATTRIBUTE);
539
540 assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, (const char **)&test_in, &prefix, &prefix_len, &name, &name_len)); /* attr2= */
541 assert_int_equal(ctx.status, LYXML_ATTR_CONTENT);
542
543 assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, (const char **)&test_in, &buf, &buf_size, &output, &length, &dynamic)); /* "value" */
544 assert_int_equal(ctx.status, LYXML_ELEMENT);
545
546 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, (const char **)&test_in, &prefix, &prefix_len, &name, &name_len)); /* </elem1> */
547 assert_int_equal(ctx.status, LYXML_END);
548
549 lyxml_context_clear(&ctx);
550 free(to_free);
551}
552
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200553int main(void)
554{
555 const struct CMUnitTest tests[] = {
Radek Krejcid972c252018-09-25 13:23:39 +0200556 cmocka_unit_test_setup(test_element, logger_setup),
557 cmocka_unit_test_setup(test_attribute, logger_setup),
Radek Krejci7a7fa902018-09-25 17:08:21 +0200558 cmocka_unit_test_setup(test_text, logger_setup),
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200559 cmocka_unit_test_setup(test_ns, logger_setup),
Radek Krejcie0734d22019-04-05 15:54:28 +0200560 cmocka_unit_test_setup(test_ns2, logger_setup),
Radek Krejcifad79c92019-06-04 11:43:30 +0200561 cmocka_unit_test_setup(test_simple_xml, logger_setup),
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200562 };
563
564 return cmocka_run_group_tests(tests, NULL, NULL);
565}