Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 1 | /* |
aPiecek | 023f83a | 2021-05-11 07:37:03 +0200 | [diff] [blame] | 2 | * @file test_xml.c |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 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 | */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 14 | #define _UTEST_MAIN_ |
| 15 | #include "utests.h" |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 16 | |
Radek Krejci | f8dc59a | 2020-11-25 13:47:44 +0100 | [diff] [blame] | 17 | #define _POSIX_C_SOURCE 200809L /* strdup */ |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 18 | |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 19 | #include <string.h> |
| 20 | |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 21 | #include "context.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 22 | #include "in_internal.h" |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 23 | #include "xml.h" |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 24 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 25 | LY_ERR lyxml_ns_add(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len, char *uri); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 26 | |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 27 | static void |
| 28 | test_element(void **state) |
| 29 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 30 | struct lyxml_ctx *xmlctx; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 31 | struct ly_in *in; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 32 | const char *str; |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 33 | |
| 34 | /* empty */ |
| 35 | str = ""; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 36 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 37 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 38 | assert_int_equal(LYXML_END, xmlctx->status); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 39 | lyxml_ctx_free(xmlctx); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 40 | ly_in_free(in, 0); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 41 | |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 42 | /* end element */ |
| 43 | str = "</element>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 44 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 45 | assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
| 46 | CHECK_LOG_CTX("Stray closing element tag (\"element\").", "Line number 1."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 47 | ly_in_free(in, 0); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 48 | |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 49 | /* no element */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 50 | UTEST_LOG_CLEAN; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 51 | str = "no data present"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 52 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 53 | assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
| 54 | CHECK_LOG_CTX("Invalid character sequence \"no data present\", expected element tag start ('<').", "Line number 1."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 55 | ly_in_free(in, 0); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 56 | |
| 57 | /* not supported DOCTYPE */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 58 | str = "<!DOCTYPE greeting SYSTEM \"hello.dtd\"><greeting/>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 59 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 60 | assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
| 61 | CHECK_LOG_CTX("Document Type Declaration not supported.", "Line number 1."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 62 | ly_in_free(in, 0); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 63 | |
Radek Krejci | c5c31bb | 2019-04-08 14:40:52 +0200 | [diff] [blame] | 64 | /* invalid XML */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 65 | str = "<!NONSENSE/>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 66 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 67 | assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
| 68 | CHECK_LOG_CTX("Unknown XML section \"<!NONSENSE/>\".", "Line number 1."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 69 | ly_in_free(in, 0); |
Radek Krejci | c5c31bb | 2019-04-08 14:40:52 +0200 | [diff] [blame] | 70 | |
Michal Vasko | 4b3f295 | 2022-08-26 09:24:04 +0200 | [diff] [blame] | 71 | /* namespace ambiguity */ |
| 72 | str = "<element xmlns=\"urn1\" xmlns=\"urn2\"/>"; |
| 73 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
| 74 | assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
| 75 | CHECK_LOG_CTX("Duplicate default XML namespaces \"urn1\" and \"urn2\".", "Line number 1."); |
| 76 | ly_in_free(in, 0); |
| 77 | |
| 78 | /* prefix duplicate */ |
| 79 | str = "<element xmlns:a=\"urn1\" xmlns:a=\"urn2\"/>"; |
| 80 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
| 81 | assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
| 82 | CHECK_LOG_CTX("Duplicate XML NS prefix \"a\" used for namespaces \"urn1\" and \"urn2\".", "Line number 1."); |
| 83 | ly_in_free(in, 0); |
| 84 | |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 85 | /* unqualified element */ |
| 86 | str = " < element/>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 87 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 88 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 89 | assert_int_equal(LYXML_ELEMENT, xmlctx->status); |
| 90 | assert_null(xmlctx->prefix); |
| 91 | assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len)); |
| 92 | assert_int_equal(1, xmlctx->elements.count); |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 93 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 94 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 95 | assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status); |
| 96 | assert_true(!strncmp("", xmlctx->value, xmlctx->value_len)); |
| 97 | |
| 98 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 99 | assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status); |
| 100 | |
| 101 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 102 | assert_int_equal(LYXML_END, xmlctx->status); |
| 103 | lyxml_ctx_free(xmlctx); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 104 | ly_in_free(in, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 105 | |
| 106 | /* element with attribute */ |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 107 | str = " < element attr=\'x\'/>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 108 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 109 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 110 | assert_int_equal(LYXML_ELEMENT, xmlctx->status); |
| 111 | assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len)); |
| 112 | assert_null(xmlctx->prefix); |
| 113 | assert_int_equal(1, xmlctx->elements.count); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 114 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 115 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 116 | assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status); |
| 117 | assert_true(!strncmp("attr", xmlctx->name, xmlctx->name_len)); |
| 118 | assert_null(xmlctx->prefix); |
| 119 | |
| 120 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 121 | assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status); |
| 122 | assert_int_equal(1, xmlctx->elements.count); |
| 123 | assert_true(!strncmp("x", xmlctx->value, xmlctx->value_len)); |
| 124 | |
| 125 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 126 | assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status); |
| 127 | |
| 128 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 129 | assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status); |
| 130 | assert_int_equal(0, xmlctx->elements.count); |
| 131 | |
| 132 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 133 | assert_int_equal(LYXML_END, xmlctx->status); |
| 134 | lyxml_ctx_free(xmlctx); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 135 | ly_in_free(in, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 136 | |
| 137 | /* headers and comments */ |
Michal Vasko | 8cf6f72 | 2022-02-18 13:08:23 +0100 | [diff] [blame] | 138 | str = "<?xml version=\"1.0\"?> <!-- comment --> <?TEST xxx?> <element/>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 139 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 140 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 141 | assert_int_equal(LYXML_ELEMENT, xmlctx->status); |
| 142 | assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len)); |
| 143 | assert_null(xmlctx->prefix); |
| 144 | assert_int_equal(1, xmlctx->elements.count); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 145 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 146 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 147 | assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status); |
| 148 | |
| 149 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 150 | assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status); |
| 151 | |
| 152 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 153 | assert_int_equal(LYXML_END, xmlctx->status); |
| 154 | lyxml_ctx_free(xmlctx); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 155 | ly_in_free(in, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 156 | |
| 157 | /* separate opening and closing tags, neamespaced parsed internally */ |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 158 | str = "<element xmlns=\"urn\"></element>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 159 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 160 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 161 | assert_int_equal(LYXML_ELEMENT, xmlctx->status); |
| 162 | assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len)); |
| 163 | assert_null(xmlctx->prefix); |
| 164 | assert_int_equal(1, xmlctx->elements.count); |
| 165 | assert_int_equal(1, xmlctx->ns.count); |
| 166 | |
| 167 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 168 | assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status); |
| 169 | |
| 170 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 171 | assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status); |
| 172 | assert_int_equal(0, xmlctx->elements.count); |
| 173 | assert_int_equal(0, xmlctx->ns.count); |
| 174 | |
| 175 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 176 | assert_int_equal(LYXML_END, xmlctx->status); |
| 177 | lyxml_ctx_free(xmlctx); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 178 | ly_in_free(in, 0); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 179 | |
| 180 | /* qualified element */ |
| 181 | str = " < yin:element/>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 182 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 183 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 184 | assert_int_equal(LYXML_ELEMENT, xmlctx->status); |
| 185 | assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len)); |
| 186 | assert_true(!strncmp("yin", xmlctx->prefix, xmlctx->prefix_len)); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 187 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 188 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 189 | assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status); |
| 190 | |
| 191 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 192 | assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status); |
| 193 | |
| 194 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 195 | assert_int_equal(LYXML_END, xmlctx->status); |
| 196 | lyxml_ctx_free(xmlctx); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 197 | ly_in_free(in, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 198 | |
| 199 | /* non-matching closing tag */ |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 200 | str = "<yin:element xmlns=\"urn\"></element>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 201 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 202 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 203 | assert_int_equal(LYXML_ELEMENT, xmlctx->status); |
| 204 | assert_true(!strncmp("element", xmlctx->name, xmlctx->name_len)); |
| 205 | assert_true(!strncmp("yin", xmlctx->prefix, xmlctx->prefix_len)); |
| 206 | assert_int_equal(1, xmlctx->elements.count); |
| 207 | assert_int_equal(1, xmlctx->ns.count); |
| 208 | |
| 209 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 210 | assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status); |
| 211 | |
| 212 | assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 213 | CHECK_LOG_CTX("Opening (\"yin:element\") and closing (\"element\") elements tag mismatch.", "Line number 1."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 214 | lyxml_ctx_free(xmlctx); |
| 215 | ly_in_free(in, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 216 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 217 | /* invalid closing tag */ |
| 218 | str = "<yin:element xmlns=\"urn\"></yin:element/>"; |
| 219 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 220 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 221 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 222 | assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 223 | CHECK_LOG_CTX("Invalid character sequence \"/>\", expected element tag termination ('>').", "Line number 1."); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 224 | lyxml_ctx_free(xmlctx); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 225 | ly_in_free(in, 0); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 226 | |
| 227 | /* UTF8 characters */ |
| 228 | str = "<𠜎€𠜎Øn:𠜎€𠜎Øn/>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 229 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 230 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 231 | assert_true(!strncmp("𠜎€𠜎Øn", xmlctx->name, xmlctx->name_len)); |
| 232 | assert_true(!strncmp("𠜎€𠜎Øn", xmlctx->prefix, xmlctx->prefix_len)); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 233 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 234 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 235 | assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status); |
| 236 | |
| 237 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 238 | assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status); |
| 239 | |
| 240 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 241 | assert_int_equal(LYXML_END, xmlctx->status); |
| 242 | lyxml_ctx_free(xmlctx); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 243 | ly_in_free(in, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 244 | |
| 245 | /* invalid UTF-8 characters */ |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 246 | str = "<¢:element>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 247 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 248 | assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
| 249 | CHECK_LOG_CTX("Identifier \"¢:element>\" starts with an invalid character.", "Line number 1."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 250 | ly_in_free(in, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 251 | |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 252 | str = "<yin:c⁐element>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 253 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 254 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 255 | assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 256 | CHECK_LOG_CTX("Invalid character sequence \"⁐element>\", expected element tag end ('>' or '/>') or an attribute.", "Line number 1."); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 257 | lyxml_ctx_free(xmlctx); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 258 | ly_in_free(in, 0); |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 259 | |
| 260 | /* mixed content */ |
| 261 | str = "<a>text <b>x</b></a>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 262 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 263 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 264 | assert_int_equal(LYXML_ELEMENT, xmlctx->status); |
| 265 | assert_true(!strncmp("a", xmlctx->name, xmlctx->name_len)); |
| 266 | assert_null(xmlctx->prefix); |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 267 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 268 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 269 | assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status); |
| 270 | assert_true(!strncmp("text ", xmlctx->value, xmlctx->value_len)); |
| 271 | |
| 272 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 273 | assert_int_equal(LYXML_ELEMENT, xmlctx->status); |
| 274 | assert_true(!strncmp("b", xmlctx->name, xmlctx->name_len)); |
| 275 | assert_null(xmlctx->prefix); |
| 276 | |
| 277 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 278 | assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status); |
| 279 | assert_true(!strncmp("x", xmlctx->value, xmlctx->value_len)); |
| 280 | |
| 281 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 282 | assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status); |
| 283 | |
| 284 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 285 | assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status); |
| 286 | |
| 287 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 288 | assert_int_equal(LYXML_END, xmlctx->status); |
| 289 | lyxml_ctx_free(xmlctx); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 290 | ly_in_free(in, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 291 | |
| 292 | /* tag mismatch */ |
David Sedlák | 54a6f13 | 2019-07-16 15:14:18 +0200 | [diff] [blame] | 293 | str = "<a>text</b>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 294 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 295 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 296 | assert_int_equal(LYXML_ELEMENT, xmlctx->status); |
| 297 | assert_true(!strncmp("a", xmlctx->name, xmlctx->name_len)); |
| 298 | assert_null(xmlctx->prefix); |
David Sedlák | 54a6f13 | 2019-07-16 15:14:18 +0200 | [diff] [blame] | 299 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 300 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 301 | assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status); |
| 302 | assert_true(!strncmp("text", xmlctx->value, xmlctx->value_len)); |
| 303 | |
| 304 | assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 305 | CHECK_LOG_CTX("Opening (\"a\") and closing (\"b\") elements tag mismatch.", "Line number 1."); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 306 | lyxml_ctx_free(xmlctx); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 307 | ly_in_free(in, 0); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static void |
| 311 | test_attribute(void **state) |
| 312 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 313 | const char *str; |
| 314 | struct lyxml_ctx *xmlctx; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 315 | struct ly_in *in; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 316 | struct lyxml_ns *ns; |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 317 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 318 | /* not an attribute */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 319 | str = "<e unknown/>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 320 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 321 | assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
| 322 | CHECK_LOG_CTX("Invalid character sequence \"/>\", expected '='.", "Line number 1."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 323 | ly_in_free(in, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 324 | |
| 325 | str = "<e xxx=/>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 326 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 327 | assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
| 328 | CHECK_LOG_CTX("Invalid character sequence \"/>\", expected either single or double quotation mark.", "Line number 1."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 329 | ly_in_free(in, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 330 | |
| 331 | str = "<e xxx\n = yyy/>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 332 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 333 | assert_int_equal(LY_EVALID, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
| 334 | CHECK_LOG_CTX("Invalid character sequence \"yyy/>\", expected either single or double quotation mark.", "Line number 2."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 335 | ly_in_free(in, 0); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 336 | |
| 337 | /* valid attribute */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 338 | str = "<e attr=\"val\""; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 339 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 340 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 341 | assert_int_equal(LYXML_ELEMENT, xmlctx->status); |
| 342 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 343 | assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status); |
| 344 | assert_true(!strncmp("attr", xmlctx->name, xmlctx->name_len)); |
| 345 | assert_null(xmlctx->prefix); |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 346 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 347 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 348 | assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status); |
Radek Iša | 85a5523 | 2021-02-16 10:44:43 +0100 | [diff] [blame] | 349 | assert_true(!strncmp("val", xmlctx->value, xmlctx->value_len)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 350 | assert_int_equal(xmlctx->ws_only, 0); |
| 351 | assert_int_equal(xmlctx->dynamic, 0); |
| 352 | lyxml_ctx_free(xmlctx); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 353 | ly_in_free(in, 0); |
Radek Krejci | 28e8cb5 | 2019-03-08 11:31:31 +0100 | [diff] [blame] | 354 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 355 | /* valid namespace with prefix */ |
| 356 | str = "<e xmlns:nc\n = \'urn\'/>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 357 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 358 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 359 | assert_int_equal(LYXML_ELEMENT, xmlctx->status); |
| 360 | assert_int_equal(1, xmlctx->ns.count); |
| 361 | ns = (struct lyxml_ns *)xmlctx->ns.objs[0]; |
| 362 | assert_string_equal(ns->prefix, "nc"); |
| 363 | assert_string_equal(ns->uri, "urn"); |
| 364 | lyxml_ctx_free(xmlctx); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 365 | ly_in_free(in, 0); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 366 | } |
| 367 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 368 | static void |
| 369 | test_text(void **state) |
| 370 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 371 | const char *str; |
| 372 | struct lyxml_ctx *xmlctx; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 373 | struct ly_in *in; |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 374 | |
| 375 | /* empty attribute value */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 376 | str = "<e a=\"\""; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 377 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 378 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 379 | assert_int_equal(LYXML_ELEMENT, xmlctx->status); |
| 380 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 381 | assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status); |
Radek Krejci | b189064 | 2018-10-03 14:05:40 +0200 | [diff] [blame] | 382 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 383 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 384 | assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status); |
| 385 | assert_true(!strncmp("", xmlctx->value, xmlctx->value_len)); |
| 386 | assert_int_equal(xmlctx->ws_only, 1); |
| 387 | assert_int_equal(xmlctx->dynamic, 0); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 388 | ly_in_free(in, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 389 | |
| 390 | /* empty value but in single quotes */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 391 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'\'", &in)); |
| 392 | xmlctx->in = in; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 393 | LOG_LOCINIT(NULL, NULL, NULL, in); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 394 | xmlctx->status = LYXML_ATTRIBUTE; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 395 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 396 | assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status); |
| 397 | assert_true(!strncmp("", xmlctx->value, xmlctx->value_len)); |
| 398 | assert_int_equal(xmlctx->ws_only, 1); |
| 399 | assert_int_equal(xmlctx->dynamic, 0); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 400 | ly_in_free(in, 0); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 401 | |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 402 | /* empty element content - only formating before defining child */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 403 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(">\n <y>", &in)); |
| 404 | xmlctx->in = in; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 405 | LOG_LOCINIT(NULL, NULL, NULL, in); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 406 | xmlctx->status = LYXML_ELEMENT; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 407 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 408 | assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status); |
| 409 | assert_true(!strncmp("\n ", xmlctx->value, xmlctx->value_len)); |
| 410 | assert_int_equal(xmlctx->ws_only, 1); |
| 411 | assert_int_equal(xmlctx->dynamic, 0); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 412 | ly_in_free(in, 0); |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 413 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 414 | /* empty element content is invalid - missing content terminating character < */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 415 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("", &in)); |
| 416 | xmlctx->in = in; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 417 | LOG_LOCINIT(NULL, NULL, NULL, in); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 418 | xmlctx->status = LYXML_ELEM_CONTENT; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 419 | assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx)); |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 420 | CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 421 | ly_in_free(in, 0); |
Radek Krejci | ed6c6ad | 2018-09-26 09:10:18 +0200 | [diff] [blame] | 422 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 423 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("xxx", &in)); |
| 424 | xmlctx->in = in; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 425 | LOG_LOCINIT(NULL, NULL, NULL, in); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 426 | xmlctx->status = LYXML_ELEM_CONTENT; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 427 | assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx)); |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 428 | CHECK_LOG_CTX("Invalid character sequence \"xxx\", expected element tag start ('<').", "Line number 1."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 429 | ly_in_free(in, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 430 | |
| 431 | lyxml_ctx_free(xmlctx); |
Michal Vasko | beaca3f | 2022-08-26 09:24:36 +0200 | [diff] [blame] | 432 | LOG_LOCBACK(0, 0, 0, 4); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 433 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 434 | /* valid strings */ |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 435 | str = "<a>€𠜎Øn \n<&"'> ROK</a>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 436 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 437 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 438 | assert_int_equal(LYXML_ELEMENT, xmlctx->status); |
| 439 | |
| 440 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 441 | assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status); |
| 442 | assert_true(!strncmp("€𠜎Øn \n<&\"\'> ROK", xmlctx->value, xmlctx->value_len)); |
| 443 | assert_int_equal(xmlctx->ws_only, 0); |
| 444 | assert_int_equal(xmlctx->dynamic, 1); |
| 445 | free((char *)xmlctx->value); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 446 | ly_in_free(in, 0); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 447 | |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 448 | /* test using n-bytes UTF8 hexadecimal code points */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 449 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'$¢€𐍈\'", &in)); |
| 450 | xmlctx->in = in; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 451 | LOG_LOCINIT(NULL, NULL, NULL, in); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 452 | xmlctx->status = LYXML_ATTRIBUTE; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 453 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 454 | assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status); |
| 455 | assert_true(!strncmp("$¢€𐍈", xmlctx->value, xmlctx->value_len)); |
| 456 | assert_int_equal(xmlctx->ws_only, 0); |
| 457 | assert_int_equal(xmlctx->dynamic, 1); |
Michal Vasko | 8cf6f72 | 2022-02-18 13:08:23 +0100 | [diff] [blame] | 458 | ly_in_free(in, 0); |
| 459 | |
| 460 | /* CDATA value */ |
| 461 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("> <![CDATA[ special non-escaped chars <>&\"' ]]> </a>", &in)); |
| 462 | xmlctx->in = in; |
| 463 | LOG_LOCINIT(NULL, NULL, NULL, in); |
| 464 | xmlctx->status = LYXML_ATTR_CONTENT; |
| 465 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 466 | assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status); |
| 467 | assert_true(!strncmp(" special non-escaped chars <>&\"' ", xmlctx->value, xmlctx->value_len)); |
| 468 | assert_int_equal(xmlctx->ws_only, 0); |
| 469 | assert_int_equal(xmlctx->dynamic, 1); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 470 | free((char *)xmlctx->value); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 471 | ly_in_free(in, 0); |
Radek Krejci | d70d107 | 2018-10-09 14:20:47 +0200 | [diff] [blame] | 472 | |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 473 | /* invalid characters in string */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 474 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'R\'", &in)); |
| 475 | xmlctx->in = in; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 476 | LOG_LOCINIT(NULL, NULL, NULL, in); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 477 | xmlctx->status = LYXML_ATTRIBUTE; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 478 | assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx)); |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 479 | CHECK_LOG_CTX("Invalid character sequence \"'\", expected ;.", "Line number 1."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 480 | ly_in_free(in, 0); |
Radek Krejci | 117d208 | 2018-09-26 10:05:14 +0200 | [diff] [blame] | 481 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 482 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\"R\"", &in)); |
| 483 | xmlctx->in = in; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 484 | LOG_LOCINIT(NULL, NULL, NULL, in); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 485 | xmlctx->status = LYXML_ATTRIBUTE; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 486 | assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx)); |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 487 | CHECK_LOG_CTX("Invalid character sequence \"\"\", expected ;.", "Line number 1."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 488 | ly_in_free(in, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 489 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 490 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\"&nonsense;\"", &in)); |
| 491 | xmlctx->in = in; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 492 | LOG_LOCINIT(NULL, NULL, NULL, in); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 493 | xmlctx->status = LYXML_ATTRIBUTE; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 494 | assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx)); |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 495 | CHECK_LOG_CTX("Entity reference \"&nonsense;\" not supported, only predefined references allowed.", "Line number 1."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 496 | ly_in_free(in, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 497 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 498 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(">&#o122;", &in)); |
| 499 | xmlctx->in = in; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 500 | LOG_LOCINIT(NULL, NULL, NULL, in); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 501 | xmlctx->status = LYXML_ELEMENT; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 502 | assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx)); |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 503 | CHECK_LOG_CTX("Invalid character reference \"&#o122;\".", "Line number 1."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 504 | ly_in_free(in, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 505 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 506 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'\'", &in)); |
| 507 | xmlctx->in = in; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 508 | LOG_LOCINIT(NULL, NULL, NULL, in); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 509 | xmlctx->status = LYXML_ATTRIBUTE; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 510 | assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx)); |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 511 | CHECK_LOG_CTX("Invalid character reference \"\'\" (0x00000006).", "Line number 1."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 512 | ly_in_free(in, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 513 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 514 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'\'", &in)); |
| 515 | xmlctx->in = in; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 516 | LOG_LOCINIT(NULL, NULL, NULL, in); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 517 | xmlctx->status = LYXML_ATTRIBUTE; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 518 | assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx)); |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 519 | CHECK_LOG_CTX("Invalid character reference \"\'\" (0x0000fdd0).", "Line number 1."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 520 | ly_in_free(in, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 521 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 522 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("=\'\'", &in)); |
| 523 | xmlctx->in = in; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 524 | LOG_LOCINIT(NULL, NULL, NULL, in); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 525 | xmlctx->status = LYXML_ATTRIBUTE; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 526 | assert_int_equal(LY_EVALID, lyxml_ctx_next(xmlctx)); |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 527 | CHECK_LOG_CTX("Invalid character reference \"\'\" (0x0000ffff).", "Line number 1."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 528 | ly_in_free(in, 0); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 529 | |
| 530 | lyxml_ctx_free(xmlctx); |
Michal Vasko | beaca3f | 2022-08-26 09:24:36 +0200 | [diff] [blame] | 531 | LOG_LOCBACK(0, 0, 0, 9); |
Radek Krejci | 7a7fa90 | 2018-09-25 17:08:21 +0200 | [diff] [blame] | 532 | } |
| 533 | |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 534 | static void |
| 535 | test_ns(void **state) |
| 536 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 537 | const char *str; |
| 538 | struct lyxml_ctx *xmlctx; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 539 | struct ly_in *in; |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 540 | const struct lyxml_ns *ns; |
| 541 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 542 | /* opening element1 */ |
| 543 | str = "<element1/>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 544 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 545 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 546 | |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 547 | /* processing namespace definitions */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 548 | assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, NULL, 0, strdup("urn:default"))); |
| 549 | assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, "nc", 2, strdup("urn:nc1"))); |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 550 | /* simulate adding open element2 into context */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 551 | xmlctx->elements.count++; |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 552 | /* processing namespace definitions */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 553 | assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, "nc", 2, strdup("urn:nc2"))); |
| 554 | assert_int_equal(3, xmlctx->ns.count); |
| 555 | assert_int_not_equal(0, xmlctx->ns.size); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 556 | |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 557 | ns = lyxml_ns_get(&xmlctx->ns, NULL, 0); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 558 | assert_non_null(ns); |
| 559 | assert_null(ns->prefix); |
| 560 | assert_string_equal("urn:default", ns->uri); |
| 561 | |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 562 | ns = lyxml_ns_get(&xmlctx->ns, "nc", 2); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 563 | assert_non_null(ns); |
| 564 | assert_string_equal("nc", ns->prefix); |
| 565 | assert_string_equal("urn:nc2", ns->uri); |
| 566 | |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 567 | /* simulate closing element2 */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 568 | xmlctx->elements.count--; |
| 569 | lyxml_ns_rm(xmlctx); |
| 570 | assert_int_equal(2, xmlctx->ns.count); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 571 | |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 572 | ns = lyxml_ns_get(&xmlctx->ns, "nc", 2); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 573 | assert_non_null(ns); |
| 574 | assert_string_equal("nc", ns->prefix); |
| 575 | assert_string_equal("urn:nc1", ns->uri); |
| 576 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 577 | /* close element1 */ |
| 578 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 579 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 580 | assert_int_equal(0, xmlctx->ns.count); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 581 | |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 582 | assert_null(lyxml_ns_get(&xmlctx->ns, "nc", 2)); |
| 583 | assert_null(lyxml_ns_get(&xmlctx->ns, NULL, 0)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 584 | |
| 585 | lyxml_ctx_free(xmlctx); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 586 | ly_in_free(in, 0); |
Radek Krejci | 4b74d5e | 2018-09-26 14:30:55 +0200 | [diff] [blame] | 587 | } |
| 588 | |
David Sedlák | b3bed7a | 2019-03-08 11:53:37 +0100 | [diff] [blame] | 589 | static void |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 590 | test_ns2(void **state) |
| 591 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 592 | const char *str; |
| 593 | struct lyxml_ctx *xmlctx; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 594 | struct ly_in *in; |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 595 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 596 | /* opening element1 */ |
| 597 | str = "<element1/>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 598 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 599 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 600 | |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 601 | /* default namespace defined in parent element1 */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 602 | assert_int_equal(LY_SUCCESS, lyxml_ns_add(xmlctx, NULL, 0, strdup("urn:default"))); |
| 603 | assert_int_equal(1, xmlctx->ns.count); |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 604 | /* going into child element1 */ |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 605 | /* simulate adding open element1 into context */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 606 | xmlctx->elements.count++; |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 607 | /* no namespace defined, going out (first, simulate closing of so far open element) */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 608 | xmlctx->elements.count--; |
| 609 | lyxml_ns_rm(xmlctx); |
| 610 | assert_int_equal(1, xmlctx->ns.count); |
| 611 | |
| 612 | /* nothing else, going out of the parent element1 */ |
| 613 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 614 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 615 | assert_int_equal(0, xmlctx->ns.count); |
| 616 | |
| 617 | lyxml_ctx_free(xmlctx); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 618 | ly_in_free(in, 0); |
Radek Krejci | e0734d2 | 2019-04-05 15:54:28 +0200 | [diff] [blame] | 619 | } |
| 620 | |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 621 | static void |
| 622 | test_simple_xml(void **state) |
| 623 | { |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 624 | struct lyxml_ctx *xmlctx; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 625 | struct ly_in *in; |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 626 | const char *test_input = "<elem1 attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>"; |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 627 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 628 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(test_input, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 629 | assert_int_equal(LY_SUCCESS, lyxml_ctx_new(UTEST_LYCTX, in, &xmlctx)); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 630 | assert_int_equal(LYXML_ELEMENT, xmlctx->status); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 631 | assert_string_equal(xmlctx->in->current, "attr1=\"value\"> <elem2 attr2=\"value\" /> </elem1>"); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 632 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 633 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 634 | assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 635 | assert_string_equal(xmlctx->in->current, "=\"value\"> <elem2 attr2=\"value\" /> </elem1>"); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 636 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 637 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 638 | assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 639 | assert_string_equal(xmlctx->in->current, "> <elem2 attr2=\"value\" /> </elem1>"); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 640 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 641 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 642 | assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 643 | assert_string_equal(xmlctx->in->current, "<elem2 attr2=\"value\" /> </elem1>"); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 644 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 645 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 646 | assert_int_equal(LYXML_ELEMENT, xmlctx->status); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 647 | assert_string_equal(xmlctx->in->current, "attr2=\"value\" /> </elem1>"); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 648 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 649 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 650 | assert_int_equal(LYXML_ATTRIBUTE, xmlctx->status); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 651 | assert_string_equal(xmlctx->in->current, "=\"value\" /> </elem1>"); |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 652 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 653 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 654 | assert_int_equal(LYXML_ATTR_CONTENT, xmlctx->status); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 655 | assert_string_equal(xmlctx->in->current, " /> </elem1>"); |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 656 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 657 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 658 | assert_int_equal(LYXML_ELEM_CONTENT, xmlctx->status); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 659 | assert_string_equal(xmlctx->in->current, "/> </elem1>"); |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 660 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 661 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 662 | assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 663 | assert_string_equal(xmlctx->in->current, " </elem1>"); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 664 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 665 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 666 | assert_int_equal(LYXML_ELEM_CLOSE, xmlctx->status); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 667 | assert_string_equal(xmlctx->in->current, ""); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 668 | |
| 669 | assert_int_equal(LY_SUCCESS, lyxml_ctx_next(xmlctx)); |
| 670 | assert_int_equal(LYXML_END, xmlctx->status); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 671 | assert_string_equal(xmlctx->in->current, ""); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 672 | |
| 673 | lyxml_ctx_free(xmlctx); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 674 | ly_in_free(in, 0); |
Radek Krejci | fad79c9 | 2019-06-04 11:43:30 +0200 | [diff] [blame] | 675 | } |
| 676 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 677 | int |
| 678 | main(void) |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 679 | { |
| 680 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 681 | UTEST(test_element), |
| 682 | UTEST(test_attribute), |
| 683 | UTEST(test_text), |
| 684 | UTEST(test_ns), |
| 685 | UTEST(test_ns2), |
| 686 | UTEST(test_simple_xml), |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 687 | }; |
| 688 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 689 | return cmocka_run_group_tests(tests, NULL, NULL); |
Radek Krejci | d91dbaf | 2018-09-21 15:51:39 +0200 | [diff] [blame] | 690 | } |