blob: 1999f6573b47f62e48954cfc057515ee148ff5b4 [file] [log] [blame]
Radek Krejcid91dbaf2018-09-21 15:51:39 +02001/*
aPiecek023f83a2021-05-11 07:37:03 +02002 * @file test_xml.c
Radek Krejcid91dbaf2018-09-21 15:51:39 +02003 * @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 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
15#include "utests.h"
Radek Krejcid91dbaf2018-09-21 15:51:39 +020016
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 Krejci70593c12020-06-13 20:48:09 +020021#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020022#include "in_internal.h"
Michal Vasko63f3d842020-07-08 10:10:14 +020023#include "xml.h"
Radek Krejci2d7a47b2019-05-16 13:34:10 +020024
Michal Vaskob36053d2020-03-26 15:49:30 +010025LY_ERR lyxml_ns_add(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len, char *uri);
26LY_ERR lyxml_ns_rm(struct lyxml_ctx *xmlctx);
27
Radek Krejcid91dbaf2018-09-21 15:51:39 +020028static void
29test_element(void **state)
30{
Michal Vaskob36053d2020-03-26 15:49:30 +010031 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +020032 struct ly_in *in;
Michal Vaskob36053d2020-03-26 15:49:30 +010033 const char *str;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020034
35 /* empty */
36 str = "";
Michal Vasko63f3d842020-07-08 10:10:14 +020037 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +020038 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +010039 assert_int_equal(LYXML_END, xmlctx->status);
Michal Vaskob36053d2020-03-26 15:49:30 +010040 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +020041 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +020042
Radek Krejcib1890642018-10-03 14:05:40 +020043 /* end element */
44 str = "</element>";
Michal Vasko63f3d842020-07-08 10:10:14 +020045 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +020046 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
47 CHECK_LOG_CTX("Stray closing element tag (\"element\").", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +020048 ly_in_free(in, 0);
Radek Krejcib1890642018-10-03 14:05:40 +020049
Radek Krejcid91dbaf2018-09-21 15:51:39 +020050 /* no element */
Radek Iša56ca9e42020-09-08 18:42:00 +020051 UTEST_LOG_CLEAN;
Michal Vaskob36053d2020-03-26 15:49:30 +010052 str = "no data present";
Michal Vasko63f3d842020-07-08 10:10:14 +020053 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +020054 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
55 CHECK_LOG_CTX("Invalid character sequence \"no data present\", expected element tag start ('<').", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +020056 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +020057
58 /* not supported DOCTYPE */
Michal Vaskob36053d2020-03-26 15:49:30 +010059 str = "<!DOCTYPE greeting SYSTEM \"hello.dtd\"><greeting/>";
Michal Vasko63f3d842020-07-08 10:10:14 +020060 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +020061 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
62 CHECK_LOG_CTX("Document Type Declaration not supported.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +020063 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +020064
Radek Krejcic5c31bb2019-04-08 14:40:52 +020065 /* invalid XML */
Michal Vaskob36053d2020-03-26 15:49:30 +010066 str = "<!NONSENSE/>";
Michal Vasko63f3d842020-07-08 10:10:14 +020067 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +020068 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
69 CHECK_LOG_CTX("Unknown XML section \"<!NONSENSE/>\".", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +020070 ly_in_free(in, 0);
Radek Krejcic5c31bb2019-04-08 14:40:52 +020071
Radek Krejcid91dbaf2018-09-21 15:51:39 +020072 /* unqualified element */
73 str = " < element/>";
Michal Vasko63f3d842020-07-08 10:10:14 +020074 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +020075 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +010076 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
77 assert_null(xmlctx->prefix);
78 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
79 assert_int_equal(1, xmlctx->elements.count);
Radek Krejci28e8cb52019-03-08 11:31:31 +010080
Michal Vaskob36053d2020-03-26 15:49:30 +010081 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
82 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
83 assert_true(!strncmp("", xmlctx->value, xmlctx->value_len));
84
85 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
86 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
87
88 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
89 assert_int_equal(LYXML_END, xmlctx->status);
90 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +020091 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +010092
93 /* element with attribute */
Radek Krejci28e8cb52019-03-08 11:31:31 +010094 str = " < element attr=\'x\'/>";
Michal Vasko63f3d842020-07-08 10:10:14 +020095 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +020096 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +010097 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
98 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
99 assert_null(xmlctx->prefix);
100 assert_int_equal(1, xmlctx->elements.count);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200101
Michal Vaskob36053d2020-03-26 15:49:30 +0100102 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
103 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
104 assert_true(!strncmp("attr", xmlctx->name, xmlctx->name_len));
105 assert_null(xmlctx->prefix);
106
107 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
108 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
109 assert_int_equal(1, xmlctx->elements.count);
110 assert_true(!strncmp("x", xmlctx->value, xmlctx->value_len));
111
112 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
113 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
114
115 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
116 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
117 assert_int_equal(0, xmlctx->elements.count);
118
119 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
120 assert_int_equal(LYXML_END, xmlctx->status);
121 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200122 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100123
124 /* headers and comments */
Radek Krejcifb7c6582018-09-21 16:12:45 +0200125 str = "<?xml version=\"1.0\"?> <!-- comment --> <![CDATA[<greeting>Hello, world!</greeting>]]> <?TEST xxx?> <element/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200126 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200127 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100128 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
129 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
130 assert_null(xmlctx->prefix);
131 assert_int_equal(1, xmlctx->elements.count);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200132
Michal Vaskob36053d2020-03-26 15:49:30 +0100133 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
134 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
135
136 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
137 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
138
139 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
140 assert_int_equal(LYXML_END, xmlctx->status);
141 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200142 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100143
144 /* separate opening and closing tags, neamespaced parsed internally */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200145 str = "<element xmlns=\"urn\"></element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200146 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200147 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100148 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
149 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
150 assert_null(xmlctx->prefix);
151 assert_int_equal(1, xmlctx->elements.count);
152 assert_int_equal(1, xmlctx->ns.count);
153
154 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
155 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
156
157 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
158 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
159 assert_int_equal(0, xmlctx->elements.count);
160 assert_int_equal(0, xmlctx->ns.count);
161
162 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
163 assert_int_equal(LYXML_END, xmlctx->status);
164 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200165 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200166
167 /* qualified element */
168 str = " < yin:element/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200169 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200170 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100171 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
172 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
173 assert_true(!strncmp("yin", xmlctx->prefix, xmlctx->prefix_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200174
Michal Vaskob36053d2020-03-26 15:49:30 +0100175 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
181 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
182 assert_int_equal(LYXML_END, xmlctx->status);
183 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200184 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100185
186 /* non-matching closing tag */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200187 str = "<yin:element xmlns=\"urn\"></element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200188 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200189 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100190 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
191 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
192 assert_true(!strncmp("yin", xmlctx->prefix, xmlctx->prefix_len));
193 assert_int_equal(1, xmlctx->elements.count);
194 assert_int_equal(1, xmlctx->ns.count);
195
196 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
197 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
198
199 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200200 CHECK_LOG_CTX("Opening (\"yin:element\") and closing (\"element\") elements tag mismatch.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200201 lyxml_ctx_free(xmlctx);
202 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100203
Michal Vasko63f3d842020-07-08 10:10:14 +0200204 /* invalid closing tag */
205 str = "<yin:element xmlns=\"urn\"></yin:element/>";
206 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200207 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vasko63f3d842020-07-08 10:10:14 +0200208 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100209 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200210 CHECK_LOG_CTX("Invalid character sequence \"/>\", expected element tag termination ('>').", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100211 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200212 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200213
214 /* UTF8 characters */
215 str = "<𠜎€𠜎Øn:𠜎€𠜎Øn/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200216 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200217 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100218 assert_true(!strncmp("𠜎€𠜎Øn", xmlctx->name, xmlctx->name_len));
219 assert_true(!strncmp("𠜎€𠜎Øn", xmlctx->prefix, xmlctx->prefix_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200220
Michal Vaskob36053d2020-03-26 15:49:30 +0100221 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
222 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
223
224 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
225 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
226
227 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
228 assert_int_equal(LYXML_END, xmlctx->status);
229 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200230 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100231
232 /* invalid UTF-8 characters */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200233 str = "<¢:element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200234 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200235 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
236 CHECK_LOG_CTX("Identifier \"¢:element>\" starts with an invalid character.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200237 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100238
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200239 str = "<yin:c⁐element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200240 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200241 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100242 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200243 CHECK_LOG_CTX("Invalid character sequence \"⁐element>\", expected element tag end ('>' or '/>') or an attribute.", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100244 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200245 ly_in_free(in, 0);
Radek Krejci339e2de2019-05-17 14:28:24 +0200246
247 /* mixed content */
248 str = "<a>text <b>x</b></a>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200249 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200250 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100251 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
252 assert_true(!strncmp("a", xmlctx->name, xmlctx->name_len));
253 assert_null(xmlctx->prefix);
Radek Krejci339e2de2019-05-17 14:28:24 +0200254
Michal Vaskob36053d2020-03-26 15:49:30 +0100255 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
256 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
257 assert_true(!strncmp("text ", xmlctx->value, xmlctx->value_len));
258
259 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
260 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
261 assert_true(!strncmp("b", xmlctx->name, xmlctx->name_len));
262 assert_null(xmlctx->prefix);
263
264 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
265 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
266 assert_true(!strncmp("x", xmlctx->value, xmlctx->value_len));
267
268 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
269 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
270
271 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
272 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
273
274 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
275 assert_int_equal(LYXML_END, xmlctx->status);
276 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200277 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100278
279 /* tag mismatch */
David Sedlák54a6f132019-07-16 15:14:18 +0200280 str = "<a>text</b>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200281 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200282 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100283 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
284 assert_true(!strncmp("a", xmlctx->name, xmlctx->name_len));
285 assert_null(xmlctx->prefix);
David Sedlák54a6f132019-07-16 15:14:18 +0200286
Michal Vaskob36053d2020-03-26 15:49:30 +0100287 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
288 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
289 assert_true(!strncmp("text", xmlctx->value, xmlctx->value_len));
290
291 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200292 CHECK_LOG_CTX("Opening (\"a\") and closing (\"b\") elements tag mismatch.", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100293 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200294 ly_in_free(in, 0);
Radek Krejcid972c252018-09-25 13:23:39 +0200295}
296
297static void
298test_attribute(void **state)
299{
Michal Vaskob36053d2020-03-26 15:49:30 +0100300 const char *str;
301 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200302 struct ly_in *in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100303 struct lyxml_ns *ns;
Radek Krejcid972c252018-09-25 13:23:39 +0200304
Radek Krejcid972c252018-09-25 13:23:39 +0200305 /* not an attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +0100306 str = "<e unknown/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200307 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200308 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
309 CHECK_LOG_CTX("Invalid character sequence \"/>\", expected '='.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200310 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100311
312 str = "<e xxx=/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200313 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200314 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
315 CHECK_LOG_CTX("Invalid character sequence \"/>\", expected either single or double quotation mark.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200316 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100317
318 str = "<e xxx\n = yyy/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200319 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200320 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
321 CHECK_LOG_CTX("Invalid character sequence \"yyy/>\", expected either single or double quotation mark.", "Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200322 ly_in_free(in, 0);
Radek Krejcid972c252018-09-25 13:23:39 +0200323
324 /* valid attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +0100325 str = "<e attr=\"val\"";
Michal Vasko63f3d842020-07-08 10:10:14 +0200326 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200327 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100328 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
329 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
330 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
331 assert_true(!strncmp("attr", xmlctx->name, xmlctx->name_len));
332 assert_null(xmlctx->prefix);
Radek Krejcid972c252018-09-25 13:23:39 +0200333
Michal Vaskob36053d2020-03-26 15:49:30 +0100334 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
335 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
Radek Iša85a55232021-02-16 10:44:43 +0100336 assert_true(!strncmp("val", xmlctx->value, xmlctx->value_len));
Michal Vaskob36053d2020-03-26 15:49:30 +0100337 assert_int_equal(xmlctx->ws_only, 0);
338 assert_int_equal(xmlctx->dynamic, 0);
339 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200340 ly_in_free(in, 0);
Radek Krejci28e8cb52019-03-08 11:31:31 +0100341
Michal Vaskob36053d2020-03-26 15:49:30 +0100342 /* valid namespace with prefix */
343 str = "<e xmlns:nc\n = \'urn\'/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200344 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200345 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100346 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
347 assert_int_equal(1, xmlctx->ns.count);
348 ns = (struct lyxml_ns *)xmlctx->ns.objs[0];
349 assert_string_equal(ns->prefix, "nc");
350 assert_string_equal(ns->uri, "urn");
351 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200352 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200353}
354
Radek Krejci7a7fa902018-09-25 17:08:21 +0200355static void
356test_text(void **state)
357{
Michal Vaskob36053d2020-03-26 15:49:30 +0100358 const char *str;
359 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200360 struct ly_in *in;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200361
362 /* empty attribute value */
Michal Vaskob36053d2020-03-26 15:49:30 +0100363 str = "<e a=\"\"";
Michal Vasko63f3d842020-07-08 10:10:14 +0200364 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200365 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100366 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
367 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
368 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Radek Krejcib1890642018-10-03 14:05:40 +0200369
Michal Vaskob36053d2020-03-26 15:49:30 +0100370 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
371 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
372 assert_true(!strncmp("", xmlctx->value, xmlctx->value_len));
373 assert_int_equal(xmlctx->ws_only, 1);
374 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200375 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100376
377 /* empty value but in single quotes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200378 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'\'", &in));
379 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100380 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100381 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100382 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
383 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
384 assert_true(!strncmp("", xmlctx->value, xmlctx->value_len));
385 assert_int_equal(xmlctx->ws_only, 1);
386 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200387 ly_in_free(in, 0);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200388
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200389 /* empty element content - only formating before defining child */
Michal Vasko63f3d842020-07-08 10:10:14 +0200390 assert_int_equal(LY_SUCCESS, ly_in_new_memory(">\n <y>", &in));
391 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100392 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100393 xmlctx->status = LYXML_ELEMENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100394 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
395 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
396 assert_true(!strncmp("\n ", xmlctx->value, xmlctx->value_len));
397 assert_int_equal(xmlctx->ws_only, 1);
398 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200399 ly_in_free(in, 0);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200400
Radek Krejci7a7fa902018-09-25 17:08:21 +0200401 /* empty element content is invalid - missing content terminating character < */
Michal Vasko63f3d842020-07-08 10:10:14 +0200402 assert_int_equal(LY_SUCCESS, ly_in_new_memory("", &in));
403 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100404 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100405 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100406 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100407 CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200408 ly_in_free(in, 0);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200409
Michal Vasko63f3d842020-07-08 10:10:14 +0200410 assert_int_equal(LY_SUCCESS, ly_in_new_memory("xxx", &in));
411 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100412 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100413 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100414 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100415 CHECK_LOG_CTX("Invalid character sequence \"xxx\", expected element tag start ('<').", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200416 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100417
418 lyxml_ctx_free(xmlctx);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200419
Radek Krejci7a7fa902018-09-25 17:08:21 +0200420 /* valid strings */
Radek Krejci339e2de2019-05-17 14:28:24 +0200421 str = "<a>€𠜎Øn \n&lt;&amp;&quot;&apos;&gt; &#82;&#x4f;&#x4B;</a>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200422 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200423 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100424 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
425
426 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
427 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
428 assert_true(!strncmp("€𠜎Øn \n<&\"\'> ROK", xmlctx->value, xmlctx->value_len));
429 assert_int_equal(xmlctx->ws_only, 0);
430 assert_int_equal(xmlctx->dynamic, 1);
431 free((char *)xmlctx->value);
Michal Vasko63f3d842020-07-08 10:10:14 +0200432 ly_in_free(in, 0);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200433
Radek Krejci117d2082018-09-26 10:05:14 +0200434 /* test using n-bytes UTF8 hexadecimal code points */
Michal Vasko63f3d842020-07-08 10:10:14 +0200435 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x0024;&#x00A2;&#x20ac;&#x10348;\'", &in));
436 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100437 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100438 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100439 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
440 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
441 assert_true(!strncmp("$¢€𐍈", xmlctx->value, xmlctx->value_len));
442 assert_int_equal(xmlctx->ws_only, 0);
443 assert_int_equal(xmlctx->dynamic, 1);
444 free((char *)xmlctx->value);
Michal Vasko63f3d842020-07-08 10:10:14 +0200445 ly_in_free(in, 0);
Radek Krejcid70d1072018-10-09 14:20:47 +0200446
Radek Krejci7a7fa902018-09-25 17:08:21 +0200447 /* invalid characters in string */
Michal Vasko63f3d842020-07-08 10:10:14 +0200448 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x52\'", &in));
449 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100450 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100451 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100452 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100453 CHECK_LOG_CTX("Invalid character sequence \"'\", expected ;.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200454 ly_in_free(in, 0);
Radek Krejci117d2082018-09-26 10:05:14 +0200455
Michal Vasko63f3d842020-07-08 10:10:14 +0200456 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\"&#82\"", &in));
457 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100458 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100459 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100460 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100461 CHECK_LOG_CTX("Invalid character sequence \"\"\", expected ;.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200462 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100463
Michal Vasko63f3d842020-07-08 10:10:14 +0200464 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\"&nonsense;\"", &in));
465 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100466 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100467 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100468 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100469 CHECK_LOG_CTX("Entity reference \"&nonsense;\" not supported, only predefined references allowed.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200470 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100471
Michal Vasko63f3d842020-07-08 10:10:14 +0200472 assert_int_equal(LY_SUCCESS, ly_in_new_memory(">&#o122;", &in));
473 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100474 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100475 xmlctx->status = LYXML_ELEMENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100476 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100477 CHECK_LOG_CTX("Invalid character reference \"&#o122;\".", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200478 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100479
Michal Vasko63f3d842020-07-08 10:10:14 +0200480 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x06;\'", &in));
481 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100482 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100483 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100484 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100485 CHECK_LOG_CTX("Invalid character reference \"&#x06;\'\" (0x00000006).", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200486 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100487
Michal Vasko63f3d842020-07-08 10:10:14 +0200488 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#xfdd0;\'", &in));
489 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100490 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100491 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100492 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100493 CHECK_LOG_CTX("Invalid character reference \"&#xfdd0;\'\" (0x0000fdd0).", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200494 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100495
Michal Vasko63f3d842020-07-08 10:10:14 +0200496 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#xffff;\'", &in));
497 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100498 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100499 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100500 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100501 CHECK_LOG_CTX("Invalid character reference \"&#xffff;\'\" (0x0000ffff).", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200502 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100503
504 lyxml_ctx_free(xmlctx);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200505}
506
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200507static void
508test_ns(void **state)
509{
Michal Vaskob36053d2020-03-26 15:49:30 +0100510 const char *str;
511 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200512 struct ly_in *in;
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200513 const struct lyxml_ns *ns;
514
Michal Vaskob36053d2020-03-26 15:49:30 +0100515 /* opening element1 */
516 str = "<element1/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200517 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200518 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200519
Radek Krejcie0734d22019-04-05 15:54:28 +0200520 /* processing namespace definitions */
Michal Vaskob36053d2020-03-26 15:49:30 +0100521 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, NULL, 0, strdup("urn:default")));
522 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, "nc", 2, strdup("urn:nc1")));
Radek Krejcie0734d22019-04-05 15:54:28 +0200523 /* simulate adding open element2 into context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100524 xmlctx->elements.count++;
Radek Krejcie0734d22019-04-05 15:54:28 +0200525 /* processing namespace definitions */
Michal Vaskob36053d2020-03-26 15:49:30 +0100526 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, "nc", 2, strdup("urn:nc2")));
527 assert_int_equal(3, xmlctx->ns.count);
528 assert_int_not_equal(0, xmlctx->ns.size);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200529
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200530 ns = lyxml_ns_get(&xmlctx->ns, NULL, 0);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200531 assert_non_null(ns);
532 assert_null(ns->prefix);
533 assert_string_equal("urn:default", ns->uri);
534
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200535 ns = lyxml_ns_get(&xmlctx->ns, "nc", 2);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200536 assert_non_null(ns);
537 assert_string_equal("nc", ns->prefix);
538 assert_string_equal("urn:nc2", ns->uri);
539
Radek Krejcie0734d22019-04-05 15:54:28 +0200540 /* simulate closing element2 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100541 xmlctx->elements.count--;
542 lyxml_ns_rm(xmlctx);
543 assert_int_equal(2, xmlctx->ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200544
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200545 ns = lyxml_ns_get(&xmlctx->ns, "nc", 2);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200546 assert_non_null(ns);
547 assert_string_equal("nc", ns->prefix);
548 assert_string_equal("urn:nc1", ns->uri);
549
Michal Vaskob36053d2020-03-26 15:49:30 +0100550 /* close element1 */
551 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
552 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
553 assert_int_equal(0, xmlctx->ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200554
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200555 assert_null(lyxml_ns_get(&xmlctx->ns, "nc", 2));
556 assert_null(lyxml_ns_get(&xmlctx->ns, NULL, 0));
Michal Vaskob36053d2020-03-26 15:49:30 +0100557
558 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200559 ly_in_free(in, 0);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200560}
561
David Sedlákb3bed7a2019-03-08 11:53:37 +0100562static void
Radek Krejcie0734d22019-04-05 15:54:28 +0200563test_ns2(void **state)
564{
Michal Vaskob36053d2020-03-26 15:49:30 +0100565 const char *str;
566 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200567 struct ly_in *in;
Radek Krejcie0734d22019-04-05 15:54:28 +0200568
Michal Vaskob36053d2020-03-26 15:49:30 +0100569 /* opening element1 */
570 str = "<element1/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200571 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200572 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Radek Krejcie0734d22019-04-05 15:54:28 +0200573
Radek Krejcie0734d22019-04-05 15:54:28 +0200574 /* default namespace defined in parent element1 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100575 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, NULL, 0, strdup("urn:default")));
576 assert_int_equal(1, xmlctx->ns.count);
Radek Krejcie0734d22019-04-05 15:54:28 +0200577 /* going into child element1 */
Radek Krejcie0734d22019-04-05 15:54:28 +0200578 /* simulate adding open element1 into context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100579 xmlctx->elements.count++;
Radek Krejcie0734d22019-04-05 15:54:28 +0200580 /* no namespace defined, going out (first, simulate closing of so far open element) */
Michal Vaskob36053d2020-03-26 15:49:30 +0100581 xmlctx->elements.count--;
582 lyxml_ns_rm(xmlctx);
583 assert_int_equal(1, xmlctx->ns.count);
584
585 /* nothing else, going out of the parent element1 */
586 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
587 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
588 assert_int_equal(0, xmlctx->ns.count);
589
590 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200591 ly_in_free(in, 0);
Radek Krejcie0734d22019-04-05 15:54:28 +0200592}
593
Radek Krejcifad79c92019-06-04 11:43:30 +0200594static void
595test_simple_xml(void **state)
596{
Michal Vaskob36053d2020-03-26 15:49:30 +0100597 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200598 struct ly_in *in;
Radek Krejcie553e6d2019-06-07 15:33:18 +0200599 const char *test_input = "<elem1 attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>";
Radek Krejcifad79c92019-06-04 11:43:30 +0200600
Michal Vasko63f3d842020-07-08 10:10:14 +0200601 assert_int_equal(LY_SUCCESS, ly_in_new_memory(test_input, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200602 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100603 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200604 assert_string_equal(xmlctx->in->current, "attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200605
Michal Vaskob36053d2020-03-26 15:49:30 +0100606 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
607 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200608 assert_string_equal(xmlctx->in->current, "=\"value\"> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200609
Michal Vaskob36053d2020-03-26 15:49:30 +0100610 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
611 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200612 assert_string_equal(xmlctx->in->current, "> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200613
Michal Vaskob36053d2020-03-26 15:49:30 +0100614 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
615 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200616 assert_string_equal(xmlctx->in->current, "<elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200617
Michal Vaskob36053d2020-03-26 15:49:30 +0100618 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
619 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200620 assert_string_equal(xmlctx->in->current, "attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200621
Michal Vaskob36053d2020-03-26 15:49:30 +0100622 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
623 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200624 assert_string_equal(xmlctx->in->current, "=\"value\" /> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200625
Michal Vaskob36053d2020-03-26 15:49:30 +0100626 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
627 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200628 assert_string_equal(xmlctx->in->current, " /> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200629
Michal Vaskob36053d2020-03-26 15:49:30 +0100630 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
631 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200632 assert_string_equal(xmlctx->in->current, "/> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200633
Michal Vaskob36053d2020-03-26 15:49:30 +0100634 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
635 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200636 assert_string_equal(xmlctx->in->current, " </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200637
Michal Vaskob36053d2020-03-26 15:49:30 +0100638 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
639 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200640 assert_string_equal(xmlctx->in->current, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100641
642 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
643 assert_int_equal(LYXML_END, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200644 assert_string_equal(xmlctx->in->current, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100645
646 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200647 ly_in_free(in, 0);
Radek Krejcifad79c92019-06-04 11:43:30 +0200648}
649
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100650int
651main(void)
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200652{
653 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200654 UTEST(test_element),
655 UTEST(test_attribute),
656 UTEST(test_text),
657 UTEST(test_ns),
658 UTEST(test_ns2),
659 UTEST(test_simple_xml),
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200660 };
661
Radek Iša56ca9e42020-09-08 18:42:00 +0200662 return cmocka_run_group_tests(tests, NULL, NULL);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200663}