OSDN Git Service

303abed4d090a0960d38377e3f88020a55422088
[qt-creator-jp/qt-creator-jp.git] / tests / manual / ssh / remoteprocess / remoteprocesstest.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 "remoteprocesstest.h"
35
36 #include <QtCore/QCoreApplication>
37 #include <QtCore/QTimer>
38
39 #include <iostream>
40
41 using namespace Utils;
42
43 RemoteProcessTest::RemoteProcessTest(const SshConnectionParameters &params)
44     : m_timeoutTimer(new QTimer(this)),
45       m_remoteRunner(SshRemoteProcessRunner::create(params)),
46       m_state(Inactive)
47 {
48     m_timeoutTimer->setInterval(5000);
49     connect(m_timeoutTimer, SIGNAL(timeout()), SLOT(handleTimeout()));
50 }
51
52 RemoteProcessTest::~RemoteProcessTest() { }
53
54 void RemoteProcessTest::run()
55 {
56     connect(m_remoteRunner.data(), SIGNAL(connectionError(Utils::SshError)),
57         SLOT(handleConnectionError()));
58     connect(m_remoteRunner.data(), SIGNAL(processStarted()),
59         SLOT(handleProcessStarted()));
60     connect(m_remoteRunner.data(), SIGNAL(processOutputAvailable(QByteArray)),
61         SLOT(handleProcessStdout(QByteArray)));
62     connect(m_remoteRunner.data(),
63         SIGNAL(processErrorOutputAvailable(QByteArray)),
64         SLOT(handleProcessStderr(QByteArray)));
65     connect(m_remoteRunner.data(), SIGNAL(processClosed(int)),
66         SLOT(handleProcessClosed(int)));
67
68     std::cout << "Testing successful remote process..." << std::endl;
69     m_state = TestingSuccess;
70     m_started = false;
71     m_timeoutTimer->start();
72     m_remoteRunner->run("ls -a /tmp");
73 }
74
75 void RemoteProcessTest::handleConnectionError()
76 {
77     std::cerr << "Error: Connection failure ("
78         << qPrintable(m_remoteRunner->connection()->errorString()) << ")."
79         << std::endl;
80     qApp->quit();
81 }
82
83 void RemoteProcessTest::handleProcessStarted()
84 {
85     if (m_started) {
86         std::cerr << "Error: Received started() signal again." << std::endl;
87         qApp->quit();
88     } else {
89         m_started = true;
90         if (m_state == TestingCrash) {
91             Utils::SshRemoteProcess::Ptr killer
92                 = m_remoteRunner->connection()->createRemoteProcess("pkill -9 sleep");
93             killer->start();
94         }
95     }
96 }
97
98 void RemoteProcessTest::handleProcessStdout(const QByteArray &output)
99 {
100     if (!m_started) {
101         std::cerr << "Error: Remote output from non-started process."
102             << std::endl;
103         qApp->quit();
104     } else if (m_state != TestingSuccess) {
105         std::cerr << "Error: Got remote standard output in state " << m_state
106             << "." << std::endl;
107         qApp->quit();
108     } else {
109         m_remoteStdout += output;
110     }
111 }
112
113 void RemoteProcessTest::handleProcessStderr(const QByteArray &output)
114 {
115     if (!m_started) {
116         std::cerr << "Error: Remote error output from non-started process."
117             << std::endl;
118         qApp->quit();
119     } else if (m_state == TestingSuccess) {
120         std::cerr << "Error: Unexpected remote standard error output."
121             << std::endl;
122         qApp->quit();
123     } else {
124         m_remoteStderr += output;
125     }
126 }
127
128 void RemoteProcessTest::handleProcessClosed(int exitStatus)
129 {
130     switch (exitStatus) {
131     case SshRemoteProcess::ExitedNormally:
132         if (!m_started) {
133             std::cerr << "Error: Process exited without starting." << std::endl;
134             qApp->quit();
135             return;
136         }
137         switch (m_state) {
138         case TestingSuccess: {
139             const int exitCode = m_remoteRunner->process()->exitCode();
140             if (exitCode != 0) {
141                 std::cerr << "Error: exit code is " << exitCode
142                     << ", expected zero." << std::endl;
143                 qApp->quit();
144                 return;
145             }
146             if (m_remoteStdout.isEmpty()) {
147                 std::cerr << "Error: Command did not produce output."
148                     << std::endl;
149                 qApp->quit();
150                 return;
151             }
152
153             std::cout << "Ok. Testing unsuccessful remote process..."
154                 << std::endl;
155             m_state = TestingFailure;
156             m_started = false;
157             m_timeoutTimer->start();
158             m_remoteRunner->run("ls /wedontexepectsuchafiletoexist");
159             break;
160         }
161         case TestingFailure: {
162             const int exitCode = m_remoteRunner->process()->exitCode();
163             if (exitCode == 0) {
164                 std::cerr << "Error: exit code is zero, expected non-zero."
165                     << std::endl;
166                 qApp->quit();
167                 return;
168             }
169             if (m_remoteStderr.isEmpty()) {
170                 std::cerr << "Error: Command did not produce error output."
171                     << std::endl;
172                 qApp->quit();
173                 return;
174             }
175
176             std::cout << "Ok. Testing crashing remote process..."
177                 << std::endl;
178             m_state = TestingCrash;
179             m_started = false;
180             m_timeoutTimer->start();
181             m_remoteRunner->run("sleep 100");
182             break;
183         }
184         case TestingCrash:
185             std::cerr << "Error: Successful exit from process that was "
186                 "supposed to crash." << std::endl;
187             qApp->quit();
188             return;
189         case Inactive:
190             Q_ASSERT(false);
191         }
192         break;
193     case SshRemoteProcess::FailedToStart:
194         if (m_started) {
195             std::cerr << "Error: Got 'failed to start' signal for process "
196                 "that has not started yet." << std::endl;
197         } else {
198             std::cerr << "Error: Process failed to start." << std::endl;
199         }
200         qApp->quit();
201         break;
202     case SshRemoteProcess::KilledBySignal:
203         if (m_state != TestingCrash) {
204             std::cerr << "Error: Unexpected crash." << std::endl;
205             qApp->quit();
206             return;
207         }
208         std::cout << "Ok. All tests succeeded." << std::endl;
209         qApp->quit();
210     }
211 }
212
213 void RemoteProcessTest::handleTimeout()
214 {
215     std::cerr << "Error: Timeout waiting for progress." << std::endl;
216     qApp->quit();
217 }