xml FEATURE xml parser
just work in progress:
- element name parser (lyxml_get_element())
diff --git a/tests/src/xml.c b/tests/src/xml.c
new file mode 100644
index 0000000..3731fbe
--- /dev/null
+++ b/tests/src/xml.c
@@ -0,0 +1,166 @@
+/*
+ * @file xml.c
+ * @author: Radek Krejci <rkrejci@cesnet.cz>
+ * @brief unit tests for functions from xml.c
+ *
+ * Copyright (c) 2018 CESNET, z.s.p.o.
+ *
+ * This source code is licensed under BSD 3-Clause License (the "License").
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://opensource.org/licenses/BSD-3-Clause
+ */
+
+#define _BSD_SOURCE
+#define _DEFAULT_SOURCE
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <stdio.h>
+#include <string.h>
+
+#include "libyang.h"
+#include "../../src/xml.c"
+
+#define BUFSIZE 1024
+char logbuf[BUFSIZE] = {0};
+
+/* set to 0 to printing error messages to stderr instead of checking them in code */
+#define ENABLE_LOGGER_CHECKING 1
+
+static void
+logger(LY_LOG_LEVEL level, const char *msg, const char *path)
+{
+ (void) level; /* unused */
+
+ if (path) {
+ snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
+ } else {
+ strncpy(logbuf, msg, BUFSIZE - 1);
+ }
+}
+
+static int
+logger_setup(void **state)
+{
+ (void) state; /* unused */
+#if ENABLE_LOGGER_CHECKING
+ ly_set_log_clb(logger, 1);
+#endif
+ return 0;
+}
+
+void
+logbuf_clean(void)
+{
+ logbuf[0] = '\0';
+}
+
+#if ENABLE_LOGGER_CHECKING
+# define logbuf_assert(str) assert_string_equal(logbuf, str)
+#else
+# define logbuf_assert(str)
+#endif
+
+static void
+test_element(void **state)
+{
+ (void) state; /* unused */
+
+ size_t name_len, prefix_len;
+ const char *name, *prefix;
+ const char *str, *p;
+
+ struct lyxml_context ctx;
+ memset(&ctx, 0, sizeof ctx);
+ ctx.line = 1;
+
+ /* empty */
+ str = "";
+ assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
+ assert_null(name);
+ assert_true(str[0] == '\0');
+
+ /* no element */
+ logbuf_clean();
+ str = p = "no data present";
+ assert_int_equal(LY_EINVAL, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
+ assert_null(name);
+ assert_ptr_equal(p, str); /* input data not eaten */
+ logbuf_assert("");
+
+ /* not supported DOCTYPE */
+ str = p = "<!DOCTYPE greeting SYSTEM \"hello.dtd\"><greeting/>";
+ assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
+ assert_null(name);
+ assert_ptr_equal(p, str); /* input data not eaten */
+ logbuf_assert("Document Type Declaration not supported. Line number 1.");
+
+ /* unqualified element */
+ str = " < element/>";
+ assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
+ assert_null(prefix);
+ assert_false(strncmp("element", name, name_len));
+ assert_int_equal(7, name_len);
+ assert_string_equal("/>", str);
+
+ str = "<?xml version=\"1.0\"?> <!-- comment --> <?TEST xxx?> <element/>";
+ assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
+ assert_null(prefix);
+ assert_false(strncmp("element", name, name_len));
+ assert_int_equal(7, name_len);
+ assert_string_equal("/>", str);
+
+ str = "<element xmlns=\"urn\"></element>";
+ assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
+ assert_null(prefix);
+ assert_false(strncmp("element", name, name_len));
+ assert_int_equal(7, name_len);
+ assert_string_equal(" xmlns=\"urn\"></element>", str);
+
+ /* qualified element */
+ str = " < yin:element/>";
+ assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
+ assert_false(strncmp("yin", prefix, prefix_len));
+ assert_false(strncmp("element", name, name_len));
+ assert_int_equal(3, prefix_len);
+ assert_int_equal(7, name_len);
+ assert_string_equal("/>", str);
+
+ str = "<yin:element xmlns=\"urn\"></element>";
+ assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
+ assert_false(strncmp("yin", prefix, prefix_len));
+ assert_false(strncmp("element", name, name_len));
+ assert_int_equal(3, prefix_len);
+ assert_int_equal(7, name_len);
+ assert_string_equal(" xmlns=\"urn\"></element>", str);
+
+ /* UTF8 characters */
+ str = "<𠜎€𠜎Øn:𠜎€𠜎Øn/>";
+ assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
+ assert_false(strncmp("𠜎€𠜎Øn", prefix, prefix_len));
+ assert_false(strncmp("𠜎€𠜎Øn", name, name_len));
+ assert_int_equal(14, prefix_len);
+ assert_int_equal(14, name_len);
+ assert_string_equal("/>", str);
+
+ /* invalid UTF-8 character */
+ str = "<¢:element>";
+ assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
+ logbuf_assert("Identifier \"¢:element>\" starts with invalid character. Line number 1.");
+ str = "<yin:c⁐element>";
+ assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, 0, &prefix, &prefix_len, &name, &name_len));
+ logbuf_assert("Invalid character 0x9081e2. Line number 1.");
+}
+
+int main(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test_setup(test_element, logger_setup),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}