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