Debian Codegraph @

tests/auto/core5/sax/qxmlsimplereader/tst_qxmlsimplereader.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QDomDocument>
5 #include <qthread.h>
6 #include <qtcpserver.h>
7 #include <qtcpsocket.h>
8 #include <QtTest/QtTest>
9 #include <QtCore/qatomic.h>
10 #include <QtCore/qsemaphore.h>
11 #include <qfile.h>
12 #include <qstring.h>
13 #include <qdir.h>
14 #include <qbuffer.h>
15 #include "parser/parser.h"
16
17 static const char *const inputString = "<!DOCTYPE inferno [<!ELEMENT inferno (circle+)><!ELEMENT circle (#PCDATA)>]><inferno><circle /><circle /></inferno>";
18 static const char *const refString = "setDocumentLocator(locator={columnNumber=1, lineNumber=1})\nstartDocument()\nstartDTD(name=\"inferno\", publicId=\"\", systemId=\"\")\nendDTD()\nstartElement(namespaceURI=\"\", localName=\"inferno\", qName=\"inferno\", atts=[])\nstartElement(namespaceURI=\"\", localName=\"circle\", qName=\"circle\", atts=[])\nendElement(namespaceURI=\"\", localName=\"circle\", qName=\"circle\")\nstartElement(namespaceURI=\"\", localName=\"circle\", qName=\"circle\", atts=[])\nendElement(namespaceURI=\"\", localName=\"circle\", qName=\"circle\")\nendElement(namespaceURI=\"\", localName=\"inferno\", qName=\"inferno\")\nendDocument()\n";
19
20 #define TEST_PORT 1088
21
22 class XmlServer : public QThread
23 {
24 Q_OBJECT
25 public:
26 XmlServer(QObject *parent = 0) : QThread(parent) {}
27
28 QSemaphore threadStarted;
29 bool listening = false;
30 QAtomicInt quitSoon;
31
32 protected:
33 void run() override;
34 };
35
36 #define CHUNK_SIZE 2048
37
38 void XmlServer::run()
39 {
40 QTcpServer srv;
41
42 listening = srv.listen(QHostAddress::Any, TEST_PORT);
43 threadStarted.release();
44
45 if (!listening) {
46 qWarning() << "Failed to listen on" << TEST_PORT << srv.errorString();
47 return;
48 }
49
50 for (;;) {
51 srv.waitForNewConnection(100);
52
53 if (QTcpSocket *sock = srv.nextPendingConnection()) {
54 QByteArray fileName;
55 for (;;) {
56 char c;
57 if (sock->getChar(&c)) {
58 if (c == '\n')
59 break;
60 fileName.append(c);
61 } else {
62 if (!sock->waitForReadyRead(-1))
63 break;
64 }
65 }
66
67 QFile file(QString::fromLocal8Bit(fileName));
68 if (!file.open(QIODevice::ReadOnly)) {
69 qWarning() << "XmlServer::run(): could not open" << fileName;
70 sock->abort();
71 delete sock;
72 continue;
73 }
74
75 QByteArray data = file.readAll();
76 for (int i = 0; i < data.size();) {
77 int cnt = qMin(CHUNK_SIZE, data.size() - i);
78 sock->write(data.constData() + i, cnt);
79 i += cnt;
80 sock->flush();
81 QTest::qSleep(1);
82
83 if (quitSoon.loadAcquire()) {
84 sock->abort();
85 break;
86 }
87 }
88
89 sock->disconnectFromHost();
90 delete sock;
91 }
92
93 if (quitSoon.loadAcquire())
94 break;
95 }
96
97 srv.close();
98 }
99
100 class tst_QXmlSimpleReader : public QObject
101 {
102 Q_OBJECT
103
104 public:
105 tst_QXmlSimpleReader();
106 ~tst_QXmlSimpleReader();
107
108 private slots:
109 void initTestCase();
110 void testGoodXmlFile();
111 void testGoodXmlFile_data();
112 void testBadXmlFile();
113 void testBadXmlFile_data();
114 void testIncrementalParsing();
115 void testIncrementalParsing_data();
116 void setDataQString();
117 void inputFromQIODevice();
118 void inputFromString();
119 void inputFromSocket_data();
120 void inputFromSocket();
121
122 void idsInParseException1();
123 void idsInParseException2();
124 void preserveCharacterReferences() const;
125 void reportNamespace() const;
126 void reportNamespace_data() const;
127 void roundtripWithNamespaces() const;
128 void dtdRecursionLimit();
129
130 private:
131 static QDomDocument fromByteArray(const QString &title, const QByteArray &ba, bool *ok);
132 XmlServer *server;
133 QString prefix;
134 };
135
136 tst_QXmlSimpleReader::tst_QXmlSimpleReader() : server(new XmlServer(this))
137 {
138 server->start();
139 }
140
141 tst_QXmlSimpleReader::~tst_QXmlSimpleReader()
142 {
143 server->quitSoon.storeRelease(1);
144 server->wait();
145 }
146
147 class MyErrorHandler : public QXmlErrorHandler
148 {
149 public:
150 QString publicId;
151 QString systemId;
152
153 bool error(const QXmlParseException &) override
154 {
155 return false;
156 }
157
158 QString errorString() const override
159 {
160 return QString();
161 }
162
163 bool fatalError(const QXmlParseException &exception) override
164 {
165 publicId = exception.publicId();
166 systemId = exception.systemId();
167 return true;
168 }
169
170 bool warning(const QXmlParseException &) override
171 {
172 return true;
173 }
174
175 };
176
177 void tst_QXmlSimpleReader::initTestCase()
178 {
179 prefix = QFileInfo(QFINDTESTDATA("xmldocs")).absolutePath();
180 if (prefix.isEmpty())
181 QFAIL("Cannot find xmldocs testdata!");
182 QDir::setCurrent(prefix);
183 }
184
185 void tst_QXmlSimpleReader::idsInParseException1()
186 {
187 MyErrorHandler handler;
188 QXmlSimpleReader reader;
189
190 reader.setErrorHandler(&handler);
191
192 /* A non-wellformed XML document with PUBLIC and SYSTEM. */
193 QByteArray input("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
194 "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
195 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">"
196 "<head>"
197 "<a/><a/><a/>"
198 "<head/>");
199
200 QBuffer buff(&input);
201 QXmlInputSource source(&buff);
202
203 /* Yes, parsing should be reported as a failure. */
204 QVERIFY(!reader.parse(source));
205
206 QCOMPARE(handler.publicId, QString::fromLatin1("-//W3C//DTD XHTML 1.0 Strict//EN"));
207 QCOMPARE(handler.systemId, QString::fromLatin1("http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"));
208 }
209
210 void tst_QXmlSimpleReader::idsInParseException2()
211 {
212 MyErrorHandler handler;
213 QXmlSimpleReader reader;
214
215 reader.setErrorHandler(&handler);
216
217 /* A non-wellformed XML document with only SYSTEM. */
218 QByteArray input("<!DOCTYPE html SYSTEM \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
219 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">"
220 "<head>"
221 "<a/><a/><a/>"
222 "<head/>");
223
224 QBuffer buff(&input);
225 QXmlInputSource source(&buff);
226
227 /* Yes, parsing should be reported as a failure. */
228 QVERIFY(!reader.parse(source));
229
230 QCOMPARE(handler.publicId, QString());
231 QCOMPARE(handler.systemId, QString::fromLatin1("http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"));
232 }
233
234 static QStringList findXmlFiles(QString dir_name)
235 {
236 QStringList result;
237
238 dir_name = QFINDTESTDATA(dir_name);
239 QDir dir(dir_name);
240 QFileInfoList file_list = dir.entryInfoList(QStringList("*.xml"), QDir::Files, QDir::Name);
241
242 QFileInfoList::const_iterator it = file_list.begin();
243 for (; it != file_list.end(); ++it) {
244 const QFileInfo &file_info = *it;
245 result.append(file_info.filePath());
246 }
247
248 return result;
249 }
250
251 void tst_QXmlSimpleReader::testGoodXmlFile_data()
252 {
253 const char * const good_data_dirs[] = {
254 "xmldocs/valid/sa",
255 "xmldocs/valid/not-sa",
256 "xmldocs/valid/ext-sa",
257 0
258 };
259 const char * const *d = good_data_dirs;
260
261 QStringList good_file_list;
262 for (; *d != 0; ++d)
263 good_file_list += findXmlFiles(*d);
264
265 QTest::addColumn<QString>("file_name");
266 QStringList::const_iterator it = good_file_list.begin();
267 for (; it != good_file_list.end(); ++it)
268 QTest::newRow((*it).toLatin1()) << *it;
269 }
270
271 void tst_QXmlSimpleReader::testGoodXmlFile()
272 {
273 QFETCH(QString, file_name);
274 QFile file(file_name);
275 QVERIFY(file.open(QIODevice::ReadOnly));
276 QString content = file.readAll();
277 file.close();
278 QVERIFY(file.open(QIODevice::ReadOnly));
279 Parser parser;
280
281 QVERIFY(parser.parseFile(&file));
282
283 QFile ref_file(file_name + ".ref");
284 QVERIFY(ref_file.open(QIODevice::ReadOnly | QIODevice::Text));
285 QByteArray data = ref_file.readAll();
286 QString ref_file_contents = QString::fromUtf8(data.constData(), data.size());
287
288 QCOMPARE(parser.result(), ref_file_contents);
289 }
290
291 void tst_QXmlSimpleReader::testBadXmlFile_data()
292 {
293 const char * const bad_data_dirs[] = {
294 "xmldocs/not-wf/sa",
295 0
296 };
297 const char * const *d = bad_data_dirs;
298
299 QStringList bad_file_list;
300 for (; *d != 0; ++d)
301 bad_file_list += findXmlFiles(*d);
302
303 QTest::addColumn<QString>("file_name");
304 QStringList::const_iterator it = bad_file_list.begin();
305 for (; it != bad_file_list.end(); ++it)
306 QTest::newRow((*it).toLatin1()) << *it;
307 }
308
309 void tst_QXmlSimpleReader::testBadXmlFile()
310 {
311 QFETCH(QString, file_name);
312 QFile file(file_name);
313 QVERIFY(file.open(QIODevice::ReadOnly));
314 Parser parser;
315
316 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/030.xml").toLocal8Bit().constData(), "a form feed character is not accepted in XML", Continue);
317 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/031.xml").toLocal8Bit().constData(), "a form feed character is not accepted in a processing instruction", Continue);
318 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/032.xml").toLocal8Bit().constData(), "a form feed character is not accepted in a comment", Continue);
319 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/033.xml").toLocal8Bit().constData(), "overlong sequence - small latin letter d should be rejected", Continue);
320 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/038.xml").toLocal8Bit().constData(), "attribute x redefined; should be rejected", Continue);
321 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/072.xml").toLocal8Bit().constData(), "entity foo not defined", Continue);
322 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/073.xml").toLocal8Bit().constData(), "entity f not defined", Continue);
323 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/074.xml").toLocal8Bit().constData(), "entity e is not well-formed (</foo><foo>)", Continue);
324 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/076.xml").toLocal8Bit().constData(), "entity foo is not defined", Continue);
325 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/077.xml").toLocal8Bit().constData(), "entity bar is not defined within the definition of entity foo", Continue);
326 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/078.xml").toLocal8Bit().constData(), "entity foo not defined", Continue);
327 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/085.xml").toLocal8Bit().constData(), "Unfinished Public or System Id", Continue);
328 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/086.xml").toLocal8Bit().constData(), "Unfinished Public or System Id", Continue);
329 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/087.xml").toLocal8Bit().constData(), "Unfinished Public or System Id", Continue);
330 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/101.xml").toLocal8Bit().constData(), "Invalid XML encoding name (space before utf-8)", Continue);
331 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/102.xml").toLocal8Bit().constData(), "Invalid version specification (1.0 followed by space)", Continue);
332 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/104.xml").toLocal8Bit().constData(), "Premature end of data in tag foo", Continue);
333 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/116.xml").toLocal8Bit().constData(), "Invalid decimal value", Continue);
334 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/117.xml").toLocal8Bit().constData(), "No name", Continue);
335 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/119.xml").toLocal8Bit().constData(), "No name", Continue);
336 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/122.xml").toLocal8Bit().constData(), "; expected in declaration of element", Continue);
337 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/132.xml").toLocal8Bit().constData(), "; expected in declaration of element", Continue);
338 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/142.xml").toLocal8Bit().constData(), "Invalid value '0'", Continue);
339 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/143.xml").toLocal8Bit().constData(), "Invalid value '31'", Continue);
340
341 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/144.xml").toLocal8Bit().constData(), "noncharacter code 0xFFFF should be rejected", Continue);
342 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/145.xml").toLocal8Bit().constData(), "surrogate code point 0xD800 should be rejected", Continue);
343 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/146.xml").toLocal8Bit().constData(), "code point out-of-range 0x110000 (must be < 0x10FFFE)", Abort);
344 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/160.xml").toLocal8Bit().constData(), "Parameter references forbidden in internal subset", Continue);
345 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/162.xml").toLocal8Bit().constData(), "Parameter references forbidden in internal subset", Continue);
346
347 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/168.xml").toLocal8Bit().constData(), "Surrogate code point 0xEDA080 should be rejected", Continue);
348 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/169.xml").toLocal8Bit().constData(), "Surrogate code point 0xEDB080 should be rejected", Continue);
349 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/170.xml").toLocal8Bit().constData(), "Code point 0xF7808080 should be rejected", Continue);
350
351 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/180.xml").toLocal8Bit().constData(), "Entity e is not defined", Continue);
352 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/181.xml").toLocal8Bit().constData(), "Unregistered error message", Continue);
353 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/182.xml").toLocal8Bit().constData(), "Comment not terminated", Continue);
354 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/185.xml").toLocal8Bit().constData(), "Entity e not defined", Continue);
355 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/186.xml").toLocal8Bit().constData(), "Attributes constructs error", Continue);
356
357 QVERIFY(!parser.parseFile(&file));
358
359 QFile ref_file(file_name + ".ref");
360 QVERIFY(ref_file.open(QIODevice::ReadOnly | QIODevice::Text));
361 QString ref_file_contents = QString::fromUtf8(ref_file.readAll());
362
363 QEXPECT_FAIL(QFINDTESTDATA("xmldocs/not-wf/sa/145.xml").toLocal8Bit().constData(), "Surrogate code point 0xD800 should be rejected", Continue);
364
365 QCOMPARE(parser.result(), ref_file_contents);
366 }
367
368 void tst_QXmlSimpleReader::testIncrementalParsing_data()
369 {
370 QTest::addColumn<QString>("file_name");
371 QTest::addColumn<int>("chunkSize");
372
373 const char * const good_data_dirs[] = {
374 "xmldocs/valid/sa",
375 "xmldocs/valid/not-sa",
376 "xmldocs/valid/ext-sa",
377 0
378 };
379 const char * const *d = good_data_dirs;
380
381 QStringList good_file_list;
382 for (; *d != 0; ++d)
383 good_file_list += findXmlFiles(*d);
384
385 for (int i=1; i<10; ++i) {
386 QStringList::const_iterator it = good_file_list.begin();
387 const QString skip49 = QFINDTESTDATA("xmldocs/valid/sa/049.xml");
388 const QString skip50 = QFINDTESTDATA("xmldocs/valid/sa/050.xml");
389 const QString skip51 = QFINDTESTDATA("xmldocs/valid/sa/051.xml");
390 const QString skip52 = QFINDTESTDATA("xmldocs/valid/sa/052.xml");
391 const QString skip89 = QFINDTESTDATA("xmldocs/valid/sa/089.xml");
392
393 for (; it != good_file_list.end(); ++it) {
394 if ( *it == skip89 )
395 continue;// TODO: fails at the moment -- don't bother
396 if ( i==1 && (
397 *it == skip49 ||
398 *it == skip50 ||
399 *it == skip51 ||
400 *it == skip52 ) ) {
401 continue; // TODO: fails at the moment -- don't bother
402 }
403 QTest::newRow(QString("%1 %2").arg(*it).arg(i).toLatin1()) << *it << i;
404 }
405 }
406 }
407
408 void tst_QXmlSimpleReader::testIncrementalParsing()
409 {
410 QFETCH(QString, file_name);
411 QFETCH(int, chunkSize);
412
413 QFile file(file_name);
414 QVERIFY(file.open(QIODevice::ReadOnly));
415
416 Parser parser;
417 QXmlInputSource source;
418 bool first = true;
419 while (!file.atEnd()) {
420 source.setData(file.read(chunkSize));
421 if (first) {
422 QVERIFY(parser.parse(&source, true));
423 first = false;
424 } else {
425 QVERIFY(parser.parseContinue());
426 }
427 }
428 // detect end of document
429 QVERIFY(parser.parseContinue());
430 // parsing should fail after the end of the document was reached
431 QVERIFY(!parser.parseContinue());
432
433 QFile ref_file(file_name + ".ref");
434 QVERIFY(ref_file.open(QIODevice::ReadOnly | QIODevice::Text));
435 QString ref_file_contents = QString::fromUtf8(ref_file.readAll());
436
437 QCOMPARE(parser.result(), ref_file_contents);
438 }
439
440 void tst_QXmlSimpleReader::setDataQString()
441 {
442 QString input = inputString;
443 QString ref = refString;
444
445 QXmlInputSource source;
446 Parser parser;
447
448 source.setData(input);
449 QVERIFY(parser.parse(&source,false));
450
451 QBuffer resultBuffer;
452 resultBuffer.setData(parser.result().toLatin1());
453
454 QBuffer refBuffer;
455 refBuffer.setData(ref.toLatin1());
456
457 resultBuffer.open(QIODevice::ReadOnly);
458 refBuffer.open(QIODevice::ReadOnly);
459
460 bool success = true;
461 while (resultBuffer.canReadLine()) {
462 if (!refBuffer.canReadLine()) {
463 success = false; break ;
464 }
465 if (resultBuffer.readLine().simplified() != refBuffer.readLine().simplified()) {
466 success = false; break ;
467 }
468 }
469 QVERIFY(success);
470 }
471
472 void tst_QXmlSimpleReader::inputFromQIODevice()
473 {
474 QBuffer inputBuffer;
475 inputBuffer.setData(inputString);
476
477 QXmlInputSource source(&inputBuffer);
478 Parser parser;
479
480 QVERIFY(parser.parse(&source,false));
481
482 QBuffer resultBuffer;
483 resultBuffer.setData(parser.result().toLatin1());
484
485 QBuffer refBuffer;
486 refBuffer.setData(refString);
487
488 resultBuffer.open(QIODevice::ReadOnly);
489 refBuffer.open(QIODevice::ReadOnly);
490
491 bool success = true;
492 while (resultBuffer.canReadLine()) {
493 if (!refBuffer.canReadLine()) {
494 success = false; break ;
495 }
496 if (resultBuffer.readLine().simplified() != refBuffer.readLine().simplified()) {
497 success = false; break ;
498 }
499 }
500 QVERIFY(success);
501 }
502
503 void tst_QXmlSimpleReader::inputFromString()
504 {
505 QString str = "<foo><bar>kake</bar><bar>ja</bar></foo>";
506 QBuffer buff;
507 buff.setData((char*)str.utf16(), int(str.size()*sizeof(ushort)));
508
509 QXmlInputSource input(&buff);
510
511 QXmlSimpleReader reader;
512 QXmlDefaultHandler handler;
513 reader.setContentHandler(&handler);
514
515 QVERIFY(reader.parse(&input));
516 }
517
518 void tst_QXmlSimpleReader::inputFromSocket_data()
519 {
520 const QStringList files = findXmlFiles(QLatin1String("encodings"));
521 QVERIFY(files.size() > 0);
522
523 QTest::addColumn<QString>("file_name");
524
525 for (const QString &file_name : files)
526 QTest::newRow(file_name.toLatin1()) << file_name;
527 }
528
529 void tst_QXmlSimpleReader::inputFromSocket()
530 {
531 QFETCH(QString, file_name);
532
533 if (!server->threadStarted.tryAcquire(1, 15000)) {
534 // If something is wrong with QThreads, it's not a reason to fail
535 // XML-test, we are not testing QThread here after all!
536 QSKIP("XmlServer/thread has not started yet");
537 }
538
539 // Subsequent runs should be able to acquire the semaphore.
540 server->threadStarted.release(1);
541
542 if (!server->listening) {
543 // Again, QTcpServer is not the subject of this test!
544 QSKIP("QTcpServer::listen failed, bailing out");
545 }
546
547 QTcpSocket sock;
548 sock.connectToHost(QHostAddress::LocalHost, TEST_PORT);
549 QVERIFY2(sock.waitForConnected(),
550 qPrintable(QStringLiteral("Cannot connect on port ") + QString::number(TEST_PORT)
551 + QStringLiteral(": ") + sock.errorString()));
552
553 sock.write(file_name.toLocal8Bit() + "\n");
554 QVERIFY(sock.waitForBytesWritten());
555
556 QXmlInputSource input(&sock);
557
558 QXmlSimpleReader reader;
559 QXmlDefaultHandler handler;
560 reader.setContentHandler(&handler);
561
562 QVERIFY(reader.parse(&input));
563
564 // qDebug() << "tst_QXmlSimpleReader::inputFromSocket(): success" << file_name;
565 }
566
567 void tst_QXmlSimpleReader::preserveCharacterReferences() const
568 {
569 class Handler : public QXmlDefaultHandler
570 {
571 public:
572 bool characters(const QString &chars) override
573 {
574 received = chars;
575 return true;
576 }
577
578 QString received;
579 };
580
581 {
582 QByteArray input("<e>A&#160;&#160;&#160;&#160;A</e>");
583
584 QBuffer buff(&input);
585 QXmlInputSource source(&buff);
586
587 Handler h;
588 QXmlSimpleReader reader;
589 reader.setContentHandler(&h);
590 QVERIFY(reader.parse(&source, false));
591
592 QCOMPARE(h.received, QLatin1Char('A') + QString(4, QChar(160)) + QLatin1Char('A'));
593 }
594
595 {
596 QByteArray input("<e>&#160;&#160;&#160;&#160;</e>");
597
598 QBuffer buff(&input);
599 QXmlInputSource source(&buff);
600
601 Handler h;
602 QXmlSimpleReader reader;
603 reader.setContentHandler(&h);
604 QVERIFY(reader.parse(&source, false));
605
606 QCOMPARE(h.received, QString(4, QChar(160)));
607 }
608 }
609
610 void tst_QXmlSimpleReader::reportNamespace() const
611 {
612 class Handler : public QXmlDefaultHandler
613 {
614 public:
615 bool startElement(const QString &namespaceURI,
616 const QString &localName,
617 const QString &qName,
618 const QXmlAttributes &) override
619 {
620 startNamespaceURI = namespaceURI;
621 startLocalName = localName;
622 startQName = qName;
623
624 return true;
625 }
626
627 bool endElement(const QString &namespaceURI,
628 const QString &localName,
629 const QString &qName) override
630 {
631 endNamespaceURI = namespaceURI;
632 endLocalName = localName;
633 endQName = qName;
634
635 return true;
636 }
637
638 QString startLocalName;
639 QString startQName;
640 QString startNamespaceURI;
641 QString endLocalName;
642 QString endQName;
643 QString endNamespaceURI;
644 };
645
646 QXmlSimpleReader reader;
647 Handler handler;
648 reader.setContentHandler(&handler);
649
650 QFETCH(QByteArray, input);
651
652 QBuffer buffer(&input);
653 QVERIFY(buffer.open(QIODevice::ReadOnly));
654
655 QXmlInputSource source(&buffer);
656 QVERIFY(reader.parse(source));
657
658 QFETCH(QString, expectedQName);
659 QFETCH(QString, expectedLocalName);
660 QFETCH(QString, expectedNamespace);
661
662 QCOMPARE(handler.startNamespaceURI, expectedNamespace);
663 QCOMPARE(handler.startLocalName, expectedLocalName);
664 QCOMPARE(handler.startQName, expectedQName);
665
666 QCOMPARE(handler.endNamespaceURI, expectedNamespace);
667 QCOMPARE(handler.endLocalName, expectedLocalName);
668 QCOMPARE(handler.endQName, expectedQName);
669 }
670
671 void tst_QXmlSimpleReader::reportNamespace_data() const
672 {
673 QTest::addColumn<QByteArray>("input");
674 QTest::addColumn<QString>("expectedQName");
675 QTest::addColumn<QString>("expectedLocalName");
676 QTest::addColumn<QString>("expectedNamespace");
677
678 QTest::newRow("default ns") << QByteArray("<element xmlns='http://example.com/'/>")
679 << QString("element")
680 << QString("element")
681 << QString("http://example.com/");
682
683 QTest::newRow("with prefix") << QByteArray("<p:element xmlns:p='http://example.com/'/>")
684 << QString("p:element")
685 << QString("element")
686 << QString("http://example.com/");
687 }
688
689 QDomDocument tst_QXmlSimpleReader::fromByteArray(const QString &title, const QByteArray &ba, bool *ok)
690 {
691 QDomDocument doc(title);
692 *ok = bool(doc.setContent(ba, QDomDocument::ParseOption::UseNamespaceProcessing));
693 return doc;
694 }
695
696 void tst_QXmlSimpleReader::roundtripWithNamespaces() const
697 {
698 const char *const expected = "<element b:attr=\"value\" xmlns:a=\"http://www.example.com/A\" xmlns:b=\"http://www.example.com/B\" />\n";
699 bool ok;
700
701 {
702 const char *const xml = "<element xmlns:b=\"http://www.example.com/B\" b:attr=\"value\" xmlns:a=\"http://www.example.com/A\"/>";
703
704 const QDomDocument one(fromByteArray("document", xml, &ok));
705 QVERIFY(ok);
706 const QDomDocument two(fromByteArray("document2", one.toByteArray(2), &ok));
707 QVERIFY(ok);
708
709 QEXPECT_FAIL("", "Known problem, see 154573. The fix happens to break uic.", Abort);
710
711 QCOMPARE(expected, one.toByteArray().constData());
712 QCOMPARE(one.toByteArray(2).constData(), two.toByteArray(2).constData());
713 QCOMPARE(two.toByteArray(2).constData(), two.toByteArray(2).constData());
714 }
715
716 {
717 const char *const xml = "<element b:attr=\"value\" xmlns:b=\"http://www.example.com/B\" xmlns:a=\"http://www.example.com/A\"/>";
718
719 const QDomDocument one(fromByteArray("document", xml, &ok));
720 QVERIFY(ok);
721 const QDomDocument two(fromByteArray("document2", one.toByteArray(2), &ok));
722 QVERIFY(ok);
723
724 QCOMPARE(expected, one.toByteArray().constData());
725 QCOMPARE(one.toByteArray(2).constData(), two.toByteArray(2).constData());
726 QCOMPARE(two.toByteArray(2).constData(), two.toByteArray(2).constData());
727 }
728 }
729
730 class TestHandler : public QXmlDefaultHandler
731 {
732 public:
733 TestHandler() :
734 recursionCount(0)
735 {
736 }
737
738 bool internalEntityDecl(const QString &name, const QString &value) override
739 {
740 ++recursionCount;
741 return QXmlDefaultHandler::internalEntityDecl(name, value);
742 }
743
744 int recursionCount;
745 };
746
747 void tst_QXmlSimpleReader::dtdRecursionLimit()
748 {
749 QFile file(QFINDTESTDATA("xmldocs/2-levels-nested-dtd.xml"));
750 QVERIFY(file.open(QIODevice::ReadOnly));
751 QXmlSimpleReader xmlReader;
752 {
753 QXmlInputSource source(&file);
754 TestHandler handler;
755 xmlReader.setDeclHandler(&handler);
756 xmlReader.setErrorHandler(&handler);
757 QVERIFY(!xmlReader.parse(&source));
758 }
759
760 file.close();
761 file.setFileName(QFINDTESTDATA("xmldocs/1-levels-nested-dtd.xml"));
762 QVERIFY(file.open(QIODevice::ReadOnly));
763 {
764 QXmlInputSource source(&file);
765 TestHandler handler;
766 xmlReader.setDeclHandler(&handler);
767 xmlReader.setErrorHandler(&handler);
768 QVERIFY(!xmlReader.parse(&source));
769 // The error wasn't because of the recursion limit being reached,
770 // it was because the document is not valid.
771 QVERIFY(handler.recursionCount < 2);
772 }
773
774 file.close();
775 file.setFileName(QFINDTESTDATA("xmldocs/internal-entity-polynomial-attribute.xml"));
776 QVERIFY(file.open(QIODevice::ReadOnly));
777 {
778 QXmlInputSource source(&file);
779 TestHandler handler;
780 xmlReader.setDeclHandler(&handler);
781 xmlReader.setErrorHandler(&handler);
782 QVERIFY(!xmlReader.parse(&source));
783 QCOMPARE(handler.recursionCount, 2);
784 }
785 }
786
787 QTEST_MAIN(tst_QXmlSimpleReader)
788 #include "tst_qxmlsimplereader.moc"