comparison ui/tests/downloadertest.cpp @ 43:5910bf9016cd

Add Downloadertest
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 14 Mar 2014 15:25:47 +0000
parents
children c6125d73faf4
comparison
equal deleted inserted replaced
42:6e7ef7e95031 43:5910bf9016cd
1 #include "downloadertest.h"
2 #include "downloader.h"
3
4 #include <QTextStream>
5 #include <QFile>
6 #include "unistd.h"
7
8 void DownloaderTest::startServer(QString additionalOptions)
9 {
10 QFile serverConfig(serverConfigDir.path() + "/" + "hiawatha.conf");
11 QFile mimeConfig(serverConfigDir.path() + "/" + "mimetype.conf");
12 if (serverProc.state() == QProcess::Running) {
13 serverProc.close();
14 }
15 serverConfig.open(QIODevice::WriteOnly);
16 mimeConfig.open(QIODevice::WriteOnly);
17 mimeConfig.close(); /* just an empty file */
18
19 QTextStream configStream(&serverConfig);
20 qDebug() << "Config file name: " << serverConfig.fileName();
21 configStream <<
22 "Binding { " << endl <<
23 "Port = 44443 " << endl <<
24 "SSLcertFile = " << SOURCE_DIR <<
25 "/data/valid_ssl_bp_priv.pem" << endl <<
26 "Interface = 127.0.0.1" << endl <<
27 "}" << endl <<
28 "Hostname = 127.0.0.1" << endl <<
29 "WebsiteRoot = " << SOURCE_DIR << endl;
30 configStream.flush();
31
32 serverConfig.close();
33 serverProc.start();
34 serverProc.waitForStarted();
35 }
36
37 void DownloaderTest::initTestCase() {
38 QStringList arguments;
39 serverProc.setProgram(HIAWATHA_EXECUTABLE);
40 arguments << "-d" << "-c" << serverConfigDir.path();
41 serverProc.setArguments(arguments);
42 qRegisterMetaType<Downloader::ErrorCode>("Downloader::ErrorCode");
43 startServer();
44 QTest::qWait(1000); /* Wait for the server to settle */
45 }
46
47 void DownloaderTest::downloaderError(const QString &message, Downloader::ErrorCode error)
48 {
49 qDebug() << "Downloader Error: " << error << " Msg: " << message;
50 }
51
52 void DownloaderTest::testNewList()
53 {
54 QFile validCert(QString::fromLatin1(SOURCE_DIR) +
55 "/data/valid_ssl_bp.pem");
56 validCert.open(QIODevice::ReadOnly);
57
58 Downloader* downloader = new Downloader(this,
59 QString::fromLatin1("https://localhost:44443"),
60 validCert.readAll(),
61 QDateTime::currentDateTime(), // Last installed SW
62 QDateTime::fromString("2010", "YYYY"),
63 "/list-valid.txt",
64 "/random_2MB");
65 validCert.close();
66
67 QSignalSpy newListAvailable(downloader,
68 SIGNAL(newListAvailable(const QString&, const QDateTime&)));
69 QSignalSpy newSoftwareAvailable(downloader,
70 SIGNAL(newSoftwareAvailable(const QString&, const QDateTime&)));
71 QSignalSpy errors(downloader, SIGNAL(error(const QString &,
72 Downloader::ErrorCode)));
73 connect(downloader, SIGNAL(error(const QString &, Downloader::ErrorCode)),
74 this, SLOT(downloaderError(const QString &, Downloader::ErrorCode)));
75
76 downloader->start();
77
78 int waited = 0;
79 while (newListAvailable.count() == 0 &&
80 errors.count() == 0 && waited < 20) {
81 QTest::qWait(200);
82 waited++;
83 }
84 QVERIFY(waited != 20);
85 QVERIFY(newListAvailable.count() == 1);
86 QVERIFY(newSoftwareAvailable.count() == 0);
87 QVERIFY(errors.count() == 0);
88 delete downloader;
89 }
90
91 void DownloaderTest::cleanupTestCase() {
92 if (serverProc.state() == QProcess::Running) {
93 serverProc.close();
94 }
95 }
96
97 QTEST_GUILESS_MAIN (DownloaderTest);
98

http://wald.intevation.org/projects/trustbridge/