blob: 0aacf298ebde082bf7bef7a559f99079a46e3faf [file] [log] [blame]
Radek Krejcid91dbaf2018-09-21 15:51:39 +02001/*
2 * @file xml.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from xml.c
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejcib7db73a2018-10-24 14:18:40 +020015#include "common.h"
Radek Krejcif3f47842018-11-15 11:22:15 +010016#include "../../src/set.c"
17#include "../../src/xml.c"
18#include "../../src/common.c"
19#include "../../src/log.c"
Radek Krejcib7db73a2018-10-24 14:18:40 +020020
Radek Krejcid91dbaf2018-09-21 15:51:39 +020021#include <stdarg.h>
22#include <stddef.h>
23#include <setjmp.h>
24#include <cmocka.h>
25
26#include <stdio.h>
27#include <string.h>
28
29#include "libyang.h"
Radek Krejcid91dbaf2018-09-21 15:51:39 +020030
31#define BUFSIZE 1024
32char logbuf[BUFSIZE] = {0};
33
34/* set to 0 to printing error messages to stderr instead of checking them in code */
35#define ENABLE_LOGGER_CHECKING 1
36
37static void
38logger(LY_LOG_LEVEL level, const char *msg, const char *path)
39{
40 (void) level; /* unused */
41
42 if (path) {
43 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
44 } else {
45 strncpy(logbuf, msg, BUFSIZE - 1);
46 }
47}
48
49static int
50logger_setup(void **state)
51{
52 (void) state; /* unused */
53#if ENABLE_LOGGER_CHECKING
54 ly_set_log_clb(logger, 1);
55#endif
56 return 0;
57}
58
59void
60logbuf_clean(void)
61{
62 logbuf[0] = '\0';
63}
64
65#if ENABLE_LOGGER_CHECKING
66# define logbuf_assert(str) assert_string_equal(logbuf, str)
67#else
68# define logbuf_assert(str)
69#endif
70
71static void
72test_element(void **state)
73{
74 (void) state; /* unused */
75
76 size_t name_len, prefix_len;
Radek Krejci28e8cb52019-03-08 11:31:31 +010077 size_t buf_len, len;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020078 const char *name, *prefix;
Radek Krejci28e8cb52019-03-08 11:31:31 +010079 char *buf = NULL, *out = NULL;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020080 const char *str, *p;
Radek Krejci28e8cb52019-03-08 11:31:31 +010081 int dynamic;
Radek Krejcid91dbaf2018-09-21 15:51:39 +020082
83 struct lyxml_context ctx;
84 memset(&ctx, 0, sizeof ctx);
85 ctx.line = 1;
86
87 /* empty */
88 str = "";
Radek Krejci7a7fa902018-09-25 17:08:21 +020089 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +020090 assert_null(name);
Radek Krejcib1890642018-10-03 14:05:40 +020091 assert_int_equal(LYXML_END, ctx.status);
Radek Krejcid91dbaf2018-09-21 15:51:39 +020092 assert_true(str[0] == '\0');
93
Radek Krejcib1890642018-10-03 14:05:40 +020094 /* end element */
95 str = "</element>";
96 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
97 logbuf_assert("Opening and closing elements tag missmatch (\"element>\"). Line number 1.");
98
99
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200100 /* no element */
101 logbuf_clean();
102 str = p = "no data present";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200103 assert_int_equal(LY_EINVAL, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200104 assert_null(name);
105 assert_ptr_equal(p, str); /* input data not eaten */
106 logbuf_assert("");
107
108 /* not supported DOCTYPE */
109 str = p = "<!DOCTYPE greeting SYSTEM \"hello.dtd\"><greeting/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200110 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200111 assert_null(name);
112 assert_ptr_equal(p, str); /* input data not eaten */
113 logbuf_assert("Document Type Declaration not supported. Line number 1.");
114
115 /* unqualified element */
116 str = " < element/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200117 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200118 assert_null(prefix);
119 assert_false(strncmp("element", name, name_len));
120 assert_int_equal(7, name_len);
Radek Krejcib1890642018-10-03 14:05:40 +0200121 assert_int_equal(LYXML_ELEMENT, ctx.status);
122 assert_string_equal("", str);
Radek Krejci28e8cb52019-03-08 11:31:31 +0100123 assert_int_equal(0, ctx.elements.count);
124
125 str = " < element attr=\'x\'/>";
126 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
127 assert_int_equal(LYXML_ATTRIBUTE, ctx.status);
128 assert_string_equal("attr=\'x\'/>", str);
129 assert_int_equal(1, ctx.elements.count);
130 assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
131 assert_int_equal(LYXML_ATTR_CONTENT, ctx.status);
132 assert_string_equal("\'x\'/>", str);
133 assert_int_equal(1, ctx.elements.count);
134 assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
135 assert_int_equal(LYXML_ELEMENT, ctx.status);
136 assert_string_equal("", str);
137 assert_int_equal(0, ctx.elements.count);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200138
Radek Krejcifb7c6582018-09-21 16:12:45 +0200139 str = "<?xml version=\"1.0\"?> <!-- comment --> <![CDATA[<greeting>Hello, world!</greeting>]]> <?TEST xxx?> <element/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200140 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200141 assert_null(prefix);
142 assert_false(strncmp("element", name, name_len));
143 assert_int_equal(7, name_len);
Radek Krejcib1890642018-10-03 14:05:40 +0200144 assert_int_equal(LYXML_ELEMENT, ctx.status);
145 assert_string_equal("", str);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200146
147 str = "<element xmlns=\"urn\"></element>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200148 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200149 assert_null(prefix);
150 assert_false(strncmp("element", name, name_len));
151 assert_int_equal(7, name_len);
Radek Krejcib1890642018-10-03 14:05:40 +0200152 assert_int_equal(LYXML_ATTRIBUTE, ctx.status);
153 assert_string_equal("xmlns=\"urn\"></element>", str);
154 /* cleean context by getting closing tag */
155 str += 12;
156 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200157
158 /* qualified element */
159 str = " < yin:element/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200160 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200161 assert_false(strncmp("yin", prefix, prefix_len));
162 assert_false(strncmp("element", name, name_len));
163 assert_int_equal(3, prefix_len);
164 assert_int_equal(7, name_len);
Radek Krejcib1890642018-10-03 14:05:40 +0200165 assert_int_equal(LYXML_ELEMENT, ctx.status);
166 assert_string_equal("", str);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200167
168 str = "<yin:element xmlns=\"urn\"></element>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200169 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200170 assert_false(strncmp("yin", prefix, prefix_len));
171 assert_false(strncmp("element", name, name_len));
172 assert_int_equal(3, prefix_len);
173 assert_int_equal(7, name_len);
Radek Krejcib1890642018-10-03 14:05:40 +0200174 assert_int_equal(LYXML_ATTRIBUTE, ctx.status);
175 assert_string_equal("xmlns=\"urn\"></element>", str);
176 /* cleean context by getting closing tag */
177 str += 12;
178 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
179 logbuf_assert("Opening and closing elements tag missmatch (\"element>\"). Line number 1.");
Radek Krejcia93621b2018-10-18 11:13:38 +0200180 str = "</yin:element/>";
181 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
182 logbuf_assert("Unexpected data \"/>\" in closing element tag. Line number 1.");
Radek Krejcib1890642018-10-03 14:05:40 +0200183 lyxml_context_clear(&ctx);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200184
185 /* UTF8 characters */
186 str = "<𠜎€𠜎Øn:𠜎€𠜎Øn/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200187 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200188 assert_false(strncmp("𠜎€𠜎Øn", prefix, prefix_len));
189 assert_false(strncmp("𠜎€𠜎Øn", name, name_len));
190 assert_int_equal(14, prefix_len);
191 assert_int_equal(14, name_len);
Radek Krejcib1890642018-10-03 14:05:40 +0200192 assert_int_equal(LYXML_ELEMENT, ctx.status);
193 assert_string_equal("", str);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200194
195 /* invalid UTF-8 character */
196 str = "<¢:element>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200197 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200198 logbuf_assert("Identifier \"¢:element>\" starts with invalid character. Line number 1.");
199 str = "<yin:c⁐element>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200200 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200201 logbuf_assert("Invalid character sequence \"⁐element>\", expected whitespace or element tag termination ('>' or '/>'. Line number 1.");
202}
203
204static void
205test_attribute(void **state)
206{
207 (void) state; /* unused */
208
209 size_t name_len, prefix_len;
210 const char *name, *prefix;
211 const char *str, *p;
212
213 struct lyxml_context ctx;
214 memset(&ctx, 0, sizeof ctx);
215 ctx.line = 1;
216
217 /* empty - without element tag termination */
218 str = "";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200219 assert_int_equal(LY_EINVAL, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200220
Radek Krejcid972c252018-09-25 13:23:39 +0200221 /* not an attribute */
222 str = p = "unknown/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200223 assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200224 assert_ptr_equal(p, str); /* input data not eaten */
225 logbuf_assert("Invalid character sequence \"/>\", expected whitespace or '='. Line number 1.");
226 str = p = "unknown />";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200227 assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200228 assert_ptr_equal(p, str); /* input data not eaten */
229 logbuf_assert("Invalid character sequence \"/>\", expected '='. Line number 1.");
230 str = p = "xxx=/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200231 assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200232 assert_ptr_equal(p, str); /* input data not eaten */
233 logbuf_assert("Invalid character sequence \"/>\", expected either single or double quotation mark. Line number 1.");
234 str = p = "xxx\n = yyy/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200235 assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200236 assert_ptr_equal(p, str); /* input data not eaten */
237 logbuf_assert("Invalid character sequence \"yyy/>\", expected either single or double quotation mark. Line number 2.");
238
239 /* valid attribute */
240 str = "xmlns=\"urn\">";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200241 assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200242 assert_non_null(name);
243 assert_null(prefix);
244 assert_int_equal(5, name_len);
245 assert_int_equal(0, prefix_len);
246 assert_false(strncmp("xmlns", name, name_len));
247 assert_string_equal("\"urn\">", str);
Radek Krejcib1890642018-10-03 14:05:40 +0200248 assert_int_equal(LYXML_ATTR_CONTENT, ctx.status);
Radek Krejcid972c252018-09-25 13:23:39 +0200249
Radek Krejci28e8cb52019-03-08 11:31:31 +0100250 str = "xmlns:nc\n = \'urn\'/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200251 assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200252 assert_non_null(name);
253 assert_non_null(prefix);
254 assert_int_equal(2, name_len);
255 assert_int_equal(5, prefix_len);
256 assert_int_equal(3, ctx.line);
257 assert_false(strncmp("xmlns", prefix, prefix_len));
258 assert_false(strncmp("nc", name, name_len));
Radek Krejci28e8cb52019-03-08 11:31:31 +0100259 assert_string_equal("\'urn\'/>", str);
Radek Krejcib1890642018-10-03 14:05:40 +0200260 assert_int_equal(LYXML_ATTR_CONTENT, ctx.status);
Radek Krejci28e8cb52019-03-08 11:31:31 +0100261
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200262}
263
Radek Krejci7a7fa902018-09-25 17:08:21 +0200264static void
265test_text(void **state)
266{
267 (void) state; /* unused */
268
Radek Krejcid70d1072018-10-09 14:20:47 +0200269 size_t buf_len, len;
270 int dynamic;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200271 const char *str, *p;
Radek Krejcid70d1072018-10-09 14:20:47 +0200272 char *buf = NULL, *out = NULL;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200273
274 struct lyxml_context ctx;
275 memset(&ctx, 0, sizeof ctx);
276 ctx.line = 1;
277
278 /* empty attribute value */
Radek Krejcib1890642018-10-03 14:05:40 +0200279 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200280 str = "\"\"";
Radek Krejcid70d1072018-10-09 14:20:47 +0200281 assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
282 assert_null(buf);
283 assert_ptr_equal(&str[-1], out);
284 assert_int_equal(0, dynamic);
285 assert_int_equal(0, len);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200286 assert_true(str[0] == '\0'); /* everything eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200287 assert_int_equal(LYXML_ATTRIBUTE, ctx.status);
288
289 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200290 str = "\'\'";
Radek Krejcid70d1072018-10-09 14:20:47 +0200291 assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
292 assert_null(buf);
293 assert_ptr_equal(&str[-1], out);
294 assert_int_equal(0, dynamic);
295 assert_int_equal(0, len);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200296 assert_true(str[0] == '\0'); /* everything eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200297 assert_int_equal(LYXML_ATTRIBUTE, ctx.status);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200298
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200299 /* empty element content - only formating before defining child */
Radek Krejcib1890642018-10-03 14:05:40 +0200300 ctx.status = LYXML_ELEM_CONTENT;
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200301 str = "\n <";
Radek Krejcid70d1072018-10-09 14:20:47 +0200302 assert_int_equal(LY_EINVAL, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
303 assert_null(buf);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200304 assert_string_equal("<", str);
305
Radek Krejci7a7fa902018-09-25 17:08:21 +0200306 /* empty element content is invalid - missing content terminating character < */
Radek Krejcib1890642018-10-03 14:05:40 +0200307 ctx.status = LYXML_ELEM_CONTENT;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200308 str = "";
Radek Krejcid70d1072018-10-09 14:20:47 +0200309 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
310 assert_null(buf);
Radek Krejci117d2082018-09-26 10:05:14 +0200311 logbuf_assert("Unexpected end-of-file. Line number 2.");
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200312
Radek Krejcib1890642018-10-03 14:05:40 +0200313 ctx.status = LYXML_ELEM_CONTENT;
Radek Krejci117d2082018-09-26 10:05:14 +0200314 str = p = "xxx";
Radek Krejcid70d1072018-10-09 14:20:47 +0200315 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
316 assert_null(buf);
Radek Krejci117d2082018-09-26 10:05:14 +0200317 logbuf_assert("Unexpected end-of-file. Line number 2.");
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200318 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejci7a7fa902018-09-25 17:08:21 +0200319
Radek Krejci7a7fa902018-09-25 17:08:21 +0200320 /* valid strings */
Radek Krejcib1890642018-10-03 14:05:40 +0200321 ctx.status = LYXML_ELEM_CONTENT;
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200322 str = "€𠜎Øn \n&lt;&amp;&quot;&apos;&gt; &#82;&#x4f;&#x4B;<";
Radek Krejcid70d1072018-10-09 14:20:47 +0200323 assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
324 assert_int_not_equal(0, dynamic);
325 assert_non_null(buf);
326 assert_ptr_equal(out, buf);
327 assert_int_equal(22, buf_len);
328 assert_int_equal(21, len);
329 assert_string_equal("€𠜎Øn \n<&\"\'> ROK", buf);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200330 assert_string_equal("<", str);
Radek Krejcib1890642018-10-03 14:05:40 +0200331 assert_int_equal(LYXML_ELEMENT, ctx.status);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200332
Radek Krejci117d2082018-09-26 10:05:14 +0200333 /* test using n-bytes UTF8 hexadecimal code points */
Radek Krejcib1890642018-10-03 14:05:40 +0200334 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci117d2082018-09-26 10:05:14 +0200335 str = "\'&#x0024;&#x00A2;&#x20ac;&#x10348;\'";
Radek Krejcid70d1072018-10-09 14:20:47 +0200336 assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
337 assert_int_not_equal(0, dynamic);
338 assert_non_null(buf);
339 assert_ptr_equal(out, buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200340 assert_int_equal(22, buf_len);
Radek Krejcid70d1072018-10-09 14:20:47 +0200341 assert_int_equal(10, len);
342 assert_string_equal("$¢€𐍈", buf);
Radek Krejcib1890642018-10-03 14:05:40 +0200343 assert_int_equal(LYXML_ATTRIBUTE, ctx.status);
Radek Krejci117d2082018-09-26 10:05:14 +0200344
Radek Krejcid70d1072018-10-09 14:20:47 +0200345 free(buf);
346 buf = NULL;
347
Radek Krejci7a7fa902018-09-25 17:08:21 +0200348 /* invalid characters in string */
Radek Krejcib1890642018-10-03 14:05:40 +0200349 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200350 str = p = "\'&#x52\'";
Radek Krejcid70d1072018-10-09 14:20:47 +0200351 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200352 logbuf_assert("Invalid character sequence \"'\", expected ;. Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200353 assert_null(buf);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200354 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200355 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200356 str = p = "\"&#82\"";
Radek Krejcid70d1072018-10-09 14:20:47 +0200357 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200358 logbuf_assert("Invalid character sequence \"\"\", expected ;. Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200359 assert_null(buf);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200360 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200361 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200362 str = p = "\"&nonsence;\"";
Radek Krejcid70d1072018-10-09 14:20:47 +0200363 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200364 logbuf_assert("Entity reference \"&nonsence;\" not supported, only predefined references allowed. Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200365 assert_null(buf);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200366 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200367 ctx.status = LYXML_ELEM_CONTENT;
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200368 str = p = "&#o122;";
Radek Krejcid70d1072018-10-09 14:20:47 +0200369 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200370 logbuf_assert("Invalid character reference \"&#o122;\". Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200371 assert_null(buf);
Radek Krejci117d2082018-09-26 10:05:14 +0200372 assert_ptr_equal(p, str); /* input data not eaten */
373
Radek Krejcib1890642018-10-03 14:05:40 +0200374 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci117d2082018-09-26 10:05:14 +0200375 str = p = "\'&#x06;\'";
Radek Krejcid70d1072018-10-09 14:20:47 +0200376 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200377 logbuf_assert("Invalid character reference \"&#x06;\'\" (0x00000006). Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200378 assert_null(buf);
Radek Krejci117d2082018-09-26 10:05:14 +0200379 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200380 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci117d2082018-09-26 10:05:14 +0200381 str = p = "\'&#xfdd0;\'";
Radek Krejcid70d1072018-10-09 14:20:47 +0200382 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200383 logbuf_assert("Invalid character reference \"&#xfdd0;\'\" (0x0000fdd0). Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200384 assert_null(buf);
Radek Krejci117d2082018-09-26 10:05:14 +0200385 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200386 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci117d2082018-09-26 10:05:14 +0200387 str = p = "\'&#xffff;\'";
Radek Krejcid70d1072018-10-09 14:20:47 +0200388 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200389 logbuf_assert("Invalid character reference \"&#xffff;\'\" (0x0000ffff). Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200390 assert_null(buf);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200391 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejci7a7fa902018-09-25 17:08:21 +0200392}
393
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200394static void
395test_ns(void **state)
396{
397 (void) state; /* unused */
398
399 const char *e1, *e2;
400 const struct lyxml_ns *ns;
401
402 struct lyxml_context ctx;
403 memset(&ctx, 0, sizeof ctx);
404 ctx.line = 1;
405
406 e1 = "element1";
407 e2 = "element2";
Radek Krejcie0734d22019-04-05 15:54:28 +0200408 /* simulate adding open element1 into context */
409 ctx.elements.count++;
410 /* processing namespace definitions */
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200411 assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, e1, NULL, 0, strdup("urn:default")));
412 assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, e1, "nc", 2, strdup("urn:nc1")));
Radek Krejcie0734d22019-04-05 15:54:28 +0200413 /* simulate adding open element2 into context */
414 ctx.elements.count++;
415 /* processing namespace definitions */
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200416 assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, e2, "nc", 2, strdup("urn:nc2")));
Radek Krejcie0734d22019-04-05 15:54:28 +0200417 assert_int_equal(3, ctx.ns.count);
418 assert_int_not_equal(0, ctx.ns.size);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200419
420 ns = lyxml_ns_get(&ctx, NULL, 0);
421 assert_non_null(ns);
422 assert_null(ns->prefix);
423 assert_string_equal("urn:default", ns->uri);
424
425 ns = lyxml_ns_get(&ctx, "nc", 2);
426 assert_non_null(ns);
427 assert_string_equal("nc", ns->prefix);
428 assert_string_equal("urn:nc2", ns->uri);
429
Radek Krejcie0734d22019-04-05 15:54:28 +0200430 /* simulate closing element2 */
431 ctx.elements.count--;
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200432 assert_int_equal(LY_SUCCESS, lyxml_ns_rm(&ctx, e2));
Radek Krejcie0734d22019-04-05 15:54:28 +0200433 assert_int_equal(2, ctx.ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200434
435 ns = lyxml_ns_get(&ctx, "nc", 2);
436 assert_non_null(ns);
437 assert_string_equal("nc", ns->prefix);
438 assert_string_equal("urn:nc1", ns->uri);
439
Radek Krejcie0734d22019-04-05 15:54:28 +0200440 /* simulate closing element1 */
441 ctx.elements.count--;
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200442 assert_int_equal(LY_SUCCESS, lyxml_ns_rm(&ctx, e1));
Radek Krejcie0734d22019-04-05 15:54:28 +0200443 assert_int_equal(0, ctx.ns.count);
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200444
445 assert_null(lyxml_ns_get(&ctx, "nc", 2));
446 assert_null(lyxml_ns_get(&ctx, NULL, 0));
447}
448
Radek Krejcie0734d22019-04-05 15:54:28 +0200449static void
450test_ns2(void **state)
451{
452 (void) state; /* unused */
453
454 const char *e1, *e2;
455
456 struct lyxml_context ctx;
457 memset(&ctx, 0, sizeof ctx);
458 ctx.line = 1;
459
460 /* parent element has the same name as its child (and for some reason both names are stored at the same place) */
461 e1 = e2 = "element1";
462 /* simulate adding open element1 into context */
463 ctx.elements.count++;
464 /* default namespace defined in parent element1 */
465 assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, e1, NULL, 0, strdup("urn:default")));
466 assert_int_equal(1, ctx.ns.count);
467 /* going into child element1 */
468 e1 = e2;
469 /* simulate adding open element1 into context */
470 ctx.elements.count++;
471 /* no namespace defined, going out (first, simulate closing of so far open element) */
472 ctx.elements.count--;
473 assert_int_equal(LY_SUCCESS, lyxml_ns_rm(&ctx, e2));
474 assert_int_equal(1, ctx.ns.count);
475 /* nothing else, going out of the parent element1 (first, simulate closing of so far open element) */
476 ctx.elements.count--;
477 assert_int_equal(LY_SUCCESS, lyxml_ns_rm(&ctx, e1));
478 assert_int_equal(0, ctx.ns.count);
479}
480
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200481int main(void)
482{
483 const struct CMUnitTest tests[] = {
Radek Krejcid972c252018-09-25 13:23:39 +0200484 cmocka_unit_test_setup(test_element, logger_setup),
485 cmocka_unit_test_setup(test_attribute, logger_setup),
Radek Krejci7a7fa902018-09-25 17:08:21 +0200486 cmocka_unit_test_setup(test_text, logger_setup),
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200487 cmocka_unit_test_setup(test_ns, logger_setup),
Radek Krejcie0734d22019-04-05 15:54:28 +0200488 cmocka_unit_test_setup(test_ns2, logger_setup),
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200489 };
490
491 return cmocka_run_group_tests(tests, NULL, NULL);
492}