Added maxMessageSize() and maxFrameSize() static getters to aid in unit testing
authorKurt Pattyn <pattyn.kurt@gmail.com>
Sun, 1 Sep 2013 11:49:00 +0000 (13:49 +0200)
committerKurt Pattyn <pattyn.kurt@gmail.com>
Sun, 1 Sep 2013 11:49:00 +0000 (13:49 +0200)
src/dataprocessor_p.cpp
src/dataprocessor_p.h
tests/tst_dataprocessor.cpp

index 82dd6e0..f666ab3 100644 (file)
@@ -626,6 +626,22 @@ DataProcessor::~DataProcessor()
 /*!
     \internal
  */
+quint64 DataProcessor::maxMessageSize()
+{
+    return MAX_MESSAGE_SIZE_IN_BYTES;
+}
+
+/*!
+    \internal
+ */
+quint64 DataProcessor::maxFrameSize()
+{
+    return MAX_FRAME_SIZE_IN_BYTES;
+}
+
+/*!
+    \internal
+ */
 void DataProcessor::process(QIODevice *pIoDevice)
 {
     bool isDone = false;
@@ -763,7 +779,12 @@ bool DataProcessor::processControlFrame(const Frame &frame)
         quint16 closeCode = QWebSocketProtocol::CC_NORMAL;
         QString closeReason;
         QByteArray payload = frame.getPayload();
-        if (payload.size() > 0)   //close frame can have a close code and reason
+        if (payload.size() == 1)
+        {
+            closeCode = QWebSocketProtocol::CC_PROTOCOL_ERROR;
+            closeReason = tr("Payload of close frame is too small.");
+        }
+        else if (payload.size() > 1)   //close frame can have a close code and reason
         {
             closeCode = qFromBigEndian<quint16>(reinterpret_cast<const uchar *>(payload.constData()));
             if (!QWebSocketProtocol::isCloseCodeValid(closeCode))
index c5850af..d2d1dcb 100644 (file)
@@ -53,6 +53,9 @@ public:
     explicit DataProcessor(QObject *parent = 0);
     virtual ~DataProcessor();
 
+    static quint64 maxMessageSize();
+    static quint64 maxFrameSize();
+
 Q_SIGNALS:
     void pingReceived(QByteArray data);
     void pongReceived(QByteArray data);
index 8fe953f..3f89dc3 100644 (file)
@@ -1107,10 +1107,7 @@ void tst_DataProcessor::frameTooBig_data()
     //only data frames are checked for being too big
     //control frames have explicit checking on a maximum payload size of 125, which is tested elsewhere
 
-    //TODO: get maximum framesize from the frame somehow
-    //the maximum framesize is currently defined as a constant in the dataprocessor_p.cpp file
-    //and is set to MAX_INT; changing that value will make this test fail (which is not good)
-    swapped64 = qToBigEndian<quint64>(quint64(INT_MAX + 1));
+    swapped64 = qToBigEndian<quint64>(DataProcessor::maxFrameSize() + 1);
     wireRepresentation = static_cast<const char *>(static_cast<const void *>(&swapped64));
     QTest::newRow("Text frame with payload size > INT_MAX")
             << quint8(FIN | QWebSocketProtocol::OC_TEXT)
@@ -1119,7 +1116,7 @@ void tst_DataProcessor::frameTooBig_data()
             << false
             << QWebSocketProtocol::CC_TOO_MUCH_DATA;
 
-    swapped64 = qToBigEndian<quint64>(quint64(INT_MAX + 1));
+    swapped64 = qToBigEndian<quint64>(DataProcessor::maxFrameSize() + 1);
     wireRepresentation = static_cast<const char *>(static_cast<const void *>(&swapped64));
     QTest::newRow("Binary frame with payload size > INT_MAX")
             << quint8(FIN | QWebSocketProtocol::OC_BINARY)
@@ -1128,7 +1125,7 @@ void tst_DataProcessor::frameTooBig_data()
             << false
             << QWebSocketProtocol::CC_TOO_MUCH_DATA;
 
-    swapped64 = qToBigEndian<quint64>(quint64(INT_MAX + 1));
+    swapped64 = qToBigEndian<quint64>(DataProcessor::maxFrameSize() + 1);
     wireRepresentation = static_cast<const char *>(static_cast<const void *>(&swapped64));
     QTest::newRow("Continuation frame with payload size > INT_MAX")
             << quint8(FIN | QWebSocketProtocol::OC_CONTINUE)