summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShane Kearns <shane.kearns@accenture.com>2012-01-11 16:00:48 +0000
committerShane Kearns <shane.kearns@accenture.com>2012-04-20 12:17:11 +0200
commit55d7651ed79750e79ab7eb073f1a9b7e5c51c6c0 (patch)
tree128a76da9403a7a5a1f56298c62823170837186e
parent420d58e8b475811ffef1af34ff6a5910fe52f19e (diff)
Add QFtp sources from Qt
Change-Id: I9c271327de6b3934b0cd289956183ff5b9256df4 Reviewed-by: Shane Kearns <shane.kearns@accenture.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r--src/qftp/qftp.cpp2433
-rw-r--r--src/qftp/qftp.h180
-rw-r--r--tests/auto/qftp/.gitattributes1
-rw-r--r--tests/auto/qftp/.gitignore2
-rw-r--r--tests/auto/qftp/qftp.pro17
-rw-r--r--tests/auto/qftp/rfc3252.txt899
-rw-r--r--tests/auto/qftp/tst_qftp.cpp2149
7 files changed, 5681 insertions, 0 deletions
diff --git a/src/qftp/qftp.cpp b/src/qftp/qftp.cpp
new file mode 100644
index 0000000..5a4981d
--- /dev/null
+++ b/src/qftp/qftp.cpp
@@ -0,0 +1,2433 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtNetwork module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+//#define QFTPPI_DEBUG
+//#define QFTPDTP_DEBUG
+
+#include "qftp.h"
+#include "qabstractsocket.h"
+
+#ifndef QT_NO_FTP
+
+#include "qcoreapplication.h"
+#include "qtcpsocket.h"
+#include "qurlinfo.h"
+#include "qstringlist.h"
+#include "qregexp.h"
+#include "qtimer.h"
+#include "qfileinfo.h"
+#include "qhash.h"
+#include "qtcpserver.h"
+#include "qlocale.h"
+
+QT_BEGIN_NAMESPACE
+
+class QFtpPI;
+
+/*
+ The QFtpDTP (DTP = Data Transfer Process) controls all client side
+ data transfer between the client and server.
+*/
+class QFtpDTP : public QObject
+{
+ Q_OBJECT
+
+public:
+ enum ConnectState {
+ CsHostFound,
+ CsConnected,
+ CsClosed,
+ CsHostNotFound,
+ CsConnectionRefused
+ };
+
+ QFtpDTP(QFtpPI *p, QObject *parent = 0);
+
+ void setData(QByteArray *);
+ void setDevice(QIODevice *);
+ void writeData();
+ void setBytesTotal(qint64 bytes);
+
+ bool hasError() const;
+ QString errorMessage() const;
+ void clearError();
+
+ void connectToHost(const QString & host, quint16 port);
+ int setupListener(const QHostAddress &address);
+ void waitForConnection();
+
+ QTcpSocket::SocketState state() const;
+ qint64 bytesAvailable() const;
+ qint64 read(char *data, qint64 maxlen);
+ QByteArray readAll();
+
+ void abortConnection();
+
+ static bool parseDir(const QByteArray &buffer, const QString &userName, QUrlInfo *info);
+
+signals:
+ void listInfo(const QUrlInfo&);
+ void readyRead();
+ void dataTransferProgress(qint64, qint64);
+
+ void connectState(int);
+
+private slots:
+ void socketConnected();
+ void socketReadyRead();
+ void socketError(QAbstractSocket::SocketError);
+ void socketConnectionClosed();
+ void socketBytesWritten(qint64);
+ void setupSocket();
+
+ void dataReadyRead();
+
+private:
+ void clearData();
+
+ QTcpSocket *socket;
+ QTcpServer listener;
+
+ QFtpPI *pi;
+ QString err;
+ qint64 bytesDone;
+ qint64 bytesTotal;
+ bool callWriteData;
+
+ // If is_ba is true, ba is used; ba is never 0.
+ // Otherwise dev is used; dev can be 0 or not.
+ union {
+ QByteArray *ba;
+ QIODevice *dev;
+ } data;
+ bool is_ba;
+
+ QByteArray bytesFromSocket;
+};
+
+/**********************************************************************
+ *
+ * QFtpPI - Protocol Interpreter
+ *
+ *********************************************************************/
+
+class QFtpPI : public QObject
+{
+ Q_OBJECT
+
+public:
+ QFtpPI(QObject *parent = 0);
+
+ void connectToHost(const QString &host, quint16 port);
+
+ bool sendCommands(const QStringList &cmds);
+ bool sendCommand(const QString &cmd)
+ { return sendCommands(QStringList(cmd)); }
+
+ void clearPendingCommands();
+ void abort();
+
+ QString currentCommand() const
+ { return currentCmd; }
+
+ bool rawCommand;
+ bool transferConnectionExtended;
+
+ QFtpDTP dtp; // the PI has a DTP which is not the design of RFC 959, but it
+ // makes the design simpler this way
+signals:
+ void connectState(int);
+ void finished(const QString&);
+ void error(int, const QString&);
+ void rawFtpReply(int, const QString&);
+
+private slots:
+ void hostFound();
+ void connected();
+ void connectionClosed();
+ void delayedCloseFinished();
+ void readyRead();
+ void error(QAbstractSocket::SocketError);
+
+ void dtpConnectState(int);
+
+private:
+ // the states are modelled after the generalized state diagram of RFC 959,
+ // page 58
+ enum State {
+ Begin,
+ Idle,
+ Waiting,
+ Success,
+ Failure
+ };
+
+ enum AbortState {
+ None,
+ AbortStarted,
+ WaitForAbortToFinish
+ };
+
+ bool processReply();
+ bool startNextCmd();
+
+ QTcpSocket commandSocket;
+ QString replyText;
+ char replyCode[3];
+ State state;
+ AbortState abortState;
+ QStringList pendingCommands;
+ QString currentCmd;
+
+ bool waitForDtpToConnect;
+ bool waitForDtpToClose;
+
+ QByteArray bytesFromSocket;
+
+ friend class QFtpDTP;
+};
+
+/**********************************************************************
+ *
+ * QFtpCommand implemenatation
+ *
+ *********************************************************************/
+class QFtpCommand
+{
+public:
+ QFtpCommand(QFtp::Command cmd, QStringList raw, const QByteArray &ba);
+ QFtpCommand(QFtp::Command cmd, QStringList raw, QIODevice *dev = 0);
+ ~QFtpCommand();
+
+ int id;
+ QFtp::Command command;
+ QStringList rawCmds;
+
+ // If is_ba is true, ba is used; ba is never 0.
+ // Otherwise dev is used; dev can be 0 or not.
+ union {
+ QByteArray *ba;
+ QIODevice *dev;
+ } data;
+ bool is_ba;
+
+ static QBasicAtomicInt idCounter;
+};
+
+QBasicAtomicInt QFtpCommand::idCounter = Q_BASIC_ATOMIC_INITIALIZER(1);
+
+QFtpCommand::QFtpCommand(QFtp::Command cmd, QStringList raw, const QByteArray &ba)
+ : command(cmd), rawCmds(raw), is_ba(true)
+{
+ id = idCounter.fetchAndAddRelaxed(1);
+ data.ba = new QByteArray(ba);
+}
+
+QFtpCommand::QFtpCommand(QFtp::Command cmd, QStringList raw, QIODevice *dev)
+ : command(cmd), rawCmds(raw), is_ba(false)
+{
+ id = idCounter.fetchAndAddRelaxed(1);
+ data.dev = dev;
+}
+
+QFtpCommand::~QFtpCommand()
+{
+ if (is_ba)
+ delete data.ba;
+}
+
+/**********************************************************************
+ *
+ * QFtpDTP implemenatation
+ *
+ *********************************************************************/
+QFtpDTP::QFtpDTP(QFtpPI *p, QObject *parent) :
+ QObject(parent),
+ socket(0),
+ listener(this),
+ pi(p),
+ callWriteData(false)
+{
+ clearData();
+ listener.setObjectName(QLatin1String("QFtpDTP active state server"));
+ connect(&listener, SIGNAL(newConnection()), SLOT(setupSocket()));
+}
+
+void QFtpDTP::setData(QByteArray *ba)
+{
+ is_ba = true;
+ data.ba = ba;
+}
+
+void QFtpDTP::setDevice(QIODevice *dev)
+{
+ is_ba = false;
+ data.dev = dev;
+}
+
+void QFtpDTP::setBytesTotal(qint64 bytes)
+{
+ bytesTotal = bytes;
+ bytesDone = 0;
+ emit dataTransferProgress(bytesDone, bytesTotal);
+}
+
+void QFtpDTP::connectToHost(const QString & host, quint16 port)
+{
+ bytesFromSocket.clear();
+
+ if (socket) {
+ delete socket;
+ socket = 0;
+ }
+ socket = new QTcpSocket(this);
+#ifndef QT_NO_BEARERMANAGEMENT
+ //copy network session down to the socket
+ socket->setProperty("_q_networksession", property("_q_networksession"));
+#endif
+ socket->setObjectName(QLatin1String("QFtpDTP Passive state socket"));
+ connect(socket, SIGNAL(connected()), SLOT(socketConnected()));
+ connect(socket, SIGNAL(readyRead()), SLOT(socketReadyRead()));
+ connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(socketError(QAbstractSocket::SocketError)));
+ connect(socket, SIGNAL(disconnected()), SLOT(socketConnectionClosed()));
+ connect(socket, SIGNAL(bytesWritten(qint64)), SLOT(socketBytesWritten(qint64)));
+
+ socket->connectToHost(host, port);
+}
+
+int QFtpDTP::setupListener(const QHostAddress &address)
+{
+#ifndef QT_NO_BEARERMANAGEMENT
+ //copy network session down to the socket
+ listener.setProperty("_q_networksession", property("_q_networksession"));
+#endif
+ if (!listener.isListening() && !listener.listen(address, 0))
+ return -1;
+ return listener.serverPort();
+}
+
+void QFtpDTP::waitForConnection()
+{
+ // This function is only interesting in Active transfer mode; it works
+ // around a limitation in QFtp's design by blocking, waiting for an
+ // incoming connection. For the default Passive mode, it does nothing.
+ if (listener.isListening())
+ listener.waitForNewConnection();
+}
+
+QTcpSocket::SocketState QFtpDTP::state() const
+{
+ return socket ? socket->state() : QTcpSocket::UnconnectedState;
+}
+
+qint64 QFtpDTP::bytesAvailable() const
+{
+ if (!socket || socket->state() != QTcpSocket::ConnectedState)
+ return (qint64) bytesFromSocket.size();
+ return socket->bytesAvailable();
+}
+
+qint64 QFtpDTP::read(char *data, qint64 maxlen)
+{
+ qint64 read;
+ if (socket && socket->state() == QTcpSocket::ConnectedState) {
+ read = socket->read(data, maxlen);
+ } else {
+ read = qMin(maxlen, qint64(bytesFromSocket.size()));
+ memcpy(data, bytesFromSocket.data(), read);
+ bytesFromSocket.remove(0, read);
+ }
+
+ bytesDone += read;
+ return read;
+}
+
+QByteArray QFtpDTP::readAll()
+{
+ QByteArray tmp;
+ if (socket && socket->state() == QTcpSocket::ConnectedState) {
+ tmp = socket->readAll();
+ bytesDone += tmp.size();
+ } else {
+ tmp = bytesFromSocket;
+ bytesFromSocket.clear();
+ }
+ return tmp;
+}
+
+void QFtpDTP::writeData()
+{
+ if (!socket)
+ return;
+
+ if (is_ba) {
+#if defined(QFTPDTP_DEBUG)
+ qDebug("QFtpDTP::writeData: write %d bytes", data.ba->size());
+#endif
+ if (data.ba->size() == 0)
+ emit dataTransferProgress(0, bytesTotal);
+ else
+ socket->write(data.ba->data(), data.ba->size());
+
+ socket->close();
+
+ clearData();
+ } else if (data.dev) {
+ callWriteData = false;
+ const qint64 blockSize = 16*1024;
+ char buf[16*1024];
+ qint64 read = data.dev->read(buf, blockSize);
+#if defined(QFTPDTP_DEBUG)
+ qDebug("QFtpDTP::writeData: write() of size %lli bytes", read);
+#endif
+ if (read > 0) {
+ socket->write(buf, read);
+ } else if (read == -1 || (!data.dev->isSequential() && data.dev->atEnd())) {
+ // error or EOF
+ if (bytesDone == 0 && socket->bytesToWrite() == 0)
+ emit dataTransferProgress(0, bytesTotal);
+ socket->close();
+ clearData();
+ }
+
+ // do we continue uploading?
+ callWriteData = data.dev != 0;
+ }
+}
+
+void QFtpDTP::dataReadyRead()
+{
+ writeData();
+}
+
+inline bool QFtpDTP::hasError() const
+{
+ return !err.isNull();
+}
+
+inline QString QFtpDTP::errorMessage() const
+{
+ return err;
+}
+
+inline void QFtpDTP::clearError()
+{
+ err.clear();
+}
+
+void QFtpDTP::abortConnection()
+{
+#if defined(QFTPDTP_DEBUG)
+ qDebug("QFtpDTP::abortConnection, bytesAvailable == %lli",
+ socket ? socket->bytesAvailable() : (qint64) 0);
+#endif
+ callWriteData = false;
+ clearData();
+
+ if (socket)
+ socket->abort();
+}
+
+static void _q_fixupDateTime(QDateTime *dateTime)
+{
+ // Adjust for future tolerance.
+ const int futureTolerance = 86400;
+ if (dateTime->secsTo(QDateTime::currentDateTime()) < -futureTolerance) {
+ QDate d = dateTime->date();
+ d.setYMD(d.year() - 1, d.month(), d.day());
+ dateTime->setDate(d);
+ }
+}
+
+static void _q_parseUnixDir(const QStringList &tokens, const QString &userName, QUrlInfo *info)
+{
+ // Unix style, 7 + 1 entries
+ // -rw-r--r-- 1 ftp ftp 17358091 Aug 10 2004 qt-x11-free-3.3.3.tar.gz
+ // drwxr-xr-x 3 ftp ftp 4096 Apr 14 2000 compiled-examples
+ // lrwxrwxrwx 1 ftp ftp 9 Oct 29 2005 qtscape -> qtmozilla
+ if (tokens.size() != 8)
+ return;
+
+ char first = tokens.at(1).at(0).toLatin1();
+ if (first == 'd') {
+ info->setDir(true);
+ info->setFile(false);
+ info->setSymLink(false);
+ } else if (first == '-') {
+ info->setDir(false);
+ info->setFile(true);
+ info->setSymLink(false);
+ } else if (first == 'l') {
+ info->setDir(true);
+ info->setFile(false);
+ info->setSymLink(true);
+ }
+
+ // Resolve filename
+ QString name = tokens.at(7);
+ if (info->isSymLink()) {
+ int linkPos = name.indexOf(QLatin1String(" ->"));
+ if (linkPos != -1)
+ name.resize(linkPos);
+ }
+ info->setName(name);
+
+ // Resolve owner & group
+ info->setOwner(tokens.at(3));
+ info->setGroup(tokens.at(4));
+
+ // Resolve size
+ info->setSize(tokens.at(5).toLongLong());
+
+ QStringList formats;
+ formats << QLatin1String("MMM dd yyyy") << QLatin1String("MMM dd hh:mm") << QLatin1String("MMM d yyyy")
+ << QLatin1String("MMM d hh:mm") << QLatin1String("MMM d yyyy") << QLatin1String("MMM dd yyyy");
+
+ QString dateString = tokens.at(6);
+ dateString[0] = dateString[0].toUpper();
+
+ // Resolve the modification date by parsing all possible formats
+ QDateTime dateTime;
+ int n = 0;
+#ifndef QT_NO_DATESTRING
+ do {
+ dateTime = QLocale::c().toDateTime(dateString, formats.at(n++));
+ } while (n < formats.size() && (!dateTime.isValid()));
+#endif
+
+ if (n == 2 || n == 4) {
+ // Guess the year.
+ dateTime.setDate(QDate(QDate::currentDate().year(),
+ dateTime.date().month(),
+ dateTime.date().day()));
+ _q_fixupDateTime(&dateTime);
+ }
+ if (dateTime.isValid())
+ info->setLastModified(dateTime);
+
+ // Resolve permissions
+ int permissions = 0;
+ QString p = tokens.at(2);
+ permissions |= (p[0] == QLatin1Char('r') ? QUrlInfo::ReadOwner : 0);
+ permissions |= (p[1] == QLatin1Char('w') ? QUrlInfo::WriteOwner : 0);
+ permissions |= (p[2] == QLatin1Char('x') ? QUrlInfo::ExeOwner : 0);
+ permissions |= (p[3] == QLatin1Char('r') ? QUrlInfo::ReadGroup : 0);
+ permissions |= (p[4] == QLatin1Char('w') ? QUrlInfo::WriteGroup : 0);
+ permissions |= (p[5] == QLatin1Char('x') ? QUrlInfo::ExeGroup : 0);
+ permissions |= (p[6] == QLatin1Char('r') ? QUrlInfo::ReadOther : 0);
+ permissions |= (p[7] == QLatin1Char('w') ? QUrlInfo::WriteOther : 0);
+ permissions |= (p[8] == QLatin1Char('x') ? QUrlInfo::ExeOther : 0);
+ info->setPermissions(permissions);
+
+ bool isOwner = info->owner() == userName;
+ info->setReadable((permissions & QUrlInfo::ReadOther) || ((permissions & QUrlInfo::ReadOwner) && isOwner));
+ info->setWritable((permissions & QUrlInfo::WriteOther) || ((permissions & QUrlInfo::WriteOwner) && isOwner));
+}
+
+static void _q_parseDosDir(const QStringList &tokens, const QString &userName, QUrlInfo *info)
+{
+ // DOS style, 3 + 1 entries
+ // 01-16-02 11:14AM <DIR> epsgroup
+ // 06-05-03 03:19PM 1973 readme.txt
+ if (tokens.size() != 4)
+ return;
+
+ Q_UNUSED(userName);
+
+ QString name = tokens.at(3);
+ info->setName(name);
+ info->setSymLink(name.toLower().endsWith(QLatin1String(".lnk")));
+
+ if (tokens.at(2) == QLatin1String("<DIR>")) {
+ info->setFile(false);
+ info->setDir(true);
+ } else {
+ info->setFile(true);
+ info->setDir(false);
+ info->setSize(tokens.at(2).toLongLong());
+ }
+
+ // Note: We cannot use QFileInfo; permissions are for the server-side
+ // machine, and QFileInfo's behavior depends on the local platform.
+ int permissions = QUrlInfo::ReadOwner | QUrlInfo::WriteOwner
+ | QUrlInfo::ReadGroup | QUrlInfo::WriteGroup
+ | QUrlInfo::ReadOther | QUrlInfo::WriteOther;
+ QString ext;
+ int extIndex = name.lastIndexOf(QLatin1Char('.'));
+ if (extIndex != -1)
+ ext = name.mid(extIndex + 1);
+ if (ext == QLatin1String("exe") || ext == QLatin1String("bat") || ext == QLatin1String("com"))
+ permissions |= QUrlInfo::ExeOwner | QUrlInfo::ExeGroup | QUrlInfo::ExeOther;
+ info->setPermissions(permissions);
+
+ info->setReadable(true);
+ info->setWritable(info->isFile());
+
+ QDateTime dateTime;
+#ifndef QT_NO_DATESTRING
+ dateTime = QLocale::c().toDateTime(tokens.at(1), QLatin1String("MM-dd-yy hh:mmAP"));
+ if (dateTime.date().year() < 1971) {
+ dateTime.setDate(QDate(dateTime.date().year() + 100,
+ dateTime.date().month(),
+ dateTime.date().day()));
+ }
+#endif
+
+ info->setLastModified(dateTime);
+
+}
+
+bool QFtpDTP::parseDir(const QByteArray &buffer, const QString &userName, QUrlInfo *info)
+{
+ if (buffer.isEmpty())
+ return false;
+
+ QString bufferStr = QString::fromLatin1(buffer).trimmed();
+
+ // Unix style FTP servers
+ QRegExp unixPattern(QLatin1String("^([\\-dl])([a-zA-Z\\-]{9,9})\\s+\\d+\\s+(\\S*)\\s+"
+ "(\\S*)\\s+(\\d+)\\s+(\\S+\\s+\\S+\\s+\\S+)\\s+(\\S.*)"));
+ if (unixPattern.indexIn(bufferStr) == 0) {
+ _q_parseUnixDir(unixPattern.capturedTexts(), userName, info);
+ return true;
+ }
+
+ // DOS style FTP servers
+ QRegExp dosPattern(QLatin1String("^(\\d\\d-\\d\\d-\\d\\d\\ \\ \\d\\d:\\d\\d[AP]M)\\s+"
+ "(<DIR>|\\d+)\\s+(\\S.*)$"));
+ if (dosPattern.indexIn(bufferStr) == 0) {
+ _q_parseDosDir(dosPattern.capturedTexts(), userName, info);
+ return true;
+ }
+
+ // Unsupported
+ return false;
+}
+
+void QFtpDTP::socketConnected()
+{
+ bytesDone = 0;
+#if defined(QFTPDTP_DEBUG)
+ qDebug("QFtpDTP::connectState(CsConnected)");
+#endif
+ emit connectState(QFtpDTP::CsConnected);
+}
+
+void QFtpDTP::socketReadyRead()
+{
+ if (!socket)
+ return;
+
+ if (pi->currentCommand().isEmpty()) {
+ socket->close();
+#if defined(QFTPDTP_DEBUG)
+ qDebug("QFtpDTP::connectState(CsClosed)");
+#endif
+ emit connectState(QFtpDTP::CsClosed);
+ return;
+ }
+
+ if (pi->abortState == QFtpPI::AbortStarted) {
+ // discard data
+ socket->readAll();
+ return;
+ }
+
+ if (pi->currentCommand().startsWith(QLatin1String("LIST"))) {
+ while (socket->canReadLine()) {
+ QUrlInfo i;
+ QByteArray line = socket->readLine();
+#if defined(QFTPDTP_DEBUG)
+ qDebug("QFtpDTP read (list): '%s'", line.constData());
+#endif
+ if (parseDir(line, QLatin1String(""), &i)) {
+ emit listInfo(i);
+ } else {
+ // some FTP servers don't return a 550 if the file or directory
+ // does not exist, but rather write a text to the data socket
+ // -- try to catch these cases
+ if (line.endsWith("No such file or directory\r\n"))
+ err = QString::fromLatin1(line);
+ }
+ }
+ } else {
+ if (!is_ba && data.dev) {
+ do {
+ QByteArray ba;
+ ba.resize(socket->bytesAvailable());
+ qint64 bytesRead = socket->read(ba.data(), ba.size());
+ if (bytesRead < 0) {
+ // a read following a readyRead() signal will
+ // never fail.
+ return;
+ }
+ ba.resize(bytesRead);
+ bytesDone += bytesRead;
+#if defined(QFTPDTP_DEBUG)
+ qDebug("QFtpDTP read: %lli bytes (total %lli bytes)", bytesRead, bytesDone);
+#endif
+ if (data.dev) // make sure it wasn't deleted in the slot
+ data.dev->write(ba);
+ emit dataTransferProgress(bytesDone, bytesTotal);
+
+ // Need to loop; dataTransferProgress is often connected to
+ // slots that update the GUI (e.g., progress bar values), and
+ // if events are processed, more data may have arrived.
+ } while (socket->bytesAvailable());
+ } else {
+#if defined(QFTPDTP_DEBUG)
+ qDebug("QFtpDTP readyRead: %lli bytes available (total %lli bytes read)",
+ bytesAvailable(), bytesDone);
+#endif
+ emit dataTransferProgress(bytesDone+socket->bytesAvailable(), bytesTotal);
+ emit readyRead();
+ }
+ }
+}
+
+void QFtpDTP::socketError(QAbstractSocket::SocketError e)
+{
+ if (e == QTcpSocket::HostNotFoundError) {
+#if defined(QFTPDTP_DEBUG)
+ qDebug("QFtpDTP::connectState(CsHostNotFound)");
+#endif
+ emit connectState(QFtpDTP::CsHostNotFound);
+ } else if (e == QTcpSocket::ConnectionRefusedError) {
+#if defined(QFTPDTP_DEBUG)
+ qDebug("QFtpDTP::connectState(CsConnectionRefused)");
+#endif
+ emit connectState(QFtpDTP::CsConnectionRefused);
+ }
+}
+
+void QFtpDTP::socketConnectionClosed()
+{
+ if (!is_ba && data.dev) {
+ clearData();
+ }
+
+ bytesFromSocket = socket->readAll();
+#if defined(QFTPDTP_DEBUG)
+ qDebug("QFtpDTP::connectState(CsClosed)");
+#endif
+ emit connectState(QFtpDTP::CsClosed);
+}
+
+void QFtpDTP::socketBytesWritten(qint64 bytes)
+{
+ bytesDone += bytes;
+#if defined(QFTPDTP_DEBUG)
+ qDebug("QFtpDTP::bytesWritten(%lli)", bytesDone);
+#endif
+ emit dataTransferProgress(bytesDone, bytesTotal);
+ if (callWriteData)
+ writeData();
+}
+
+void QFtpDTP::setupSocket()
+{
+ socket = listener.nextPendingConnection();
+ socket->setObjectName(QLatin1String("QFtpDTP Active state socket"));
+ connect(socket, SIGNAL(connected()), SLOT(socketConnected()));
+ connect(socket, SIGNAL(readyRead()), SLOT(socketReadyRead()));
+ connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(socketError(QAbstractSocket::SocketError)));
+ connect(socket, SIGNAL(disconnected()), SLOT(socketConnectionClosed()));
+ connect(socket, SIGNAL(bytesWritten(qint64)), SLOT(socketBytesWritten(qint64)));
+
+ listener.close();
+}
+
+void QFtpDTP::clearData()
+{
+ is_ba = false;
+ data.dev = 0;
+}
+
+/**********************************************************************
+ *
+ * QFtpPI implemenatation
+ *
+ *********************************************************************/
+QFtpPI::QFtpPI(QObject *parent) :
+ QObject(parent),
+ rawCommand(false),
+ transferConnectionExtended(true),
+ dtp(this),
+ commandSocket(0),
+ state(Begin), abortState(None),
+ currentCmd(QString()),
+ waitForDtpToConnect(false),
+ waitForDtpToClose(false)
+{
+ commandSocket.setObjectName(QLatin1String("QFtpPI_socket"));
+ connect(&commandSocket, SIGNAL(hostFound()),
+ SLOT(hostFound()));
+ connect(&commandSocket, SIGNAL(connected()),
+ SLOT(connected()));
+ connect(&commandSocket, SIGNAL(disconnected()),
+ SLOT(connectionClosed()));
+ connect(&commandSocket, SIGNAL(readyRead()),
+ SLOT(readyRead()));
+ connect(&commandSocket, SIGNAL(error(QAbstractSocket::SocketError)),
+ SLOT(error(QAbstractSocket::SocketError)));
+
+ connect(&dtp, SIGNAL(connectState(int)),
+ SLOT(dtpConnectState(int)));
+}
+
+void QFtpPI::connectToHost(const QString &host, quint16 port)
+{
+ emit connectState(QFtp::HostLookup);
+#ifndef QT_NO_BEARERMANAGEMENT
+ //copy network session down to the socket & DTP
+ commandSocket.setProperty("_q_networksession", property("_q_networksession"));
+ dtp.setProperty("_q_networksession", property("_q_networksession"));
+#endif
+ commandSocket.connectToHost(host, port);
+}
+
+/*
+ Sends the sequence of commands \a cmds to the FTP server. When the commands
+ are all done the finished() signal is emitted. When an error occurs, the
+ error() signal is emitted.
+
+ If there are pending commands in the queue this functions returns false and
+ the \a cmds are not added to the queue; otherwise it returns true.
+*/
+bool QFtpPI::sendCommands(const QStringList &cmds)
+{
+ if (!pendingCommands.isEmpty())
+ return false;
+
+ if (commandSocket.state() != QTcpSocket::ConnectedState || state!=Idle) {
+ emit error(QFtp::NotConnected, QFtp::tr("Not connected"));
+ return true; // there are no pending commands
+ }
+
+ pendingCommands = cmds;
+ startNextCmd();
+ return true;
+}
+
+void QFtpPI::clearPendingCommands()
+{
+ pendingCommands.clear();
+ dtp.abortConnection();
+ currentCmd.clear();
+ state = Idle;
+}
+
+void QFtpPI::abort()
+{
+ pendingCommands.clear();
+
+ if (abortState != None)
+ // ABOR already sent
+ return;
+
+ abortState = AbortStarted;
+#if defined(QFTPPI_DEBUG)
+ qDebug("QFtpPI send: ABOR");
+#endif
+ commandSocket.write("ABOR\r\n", 6);
+
+ if (currentCmd.startsWith(QLatin1String("STOR ")))
+ dtp.abortConnection();
+}
+
+void QFtpPI::hostFound()
+{
+ emit connectState(QFtp::Connecting);
+}
+
+void QFtpPI::connected()
+{
+ state = Begin;
+#if defined(QFTPPI_DEBUG)
+// qDebug("QFtpPI state: %d [connected()]", state);
+#endif
+ // try to improve performance by setting TCP_NODELAY
+ commandSocket.setSocketOption(QAbstractSocket::LowDelayOption, 1);
+
+ emit connectState(QFtp::Connected);
+}
+
+void QFtpPI::connectionClosed()
+{
+ commandSocket.close();
+ emit connectState(QFtp::Unconnected);
+}
+
+void QFtpPI::delayedCloseFinished()
+{
+ emit connectState(QFtp::Unconnected);
+}
+
+void QFtpPI::error(QAbstractSocket::SocketError e)
+{
+ if (e == QTcpSocket::HostNotFoundError) {
+ emit connectState(QFtp::Unconnected);
+ emit error(QFtp::HostNotFound,
+ QFtp::tr("Host %1 not found").arg(commandSocket.peerName()));
+ } else if (e == QTcpSocket::ConnectionRefusedError) {
+ emit connectState(QFtp::Unconnected);
+ emit error(QFtp::ConnectionRefused,
+ QFtp::tr("Connection refused to host %1").arg(commandSocket.peerName()));
+ } else if (e == QTcpSocket::SocketTimeoutError) {
+ emit connectState(QFtp::Unconnected);
+ emit error(QFtp::ConnectionRefused,
+ QFtp::tr("Connection timed out to host %1").arg(commandSocket.peerName()));
+ }
+}
+
+void QFtpPI::readyRead()
+{
+ if (waitForDtpToClose)
+ return;
+
+ while (commandSocket.canReadLine()) {
+ // read line with respect to line continuation
+ QString line = QString::fromAscii(commandSocket.readLine());
+ if (replyText.isEmpty()) {
+ if (line.length() < 3) {
+ // protocol error
+ return;
+ }
+ const int lowerLimit[3] = {1,0,0};
+ const int upperLimit[3] = {5,5,9};
+ for (int i=0; i<3; i++) {
+ replyCode[i] = line[i].digitValue();
+ if (replyCode[i]<lowerLimit[i] || replyCode[i]>upperLimit[i]) {
+ // protocol error
+ return;
+ }
+ }
+ }
+ QString endOfMultiLine;
+ endOfMultiLine[0] = '0' + replyCode[0];
+ endOfMultiLine[1] = '0' + replyCode[1];
+ endOfMultiLine[2] = '0' + replyCode[2];
+ endOfMultiLine[3] = QLatin1Char(' ');
+ QString lineCont(endOfMultiLine);
+ lineCont[3] = QLatin1Char('-');
+ QString lineLeft4 = line.left(4);
+
+ while (lineLeft4 != endOfMultiLine) {
+ if (lineLeft4 == lineCont)
+ replyText += line.mid(4); // strip 'xyz-'
+ else
+ replyText += line;
+ if (!commandSocket.canReadLine())
+ return;
+ line = QString::fromAscii(commandSocket.readLine());
+ lineLeft4 = line.left(4);
+ }
+ replyText += line.mid(4); // strip reply code 'xyz '
+ if (replyText.endsWith(QLatin1String("\r\n")))
+ replyText.chop(2);
+
+ if (processReply())
+ replyText = QLatin1String("");
+ }
+}
+
+/*
+ Process a reply from the FTP server.
+
+ Returns true if the reply was processed or false if the reply has to be
+ processed at a later point.
+*/
+bool QFtpPI::processReply()
+{
+#if defined(QFTPPI_DEBUG)
+// qDebug("QFtpPI state: %d [processReply() begin]", state);
+ if (replyText.length() < 400)
+ qDebug("QFtpPI recv: %d %s", 100*replyCode[0]+10*replyCode[1]+replyCode[2], replyText.toLatin1().constData());
+ else
+ qDebug("QFtpPI recv: %d (text skipped)", 100*replyCode[0]+10*replyCode[1]+replyCode[2]);
+#endif
+
+ int replyCodeInt = 100*replyCode[0] + 10*replyCode[1] + replyCode[2];
+
+ // process 226 replies ("Closing Data Connection") only when the data
+ // connection is really closed to avoid short reads of the DTP
+ if (replyCodeInt == 226 || (replyCodeInt == 250 && currentCmd.startsWith(QLatin1String("RETR")))) {
+ if (dtp.state() != QTcpSocket::UnconnectedState) {
+ waitForDtpToClose = true;
+ return false;
+ }
+ }
+
+ switch (abortState) {
+ case AbortStarted:
+ abortState = WaitForAbortToFinish;
+ break;
+ case WaitForAbortToFinish:
+ abortState = None;
+ return true;
+ default:
+ break;
+ }
+
+ // get new state
+ static const State table[5] = {
+ /* 1yz 2yz 3yz 4yz 5yz */
+ Waiting, Success, Idle, Failure, Failure
+ };
+ switch (state) {
+ case Begin:
+ if (replyCode[0] == 1) {
+ return true;
+ } else if (replyCode[0] == 2) {
+ state = Idle;
+ emit finished(QFtp::tr("Connected to host %1").arg(commandSocket.peerName()));
+ break;
+ }
+ // reply codes not starting with 1 or 2 are not handled.
+ return true;
+ case Waiting:
+ if (static_cast<signed char>(replyCode[0]) < 0 || replyCode[0] > 5)
+ state = Failure;
+ else
+#if defined(Q_OS_IRIX) && defined(Q_CC_GNU)
+ {
+ // work around a crash on 64 bit gcc IRIX
+ State *t = (State *) table;
+ state = t[replyCode[0] - 1];
+ }
+#else
+ if (replyCodeInt == 202)
+ state = Failure;
+ else
+ state = table[replyCode[0] - 1];
+#endif
+ break;
+ default:
+ // ignore unrequested message
+ return true;
+ }
+#if defined(QFTPPI_DEBUG)
+// qDebug("QFtpPI state: %d [processReply() intermediate]", state);
+#endif
+
+ // special actions on certain replies
+ emit rawFtpReply(replyCodeInt, replyText);
+ if (rawCommand) {
+ rawCommand = false;
+ } else if (replyCodeInt == 227) {
+ // 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2)
+ // rfc959 does not define this response precisely, and gives
+ // both examples where the parenthesis are used, and where
+ // they are missing. We need to scan for the address and host
+ // info.
+ QRegExp addrPortPattern(QLatin1String("(\\d+),(\\d+),(\\d+),(\\d+),(\\d+),(\\d+)"));
+ if (addrPortPattern.indexIn(replyText) == -1) {
+#if defined(QFTPPI_DEBUG)
+ qDebug("QFtp: bad 227 response -- address and port information missing");
+#endif
+ // this error should be reported
+ } else {
+ QStringList lst = addrPortPattern.capturedTexts();
+ QString host = lst[1] + QLatin1Char('.') + lst[2] + QLatin1Char('.') + lst[3] + QLatin1Char('.') + lst[4];
+ quint16 port = (lst[5].toUInt() << 8) + lst[6].toUInt();
+ waitForDtpToConnect = true;
+ dtp.connectToHost(host, port);
+ }
+ } else if (replyCodeInt == 229) {
+ // 229 Extended Passive mode OK (|||10982|)
+ int portPos = replyText.indexOf(QLatin1Char('('));
+ if (portPos == -1) {
+#if defined(QFTPPI_DEBUG)
+ qDebug("QFtp: bad 229 response -- port information missing");
+#endif
+ // this error should be reported
+ } else {
+ ++portPos;
+ QChar delimiter = replyText.at(portPos);
+ QStringList epsvParameters = replyText.mid(portPos).split(delimiter);
+
+ waitForDtpToConnect = true;
+ dtp.connectToHost(commandSocket.peerAddress().toString(),
+ epsvParameters.at(3).toInt());
+ }
+
+ } else if (replyCodeInt == 230) {
+ if (currentCmd.startsWith(QLatin1String("USER ")) && pendingCommands.count()>0 &&
+ pendingCommands.first().startsWith(QLatin1String("PASS "))) {
+ // no need to send the PASS -- we are already logged in
+ pendingCommands.pop_front();
+ }
+ // 230 User logged in, proceed.
+ emit connectState(QFtp::LoggedIn);
+ } else if (replyCodeInt == 213) {
+ // 213 File status.
+ if (currentCmd.startsWith(QLatin1String("SIZE ")))
+ dtp.setBytesTotal(replyText.simplified().toLongLong());
+ } else if (replyCode[0]==1 && currentCmd.startsWith(QLatin1String("STOR "))) {
+ dtp.waitForConnection();
+ dtp.writeData();
+ }
+
+ // react on new state
+ switch (state) {
+ case Begin:
+ // should never happen
+ break;
+ case Success:
+ // success handling
+ state = Idle;
+ // no break!
+ case Idle:
+ if (dtp.hasError()) {
+ emit error(QFtp::UnknownError, dtp.errorMessage());
+ dtp.clearError();
+ }
+ startNextCmd();
+ break;
+ case Waiting:
+ // do nothing
+ break;
+ case Failure:
+ // If the EPSV or EPRT commands fail, replace them with
+ // the old PASV and PORT instead and try again.
+ if (currentCmd.startsWith(QLatin1String("EPSV"))) {
+ transferConnectionExtended = false;
+ pendingCommands.prepend(QLatin1String("PASV\r\n"));
+ } else if (currentCmd.startsWith(QLatin1String("EPRT"))) {
+ transferConnectionExtended = false;
+ pendingCommands.prepend(QLatin1String("PORT\r\n"));
+ } else {
+ emit error(QFtp::UnknownError, replyText);
+ }
+ if (state != Waiting) {
+ state = Idle;
+ startNextCmd();
+ }
+ break;
+ }
+#if defined(QFTPPI_DEBUG)
+// qDebug("QFtpPI state: %d [processReply() end]", state);
+#endif
+ return true;
+}
+
+/*
+ Starts next pending command. Returns false if there are no pending commands,
+ otherwise it returns true.
+*/
+bool QFtpPI::startNextCmd()
+{
+ if (waitForDtpToConnect)
+ // don't process any new commands until we are connected
+ return true;
+
+#if defined(QFTPPI_DEBUG)
+ if (state != Idle)
+ qDebug("QFtpPI startNextCmd: Internal error! QFtpPI called in non-Idle state %d", state);
+#endif
+ if (pendingCommands.isEmpty()) {
+ currentCmd.clear();
+ emit finished(replyText);
+ return false;
+ }
+ currentCmd = pendingCommands.first();
+
+ // PORT and PASV are edited in-place, depending on whether we
+ // should try the extended transfer connection commands EPRT and
+ // EPSV. The PORT command also triggers setting up a listener, and
+ // the address/port arguments are edited in.
+ QHostAddress address = commandSocket.localAddress();
+ if (currentCmd.startsWith(QLatin1String("PORT"))) {
+ if ((address.protocol() == QTcpSocket::IPv6Protocol) && transferConnectionExtended) {
+ int port = dtp.setupListener(address);
+ currentCmd = QLatin1String("EPRT |");
+ currentCmd += (address.protocol() == QTcpSocket::IPv4Protocol) ? QLatin1Char('1') : QLatin1Char('2');
+ currentCmd += QLatin1Char('|') + address.toString() + QLatin1Char('|') + QString::number(port);
+ currentCmd += QLatin1Char('|');
+ } else if (address.protocol() == QTcpSocket::IPv4Protocol) {
+ int port = dtp.setupListener(address);
+ QString portArg;
+ quint32 ip = address.toIPv4Address();
+ portArg += QString::number((ip & 0xff000000) >> 24);
+ portArg += QLatin1Char(',') + QString::number((ip & 0xff0000) >> 16);
+ portArg += QLatin1Char(',') + QString::number((ip & 0xff00) >> 8);
+ portArg += QLatin1Char(',') + QString::number(ip & 0xff);
+ portArg += QLatin1Char(',') + QString::number((port & 0xff00) >> 8);
+ portArg += QLatin1Char(',') + QString::number(port & 0xff);
+
+ currentCmd = QLatin1String("PORT ");
+ currentCmd += portArg;
+ } else {
+ // No IPv6 connection can be set up with the PORT
+ // command.
+ return false;
+ }
+
+ currentCmd += QLatin1String("\r\n");
+ } else if (currentCmd.startsWith(QLatin1String("PASV"))) {
+ if ((address.protocol() == QTcpSocket::IPv6Protocol) && transferConnectionExtended)
+ currentCmd = QLatin1String("EPSV\r\n");
+ }
+
+ pendingCommands.pop_front();
+#if defined(QFTPPI_DEBUG)
+ qDebug("QFtpPI send: %s", currentCmd.left(currentCmd.length()-2).toLatin1().constData());
+#endif
+ state = Waiting;
+ commandSocket.write(currentCmd.toLatin1());
+ return true;
+}
+
+void QFtpPI::dtpConnectState(int s)
+{
+ switch (s) {
+ case QFtpDTP::CsClosed:
+ if (waitForDtpToClose) {
+ // there is an unprocessed reply
+ if (processReply())
+ replyText = QLatin1String("");
+ else
+ return;
+ }
+ waitForDtpToClose = false;
+ readyRead();
+ return;
+ case QFtpDTP::CsConnected:
+ waitForDtpToConnect = false;
+ startNextCmd();
+ return;
+ case QFtpDTP::CsHostNotFound:
+ case QFtpDTP::CsConnectionRefused:
+ emit error(QFtp::ConnectionRefused,
+ QFtp::tr("Connection refused for data connection"));
+ startNextCmd();
+ return;
+ default:
+ return;
+ }
+}
+
+/**********************************************************************
+ *
+ * QFtpPrivate
+ *
+ *********************************************************************/
+
+class QFtpPrivate : public QObjectPrivate
+{
+ Q_DECLARE_PUBLIC(QFtp)
+public:
+
+ inline QFtpPrivate() : close_waitForStateChange(false), state(QFtp::Unconnected),
+ transferMode(QFtp::Passive), error(QFtp::NoError)
+ { }
+
+ ~QFtpPrivate() { while (!pending.isEmpty()) delete pending.takeFirst(); }
+
+ // private slots
+ void _q_startNextCommand();
+ void _q_piFinished(const QString&);
+ void _q_piError(int, const QString&);
+ void _q_piConnectState(int);
+ void _q_piFtpReply(int, const QString&);
+
+ int addCommand(QFtpCommand *cmd);
+
+ QFtpPI pi;
+ QList<QFtpCommand *> pending;
+ bool close_waitForStateChange;
+ QFtp::State state;
+ QFtp::TransferMode transferMode;
+ QFtp::Error error;
+ QString errorString;
+
+ QString host;
+ quint16 port;
+ QString proxyHost;
+ quint16 proxyPort;
+};
+
+int QFtpPrivate::addCommand(QFtpCommand *cmd)
+{
+ pending.append(cmd);
+
+ if (pending.count() == 1) {
+ // don't emit the commandStarted() signal before the ID is returned
+ QTimer::singleShot(0, q_func(), SLOT(_q_startNextCommand()));
+ }
+ return cmd->id;
+}
+
+/**********************************************************************
+ *
+ * QFtp implementation
+ *
+ *********************************************************************/
+/*!
+ \class QFtp
+ \brief The QFtp class provides an implementation of the client side of FTP protocol.
+
+ \ingroup network
+ \inmodule QtNetwork
+
+
+ This class provides a direct interface to FTP that allows you to
+ have more control over the requests. However, for new
+ applications, it is recommended to use QNetworkAccessManager and
+ QNetworkReply, as those classes possess a simpler, yet more
+ powerful API.
+
+ The class works asynchronously, so there are no blocking
+ functions. If an operation cannot be executed immediately, the
+ function will still return straight away and the operation will be
+ scheduled for later execution. The results of scheduled operations
+ are reported via signals. This approach depends on the event loop
+ being in operation.
+
+ The operations that can be scheduled (they are called "commands"
+ in the rest of the documentation) are the following:
+ connectToHost(), login(), close(), list(), cd(), get(), put(),
+ remove(), mkdir(), rmdir(), rename() and rawCommand().
+
+ All of these commands return a unique identifier that allows you
+ to keep track of the command that is currently being executed.
+ When the execution of a command starts, the commandStarted()
+ signal with the command's identifier is emitted. When the command
+ is finished, the commandFinished() signal is emitted with the
+ command's identifier and a bool that indicates whether the command
+ finished with an error.
+
+ In some cases, you might want to execute a sequence of commands,
+ e.g. if you want to connect and login to a FTP server. This is
+ simply achieved:
+
+ \snippet doc/src/snippets/code/src_network_access_qftp.cpp 0
+
+ In this case two FTP commands have been scheduled. When the last
+ scheduled command has finished, a done() signal is emitted with
+ a bool argument that tells you whether the sequence finished with
+ an error.
+
+ If an error occurs during the execution of one of the commands in
+ a sequence of commands, all the pending commands (i.e. scheduled,
+ but not yet executed commands) are cleared and no signals are
+ emitted for them.
+
+ Some commands, e.g. list(), emit additional signals to report
+ their results.
+
+ Example: If you want to download the INSTALL file from the Qt
+ FTP server, you would write this:
+
+ \snippet doc/src/snippets/code/src_network_access_qftp.cpp 1
+
+ For this example the following sequence of signals is emitted
+ (with small variations, depending on network traffic, etc.):
+
+ \snippet doc/src/snippets/code/src_network_access_qftp.cpp 2
+
+ The dataTransferProgress() signal in the above example is useful
+ if you want to show a \link QProgressBar progress bar \endlink to
+ inform the user about the progress of the download. The
+ readyRead() signal tells you that there is data ready to be read.
+ The amount of data can be queried then with the bytesAvailable()
+ function and it can be read with the read() or readAll()
+ function.
+
+ If the login fails for the above example, the signals would look
+ like this:
+
+ \snippet doc/src/snippets/code/src_network_access_qftp.cpp 3
+
+ You can then get details about the error with the error() and
+ errorString() functions.
+
+ For file transfer, QFtp can use both active or passive mode, and
+ it uses passive file transfer mode by default; see the
+ documentation for setTransferMode() for more details about this.
+
+ Call setProxy() to make QFtp connect via an FTP proxy server.
+
+ The functions currentId() and currentCommand() provide more
+ information about the currently executing command.
+
+ The functions hasPendingCommands() and clearPendingCommands()
+ allow you to query and clear the list of pending commands.
+
+ If you are an experienced network programmer and want to have
+ complete control you can use rawCommand() to execute arbitrary FTP
+ commands.
+
+ \warning The current version of QFtp doesn't fully support
+ non-Unix FTP servers.
+
+ \sa QNetworkAccessManager, QNetworkRequest, QNetworkReply,
+ {FTP Example}
+*/
+
+
+/*!
+ Constructs a QFtp object with the given \a parent.
+*/
+QFtp::QFtp(QObject *parent)
+ : QObject(*new QFtpPrivate, parent)
+{
+ Q_D(QFtp);
+ d->errorString = tr("Unknown error");
+
+ connect(&d->pi, SIGNAL(connectState(int)),
+ SLOT(_q_piConnectState(int)));
+ connect(&d->pi, SIGNAL(finished(QString)),
+ SLOT(_q_piFinished(QString)));
+ connect(&d->pi, SIGNAL(error(int,QString)),
+ SLOT(_q_piError(int,QString)));
+ connect(&d->pi, SIGNAL(rawFtpReply(int,QString)),
+ SLOT(_q_piFtpReply(int,QString)));
+
+ connect(&d->pi.dtp, SIGNAL(readyRead()),
+ SIGNAL(readyRead()));
+ connect(&d->pi.dtp, SIGNAL(dataTransferProgress(qint64,qint64)),
+ SIGNAL(dataTransferProgress(qint64,qint64)));
+ connect(&d->pi.dtp, SIGNAL(listInfo(QUrlInfo)),
+ SIGNAL(listInfo(QUrlInfo)));
+}
+
+#ifdef QT3_SUPPORT
+/*!
+ Use one of the constructors that doesn't take the \a name
+ argument and then use setObjectName() instead.
+*/
+QFtp::QFtp(QObject *parent, const char *name)
+ : QObject(*new QFtpPrivate, parent)
+{
+ Q_D(QFtp);
+ setObjectName(QLatin1String(name));
+ d->errorString = tr("Unknown error");
+
+ connect(&d->pi, SIGNAL(connectState(int)),
+ SLOT(_q_piConnectState(int)));
+ connect(&d->pi, SIGNAL(finished(QString)),
+ SLOT(_q_piFinished(QString)));
+ connect(&d->pi, SIGNAL(error(int,QString)),
+ SLOT(_q_piError(int,QString)));
+ connect(&d->pi, SIGNAL(rawFtpReply(int,QString)),
+ SLOT(_q_piFtpReply(int,QString)));
+
+ connect(&d->pi.dtp, SIGNAL(readyRead()),
+ SIGNAL(readyRead()));
+ connect(&d->pi.dtp, SIGNAL(dataTransferProgress(qint64,qint64)),
+ SIGNAL(dataTransferProgress(qint64,qint64)));
+ connect(&d->pi.dtp, SIGNAL(listInfo(QUrlInfo)),
+ SIGNAL(listInfo(QUrlInfo)));
+}
+#endif
+
+/*!
+ \enum QFtp::State
+
+ This enum defines the connection state:
+
+ \value Unconnected There is no connection to the host.
+ \value HostLookup A host name lookup is in progress.
+ \value Connecting An attempt to connect to the host is in progress.
+ \value Connected Connection to the host has been achieved.
+ \value LoggedIn Connection and user login have been achieved.
+ \value Closing The connection is closing down, but it is not yet
+ closed. (The state will be \c Unconnected when the connection is
+ closed.)
+
+ \sa stateChanged() state()
+*/
+/*!
+ \enum QFtp::TransferMode
+
+ FTP works with two socket connections; one for commands and
+ another for transmitting data. While the command connection is
+ always initiated by the client, the second connection can be
+ initiated by either the client or the server.
+
+ This enum defines whether the client (Passive mode) or the server
+ (Active mode) should set up the data connection.
+
+ \value Passive The client connects to the server to transmit its
+ data.
+
+ \value Active The server connects to the client to transmit its
+ data.
+*/
+/*!
+ \enum QFtp::TransferType
+
+ This enum identifies the data transfer type used with get and
+ put commands.
+
+ \value Binary The data will be transferred in Binary mode.
+
+ \value Ascii The data will be transferred in Ascii mode and new line
+ characters will be converted to the local format.
+*/
+/*!
+ \enum QFtp::Error
+
+ This enum identifies the error that occurred.
+
+ \value NoError No error occurred.
+ \value HostNotFound The host name lookup failed.
+ \value ConnectionRefused The server refused the connection.
+ \value NotConnected Tried to send a command, but there is no connection to
+ a server.
+ \value UnknownError An error other than those specified above
+ occurred.
+
+ \sa error()
+*/
+
+/*!
+ \enum QFtp::Command
+
+ This enum is used as the return value for the currentCommand() function.
+ This allows you to perform specific actions for particular
+ commands, e.g. in a FTP client, you might want to clear the
+ directory view when a list() command is started; in this case you
+ can simply check in the slot connected to the start() signal if
+ the currentCommand() is \c List.
+
+ \value None No command is being executed.
+ \value SetTransferMode set the \link TransferMode transfer\endlink mode.
+ \value SetProxy switch proxying on or off.
+ \value ConnectToHost connectToHost() is being executed.
+ \value Login login() is being executed.
+ \value Close close() is being executed.
+ \value List list() is being executed.
+ \value Cd cd() is being executed.
+ \value Get get() is being executed.
+ \value Put put() is being executed.
+ \value Remove remove() is being executed.
+ \value Mkdir mkdir() is being executed.
+ \value Rmdir rmdir() is being executed.
+ \value Rename rename() is being executed.
+ \value RawCommand rawCommand() is being executed.
+
+ \sa currentCommand()
+*/
+
+/*!
+ \fn void QFtp::stateChanged(int state)
+
+ This signal is emitted when the state of the connection changes.
+ The argument \a state is the new state of the connection; it is
+ one of the \l State values.
+
+ It is usually emitted in response to a connectToHost() or close()
+ command, but it can also be emitted "spontaneously", e.g. when the
+ server closes the connection unexpectedly.
+
+ \sa connectToHost() close() state() State
+*/
+
+/*!
+ \fn void QFtp::listInfo(const QUrlInfo &i);
+
+ This signal is emitted for each directory entry the list() command
+ finds. The details of the entry are stored in \a i.
+
+ \sa list()
+*/
+
+/*!
+ \fn void QFtp::commandStarted(int id)
+
+ This signal is emitted when processing the command identified by
+ \a id starts.
+
+ \sa commandFinished() done()
+*/
+
+/*!
+ \fn void QFtp::commandFinished(int id, bool error)
+
+ This signal is emitted when processing the command identified by
+ \a id has finished. \a error is true if an error occurred during
+ the processing; otherwise \a error is false.
+
+ \sa commandStarted() done() error() errorString()
+*/
+
+/*!
+ \fn void QFtp::done(bool error)
+
+ This signal is emitted when the last pending command has finished;
+ (it is emitted after the last command's commandFinished() signal).
+ \a error is true if an error occurred during the processing;
+ otherwise \a error is false.
+
+ \sa commandFinished() error() errorString()
+*/
+
+/*!
+ \fn void QFtp::readyRead()
+
+ This signal is emitted in response to a get() command when there
+ is new data to read.
+
+ If you specify a device as the second argument in the get()
+ command, this signal is \e not emitted; instead the data is
+ written directly to the device.
+
+ You can read the data with the readAll() or read() functions.
+
+ This signal is useful if you want to process the data in chunks as
+ soon as it becomes available. If you are only interested in the
+ complete data, just connect to the commandFinished() signal and
+ read the data then instead.
+
+ \sa get() read() readAll() bytesAvailable()
+*/
+
+/*!
+ \fn void QFtp::dataTransferProgress(qint64 done, qint64 total)
+
+ This signal is emitted in response to a get() or put() request to
+ indicate the current progress of the download or upload.
+
+ \a done is the amount of data that has already been transferred
+ and \a total is the total amount of data to be read or written. It
+ is possible that the QFtp class is not able to determine the total
+ amount of data that should be transferred, in which case \a total
+ is 0. (If you connect this signal to a QProgressBar, the progress
+ bar shows a busy indicator if the total is 0).
+
+ \warning \a done and \a total are not necessarily the size in
+ bytes, since for large files these values might need to be
+ "scaled" to avoid overflow.
+
+ \sa get(), put(), QProgressBar
+*/
+
+/*!
+ \fn void QFtp::rawCommandReply(int replyCode, const QString &detail);
+
+ This signal is emitted in response to the rawCommand() function.
+ \a replyCode is the 3 digit reply code and \a detail is the text
+ that follows the reply code.
+
+ \sa rawCommand()
+*/
+
+/*!
+ Connects to the FTP server \a host using port \a port.
+
+ The stateChanged() signal is emitted when the state of the
+ connecting process changes, e.g. to \c HostLookup, then \c
+ Connecting, then \c Connected.
+
+ The function does not block and returns immediately. The command
+ is scheduled, and its execution is performed asynchronously. The
+ function returns a unique identifier which is passed by
+ commandStarted() and commandFinished().
+
+ When the command is started the commandStarted() signal is
+ emitted. When it is finished the commandFinished() signal is
+ emitted.
+
+ \sa stateChanged() commandStarted() commandFinished()
+*/
+int QFtp::connectToHost(const QString &host, quint16 port)
+{
+ QStringList cmds;
+ cmds << host;
+ cmds << QString::number((uint)port);
+ int id = d_func()->addCommand(new QFtpCommand(ConnectToHost, cmds));
+ d_func()->pi.transferConnectionExtended = true;
+ return id;
+}
+
+/*!
+ Logs in to the FTP server with the username \a user and the
+ password \a password.
+
+ The stateChanged() signal is emitted when the state of the
+ connecting process changes, e.g. to \c LoggedIn.
+
+ The function does not block and returns immediately. The command
+ is scheduled, and its execution is performed asynchronously. The
+ function returns a unique identifier which is passed by
+ commandStarted() and commandFinished().
+
+ When the command is started the commandStarted() signal is
+ emitted. When it is finished the commandFinished() signal is
+ emitted.
+
+ \sa commandStarted() commandFinished()
+*/
+int QFtp::login(const QString &user, const QString &password)
+{
+ QStringList cmds;
+ cmds << (QLatin1String("USER ") + (user.isNull() ? QLatin1String("anonymous") : user) + QLatin1String("\r\n"));
+ cmds << (QLatin1String("PASS ") + (password.isNull() ? QLatin1String("anonymous@") : password) + QLatin1String("\r\n"));
+ return d_func()->addCommand(new QFtpCommand(Login, cmds));
+}
+
+/*!
+ Closes the connection to the FTP server.
+
+ The stateChanged() signal is emitted when the state of the
+ connecting process changes, e.g. to \c Closing, then \c
+ Unconnected.
+
+ The function does not block and returns immediately. The command
+ is scheduled, and its execution is performed asynchronously. The
+ function returns a unique identifier which is passed by
+ commandStarted() and commandFinished().
+
+ When the command is started the commandStarted() signal is
+ emitted. When it is finished the commandFinished() signal is
+ emitted.
+
+ \sa stateChanged() commandStarted() commandFinished()
+*/
+int QFtp::close()
+{
+ return d_func()->addCommand(new QFtpCommand(Close, QStringList(QLatin1String("QUIT\r\n"))));
+}
+
+/*!
+ Sets the current FTP transfer mode to \a mode. The default is QFtp::Passive.
+
+ \sa QFtp::TransferMode
+*/
+int QFtp::setTransferMode(TransferMode mode)
+{
+ int id = d_func()->addCommand(new QFtpCommand(SetTransferMode, QStringList()));
+ d_func()->pi.transferConnectionExtended = true;
+ d_func()->transferMode = mode;
+ return id;
+}
+
+/*!
+ Enables use of the FTP proxy on host \a host and port \a
+ port. Calling this function with \a host empty disables proxying.
+
+ QFtp does not support FTP-over-HTTP proxy servers. Use
+ QNetworkAccessManager for this.
+*/
+int QFtp::setProxy(const QString &host, quint16 port)
+{
+ QStringList args;
+ args << host << QString::number(port);
+ return d_func()->addCommand(new QFtpCommand(SetProxy, args));
+}
+
+/*!
+ Lists the contents of directory \a dir on the FTP server. If \a
+ dir is empty, it lists the contents of the current directory.
+
+ The listInfo() signal is emitted for each directory entry found.
+
+ The function does not block and returns immediately. The command
+ is scheduled, and its execution is performed asynchronously. The
+ function returns a unique identifier which is passed by
+ commandStarted() and commandFinished().
+
+ When the command is started the commandStarted() signal is
+ emitted. When it is finished the commandFinished() signal is
+ emitted.
+
+ \sa listInfo() commandStarted() commandFinished()
+*/
+int QFtp::list(const QString &dir)
+{
+ QStringList cmds;
+ cmds << QLatin1String("TYPE A\r\n");
+ cmds << QLatin1String(d_func()->transferMode == Passive ? "PASV\r\n" : "PORT\r\n");
+ if (dir.isEmpty())
+ cmds << QLatin1String("LIST\r\n");
+ else
+ cmds << (QLatin1String("LIST ") + dir + QLatin1String("\r\n"));
+ return d_func()->addCommand(new QFtpCommand(List, cmds));
+}
+
+/*!
+ Changes the working directory of the server to \a dir.
+
+ The function does not block and returns immediately. The command
+ is scheduled, and its execution is performed asynchronously. The
+ function returns a unique identifier which is passed by
+ commandStarted() and commandFinished().
+
+ When the command is started the commandStarted() signal is
+ emitted. When it is finished the commandFinished() signal is
+ emitted.
+
+ \sa commandStarted() commandFinished()
+*/
+int QFtp::cd(const QString &dir)
+{
+ return d_func()->addCommand(new QFtpCommand(Cd, QStringList(QLatin1String("CWD ") + dir + QLatin1String("\r\n"))));
+}
+
+/*!
+ Downloads the file \a file from the server.
+
+ If \a dev is 0, then the readyRead() signal is emitted when there
+ is data available to read. You can then read the data with the
+ read() or readAll() functions.
+
+ If \a dev is not 0, the data is written directly to the device \a
+ dev. Make sure that the \a dev pointer is valid for the duration
+ of the operation (it is safe to delete it when the
+ commandFinished() signal is emitted). In this case the readyRead()
+ signal is \e not emitted and you cannot read data with the
+ read() or readAll() functions.
+
+ If you don't read the data immediately it becomes available, i.e.
+ when the readyRead() signal is emitted, it is still available
+ until the next command is started.
+
+ For example, if you want to present the data to the user as soon
+ as there is something available, connect to the readyRead() signal
+ and read the data immediately. On the other hand, if you only want
+ to work with the complete data, you can connect to the
+ commandFinished() signal and read the data when the get() command
+ is finished.
+
+ The data is transferred as Binary or Ascii depending on the value
+ of \a type.
+
+ The function does not block and returns immediately. The command
+ is scheduled, and its execution is performed asynchronously. The
+ function returns a unique identifier which is passed by
+ commandStarted() and commandFinished().
+
+ When the command is started the commandStarted() signal is
+ emitted. When it is finished the commandFinished() signal is
+ emitted.
+
+ \sa readyRead() dataTransferProgress() commandStarted()
+ commandFinished()
+*/
+int QFtp::get(const QString &file, QIODevice *dev, TransferType type)
+{
+ QStringList cmds;
+ if (type == Binary)
+ cmds << QLatin1String("TYPE I\r\n");
+ else
+ cmds << QLatin1String("TYPE A\r\n");
+ cmds << QLatin1String("SIZE ") + file + QLatin1String("\r\n");
+ cmds << QLatin1String(d_func()->transferMode == Passive ? "PASV\r\n" : "PORT\r\n");
+ cmds << QLatin1String("RETR ") + file + QLatin1String("\r\n");
+ return d_func()->addCommand(new QFtpCommand(Get, cmds, dev));
+}
+
+/*!
+ \overload
+
+ Writes a copy of the given \a data to the file called \a file on
+ the server. The progress of the upload is reported by the
+ dataTransferProgress() signal.
+
+ The data is transferred as Binary or Ascii depending on the value
+ of \a type.
+
+ The function does not block and returns immediately. The command
+ is scheduled, and its execution is performed asynchronously. The
+ function returns a unique identifier which is passed by
+ commandStarted() and commandFinished().
+
+ When the command is started the commandStarted() signal is
+ emitted. When it is finished the commandFinished() signal is
+ emitted.
+
+ Since this function takes a copy of the \a data, you can discard
+ your own copy when this function returns.
+
+ \sa dataTransferProgress() commandStarted() commandFinished()
+*/
+int QFtp::put(const QByteArray &data, const QString &file, TransferType type)
+{
+ QStringList cmds;
+ if (type == Binary)
+ cmds << QLatin1String("TYPE I\r\n");
+ else
+ cmds << QLatin1String("TYPE A\r\n");
+ cmds << QLatin1String(d_func()->transferMode == Passive ? "PASV\r\n" : "PORT\r\n");
+ cmds << QLatin1String("ALLO ") + QString::number(data.size()) + QLatin1String("\r\n");
+ cmds << QLatin1String("STOR ") + file + QLatin1String("\r\n");
+ return d_func()->addCommand(new QFtpCommand(Put, cmds, data));
+}
+
+/*!
+ Reads the data from the IO device \a dev, and writes it to the
+ file called \a file on the server. The data is read in chunks from
+ the IO device, so this overload allows you to transmit large
+ amounts of data without the need to read all the data into memory
+ at once.
+
+ The data is transferred as Binary or Ascii depending on the value
+ of \a type.
+
+ Make sure that the \a dev pointer is valid for the duration of the
+ operation (it is safe to delete it when the commandFinished() is
+ emitted).
+*/
+int QFtp::put(QIODevice *dev, const QString &file, TransferType type)
+{
+ QStringList cmds;
+ if (type == Binary)
+ cmds << QLatin1String("TYPE I\r\n");
+ else
+ cmds << QLatin1String("TYPE A\r\n");
+ cmds << QLatin1String(d_func()->transferMode == Passive ? "PASV\r\n" : "PORT\r\n");
+ if (!dev->isSequential())
+ cmds << QLatin1String("ALLO ") + QString::number(dev->size()) + QLatin1String("\r\n");
+ cmds << QLatin1String("STOR ") + file + QLatin1String("\r\n");
+ return d_func()->addCommand(new QFtpCommand(Put, cmds, dev));
+}
+
+/*!
+ Deletes the file called \a file from the server.
+
+ The function does not block and returns immediately. The command
+ is scheduled, and its execution is performed asynchronously. The
+ function returns a unique identifier which is passed by
+ commandStarted() and commandFinished().
+
+ When the command is started the commandStarted() signal is
+ emitted. When it is finished the commandFinished() signal is
+ emitted.
+
+ \sa commandStarted() commandFinished()
+*/
+int QFtp::remove(const QString &file)
+{
+ return d_func()->addCommand(new QFtpCommand(Remove, QStringList(QLatin1String("DELE ") + file + QLatin1String("\r\n"))));
+}
+
+/*!
+ Creates a directory called \a dir on the server.
+
+ The function does not block and returns immediately. The command
+ is scheduled, and its execution is performed asynchronously. The
+ function returns a unique identifier which is passed by
+ commandStarted() and commandFinished().
+
+ When the command is started the commandStarted() signal is
+ emitted. When it is finished the commandFinished() signal is
+ emitted.
+
+ \sa commandStarted() commandFinished()
+*/
+int QFtp::mkdir(const QString &dir)
+{
+ return d_func()->addCommand(new QFtpCommand(Mkdir, QStringList(QLatin1String("MKD ") + dir + QLatin1String("\r\n"))));
+}
+
+/*!
+ Removes the directory called \a dir from the server.
+
+ The function does not block and returns immediately. The command
+ is scheduled, and its execution is performed asynchronously. The
+ function returns a unique identifier which is passed by
+ commandStarted() and commandFinished().
+
+ When the command is started the commandStarted() signal is
+ emitted. When it is finished the commandFinished() signal is
+ emitted.
+
+ \sa commandStarted() commandFinished()
+*/
+int QFtp::rmdir(const QString &dir)
+{
+ return d_func()->addCommand(new QFtpCommand(Rmdir, QStringList(QLatin1String("RMD ") + dir + QLatin1String("\r\n"))));
+}
+
+/*!
+ Renames the file called \a oldname to \a newname on the server.
+
+ The function does not block and returns immediately. The command
+ is scheduled, and its execution is performed asynchronously. The
+ function returns a unique identifier which is passed by
+ commandStarted() and commandFinished().
+
+ When the command is started the commandStarted() signal is
+ emitted. When it is finished the commandFinished() signal is
+ emitted.
+
+ \sa commandStarted() commandFinished()
+*/
+int QFtp::rename(const QString &oldname, const QString &newname)
+{
+ QStringList cmds;
+ cmds << QLatin1String("RNFR ") + oldname + QLatin1String("\r\n");
+ cmds << QLatin1String("RNTO ") + newname + QLatin1String("\r\n");
+ return d_func()->addCommand(new QFtpCommand(Rename, cmds));
+}
+
+/*!
+ Sends the raw FTP command \a command to the FTP server. This is
+ useful for low-level FTP access. If the operation you wish to
+ perform has an equivalent QFtp function, we recommend using the
+ function instead of raw FTP commands since the functions are
+ easier and safer.
+
+ The function does not block and returns immediately. The command
+ is scheduled, and its execution is performed asynchronously. The
+ function returns a unique identifier which is passed by
+ commandStarted() and commandFinished().
+
+ When the command is started the commandStarted() signal is
+ emitted. When it is finished the commandFinished() signal is
+ emitted.
+
+ \sa rawCommandReply() commandStarted() commandFinished()
+*/
+int QFtp::rawCommand(const QString &command)
+{
+ QString cmd = command.trimmed() + QLatin1String("\r\n");
+ return d_func()->addCommand(new QFtpCommand(RawCommand, QStringList(cmd)));
+}
+
+/*!
+ Returns the number of bytes that can be read from the data socket
+ at the moment.
+
+ \sa get() readyRead() read() readAll()
+*/
+qint64 QFtp::bytesAvailable() const
+{
+ return d_func()->pi.dtp.bytesAvailable();
+}
+
+/*! \fn qint64 QFtp::readBlock(char *data, quint64 maxlen)
+
+ Use read() instead.
+*/
+
+/*!
+ Reads \a maxlen bytes from the data socket into \a data and
+ returns the number of bytes read. Returns -1 if an error occurred.
+
+ \sa get() readyRead() bytesAvailable() readAll()
+*/
+qint64 QFtp::read(char *data, qint64 maxlen)
+{
+ return d_func()->pi.dtp.read(data, maxlen);
+}
+
+/*!
+ Reads all the bytes available from the data socket and returns
+ them.
+
+ \sa get() readyRead() bytesAvailable() read()
+*/
+QByteArray QFtp::readAll()
+{
+ return d_func()->pi.dtp.readAll();
+}
+
+/*!
+ Aborts the current command and deletes all scheduled commands.
+
+ If there is an unfinished command (i.e. a command for which the
+ commandStarted() signal has been emitted, but for which the
+ commandFinished() signal has not been emitted), this function
+ sends an \c ABORT command to the server. When the server replies
+ that the command is aborted, the commandFinished() signal with the
+ \c error argument set to \c true is emitted for the command. Due
+ to timing issues, it is possible that the command had already
+ finished before the abort request reached the server, in which
+ case, the commandFinished() signal is emitted with the \c error
+ argument set to \c false.
+
+ For all other commands that are affected by the abort(), no
+ signals are emitted.
+
+ If you don't start further FTP commands directly after the
+ abort(), there won't be any scheduled commands and the done()
+ signal is emitted.
+
+ \warning Some FTP servers, for example the BSD FTP daemon (version
+ 0.3), wrongly return a positive reply even when an abort has
+ occurred. For these servers the commandFinished() signal has its
+ error flag set to \c false, even though the command did not
+ complete successfully.
+
+ \sa clearPendingCommands()
+*/
+void QFtp::abort()
+{
+ if (d_func()->pending.isEmpty())
+ return;
+
+ clearPendingCommands();
+ d_func()->pi.abort();
+}
+
+/*!
+ Returns the identifier of the FTP command that is being executed
+ or 0 if there is no command being executed.
+
+ \sa currentCommand()
+*/
+int QFtp::currentId() const
+{
+ if (d_func()->pending.isEmpty())
+ return 0;
+ return d_func()->pending.first()->id;
+}
+
+/*!
+ Returns the command type of the FTP command being executed or \c
+ None if there is no command being executed.
+
+ \sa currentId()
+*/
+QFtp::Command QFtp::currentCommand() const
+{
+ if (d_func()->pending.isEmpty())
+ return None;
+ return d_func()->pending.first()->command;
+}
+
+/*!
+ Returns the QIODevice pointer that is used by the FTP command to read data
+ from or store data to. If there is no current FTP command being executed or
+ if the command does not use an IO device, this function returns 0.
+
+ This function can be used to delete the QIODevice in the slot connected to
+ the commandFinished() signal.
+
+ \sa get() put()
+*/
+QIODevice* QFtp::currentDevice() const
+{
+ if (d_func()->pending.isEmpty())
+ return 0;
+ QFtpCommand *c = d_func()->pending.first();
+ if (c->is_ba)
+ return 0;
+ return c->data.dev;
+}
+
+/*!
+ Returns true if there are any commands scheduled that have not yet
+ been executed; otherwise returns false.
+
+ The command that is being executed is \e not considered as a
+ scheduled command.
+
+ \sa clearPendingCommands() currentId() currentCommand()
+*/
+bool QFtp::hasPendingCommands() const
+{
+ return d_func()->pending.count() > 1;
+}
+
+/*!
+ Deletes all pending commands from the list of scheduled commands.
+ This does not affect the command that is being executed. If you
+ want to stop this as well, use abort().
+
+ \sa hasPendingCommands() abort()
+*/
+void QFtp::clearPendingCommands()
+{
+ // delete all entires except the first one
+ while (d_func()->pending.count() > 1)
+ delete d_func()->pending.takeLast();
+}
+
+/*!
+ Returns the current state of the object. When the state changes,
+ the stateChanged() signal is emitted.
+
+ \sa State stateChanged()
+*/
+QFtp::State QFtp::state() const
+{
+ return d_func()->state;
+}
+
+/*!
+ Returns the last error that occurred. This is useful to find out
+ what went wrong when receiving a commandFinished() or a done()
+ signal with the \c error argument set to \c true.
+
+ If you start a new command, the error status is reset to \c NoError.
+*/
+QFtp::Error QFtp::error() const
+{
+ return d_func()->error;
+}
+
+/*!
+ Returns a human-readable description of the last error that
+ occurred. This is useful for presenting a error message to the
+ user when receiving a commandFinished() or a done() signal with
+ the \c error argument set to \c true.
+
+ The error string is often (but not always) the reply from the
+ server, so it is not always possible to translate the string. If
+ the message comes from Qt, the string has already passed through
+ tr().
+*/
+QString QFtp::errorString() const
+{
+ return d_func()->errorString;
+}
+
+/*! \internal
+*/
+void QFtpPrivate::_q_startNextCommand()
+{
+ Q_Q(QFtp);
+ if (pending.isEmpty())
+ return;
+ QFtpCommand *c = pending.first();
+
+ error = QFtp::NoError;
+ errorString = QT_TRANSLATE_NOOP(QFtp, QLatin1String("Unknown error"));
+
+ if (q->bytesAvailable())
+ q->readAll(); // clear the data
+ emit q->commandStarted(c->id);
+
+ // Proxy support, replace the Login argument in place, then fall
+ // through.
+ if (c->command == QFtp::Login && !proxyHost.isEmpty()) {
+ QString loginString = c->rawCmds.first().trimmed();
+ loginString += QLatin1Char('@') + host;
+ if (port && port != 21)
+ loginString += QLatin1Char(':') + QString::number(port);
+ loginString += QLatin1String("\r\n");
+ c->rawCmds[0] = loginString;
+ }
+
+ if (c->command == QFtp::SetTransferMode) {
+ _q_piFinished(QLatin1String("Transfer mode set"));
+ } else if (c->command == QFtp::SetProxy) {
+ proxyHost = c->rawCmds[0];
+ proxyPort = c->rawCmds[1].toUInt();
+ c->rawCmds.clear();
+ _q_piFinished(QLatin1String("Proxy set to ") + proxyHost + QLatin1Char(':') + QString::number(proxyPort));
+ } else if (c->command == QFtp::ConnectToHost) {
+#ifndef QT_NO_BEARERMANAGEMENT
+ //copy network session down to the PI
+ pi.setProperty("_q_networksession", q->property("_q_networksession"));
+#endif
+ if (!proxyHost.isEmpty()) {
+ host = c->rawCmds[0];
+ port = c->rawCmds[1].toUInt();
+ pi.connectToHost(proxyHost, proxyPort);
+ } else {
+ pi.connectToHost(c->rawCmds[0], c->rawCmds[1].toUInt());
+ }
+ } else {
+ if (c->command == QFtp::Put) {
+ if (c->is_ba) {
+ pi.dtp.setData(c->data.ba);
+ pi.dtp.setBytesTotal(c->data.ba->size());
+ } else if (c->data.dev && (c->data.dev->isOpen() || c->data.dev->open(QIODevice::ReadOnly))) {
+ pi.dtp.setDevice(c->data.dev);
+ if (c->data.dev->isSequential()) {
+ pi.dtp.setBytesTotal(0);
+ pi.dtp.connect(c->data.dev, SIGNAL(readyRead()), SLOT(dataReadyRead()));
+ pi.dtp.connect(c->data.dev, SIGNAL(readChannelFinished()), SLOT(dataReadyRead()));
+ } else {
+ pi.dtp.setBytesTotal(c->data.dev->size());
+ }
+ }
+ } else if (c->command == QFtp::Get) {
+ if (!c->is_ba && c->data.dev) {
+ pi.dtp.setDevice(c->data.dev);
+ }
+ } else if (c->command == QFtp::Close) {
+ state = QFtp::Closing;
+ emit q->stateChanged(state);
+ }
+ pi.sendCommands(c->rawCmds);
+ }
+}
+
+/*! \internal
+*/
+void QFtpPrivate::_q_piFinished(const QString&)
+{
+ if (pending.isEmpty())
+ return;
+ QFtpCommand *c = pending.first();
+
+ if (c->command == QFtp::Close) {
+ // The order of in which the slots are called is arbitrary, so
+ // disconnect the SIGNAL-SIGNAL temporary to make sure that we
+ // don't get the commandFinished() signal before the stateChanged()
+ // signal.
+ if (state != QFtp::Unconnected) {
+ close_waitForStateChange = true;
+ return;
+ }
+ }
+ emit q_func()->commandFinished(c->id, false);
+ pending.removeFirst();
+
+ delete c;
+
+ if (pending.isEmpty()) {
+ emit q_func()->done(false);
+ } else {
+ _q_startNextCommand();
+ }
+}
+
+/*! \internal
+*/
+void QFtpPrivate::_q_piError(int errorCode, const QString &text)
+{
+ Q_Q(QFtp);
+
+ if (pending.isEmpty()) {
+ qWarning("QFtpPrivate::_q_piError was called without pending command!");
+ return;
+ }
+
+ QFtpCommand *c = pending.first();
+
+ // non-fatal errors
+ if (c->command == QFtp::Get && pi.currentCommand().startsWith(QLatin1String("SIZE "))) {
+ pi.dtp.setBytesTotal(0);
+ return;
+ } else if (c->command==QFtp::Put && pi.currentCommand().startsWith(QLatin1String("ALLO "))) {
+ return;
+ }
+
+ error = QFtp::Error(errorCode);
+ switch (q->currentCommand()) {
+ case QFtp::ConnectToHost:
+ errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QFtp", "Connecting to host failed:\n%1"))
+ .arg(text);
+ break;
+ case QFtp::Login:
+ errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QFtp", "Login failed:\n%1"))
+ .arg(text);
+ break;
+ case QFtp::List:
+ errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QFtp", "Listing directory failed:\n%1"))
+ .arg(text);
+ break;
+ case QFtp::Cd:
+ errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QFtp", "Changing directory failed:\n%1"))
+ .arg(text);
+ break;
+ case QFtp::Get:
+ errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QFtp", "Downloading file failed:\n%1"))
+ .arg(text);
+ break;
+ case QFtp::Put:
+ errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QFtp", "Uploading file failed:\n%1"))
+ .arg(text);
+ break;
+ case QFtp::Remove:
+ errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QFtp", "Removing file failed:\n%1"))
+ .arg(text);
+ break;
+ case QFtp::Mkdir:
+ errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QFtp", "Creating directory failed:\n%1"))
+ .arg(text);
+ break;
+ case QFtp::Rmdir:
+ errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QFtp", "Removing directory failed:\n%1"))
+ .arg(text);
+ break;
+ default:
+ errorString = text;
+ break;
+ }
+
+ pi.clearPendingCommands();
+ q->clearPendingCommands();
+ emit q->commandFinished(c->id, true);
+
+ pending.removeFirst();
+ delete c;
+ if (pending.isEmpty())
+ emit q->done(true);
+ else
+ _q_startNextCommand();
+}
+
+/*! \internal
+*/
+void QFtpPrivate::_q_piConnectState(int connectState)
+{
+ state = QFtp::State(connectState);
+ emit q_func()->stateChanged(state);
+ if (close_waitForStateChange) {
+ close_waitForStateChange = false;
+ _q_piFinished(QLatin1String(QT_TRANSLATE_NOOP("QFtp", "Connection closed")));
+ }
+}
+
+/*! \internal
+*/
+void QFtpPrivate::_q_piFtpReply(int code, const QString &text)
+{
+ if (q_func()->currentCommand() == QFtp::RawCommand) {
+ pi.rawCommand = true;
+ emit q_func()->rawCommandReply(code, text);
+ }
+}
+
+/*!
+ Destructor.
+*/
+QFtp::~QFtp()
+{
+ abort();
+ close();
+}
+
+QT_END_NAMESPACE
+
+#include "qftp.moc"
+
+#include "moc_qftp.cpp"
+
+#endif // QT_NO_FTP
diff --git a/src/qftp/qftp.h b/src/qftp/qftp.h
new file mode 100644
index 0000000..2a58344
--- /dev/null
+++ b/src/qftp/qftp.h
@@ -0,0 +1,180 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtNetwork module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QFTP_H
+#define QFTP_H
+
+#include <QtCore/qstring.h>
+#include <QtNetwork/qurlinfo.h>
+#include <QtCore/qobject.h>
+
+QT_BEGIN_HEADER
+
+QT_BEGIN_NAMESPACE
+
+QT_MODULE(Network)
+
+#ifndef QT_NO_FTP
+
+class QFtpPrivate;
+
+class Q_NETWORK_EXPORT QFtp : public QObject
+{
+ Q_OBJECT
+
+public:
+ explicit QFtp(QObject *parent = 0);
+ virtual ~QFtp();
+
+ enum State {
+ Unconnected,
+ HostLookup,
+ Connecting,
+ Connected,
+ LoggedIn,
+ Closing
+ };
+ enum Error {
+ NoError,
+ UnknownError,
+ HostNotFound,
+ ConnectionRefused,
+ NotConnected
+ };
+ enum Command {
+ None,
+ SetTransferMode,
+ SetProxy,
+ ConnectToHost,
+ Login,
+ Close,
+ List,
+ Cd,
+ Get,
+ Put,
+ Remove,
+ Mkdir,
+ Rmdir,
+ Rename,
+ RawCommand
+ };
+ enum TransferMode {
+ Active,
+ Passive
+ };
+ enum TransferType {
+ Binary,
+ Ascii
+ };
+
+ int setProxy(const QString &host, quint16 port);
+ int connectToHost(const QString &host, quint16 port=21);
+ int login(const QString &user = QString(), const QString &password = QString());
+ int close();
+ int setTransferMode(TransferMode mode);
+ int list(const QString &dir = QString());
+ int cd(const QString &dir);
+ int get(const QString &file, QIODevice *dev=0, TransferType type = Binary);
+ int put(const QByteArray &data, const QString &file, TransferType type = Binary);
+ int put(QIODevice *dev, const QString &file, TransferType type = Binary);
+ int remove(const QString &file);
+ int mkdir(const QString &dir);
+ int rmdir(const QString &dir);
+ int rename(const QString &oldname, const QString &newname);
+
+ int rawCommand(const QString &command);
+
+ qint64 bytesAvailable() const;
+ qint64 read(char *data, qint64 maxlen);
+#ifdef QT3_SUPPORT
+ inline QT3_SUPPORT qint64 readBlock(char *data, quint64 maxlen)
+ { return read(data, qint64(maxlen)); }
+#endif
+ QByteArray readAll();
+
+ int currentId() const;
+ QIODevice* currentDevice() const;
+ Command currentCommand() const;
+ bool hasPendingCommands() const;
+ void clearPendingCommands();
+
+ State state() const;
+
+ Error error() const;
+ QString errorString() const;
+
+public Q_SLOTS:
+ void abort();
+
+Q_SIGNALS:
+ void stateChanged(int);
+ void listInfo(const QUrlInfo&);
+ void readyRead();
+ void dataTransferProgress(qint64, qint64);
+ void rawCommandReply(int, const QString&);
+
+ void commandStarted(int);
+ void commandFinished(int, bool);
+ void done(bool);
+
+#ifdef QT3_SUPPORT
+public:
+ QT3_SUPPORT_CONSTRUCTOR QFtp(QObject *parent, const char *name);
+#endif
+
+private:
+ Q_DISABLE_COPY(QFtp)
+ Q_DECLARE_PRIVATE(QFtp)
+
+ Q_PRIVATE_SLOT(d_func(), void _q_startNextCommand())
+ Q_PRIVATE_SLOT(d_func(), void _q_piFinished(const QString&))
+ Q_PRIVATE_SLOT(d_func(), void _q_piError(int, const QString&))
+ Q_PRIVATE_SLOT(d_func(), void _q_piConnectState(int))
+ Q_PRIVATE_SLOT(d_func(), void _q_piFtpReply(int, const QString&))
+};
+
+#endif // QT_NO_FTP
+
+QT_END_NAMESPACE
+
+QT_END_HEADER
+
+#endif // QFTP_H
diff --git a/tests/auto/qftp/.gitattributes b/tests/auto/qftp/.gitattributes
new file mode 100644
index 0000000..e04709a
--- /dev/null
+++ b/tests/auto/qftp/.gitattributes
@@ -0,0 +1 @@
+rfc3252.txt -crlf
diff --git a/tests/auto/qftp/.gitignore b/tests/auto/qftp/.gitignore
new file mode 100644
index 0000000..7a4845d
--- /dev/null
+++ b/tests/auto/qftp/.gitignore
@@ -0,0 +1,2 @@
+tst_qftp
+tst_QFtp_activeMode_inittab
diff --git a/tests/auto/qftp/qftp.pro b/tests/auto/qftp/qftp.pro
new file mode 100644
index 0000000..0f5bd5f
--- /dev/null
+++ b/tests/auto/qftp/qftp.pro
@@ -0,0 +1,17 @@
+CONFIG += testcase
+TARGET = tst_qftp
+SOURCES += tst_qftp.cpp
+
+
+QT = core network network-private testlib
+
+wince*: {
+ addFiles.files = rfc3252.txt
+ addFiles.path = .
+ DEPLOYMENT += addFiles
+ DEFINES += SRCDIR=\\\"\\\"
+} else {
+ DEFINES += SRCDIR=\\\"$$PWD/\\\"
+}
+
+CONFIG+=insignificant_test # uses live qt-test-server, inherently unstable
diff --git a/tests/auto/qftp/rfc3252.txt b/tests/auto/qftp/rfc3252.txt
new file mode 100644
index 0000000..b80c61b
--- /dev/null
+++ b/tests/auto/qftp/rfc3252.txt
@@ -0,0 +1,899 @@
+
+
+
+
+
+
+Network Working Group H. Kennedy
+Request for Comments: 3252 Mimezine
+Category: Informational 1 April 2002
+
+
+ Binary Lexical Octet Ad-hoc Transport
+
+Status of this Memo
+
+ This memo provides information for the Internet community. It does
+ not specify an Internet standard of any kind. Distribution of this
+ memo is unlimited.
+
+Copyright Notice
+
+ Copyright (C) The Internet Society (2002). All Rights Reserved.
+
+Abstract
+
+ This document defines a reformulation of IP and two transport layer
+ protocols (TCP and UDP) as XML applications.
+
+1. Introduction
+
+1.1. Overview
+
+ This document describes the Binary Lexical Octet Ad-hoc Transport
+ (BLOAT): a reformulation of a widely-deployed network-layer protocol
+ (IP [RFC791]), and two associated transport layer protocols (TCP
+ [RFC793] and UDP [RFC768]) as XML [XML] applications. It also
+ describes methods for transporting BLOAT over Ethernet and IEEE 802
+ networks as well as encapsulating BLOAT in IP for gatewaying BLOAT
+ across the public Internet.
+
+1.2. Motivation
+
+ The wild popularity of XML as a basis for application-level protocols
+ such as the Blocks Extensible Exchange Protocol [RFC3080], the Simple
+ Object Access Protocol [SOAP], and Jabber [JABBER] prompted
+ investigation into the possibility of extending the use of XML in the
+ protocol stack. Using XML at both the transport and network layer in
+ addition to the application layer would provide for an amazing amount
+ of power and flexibility while removing dependencies on proprietary
+ and hard-to-understand binary protocols. This protocol unification
+ would also allow applications to use a single XML parser for all
+ aspects of their operation, eliminating developer time spent figuring
+ out the intricacies of each new protocol, and moving the hard work of
+
+
+
+
+Kennedy Informational [Page 1]
+
+RFC 3252 Binary Lexical Octet Ad-hoc Transport 1 April 2002
+
+
+ parsing to the XML toolset. The use of XML also mitigates concerns
+ over "network vs. host" byte ordering which is at the root of many
+ network application bugs.
+
+1.3. Relation to Existing Protocols
+
+ The reformulations specified in this RFC follow as closely as
+ possible the spirit of the RFCs on which they are based, and so MAY
+ contain elements or attributes that would not be needed in a pure
+ reworking (e.g. length attributes, which are implicit in XML.)
+
+ The layering of network and transport protocols are maintained in
+ this RFC despite the optimizations that could be made if the line
+ were somewhat blurred (i.e. merging TCP and IP into a single, larger
+ element in the DTD) in order to foster future use of this protocol as
+ a basis for reformulating other protocols (such as ICMP.)
+
+ Other than the encoding, the behavioral aspects of each of the
+ existing protocols remain unchanged. Routing, address spaces, TCP
+ congestion control, etc. behave as specified in the extant standards.
+ Adapting to new standards and experimental algorithm heuristics for
+ improving performance will become much easier once the move to BLOAT
+ has been completed.
+
+1.4. Requirement Levels
+
+ The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
+ "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
+ document are to be interpreted as described in BCP 14, RFC 2119
+ [RFC2119].
+
+2. IPoXML
+
+ This protocol MUST be implemented to be compliant with this RFC.
+ IPoXML is the root protocol REQUIRED for effective use of TCPoXML
+ (section 3.) and higher-level application protocols.
+
+ The DTD for this document type can be found in section 7.1.
+
+ The routing of IPoXML can be easily implemented on hosts with an XML
+ parser, as the regular structure lends itself handily to parsing and
+ validation of the document/datagram and then processing the
+ destination address, TTL, and checksum before sending it on to its
+ next-hop.
+
+ The reformulation of IPv4 was chosen over IPv6 [RFC2460] due to the
+ wider deployment of IPv4 and the fact that implementing IPv6 as XML
+ would have exceeded the 1500 byte Ethernet MTU.
+
+
+
+Kennedy Informational [Page 2]
+
+RFC 3252 Binary Lexical Octet Ad-hoc Transport 1 April 2002
+
+
+ All BLOAT implementations MUST use - and specify - the UTF-8 encoding
+ of RFC 2279 [RFC2279]. All BLOAT document/datagrams MUST be well-
+ formed and include the XMLDecl.
+
+2.1. IP Description
+
+ A number of items have changed (for the better) from the original IP
+ specification. Bit-masks, where present have been converted into
+ human-readable values. IP addresses are listed in their dotted-
+ decimal notation [RFC1123]. Length and checksum values are present
+ as decimal integers.
+
+ To calculate the length and checksum fields of the IP element, a
+ canonicalized form of the element MUST be used. The canonical form
+ SHALL have no whitespace (including newline characters) between
+ elements and only one space character between attributes. There
+ SHALL NOT be a space following the last attribute in an element.
+
+ An iterative method SHOULD be used to calculate checksums, as the
+ length field will vary based on the size of the checksum.
+
+ The payload element bears special attention. Due to the character
+ set restrictions of XML, the payload of IP datagrams (which MAY
+ contain arbitrary data) MUST be encoded for transport. This RFC
+ REQUIRES the contents of the payload to be encoded in the base-64
+ encoding of RFC 2045 [RFC2045], but removes the requirement that the
+ encoded output MUST be wrapped on 76-character lines.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Kennedy Informational [Page 3]
+
+RFC 3252 Binary Lexical Octet Ad-hoc Transport 1 April 2002
+
+
+2.2. Example Datagram
+
+ The following is an example IPoXML datagram with an empty payload:
+
+ <?xml version="1.0" encoding="UTF-8"?>
+ <!DOCTYPE ip PUBLIC "-//IETF//DTD BLOAT 1.0 IP//EN" "bloat.dtd">
+ <ip>
+ <header length="474">
+ <version value="4"/>
+ <tos precedence="Routine" delay="Normal" throughput="Normal"
+ relibility="Normal" reserved="0"/>
+ <total.length value="461"/>
+ <id value="1"/>
+ <flags reserved="0" df="dont" mf="last"/>
+ <offset value="0"/>
+ <ttl value="255"/>
+ <protocol value="6"/>
+ <checksum value="8707"/>
+ <source address="10.0.0.22"/>
+ <destination address="10.0.0.1"/>
+ <options>
+ <end copied="0" class="0" number="0"/>
+ </options>
+ <padding pad="0"/>
+ </header>
+ <payload>
+ </payload>
+ </ip>
+
+3. TCPoXML
+
+ This protocol MUST be implemented to be compliant with this RFC. The
+ DTD for this document type can be found in section 7.2.
+
+3.1. TCP Description
+
+ A number of items have changed from the original TCP specification.
+ Bit-masks, where present have been converted into human-readable
+ values. Length and checksum and port values are present as decimal
+ integers.
+
+ To calculate the length and checksum fields of the TCP element, a
+ canonicalized form of the element MUST be used as in section 2.1.
+
+ An iterative method SHOULD be used to calculate checksums as in
+ section 2.1.
+
+ The payload element MUST be encoded as in section 2.1.
+
+
+
+Kennedy Informational [Page 4]
+
+RFC 3252 Binary Lexical Octet Ad-hoc Transport 1 April 2002
+
+
+ The TCP offset element was expanded to a maximum of 255 from 16 to
+ allow for the increased size of the header in XML.
+
+ TCPoXML datagrams encapsulated by IPoXML MAY omit the <?xml?> header
+ as well as the <!DOCTYPE> declaration.
+
+3.2. Example Datagram
+
+ The following is an example TCPoXML datagram with an empty payload:
+
+ <?xml version="1.0" encoding="UTF-8"?>
+ <!DOCTYPE tcp PUBLIC "-//IETF//DTD BLOAT 1.0 TCP//EN" "bloat.dtd">
+ <tcp>
+ <tcp.header>
+ <src port="31415"/>
+ <dest port="42424"/>
+ <sequence number="322622954"/>
+ <acknowledgement number="689715995"/>
+ <offset number=""/>
+ <reserved value="0"/>
+ <control syn="1" ack="1"/>
+ <window size="1"/>
+ <urgent pointer="0"/>
+ <checksum value="2988"/>
+ <tcp.options>
+ <tcp.end kind="0"/>
+ </tcp.options>
+ <padding pad="0"/>
+ </tcp.header>
+ <payload>
+ </payload>
+ </tcp>
+
+4. UDPoXML
+
+ This protocol MUST be implemented to be compliant with this RFC. The
+ DTD for this document type can be found in section 7.3.
+
+4.1. UDP Description
+
+ A number of items have changed from the original UDP specification.
+ Bit-masks, where present have been converted into human-readable
+ values. Length and checksum and port values are present as decimal
+ integers.
+
+
+
+
+
+
+
+Kennedy Informational [Page 5]
+
+RFC 3252 Binary Lexical Octet Ad-hoc Transport 1 April 2002
+
+
+ To calculate the length and checksum fields of the UDP element, a
+ canonicalized form of the element MUST be used as in section 2.1. An
+ iterative method SHOULD be used to calculate checksums as in section
+ 2.1.
+
+ The payload element MUST be encoded as in section 2.1.
+
+ UDPoXML datagrams encapsulated by IPoXML MAY omit the <?xml?> header
+ as well as the <!DOCTYPE> declaration.
+
+4.2. Example Datagram
+
+ The following is an example UDPoXML datagram with an empty payload:
+
+ <?xml version="1.0" encoding="UTF-8"?>
+ <!DOCTYPE udp PUBLIC "-//IETF//DTD BLOAT 1.0 UDP//EN" "bloat.dtd">
+ <udp>
+ <udp.header>
+ <src port="31415"/>
+ <dest port="42424"/>
+ <udp.length value="143"/>
+ <checksum value="2988"/>
+ </udp.header>
+ <payload>
+ </payload>
+ </udp>
+
+5. Network Transport
+
+ This document provides for the transmission of BLOAT datagrams over
+ two common families of physical layer transport. Future RFCs will
+ address additional transports as routing vendors catch up to the
+ specification, and we begin to see BLOAT routed across the Internet
+ backbone.
+
+5.1. Ethernet
+
+ BLOAT is encapsulated in Ethernet datagrams as in [RFC894] with the
+ exception that the type field of the Ethernet frame MUST contain the
+ value 0xBEEF. The first 5 octets of the Ethernet frame payload will
+ be 0x3c 3f 78 6d 6c ("<?xml".)
+
+5.2. IEEE 802
+
+ BLOAT is encapsulated in IEEE 802 Networks as in [RFC1042] except
+ that the protocol type code for IPoXML is 0xBEEF.
+
+
+
+
+
+Kennedy Informational [Page 6]
+
+RFC 3252 Binary Lexical Octet Ad-hoc Transport 1 April 2002
+
+
+6. Gatewaying over IP
+
+ In order to facilitate the gradual introduction of BLOAT into the
+ public Internet, BLOAT MAY be encapsulated in IP as in [RFC2003] to
+ gateway between networks that run BLOAT natively on their LANs.
+
+7. DTDs
+
+ The Transport DTDs (7.2. and 7.3.) build on the definitions in the
+ Network DTD (7.1.)
+
+ The DTDs are referenced by their PubidLiteral and SystemLiteral (from
+ [XML]) although it is understood that most IPoXML implementations
+ will not need to pull down the DTD, as it will normally be embedded
+ in the implementation, and presents something of a catch-22 if you
+ need to load part of your network protocol over the network.
+
+7.1. IPoXML DTD
+
+ <!--
+ DTD for IP over XML.
+ Refer to this DTD as:
+
+ <!DOCTYPE ip PUBLIC "-//IETF//DTD BLOAT 1.0 IP//EN" "bloat.dtd">
+ -->
+ <!--
+ DTD data types:
+
+ Digits [0..9]+
+
+ Precedence "NetworkControl | InternetworkControl |
+ CRITIC | FlashOverride | Flash | Immediate |
+ Priority | Routine"
+
+ IP4Addr "dotted-decimal" notation of [RFC1123]
+
+ Class [0..3]
+
+ Sec "Unclassified | Confidential | EFTO | MMMM | PROG |
+ Restricted | Secret | Top Secret | Reserved"
+
+ Compartments [0..65535]
+
+ Handling [0..65535]
+
+ TCC [0..16777216]
+
+ -->
+
+
+
+Kennedy Informational [Page 7]
+
+RFC 3252 Binary Lexical Octet Ad-hoc Transport 1 April 2002
+
+
+ <!ENTITY % Digits "CDATA">
+ <!ENTITY % Precedence "CDATA">
+ <!ENTITY % IP4Addr "CDATA">
+ <!ENTITY % Class "CDATA">
+ <!ENTITY % Sec "CDATA">
+ <!ENTITY % Compartments "CDATA">
+ <!ENTITY % Handling "CDATA">
+ <!ENTITY % TCC "CDATA">
+
+ <!ELEMENT ip (header, payload)>
+
+ <!ELEMENT header (version, tos, total.length, id, flags, offset, ttl,
+ protocol, checksum, source, destination, options,
+ padding)>
+ <!-- length of header in 32-bit words -->
+ <!ATTLIST header
+ length %Digits; #REQUIRED>
+
+ <!ELEMENT version EMPTY>
+ <!-- ip version. SHOULD be "4" -->
+ <!ATTLIST version
+ value %Digits; #REQUIRED>
+
+ <!ELEMENT tos EMPTY>
+ <!ATTLIST tos
+ precedence %Precedence; #REQUIRED
+ delay (normal | low) #REQUIRED
+ throughput (normal | high) #REQUIRED
+ relibility (normal | high) #REQUIRED
+ reserved CDATA #FIXED "0">
+
+ <!ELEMENT total.length EMPTY>
+ <!--
+ total length of datagram (header and payload) in octets, MUST be
+ less than 65,535 (and SHOULD be less than 1024 for IPoXML on local
+ ethernets).
+ -->
+ <!ATTLIST total.length
+ value %Digits; #REQUIRED>
+
+ <!ELEMENT id EMPTY>
+ <!-- 0 <= id <= 65,535 -->
+ <!ATTLIST id
+ value %Digits; #REQUIRED>
+
+ <!ELEMENT flags EMPTY>
+ <!-- df = don't fragment, mf = more fragments -->
+ <!ATTLIST flags
+
+
+
+Kennedy Informational [Page 8]
+
+RFC 3252 Binary Lexical Octet Ad-hoc Transport 1 April 2002
+
+
+ reserved CDATA #FIXED "0"
+ df (may|dont) #REQUIRED
+ mf (last|more) #REQUIRED>
+
+ <!ELEMENT offset EMPTY>
+ <!-- 0 <= offset <= 8192 measured in 8 octet (64-bit) chunks -->
+ <!ATTLIST offset
+ value %Digits; #REQUIRED>
+
+ <!ELEMENT ttl EMPTY>
+ <!-- 0 <= ttl <= 255 -->
+ <!ATTLIST ttl
+ value %Digits; #REQUIRED>
+
+ <!ELEMENT protocol EMPTY>
+ <!-- 0 <= protocol <= 255 (per IANA) -->
+ <!ATTLIST protocol
+ value %Digits; #REQUIRED>
+
+ <!ELEMENT checksum EMPTY>
+ <!-- 0 <= checksum <= 65535 (over header only) -->
+ <!ATTLIST checksum
+ value %Digits; #REQUIRED>
+
+ <!ELEMENT source EMPTY>
+ <!ATTLIST source
+ address %IP4Addr; #REQUIRED>
+
+ <!ELEMENT destination EMPTY>
+ <!ATTLIST destination
+ address %IP4Addr; #REQUIRED>
+
+ <!ELEMENT options ( end | noop | security | loose | strict | record
+ | stream | timestamp )*>
+
+ <!ELEMENT end EMPTY>
+ <!ATTLIST end
+ copied (0|1) #REQUIRED
+ class CDATA #FIXED "0"
+ number CDATA #FIXED "0">
+
+ <!ELEMENT noop EMPTY>
+ <!ATTLIST noop
+ copied (0|1) #REQUIRED
+ class CDATA #FIXED "0"
+ number CDATA #FIXED "1">
+
+ <!ELEMENT security EMPTY>
+
+
+
+Kennedy Informational [Page 9]
+
+RFC 3252 Binary Lexical Octet Ad-hoc Transport 1 April 2002
+
+
+ <!ATTLIST security
+ copied CDATA #FIXED "1"
+ class CDATA #FIXED "0"
+ number CDATA #FIXED "2"
+ length CDATA #FIXED "11"
+ security %Sec; #REQUIRED
+ compartments %Compartments; #REQUIRED
+ handling %Handling; #REQUIRED
+ tcc %TCC; #REQUIRED>
+ <!ELEMENT loose (hop)+>
+ <!ATTLIST loose
+ copied CDATA #FIXED "1"
+ class CDATA #FIXED "0"
+ number CDATA #FIXED "3"
+ length %Digits; #REQUIRED
+ pointer %Digits; #REQUIRED>
+
+ <!ELEMENT hop EMPTY>
+ <!ATTLIST hop
+ address %IP4Addr; #REQUIRED>
+
+ <!ELEMENT strict (hop)+>
+ <!ATTLIST strict
+ copied CDATA #FIXED "1"
+ class CDATA #FIXED "0"
+ number CDATA #FIXED "9"
+ length %Digits; #REQUIRED
+ pointer %Digits; #REQUIRED>
+
+ <!ELEMENT record (hop)+>
+ <!ATTLIST record
+ copied CDATA #FIXED "0"
+ class CDATA #FIXED "0"
+ number CDATA #FIXED "7"
+ length %Digits; #REQUIRED
+ pointer %Digits; #REQUIRED>
+
+ <!ELEMENT stream EMPTY>
+ <!-- 0 <= id <= 65,535 -->
+ <!ATTLIST stream
+ copied CDATA #FIXED "1"
+ class CDATA #FIXED "0"
+ number CDATA #FIXED "8"
+ length CDATA #FIXED "4"
+ id %Digits; #REQUIRED>
+
+ <!ELEMENT timestamp (tstamp)+>
+ <!-- 0 <= oflw <=15 -->
+
+
+
+Kennedy Informational [Page 10]
+
+RFC 3252 Binary Lexical Octet Ad-hoc Transport 1 April 2002
+
+
+ <!ATTLIST timestamp
+ copied CDATA #FIXED "0"
+ class CDATA #FIXED "2"
+ number CDATA #FIXED "4"
+ length %Digits; #REQUIRED
+ pointer %Digits; #REQUIRED
+ oflw %Digits; #REQUIRED
+ flag (0 | 1 | 3) #REQUIRED>
+
+ <!ELEMENT tstamp EMPTY>
+ <!ATTLIST tstamp
+ time %Digits; #REQUIRED
+ address %IP4Addr; #IMPLIED>
+ <!--
+ padding to bring header to 32-bit boundary.
+ pad MUST be "0"*
+ -->
+ <!ELEMENT padding EMPTY>
+ <!ATTLIST padding
+ pad CDATA #REQUIRED>
+
+ <!-- payload MUST be encoded as base-64 [RFC2045], as modified
+ by section 2.1 of this RFC -->
+ <!ELEMENT payload (CDATA)>
+
+7.2. TCPoXML DTD
+
+ <!--
+ DTD for TCP over XML.
+ Refer to this DTD as:
+
+ <!DOCTYPE tcp PUBLIC "-//IETF//DTD BLOAT 1.0 TCP//EN" "bloat.dtd">
+ -->
+
+ <!-- the pseudoheader is only included for checksum calculations -->
+ <!ELEMENT tcp (tcp.pseudoheader?, tcp.header, payload)>
+
+ <!ELEMENT tcp.header (src, dest, sequence, acknowledgement, offset,
+ reserved, control, window, checksum, urgent,
+ tcp.options, padding)>
+
+ <!ELEMENT src EMPTY>
+ <!-- 0 <= port <= 65,535 -->
+ <!ATTLIST src
+ port %Digits; #REQUIRED>
+
+ <!ELEMENT dest EMPTY>
+ <!-- 0 <= port <= 65,535 -->
+
+
+
+Kennedy Informational [Page 11]
+
+RFC 3252 Binary Lexical Octet Ad-hoc Transport 1 April 2002
+
+
+ <!ATTLIST dest
+ port %Digits; #REQUIRED>
+
+ <!ELEMENT sequence EMPTY>
+ <!-- 0 <= number <= 4294967295 -->
+ <!ATTLIST sequence
+ number %Digits; #REQUIRED>
+
+ <!ELEMENT acknowledgement EMPTY>
+ <!-- 0 <= number <= 4294967295 -->
+ <!ATTLIST acknowledgement
+ number %Digits; #REQUIRED>
+
+ <!ELEMENT offset EMPTY>
+ <!-- 0 <= number <= 255 -->
+ <!ATTLIST offset
+ number %Digits; #REQUIRED>
+
+ <!ELEMENT reserved EMPTY>
+ <!ATTLIST reserved
+ value CDATA #FIXED "0">
+
+ <!ELEMENT control EMPTY>
+ <!ATTLIST control
+ urg (0|1) #IMPLIED
+ ack (0|1) #IMPLIED
+ psh (0|1) #IMPLIED
+ rst (0|1) #IMPLIED
+ syn (0|1) #IMPLIED
+ fin (0|1) #IMPLIED>
+
+ <!ELEMENT window EMPTY>
+ <!-- 0 <= size <= 65,535 -->
+ <!ATTLIST window
+ size %Digits; #REQUIRED>
+
+ <!--
+ checksum as in ip, but with
+ the following pseudo-header added into the tcp element:
+ -->
+ <!ELEMENT tcp.pseudoheader (source, destination, protocol,
+ tcp.length)>
+
+ <!--
+ tcp header + data length in octets. does not include the size of
+
+ the pseudoheader.
+ -->
+
+
+
+Kennedy Informational [Page 12]
+
+RFC 3252 Binary Lexical Octet Ad-hoc Transport 1 April 2002
+
+
+ <!ELEMENT tcp.length EMPTY>
+ <!ATTLIST tcp.length
+ value %Digits; #REQUIRED>
+
+ <!ELEMENT urgent EMPTY>
+ <!-- 0 <= pointer <= 65,535 -->
+ <!ATTLIST urgent
+ pointer %Digits; #REQUIRED>
+
+ <!ELEMENT tcp.options (tcp.end | tcp.noop | tcp.mss)+>
+
+ <!ELEMENT tcp.end EMPTY>
+ <!ATTLIST tcp.end
+ kind CDATA #FIXED "0">
+
+ <!ELEMENT tcp.noop EMPTY>
+ <!ATTLIST tcp.noop
+ kind CDATA #FIXED "1">
+
+ <!ELEMENT tcp.mss EMPTY>
+ <!ATTLIST tcp.mss
+ kind CDATA #FIXED "2"
+ length CDATA #FIXED "4"
+ size %Digits; #REQUIRED>
+
+7.3. UDPoXML DTD
+
+ <!--
+ DTD for UDP over XML.
+ Refer to this DTD as:
+
+ <!DOCTYPE udp PUBLIC "-//IETF//DTD BLOAT 1.0 UDP//EN" "bloat.dtd">
+ -->
+
+ <!ELEMENT udp (udp.pseudoheader?, udp.header, payload)>
+
+ <!ELEMENT udp.header (src, dest, udp.length, checksum)>
+
+ <!ELEMENT udp.pseudoheader (source, destination, protocol,
+ udp.length)>
+
+ <!--
+ udp header + data length in octets. does not include the size of
+ the pseudoheader.
+ -->
+ <!ELEMENT udp.length EMPTY>
+ <!ATTLIST udp.length
+ value %Digits; #REQUIRED>
+
+
+
+Kennedy Informational [Page 13]
+
+RFC 3252 Binary Lexical Octet Ad-hoc Transport 1 April 2002
+
+
+8. Security Considerations
+
+ XML, as a subset of SGML, has the same security considerations as
+ specified in SGML Media Types [RFC1874]. Security considerations
+ that apply to IP, TCP and UDP also likely apply to BLOAT as it does
+ not attempt to correct for issues not related to message format.
+
+9. References
+
+ [JABBER] Miller, J., "Jabber", draft-miller-jabber-00.txt,
+ February 2002. (Work in Progress)
+
+ [RFC768] Postel, J., "User Datagram Protocol", STD 6, RFC 768,
+ August 1980.
+
+ [RFC791] Postel, J., "Internet Protocol", STD 5, RFC 791,
+ September 1981.
+
+ [RFC793] Postel, J., "Transmission Control Protocol", STD 7, RFC
+ 793, September 1981.
+
+ [RFC894] Hornig, C., "Standard for the Transmission of IP
+ Datagrams over Ethernet Networks.", RFC 894, April 1984.
+
+ [RFC1042] Postel, J. and J. Reynolds, "Standard for the
+ Transmission of IP Datagrams Over IEEE 802 Networks", STD
+ 43, RFC 1042, February 1988.
+
+ [RFC1123] Braden, R., "Requirements for Internet Hosts -
+ Application and Support", RFC 1123, October 1989.
+
+ [RFC1874] Levinson, E., "SGML Media Types", RFC 1874, December
+ 1995.
+
+ [RFC2003] Perkins, C., "IP Encapsulation within IP", RFC 2003,
+ October 1996.
+
+ [RFC2045] Freed, N. and N. Borenstein, "Multipurpose Internet Mail
+ Extensions (MIME) Part One: Format of Internet Message
+ Bodies", RFC 2045, November 1996.
+
+ [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate
+ Requirement Levels", BCP 14, RFC 2119, March 1997.
+
+ [RFC2279] Yergeau, F., "UTF-8, a transformation format of ISO
+ 10646", RFC 2279, January 1998.
+
+
+
+
+
+Kennedy Informational [Page 14]
+
+RFC 3252 Binary Lexical Octet Ad-hoc Transport 1 April 2002
+
+
+ [RFC2460] Deering, S. and R. Hinden, "Internet Protocol, Version 6
+ (IPv6) Specification", RFC 2460, December 1998.
+
+ [RFC3080] Rose, M., "The Blocks Extensible Exchange Protocol Core",
+ RFC 3080, March 2001.
+
+ [SOAP] Box, D., Ehnebuske, D., Kakivaya, G., Layman, A.,
+ Mendelsohn, N., Nielsen, H. F., Thatte, S. Winer, D.,
+ "Simple Object Access Protocol (SOAP) 1.1" World Wide Web
+ Consortium Note, May 2000 http://www.w3.org/TR/SOAP/
+
+ [XML] Bray, T., Paoli, J., Sperberg-McQueen, C. M., "Extensible
+ Markup Language (XML)" World Wide Web Consortium
+ Recommendation REC- xml-19980210.
+ http://www.w3.org/TR/1998/REC-xml-19980210
+
+10. Author's Address
+
+ Hugh Kennedy
+ Mimezine
+ 1060 West Addison
+ Chicago, IL 60613
+ USA
+
+ EMail: kennedyh@engin.umich.edu
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Kennedy Informational [Page 15]
+
+RFC 3252 Binary Lexical Octet Ad-hoc Transport 1 April 2002
+
+
+11. Full Copyright Statement
+
+ Copyright (C) The Internet Society (2002). All Rights Reserved.
+
+ This document and translations of it may be copied and furnished to
+ others, and derivative works that comment on or otherwise explain it
+ or assist in its implementation may be prepared, copied, published
+ and distributed, in whole or in part, without restriction of any
+ kind, provided that the above copyright notice and this paragraph are
+ included on all such copies and derivative works. However, this
+ document itself may not be modified in any way, such as by removing
+ the copyright notice or references to the Internet Society or other
+ Internet organizations, except as needed for the purpose of
+ developing Internet standards in which case the procedures for
+ copyrights defined in the Internet Standards process must be
+ followed, or as required to translate it into languages other than
+ English.
+
+ The limited permissions granted above are perpetual and will not be
+ revoked by the Internet Society or its successors or assigns.
+
+ This document and the information contained herein is provided on an
+ "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
+ TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
+ BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
+ HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
+ MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+
+Acknowledgement
+
+ Funding for the RFC Editor function is currently provided by the
+ Internet Society.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Kennedy Informational [Page 16]
+
diff --git a/tests/auto/qftp/tst_qftp.cpp b/tests/auto/qftp/tst_qftp.cpp
new file mode 100644
index 0000000..49e1f4b
--- /dev/null
+++ b/tests/auto/qftp/tst_qftp.cpp
@@ -0,0 +1,2149 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+
+#include <QtTest/QtTest>
+
+#include <qcoreapplication.h>
+#include <qfile.h>
+#include <qbuffer.h>
+#include "qftp.h"
+#include <qmap.h>
+#include <time.h>
+#include <stdlib.h>
+#include <QNetworkProxy>
+#include <QNetworkConfiguration>
+#include <qnetworkconfigmanager.h>
+#include <QNetworkSession>
+#include <QtNetwork/private/qnetworksession_p.h>
+
+#include "../../../network-settings.h"
+
+#ifndef QT_NO_BEARERMANAGEMENT
+Q_DECLARE_METATYPE(QNetworkConfiguration)
+#endif
+
+class tst_QFtp : public QObject
+{
+ Q_OBJECT
+
+public:
+ tst_QFtp();
+ virtual ~tst_QFtp();
+
+
+public slots:
+ void initTestCase_data();
+ void initTestCase();
+ void cleanupTestCase();
+ void init();
+ void cleanup();
+private slots:
+ void connectToHost_data();
+ void connectToHost();
+ void connectToUnresponsiveHost();
+ void login_data();
+ void login();
+ void close_data();
+ void close();
+
+ void list_data();
+ void list();
+ void cd_data();
+ void cd();
+ void get_data();
+ void get();
+ void put_data();
+ void put();
+ void mkdir_data();
+ void mkdir();
+ void mkdir2();
+ void rename_data();
+ void rename();
+
+ void commandSequence_data();
+ void commandSequence();
+
+ void abort_data();
+ void abort();
+
+ void bytesAvailable_data();
+ void bytesAvailable();
+
+ void activeMode();
+
+ void proxy_data();
+ void proxy();
+
+ void binaryAscii();
+
+ void doneSignal();
+ void queueMoreCommandsInDoneSlot();
+
+ void qtbug7359Crash();
+
+protected slots:
+ void stateChanged( int );
+ void listInfo( const QUrlInfo & );
+ void readyRead();
+ void dataTransferProgress(qint64, qint64);
+
+ void commandStarted( int );
+ void commandFinished( int, bool );
+ void done( bool );
+ void activeModeDone( bool );
+ void mkdir2Slot(int id, bool error);
+ void cdUpSlot(bool);
+
+private:
+ QFtp *newFtp();
+ void addCommand( QFtp::Command, int );
+ bool fileExists( const QString &host, quint16 port, const QString &user, const QString &password, const QString &file, const QString &cdDir = QString::null );
+ bool dirExists( const QString &host, quint16 port, const QString &user, const QString &password, const QString &cdDir, const QString &dirToCreate );
+
+ void renameInit( const QString &host, const QString &user, const QString &password, const QString &createFile );
+ void renameCleanup( const QString &host, const QString &user, const QString &password, const QString &fileToDelete );
+
+ QFtp *ftp;
+#ifndef QT_NO_BEARERMANAGEMENT
+ QSharedPointer<QNetworkSession> networkSessionExplicit;
+ QSharedPointer<QNetworkSession> networkSessionImplicit;
+#endif
+
+ QList<int> ids; // helper to make sure that all expected signals are emitted
+ int current_id;
+
+ int connectToHost_state;
+ int close_state;
+ int login_state;
+ int cur_state;
+ struct CommandResult
+ {
+ int id;
+ int success;
+ };
+ QMap< QFtp::Command, CommandResult > resultMap;
+ typedef QMap<QFtp::Command,CommandResult>::Iterator ResMapIt;
+
+ int done_success;
+ int commandSequence_success;
+
+ qlonglong bytesAvailable_finishedGet;
+ qlonglong bytesAvailable_finished;
+ qlonglong bytesAvailable_done;
+
+ QList<QUrlInfo> listInfo_i;
+ QByteArray newData_ba;
+ qlonglong bytesTotal;
+ qlonglong bytesDone;
+
+ bool inFileDirExistsFunction;
+
+ QString uniqueExtension;
+};
+
+//#define DUMP_SIGNALS
+
+const int bytesTotal_init = -10;
+const int bytesDone_init = -10;
+
+tst_QFtp::tst_QFtp() :
+ ftp(0)
+{
+}
+
+tst_QFtp::~tst_QFtp()
+{
+}
+
+void tst_QFtp::initTestCase_data()
+{
+ QTest::addColumn<bool>("setProxy");
+ QTest::addColumn<int>("proxyType");
+ QTest::addColumn<bool>("setSession");
+
+ QTest::newRow("WithoutProxy") << false << 0 << false;
+ QTest::newRow("WithSocks5Proxy") << true << int(QNetworkProxy::Socks5Proxy) << false;
+ //### doesn't work well yet.
+ //QTest::newRow("WithHttpProxy") << true << int(QNetworkProxy::HttpProxy);
+
+#ifndef QT_NO_BEARERMANAGEMENT
+ QTest::newRow("WithoutProxyWithSession") << false << 0 << true;
+ QTest::newRow("WithSocks5ProxyAndSession") << true << int(QNetworkProxy::Socks5Proxy) << true;
+#endif
+}
+
+void tst_QFtp::initTestCase()
+{
+ QVERIFY(QtNetworkSettings::verifyTestNetworkSettings());
+#ifndef QT_NO_BEARERMANAGEMENT
+ QNetworkConfigurationManager manager;
+ networkSessionImplicit = QSharedPointer<QNetworkSession>(new QNetworkSession(manager.defaultConfiguration()));
+ networkSessionImplicit->open();
+ QVERIFY(networkSessionImplicit->waitForOpened(60000)); //there may be user prompt on 1st connect
+#endif
+}
+
+void tst_QFtp::cleanupTestCase()
+{
+#ifndef QT_NO_BEARERMANAGEMENT
+ networkSessionExplicit.clear();
+ networkSessionImplicit.clear();
+#endif
+}
+
+void tst_QFtp::init()
+{
+ QFETCH_GLOBAL(bool, setProxy);
+ QFETCH_GLOBAL(int, proxyType);
+ QFETCH_GLOBAL(bool, setSession);
+ if (setProxy) {
+ if (proxyType == QNetworkProxy::Socks5Proxy) {
+ QNetworkProxy::setApplicationProxy(QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1080));
+ } else if (proxyType == QNetworkProxy::HttpProxy) {
+ QNetworkProxy::setApplicationProxy(QNetworkProxy(QNetworkProxy::HttpProxy, QtNetworkSettings::serverName(), 3128));
+ }
+ }
+#ifndef QT_NO_BEARERMANAGEMENT
+ if (setSession) {
+ networkSessionExplicit = networkSessionImplicit;
+ if (!networkSessionExplicit->isOpen()) {
+ networkSessionExplicit->open();
+ QVERIFY(networkSessionExplicit->waitForOpened(30000));
+ }
+ } else {
+ networkSessionExplicit.clear();
+ }
+#endif
+
+ delete ftp;
+ ftp = 0;
+
+ ids.clear();
+ current_id = 0;
+
+ resultMap.clear();
+ connectToHost_state = -1;
+ close_state = -1;
+ login_state = -1;
+ cur_state = QFtp::Unconnected;
+
+ listInfo_i.clear();
+ newData_ba = QByteArray();
+ bytesTotal = bytesTotal_init;
+ bytesDone = bytesDone_init;
+
+ done_success = -1;
+ commandSequence_success = -1;
+
+ bytesAvailable_finishedGet = 1234567890;
+ bytesAvailable_finished = 1234567890;
+ bytesAvailable_done = 1234567890;
+
+ inFileDirExistsFunction = false;
+
+#if !defined(Q_OS_WINCE)
+ srand(time(0));
+ uniqueExtension = QString("%1%2%3").arg((qulonglong)this).arg(rand()).arg((qulonglong)time(0));
+#else
+ srand(0);
+ uniqueExtension = QString("%1%2%3").arg((qulonglong)this).arg(rand()).arg((qulonglong)(0));
+#endif
+}
+
+void tst_QFtp::cleanup()
+{
+ if (ftp) {
+ delete ftp;
+ ftp = 0;
+ }
+ QFETCH_GLOBAL(bool, setProxy);
+ if (setProxy) {
+ QNetworkProxy::setApplicationProxy(QNetworkProxy::DefaultProxy);
+ }
+
+ delete ftp;
+ ftp = 0;
+#ifndef QT_NO_BEARERMANAGEMENT
+ networkSessionExplicit.clear();
+#endif
+}
+
+void tst_QFtp::connectToHost_data()
+{
+ QTest::addColumn<QString>("host");
+ QTest::addColumn<uint>("port");
+ QTest::addColumn<int>("state");
+
+ QTest::newRow( "ok01" ) << QtNetworkSettings::serverName() << (uint)21 << (int)QFtp::Connected;
+ QTest::newRow( "error01" ) << QtNetworkSettings::serverName() << (uint)2222 << (int)QFtp::Unconnected;
+ QTest::newRow( "error02" ) << QString("foo.bar") << (uint)21 << (int)QFtp::Unconnected;
+}
+
+void tst_QFtp::connectToHost()
+{
+ QFETCH( QString, host );
+ QFETCH( uint, port );
+
+ ftp = newFtp();
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host, port ) );
+
+ QTestEventLoop::instance().enterLoop( 61 );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ QTEST( connectToHost_state, "state" );
+
+ ResMapIt it = resultMap.find( QFtp::ConnectToHost );
+ QVERIFY( it != resultMap.end() );
+ QFETCH( int, state );
+ if ( state == QFtp::Connected ) {
+ QVERIFY( it.value().success == 1 );
+ } else {
+ QVERIFY( it.value().success == 0 );
+ }
+}
+
+void tst_QFtp::connectToUnresponsiveHost()
+{
+ QFETCH_GLOBAL(bool, setProxy);
+ if (setProxy)
+ QSKIP( "This test takes too long if we test with proxies too");
+
+ QString host = "192.0.2.42"; // IP out of TEST-NET, should be unreachable
+ uint port = 21;
+
+ ftp = newFtp();
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host, port ) );
+
+ qDebug( "About to connect to host that won't reply (this test takes 60 seconds)" );
+ QTestEventLoop::instance().enterLoop( 61 );
+#ifdef Q_OS_WIN
+ /* On Windows, we do not get a timeout, because Winsock is behaving in a strange way:
+ We issue two "WSAConnect()" calls, after the first, as a result we get WSAEWOULDBLOCK,
+ after the second, we get WSAEISCONN, which means that the socket is connected, which cannot be.
+ However, after some seconds we get a socket error saying that the remote host closed the connection,
+ which can neither be. For this test, that would actually enable us to finish before timout, but handling that case
+ (in void QFtpPI::error(QAbstractSocket::SocketError e)) breaks
+ a lot of other stuff in QFtp, so we just expect this test to fail on Windows.
+ */
+ QEXPECT_FAIL("", "timeout not working due to strange Windows socket behaviour (see source file of this test for explanation)", Abort);
+#else
+ QEXPECT_FAIL("", "QTBUG-20687", Abort);
+#endif
+ QVERIFY2(! QTestEventLoop::instance().timeout(), "Network timeout longer than expected (should have been 60 seconds)");
+
+ QVERIFY( ftp->state() == QFtp::Unconnected);
+ ResMapIt it = resultMap.find( QFtp::ConnectToHost );
+ QVERIFY( it != resultMap.end() );
+ QVERIFY( it.value().success == 0 );
+
+ delete ftp;
+ ftp = 0;
+}
+
+void tst_QFtp::login_data()
+{
+ QTest::addColumn<QString>("host");
+ QTest::addColumn<uint>("port");
+ QTest::addColumn<QString>("user");
+ QTest::addColumn<QString>("password");
+ QTest::addColumn<int>("success");
+
+ QTest::newRow( "ok01" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString() << 1;
+ QTest::newRow( "ok02" ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftp") << QString() << 1;
+ QTest::newRow( "ok03" ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftp") << QString("foo") << 1;
+ QTest::newRow( "ok04" ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftptest") << QString("password") << 1;
+
+ QTest::newRow( "error01" ) << QtNetworkSettings::serverName() << (uint)21 << QString("foo") << QString() << 0;
+ QTest::newRow( "error02" ) << QtNetworkSettings::serverName() << (uint)21 << QString("foo") << QString("bar") << 0;
+}
+
+void tst_QFtp::login()
+{
+ QFETCH( QString, host );
+ QFETCH( uint, port );
+ QFETCH( QString, user );
+ QFETCH( QString, password );
+
+ ftp = newFtp();
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host, port ) );
+ addCommand( QFtp::Login, ftp->login( user, password ) );
+
+ QTestEventLoop::instance().enterLoop( 30 );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ ResMapIt it = resultMap.find( QFtp::Login );
+ QVERIFY( it != resultMap.end() );
+ QTEST( it.value().success, "success" );
+
+ if ( it.value().success ) {
+ QVERIFY( login_state == QFtp::LoggedIn );
+ } else {
+ QVERIFY( login_state != QFtp::LoggedIn );
+ }
+}
+
+void tst_QFtp::close_data()
+{
+ QTest::addColumn<QString>("host");
+ QTest::addColumn<uint>("port");
+ QTest::addColumn<QString>("user");
+ QTest::addColumn<QString>("password");
+ QTest::addColumn<bool>("login");
+
+ QTest::newRow( "login01" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString() << true;
+ QTest::newRow( "login02" ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftp") << QString() << true;
+ QTest::newRow( "login03" ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftp") << QString("foo") << true;
+ QTest::newRow( "login04" ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftptest") << QString("password") << true;
+
+ QTest::newRow( "no-login01" ) << QtNetworkSettings::serverName() << (uint)21 << QString("") << QString("") << false;
+}
+
+void tst_QFtp::close()
+{
+ QFETCH( QString, host );
+ QFETCH( uint, port );
+ QFETCH( QString, user );
+ QFETCH( QString, password );
+ QFETCH( bool, login );
+
+ ftp = newFtp();
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host, port ) );
+ if ( login )
+ addCommand( QFtp::Login, ftp->login( user, password ) );
+ addCommand( QFtp::Close, ftp->close() );
+
+ QTestEventLoop::instance().enterLoop( 30 );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ QCOMPARE( close_state, (int)QFtp::Unconnected );
+
+ ResMapIt it = resultMap.find( QFtp::Close );
+ QVERIFY( it != resultMap.end() );
+ QVERIFY( it.value().success == 1 );
+}
+
+void tst_QFtp::list_data()
+{
+ QTest::addColumn<QString>("host");
+ QTest::addColumn<uint>("port");
+ QTest::addColumn<QString>("user");
+ QTest::addColumn<QString>("password");
+ QTest::addColumn<QString>("dir");
+ QTest::addColumn<int>("success");
+ QTest::addColumn<QStringList>("entryNames"); // ### we should rather use a QList<QUrlInfo> here
+
+ QStringList flukeRoot;
+ flukeRoot << "pub";
+ flukeRoot << "qtest";
+ QStringList flukeQtest;
+ flukeQtest << "bigfile";
+ flukeQtest << "nonASCII";
+ flukeQtest << "rfc3252";
+ flukeQtest << "rfc3252.txt";
+ flukeQtest << "upload";
+
+ QTest::newRow( "workDir01" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString() << QString() << 1 << flukeRoot;
+ QTest::newRow( "workDir02" ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftptest") << QString("password") << QString() << 1 << flukeRoot;
+
+ QTest::newRow( "relPath01" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString() << QString("qtest") << 1 << flukeQtest;
+ QTest::newRow( "relPath02" ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftptest") << QString("password") << QString("qtest") << 1 << flukeQtest;
+
+ QTest::newRow( "absPath01" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString() << QString("/qtest") << 1 << flukeQtest;
+ QTest::newRow( "absPath02" ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftptest") << QString("password") << QString("/var/ftp/qtest") << 1 << flukeQtest;
+
+ QTest::newRow( "nonExist01" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString() << QString("foo") << 1 << QStringList();
+ QTest::newRow( "nonExist02" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString() << QString("/foo") << 1 << QStringList();
+ // ### The microsoft server does not seem to work properly at the moment --
+ // I am also not able to open a data connection with other, non-Qt FTP
+ // clients to it.
+ // QTest::newRow( "nonExist03" ) << "ftp.microsoft.com" << (uint)21 << QString() << QString() << QString("/foo") << 0 << QStringList();
+
+ QStringList susePub;
+ susePub << "README.mirror-policy" << "axp" << "i386" << "ia64" << "install" << "noarch" << "pubring.gpg-build.suse.de" << "update" << "x86_64";
+ QTest::newRow( "epsvNotSupported" ) << QString("ftp.funet.fi") << (uint)21 << QString::fromLatin1("ftp") << QString::fromLatin1("root@") << QString("/pub/Linux/suse/suse") << 1 << susePub;
+}
+
+void tst_QFtp::list()
+{
+ QFETCH( QString, host );
+ QFETCH( uint, port );
+ QFETCH( QString, user );
+ QFETCH( QString, password );
+ QFETCH( QString, dir );
+
+ ftp = newFtp();
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host, port ) );
+ addCommand( QFtp::Login, ftp->login( user, password ) );
+ addCommand( QFtp::List, ftp->list( dir ) );
+ addCommand( QFtp::Close, ftp->close() );
+
+ QTestEventLoop::instance().enterLoop( 30 );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ ResMapIt it = resultMap.find( QFtp::List );
+ QVERIFY( it != resultMap.end() );
+ QTEST( it.value().success, "success" );
+ QFETCH( QStringList, entryNames );
+ QCOMPARE( listInfo_i.count(), entryNames.count() );
+ for ( uint i=0; i < (uint) entryNames.count(); i++ ) {
+ QCOMPARE( listInfo_i[i].name(), entryNames[i] );
+ }
+}
+
+void tst_QFtp::cd_data()
+{
+ QTest::addColumn<QString>("host");
+ QTest::addColumn<uint>("port");
+ QTest::addColumn<QString>("user");
+ QTest::addColumn<QString>("password");
+ QTest::addColumn<QString>("dir");
+ QTest::addColumn<int>("success");
+ QTest::addColumn<QStringList>("entryNames"); // ### we should rather use a QList<QUrlInfo> here
+
+ QStringList flukeRoot;
+ flukeRoot << "qtest";
+ QStringList flukeQtest;
+ flukeQtest << "bigfile";
+ flukeQtest << "nonASCII";
+ flukeQtest << "rfc3252";
+ flukeQtest << "rfc3252.txt";
+ flukeQtest << "upload";
+
+ QTest::newRow( "relPath01" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString() << QString("qtest") << 1 << flukeQtest;
+ QTest::newRow( "relPath02" ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftptest") << QString("password") << QString("qtest") << 1 << flukeQtest;
+
+ QTest::newRow( "absPath01" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString() << QString("/qtest") << 1 << flukeQtest;
+ QTest::newRow( "absPath02" ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftptest") << QString("password") << QString("/var/ftp/qtest") << 1 << flukeQtest;
+
+ QTest::newRow( "nonExist01" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString() << QString("foo") << 0 << QStringList();
+ QTest::newRow( "nonExist03" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString() << QString("/foo") << 0 << QStringList();
+}
+
+void tst_QFtp::cd()
+{
+ QFETCH( QString, host );
+ QFETCH( uint, port );
+ QFETCH( QString, user );
+ QFETCH( QString, password );
+ QFETCH( QString, dir );
+
+ ftp = newFtp();
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host, port ) );
+ addCommand( QFtp::Login, ftp->login( user, password ) );
+ addCommand( QFtp::Cd, ftp->cd( dir ) );
+ addCommand( QFtp::List, ftp->list() );
+ addCommand( QFtp::Close, ftp->close() );
+
+ QTestEventLoop::instance().enterLoop( 30 );
+
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() ) {
+ QFAIL( "Network operation timed out" );
+ }
+
+ ResMapIt it = resultMap.find( QFtp::Cd );
+ QVERIFY( it != resultMap.end() );
+ QTEST( it.value().success, "success" );
+ QFETCH( QStringList, entryNames );
+ QCOMPARE( listInfo_i.count(), entryNames.count() );
+ for ( uint i=0; i < (uint) entryNames.count(); i++ ) {
+ QCOMPARE( listInfo_i[i].name(), entryNames[i] );
+ }
+}
+
+void tst_QFtp::get_data()
+{
+ QTest::addColumn<QString>("host");
+ QTest::addColumn<uint>("port");
+ QTest::addColumn<QString>("user");
+ QTest::addColumn<QString>("password");
+ QTest::addColumn<QString>("file");
+ QTest::addColumn<int>("success");
+ QTest::addColumn<QByteArray>("res");
+ QTest::addColumn<bool>("useIODevice");
+
+ // ### move this into external testdata
+ QFile file( SRCDIR "rfc3252.txt" );
+ QVERIFY( file.open( QIODevice::ReadOnly ) );
+ QByteArray rfc3252 = file.readAll();
+
+ // test the two get() overloads in one routine
+ for ( int i=0; i<2; i++ ) {
+ QTest::newRow( QString("relPath01_%1").arg(i).toLatin1().constData() ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString()
+ << "qtest/rfc3252" << 1 << rfc3252 << (bool)(i==1);
+ QTest::newRow( QString("relPath02_%1").arg(i).toLatin1().constData() ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftptest") << QString("password")
+ << "qtest/rfc3252" << 1 << rfc3252 << (bool)(i==1);
+
+ QTest::newRow( QString("absPath01_%1").arg(i).toLatin1().constData() ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString()
+ << "/qtest/rfc3252" << 1 << rfc3252 << (bool)(i==1);
+ QTest::newRow( QString("absPath02_%1").arg(i).toLatin1().constData() ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftptest") << QString("password")
+ << "/var/ftp/qtest/rfc3252" << 1 << rfc3252 << (bool)(i==1);
+
+ QTest::newRow( QString("nonExist01_%1").arg(i).toLatin1().constData() ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString()
+ << QString("foo") << 0 << QByteArray() << (bool)(i==1);
+ QTest::newRow( QString("nonExist02_%1").arg(i).toLatin1().constData() ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString()
+ << QString("/foo") << 0 << QByteArray() << (bool)(i==1);
+ }
+}
+
+void tst_QFtp::get()
+{
+ // for the overload that takes a QIODevice
+ QByteArray buf_ba;
+ QBuffer buf( &buf_ba );
+
+ QFETCH( QString, host );
+ QFETCH( uint, port );
+ QFETCH( QString, user );
+ QFETCH( QString, password );
+ QFETCH( QString, file );
+ QFETCH( bool, useIODevice );
+
+ ftp = newFtp();
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host, port ) );
+ addCommand( QFtp::Login, ftp->login( user, password ) );
+ if ( useIODevice ) {
+ buf.open( QIODevice::WriteOnly );
+ addCommand( QFtp::Get, ftp->get( file, &buf ) );
+ } else {
+ addCommand( QFtp::Get, ftp->get( file ) );
+ }
+ addCommand( QFtp::Close, ftp->close() );
+
+ QTestEventLoop::instance().enterLoop( 50 );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ ResMapIt it = resultMap.find( QFtp::Get );
+ QVERIFY( it != resultMap.end() );
+ QTEST( it.value().success, "success" );
+ if ( useIODevice ) {
+ QTEST( buf_ba, "res" );
+ } else {
+ QTEST( newData_ba, "res" );
+ }
+ QVERIFY( bytesTotal != bytesTotal_init );
+ if ( bytesTotal != -1 ) {
+ QVERIFY( bytesDone == bytesTotal );
+ }
+ if ( useIODevice ) {
+ if ( bytesDone != bytesDone_init ) {
+ QVERIFY( (int)buf_ba.size() == bytesDone );
+ }
+ } else {
+ if ( bytesDone != bytesDone_init ) {
+ QVERIFY( (int)newData_ba.size() == bytesDone );
+ }
+ }
+}
+
+void tst_QFtp::put_data()
+{
+ QTest::addColumn<QString>("host");
+ QTest::addColumn<uint>("port");
+ QTest::addColumn<QString>("user");
+ QTest::addColumn<QString>("password");
+ QTest::addColumn<QString>("file");
+ QTest::addColumn<QByteArray>("fileData");
+ QTest::addColumn<bool>("useIODevice");
+ QTest::addColumn<int>("success");
+
+ // ### move this into external testdata
+ QFile file( SRCDIR "rfc3252.txt" );
+ QVERIFY( file.open( QIODevice::ReadOnly ) );
+ QByteArray rfc3252 = file.readAll();
+
+ QByteArray bigData( 10*1024*1024, 0 );
+ bigData.fill( 'A' );
+
+ // test the two put() overloads in one routine
+ for ( int i=0; i<2; i++ ) {
+ QTest::newRow( QString("relPath01_%1").arg(i).toLatin1().constData() ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString()
+ << QString("qtest/upload/rel01_%1") << rfc3252
+ << (bool)(i==1) << 1;
+ /*
+ QTest::newRow( QString("relPath02_%1").arg(i).toLatin1().constData() ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftptest") << QString("password")
+ << QString("qtest/upload/rel02_%1") << rfc3252
+ << (bool)(i==1) << 1;
+ QTest::newRow( QString("relPath03_%1").arg(i).toLatin1().constData() ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftptest") << QString("password")
+ << QString("qtest/upload/rel03_%1") << QByteArray()
+ << (bool)(i==1) << 1;
+ QTest::newRow( QString("relPath04_%1").arg(i).toLatin1().constData() ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftptest") << QString("password")
+ << QString("qtest/upload/rel04_%1") << bigData
+ << (bool)(i==1) << 1;
+
+ QTest::newRow( QString("absPath01_%1").arg(i).toLatin1().constData() ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString()
+ << QString("/qtest/upload/abs01_%1") << rfc3252
+ << (bool)(i==1) << 1;
+ QTest::newRow( QString("absPath02_%1").arg(i).toLatin1().constData() ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftptest") << QString("password")
+ << QString("/srv/ftp/qtest/upload/abs02_%1") << rfc3252
+ << (bool)(i==1) << 1;
+
+ QTest::newRow( QString("nonExist01_%1").arg(i).toLatin1().constData() ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString()
+ << QString("foo") << QByteArray()
+ << (bool)(i==1) << 0;
+ QTest::newRow( QString("nonExist02_%1").arg(i).toLatin1().constData() ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString()
+ << QString("/foo") << QByteArray()
+ << (bool)(i==1) << 0;
+*/
+ }
+}
+
+void tst_QFtp::put()
+{
+ QFETCH( QString, host );
+ QFETCH( uint, port );
+ QFETCH( QString, user );
+ QFETCH( QString, password );
+ QFETCH( QString, file );
+ QFETCH( QByteArray, fileData );
+ QFETCH( bool, useIODevice );
+
+#ifdef Q_OS_WIN
+ QFETCH_GLOBAL(bool, setProxy);
+ if (setProxy) {
+ QFETCH_GLOBAL(int, proxyType);
+ if (proxyType == QNetworkProxy::Socks5Proxy)
+ QSKIP("With socks5 the put() test takes too long time on Windows.");
+ }
+#endif
+
+ const int timestep = 50;
+
+ if(file.contains('%'))
+ file = file.arg(uniqueExtension);
+
+ // for the overload that takes a QIODevice
+ QBuffer buf_fileData( &fileData );
+ buf_fileData.open( QIODevice::ReadOnly );
+
+ ResMapIt it;
+ //////////////////////////////////////////////////////////////////
+ // upload the file
+ init();
+ ftp = newFtp();
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host, port ) );
+ addCommand( QFtp::Login, ftp->login( user, password ) );
+ if ( useIODevice )
+ addCommand( QFtp::Put, ftp->put( &buf_fileData, file ) );
+ else
+ addCommand( QFtp::Put, ftp->put( fileData, file ) );
+ addCommand( QFtp::Close, ftp->close() );
+
+ for(int time = 0; time <= fileData.length() / 20000; time += timestep) {
+ QTestEventLoop::instance().enterLoop( timestep );
+ if(ftp->currentCommand() == QFtp::None)
+ break;
+ }
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ it = resultMap.find( QFtp::Put );
+ QVERIFY( it != resultMap.end() );
+ QTEST( it.value().success, "success" );
+ if ( !it.value().success ) {
+ QVERIFY( !fileExists( host, port, user, password, file ) );
+ return; // the following tests are only meaningful if the file could be put
+ }
+ QVERIFY( bytesTotal == (int)fileData.size() );
+ QVERIFY( bytesDone == bytesTotal );
+
+ QVERIFY( fileExists( host, port, user, password, file ) );
+
+ //////////////////////////////////////////////////////////////////
+ // fetch file to make sure that it is equal to the uploaded file
+ init();
+ ftp = newFtp();
+ QBuffer buf;
+ buf.open( QIODevice::WriteOnly );
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host, port ) );
+ addCommand( QFtp::Login, ftp->login( user, password ) );
+ addCommand( QFtp::Get, ftp->get( file, &buf ) );
+ addCommand( QFtp::Close, ftp->close() );
+
+ for(int time = 0; time <= fileData.length() / 20000; time += timestep) {
+ QTestEventLoop::instance().enterLoop( timestep );
+ if(ftp->currentCommand() == QFtp::None)
+ break;
+ }
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ QVERIFY( done_success == 1 );
+ QTEST( buf.buffer(), "fileData" );
+
+ //////////////////////////////////////////////////////////////////
+ // cleanup (i.e. remove the file) -- this also tests the remove command
+ init();
+ ftp = newFtp();
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host, port ) );
+ addCommand( QFtp::Login, ftp->login( user, password ) );
+ addCommand( QFtp::Remove, ftp->remove( file ) );
+ addCommand( QFtp::Close, ftp->close() );
+
+ QTestEventLoop::instance().enterLoop( timestep );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ it = resultMap.find( QFtp::Remove );
+ QVERIFY( it != resultMap.end() );
+ QCOMPARE( it.value().success, 1 );
+
+ QVERIFY( !fileExists( host, port, user, password, file ) );
+}
+
+void tst_QFtp::mkdir_data()
+{
+ QTest::addColumn<QString>("host");
+ QTest::addColumn<uint>("port");
+ QTest::addColumn<QString>("user");
+ QTest::addColumn<QString>("password");
+ QTest::addColumn<QString>("cdDir");
+ QTest::addColumn<QString>("dirToCreate");
+ QTest::addColumn<int>("success");
+
+ QTest::newRow( "relPath01" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString()
+ << "qtest/upload" << QString("rel01_%1") << 1;
+ QTest::newRow( "relPath02" ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftptest") << QString("password")
+ << "qtest/upload" << QString("rel02_%1") << 1;
+ QTest::newRow( "relPath03" ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftptest") << QString("password")
+ << "qtest/upload" << QString("rel03_%1") << 1;
+
+ QTest::newRow( "absPath01" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString()
+ << "." << QString("/qtest/upload/abs01_%1") << 1;
+ QTest::newRow( "absPath02" ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftptest") << QString("password")
+ << "." << QString("/var/ftp/qtest/upload/abs02_%1") << 1;
+
+ // QTest::newRow( "nonExist01" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString() << QString("foo") << 0;
+ QTest::newRow( "nonExist01" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString()
+ << "." << QString("foo") << 0;
+ QTest::newRow( "nonExist02" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString()
+ << "." << QString("/foo") << 0;
+}
+
+void tst_QFtp::mkdir()
+{
+ QFETCH( QString, host );
+ QFETCH( uint, port );
+ QFETCH( QString, user );
+ QFETCH( QString, password );
+ QFETCH( QString, cdDir );
+ QFETCH( QString, dirToCreate );
+
+ if(dirToCreate.contains('%'))
+ dirToCreate = dirToCreate.arg(uniqueExtension);
+
+ //////////////////////////////////////////////////////////////////
+ // create the directory
+ init();
+ ftp = newFtp();
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host, port ) );
+ addCommand( QFtp::Login, ftp->login( user, password ) );
+ addCommand( QFtp::Cd, ftp->cd( cdDir ) );
+ addCommand( QFtp::Mkdir, ftp->mkdir( dirToCreate ) );
+ addCommand( QFtp::Close, ftp->close() );
+
+ QTestEventLoop::instance().enterLoop( 30 );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ ResMapIt it = resultMap.find( QFtp::Mkdir );
+ QVERIFY( it != resultMap.end() );
+ QTEST( it.value().success, "success" );
+ if ( !it.value().success ) {
+ QVERIFY( !dirExists( host, port, user, password, cdDir, dirToCreate ) );
+ return; // the following tests are only meaningful if the dir could be created
+ }
+ QVERIFY( dirExists( host, port, user, password, cdDir, dirToCreate ) );
+
+ //////////////////////////////////////////////////////////////////
+ // create the directory again (should always fail!)
+ init();
+ ftp = newFtp();
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host, port ) );
+ addCommand( QFtp::Login, ftp->login( user, password ) );
+ addCommand( QFtp::Cd, ftp->cd( cdDir ) );
+ addCommand( QFtp::Mkdir, ftp->mkdir( dirToCreate ) );
+ addCommand( QFtp::Close, ftp->close() );
+
+ QTestEventLoop::instance().enterLoop( 30 );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ it = resultMap.find( QFtp::Mkdir );
+ QVERIFY( it != resultMap.end() );
+ QCOMPARE( it.value().success, 0 );
+
+ //////////////////////////////////////////////////////////////////
+ // remove the directory
+ init();
+ ftp = newFtp();
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host, port ) );
+ addCommand( QFtp::Login, ftp->login( user, password ) );
+ addCommand( QFtp::Cd, ftp->cd( cdDir ) );
+ addCommand( QFtp::Rmdir, ftp->rmdir( dirToCreate ) );
+ addCommand( QFtp::Close, ftp->close() );
+
+ QTestEventLoop::instance().enterLoop( 30 );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ it = resultMap.find( QFtp::Rmdir );
+ QVERIFY( it != resultMap.end() );
+ QCOMPARE( it.value().success, 1 );
+
+ QVERIFY( !dirExists( host, port, user, password, cdDir, dirToCreate ) );
+}
+
+void tst_QFtp::mkdir2()
+{
+ ftp = new QFtp;
+ ftp->connectToHost(QtNetworkSettings::serverName());
+ ftp->login();
+ current_id = ftp->cd("kake/test");
+
+ QEventLoop loop;
+ connect(ftp, SIGNAL(done(bool)), &loop, SLOT(quit()));
+ connect(ftp, SIGNAL(commandFinished(int, bool)), this, SLOT(mkdir2Slot(int, bool)));
+ QTimer::singleShot(5000, &loop, SLOT(quit()));
+
+ QSignalSpy commandStartedSpy(ftp, SIGNAL(commandStarted(int)));
+ QSignalSpy commandFinishedSpy(ftp, SIGNAL(commandFinished(int, bool)));
+
+ loop.exec();
+
+ QCOMPARE(commandStartedSpy.count(), 4); // connect, login, cd, mkdir
+ QCOMPARE(commandFinishedSpy.count(), 4);
+
+ for (int i = 0; i < 4; ++i)
+ QCOMPARE(commandFinishedSpy.at(i).at(0), commandStartedSpy.at(i).at(0));
+
+ QVERIFY(!commandFinishedSpy.at(0).at(1).toBool());
+ QVERIFY(!commandFinishedSpy.at(1).at(1).toBool());
+ QVERIFY(commandFinishedSpy.at(2).at(1).toBool());
+ QVERIFY(commandFinishedSpy.at(3).at(1).toBool());
+
+ delete ftp;
+ ftp = 0;
+}
+
+void tst_QFtp::mkdir2Slot(int id, bool)
+{
+ if (id == current_id)
+ ftp->mkdir("kake/test");
+}
+
+void tst_QFtp::rename_data()
+{
+ QTest::addColumn<QString>("host");
+ QTest::addColumn<QString>("user");
+ QTest::addColumn<QString>("password");
+ QTest::addColumn<QString>("cdDir");
+ QTest::addColumn<QString>("oldfile");
+ QTest::addColumn<QString>("newfile");
+ QTest::addColumn<QString>("createFile");
+ QTest::addColumn<QString>("renamedFile");
+ QTest::addColumn<int>("success");
+
+ QTest::newRow("relPath01") << QtNetworkSettings::serverName() << QString() << QString()
+ << "qtest/upload"
+ << QString("rel_old01_%1") << QString("rel_new01_%1")
+ << QString("qtest/upload/rel_old01_%1") << QString("qtest/upload/rel_new01_%1")
+ << 1;
+ QTest::newRow("relPath02") << QtNetworkSettings::serverName() << QString("ftptest") << "password"
+ << "qtest/upload"
+ << QString("rel_old02_%1") << QString("rel_new02_%1")
+ << QString("qtest/upload/rel_old02_%1") << QString("qtest/upload/rel_new02_%1")
+ << 1;
+ QTest::newRow("relPath03") << QtNetworkSettings::serverName() << QString("ftptest") << "password"
+ << "qtest/upload"
+ << QString("rel_old03_%1")<< QString("rel_new03_%1")
+ << QString("qtest/upload/rel_old03_%1") << QString("qtest/upload/rel_new03_%1")
+ << 1;
+
+ QTest::newRow("absPath01") << QtNetworkSettings::serverName() << QString() << QString()
+ << QString()
+ << QString("/qtest/upload/abs_old01_%1") << QString("/qtest/upload/abs_new01_%1")
+ << QString("/qtest/upload/abs_old01_%1") << QString("/qtest/upload/abs_new01_%1")
+ << 1;
+ QTest::newRow("absPath02") << QtNetworkSettings::serverName() << QString("ftptest") << "password"
+ << QString()
+ << QString("/var/ftp/qtest/upload/abs_old02_%1") << QString("/var/ftp/qtest/upload/abs_new02_%1")
+ << QString("/var/ftp/qtest/upload/abs_old02_%1") << QString("/var/ftp/qtest/upload/abs_new02_%1")
+ << 1;
+
+ QTest::newRow("nonExist01") << QtNetworkSettings::serverName() << QString() << QString()
+ << QString()
+ << QString("foo") << "new_foo"
+ << QString() << QString()
+ << 0;
+ QTest::newRow("nonExist02") << QtNetworkSettings::serverName() << QString() << QString()
+ << QString()
+ << QString("/foo") << QString("/new_foo")
+ << QString() << QString()
+ << 0;
+}
+
+void tst_QFtp::renameInit( const QString &host, const QString &user, const QString &password, const QString &createFile )
+{
+ if ( !createFile.isNull() ) {
+ // upload the file
+ init();
+ ftp = newFtp();
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host ) );
+ addCommand( QFtp::Login, ftp->login( user, password ) );
+ addCommand( QFtp::Put, ftp->put( QByteArray(), createFile ) );
+ addCommand( QFtp::Close, ftp->close() );
+
+ QTestEventLoop::instance().enterLoop( 50 );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ ResMapIt it = resultMap.find( QFtp::Put );
+ QVERIFY( it != resultMap.end() );
+ QVERIFY( it.value().success == 1 );
+
+ QVERIFY( fileExists( host, 21, user, password, createFile ) );
+ }
+}
+
+void tst_QFtp::renameCleanup( const QString &host, const QString &user, const QString &password, const QString &fileToDelete )
+{
+ if ( !fileToDelete.isNull() ) {
+ // cleanup (i.e. remove the file)
+ init();
+ ftp = newFtp();
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host ) );
+ addCommand( QFtp::Login, ftp->login( user, password ) );
+ addCommand( QFtp::Remove, ftp->remove( fileToDelete ) );
+ addCommand( QFtp::Close, ftp->close() );
+
+ QTestEventLoop::instance().enterLoop( 30 );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ ResMapIt it = resultMap.find( QFtp::Remove );
+ QVERIFY( it != resultMap.end() );
+ QVERIFY( it.value().success == 1 );
+
+ QVERIFY( !fileExists( host, 21, user, password, fileToDelete ) );
+ }
+}
+
+void tst_QFtp::rename()
+{
+ QFETCH( QString, host );
+ QFETCH( QString, user );
+ QFETCH( QString, password );
+ QFETCH( QString, cdDir );
+ QFETCH( QString, oldfile );
+ QFETCH( QString, newfile );
+ QFETCH( QString, createFile );
+ QFETCH( QString, renamedFile );
+
+ if(oldfile.contains('%'))
+ oldfile = oldfile.arg(uniqueExtension);
+ if(newfile.contains('%'))
+ newfile = newfile.arg(uniqueExtension);
+ if(createFile.contains('%'))
+ createFile = createFile.arg(uniqueExtension);
+ if(renamedFile.contains('%'))
+ renamedFile = renamedFile.arg(uniqueExtension);
+
+ renameInit( host, user, password, createFile );
+
+ init();
+ ftp = newFtp();
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host ) );
+ addCommand( QFtp::Login, ftp->login( user, password ) );
+ if ( !cdDir.isNull() )
+ addCommand( QFtp::Cd, ftp->cd( cdDir ) );
+ addCommand( QFtp::Rename, ftp->rename( oldfile, newfile ) );
+ addCommand( QFtp::Close, ftp->close() );
+
+ QTestEventLoop::instance().enterLoop( 30 );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ ResMapIt it = resultMap.find( QFtp::Rename );
+ QVERIFY( it != resultMap.end() );
+ QTEST( it.value().success, "success" );
+
+ if ( it.value().success ) {
+ QVERIFY( !fileExists( host, 21, user, password, oldfile, cdDir ) );
+ QVERIFY( fileExists( host, 21, user, password, newfile, cdDir ) );
+ QVERIFY( fileExists( host, 21, user, password, renamedFile ) );
+ } else {
+ QVERIFY( !fileExists( host, 21, user, password, newfile, cdDir ) );
+ QVERIFY( !fileExists( host, 21, user, password, renamedFile ) );
+ }
+
+ renameCleanup( host, user, password, renamedFile );
+}
+
+/*
+ The commandSequence() test does not test any particular function. It rather
+ tests a sequence of arbitrary commands specified in the test data.
+*/
+class FtpCommand
+{
+public:
+ FtpCommand() :
+ cmd(QFtp::None)
+ { }
+
+ FtpCommand( QFtp::Command command ) :
+ cmd(command)
+ { }
+
+ FtpCommand( QFtp::Command command, const QStringList &arguments ) :
+ cmd(command), args(arguments)
+ { }
+
+ FtpCommand( const FtpCommand &c )
+ { *this = c; }
+
+ FtpCommand &operator=( const FtpCommand &c )
+ {
+ this->cmd = c.cmd;
+ this->args = c.args;
+ return *this;
+ }
+
+ QFtp::Command cmd;
+ QStringList args;
+};
+QDataStream &operator<<( QDataStream &s, const FtpCommand &command )
+{
+ s << (int)command.cmd;
+ s << command.args;
+ return s;
+}
+QDataStream &operator>>( QDataStream &s, FtpCommand &command )
+{
+ int tmp;
+ s >> tmp;
+ command.cmd = (QFtp::Command)tmp;
+ s >> command.args;
+ return s;
+}
+Q_DECLARE_METATYPE(QList<FtpCommand>)
+
+void tst_QFtp::commandSequence_data()
+{
+ // some "constants"
+ QStringList argConnectToHost01;
+ argConnectToHost01 << QtNetworkSettings::serverName() << "21";
+
+ QStringList argLogin01, argLogin02, argLogin03, argLogin04;
+ argLogin01 << QString() << QString();
+ argLogin02 << "ftp" << QString();
+ argLogin03 << "ftp" << "foo";
+ argLogin04 << QString("ftptest") << "password";
+
+ FtpCommand connectToHost01( QFtp::ConnectToHost, argConnectToHost01 );
+ FtpCommand login01( QFtp::Login, argLogin01 );
+ FtpCommand login02( QFtp::Login, argLogin01 );
+ FtpCommand login03( QFtp::Login, argLogin01 );
+ FtpCommand login04( QFtp::Login, argLogin01 );
+ FtpCommand close01( QFtp::Close );
+
+ QTest::addColumn<QList<FtpCommand> >("cmds");
+ QTest::addColumn<int>("success");
+
+ // success data
+ {
+ QList<FtpCommand> cmds;
+ cmds << connectToHost01;
+ QTest::newRow( "simple_ok01" ) << cmds << 1;
+ }
+ {
+ QList<FtpCommand> cmds;
+ cmds << connectToHost01;
+ cmds << login01;
+ QTest::newRow( "simple_ok02" ) << cmds << 1;
+ }
+ {
+ QList<FtpCommand> cmds;
+ cmds << connectToHost01;
+ cmds << login01;
+ cmds << close01;
+ QTest::newRow( "simple_ok03" ) << cmds << 1;
+ }
+ {
+ QList<FtpCommand> cmds;
+ cmds << connectToHost01;
+ cmds << close01;
+ QTest::newRow( "simple_ok04" ) << cmds << 1;
+ }
+ {
+ QList<FtpCommand> cmds;
+ cmds << connectToHost01;
+ cmds << login01;
+ cmds << close01;
+ cmds << connectToHost01;
+ cmds << login02;
+ cmds << close01;
+ QTest::newRow( "connect_twice" ) << cmds << 1;
+ }
+
+ // error data
+ {
+ QList<FtpCommand> cmds;
+ cmds << close01;
+ QTest::newRow( "error01" ) << cmds << 0;
+ }
+ {
+ QList<FtpCommand> cmds;
+ cmds << login01;
+ QTest::newRow( "error02" ) << cmds << 0;
+ }
+ {
+ QList<FtpCommand> cmds;
+ cmds << login01;
+ cmds << close01;
+ QTest::newRow( "error03" ) << cmds << 0;
+ }
+ {
+ QList<FtpCommand> cmds;
+ cmds << connectToHost01;
+ cmds << login01;
+ cmds << close01;
+ cmds << login01;
+ QTest::newRow( "error04" ) << cmds << 0;
+ }
+}
+
+void tst_QFtp::commandSequence()
+{
+ QFETCH( QList<FtpCommand>, cmds );
+
+ ftp = newFtp();
+ QList<FtpCommand>::iterator it;
+ for ( it = cmds.begin(); it != cmds.end(); ++it ) {
+ switch ( (*it).cmd ) {
+ case QFtp::ConnectToHost:
+ {
+ QVERIFY( (*it).args.count() == 2 );
+ uint port;
+ bool portOk;
+ port = (*it).args[1].toUInt( &portOk );
+ QVERIFY( portOk );
+ ids << ftp->connectToHost( (*it).args[0], port );
+ }
+ break;
+ case QFtp::Login:
+ QVERIFY( (*it).args.count() == 2 );
+ ids << ftp->login( (*it).args[0], (*it).args[1] );
+ break;
+ case QFtp::Close:
+ QVERIFY( (*it).args.count() == 0 );
+ ids << ftp->close();
+ break;
+ default:
+ QFAIL( "Error in test: unexpected enum value" );
+ break;
+ }
+ }
+
+ QTestEventLoop::instance().enterLoop( 30 );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ QTEST( commandSequence_success, "success" );
+}
+
+void tst_QFtp::abort_data()
+{
+ QTest::addColumn<QString>("host");
+ QTest::addColumn<uint>("port");
+ QTest::addColumn<QString>("file");
+ QTest::addColumn<QByteArray>("uploadData");
+
+ QTest::newRow( "get_fluke01" ) << QtNetworkSettings::serverName() << (uint)21 << QString("qtest/bigfile") << QByteArray();
+ QTest::newRow( "get_fluke02" ) << QtNetworkSettings::serverName() << (uint)21 << QString("qtest/rfc3252") << QByteArray();
+
+ // Qt/CE test environment has too little memory for this test
+#if !defined(Q_OS_WINCE)
+ QByteArray bigData( 10*1024*1024, 0 );
+#else
+ QByteArray bigData( 1*1024*1024, 0 );
+#endif
+ bigData.fill( 'B' );
+ QTest::newRow( "put_fluke01" ) << QtNetworkSettings::serverName() << (uint)21 << QString("qtest/upload/abort_put") << bigData;
+}
+
+void tst_QFtp::abort()
+{
+ // In case you wonder where the abort() actually happens, look into
+ // tst_QFtp::dataTransferProgress
+ //
+ QFETCH( QString, host );
+ QFETCH( uint, port );
+ QFETCH( QString, file );
+ QFETCH( QByteArray, uploadData );
+
+ QFtp::Command cmd;
+ if ( uploadData.size() == 0 )
+ cmd = QFtp::Get;
+ else
+ cmd = QFtp::Put;
+
+ ftp = newFtp();
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host, port ) );
+ addCommand( QFtp::Login, ftp->login() );
+ if ( cmd == QFtp::Get )
+ addCommand( cmd, ftp->get( file ) );
+ else
+ addCommand( cmd, ftp->put( uploadData, file ) );
+ addCommand( QFtp::Close, ftp->close() );
+
+ for(int time = 0; time <= uploadData.length() / 30000; time += 30) {
+ QTestEventLoop::instance().enterLoop( 50 );
+ if(ftp->currentCommand() == QFtp::None)
+ break;
+ }
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ ResMapIt it = resultMap.find( cmd );
+ QVERIFY( it != resultMap.end() );
+ // ### how to test the abort?
+ if ( it.value().success ) {
+ // The FTP server on fluke is sadly returning a success, even when
+ // the operation was aborted. So we have to use some heuristics.
+ if ( host == QtNetworkSettings::serverName() ) {
+ if ( cmd == QFtp::Get ) {
+ QVERIFY(bytesDone <= bytesTotal);
+ } else {
+ // put commands should always be aborted, since we use really
+ // big data
+ QVERIFY( bytesDone != bytesTotal );
+ }
+ } else {
+ // this could be tested by verifying that no more progress signals are emitted
+ QVERIFY(bytesDone <= bytesTotal);
+ }
+ } else {
+ QVERIFY( bytesDone != bytesTotal );
+ }
+
+ if ( cmd == QFtp::Put ) {
+ //////////////////////////////////////
+ // cleanup (i.e. remove the file)
+ init();
+ ftp = newFtp();
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host, port ) );
+ addCommand( QFtp::Login, ftp->login() );
+ addCommand( QFtp::Remove, ftp->remove( file ) );
+ addCommand( QFtp::Close, ftp->close() );
+
+ QTestEventLoop::instance().enterLoop( 30 );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ it = resultMap.find( QFtp::Remove );
+ QVERIFY( it != resultMap.end() );
+ QVERIFY( it.value().success == 1 );
+ }
+}
+
+void tst_QFtp::bytesAvailable_data()
+{
+ QTest::addColumn<QString>("host");
+ QTest::addColumn<QString>("file");
+ QTest::addColumn<int>("type");
+ QTest::addColumn<qlonglong>("bytesAvailFinishedGet");
+ QTest::addColumn<qlonglong>("bytesAvailFinished");
+ QTest::addColumn<qlonglong>("bytesAvailDone");
+
+ QTest::newRow( "fluke01" ) << QtNetworkSettings::serverName() << QString("qtest/bigfile") << 0 << (qlonglong)519240 << (qlonglong)519240 << (qlonglong)519240;
+ QTest::newRow( "fluke02" ) << QtNetworkSettings::serverName() << QString("qtest/rfc3252") << 0 << (qlonglong)25962 << (qlonglong)25962 << (qlonglong)25962;
+
+ QTest::newRow( "fluke03" ) << QtNetworkSettings::serverName() << QString("qtest/bigfile") << 1 << (qlonglong)519240 << (qlonglong)0 << (qlonglong)0;
+ QTest::newRow( "fluke04" ) << QtNetworkSettings::serverName() << QString("qtest/rfc3252") << 1 << (qlonglong)25962 << (qlonglong)0 << (qlonglong)0;
+}
+
+void tst_QFtp::bytesAvailable()
+{
+ QFETCH( QString, host );
+ QFETCH( QString, file );
+ QFETCH( int, type );
+
+ ftp = newFtp();
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host ) );
+ addCommand( QFtp::Login, ftp->login() );
+ addCommand( QFtp::Get, ftp->get( file ) );
+ if ( type != 0 )
+ addCommand( QFtp::Close, ftp->close() );
+
+ QTestEventLoop::instance().enterLoop( 40 );
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ ResMapIt it = resultMap.find( QFtp::Get );
+ QVERIFY( it != resultMap.end() );
+ QVERIFY( it.value().success );
+
+ QFETCH(qlonglong, bytesAvailFinishedGet);
+ QCOMPARE(bytesAvailable_finishedGet, bytesAvailFinishedGet);
+
+ QFETCH(qlonglong, bytesAvailFinished);
+ QCOMPARE(bytesAvailable_finished, bytesAvailFinished);
+
+ QFETCH(qlonglong, bytesAvailDone);
+ QCOMPARE(bytesAvailable_done, bytesAvailDone);
+
+ ftp->readAll();
+ QVERIFY( ftp->bytesAvailable() == 0 );
+ delete ftp;
+ ftp = 0;
+}
+
+void tst_QFtp::activeMode()
+{
+ QFile file("tst_QFtp_activeMode_inittab");
+ file.open(QIODevice::ReadWrite);
+ QFtp ftp;
+ ftp.setTransferMode(QFtp::Active);
+ ftp.connectToHost(QtNetworkSettings::serverName(), 21);
+ ftp.login();
+ ftp.list();
+ ftp.get("/qtest/rfc3252.txt", &file);
+ connect(&ftp, SIGNAL(done(bool)), SLOT(activeModeDone(bool)));
+ QTestEventLoop::instance().enterLoop(900);
+ QFile::remove("tst_QFtp_activeMode_inittab");
+ QVERIFY(done_success == 1);
+
+}
+
+void tst_QFtp::activeModeDone(bool error)
+{
+ done_success = error ? -1 : 1;
+ QTestEventLoop::instance().exitLoop();
+}
+
+void tst_QFtp::proxy_data()
+{
+ QTest::addColumn<QString>("host");
+ QTest::addColumn<uint>("port");
+ QTest::addColumn<QString>("user");
+ QTest::addColumn<QString>("password");
+ QTest::addColumn<QString>("dir");
+ QTest::addColumn<int>("success");
+ QTest::addColumn<QStringList>("entryNames"); // ### we should rather use a QList<QUrlInfo> here
+
+ QStringList flukeRoot;
+ flukeRoot << "qtest";
+ QStringList flukeQtest;
+ flukeQtest << "bigfile";
+ flukeQtest << "nonASCII";
+ flukeQtest << "rfc3252";
+ flukeQtest << "rfc3252.txt";
+ flukeQtest << "upload";
+
+ QTest::newRow( "proxy_relPath01" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString() << QString("qtest") << 1 << flukeQtest;
+ QTest::newRow( "proxy_relPath02" ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftptest") << QString("password") << QString("qtest") << 1 << flukeQtest;
+
+ QTest::newRow( "proxy_absPath01" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString() << QString("/qtest") << 1 << flukeQtest;
+ QTest::newRow( "proxy_absPath02" ) << QtNetworkSettings::serverName() << (uint)21 << QString("ftptest") << QString("password") << QString("/var/ftp/qtest") << 1 << flukeQtest;
+
+ QTest::newRow( "proxy_nonExist01" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString() << QString("foo") << 0 << QStringList();
+ QTest::newRow( "proxy_nonExist03" ) << QtNetworkSettings::serverName() << (uint)21 << QString() << QString() << QString("/foo") << 0 << QStringList();
+}
+
+void tst_QFtp::proxy()
+{
+ QFETCH( QString, host );
+ QFETCH( uint, port );
+ QFETCH( QString, user );
+ QFETCH( QString, password );
+ QFETCH( QString, dir );
+
+ ftp = newFtp();
+ addCommand( QFtp::SetProxy, ftp->setProxy( QtNetworkSettings::serverName(), 2121 ) );
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host, port ) );
+ addCommand( QFtp::Login, ftp->login( user, password ) );
+ addCommand( QFtp::Cd, ftp->cd( dir ) );
+ addCommand( QFtp::List, ftp->list() );
+
+ QTestEventLoop::instance().enterLoop( 50 );
+
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() ) {
+ QFAIL( "Network operation timed out" );
+ }
+
+ ResMapIt it = resultMap.find( QFtp::Cd );
+ QVERIFY( it != resultMap.end() );
+ QFETCH( int, success );
+ QCOMPARE( it.value().success, success );
+ QFETCH( QStringList, entryNames );
+ QCOMPARE( listInfo_i.count(), entryNames.count() );
+ for ( uint i=0; i < (uint) entryNames.count(); i++ ) {
+ QCOMPARE( listInfo_i[i].name(), entryNames[i] );
+ }
+}
+
+void tst_QFtp::binaryAscii()
+{
+ QString file = "asciifile%1.txt";
+
+ if(file.contains('%'))
+ file = file.arg(uniqueExtension);
+
+ QByteArray putData = "a line of text\r\n";
+
+ init();
+ ftp = newFtp();
+ addCommand(QFtp::ConnectToHost, ftp->connectToHost(QtNetworkSettings::serverName(), 21));
+ addCommand(QFtp::Login, ftp->login("ftptest", "password"));
+ addCommand(QFtp::Cd, ftp->cd("qtest/upload"));
+ addCommand(QFtp::Put, ftp->put(putData, file, QFtp::Ascii));
+ addCommand(QFtp::Close, ftp->close());
+
+ QTestEventLoop::instance().enterLoop( 30 );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ ResMapIt it = resultMap.find(QFtp::Put);
+ QVERIFY(it != resultMap.end());
+ QVERIFY(it.value().success);
+
+ QByteArray getData;
+ QBuffer getBuf(&getData);
+ getBuf.open(QBuffer::WriteOnly);
+
+ init();
+ ftp = newFtp();
+ addCommand(QFtp::ConnectToHost, ftp->connectToHost(QtNetworkSettings::serverName(), 21));
+ addCommand(QFtp::Login, ftp->login("ftptest", "password"));
+ addCommand(QFtp::Cd, ftp->cd("qtest/upload"));
+ addCommand(QFtp::Get, ftp->get(file, &getBuf, QFtp::Binary));
+ addCommand(QFtp::Close, ftp->close());
+
+ QTestEventLoop::instance().enterLoop( 30 );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ ResMapIt it2 = resultMap.find(QFtp::Get);
+ QVERIFY(it2 != resultMap.end());
+ QVERIFY(it2.value().success);
+ // most modern ftp servers leave the file as it is by default
+ // (and do not remove the windows line ending), the -1 below could be
+ // deleted in the future
+ QVERIFY(getData.size() == putData.size()-1);
+ //////////////////////////////////////////////////////////////////
+ // cleanup (i.e. remove the file) -- this also tests the remove command
+ init();
+ ftp = newFtp();
+ addCommand(QFtp::ConnectToHost, ftp->connectToHost(QtNetworkSettings::serverName(), 21));
+ addCommand(QFtp::Login, ftp->login("ftptest", "password"));
+ addCommand(QFtp::Cd, ftp->cd("qtest/upload"));
+ addCommand(QFtp::Remove, ftp->remove(file));
+ addCommand(QFtp::Close, ftp->close());
+
+ QTestEventLoop::instance().enterLoop( 30 );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() )
+ QFAIL( "Network operation timed out" );
+
+ it = resultMap.find( QFtp::Remove );
+ QVERIFY( it != resultMap.end() );
+ QCOMPARE( it.value().success, 1 );
+
+ QVERIFY(!fileExists(QtNetworkSettings::serverName(), 21, "ftptest", "password", file));
+}
+
+
+// test QFtp::currentId() and QFtp::currentCommand()
+#define CURRENTCOMMAND_TEST \
+{ \
+ ResMapIt it; \
+ for ( it = resultMap.begin(); it != resultMap.end(); ++it ) { \
+ if ( it.value().id == ftp->currentId() ) { \
+ QVERIFY( it.key() == ftp->currentCommand() ); \
+ } \
+} \
+}
+
+void tst_QFtp::commandStarted( int id )
+{
+#if defined( DUMP_SIGNALS )
+ qDebug( "%d:commandStarted( %d )", ftp->currentId(), id );
+#endif
+ // make sure that the commandStarted and commandFinished are nested correctly
+ QVERIFY( current_id == 0 );
+ current_id = id;
+
+ QVERIFY( !ids.isEmpty() );
+ QVERIFY( ids.first() == id );
+ if ( ids.count() > 1 ) {
+ QVERIFY( ftp->hasPendingCommands() );
+ } else {
+ QVERIFY( !ftp->hasPendingCommands() );
+ }
+
+ QVERIFY( ftp->currentId() == id );
+ QVERIFY( cur_state == ftp->state() );
+ CURRENTCOMMAND_TEST;
+
+ QVERIFY( ftp->error() == QFtp::NoError );
+}
+
+void tst_QFtp::commandFinished( int id, bool error )
+{
+#if defined( DUMP_SIGNALS )
+ qDebug( "%d:commandFinished( %d, %d ) -- errorString: '%s'",
+ ftp->currentId(), id, (int)error, ftp->errorString().toLatin1().constData() );
+#endif
+ if ( ftp->currentCommand() == QFtp::Get ) {
+ bytesAvailable_finishedGet = ftp->bytesAvailable();
+ }
+ bytesAvailable_finished = ftp->bytesAvailable();
+
+ // make sure that the commandStarted and commandFinished are nested correctly
+ QVERIFY( current_id == id );
+ current_id = 0;
+
+ QVERIFY( !ids.isEmpty() );
+ QVERIFY( ids.first() == id );
+ if ( !error && ids.count() > 1) {
+ QVERIFY( ftp->hasPendingCommands() );
+ } else {
+ QVERIFY( !ftp->hasPendingCommands() );
+ }
+ if ( error ) {
+ QVERIFY( ftp->error() != QFtp::NoError );
+ ids.clear();
+ } else {
+ QVERIFY( ftp->error() == QFtp::NoError );
+ ids.pop_front();
+ }
+
+ QVERIFY( ftp->currentId() == id );
+ QVERIFY( cur_state == ftp->state() );
+ CURRENTCOMMAND_TEST;
+
+ if ( QTest::currentTestFunction() != QLatin1String("commandSequence") ) {
+ ResMapIt it = resultMap.find( ftp->currentCommand() );
+ QVERIFY( it != resultMap.end() );
+ QVERIFY( it.value().success == -1 );
+ if ( error )
+ it.value().success = 0;
+ else
+ it.value().success = 1;
+ }
+}
+
+void tst_QFtp::done( bool error )
+{
+#if defined( DUMP_SIGNALS )
+ qDebug( "%d:done( %d )", ftp->currentId(), (int)error );
+#endif
+ bytesAvailable_done = ftp->bytesAvailable();
+
+ QVERIFY( ftp->currentId() == 0 );
+ QVERIFY( current_id == 0 );
+ QVERIFY( ids.isEmpty() );
+ QVERIFY( cur_state == ftp->state() );
+ QVERIFY( !ftp->hasPendingCommands() );
+
+ if ( QTest::currentTestFunction() == QLatin1String("commandSequence") ) {
+ QVERIFY( commandSequence_success == -1 );
+ if ( error )
+ commandSequence_success = 0;
+ else
+ commandSequence_success = 1;
+ }
+ QVERIFY( done_success == -1 );
+ if ( error ) {
+ QVERIFY( ftp->error() != QFtp::NoError );
+ done_success = 0;
+ } else {
+ QVERIFY( ftp->error() == QFtp::NoError );
+ done_success = 1;
+ }
+ QTestEventLoop::instance().exitLoop();
+}
+
+void tst_QFtp::stateChanged( int state )
+{
+#if defined( DUMP_SIGNALS )
+ qDebug( "%d: stateChanged( %d )", ftp->currentId(), state );
+#endif
+ QCOMPARE( ftp->currentId(), current_id );
+ CURRENTCOMMAND_TEST;
+
+ QVERIFY( state != cur_state );
+ QCOMPARE( state, (int)ftp->state() );
+ if ( state != QFtp::Unconnected ) {
+ // make sure that the states are always emitted in the right order (for
+ // this, we assume an ordering on the enum values, which they have at
+ // the moment)
+ QVERIFY( cur_state < state );
+
+ // make sure that state changes are only emitted in response to certain
+ // commands
+ switch ( state ) {
+ case QFtp::HostLookup:
+ case QFtp::Connecting:
+ case QFtp::Connected:
+ QCOMPARE( (int)ftp->currentCommand(), (int)QFtp::ConnectToHost );
+ break;
+ case QFtp::LoggedIn:
+ QCOMPARE( (int)ftp->currentCommand(), (int)QFtp::Login );
+ break;
+ case QFtp::Closing:
+ QCOMPARE( (int)ftp->currentCommand(), (int)QFtp::Close );
+ break;
+ default:
+ QWARN( QString("Unexpected state '%1'").arg(state).toLatin1().constData() );
+ break;
+ }
+ }
+ cur_state = state;
+
+ if ( QTest::currentTestFunction() == QLatin1String("connectToHost") ) {
+ switch ( state ) {
+ case QFtp::HostLookup:
+ case QFtp::Connecting:
+ case QFtp::LoggedIn:
+ case QFtp::Closing:
+ // ignore
+ break;
+ case QFtp::Connected:
+ case QFtp::Unconnected:
+ QVERIFY( connectToHost_state == -1 );
+ connectToHost_state = state;
+ break;
+ default:
+ QWARN( QString("Unknown state '%1'").arg(state).toLatin1().constData() );
+ break;
+ }
+ } else if ( QTest::currentTestFunction() == QLatin1String("close") ) {
+ ResMapIt it = resultMap.find( QFtp::Close );
+ if ( it!=resultMap.end() && ftp->currentId()==it.value().id ) {
+ if ( state == QFtp::Closing ) {
+ QVERIFY( close_state == -1 );
+ close_state = state;
+ } else if ( state == QFtp::Unconnected ) {
+ QVERIFY( close_state == QFtp::Closing );
+ close_state = state;
+ }
+ }
+ } else if ( QTest::currentTestFunction() == QLatin1String("login") ) {
+ ResMapIt it = resultMap.find( QFtp::Login );
+ if ( it!=resultMap.end() && ftp->currentId()==it.value().id ) {
+ if ( state == QFtp::LoggedIn ) {
+ QVERIFY( login_state == -1 );
+ login_state = state;
+ }
+ }
+ }
+}
+
+void tst_QFtp::listInfo( const QUrlInfo &i )
+{
+#if defined( DUMP_SIGNALS )
+ qDebug( "%d: listInfo( %s )", ftp->currentId(), i.name().toLatin1().constData() );
+#endif
+ QCOMPARE( ftp->currentId(), current_id );
+ if ( ids.count() > 1 ) {
+ QVERIFY( ftp->hasPendingCommands() );
+ } else {
+ QVERIFY( !ftp->hasPendingCommands() );
+ }
+ QVERIFY( cur_state == ftp->state() );
+ CURRENTCOMMAND_TEST;
+
+ if ( QTest::currentTestFunction()==QLatin1String("list") || QTest::currentTestFunction()==QLatin1String("cd") || QTest::currentTestFunction()==QLatin1String("proxy") || inFileDirExistsFunction ) {
+ ResMapIt it = resultMap.find( QFtp::List );
+ QVERIFY( it != resultMap.end() );
+ QVERIFY( ftp->currentId() == it.value().id );
+ listInfo_i << i;
+ }
+}
+
+void tst_QFtp::readyRead()
+{
+#if defined( DUMP_SIGNALS )
+ qDebug( "%d: readyRead(), bytesAvailable == %lu", ftp->currentId(), ftp->bytesAvailable() );
+#endif
+ QCOMPARE( ftp->currentId(), current_id );
+ if ( ids.count() > 1 ) {
+ QVERIFY( ftp->hasPendingCommands() );
+ } else {
+ QVERIFY( !ftp->hasPendingCommands() );
+ }
+ QVERIFY( cur_state == ftp->state() );
+ CURRENTCOMMAND_TEST;
+
+ if ( QTest::currentTestFunction() != QLatin1String("bytesAvailable") ) {
+ int oldSize = newData_ba.size();
+ qlonglong bytesAvail = ftp->bytesAvailable();
+ QByteArray ba = ftp->readAll();
+ QVERIFY( ba.size() == (int) bytesAvail );
+ newData_ba.resize( oldSize + ba.size() );
+ memcpy( newData_ba.data()+oldSize, ba.data(), ba.size() );
+
+ if ( bytesTotal != -1 ) {
+ QVERIFY( (int)newData_ba.size() <= bytesTotal );
+ }
+ QVERIFY( (int)newData_ba.size() == bytesDone );
+ }
+}
+
+void tst_QFtp::dataTransferProgress( qint64 done, qint64 total )
+{
+#if defined( DUMP_SIGNALS )
+ qDebug( "%d: dataTransferProgress( %lli, %lli )", ftp->currentId(), done, total );
+#endif
+ QCOMPARE( ftp->currentId(), current_id );
+ if ( ids.count() > 1 ) {
+ QVERIFY( ftp->hasPendingCommands() );
+ } else {
+ QVERIFY( !ftp->hasPendingCommands() );
+ }
+ QVERIFY( cur_state == ftp->state() );
+ CURRENTCOMMAND_TEST;
+
+ if ( bytesTotal == bytesTotal_init ) {
+ bytesTotal = total;
+ } else {
+ QVERIFY( bytesTotal == total );
+ }
+
+ QVERIFY( bytesTotal != bytesTotal_init );
+ QVERIFY( bytesDone <= done );
+ bytesDone = done;
+ if ( bytesTotal != -1 ) {
+ QVERIFY( bytesDone <= bytesTotal );
+ }
+
+ if ( QTest::currentTestFunction() == QLatin1String("abort") ) {
+ // ### it would be nice if we could specify in our testdata when to do
+ // the abort
+ if ( done >= total/100000 ) {
+ if ( ids.count() != 1 ) {
+ // do abort only once
+ int tmpId = ids.first();
+ ids.clear();
+ ids << tmpId;
+ ftp->abort();
+ }
+ }
+ }
+}
+
+
+QFtp *tst_QFtp::newFtp()
+{
+ QFtp *nFtp = new QFtp( this );
+#ifndef QT_NO_BEARERMANAGEMENT
+ if (networkSessionExplicit) {
+ nFtp->setProperty("_q_networksession", QVariant::fromValue(networkSessionExplicit));
+ }
+#endif
+ connect( nFtp, SIGNAL(commandStarted(int)),
+ SLOT(commandStarted(int)) );
+ connect( nFtp, SIGNAL(commandFinished(int,bool)),
+ SLOT(commandFinished(int,bool)) );
+ connect( nFtp, SIGNAL(done(bool)),
+ SLOT(done(bool)) );
+ connect( nFtp, SIGNAL(stateChanged(int)),
+ SLOT(stateChanged(int)) );
+ connect( nFtp, SIGNAL(listInfo(const QUrlInfo&)),
+ SLOT(listInfo(const QUrlInfo&)) );
+ connect( nFtp, SIGNAL(readyRead()),
+ SLOT(readyRead()) );
+ connect( nFtp, SIGNAL(dataTransferProgress(qint64, qint64)),
+ SLOT(dataTransferProgress(qint64, qint64)) );
+
+ return nFtp;
+}
+
+void tst_QFtp::addCommand( QFtp::Command cmd, int id )
+{
+ ids << id;
+ CommandResult res;
+ res.id = id;
+ res.success = -1;
+ resultMap[ cmd ] = res;
+}
+
+bool tst_QFtp::fileExists( const QString &host, quint16 port, const QString &user, const QString &password, const QString &file, const QString &cdDir )
+{
+ init();
+ ftp = newFtp();
+ // ### make these tests work
+ if (ftp->currentId() != 0) {
+ qWarning("ftp->currentId() != 0");
+ return false;
+ }
+
+ if (ftp->state() != QFtp::Unconnected) {
+ qWarning("ftp->state() != QFtp::Unconnected");
+ return false;
+ }
+
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host, port ) );
+ addCommand( QFtp::Login, ftp->login( user, password ) );
+ if ( !cdDir.isNull() )
+ addCommand( QFtp::Cd, ftp->cd( cdDir ) );
+ addCommand( QFtp::List, ftp->list( file ) );
+ addCommand( QFtp::Close, ftp->close() );
+
+ inFileDirExistsFunction = true;
+ QTestEventLoop::instance().enterLoop( 30 );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() ) {
+ // ### make this test work
+ qWarning("tst_QFtp::fileExists: Network operation timed out");
+ return false;
+ }
+ inFileDirExistsFunction = false;
+
+ ResMapIt it = resultMap.find( QFtp::ConnectToHost );
+ // ### make these tests work
+ if (it == resultMap.end()) {
+ qWarning("it != resultMap.end()");
+ return false;
+ }
+
+ if (it.value().success == -1) {
+ qWarning("it.value().success != -1");
+ return false;
+ }
+
+ if ( it.value().success == 1 ) {
+ for ( uint i=0; i < (uint) listInfo_i.count(); i++ ) {
+ if ( QFileInfo(listInfo_i[i].name()).fileName() == QFileInfo(file).fileName() )
+ return true;
+ }
+ }
+
+ //this is not a good warning considering sometime this function is used to test that a file does not exist
+ //qWarning("file doesn't exist");
+ return false;
+}
+
+bool tst_QFtp::dirExists( const QString &host, quint16 port, const QString &user, const QString &password, const QString &cdDir, const QString &dirToCreate )
+{
+ init();
+ ftp = newFtp();
+ // ### make these tests work
+ // QCOMPARE( ftp->currentId(), 0 );
+ // QCOMPARE( (int)ftp->state(), (int)QFtp::Unconnected );
+
+ addCommand( QFtp::ConnectToHost, ftp->connectToHost( host, port ) );
+ addCommand( QFtp::Login, ftp->login( user, password ) );
+ if ( dirToCreate.startsWith( "/" ) )
+ addCommand( QFtp::Cd, ftp->cd( dirToCreate ) );
+ else
+ addCommand( QFtp::Cd, ftp->cd( cdDir + "/" + dirToCreate ) );
+ addCommand( QFtp::Close, ftp->close() );
+
+ inFileDirExistsFunction = true;
+ QTestEventLoop::instance().enterLoop( 30 );
+ delete ftp;
+ ftp = 0;
+ if ( QTestEventLoop::instance().timeout() ) {
+ // ### make this test work
+ // QFAIL( "Network operation timed out" );
+ qWarning("tst_QFtp::dirExists: Network operation timed out");
+ return false;
+ }
+ inFileDirExistsFunction = false;
+
+ ResMapIt it = resultMap.find( QFtp::Cd );
+ // ### make these tests work
+ // QVERIFY( it != resultMap.end() );
+ // QVERIFY( it.value().success != -1 );
+ return it.value().success == 1;
+}
+
+void tst_QFtp::doneSignal()
+{
+ QFtp ftp;
+ QSignalSpy spy(&ftp, SIGNAL(done(bool)));
+
+ ftp.connectToHost(QtNetworkSettings::serverName());
+ ftp.login("anonymous");
+ ftp.list();
+ ftp.close();
+
+ done_success = 0;
+ connect(&ftp, SIGNAL(done(bool)), &(QTestEventLoop::instance()), SLOT(exitLoop()));
+ QTestEventLoop::instance().enterLoop(61);
+ if (QTestEventLoop::instance().timeout())
+ QFAIL("Network operation timed out");
+
+ QTest::qWait(200);
+
+ QCOMPARE(spy.count(), 1);
+ QCOMPARE(spy.first().first().toBool(), false);
+}
+
+void tst_QFtp::queueMoreCommandsInDoneSlot()
+{
+ QSKIP("Task 127050 && 113966");
+
+ QFtp ftp;
+ QSignalSpy doneSpy(&ftp, SIGNAL(done(bool)));
+ QSignalSpy commandFinishedSpy(&ftp, SIGNAL(commandFinished(int, bool)));
+
+ this->ftp = &ftp;
+ connect(&ftp, SIGNAL(done(bool)), this, SLOT(cdUpSlot(bool)));
+
+ ftp.connectToHost("ftp.qt.nokia.com");
+ ftp.login();
+ ftp.cd("qt");
+ ftp.rmdir("qtest-removedir-noexist");
+
+ while ( ftp.hasPendingCommands() || ftp.currentCommand() != QFtp::None ) {
+ QCoreApplication::instance()->processEvents(QEventLoop::AllEvents
+ | QEventLoop::WaitForMoreEvents);
+ }
+
+ QCOMPARE(doneSpy.count(), 2);
+ QCOMPARE(doneSpy.first().first().toBool(), true);
+ QCOMPARE(doneSpy.last().first().toBool(), false);
+
+ QCOMPARE(commandFinishedSpy.count(), 6);
+ int firstId = commandFinishedSpy.at(0).at(0).toInt();
+ QCOMPARE(commandFinishedSpy.at(0).at(1).toBool(), false);
+ QCOMPARE(commandFinishedSpy.at(1).at(0).toInt(), firstId + 1);
+ QCOMPARE(commandFinishedSpy.at(1).at(1).toBool(), false);
+ QCOMPARE(commandFinishedSpy.at(2).at(0).toInt(), firstId + 2);
+ QCOMPARE(commandFinishedSpy.at(2).at(1).toBool(), false);
+ QCOMPARE(commandFinishedSpy.at(3).at(0).toInt(), firstId + 3);
+ QCOMPARE(commandFinishedSpy.at(3).at(1).toBool(), true);
+ QCOMPARE(commandFinishedSpy.at(4).at(0).toInt(), firstId + 4);
+ QCOMPARE(commandFinishedSpy.at(4).at(1).toBool(), false);
+ QCOMPARE(commandFinishedSpy.at(5).at(0).toInt(), firstId + 5);
+ QCOMPARE(commandFinishedSpy.at(5).at(1).toBool(), false);
+
+ this->ftp = 0;
+}
+
+void tst_QFtp::cdUpSlot(bool error)
+{
+ if (error) {
+ ftp->cd("..");
+ ftp->cd("qt");
+ }
+}
+
+void tst_QFtp::qtbug7359Crash()
+{
+ QFtp ftp;
+ ftp.connectToHost("127.0.0.1");
+
+ QTime t;
+ int elapsed;
+
+ t.start();
+ while ((elapsed = t.elapsed()) < 200)
+ QCoreApplication::processEvents(QEventLoop::AllEvents, 200 - elapsed);
+
+ ftp.close();
+ t.restart();
+ while ((elapsed = t.elapsed()) < 1000)
+ QCoreApplication::processEvents(QEventLoop::AllEvents, 1000 - elapsed);
+
+ ftp.connectToHost("127.0.0.1");
+
+ t.restart();
+ while ((elapsed = t.elapsed()) < 2000)
+ QCoreApplication::processEvents(QEventLoop::AllEvents, 2000 - elapsed);
+}
+
+QTEST_MAIN(tst_QFtp)
+
+#include "tst_qftp.moc"