blob: 8bf6bed8b2c75d62213d77651b2a2ca5cb7da251 [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 */
Michal Vasko8cf6f722022-02-18 13:08:23 +0100124 str = "<?xml version=\"1.0\"?> <!-- comment --> <?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);
Michal Vasko8cf6f722022-02-18 13:08:23 +0100443 ly_in_free(in, 0);
444
445 /* CDATA value */
446 assert_int_equal(LY_SUCCESS, ly_in_new_memory("> <![CDATA[ special non-escaped chars <>&\"' ]]> </a>", &in));
447 xmlctx->in = in;
448 LOG_LOCINIT(NULL, NULL, NULL, in);
449 xmlctx->status = LYXML_ATTR_CONTENT;
450 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
451 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
452 assert_true(!strncmp(" special non-escaped chars <>&\"' ", xmlctx->value, xmlctx->value_len));
453 assert_int_equal(xmlctx->ws_only, 0);
454 assert_int_equal(xmlctx->dynamic, 1);
Michal Vaskob36053d2020-03-26 15:49:30 +0100455 free((char *)xmlctx->value);
Michal Vasko63f3d842020-07-08 10:10:14 +0200456 ly_in_free(in, 0);
Radek Krejcid70d1072018-10-09 14:20:47 +0200457
Radek Krejci7a7fa902018-09-25 17:08:21 +0200458 /* invalid characters in string */
Michal Vasko63f3d842020-07-08 10:10:14 +0200459 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x52\'", &in));
460 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100461 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100462 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100463 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100464 CHECK_LOG_CTX("Invalid character sequence \"'\", expected ;.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200465 ly_in_free(in, 0);
Radek Krejci117d2082018-09-26 10:05:14 +0200466
Michal Vasko63f3d842020-07-08 10:10:14 +0200467 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\"&#82\"", &in));
468 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100469 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100470 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100471 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100472 CHECK_LOG_CTX("Invalid character sequence \"\"\", expected ;.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200473 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100474
Michal Vasko63f3d842020-07-08 10:10:14 +0200475 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\"&nonsense;\"", &in));
476 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100477 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100478 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100479 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100480 CHECK_LOG_CTX("Entity reference \"&nonsense;\" not supported, only predefined references allowed.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200481 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100482
Michal Vasko63f3d842020-07-08 10:10:14 +0200483 assert_int_equal(LY_SUCCESS, ly_in_new_memory(">&#o122;", &in));
484 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100485 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100486 xmlctx->status = LYXML_ELEMENT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100487 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100488 CHECK_LOG_CTX("Invalid character reference \"&#o122;\".", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200489 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100490
Michal Vasko63f3d842020-07-08 10:10:14 +0200491 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#x06;\'", &in));
492 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100493 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100494 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100495 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100496 CHECK_LOG_CTX("Invalid character reference \"&#x06;\'\" (0x00000006).", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200497 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100498
Michal Vasko63f3d842020-07-08 10:10:14 +0200499 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#xfdd0;\'", &in));
500 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100501 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100502 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100503 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100504 CHECK_LOG_CTX("Invalid character reference \"&#xfdd0;\'\" (0x0000fdd0).", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200505 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100506
Michal Vasko63f3d842020-07-08 10:10:14 +0200507 assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'&#xffff;\'", &in));
508 xmlctx->in = in;
Radek Krejciddace2c2021-01-08 11:30:56 +0100509 LOG_LOCINIT(NULL, NULL, NULL, in);
Michal Vaskob36053d2020-03-26 15:49:30 +0100510 xmlctx->status = LYXML_ATTRIBUTE;
Michal Vaskob36053d2020-03-26 15:49:30 +0100511 assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx));
Radek Krejcid54412f2020-12-17 20:25:35 +0100512 CHECK_LOG_CTX("Invalid character reference \"&#xffff;\'\" (0x0000ffff).", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200513 ly_in_free(in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100514
515 lyxml_ctx_free(xmlctx);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200516}
517
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200518static void
519test_ns(void **state)
520{
Michal Vaskob36053d2020-03-26 15:49:30 +0100521 const char *str;
522 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200523 struct ly_in *in;
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200524 const struct lyxml_ns *ns;
525
Michal Vaskob36053d2020-03-26 15:49:30 +0100526 /* opening element1 */
527 str = "<element1/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200528 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200529 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200530
Radek Krejcie0734d22019-04-05 15:54:28 +0200531 /* processing namespace definitions */
Michal Vaskob36053d2020-03-26 15:49:30 +0100532 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, NULL, 0, strdup("urn:default")));
533 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, "nc", 2, strdup("urn:nc1")));
Radek Krejcie0734d22019-04-05 15:54:28 +0200534 /* simulate adding open element2 into context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100535 xmlctx->elements.count++;
Radek Krejcie0734d22019-04-05 15:54:28 +0200536 /* processing namespace definitions */
Michal Vaskob36053d2020-03-26 15:49:30 +0100537 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, "nc", 2, strdup("urn:nc2")));
538 assert_int_equal(3, xmlctx->ns.count);
539 assert_int_not_equal(0, xmlctx->ns.size);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200540
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200541 ns = lyxml_ns_get(&xmlctx->ns, NULL, 0);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200542 assert_non_null(ns);
543 assert_null(ns->prefix);
544 assert_string_equal("urn:default", ns->uri);
545
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200546 ns = lyxml_ns_get(&xmlctx->ns, "nc", 2);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200547 assert_non_null(ns);
548 assert_string_equal("nc", ns->prefix);
549 assert_string_equal("urn:nc2", ns->uri);
550
Radek Krejcie0734d22019-04-05 15:54:28 +0200551 /* simulate closing element2 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100552 xmlctx->elements.count--;
553 lyxml_ns_rm(xmlctx);
554 assert_int_equal(2, xmlctx->ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200555
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200556 ns = lyxml_ns_get(&xmlctx->ns, "nc", 2);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200557 assert_non_null(ns);
558 assert_string_equal("nc", ns->prefix);
559 assert_string_equal("urn:nc1", ns->uri);
560
Michal Vaskob36053d2020-03-26 15:49:30 +0100561 /* close element1 */
562 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
563 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
564 assert_int_equal(0, xmlctx->ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200565
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200566 assert_null(lyxml_ns_get(&xmlctx->ns, "nc", 2));
567 assert_null(lyxml_ns_get(&xmlctx->ns, NULL, 0));
Michal Vaskob36053d2020-03-26 15:49:30 +0100568
569 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200570 ly_in_free(in, 0);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200571}
572
David Sedlákb3bed7a2019-03-08 11:53:37 +0100573static void
Radek Krejcie0734d22019-04-05 15:54:28 +0200574test_ns2(void **state)
575{
Michal Vaskob36053d2020-03-26 15:49:30 +0100576 const char *str;
577 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200578 struct ly_in *in;
Radek Krejcie0734d22019-04-05 15:54:28 +0200579
Michal Vaskob36053d2020-03-26 15:49:30 +0100580 /* opening element1 */
581 str = "<element1/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200582 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200583 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Radek Krejcie0734d22019-04-05 15:54:28 +0200584
Radek Krejcie0734d22019-04-05 15:54:28 +0200585 /* default namespace defined in parent element1 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100586 assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, NULL, 0, strdup("urn:default")));
587 assert_int_equal(1, xmlctx->ns.count);
Radek Krejcie0734d22019-04-05 15:54:28 +0200588 /* going into child element1 */
Radek Krejcie0734d22019-04-05 15:54:28 +0200589 /* simulate adding open element1 into context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100590 xmlctx->elements.count++;
Radek Krejcie0734d22019-04-05 15:54:28 +0200591 /* no namespace defined, going out (first, simulate closing of so far open element) */
Michal Vaskob36053d2020-03-26 15:49:30 +0100592 xmlctx->elements.count--;
593 lyxml_ns_rm(xmlctx);
594 assert_int_equal(1, xmlctx->ns.count);
595
596 /* nothing else, going out of the parent element1 */
597 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
598 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
599 assert_int_equal(0, xmlctx->ns.count);
600
601 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200602 ly_in_free(in, 0);
Radek Krejcie0734d22019-04-05 15:54:28 +0200603}
604
Radek Krejcifad79c92019-06-04 11:43:30 +0200605static void
606test_simple_xml(void **state)
607{
Michal Vaskob36053d2020-03-26 15:49:30 +0100608 struct lyxml_ctx *xmlctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200609 struct ly_in *in;
Radek Krejcie553e6d2019-06-07 15:33:18 +0200610 const char *test_input = "<elem1 attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>";
Radek Krejcifad79c92019-06-04 11:43:30 +0200611
Michal Vasko63f3d842020-07-08 10:10:14 +0200612 assert_int_equal(LY_SUCCESS, ly_in_new_memory(test_input, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200613 assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100614 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200615 assert_string_equal(xmlctx->in->current, "attr1=\"value\"> <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_ATTRIBUTE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200619 assert_string_equal(xmlctx->in->current, "=\"value\"> <elem2 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_ATTR_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200623 assert_string_equal(xmlctx->in->current, "> <elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200624
Michal Vaskob36053d2020-03-26 15:49:30 +0100625 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
626 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200627 assert_string_equal(xmlctx->in->current, "<elem2 attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200628
Michal Vaskob36053d2020-03-26 15:49:30 +0100629 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
630 assert_int_equal(LYXML_ELEMENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200631 assert_string_equal(xmlctx->in->current, "attr2=\"value\" /> </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200632
Michal Vaskob36053d2020-03-26 15:49:30 +0100633 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
634 assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200635 assert_string_equal(xmlctx->in->current, "=\"value\" /> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200636
Michal Vaskob36053d2020-03-26 15:49:30 +0100637 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
638 assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200639 assert_string_equal(xmlctx->in->current, " /> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200640
Michal Vaskob36053d2020-03-26 15:49:30 +0100641 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
642 assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200643 assert_string_equal(xmlctx->in->current, "/> </elem1>");
Radek Krejcie553e6d2019-06-07 15:33:18 +0200644
Michal Vaskob36053d2020-03-26 15:49:30 +0100645 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
646 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200647 assert_string_equal(xmlctx->in->current, " </elem1>");
Radek Krejcifad79c92019-06-04 11:43:30 +0200648
Michal Vaskob36053d2020-03-26 15:49:30 +0100649 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
650 assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200651 assert_string_equal(xmlctx->in->current, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100652
653 assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx));
654 assert_int_equal(LYXML_END, xmlctx->status);
Michal Vasko63f3d842020-07-08 10:10:14 +0200655 assert_string_equal(xmlctx->in->current, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100656
657 lyxml_ctx_free(xmlctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200658 ly_in_free(in, 0);
Radek Krejcifad79c92019-06-04 11:43:30 +0200659}
660
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100661int
662main(void)
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200663{
664 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200665 UTEST(test_element),
666 UTEST(test_attribute),
667 UTEST(test_text),
668 UTEST(test_ns),
669 UTEST(test_ns2),
670 UTEST(test_simple_xml),
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200671 };
672
Radek Iša56ca9e42020-09-08 18:42:00 +0200673 return cmocka_run_group_tests(tests, NULL, NULL);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200674}