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