blob: cbabebbfeb21df6709ee9d5a2d0e390b9fee1fa3 [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
15#define _BSD_SOURCE
16#define _DEFAULT_SOURCE
17#include <stdarg.h>
18#include <stddef.h>
19#include <setjmp.h>
20#include <cmocka.h>
21
22#include <stdio.h>
23#include <string.h>
24
25#include "libyang.h"
26#include "../../src/xml.c"
27
28#define BUFSIZE 1024
29char logbuf[BUFSIZE] = {0};
30
31/* set to 0 to printing error messages to stderr instead of checking them in code */
32#define ENABLE_LOGGER_CHECKING 1
33
34static void
35logger(LY_LOG_LEVEL level, const char *msg, const char *path)
36{
37 (void) level; /* unused */
38
39 if (path) {
40 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
41 } else {
42 strncpy(logbuf, msg, BUFSIZE - 1);
43 }
44}
45
46static int
47logger_setup(void **state)
48{
49 (void) state; /* unused */
50#if ENABLE_LOGGER_CHECKING
51 ly_set_log_clb(logger, 1);
52#endif
53 return 0;
54}
55
56void
57logbuf_clean(void)
58{
59 logbuf[0] = '\0';
60}
61
62#if ENABLE_LOGGER_CHECKING
63# define logbuf_assert(str) assert_string_equal(logbuf, str)
64#else
65# define logbuf_assert(str)
66#endif
67
68static void
69test_element(void **state)
70{
71 (void) state; /* unused */
72
73 size_t name_len, prefix_len;
74 const char *name, *prefix;
75 const char *str, *p;
76
77 struct lyxml_context ctx;
78 memset(&ctx, 0, sizeof ctx);
79 ctx.line = 1;
80
81 /* empty */
82 str = "";
Radek Krejci7a7fa902018-09-25 17:08:21 +020083 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +020084 assert_null(name);
Radek Krejcib1890642018-10-03 14:05:40 +020085 assert_int_equal(LYXML_END, ctx.status);
Radek Krejcid91dbaf2018-09-21 15:51:39 +020086 assert_true(str[0] == '\0');
87
Radek Krejcib1890642018-10-03 14:05:40 +020088 /* end element */
89 str = "</element>";
90 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
91 logbuf_assert("Opening and closing elements tag missmatch (\"element>\"). Line number 1.");
92
93
Radek Krejcid91dbaf2018-09-21 15:51:39 +020094 /* no element */
95 logbuf_clean();
96 str = p = "no data present";
Radek Krejci7a7fa902018-09-25 17:08:21 +020097 assert_int_equal(LY_EINVAL, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +020098 assert_null(name);
99 assert_ptr_equal(p, str); /* input data not eaten */
100 logbuf_assert("");
101
102 /* not supported DOCTYPE */
103 str = p = "<!DOCTYPE greeting SYSTEM \"hello.dtd\"><greeting/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200104 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200105 assert_null(name);
106 assert_ptr_equal(p, str); /* input data not eaten */
107 logbuf_assert("Document Type Declaration not supported. Line number 1.");
108
109 /* unqualified element */
110 str = " < element/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200111 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200112 assert_null(prefix);
113 assert_false(strncmp("element", name, name_len));
114 assert_int_equal(7, name_len);
Radek Krejcib1890642018-10-03 14:05:40 +0200115 assert_int_equal(LYXML_ELEMENT, ctx.status);
116 assert_string_equal("", str);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200117
Radek Krejcifb7c6582018-09-21 16:12:45 +0200118 str = "<?xml version=\"1.0\"?> <!-- comment --> <![CDATA[<greeting>Hello, world!</greeting>]]> <?TEST xxx?> <element/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200119 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200120 assert_null(prefix);
121 assert_false(strncmp("element", name, name_len));
122 assert_int_equal(7, name_len);
Radek Krejcib1890642018-10-03 14:05:40 +0200123 assert_int_equal(LYXML_ELEMENT, ctx.status);
124 assert_string_equal("", str);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200125
126 str = "<element xmlns=\"urn\"></element>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200127 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200128 assert_null(prefix);
129 assert_false(strncmp("element", name, name_len));
130 assert_int_equal(7, name_len);
Radek Krejcib1890642018-10-03 14:05:40 +0200131 assert_int_equal(LYXML_ATTRIBUTE, ctx.status);
132 assert_string_equal("xmlns=\"urn\"></element>", str);
133 /* cleean context by getting closing tag */
134 str += 12;
135 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200136
137 /* qualified element */
138 str = " < yin:element/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200139 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200140 assert_false(strncmp("yin", prefix, prefix_len));
141 assert_false(strncmp("element", name, name_len));
142 assert_int_equal(3, prefix_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 = "<yin: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_false(strncmp("yin", prefix, prefix_len));
150 assert_false(strncmp("element", name, name_len));
151 assert_int_equal(3, prefix_len);
152 assert_int_equal(7, name_len);
Radek Krejcib1890642018-10-03 14:05:40 +0200153 assert_int_equal(LYXML_ATTRIBUTE, ctx.status);
154 assert_string_equal("xmlns=\"urn\"></element>", str);
155 /* cleean context by getting closing tag */
156 str += 12;
157 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
158 logbuf_assert("Opening and closing elements tag missmatch (\"element>\"). Line number 1.");
159 lyxml_context_clear(&ctx);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200160
161 /* UTF8 characters */
162 str = "<𠜎€𠜎Øn:𠜎€𠜎Øn/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200163 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200164 assert_false(strncmp("𠜎€𠜎Øn", prefix, prefix_len));
165 assert_false(strncmp("𠜎€𠜎Øn", name, name_len));
166 assert_int_equal(14, prefix_len);
167 assert_int_equal(14, name_len);
Radek Krejcib1890642018-10-03 14:05:40 +0200168 assert_int_equal(LYXML_ELEMENT, ctx.status);
169 assert_string_equal("", str);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200170
171 /* invalid UTF-8 character */
172 str = "<¢:element>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200173 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200174 logbuf_assert("Identifier \"¢:element>\" starts with invalid character. Line number 1.");
175 str = "<yin:c⁐element>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200176 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200177 logbuf_assert("Invalid character sequence \"⁐element>\", expected whitespace or element tag termination ('>' or '/>'. Line number 1.");
178}
179
180static void
181test_attribute(void **state)
182{
183 (void) state; /* unused */
184
185 size_t name_len, prefix_len;
186 const char *name, *prefix;
187 const char *str, *p;
188
189 struct lyxml_context ctx;
190 memset(&ctx, 0, sizeof ctx);
191 ctx.line = 1;
192
193 /* empty - without element tag termination */
194 str = "";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200195 assert_int_equal(LY_EINVAL, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200196
197 /* empty - without element tag termination */
198 str = " />";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200199 assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200200 assert_null(name);
Radek Krejcib1890642018-10-03 14:05:40 +0200201 assert_true(str[0] == '\0');
202 assert_int_equal(LYXML_ELEMENT, ctx.status);
Radek Krejcid972c252018-09-25 13:23:39 +0200203 str = ">";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200204 assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200205 assert_null(name);
Radek Krejcib1890642018-10-03 14:05:40 +0200206 assert_true(str[0] == '\0');
207 assert_int_equal(LYXML_ELEM_CONTENT, ctx.status);
Radek Krejcid972c252018-09-25 13:23:39 +0200208
209 /* not an attribute */
210 str = p = "unknown/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200211 assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200212 assert_ptr_equal(p, str); /* input data not eaten */
213 logbuf_assert("Invalid character sequence \"/>\", expected whitespace or '='. Line number 1.");
214 str = p = "unknown />";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200215 assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200216 assert_ptr_equal(p, str); /* input data not eaten */
217 logbuf_assert("Invalid character sequence \"/>\", expected '='. Line number 1.");
218 str = p = "xxx=/>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200219 assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200220 assert_ptr_equal(p, str); /* input data not eaten */
221 logbuf_assert("Invalid character sequence \"/>\", expected either single or double quotation mark. Line number 1.");
222 str = p = "xxx\n = yyy/>";
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 \"yyy/>\", expected either single or double quotation mark. Line number 2.");
226
227 /* valid attribute */
228 str = "xmlns=\"urn\">";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200229 assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200230 assert_non_null(name);
231 assert_null(prefix);
232 assert_int_equal(5, name_len);
233 assert_int_equal(0, prefix_len);
234 assert_false(strncmp("xmlns", name, name_len));
235 assert_string_equal("\"urn\">", str);
Radek Krejcib1890642018-10-03 14:05:40 +0200236 assert_int_equal(LYXML_ATTR_CONTENT, ctx.status);
Radek Krejcid972c252018-09-25 13:23:39 +0200237
238 str = "xmlns:nc\n = \'urn\'>";
Radek Krejci7a7fa902018-09-25 17:08:21 +0200239 assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200240 assert_non_null(name);
241 assert_non_null(prefix);
242 assert_int_equal(2, name_len);
243 assert_int_equal(5, prefix_len);
244 assert_int_equal(3, ctx.line);
245 assert_false(strncmp("xmlns", prefix, prefix_len));
246 assert_false(strncmp("nc", 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 Krejcid91dbaf2018-09-21 15:51:39 +0200249}
250
Radek Krejci7a7fa902018-09-25 17:08:21 +0200251static void
252test_text(void **state)
253{
254 (void) state; /* unused */
255
Radek Krejcid70d1072018-10-09 14:20:47 +0200256 size_t buf_len, len;
257 int dynamic;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200258 const char *str, *p;
Radek Krejcid70d1072018-10-09 14:20:47 +0200259 char *buf = NULL, *out = NULL;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200260
261 struct lyxml_context ctx;
262 memset(&ctx, 0, sizeof ctx);
263 ctx.line = 1;
264
265 /* empty attribute value */
Radek Krejcib1890642018-10-03 14:05:40 +0200266 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200267 str = "\"\"";
Radek Krejcid70d1072018-10-09 14:20:47 +0200268 assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
269 assert_null(buf);
270 assert_ptr_equal(&str[-1], out);
271 assert_int_equal(0, dynamic);
272 assert_int_equal(0, len);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200273 assert_true(str[0] == '\0'); /* everything eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200274 assert_int_equal(LYXML_ATTRIBUTE, ctx.status);
275
276 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200277 str = "\'\'";
Radek Krejcid70d1072018-10-09 14:20:47 +0200278 assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
279 assert_null(buf);
280 assert_ptr_equal(&str[-1], out);
281 assert_int_equal(0, dynamic);
282 assert_int_equal(0, len);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200283 assert_true(str[0] == '\0'); /* everything eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200284 assert_int_equal(LYXML_ATTRIBUTE, ctx.status);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200285
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200286 /* empty element content - only formating before defining child */
Radek Krejcib1890642018-10-03 14:05:40 +0200287 ctx.status = LYXML_ELEM_CONTENT;
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200288 str = "\n <";
Radek Krejcid70d1072018-10-09 14:20:47 +0200289 assert_int_equal(LY_EINVAL, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
290 assert_null(buf);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200291 assert_string_equal("<", str);
292
Radek Krejci7a7fa902018-09-25 17:08:21 +0200293 /* empty element content is invalid - missing content terminating character < */
Radek Krejcib1890642018-10-03 14:05:40 +0200294 ctx.status = LYXML_ELEM_CONTENT;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200295 str = "";
Radek Krejcid70d1072018-10-09 14:20:47 +0200296 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
297 assert_null(buf);
Radek Krejci117d2082018-09-26 10:05:14 +0200298 logbuf_assert("Unexpected end-of-file. Line number 2.");
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200299
Radek Krejcib1890642018-10-03 14:05:40 +0200300 ctx.status = LYXML_ELEM_CONTENT;
Radek Krejci117d2082018-09-26 10:05:14 +0200301 str = p = "xxx";
Radek Krejcid70d1072018-10-09 14:20:47 +0200302 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
303 assert_null(buf);
Radek Krejci117d2082018-09-26 10:05:14 +0200304 logbuf_assert("Unexpected end-of-file. Line number 2.");
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200305 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejci7a7fa902018-09-25 17:08:21 +0200306
Radek Krejci7a7fa902018-09-25 17:08:21 +0200307 /* valid strings */
Radek Krejcib1890642018-10-03 14:05:40 +0200308 ctx.status = LYXML_ELEM_CONTENT;
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200309 str = "€𠜎Øn \n&lt;&amp;&quot;&apos;&gt; &#82;&#x4f;&#x4B;<";
Radek Krejcid70d1072018-10-09 14:20:47 +0200310 assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
311 assert_int_not_equal(0, dynamic);
312 assert_non_null(buf);
313 assert_ptr_equal(out, buf);
314 assert_int_equal(22, buf_len);
315 assert_int_equal(21, len);
316 assert_string_equal("€𠜎Øn \n<&\"\'> ROK", buf);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200317 assert_string_equal("<", str);
Radek Krejcib1890642018-10-03 14:05:40 +0200318 assert_int_equal(LYXML_ELEMENT, ctx.status);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200319
Radek Krejci117d2082018-09-26 10:05:14 +0200320 /* test using n-bytes UTF8 hexadecimal code points */
Radek Krejcib1890642018-10-03 14:05:40 +0200321 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci117d2082018-09-26 10:05:14 +0200322 str = "\'&#x0024;&#x00A2;&#x20ac;&#x10348;\'";
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);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200327 assert_int_equal(22, buf_len);
Radek Krejcid70d1072018-10-09 14:20:47 +0200328 assert_int_equal(10, len);
329 assert_string_equal("$¢€𐍈", buf);
Radek Krejcib1890642018-10-03 14:05:40 +0200330 assert_int_equal(LYXML_ATTRIBUTE, ctx.status);
Radek Krejci117d2082018-09-26 10:05:14 +0200331
Radek Krejcid70d1072018-10-09 14:20:47 +0200332 free(buf);
333 buf = NULL;
334
Radek Krejci7a7fa902018-09-25 17:08:21 +0200335 /* invalid characters in string */
Radek Krejcib1890642018-10-03 14:05:40 +0200336 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200337 str = p = "\'&#x52\'";
Radek Krejcid70d1072018-10-09 14:20:47 +0200338 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200339 logbuf_assert("Invalid character sequence \"'\", expected ;. Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200340 assert_null(buf);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200341 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200342 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci7a7fa902018-09-25 17:08:21 +0200343 str = p = "\"&#82\"";
Radek Krejcid70d1072018-10-09 14:20:47 +0200344 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200345 logbuf_assert("Invalid character sequence \"\"\", expected ;. Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200346 assert_null(buf);
Radek Krejci7a7fa902018-09-25 17:08:21 +0200347 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200348 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200349 str = p = "\"&nonsence;\"";
Radek Krejcid70d1072018-10-09 14:20:47 +0200350 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200351 logbuf_assert("Entity reference \"&nonsence;\" not supported, only predefined references allowed. Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200352 assert_null(buf);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200353 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200354 ctx.status = LYXML_ELEM_CONTENT;
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200355 str = p = "&#o122;";
Radek Krejcid70d1072018-10-09 14:20:47 +0200356 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200357 logbuf_assert("Invalid character reference \"&#o122;\". Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200358 assert_null(buf);
Radek Krejci117d2082018-09-26 10:05:14 +0200359 assert_ptr_equal(p, str); /* input data not eaten */
360
Radek Krejcib1890642018-10-03 14:05:40 +0200361 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci117d2082018-09-26 10:05:14 +0200362 str = p = "\'&#x06;\'";
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("Invalid character reference \"&#x06;\'\" (0x00000006). Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200365 assert_null(buf);
Radek Krejci117d2082018-09-26 10:05:14 +0200366 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejcib1890642018-10-03 14:05:40 +0200367 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci117d2082018-09-26 10:05:14 +0200368 str = p = "\'&#xfdd0;\'";
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 \"&#xfdd0;\'\" (0x0000fdd0). 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 */
Radek Krejcib1890642018-10-03 14:05:40 +0200373 ctx.status = LYXML_ATTR_CONTENT;
Radek Krejci117d2082018-09-26 10:05:14 +0200374 str = p = "\'&#xffff;\'";
Radek Krejcid70d1072018-10-09 14:20:47 +0200375 assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic));
Radek Krejci117d2082018-09-26 10:05:14 +0200376 logbuf_assert("Invalid character reference \"&#xffff;\'\" (0x0000ffff). Line number 3.");
Radek Krejcid70d1072018-10-09 14:20:47 +0200377 assert_null(buf);
Radek Krejcied6c6ad2018-09-26 09:10:18 +0200378 assert_ptr_equal(p, str); /* input data not eaten */
Radek Krejci7a7fa902018-09-25 17:08:21 +0200379}
380
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200381static void
382test_ns(void **state)
383{
384 (void) state; /* unused */
385
386 const char *e1, *e2;
387 const struct lyxml_ns *ns;
388
389 struct lyxml_context ctx;
390 memset(&ctx, 0, sizeof ctx);
391 ctx.line = 1;
392
393 e1 = "element1";
394 e2 = "element2";
395 assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, e1, NULL, 0, strdup("urn:default")));
396 assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, e1, "nc", 2, strdup("urn:nc1")));
397 assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, e2, "nc", 2, strdup("urn:nc2")));
398 assert_int_equal(3, (&ctx)->ns.count);
399 assert_int_not_equal(0, (&ctx)->ns.size);
400
401 ns = lyxml_ns_get(&ctx, NULL, 0);
402 assert_non_null(ns);
403 assert_null(ns->prefix);
404 assert_string_equal("urn:default", ns->uri);
405
406 ns = lyxml_ns_get(&ctx, "nc", 2);
407 assert_non_null(ns);
408 assert_string_equal("nc", ns->prefix);
409 assert_string_equal("urn:nc2", ns->uri);
410
411 assert_int_equal(LY_SUCCESS, lyxml_ns_rm(&ctx, e2));
412 assert_int_equal(2, (&ctx)->ns.count);
413
414 ns = lyxml_ns_get(&ctx, "nc", 2);
415 assert_non_null(ns);
416 assert_string_equal("nc", ns->prefix);
417 assert_string_equal("urn:nc1", ns->uri);
418
419 assert_int_equal(LY_SUCCESS, lyxml_ns_rm(&ctx, e1));
420 assert_int_equal(0, (&ctx)->ns.count);
421
422 assert_null(lyxml_ns_get(&ctx, "nc", 2));
423 assert_null(lyxml_ns_get(&ctx, NULL, 0));
424}
425
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200426int main(void)
427{
428 const struct CMUnitTest tests[] = {
Radek Krejcid972c252018-09-25 13:23:39 +0200429 cmocka_unit_test_setup(test_element, logger_setup),
430 cmocka_unit_test_setup(test_attribute, logger_setup),
Radek Krejci7a7fa902018-09-25 17:08:21 +0200431 cmocka_unit_test_setup(test_text, logger_setup),
Radek Krejci4b74d5e2018-09-26 14:30:55 +0200432 cmocka_unit_test_setup(test_ns, logger_setup),
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200433 };
434
435 return cmocka_run_group_tests(tests, NULL, NULL);
436}