annotate ui/tests/downloadertest.cpp @ 49:c389915fd55e

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

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