1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include <utils/ssh/sftpchannel.h>
35 #include <utils/ssh/sshconnection.h>
36 #include <utils/ssh/sshremoteprocess.h>
38 #include <QtCore/QCoreApplication>
39 #include <QtCore/QList>
40 #include <QtCore/QObject>
41 #include <QtCore/QPair>
42 #include <QtCore/QTimer>
44 using namespace Utils;
46 class Test : public QObject {
51 m_timeoutTimer.setSingleShot(true);
52 m_connection = SshConnection::create();
53 if (m_connection->state() != SshConnection::Unconnected) {
54 qDebug("Error: Newly created SSH connection has state %d.",
55 m_connection->state());
58 if (m_connection->createRemoteProcess(""))
59 qDebug("Error: Unconnected SSH connection creates remote process.");
60 if (m_connection->createSftpChannel())
61 qDebug("Error: Unconnected SSH connection creates SFTP channel.");
63 SshConnectionParameters noHost=SshConnectionParameters(SshConnectionParameters::DefaultProxy);
64 noHost.host = QLatin1String("hgdfxgfhgxfhxgfchxgcf");
68 SshConnectionParameters noUser=SshConnectionParameters(SshConnectionParameters::DefaultProxy);
69 noUser.host = QLatin1String("localhost");
72 noUser.authenticationType = SshConnectionParameters::AuthenticationByPassword;
73 noUser.userName = QLatin1String("dumdidumpuffpuff");
74 noUser.password = QLatin1String("whatever");
76 SshConnectionParameters wrongPwd=SshConnectionParameters(SshConnectionParameters::DefaultProxy);
77 wrongPwd.host = QLatin1String("localhost");
79 wrongPwd.timeout = 30;
80 wrongPwd.authenticationType = SshConnectionParameters::AuthenticationByPassword;
81 wrongPwd.userName = QLatin1String("root");
82 noUser.password = QLatin1String("thiscantpossiblybeapasswordcanit");
84 SshConnectionParameters invalidKeyFile=SshConnectionParameters(SshConnectionParameters::DefaultProxy);
85 invalidKeyFile.host = QLatin1String("localhost");
86 invalidKeyFile.port = 22;
87 invalidKeyFile.timeout = 30;
88 invalidKeyFile.authenticationType = SshConnectionParameters::AuthenticationByKey;
89 invalidKeyFile.userName = QLatin1String("root");
90 invalidKeyFile.privateKeyFile
91 = QLatin1String("somefilenamethatwedontexpecttocontainavalidkey");
93 // TODO: Create a valid key file and check for authentication error.
95 m_testSet << TestItem("Behavior with non-existing host",
96 noHost, ErrorList() << SshSocketError);
97 m_testSet << TestItem("Behavior with non-existing user", noUser,
98 ErrorList() << SshSocketError << SshTimeoutError
99 << SshAuthenticationError);
100 m_testSet << TestItem("Behavior with wrong password", wrongPwd,
101 ErrorList() << SshSocketError << SshTimeoutError
102 << SshAuthenticationError);
103 m_testSet << TestItem("Behavior with invalid key file", invalidKeyFile,
104 ErrorList() << SshSocketError << SshTimeoutError
113 void handleConnected()
115 qDebug("Error: Received unexpected connected() signal.");
119 void handleDisconnected()
121 qDebug("Error: Received unexpected disconnected() signal.");
125 void handleDataAvailable(const QString &msg)
127 qDebug("Error: Received unexpected dataAvailable() signal. "
128 "Message was: '%s'.", qPrintable(msg));
132 void handleError(Utils::SshError error)
134 if (m_testSet.isEmpty()) {
135 qDebug("Error: Received error %d, but no test was running.", error);
139 const TestItem testItem = m_testSet.takeFirst();
140 if (testItem.allowedErrors.contains(error)) {
141 qDebug("Received error %d, as expected.", error);
142 if (m_testSet.isEmpty()) {
143 qDebug("All tests finished successfully.");
149 qDebug("Received unexpected error %d.", error);
156 if (m_testSet.isEmpty()) {
157 qDebug("Error: timeout, but no test was running.");
160 const TestItem testItem = m_testSet.takeFirst();
161 qDebug("Error: The following test timed out: %s", testItem.description);
168 disconnect(m_connection.data(), 0, this, 0);
169 m_connection = SshConnection::create();
170 connect(m_connection.data(), SIGNAL(connected()), this,
171 SLOT(handleConnected()));
172 connect(m_connection.data(), SIGNAL(disconnected()), this,
173 SLOT(handleDisconnected()));
174 connect(m_connection.data(), SIGNAL(dataAvailable(QString)), this,
175 SLOT(handleDataAvailable(QString)));
176 connect(m_connection.data(), SIGNAL(error(Utils::SshError)), this,
177 SLOT(handleError(Utils::SshError)));
178 const TestItem &nextItem = m_testSet.first();
179 m_timeoutTimer.stop();
180 m_timeoutTimer.setInterval(qMax(10000, nextItem.params.timeout * 1000));
181 qDebug("Testing: %s", nextItem.description);
182 m_connection->connectToHost(m_testSet.first().params);
185 SshConnection::Ptr m_connection;
186 typedef QList<SshError> ErrorList;
188 TestItem(const char *d, const SshConnectionParameters &p,
189 const ErrorList &e) : description(d), params(p), allowedErrors(e) {}
191 const char *description;
192 SshConnectionParameters params;
193 ErrorList allowedErrors;
195 QList<TestItem> m_testSet;
196 QTimer m_timeoutTimer;
201 int main(int argc, char *argv[])
203 QCoreApplication a(argc, argv);