Mercurial > trustbridge
annotate ui/tests/downloadertest.cpp @ 51:78633b2b580c
Add macro for common signal setup tasks
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Mon, 17 Mar 2014 11:38:13 +0000 |
parents | a7b8cb29f1f6 |
children | d73a2f0170d4 |
rev | line source |
---|---|
43 | 1 #include "downloadertest.h" |
2 #include "downloader.h" | |
3 | |
4 #include <QTextStream> | |
5 #include <QFile> | |
48
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
6 #include <QVariant> |
43 | 7 #include "unistd.h" |
8 | |
51
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
9 #define MAX_WAIT 20 |
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
10 |
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
11 #define SETUP_SPY \ |
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
12 QSignalSpy newListAvailable(downloader, \ |
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
13 SIGNAL(newListAvailable(const QString&, const QDateTime&))); \ |
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
14 QSignalSpy newSoftwareAvailable(downloader, \ |
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
15 SIGNAL(newSoftwareAvailable(const QString&, const QDateTime&))); \ |
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
16 QSignalSpy errors(downloader, SIGNAL(error(const QString &, \ |
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
17 SSLConnection::ErrorCode))); \ |
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
18 |
43 | 19 void DownloaderTest::startServer(QString additionalOptions) |
20 { | |
21 QFile serverConfig(serverConfigDir.path() + "/" + "hiawatha.conf"); | |
22 QFile mimeConfig(serverConfigDir.path() + "/" + "mimetype.conf"); | |
23 if (serverProc.state() == QProcess::Running) { | |
24 serverProc.close(); | |
25 } | |
26 serverConfig.open(QIODevice::WriteOnly); | |
27 mimeConfig.open(QIODevice::WriteOnly); | |
28 mimeConfig.close(); /* just an empty file */ | |
29 | |
30 QTextStream configStream(&serverConfig); | |
31 qDebug() << "Config file name: " << serverConfig.fileName(); | |
32 configStream << | |
33 "Binding { " << endl << | |
34 "Port = 44443 " << endl << | |
35 "SSLcertFile = " << SOURCE_DIR << | |
36 "/data/valid_ssl_bp_priv.pem" << endl << | |
37 "Interface = 127.0.0.1" << endl << | |
38 "}" << endl << | |
39 "Hostname = 127.0.0.1" << endl << | |
46
d28e2624c1d5
Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents:
45
diff
changeset
|
40 "WebsiteRoot = " << SOURCE_DIR << "/data" << endl; |
43 | 41 configStream.flush(); |
42 | |
43 serverConfig.close(); | |
44 serverProc.start(); | |
45 serverProc.waitForStarted(); | |
46 } | |
47 | |
48 void DownloaderTest::initTestCase() { | |
49 QStringList arguments; | |
50 serverProc.setProgram(HIAWATHA_EXECUTABLE); | |
51 arguments << "-d" << "-c" << serverConfigDir.path(); | |
52 serverProc.setArguments(arguments); | |
45
c6125d73faf4
Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents:
43
diff
changeset
|
53 qRegisterMetaType<SSLConnection::ErrorCode>("SSLConnection::ErrorCode"); |
43 | 54 startServer(); |
55 QTest::qWait(1000); /* Wait for the server to settle */ | |
56 } | |
57 | |
45
c6125d73faf4
Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents:
43
diff
changeset
|
58 void DownloaderTest::downloaderError(const QString &message, SSLConnection::ErrorCode error) |
43 | 59 { |
60 qDebug() << "Downloader Error: " << error << " Msg: " << message; | |
61 } | |
62 | |
47
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
63 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
|
64 { |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
65 bool retval = false; |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
66 QFile file1(filename1); |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
67 QFile file2(filename2); |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
68 file1.open(QIODevice::ReadOnly); |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
69 file2.open(QIODevice::ReadOnly); |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
70 |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
71 retval = (file1.readAll() == file2.readAll()); |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
72 file1.close(); |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
73 file2.close(); |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
74 return retval; |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
75 } |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
76 |
48
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
77 void DownloaderTest::testNoConnection() |
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
78 { |
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
79 Downloader* downloader = new Downloader(this, |
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
80 QString::fromLatin1("https://foobar.intevation.de")); |
51
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
81 |
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
82 SETUP_SPY |
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
83 |
48
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
84 downloader->start(); |
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
85 |
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
86 int waited = 0; |
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
87 while (newListAvailable.count() == 0 && |
51
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
88 errors.count() == 0 && waited < MAX_WAIT) { |
48
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
89 QTest::qWait(200); |
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
90 waited++; |
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
91 } |
51
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
92 QVERIFY(waited != MAX_WAIT); |
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
93 QVERIFY(newListAvailable.count() == 0); |
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
94 QVERIFY(newSoftwareAvailable.count() == 0); |
48
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
95 |
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
96 QList<QVariant> arguments = errors.takeFirst(); |
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
97 |
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
98 int error = arguments.at(1).toInt(); |
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
99 |
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
100 QVERIFY(error == SSLConnection::NoConnection); |
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
101 } |
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
102 |
43 | 103 void DownloaderTest::testNewList() |
104 { | |
105 QFile validCert(QString::fromLatin1(SOURCE_DIR) + | |
106 "/data/valid_ssl_bp.pem"); | |
107 validCert.open(QIODevice::ReadOnly); | |
108 | |
109 Downloader* downloader = new Downloader(this, | |
110 QString::fromLatin1("https://localhost:44443"), | |
111 validCert.readAll(), | |
112 QDateTime::currentDateTime(), // Last installed SW | |
113 QDateTime::fromString("2010", "YYYY"), | |
50
a7b8cb29f1f6
Rename random files to garbage to clarify what they contain
Andre Heinecke <aheinecke@intevation.de>
parents:
48
diff
changeset
|
114 "/garbage_2MB", |
46
d28e2624c1d5
Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents:
45
diff
changeset
|
115 "/list-valid.txt"); |
43 | 116 validCert.close(); |
117 | |
51
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
118 SETUP_SPY |
78633b2b580c
Add macro for common signal setup tasks
Andre Heinecke <aheinecke@intevation.de>
parents:
50
diff
changeset
|
119 |
45
c6125d73faf4
Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents:
43
diff
changeset
|
120 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
|
121 this, SLOT(downloaderError(const QString &, SSLConnection::ErrorCode))); |
43 | 122 |
123 downloader->start(); | |
124 | |
125 int waited = 0; | |
126 while (newListAvailable.count() == 0 && | |
127 errors.count() == 0 && waited < 20) { | |
48
3f8c2d46ded6
Add test for no connection handling
Andre Heinecke <aheinecke@intevation.de>
parents:
47
diff
changeset
|
128 QTest::qWait(200); |
43 | 129 waited++; |
130 } | |
131 QVERIFY(waited != 20); | |
132 QVERIFY(newListAvailable.count() == 1); | |
133 QVERIFY(newSoftwareAvailable.count() == 0); | |
134 QVERIFY(errors.count() == 0); | |
47
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
135 |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
136 QList<QVariant> arguments = newListAvailable.takeFirst(); |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
137 |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
138 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
|
139 arguments.at(0).toString())); |
7e37c6033c81
Verify that the downloaded file matches the served one
Andre Heinecke <aheinecke@intevation.de>
parents:
46
diff
changeset
|
140 |
43 | 141 delete downloader; |
142 } | |
143 | |
144 void DownloaderTest::cleanupTestCase() { | |
145 if (serverProc.state() == QProcess::Running) { | |
146 serverProc.close(); | |
147 } | |
148 } | |
149 | |
150 QTEST_GUILESS_MAIN (DownloaderTest); | |
151 |