OSDN Git Service

3900ab43c24c97a7534ba470611dd47282346fc7
[qt-creator-jp/qt-creator-jp.git] / tests / manual / ssh / errorhandling / main.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include <utils/ssh/sftpchannel.h>
35 #include <utils/ssh/sshconnection.h>
36 #include <utils/ssh/sshremoteprocess.h>
37
38 #include <QtCore/QCoreApplication>
39 #include <QtCore/QList>
40 #include <QtCore/QObject>
41 #include <QtCore/QPair>
42 #include <QtCore/QTimer>
43
44 using namespace Utils;
45
46 class Test : public QObject {
47     Q_OBJECT
48 public:
49     Test()
50     {
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());
56         }
57
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.");
62
63         SshConnectionParameters noHost=SshConnectionParameters(SshConnectionParameters::DefaultProxy);
64         noHost.host = QLatin1String("hgdfxgfhgxfhxgfchxgcf");
65         noHost.port = 12345;
66         noHost.timeout = 10;
67
68         SshConnectionParameters noUser=SshConnectionParameters(SshConnectionParameters::DefaultProxy);
69         noUser.host = QLatin1String("localhost");
70         noUser.port = 22;
71         noUser.timeout = 30;
72         noUser.authenticationType = SshConnectionParameters::AuthenticationByPassword;
73         noUser.userName = QLatin1String("dumdidumpuffpuff");
74         noUser.password = QLatin1String("whatever");
75
76         SshConnectionParameters wrongPwd=SshConnectionParameters(SshConnectionParameters::DefaultProxy);
77         wrongPwd.host = QLatin1String("localhost");
78         wrongPwd.port = 22;
79         wrongPwd.timeout = 30;
80         wrongPwd.authenticationType = SshConnectionParameters::AuthenticationByPassword;
81         wrongPwd.userName = QLatin1String("root");
82         noUser.password = QLatin1String("thiscantpossiblybeapasswordcanit");
83
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");
92
93         // TODO: Create a valid key file and check for authentication error.
94
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
105                 << SshKeyFileError);
106
107         runNextTest();
108     }
109
110     ~Test();
111
112 private slots:
113     void handleConnected()
114     {
115         qDebug("Error: Received unexpected connected() signal.");
116         qApp->quit();
117     }
118
119     void handleDisconnected()
120     {
121         qDebug("Error: Received unexpected disconnected() signal.");
122         qApp->quit();
123     }
124
125     void handleDataAvailable(const QString &msg)
126     {
127         qDebug("Error: Received unexpected dataAvailable() signal. "
128             "Message was: '%s'.", qPrintable(msg));
129         qApp->quit();
130     }
131
132     void handleError(Utils::SshError error)
133     {
134         if (m_testSet.isEmpty()) {
135             qDebug("Error: Received error %d, but no test was running.", error);
136             qApp->quit();
137         }
138
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.");
144                 qApp->quit();
145             } else {
146                 runNextTest();
147             }
148         } else {
149             qDebug("Received unexpected error %d.", error);
150             qApp->quit();
151         }
152     }
153
154     void handleTimeout()
155     {
156         if (m_testSet.isEmpty()) {
157             qDebug("Error: timeout, but no test was running.");
158             qApp->quit();
159         }
160         const TestItem testItem = m_testSet.takeFirst();
161         qDebug("Error: The following test timed out: %s", testItem.description);
162     }
163
164 private:
165     void runNextTest()
166     {
167         if (m_connection)
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);
183     }
184
185     SshConnection::Ptr m_connection;
186     typedef QList<SshError> ErrorList;
187     struct TestItem {
188         TestItem(const char *d, const SshConnectionParameters &p,
189             const ErrorList &e) : description(d), params(p), allowedErrors(e) {}
190
191         const char *description;
192         SshConnectionParameters params;
193         ErrorList allowedErrors;
194     };
195     QList<TestItem> m_testSet;
196     QTimer m_timeoutTimer;
197 };
198
199 Test::~Test() {}
200
201 int main(int argc, char *argv[])
202 {
203     QCoreApplication a(argc, argv);
204     Test t;
205
206     return a.exec();
207 }
208
209
210 #include "main.moc"