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 |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 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'); |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 94 | ctx.status = 0; |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 95 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 96 | /* end element */ |
| 97 | str = "</element>"; |
| 98 | 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] | 99 | logbuf_assert("Opening and closing elements tag missmatch (\"element\"). Line number 1."); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 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 | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 129 | assert_int_equal(LYXML_END, ctx.status); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 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); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 132 | lyxml_context_clear(&ctx); |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 133 | |
| 134 | str = " < element attr=\'x\'/>"; |
| 135 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
| 136 | assert_int_equal(LYXML_ATTRIBUTE, ctx.status); |
| 137 | assert_string_equal("attr=\'x\'/>", str); |
| 138 | assert_int_equal(1, ctx.elements.count); |
| 139 | assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
| 140 | assert_int_equal(LYXML_ATTR_CONTENT, ctx.status); |
| 141 | assert_string_equal("\'x\'/>", str); |
| 142 | assert_int_equal(1, ctx.elements.count); |
| 143 | assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 144 | assert_int_equal(LYXML_END, ctx.status); |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 145 | assert_string_equal("", str); |
| 146 | assert_int_equal(0, ctx.elements.count); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 147 | lyxml_context_clear(&ctx); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 148 | |
Radek Krejci | fb7c658 | 2018-09-21 16:12:45 +0200 | [diff] [blame] | 149 | 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] | 150 | 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] | 151 | assert_null(prefix); |
| 152 | assert_false(strncmp("element", name, name_len)); |
| 153 | assert_int_equal(7, name_len); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 154 | assert_int_equal(LYXML_END, ctx.status); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 155 | assert_string_equal("", str); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 156 | lyxml_context_clear(&ctx); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 157 | |
| 158 | str = "<element xmlns=\"urn\"></element>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 159 | 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] | 160 | assert_null(prefix); |
| 161 | assert_false(strncmp("element", name, name_len)); |
| 162 | assert_int_equal(7, name_len); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 163 | assert_int_equal(LYXML_ATTRIBUTE, ctx.status); |
| 164 | assert_string_equal("xmlns=\"urn\"></element>", str); |
| 165 | /* cleean context by getting closing tag */ |
| 166 | str += 12; |
| 167 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 168 | lyxml_context_clear(&ctx); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 169 | |
| 170 | /* qualified element */ |
| 171 | str = " < yin:element/>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 172 | 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] | 173 | assert_false(strncmp("yin", prefix, prefix_len)); |
| 174 | assert_false(strncmp("element", name, name_len)); |
| 175 | assert_int_equal(3, prefix_len); |
| 176 | assert_int_equal(7, name_len); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 177 | assert_int_equal(LYXML_END, ctx.status); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 178 | assert_string_equal("", str); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 179 | lyxml_context_clear(&ctx); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 180 | |
| 181 | str = "<yin:element xmlns=\"urn\"></element>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 182 | 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] | 183 | assert_false(strncmp("yin", prefix, prefix_len)); |
| 184 | assert_false(strncmp("element", name, name_len)); |
| 185 | assert_int_equal(3, prefix_len); |
| 186 | assert_int_equal(7, name_len); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 187 | assert_int_equal(LYXML_ATTRIBUTE, ctx.status); |
| 188 | assert_string_equal("xmlns=\"urn\"></element>", str); |
| 189 | /* cleean context by getting closing tag */ |
| 190 | str += 12; |
| 191 | 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] | 192 | logbuf_assert("Opening and closing elements tag missmatch (\"element\"). Line number 1."); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 193 | str = "</yin:element/>"; |
| 194 | assert_int_equal(LY_EVALID, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
| 195 | logbuf_assert("Unexpected data \"/>\" in closing element tag. Line number 1."); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 196 | lyxml_context_clear(&ctx); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 197 | |
| 198 | /* UTF8 characters */ |
| 199 | str = "<𠜎€𠜎Øn:𠜎€𠜎Øn/>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 200 | 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] | 201 | assert_false(strncmp("𠜎€𠜎Øn", prefix, prefix_len)); |
| 202 | assert_false(strncmp("𠜎€𠜎Øn", name, name_len)); |
| 203 | assert_int_equal(14, prefix_len); |
| 204 | assert_int_equal(14, name_len); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 205 | assert_int_equal(LYXML_END, ctx.status); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 206 | assert_string_equal("", str); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 207 | lyxml_context_clear(&ctx); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 208 | |
| 209 | /* invalid UTF-8 character */ |
| 210 | str = "<¢:element>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 211 | 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] | 212 | logbuf_assert("Identifier \"¢:element>\" starts with invalid character. Line number 1."); |
| 213 | str = "<yin:c⁐element>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 214 | 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] | 215 | logbuf_assert("Invalid character sequence \"⁐element>\", expected whitespace or element tag termination ('>' or '/>'. Line number 1."); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 216 | lyxml_context_clear(&ctx); |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 217 | |
| 218 | /* mixed content */ |
| 219 | str = "<a>text <b>x</b></a>"; |
| 220 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
| 221 | assert_string_equal("text <b>x</b></a>", str); |
| 222 | assert_int_equal(LYXML_ELEM_CONTENT, ctx.status); |
| 223 | assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
| 224 | logbuf_assert("Mixed XML content is not allowed (text <b>). Line number 1."); |
| 225 | lyxml_context_clear(&ctx); |
| 226 | |
David Sedlák | 54a6f13 | 2019-07-16 15:14:18 +0200 | [diff] [blame] | 227 | /* tag missmatch */ |
| 228 | str = "<a>text</b>"; |
| 229 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &str, &prefix, &prefix_len, &name, &name_len)); |
| 230 | assert_string_equal("text</b>", str); |
| 231 | assert_int_equal(LYXML_ELEM_CONTENT, ctx.status); |
| 232 | assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 233 | logbuf_assert("Opening and closing elements tag missmatch (\"b\", expected \"a\"). Line number 1."); |
David Sedlák | 54a6f13 | 2019-07-16 15:14:18 +0200 | [diff] [blame] | 234 | lyxml_context_clear(&ctx); |
| 235 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static void |
| 239 | test_attribute(void **state) |
| 240 | { |
| 241 | (void) state; /* unused */ |
| 242 | |
| 243 | size_t name_len, prefix_len; |
| 244 | const char *name, *prefix; |
| 245 | const char *str, *p; |
| 246 | |
| 247 | struct lyxml_context ctx; |
| 248 | memset(&ctx, 0, sizeof ctx); |
| 249 | ctx.line = 1; |
| 250 | |
| 251 | /* empty - without element tag termination */ |
| 252 | str = ""; |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +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 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 255 | /* not an attribute */ |
| 256 | str = p = "unknown/>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 257 | 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] | 258 | assert_ptr_equal(p, str); /* input data not eaten */ |
| 259 | logbuf_assert("Invalid character sequence \"/>\", expected whitespace or '='. Line number 1."); |
| 260 | str = p = "unknown />"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 261 | 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] | 262 | assert_ptr_equal(p, str); /* input data not eaten */ |
| 263 | logbuf_assert("Invalid character sequence \"/>\", expected '='. Line number 1."); |
| 264 | str = p = "xxx=/>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 265 | 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] | 266 | assert_ptr_equal(p, str); /* input data not eaten */ |
| 267 | logbuf_assert("Invalid character sequence \"/>\", expected either single or double quotation mark. Line number 1."); |
| 268 | str = p = "xxx\n = yyy/>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 269 | 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] | 270 | assert_ptr_equal(p, str); /* input data not eaten */ |
| 271 | logbuf_assert("Invalid character sequence \"yyy/>\", expected either single or double quotation mark. Line number 2."); |
| 272 | |
| 273 | /* valid attribute */ |
| 274 | str = "xmlns=\"urn\">"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 275 | 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] | 276 | assert_null(name); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 277 | assert_null(prefix); |
Radek Krejci | 4916573 | 2019-05-15 16:41:58 +0200 | [diff] [blame] | 278 | assert_int_equal(0, name_len); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 279 | assert_int_equal(0, prefix_len); |
Radek Krejci | 4916573 | 2019-05-15 16:41:58 +0200 | [diff] [blame] | 280 | assert_int_equal(1, ctx.ns.count); |
| 281 | assert_string_equal("", str); |
| 282 | assert_int_equal(LYXML_ELEM_CONTENT, ctx.status); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 283 | |
Radek Krejci | 4916573 | 2019-05-15 16:41:58 +0200 | [diff] [blame] | 284 | str = "xmlns:nc\n = \'urn\'>"; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 285 | 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] | 286 | assert_null(name); |
| 287 | assert_null(prefix); |
| 288 | assert_int_equal(0, name_len); |
| 289 | assert_int_equal(0, prefix_len); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 290 | assert_int_equal(3, ctx.line); |
Radek Krejci | 4916573 | 2019-05-15 16:41:58 +0200 | [diff] [blame] | 291 | assert_int_equal(2, ctx.ns.count); |
| 292 | assert_string_equal("", str); |
| 293 | assert_int_equal(LYXML_ELEM_CONTENT, ctx.status); |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 294 | |
Radek Krejci | 4916573 | 2019-05-15 16:41:58 +0200 | [diff] [blame] | 295 | lyxml_context_clear(&ctx); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 298 | static void |
| 299 | test_text(void **state) |
| 300 | { |
| 301 | (void) state; /* unused */ |
| 302 | |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 303 | size_t buf_len, len; |
| 304 | int dynamic; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 305 | const char *str, *p; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 306 | char *buf = NULL, *out = NULL; |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 307 | const char *prefix, *name; |
| 308 | size_t prefix_len, name_len; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 309 | |
| 310 | struct lyxml_context ctx; |
| 311 | memset(&ctx, 0, sizeof ctx); |
| 312 | ctx.line = 1; |
| 313 | |
| 314 | /* empty attribute value */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 315 | ctx.status = LYXML_ATTR_CONTENT; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 316 | str = "\"\""; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 317 | assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
| 318 | assert_null(buf); |
| 319 | assert_ptr_equal(&str[-1], out); |
| 320 | assert_int_equal(0, dynamic); |
| 321 | assert_int_equal(0, len); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 322 | assert_true(str[0] == '\0'); /* everything eaten */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 323 | assert_int_equal(LYXML_ATTRIBUTE, ctx.status); |
| 324 | |
| 325 | ctx.status = LYXML_ATTR_CONTENT; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 326 | str = "\'\'"; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 327 | assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
| 328 | assert_null(buf); |
| 329 | assert_ptr_equal(&str[-1], out); |
| 330 | assert_int_equal(0, dynamic); |
| 331 | assert_int_equal(0, len); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 332 | assert_true(str[0] == '\0'); /* everything eaten */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 333 | assert_int_equal(LYXML_ATTRIBUTE, ctx.status); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 334 | |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 335 | /* empty element content - only formating before defining child */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 336 | ctx.status = LYXML_ELEM_CONTENT; |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 337 | str = "<x>\n <y>"; |
| 338 | 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] | 339 | assert_int_equal(LY_EINVAL, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
| 340 | assert_null(buf); |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 341 | assert_string_equal("<y>", str); |
| 342 | lyxml_context_clear(&ctx); |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 343 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 344 | /* empty element content is invalid - missing content terminating character < */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 345 | ctx.status = LYXML_ELEM_CONTENT; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 346 | str = ""; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 347 | assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
| 348 | assert_null(buf); |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 349 | logbuf_assert("Unexpected end-of-input. Line number 2."); |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 350 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 351 | ctx.status = LYXML_ELEM_CONTENT; |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 352 | str = p = "xxx"; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 353 | assert_int_equal(LY_EVALID, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
| 354 | assert_null(buf); |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 355 | logbuf_assert("Unexpected end-of-input. Line number 2."); |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 356 | assert_ptr_equal(p, str); /* input data not eaten */ |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 357 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 358 | /* valid strings */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 359 | ctx.status = LYXML_ELEM_CONTENT; |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 360 | str = "<a>€𠜎Øn \n<&"'> ROK</a>"; |
| 361 | 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] | 362 | assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
| 363 | assert_int_not_equal(0, dynamic); |
| 364 | assert_non_null(buf); |
| 365 | assert_ptr_equal(out, buf); |
| 366 | assert_int_equal(22, buf_len); |
| 367 | assert_int_equal(21, len); |
| 368 | assert_string_equal("€𠜎Øn \n<&\"\'> ROK", buf); |
Radek Krejci | f2c721d | 2019-06-03 16:37:58 +0200 | [diff] [blame] | 369 | assert_string_equal("</a>", str); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 370 | assert_int_equal(LYXML_ELEMENT, ctx.status); |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 371 | lyxml_context_clear(&ctx); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 372 | |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 373 | /* test using n-bytes UTF8 hexadecimal code points */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 374 | ctx.status = LYXML_ATTR_CONTENT; |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 375 | str = "\'$¢€𐍈\'"; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 376 | assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &str, &buf, &buf_len, &out, &len, &dynamic)); |
| 377 | assert_int_not_equal(0, dynamic); |
| 378 | assert_non_null(buf); |
| 379 | assert_ptr_equal(out, buf); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 380 | assert_int_equal(22, buf_len); |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 381 | assert_int_equal(10, len); |
| 382 | assert_string_equal("$¢€𐍈", buf); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 383 | assert_int_equal(LYXML_ATTRIBUTE, ctx.status); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 384 | |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 385 | free(buf); |
| 386 | buf = NULL; |
| 387 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 388 | /* invalid characters in string */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 389 | ctx.status = LYXML_ATTR_CONTENT; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 390 | str = p = "\'R\'"; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 391 | 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] | 392 | logbuf_assert("Invalid character sequence \"'\", expected ;. Line number 3."); |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 393 | assert_null(buf); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 394 | assert_ptr_equal(p, str); /* input data not eaten */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 395 | ctx.status = LYXML_ATTR_CONTENT; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 396 | str = p = "\"R\""; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 397 | 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] | 398 | logbuf_assert("Invalid character sequence \"\"\", expected ;. Line number 3."); |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 399 | assert_null(buf); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 400 | assert_ptr_equal(p, str); /* input data not eaten */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 401 | ctx.status = LYXML_ATTR_CONTENT; |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 402 | str = p = "\"&nonsence;\""; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 403 | 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] | 404 | 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] | 405 | assert_null(buf); |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 406 | assert_ptr_equal(p, str); /* input data not eaten */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 407 | ctx.status = LYXML_ELEM_CONTENT; |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 408 | str = p = "&#o122;"; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 409 | 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] | 410 | logbuf_assert("Invalid character reference \"&#o122;\". Line number 3."); |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 411 | assert_null(buf); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 412 | assert_ptr_equal(p, str); /* input data not eaten */ |
| 413 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 414 | ctx.status = LYXML_ATTR_CONTENT; |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 415 | str = p = "\'\'"; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 416 | 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] | 417 | logbuf_assert("Invalid character reference \"\'\" (0x00000006). Line number 3."); |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 418 | assert_null(buf); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 419 | assert_ptr_equal(p, str); /* input data not eaten */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 420 | ctx.status = LYXML_ATTR_CONTENT; |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 421 | str = p = "\'\'"; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 422 | 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] | 423 | logbuf_assert("Invalid character reference \"\'\" (0x0000fdd0). Line number 3."); |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 424 | assert_null(buf); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 425 | assert_ptr_equal(p, str); /* input data not eaten */ |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 426 | ctx.status = LYXML_ATTR_CONTENT; |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 427 | str = p = "\'\'"; |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 428 | 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] | 429 | logbuf_assert("Invalid character reference \"\'\" (0x0000ffff). Line number 3."); |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 430 | assert_null(buf); |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 431 | assert_ptr_equal(p, str); /* input data not eaten */ |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 434 | static void |
| 435 | test_ns(void **state) |
| 436 | { |
| 437 | (void) state; /* unused */ |
| 438 | |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 439 | const struct lyxml_ns *ns; |
| 440 | |
| 441 | struct lyxml_context ctx; |
| 442 | memset(&ctx, 0, sizeof ctx); |
| 443 | ctx.line = 1; |
| 444 | |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 445 | /* simulate adding open element1 into context */ |
| 446 | ctx.elements.count++; |
| 447 | /* processing namespace definitions */ |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 448 | assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, NULL, 0, strdup("urn:default"))); |
| 449 | 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] | 450 | /* simulate adding open element2 into context */ |
| 451 | ctx.elements.count++; |
| 452 | /* processing namespace definitions */ |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 453 | 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] | 454 | assert_int_equal(3, ctx.ns.count); |
| 455 | assert_int_not_equal(0, ctx.ns.size); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 456 | |
| 457 | ns = lyxml_ns_get(&ctx, NULL, 0); |
| 458 | assert_non_null(ns); |
| 459 | assert_null(ns->prefix); |
| 460 | assert_string_equal("urn:default", ns->uri); |
| 461 | |
| 462 | ns = lyxml_ns_get(&ctx, "nc", 2); |
| 463 | assert_non_null(ns); |
| 464 | assert_string_equal("nc", ns->prefix); |
| 465 | assert_string_equal("urn:nc2", ns->uri); |
| 466 | |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 467 | /* simulate closing element2 */ |
| 468 | ctx.elements.count--; |
Radek Krejci | 17dca99 | 2019-05-17 10:53:27 +0200 | [diff] [blame] | 469 | lyxml_ns_rm(&ctx); |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 470 | assert_int_equal(2, ctx.ns.count); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 471 | |
| 472 | ns = lyxml_ns_get(&ctx, "nc", 2); |
| 473 | assert_non_null(ns); |
| 474 | assert_string_equal("nc", ns->prefix); |
| 475 | assert_string_equal("urn:nc1", ns->uri); |
| 476 | |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 477 | /* simulate closing element1 */ |
| 478 | ctx.elements.count--; |
Radek Krejci | 17dca99 | 2019-05-17 10:53:27 +0200 | [diff] [blame] | 479 | lyxml_ns_rm(&ctx); |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 480 | assert_int_equal(0, ctx.ns.count); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 481 | |
| 482 | assert_null(lyxml_ns_get(&ctx, "nc", 2)); |
| 483 | assert_null(lyxml_ns_get(&ctx, NULL, 0)); |
| 484 | } |
| 485 | |
David Sedlák | b3bed7a | 2019-03-08 11:53:37 +0100 | [diff] [blame] | 486 | static void |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 487 | test_ns2(void **state) |
| 488 | { |
| 489 | (void) state; /* unused */ |
| 490 | |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 491 | struct lyxml_context ctx; |
| 492 | memset(&ctx, 0, sizeof ctx); |
| 493 | ctx.line = 1; |
| 494 | |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 495 | /* simulate adding open element1 into context */ |
| 496 | ctx.elements.count++; |
| 497 | /* default namespace defined in parent element1 */ |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 498 | 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] | 499 | assert_int_equal(1, ctx.ns.count); |
| 500 | /* going into child element1 */ |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 501 | /* simulate adding open element1 into context */ |
| 502 | ctx.elements.count++; |
| 503 | /* no namespace defined, going out (first, simulate closing of so far open element) */ |
| 504 | ctx.elements.count--; |
Radek Krejci | 17dca99 | 2019-05-17 10:53:27 +0200 | [diff] [blame] | 505 | lyxml_ns_rm(&ctx); |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 506 | assert_int_equal(1, ctx.ns.count); |
| 507 | /* nothing else, going out of the parent element1 (first, simulate closing of so far open element) */ |
| 508 | ctx.elements.count--; |
Radek Krejci | 17dca99 | 2019-05-17 10:53:27 +0200 | [diff] [blame] | 509 | lyxml_ns_rm(&ctx); |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 510 | assert_int_equal(0, ctx.ns.count); |
| 511 | } |
| 512 | |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 513 | static void |
| 514 | test_simple_xml(void **state) |
| 515 | { |
| 516 | (void)state; /* unused */ |
| 517 | size_t name_len, prefix_len; |
| 518 | const char *prefix, *name; |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 519 | struct lyxml_context ctx; |
| 520 | |
| 521 | char *buf = NULL, *output = NULL; |
| 522 | size_t buf_size, length; |
| 523 | int dynamic; |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 524 | const char *test_input = "<elem1 attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>"; |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 525 | |
| 526 | memset(&ctx, 0, sizeof ctx); |
| 527 | ctx.line = 1; |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 528 | |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 529 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &test_input, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 530 | assert_int_equal(ctx.status, LYXML_ATTRIBUTE); |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 531 | assert_string_equal(test_input, "attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>"); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 532 | |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 533 | assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &test_input, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 534 | assert_int_equal(ctx.status, LYXML_ATTR_CONTENT); |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 535 | assert_string_equal(test_input, "\"value\"> <elem2 attr2=\"value\" /> </elem1>"); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 536 | |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 537 | assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &test_input, &buf, &buf_size, &output, &length, &dynamic)); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 538 | assert_int_equal(ctx.status, LYXML_ELEM_CONTENT); |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 539 | assert_string_equal(test_input, " <elem2 attr2=\"value\" /> </elem1>"); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 540 | |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 541 | /* try to get string content of elem1 whitespace is removed and EINVAL is expected in this case as well as moving status from element |
| 542 | * content to the element */ |
| 543 | assert_int_equal(LY_EINVAL, lyxml_get_string(&ctx, &test_input, &buf, &buf_size, &output, &length, &dynamic)); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 544 | assert_int_equal(ctx.status, LYXML_ELEMENT); |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 545 | assert_string_equal(test_input, "<elem2 attr2=\"value\" /> </elem1>"); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 546 | |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 547 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &test_input, &prefix, &prefix_len, &name, &name_len)); |
| 548 | assert_int_equal(ctx.status, LYXML_ATTRIBUTE); |
| 549 | assert_string_equal(test_input, "attr2=\"value\" /> </elem1>"); |
| 550 | |
| 551 | assert_int_equal(LY_SUCCESS, lyxml_get_attribute(&ctx, &test_input, &prefix, &prefix_len, &name, &name_len)); |
| 552 | assert_int_equal(ctx.status, LYXML_ATTR_CONTENT); |
| 553 | assert_string_equal(test_input, "\"value\" /> </elem1>"); |
| 554 | |
| 555 | assert_int_equal(LY_SUCCESS, lyxml_get_string(&ctx, &test_input, &buf, &buf_size, &output, &length, &dynamic)); |
| 556 | assert_int_equal(ctx.status, LYXML_ELEMENT); |
| 557 | assert_string_equal(test_input, " </elem1>"); |
| 558 | |
| 559 | assert_int_equal(LY_SUCCESS, lyxml_get_element(&ctx, &test_input, &prefix, &prefix_len, &name, &name_len)); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 560 | assert_int_equal(ctx.status, LYXML_END); |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 561 | assert_string_equal(test_input, ""); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 562 | |
| 563 | lyxml_context_clear(&ctx); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 564 | } |
| 565 | |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 566 | int main(void) |
| 567 | { |
| 568 | const struct CMUnitTest tests[] = { |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 569 | cmocka_unit_test_setup(test_element, logger_setup), |
| 570 | cmocka_unit_test_setup(test_attribute, logger_setup), |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 571 | cmocka_unit_test_setup(test_text, logger_setup), |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 572 | cmocka_unit_test_setup(test_ns, logger_setup), |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 573 | cmocka_unit_test_setup(test_ns2, logger_setup), |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 574 | cmocka_unit_test_setup(test_simple_xml, logger_setup), |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 575 | }; |
| 576 | |
| 577 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 578 | } |