Added TODO explaining some optimizations
authorKurt Pattyn <pattyn.kurt@gmail.com>
Sun, 13 Oct 2013 10:15:07 +0000 (12:15 +0200)
committerKurt Pattyn <pattyn.kurt@gmail.com>
Sun, 13 Oct 2013 10:25:04 +0000 (12:25 +0200)
Moved documentation from .h to .cpp
Export QWebSocketFrame class for unit testing
Added unit tests for web socket frames

Change-Id: I824da86fed37fbcd1f5671a21467af7a66f5c019
Reviewed-by: Kurt Pattyn <pattyn.kurt@gmail.com>
src/websockets/qwebsocket_p.cpp
src/websockets/qwebsocketframe_p.cpp
src/websockets/qwebsocketframe_p.h
tests/auto/websocketframe/tst_websocketframe.cpp [new file with mode: 0644]
tests/auto/websocketframe/websocketframe.pro [new file with mode: 0644]

index 43cca86..414cd9e 100644 (file)
@@ -153,6 +153,8 @@ bool QWebSocketPrivate::flush()
  */
 qint64 QWebSocketPrivate::write(const char *message)
 {
+    //TODO: create a QByteArray from message, and directly call doWriteData
+    //now the data is converted to a string, and then converted back to a bytearray
     return write(QString::fromUtf8(message));
 }
 
@@ -161,6 +163,8 @@ qint64 QWebSocketPrivate::write(const char *message)
  */
 qint64 QWebSocketPrivate::write(const char *message, qint64 maxSize)
 {
+    //TODO: create a QByteArray from message, and directly call doWriteData
+    //now the data is converted to a string, and then converted back to a bytearray
     return write(QString::fromUtf8(message, static_cast<int>(maxSize)));
 }
 
index 7a26296..3c8a3a9 100644 (file)
@@ -17,6 +17,19 @@ License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
+/*!
+    \class QWebSocketFrame
+    The class QWebSocketFrame is responsible for reading, validating and interpreting frames from a websocket.
+    It reads data from a QIODevice, validates it against RFC 6455, and parses it into a frame (data, control).
+    Whenever an error is detected, isValid() returns false.
+
+    \note The QWebSocketFrame class does not look at valid sequences of frames. It processes frames one at a time.
+    \note It is the QWebSocketDataProcessor that takes the sequence into account.
+
+    \sa DataProcessor()
+    \internal
+ */
+
 #include "qwebsocketframe_p.h"
 
 #include <QtEndian>
@@ -231,6 +244,10 @@ QWebSocketFrame QWebSocketFrame::readFrame(QIODevice *pIoDevice)
         {
             case PS_WAIT_FOR_MORE_DATA:
             {
+                //TODO: waitForReadyRead should really be changed
+                //now, when a websocket is used in a GUI thread
+                //the GUI will hang for at most 5 seconds
+                //maybe, a QStateMachine should be used
                 bool ok = pIoDevice->waitForReadyRead(5000);
                 if (!ok)
                 {
index 434fead..5dc0957 100644 (file)
@@ -29,19 +29,7 @@ QT_BEGIN_NAMESPACE
 const quint64 MAX_FRAME_SIZE_IN_BYTES = INT_MAX - 1;
 const quint64 MAX_MESSAGE_SIZE_IN_BYTES = INT_MAX - 1;
 
-/*!
-    \class Frame
-    The class Frame is responsible for reading, validating and interpreting frames from a websocket.
-    It reads data from a QIODevice, validates it against RFC 6455, and parses it into a frame (data, control).
-    Whenever an error is detected, the isValid() returns false.
-
-    \note The Frame class does not look at valid sequences of frames. It processes frames one at a time.
-    \note It is the DataProcessor that takes the sequence into account.
-
-    \sa DataProcessor()
-    \internal
- */
-class QWebSocketFrame
+class Q_AUTOTEST_EXPORT QWebSocketFrame
 {
 public:
     QWebSocketFrame();
diff --git a/tests/auto/websocketframe/tst_websocketframe.cpp b/tests/auto/websocketframe/tst_websocketframe.cpp
new file mode 100644 (file)
index 0000000..e08307b
--- /dev/null
@@ -0,0 +1,93 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the test suite 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 Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/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 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+#include <QtTest/QtTest>
+#include <QtTest/qtestcase.h>
+#include <QDebug>
+
+#include "private/qwebsocketframe_p.h"
+
+QT_USE_NAMESPACE
+
+class tst_WebSocketFrame : public QObject
+{
+    Q_OBJECT
+
+public:
+    tst_WebSocketFrame();
+
+private Q_SLOTS:
+    void initTestCase();
+    void cleanupTestCase();
+    void init();
+    void cleanup();
+
+    void tst_initialization();
+};
+
+tst_WebSocketFrame::tst_WebSocketFrame()
+{}
+
+void tst_WebSocketFrame::initTestCase()
+{
+}
+
+void tst_WebSocketFrame::cleanupTestCase()
+{}
+
+void tst_WebSocketFrame::init()
+{
+}
+
+void tst_WebSocketFrame::cleanup()
+{
+}
+
+void tst_WebSocketFrame::tst_initialization()
+{
+    QWebSocketFrame frame;
+    QVERIFY(!frame.isValid());
+    QCOMPARE(frame.getPayload().length(), 0);
+}
+
+QTEST_MAIN(tst_WebSocketFrame)
+
+#include "tst_websocketframe.moc"
+
diff --git a/tests/auto/websocketframe/websocketframe.pro b/tests/auto/websocketframe/websocketframe.pro
new file mode 100644 (file)
index 0000000..d18aa7d
--- /dev/null
@@ -0,0 +1,15 @@
+CONFIG += console
+CONFIG += c++11
+CONFIG += testcase
+CONFIG -= app_bundle
+
+TEMPLATE = app
+
+TARGET = tst_websocketframe
+
+QT = core testlib websockets websockets-private
+
+SOURCES += tst_websocketframe.cpp
+
+requires(contains(QT_CONFIG, private_tests))
+DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0