aheinecke@43: #include "downloadertest.h" aheinecke@43: #include "downloader.h" aheinecke@43: aheinecke@43: #include aheinecke@43: #include aheinecke@48: #include aheinecke@43: #include "unistd.h" aheinecke@43: aheinecke@43: void DownloaderTest::startServer(QString additionalOptions) aheinecke@43: { aheinecke@43: QFile serverConfig(serverConfigDir.path() + "/" + "hiawatha.conf"); aheinecke@43: QFile mimeConfig(serverConfigDir.path() + "/" + "mimetype.conf"); aheinecke@43: if (serverProc.state() == QProcess::Running) { aheinecke@43: serverProc.close(); aheinecke@43: } aheinecke@43: serverConfig.open(QIODevice::WriteOnly); aheinecke@43: mimeConfig.open(QIODevice::WriteOnly); aheinecke@43: mimeConfig.close(); /* just an empty file */ aheinecke@43: aheinecke@43: QTextStream configStream(&serverConfig); aheinecke@43: qDebug() << "Config file name: " << serverConfig.fileName(); aheinecke@43: configStream << aheinecke@43: "Binding { " << endl << aheinecke@43: "Port = 44443 " << endl << aheinecke@43: "SSLcertFile = " << SOURCE_DIR << aheinecke@43: "/data/valid_ssl_bp_priv.pem" << endl << aheinecke@43: "Interface = 127.0.0.1" << endl << aheinecke@43: "}" << endl << aheinecke@43: "Hostname = 127.0.0.1" << endl << aheinecke@46: "WebsiteRoot = " << SOURCE_DIR << "/data" << endl; aheinecke@43: configStream.flush(); aheinecke@43: aheinecke@43: serverConfig.close(); aheinecke@43: serverProc.start(); aheinecke@43: serverProc.waitForStarted(); aheinecke@43: } aheinecke@43: aheinecke@43: void DownloaderTest::initTestCase() { aheinecke@43: QStringList arguments; aheinecke@43: serverProc.setProgram(HIAWATHA_EXECUTABLE); aheinecke@43: arguments << "-d" << "-c" << serverConfigDir.path(); aheinecke@43: serverProc.setArguments(arguments); aheinecke@45: qRegisterMetaType("SSLConnection::ErrorCode"); aheinecke@43: startServer(); aheinecke@43: QTest::qWait(1000); /* Wait for the server to settle */ aheinecke@43: } aheinecke@43: aheinecke@45: void DownloaderTest::downloaderError(const QString &message, SSLConnection::ErrorCode error) aheinecke@43: { aheinecke@43: qDebug() << "Downloader Error: " << error << " Msg: " << message; aheinecke@43: } aheinecke@43: aheinecke@47: bool filesEqual(QString filename1, QString filename2) aheinecke@47: { aheinecke@47: bool retval = false; aheinecke@47: QFile file1(filename1); aheinecke@47: QFile file2(filename2); aheinecke@47: file1.open(QIODevice::ReadOnly); aheinecke@47: file2.open(QIODevice::ReadOnly); aheinecke@47: aheinecke@47: retval = (file1.readAll() == file2.readAll()); aheinecke@47: file1.close(); aheinecke@47: file2.close(); aheinecke@47: return retval; aheinecke@47: } aheinecke@47: aheinecke@48: void DownloaderTest::testNoConnection() aheinecke@48: { aheinecke@48: Downloader* downloader = new Downloader(this, aheinecke@48: QString::fromLatin1("https://foobar.intevation.de")); aheinecke@48: QSignalSpy newListAvailable(downloader, aheinecke@48: SIGNAL(newListAvailable(const QString&, const QDateTime&))); aheinecke@48: QSignalSpy newSoftwareAvailable(downloader, aheinecke@48: SIGNAL(newSoftwareAvailable(const QString&, const QDateTime&))); aheinecke@48: QSignalSpy errors(downloader, SIGNAL(error(const QString &, aheinecke@48: SSLConnection::ErrorCode))); aheinecke@48: downloader->start(); aheinecke@48: aheinecke@48: int waited = 0; aheinecke@48: while (newListAvailable.count() == 0 && aheinecke@48: errors.count() == 0 && waited < 20) { aheinecke@48: QTest::qWait(200); aheinecke@48: waited++; aheinecke@48: } aheinecke@48: QVERIFY(waited != 20); aheinecke@48: aheinecke@48: QList arguments = errors.takeFirst(); aheinecke@48: aheinecke@48: int error = arguments.at(1).toInt(); aheinecke@48: aheinecke@48: QVERIFY(error == SSLConnection::NoConnection); aheinecke@48: } aheinecke@48: aheinecke@43: void DownloaderTest::testNewList() aheinecke@43: { aheinecke@43: QFile validCert(QString::fromLatin1(SOURCE_DIR) + aheinecke@43: "/data/valid_ssl_bp.pem"); aheinecke@43: validCert.open(QIODevice::ReadOnly); aheinecke@43: aheinecke@43: Downloader* downloader = new Downloader(this, aheinecke@43: QString::fromLatin1("https://localhost:44443"), aheinecke@43: validCert.readAll(), aheinecke@43: QDateTime::currentDateTime(), // Last installed SW aheinecke@43: QDateTime::fromString("2010", "YYYY"), aheinecke@50: "/garbage_2MB", aheinecke@46: "/list-valid.txt"); aheinecke@43: validCert.close(); aheinecke@43: aheinecke@43: QSignalSpy newListAvailable(downloader, aheinecke@43: SIGNAL(newListAvailable(const QString&, const QDateTime&))); aheinecke@43: QSignalSpy newSoftwareAvailable(downloader, aheinecke@43: SIGNAL(newSoftwareAvailable(const QString&, const QDateTime&))); aheinecke@43: QSignalSpy errors(downloader, SIGNAL(error(const QString &, aheinecke@45: SSLConnection::ErrorCode))); aheinecke@45: connect(downloader, SIGNAL(error(const QString &, SSLConnection::ErrorCode)), aheinecke@45: this, SLOT(downloaderError(const QString &, SSLConnection::ErrorCode))); aheinecke@43: aheinecke@43: downloader->start(); aheinecke@43: aheinecke@43: int waited = 0; aheinecke@43: while (newListAvailable.count() == 0 && aheinecke@43: errors.count() == 0 && waited < 20) { aheinecke@48: QTest::qWait(200); aheinecke@43: waited++; aheinecke@43: } aheinecke@43: QVERIFY(waited != 20); aheinecke@43: QVERIFY(newListAvailable.count() == 1); aheinecke@43: QVERIFY(newSoftwareAvailable.count() == 0); aheinecke@43: QVERIFY(errors.count() == 0); aheinecke@47: aheinecke@47: QList arguments = newListAvailable.takeFirst(); aheinecke@47: aheinecke@47: QVERIFY(filesEqual(QString::fromLatin1(SOURCE_DIR) + "/data/list-valid.txt", aheinecke@47: arguments.at(0).toString())); aheinecke@47: aheinecke@43: delete downloader; aheinecke@43: } aheinecke@43: aheinecke@43: void DownloaderTest::cleanupTestCase() { aheinecke@43: if (serverProc.state() == QProcess::Running) { aheinecke@43: serverProc.close(); aheinecke@43: } aheinecke@43: } aheinecke@43: aheinecke@43: QTEST_GUILESS_MAIN (DownloaderTest); aheinecke@43: