Skip to content

Commit

Permalink
MsgpackIODevice: invalid messages cant be fatal
Browse files Browse the repository at this point in the history
We were failing for one case of invalid RPC messages, raising a
fatal error. This commit removes that cases and adds several
additional cases to the unit tests to increase test coverage.
  • Loading branch information
equalsraf committed May 3, 2015
1 parent d581646 commit a4a3d34
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/msgpackiodevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void MsgpackIODevice::dispatch(msgpack_object& req)
//

if (req.type != MSGPACK_OBJECT_ARRAY) {
setError(InvalidMsgpack, "Received Invalid msgpack: not an array");
qDebug() << "Received Invalid msgpack: not an array";
return;
}

Expand Down
41 changes: 35 additions & 6 deletions test/tst_msgpackiodevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QTcpServer>
#include <QTcpSocket>
#include <QRegularExpression>
#include <QBuffer>

#include <msgpackiodevice.h>
#include <msgpackrequest.h>
Expand Down Expand Up @@ -60,6 +61,16 @@ private slots:
resetLoop();
}

void cleanup() {
delete one;
delete two;
}

void invalidDevice() {
MsgpackIODevice *io = new MsgpackIODevice(new QBuffer);
QCOMPARE(io->errorCause(), MsgpackIODevice::InvalidDevice);
}

void defaultValues() {
QVERIFY(one->encoding().isEmpty());
QCOMPARE(one->errorCause(), MsgpackIODevice::NoError);
Expand All @@ -82,14 +93,32 @@ private slots:
QCOMPARE(one->errorCause(), MsgpackIODevice::UnsupportedEncoding);
}

/**
* These errors are not fatal but increase coverage
*/
void recvError() {
QSignalSpy onError(two, SIGNAL(error(MsgpackError)));
QVERIFY(onError.isValid());

// Ignore qWarn
one->send(QByteArray("Hello!"));
QVERIFY(SPYWAIT(onError));
QCOMPARE(two->errorCause(), MsgpackIODevice::InvalidMsgpack);

// An array of size 1 is an invalid msgpack-rpc
QVariantList brokenRequest;
brokenRequest << 42;
one->send(brokenRequest);

// Invalid method
QVariantList brokenRequest2;
brokenRequest2 << "X" << 42 << 42 << 42;
one->send(brokenRequest2);

// Invalid method
QVariantList brokenRequest3;
brokenRequest2 << 0 << 42 << 42 << 42;
one->send(brokenRequest3);

// Just to finish
auto req = one->startRequestUnchecked("testRequest", 0);
QSignalSpy gotResp(req, SIGNAL(error(quint32, Function::FunctionId, QVariant)));
QVERIFY(gotResp.isValid());
QVERIFY2(SPYWAIT(gotResp), "By default all requests get an error");
}

void notification() {
Expand Down

0 comments on commit a4a3d34

Please sign in to comment.