blob: abf0bf11f1e7194b9ecf918f8ff0261abef20278 [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 */
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);
336 assert_true(!strncmp("val", xmlctx->name, xmlctx->name_len));
337 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;
Michal Vaskob36053d2020-03-26 15:49:30 +0100380 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100381 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
382 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
383 assert_true(!strncmp("", xmlctx->value, xmlctx->value_len));
384 assert_int_equal(xmlctx->ws_only, 1);
385 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200386 ly_in_free(in, 0);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200387
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200388 /* empty element content - only formating before defining child */
Michal Vasko63f3d842020-07-08 10:10:14 +0200389 assert_int_equal(LY_SUCCESS, ly_in_new_memory(">\n <y>", &in));
390 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100391 xmlctx->status = LYXML_ELEMENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100392 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
393 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
394 assert_true(!strncmp("\n ", xmlctx->value, xmlctx->value_len));
395 assert_int_equal(xmlctx->ws_only, 1);
396 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200397 ly_in_free(in, 0);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200398
Radek Krejci7a7fa902018-09-25 17:08:21 +0200399 /* empty element content is invalid - missing content terminating character < */
Michal Vasko63f3d842020-07-08 10:10:14 +0200400 assert_int_equal(LY_SUCCESS, ly_in_new_memory("", &in));
401 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100402 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100403 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200404 CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200405 ly_in_free(in, 0);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200406
Michal Vasko63f3d842020-07-08 10:10:14 +0200407 assert_int_equal(LY_SUCCESS, ly_in_new_memory("xxx", &in));
408 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100409 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100410 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200411 CHECK_LOG_CTX("Invalid character sequence \"xxx\", expected element tag start ('<').", "Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200412 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100413
414 lyxml_ctx_free(xmlctx);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200415
Radek Krejci7a7fa902018-09-25 17:08:21 +0200416 /* valid strings */
Radek Krejci339e2de2019-05-17 14:28:24 +0200417 str = "<a>€𠜎Øn \n&lt;&amp;&quot;&apos;&gt; &#82;&#x4f;&#x4B;</a>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200418 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200419 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100420 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
421
422 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
423 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
424 assert_true(!strncmp("€𠜎Øn \n<&\"\'> ROK", xmlctx->value, xmlctx->value_len));
425 assert_int_equal(xmlctx->ws_only, 0);
426 assert_int_equal(xmlctx->dynamic, 1);
427 free((char *)xmlctx->value);
Michal Vasko63f3d842020-07-08 10:10:14 +0200428 ly_in_free(in, 0);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200429
Radek Krejci117d2082018-09-26 10:05:14 +0200430 /* test using n-bytes UTF8 hexadecimal code points */
Michal Vasko63f3d842020-07-08 10:10:14 +0200431 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x0024;&#x00A2;&#x20ac;&#x10348;\'", &in));
432 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100433 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100434 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
435 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
436 assert_true(!strncmp("$¢€𐍈", xmlctx->value, xmlctx->value_len));
437 assert_int_equal(xmlctx->ws_only, 0);
438 assert_int_equal(xmlctx->dynamic, 1);
439 free((char *)xmlctx->value);
Michal Vasko63f3d842020-07-08 10:10:14 +0200440 ly_in_free(in, 0);
Radek Krejcid70d1072018-10-09 14:20:47 +0200441
Radek Krejci7a7fa902018-09-25 17:08:21 +0200442 /* invalid characters in string */
Michal Vasko63f3d842020-07-08 10:10:14 +0200443 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x52\'", &in));
444 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100445 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100446 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200447 CHECK_LOG_CTX("Invalid character sequence \"'\", expected ;.", "Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200448 ly_in_free(in, 0);
Radek Krejci117d2082018-09-26 10:05:14 +0200449
Michal Vasko63f3d842020-07-08 10:10:14 +0200450 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\"&#82\"", &in));
451 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100452 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100453 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200454 CHECK_LOG_CTX("Invalid character sequence \"\"\", expected ;.", "Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200455 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100456
Michal Vasko63f3d842020-07-08 10:10:14 +0200457 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\"&nonsense;\"", &in));
458 xmlctx->in = 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 Iša56ca9e42020-09-08 18:42:00 +0200461 CHECK_LOG_CTX("Entity reference \"&nonsense;\" not supported, only predefined references allowed.", "Line number 2.");
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(">&#o122;", &in));
465 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100466 xmlctx->status = LYXML_ELEMENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100467 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200468 CHECK_LOG_CTX("Invalid character reference \"&#o122;\".", "Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200469 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100470
Michal Vasko63f3d842020-07-08 10:10:14 +0200471 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x06;\'", &in));
472 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100473 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100474 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200475 CHECK_LOG_CTX("Invalid character reference \"&#x06;\'\" (0x00000006).", "Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200476 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100477
Michal Vasko63f3d842020-07-08 10:10:14 +0200478 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#xfdd0;\'", &in));
479 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100480 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100481 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200482 CHECK_LOG_CTX("Invalid character reference \"&#xfdd0;\'\" (0x0000fdd0).", "Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200483 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100484
Michal Vasko63f3d842020-07-08 10:10:14 +0200485 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#xffff;\'", &in));
486 xmlctx->in = in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100487 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100488 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200489 CHECK_LOG_CTX("Invalid character reference \"&#xffff;\'\" (0x0000ffff).", "Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200490 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100491
492 lyxml_ctx_free(xmlctx);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200493}
494
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200495static void
496test_ns(void **state)
497{
Michal Vaskob36053d2020-03-26 15:49:30 +0100498 const char *str;
499 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200500 struct ly_in *in;
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200501 const struct lyxml_ns *ns;
502
Michal Vaskob36053d2020-03-26 15:49:30 +0100503 /* opening element1 */
504 str = "<element1/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200505 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200506 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200507
Radek Krejcie0734d22019-04-05 15:54:28 +0200508 /* processing namespace definitions */
Michal Vaskob36053d2020-03-26 15:49:30 +0100509 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, NULL, 0, strdup("urn:default")));
510 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, "nc", 2, strdup("urn:nc1")));
Radek Krejcie0734d22019-04-05 15:54:28 +0200511 /* simulate adding open element2 into context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100512 xmlctx->elements.count++;
Radek Krejcie0734d22019-04-05 15:54:28 +0200513 /* processing namespace definitions */
Michal Vaskob36053d2020-03-26 15:49:30 +0100514 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, "nc", 2, strdup("urn:nc2")));
515 assert_int_equal(3, xmlctx->ns.count);
516 assert_int_not_equal(0, xmlctx->ns.size);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200517
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200518 ns = lyxml_ns_get(&xmlctx->ns, NULL, 0);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200519 assert_non_null(ns);
520 assert_null(ns->prefix);
521 assert_string_equal("urn:default", ns->uri);
522
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200523 ns = lyxml_ns_get(&xmlctx->ns, "nc", 2);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200524 assert_non_null(ns);
525 assert_string_equal("nc", ns->prefix);
526 assert_string_equal("urn:nc2", ns->uri);
527
Radek Krejcie0734d22019-04-05 15:54:28 +0200528 /* simulate closing element2 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100529 xmlctx->elements.count--;
530 lyxml_ns_rm(xmlctx);
531 assert_int_equal(2, xmlctx->ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200532
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200533 ns = lyxml_ns_get(&xmlctx->ns, "nc", 2);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200534 assert_non_null(ns);
535 assert_string_equal("nc", ns->prefix);
536 assert_string_equal("urn:nc1", ns->uri);
537
Michal Vaskob36053d2020-03-26 15:49:30 +0100538 /* close element1 */
539 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
540 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
541 assert_int_equal(0, xmlctx->ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200542
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200543 assert_null(lyxml_ns_get(&xmlctx->ns, "nc", 2));
544 assert_null(lyxml_ns_get(&xmlctx->ns, NULL, 0));
Michal Vaskob36053d2020-03-26 15:49:30 +0100545
546 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200547 ly_in_free(in, 0);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200548}
549
David Sedlákb3bed7a2019-03-08 11:53:37 +0100550static void
Radek Krejcie0734d22019-04-05 15:54:28 +0200551test_ns2(void **state)
552{
Michal Vaskob36053d2020-03-26 15:49:30 +0100553 const char *str;
554 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200555 struct ly_in *in;
Radek Krejcie0734d22019-04-05 15:54:28 +0200556
Michal Vaskob36053d2020-03-26 15:49:30 +0100557 /* opening element1 */
558 str = "<element1/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200559 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200560 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Radek Krejcie0734d22019-04-05 15:54:28 +0200561
Radek Krejcie0734d22019-04-05 15:54:28 +0200562 /* default namespace defined in parent element1 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100563 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, NULL, 0, strdup("urn:default")));
564 assert_int_equal(1, xmlctx->ns.count);
Radek Krejcie0734d22019-04-05 15:54:28 +0200565 /* going into child element1 */
Radek Krejcie0734d22019-04-05 15:54:28 +0200566 /* simulate adding open element1 into context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100567 xmlctx->elements.count++;
Radek Krejcie0734d22019-04-05 15:54:28 +0200568 /* no namespace defined, going out (first, simulate closing of so far open element) */
Michal Vaskob36053d2020-03-26 15:49:30 +0100569 xmlctx->elements.count--;
570 lyxml_ns_rm(xmlctx);
571 assert_int_equal(1, xmlctx->ns.count);
572
573 /* nothing else, going out of the parent element1 */
574 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
575 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
576 assert_int_equal(0, xmlctx->ns.count);
577
578 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200579 ly_in_free(in, 0);
Radek Krejcie0734d22019-04-05 15:54:28 +0200580}
581
Radek Krejcifad79c92019-06-04 11:43:30 +0200582static void
583test_simple_xml(void **state)
584{
Michal Vaskob36053d2020-03-26 15:49:30 +0100585 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200586 struct ly_in *in;
Radek Krejcie553e6d2019-06-07 15:33:18 +0200587 const char *test_input = "<elem1 attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>";
Radek Krejcifad79c92019-06-04 11:43:30 +0200588
Michal Vasko63f3d842020-07-08 10:10:14 +0200589 assert_int_equal(LY_SUCCESS, ly_in_new_memory(test_input, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200590 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100591 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200592 assert_string_equal(xmlctx->in->current, "attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200593
Michal Vaskob36053d2020-03-26 15:49:30 +0100594 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
595 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200596 assert_string_equal(xmlctx->in->current, "=\"value\"> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200597
Michal Vaskob36053d2020-03-26 15:49:30 +0100598 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
599 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200600 assert_string_equal(xmlctx->in->current, "> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200601
Michal Vaskob36053d2020-03-26 15:49:30 +0100602 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
603 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200604 assert_string_equal(xmlctx->in->current, "<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_ELEMENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200608 assert_string_equal(xmlctx->in->current, "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_ATTRIBUTE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200612 assert_string_equal(xmlctx->in->current, "=\"value\" /> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200613
Michal Vaskob36053d2020-03-26 15:49:30 +0100614 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
615 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200616 assert_string_equal(xmlctx->in->current, " /> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200617
Michal Vaskob36053d2020-03-26 15:49:30 +0100618 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
619 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200620 assert_string_equal(xmlctx->in->current, "/> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200621
Michal Vaskob36053d2020-03-26 15:49:30 +0100622 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
623 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200624 assert_string_equal(xmlctx->in->current, " </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200625
Michal Vaskob36053d2020-03-26 15:49:30 +0100626 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
627 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200628 assert_string_equal(xmlctx->in->current, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100629
630 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
631 assert_int_equal(LYXML_END, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200632 assert_string_equal(xmlctx->in->current, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100633
634 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200635 ly_in_free(in, 0);
Radek Krejcifad79c92019-06-04 11:43:30 +0200636}
637
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100638int
639main(void)
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200640{
641 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200642 UTEST(test_element),
643 UTEST(test_attribute),
644 UTEST(test_text),
645 UTEST(test_ns),
646 UTEST(test_ns2),
647 UTEST(test_simple_xml),
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200648 };
649
Radek Iša56ca9e42020-09-08 18:42:00 +0200650 return cmocka_run_group_tests(tests, NULL, NULL);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200651}