diff options
Diffstat (limited to 'tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp')
-rw-r--r-- | tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp | 200 |
1 files changed, 144 insertions, 56 deletions
diff --git a/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp b/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp index 8fbe8d745e..9a227c782d 100644 --- a/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp +++ b/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> #include <QBuffer> @@ -44,6 +19,8 @@ #include <QtGui/QPixmap> #include <QtGui/QTextLength> +using namespace Qt::StringLiterals; + static_assert(QTypeTraits::has_ostream_operator_v<QDataStream, int>); static_assert(QTypeTraits::has_ostream_operator_v<QDataStream, QList<int>>); static_assert(QTypeTraits::has_ostream_operator_v<QDataStream, QMap<int, QString>>); @@ -157,6 +134,7 @@ private slots: void stream_atEnd(); void stream_writeError(); + void stream_writeSizeLimitExceeded(); void stream_QByteArray2(); @@ -165,9 +143,11 @@ private slots: void stream_QJsonObject(); void stream_QJsonValue(); +#if QT_CONFIG(cborstreamwriter) void stream_QCborArray(); void stream_QCborMap(); void stream_QCborValue(); +#endif void setVersion_data(); void setVersion(); @@ -221,6 +201,8 @@ private slots: void nestedTransactionsResult_data(); void nestedTransactionsResult(); + void typedefQt5Compat(); + private: void writebool(QDataStream *s); void writeQBitArray(QDataStream *s); @@ -316,6 +298,8 @@ static int NColorRoles[] = { QPalette::PlaceholderText + 1, // Qt_5_12 QPalette::PlaceholderText + 1, // Qt_5_13, Qt_5_14, Qt_5_15 QPalette::PlaceholderText + 1, // Qt_6_0 + QPalette::Accent + 1, // Qt_6_6 + QPalette::Accent + 1, // Qt_6_7 0 // add the correct value for Qt_5_14 here later }; @@ -981,10 +965,10 @@ static void QBitArrayData(QBitArray *b, int index) case 18: filler = "1111111111111111111111111111111111111111111111111111111111111111"; break; } - b->resize(filler.length()); + b->resize(filler.size()); b->fill(0); // reset all bits to zero - for (int i = 0; i < filler.length(); ++i) { + for (int i = 0; i < filler.size(); ++i) { if (filler.at(i) == '1') b->setBit(i, true); } @@ -1226,15 +1210,15 @@ void tst_QDataStream::readQCursor(QDataStream *s) QCOMPARE(d5.hotSpot(), test.hotSpot()); // Comparing non-null QBitmaps will fail. Upcast them first to pass. - QCOMPARE(d5.bitmap(Qt::ReturnByValue).isNull(), test.bitmap(Qt::ReturnByValue).isNull()); + QCOMPARE(d5.bitmap().isNull(), test.bitmap().isNull()); QCOMPARE( - static_cast<QPixmap>(d5.bitmap(Qt::ReturnByValue)), - static_cast<QPixmap>(test.bitmap(Qt::ReturnByValue)) + static_cast<QPixmap>(d5.bitmap()), + static_cast<QPixmap>(test.bitmap()) ); - QCOMPARE(d5.mask(Qt::ReturnByValue).isNull(), test.mask(Qt::ReturnByValue).isNull()); + QCOMPARE(d5.mask().isNull(), test.mask().isNull()); QCOMPARE( - static_cast<QPixmap>(d5.mask(Qt::ReturnByValue)), - static_cast<QPixmap>(test.mask(Qt::ReturnByValue)) + static_cast<QPixmap>(d5.mask()), + static_cast<QPixmap>(test.mask()) ); } #endif @@ -2209,6 +2193,19 @@ void tst_QDataStream::stream_writeError() TEST_WRITE_ERROR(.writeRawData("test", 4)) } +void tst_QDataStream::stream_writeSizeLimitExceeded() +{ + QByteArray ba; + QDataStream ds(&ba, QDataStream::ReadWrite); + // Set the version that supports only 32-bit data size + ds.setVersion(QDataStream::Qt_6_6); + QCOMPARE(ds.status(), QDataStream::Ok); + const qint64 size = qint64(std::numeric_limits<quint32>::max()) + 1; + ds.writeBytes("", size); + QCOMPARE(ds.status(), QDataStream::SizeLimitExceeded); + QVERIFY(ba.isEmpty()); +} + void tst_QDataStream::stream_QByteArray2() { QByteArray ba; @@ -2331,6 +2328,7 @@ void tst_QDataStream::stream_QJsonValue() } } +#if QT_CONFIG(cborstreamwriter) void tst_QDataStream::stream_QCborArray() { QByteArray buffer; @@ -2366,6 +2364,7 @@ void tst_QDataStream::stream_QCborValue() load >> valueLoad; QCOMPARE(valueLoad, valueSave); } +#endif void tst_QDataStream::setVersion_data() { @@ -2413,8 +2412,8 @@ void tst_QDataStream::setVersion() */ // revise the test if new color roles or color groups are added - QVERIFY(QPalette::NColorRoles == QPalette::PlaceholderText + 1); - QCOMPARE(int(QPalette::NColorGroups), 3); + QCOMPARE(QPalette::NColorRoles, QPalette::Accent + 1); + QCOMPARE(static_cast<int>(QPalette::NColorGroups), 3); QByteArray ba2; QPalette pal1, pal2; @@ -2815,7 +2814,6 @@ void tst_QDataStream::status_charptr_QByteArray_data() QTest::newRow("badsize 1MB+1") << QByteArray("\x00\x10\x00\x01", 4) + oneMbMinus1 + QByteArray("j") << (int) QDataStream::ReadPastEnd << QByteArray(); QTest::newRow("badsize 3MB") << QByteArray("\x00\x30\x00\x00", 4) + threeMbMinus1 << (int) QDataStream::ReadPastEnd << QByteArray(); QTest::newRow("badsize 3MB+1") << QByteArray("\x00\x30\x00\x01", 4) + threeMbMinus1 + QByteArray("j") << (int) QDataStream::ReadPastEnd << QByteArray(); - QTest::newRow("size -1") << QByteArray("\xff\xff\xff\xff", 4) << (int) QDataStream::ReadPastEnd << QByteArray(); QTest::newRow("size -2") << QByteArray("\xff\xff\xff\xfe", 4) << (int) QDataStream::ReadPastEnd << QByteArray(); } @@ -2838,17 +2836,35 @@ void tst_QDataStream::status_charptr_QByteArray() { QDataStream stream(&data, QIODevice::ReadOnly); char *buf; - uint len; + qint64 len; stream.readBytes(buf, len); - QCOMPARE((int)len, expectedString.size()); + QCOMPARE(len, qint64(expectedString.size())); QCOMPARE(QByteArray(buf, len), expectedString); QCOMPARE(int(stream.status()), expectedStatus); delete [] buf; } +#if QT_DEPRECATED_SINCE(6, 11) +QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED { + // check that old overload still works as expected QDataStream stream(&data, QIODevice::ReadOnly); - QByteArray buf; + char *buf; + auto cleanup = qScopeGuard([&buf] { + delete [] buf; + }); + uint len; + stream.readBytes(buf, len); + + QCOMPARE(len, expectedString.size()); + QCOMPARE(QByteArray(buf, len), expectedString); + QCOMPARE(int(stream.status()), expectedStatus); + } +QT_WARNING_POP +#endif // QT_DEPRECATED_SINCE(6, 11) + { + QDataStream stream(&data, QIODevice::ReadOnly); + QByteArray buf = "Content to be overwritten"; stream >> buf; if (data.startsWith("\xff\xff\xff\xff")) { @@ -2917,12 +2933,20 @@ void tst_QDataStream::status_QString_data() QTest::newRow("badsize 1MB+1") << QByteArray("\x00\x20\x00\x02", 4) + oneMbMinus1Data + QByteArray("j") << (int) QDataStream::ReadPastEnd << QString(); QTest::newRow("badsize 3MB") << QByteArray("\x00\x60\x00\x00", 4) + threeMbMinus1Data << (int) QDataStream::ReadPastEnd << QString(); QTest::newRow("badsize 3MB+1") << QByteArray("\x00\x60\x00\x02", 4) + threeMbMinus1Data + QByteArray("j") << (int) QDataStream::ReadPastEnd << QString(); - QTest::newRow("size -2") << QByteArray("\xff\xff\xff\xfe", 4) << (int) QDataStream::ReadPastEnd << QString(); - QTest::newRow("size MAX") << QByteArray("\x7f\xff\xff\xfe", 4) << (int) QDataStream::ReadPastEnd << QString(); + QTest::newRow("32 bit size should be 64 bit") << QByteArray("\xff\xff\xff\xfe", 4) << (int) QDataStream::ReadPastEnd << QString(); - // corrupt data +#if QT_POINTER_SIZE != 4 + // past end on 64 bit platforms + QTest::newRow("32 bit size MAX string no content") << QByteArray("\xff\xff\xff\xfc", 4) << (int) QDataStream::ReadPastEnd << QString(); +#else + // too big for 32 bit platforms + QTest::newRow("32 bit size MAX string no content") << QByteArray("\xff\xff\xff\xfc", 4) << (int) QDataStream::SizeLimitExceeded << QString(); +#endif + // too big on both 32 and 64 bit platforms because qsizetype is signed + QTest::newRow("64 bit size MAX string no content") << QByteArray("\xff\xff\xff\xfe\xff\xff\xff\xff\xff\xff\xff\xfe", 12) << (int) QDataStream::SizeLimitExceeded << QString(); + + // corrupt data because QChar is 16 bit => even size required QTest::newRow("corrupt1") << QByteArray("yyyy") << (int) QDataStream::ReadCorruptData << QString(); - QTest::newRow("size -3") << QByteArray("\xff\xff\xff\xfd", 4) << (int) QDataStream::ReadCorruptData << QString(); } void tst_QDataStream::status_QString() @@ -2932,7 +2956,7 @@ void tst_QDataStream::status_QString() QFETCH(QString, expectedString); QDataStream stream(&data, QIODevice::ReadOnly); - QString str; + QString str = "Content to be overwritten"; stream >> str; QCOMPARE(str.size(), expectedString.size()); @@ -2996,6 +3020,8 @@ void tst_QDataStream::status_QBitArray_data() QTest::newRow("badsize 16") << QDataStream::Qt_5_15 << QByteArray("\x00\x00\x00\x10\xff", 5) << (int) QDataStream::ReadPastEnd << QBitArray(); QTest::newRow("badsize 17") << QDataStream::Qt_5_15 << QByteArray("\x00\x00\x00\x11\xff\xff", 6) << (int) QDataStream::ReadPastEnd << QBitArray(); QTest::newRow("badsize 32") << QDataStream::Qt_5_15 << QByteArray("\x00\x00\x00\x20\xff\xff\xff", 7) << (int) QDataStream::ReadPastEnd << QBitArray(); + QTest::newRow("badsize INT_MAX") << QDataStream::Qt_5_15 << QByteArray("\x7f\xff\xff\xff\xff\xff\xff", 7) << int(QDataStream::ReadPastEnd) << QBitArray(); // size accepted + QTest::addRow("badsize INT_MAX + 1") << QDataStream::Qt_5_15 << QByteArray("\x80\x00\x00\x01" "\xff\xff\xff", 7) << int(QDataStream::ReadCorruptData) << QBitArray(); // size rejected QTest::newRow("new badsize 0") << QDataStream::Qt_6_0 << QByteArray("\x00\x00\x00\x00", 4) << (int) QDataStream::ReadPastEnd << QBitArray(); QTest::newRow("new badsize 9") << QDataStream::Qt_6_0 << QByteArray("\x00\x00\x00\x00\x00\x00\x00\x09\xff", 9) << (int) QDataStream::ReadPastEnd << QBitArray(); QTest::newRow("new badsize 0x10000") << QDataStream::Qt_6_0 << QByteArray("\x00\x00\x00\x01\x00\x00\x00\x00\x00", 9) << (int) QDataStream::ReadPastEnd << QBitArray(); @@ -3025,7 +3051,7 @@ void tst_QDataStream::status_QBitArray() QDataStream stream(&data, QIODevice::ReadOnly); stream.setVersion(version); - QBitArray str; + QBitArray str(255, true); stream >> str; if (sizeof(qsizetype) == sizeof(int)) @@ -3092,7 +3118,9 @@ void tst_QDataStream::status_QHash_QMap() hash2.insert("L", "MN"); // ok + hash = hash2; MAP_TEST(QByteArray("\x00\x00\x00\x00", 4), QDataStream::Ok, QDataStream::Ok, StringHash()); + hash = hash2; MAP_TEST(QByteArray("\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00", 12), QDataStream::Ok, QDataStream::Ok, hash1); MAP_TEST(QByteArray("\x00\x00\x00\x02\x00\x00\x00\x02\x00J\x00\x00\x00\x02\x00K" "\x00\x00\x00\x02\x00L\x00\x00\x00\x04\x00M\x00N", 30), QDataStream::Ok, QDataStream::Ok, hash2); @@ -3173,7 +3201,9 @@ void tst_QDataStream::status_QList_QVector() someList.append("J"); someList.append("MN"); + list = someList; LIST_TEST(QByteArray("\x00\x00\x00\x00", 4), QDataStream::Ok, QDataStream::Ok, List()); + list = someList; LIST_TEST(QByteArray("\x00\x00\x00\x01\x00\x00\x00\x00", 8), QDataStream::Ok, QDataStream::Ok, listWithEmptyString); LIST_TEST(QByteArray("\x00\x00\x00\x02\x00\x00\x00\x02\x00J" "\x00\x00\x00\x04\x00M\x00N", 18), QDataStream::Ok, QDataStream::Ok, someList); @@ -3228,11 +3258,13 @@ void tst_QDataStream::streamRealDataTypes() // Generate QPicture from pixmap. QPixmap pm(open_xpm); QVERIFY(!pm.isNull()); +#ifndef QT_NO_PICTURE QPicture picture; picture.setBoundingRect(QRect(QPoint(0, 0), pm.size())); QPainter painter(&picture); painter.drawPixmap(0, 0, pm); painter.end(); +#endif // Generate path QPainterPath path; @@ -3241,6 +3273,13 @@ void tst_QDataStream::streamRealDataTypes() path.arcTo(4, 5, 6, 7, 8, 9); path.quadTo(1, 2, 3, 4); + QPainterPath otherPath; + otherPath.arcTo(10, 4, 5, 6, 7, 8); + otherPath.lineTo(9, 0); + otherPath.cubicTo(0, 0, 10, 10, 20, 20); + otherPath.quadTo(2, 4, 5, 6); + QCOMPARE(otherPath.elementCount(), 12); + QColor color(64, 64, 64); color.setAlphaF(0.5); QRadialGradient radialGradient(5, 6, 7, 8, 9); @@ -3263,7 +3302,9 @@ void tst_QDataStream::streamRealDataTypes() stream << QPointF(3, 5) << QRectF(-1, -2, 3, 4) << (QPolygonF() << QPointF(0, 0) << QPointF(1, 2)); stream << QTransform().rotate(90).scale(2, 2).asAffineMatrix(); stream << path; +#ifndef QT_NO_PICTURE stream << picture; +#endif stream << QTextLength(QTextLength::VariableLength, 1.5); stream << color; stream << radialBrush << conicalBrush; @@ -3272,17 +3313,19 @@ void tst_QDataStream::streamRealDataTypes() file.close(); } - QPointF point; - QRectF rect; - QPolygonF polygon; + QPointF point(1, 2); + QRectF rect(1, 2, 5, 6); + QPolygonF polygon {{3, 4}, {5, 6}}; QTransform transform; - QPainterPath p; + QPainterPath p = otherPath; +#ifndef QT_NO_PICTURE QPicture pict; - QTextLength textLength; - QColor col; - QBrush rGrad; - QBrush cGrad; - QPen pen; +#endif + QTextLength textLength(QTextLength::FixedLength, 2.5); + QColor col(128, 128, 127); + QBrush rGrad(Qt::CrossPattern); + QBrush cGrad(Qt::CrossPattern); + QPen pen(conicalBrush, 10); QVERIFY(file.open(QIODevice::ReadOnly)); QDataStream stream(&file); @@ -3330,6 +3373,7 @@ void tst_QDataStream::streamRealDataTypes() QCOMPARE(transform, QTransform().rotate(90).scale(2, 2)); stream >> p; QCOMPARE(p, path); +#ifndef QT_NO_PICTURE if (i == 1) { stream >> pict; @@ -3343,6 +3387,7 @@ void tst_QDataStream::streamRealDataTypes() QCOMPARE(pictA, pictB); } +#endif stream >> textLength; QCOMPARE(textLength, QTextLength(QTextLength::VariableLength, 1.5)); stream >> col; @@ -3872,6 +3917,49 @@ void tst_QDataStream::nestedTransactionsResult() QCOMPARE(int(stream.status()), expectedStatus); } +using CustomPair = QPair<int, int>; +QDataStream &operator<<(QDataStream &ds, CustomPair pd) +{ return ds << pd.first << pd.second; } +QDataStream &operator>>(QDataStream &ds, CustomPair &pd) +{ return ds >> pd.first >> pd.second; } + + +void tst_QDataStream::typedefQt5Compat() +{ + qRegisterMetaType<CustomPair>("CustomPair"); + QByteArray qt5Data; + { + // we can read the qt5 version + QFile in(QFINDTESTDATA("typedef.q5")); + QVERIFY(in.open(QIODevice::ReadOnly)); + qt5Data = in.readAll(); + QVERIFY(in.seek(0)); + QDataStream stream(&in); + stream.setVersion(QDataStream::Qt_5_15); + QVariant var; + stream >> var; + QCOMPARE(stream.status(), QDataStream::Ok); + CustomPair p = var.value<CustomPair>(); + QCOMPARE(p.first, 42); + QCOMPARE(p.second, 100); + } + { + // writing in Qt 6 results in the same file + QTemporaryDir dir; + QVERIFY(dir.isValid()); + QFile file(dir.filePath(u"typedef.q6"_s)); + QVERIFY(file.open(QIODevice::WriteOnly)); + QDataStream stream(&file); + stream.setVersion(QDataStream::Qt_5_15); + CustomPair p {42, 100}; + stream << QVariant::fromValue(p); + file.close(); + QVERIFY(file.open(QIODevice::ReadOnly)); + QCOMPARE(file.readAll(), qt5Data); + } +} + QTEST_MAIN(tst_QDataStream) + #include "tst_qdatastream.moc" |