summaryrefslogtreecommitdiff
path: root/tests/auto/websockets/websocketprotocol/tst_websocketprotocol.cpp
blob: 94816de46a7a2ca92d7986aae684c369efbca60d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/****************************************************************************
**
** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** 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 http://www.qt.io/terms-conditions. For further
** information use the contact form at http://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 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <QtTest/qtestcase.h>
#include <QtEndian>

#include <QDebug>

#include "QtWebSockets/qwebsocketprotocol.h"
#include "private/qwebsocketprotocol_p.h"

QT_USE_NAMESPACE

Q_DECLARE_METATYPE(QWebSocketProtocol::CloseCode)
Q_DECLARE_METATYPE(QWebSocketProtocol::OpCode)
Q_DECLARE_METATYPE(QWebSocketProtocol::Version)

class tst_WebSocketProtocol : public QObject
{
    Q_OBJECT

public:
    tst_WebSocketProtocol();

private Q_SLOTS:
    void initTestCase();
    void cleanupTestCase();
    void init();
    void cleanup();

    void tst_validMasks_data();
    void tst_validMasks();

    void tst_opCodes_data();
    void tst_opCodes();

    void tst_closeCodes_data();
    void tst_closeCodes();

    void tst_versionFromString_data();
    void tst_versionFromString();
};

tst_WebSocketProtocol::tst_WebSocketProtocol()
{}

void tst_WebSocketProtocol::initTestCase()
{
}

void tst_WebSocketProtocol::cleanupTestCase()
{}

void tst_WebSocketProtocol::init()
{
    qRegisterMetaType<QWebSocketProtocol::OpCode>("QWebSocketProtocol::OpCode");
    qRegisterMetaType<QWebSocketProtocol::CloseCode>("QWebSocketProtocol::CloseCode");
}

void tst_WebSocketProtocol::cleanup()
{
}

void tst_WebSocketProtocol::tst_validMasks_data()
{
    QTest::addColumn<quint32>("mask");
    QTest::addColumn<QString>("inputdata");
    QTest::addColumn<QByteArray>("result");

    QTest::newRow("Empty payload") << 0x12345678u << QString() << QByteArray();
    QTest::newRow("ASCII payload of 8 characters")
            << 0x12345678u
            << QStringLiteral("abcdefgh")
            << QByteArrayLiteral("\x73\x56\x35\x1C\x77\x52\x31\x10");
    QTest::newRow("ASCII payload of 9 characters")
            << 0x12345678u
            << QStringLiteral("abcdefghi")
            << QByteArrayLiteral("\x73\x56\x35\x1C\x77\x52\x31\x10\x7B");
    //MSVC doesn't like UTF-8 in source code;
    //the following text is represented in the string below: ∫∂ƒ©øØ
    QTest::newRow("UTF-8 payload")
            << 0x12345678u
            << QString::fromUtf8("\xE2\x88\xAB\xE2\x88\x82\xC6\x92\xC2\xA9\xC3\xB8\xC3\x98")
            << QByteArrayLiteral("\x2D\x0B\x69\xD1\xEA\xEC");
}

void tst_WebSocketProtocol::tst_validMasks()
{
    QFETCH(quint32, mask);
    QFETCH(QString, inputdata);
    QFETCH(QByteArray, result);

    //put latin1 into an explicit array
    //otherwise, the intermediate object is deleted and the data pointer becomes invalid
    QByteArray latin1 = inputdata.toLatin1();
    char *data = latin1.data();

    QWebSocketProtocol::mask(data, inputdata.size(), mask);
    QCOMPARE(QByteArray::fromRawData(data, inputdata.size()), result);
}

void tst_WebSocketProtocol::tst_opCodes_data()
{
    QTest::addColumn<QWebSocketProtocol::OpCode>("opCode");
    QTest::addColumn<bool>("isReserved");

    QTest::newRow("OpCodeBinary")       << QWebSocketProtocol::OpCodeBinary << false;
    QTest::newRow("OpCodeClose")        << QWebSocketProtocol::OpCodeClose << false;
    QTest::newRow("OpCodeContinue")     << QWebSocketProtocol::OpCodeContinue << false;
    QTest::newRow("OpCodePing")         << QWebSocketProtocol::OpCodePing << false;
    QTest::newRow("OpCodePong")         << QWebSocketProtocol::OpCodePong << false;
    QTest::newRow("OpCodeReserved3")    << QWebSocketProtocol::OpCodeReserved3 << true;
    QTest::newRow("OpCodeReserved4")    << QWebSocketProtocol::OpCodeReserved4 << true;
    QTest::newRow("OpCodeReserved5")    << QWebSocketProtocol::OpCodeReserved5 << true;
    QTest::newRow("OpCodeReserved6")    << QWebSocketProtocol::OpCodeReserved6 << true;
    QTest::newRow("OpCodeReserved7")    << QWebSocketProtocol::OpCodeReserved7 << true;
    QTest::newRow("OpCodeReserved8")    << QWebSocketProtocol::OpCodeReservedB << true;
    QTest::newRow("OpCodeReservedC")    << QWebSocketProtocol::OpCodeReservedC << true;
    QTest::newRow("OpCodeReservedD")    << QWebSocketProtocol::OpCodeReservedD << true;
    QTest::newRow("OpCodeReservedE")    << QWebSocketProtocol::OpCodeReservedE << true;
    QTest::newRow("OpCodeReservedF")    << QWebSocketProtocol::OpCodeReservedF << true;
    QTest::newRow("OpCodeText")         << QWebSocketProtocol::OpCodeText << false;
}

void tst_WebSocketProtocol::tst_opCodes()
{
    QFETCH(QWebSocketProtocol::OpCode, opCode);
    QFETCH(bool, isReserved);

    bool result = QWebSocketProtocol::isOpCodeReserved(opCode);

    QCOMPARE(result, isReserved);
}

void tst_WebSocketProtocol::tst_closeCodes_data()
{
    QTest::addColumn<int>("closeCode");
    QTest::addColumn<bool>("isValid");

    for (int i = 0; i < 1000; ++i)
    {
        QTest::newRow(QStringLiteral("Close code %1").arg(i).toLatin1().constData()) << i << false;
    }

    for (int i = 1000; i < 1004; ++i)
    {
        QTest::newRow(QStringLiteral("Close code %1").arg(i).toLatin1().constData()) << i << true;
    }

    QTest::newRow("Close code 1004") << 1004 << false;
    QTest::newRow("Close code 1005") << 1005 << false;
    QTest::newRow("Close code 1006") << 1006 << false;

    for (int i = 1007; i < 1012; ++i)
    {
        QTest::newRow(QStringLiteral("Close code %1").arg(i).toLatin1().constData()) << i << true;
    }

    for (int i = 1013; i < 3000; ++i)
    {
        QTest::newRow(QStringLiteral("Close code %1").arg(i).toLatin1().constData()) << i << false;
    }

    for (int i = 3000; i < 5000; ++i)
    {
        QTest::newRow(QStringLiteral("Close code %1").arg(i).toLatin1().constData()) << i << true;
    }

    QTest::newRow("Close code 5000") << 1004 << false;
    QTest::newRow("Close code 6000") << 1004 << false;
    QTest::newRow("Close code 7000") << 1004 << false;
}

void tst_WebSocketProtocol::tst_closeCodes()
{
    QFETCH(int, closeCode);
    QFETCH(bool, isValid);

    bool result = QWebSocketProtocol::isCloseCodeValid(closeCode);

    QCOMPARE(result, isValid);
}

void tst_WebSocketProtocol::tst_versionFromString_data()
{
    QTest::addColumn<QWebSocketProtocol::Version>("version");
    QTest::addColumn<QString>("versionString");

    //happy flow; good data
    QTest::newRow("Version 0")
            << QWebSocketProtocol::Version0
            << QStringLiteral("0");
    QTest::newRow("Version 4")
            << QWebSocketProtocol::Version4
            << QStringLiteral("4");
    QTest::newRow("Version 5")
            << QWebSocketProtocol::Version5
            << QStringLiteral("5");
    QTest::newRow("Version 6")
            << QWebSocketProtocol::Version6
            << QStringLiteral("6");
    QTest::newRow("Version 7")
            << QWebSocketProtocol::Version7
            << QStringLiteral("7");
    QTest::newRow("Version 8")
            << QWebSocketProtocol::Version8
            << QStringLiteral("8");
    QTest::newRow("Version 13")
            << QWebSocketProtocol::Version13
            << QStringLiteral("13");

    //rainy flow; invalid data
    QTest::newRow("Version -1")
            << QWebSocketProtocol::VersionUnknown
            << QStringLiteral("-1");
    QTest::newRow("Version 1")
            << QWebSocketProtocol::VersionUnknown
            << QStringLiteral("1");
    QTest::newRow("Version 2")
            << QWebSocketProtocol::VersionUnknown
            << QStringLiteral("2");
    QTest::newRow("Version 3")
            << QWebSocketProtocol::VersionUnknown
            << QStringLiteral("3");
    QTest::newRow("Version 9")
            << QWebSocketProtocol::VersionUnknown
            << QStringLiteral("9");
    QTest::newRow("Version 10")
            << QWebSocketProtocol::VersionUnknown
            << QStringLiteral("10");
    QTest::newRow("Version 11")
            << QWebSocketProtocol::VersionUnknown
            << QStringLiteral("11");
    QTest::newRow("Version 12")
            << QWebSocketProtocol::VersionUnknown
            << QStringLiteral("12");
    QTest::newRow("Version abcd")
            << QWebSocketProtocol::VersionUnknown
            << QStringLiteral("abcd");
    QTest::newRow("Version 1.6")
            << QWebSocketProtocol::VersionUnknown
            << QStringLiteral("1.6");
    QTest::newRow("Version empty")
            << QWebSocketProtocol::VersionUnknown
            << QString();
}

void tst_WebSocketProtocol::tst_versionFromString()
{
    QFETCH(QWebSocketProtocol::Version, version);
    QFETCH(QString, versionString);

    QCOMPARE(QWebSocketProtocol::versionFromString(versionString), version);
}

QTEST_MAIN(tst_WebSocketProtocol)

#include "tst_websocketprotocol.moc"