| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 29 окт. 2019 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/test-fw/utest.h> |
| 23 |
#include <lsp-plug.in/test-fw/helpers.h> |
| 24 |
#include <lsp-plug.in/fmt/xml/PushParser.h> |
| 25 |
#include <lsp-plug.in/stdlib/math.h> |
| 26 |
#include <stdarg.h> |
| 27 |
|
| 28 |
using namespace lsp; |
| 29 |
using namespace lsp::xml; |
| 30 |
|
| 31 |
UTEST_BEGIN("runtime.fmt.xml", pushparser) |
| 32 |
|
| 33 |
class Validator: public IXMLHandler |
| 34 |
{ |
| 35 |
private: |
| 36 |
const char **pTok; |
| 37 |
test_type_t *pTest; |
| 38 |
|
| 39 |
public: |
| 40 |
explicit Validator(const char **instr, test_type_t *pt): pTok(instr), pTest(pt) { } |
| 41 |
|
| 42 |
inline bool no_more_tokens() const { return *pTok == NULL; } |
| 43 |
|
| 44 |
private: |
| 45 |
status_t validate(const char *prefix...) |
| 46 |
{ |
| 47 |
LSPString tmp, ck; |
| 48 |
va_list v; |
| 49 |
|
| 50 |
const char *p = *(pTok++); |
| 51 |
pTest->printf(" Validating: %s\n", p); |
| 52 |
|
| 53 |
va_start(v, prefix); |
| 54 |
if (!tmp.append_utf8(prefix)) |
| 55 |
return STATUS_NO_MEM; |
| 56 |
|
| 57 |
while (true) |
| 58 |
{ |
| 59 |
const LSPString *s = va_arg(v, const LSPString *); |
| 60 |
if (s == NULL) |
| 61 |
break; |
| 62 |
if (!tmp.append(',')) |
| 63 |
return STATUS_NO_MEM; |
| 64 |
if (!tmp.append(s)) |
| 65 |
return STATUS_NO_MEM; |
| 66 |
} |
| 67 |
va_end(v); |
| 68 |
|
| 69 |
if ((p != NULL) && (!ck.set_utf8(p))) |
| 70 |
return STATUS_NO_MEM; |
| 71 |
|
| 72 |
if (tmp.equals(&ck)) |
| 73 |
return STATUS_OK; |
| 74 |
|
| 75 |
pTest->eprintf("Expected: %s, actually get: %s\n", ck.get_utf8(), tmp.get_utf8()); |
| 76 |
return STATUS_CORRUPTED; |
| 77 |
} |
| 78 |
|
| 79 |
public: |
| 80 |
virtual status_t start_document(xml_version_t xversion, const LSPString *version, const LSPString *encoding, bool standalone) |
| 81 |
{ |
| 82 |
if (xversion != XML_VERSION_1_1) |
| 83 |
{ |
| 84 |
pTest->eprintf("Bat XML version: %d", int(xversion)); |
| 85 |
return STATUS_CORRUPTED; |
| 86 |
} |
| 87 |
if (!standalone) |
| 88 |
{ |
| 89 |
pTest->eprintf("Bat standalone param: %s", (standalone) ? "true" : "false"); |
| 90 |
return STATUS_CORRUPTED; |
| 91 |
} |
| 92 |
return validate("START", version, encoding, NULL); |
| 93 |
} |
| 94 |
|
| 95 |
virtual status_t end_document() |
| 96 |
{ |
| 97 |
return validate("END", NULL); |
| 98 |
} |
| 99 |
|
| 100 |
virtual status_t cdata(const LSPString *cdata) |
| 101 |
{ |
| 102 |
return validate("CDATA", cdata, NULL); |
| 103 |
} |
| 104 |
|
| 105 |
virtual status_t characters(const LSPString *text) |
| 106 |
{ |
| 107 |
return validate("CHAR", text, NULL); |
| 108 |
} |
| 109 |
|
| 110 |
virtual status_t comment(const LSPString *text) |
| 111 |
{ |
| 112 |
return validate("COMM", text, NULL); |
| 113 |
} |
| 114 |
|
| 115 |
virtual status_t resolve(LSPString *value, const LSPString *name) |
| 116 |
{ |
| 117 |
if (!value->set(name)) |
| 118 |
return STATUS_NO_MEM; |
| 119 |
value->toupper(); |
| 120 |
return STATUS_OK; |
| 121 |
} |
| 122 |
|
| 123 |
virtual status_t start_element(const LSPString *name, const LSPString * const *atts) |
| 124 |
{ |
| 125 |
LSPString tmp; |
| 126 |
if (!tmp.append(name)) |
| 127 |
return STATUS_NO_MEM; |
| 128 |
|
| 129 |
while (*atts != NULL) |
| 130 |
{ |
| 131 |
if (!tmp.append(',')) |
| 132 |
return STATUS_NO_MEM; |
| 133 |
if (!tmp.append(*(atts++))) |
| 134 |
return STATUS_NO_MEM; |
| 135 |
if (!tmp.append(',')) |
| 136 |
return STATUS_NO_MEM; |
| 137 |
if (!tmp.append(*(atts++))) |
| 138 |
return STATUS_NO_MEM; |
| 139 |
} |
| 140 |
return validate("OPEN", &tmp, NULL); |
| 141 |
} |
| 142 |
|
| 143 |
virtual status_t end_element(const LSPString *name) |
| 144 |
{ |
| 145 |
return validate("CLOSE", name, NULL); |
| 146 |
} |
| 147 |
|
| 148 |
virtual status_t processing(const LSPString *name, const LSPString *args) |
| 149 |
{ |
| 150 |
return validate("PI", name, args, NULL); |
| 151 |
} |
| 152 |
|
| 153 |
virtual status_t doctype(const LSPString *name, const LSPString *pub, const LSPString *sys) |
| 154 |
{ |
| 155 |
return validate("DTD", name, pub, sys, NULL); |
| 156 |
} |
| 157 |
}; |
| 158 |
|
| 159 |
void test_xml_parsing() |
| 160 |
{ |
| 161 |
const char *xml = |
| 162 |
"<?xml version='1.1' encoding='US-ASCII' standalone='yes'?>\n" |
| 163 |
"<!DOCTYPE TEST PUBLIC 'lsp-plugin' 'http://lsp-plug.in/test.dtd'>\n" |
| 164 |
"<!--c1-->\n" |
| 165 |
"<?pi v1?>\n" |
| 166 |
"<!--c2-->\n" |
| 167 |
"<root:root xmlns:test=\"http://some test namespace\">\n" |
| 168 |
"<?pi v2?>\n" |
| 169 |
"<!--c3-->\n" |
| 170 |
"<?pi v3?>\n" |
| 171 |
"<tag1 p1='10' p2='20'>" |
| 172 |
"<?pi v4?>\n" |
| 173 |
"<!--c4-->\n" |
| 174 |
"some text characters\n" |
| 175 |
"<?pi v5?>\n" |
| 176 |
"</tag1>\n" |
| 177 |
"&reference1;\n" |
| 178 |
"<test:tag2 attr='&"'<>' attr2=\"Привет\" />\n" |
| 179 |
"<tag3 param='A-Z' ref=\"&reference2;-&reference3;\">\n" |
| 180 |
"<!--cdata-->\n" |
| 181 |
"<![CDATA[175bf801fddd9ea5ded1da4e26a75ad3]]>\n" |
| 182 |
"<?xi v5?>\n" |
| 183 |
"</tag3>\n" |
| 184 |
"</root:root>\n\n" |
| 185 |
"<?pi v6?>\n" |
| 186 |
"<!-- end -->\n" |
| 187 |
; |
| 188 |
|
| 189 |
const char *events[] = |
| 190 |
{ |
| 191 |
"START,1.1,US-ASCII", |
| 192 |
"DTD,TEST,lsp-plugin,http://lsp-plug.in/test.dtd", |
| 193 |
"COMM,c1", |
| 194 |
"PI,pi,v1", |
| 195 |
"COMM,c2", |
| 196 |
"OPEN,root:root,xmlns:test,http://some test namespace", |
| 197 |
"CHAR,\n", |
| 198 |
"PI,pi,v2", |
| 199 |
"CHAR,\n", |
| 200 |
"COMM,c3", |
| 201 |
"CHAR,\n", |
| 202 |
"PI,pi,v3", |
| 203 |
"CHAR,\n", |
| 204 |
"OPEN,tag1,p1,10,p2,20", |
| 205 |
"PI,pi,v4", |
| 206 |
"CHAR,\n", |
| 207 |
"COMM,c4", |
| 208 |
"CHAR,\nsome text characters\n", |
| 209 |
"PI,pi,v5", |
| 210 |
"CHAR,\n", |
| 211 |
"CLOSE,tag1", |
| 212 |
"CHAR,\nREFERENCE1\n", |
| 213 |
"OPEN,test:tag2,attr,&\"\'<>,attr2,Привет", |
| 214 |
"CLOSE,test:tag2", |
| 215 |
"CHAR,\n", |
| 216 |
"OPEN,tag3,param,A-Z,ref,REFERENCE2-REFERENCE3", |
| 217 |
"CHAR,\n", |
| 218 |
"COMM,cdata", |
| 219 |
"CHAR,\n", |
| 220 |
"CDATA,175bf801fddd9ea5ded1da4e26a75ad3", |
| 221 |
"CHAR,\n", |
| 222 |
"PI,xi,v5", |
| 223 |
"CHAR,\n", |
| 224 |
"CLOSE,tag3", |
| 225 |
"CHAR,\n", |
| 226 |
"CLOSE,root:root", |
| 227 |
"PI,pi,v6", |
| 228 |
"COMM, end ", |
| 229 |
"END", |
| 230 |
NULL |
| 231 |
}; |
| 232 |
|
| 233 |
LSPString text; |
| 234 |
printf("Reading document: %s\n", xml); |
| 235 |
UTEST_ASSERT(text.set_utf8(xml)); |
| 236 |
|
| 237 |
PushParser p; |
| 238 |
Validator v(events, this); |
| 239 |
UTEST_ASSERT(p.parse_data(&v, &text) == STATUS_OK); |
| 240 |
UTEST_ASSERT(v.no_more_tokens()); |
| 241 |
} |
| 242 |
|
| 243 |
UTEST_MAIN |
| 244 |
{ |
| 245 |
printf("Testing XML document parsing...\n"); |
| 246 |
test_xml_parsing(); |
| 247 |
} |
| 248 |
|
| 249 |
UTEST_END |
| 250 |
|