diff options
Diffstat (limited to 'tests/auto/corelib/serialization')
3 files changed, 173 insertions, 66 deletions
diff --git a/tests/auto/corelib/serialization/json/tst_qtjson.cpp b/tests/auto/corelib/serialization/json/tst_qtjson.cpp index 57aa67c142..45f815f810 100644 --- a/tests/auto/corelib/serialization/json/tst_qtjson.cpp +++ b/tests/auto/corelib/serialization/json/tst_qtjson.cpp @@ -527,6 +527,8 @@ void tst_QtJson::testObjectSmallKeys() QVERIFY(data1.contains(QStringLiteral("123"))); QCOMPARE(data1.value(QStringLiteral("123")).type(), QJsonValue::Double); QCOMPARE(data1.value(QStringLiteral("123")).toDouble(), (double)323); + QCOMPARE(data1.constEnd() - data1.constBegin(), 3); + QCOMPARE(data1.end() - data1.begin(), 3); } void tst_QtJson::testArraySimple() diff --git a/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp b/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp index 8197c386c5..7ada913778 100644 --- a/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp +++ b/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp @@ -28,12 +28,13 @@ #include <QtTest/QtTest> #include <QtGui/QBitmap> +#include <QtGui/QPainter> +#include <QtGui/QPainterPath> #include <QtGui/QPalette> -#include <QtGui/QPixmap> +#include <QtGui/QPen> #include <QtGui/QPicture> +#include <QtGui/QPixmap> #include <QtGui/QTextLength> -#include <QtGui/QPainter> -#include <QtGui/QPen> class tst_QDataStream : public QObject { @@ -1170,18 +1171,18 @@ void tst_QDataStream::readQCursor(QDataStream *s) QVERIFY(d5.shape() == test.shape()); //## lacks operator== QCOMPARE(d5.hotSpot(), test.hotSpot()); - QVERIFY((d5.bitmap() != 0 && test.bitmap() != 0) || (d5.bitmap() == 0 && test.bitmap() == 0)); - if (d5.bitmap() != 0) { - QPixmap actual = *(d5.bitmap()); - QPixmap expected = *(test.bitmap()); - QCOMPARE(actual, expected); - } - QVERIFY((d5.mask() != 0 && test.mask() != 0) || (d5.mask() == 0 && test.mask() == 0)); - if (d5.mask() != 0) { - QPixmap actual = *(d5.mask()); - QPixmap expected = *(test.mask()); - QCOMPARE(actual, expected); - } + + // Comparing non-null QBitmaps will fail. Upcast them first to pass. + QCOMPARE(d5.bitmap(Qt::ReturnByValue).isNull(), test.bitmap(Qt::ReturnByValue).isNull()); + QCOMPARE( + static_cast<QPixmap>(d5.bitmap(Qt::ReturnByValue)), + static_cast<QPixmap>(test.bitmap(Qt::ReturnByValue)) + ); + QCOMPARE(d5.mask(Qt::ReturnByValue).isNull(), test.mask(Qt::ReturnByValue).isNull()); + QCOMPARE( + static_cast<QPixmap>(d5.mask(Qt::ReturnByValue)), + static_cast<QPixmap>(test.mask(Qt::ReturnByValue)) + ); } #endif @@ -3064,39 +3065,45 @@ void tst_QDataStream::status_QHash_QMap() } \ } \ { \ - LinkedList expectedLinkedList; \ + Vector expectedVector; \ for (int i = 0; i < expectedList.count(); ++i) \ - expectedLinkedList << expectedList.at(i); \ + expectedVector << expectedList.at(i); \ QByteArray ba = byteArray; \ QDataStream stream(&ba, QIODevice::ReadOnly); \ if (inTransaction) \ stream.startTransaction(); \ stream.setStatus(initialStatus); \ - stream >> linkedList; \ + stream >> vector; \ QCOMPARE((int)stream.status(), (int)expectedStatus); \ if (!inTransaction || stream.commitTransaction()) { \ - QCOMPARE(linkedList.size(), expectedLinkedList.size()); \ - QCOMPARE(linkedList, expectedLinkedList); \ + QCOMPARE(vector.size(), expectedVector.size()); \ + QCOMPARE(vector, expectedVector); \ } else { \ - QVERIFY(linkedList.isEmpty()); \ + QVERIFY(vector.isEmpty()); \ } \ } \ + if (inTransaction) \ + break; \ + } + +#define LINKED_LIST_TEST(byteArray, initialStatus, expectedStatus, expectedList) \ + for (bool inTransaction = false;; inTransaction = true) { \ { \ - Vector expectedVector; \ + LinkedList expectedLinkedList; \ for (int i = 0; i < expectedList.count(); ++i) \ - expectedVector << expectedList.at(i); \ + expectedLinkedList << expectedList.at(i); \ QByteArray ba = byteArray; \ QDataStream stream(&ba, QIODevice::ReadOnly); \ if (inTransaction) \ stream.startTransaction(); \ stream.setStatus(initialStatus); \ - stream >> vector; \ + stream >> linkedList; \ QCOMPARE((int)stream.status(), (int)expectedStatus); \ if (!inTransaction || stream.commitTransaction()) { \ - QCOMPARE(vector.size(), expectedVector.size()); \ - QCOMPARE(vector, expectedVector); \ + QCOMPARE(linkedList.size(), expectedLinkedList.size()); \ + QCOMPARE(linkedList, expectedLinkedList); \ } else { \ - QVERIFY(vector.isEmpty()); \ + QVERIFY(linkedList.isEmpty()); \ } \ } \ if (inTransaction) \ @@ -3105,10 +3112,8 @@ void tst_QDataStream::status_QHash_QMap() void tst_QDataStream::status_QLinkedList_QList_QVector() { - typedef QLinkedList<QString> LinkedList; typedef QList<QString> List; typedef QVector<QString> Vector; - LinkedList linkedList; List list; Vector vector; @@ -3155,6 +3160,61 @@ void tst_QDataStream::status_QLinkedList_QList_QVector() LIST_TEST(QByteArray("\x00\x00\x00\x01", 4), QDataStream::ReadCorruptData, QDataStream::ReadCorruptData, List()); LIST_TEST(QByteArray("\x00\x00\x00\x01\x00\x00\x00\x01", 8), QDataStream::ReadPastEnd, QDataStream::ReadPastEnd, List()); } + +#if QT_DEPRECATED_SINCE(5, 15) +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED + // The same as above with QLinkedList + + typedef QLinkedList<QString> LinkedList; + LinkedList linkedList; + + // ok + { + List listWithEmptyString; + listWithEmptyString.append(""); + + List someList; + someList.append("J"); + someList.append("MN"); + + LINKED_LIST_TEST(QByteArray("\x00\x00\x00\x00", 4), QDataStream::Ok, QDataStream::Ok, List()); + LINKED_LIST_TEST(QByteArray("\x00\x00\x00\x01\x00\x00\x00\x00", 8), QDataStream::Ok, QDataStream::Ok, listWithEmptyString); + LINKED_LIST_TEST(QByteArray("\x00\x00\x00\x02\x00\x00\x00\x02\x00J" + "\x00\x00\x00\x04\x00M\x00N", 18), QDataStream::Ok, QDataStream::Ok, someList); + } + + // past end + { + LINKED_LIST_TEST(QByteArray(), QDataStream::Ok, QDataStream::ReadPastEnd, List()); + LINKED_LIST_TEST(QByteArray("\x00", 1), QDataStream::Ok, QDataStream::ReadPastEnd, List()); + LINKED_LIST_TEST(QByteArray("\x00\x00", 2), QDataStream::Ok, QDataStream::ReadPastEnd, List()); + LINKED_LIST_TEST(QByteArray("\x00\x00\x00", 3), QDataStream::Ok, QDataStream::ReadPastEnd, List()); + LINKED_LIST_TEST(QByteArray("\x00\x00\x00\x01", 4), QDataStream::Ok, QDataStream::ReadPastEnd, List()); + for (int i = 4; i < 12; ++i) { + LINKED_LIST_TEST(QByteArray("\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00", i), QDataStream::Ok, QDataStream::ReadPastEnd, List()); + } + } + + // corrupt data + { + LINKED_LIST_TEST(QByteArray("\x00\x00\x00\x01\x00\x00\x00\x01", 8), QDataStream::Ok, QDataStream::ReadCorruptData, List()); + LINKED_LIST_TEST(QByteArray("\x00\x00\x00\x02\x00\x00\x00\x01\x00J" + "\x00\x00\x00\x02\x00M\x00N", 18), QDataStream::Ok, QDataStream::ReadCorruptData, List()); + } + + // test the previously latched error status is not affected by reading + { + List listWithEmptyString; + listWithEmptyString.append(""); + + LINKED_LIST_TEST(QByteArray("\x00\x00\x00\x01\x00\x00\x00\x00", 8), QDataStream::ReadPastEnd, QDataStream::ReadPastEnd, listWithEmptyString); + LINKED_LIST_TEST(QByteArray("\x00\x00\x00\x01", 4), QDataStream::ReadCorruptData, QDataStream::ReadCorruptData, List()); + LINKED_LIST_TEST(QByteArray("\x00\x00\x00\x01\x00\x00\x00\x01", 8), QDataStream::ReadPastEnd, QDataStream::ReadPastEnd, List()); + } + +QT_WARNING_POP +#endif } void tst_QDataStream::streamToAndFromQByteArray() @@ -3296,7 +3356,10 @@ void tst_QDataStream::streamRealDataTypes() QCOMPARE(col, color); stream >> rGrad; QCOMPARE(rGrad.style(), radialBrush.style()); +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED QCOMPARE(rGrad.matrix(), radialBrush.matrix()); +QT_WARNING_POP QCOMPARE(rGrad.gradient()->type(), radialBrush.gradient()->type()); QCOMPARE(rGrad.gradient()->stops(), radialBrush.gradient()->stops()); QCOMPARE(rGrad.gradient()->spread(), radialBrush.gradient()->spread()); @@ -3305,7 +3368,10 @@ void tst_QDataStream::streamRealDataTypes() QCOMPARE(((QRadialGradient *)rGrad.gradient())->radius(), ((QRadialGradient *)radialBrush.gradient())->radius()); stream >> cGrad; QCOMPARE(cGrad.style(), conicalBrush.style()); +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED QCOMPARE(cGrad.matrix(), conicalBrush.matrix()); +QT_WARNING_POP QCOMPARE(cGrad.gradient()->type(), conicalBrush.gradient()->type()); QCOMPARE(cGrad.gradient()->stops(), conicalBrush.gradient()->stops()); QCOMPARE(cGrad.gradient()->spread(), conicalBrush.gradient()->spread()); diff --git a/tests/auto/corelib/serialization/qxmlstream/tst_qxmlstream.cpp b/tests/auto/corelib/serialization/qxmlstream/tst_qxmlstream.cpp index 92a0d8bbfa..28922574b8 100644 --- a/tests/auto/corelib/serialization/qxmlstream/tst_qxmlstream.cpp +++ b/tests/auto/corelib/serialization/qxmlstream/tst_qxmlstream.cpp @@ -221,7 +221,7 @@ static QString documentElement(const QByteArray &document) * * See \l {http://www.w3.org/XML/Test/} {Extensible Markup Language (XML) Conformance Test Suites} */ -class TestSuiteHandler : public QXmlDefaultHandler +class TestSuiteHandler { public: /** @@ -286,29 +286,33 @@ public: m_baseURI.push(baseURI); } - virtual bool characters(const QString &chars) + bool runTests(QFile *file) { - m_ch = chars; - return true; + QXmlStreamReader reader(file); + while (!reader.atEnd() && !reader.hasError()) { + reader.readNext(); + + if (reader.isStartElement() && !startElement(reader.attributes())) + return false; + + if (reader.isEndElement() && !endElement(reader.name().toString())) + return false; + } + return !reader.hasError(); } - virtual bool startElement(const QString &, - const QString &, - const QString &, - const QXmlAttributes &atts) + bool startElement(const QXmlStreamAttributes &atts) { m_atts.push(atts); - const int i = atts.index(QLatin1String("xml:base")); - if(i != -1) - m_baseURI.push(m_baseURI.top().resolved(atts.value(i))); + const auto attr = atts.value(QLatin1String("xml:base")); + if (!attr.isEmpty()) + m_baseURI.push(m_baseURI.top().resolved(attr.toString())); return true; } - virtual bool endElement(const QString &, - const QString &localName, - const QString &) + bool endElement(const QString &localName) { if(localName == QLatin1String("TEST")) { @@ -329,19 +333,19 @@ public: return true; } - const QString inputFilePath(m_baseURI.top().resolved(m_atts.top().value(QString(), QLatin1String("URI"))) - .toLocalFile()); - const QString id(m_atts.top().value(QString(), QLatin1String("ID"))); - const QString type(m_atts.top().value(QString(), QLatin1String("TYPE"))); + const QString inputFilePath( + m_baseURI.top() + .resolved( + m_atts.top().value(QString(), QLatin1String("URI")).toString()) + .toLocalFile()); + const QString id(m_atts.top().value(QString(), QLatin1String("ID")).toString()); + const QString type(m_atts.top().value(QString(), QLatin1String("TYPE")).toString()); QString expectedFilePath; - const int index = m_atts.top().index(QString(), QLatin1String("OUTPUT")); - if(index != -1) - { - expectedFilePath = m_baseURI.top().resolved(m_atts.top().value(QString(), - QLatin1String("OUTPUT"))).toLocalFile(); - } + const auto attr = m_atts.top().value(QString(), QLatin1String("OUTPUT")); + if (!attr.isEmpty()) + expectedFilePath = m_baseURI.top().resolved(attr.toString()).toLocalFile(); /* testcases.dtd: 'No parser should accept a "not-wf" testcase * unless it's a nonvalidating parser and the test contains @@ -349,7 +353,7 @@ public: * * We also let this apply to "valid", "invalid" and "error" tests, although * I'm not fully sure this is correct. */ - const QString ents(m_atts.top().value(QString(), QLatin1String("ENTITIES"))); + const QString ents(m_atts.top().value(QString(), QLatin1String("ENTITIES")).toString()); m_atts.pop(); if(ents == QLatin1String("both") || @@ -393,8 +397,6 @@ public: return true; } - QXmlStreamReader reader(&inputFile); - /* See testcases.dtd which reads: 'Nonvalidating parsers * must also accept "invalid" testcases, but validating ones must reject them.' */ if(type == QLatin1String("invalid") || type == QLatin1String("valid")) @@ -455,8 +457,8 @@ public: qFatal("The input catalog is invalid."); return false; } - } - else if(localName == QLatin1String("TESTCASES") && m_atts.top().index(QLatin1String("xml:base")) != -1) + } else if (localName == QLatin1String("TESTCASES") + && m_atts.top().hasAttribute(QLatin1String("xml:base"))) m_baseURI.pop(); m_atts.pop(); @@ -516,9 +518,8 @@ public: } private: - QStack<QXmlAttributes> m_atts; - QString m_ch; - QStack<QUrl> m_baseURI; + QStack<QXmlStreamAttributes> m_atts; + QStack<QUrl> m_baseURI; }; QT_BEGIN_NAMESPACE Q_DECLARE_SHARED(TestSuiteHandler::MissedBaseline) @@ -580,6 +581,8 @@ private slots: void roundTrip() const; void roundTrip_data() const; + void entityExpansionLimit() const; + private: static QByteArray readFile(const QString &filename); @@ -592,11 +595,7 @@ void tst_QXmlStream::initTestCase() QVERIFY2(file.open(QIODevice::ReadOnly), qPrintable(QString::fromLatin1("Failed to open the test suite catalog; %1").arg(file.fileName()))); - QXmlInputSource source(&file); - QXmlSimpleReader reader; - reader.setContentHandler(&m_handler); - - QVERIFY(reader.parse(&source, false)); + QVERIFY(m_handler.runTests(&file)); } void tst_QXmlStream::cleanupTestCase() @@ -1756,6 +1755,46 @@ void tst_QXmlStream::roundTrip_data() const "</root>\n"; } +void tst_QXmlStream::entityExpansionLimit() const +{ + QString xml = QStringLiteral("<?xml version=\"1.0\"?>" + "<!DOCTYPE foo [" + "<!ENTITY a \"0123456789\" >" + "<!ENTITY b \"&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;\" >" + "<!ENTITY c \"&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;\" >" + "<!ENTITY d \"&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;\" >" + "]>" + "<foo>&d;&d;&d;</foo>"); + { + QXmlStreamReader reader(xml); + QCOMPARE(reader.entityExpansionLimit(), 4096); + do { + reader.readNext(); + } while (!reader.atEnd()); + QCOMPARE(reader.error(), QXmlStreamReader::NotWellFormedError); + } + + // &d; expands to 10k characters, minus the 3 removed (&d;) means it should fail + // with a limit of 9996 chars and pass with 9997 + { + QXmlStreamReader reader(xml); + reader.setEntityExpansionLimit(9996); + do { + reader.readNext(); + } while (!reader.atEnd()); + + QCOMPARE(reader.error(), QXmlStreamReader::NotWellFormedError); + } + { + QXmlStreamReader reader(xml); + reader.setEntityExpansionLimit(9997); + do { + reader.readNext(); + } while (!reader.atEnd()); + QCOMPARE(reader.error(), QXmlStreamReader::NoError); + } +} + void tst_QXmlStream::roundTrip() const { QFETCH(QString, in); |