| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2020 Intel Corporation. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU Lesser General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | 
| 21 | ** packaging of this file. Please review the following information to | 
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | 
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | 
| 24 | ** | 
| 25 | ** GNU General Public License Usage | 
| 26 | ** Alternatively, this file may be used under the terms of the GNU | 
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | 
| 28 | ** Public license version 3 or any later version approved by the KDE Free | 
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | 
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
| 31 | ** included in the packaging of this file. Please review the following | 
| 32 | ** information to ensure the GNU General Public License requirements will | 
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
| 35 | ** | 
| 36 | ** $QT_END_LICENSE$ | 
| 37 | ** | 
| 38 | ****************************************************************************/ | 
| 39 |  | 
| 40 | #include <QtTest/QtTest> | 
| 41 | #include <cbor.h> | 
| 42 |  | 
| 43 | namespace { | 
| 44 | // A QIODevice that supplies a fixed header followed by a large sequence of | 
| 45 | // null bytes up until a pre-determined size. | 
| 46 | class LargeIODevice final : public QIODevice | 
| 47 | { | 
| 48 | public: | 
| 49 |     qint64 realSize; | 
| 50 |     QByteArray start; | 
| 51 |  | 
| 52 |     LargeIODevice(const QByteArray &start, qint64 size, QObject *parent = nullptr) | 
| 53 |         : QIODevice(parent), realSize(size), start(start) | 
| 54 |     {} | 
| 55 |  | 
| 56 |     qint64 size() const override { return realSize; } | 
| 57 |     bool isSequential() const override { return false; } | 
| 58 |  | 
| 59 | protected: | 
| 60 |     qint64 readData(char *data, qint64 maxlen) override; | 
| 61 |     qint64 writeData(const char *, qint64) override { return -1; } | 
| 62 | }; | 
| 63 | }; | 
| 64 |  | 
| 65 | qint64 LargeIODevice::readData(char *data, qint64 maxlen) | 
| 66 | { | 
| 67 |     qint64 p = pos(); | 
| 68 |     if (maxlen > realSize - p) | 
| 69 |         maxlen = realSize - p; | 
| 70 |     memset(s: data, c: '\0', n: maxlen); | 
| 71 |  | 
| 72 |     qint64 fromstart = start.size() - p; | 
| 73 |     if (fromstart > maxlen) | 
| 74 |         fromstart = maxlen; | 
| 75 |     else if (fromstart < 0) | 
| 76 |         fromstart = 0; | 
| 77 |     if (fromstart) | 
| 78 |         memcpy(dest: data, src: start.constData() + p, n: fromstart); | 
| 79 |     return maxlen; | 
| 80 | } | 
| 81 |  | 
| 82 | void addValidationLargeData(qsizetype minInvalid, qsizetype maxInvalid) | 
| 83 | { | 
| 84 |     char toolong[1 + sizeof(qsizetype)]; | 
| 85 |     for (qsizetype v = maxInvalid; v >= minInvalid; --v) { | 
| 86 |         // 0x5a for 32-bit, 0x5b for 64-bit | 
| 87 |         toolong[0] = sizeof(v) > 4 ? 0x5b : 0x5a; | 
| 88 |         qToBigEndian(src: v, dest: toolong + 1); | 
| 89 |  | 
| 90 |         QTest::addRow(format: "bytearray-too-big-for-qbytearray-%llx" , v) | 
| 91 |                 << QByteArray(toolong, sizeof(toolong)) << 0 << CborErrorDataTooLarge; | 
| 92 |         QTest::addRow(format: "bytearray-chunked-too-big-for-qbytearray-%llx" , v) | 
| 93 |                 << ('\x5f' + QByteArray(toolong, sizeof(toolong)) + '\xff') | 
| 94 |                 << 0 << CborErrorDataTooLarge; | 
| 95 |         QTest::addRow(format: "bytearray-2chunked-too-big-for-qbytearray-%llx" , v) | 
| 96 |                 << ("\x5f\x40"  + QByteArray(toolong, sizeof(toolong)) + '\xff') | 
| 97 |                 << 0 << CborErrorDataTooLarge; | 
| 98 |         toolong[0] |= 0x20; | 
| 99 |  | 
| 100 |         // QCborStreamReader::readString copies to a QByteArray first | 
| 101 |         QTest::addRow(format: "string-too-big-for-qbytearray-%llx" , v) | 
| 102 |                 << QByteArray(toolong, sizeof(toolong)) << 0 << CborErrorDataTooLarge; | 
| 103 |         QTest::addRow(format: "string-chunked-too-big-for-qbytearray-%llx" , v) | 
| 104 |                 << ('\x7f' + QByteArray(toolong, sizeof(toolong)) + '\xff') | 
| 105 |                 << 0 << CborErrorDataTooLarge; | 
| 106 |         QTest::addRow(format: "string-2chunked-too-big-for-qbytearray-%llx" , v) | 
| 107 |                 << ("\x7f\x60"  + QByteArray(toolong, sizeof(toolong)) + '\xff') | 
| 108 |                 << 0 << CborErrorDataTooLarge; | 
| 109 |     } | 
| 110 | } | 
| 111 |  | 
| 112 | void addValidationHugeDevice(qsizetype byteArrayInvalid, qsizetype stringInvalid) | 
| 113 | { | 
| 114 |     qRegisterMetaType<QSharedPointer<QIODevice>>(); | 
| 115 |     QTest::addColumn<QSharedPointer<QIODevice>>(name: "device" ); | 
| 116 |     QTest::addColumn<CborError>(name: "expectedError" ); | 
| 117 |  | 
| 118 |     char buf[1 + sizeof(quint64)]; | 
| 119 |     auto device = [&buf](QCborStreamReader::Type t, quint64 size) { | 
| 120 |         buf[0] = quint8(t) | 0x1b; | 
| 121 |         qToBigEndian(src: size, dest: buf + 1); | 
| 122 |         size += sizeof(buf); | 
| 123 |         QSharedPointer<QIODevice> p = | 
| 124 |                 QSharedPointer<LargeIODevice>::create(arguments: QByteArray(buf, sizeof(buf)), arguments&: size); | 
| 125 |         return p; | 
| 126 |     }; | 
| 127 |  | 
| 128 |     // do the exact limits | 
| 129 |     QTest::newRow(dataTag: "bytearray-just-too-big" ) | 
| 130 |             << device(QCborStreamReader::ByteArray, byteArrayInvalid) << CborErrorDataTooLarge; | 
| 131 |     QTest::newRow(dataTag: "string-just-too-big" ) | 
| 132 |             << device(QCborStreamReader::String, stringInvalid) << CborErrorDataTooLarge; | 
| 133 |  | 
| 134 |     auto addSize = [=](const char *sizename, qint64 size) { | 
| 135 |         if (byteArrayInvalid < size) | 
| 136 |             QTest::addRow(format: "bytearray-%s" , sizename) | 
| 137 |                 << device(QCborStreamReader::ByteArray, size) << CborErrorDataTooLarge; | 
| 138 |         if (stringInvalid < size) | 
| 139 |             QTest::addRow(format: "string-%s" , sizename) | 
| 140 |                 << device(QCborStreamReader::String, size) << CborErrorDataTooLarge; | 
| 141 |     }; | 
| 142 |     addSize("1GB" , quint64(1) << 30); | 
| 143 |     addSize("2GB" , quint64(1) << 31); | 
| 144 |     addSize("4GB" , quint64(1) << 32); | 
| 145 |     addSize("max" , std::numeric_limits<qint64>::max() - sizeof(buf)); | 
| 146 | } | 
| 147 |  |