|
| 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