1 /****************************************************************************
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the test suite of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
41 #include <QtTest/QtTest>
42 #include <QtTest/qtestcase.h>
47 #include "qwebsocketprotocol.h"
48 #include "private/qwebsocketprotocol_p.h"
52 Q_DECLARE_METATYPE(QWebSocketProtocol::CloseCode)
53 Q_DECLARE_METATYPE(QWebSocketProtocol::OpCode)
55 class tst_WebSocketProtocol : public QObject
60 tst_WebSocketProtocol();
64 void cleanupTestCase();
68 void tst_validMasks_data();
69 void tst_validMasks();
71 void tst_opCodes_data();
74 void tst_closeCodes_data();
75 void tst_closeCodes();
78 tst_WebSocketProtocol::tst_WebSocketProtocol()
81 void tst_WebSocketProtocol::initTestCase()
85 void tst_WebSocketProtocol::cleanupTestCase()
88 void tst_WebSocketProtocol::init()
90 qRegisterMetaType<QWebSocketProtocol::OpCode>("QWebSocketProtocol::OpCode");
91 qRegisterMetaType<QWebSocketProtocol::CloseCode>("QWebSocketProtocol::CloseCode");
94 void tst_WebSocketProtocol::cleanup()
98 void tst_WebSocketProtocol::tst_validMasks_data()
100 QTest::addColumn<quint32>("mask");
101 QTest::addColumn<QString>("inputdata");
102 QTest::addColumn<QByteArray>("result");
104 QTest::newRow("Empty payload") << 0x12345678u << QString("") << QByteArray("");
105 QTest::newRow("ASCII payload of 8 characters") << 0x12345678u << QString("abcdefgh") << QByteArray("\x73\x56\x35\x1C\x77\x52\x31\x10");
106 QTest::newRow("ASCII payload of 9 characters") << 0x12345678u << QString("abcdefghi") << QByteArray("\x73\x56\x35\x1C\x77\x52\x31\x10\x7B");
107 QTest::newRow("UTF-8 payload") << 0x12345678u << QString("∫∂ƒ©øØ") << QByteArray("\x2D\x0B\x69\xD1\xEA\xEC");
110 void tst_WebSocketProtocol::tst_validMasks()
112 QFETCH(quint32, mask);
113 QFETCH(QString, inputdata);
114 QFETCH(QByteArray, result);
116 //put latin1 into an explicit array
117 //otherwise, the intermediate object is deleted and the data pointer becomes invalid
118 QByteArray latin1 = inputdata.toLatin1();
119 char *data = latin1.data();
121 QWebSocketProtocol::mask(data, inputdata.size(), mask);
122 QCOMPARE(QByteArray::fromRawData(data, inputdata.size()), result);
125 void tst_WebSocketProtocol::tst_opCodes_data()
127 QTest::addColumn<QWebSocketProtocol::OpCode>("opCode");
128 QTest::addColumn<bool>("isReserved");
130 QTest::newRow("OC_BINARY") << QWebSocketProtocol::OC_BINARY << false;
131 QTest::newRow("OC_CLOSE") << QWebSocketProtocol::OC_CLOSE << false;
132 QTest::newRow("OC_CONTINUE") << QWebSocketProtocol::OC_CONTINUE << false;
133 QTest::newRow("OC_PING") << QWebSocketProtocol::OC_PING << false;
134 QTest::newRow("OC_PONG") << QWebSocketProtocol::OC_PONG << false;
135 QTest::newRow("OC_RESERVED3") << QWebSocketProtocol::OC_RESERVED_3 << true;
136 QTest::newRow("OC_RESERVED4") << QWebSocketProtocol::OC_RESERVED_4 << true;
137 QTest::newRow("OC_RESERVED5") << QWebSocketProtocol::OC_RESERVED_5 << true;
138 QTest::newRow("OC_RESERVED6") << QWebSocketProtocol::OC_RESERVED_6 << true;
139 QTest::newRow("OC_RESERVED7") << QWebSocketProtocol::OC_RESERVED_7 << true;
140 QTest::newRow("OC_RESERVEDB") << QWebSocketProtocol::OC_RESERVED_B << true;
141 QTest::newRow("OC_RESERVEDC") << QWebSocketProtocol::OC_RESERVED_C << true;
142 QTest::newRow("OC_RESERVEDD") << QWebSocketProtocol::OC_RESERVED_D << true;
143 QTest::newRow("OC_RESERVEDE") << QWebSocketProtocol::OC_RESERVED_E << true;
144 QTest::newRow("OC_RESERVEDF") << QWebSocketProtocol::OC_RESERVED_F << true;
145 QTest::newRow("OC_TEXT") << QWebSocketProtocol::OC_TEXT << false;
148 void tst_WebSocketProtocol::tst_opCodes()
150 QFETCH(QWebSocketProtocol::OpCode, opCode);
151 QFETCH(bool, isReserved);
153 bool result = QWebSocketProtocol::isOpCodeReserved(opCode);
155 QCOMPARE(result, isReserved);
158 void tst_WebSocketProtocol::tst_closeCodes_data()
160 QTest::addColumn<int>("closeCode");
161 QTest::addColumn<bool>("isValid");
163 for (int i = 0; i < 1000; ++i)
165 QTest::newRow(QString("Close code %1").arg(i).toLatin1().constData()) << i << false;
168 for (int i = 1000; i < 1004; ++i)
170 QTest::newRow(QString("Close code %1").arg(i).toLatin1().constData()) << i << true;
173 QTest::newRow("Close code 1004") << 1004 << false;
174 QTest::newRow("Close code 1005") << 1005 << false;
175 QTest::newRow("Close code 1006") << 1006 << false;
177 for (int i = 1007; i < 1012; ++i)
179 QTest::newRow(QString("Close code %1").arg(i).toLatin1().constData()) << i << true;
182 for (int i = 1013; i < 3000; ++i)
184 QTest::newRow(QString("Close code %1").arg(i).toLatin1().constData()) << i << false;
187 for (int i = 3000; i < 5000; ++i)
189 QTest::newRow(QString("Close code %1").arg(i).toLatin1().constData()) << i << true;
192 QTest::newRow("Close code 5000") << 1004 << false;
193 QTest::newRow("Close code 6000") << 1004 << false;
194 QTest::newRow("Close code 7000") << 1004 << false;
197 void tst_WebSocketProtocol::tst_closeCodes()
199 QFETCH(int, closeCode);
200 QFETCH(bool, isValid);
202 bool result = QWebSocketProtocol::isCloseCodeValid(closeCode);
204 QCOMPARE(result, isValid);
207 QTEST_MAIN(tst_WebSocketProtocol)
209 #include "tst_websocketprotocol.moc"