add vendor requests and server xml tests

pull/135/head
Simon Stürz 2015-12-10 14:26:34 +01:00 committed by Michael Zanetti
parent 45e7e4798e
commit e4d7d0d1c5
4 changed files with 95 additions and 34 deletions

View File

@ -307,10 +307,12 @@ void GuhTestBase::verifyReply(QNetworkReply *reply, const QByteArray &data, cons
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
QCOMPARE(statusCode, expectedStatus); QCOMPARE(statusCode, expectedStatus);
QJsonParseError error; if (!data.isEmpty()) {
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error); QJsonParseError error;
QCOMPARE(error.error, QJsonParseError::NoError); QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
Q_UNUSED(jsonDoc); QCOMPARE(error.error, QJsonParseError::NoError);
Q_UNUSED(jsonDoc);
}
} }
bool GuhTestBase::enableNotifications() bool GuhTestBase::enableNotifications()

View File

@ -43,6 +43,11 @@ class TestRestVendors: public GuhTestBase
private slots: private slots:
void getVendors(); void getVendors();
void invalidMethod();
void invalidPath();
void invalidVendor_data();
void invalidVendor();
}; };
void TestRestVendors::getVendors() void TestRestVendors::getVendors()
@ -63,6 +68,66 @@ void TestRestVendors::getVendors()
} }
} }
void TestRestVendors::invalidMethod()
{
QNetworkAccessManager *nam = new QNetworkAccessManager(this);
QSignalSpy clientSpy(nam, SIGNAL(finished(QNetworkReply*)));
QNetworkRequest request;
request.setUrl(QUrl("http://localhost:3333/api/v1/vendors"));
QNetworkReply *reply = nam->post(request, QByteArray());
clientSpy.wait();
QVERIFY2(clientSpy.count() == 1, "expected exactly 1 response from webserver");
bool ok = false;
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(&ok);
QVERIFY2(ok, "Could not convert statuscode from response to int");
QCOMPARE(statusCode, 400);
reply->deleteLater();
nam->deleteLater();
}
void TestRestVendors::invalidPath()
{
QNetworkAccessManager *nam = new QNetworkAccessManager(this);
QSignalSpy clientSpy(nam, SIGNAL(finished(QNetworkReply*)));
QNetworkRequest request;
request.setUrl(QUrl("http://localhost:3333/api/v1/vendors/" + QUuid::createUuid().toString() + "/" + QUuid::createUuid().toString()));
QNetworkReply *reply = nam->get(request);
clientSpy.wait();
QVERIFY2(clientSpy.count() == 1, "expected exactly 1 response from webserver");
bool ok = false;
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(&ok);
QVERIFY2(ok, "Could not convert statuscode from response to int");
QCOMPARE(statusCode, 501);
reply->deleteLater();
nam->deleteLater();
}
void TestRestVendors::invalidVendor_data()
{
QTest::addColumn<QString>("path");
QTest::addColumn<int>("expectedStatusCode");
QTest::newRow("invalid VendorId") << QUuid::createUuid().toString() << 404;
QTest::newRow("invalid VendorId format") << "uuid" << 400;
}
void TestRestVendors::invalidVendor()
{
QFETCH(QString, path);
QFETCH(int, expectedStatusCode);
QNetworkRequest request(QUrl("http://localhost:3333/api/v1/vendors/" + path));
QVariant response = getAndWait(request, expectedStatusCode);
QCOMPARE(JsonTypes::deviceErrorToString(DeviceManager::DeviceErrorVendorNotFound), response.toMap().value("error").toString());
}
#include "testrestvendors.moc" #include "testrestvendors.moc"
QTEST_MAIN(TestRestVendors) QTEST_MAIN(TestRestVendors)

View File

@ -33,6 +33,7 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QMetaType> #include <QMetaType>
#include <QByteArray> #include <QByteArray>
#include <QXmlReader>
using namespace guhserver; using namespace guhserver;
@ -57,8 +58,7 @@ private slots:
void getFiles_data(); void getFiles_data();
void getFiles(); void getFiles();
void upnpDiscovery(); void getServerDescription();
}; };
void TestWebserver::httpVersion() void TestWebserver::httpVersion()
@ -99,7 +99,6 @@ void TestWebserver::httpVersion()
void TestWebserver::multiPackageMessage() void TestWebserver::multiPackageMessage()
{ {
QTcpSocket *socket = new QTcpSocket(this); QTcpSocket *socket = new QTcpSocket(this);
socket->connectToHost(QHostAddress("127.0.0.1"), 3333); socket->connectToHost(QHostAddress("127.0.0.1"), 3333);
bool connected = socket->waitForConnected(1000); bool connected = socket->waitForConnected(1000);
@ -208,6 +207,7 @@ void TestWebserver::checkAllowedMethodCall()
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), expectedStatusCode); QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), expectedStatusCode);
QVERIFY2(reply->hasRawHeader("Allow"), "405 should contain the allowed methods header"); QVERIFY2(reply->hasRawHeader("Allow"), "405 should contain the allowed methods header");
} }
reply->deleteLater(); reply->deleteLater();
nam->deleteLater(); nam->deleteLater();
} }
@ -310,6 +310,7 @@ void TestWebserver::getOptions()
QCOMPARE(statusCode, 200); QCOMPARE(statusCode, 200);
reply->deleteLater(); reply->deleteLater();
nam->deleteLater();
} }
void TestWebserver::getFiles_data() void TestWebserver::getFiles_data()
@ -318,6 +319,10 @@ void TestWebserver::getFiles_data()
QTest::addColumn<int>("expectedStatusCode"); QTest::addColumn<int>("expectedStatusCode");
QTest::newRow("get /etc/passwd") << "/etc/passwd" << 404; QTest::newRow("get /etc/passwd") << "/etc/passwd" << 404;
QTest::newRow("get /blub/blub/blabla") << "/etc/passwd" << 404;
QTest::newRow("get /../../etc/passwd") << "/../../etc/passwd" << 404;
QTest::newRow("get /../../") << "/../../" << 403;
QTest::newRow("get /../") << "/../" << 403;
QTest::newRow("get /etc/guh/guhd.conf") << "/etc/guh/guhd.conf" << 404; QTest::newRow("get /etc/guh/guhd.conf") << "/etc/guh/guhd.conf" << 404;
QTest::newRow("get /etc/sudoers") << "/etc/sudoers" << 404; QTest::newRow("get /etc/sudoers") << "/etc/sudoers" << 404;
QTest::newRow("get /root/.ssh/id_rsa.pub") << "/root/.ssh/id_rsa.pub" << 404; QTest::newRow("get /root/.ssh/id_rsa.pub") << "/root/.ssh/id_rsa.pub" << 404;
@ -344,40 +349,27 @@ void TestWebserver::getFiles()
QCOMPARE(statusCode, expectedStatusCode); QCOMPARE(statusCode, expectedStatusCode);
reply->deleteLater(); reply->deleteLater();
nam->deleteLater();
} }
void TestWebserver::upnpDiscovery() void TestWebserver::getServerDescription()
{ {
QUdpSocket *socket = new QUdpSocket(this); QNetworkAccessManager *nam = new QNetworkAccessManager(this);
socket->setSocketOption(QAbstractSocket::MulticastTtlOption,QVariant(1)); QSignalSpy clientSpy(nam, SIGNAL(finished(QNetworkReply*)));
socket->setSocketOption(QAbstractSocket::MulticastLoopbackOption,QVariant(1));
QHostAddress host = QHostAddress("239.255.255.250"); QNetworkRequest request;
request.setUrl(QUrl("http://localhost:3333/server.xml"));
QNetworkReply *reply = nam->get(request);
QVERIFY(socket->bind(QHostAddress::AnyIPv4, 1900, QUdpSocket::ShareAddress)); clientSpy.wait();
QVERIFY(socket->joinMulticastGroup(host)); QVERIFY2(clientSpy.count() == 1, "expected exactly 1 response from webserver");
//QSignalSpy spy(socket, SIGNAL(readyRead())); QXmlSimpleReader xmlReader; QXmlInputSource xmlSource;
xmlSource.setData(reply->readAll());
QVERIFY(xmlReader.parse(xmlSource));
QByteArray ssdpSearchMessage = QByteArray("M-SEARCH * HTTP/1.1\r\n" reply->deleteLater();
"HOST:239.255.255.250:1900\r\n" nam->deleteLater();
"MAN:\"ssdp:discover\"\r\n"
"MX:4\r\n"
"ST: ssdp:all\r\n\r\n");
socket->writeDatagram(ssdpSearchMessage, host, 1900);
socket->waitForBytesWritten();
socket->waitForReadyRead(2000);
qDebug() << socket->readAll();
//QVERIFY(spy.count() > 0);
// discovery->deleteLater();
} }
#include "testwebserver.moc" #include "testwebserver.moc"

View File

@ -1,5 +1,7 @@
include(../../../guh.pri) include(../../../guh.pri)
include(../autotests.pri) include(../autotests.pri)
QT += xml
TARGET = webserver TARGET = webserver
SOURCES += testwebserver.cpp SOURCES += testwebserver.cpp