blob: 7e8132de63d318533259fd259ee54b45367c20f8 [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
Radek Krejcid91dbaf2018-09-21 15:51:39 +020071 /* unqualified element */
72 str = " < element/>";
Michal Vasko63f3d842020-07-08 10:10:14 +020073 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +020074 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +010075 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
76 assert_null(xmlctx->prefix);
77 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
78 assert_int_equal(1, xmlctx->elements.count);
Radek Krejci28e8cb52019-03-08 11:31:31 +010079
Michal Vaskob36053d2020-03-26 15:49:30 +010080 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
81 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
82 assert_true(!strncmp("", xmlctx->value, xmlctx->value_len));
83
84 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
85 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
86
87 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
88 assert_int_equal(LYXML_END, xmlctx->status);
89 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +020090 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +010091
92 /* element with attribute */
Radek Krejci28e8cb52019-03-08 11:31:31 +010093 str = " < element attr=\'x\'/>";
Michal Vasko63f3d842020-07-08 10:10:14 +020094 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +020095 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +010096 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
97 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
98 assert_null(xmlctx->prefix);
99 assert_int_equal(1, xmlctx->elements.count);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200100
Michal Vaskob36053d2020-03-26 15:49:30 +0100101 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
102 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
103 assert_true(!strncmp("attr", xmlctx->name, xmlctx->name_len));
104 assert_null(xmlctx->prefix);
105
106 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
107 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
108 assert_int_equal(1, xmlctx->elements.count);
109 assert_true(!strncmp("x", xmlctx->value, xmlctx->value_len));
110
111 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
112 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
113
114 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
115 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
116 assert_int_equal(0, xmlctx->elements.count);
117
118 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
119 assert_int_equal(LYXML_END, xmlctx->status);
120 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200121 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100122
123 /* headers and comments */
Radek Krejcifb7c6582018-09-21 16:12:45 +0200124 str = "<?xml version=\"1.0\"?> <!-- comment --> <![CDATA[<greeting>Hello, world!</greeting>]]> <?TEST xxx?> <element/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200125 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200126 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100127 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
128 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
129 assert_null(xmlctx->prefix);
130 assert_int_equal(1, xmlctx->elements.count);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200131
Michal Vaskob36053d2020-03-26 15:49:30 +0100132 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
133 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
134
135 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
136 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
137
138 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
139 assert_int_equal(LYXML_END, xmlctx->status);
140 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200141 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100142
143 /* separate opening and closing tags, neamespaced parsed internally */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200144 str = "<element xmlns=\"urn\"></element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200145 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200146 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100147 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
148 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
149 assert_null(xmlctx->prefix);
150 assert_int_equal(1, xmlctx->elements.count);
151 assert_int_equal(1, xmlctx->ns.count);
152
153 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
154 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
155
156 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
157 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
158 assert_int_equal(0, xmlctx->elements.count);
159 assert_int_equal(0, xmlctx->ns.count);
160
161 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
162 assert_int_equal(LYXML_END, xmlctx->status);
163 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200164 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200165
166 /* qualified element */
167 str = " < yin:element/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200168 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200169 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100170 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
171 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
172 assert_true(!strncmp("yin", xmlctx->prefix, xmlctx->prefix_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200173
Michal Vaskob36053d2020-03-26 15:49:30 +0100174 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
175 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
176
177 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
178 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
179
180 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
181 assert_int_equal(LYXML_END, xmlctx->status);
182 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200183 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100184
185 /* non-matching closing tag */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200186 str = "<yin:element xmlns=\"urn\"></element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200187 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200188 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100189 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
190 assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len));
191 assert_true(!strncmp("yin", xmlctx->prefix, xmlctx->prefix_len));
192 assert_int_equal(1, xmlctx->elements.count);
193 assert_int_equal(1, xmlctx->ns.count);
194
195 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
196 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
197
198 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200199 CHECK_LOG_CTX("Opening (\"yin:element\") and closing (\"element\") elements tag mismatch.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200200 lyxml_ctx_free(xmlctx);
201 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100202
Michal Vasko63f3d842020-07-08 10:10:14 +0200203 /* invalid closing tag */
204 str = "<yin:element xmlns=\"urn\"></yin:element/>";
205 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200206 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vasko63f3d842020-07-08 10:10:14 +0200207 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100208 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200209 CHECK_LOG_CTX("Invalid character sequence \"/>\", expected element tag termination ('>').", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100210 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200211 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200212
213 /* UTF8 characters */
214 str = "<𠜎€𠜎Øn:𠜎€𠜎Øn/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200215 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200216 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100217 assert_true(!strncmp("𠜎€𠜎Øn", xmlctx->name, xmlctx->name_len));
218 assert_true(!strncmp("𠜎€𠜎Øn", xmlctx->prefix, xmlctx->prefix_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200219
Michal Vaskob36053d2020-03-26 15:49:30 +0100220 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
221 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
222
223 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
224 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
225
226 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
227 assert_int_equal(LYXML_END, xmlctx->status);
228 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200229 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100230
231 /* invalid UTF-8 characters */
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200232 str = "<¢:element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200233 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200234 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
235 CHECK_LOG_CTX("Identifier \"¢:element>\" starts with an invalid character.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200236 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100237
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200238 str = "<yin:c⁐element>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200239 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200240 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100241 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200242 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 +0100243 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200244 ly_in_free(in, 0);
Radek Krejci339e2de2019-05-17 14:28:24 +0200245
246 /* mixed content */
247 str = "<a>text <b>x</b></a>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200248 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200249 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100250 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
251 assert_true(!strncmp("a", xmlctx->name, xmlctx->name_len));
252 assert_null(xmlctx->prefix);
Radek Krejci339e2de2019-05-17 14:28:24 +0200253
Michal Vaskob36053d2020-03-26 15:49:30 +0100254 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
255 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
256 assert_true(!strncmp("text ", xmlctx->value, xmlctx->value_len));
257
258 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
259 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
260 assert_true(!strncmp("b", xmlctx->name, xmlctx->name_len));
261 assert_null(xmlctx->prefix);
262
263 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
264 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
265 assert_true(!strncmp("x", xmlctx->value, xmlctx->value_len));
266
267 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
268 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
269
270 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
271 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
272
273 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
274 assert_int_equal(LYXML_END, xmlctx->status);
275 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200276 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100277
278 /* tag mismatch */
David Sedlák54a6f132019-07-16 15:14:18 +0200279 str = "<a>text</b>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200280 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200281 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100282 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
283 assert_true(!strncmp("a", xmlctx->name, xmlctx->name_len));
284 assert_null(xmlctx->prefix);
David Sedlák54a6f132019-07-16 15:14:18 +0200285
Michal Vaskob36053d2020-03-26 15:49:30 +0100286 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
287 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
288 assert_true(!strncmp("text", xmlctx->value, xmlctx->value_len));
289
290 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200291 CHECK_LOG_CTX("Opening (\"a\") and closing (\"b\") elements tag mismatch.", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100292 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200293 ly_in_free(in, 0);
Radek Krejcid972c252018-09-25 13:23:39 +0200294}
295
296static void
297test_attribute(void **state)
298{
Michal Vaskob36053d2020-03-26 15:49:30 +0100299 const char *str;
300 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200301 struct ly_in *in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100302 struct lyxml_ns *ns;
Radek Krejcid972c252018-09-25 13:23:39 +0200303
Radek Krejcid972c252018-09-25 13:23:39 +0200304 /* not an attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +0100305 str = "<e unknown/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200306 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200307 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
308 CHECK_LOG_CTX("Invalid character sequence \"/>\", expected '='.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200309 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100310
311 str = "<e xxx=/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200312 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200313 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
314 CHECK_LOG_CTX("Invalid character sequence \"/>\", expected either single or double quotation mark.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200315 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100316
317 str = "<e xxx\n = yyy/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200318 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200319 assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
320 CHECK_LOG_CTX("Invalid character sequence \"yyy/>\", expected either single or double quotation mark.", "Line number 2.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200321 ly_in_free(in, 0);
Radek Krejcid972c252018-09-25 13:23:39 +0200322
323 /* valid attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +0100324 str = "<e attr=\"val\"";
Michal Vasko63f3d842020-07-08 10:10:14 +0200325 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200326 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100327 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
328 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
329 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
330 assert_true(!strncmp("attr", xmlctx->name, xmlctx->name_len));
331 assert_null(xmlctx->prefix);
Radek Krejcid972c252018-09-25 13:23:39 +0200332
Michal Vaskob36053d2020-03-26 15:49:30 +0100333 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
334 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
Radek Iša85a55232021-02-16 10:44:43 +0100335 assert_true(!strncmp("val", xmlctx->value, xmlctx->value_len));
Michal Vaskob36053d2020-03-26 15:49:30 +0100336 assert_int_equal(xmlctx->ws_only, 0);
337 assert_int_equal(xmlctx->dynamic, 0);
338 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200339 ly_in_free(in, 0);
Radek Krejci28e8cb52019-03-08 11:31:31 +0100340
Michal Vaskob36053d2020-03-26 15:49:30 +0100341 /* valid namespace with prefix */
342 str = "<e xmlns:nc\n = \'urn\'/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200343 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200344 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100345 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
346 assert_int_equal(1, xmlctx->ns.count);
347 ns = (struct lyxml_ns *)xmlctx->ns.objs[0];
348 assert_string_equal(ns->prefix, "nc");
349 assert_string_equal(ns->uri, "urn");
350 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200351 ly_in_free(in, 0);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200352}
353
Radek Krejci7a7fa902018-09-25 17:08:21 +0200354static void
355test_text(void **state)
356{
Michal Vaskob36053d2020-03-26 15:49:30 +0100357 const char *str;
358 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200359 struct ly_in *in;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200360
361 /* empty attribute value */
Michal Vaskob36053d2020-03-26 15:49:30 +0100362 str = "<e a=\"\"";
Michal Vasko63f3d842020-07-08 10:10:14 +0200363 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200364 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100365 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
366 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
367 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Radek Krejcib1890642018-10-03 14:05:40 +0200368
Michal Vaskob36053d2020-03-26 15:49:30 +0100369 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
370 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
371 assert_true(!strncmp("", xmlctx->value, xmlctx->value_len));
372 assert_int_equal(xmlctx->ws_only, 1);
373 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200374 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100375
376 /* empty value but in single quotes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200377 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'\'", &in));
378 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100379 LOG_LOCINIT(NULL, NULL, NULL, 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;
Radek Krejciddace2c2021-01-08 11:30:56 +0100391 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100392 xmlctx->status = LYXML_ELEMENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100393 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
394 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
395 assert_true(!strncmp("\n ", xmlctx->value, xmlctx->value_len));
396 assert_int_equal(xmlctx->ws_only, 1);
397 assert_int_equal(xmlctx->dynamic, 0);
Michal Vasko63f3d842020-07-08 10:10:14 +0200398 ly_in_free(in, 0);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200399
Radek Krejci7a7fa902018-09-25 17:08:21 +0200400 /* empty element content is invalid - missing content terminating character < */
Michal Vasko63f3d842020-07-08 10:10:14 +0200401 assert_int_equal(LY_SUCCESS, ly_in_new_memory("", &in));
402 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100403 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100404 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100405 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100406 CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200407 ly_in_free(in, 0);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200408
Michal Vasko63f3d842020-07-08 10:10:14 +0200409 assert_int_equal(LY_SUCCESS, ly_in_new_memory("xxx", &in));
410 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100411 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100412 xmlctx->status = LYXML_ELEM_CONTENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100413 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100414 CHECK_LOG_CTX("Invalid character sequence \"xxx\", expected element tag start ('<').", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200415 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100416
417 lyxml_ctx_free(xmlctx);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200418
Radek Krejci7a7fa902018-09-25 17:08:21 +0200419 /* valid strings */
Radek Krejci339e2de2019-05-17 14:28:24 +0200420 str = "<a>€𠜎Øn \n&lt;&amp;&quot;&apos;&gt; &#82;&#x4f;&#x4B;</a>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200421 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200422 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100423 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
424
425 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
426 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
427 assert_true(!strncmp("€𠜎Øn \n<&\"\'> ROK", xmlctx->value, xmlctx->value_len));
428 assert_int_equal(xmlctx->ws_only, 0);
429 assert_int_equal(xmlctx->dynamic, 1);
430 free((char *)xmlctx->value);
Michal Vasko63f3d842020-07-08 10:10:14 +0200431 ly_in_free(in, 0);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200432
Radek Krejci117d2082018-09-26 10:05:14 +0200433 /* test using n-bytes UTF8 hexadecimal code points */
Michal Vasko63f3d842020-07-08 10:10:14 +0200434 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x0024;&#x00A2;&#x20ac;&#x10348;\'", &in));
435 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100436 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100437 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100438 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
439 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
440 assert_true(!strncmp("$¢€𐍈", xmlctx->value, xmlctx->value_len));
441 assert_int_equal(xmlctx->ws_only, 0);
442 assert_int_equal(xmlctx->dynamic, 1);
443 free((char *)xmlctx->value);
Michal Vasko63f3d842020-07-08 10:10:14 +0200444 ly_in_free(in, 0);
Radek Krejcid70d1072018-10-09 14:20:47 +0200445
Radek Krejci7a7fa902018-09-25 17:08:21 +0200446 /* invalid characters in string */
Michal Vasko63f3d842020-07-08 10:10:14 +0200447 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x52\'", &in));
448 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100449 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100450 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100451 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100452 CHECK_LOG_CTX("Invalid character sequence \"'\", expected ;.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200453 ly_in_free(in, 0);
Radek Krejci117d2082018-09-26 10:05:14 +0200454
Michal Vasko63f3d842020-07-08 10:10:14 +0200455 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\"&#82\"", &in));
456 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100457 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100458 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100459 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100460 CHECK_LOG_CTX("Invalid character sequence \"\"\", expected ;.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200461 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100462
Michal Vasko63f3d842020-07-08 10:10:14 +0200463 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\"&nonsense;\"", &in));
464 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100465 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100466 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100467 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100468 CHECK_LOG_CTX("Entity reference \"&nonsense;\" not supported, only predefined references allowed.", "Line number 1.");
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(">&#o122;", &in));
472 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100473 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100474 xmlctx->status = LYXML_ELEMENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100475 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100476 CHECK_LOG_CTX("Invalid character reference \"&#o122;\".", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200477 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100478
Michal Vasko63f3d842020-07-08 10:10:14 +0200479 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x06;\'", &in));
480 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100481 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100482 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100483 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100484 CHECK_LOG_CTX("Invalid character reference \"&#x06;\'\" (0x00000006).", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200485 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100486
Michal Vasko63f3d842020-07-08 10:10:14 +0200487 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#xfdd0;\'", &in));
488 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100489 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100490 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100491 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100492 CHECK_LOG_CTX("Invalid character reference \"&#xfdd0;\'\" (0x0000fdd0).", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200493 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100494
Michal Vasko63f3d842020-07-08 10:10:14 +0200495 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#xffff;\'", &in));
496 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100497 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100498 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100499 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100500 CHECK_LOG_CTX("Invalid character reference \"&#xffff;\'\" (0x0000ffff).", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200501 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100502
503 lyxml_ctx_free(xmlctx);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200504}
505
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200506static void
507test_ns(void **state)
508{
Michal Vaskob36053d2020-03-26 15:49:30 +0100509 const char *str;
510 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200511 struct ly_in *in;
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200512 const struct lyxml_ns *ns;
513
Michal Vaskob36053d2020-03-26 15:49:30 +0100514 /* opening element1 */
515 str = "<element1/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200516 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200517 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200518
Radek Krejcie0734d22019-04-05 15:54:28 +0200519 /* processing namespace definitions */
Michal Vaskob36053d2020-03-26 15:49:30 +0100520 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, NULL, 0, strdup("urn:default")));
521 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, "nc", 2, strdup("urn:nc1")));
Radek Krejcie0734d22019-04-05 15:54:28 +0200522 /* simulate adding open element2 into context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100523 xmlctx->elements.count++;
Radek Krejcie0734d22019-04-05 15:54:28 +0200524 /* processing namespace definitions */
Michal Vaskob36053d2020-03-26 15:49:30 +0100525 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, "nc", 2, strdup("urn:nc2")));
526 assert_int_equal(3, xmlctx->ns.count);
527 assert_int_not_equal(0, xmlctx->ns.size);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200528
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200529 ns = lyxml_ns_get(&xmlctx->ns, NULL, 0);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200530 assert_non_null(ns);
531 assert_null(ns->prefix);
532 assert_string_equal("urn:default", ns->uri);
533
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200534 ns = lyxml_ns_get(&xmlctx->ns, "nc", 2);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200535 assert_non_null(ns);
536 assert_string_equal("nc", ns->prefix);
537 assert_string_equal("urn:nc2", ns->uri);
538
Radek Krejcie0734d22019-04-05 15:54:28 +0200539 /* simulate closing element2 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100540 xmlctx->elements.count--;
541 lyxml_ns_rm(xmlctx);
542 assert_int_equal(2, xmlctx->ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200543
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200544 ns = lyxml_ns_get(&xmlctx->ns, "nc", 2);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200545 assert_non_null(ns);
546 assert_string_equal("nc", ns->prefix);
547 assert_string_equal("urn:nc1", ns->uri);
548
Michal Vaskob36053d2020-03-26 15:49:30 +0100549 /* close element1 */
550 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
551 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
552 assert_int_equal(0, xmlctx->ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200553
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200554 assert_null(lyxml_ns_get(&xmlctx->ns, "nc", 2));
555 assert_null(lyxml_ns_get(&xmlctx->ns, NULL, 0));
Michal Vaskob36053d2020-03-26 15:49:30 +0100556
557 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200558 ly_in_free(in, 0);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200559}
560
David Sedlákb3bed7a2019-03-08 11:53:37 +0100561static void
Radek Krejcie0734d22019-04-05 15:54:28 +0200562test_ns2(void **state)
563{
Michal Vaskob36053d2020-03-26 15:49:30 +0100564 const char *str;
565 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200566 struct ly_in *in;
Radek Krejcie0734d22019-04-05 15:54:28 +0200567
Michal Vaskob36053d2020-03-26 15:49:30 +0100568 /* opening element1 */
569 str = "<element1/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200570 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200571 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Radek Krejcie0734d22019-04-05 15:54:28 +0200572
Radek Krejcie0734d22019-04-05 15:54:28 +0200573 /* default namespace defined in parent element1 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100574 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, NULL, 0, strdup("urn:default")));
575 assert_int_equal(1, xmlctx->ns.count);
Radek Krejcie0734d22019-04-05 15:54:28 +0200576 /* going into child element1 */
Radek Krejcie0734d22019-04-05 15:54:28 +0200577 /* simulate adding open element1 into context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100578 xmlctx->elements.count++;
Radek Krejcie0734d22019-04-05 15:54:28 +0200579 /* no namespace defined, going out (first, simulate closing of so far open element) */
Michal Vaskob36053d2020-03-26 15:49:30 +0100580 xmlctx->elements.count--;
581 lyxml_ns_rm(xmlctx);
582 assert_int_equal(1, xmlctx->ns.count);
583
584 /* nothing else, going out of the parent element1 */
585 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
586 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
587 assert_int_equal(0, xmlctx->ns.count);
588
589 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200590 ly_in_free(in, 0);
Radek Krejcie0734d22019-04-05 15:54:28 +0200591}
592
Radek Krejcifad79c92019-06-04 11:43:30 +0200593static void
594test_simple_xml(void **state)
595{
Michal Vaskob36053d2020-03-26 15:49:30 +0100596 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200597 struct ly_in *in;
Radek Krejcie553e6d2019-06-07 15:33:18 +0200598 const char *test_input = "<elem1 attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>";
Radek Krejcifad79c92019-06-04 11:43:30 +0200599
Michal Vasko63f3d842020-07-08 10:10:14 +0200600 assert_int_equal(LY_SUCCESS, ly_in_new_memory(test_input, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200601 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100602 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200603 assert_string_equal(xmlctx->in->current, "attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200604
Michal Vaskob36053d2020-03-26 15:49:30 +0100605 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
606 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200607 assert_string_equal(xmlctx->in->current, "=\"value\"> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200608
Michal Vaskob36053d2020-03-26 15:49:30 +0100609 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
610 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200611 assert_string_equal(xmlctx->in->current, "> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200612
Michal Vaskob36053d2020-03-26 15:49:30 +0100613 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
614 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200615 assert_string_equal(xmlctx->in->current, "<elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200616
Michal Vaskob36053d2020-03-26 15:49:30 +0100617 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
618 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200619 assert_string_equal(xmlctx->in->current, "attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200620
Michal Vaskob36053d2020-03-26 15:49:30 +0100621 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
622 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200623 assert_string_equal(xmlctx->in->current, "=\"value\" /> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200624
Michal Vaskob36053d2020-03-26 15:49:30 +0100625 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
626 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200627 assert_string_equal(xmlctx->in->current, " /> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200628
Michal Vaskob36053d2020-03-26 15:49:30 +0100629 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
630 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200631 assert_string_equal(xmlctx->in->current, "/> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200632
Michal Vaskob36053d2020-03-26 15:49:30 +0100633 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
634 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200635 assert_string_equal(xmlctx->in->current, " </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200636
Michal Vaskob36053d2020-03-26 15:49:30 +0100637 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
638 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200639 assert_string_equal(xmlctx->in->current, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100640
641 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
642 assert_int_equal(LYXML_END, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200643 assert_string_equal(xmlctx->in->current, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100644
645 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200646 ly_in_free(in, 0);
Radek Krejcifad79c92019-06-04 11:43:30 +0200647}
648
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100649int
650main(void)
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200651{
652 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200653 UTEST(test_element),
654 UTEST(test_attribute),
655 UTEST(test_text),
656 UTEST(test_ns),
657 UTEST(test_ns2),
658 UTEST(test_simple_xml),
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200659 };
660
Radek Iša56ca9e42020-09-08 18:42:00 +0200661 return cmocka_run_group_tests(tests, NULL, NULL);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200662}