From 100e7145c943addbf948aaa7c4cfcf194f054149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Wed, 13 Apr 2016 14:11:14 +0200 Subject: [PATCH] improve code coverage --- libguh/coap/coapoption.cpp | 7 -- libguh/coap/coapoption.h | 1 - libguh/coap/coappdu.cpp | 6 +- libguh/coap/coaprequest.cpp | 13 ---- libguh/coap/coaprequest.h | 3 - tests/auto/coap/coaptests.cpp | 122 +++++++++++++--------------------- 6 files changed, 53 insertions(+), 99 deletions(-) diff --git a/libguh/coap/coapoption.cpp b/libguh/coap/coapoption.cpp index f061cb97..248bbef3 100644 --- a/libguh/coap/coapoption.cpp +++ b/libguh/coap/coapoption.cpp @@ -82,13 +82,6 @@ CoapOption::CoapOption() } -CoapOption::CoapOption(const CoapOption::Option &option, const QByteArray &data) : - m_option(option), - m_data(data) -{ - -} - void CoapOption::setOption(const CoapOption::Option &option) { m_option = option; diff --git a/libguh/coap/coapoption.h b/libguh/coap/coapoption.h index d744ea2c..b49de913 100644 --- a/libguh/coap/coapoption.h +++ b/libguh/coap/coapoption.h @@ -56,7 +56,6 @@ public: }; CoapOption(); - CoapOption(const Option &option, const QByteArray &data); void setOption(const Option &option); Option option() const; diff --git a/libguh/coap/coappdu.cpp b/libguh/coap/coappdu.cpp index b8841d6a..83380d6b 100644 --- a/libguh/coap/coappdu.cpp +++ b/libguh/coap/coappdu.cpp @@ -356,7 +356,11 @@ void CoapPdu::addOption(const CoapOption::Option &option, const QByteArray &data break; } } - m_options.insert(index + 1, CoapOption(option, data)); + + CoapOption o; + o.setOption(option); + o.setData(data); + m_options.insert(index + 1, o); } /*! Returns the block of this \l{CoapPdu}. */ diff --git a/libguh/coap/coaprequest.cpp b/libguh/coap/coaprequest.cpp index de0454eb..daed9430 100644 --- a/libguh/coap/coaprequest.cpp +++ b/libguh/coap/coaprequest.cpp @@ -75,16 +75,3 @@ CoapPdu::MessageType CoapRequest::messageType() const { return m_messageType; } - -void CoapRequest::setStatusCode(const CoapPdu::StatusCode &statusCode) -{ - m_statusCode = statusCode; -} - -CoapPdu::StatusCode CoapRequest::statusCode() -{ - return m_statusCode; -} - - - diff --git a/libguh/coap/coaprequest.h b/libguh/coap/coaprequest.h index bdc3f649..19b1d94f 100644 --- a/libguh/coap/coaprequest.h +++ b/libguh/coap/coaprequest.h @@ -49,9 +49,6 @@ private: CoapPdu::MessageType m_messageType; CoapPdu::StatusCode m_statusCode; - void setStatusCode(const CoapPdu::StatusCode &statusCode); - CoapPdu::StatusCode statusCode(); - }; #endif // COAPREQUEST_H diff --git a/tests/auto/coap/coaptests.cpp b/tests/auto/coap/coaptests.cpp index 072c1119..ebbe693b 100644 --- a/tests/auto/coap/coaptests.cpp +++ b/tests/auto/coap/coaptests.cpp @@ -74,9 +74,6 @@ void CoapTests::invalidUrl() CoapReply *reply = m_coap->get(request); spy.wait(); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(reply->isFinished(), "Reply not finished."); QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->error(), CoapReply::HostNotFoundError); @@ -85,14 +82,50 @@ void CoapTests::invalidUrl() void CoapTests::invalidScheme() { CoapRequest request(QUrl("http://coap.me")); - qDebug() << request.url().toString(); QSignalSpy spy(m_coap, SIGNAL(replyFinished(CoapReply*))); + + // Get CoapReply *reply = m_coap->get(request); spy.wait(1000); - qDebug() << "===================================="; - qDebug() << reply; + QVERIFY2(reply->isFinished(), "Reply not finished."); + QVERIFY2(spy.count() == 0, "Got a response."); + QCOMPARE(reply->error(), CoapReply::InvalidUrlSchemeError); + + reply->deleteLater(); + + // Post + spy.clear(); + reply = m_coap->post(request); + spy.wait(1000); + + QVERIFY2(reply->isFinished(), "Reply not finished."); + QVERIFY2(spy.count() == 0, "Got a response."); + QCOMPARE(reply->error(), CoapReply::InvalidUrlSchemeError); + + // Put + spy.clear(); + reply = m_coap->put(request); + spy.wait(1000); + + QVERIFY2(reply->isFinished(), "Reply not finished."); + QVERIFY2(spy.count() == 0, "Got a response."); + QCOMPARE(reply->error(), CoapReply::InvalidUrlSchemeError); + + // Delete + spy.clear(); + reply = m_coap->deleteResource(request); + spy.wait(1000); + + QVERIFY2(reply->isFinished(), "Reply not finished."); + QVERIFY2(spy.count() == 0, "Got a response."); + QCOMPARE(reply->error(), CoapReply::InvalidUrlSchemeError); + + // Ping + spy.clear(); + reply = m_coap->ping(request); + spy.wait(1000); QVERIFY2(reply->isFinished(), "Reply not finished."); QVERIFY2(spy.count() == 0, "Got a response."); @@ -101,16 +134,14 @@ void CoapTests::invalidScheme() void CoapTests::ping() { - CoapRequest request(QUrl("coap://coap.me/")); + CoapRequest request; + request.setUrl(QUrl("coap://coap.me/")); qDebug() << request.url().toString(); QSignalSpy spy(m_coap, SIGNAL(replyFinished(CoapReply*))); CoapReply *reply = m_coap->ping(request); spy.wait(); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(reply->isFinished(), "Reply not finished."); QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->statusCode(), CoapPdu::Empty); @@ -122,15 +153,14 @@ void CoapTests::ping() void CoapTests::hello() { CoapRequest request(QUrl("coap://coap.me/hello")); + request.setMessageType(CoapPdu::Confirmable); + qDebug() << request.url().toString(); QSignalSpy spy(m_coap, SIGNAL(replyFinished(CoapReply*))); CoapReply *reply = m_coap->get(request); spy.wait(); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Content); @@ -149,9 +179,6 @@ void CoapTests::broken() CoapReply *reply = m_coap->get(request); spy.wait(); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::InternalServerError); @@ -170,9 +197,6 @@ void CoapTests::query() CoapReply *reply = m_coap->get(request); spy.wait(); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Content); @@ -191,9 +215,6 @@ void CoapTests::subPath() CoapReply *reply = m_coap->get(request); spy.wait(); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Content); @@ -212,9 +233,6 @@ void CoapTests::extendedOptionLength() CoapReply *reply = m_coap->get(request); spy.wait(); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Content); @@ -233,9 +251,6 @@ void CoapTests::specialCharacters() CoapReply *reply = m_coap->get(request); spy.wait(); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Content); @@ -268,9 +283,6 @@ void CoapTests::extendedDelta() CoapReply *reply = m_coap->get(request); spy.wait(); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Content); @@ -289,9 +301,6 @@ void CoapTests::secret() CoapReply *reply = m_coap->get(request); spy.wait(); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Unauthorized); @@ -310,9 +319,6 @@ void CoapTests::separated() CoapReply *reply = m_coap->get(request); spy.wait(10000); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Content); @@ -331,9 +337,6 @@ void CoapTests::deleteResource() CoapReply *reply = m_coap->deleteResource(request); spy.wait(); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Deleted); @@ -346,15 +349,13 @@ void CoapTests::deleteResource() void CoapTests::post() { CoapRequest request(QUrl("coap://coap.me:5683/validate")); + request.setContentType(); qDebug() << request.url().toString(); QSignalSpy spy(m_coap, SIGNAL(replyFinished(CoapReply*))); CoapReply *reply = m_coap->post(request, "guh is awesome"); spy.wait(); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Created); @@ -373,9 +374,6 @@ void CoapTests::put() CoapReply *reply = m_coap->put(request, "guh is awesome"); spy.wait(); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Changed); @@ -419,9 +417,6 @@ void CoapTests::largeDownload() CoapReply *reply = m_coap->get(request); spy.wait(20000); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Content); @@ -442,9 +437,6 @@ void CoapTests::largeCreate() CoapReply *reply = m_coap->post(request, m_uploadData); spy.wait(20000); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Created); @@ -459,9 +451,6 @@ void CoapTests::largeCreate() reply = m_coap->get(request); spy.wait(20000); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Content); @@ -482,9 +471,6 @@ void CoapTests::largeUpdate() CoapReply *reply = m_coap->put(request, m_uploadData); spy.wait(20000); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Changed); @@ -499,9 +485,6 @@ void CoapTests::largeUpdate() reply = m_coap->get(request); spy.wait(20000); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Content); @@ -531,8 +514,6 @@ void CoapTests::multipleCalls() spy.clear(); foreach (CoapReply *reply, replies) { - qDebug() << "===================================="; - qDebug() << reply; QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->error(), CoapReply::NoError); reply->deleteLater(); @@ -550,9 +531,6 @@ void CoapTests::coreLinkParser() CoapReply *reply = m_coap->get(request); spy.wait(); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Content); @@ -562,6 +540,10 @@ void CoapTests::coreLinkParser() CoreLinkParser parser(reply->payload()); QCOMPARE(parser.links().count(), 28); + foreach (const CoreLink &link, parser.links()) { + qDebug() << link; + } + reply->deleteLater(); } @@ -576,9 +558,6 @@ void CoapTests::observeResource() CoapReply *reply = m_coap->enableResourceNotifications(request); spy.wait(); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Content); @@ -603,9 +582,6 @@ void CoapTests::observeLargeResource() CoapReply *reply = m_coap->enableResourceNotifications(request); spy.wait(); - qDebug() << "===================================="; - qDebug() << reply; - QVERIFY2(spy.count() > 0, "Did not get a response."); QCOMPARE(reply->messageType(), CoapPdu::Acknowledgement); QCOMPARE(reply->statusCode(), CoapPdu::Content); @@ -619,6 +595,4 @@ void CoapTests::observeLargeResource() QTest::qWait(5000); } - - QTEST_MAIN(CoapTests)