From a4a3d349bc0ea5605bba3aeaa2a26b52dcee85bb Mon Sep 17 00:00:00 2001 From: Rui Abreu Ferreira Date: Sun, 3 May 2015 00:55:29 +0100 Subject: [PATCH] MsgpackIODevice: invalid messages cant be fatal 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. --- src/msgpackiodevice.cpp | 2 +- test/tst_msgpackiodevice.cpp | 41 ++++++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/msgpackiodevice.cpp b/src/msgpackiodevice.cpp index 89434a3de..1fb6baa02 100644 --- a/src/msgpackiodevice.cpp +++ b/src/msgpackiodevice.cpp @@ -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; } diff --git a/test/tst_msgpackiodevice.cpp b/test/tst_msgpackiodevice.cpp index 92093974e..aa4a40b38 100644 --- a/test/tst_msgpackiodevice.cpp +++ b/test/tst_msgpackiodevice.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -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); @@ -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() {