Cleanup code to comply with Qt style
[contrib/qtwebsockets.git] / tests / auto / websocketprotocol / tst_websocketprotocol.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the test suite 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 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.
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 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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <QtTest/QtTest>
42 #include <QtTest/qtestcase.h>
43 #include <QtEndian>
44
45 #include <QDebug>
46
47 #include "qwebsocketprotocol.h"
48 #include "private/qwebsocketprotocol_p.h"
49
50 QT_USE_NAMESPACE
51
52 Q_DECLARE_METATYPE(QWebSocketProtocol::CloseCode)
53 Q_DECLARE_METATYPE(QWebSocketProtocol::OpCode)
54
55 class tst_WebSocketProtocol : public QObject
56 {
57     Q_OBJECT
58
59 public:
60     tst_WebSocketProtocol();
61
62 private Q_SLOTS:
63     void initTestCase();
64     void cleanupTestCase();
65     void init();
66     void cleanup();
67
68     void tst_validMasks_data();
69     void tst_validMasks();
70
71     void tst_opCodes_data();
72     void tst_opCodes();
73
74     void tst_closeCodes_data();
75     void tst_closeCodes();
76 };
77
78 tst_WebSocketProtocol::tst_WebSocketProtocol()
79 {}
80
81 void tst_WebSocketProtocol::initTestCase()
82 {
83 }
84
85 void tst_WebSocketProtocol::cleanupTestCase()
86 {}
87
88 void tst_WebSocketProtocol::init()
89 {
90     qRegisterMetaType<QWebSocketProtocol::OpCode>("QWebSocketProtocol::OpCode");
91     qRegisterMetaType<QWebSocketProtocol::CloseCode>("QWebSocketProtocol::CloseCode");
92 }
93
94 void tst_WebSocketProtocol::cleanup()
95 {
96 }
97
98 void tst_WebSocketProtocol::tst_validMasks_data()
99 {
100     QTest::addColumn<quint32>("mask");
101     QTest::addColumn<QString>("inputdata");
102     QTest::addColumn<QByteArray>("result");
103
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");
108 }
109
110 void tst_WebSocketProtocol::tst_validMasks()
111 {
112     QFETCH(quint32, mask);
113     QFETCH(QString, inputdata);
114     QFETCH(QByteArray, result);
115
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();
120
121     QWebSocketProtocol::mask(data, inputdata.size(), mask);
122     QCOMPARE(QByteArray::fromRawData(data, inputdata.size()), result);
123 }
124
125 void tst_WebSocketProtocol::tst_opCodes_data()
126 {
127     QTest::addColumn<QWebSocketProtocol::OpCode>("opCode");
128     QTest::addColumn<bool>("isReserved");
129
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;
146 }
147
148 void tst_WebSocketProtocol::tst_opCodes()
149 {
150     QFETCH(QWebSocketProtocol::OpCode, opCode);
151     QFETCH(bool, isReserved);
152
153     bool result = QWebSocketProtocol::isOpCodeReserved(opCode);
154
155     QCOMPARE(result, isReserved);
156 }
157
158 void tst_WebSocketProtocol::tst_closeCodes_data()
159 {
160     QTest::addColumn<int>("closeCode");
161     QTest::addColumn<bool>("isValid");
162
163     for (int i = 0; i < 1000; ++i)
164     {
165         QTest::newRow(QString("Close code %1").arg(i).toLatin1().constData()) << i << false;
166     }
167
168     for (int i = 1000; i < 1004; ++i)
169     {
170         QTest::newRow(QString("Close code %1").arg(i).toLatin1().constData()) << i << true;
171     }
172
173     QTest::newRow("Close code 1004") << 1004 << false;
174     QTest::newRow("Close code 1005") << 1005 << false;
175     QTest::newRow("Close code 1006") << 1006 << false;
176
177     for (int i = 1007; i < 1012; ++i)
178     {
179         QTest::newRow(QString("Close code %1").arg(i).toLatin1().constData()) << i << true;
180     }
181
182     for (int i = 1013; i < 3000; ++i)
183     {
184         QTest::newRow(QString("Close code %1").arg(i).toLatin1().constData()) << i << false;
185     }
186
187     for (int i = 3000; i < 5000; ++i)
188     {
189         QTest::newRow(QString("Close code %1").arg(i).toLatin1().constData()) << i << true;
190     }
191
192     QTest::newRow("Close code 5000") << 1004 << false;
193     QTest::newRow("Close code 6000") << 1004 << false;
194     QTest::newRow("Close code 7000") << 1004 << false;
195 }
196
197 void tst_WebSocketProtocol::tst_closeCodes()
198 {
199     QFETCH(int, closeCode);
200     QFETCH(bool, isValid);
201
202     bool result = QWebSocketProtocol::isCloseCodeValid(closeCode);
203
204     QCOMPARE(result, isValid);
205 }
206
207 QTEST_MAIN(tst_WebSocketProtocol)
208
209 #include "tst_websocketprotocol.moc"
210