blob: c94e5adcbf3238c2e5af22bfd1779181c73fdd61 [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);
Michal Vaskob36053d2020-03-26 15:49:30 +010026
Radek Krejcid91dbaf2018-09-21 15:51:39 +020027static void
28test_element(void **state)
29{
Michal Vaskob36053d2020-03-26 15:49:30 +010030 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +020031 struct ly_in *in;
Michal Vaskob36053d2020-03-26 15:49:30 +010032 const char *str;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020033
34 /* empty */
35 str = "";
Michal Vasko63f3d842020-07-08 10:10:14 +020036 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +020037 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +010038 assert_int_equal(LYXML_END, xmlctx->status);
Michal Vaskob36053d2020-03-26 15:49:30 +010039 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +020040 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +020041
Radek Krejcib1890642018-10-03 14:05:40 +020042 /* end element */
43 str = "</element>";
Michal Vasko63f3d842020-07-08 10:10:14 +020044 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +020045 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
46 CHECK_LOG_CTX("Stray closing element tag (\"element\").", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +020047 ly_in_free(in, 0);
Radek Krejcib1890642018-10-03 14:05:40 +020048
Radek Krejcid91dbaf2018-09-21 15:51:39 +020049 /* no element */
Radek Iša56ca9e42020-09-08 18:42:00 +020050 UTEST_LOG_CLEAN;
Michal Vaskob36053d2020-03-26 15:49:30 +010051 str = "no data present";
Michal Vasko63f3d842020-07-08 10:10:14 +020052 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +020053 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
54 CHECK_LOG_CTX("Invalid character sequence \"no data present\", expected element tag start ('<').", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +020055 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +020056
57 /* not supported DOCTYPE */
Michal Vaskob36053d2020-03-26 15:49:30 +010058 str = "<!DOCTYPE greeting SYSTEM \"hello.dtd\"><greeting/>";
Michal Vasko63f3d842020-07-08 10:10:14 +020059 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +020060 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
61 CHECK_LOG_CTX("Document Type Declaration not supported.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +020062 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +020063
Radek Krejcic5c31bb2019-04-08 14:40:52 +020064 /* invalid XML */
Michal Vaskob36053d2020-03-26 15:49:30 +010065 str = "<!NONSENSE/>";
Michal Vasko63f3d842020-07-08 10:10:14 +020066 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +020067 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
68 CHECK_LOG_CTX("Unknown XML section \"<!NONSENSE/>\".", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +020069 ly_in_free(in, 0);
Radek Krejcic5c31bb2019-04-08 14:40:52 +020070
Michal Vasko4b3f2952022-08-26 09:24:04 +020071 /* namespace ambiguity */
72 str = "<element xmlns=\"urn1\" xmlns=\"urn2\"/>";
73 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
74 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
75 CHECK_LOG_CTX("Duplicate default XML namespaces \"urn1\" and \"urn2\".", "Line number 1.");
76 ly_in_free(in, 0);
77
78 /* prefix duplicate */
79 str = "<element xmlns:a=\"urn1\" xmlns:a=\"urn2\"/>";
80 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
81 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
82 CHECK_LOG_CTX("Duplicate XML NS prefix \"a\" used for namespaces \"urn1\" and \"urn2\".", "Line number 1.");
83 ly_in_free(in, 0);
84
Radek Krejcid91dbaf2018-09-21 15:51:39 +020085 /* unqualified element */
86 str = " < element/>";
Michal Vasko63f3d842020-07-08 10:10:14 +020087 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +020088 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +010089 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
90 assert_null(xmlctx->prefix);
91 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
92 assert_int_equal(1, xmlctx->elements.count);
Radek Krejci28e8cb52019-03-08 11:31:31 +010093
Michal Vaskob36053d2020-03-26 15:49:30 +010094 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
95 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
96 assert_true(!strncmp("", xmlctx->value, xmlctx->value_len));
97
98 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
99 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
100
101 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
102 assert_int_equal(LYXML_END, xmlctx->status);
103 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200104 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100105
106 /* element with attribute */
Radek Krejci28e8cb52019-03-08 11:31:31 +0100107 str = " < element attr=\'x\'/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200108 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200109 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100110 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
111 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
112 assert_null(xmlctx->prefix);
113 assert_int_equal(1, xmlctx->elements.count);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200114
Michal Vaskob36053d2020-03-26 15:49:30 +0100115 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
116 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
117 assert_true(!strncmp("attr", xmlctx->name, xmlctx->name_len));
118 assert_null(xmlctx->prefix);
119
120 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
121 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
122 assert_int_equal(1, xmlctx->elements.count);
123 assert_true(!strncmp("x", xmlctx->value, xmlctx->value_len));
124
125 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
126 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
127
128 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
129 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
130 assert_int_equal(0, xmlctx->elements.count);
131
132 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
133 assert_int_equal(LYXML_END, xmlctx->status);
134 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200135 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100136
137 /* headers and comments */
Michal Vasko8cf6f722022-02-18 13:08:23 +0100138 str = "<?xml version=\"1.0\"?> <!-- comment --> <?TEST xxx?> <element/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200139 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200140 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100141 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
142 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
143 assert_null(xmlctx->prefix);
144 assert_int_equal(1, xmlctx->elements.count);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200145
Michal Vaskob36053d2020-03-26 15:49:30 +0100146 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
147 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
148
149 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
150 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
151
152 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
153 assert_int_equal(LYXML_END, xmlctx->status);
154 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200155 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100156
157 /* separate opening and closing tags, neamespaced parsed internally */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200158 str = "<element xmlns=\"urn\"></element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200159 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200160 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100161 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
162 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
163 assert_null(xmlctx->prefix);
164 assert_int_equal(1, xmlctx->elements.count);
165 assert_int_equal(1, xmlctx->ns.count);
166
167 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
168 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
169
170 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
171 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
172 assert_int_equal(0, xmlctx->elements.count);
173 assert_int_equal(0, xmlctx->ns.count);
174
175 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
176 assert_int_equal(LYXML_END, xmlctx->status);
177 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200178 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200179
180 /* qualified element */
181 str = " < yin:element/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200182 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200183 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100184 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
185 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
186 assert_true(!strncmp("yin", xmlctx->prefix, xmlctx->prefix_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200187
Michal Vaskob36053d2020-03-26 15:49:30 +0100188 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
189 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
190
191 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
192 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
193
194 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
195 assert_int_equal(LYXML_END, xmlctx->status);
196 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200197 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100198
199 /* non-matching closing tag */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200200 str = "<yin:element xmlns=\"urn\"></element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200201 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200202 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100203 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
204 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
205 assert_true(!strncmp("yin", xmlctx->prefix, xmlctx->prefix_len));
206 assert_int_equal(1, xmlctx->elements.count);
207 assert_int_equal(1, xmlctx->ns.count);
208
209 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
210 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
211
212 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200213 CHECK_LOG_CTX("Opening (\"yin:element\") and closing (\"element\") elements tag mismatch.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200214 lyxml_ctx_free(xmlctx);
215 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100216
Michal Vasko63f3d842020-07-08 10:10:14 +0200217 /* invalid closing tag */
218 str = "<yin:element xmlns=\"urn\"></yin:element/>";
219 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200220 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vasko63f3d842020-07-08 10:10:14 +0200221 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100222 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200223 CHECK_LOG_CTX("Invalid character sequence \"/>\", expected element tag termination ('>').", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100224 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200225 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200226
227 /* UTF8 characters */
228 str = "<𠜎€𠜎Øn:𠜎€𠜎Øn/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200229 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200230 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100231 assert_true(!strncmp("𠜎€𠜎Øn", xmlctx->name, xmlctx->name_len));
232 assert_true(!strncmp("𠜎€𠜎Øn", xmlctx->prefix, xmlctx->prefix_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200233
Michal Vaskob36053d2020-03-26 15:49:30 +0100234 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
235 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
236
237 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
238 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
239
240 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
241 assert_int_equal(LYXML_END, xmlctx->status);
242 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200243 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100244
245 /* invalid UTF-8 characters */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200246 str = "<¢:element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200247 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200248 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
249 CHECK_LOG_CTX("Identifier \"¢:element>\" starts with an invalid character.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200250 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100251
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200252 str = "<yin:c⁐element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200253 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200254 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100255 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200256 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 +0100257 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200258 ly_in_free(in, 0);
Radek Krejci339e2de2019-05-17 14:28:24 +0200259
260 /* mixed content */
261 str = "<a>text <b>x</b></a>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200262 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200263 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100264 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
265 assert_true(!strncmp("a", xmlctx->name, xmlctx->name_len));
266 assert_null(xmlctx->prefix);
Radek Krejci339e2de2019-05-17 14:28:24 +0200267
Michal Vaskob36053d2020-03-26 15:49:30 +0100268 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
269 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
270 assert_true(!strncmp("text ", xmlctx->value, xmlctx->value_len));
271
272 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
273 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
274 assert_true(!strncmp("b", xmlctx->name, xmlctx->name_len));
275 assert_null(xmlctx->prefix);
276
277 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
278 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
279 assert_true(!strncmp("x", xmlctx->value, xmlctx->value_len));
280
281 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
282 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
283
284 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
285 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
286
287 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
288 assert_int_equal(LYXML_END, xmlctx->status);
289 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200290 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100291
292 /* tag mismatch */
David Sedlák54a6f132019-07-16 15:14:18 +0200293 str = "<a>text</b>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200294 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200295 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100296 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
297 assert_true(!strncmp("a", xmlctx->name, xmlctx->name_len));
298 assert_null(xmlctx->prefix);
David Sedlák54a6f132019-07-16 15:14:18 +0200299
Michal Vaskob36053d2020-03-26 15:49:30 +0100300 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
301 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
302 assert_true(!strncmp("text", xmlctx->value, xmlctx->value_len));
303
304 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200305 CHECK_LOG_CTX("Opening (\"a\") and closing (\"b\") elements tag mismatch.", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100306 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200307 ly_in_free(in, 0);
Radek Krejcid972c252018-09-25 13:23:39 +0200308}
309
310static void
311test_attribute(void **state)
312{
Michal Vaskob36053d2020-03-26 15:49:30 +0100313 const char *str;
314 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200315 struct ly_in *in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100316 struct lyxml_ns *ns;
Radek Krejcid972c252018-09-25 13:23:39 +0200317
Radek Krejcid972c252018-09-25 13:23:39 +0200318 /* not an attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +0100319 str = "<e unknown/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200320 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200321 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
322 CHECK_LOG_CTX("Invalid character sequence \"/>\", expected '='.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200323 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100324
325 str = "<e xxx=/>";
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_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
328 CHECK_LOG_CTX("Invalid character sequence \"/>\", expected either single or double quotation mark.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200329 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100330
331 str = "<e xxx\n = yyy/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200332 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200333 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
334 CHECK_LOG_CTX("Invalid character sequence \"yyy/>\", expected either single or double quotation mark.", "Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200335 ly_in_free(in, 0);
Radek Krejcid972c252018-09-25 13:23:39 +0200336
337 /* valid attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +0100338 str = "<e attr=\"val\"";
Michal Vasko63f3d842020-07-08 10:10:14 +0200339 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200340 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100341 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
342 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
343 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
344 assert_true(!strncmp("attr", xmlctx->name, xmlctx->name_len));
345 assert_null(xmlctx->prefix);
Radek Krejcid972c252018-09-25 13:23:39 +0200346
Michal Vaskob36053d2020-03-26 15:49:30 +0100347 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
348 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
Radek Iša85a55232021-02-16 10:44:43 +0100349 assert_true(!strncmp("val", xmlctx->value, xmlctx->value_len));
Michal Vaskob36053d2020-03-26 15:49:30 +0100350 assert_int_equal(xmlctx->ws_only, 0);
351 assert_int_equal(xmlctx->dynamic, 0);
352 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200353 ly_in_free(in, 0);
Radek Krejci28e8cb52019-03-08 11:31:31 +0100354
Michal Vaskob36053d2020-03-26 15:49:30 +0100355 /* valid namespace with prefix */
356 str = "<e xmlns:nc\n = \'urn\'/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200357 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200358 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100359 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
360 assert_int_equal(1, xmlctx->ns.count);
361 ns = (struct lyxml_ns *)xmlctx->ns.objs[0];
362 assert_string_equal(ns->prefix, "nc");
363 assert_string_equal(ns->uri, "urn");
364 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200365 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200366}
367
Radek Krejci7a7fa902018-09-25 17:08:21 +0200368static void
369test_text(void **state)
370{
Michal Vaskob36053d2020-03-26 15:49:30 +0100371 const char *str;
372 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200373 struct ly_in *in;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200374
375 /* empty attribute value */
Michal Vaskob36053d2020-03-26 15:49:30 +0100376 str = "<e a=\"\"";
Michal Vasko63f3d842020-07-08 10:10:14 +0200377 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200378 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100379 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
380 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
381 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Radek Krejcib1890642018-10-03 14:05:40 +0200382
Michal Vaskob36053d2020-03-26 15:49:30 +0100383 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
384 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
385 assert_true(!strncmp("", xmlctx->value, xmlctx->value_len));
386 assert_int_equal(xmlctx->ws_only, 1);
387 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200388 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100389
390 /* empty value but in single quotes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200391 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'\'", &in));
392 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100393 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100394 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100395 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
396 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
397 assert_true(!strncmp("", xmlctx->value, xmlctx->value_len));
398 assert_int_equal(xmlctx->ws_only, 1);
399 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200400 ly_in_free(in, 0);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200401
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200402 /* empty element content - only formating before defining child */
Michal Vasko63f3d842020-07-08 10:10:14 +0200403 assert_int_equal(LY_SUCCESS, ly_in_new_memory(">\n <y>", &in));
404 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100405 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100406 xmlctx->status = LYXML_ELEMENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100407 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
408 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
409 assert_true(!strncmp("\n ", xmlctx->value, xmlctx->value_len));
410 assert_int_equal(xmlctx->ws_only, 1);
411 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200412 ly_in_free(in, 0);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200413
Radek Krejci7a7fa902018-09-25 17:08:21 +0200414 /* empty element content is invalid - missing content terminating character < */
Michal Vasko63f3d842020-07-08 10:10:14 +0200415 assert_int_equal(LY_SUCCESS, ly_in_new_memory("", &in));
416 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100417 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100418 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100419 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100420 CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200421 ly_in_free(in, 0);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200422
Michal Vasko63f3d842020-07-08 10:10:14 +0200423 assert_int_equal(LY_SUCCESS, ly_in_new_memory("xxx", &in));
424 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100425 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100426 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100427 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100428 CHECK_LOG_CTX("Invalid character sequence \"xxx\", expected element tag start ('<').", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200429 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100430
431 lyxml_ctx_free(xmlctx);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200432
Radek Krejci7a7fa902018-09-25 17:08:21 +0200433 /* valid strings */
Radek Krejci339e2de2019-05-17 14:28:24 +0200434 str = "<a>€𠜎Øn \n&lt;&amp;&quot;&apos;&gt; &#82;&#x4f;&#x4B;</a>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200435 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200436 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100437 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
438
439 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
440 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
441 assert_true(!strncmp("€𠜎Øn \n<&\"\'> ROK", 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 Krejci7a7fa902018-09-25 17:08:21 +0200446
Radek Krejci117d2082018-09-26 10:05:14 +0200447 /* test using n-bytes UTF8 hexadecimal code points */
Michal Vasko63f3d842020-07-08 10:10:14 +0200448 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x0024;&#x00A2;&#x20ac;&#x10348;\'", &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_SUCCESS, lyxml_ctx_next(xmlctx));
453 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
454 assert_true(!strncmp("$¢€𐍈", xmlctx->value, xmlctx->value_len));
455 assert_int_equal(xmlctx->ws_only, 0);
456 assert_int_equal(xmlctx->dynamic, 1);
Michal Vasko8cf6f722022-02-18 13:08:23 +0100457 ly_in_free(in, 0);
458
459 /* CDATA value */
460 assert_int_equal(LY_SUCCESS, ly_in_new_memory("> <![CDATA[ special non-escaped chars <>&\"' ]]> </a>", &in));
461 xmlctx->in = in;
462 LOG_LOCINIT(NULL, NULL, NULL, in);
463 xmlctx->status = LYXML_ATTR_CONTENT;
464 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
465 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
466 assert_true(!strncmp(" special non-escaped chars <>&\"' ", xmlctx->value, xmlctx->value_len));
467 assert_int_equal(xmlctx->ws_only, 0);
468 assert_int_equal(xmlctx->dynamic, 1);
Michal Vaskob36053d2020-03-26 15:49:30 +0100469 free((char *)xmlctx->value);
Michal Vasko63f3d842020-07-08 10:10:14 +0200470 ly_in_free(in, 0);
Radek Krejcid70d1072018-10-09 14:20:47 +0200471
Radek Krejci7a7fa902018-09-25 17:08:21 +0200472 /* invalid characters in string */
Michal Vasko63f3d842020-07-08 10:10:14 +0200473 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x52\'", &in));
474 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100475 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100476 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100477 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100478 CHECK_LOG_CTX("Invalid character sequence \"'\", expected ;.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200479 ly_in_free(in, 0);
Radek Krejci117d2082018-09-26 10:05:14 +0200480
Michal Vasko63f3d842020-07-08 10:10:14 +0200481 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\"&#82\"", &in));
482 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100483 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100484 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100485 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100486 CHECK_LOG_CTX("Invalid character sequence \"\"\", expected ;.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200487 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100488
Michal Vasko63f3d842020-07-08 10:10:14 +0200489 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\"&nonsense;\"", &in));
490 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100491 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100492 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100493 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100494 CHECK_LOG_CTX("Entity reference \"&nonsense;\" not supported, only predefined references allowed.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200495 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100496
Michal Vasko63f3d842020-07-08 10:10:14 +0200497 assert_int_equal(LY_SUCCESS, ly_in_new_memory(">&#o122;", &in));
498 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100499 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100500 xmlctx->status = LYXML_ELEMENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100501 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100502 CHECK_LOG_CTX("Invalid character reference \"&#o122;\".", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200503 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100504
Michal Vasko63f3d842020-07-08 10:10:14 +0200505 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x06;\'", &in));
506 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100507 LOG_LOCINIT(NULL, NULL, NULL, 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));
Radek Krejcid54412f2020-12-17 20:25:35 +0100510 CHECK_LOG_CTX("Invalid character reference \"&#x06;\'\" (0x00000006).", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200511 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100512
Michal Vasko63f3d842020-07-08 10:10:14 +0200513 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#xfdd0;\'", &in));
514 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100515 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100516 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100517 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100518 CHECK_LOG_CTX("Invalid character reference \"&#xfdd0;\'\" (0x0000fdd0).", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200519 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100520
Michal Vasko63f3d842020-07-08 10:10:14 +0200521 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#xffff;\'", &in));
522 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100523 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100524 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100525 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100526 CHECK_LOG_CTX("Invalid character reference \"&#xffff;\'\" (0x0000ffff).", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200527 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100528
529 lyxml_ctx_free(xmlctx);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200530}
531
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200532static void
533test_ns(void **state)
534{
Michal Vaskob36053d2020-03-26 15:49:30 +0100535 const char *str;
536 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200537 struct ly_in *in;
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200538 const struct lyxml_ns *ns;
539
Michal Vaskob36053d2020-03-26 15:49:30 +0100540 /* opening element1 */
541 str = "<element1/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200542 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200543 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200544
Radek Krejcie0734d22019-04-05 15:54:28 +0200545 /* processing namespace definitions */
Michal Vaskob36053d2020-03-26 15:49:30 +0100546 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, NULL, 0, strdup("urn:default")));
547 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, "nc", 2, strdup("urn:nc1")));
Radek Krejcie0734d22019-04-05 15:54:28 +0200548 /* simulate adding open element2 into context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100549 xmlctx->elements.count++;
Radek Krejcie0734d22019-04-05 15:54:28 +0200550 /* processing namespace definitions */
Michal Vaskob36053d2020-03-26 15:49:30 +0100551 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, "nc", 2, strdup("urn:nc2")));
552 assert_int_equal(3, xmlctx->ns.count);
553 assert_int_not_equal(0, xmlctx->ns.size);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200554
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200555 ns = lyxml_ns_get(&xmlctx->ns, NULL, 0);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200556 assert_non_null(ns);
557 assert_null(ns->prefix);
558 assert_string_equal("urn:default", ns->uri);
559
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200560 ns = lyxml_ns_get(&xmlctx->ns, "nc", 2);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200561 assert_non_null(ns);
562 assert_string_equal("nc", ns->prefix);
563 assert_string_equal("urn:nc2", ns->uri);
564
Radek Krejcie0734d22019-04-05 15:54:28 +0200565 /* simulate closing element2 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100566 xmlctx->elements.count--;
567 lyxml_ns_rm(xmlctx);
568 assert_int_equal(2, xmlctx->ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200569
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200570 ns = lyxml_ns_get(&xmlctx->ns, "nc", 2);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200571 assert_non_null(ns);
572 assert_string_equal("nc", ns->prefix);
573 assert_string_equal("urn:nc1", ns->uri);
574
Michal Vaskob36053d2020-03-26 15:49:30 +0100575 /* close element1 */
576 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
577 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
578 assert_int_equal(0, xmlctx->ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200579
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200580 assert_null(lyxml_ns_get(&xmlctx->ns, "nc", 2));
581 assert_null(lyxml_ns_get(&xmlctx->ns, NULL, 0));
Michal Vaskob36053d2020-03-26 15:49:30 +0100582
583 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200584 ly_in_free(in, 0);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200585}
586
David Sedlákb3bed7a2019-03-08 11:53:37 +0100587static void
Radek Krejcie0734d22019-04-05 15:54:28 +0200588test_ns2(void **state)
589{
Michal Vaskob36053d2020-03-26 15:49:30 +0100590 const char *str;
591 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200592 struct ly_in *in;
Radek Krejcie0734d22019-04-05 15:54:28 +0200593
Michal Vaskob36053d2020-03-26 15:49:30 +0100594 /* opening element1 */
595 str = "<element1/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200596 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200597 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Radek Krejcie0734d22019-04-05 15:54:28 +0200598
Radek Krejcie0734d22019-04-05 15:54:28 +0200599 /* default namespace defined in parent element1 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100600 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, NULL, 0, strdup("urn:default")));
601 assert_int_equal(1, xmlctx->ns.count);
Radek Krejcie0734d22019-04-05 15:54:28 +0200602 /* going into child element1 */
Radek Krejcie0734d22019-04-05 15:54:28 +0200603 /* simulate adding open element1 into context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100604 xmlctx->elements.count++;
Radek Krejcie0734d22019-04-05 15:54:28 +0200605 /* no namespace defined, going out (first, simulate closing of so far open element) */
Michal Vaskob36053d2020-03-26 15:49:30 +0100606 xmlctx->elements.count--;
607 lyxml_ns_rm(xmlctx);
608 assert_int_equal(1, xmlctx->ns.count);
609
610 /* nothing else, going out of the parent element1 */
611 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
612 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
613 assert_int_equal(0, xmlctx->ns.count);
614
615 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200616 ly_in_free(in, 0);
Radek Krejcie0734d22019-04-05 15:54:28 +0200617}
618
Radek Krejcifad79c92019-06-04 11:43:30 +0200619static void
620test_simple_xml(void **state)
621{
Michal Vaskob36053d2020-03-26 15:49:30 +0100622 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200623 struct ly_in *in;
Radek Krejcie553e6d2019-06-07 15:33:18 +0200624 const char *test_input = "<elem1 attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>";
Radek Krejcifad79c92019-06-04 11:43:30 +0200625
Michal Vasko63f3d842020-07-08 10:10:14 +0200626 assert_int_equal(LY_SUCCESS, ly_in_new_memory(test_input, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200627 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100628 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200629 assert_string_equal(xmlctx->in->current, "attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200630
Michal Vaskob36053d2020-03-26 15:49:30 +0100631 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
632 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200633 assert_string_equal(xmlctx->in->current, "=\"value\"> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200634
Michal Vaskob36053d2020-03-26 15:49:30 +0100635 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
636 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200637 assert_string_equal(xmlctx->in->current, "> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200638
Michal Vaskob36053d2020-03-26 15:49:30 +0100639 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
640 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200641 assert_string_equal(xmlctx->in->current, "<elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200642
Michal Vaskob36053d2020-03-26 15:49:30 +0100643 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
644 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200645 assert_string_equal(xmlctx->in->current, "attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200646
Michal Vaskob36053d2020-03-26 15:49:30 +0100647 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
648 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200649 assert_string_equal(xmlctx->in->current, "=\"value\" /> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200650
Michal Vaskob36053d2020-03-26 15:49:30 +0100651 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
652 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200653 assert_string_equal(xmlctx->in->current, " /> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200654
Michal Vaskob36053d2020-03-26 15:49:30 +0100655 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
656 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200657 assert_string_equal(xmlctx->in->current, "/> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200658
Michal Vaskob36053d2020-03-26 15:49:30 +0100659 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
660 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200661 assert_string_equal(xmlctx->in->current, " </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200662
Michal Vaskob36053d2020-03-26 15:49:30 +0100663 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
664 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200665 assert_string_equal(xmlctx->in->current, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100666
667 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
668 assert_int_equal(LYXML_END, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200669 assert_string_equal(xmlctx->in->current, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100670
671 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200672 ly_in_free(in, 0);
Radek Krejcifad79c92019-06-04 11:43:30 +0200673}
674
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100675int
676main(void)
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200677{
678 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200679 UTEST(test_element),
680 UTEST(test_attribute),
681 UTEST(test_text),
682 UTEST(test_ns),
683 UTEST(test_ns2),
684 UTEST(test_simple_xml),
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200685 };
686
Radek Iša56ca9e42020-09-08 18:42:00 +0200687 return cmocka_run_group_tests(tests, NULL, NULL);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200688}