Skip to content

Commit fc18355

Browse files
committed
Add unit test project.
This project depends on QFtp so it's only available for Qt4.
1 parent 795f30f commit fc18355

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

QFtpServer.pro

+7
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@ SUBDIRS += QFtpServer \
55

66
QFtpServer.depends = QFtpServerLib
77
QFtpServerCommandLine.depends = QFtpServerLib
8+
9+
equals(QT_MAJOR_VERSION, 4) {
10+
SUBDIRS += QFtpServerTests
11+
QFtpServerTests.depends = QFtpServerLib
12+
} else {
13+
message(Unit tests project is available only for Qt4 because it depends on QFtp)
14+
}

QFtpServerTests/QFtpServerTests.pro

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#-------------------------------------------------
2+
#
3+
# Project created by QtCreator 2018-07-08T11:51:47
4+
#
5+
#-------------------------------------------------
6+
7+
QT += testlib network
8+
9+
QT -= gui
10+
11+
TARGET = tst_qftpservertests
12+
CONFIG += console
13+
CONFIG -= app_bundle
14+
15+
TEMPLATE = app
16+
17+
# The following define makes your compiler emit warnings if you use
18+
# any feature of Qt which has been marked as deprecated (the exact warnings
19+
# depend on your compiler). Please consult the documentation of the
20+
# deprecated API in order to know how to port your code away from it.
21+
DEFINES += QT_DEPRECATED_WARNINGS
22+
23+
# You can also make your code fail to compile if you use deprecated APIs.
24+
# In order to do so, uncomment the following line.
25+
# You can also select to disable deprecated APIs only up to a certain version of Qt.
26+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
27+
28+
29+
SOURCES += \
30+
tst_qftpservertests.cpp
31+
32+
DEFINES += SRCDIR=\\\"$$PWD/\\\"
33+
34+
# Link to QFtpServerLib.
35+
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../QFtpServerLib/release/ -lQFtpServerLib
36+
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../QFtpServerLib/debug/ -lQFtpServerLib
37+
else:unix: LIBS += -L$$OUT_PWD/../QFtpServerLib/ -lQFtpServerLib
38+
39+
INCLUDEPATH += $$PWD/../QFtpServerLib
40+
DEPENDPATH += $$PWD/../QFtpServerLib
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <QString>
2+
#include <QtTest>
3+
#include <ftpserver.h>
4+
#include <QFtp>
5+
6+
class QFtpServerTests : public QObject
7+
{
8+
Q_OBJECT
9+
10+
public:
11+
QFtpServerTests();
12+
13+
private Q_SLOTS:
14+
void testCaseMkd();
15+
};
16+
17+
QFtpServerTests::QFtpServerTests()
18+
{
19+
}
20+
21+
// Removes a folder and all its contents, recursively. We use this because
22+
// Qt4 lacks QDir::removeRecursively().
23+
// From https://stackoverflow.com/questions/27758573/deleting-a-folder-and-all-its-contents-with-qt/27758627
24+
bool removeRecursively(const QString & dirName)
25+
{
26+
bool result = true;
27+
QDir dir(dirName);
28+
29+
if (dir.exists(dirName)) {
30+
Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
31+
if (info.isDir()) {
32+
result = removeRecursively(info.absoluteFilePath());
33+
}
34+
else {
35+
result = QFile::remove(info.absoluteFilePath());
36+
}
37+
38+
if (!result) {
39+
return result;
40+
}
41+
}
42+
result = dir.rmdir(dirName);
43+
}
44+
return result;
45+
}
46+
47+
void QFtpServerTests::testCaseMkd()
48+
{
49+
// Arrange
50+
int port = 9421;
51+
QString rootPath = "/tmp/ftpservertest/";
52+
removeRecursively(rootPath);
53+
QDir().mkpath(rootPath);
54+
FtpServer server(this, rootPath, port);
55+
Q_UNUSED(server);
56+
57+
// Act
58+
QFtp client;
59+
client.connectToHost("localhost", port);
60+
client.login();
61+
client.mkdir("foo");
62+
63+
// Wait for the server to finish its work. The
64+
// ftp client emits a "done()" signal when the
65+
// last pending command has finished.
66+
QEventLoop loop;
67+
connect(&client, SIGNAL(done(bool)), &loop, SLOT(quit()));
68+
loop.exec();
69+
70+
// Assert
71+
QVERIFY(QDir(rootPath + "/foo").exists());
72+
}
73+
74+
QTEST_MAIN(QFtpServerTests)
75+
76+
#include "tst_qftpservertests.moc"

0 commit comments

Comments
 (0)