Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 1 | /* |
| 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 Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 15 | #include "common.h" |
Radek Krejci | f3f4784 | 2018-11-15 11:22:15 +0100 | [diff] [blame] | 16 | #include "../../src/set.c" |
| 17 | #include "../../src/xml.c" |
| 18 | #include "../../src/common.c" |
| 19 | #include "../../src/log.c" |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 20 | |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 21 | #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 Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 30 | |
| 31 | #define BUFSIZE 1024 |
| 32 | char 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 | |
| 37 | static void |
| 38 | logger(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 | |
| 49 | static int |
| 50 | logger_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 | |
| 59 | void |
| 60 | logbuf_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 | |
| 71 | static void |
| 72 | test_element(void **state) |
| 73 | { |
| 74 | (void) state; /* unused */ |
| 75 | |
| 76 | size_t name_len, prefix_len; |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 77 | size_t buf_len, len; |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 78 | const char *name, *prefix; |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 79 | char *buf = NULL, *out = NULL; |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 80 | const char *str, *p; |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 81 | int dynamic; |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 82 | |
| 83 | struct lyxml_context ctx; |
| 84 | memset(&ctx, 0, sizeof ctx); |
| 85 | ctx.line = 1; |
| 86 | |
| 87 | /* empty */ |
| 88 | str = ""; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 89 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 90 | assert_null(name); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 91 | assert_int_equal(LYXML_END, ctx.status); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 92 | assert_true(str[0] == '\0'); |
| 93 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 94 | /* 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 Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 100 | /* no element */ |
| 101 | logbuf_clean(); |
| 102 | str = p = "no data present"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 103 | assert_int_equal(LY_EINVAL, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 104 | 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 Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 110 | assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 111 | 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 | |
Radek Krejci | c5c31bb | 2019-04-08 14:40:52 +0200 | [diff] [blame^] | 115 | /* invalid XML */ |
| 116 | str = p = "<!NONSENCE/>"; |
| 117 | assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
| 118 | assert_null(name); |
| 119 | assert_ptr_equal(p, str); /* input data not eaten */ |
| 120 | logbuf_assert("Unknown XML section \"<!NONSENCE/>\". Line number 1."); |
| 121 | |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 122 | /* unqualified element */ |
| 123 | str = " < element/>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 124 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 125 | assert_null(prefix); |
| 126 | assert_false(strncmp("element", name, name_len)); |
| 127 | assert_int_equal(7, name_len); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 128 | assert_int_equal(LYXML_ELEMENT, ctx.status); |
| 129 | assert_string_equal("", str); |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 130 | assert_int_equal(0, ctx.elements.count); |
| 131 | |
| 132 | str = " < element attr=\'x\'/>"; |
| 133 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
| 134 | assert_int_equal(LYXML_ATTRIBUTE, ctx.status); |
| 135 | assert_string_equal("attr=\'x\'/>", str); |
| 136 | assert_int_equal(1, ctx.elements.count); |
| 137 | assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
| 138 | assert_int_equal(LYXML_ATTR_CONTENT, ctx.status); |
| 139 | assert_string_equal("\'x\'/>", str); |
| 140 | assert_int_equal(1, ctx.elements.count); |
| 141 | assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
| 142 | assert_int_equal(LYXML_ELEMENT, ctx.status); |
| 143 | assert_string_equal("", str); |
| 144 | assert_int_equal(0, ctx.elements.count); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 145 | |
Radek Krejci | fb7c658 | 2018-09-21 16:12:45 +0200 | [diff] [blame] | 146 | str = "<?xml version=\"1.0\"?> <!-- comment --> <![CDATA[<greeting>Hello, world!</greeting>]]> <?TEST xxx?> <element/>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 147 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 148 | assert_null(prefix); |
| 149 | assert_false(strncmp("element", name, name_len)); |
| 150 | assert_int_equal(7, name_len); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 151 | assert_int_equal(LYXML_ELEMENT, ctx.status); |
| 152 | assert_string_equal("", str); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 153 | |
| 154 | str = "<element xmlns=\"urn\"></element>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 155 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 156 | assert_null(prefix); |
| 157 | assert_false(strncmp("element", name, name_len)); |
| 158 | assert_int_equal(7, name_len); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 159 | assert_int_equal(LYXML_ATTRIBUTE, ctx.status); |
| 160 | assert_string_equal("xmlns=\"urn\"></element>", str); |
| 161 | /* cleean context by getting closing tag */ |
| 162 | str += 12; |
| 163 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 164 | |
| 165 | /* qualified element */ |
| 166 | str = " < yin:element/>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 167 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 168 | assert_false(strncmp("yin", prefix, prefix_len)); |
| 169 | assert_false(strncmp("element", name, name_len)); |
| 170 | assert_int_equal(3, prefix_len); |
| 171 | assert_int_equal(7, name_len); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 172 | assert_int_equal(LYXML_ELEMENT, ctx.status); |
| 173 | assert_string_equal("", str); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 174 | |
| 175 | str = "<yin:element xmlns=\"urn\"></element>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 176 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 177 | assert_false(strncmp("yin", prefix, prefix_len)); |
| 178 | assert_false(strncmp("element", name, name_len)); |
| 179 | assert_int_equal(3, prefix_len); |
| 180 | assert_int_equal(7, name_len); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 181 | assert_int_equal(LYXML_ATTRIBUTE, ctx.status); |
| 182 | assert_string_equal("xmlns=\"urn\"></element>", str); |
| 183 | /* cleean context by getting closing tag */ |
| 184 | str += 12; |
| 185 | assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
| 186 | logbuf_assert("Opening and closing elements tag missmatch (\"element>\"). Line number 1."); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 187 | str = "</yin:element/>"; |
| 188 | assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
| 189 | logbuf_assert("Unexpected data \"/>\" in closing element tag. Line number 1."); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 190 | lyxml_context_clear(&ctx); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 191 | |
| 192 | /* UTF8 characters */ |
| 193 | str = "<𠜎€𠜎Øn:𠜎€𠜎Øn/>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 194 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 195 | assert_false(strncmp("𠜎€𠜎Øn", prefix, prefix_len)); |
| 196 | assert_false(strncmp("𠜎€𠜎Øn", name, name_len)); |
| 197 | assert_int_equal(14, prefix_len); |
| 198 | assert_int_equal(14, name_len); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 199 | assert_int_equal(LYXML_ELEMENT, ctx.status); |
| 200 | assert_string_equal("", str); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 201 | |
| 202 | /* invalid UTF-8 character */ |
| 203 | str = "<¢:element>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 204 | assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 205 | logbuf_assert("Identifier \"¢:element>\" starts with invalid character. Line number 1."); |
| 206 | str = "<yin:c⁐element>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 207 | assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 208 | logbuf_assert("Invalid character sequence \"⁐element>\", expected whitespace or element tag termination ('>' or '/>'. Line number 1."); |
| 209 | } |
| 210 | |
| 211 | static void |
| 212 | test_attribute(void **state) |
| 213 | { |
| 214 | (void) state; /* unused */ |
| 215 | |
| 216 | size_t name_len, prefix_len; |
| 217 | const char *name, *prefix; |
| 218 | const char *str, *p; |
| 219 | |
| 220 | struct lyxml_context ctx; |
| 221 | memset(&ctx, 0, sizeof ctx); |
| 222 | ctx.line = 1; |
| 223 | |
| 224 | /* empty - without element tag termination */ |
| 225 | str = ""; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 226 | assert_int_equal(LY_EINVAL, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 227 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 228 | /* not an attribute */ |
| 229 | str = p = "unknown/>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 230 | assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 231 | assert_ptr_equal(p, str); /* input data not eaten */ |
| 232 | logbuf_assert("Invalid character sequence \"/>\", expected whitespace or '='. Line number 1."); |
| 233 | str = p = "unknown />"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 234 | assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 235 | assert_ptr_equal(p, str); /* input data not eaten */ |
| 236 | logbuf_assert("Invalid character sequence \"/>\", expected '='. Line number 1."); |
| 237 | str = p = "xxx=/>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 238 | assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 239 | assert_ptr_equal(p, str); /* input data not eaten */ |
| 240 | logbuf_assert("Invalid character sequence \"/>\", expected either single or double quotation mark. Line number 1."); |
| 241 | str = p = "xxx\n = yyy/>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 242 | assert_int_equal(LY_EVALID, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 243 | assert_ptr_equal(p, str); /* input data not eaten */ |
| 244 | logbuf_assert("Invalid character sequence \"yyy/>\", expected either single or double quotation mark. Line number 2."); |
| 245 | |
| 246 | /* valid attribute */ |
| 247 | str = "xmlns=\"urn\">"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 248 | assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 249 | assert_non_null(name); |
| 250 | assert_null(prefix); |
| 251 | assert_int_equal(5, name_len); |
| 252 | assert_int_equal(0, prefix_len); |
| 253 | assert_false(strncmp("xmlns", name, name_len)); |
| 254 | assert_string_equal("\"urn\">", str); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 255 | assert_int_equal(LYXML_ATTR_CONTENT, ctx.status); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 256 | |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 257 | str = "xmlns:nc\n = \'urn\'/>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 258 | assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 259 | assert_non_null(name); |
| 260 | assert_non_null(prefix); |
| 261 | assert_int_equal(2, name_len); |
| 262 | assert_int_equal(5, prefix_len); |
| 263 | assert_int_equal(3, ctx.line); |
| 264 | assert_false(strncmp("xmlns", prefix, prefix_len)); |
| 265 | assert_false(strncmp("nc", name, name_len)); |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 266 | assert_string_equal("\'urn\'/>", str); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 267 | assert_int_equal(LYXML_ATTR_CONTENT, ctx.status); |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 268 | |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 271 | static void |
| 272 | test_text(void **state) |
| 273 | { |
| 274 | (void) state; /* unused */ |
| 275 | |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 276 | size_t buf_len, len; |
| 277 | int dynamic; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 278 | const char *str, *p; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 279 | char *buf = NULL, *out = NULL; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 280 | |
| 281 | struct lyxml_context ctx; |
| 282 | memset(&ctx, 0, sizeof ctx); |
| 283 | ctx.line = 1; |
| 284 | |
| 285 | /* empty attribute value */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 286 | ctx.status = LYXML_ATTR_CONTENT; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 287 | str = "\"\""; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 288 | assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
| 289 | assert_null(buf); |
| 290 | assert_ptr_equal(&str[-1], out); |
| 291 | assert_int_equal(0, dynamic); |
| 292 | assert_int_equal(0, len); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 293 | assert_true(str[0] == '\0'); /* everything eaten */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 294 | assert_int_equal(LYXML_ATTRIBUTE, ctx.status); |
| 295 | |
| 296 | ctx.status = LYXML_ATTR_CONTENT; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 297 | str = "\'\'"; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 298 | assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
| 299 | assert_null(buf); |
| 300 | assert_ptr_equal(&str[-1], out); |
| 301 | assert_int_equal(0, dynamic); |
| 302 | assert_int_equal(0, len); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 303 | assert_true(str[0] == '\0'); /* everything eaten */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 304 | assert_int_equal(LYXML_ATTRIBUTE, ctx.status); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 305 | |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 306 | /* empty element content - only formating before defining child */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 307 | ctx.status = LYXML_ELEM_CONTENT; |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 308 | str = "\n <"; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 309 | assert_int_equal(LY_EINVAL, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
| 310 | assert_null(buf); |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 311 | assert_string_equal("<", str); |
| 312 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 313 | /* empty element content is invalid - missing content terminating character < */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 314 | ctx.status = LYXML_ELEM_CONTENT; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 315 | str = ""; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 316 | assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
| 317 | assert_null(buf); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 318 | logbuf_assert("Unexpected end-of-file. Line number 2."); |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 319 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 320 | ctx.status = LYXML_ELEM_CONTENT; |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 321 | str = p = "xxx"; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 322 | assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
| 323 | assert_null(buf); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 324 | logbuf_assert("Unexpected end-of-file. Line number 2."); |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 325 | assert_ptr_equal(p, str); /* input data not eaten */ |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 326 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 327 | /* valid strings */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 328 | ctx.status = LYXML_ELEM_CONTENT; |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 329 | str = "€𠜎Øn \n<&"'> ROK<"; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 330 | assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
| 331 | assert_int_not_equal(0, dynamic); |
| 332 | assert_non_null(buf); |
| 333 | assert_ptr_equal(out, buf); |
| 334 | assert_int_equal(22, buf_len); |
| 335 | assert_int_equal(21, len); |
| 336 | assert_string_equal("€𠜎Øn \n<&\"\'> ROK", buf); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 337 | assert_string_equal("<", str); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 338 | assert_int_equal(LYXML_ELEMENT, ctx.status); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 339 | |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 340 | /* test using n-bytes UTF8 hexadecimal code points */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 341 | ctx.status = LYXML_ATTR_CONTENT; |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 342 | str = "\'$¢€𐍈\'"; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 343 | assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
| 344 | assert_int_not_equal(0, dynamic); |
| 345 | assert_non_null(buf); |
| 346 | assert_ptr_equal(out, buf); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 347 | assert_int_equal(22, buf_len); |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 348 | assert_int_equal(10, len); |
| 349 | assert_string_equal("$¢€𐍈", buf); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 350 | assert_int_equal(LYXML_ATTRIBUTE, ctx.status); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 351 | |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 352 | free(buf); |
| 353 | buf = NULL; |
| 354 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 355 | /* invalid characters in string */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 356 | ctx.status = LYXML_ATTR_CONTENT; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 357 | str = p = "\'R\'"; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 358 | assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 359 | logbuf_assert("Invalid character sequence \"'\", expected ;. Line number 3."); |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 360 | assert_null(buf); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 361 | assert_ptr_equal(p, str); /* input data not eaten */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 362 | ctx.status = LYXML_ATTR_CONTENT; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 363 | str = p = "\"R\""; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 364 | assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 365 | logbuf_assert("Invalid character sequence \"\"\", expected ;. Line number 3."); |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 366 | assert_null(buf); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 367 | assert_ptr_equal(p, str); /* input data not eaten */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 368 | ctx.status = LYXML_ATTR_CONTENT; |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 369 | str = p = "\"&nonsence;\""; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 370 | assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 371 | logbuf_assert("Entity reference \"&nonsence;\" not supported, only predefined references allowed. Line number 3."); |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 372 | assert_null(buf); |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 373 | assert_ptr_equal(p, str); /* input data not eaten */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 374 | ctx.status = LYXML_ELEM_CONTENT; |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 375 | str = p = "&#o122;"; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 376 | assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 377 | logbuf_assert("Invalid character reference \"&#o122;\". Line number 3."); |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 378 | assert_null(buf); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 379 | assert_ptr_equal(p, str); /* input data not eaten */ |
| 380 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 381 | ctx.status = LYXML_ATTR_CONTENT; |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 382 | str = p = "\'\'"; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 383 | assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 384 | logbuf_assert("Invalid character reference \"\'\" (0x00000006). Line number 3."); |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 385 | assert_null(buf); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 386 | assert_ptr_equal(p, str); /* input data not eaten */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 387 | ctx.status = LYXML_ATTR_CONTENT; |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 388 | str = p = "\'\'"; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 389 | assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 390 | logbuf_assert("Invalid character reference \"\'\" (0x0000fdd0). Line number 3."); |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 391 | assert_null(buf); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 392 | assert_ptr_equal(p, str); /* input data not eaten */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 393 | ctx.status = LYXML_ATTR_CONTENT; |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 394 | str = p = "\'\'"; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 395 | assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 396 | logbuf_assert("Invalid character reference \"\'\" (0x0000ffff). Line number 3."); |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 397 | assert_null(buf); |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 398 | assert_ptr_equal(p, str); /* input data not eaten */ |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 401 | static void |
| 402 | test_ns(void **state) |
| 403 | { |
| 404 | (void) state; /* unused */ |
| 405 | |
| 406 | const char *e1, *e2; |
| 407 | const struct lyxml_ns *ns; |
| 408 | |
| 409 | struct lyxml_context ctx; |
| 410 | memset(&ctx, 0, sizeof ctx); |
| 411 | ctx.line = 1; |
| 412 | |
| 413 | e1 = "element1"; |
| 414 | e2 = "element2"; |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 415 | /* simulate adding open element1 into context */ |
| 416 | ctx.elements.count++; |
| 417 | /* processing namespace definitions */ |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 418 | assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, e1, NULL, 0, strdup("urn:default"))); |
| 419 | assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, e1, "nc", 2, strdup("urn:nc1"))); |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 420 | /* simulate adding open element2 into context */ |
| 421 | ctx.elements.count++; |
| 422 | /* processing namespace definitions */ |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 423 | assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, e2, "nc", 2, strdup("urn:nc2"))); |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 424 | assert_int_equal(3, ctx.ns.count); |
| 425 | assert_int_not_equal(0, ctx.ns.size); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 426 | |
| 427 | ns = lyxml_ns_get(&ctx, NULL, 0); |
| 428 | assert_non_null(ns); |
| 429 | assert_null(ns->prefix); |
| 430 | assert_string_equal("urn:default", ns->uri); |
| 431 | |
| 432 | ns = lyxml_ns_get(&ctx, "nc", 2); |
| 433 | assert_non_null(ns); |
| 434 | assert_string_equal("nc", ns->prefix); |
| 435 | assert_string_equal("urn:nc2", ns->uri); |
| 436 | |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 437 | /* simulate closing element2 */ |
| 438 | ctx.elements.count--; |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 439 | assert_int_equal(LY_SUCCESS, lyxml_ns_rm(&ctx, e2)); |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 440 | assert_int_equal(2, ctx.ns.count); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 441 | |
| 442 | ns = lyxml_ns_get(&ctx, "nc", 2); |
| 443 | assert_non_null(ns); |
| 444 | assert_string_equal("nc", ns->prefix); |
| 445 | assert_string_equal("urn:nc1", ns->uri); |
| 446 | |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 447 | /* simulate closing element1 */ |
| 448 | ctx.elements.count--; |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 449 | assert_int_equal(LY_SUCCESS, lyxml_ns_rm(&ctx, e1)); |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 450 | assert_int_equal(0, ctx.ns.count); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 451 | |
| 452 | assert_null(lyxml_ns_get(&ctx, "nc", 2)); |
| 453 | assert_null(lyxml_ns_get(&ctx, NULL, 0)); |
| 454 | } |
| 455 | |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 456 | static void |
| 457 | test_ns2(void **state) |
| 458 | { |
| 459 | (void) state; /* unused */ |
| 460 | |
| 461 | const char *e1, *e2; |
| 462 | |
| 463 | struct lyxml_context ctx; |
| 464 | memset(&ctx, 0, sizeof ctx); |
| 465 | ctx.line = 1; |
| 466 | |
| 467 | /* parent element has the same name as its child (and for some reason both names are stored at the same place) */ |
| 468 | e1 = e2 = "element1"; |
| 469 | /* simulate adding open element1 into context */ |
| 470 | ctx.elements.count++; |
| 471 | /* default namespace defined in parent element1 */ |
| 472 | assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, e1, NULL, 0, strdup("urn:default"))); |
| 473 | assert_int_equal(1, ctx.ns.count); |
| 474 | /* going into child element1 */ |
| 475 | e1 = e2; |
| 476 | /* simulate adding open element1 into context */ |
| 477 | ctx.elements.count++; |
| 478 | /* no namespace defined, going out (first, simulate closing of so far open element) */ |
| 479 | ctx.elements.count--; |
| 480 | assert_int_equal(LY_SUCCESS, lyxml_ns_rm(&ctx, e2)); |
| 481 | assert_int_equal(1, ctx.ns.count); |
| 482 | /* nothing else, going out of the parent element1 (first, simulate closing of so far open element) */ |
| 483 | ctx.elements.count--; |
| 484 | assert_int_equal(LY_SUCCESS, lyxml_ns_rm(&ctx, e1)); |
| 485 | assert_int_equal(0, ctx.ns.count); |
| 486 | } |
| 487 | |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 488 | int main(void) |
| 489 | { |
| 490 | const struct CMUnitTest tests[] = { |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 491 | cmocka_unit_test_setup(test_element, logger_setup), |
| 492 | cmocka_unit_test_setup(test_attribute, logger_setup), |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 493 | cmocka_unit_test_setup(test_text, logger_setup), |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 494 | cmocka_unit_test_setup(test_ns, logger_setup), |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 495 | cmocka_unit_test_setup(test_ns2, logger_setup), |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 496 | }; |
| 497 | |
| 498 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 499 | } |