summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Riebeling <Dominik.Riebeling@gmail.com>2015-12-18 23:15:13 +0100
committerDominik Riebeling <Dominik.Riebeling@gmail.com>2015-12-18 23:42:18 +0100
commit7e7fd0c7b85f85724f7d81d9185f863f1e712400 (patch)
tree58e0b4c7751557c05cf8797cded0cd0c09676c38
parent79d513dd7e0a1e5b8a9e7d821ef575b3e7f6f173 (diff)
downloadrockbox-7e7fd0c7b85f85724f7d81d9185f863f1e712400.tar.gz
rockbox-7e7fd0c7b85f85724f7d81d9185f863f1e712400.zip
Use random port for HttpGet unit tests.
Make local HttpDaemon for testing use a random unused port instead of a fixed one. Avoids possible issues with the port chosen already being used on the local machine. Change-Id: I1ca10b7e5ce198350e14321afc50c36d78b1c0b4
-rw-r--r--rbutil/rbutilqt/test/test-httpget.cpp55
1 files changed, 31 insertions, 24 deletions
diff --git a/rbutil/rbutilqt/test/test-httpget.cpp b/rbutil/rbutilqt/test/test-httpget.cpp
index a062203e70..c6f5abf9fc 100644
--- a/rbutil/rbutilqt/test/test-httpget.cpp
+++ b/rbutil/rbutilqt/test/test-httpget.cpp
@@ -32,11 +32,13 @@
32{ 32{
33 Q_OBJECT 33 Q_OBJECT
34 public: 34 public:
35 HttpDaemon(quint16 port, QObject* parent = 0) : QTcpServer(parent) 35 HttpDaemon(quint16 port = 0, QObject* parent = 0) : QTcpServer(parent)
36 { 36 {
37 listen(QHostAddress::Any, port); 37 listen(QHostAddress::Any, port);
38 } 38 }
39 39
40 quint16 port(void) { return this->serverPort(); }
41
40#if QT_VERSION < 0x050000 42#if QT_VERSION < 0x050000
41 void incomingConnection(int socket) 43 void incomingConnection(int socket)
42#else 44#else
@@ -133,7 +135,10 @@ class TestHttpGet : public QObject
133 void cleanup(void); 135 void cleanup(void);
134 136
135 public slots: 137 public slots:
136 void waitTimeout(void) { m_waitTimeoutOccured = true; } 138 void waitTimeout(void)
139 {
140 m_waitTimeoutOccured = true;
141 }
137 QDir temporaryFolder(void) 142 QDir temporaryFolder(void)
138 { 143 {
139 // Qt unfortunately doesn't support creating temporary folders so 144 // Qt unfortunately doesn't support creating temporary folders so
@@ -160,19 +165,21 @@ class TestHttpGet : public QObject
160 } 165 }
161 private: 166 private:
162 HttpDaemon *m_daemon; 167 HttpDaemon *m_daemon;
168 QByteArray m_port;
163 bool m_waitTimeoutOccured; 169 bool m_waitTimeoutOccured;
164 QString m_now; 170 QString m_now;
165 QDir m_cachedir; 171 QDir m_cachedir;
166 HttpGet *m_getter; 172 HttpGet *m_getter = NULL;
167 QSignalSpy *m_doneSpy; 173 QSignalSpy *m_doneSpy = NULL;
168}; 174};
169 175
170 176
171void TestHttpGet::init(void) 177void TestHttpGet::init(void)
172{ 178{
173 m_now = QDateTime::currentDateTime().toString("ddd, d MMM yyyy hh:mm:ss"); 179 m_now = QDateTime::currentDateTime().toString("ddd, d MMM yyyy hh:mm:ss");
174 m_daemon = new HttpDaemon(8080, this); 180 m_daemon = new HttpDaemon(0, this); // use port 0 to auto-pick
175 m_daemon->reset(); 181 m_daemon->reset();
182 m_port = QString("%1").arg(m_daemon->port()).toLatin1();
176 m_cachedir = temporaryFolder(); 183 m_cachedir = temporaryFolder();
177 m_getter = new HttpGet(this); 184 m_getter = new HttpGet(this);
178 m_doneSpy = new QSignalSpy(m_getter, SIGNAL(done(bool))); 185 m_doneSpy = new QSignalSpy(m_getter, SIGNAL(done(bool)));
@@ -183,10 +190,10 @@ void TestHttpGet::cleanup(void)
183{ 190{
184 rmTree(m_cachedir.absolutePath()); 191 rmTree(m_cachedir.absolutePath());
185 if(m_getter) { 192 if(m_getter) {
186 m_getter->abort(); delete m_getter; 193 m_getter->abort(); delete m_getter; m_getter = NULL;
187 } 194 }
188 if(m_daemon) delete m_daemon; 195 if(m_daemon) { delete m_daemon; m_daemon = NULL; }
189 if(m_doneSpy) delete m_doneSpy; 196 if(m_doneSpy) { delete m_doneSpy; m_doneSpy = NULL; }
190} 197}
191 198
192void TestHttpGet::testFileUrlRequest(void) 199void TestHttpGet::testFileUrlRequest(void)
@@ -197,7 +204,7 @@ void TestHttpGet::testFileUrlRequest(void)
197 QTemporaryFile datafile; 204 QTemporaryFile datafile;
198 datafile.open(); 205 datafile.open();
199 datafile.write(teststring.toLatin1()); 206 datafile.write(teststring.toLatin1());
200 m_getter->getFile("file://" + datafile.fileName()); 207 m_getter->getFile(QUrl("file://" + datafile.fileName()));
201 datafile.close(); 208 datafile.close();
202 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false) 209 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
203 QCoreApplication::processEvents(); 210 QCoreApplication::processEvents();
@@ -230,7 +237,7 @@ void TestHttpGet::testUncachedRepeatedRequest(void)
230 237
231 QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void))); 238 QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));
232 239
233 m_getter->getFile(QUrl("http://localhost:8080/test1.txt")); 240 m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.txt"));
234 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false) 241 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
235 QCoreApplication::processEvents(); 242 QCoreApplication::processEvents();
236 243
@@ -240,7 +247,7 @@ void TestHttpGet::testUncachedRepeatedRequest(void)
240 QCOMPARE(m_daemon->lastRequestData().at(0).startsWith("GET"), true); 247 QCOMPARE(m_daemon->lastRequestData().at(0).startsWith("GET"), true);
241 248
242 // request second time 249 // request second time
243 m_getter->getFile(QUrl("http://localhost:8080/test1.txt")); 250 m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.txt"));
244 while(m_doneSpy->count() < 2 && m_waitTimeoutOccured == false) 251 while(m_doneSpy->count() < 2 && m_waitTimeoutOccured == false)
245 QCoreApplication::processEvents(); 252 QCoreApplication::processEvents();
246 QCOMPARE(m_doneSpy->count(), 2); 253 QCOMPARE(m_doneSpy->count(), 2);
@@ -259,7 +266,7 @@ void TestHttpGet::testCachedRequest(void)
259 QList<QByteArray> responses; 266 QList<QByteArray> responses;
260 responses << QByteArray( 267 responses << QByteArray(
261 "HTTP/1.1 302 Found\r\n" 268 "HTTP/1.1 302 Found\r\n"
262 "Location: http://localhost:8080/test2.txt\r\n" 269 "Location: http://localhost:" + m_port + "/test2.txt\r\n"
263 "Date: " + m_now.toLatin1() + "\r\n" 270 "Date: " + m_now.toLatin1() + "\r\n"
264 "Last-Modified: " + m_now.toLatin1() + "\r\n" 271 "Last-Modified: " + m_now.toLatin1() + "\r\n"
265 "\r\n"); 272 "\r\n");
@@ -279,7 +286,7 @@ void TestHttpGet::testCachedRequest(void)
279 QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void))); 286 QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));
280 287
281 m_getter->setCache(m_cachedir); 288 m_getter->setCache(m_cachedir);
282 m_getter->getFile(QUrl("http://localhost:8080/test1.txt")); 289 m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.txt"));
283 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false) 290 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
284 QCoreApplication::processEvents(); 291 QCoreApplication::processEvents();
285 292
@@ -293,7 +300,7 @@ void TestHttpGet::testCachedRequest(void)
293 QCOMPARE(m_getter->httpResponse(), 200); 300 QCOMPARE(m_getter->httpResponse(), 200);
294 301
295 // request real file, this time the response should come from cache. 302 // request real file, this time the response should come from cache.
296 m_getter->getFile(QUrl("http://localhost:8080/test2.txt")); 303 m_getter->getFile(QUrl("http://localhost:" + m_port + "/test2.txt"));
297 while(m_doneSpy->count() < 2 && m_waitTimeoutOccured == false) 304 while(m_doneSpy->count() < 2 && m_waitTimeoutOccured == false)
298 QCoreApplication::processEvents(); 305 QCoreApplication::processEvents();
299 QCOMPARE(m_doneSpy->count(), 2); // 2 requests, 2 times done() 306 QCOMPARE(m_doneSpy->count(), 2); // 2 requests, 2 times done()
@@ -329,7 +336,7 @@ void TestHttpGet::testUserAgent(void)
329 336
330 m_getter->setGlobalUserAgent(TEST_USER_AGENT); 337 m_getter->setGlobalUserAgent(TEST_USER_AGENT);
331 m_getter->setCache(m_cachedir); 338 m_getter->setCache(m_cachedir);
332 m_getter->getFile(QUrl("http://localhost:8080/test1.txt")); 339 m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.txt"));
333 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false) 340 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
334 QCoreApplication::processEvents(); 341 QCoreApplication::processEvents();
335 342
@@ -353,7 +360,7 @@ void TestHttpGet::testUncachedMovedRequest(void)
353 QList<QByteArray> responses; 360 QList<QByteArray> responses;
354 responses << QByteArray( 361 responses << QByteArray(
355 "HTTP/1.1 302 Found\r\n" 362 "HTTP/1.1 302 Found\r\n"
356 "Location: http://localhost:8080/test2.txt\r\n" 363 "Location: http://localhost:" + m_port + "/test2.txt\r\n"
357 "Date: " + m_now.toLatin1() + "\r\n" 364 "Date: " + m_now.toLatin1() + "\r\n"
358 "Last-Modified: " + m_now.toLatin1() + "\r\n" 365 "Last-Modified: " + m_now.toLatin1() + "\r\n"
359 "\r\n"); 366 "\r\n");
@@ -367,7 +374,7 @@ void TestHttpGet::testUncachedMovedRequest(void)
367 374
368 QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void))); 375 QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));
369 376
370 m_getter->getFile(QUrl("http://localhost:8080/test1.php?var=1&b=foo")); 377 m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.php?var=1&b=foo"));
371 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false) 378 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
372 QCoreApplication::processEvents(); 379 QCoreApplication::processEvents();
373 380
@@ -382,7 +389,7 @@ void TestHttpGet::testResponseCode(void)
382{ 389{
383 QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void))); 390 QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));
384 391
385 m_getter->getFile(QUrl("http://localhost:8080/test1.txt")); 392 m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.txt"));
386 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false) 393 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
387 QCoreApplication::processEvents(); 394 QCoreApplication::processEvents();
388 395
@@ -407,7 +414,7 @@ void TestHttpGet::testContentToBuffer(void)
407 414
408 QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void))); 415 QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));
409 416
410 m_getter->getFile(QUrl("http://localhost:8080/test1.txt")); 417 m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.txt"));
411 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false) 418 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
412 QCoreApplication::processEvents(); 419 QCoreApplication::processEvents();
413 420
@@ -433,7 +440,7 @@ void TestHttpGet::testContentToFile(void)
433 QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void))); 440 QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));
434 441
435 m_getter->setFile(&tf); 442 m_getter->setFile(&tf);
436 m_getter->getFile(QUrl("http://localhost:8080/test1.txt")); 443 m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.txt"));
437 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false) 444 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
438 QCoreApplication::processEvents(); 445 QCoreApplication::processEvents();
439 446
@@ -450,7 +457,7 @@ void TestHttpGet::testContentToFile(void)
450void TestHttpGet::testNoServer(void) 457void TestHttpGet::testNoServer(void)
451{ 458{
452 QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void))); 459 QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));
453 m_getter->getFile(QUrl("http://localhost:8081/test1.txt")); 460 m_getter->getFile(QUrl("http://localhost:53/test1.txt"));
454 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false) 461 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
455 QCoreApplication::processEvents(); 462 QCoreApplication::processEvents();
456 463
@@ -485,7 +492,7 @@ void TestHttpGet::testServerTimestamp(void)
485 492
486 int count = m_doneSpy->count(); 493 int count = m_doneSpy->count();
487 for(int i = 0; i < responses.size(); ++i) { 494 for(int i = 0; i < responses.size(); ++i) {
488 m_getter->getFile(QUrl("http://localhost:8080/test1.txt")); 495 m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.txt"));
489 while(m_doneSpy->count() == count && m_waitTimeoutOccured == false) 496 while(m_doneSpy->count() == count && m_waitTimeoutOccured == false)
490 QCoreApplication::processEvents(); 497 QCoreApplication::processEvents();
491 count = m_doneSpy->count(); 498 count = m_doneSpy->count();
@@ -498,7 +505,7 @@ void TestHttpGet::testMovedQuery(void)
498 QList<QByteArray> responses; 505 QList<QByteArray> responses;
499 responses << QByteArray( 506 responses << QByteArray(
500 "HTTP/1.1 302 Found\r\n" 507 "HTTP/1.1 302 Found\r\n"
501 "Location: http://localhost:8080/test2.php\r\n" 508 "Location: http://localhost:" + m_port + "/test2.php\r\n"
502 "Date: " + m_now.toLatin1() + "\r\n" 509 "Date: " + m_now.toLatin1() + "\r\n"
503 "Last-Modified: " + m_now.toLatin1() + "\r\n" 510 "Last-Modified: " + m_now.toLatin1() + "\r\n"
504 "\r\n"); 511 "\r\n");
@@ -512,7 +519,7 @@ void TestHttpGet::testMovedQuery(void)
512 519
513 QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void))); 520 QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));
514 521
515 m_getter->getFile(QUrl("http://localhost:8080/test1.php?var=1&b=foo")); 522 m_getter->getFile(QUrl("http://localhost:" + m_port + "/test1.php?var=1&b=foo"));
516 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false) 523 while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
517 QCoreApplication::processEvents(); 524 QCoreApplication::processEvents();
518 525