aheinecke@43: #include "downloadertest.h" aheinecke@43: #include "downloader.h" aheinecke@43: aheinecke@43: #include aheinecke@43: #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@43: "WebsiteRoot = " << SOURCE_DIR << 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@43: qRegisterMetaType("Downloader::ErrorCode"); aheinecke@43: startServer(); aheinecke@43: QTest::qWait(1000); /* Wait for the server to settle */ aheinecke@43: } aheinecke@43: aheinecke@43: void DownloaderTest::downloaderError(const QString &message, Downloader::ErrorCode error) aheinecke@43: { aheinecke@43: qDebug() << "Downloader Error: " << error << " Msg: " << message; aheinecke@43: } aheinecke@43: 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@43: "/list-valid.txt", aheinecke@43: "/random_2MB"); 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@43: Downloader::ErrorCode))); aheinecke@43: connect(downloader, SIGNAL(error(const QString &, Downloader::ErrorCode)), aheinecke@43: this, SLOT(downloaderError(const QString &, Downloader::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@43: 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@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: