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 "remoteprocesstest.h"
36 #include <QtCore/QCoreApplication>
37 #include <QtCore/QTimer>
41 using namespace Utils;
43 RemoteProcessTest::RemoteProcessTest(const SshConnectionParameters ¶ms)
44 : m_timeoutTimer(new QTimer(this)),
45 m_remoteRunner(SshRemoteProcessRunner::create(params)),
48 m_timeoutTimer->setInterval(5000);
49 connect(m_timeoutTimer, SIGNAL(timeout()), SLOT(handleTimeout()));
52 RemoteProcessTest::~RemoteProcessTest() { }
54 void RemoteProcessTest::run()
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)));
68 std::cout << "Testing successful remote process..." << std::endl;
69 m_state = TestingSuccess;
71 m_timeoutTimer->start();
72 m_remoteRunner->run("ls -a /tmp");
75 void RemoteProcessTest::handleConnectionError()
77 std::cerr << "Error: Connection failure ("
78 << qPrintable(m_remoteRunner->connection()->errorString()) << ")."
83 void RemoteProcessTest::handleProcessStarted()
86 std::cerr << "Error: Received started() signal again." << std::endl;
90 if (m_state == TestingCrash) {
91 Utils::SshRemoteProcess::Ptr killer
92 = m_remoteRunner->connection()->createRemoteProcess("pkill -9 sleep");
98 void RemoteProcessTest::handleProcessStdout(const QByteArray &output)
101 std::cerr << "Error: Remote output from non-started process."
104 } else if (m_state != TestingSuccess) {
105 std::cerr << "Error: Got remote standard output in state " << m_state
109 m_remoteStdout += output;
113 void RemoteProcessTest::handleProcessStderr(const QByteArray &output)
116 std::cerr << "Error: Remote error output from non-started process."
119 } else if (m_state == TestingSuccess) {
120 std::cerr << "Error: Unexpected remote standard error output."
124 m_remoteStderr += output;
128 void RemoteProcessTest::handleProcessClosed(int exitStatus)
130 switch (exitStatus) {
131 case SshRemoteProcess::ExitedNormally:
133 std::cerr << "Error: Process exited without starting." << std::endl;
138 case TestingSuccess: {
139 const int exitCode = m_remoteRunner->process()->exitCode();
141 std::cerr << "Error: exit code is " << exitCode
142 << ", expected zero." << std::endl;
146 if (m_remoteStdout.isEmpty()) {
147 std::cerr << "Error: Command did not produce output."
153 std::cout << "Ok. Testing unsuccessful remote process..."
155 m_state = TestingFailure;
157 m_timeoutTimer->start();
158 m_remoteRunner->run("ls /wedontexepectsuchafiletoexist");
161 case TestingFailure: {
162 const int exitCode = m_remoteRunner->process()->exitCode();
164 std::cerr << "Error: exit code is zero, expected non-zero."
169 if (m_remoteStderr.isEmpty()) {
170 std::cerr << "Error: Command did not produce error output."
176 std::cout << "Ok. Testing crashing remote process..."
178 m_state = TestingCrash;
180 m_timeoutTimer->start();
181 m_remoteRunner->run("sleep 100");
185 std::cerr << "Error: Successful exit from process that was "
186 "supposed to crash." << std::endl;
193 case SshRemoteProcess::FailedToStart:
195 std::cerr << "Error: Got 'failed to start' signal for process "
196 "that has not started yet." << std::endl;
198 std::cerr << "Error: Process failed to start." << std::endl;
202 case SshRemoteProcess::KilledBySignal:
203 if (m_state != TestingCrash) {
204 std::cerr << "Error: Unexpected crash." << std::endl;
208 std::cout << "Ok. All tests succeeded." << std::endl;
213 void RemoteProcessTest::handleTimeout()
215 std::cerr << "Error: Timeout waiting for progress." << std::endl;