blob: 4d04b6380a887d0936be7a4af54dc14d97e3780c [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 <string.h>
20
Radek Krejcib4ac5a92020-11-23 17:54:33 +010021#include "utests.h"
22
Radek Krejci70593c12020-06-13 20:48:09 +020023#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020024#include "in_internal.h"
Michal Vasko63f3d842020-07-08 10:10:14 +020025#include "xml.h"
Radek Krejci2d7a47b2019-05-16 13:34:10 +020026
Michal Vaskob36053d2020-03-26 15:49:30 +010027LY_ERR lyxml_ns_add(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len, char *uri);
28LY_ERR lyxml_ns_rm(struct lyxml_ctx *xmlctx);
29
30static int
31setup(void **state)
32{
33 if (ly_ctx_new(NULL, 0, (struct ly_ctx **)state)) {
34 return 1;
35 }
36
37 return 0;
38}
39
40static int
41teardown(void **state)
42{
43 ly_ctx_destroy(*state, NULL);
44 return 0;
45}
Radek Krejcid91dbaf2018-09-21 15:51:39 +020046
47#define BUFSIZE 1024
48char logbuf[BUFSIZE] = {0};
49
50/* set to 0 to printing error messages to stderr instead of checking them in code */
51#define ENABLE_LOGGER_CHECKING 1
52
53static void
54logger(LY_LOG_LEVEL level, const char *msg, const char *path)
55{
56 (void) level; /* unused */
57
58 if (path) {
59 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
60 } else {
61 strncpy(logbuf, msg, BUFSIZE - 1);
62 }
63}
64
65static int
66logger_setup(void **state)
67{
68 (void) state; /* unused */
69#if ENABLE_LOGGER_CHECKING
70 ly_set_log_clb(logger, 1);
71#endif
72 return 0;
73}
74
75void
76logbuf_clean(void)
77{
78 logbuf[0] = '\0';
79}
80
81#if ENABLE_LOGGER_CHECKING
82# define logbuf_assert(str) assert_string_equal(logbuf, str)
83#else
84# define logbuf_assert(str)
85#endif
86
87static void
88test_element(void **state)
89{
Michal Vaskob36053d2020-03-26 15:49:30 +010090 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +020091 struct ly_in *in;
Michal Vaskob36053d2020-03-26 15:49:30 +010092 const char *str;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020093
94 /* empty */
95 str = "";
Michal Vasko63f3d842020-07-08 10:10:14 +020096 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
97 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +010098 assert_int_equal(LYXML_END, xmlctx->status);
Michal Vaskob36053d2020-03-26 15:49:30 +010099 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200100 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200101
Radek Krejcib1890642018-10-03 14:05:40 +0200102 /* end element */
103 str = "</element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200104 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
105 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100106 logbuf_assert("Stray closing element tag (\"element\"). Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200107 ly_in_free(in, 0);
Radek Krejcib1890642018-10-03 14:05:40 +0200108
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200109 /* no element */
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100110 // logbuf_clean();
Michal Vaskob36053d2020-03-26 15:49:30 +0100111 str = "no data present";
Michal Vasko63f3d842020-07-08 10:10:14 +0200112 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
113 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100114 logbuf_assert("Invalid character sequence \"no data present\", expected element tag start ('<'). Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200115 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200116
117 /* not supported DOCTYPE */
Michal Vaskob36053d2020-03-26 15:49:30 +0100118 str = "<!DOCTYPE greeting SYSTEM \"hello.dtd\"><greeting/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200119 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
120 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200121 logbuf_assert("Document Type Declaration not supported. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200122 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200123
Radek Krejcic5c31bb2019-04-08 14:40:52 +0200124 /* invalid XML */
Michal Vaskob36053d2020-03-26 15:49:30 +0100125 str = "<!NONSENSE/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200126 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
127 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100128 logbuf_assert("Unknown XML section \"<!NONSENSE/>\". Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200129 ly_in_free(in, 0);
Radek Krejcic5c31bb2019-04-08 14:40:52 +0200130
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200131 /* unqualified element */
132 str = " < element/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200133 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
134 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100135 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
136 assert_null(xmlctx->prefix);
137 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
138 assert_int_equal(1, xmlctx->elements.count);
Radek Krejci28e8cb52019-03-08 11:31:31 +0100139
Michal Vaskob36053d2020-03-26 15:49:30 +0100140 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
141 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
142 assert_true(!strncmp("", xmlctx->value, xmlctx->value_len));
143
144 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
145 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
146
147 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
148 assert_int_equal(LYXML_END, xmlctx->status);
149 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200150 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100151
152 /* element with attribute */
Radek Krejci28e8cb52019-03-08 11:31:31 +0100153 str = " < element attr=\'x\'/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200154 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
155 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100156 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
157 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
158 assert_null(xmlctx->prefix);
159 assert_int_equal(1, xmlctx->elements.count);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200160
Michal Vaskob36053d2020-03-26 15:49:30 +0100161 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
162 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
163 assert_true(!strncmp("attr", xmlctx->name, xmlctx->name_len));
164 assert_null(xmlctx->prefix);
165
166 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
167 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
168 assert_int_equal(1, xmlctx->elements.count);
169 assert_true(!strncmp("x", xmlctx->value, xmlctx->value_len));
170
171 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
172 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
173
174 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
175 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
176 assert_int_equal(0, xmlctx->elements.count);
177
178 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
179 assert_int_equal(LYXML_END, xmlctx->status);
180 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200181 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100182
183 /* headers and comments */
Radek Krejcifb7c6582018-09-21 16:12:45 +0200184 str = "<?xml version=\"1.0\"?> <!-- comment --> <![CDATA[<greeting>Hello, world!</greeting>]]> <?TEST xxx?> <element/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200185 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
186 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100187 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
188 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
189 assert_null(xmlctx->prefix);
190 assert_int_equal(1, xmlctx->elements.count);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200191
Michal Vaskob36053d2020-03-26 15:49:30 +0100192 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
193 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
194
195 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
196 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
197
198 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
199 assert_int_equal(LYXML_END, xmlctx->status);
200 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200201 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100202
203 /* separate opening and closing tags, neamespaced parsed internally */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200204 str = "<element xmlns=\"urn\"></element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200205 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
206 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100207 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
208 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
209 assert_null(xmlctx->prefix);
210 assert_int_equal(1, xmlctx->elements.count);
211 assert_int_equal(1, xmlctx->ns.count);
212
213 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
214 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
215
216 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
217 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
218 assert_int_equal(0, xmlctx->elements.count);
219 assert_int_equal(0, xmlctx->ns.count);
220
221 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
222 assert_int_equal(LYXML_END, xmlctx->status);
223 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200224 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200225
226 /* qualified element */
227 str = " < yin:element/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200228 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
229 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100230 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
231 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
232 assert_true(!strncmp("yin", xmlctx->prefix, xmlctx->prefix_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200233
Michal Vaskob36053d2020-03-26 15:49:30 +0100234 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
235 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
236
237 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
238 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
239
240 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
241 assert_int_equal(LYXML_END, xmlctx->status);
242 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200243 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100244
245 /* non-matching closing tag */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200246 str = "<yin:element xmlns=\"urn\"></element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200247 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
248 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100249 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
250 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
251 assert_true(!strncmp("yin", xmlctx->prefix, xmlctx->prefix_len));
252 assert_int_equal(1, xmlctx->elements.count);
253 assert_int_equal(1, xmlctx->ns.count);
254
255 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
256 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
257
258 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
259 logbuf_assert("Opening (\"yin:element\") and closing (\"element\") elements tag mismatch. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200260 lyxml_ctx_free(xmlctx);
261 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100262
Michal Vasko63f3d842020-07-08 10:10:14 +0200263 /* invalid closing tag */
264 str = "<yin:element xmlns=\"urn\"></yin:element/>";
265 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
266 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
267 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100268 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
269 logbuf_assert("Invalid character sequence \"/>\", expected element tag termination ('>'). Line number 1.");
270 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200271 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200272
273 /* UTF8 characters */
274 str = "<𠜎€𠜎Øn:𠜎€𠜎Øn/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200275 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
276 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100277 assert_true(!strncmp("𠜎€𠜎Øn", xmlctx->name, xmlctx->name_len));
278 assert_true(!strncmp("𠜎€𠜎Øn", xmlctx->prefix, xmlctx->prefix_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200279
Michal Vaskob36053d2020-03-26 15:49:30 +0100280 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
281 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
282
283 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
284 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
285
286 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
287 assert_int_equal(LYXML_END, xmlctx->status);
288 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200289 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100290
291 /* invalid UTF-8 characters */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200292 str = "<¢:element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200293 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
294 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100295 logbuf_assert("Identifier \"¢:element>\" starts with an invalid character. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200296 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100297
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200298 str = "<yin:c⁐element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200299 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
300 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100301 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
302 logbuf_assert("Invalid character sequence \"⁐element>\", expected element tag end ('>' or '/>') or an attribute. Line number 1.");
303 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200304 ly_in_free(in, 0);
Radek Krejci339e2de2019-05-17 14:28:24 +0200305
306 /* mixed content */
307 str = "<a>text <b>x</b></a>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200308 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
309 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100310 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
311 assert_true(!strncmp("a", xmlctx->name, xmlctx->name_len));
312 assert_null(xmlctx->prefix);
Radek Krejci339e2de2019-05-17 14:28:24 +0200313
Michal Vaskob36053d2020-03-26 15:49:30 +0100314 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
315 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
316 assert_true(!strncmp("text ", xmlctx->value, xmlctx->value_len));
317
318 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
319 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
320 assert_true(!strncmp("b", xmlctx->name, xmlctx->name_len));
321 assert_null(xmlctx->prefix);
322
323 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
324 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
325 assert_true(!strncmp("x", xmlctx->value, xmlctx->value_len));
326
327 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
328 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
329
330 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
331 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
332
333 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
334 assert_int_equal(LYXML_END, xmlctx->status);
335 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200336 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100337
338 /* tag mismatch */
David Sedlák54a6f132019-07-16 15:14:18 +0200339 str = "<a>text</b>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200340 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
341 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100342 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
343 assert_true(!strncmp("a", xmlctx->name, xmlctx->name_len));
344 assert_null(xmlctx->prefix);
David Sedlák54a6f132019-07-16 15:14:18 +0200345
Michal Vaskob36053d2020-03-26 15:49:30 +0100346 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
347 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
348 assert_true(!strncmp("text", xmlctx->value, xmlctx->value_len));
349
350 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
351 logbuf_assert("Opening (\"a\") and closing (\"b\") elements tag mismatch. Line number 1.");
352 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200353 ly_in_free(in, 0);
Radek Krejcid972c252018-09-25 13:23:39 +0200354}
355
356static void
357test_attribute(void **state)
358{
Michal Vaskob36053d2020-03-26 15:49:30 +0100359 const char *str;
360 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200361 struct ly_in *in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100362 struct lyxml_ns *ns;
Radek Krejcid972c252018-09-25 13:23:39 +0200363
Radek Krejcid972c252018-09-25 13:23:39 +0200364 /* not an attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +0100365 str = "<e unknown/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200366 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
367 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Radek Krejcid972c252018-09-25 13:23:39 +0200368 logbuf_assert("Invalid character sequence \"/>\", expected '='. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200369 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100370
371 str = "<e xxx=/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200372 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
373 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Radek Krejcid972c252018-09-25 13:23:39 +0200374 logbuf_assert("Invalid character sequence \"/>\", expected either single or double quotation mark. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200375 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100376
377 str = "<e xxx\n = yyy/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200378 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
379 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Radek Krejcid972c252018-09-25 13:23:39 +0200380 logbuf_assert("Invalid character sequence \"yyy/>\", expected either single or double quotation mark. Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200381 ly_in_free(in, 0);
Radek Krejcid972c252018-09-25 13:23:39 +0200382
383 /* valid attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +0100384 str = "<e attr=\"val\"";
Michal Vasko63f3d842020-07-08 10:10:14 +0200385 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
386 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100387 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
388 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
389 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
390 assert_true(!strncmp("attr", xmlctx->name, xmlctx->name_len));
391 assert_null(xmlctx->prefix);
Radek Krejcid972c252018-09-25 13:23:39 +0200392
Michal Vaskob36053d2020-03-26 15:49:30 +0100393 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
394 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
395 assert_true(!strncmp("val", xmlctx->name, xmlctx->name_len));
396 assert_int_equal(xmlctx->ws_only, 0);
397 assert_int_equal(xmlctx->dynamic, 0);
398 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200399 ly_in_free(in, 0);
Radek Krejci28e8cb52019-03-08 11:31:31 +0100400
Michal Vaskob36053d2020-03-26 15:49:30 +0100401 /* valid namespace with prefix */
402 str = "<e xmlns:nc\n = \'urn\'/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200403 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
404 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100405 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
406 assert_int_equal(1, xmlctx->ns.count);
407 ns = (struct lyxml_ns *)xmlctx->ns.objs[0];
408 assert_string_equal(ns->prefix, "nc");
409 assert_string_equal(ns->uri, "urn");
410 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200411 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200412}
413
Radek Krejci7a7fa902018-09-25 17:08:21 +0200414static void
415test_text(void **state)
416{
Michal Vaskob36053d2020-03-26 15:49:30 +0100417 const char *str;
418 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200419 struct ly_in *in;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200420
421 /* empty attribute value */
Michal Vaskob36053d2020-03-26 15:49:30 +0100422 str = "<e a=\"\"";
Michal Vasko63f3d842020-07-08 10:10:14 +0200423 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
424 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100425 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
426 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
427 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Radek Krejcib1890642018-10-03 14:05:40 +0200428
Michal Vaskob36053d2020-03-26 15:49:30 +0100429 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
430 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
431 assert_true(!strncmp("", xmlctx->value, xmlctx->value_len));
432 assert_int_equal(xmlctx->ws_only, 1);
433 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200434 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100435
436 /* empty value but in single quotes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200437 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'\'", &in));
438 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100439 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100440 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
441 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
442 assert_true(!strncmp("", xmlctx->value, xmlctx->value_len));
443 assert_int_equal(xmlctx->ws_only, 1);
444 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200445 ly_in_free(in, 0);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200446
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200447 /* empty element content - only formating before defining child */
Michal Vasko63f3d842020-07-08 10:10:14 +0200448 assert_int_equal(LY_SUCCESS, ly_in_new_memory(">\n <y>", &in));
449 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100450 xmlctx->status = LYXML_ELEMENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100451 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
452 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
453 assert_true(!strncmp("\n ", xmlctx->value, xmlctx->value_len));
454 assert_int_equal(xmlctx->ws_only, 1);
455 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200456 ly_in_free(in, 0);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200457
Radek Krejci7a7fa902018-09-25 17:08:21 +0200458 /* empty element content is invalid - missing content terminating character < */
Michal Vasko63f3d842020-07-08 10:10:14 +0200459 assert_int_equal(LY_SUCCESS, ly_in_new_memory("", &in));
460 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100461 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100462 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200463 logbuf_assert("Unexpected end-of-input. Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200464 ly_in_free(in, 0);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200465
Michal Vasko63f3d842020-07-08 10:10:14 +0200466 assert_int_equal(LY_SUCCESS, ly_in_new_memory("xxx", &in));
467 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100468 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100469 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
470 logbuf_assert("Invalid character sequence \"xxx\", expected element tag start ('<'). Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200471 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100472
473 lyxml_ctx_free(xmlctx);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200474
Radek Krejci7a7fa902018-09-25 17:08:21 +0200475 /* valid strings */
Radek Krejci339e2de2019-05-17 14:28:24 +0200476 str = "<a>€𠜎Øn \n&lt;&amp;&quot;&apos;&gt; &#82;&#x4f;&#x4B;</a>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200477 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
478 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100479 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
480
481 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
482 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
483 assert_true(!strncmp("€𠜎Øn \n<&\"\'> ROK", xmlctx->value, xmlctx->value_len));
484 assert_int_equal(xmlctx->ws_only, 0);
485 assert_int_equal(xmlctx->dynamic, 1);
486 free((char *)xmlctx->value);
Michal Vasko63f3d842020-07-08 10:10:14 +0200487 ly_in_free(in, 0);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200488
Radek Krejci117d2082018-09-26 10:05:14 +0200489 /* test using n-bytes UTF8 hexadecimal code points */
Michal Vasko63f3d842020-07-08 10:10:14 +0200490 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x0024;&#x00A2;&#x20ac;&#x10348;\'", &in));
491 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100492 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100493 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
494 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
495 assert_true(!strncmp("$¢€𐍈", xmlctx->value, xmlctx->value_len));
496 assert_int_equal(xmlctx->ws_only, 0);
497 assert_int_equal(xmlctx->dynamic, 1);
498 free((char *)xmlctx->value);
Michal Vasko63f3d842020-07-08 10:10:14 +0200499 ly_in_free(in, 0);
Radek Krejcid70d1072018-10-09 14:20:47 +0200500
Radek Krejci7a7fa902018-09-25 17:08:21 +0200501 /* invalid characters in string */
Michal Vasko63f3d842020-07-08 10:10:14 +0200502 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x52\'", &in));
503 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100504 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100505 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
506 logbuf_assert("Invalid character sequence \"'\", expected ;. Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200507 ly_in_free(in, 0);
Radek Krejci117d2082018-09-26 10:05:14 +0200508
Michal Vasko63f3d842020-07-08 10:10:14 +0200509 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\"&#82\"", &in));
510 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100511 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100512 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
513 logbuf_assert("Invalid character sequence \"\"\", expected ;. Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200514 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100515
Michal Vasko63f3d842020-07-08 10:10:14 +0200516 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\"&nonsense;\"", &in));
517 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100518 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100519 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
520 logbuf_assert("Entity reference \"&nonsense;\" not supported, only predefined references allowed. Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200521 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100522
Michal Vasko63f3d842020-07-08 10:10:14 +0200523 assert_int_equal(LY_SUCCESS, ly_in_new_memory(">&#o122;", &in));
524 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100525 xmlctx->status = LYXML_ELEMENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100526 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
527 logbuf_assert("Invalid character reference \"&#o122;\". Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200528 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100529
Michal Vasko63f3d842020-07-08 10:10:14 +0200530 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x06;\'", &in));
531 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100532 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100533 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
534 logbuf_assert("Invalid character reference \"&#x06;\'\" (0x00000006). Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200535 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100536
Michal Vasko63f3d842020-07-08 10:10:14 +0200537 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#xfdd0;\'", &in));
538 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100539 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100540 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
541 logbuf_assert("Invalid character reference \"&#xfdd0;\'\" (0x0000fdd0). Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200542 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100543
Michal Vasko63f3d842020-07-08 10:10:14 +0200544 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#xffff;\'", &in));
545 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100546 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100547 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
548 logbuf_assert("Invalid character reference \"&#xffff;\'\" (0x0000ffff). Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200549 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100550
551 lyxml_ctx_free(xmlctx);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200552}
553
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200554static void
555test_ns(void **state)
556{
Michal Vaskob36053d2020-03-26 15:49:30 +0100557 const char *str;
558 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200559 struct ly_in *in;
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200560 const struct lyxml_ns *ns;
561
Michal Vaskob36053d2020-03-26 15:49:30 +0100562 /* opening element1 */
563 str = "<element1/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200564 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
565 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200566
Radek Krejcie0734d22019-04-05 15:54:28 +0200567 /* processing namespace definitions */
Michal Vaskob36053d2020-03-26 15:49:30 +0100568 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, NULL, 0, strdup("urn:default")));
569 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, "nc", 2, strdup("urn:nc1")));
Radek Krejcie0734d22019-04-05 15:54:28 +0200570 /* simulate adding open element2 into context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100571 xmlctx->elements.count++;
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, "nc", 2, strdup("urn:nc2")));
574 assert_int_equal(3, xmlctx->ns.count);
575 assert_int_not_equal(0, xmlctx->ns.size);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200576
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200577 ns = lyxml_ns_get(&xmlctx->ns, NULL, 0);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200578 assert_non_null(ns);
579 assert_null(ns->prefix);
580 assert_string_equal("urn:default", ns->uri);
581
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200582 ns = lyxml_ns_get(&xmlctx->ns, "nc", 2);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200583 assert_non_null(ns);
584 assert_string_equal("nc", ns->prefix);
585 assert_string_equal("urn:nc2", ns->uri);
586
Radek Krejcie0734d22019-04-05 15:54:28 +0200587 /* simulate closing element2 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100588 xmlctx->elements.count--;
589 lyxml_ns_rm(xmlctx);
590 assert_int_equal(2, xmlctx->ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200591
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200592 ns = lyxml_ns_get(&xmlctx->ns, "nc", 2);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200593 assert_non_null(ns);
594 assert_string_equal("nc", ns->prefix);
595 assert_string_equal("urn:nc1", ns->uri);
596
Michal Vaskob36053d2020-03-26 15:49:30 +0100597 /* close element1 */
598 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
599 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
600 assert_int_equal(0, xmlctx->ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200601
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200602 assert_null(lyxml_ns_get(&xmlctx->ns, "nc", 2));
603 assert_null(lyxml_ns_get(&xmlctx->ns, NULL, 0));
Michal Vaskob36053d2020-03-26 15:49:30 +0100604
605 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200606 ly_in_free(in, 0);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200607}
608
David Sedlákb3bed7a2019-03-08 11:53:37 +0100609static void
Radek Krejcie0734d22019-04-05 15:54:28 +0200610test_ns2(void **state)
611{
Michal Vaskob36053d2020-03-26 15:49:30 +0100612 const char *str;
613 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200614 struct ly_in *in;
Radek Krejcie0734d22019-04-05 15:54:28 +0200615
Michal Vaskob36053d2020-03-26 15:49:30 +0100616 /* opening element1 */
617 str = "<element1/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200618 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
619 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Radek Krejcie0734d22019-04-05 15:54:28 +0200620
Radek Krejcie0734d22019-04-05 15:54:28 +0200621 /* default namespace defined in parent element1 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100622 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, NULL, 0, strdup("urn:default")));
623 assert_int_equal(1, xmlctx->ns.count);
Radek Krejcie0734d22019-04-05 15:54:28 +0200624 /* going into child element1 */
Radek Krejcie0734d22019-04-05 15:54:28 +0200625 /* simulate adding open element1 into context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100626 xmlctx->elements.count++;
Radek Krejcie0734d22019-04-05 15:54:28 +0200627 /* no namespace defined, going out (first, simulate closing of so far open element) */
Michal Vaskob36053d2020-03-26 15:49:30 +0100628 xmlctx->elements.count--;
629 lyxml_ns_rm(xmlctx);
630 assert_int_equal(1, xmlctx->ns.count);
631
632 /* nothing else, going out of the parent element1 */
633 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
634 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
635 assert_int_equal(0, xmlctx->ns.count);
636
637 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200638 ly_in_free(in, 0);
Radek Krejcie0734d22019-04-05 15:54:28 +0200639}
640
Radek Krejcifad79c92019-06-04 11:43:30 +0200641static void
642test_simple_xml(void **state)
643{
Michal Vaskob36053d2020-03-26 15:49:30 +0100644 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200645 struct ly_in *in;
Radek Krejcie553e6d2019-06-07 15:33:18 +0200646 const char *test_input = "<elem1 attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>";
Radek Krejcifad79c92019-06-04 11:43:30 +0200647
Michal Vasko63f3d842020-07-08 10:10:14 +0200648 assert_int_equal(LY_SUCCESS, ly_in_new_memory(test_input, &in));
649 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100650 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200651 assert_string_equal(xmlctx->in->current, "attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200652
Michal Vaskob36053d2020-03-26 15:49:30 +0100653 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
654 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200655 assert_string_equal(xmlctx->in->current, "=\"value\"> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200656
Michal Vaskob36053d2020-03-26 15:49:30 +0100657 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
658 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200659 assert_string_equal(xmlctx->in->current, "> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200660
Michal Vaskob36053d2020-03-26 15:49:30 +0100661 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
662 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200663 assert_string_equal(xmlctx->in->current, "<elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200664
Michal Vaskob36053d2020-03-26 15:49:30 +0100665 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
666 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200667 assert_string_equal(xmlctx->in->current, "attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200668
Michal Vaskob36053d2020-03-26 15:49:30 +0100669 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
670 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200671 assert_string_equal(xmlctx->in->current, "=\"value\" /> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200672
Michal Vaskob36053d2020-03-26 15:49:30 +0100673 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
674 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200675 assert_string_equal(xmlctx->in->current, " /> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200676
Michal Vaskob36053d2020-03-26 15:49:30 +0100677 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
678 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200679 assert_string_equal(xmlctx->in->current, "/> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200680
Michal Vaskob36053d2020-03-26 15:49:30 +0100681 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
682 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200683 assert_string_equal(xmlctx->in->current, " </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200684
Michal Vaskob36053d2020-03-26 15:49:30 +0100685 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
686 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200687 assert_string_equal(xmlctx->in->current, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100688
689 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
690 assert_int_equal(LYXML_END, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200691 assert_string_equal(xmlctx->in->current, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100692
693 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200694 ly_in_free(in, 0);
Radek Krejcifad79c92019-06-04 11:43:30 +0200695}
696
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100697int
698main(void)
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200699{
700 const struct CMUnitTest tests[] = {
Radek Krejcid972c252018-09-25 13:23:39 +0200701 cmocka_unit_test_setup(test_element, logger_setup),
702 cmocka_unit_test_setup(test_attribute, logger_setup),
Radek Krejci7a7fa902018-09-25 17:08:21 +0200703 cmocka_unit_test_setup(test_text, logger_setup),
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200704 cmocka_unit_test_setup(test_ns, logger_setup),
Radek Krejcie0734d22019-04-05 15:54:28 +0200705 cmocka_unit_test_setup(test_ns2, logger_setup),
Radek Krejcifad79c92019-06-04 11:43:30 +0200706 cmocka_unit_test_setup(test_simple_xml, logger_setup),
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200707 };
708
Michal Vaskob36053d2020-03-26 15:49:30 +0100709 return cmocka_run_group_tests(tests, setup, teardown);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200710}