diff options
Diffstat (limited to 'tests/auto/corelib/serialization/qcborstreamreader/tst_qcborstreamreader.cpp')
-rw-r--r-- | tests/auto/corelib/serialization/qcborstreamreader/tst_qcborstreamreader.cpp | 144 |
1 files changed, 101 insertions, 43 deletions
diff --git a/tests/auto/corelib/serialization/qcborstreamreader/tst_qcborstreamreader.cpp b/tests/auto/corelib/serialization/qcborstreamreader/tst_qcborstreamreader.cpp index 8d0b5a054e..63cfbce75f 100644 --- a/tests/auto/corelib/serialization/qcborstreamreader/tst_qcborstreamreader.cpp +++ b/tests/auto/corelib/serialization/qcborstreamreader/tst_qcborstreamreader.cpp @@ -1,48 +1,10 @@ -/**************************************************************************** -** -** Copyright (C) 2020 Intel Corporation. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** 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 Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** 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-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2020 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QtCore/qcborstream.h> #include <QTest> #include <QBuffer> -#include <QtCore/private/qbytearray_p.h> - class tst_QCborStreamReader : public QObject { Q_OBJECT @@ -124,7 +86,7 @@ template<> char *toString<QCborStreamReader::Type>(const QCborStreamReader::Type QT_END_NAMESPACE // Get the data from TinyCBOR (see src/3rdparty/tinycbor/tests/parser/data.cpp) -#include "data.cpp" +#include "parser/data.cpp" void tst_QCborStreamReader::initTestCase_data() { @@ -693,6 +655,7 @@ void tst_QCborStreamReader::strings() QCOMPARE(reader.currentStringChunkSize(), qsizetype(reader.length())); int chunks = 0; + QByteArray fullString; forever { QCborStreamReader::StringResult<QByteArray> controlData; if (reader.isString()) { @@ -703,6 +666,7 @@ void tst_QCborStreamReader::strings() controlData = controlReader.readByteArray(); } QVERIFY(controlData.status != QCborStreamReader::Error); + fullString += controlData.data; for (int i = 0; i < 10; ++i) { // this call must work several times with the same result @@ -725,6 +689,43 @@ void tst_QCborStreamReader::strings() if (!isChunked) QCOMPARE(chunks, 1); + + // Now re-do and compare with toString() and toByteArray(), against + // the control data we calculated above + reader.reset(); + QVERIFY(reader.isString() || reader.isByteArray()); + if (reader.isByteArray()) { + QByteArray prefix("some prefix"); + QByteArray ba = prefix; + QVERIFY(reader.readAndAppendToByteArray(ba)); + QCOMPARE(ba, prefix + fullString); + } else { + QString prefix("some prefix"); + QString str = prefix; + QVERIFY(reader.readAndAppendToString(str)); + QCOMPARE(str, prefix + QString::fromUtf8(fullString)); + } + + // Re-do again using the UTF-8 interface. + reader.reset(); + QVERIFY(reader.isString() || reader.isByteArray()); + if (reader.isString()) { + QByteArray prefix("some prefix"); + QByteArray utf8 = prefix; + QVERIFY(reader.readAndAppendToUtf8String(utf8)); + QCOMPARE(utf8, prefix + fullString); + + reader.reset(); + fullString = prefix; + forever { + auto r = reader.readUtf8String(); + QCOMPARE_NE(r.status, QCborStreamReader::Error); + fullString += r.data; + if (r.status == QCborStreamReader::EndOfString) + break; + } + QCOMPARE(fullString, utf8); + } } void tst_QCborStreamReader::tags_data() @@ -917,7 +918,7 @@ void tst_QCborStreamReader::validation_data() // Add QCborStreamReader-specific limitations due to use of QByteArray and // QString, which are allocated by QArrayData::allocate(). const qsizetype MaxInvalid = std::numeric_limits<QByteArray::size_type>::max(); - const qsizetype MinInvalid = MaxByteArraySize + 1; + const qsizetype MinInvalid = QByteArray::max_size() + 1; addValidationColumns(); addValidationData(MinInvalid); @@ -944,11 +945,57 @@ void tst_QCborStreamReader::validation() reader.reset(); QVERIFY(!reader.next()); QCOMPARE(reader.lastError(), error); + + // check toString() and toByteArray() too + if (reader.isString() || reader.isByteArray()) { + reader.reset(); + if (reader.isString()) { + QString prefix = "some prefix"; + QString str = prefix; + QVERIFY(!reader.readAndAppendToString(str)); + QVERIFY(str.startsWith(prefix)); // but may have decoded some + } else if (reader.isByteArray()) { + QByteArray prefix = "some prefix"; + QByteArray ba = prefix; + QVERIFY(!reader.readAndAppendToByteArray(ba)); + QVERIFY(ba.startsWith(prefix)); // but may have decoded some + } + QCOMPARE(reader.lastError(), error); + + reader.reset(); + if (reader.isString()) + QVERIFY(reader.readAllString().isNull()); + else + QVERIFY(reader.readAllByteArray().isNull()); + } + + reader.reset(); + + // and the UTF-8 API + if (reader.isString()) { + QByteArray prefix = "some prefix"; + QByteArray ba = prefix; + QVERIFY(!reader.readAndAppendToUtf8String(ba)); + QVERIFY(ba.startsWith(prefix)); // but may have decoded some + QCOMPARE(reader.lastError(), error); + + reader.reset(); + QVERIFY(reader.readAllUtf8String().isNull()); + + reader.reset(); + auto r = reader.readUtf8String(); + for ( ; r.status == QCborStreamReader::Ok; r = reader.readUtf8String()) { + // while the data is valid... + QVERIFY(!r.data.isNull()); + } + QCOMPARE_NE(r.status, QCborStreamReader::EndOfString); + QCOMPARE(reader.lastError(), error); + } } void tst_QCborStreamReader::hugeDeviceValidation_data() { - addValidationHugeDevice(MaxByteArraySize + 1, MaxStringSize + 1); + addValidationHugeDevice(QByteArray::max_size() + 1, QString::max_size() + 1); } void tst_QCborStreamReader::hugeDeviceValidation() @@ -957,6 +1004,17 @@ void tst_QCborStreamReader::hugeDeviceValidation() if (!useDevice) return; +#if (defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)) + if ( qstrcmp(QTest::currentDataTag(), "bytearray-just-too-big") == 0 + || qstrcmp(QTest::currentDataTag(), "string-just-too-big") == 0) + QSKIP("This test tries to allocate a huge memory buffer," + " which Address Sanitizer flags as a problem"); +#endif +#if defined(Q_OS_WASM) + QSKIP("This test tries to allocate a huge memory buffer," + " causes problem on WebAssembly platform which has limited resources."); +#endif // Q_OS_WASM + QFETCH(QSharedPointer<QIODevice>, device); QFETCH(CborError, expectedError); QFETCH(CborError, expectedValidationError); |