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