blob: a4dee5930af9d00eb5ab33ac446e5ced9645ca18 [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
Radek Krejcif8dc59a2020-11-25 13:47:44 +010017#define _POSIX_C_SOURCE 200809L /* strdup */
Radek Krejcib7db73a2018-10-24 14:18:40 +020018
Radek Krejcid91dbaf2018-09-21 15:51:39 +020019#include <stdarg.h>
20#include <stddef.h>
21#include <setjmp.h>
22#include <cmocka.h>
23
Radek Krejci2d7a47b2019-05-16 13:34:10 +020024#include <stdlib.h>
Radek Krejcid91dbaf2018-09-21 15:51:39 +020025#include <stdio.h>
26#include <string.h>
27
Radek Krejci70593c12020-06-13 20:48:09 +020028#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020029#include "in_internal.h"
Michal Vasko63f3d842020-07-08 10:10:14 +020030#include "xml.h"
Radek Krejci2d7a47b2019-05-16 13:34:10 +020031
Michal Vaskob36053d2020-03-26 15:49:30 +010032LY_ERR lyxml_ns_add(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len, char *uri);
33LY_ERR lyxml_ns_rm(struct lyxml_ctx *xmlctx);
34
35static int
36setup(void **state)
37{
38 if (ly_ctx_new(NULL, 0, (struct ly_ctx **)state)) {
39 return 1;
40 }
41
42 return 0;
43}
44
45static int
46teardown(void **state)
47{
48 ly_ctx_destroy(*state, NULL);
49 return 0;
50}
Radek Krejcid91dbaf2018-09-21 15:51:39 +020051
52#define BUFSIZE 1024
53char logbuf[BUFSIZE] = {0};
54
55/* set to 0 to printing error messages to stderr instead of checking them in code */
56#define ENABLE_LOGGER_CHECKING 1
57
58static void
59logger(LY_LOG_LEVEL level, const char *msg, const char *path)
60{
61 (void) level; /* unused */
62
63 if (path) {
64 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
65 } else {
66 strncpy(logbuf, msg, BUFSIZE - 1);
67 }
68}
69
70static int
71logger_setup(void **state)
72{
73 (void) state; /* unused */
74#if ENABLE_LOGGER_CHECKING
75 ly_set_log_clb(logger, 1);
76#endif
77 return 0;
78}
79
80void
81logbuf_clean(void)
82{
83 logbuf[0] = '\0';
84}
85
86#if ENABLE_LOGGER_CHECKING
87# define logbuf_assert(str) assert_string_equal(logbuf, str)
88#else
89# define logbuf_assert(str)
90#endif
91
92static void
93test_element(void **state)
94{
Michal Vaskob36053d2020-03-26 15:49:30 +010095 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +020096 struct ly_in *in;
Michal Vaskob36053d2020-03-26 15:49:30 +010097 const char *str;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020098
99 /* empty */
100 str = "";
Michal Vasko63f3d842020-07-08 10:10:14 +0200101 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
102 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100103 assert_int_equal(LYXML_END, xmlctx->status);
Michal Vaskob36053d2020-03-26 15:49:30 +0100104 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200105 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200106
Radek Krejcib1890642018-10-03 14:05:40 +0200107 /* end element */
108 str = "</element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200109 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
110 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100111 logbuf_assert("Stray closing element tag (\"element\"). Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200112 ly_in_free(in, 0);
Radek Krejcib1890642018-10-03 14:05:40 +0200113
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200114 /* no element */
Michal Vaskob36053d2020-03-26 15:49:30 +0100115 //logbuf_clean();
116 str = "no data present";
Michal Vasko63f3d842020-07-08 10:10:14 +0200117 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
118 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100119 logbuf_assert("Invalid character sequence \"no data present\", expected element tag start ('<'). Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200120 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200121
122 /* not supported DOCTYPE */
Michal Vaskob36053d2020-03-26 15:49:30 +0100123 str = "<!DOCTYPE greeting SYSTEM \"hello.dtd\"><greeting/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200124 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
125 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200126 logbuf_assert("Document Type Declaration not supported. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200127 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200128
Radek Krejcic5c31bb2019-04-08 14:40:52 +0200129 /* invalid XML */
Michal Vaskob36053d2020-03-26 15:49:30 +0100130 str = "<!NONSENSE/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200131 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
132 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100133 logbuf_assert("Unknown XML section \"<!NONSENSE/>\". Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200134 ly_in_free(in, 0);
Radek Krejcic5c31bb2019-04-08 14:40:52 +0200135
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200136 /* unqualified element */
137 str = " < element/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200138 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
139 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100140 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
141 assert_null(xmlctx->prefix);
142 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
143 assert_int_equal(1, xmlctx->elements.count);
Radek Krejci28e8cb52019-03-08 11:31:31 +0100144
Michal Vaskob36053d2020-03-26 15:49:30 +0100145 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
146 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
147 assert_true(!strncmp("", xmlctx->value, xmlctx->value_len));
148
149 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
150 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
151
152 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
153 assert_int_equal(LYXML_END, xmlctx->status);
154 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200155 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100156
157 /* element with attribute */
Radek Krejci28e8cb52019-03-08 11:31:31 +0100158 str = " < element attr=\'x\'/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200159 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
160 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100161 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
162 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
163 assert_null(xmlctx->prefix);
164 assert_int_equal(1, xmlctx->elements.count);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200165
Michal Vaskob36053d2020-03-26 15:49:30 +0100166 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
167 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
168 assert_true(!strncmp("attr", xmlctx->name, xmlctx->name_len));
169 assert_null(xmlctx->prefix);
170
171 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
172 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
173 assert_int_equal(1, xmlctx->elements.count);
174 assert_true(!strncmp("x", xmlctx->value, xmlctx->value_len));
175
176 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
177 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
178
179 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
180 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
181 assert_int_equal(0, xmlctx->elements.count);
182
183 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
184 assert_int_equal(LYXML_END, xmlctx->status);
185 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200186 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100187
188 /* headers and comments */
Radek Krejcifb7c6582018-09-21 16:12:45 +0200189 str = "<?xml version=\"1.0\"?> <!-- comment --> <![CDATA[<greeting>Hello, world!</greeting>]]> <?TEST xxx?> <element/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200190 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
191 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100192 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
193 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
194 assert_null(xmlctx->prefix);
195 assert_int_equal(1, xmlctx->elements.count);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200196
Michal Vaskob36053d2020-03-26 15:49:30 +0100197 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
198 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
199
200 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
201 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
202
203 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
204 assert_int_equal(LYXML_END, xmlctx->status);
205 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200206 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100207
208 /* separate opening and closing tags, neamespaced parsed internally */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200209 str = "<element xmlns=\"urn\"></element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200210 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
211 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100212 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
213 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
214 assert_null(xmlctx->prefix);
215 assert_int_equal(1, xmlctx->elements.count);
216 assert_int_equal(1, xmlctx->ns.count);
217
218 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
219 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
220
221 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
222 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
223 assert_int_equal(0, xmlctx->elements.count);
224 assert_int_equal(0, xmlctx->ns.count);
225
226 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
227 assert_int_equal(LYXML_END, xmlctx->status);
228 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200229 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200230
231 /* qualified element */
232 str = " < yin:element/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200233 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
234 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100235 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
236 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
237 assert_true(!strncmp("yin", xmlctx->prefix, xmlctx->prefix_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200238
Michal Vaskob36053d2020-03-26 15:49:30 +0100239 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
240 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
241
242 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
243 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
244
245 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
246 assert_int_equal(LYXML_END, xmlctx->status);
247 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200248 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100249
250 /* non-matching closing tag */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200251 str = "<yin:element xmlns=\"urn\"></element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200252 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
253 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100254 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
255 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
256 assert_true(!strncmp("yin", xmlctx->prefix, xmlctx->prefix_len));
257 assert_int_equal(1, xmlctx->elements.count);
258 assert_int_equal(1, xmlctx->ns.count);
259
260 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
261 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
262
263 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
264 logbuf_assert("Opening (\"yin:element\") and closing (\"element\") elements tag mismatch. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200265 lyxml_ctx_free(xmlctx);
266 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100267
Michal Vasko63f3d842020-07-08 10:10:14 +0200268 /* invalid closing tag */
269 str = "<yin:element xmlns=\"urn\"></yin:element/>";
270 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
271 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
272 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100273 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
274 logbuf_assert("Invalid character sequence \"/>\", expected element tag termination ('>'). Line number 1.");
275 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200276 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200277
278 /* UTF8 characters */
279 str = "<𠜎€𠜎Øn:𠜎€𠜎Øn/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200280 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
281 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100282 assert_true(!strncmp("𠜎€𠜎Øn", xmlctx->name, xmlctx->name_len));
283 assert_true(!strncmp("𠜎€𠜎Øn", xmlctx->prefix, xmlctx->prefix_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200284
Michal Vaskob36053d2020-03-26 15:49:30 +0100285 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
286 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
287
288 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
289 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
290
291 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
292 assert_int_equal(LYXML_END, xmlctx->status);
293 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200294 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100295
296 /* invalid UTF-8 characters */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200297 str = "<¢:element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200298 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
299 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100300 logbuf_assert("Identifier \"¢:element>\" starts with an invalid character. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200301 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100302
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200303 str = "<yin:c⁐element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200304 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
305 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100306 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
307 logbuf_assert("Invalid character sequence \"⁐element>\", expected element tag end ('>' or '/>') or an attribute. Line number 1.");
308 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200309 ly_in_free(in, 0);
Radek Krejci339e2de2019-05-17 14:28:24 +0200310
311 /* mixed content */
312 str = "<a>text <b>x</b></a>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200313 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
314 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100315 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
316 assert_true(!strncmp("a", xmlctx->name, xmlctx->name_len));
317 assert_null(xmlctx->prefix);
Radek Krejci339e2de2019-05-17 14:28:24 +0200318
Michal Vaskob36053d2020-03-26 15:49:30 +0100319 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
320 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
321 assert_true(!strncmp("text ", xmlctx->value, xmlctx->value_len));
322
323 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
324 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
325 assert_true(!strncmp("b", xmlctx->name, xmlctx->name_len));
326 assert_null(xmlctx->prefix);
327
328 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
329 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
330 assert_true(!strncmp("x", xmlctx->value, xmlctx->value_len));
331
332 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
333 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
334
335 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
336 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
337
338 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
339 assert_int_equal(LYXML_END, xmlctx->status);
340 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200341 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100342
343 /* tag mismatch */
David Sedlák54a6f132019-07-16 15:14:18 +0200344 str = "<a>text</b>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200345 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
346 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100347 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
348 assert_true(!strncmp("a", xmlctx->name, xmlctx->name_len));
349 assert_null(xmlctx->prefix);
David Sedlák54a6f132019-07-16 15:14:18 +0200350
Michal Vaskob36053d2020-03-26 15:49:30 +0100351 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
352 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
353 assert_true(!strncmp("text", xmlctx->value, xmlctx->value_len));
354
355 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
356 logbuf_assert("Opening (\"a\") and closing (\"b\") elements tag mismatch. Line number 1.");
357 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200358 ly_in_free(in, 0);
Radek Krejcid972c252018-09-25 13:23:39 +0200359}
360
361static void
362test_attribute(void **state)
363{
Michal Vaskob36053d2020-03-26 15:49:30 +0100364 const char *str;
365 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200366 struct ly_in *in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100367 struct lyxml_ns *ns;
Radek Krejcid972c252018-09-25 13:23:39 +0200368
Radek Krejcid972c252018-09-25 13:23:39 +0200369 /* not an attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +0100370 str = "<e unknown/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200371 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
372 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Radek Krejcid972c252018-09-25 13:23:39 +0200373 logbuf_assert("Invalid character sequence \"/>\", expected '='. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200374 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100375
376 str = "<e xxx=/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200377 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
378 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Radek Krejcid972c252018-09-25 13:23:39 +0200379 logbuf_assert("Invalid character sequence \"/>\", expected either single or double quotation mark. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200380 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100381
382 str = "<e xxx\n = yyy/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200383 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
384 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Radek Krejcid972c252018-09-25 13:23:39 +0200385 logbuf_assert("Invalid character sequence \"yyy/>\", expected either single or double quotation mark. Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200386 ly_in_free(in, 0);
Radek Krejcid972c252018-09-25 13:23:39 +0200387
388 /* valid attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +0100389 str = "<e attr=\"val\"";
Michal Vasko63f3d842020-07-08 10:10:14 +0200390 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
391 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100392 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
393 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
394 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
395 assert_true(!strncmp("attr", xmlctx->name, xmlctx->name_len));
396 assert_null(xmlctx->prefix);
Radek Krejcid972c252018-09-25 13:23:39 +0200397
Michal Vaskob36053d2020-03-26 15:49:30 +0100398 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
399 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
400 assert_true(!strncmp("val", xmlctx->name, xmlctx->name_len));
401 assert_int_equal(xmlctx->ws_only, 0);
402 assert_int_equal(xmlctx->dynamic, 0);
403 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200404 ly_in_free(in, 0);
Radek Krejci28e8cb52019-03-08 11:31:31 +0100405
Michal Vaskob36053d2020-03-26 15:49:30 +0100406 /* valid namespace with prefix */
407 str = "<e xmlns:nc\n = \'urn\'/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200408 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
409 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100410 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
411 assert_int_equal(1, xmlctx->ns.count);
412 ns = (struct lyxml_ns *)xmlctx->ns.objs[0];
413 assert_string_equal(ns->prefix, "nc");
414 assert_string_equal(ns->uri, "urn");
415 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200416 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200417}
418
Radek Krejci7a7fa902018-09-25 17:08:21 +0200419static void
420test_text(void **state)
421{
Michal Vaskob36053d2020-03-26 15:49:30 +0100422 const char *str;
423 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200424 struct ly_in *in;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200425
426 /* empty attribute value */
Michal Vaskob36053d2020-03-26 15:49:30 +0100427 str = "<e a=\"\"";
Michal Vasko63f3d842020-07-08 10:10:14 +0200428 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
429 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100430 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
431 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
432 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Radek Krejcib1890642018-10-03 14:05:40 +0200433
Michal Vaskob36053d2020-03-26 15:49:30 +0100434 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
435 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
436 assert_true(!strncmp("", xmlctx->value, xmlctx->value_len));
437 assert_int_equal(xmlctx->ws_only, 1);
438 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200439 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100440
441 /* empty value but in single quotes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200442 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'\'", &in));
443 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100444 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100445 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
446 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
447 assert_true(!strncmp("", xmlctx->value, xmlctx->value_len));
448 assert_int_equal(xmlctx->ws_only, 1);
449 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200450 ly_in_free(in, 0);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200451
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200452 /* empty element content - only formating before defining child */
Michal Vasko63f3d842020-07-08 10:10:14 +0200453 assert_int_equal(LY_SUCCESS, ly_in_new_memory(">\n <y>", &in));
454 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100455 xmlctx->status = LYXML_ELEMENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100456 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
457 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
458 assert_true(!strncmp("\n ", xmlctx->value, xmlctx->value_len));
459 assert_int_equal(xmlctx->ws_only, 1);
460 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200461 ly_in_free(in, 0);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200462
Radek Krejci7a7fa902018-09-25 17:08:21 +0200463 /* empty element content is invalid - missing content terminating character < */
Michal Vasko63f3d842020-07-08 10:10:14 +0200464 assert_int_equal(LY_SUCCESS, ly_in_new_memory("", &in));
465 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100466 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100467 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200468 logbuf_assert("Unexpected end-of-input. Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200469 ly_in_free(in, 0);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200470
Michal Vasko63f3d842020-07-08 10:10:14 +0200471 assert_int_equal(LY_SUCCESS, ly_in_new_memory("xxx", &in));
472 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100473 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100474 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
475 logbuf_assert("Invalid character sequence \"xxx\", expected element tag start ('<'). Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200476 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100477
478 lyxml_ctx_free(xmlctx);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200479
Radek Krejci7a7fa902018-09-25 17:08:21 +0200480 /* valid strings */
Radek Krejci339e2de2019-05-17 14:28:24 +0200481 str = "<a>€𠜎Øn \n&lt;&amp;&quot;&apos;&gt; &#82;&#x4f;&#x4B;</a>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200482 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
483 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100484 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
485
486 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
487 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
488 assert_true(!strncmp("€𠜎Øn \n<&\"\'> ROK", xmlctx->value, xmlctx->value_len));
489 assert_int_equal(xmlctx->ws_only, 0);
490 assert_int_equal(xmlctx->dynamic, 1);
491 free((char *)xmlctx->value);
Michal Vasko63f3d842020-07-08 10:10:14 +0200492 ly_in_free(in, 0);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200493
Radek Krejci117d2082018-09-26 10:05:14 +0200494 /* test using n-bytes UTF8 hexadecimal code points */
Michal Vasko63f3d842020-07-08 10:10:14 +0200495 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x0024;&#x00A2;&#x20ac;&#x10348;\'", &in));
496 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100497 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100498 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
499 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
500 assert_true(!strncmp("$¢€𐍈", xmlctx->value, xmlctx->value_len));
501 assert_int_equal(xmlctx->ws_only, 0);
502 assert_int_equal(xmlctx->dynamic, 1);
503 free((char *)xmlctx->value);
Michal Vasko63f3d842020-07-08 10:10:14 +0200504 ly_in_free(in, 0);
Radek Krejcid70d1072018-10-09 14:20:47 +0200505
Radek Krejci7a7fa902018-09-25 17:08:21 +0200506 /* invalid characters in string */
Michal Vasko63f3d842020-07-08 10:10:14 +0200507 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x52\'", &in));
508 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100509 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100510 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
511 logbuf_assert("Invalid character sequence \"'\", expected ;. Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200512 ly_in_free(in, 0);
Radek Krejci117d2082018-09-26 10:05:14 +0200513
Michal Vasko63f3d842020-07-08 10:10:14 +0200514 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\"&#82\"", &in));
515 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100516 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100517 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
518 logbuf_assert("Invalid character sequence \"\"\", expected ;. Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200519 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100520
Michal Vasko63f3d842020-07-08 10:10:14 +0200521 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\"&nonsense;\"", &in));
522 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100523 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100524 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
525 logbuf_assert("Entity reference \"&nonsense;\" not supported, only predefined references allowed. Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200526 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100527
Michal Vasko63f3d842020-07-08 10:10:14 +0200528 assert_int_equal(LY_SUCCESS, ly_in_new_memory(">&#o122;", &in));
529 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100530 xmlctx->status = LYXML_ELEMENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100531 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
532 logbuf_assert("Invalid character reference \"&#o122;\". Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200533 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100534
Michal Vasko63f3d842020-07-08 10:10:14 +0200535 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x06;\'", &in));
536 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100537 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100538 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
539 logbuf_assert("Invalid character reference \"&#x06;\'\" (0x00000006). Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200540 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100541
Michal Vasko63f3d842020-07-08 10:10:14 +0200542 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#xfdd0;\'", &in));
543 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100544 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100545 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
546 logbuf_assert("Invalid character reference \"&#xfdd0;\'\" (0x0000fdd0). Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200547 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100548
Michal Vasko63f3d842020-07-08 10:10:14 +0200549 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#xffff;\'", &in));
550 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100551 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100552 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
553 logbuf_assert("Invalid character reference \"&#xffff;\'\" (0x0000ffff). Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200554 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100555
556 lyxml_ctx_free(xmlctx);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200557}
558
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200559static void
560test_ns(void **state)
561{
Michal Vaskob36053d2020-03-26 15:49:30 +0100562 const char *str;
563 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200564 struct ly_in *in;
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200565 const struct lyxml_ns *ns;
566
Michal Vaskob36053d2020-03-26 15:49:30 +0100567 /* opening element1 */
568 str = "<element1/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200569 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
570 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200571
Radek Krejcie0734d22019-04-05 15:54:28 +0200572 /* processing namespace definitions */
Michal Vaskob36053d2020-03-26 15:49:30 +0100573 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, NULL, 0, strdup("urn:default")));
574 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, "nc", 2, strdup("urn:nc1")));
Radek Krejcie0734d22019-04-05 15:54:28 +0200575 /* simulate adding open element2 into context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100576 xmlctx->elements.count++;
Radek Krejcie0734d22019-04-05 15:54:28 +0200577 /* processing namespace definitions */
Michal Vaskob36053d2020-03-26 15:49:30 +0100578 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, "nc", 2, strdup("urn:nc2")));
579 assert_int_equal(3, xmlctx->ns.count);
580 assert_int_not_equal(0, xmlctx->ns.size);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200581
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200582 ns = lyxml_ns_get(&xmlctx->ns, NULL, 0);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200583 assert_non_null(ns);
584 assert_null(ns->prefix);
585 assert_string_equal("urn:default", ns->uri);
586
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200587 ns = lyxml_ns_get(&xmlctx->ns, "nc", 2);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200588 assert_non_null(ns);
589 assert_string_equal("nc", ns->prefix);
590 assert_string_equal("urn:nc2", ns->uri);
591
Radek Krejcie0734d22019-04-05 15:54:28 +0200592 /* simulate closing element2 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100593 xmlctx->elements.count--;
594 lyxml_ns_rm(xmlctx);
595 assert_int_equal(2, xmlctx->ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200596
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200597 ns = lyxml_ns_get(&xmlctx->ns, "nc", 2);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200598 assert_non_null(ns);
599 assert_string_equal("nc", ns->prefix);
600 assert_string_equal("urn:nc1", ns->uri);
601
Michal Vaskob36053d2020-03-26 15:49:30 +0100602 /* close element1 */
603 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
604 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
605 assert_int_equal(0, xmlctx->ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200606
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200607 assert_null(lyxml_ns_get(&xmlctx->ns, "nc", 2));
608 assert_null(lyxml_ns_get(&xmlctx->ns, NULL, 0));
Michal Vaskob36053d2020-03-26 15:49:30 +0100609
610 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200611 ly_in_free(in, 0);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200612}
613
David Sedlákb3bed7a2019-03-08 11:53:37 +0100614static void
Radek Krejcie0734d22019-04-05 15:54:28 +0200615test_ns2(void **state)
616{
Michal Vaskob36053d2020-03-26 15:49:30 +0100617 const char *str;
618 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200619 struct ly_in *in;
Radek Krejcie0734d22019-04-05 15:54:28 +0200620
Michal Vaskob36053d2020-03-26 15:49:30 +0100621 /* opening element1 */
622 str = "<element1/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200623 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
624 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Radek Krejcie0734d22019-04-05 15:54:28 +0200625
Radek Krejcie0734d22019-04-05 15:54:28 +0200626 /* default namespace defined in parent element1 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100627 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, NULL, 0, strdup("urn:default")));
628 assert_int_equal(1, xmlctx->ns.count);
Radek Krejcie0734d22019-04-05 15:54:28 +0200629 /* going into child element1 */
Radek Krejcie0734d22019-04-05 15:54:28 +0200630 /* simulate adding open element1 into context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100631 xmlctx->elements.count++;
Radek Krejcie0734d22019-04-05 15:54:28 +0200632 /* no namespace defined, going out (first, simulate closing of so far open element) */
Michal Vaskob36053d2020-03-26 15:49:30 +0100633 xmlctx->elements.count--;
634 lyxml_ns_rm(xmlctx);
635 assert_int_equal(1, xmlctx->ns.count);
636
637 /* nothing else, going out of the parent element1 */
638 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
639 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
640 assert_int_equal(0, xmlctx->ns.count);
641
642 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200643 ly_in_free(in, 0);
Radek Krejcie0734d22019-04-05 15:54:28 +0200644}
645
Radek Krejcifad79c92019-06-04 11:43:30 +0200646static void
647test_simple_xml(void **state)
648{
Michal Vaskob36053d2020-03-26 15:49:30 +0100649 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200650 struct ly_in *in;
Radek Krejcie553e6d2019-06-07 15:33:18 +0200651 const char *test_input = "<elem1 attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>";
Radek Krejcifad79c92019-06-04 11:43:30 +0200652
Michal Vasko63f3d842020-07-08 10:10:14 +0200653 assert_int_equal(LY_SUCCESS, ly_in_new_memory(test_input, &in));
654 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100655 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200656 assert_string_equal(xmlctx->in->current, "attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200657
Michal Vaskob36053d2020-03-26 15:49:30 +0100658 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
659 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200660 assert_string_equal(xmlctx->in->current, "=\"value\"> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200661
Michal Vaskob36053d2020-03-26 15:49:30 +0100662 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
663 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200664 assert_string_equal(xmlctx->in->current, "> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200665
Michal Vaskob36053d2020-03-26 15:49:30 +0100666 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
667 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200668 assert_string_equal(xmlctx->in->current, "<elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200669
Michal Vaskob36053d2020-03-26 15:49:30 +0100670 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
671 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200672 assert_string_equal(xmlctx->in->current, "attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200673
Michal Vaskob36053d2020-03-26 15:49:30 +0100674 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
675 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200676 assert_string_equal(xmlctx->in->current, "=\"value\" /> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200677
Michal Vaskob36053d2020-03-26 15:49:30 +0100678 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
679 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200680 assert_string_equal(xmlctx->in->current, " /> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200681
Michal Vaskob36053d2020-03-26 15:49:30 +0100682 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
683 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200684 assert_string_equal(xmlctx->in->current, "/> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200685
Michal Vaskob36053d2020-03-26 15:49:30 +0100686 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
687 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200688 assert_string_equal(xmlctx->in->current, " </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200689
Michal Vaskob36053d2020-03-26 15:49:30 +0100690 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
691 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200692 assert_string_equal(xmlctx->in->current, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100693
694 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
695 assert_int_equal(LYXML_END, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200696 assert_string_equal(xmlctx->in->current, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100697
698 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200699 ly_in_free(in, 0);
Radek Krejcifad79c92019-06-04 11:43:30 +0200700}
701
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200702int main(void)
703{
704 const struct CMUnitTest tests[] = {
Radek Krejcid972c252018-09-25 13:23:39 +0200705 cmocka_unit_test_setup(test_element, logger_setup),
706 cmocka_unit_test_setup(test_attribute, logger_setup),
Radek Krejci7a7fa902018-09-25 17:08:21 +0200707 cmocka_unit_test_setup(test_text, logger_setup),
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200708 cmocka_unit_test_setup(test_ns, logger_setup),
Radek Krejcie0734d22019-04-05 15:54:28 +0200709 cmocka_unit_test_setup(test_ns2, logger_setup),
Radek Krejcifad79c92019-06-04 11:43:30 +0200710 cmocka_unit_test_setup(test_simple_xml, logger_setup),
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200711 };
712
Michal Vaskob36053d2020-03-26 15:49:30 +0100713 return cmocka_run_group_tests(tests, setup, teardown);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200714}