blob: 9351e323470efa25ca7153298b4b9a42e006f9d4 [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
Radek Krejcifb7c6582018-09-21 16:12:45 +020068
69static void
70test_utf8(void **state)
71{
72 (void) state; /* unused */
73
74 char buf[5] = {0};
75 const char *str = buf;
76 unsigned int c;
77 size_t len;
78
79 /* test invalid UTF-8 characters in lyxml_getutf8
80 * - https://en.wikipedia.org/wiki/UTF-8 */
Radek Krejcide362c02018-09-21 16:27:37 +020081 buf[0] = 0x04;
82 assert_int_equal(LY_EINVAL, lyxml_getutf8(&str, &c, &len));
Radek Krejcifb7c6582018-09-21 16:12:45 +020083 buf[0] = 0x80;
84 assert_int_equal(LY_EINVAL, lyxml_getutf8(&str, &c, &len));
85
Radek Krejci234ace02018-09-21 17:01:12 +020086 buf[0] = 0xc0;
Radek Krejcide362c02018-09-21 16:27:37 +020087 buf[1] = 0x00;
Radek Krejcifb7c6582018-09-21 16:12:45 +020088 assert_int_equal(LY_EINVAL, lyxml_getutf8(&str, &c, &len));
89 buf[1] = 0x80;
90 assert_int_equal(LY_EINVAL, lyxml_getutf8(&str, &c, &len));
91
Radek Krejci234ace02018-09-21 17:01:12 +020092 buf[0] = 0xe0;
93 buf[1] = 0x00;
Radek Krejcifb7c6582018-09-21 16:12:45 +020094 buf[2] = 0x80;
95 assert_int_equal(LY_EINVAL, lyxml_getutf8(&str, &c, &len));
Radek Krejci234ace02018-09-21 17:01:12 +020096 buf[1] = 0x80;
97 assert_int_equal(LY_EINVAL, lyxml_getutf8(&str, &c, &len));
98
99 buf[0] = 0xf0;
100 buf[1] = 0x00;
101 buf[2] = 0x80;
102 buf[3] = 0x80;
103 assert_int_equal(LY_EINVAL, lyxml_getutf8(&str, &c, &len));
104 buf[1] = 0x80;
105 assert_int_equal(LY_EINVAL, lyxml_getutf8(&str, &c, &len));
Radek Krejcifb7c6582018-09-21 16:12:45 +0200106}
107
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200108static void
109test_element(void **state)
110{
111 (void) state; /* unused */
112
113 size_t name_len, prefix_len;
114 const char *name, *prefix;
115 const char *str, *p;
116
117 struct lyxml_context ctx;
118 memset(&ctx, 0, sizeof ctx);
119 ctx.line = 1;
120
121 /* empty */
122 str = "";
123 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
124 assert_null(name);
125 assert_true(str[0] == '\0');
126
127 /* no element */
128 logbuf_clean();
129 str = p = "no data present";
130 assert_int_equal(LY_EINVAL, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
131 assert_null(name);
132 assert_ptr_equal(p, str); /* input data not eaten */
133 logbuf_assert("");
134
135 /* not supported DOCTYPE */
136 str = p = "<!DOCTYPE greeting SYSTEM \"hello.dtd\"><greeting/>";
137 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
138 assert_null(name);
139 assert_ptr_equal(p, str); /* input data not eaten */
140 logbuf_assert("Document Type Declaration not supported. Line number 1.");
141
142 /* unqualified element */
143 str = " < element/>";
144 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
145 assert_null(prefix);
146 assert_false(strncmp("element", name, name_len));
147 assert_int_equal(7, name_len);
148 assert_string_equal("/>", str);
149
Radek Krejcifb7c6582018-09-21 16:12:45 +0200150 str = "<?xml version=\"1.0\"?> <!-- comment --> <![CDATA[<greeting>Hello, world!</greeting>]]> <?TEST xxx?> <element/>";
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200151 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
152 assert_null(prefix);
153 assert_false(strncmp("element", name, name_len));
154 assert_int_equal(7, name_len);
155 assert_string_equal("/>", str);
156
157 str = "<element xmlns=\"urn\"></element>";
158 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
159 assert_null(prefix);
160 assert_false(strncmp("element", name, name_len));
161 assert_int_equal(7, name_len);
162 assert_string_equal(" xmlns=\"urn\"></element>", str);
163
164 /* qualified element */
165 str = " < yin:element/>";
166 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
167 assert_false(strncmp("yin", prefix, prefix_len));
168 assert_false(strncmp("element", name, name_len));
169 assert_int_equal(3, prefix_len);
170 assert_int_equal(7, name_len);
171 assert_string_equal("/>", str);
172
173 str = "<yin:element xmlns=\"urn\"></element>";
174 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
175 assert_false(strncmp("yin", prefix, prefix_len));
176 assert_false(strncmp("element", name, name_len));
177 assert_int_equal(3, prefix_len);
178 assert_int_equal(7, name_len);
179 assert_string_equal(" xmlns=\"urn\"></element>", str);
180
181 /* UTF8 characters */
182 str = "<𠜎€𠜎Øn:𠜎€𠜎Øn/>";
183 assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
184 assert_false(strncmp("𠜎€𠜎Øn", prefix, prefix_len));
185 assert_false(strncmp("𠜎€𠜎Øn", name, name_len));
186 assert_int_equal(14, prefix_len);
187 assert_int_equal(14, name_len);
188 assert_string_equal("/>", str);
189
190 /* invalid UTF-8 character */
191 str = "<¢:element>";
192 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
193 logbuf_assert("Identifier \"¢:element>\" starts with invalid character. Line number 1.");
194 str = "<yin:c⁐element>";
195 assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
Radek Krejcid972c252018-09-25 13:23:39 +0200196 logbuf_assert("Invalid character sequence \"⁐element>\", expected whitespace or element tag termination ('>' or '/>'. Line number 1.");
197}
198
199static void
200test_attribute(void **state)
201{
202 (void) state; /* unused */
203
204 size_t name_len, prefix_len;
205 const char *name, *prefix;
206 const char *str, *p;
207
208 struct lyxml_context ctx;
209 memset(&ctx, 0, sizeof ctx);
210 ctx.line = 1;
211
212 /* empty - without element tag termination */
213 str = "";
214 assert_int_equal(LY_EINVAL, lyxml_get_attribute(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
215
216 /* empty - without element tag termination */
217 str = " />";
218 assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
219 assert_null(name);
220 assert_true(str[0] == '/');
221 str = ">";
222 assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
223 assert_null(name);
224 assert_true(str[0] == '>');
225
226 /* not an attribute */
227 str = p = "unknown/>";
228 assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
229 assert_ptr_equal(p, str); /* input data not eaten */
230 logbuf_assert("Invalid character sequence \"/>\", expected whitespace or '='. Line number 1.");
231 str = p = "unknown />";
232 assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
233 assert_ptr_equal(p, str); /* input data not eaten */
234 logbuf_assert("Invalid character sequence \"/>\", expected '='. Line number 1.");
235 str = p = "xxx=/>";
236 assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
237 assert_ptr_equal(p, str); /* input data not eaten */
238 logbuf_assert("Invalid character sequence \"/>\", expected either single or double quotation mark. Line number 1.");
239 str = p = "xxx\n = yyy/>";
240 assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
241 assert_ptr_equal(p, str); /* input data not eaten */
242 logbuf_assert("Invalid character sequence \"yyy/>\", expected either single or double quotation mark. Line number 2.");
243
244 /* valid attribute */
245 str = "xmlns=\"urn\">";
246 assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
247 assert_non_null(name);
248 assert_null(prefix);
249 assert_int_equal(5, name_len);
250 assert_int_equal(0, prefix_len);
251 assert_false(strncmp("xmlns", name, name_len));
252 assert_string_equal("\"urn\">", str);
253
254 str = "xmlns:nc\n = \'urn\'>";
255 assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
256 assert_non_null(name);
257 assert_non_null(prefix);
258 assert_int_equal(2, name_len);
259 assert_int_equal(5, prefix_len);
260 assert_int_equal(3, ctx.line);
261 assert_false(strncmp("xmlns", prefix, prefix_len));
262 assert_false(strncmp("nc", name, name_len));
263 assert_string_equal("\'urn\'>", str);
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200264}
265
266int main(void)
267{
268 const struct CMUnitTest tests[] = {
Radek Krejcifb7c6582018-09-21 16:12:45 +0200269 cmocka_unit_test_setup(test_utf8, logger_setup),
Radek Krejcid972c252018-09-25 13:23:39 +0200270 cmocka_unit_test_setup(test_element, logger_setup),
271 cmocka_unit_test_setup(test_attribute, logger_setup),
Radek Krejcid91dbaf2018-09-21 15:51:39 +0200272 };
273
274 return cmocka_run_group_tests(tests, NULL, NULL);
275}