Mercurial > trustbridge
annotate ui/tests/downloadertest.cpp @ 47:7e37c6033c81
Verify that the downloaded file matches the served one
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Mon, 17 Mar 2014 11:03:17 +0000 |
parents | d28e2624c1d5 |
children | 3f8c2d46ded6 |
rev | line source |
---|---|
43 | 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 << | |
46
d28e2624c1d5
Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents:
45
diff
changeset
|
29 "WebsiteRoot = " << SOURCE_DIR << "/data" << endl; |
43 | 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); | |
45
c6125d73faf4
Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents:
43
diff
changeset
|
42 qRegisterMetaType<SSLConnection::ErrorCode>("SSLConnection::ErrorCode"); |
43 | 43 startServer(); |
44 QTest::qWait(1000); /* Wait for the server to settle */ | |
45 } | |
46 | |
45
c6125d73faf4
Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents:
43
diff
changeset
|
47 void DownloaderTest::downloaderError(const QString &message, SSLConnection::ErrorCode error) |
43 | 48 { |
49 qDebug() << "Downloader Error: " << error << " Msg: " << message; | |
50 } | |
51 | |
47
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
52 bool filesEqual(QString filename1, QString filename2) |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
53 { |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
54 bool retval = false; |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
55 QFile file1(filename1); |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
56 QFile file2(filename2); |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
57 file1.open(QIODevice::ReadOnly); |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
58 file2.open(QIODevice::ReadOnly); |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
59 |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
60 retval = (file1.readAll() == file2.readAll()); |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
61 file1.close(); |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
62 file2.close(); |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
63 return retval; |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
64 } |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
65 |
43 | 66 void DownloaderTest::testNewList() |
67 { | |
68 QFile validCert(QString::fromLatin1(SOURCE_DIR) + | |
69 "/data/valid_ssl_bp.pem"); | |
70 validCert.open(QIODevice::ReadOnly); | |
71 | |
72 Downloader* downloader = new Downloader(this, | |
73 QString::fromLatin1("https://localhost:44443"), | |
74 validCert.readAll(), | |
75 QDateTime::currentDateTime(), // Last installed SW | |
76 QDateTime::fromString("2010", "YYYY"), | |
46
d28e2624c1d5
Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents:
45
diff
changeset
|
77 "/random_2MB", |
d28e2624c1d5
Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents:
45
diff
changeset
|
78 "/list-valid.txt"); |
43 | 79 validCert.close(); |
80 | |
81 QSignalSpy newListAvailable(downloader, | |
82 SIGNAL(newListAvailable(const QString&, const QDateTime&))); | |
83 QSignalSpy newSoftwareAvailable(downloader, | |
84 SIGNAL(newSoftwareAvailable(const QString&, const QDateTime&))); | |
85 QSignalSpy errors(downloader, SIGNAL(error(const QString &, | |
45
c6125d73faf4
Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents:
43
diff
changeset
|
86 SSLConnection::ErrorCode))); |
c6125d73faf4
Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents:
43
diff
changeset
|
87 connect(downloader, SIGNAL(error(const QString &, SSLConnection::ErrorCode)), |
c6125d73faf4
Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents:
43
diff
changeset
|
88 this, SLOT(downloaderError(const QString &, SSLConnection::ErrorCode))); |
43 | 89 |
90 downloader->start(); | |
91 | |
92 int waited = 0; | |
93 while (newListAvailable.count() == 0 && | |
94 errors.count() == 0 && waited < 20) { | |
46
d28e2624c1d5
Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents:
45
diff
changeset
|
95 QTest::qWait(2000); |
43 | 96 waited++; |
97 } | |
98 QVERIFY(waited != 20); | |
99 QVERIFY(newListAvailable.count() == 1); | |
100 QVERIFY(newSoftwareAvailable.count() == 0); | |
101 QVERIFY(errors.count() == 0); | |
47
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
102 |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
103 QList<QVariant> arguments = newListAvailable.takeFirst(); |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
104 |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
105 QVERIFY(filesEqual(QString::fromLatin1(SOURCE_DIR) + "/data/list-valid.txt", |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
106 arguments.at(0).toString())); |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
107 |
43 | 108 delete downloader; |
109 } | |
110 | |
111 void DownloaderTest::cleanupTestCase() { | |
112 if (serverProc.state() == QProcess::Running) { | |
113 serverProc.close(); | |
114 } | |
115 } | |
116 | |
117 QTEST_GUILESS_MAIN (DownloaderTest); | |
118 |