blob: 89a8ce8b831c828b99ce0757177814ac0dbc1f8a [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 Krejcib7db73a2018-10-24 14:18:40 +020017
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 Krejci70593c12020-06-13 20:48:09 +020027#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020028#include "in_internal.h"
Michal Vasko63f3d842020-07-08 10:10:14 +020029#include "xml.h"
Radek Krejci2d7a47b2019-05-16 13:34:10 +020030
Michal Vaskob36053d2020-03-26 15:49:30 +010031LY_ERR lyxml_ns_add(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len, char *uri);
32LY_ERR lyxml_ns_rm(struct lyxml_ctx *xmlctx);
33
34static int
35setup(void **state)
36{
37 if (ly_ctx_new(NULL, 0, (struct ly_ctx **)state)) {
38 return 1;
39 }
40
41 return 0;
42}
43
44static int
45teardown(void **state)
46{
47 ly_ctx_destroy(*state, NULL);
48 return 0;
49}
Radek Krejcid91dbaf2018-09-21 15:51:39 +020050
51#define BUFSIZE 1024
52char logbuf[BUFSIZE] = {0};
53
54/* set to 0 to printing error messages to stderr instead of checking them in code */
55#define ENABLE_LOGGER_CHECKING 1
56
57static void
58logger(LY_LOG_LEVEL level, const char *msg, const char *path)
59{
60 (void) level; /* unused */
61
62 if (path) {
63 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
64 } else {
65 strncpy(logbuf, msg, BUFSIZE - 1);
66 }
67}
68
69static int
70logger_setup(void **state)
71{
72 (void) state; /* unused */
73#if ENABLE_LOGGER_CHECKING
74 ly_set_log_clb(logger, 1);
75#endif
76 return 0;
77}
78
79void
80logbuf_clean(void)
81{
82 logbuf[0] = '\0';
83}
84
85#if ENABLE_LOGGER_CHECKING
86# define logbuf_assert(str) assert_string_equal(logbuf, str)
87#else
88# define logbuf_assert(str)
89#endif
90
91static void
92test_element(void **state)
93{
Michal Vaskob36053d2020-03-26 15:49:30 +010094 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +020095 struct ly_in *in;
Michal Vaskob36053d2020-03-26 15:49:30 +010096 const char *str;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020097
98 /* empty */
99 str = "";
Michal Vasko63f3d842020-07-08 10:10:14 +0200100 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
101 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100102 assert_int_equal(LYXML_END, xmlctx->status);
Michal Vaskob36053d2020-03-26 15:49:30 +0100103 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200104 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200105
Radek Krejcib1890642018-10-03 14:05:40 +0200106 /* end element */
107 str = "</element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200108 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
109 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100110 logbuf_assert("Stray closing element tag (\"element\"). Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200111 ly_in_free(in, 0);
Radek Krejcib1890642018-10-03 14:05:40 +0200112
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200113 /* no element */
Michal Vaskob36053d2020-03-26 15:49:30 +0100114 //logbuf_clean();
115 str = "no data present";
Michal Vasko63f3d842020-07-08 10:10:14 +0200116 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
117 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100118 logbuf_assert("Invalid character sequence \"no data present\", expected element tag start ('<'). Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200119 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200120
121 /* not supported DOCTYPE */
Michal Vaskob36053d2020-03-26 15:49:30 +0100122 str = "<!DOCTYPE greeting SYSTEM \"hello.dtd\"><greeting/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200123 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
124 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200125 logbuf_assert("Document Type Declaration not supported. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200126 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200127
Radek Krejcic5c31bb2019-04-08 14:40:52 +0200128 /* invalid XML */
Michal Vaskob36053d2020-03-26 15:49:30 +0100129 str = "<!NONSENSE/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200130 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
131 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100132 logbuf_assert("Unknown XML section \"<!NONSENSE/>\". Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200133 ly_in_free(in, 0);
Radek Krejcic5c31bb2019-04-08 14:40:52 +0200134
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200135 /* unqualified element */
136 str = " < element/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200137 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
138 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100139 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
140 assert_null(xmlctx->prefix);
141 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
142 assert_int_equal(1, xmlctx->elements.count);
Radek Krejci28e8cb52019-03-08 11:31:31 +0100143
Michal Vaskob36053d2020-03-26 15:49:30 +0100144 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
145 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
146 assert_true(!strncmp("", xmlctx->value, xmlctx->value_len));
147
148 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
149 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
150
151 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
152 assert_int_equal(LYXML_END, xmlctx->status);
153 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200154 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100155
156 /* element with attribute */
Radek Krejci28e8cb52019-03-08 11:31:31 +0100157 str = " < element attr=\'x\'/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200158 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
159 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100160 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
161 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
162 assert_null(xmlctx->prefix);
163 assert_int_equal(1, xmlctx->elements.count);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200164
Michal Vaskob36053d2020-03-26 15:49:30 +0100165 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
166 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
167 assert_true(!strncmp("attr", xmlctx->name, xmlctx->name_len));
168 assert_null(xmlctx->prefix);
169
170 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
171 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
172 assert_int_equal(1, xmlctx->elements.count);
173 assert_true(!strncmp("x", xmlctx->value, xmlctx->value_len));
174
175 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
176 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
177
178 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
179 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
180 assert_int_equal(0, xmlctx->elements.count);
181
182 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
183 assert_int_equal(LYXML_END, xmlctx->status);
184 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200185 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100186
187 /* headers and comments */
Radek Krejcifb7c6582018-09-21 16:12:45 +0200188 str = "<?xml version=\"1.0\"?> <!-- comment --> <![CDATA[<greeting>Hello, world!</greeting>]]> <?TEST xxx?> <element/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200189 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
190 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100191 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
192 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
193 assert_null(xmlctx->prefix);
194 assert_int_equal(1, xmlctx->elements.count);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200195
Michal Vaskob36053d2020-03-26 15:49:30 +0100196 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
197 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
198
199 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
200 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
201
202 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
203 assert_int_equal(LYXML_END, xmlctx->status);
204 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200205 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100206
207 /* separate opening and closing tags, neamespaced parsed internally */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200208 str = "<element xmlns=\"urn\"></element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200209 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
210 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100211 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
212 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
213 assert_null(xmlctx->prefix);
214 assert_int_equal(1, xmlctx->elements.count);
215 assert_int_equal(1, xmlctx->ns.count);
216
217 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
218 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
219
220 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
221 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
222 assert_int_equal(0, xmlctx->elements.count);
223 assert_int_equal(0, xmlctx->ns.count);
224
225 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
226 assert_int_equal(LYXML_END, xmlctx->status);
227 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200228 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200229
230 /* qualified element */
231 str = " < yin:element/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200232 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
233 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100234 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
235 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
236 assert_true(!strncmp("yin", xmlctx->prefix, xmlctx->prefix_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200237
Michal Vaskob36053d2020-03-26 15:49:30 +0100238 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
239 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
240
241 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
242 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
243
244 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
245 assert_int_equal(LYXML_END, xmlctx->status);
246 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200247 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100248
249 /* non-matching closing tag */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200250 str = "<yin:element xmlns=\"urn\"></element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200251 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
252 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100253 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
254 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
255 assert_true(!strncmp("yin", xmlctx->prefix, xmlctx->prefix_len));
256 assert_int_equal(1, xmlctx->elements.count);
257 assert_int_equal(1, xmlctx->ns.count);
258
259 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
260 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
261
262 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
263 logbuf_assert("Opening (\"yin:element\") and closing (\"element\") elements tag mismatch. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200264 lyxml_ctx_free(xmlctx);
265 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100266
Michal Vasko63f3d842020-07-08 10:10:14 +0200267 /* invalid closing tag */
268 str = "<yin:element xmlns=\"urn\"></yin:element/>";
269 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
270 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
271 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100272 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
273 logbuf_assert("Invalid character sequence \"/>\", expected element tag termination ('>'). Line number 1.");
274 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200275 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200276
277 /* UTF8 characters */
278 str = "<𠜎€𠜎Øn:𠜎€𠜎Øn/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200279 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
280 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100281 assert_true(!strncmp("𠜎€𠜎Øn", xmlctx->name, xmlctx->name_len));
282 assert_true(!strncmp("𠜎€𠜎Øn", xmlctx->prefix, xmlctx->prefix_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200283
Michal Vaskob36053d2020-03-26 15:49:30 +0100284 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
285 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
286
287 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
288 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
289
290 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
291 assert_int_equal(LYXML_END, xmlctx->status);
292 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200293 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100294
295 /* invalid UTF-8 characters */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200296 str = "<¢:element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200297 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
298 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100299 logbuf_assert("Identifier \"¢:element>\" starts with an invalid character. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200300 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100301
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200302 str = "<yin:c⁐element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200303 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
304 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100305 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
306 logbuf_assert("Invalid character sequence \"⁐element>\", expected element tag end ('>' or '/>') or an attribute. Line number 1.");
307 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200308 ly_in_free(in, 0);
Radek Krejci339e2de2019-05-17 14:28:24 +0200309
310 /* mixed content */
311 str = "<a>text <b>x</b></a>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200312 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
313 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100314 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
315 assert_true(!strncmp("a", xmlctx->name, xmlctx->name_len));
316 assert_null(xmlctx->prefix);
Radek Krejci339e2de2019-05-17 14:28:24 +0200317
Michal Vaskob36053d2020-03-26 15:49:30 +0100318 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
319 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
320 assert_true(!strncmp("text ", xmlctx->value, xmlctx->value_len));
321
322 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
323 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
324 assert_true(!strncmp("b", xmlctx->name, xmlctx->name_len));
325 assert_null(xmlctx->prefix);
326
327 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
328 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
329 assert_true(!strncmp("x", xmlctx->value, xmlctx->value_len));
330
331 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
332 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
333
334 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
335 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
336
337 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
338 assert_int_equal(LYXML_END, xmlctx->status);
339 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200340 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100341
342 /* tag mismatch */
David Sedlák54a6f132019-07-16 15:14:18 +0200343 str = "<a>text</b>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200344 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
345 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100346 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
347 assert_true(!strncmp("a", xmlctx->name, xmlctx->name_len));
348 assert_null(xmlctx->prefix);
David Sedlák54a6f132019-07-16 15:14:18 +0200349
Michal Vaskob36053d2020-03-26 15:49:30 +0100350 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
351 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
352 assert_true(!strncmp("text", xmlctx->value, xmlctx->value_len));
353
354 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
355 logbuf_assert("Opening (\"a\") and closing (\"b\") elements tag mismatch. Line number 1.");
356 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200357 ly_in_free(in, 0);
Radek Krejcid972c252018-09-25 13:23:39 +0200358}
359
360static void
361test_attribute(void **state)
362{
Michal Vaskob36053d2020-03-26 15:49:30 +0100363 const char *str;
364 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200365 struct ly_in *in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100366 struct lyxml_ns *ns;
Radek Krejcid972c252018-09-25 13:23:39 +0200367
Radek Krejcid972c252018-09-25 13:23:39 +0200368 /* not an attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +0100369 str = "<e unknown/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200370 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
371 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Radek Krejcid972c252018-09-25 13:23:39 +0200372 logbuf_assert("Invalid character sequence \"/>\", expected '='. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200373 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100374
375 str = "<e xxx=/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200376 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
377 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Radek Krejcid972c252018-09-25 13:23:39 +0200378 logbuf_assert("Invalid character sequence \"/>\", expected either single or double quotation mark. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200379 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100380
381 str = "<e xxx\n = yyy/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200382 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
383 assert_int_equal(LY_EVALID, lyxml_ctx_new(*state, in, &xmlctx));
Radek Krejcid972c252018-09-25 13:23:39 +0200384 logbuf_assert("Invalid character sequence \"yyy/>\", expected either single or double quotation mark. Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200385 ly_in_free(in, 0);
Radek Krejcid972c252018-09-25 13:23:39 +0200386
387 /* valid attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +0100388 str = "<e attr=\"val\"";
Michal Vasko63f3d842020-07-08 10:10:14 +0200389 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
390 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100391 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
392 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
393 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
394 assert_true(!strncmp("attr", xmlctx->name, xmlctx->name_len));
395 assert_null(xmlctx->prefix);
Radek Krejcid972c252018-09-25 13:23:39 +0200396
Michal Vaskob36053d2020-03-26 15:49:30 +0100397 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
398 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
399 assert_true(!strncmp("val", xmlctx->name, xmlctx->name_len));
400 assert_int_equal(xmlctx->ws_only, 0);
401 assert_int_equal(xmlctx->dynamic, 0);
402 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200403 ly_in_free(in, 0);
Radek Krejci28e8cb52019-03-08 11:31:31 +0100404
Michal Vaskob36053d2020-03-26 15:49:30 +0100405 /* valid namespace with prefix */
406 str = "<e xmlns:nc\n = \'urn\'/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200407 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
408 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100409 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
410 assert_int_equal(1, xmlctx->ns.count);
411 ns = (struct lyxml_ns *)xmlctx->ns.objs[0];
412 assert_string_equal(ns->prefix, "nc");
413 assert_string_equal(ns->uri, "urn");
414 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200415 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200416}
417
Radek Krejci7a7fa902018-09-25 17:08:21 +0200418static void
419test_text(void **state)
420{
Michal Vaskob36053d2020-03-26 15:49:30 +0100421 const char *str;
422 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200423 struct ly_in *in;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200424
425 /* empty attribute value */
Michal Vaskob36053d2020-03-26 15:49:30 +0100426 str = "<e a=\"\"";
Michal Vasko63f3d842020-07-08 10:10:14 +0200427 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
428 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100429 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
430 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
431 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Radek Krejcib1890642018-10-03 14:05:40 +0200432
Michal Vaskob36053d2020-03-26 15:49:30 +0100433 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
434 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
435 assert_true(!strncmp("", xmlctx->value, xmlctx->value_len));
436 assert_int_equal(xmlctx->ws_only, 1);
437 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200438 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100439
440 /* empty value but in single quotes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200441 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'\'", &in));
442 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100443 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100444 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
445 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
446 assert_true(!strncmp("", xmlctx->value, xmlctx->value_len));
447 assert_int_equal(xmlctx->ws_only, 1);
448 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200449 ly_in_free(in, 0);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200450
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200451 /* empty element content - only formating before defining child */
Michal Vasko63f3d842020-07-08 10:10:14 +0200452 assert_int_equal(LY_SUCCESS, ly_in_new_memory(">\n <y>", &in));
453 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100454 xmlctx->status = LYXML_ELEMENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100455 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
456 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
457 assert_true(!strncmp("\n ", xmlctx->value, xmlctx->value_len));
458 assert_int_equal(xmlctx->ws_only, 1);
459 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200460 ly_in_free(in, 0);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200461
Radek Krejci7a7fa902018-09-25 17:08:21 +0200462 /* empty element content is invalid - missing content terminating character < */
Michal Vasko63f3d842020-07-08 10:10:14 +0200463 assert_int_equal(LY_SUCCESS, ly_in_new_memory("", &in));
464 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100465 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100466 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200467 logbuf_assert("Unexpected end-of-input. Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200468 ly_in_free(in, 0);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200469
Michal Vasko63f3d842020-07-08 10:10:14 +0200470 assert_int_equal(LY_SUCCESS, ly_in_new_memory("xxx", &in));
471 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100472 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100473 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
474 logbuf_assert("Invalid character sequence \"xxx\", expected element tag start ('<'). Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200475 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100476
477 lyxml_ctx_free(xmlctx);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200478
Radek Krejci7a7fa902018-09-25 17:08:21 +0200479 /* valid strings */
Radek Krejci339e2de2019-05-17 14:28:24 +0200480 str = "<a>€𠜎Øn \n&lt;&amp;&quot;&apos;&gt; &#82;&#x4f;&#x4B;</a>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200481 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
482 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100483 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
484
485 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
486 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
487 assert_true(!strncmp("€𠜎Øn \n<&\"\'> ROK", xmlctx->value, xmlctx->value_len));
488 assert_int_equal(xmlctx->ws_only, 0);
489 assert_int_equal(xmlctx->dynamic, 1);
490 free((char *)xmlctx->value);
Michal Vasko63f3d842020-07-08 10:10:14 +0200491 ly_in_free(in, 0);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200492
Radek Krejci117d2082018-09-26 10:05:14 +0200493 /* test using n-bytes UTF8 hexadecimal code points */
Michal Vasko63f3d842020-07-08 10:10:14 +0200494 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x0024;&#x00A2;&#x20ac;&#x10348;\'", &in));
495 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100496 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100497 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
498 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
499 assert_true(!strncmp("$¢€𐍈", xmlctx->value, xmlctx->value_len));
500 assert_int_equal(xmlctx->ws_only, 0);
501 assert_int_equal(xmlctx->dynamic, 1);
502 free((char *)xmlctx->value);
Michal Vasko63f3d842020-07-08 10:10:14 +0200503 ly_in_free(in, 0);
Radek Krejcid70d1072018-10-09 14:20:47 +0200504
Radek Krejci7a7fa902018-09-25 17:08:21 +0200505 /* invalid characters in string */
Michal Vasko63f3d842020-07-08 10:10:14 +0200506 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x52\'", &in));
507 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100508 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100509 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
510 logbuf_assert("Invalid character sequence \"'\", expected ;. Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200511 ly_in_free(in, 0);
Radek Krejci117d2082018-09-26 10:05:14 +0200512
Michal Vasko63f3d842020-07-08 10:10:14 +0200513 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\"&#82\"", &in));
514 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100515 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100516 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
517 logbuf_assert("Invalid character sequence \"\"\", expected ;. Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200518 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100519
Michal Vasko63f3d842020-07-08 10:10:14 +0200520 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\"&nonsense;\"", &in));
521 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100522 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100523 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
524 logbuf_assert("Entity reference \"&nonsense;\" not supported, only predefined references allowed. Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200525 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100526
Michal Vasko63f3d842020-07-08 10:10:14 +0200527 assert_int_equal(LY_SUCCESS, ly_in_new_memory(">&#o122;", &in));
528 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100529 xmlctx->status = LYXML_ELEMENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100530 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
531 logbuf_assert("Invalid character reference \"&#o122;\". Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200532 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100533
Michal Vasko63f3d842020-07-08 10:10:14 +0200534 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x06;\'", &in));
535 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100536 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100537 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
538 logbuf_assert("Invalid character reference \"&#x06;\'\" (0x00000006). Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200539 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100540
Michal Vasko63f3d842020-07-08 10:10:14 +0200541 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#xfdd0;\'", &in));
542 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100543 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100544 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
545 logbuf_assert("Invalid character reference \"&#xfdd0;\'\" (0x0000fdd0). Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200546 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100547
Michal Vasko63f3d842020-07-08 10:10:14 +0200548 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#xffff;\'", &in));
549 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100550 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100551 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
552 logbuf_assert("Invalid character reference \"&#xffff;\'\" (0x0000ffff). Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200553 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100554
555 lyxml_ctx_free(xmlctx);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200556}
557
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200558static void
559test_ns(void **state)
560{
Michal Vaskob36053d2020-03-26 15:49:30 +0100561 const char *str;
562 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200563 struct ly_in *in;
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200564 const struct lyxml_ns *ns;
565
Michal Vaskob36053d2020-03-26 15:49:30 +0100566 /* opening element1 */
567 str = "<element1/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200568 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
569 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200570
Radek Krejcie0734d22019-04-05 15:54:28 +0200571 /* processing namespace definitions */
Michal Vaskob36053d2020-03-26 15:49:30 +0100572 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, NULL, 0, strdup("urn:default")));
573 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, "nc", 2, strdup("urn:nc1")));
Radek Krejcie0734d22019-04-05 15:54:28 +0200574 /* simulate adding open element2 into context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100575 xmlctx->elements.count++;
Radek Krejcie0734d22019-04-05 15:54:28 +0200576 /* processing namespace definitions */
Michal Vaskob36053d2020-03-26 15:49:30 +0100577 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, "nc", 2, strdup("urn:nc2")));
578 assert_int_equal(3, xmlctx->ns.count);
579 assert_int_not_equal(0, xmlctx->ns.size);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200580
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200581 ns = lyxml_ns_get(&xmlctx->ns, NULL, 0);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200582 assert_non_null(ns);
583 assert_null(ns->prefix);
584 assert_string_equal("urn:default", ns->uri);
585
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200586 ns = lyxml_ns_get(&xmlctx->ns, "nc", 2);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200587 assert_non_null(ns);
588 assert_string_equal("nc", ns->prefix);
589 assert_string_equal("urn:nc2", ns->uri);
590
Radek Krejcie0734d22019-04-05 15:54:28 +0200591 /* simulate closing element2 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100592 xmlctx->elements.count--;
593 lyxml_ns_rm(xmlctx);
594 assert_int_equal(2, xmlctx->ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200595
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200596 ns = lyxml_ns_get(&xmlctx->ns, "nc", 2);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200597 assert_non_null(ns);
598 assert_string_equal("nc", ns->prefix);
599 assert_string_equal("urn:nc1", ns->uri);
600
Michal Vaskob36053d2020-03-26 15:49:30 +0100601 /* close element1 */
602 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
603 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
604 assert_int_equal(0, xmlctx->ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200605
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200606 assert_null(lyxml_ns_get(&xmlctx->ns, "nc", 2));
607 assert_null(lyxml_ns_get(&xmlctx->ns, NULL, 0));
Michal Vaskob36053d2020-03-26 15:49:30 +0100608
609 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200610 ly_in_free(in, 0);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200611}
612
David Sedlákb3bed7a2019-03-08 11:53:37 +0100613static void
Radek Krejcie0734d22019-04-05 15:54:28 +0200614test_ns2(void **state)
615{
Michal Vaskob36053d2020-03-26 15:49:30 +0100616 const char *str;
617 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200618 struct ly_in *in;
Radek Krejcie0734d22019-04-05 15:54:28 +0200619
Michal Vaskob36053d2020-03-26 15:49:30 +0100620 /* opening element1 */
621 str = "<element1/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200622 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
623 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Radek Krejcie0734d22019-04-05 15:54:28 +0200624
Radek Krejcie0734d22019-04-05 15:54:28 +0200625 /* default namespace defined in parent element1 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100626 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, NULL, 0, strdup("urn:default")));
627 assert_int_equal(1, xmlctx->ns.count);
Radek Krejcie0734d22019-04-05 15:54:28 +0200628 /* going into child element1 */
Radek Krejcie0734d22019-04-05 15:54:28 +0200629 /* simulate adding open element1 into context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100630 xmlctx->elements.count++;
Radek Krejcie0734d22019-04-05 15:54:28 +0200631 /* no namespace defined, going out (first, simulate closing of so far open element) */
Michal Vaskob36053d2020-03-26 15:49:30 +0100632 xmlctx->elements.count--;
633 lyxml_ns_rm(xmlctx);
634 assert_int_equal(1, xmlctx->ns.count);
635
636 /* nothing else, going out of the parent element1 */
637 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
638 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
639 assert_int_equal(0, xmlctx->ns.count);
640
641 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200642 ly_in_free(in, 0);
Radek Krejcie0734d22019-04-05 15:54:28 +0200643}
644
Radek Krejcifad79c92019-06-04 11:43:30 +0200645static void
646test_simple_xml(void **state)
647{
Michal Vaskob36053d2020-03-26 15:49:30 +0100648 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200649 struct ly_in *in;
Radek Krejcie553e6d2019-06-07 15:33:18 +0200650 const char *test_input = "<elem1 attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>";
Radek Krejcifad79c92019-06-04 11:43:30 +0200651
Michal Vasko63f3d842020-07-08 10:10:14 +0200652 assert_int_equal(LY_SUCCESS, ly_in_new_memory(test_input, &in));
653 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(*state, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100654 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200655 assert_string_equal(xmlctx->in->current, "attr1=\"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_ATTRIBUTE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200659 assert_string_equal(xmlctx->in->current, "=\"value\"> <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_ATTR_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_ELEM_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200667 assert_string_equal(xmlctx->in->current, "<elem2 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_ELEMENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200671 assert_string_equal(xmlctx->in->current, "attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200672
Michal Vaskob36053d2020-03-26 15:49:30 +0100673 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
674 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200675 assert_string_equal(xmlctx->in->current, "=\"value\" /> </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_ATTR_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_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200683 assert_string_equal(xmlctx->in->current, "/> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +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, " </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200688
Michal Vaskob36053d2020-03-26 15:49:30 +0100689 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
690 assert_int_equal(LYXML_ELEM_CLOSE, 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 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
694 assert_int_equal(LYXML_END, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200695 assert_string_equal(xmlctx->in->current, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100696
697 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200698 ly_in_free(in, 0);
Radek Krejcifad79c92019-06-04 11:43:30 +0200699}
700
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200701int main(void)
702{
703 const struct CMUnitTest tests[] = {
Radek Krejcid972c252018-09-25 13:23:39 +0200704 cmocka_unit_test_setup(test_element, logger_setup),
705 cmocka_unit_test_setup(test_attribute, logger_setup),
Radek Krejci7a7fa902018-09-25 17:08:21 +0200706 cmocka_unit_test_setup(test_text, logger_setup),
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200707 cmocka_unit_test_setup(test_ns, logger_setup),
Radek Krejcie0734d22019-04-05 15:54:28 +0200708 cmocka_unit_test_setup(test_ns2, logger_setup),
Radek Krejcifad79c92019-06-04 11:43:30 +0200709 cmocka_unit_test_setup(test_simple_xml, logger_setup),
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200710 };
711
Michal Vaskob36053d2020-03-26 15:49:30 +0100712 return cmocka_run_group_tests(tests, setup, teardown);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200713}